BatchedMesh.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. uniform int batchingTextureSize;
  25. mat4 getBatchingMatrix( const in float i ) {
  26. float j = i * 4.0;
  27. float x = mod( j, float( batchingTextureSize ) );
  28. float y = floor( j / float( batchingTextureSize ) );
  29. float dx = 1.0 / float( batchingTextureSize );
  30. float dy = 1.0 / float( batchingTextureSize );
  31. y = dy * ( y + 0.5 );
  32. vec4 v1 = texture2D( batchingTexture, vec2( dx * ( x + 0.5 ), y ) );
  33. vec4 v2 = texture2D( batchingTexture, vec2( dx * ( x + 1.5 ), y ) );
  34. vec4 v3 = texture2D( batchingTexture, vec2( dx * ( x + 2.5 ), y ) );
  35. vec4 v4 = texture2D( batchingTexture, vec2( dx * ( x + 3.5 ), y ) );
  36. return mat4( v1, v2, v3, v4 );
  37. }
  38. #endif
  39. `;
  40. const batchingbaseVertex = /* glsl */`
  41. #ifdef BATCHING
  42. mat4 batchingMatrix = getBatchingMatrix( ${ ID_ATTR_NAME } );
  43. #endif
  44. `;
  45. const batchingnormalVertex = /* glsl */`
  46. #ifdef BATCHING
  47. objectNormal = vec4( batchingMatrix * vec4( objectNormal, 0.0 ) ).xyz;
  48. #ifdef USE_TANGENT
  49. objectTangent = vec4( batchingMatrix * vec4( objectTangent, 0.0 ) ).xyz;
  50. #endif
  51. #endif
  52. `;
  53. const batchingVertex = /* glsl */`
  54. #ifdef BATCHING
  55. transformed = ( batchingMatrix * vec4( transformed, 1.0 ) ).xyz;
  56. #endif
  57. `;
  58. // @TODO: SkinnedMesh support?
  59. // @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.
  60. // copies data from attribute "src" into "target" starting at "targetOffset"
  61. function copyAttributeData( src, target, targetOffset = 0 ) {
  62. const itemSize = target.itemSize;
  63. if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) {
  64. // use the component getters and setters if the array data cannot
  65. // be copied directly
  66. const vertexCount = src.count;
  67. for ( let i = 0; i < vertexCount; i ++ ) {
  68. for ( let c = 0; c < itemSize; c ++ ) {
  69. target.setComponent( i + targetOffset, c, src.getComponent( i, c ) );
  70. }
  71. }
  72. } else {
  73. // faster copy approach using typed array set function
  74. target.array.set( src.array, targetOffset * itemSize );
  75. }
  76. target.needsUpdate = true;
  77. }
  78. class BatchedMesh extends Mesh {
  79. constructor( maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) {
  80. super( new BufferGeometry(), material );
  81. this._vertexStarts = [];
  82. this._vertexCounts = [];
  83. this._indexStarts = [];
  84. this._indexCounts = [];
  85. this._visible = [];
  86. this._active = [];
  87. this._maxGeometryCount = maxGeometryCount;
  88. this._maxVertexCount = maxVertexCount;
  89. this._maxIndexCount = maxIndexCount;
  90. this._geometryInitialized = false;
  91. this._geometryCount = 0;
  92. this._vertexCount = 0;
  93. this._indexCount = 0;
  94. // Local matrix per geometry by using data texture
  95. // @TODO: Support uniform parameter per geometry
  96. this._matrices = [];
  97. this._matricesTexture = null;
  98. // @TODO: Calculate the entire binding box and make frustumCulled true
  99. this.frustumCulled = false;
  100. this._customUniforms = {
  101. batchingTexture: { value: null },
  102. batchingTextureSize: { value: 0 }
  103. };
  104. this._initMatricesTexture();
  105. this._initShader();
  106. }
  107. _initMatricesTexture() {
  108. // layout (1 matrix = 4 pixels)
  109. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  110. // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)
  111. // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)
  112. // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)
  113. // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)
  114. let size = Math.sqrt( this._maxGeometryCount * 4 ); // 4 pixels needed for 1 matrix
  115. size = MathUtils.ceilPowerOfTwo( size );
  116. size = Math.max( size, 4 );
  117. const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  118. const matricesTexture = new DataTexture( matricesArray, size, size, RGBAFormat, FloatType );
  119. this._matricesTexture = matricesTexture;
  120. this._customUniforms.batchingTexture.value = this._matricesTexture;
  121. this._customUniforms.batchingTextureSize.value = size;
  122. }
  123. _initShader() {
  124. const material = this.material;
  125. const currentOnBeforeCompile = material.onBeforeCompile;
  126. const customUniforms = this._customUniforms;
  127. material.onBeforeCompile = function onBeforeCompile( parameters, renderer ) {
  128. // Is this replacement stable across any materials?
  129. parameters.vertexShader = parameters.vertexShader
  130. .replace(
  131. '#include <skinning_pars_vertex>',
  132. '#include <skinning_pars_vertex>\n'
  133. + batchingParsVertex
  134. )
  135. .replace(
  136. '#include <uv_vertex>',
  137. '#include <uv_vertex>\n'
  138. + batchingbaseVertex
  139. )
  140. .replace(
  141. '#include <skinnormal_vertex>',
  142. '#include <skinnormal_vertex>\n'
  143. + batchingnormalVertex
  144. )
  145. .replace(
  146. '#include <skinning_vertex>',
  147. '#include <skinning_vertex>\n'
  148. + batchingVertex
  149. );
  150. for ( const uniformName in customUniforms ) {
  151. parameters.uniforms[ uniformName ] = customUniforms[ uniformName ];
  152. }
  153. currentOnBeforeCompile.call( this, parameters, renderer );
  154. };
  155. material.defines = material.defines || {};
  156. material.defines.BATCHING = false;
  157. }
  158. _initializeGeometry( reference ) {
  159. // @TODO: geometry.groups support?
  160. // @TODO: geometry.drawRange support?
  161. // @TODO: geometry.morphAttributes support?
  162. const geometry = this.geometry;
  163. const maxVertexCount = this._maxVertexCount;
  164. const maxGeometryCount = this._maxGeometryCount;
  165. const maxIndexCount = this._maxIndexCount;
  166. if ( this._geometryInitialized === false ) {
  167. for ( const attributeName in reference.attributes ) {
  168. const srcAttribute = reference.getAttribute( attributeName );
  169. const { array, itemSize, normalized } = srcAttribute;
  170. const dstArray = new array.constructor( maxVertexCount * itemSize );
  171. const dstAttribute = new srcAttribute.constructor( dstArray, itemSize, normalized );
  172. dstAttribute.setUsage( srcAttribute.usage );
  173. geometry.setAttribute( attributeName, dstAttribute );
  174. }
  175. if ( reference.getIndex() !== null ) {
  176. const indexArray = maxVertexCount > 65536
  177. ? new Uint32Array( maxIndexCount )
  178. : new Uint16Array( maxIndexCount );
  179. geometry.setIndex( new BufferAttribute( indexArray, 1 ) );
  180. }
  181. const idArray = maxGeometryCount > 65536
  182. ? new Uint32Array( maxVertexCount )
  183. : new Uint16Array( maxVertexCount );
  184. geometry.setAttribute( ID_ATTR_NAME, new BufferAttribute( idArray, 1 ) );
  185. this._geometryInitialized = true;
  186. }
  187. }
  188. getGeometryCount() {
  189. return this._geometryCount;
  190. }
  191. getVertexCount() {
  192. return this._vertexCount;
  193. }
  194. getIndexCount() {
  195. return this._indexCount;
  196. }
  197. applyGeometry( geometry ) {
  198. this._initializeGeometry( 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. // check that the geometry doesn't have a version of our reserved id attribute
  204. if ( geometry.getAttribute( ID_ATTR_NAME ) ) {
  205. throw new Error( `BatchedMesh: Geometry cannot use attribute "${ ID_ATTR_NAME }"` );
  206. }
  207. // check to ensure the geometries are using consistent attributes and indices
  208. const batchGeometry = this.geometry;
  209. if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {
  210. throw new Error( 'BatchedMesh: All geometries must consistently have "index".' );
  211. }
  212. for ( const attributeName in batchGeometry.attributes ) {
  213. if ( attributeName === ID_ATTR_NAME ) {
  214. continue;
  215. }
  216. if ( ! geometry.hasAttribute( attributeName ) ) {
  217. throw new Error( `BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );
  218. }
  219. const srcAttribute = geometry.getAttribute( attributeName );
  220. const dstAttribute = batchGeometry.getAttribute( attributeName );
  221. if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) {
  222. throw new Error( 'BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );
  223. }
  224. }
  225. // Assuming geometry has position attribute
  226. const srcPositionAttribute = geometry.getAttribute( 'position' );
  227. const vertexCount = this._vertexCount;
  228. const indexCount = this._indexCount;
  229. const maxVertexCount = this._maxVertexCount;
  230. const maxIndexCount = this._maxIndexCount;
  231. // check if we're going over our maximum buffer capacity
  232. if (
  233. geometry.getIndex() !== null &&
  234. indexCount + geometry.getIndex().count > maxIndexCount ||
  235. vertexCount + srcPositionAttribute.count > maxVertexCount
  236. ) {
  237. throw new Error( 'BatchedMesh: Added geometry is larger than available buffer capacity.' );
  238. }
  239. const visible = this._visible;
  240. const active = this._active;
  241. const matricesTexture = this._matricesTexture;
  242. const matrices = this._matrices;
  243. const matricesArray = this._matricesTexture.image.data;
  244. const indexCounts = this._indexCounts;
  245. const indexStarts = this._indexStarts;
  246. const vertexCounts = this._vertexCounts;
  247. const vertexStarts = this._vertexStarts;
  248. const hasIndex = batchGeometry.getIndex() !== null;
  249. const dstIndex = batchGeometry.getIndex();
  250. const srcIndex = geometry.getIndex();
  251. // push new geometry data range
  252. vertexStarts.push( vertexCount );
  253. vertexCounts.push( srcPositionAttribute.count );
  254. // copy attribute data over
  255. for ( const attributeName in batchGeometry.attributes ) {
  256. if ( attributeName === ID_ATTR_NAME ) {
  257. continue;
  258. }
  259. const srcAttribute = geometry.getAttribute( attributeName );
  260. const dstAttribute = batchGeometry.getAttribute( attributeName );
  261. copyAttributeData( srcAttribute, dstAttribute, vertexCount );
  262. }
  263. if ( hasIndex ) {
  264. // push new index range
  265. indexStarts.push( indexCount );
  266. indexCounts.push( srcIndex.count );
  267. // copy index data over
  268. for ( let i = 0; i < srcIndex.count; i ++ ) {
  269. dstIndex.setX( indexCount + i, vertexCount + srcIndex.getX( i ) );
  270. }
  271. this._indexCount += srcIndex.count;
  272. dstIndex.needsUpdate = true;
  273. }
  274. // fill in the geometry ids
  275. const geometryId = this._geometryCount;
  276. this._geometryCount ++;
  277. const idAttribute = batchGeometry.getAttribute( ID_ATTR_NAME );
  278. for ( let i = 0; i < srcPositionAttribute.count; i ++ ) {
  279. idAttribute.setX( this._vertexCount + i, geometryId );
  280. }
  281. idAttribute.needsUpdate = true;
  282. // extend new range
  283. this._vertexCount += srcPositionAttribute.count;
  284. // push new visibility states
  285. visible.push( true );
  286. active.push( true );
  287. // initialize matrix information
  288. matrices.push( new Matrix4() );
  289. _identityMatrix.toArray( matricesArray, geometryId * 16 );
  290. matricesTexture.needsUpdate = true;
  291. return geometryId;
  292. }
  293. deleteGeometry( geometryId ) {
  294. // Note: User needs to call optimize() afterward to pack the data.
  295. const active = this._active;
  296. const matricesArray = this._matricesTexture.image.data;
  297. const matricesTexture = this._matricesTexture;
  298. if ( geometryId >= active.length || active[ geometryId ] === false ) {
  299. return this;
  300. }
  301. active[ geometryId ] = false;
  302. _zeroScaleMatrix.toArray( matricesArray, geometryId * 16 );
  303. matricesTexture.needsUpdate = true;
  304. return this;
  305. }
  306. optimize() {
  307. throw new Error( 'BatchedMesh: Optimize function not implemented.' );
  308. }
  309. setMatrixAt( geometryId, matrix ) {
  310. // @TODO: Map geometryId to index of the arrays because
  311. // optimize() can make geometryId mismatch the index
  312. const visible = this._visible;
  313. const active = this._active;
  314. const matricesTexture = this._matricesTexture;
  315. const matrices = this._matrices;
  316. const matricesArray = this._matricesTexture.image.data;
  317. if ( geometryId >= matrices.length || active[ geometryId ] === false ) {
  318. return this;
  319. }
  320. if ( visible[ geometryId ] === true ) {
  321. matrix.toArray( matricesArray, geometryId * 16 );
  322. matricesTexture.needsUpdate = true;
  323. }
  324. matrices[ geometryId ].copy( matrix );
  325. return this;
  326. }
  327. getMatrixAt( geometryId, matrix ) {
  328. const matrices = this._matrices;
  329. const active = this._active;
  330. if ( geometryId >= matrices.length || active[ geometryId ] === false ) {
  331. return matrix;
  332. }
  333. return matrix.copy( matrices[ geometryId ] );
  334. }
  335. setVisibleAt( geometryId, value ) {
  336. const visible = this._visible;
  337. const active = this._active;
  338. const matricesTexture = this._matricesTexture;
  339. const matrices = this._matrices;
  340. const matricesArray = this._matricesTexture.image.data;
  341. // if the geometry is out of range, not active, or visibility state
  342. // does not change then return early
  343. if (
  344. geometryId >= visible.length ||
  345. active[ geometryId ] === false ||
  346. visible[ geometryId ] === value
  347. ) {
  348. return this;
  349. }
  350. // scale the matrix to zero if it's hidden
  351. if ( value === true ) {
  352. matrices[ geometryId ].toArray( matricesArray, geometryId * 16 );
  353. } else {
  354. _zeroScaleMatrix.toArray( matricesArray, geometryId * 16 );
  355. }
  356. matricesTexture.needsUpdate = true;
  357. visible[ geometryId ] = value;
  358. return this;
  359. }
  360. getVisibleAt( geometryId ) {
  361. const visible = this._visible;
  362. const active = this._active;
  363. // return early if the geometry is out of range or not active
  364. if ( geometryId >= visible.length || active[ geometryId ] === false ) {
  365. return false;
  366. }
  367. return visible[ geometryId ];
  368. }
  369. raycast() {
  370. console.warn( 'BatchedMesh: Raycast function not implemented.' );
  371. }
  372. copy() {
  373. // super.copy( source );
  374. throw new Error( 'BatchedMesh: Copy function not implemented.' );
  375. }
  376. toJSON() {
  377. throw new Error( 'BatchedMesh: toJSON function not implemented.' );
  378. }
  379. dispose() {
  380. // Assuming the geometry is not shared with other meshes
  381. this.geometry.dispose();
  382. this._matricesTexture.dispose();
  383. this._matricesTexture = null;
  384. return this;
  385. }
  386. onBeforeRender( _renderer, _scene, _camera, _geometry, material/*, _group*/ ) {
  387. material.defines.BATCHING = true;
  388. // @TODO: Implement frustum culling for each geometry
  389. }
  390. onAfterRender( _renderer, _scene, _camera, _geometry, material/*, _group*/ ) {
  391. material.defines.BATCHING = false;
  392. }
  393. }
  394. export { BatchedMesh };