InstancedMesh.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
  2. import { Mesh } from './Mesh.js';
  3. import { Box3 } from '../math/Box3.js';
  4. import { Matrix4 } from '../math/Matrix4.js';
  5. import { Sphere } from '../math/Sphere.js';
  6. import { DataTexture } from '../textures/DataTexture.js';
  7. import { FloatType, RedFormat } from '../constants.js';
  8. const _instanceLocalMatrix = /*@__PURE__*/ new Matrix4();
  9. const _instanceWorldMatrix = /*@__PURE__*/ new Matrix4();
  10. const _instanceIntersects = [];
  11. const _box3 = /*@__PURE__*/ new Box3();
  12. const _identity = /*@__PURE__*/ new Matrix4();
  13. const _mesh = /*@__PURE__*/ new Mesh();
  14. const _sphere = /*@__PURE__*/ new Sphere();
  15. class InstancedMesh extends Mesh {
  16. constructor( geometry, material, count ) {
  17. super( geometry, material );
  18. this.isInstancedMesh = true;
  19. this.instanceMatrix = new InstancedBufferAttribute( new Float32Array( count * 16 ), 16 );
  20. this.instanceColor = null;
  21. this.morphTexture = null;
  22. this.count = count;
  23. this.boundingBox = null;
  24. this.boundingSphere = null;
  25. for ( let i = 0; i < count; i ++ ) {
  26. this.setMatrixAt( i, _identity );
  27. }
  28. }
  29. computeBoundingBox() {
  30. const geometry = this.geometry;
  31. const count = this.count;
  32. if ( this.boundingBox === null ) {
  33. this.boundingBox = new Box3();
  34. }
  35. if ( geometry.boundingBox === null ) {
  36. geometry.computeBoundingBox();
  37. }
  38. this.boundingBox.makeEmpty();
  39. for ( let i = 0; i < count; i ++ ) {
  40. this.getMatrixAt( i, _instanceLocalMatrix );
  41. _box3.copy( geometry.boundingBox ).applyMatrix4( _instanceLocalMatrix );
  42. this.boundingBox.union( _box3 );
  43. }
  44. }
  45. computeBoundingSphere() {
  46. const geometry = this.geometry;
  47. const count = this.count;
  48. if ( this.boundingSphere === null ) {
  49. this.boundingSphere = new Sphere();
  50. }
  51. if ( geometry.boundingSphere === null ) {
  52. geometry.computeBoundingSphere();
  53. }
  54. this.boundingSphere.makeEmpty();
  55. for ( let i = 0; i < count; i ++ ) {
  56. this.getMatrixAt( i, _instanceLocalMatrix );
  57. _sphere.copy( geometry.boundingSphere ).applyMatrix4( _instanceLocalMatrix );
  58. this.boundingSphere.union( _sphere );
  59. }
  60. }
  61. copy( source, recursive ) {
  62. super.copy( source, recursive );
  63. this.instanceMatrix.copy( source.instanceMatrix );
  64. if ( source.morphTexture !== null ) this.morphTexture = source.morphTexture.clone();
  65. if ( source.instanceColor !== null ) this.instanceColor = source.instanceColor.clone();
  66. this.count = source.count;
  67. if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
  68. if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
  69. return this;
  70. }
  71. getColorAt( index, color ) {
  72. color.fromArray( this.instanceColor.array, index * 3 );
  73. }
  74. getMatrixAt( index, matrix ) {
  75. matrix.fromArray( this.instanceMatrix.array, index * 16 );
  76. }
  77. getMorphAt( index, object ) {
  78. const objectInfluences = object.morphTargetInfluences;
  79. const array = this.morphTexture.source.data.data;
  80. const len = objectInfluences.length + 1; // All influences + the baseInfluenceSum
  81. const dataIndex = index * len + 1; // Skip the baseInfluenceSum at the beginning
  82. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  83. objectInfluences[ i ] = array[ dataIndex + i ];
  84. }
  85. }
  86. raycast( raycaster, intersects ) {
  87. const matrixWorld = this.matrixWorld;
  88. const raycastTimes = this.count;
  89. _mesh.geometry = this.geometry;
  90. _mesh.material = this.material;
  91. if ( _mesh.material === undefined ) return;
  92. // test with bounding sphere first
  93. if ( this.boundingSphere === null ) this.computeBoundingSphere();
  94. _sphere.copy( this.boundingSphere );
  95. _sphere.applyMatrix4( matrixWorld );
  96. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  97. // now test each instance
  98. for ( let instanceId = 0; instanceId < raycastTimes; instanceId ++ ) {
  99. // calculate the world matrix for each instance
  100. this.getMatrixAt( instanceId, _instanceLocalMatrix );
  101. _instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix );
  102. // the mesh represents this single instance
  103. _mesh.matrixWorld = _instanceWorldMatrix;
  104. _mesh.raycast( raycaster, _instanceIntersects );
  105. // process the result of raycast
  106. for ( let i = 0, l = _instanceIntersects.length; i < l; i ++ ) {
  107. const intersect = _instanceIntersects[ i ];
  108. intersect.instanceId = instanceId;
  109. intersect.object = this;
  110. intersects.push( intersect );
  111. }
  112. _instanceIntersects.length = 0;
  113. }
  114. }
  115. setColorAt( index, color ) {
  116. if ( this.instanceColor === null ) {
  117. this.instanceColor = new InstancedBufferAttribute( new Float32Array( this.instanceMatrix.count * 3 ).fill( 1 ), 3 );
  118. }
  119. color.toArray( this.instanceColor.array, index * 3 );
  120. }
  121. setMatrixAt( index, matrix ) {
  122. matrix.toArray( this.instanceMatrix.array, index * 16 );
  123. }
  124. setMorphAt( index, object ) {
  125. const objectInfluences = object.morphTargetInfluences;
  126. const len = objectInfluences.length + 1; // morphBaseInfluence + all influences
  127. if ( this.morphTexture === null ) {
  128. this.morphTexture = new DataTexture( new Float32Array( len * this.count ), len, this.count, RedFormat, FloatType );
  129. }
  130. const array = this.morphTexture.source.data.data;
  131. let morphInfluencesSum = 0;
  132. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  133. morphInfluencesSum += objectInfluences[ i ];
  134. }
  135. const morphBaseInfluence = this.geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
  136. const dataIndex = len * index;
  137. array[ dataIndex ] = morphBaseInfluence;
  138. array.set( objectInfluences, dataIndex + 1 );
  139. }
  140. updateMorphTargets() {
  141. }
  142. dispose() {
  143. this.dispatchEvent( { type: 'dispose' } );
  144. if ( this.morphTexture !== null ) {
  145. this.morphTexture.dispose();
  146. this.morphTexture = null;
  147. }
  148. return this;
  149. }
  150. }
  151. export { InstancedMesh };
粤ICP备19079148号