BatchedMesh.js 21 KB

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