BatchedMesh.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. DataTexture,
  5. FloatType,
  6. MathUtils,
  7. Matrix4,
  8. Mesh,
  9. RGBAFormat
  10. } from 'three';
  11. const ID_ATTR_NAME = 'batchId';
  12. const _identityMatrix = new Matrix4();
  13. const _zeroScaleMatrix = new Matrix4().set(
  14. 0, 0, 0, 0,
  15. 0, 0, 0, 0,
  16. 0, 0, 0, 0,
  17. 0, 0, 0, 1,
  18. );
  19. // @TODO: SkinnedMesh support?
  20. // @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.
  21. // copies data from attribute "src" into "target" starting at "targetOffset"
  22. function copyAttributeData( src, target, targetOffset = 0 ) {
  23. const itemSize = target.itemSize;
  24. if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) {
  25. // use the component getters and setters if the array data cannot
  26. // be copied directly
  27. const vertexCount = src.count;
  28. for ( let i = 0; i < vertexCount; i ++ ) {
  29. for ( let c = 0; c < itemSize; c ++ ) {
  30. target.setComponent( i + targetOffset, c, src.getComponent( i, c ) );
  31. }
  32. }
  33. } else {
  34. // faster copy approach using typed array set function
  35. target.array.set( src.array, targetOffset * itemSize );
  36. }
  37. target.needsUpdate = true;
  38. }
  39. class BatchedMesh extends Mesh {
  40. constructor( maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) {
  41. super( new BufferGeometry(), material );
  42. this.isBatchedMesh = true;
  43. this._drawRanges = [];
  44. this._reservedRanges = [];
  45. this._visible = [];
  46. this._active = [];
  47. this._maxGeometryCount = maxGeometryCount;
  48. this._maxVertexCount = maxVertexCount;
  49. this._maxIndexCount = maxIndexCount;
  50. this._geometryInitialized = false;
  51. this._geometryCount = 0;
  52. this._multiDrawCounts = null;
  53. this._multiDrawStarts = null;
  54. this._multiDrawCount = 0;
  55. // Local matrix per geometry by using data texture
  56. // @TODO: Support uniform parameter per geometry
  57. this._matrices = [];
  58. this._matricesTexture = null;
  59. // @TODO: Calculate the entire binding box and make frustumCulled true
  60. this.frustumCulled = false;
  61. this._initMatricesTexture();
  62. }
  63. _initMatricesTexture() {
  64. // layout (1 matrix = 4 pixels)
  65. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  66. // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)
  67. // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)
  68. // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)
  69. // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)
  70. let size = Math.sqrt( this._maxGeometryCount * 4 ); // 4 pixels needed for 1 matrix
  71. size = MathUtils.ceilPowerOfTwo( size );
  72. size = Math.max( size, 4 );
  73. const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  74. const matricesTexture = new DataTexture( matricesArray, size, size, RGBAFormat, FloatType );
  75. this._matricesTexture = matricesTexture;
  76. }
  77. _initializeGeometry( reference ) {
  78. // @TODO: geometry.groups support?
  79. // @TODO: geometry.drawRange support?
  80. // @TODO: geometry.morphAttributes support?
  81. const geometry = this.geometry;
  82. const maxVertexCount = this._maxVertexCount;
  83. const maxGeometryCount = this._maxGeometryCount;
  84. const maxIndexCount = this._maxIndexCount;
  85. if ( this._geometryInitialized === false ) {
  86. for ( const attributeName in reference.attributes ) {
  87. const srcAttribute = reference.getAttribute( attributeName );
  88. const { array, itemSize, normalized } = srcAttribute;
  89. const dstArray = new array.constructor( maxVertexCount * itemSize );
  90. const dstAttribute = new srcAttribute.constructor( dstArray, itemSize, normalized );
  91. dstAttribute.setUsage( srcAttribute.usage );
  92. geometry.setAttribute( attributeName, dstAttribute );
  93. }
  94. if ( reference.getIndex() !== null ) {
  95. const indexArray = maxVertexCount > 65536
  96. ? new Uint32Array( maxIndexCount )
  97. : new Uint16Array( maxIndexCount );
  98. geometry.setIndex( new BufferAttribute( indexArray, 1 ) );
  99. }
  100. const idArray = maxGeometryCount > 65536
  101. ? new Uint32Array( maxVertexCount )
  102. : new Uint16Array( maxVertexCount );
  103. geometry.setAttribute( ID_ATTR_NAME, new BufferAttribute( idArray, 1 ) );
  104. this._geometryInitialized = true;
  105. this._multiDrawCounts = new Int32Array( maxGeometryCount );
  106. this._multiDrawStarts = new Int32Array( maxGeometryCount );
  107. }
  108. }
  109. // Make sure the geometry is compatible with the existing combined geometry atributes
  110. _validateGeometry( geometry ) {
  111. // check that the geometry doesn't have a version of our reserved id attribute
  112. if ( geometry.getAttribute( ID_ATTR_NAME ) ) {
  113. throw new Error( `BatchedMesh: Geometry cannot use attribute "${ ID_ATTR_NAME }"` );
  114. }
  115. // check to ensure the geometries are using consistent attributes and indices
  116. const batchGeometry = this.geometry;
  117. if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {
  118. throw new Error( 'BatchedMesh: All geometries must consistently have "index".' );
  119. }
  120. for ( const attributeName in batchGeometry.attributes ) {
  121. if ( attributeName === ID_ATTR_NAME ) {
  122. continue;
  123. }
  124. if ( ! geometry.hasAttribute( attributeName ) ) {
  125. throw new Error( `BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );
  126. }
  127. const srcAttribute = geometry.getAttribute( attributeName );
  128. const dstAttribute = batchGeometry.getAttribute( attributeName );
  129. if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) {
  130. throw new Error( 'BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );
  131. }
  132. }
  133. }
  134. getGeometryCount() {
  135. return this._geometryCount;
  136. }
  137. getVertexCount() {
  138. const reservedRanges = this._reservedRanges;
  139. if ( reservedRanges.length === 0 ) {
  140. return 0;
  141. } else {
  142. const finalRange = reservedRanges[ reservedRanges.length - 1 ];
  143. return finalRange.vertexStart + finalRange.vertexCount;
  144. }
  145. }
  146. getIndexCount() {
  147. const reservedRanges = this._reservedRanges;
  148. const geometry = this.geometry;
  149. if ( geometry.getIndex() === null || reservedRanges.length === 0 ) {
  150. return 0;
  151. } else {
  152. const finalRange = reservedRanges[ reservedRanges.length - 1 ];
  153. return finalRange.indexStart + finalRange.indexCount;
  154. }
  155. }
  156. addGeometry( geometry, vertexCount = - 1, indexCount = - 1 ) {
  157. this._initializeGeometry( geometry );
  158. this._validateGeometry( geometry );
  159. // ensure we're not over geometry
  160. if ( this._geometryCount >= this._maxGeometryCount ) {
  161. throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
  162. }
  163. // get the necessary range fo the geometry
  164. const reservedRange = {
  165. vertexStart: - 1,
  166. vertexCount: - 1,
  167. indexStart: - 1,
  168. indexCount: - 1,
  169. };
  170. let lastRange = null;
  171. const reservedRanges = this._reservedRanges;
  172. const drawRanges = this._drawRanges;
  173. if ( this._geometryCount !== 0 ) {
  174. lastRange = reservedRanges[ reservedRanges.length - 1 ];
  175. }
  176. if ( vertexCount === - 1 ) {
  177. reservedRange.vertexCount = geometry.getAttribute( 'position' ).count;
  178. } else {
  179. reservedRange.vertexCount = vertexCount;
  180. }
  181. if ( lastRange === null ) {
  182. reservedRange.vertexStart = 0;
  183. } else {
  184. reservedRange.vertexStart = lastRange.vertexStart + lastRange.vertexCount;
  185. }
  186. const index = geometry.getIndex();
  187. const hasIndex = index !== null;
  188. if ( hasIndex ) {
  189. if ( indexCount === - 1 ) {
  190. reservedRange.indexCount = index.count;
  191. } else {
  192. reservedRange.indexCount = indexCount;
  193. }
  194. if ( lastRange === null ) {
  195. reservedRange.indexStart = 0;
  196. } else {
  197. reservedRange.indexStart = lastRange.indexStart + lastRange.indexCount;
  198. }
  199. }
  200. if (
  201. reservedRange.indexStart !== - 1 &&
  202. reservedRange.indexStart + reservedRange.indexCount > this._maxIndexCount ||
  203. reservedRange.vertexStart + reservedRange.vertexCount > this._maxVertexCount
  204. ) {
  205. throw new Error( 'BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
  206. }
  207. const visible = this._visible;
  208. const active = this._active;
  209. const matricesTexture = this._matricesTexture;
  210. const matrices = this._matrices;
  211. const matricesArray = this._matricesTexture.image.data;
  212. // push new visibility states
  213. visible.push( true );
  214. active.push( true );
  215. // update id
  216. const geometryId = this._geometryCount;
  217. this._geometryCount ++;
  218. // initialize matrix information
  219. matrices.push( new Matrix4() );
  220. _identityMatrix.toArray( matricesArray, geometryId * 16 );
  221. matricesTexture.needsUpdate = true;
  222. // add the reserved range and draw range objects
  223. reservedRanges.push( reservedRange );
  224. drawRanges.push( {
  225. start: hasIndex ? reservedRange.indexStart : reservedRange.vertexStart,
  226. count: - 1
  227. } );
  228. // set the id for the geometry
  229. const idAttribute = this.geometry.getAttribute( ID_ATTR_NAME );
  230. for ( let i = 0; i < reservedRange.vertexCount; i ++ ) {
  231. idAttribute.setX( reservedRange.vertexStart + i, geometryId );
  232. }
  233. idAttribute.needsUpdate = true;
  234. // update the geometry
  235. this.setGeometryAt( geometryId, geometry );
  236. return geometryId;
  237. }
  238. setGeometryAt( id, geometry ) {
  239. if ( id >= this._geometryCount ) {
  240. throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
  241. }
  242. this._validateGeometry( geometry );
  243. const batchGeometry = this.geometry;
  244. const hasIndex = batchGeometry.getIndex() !== null;
  245. const dstIndex = batchGeometry.getIndex();
  246. const srcIndex = geometry.getIndex();
  247. const reservedRange = this._reservedRanges[ id ];
  248. if (
  249. hasIndex &&
  250. srcIndex.count > reservedRange.indexCount ||
  251. geometry.attributes.position.count > reservedRange.vertexCount
  252. ) {
  253. throw new Error( 'BatchedMesh: Reserved space not large enough for provided geometry.' );
  254. }
  255. // copy geometry over
  256. const vertexStart = reservedRange.vertexStart;
  257. const vertexCount = reservedRange.vertexCount;
  258. for ( const attributeName in batchGeometry.attributes ) {
  259. if ( attributeName === ID_ATTR_NAME ) {
  260. continue;
  261. }
  262. // copy attribute data
  263. const srcAttribute = geometry.getAttribute( attributeName );
  264. const dstAttribute = batchGeometry.getAttribute( attributeName );
  265. copyAttributeData( srcAttribute, dstAttribute, vertexStart );
  266. // fill the rest in with zeroes
  267. const itemSize = srcAttribute.itemSize;
  268. for ( let i = srcAttribute.count, l = vertexCount; i < l; i ++ ) {
  269. const index = vertexStart + i;
  270. for ( let c = 0; c < itemSize; c ++ ) {
  271. dstAttribute.setComponent( index, c, 0 );
  272. }
  273. }
  274. dstAttribute.needsUpdate = true;
  275. }
  276. // copy index
  277. if ( hasIndex ) {
  278. const indexStart = reservedRange.indexStart;
  279. // copy index data over
  280. for ( let i = 0; i < srcIndex.count; i ++ ) {
  281. dstIndex.setX( indexStart + i, vertexStart + srcIndex.getX( i ) );
  282. }
  283. // fill the rest in with zeroes
  284. for ( let i = srcIndex.count, l = reservedRange.indexCount; i < l; i ++ ) {
  285. dstIndex.setX( indexStart + i, vertexStart );
  286. }
  287. dstIndex.needsUpdate = true;
  288. }
  289. // set drawRange count
  290. const drawRange = this._drawRanges[ id ];
  291. const posAttr = geometry.getAttribute( 'position' );
  292. drawRange.count = hasIndex ? srcIndex.count : posAttr.count;
  293. return id;
  294. }
  295. deleteGeometry( geometryId ) {
  296. // Note: User needs to call optimize() afterward to pack the data.
  297. const active = this._active;
  298. const matricesArray = this._matricesTexture.image.data;
  299. const matricesTexture = this._matricesTexture;
  300. if ( geometryId >= active.length || active[ geometryId ] === false ) {
  301. return this;
  302. }
  303. active[ geometryId ] = false;
  304. _zeroScaleMatrix.toArray( matricesArray, geometryId * 16 );
  305. matricesTexture.needsUpdate = true;
  306. return this;
  307. }
  308. optimize() {
  309. throw new Error( 'BatchedMesh: Optimize function not implemented.' );
  310. }
  311. setMatrixAt( geometryId, matrix ) {
  312. // @TODO: Map geometryId to index of the arrays because
  313. // optimize() can make geometryId mismatch the index
  314. const visible = this._visible;
  315. const active = this._active;
  316. const matricesTexture = this._matricesTexture;
  317. const matrices = this._matrices;
  318. const matricesArray = this._matricesTexture.image.data;
  319. if ( geometryId >= matrices.length || active[ geometryId ] === false ) {
  320. return this;
  321. }
  322. if ( visible[ geometryId ] === true ) {
  323. matrix.toArray( matricesArray, geometryId * 16 );
  324. matricesTexture.needsUpdate = true;
  325. }
  326. matrices[ geometryId ].copy( matrix );
  327. return this;
  328. }
  329. getMatrixAt( geometryId, matrix ) {
  330. const matrices = this._matrices;
  331. const active = this._active;
  332. if ( geometryId >= matrices.length || active[ geometryId ] === false ) {
  333. return matrix;
  334. }
  335. return matrix.copy( matrices[ geometryId ] );
  336. }
  337. setVisibleAt( geometryId, value ) {
  338. const visible = this._visible;
  339. const active = this._active;
  340. const matricesTexture = this._matricesTexture;
  341. const matrices = this._matrices;
  342. const matricesArray = this._matricesTexture.image.data;
  343. // if the geometry is out of range, not active, or visibility state
  344. // does not change then return early
  345. if (
  346. geometryId >= visible.length ||
  347. active[ geometryId ] === false ||
  348. visible[ geometryId ] === value
  349. ) {
  350. return this;
  351. }
  352. // scale the matrix to zero if it's hidden
  353. if ( value === true ) {
  354. matrices[ geometryId ].toArray( matricesArray, geometryId * 16 );
  355. } else {
  356. _zeroScaleMatrix.toArray( matricesArray, geometryId * 16 );
  357. }
  358. matricesTexture.needsUpdate = true;
  359. visible[ geometryId ] = value;
  360. return this;
  361. }
  362. getVisibleAt( geometryId ) {
  363. const visible = this._visible;
  364. const active = this._active;
  365. // return early if the geometry is out of range or not active
  366. if ( geometryId >= visible.length || active[ geometryId ] === false ) {
  367. return false;
  368. }
  369. return visible[ geometryId ];
  370. }
  371. raycast() {
  372. console.warn( 'BatchedMesh: Raycast function not implemented.' );
  373. }
  374. copy() {
  375. // super.copy( source );
  376. throw new Error( 'BatchedMesh: Copy function not implemented.' );
  377. }
  378. toJSON() {
  379. throw new Error( 'BatchedMesh: toJSON function not implemented.' );
  380. }
  381. dispose() {
  382. // Assuming the geometry is not shared with other meshes
  383. this.geometry.dispose();
  384. this._matricesTexture.dispose();
  385. this._matricesTexture = null;
  386. return this;
  387. }
  388. onBeforeRender( _renderer, _scene, _camera, geometry ) {
  389. // the indexed version of the multi draw function requires specifying the start
  390. // offset in bytes.
  391. const index = geometry.getIndex();
  392. const bytesPerElement = index === null ? 1 : index.array.BYTES_PER_ELEMENT;
  393. const visible = this._visible;
  394. const multiDrawStarts = this._multiDrawStarts;
  395. const multiDrawCounts = this._multiDrawCounts;
  396. const drawRanges = this._drawRanges;
  397. let count = 0;
  398. for ( let i = 0, l = visible.length; i < l; i ++ ) {
  399. if ( visible[ i ] ) {
  400. const range = drawRanges[ i ];
  401. multiDrawStarts[ count ] = range.start * bytesPerElement;
  402. multiDrawCounts[ count ] = range.count;
  403. count ++;
  404. }
  405. }
  406. this._multiDrawCount = count;
  407. // @TODO: Implement frustum culling for each geometry
  408. // @TODO: Implement geometry sorting for transparent and opaque materials
  409. }
  410. }
  411. export { BatchedMesh };