BatchedMesh.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. import { BufferAttribute } from '../core/BufferAttribute.js';
  2. import { BufferGeometry } from '../core/BufferGeometry.js';
  3. import { DataTexture } from '../textures/DataTexture.js';
  4. import { FloatType } from '../constants.js';
  5. import { MathUtils } from '../math/MathUtils.js';
  6. import { Matrix4 } from '../math/Matrix4.js';
  7. import { Mesh } from './Mesh.js';
  8. import { RGBAFormat } from '../constants.js';
  9. import { Box3 } from '../math/Box3.js';
  10. import { Sphere } from '../math/Sphere.js';
  11. import { Frustum } from '../math/Frustum.js';
  12. import { WebGLCoordinateSystem } from '../constants.js';
  13. import { WebGPUCoordinateSystem } from '../constants.js';
  14. import { Vector3 } from '../math/Vector3.js';
  15. function sortOpaque( a, b ) {
  16. return a.z - b.z;
  17. }
  18. function sortTransparent( a, b ) {
  19. return b.z - a.z;
  20. }
  21. class MultiDrawRenderList {
  22. constructor() {
  23. this.index = 0;
  24. this.pool = [];
  25. this.list = [];
  26. }
  27. push( drawRange, z ) {
  28. const pool = this.pool;
  29. const list = this.list;
  30. if ( this.index >= pool.length ) {
  31. pool.push( {
  32. start: - 1,
  33. count: - 1,
  34. z: - 1,
  35. } );
  36. }
  37. const item = pool[ this.index ];
  38. list.push( item );
  39. this.index ++;
  40. item.start = drawRange.start;
  41. item.count = drawRange.count;
  42. item.z = z;
  43. }
  44. reset() {
  45. this.list.length = 0;
  46. this.index = 0;
  47. }
  48. }
  49. const ID_ATTR_NAME = 'batchId';
  50. const _matrix = new Matrix4();
  51. const _identityMatrix = new Matrix4();
  52. const _projScreenMatrix = new Matrix4();
  53. const _frustum = new Frustum();
  54. const _box = new Box3();
  55. const _sphere = new Sphere();
  56. const _vector = new Vector3();
  57. const _renderList = new MultiDrawRenderList();
  58. const _mesh = new Mesh();
  59. const _batchIntersects = [];
  60. // @TODO: SkinnedMesh support?
  61. // @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.
  62. // @TODO: geometry.groups support?
  63. // @TODO: geometry.drawRange support?
  64. // @TODO: geometry.morphAttributes support?
  65. // @TODO: Support uniform parameter per geometry
  66. // @TODO: Add an "optimize" function to pack geometry and remove data gaps
  67. // copies data from attribute "src" into "target" starting at "targetOffset"
  68. function copyAttributeData( src, target, targetOffset = 0 ) {
  69. const itemSize = target.itemSize;
  70. if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) {
  71. // use the component getters and setters if the array data cannot
  72. // be copied directly
  73. const vertexCount = src.count;
  74. for ( let i = 0; i < vertexCount; i ++ ) {
  75. for ( let c = 0; c < itemSize; c ++ ) {
  76. target.setComponent( i + targetOffset, c, src.getComponent( i, c ) );
  77. }
  78. }
  79. } else {
  80. // faster copy approach using typed array set function
  81. target.array.set( src.array, targetOffset * itemSize );
  82. }
  83. target.needsUpdate = true;
  84. }
  85. class BatchedMesh extends Mesh {
  86. constructor( maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) {
  87. super( new BufferGeometry(), material );
  88. this.isBatchedMesh = true;
  89. this.perObjectFrustumCulled = true;
  90. this.sortObjects = true;
  91. this.boundingBox = null;
  92. this.boundingSphere = null;
  93. this._drawRanges = [];
  94. this._reservedRanges = [];
  95. this._visible = [];
  96. this._active = [];
  97. this._bounds = [];
  98. this._maxGeometryCount = maxGeometryCount;
  99. this._maxVertexCount = maxVertexCount;
  100. this._maxIndexCount = maxIndexCount;
  101. this._geometryInitialized = false;
  102. this._geometryCount = 0;
  103. this._multiDrawCounts = new Int32Array( maxGeometryCount );
  104. this._multiDrawStarts = new Int32Array( maxGeometryCount );
  105. this._multiDrawCount = 0;
  106. // Local matrix per geometry by using data texture
  107. this._matricesTexture = null;
  108. this._initMatricesTexture();
  109. }
  110. _initMatricesTexture() {
  111. // layout (1 matrix = 4 pixels)
  112. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  113. // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)
  114. // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)
  115. // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)
  116. // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)
  117. let size = Math.sqrt( this._maxGeometryCount * 4 ); // 4 pixels needed for 1 matrix
  118. size = MathUtils.ceilPowerOfTwo( size );
  119. size = Math.max( size, 4 );
  120. const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  121. const matricesTexture = new DataTexture( matricesArray, size, size, RGBAFormat, FloatType );
  122. this._matricesTexture = matricesTexture;
  123. }
  124. _initializeGeometry( reference ) {
  125. const geometry = this.geometry;
  126. const maxVertexCount = this._maxVertexCount;
  127. const maxGeometryCount = this._maxGeometryCount;
  128. const maxIndexCount = this._maxIndexCount;
  129. if ( this._geometryInitialized === false ) {
  130. for ( const attributeName in reference.attributes ) {
  131. const srcAttribute = reference.getAttribute( attributeName );
  132. const { array, itemSize, normalized } = srcAttribute;
  133. const dstArray = new array.constructor( maxVertexCount * itemSize );
  134. const dstAttribute = new srcAttribute.constructor( dstArray, itemSize, normalized );
  135. dstAttribute.setUsage( srcAttribute.usage );
  136. geometry.setAttribute( attributeName, dstAttribute );
  137. }
  138. if ( reference.getIndex() !== null ) {
  139. const indexArray = maxVertexCount > 65536
  140. ? new Uint32Array( maxIndexCount )
  141. : new Uint16Array( maxIndexCount );
  142. geometry.setIndex( new BufferAttribute( indexArray, 1 ) );
  143. }
  144. const idArray = maxGeometryCount > 65536
  145. ? new Uint32Array( maxVertexCount )
  146. : new Uint16Array( maxVertexCount );
  147. geometry.setAttribute( ID_ATTR_NAME, new BufferAttribute( idArray, 1 ) );
  148. this._geometryInitialized = true;
  149. }
  150. }
  151. // Make sure the geometry is compatible with the existing combined geometry atributes
  152. _validateGeometry( geometry ) {
  153. // check that the geometry doesn't have a version of our reserved id attribute
  154. if ( geometry.getAttribute( ID_ATTR_NAME ) ) {
  155. throw new Error( `BatchedMesh: Geometry cannot use attribute "${ ID_ATTR_NAME }"` );
  156. }
  157. // check to ensure the geometries are using consistent attributes and indices
  158. const batchGeometry = this.geometry;
  159. if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {
  160. throw new Error( 'BatchedMesh: All geometries must consistently have "index".' );
  161. }
  162. for ( const attributeName in batchGeometry.attributes ) {
  163. if ( attributeName === ID_ATTR_NAME ) {
  164. continue;
  165. }
  166. if ( ! geometry.hasAttribute( attributeName ) ) {
  167. throw new Error( `BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );
  168. }
  169. const srcAttribute = geometry.getAttribute( attributeName );
  170. const dstAttribute = batchGeometry.getAttribute( attributeName );
  171. if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) {
  172. throw new Error( 'BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );
  173. }
  174. }
  175. }
  176. getGeometryCount() {
  177. return this._geometryCount;
  178. }
  179. getVertexCount() {
  180. const reservedRanges = this._reservedRanges;
  181. if ( reservedRanges.length === 0 ) {
  182. return 0;
  183. } else {
  184. const finalRange = reservedRanges[ reservedRanges.length - 1 ];
  185. return finalRange.vertexStart + finalRange.vertexCount;
  186. }
  187. }
  188. getIndexCount() {
  189. const reservedRanges = this._reservedRanges;
  190. const geometry = this.geometry;
  191. if ( geometry.getIndex() === null || reservedRanges.length === 0 ) {
  192. return 0;
  193. } else {
  194. const finalRange = reservedRanges[ reservedRanges.length - 1 ];
  195. return finalRange.indexStart + finalRange.indexCount;
  196. }
  197. }
  198. computeBoundingBox() {
  199. if ( this.boundingBox === null ) {
  200. this.boundingBox = new Box3();
  201. }
  202. const geometryCount = this._geometryCount;
  203. const boundingBox = this.boundingBox;
  204. const active = this._active;
  205. boundingBox.makeEmpty();
  206. for ( let i = 0; i < geometryCount; i ++ ) {
  207. if ( active[ i ] === false ) continue;
  208. this.getMatrixAt( i, _matrix );
  209. this.getBoundingBoxAt( i, _box ).applyMatrix4( _matrix );
  210. boundingBox.union( _box );
  211. }
  212. }
  213. computeBoundingSphere() {
  214. if ( this.boundingSphere === null ) {
  215. this.boundingSphere = new Sphere();
  216. }
  217. const geometryCount = this._geometryCount;
  218. const boundingSphere = this.boundingSphere;
  219. const active = this._active;
  220. boundingSphere.makeEmpty();
  221. for ( let i = 0; i < geometryCount; i ++ ) {
  222. if ( active[ i ] === false ) continue;
  223. this.getMatrixAt( i, _matrix );
  224. this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
  225. boundingSphere.union( _sphere );
  226. }
  227. }
  228. addGeometry( geometry, vertexCount = - 1, indexCount = - 1 ) {
  229. this._initializeGeometry( geometry );
  230. this._validateGeometry( geometry );
  231. // ensure we're not over geometry
  232. if ( this._geometryCount >= this._maxGeometryCount ) {
  233. throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
  234. }
  235. // get the necessary range fo the geometry
  236. const reservedRange = {
  237. vertexStart: - 1,
  238. vertexCount: - 1,
  239. indexStart: - 1,
  240. indexCount: - 1,
  241. };
  242. let lastRange = null;
  243. const reservedRanges = this._reservedRanges;
  244. const drawRanges = this._drawRanges;
  245. const bounds = this._bounds;
  246. if ( this._geometryCount !== 0 ) {
  247. lastRange = reservedRanges[ reservedRanges.length - 1 ];
  248. }
  249. if ( vertexCount === - 1 ) {
  250. reservedRange.vertexCount = geometry.getAttribute( 'position' ).count;
  251. } else {
  252. reservedRange.vertexCount = vertexCount;
  253. }
  254. if ( lastRange === null ) {
  255. reservedRange.vertexStart = 0;
  256. } else {
  257. reservedRange.vertexStart = lastRange.vertexStart + lastRange.vertexCount;
  258. }
  259. const index = geometry.getIndex();
  260. const hasIndex = index !== null;
  261. if ( hasIndex ) {
  262. if ( indexCount === - 1 ) {
  263. reservedRange.indexCount = index.count;
  264. } else {
  265. reservedRange.indexCount = indexCount;
  266. }
  267. if ( lastRange === null ) {
  268. reservedRange.indexStart = 0;
  269. } else {
  270. reservedRange.indexStart = lastRange.indexStart + lastRange.indexCount;
  271. }
  272. }
  273. if (
  274. reservedRange.indexStart !== - 1 &&
  275. reservedRange.indexStart + reservedRange.indexCount > this._maxIndexCount ||
  276. reservedRange.vertexStart + reservedRange.vertexCount > this._maxVertexCount
  277. ) {
  278. throw new Error( 'BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
  279. }
  280. const visible = this._visible;
  281. const active = this._active;
  282. const matricesTexture = this._matricesTexture;
  283. const matricesArray = this._matricesTexture.image.data;
  284. // push new visibility states
  285. visible.push( true );
  286. active.push( true );
  287. // update id
  288. const geometryId = this._geometryCount;
  289. this._geometryCount ++;
  290. // initialize matrix information
  291. _identityMatrix.toArray( matricesArray, geometryId * 16 );
  292. matricesTexture.needsUpdate = true;
  293. // add the reserved range and draw range objects
  294. reservedRanges.push( reservedRange );
  295. drawRanges.push( {
  296. start: hasIndex ? reservedRange.indexStart : reservedRange.vertexStart,
  297. count: - 1
  298. } );
  299. bounds.push( {
  300. boxInitialized: false,
  301. box: new Box3(),
  302. sphereInitialized: false,
  303. sphere: new Sphere()
  304. } );
  305. // set the id for the geometry
  306. const idAttribute = this.geometry.getAttribute( ID_ATTR_NAME );
  307. for ( let i = 0; i < reservedRange.vertexCount; i ++ ) {
  308. idAttribute.setX( reservedRange.vertexStart + i, geometryId );
  309. }
  310. idAttribute.needsUpdate = true;
  311. // update the geometry
  312. this.setGeometryAt( geometryId, geometry );
  313. return geometryId;
  314. }
  315. setGeometryAt( id, geometry ) {
  316. if ( id >= this._geometryCount ) {
  317. throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
  318. }
  319. this._validateGeometry( geometry );
  320. const batchGeometry = this.geometry;
  321. const hasIndex = batchGeometry.getIndex() !== null;
  322. const dstIndex = batchGeometry.getIndex();
  323. const srcIndex = geometry.getIndex();
  324. const reservedRange = this._reservedRanges[ id ];
  325. if (
  326. hasIndex &&
  327. srcIndex.count > reservedRange.indexCount ||
  328. geometry.attributes.position.count > reservedRange.vertexCount
  329. ) {
  330. throw new Error( 'BatchedMesh: Reserved space not large enough for provided geometry.' );
  331. }
  332. // copy geometry over
  333. const vertexStart = reservedRange.vertexStart;
  334. const vertexCount = reservedRange.vertexCount;
  335. for ( const attributeName in batchGeometry.attributes ) {
  336. if ( attributeName === ID_ATTR_NAME ) {
  337. continue;
  338. }
  339. // copy attribute data
  340. const srcAttribute = geometry.getAttribute( attributeName );
  341. const dstAttribute = batchGeometry.getAttribute( attributeName );
  342. copyAttributeData( srcAttribute, dstAttribute, vertexStart );
  343. // fill the rest in with zeroes
  344. const itemSize = srcAttribute.itemSize;
  345. for ( let i = srcAttribute.count, l = vertexCount; i < l; i ++ ) {
  346. const index = vertexStart + i;
  347. for ( let c = 0; c < itemSize; c ++ ) {
  348. dstAttribute.setComponent( index, c, 0 );
  349. }
  350. }
  351. dstAttribute.needsUpdate = true;
  352. }
  353. // copy index
  354. if ( hasIndex ) {
  355. const indexStart = reservedRange.indexStart;
  356. // copy index data over
  357. for ( let i = 0; i < srcIndex.count; i ++ ) {
  358. dstIndex.setX( indexStart + i, vertexStart + srcIndex.getX( i ) );
  359. }
  360. // fill the rest in with zeroes
  361. for ( let i = srcIndex.count, l = reservedRange.indexCount; i < l; i ++ ) {
  362. dstIndex.setX( indexStart + i, vertexStart );
  363. }
  364. dstIndex.needsUpdate = true;
  365. }
  366. // store the bounding boxes
  367. const bound = this._bounds[ id ];
  368. if ( geometry.boundingBox !== null ) {
  369. bound.box.copy( geometry.boundingBox );
  370. bound.boxInitialized = true;
  371. } else {
  372. bound.boxInitialized = false;
  373. }
  374. if ( geometry.boundingSphere !== null ) {
  375. bound.sphere.copy( geometry.boundingSphere );
  376. bound.sphereInitialized = true;
  377. } else {
  378. bound.sphereInitialized = false;
  379. }
  380. // set drawRange count
  381. const drawRange = this._drawRanges[ id ];
  382. const posAttr = geometry.getAttribute( 'position' );
  383. drawRange.count = hasIndex ? srcIndex.count : posAttr.count;
  384. return id;
  385. }
  386. deleteGeometry( geometryId ) {
  387. // Note: User needs to call optimize() afterward to pack the data.
  388. const active = this._active;
  389. if ( geometryId >= active.length || active[ geometryId ] === false ) {
  390. return this;
  391. }
  392. active[ geometryId ] = false;
  393. return this;
  394. }
  395. // get bounding box and compute it if it doesn't exist
  396. getBoundingBoxAt( id, target ) {
  397. const active = this._active;
  398. if ( active[ id ] === false ) {
  399. return this;
  400. }
  401. // compute bounding box
  402. const bound = this._bounds[ id ];
  403. const box = bound.box;
  404. const geometry = this.geometry;
  405. if ( bound.boxInitialized === false ) {
  406. box.makeEmpty();
  407. const index = geometry.index;
  408. const position = geometry.attributes.position;
  409. const drawRange = this._drawRanges[ id ];
  410. for ( let i = drawRange.start, l = drawRange.start + drawRange.count; i < l; i ++ ) {
  411. let iv = i;
  412. if ( index ) {
  413. iv = index.getX( iv );
  414. }
  415. box.expandByPoint( _vector.fromBufferAttribute( position, iv ) );
  416. }
  417. bound.boxInitialized = true;
  418. }
  419. target.copy( box );
  420. return target;
  421. }
  422. // get bounding sphere and compute it if it doesn't exist
  423. getBoundingSphereAt( id, target ) {
  424. const active = this._active;
  425. if ( active[ id ] === false ) {
  426. return this;
  427. }
  428. // compute bounding sphere
  429. const bound = this._bounds[ id ];
  430. const sphere = bound.sphere;
  431. const geometry = this.geometry;
  432. if ( bound.sphereInitialized === false ) {
  433. sphere.makeEmpty();
  434. this.getBoundingBoxAt( id, _box );
  435. _box.getCenter( sphere.center );
  436. const index = geometry.index;
  437. const position = geometry.attributes.position;
  438. const drawRange = this._drawRanges[ id ];
  439. let maxRadiusSq = 0;
  440. for ( let i = drawRange.start, l = drawRange.start + drawRange.count; i < l; i ++ ) {
  441. let iv = i;
  442. if ( index ) {
  443. iv = index.getX( iv );
  444. }
  445. _vector.fromBufferAttribute( position, iv );
  446. maxRadiusSq = Math.max( maxRadiusSq, sphere.center.distanceToSquared( _vector ) );
  447. }
  448. sphere.radius = Math.sqrt( maxRadiusSq );
  449. bound.sphereInitialized = true;
  450. }
  451. target.copy( sphere );
  452. return target;
  453. }
  454. setMatrixAt( geometryId, matrix ) {
  455. // @TODO: Map geometryId to index of the arrays because
  456. // optimize() can make geometryId mismatch the index
  457. const active = this._active;
  458. const matricesTexture = this._matricesTexture;
  459. const matricesArray = this._matricesTexture.image.data;
  460. const geometryCount = this._geometryCount;
  461. if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
  462. return this;
  463. }
  464. matrix.toArray( matricesArray, geometryId * 16 );
  465. matricesTexture.needsUpdate = true;
  466. return this;
  467. }
  468. getMatrixAt( geometryId, matrix ) {
  469. const active = this._active;
  470. const matricesArray = this._matricesTexture.image.data;
  471. const geometryCount = this._geometryCount;
  472. if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
  473. return null;
  474. }
  475. return matrix.fromArray( matricesArray, geometryId * 16 );
  476. }
  477. setVisibleAt( geometryId, value ) {
  478. const visible = this._visible;
  479. const active = this._active;
  480. const geometryCount = this._geometryCount;
  481. // if the geometry is out of range, not active, or visibility state
  482. // does not change then return early
  483. if (
  484. geometryId >= geometryCount ||
  485. active[ geometryId ] === false ||
  486. visible[ geometryId ] === value
  487. ) {
  488. return this;
  489. }
  490. visible[ geometryId ] = value;
  491. return this;
  492. }
  493. getVisibleAt( geometryId ) {
  494. const visible = this._visible;
  495. const active = this._active;
  496. const geometryCount = this._geometryCount;
  497. // return early if the geometry is out of range or not active
  498. if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
  499. return false;
  500. }
  501. return visible[ geometryId ];
  502. }
  503. raycast( raycaster, intersects ) {
  504. const visible = this._visible;
  505. const active = this._active;
  506. const drawRanges = this._drawRanges;
  507. const geometryCount = this._geometryCount;
  508. const matrixWorld = this.matrixWorld;
  509. const batchGeometry = this.geometry;
  510. // iterate over each geometry
  511. _mesh.material = this.material;
  512. _mesh.geometry.index = batchGeometry.index;
  513. _mesh.geometry.attributes = batchGeometry.attributes;
  514. if ( _mesh.geometry.boundingBox === null ) {
  515. _mesh.geometry.boundingBox = new Box3();
  516. }
  517. if ( _mesh.geometry.boundingSphere === null ) {
  518. _mesh.geometry.boundingSphere = new Sphere();
  519. }
  520. for ( let i = 0; i < geometryCount; i ++ ) {
  521. if ( ! visible[ i ] || ! active[ i ] ) {
  522. continue;
  523. }
  524. const drawRange = drawRanges[ i ];
  525. _mesh.geometry.setDrawRange( drawRange.start, drawRange.count );
  526. // ge the intersects
  527. this.getMatrixAt( i, _mesh.matrixWorld ).premultiply( matrixWorld );
  528. this.getBoundingBoxAt( i, _mesh.geometry.boundingBox );
  529. this.getBoundingSphereAt( i, _mesh.geometry.boundingSphere );
  530. _mesh.raycast( raycaster, _batchIntersects );
  531. // add batch id to the intersects
  532. for ( let j = 0, l = _batchIntersects.length; j < l; j ++ ) {
  533. const intersect = _batchIntersects[ j ];
  534. intersect.object = this;
  535. intersect.batchId = i;
  536. intersects.push( intersect );
  537. }
  538. _batchIntersects.length = 0;
  539. }
  540. _mesh.material = null;
  541. _mesh.geometry.index = null;
  542. _mesh.geometry.attributes = {};
  543. _mesh.geometry.setDrawRange( 0, Infinity );
  544. }
  545. copy( source ) {
  546. super.copy( source );
  547. this.geometry = source.geometry.clone();
  548. this.perObjectFrustumCulled = source.perObjectFrustumCulled;
  549. this.sortObjects = source.sortObjects;
  550. this.boundingBox = source.boundingBox !== null ? source.boundingBox.clone() : null;
  551. this.boundingSphere = source.boundingSphere !== null ? source.boundingSphere.clone() : null;
  552. this._drawRanges = source._drawRanges.map( range => ( { ...range } ) );
  553. this._reservedRanges = source._reservedRanges.map( range => ( { ...range } ) );
  554. this._visible = source._visible.slice();
  555. this._active = source._active.slice();
  556. this._bounds = source._bounds.map( bound => ( {
  557. boxInitialized: bound.boxInitialized,
  558. box: bound.box.clone(),
  559. sphereInitialized: bound.sphereInitialized,
  560. sphere: bound.sphere.clone()
  561. } ) );
  562. this._maxGeometryCount = source._maxGeometryCount;
  563. this._maxVertexCount = source._maxVertexCount;
  564. this._maxIndexCount = source._maxIndexCount;
  565. this._geometryInitialized = source._geometryInitialized;
  566. this._geometryCount = source._geometryCount;
  567. this._multiDrawCounts = source._multiDrawCounts.slice();
  568. this._multiDrawStarts = source._multiDrawStarts.slice();
  569. this._matricesTexture = source._matricesTexture.clone();
  570. this._matricesTexture.image.data = this._matricesTexture.image.slice();
  571. return this;
  572. }
  573. dispose() {
  574. // Assuming the geometry is not shared with other meshes
  575. this.geometry.dispose();
  576. this._matricesTexture.dispose();
  577. this._matricesTexture = null;
  578. return this;
  579. }
  580. onBeforeRender( _renderer, _scene, camera, geometry, material/*, _group*/ ) {
  581. // the indexed version of the multi draw function requires specifying the start
  582. // offset in bytes.
  583. const index = geometry.getIndex();
  584. const bytesPerElement = index === null ? 1 : index.array.BYTES_PER_ELEMENT;
  585. const visible = this._visible;
  586. const multiDrawStarts = this._multiDrawStarts;
  587. const multiDrawCounts = this._multiDrawCounts;
  588. const drawRanges = this._drawRanges;
  589. const perObjectFrustumCulled = this.perObjectFrustumCulled;
  590. // prepare the frustum
  591. if ( perObjectFrustumCulled ) {
  592. _projScreenMatrix
  593. .multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse )
  594. .multiply( this.matrixWorld );
  595. _frustum.setFromProjectionMatrix(
  596. _projScreenMatrix,
  597. _renderer.isWebGPURenderer ? WebGPUCoordinateSystem : WebGLCoordinateSystem
  598. );
  599. }
  600. let count = 0;
  601. if ( this.sortObjects ) {
  602. // get the camera position
  603. _vector.setFromMatrixPosition( camera.matrixWorld );
  604. for ( let i = 0, l = visible.length; i < l; i ++ ) {
  605. if ( visible[ i ] ) {
  606. this.getMatrixAt( i, _matrix );
  607. this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
  608. // determine whether the batched geometry is within the frustum
  609. let culled = false;
  610. if ( perObjectFrustumCulled ) {
  611. // get the bounds in camera space
  612. this.getMatrixAt( i, _matrix );
  613. // get the bounds
  614. this.getBoundingBoxAt( i, _box ).applyMatrix4( _matrix );
  615. culled = ! _frustum.intersectsBox( _box ) || ! _frustum.intersectsSphere( _sphere );
  616. }
  617. if ( ! culled ) {
  618. // get the distance from camera used for sorting
  619. const z = _vector.distanceToSquared( _sphere.center );
  620. _renderList.push( drawRanges[ i ], z );
  621. }
  622. }
  623. }
  624. // Sort the draw ranges and prep for rendering
  625. const list = _renderList.list;
  626. list.sort( material.transparent ? sortTransparent : sortOpaque );
  627. for ( let i = 0, l = list.length; i < l; i ++ ) {
  628. const item = list[ i ];
  629. multiDrawStarts[ count ] = item.start * bytesPerElement;
  630. multiDrawCounts[ count ] = item.count;
  631. count ++;
  632. }
  633. _renderList.reset();
  634. } else {
  635. for ( let i = 0, l = visible.length; i < l; i ++ ) {
  636. if ( visible[ i ] ) {
  637. // determine whether the batched geometry is within the frustum
  638. let culled = false;
  639. if ( perObjectFrustumCulled ) {
  640. // get the bounds in camera space
  641. this.getMatrixAt( i, _matrix );
  642. // get the bounds
  643. this.getBoundingBoxAt( i, _box ).applyMatrix4( _matrix );
  644. this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
  645. culled = ! _frustum.intersectsBox( _box ) || ! _frustum.intersectsSphere( _sphere );
  646. }
  647. if ( ! culled ) {
  648. const range = drawRanges[ i ];
  649. multiDrawStarts[ count ] = range.start * bytesPerElement;
  650. multiDrawCounts[ count ] = range.count;
  651. count ++;
  652. }
  653. }
  654. }
  655. }
  656. this._multiDrawCount = count;
  657. // @TODO: Implement geometry sorting for transparent and opaque materials
  658. }
  659. }
  660. export { BatchedMesh };
粤ICP备19079148号