BatchedMesh.js 17 KB

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