BatchedMesh.js 20 KB

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