InstancedMesh.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.instanceColor !== null ) this.instanceColor = source.instanceColor.clone();
  65. this.count = source.count;
  66. if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
  67. if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
  68. return this;
  69. }
  70. getColorAt( index, color ) {
  71. color.fromArray( this.instanceColor.array, index * 3 );
  72. }
  73. getMatrixAt( index, matrix ) {
  74. matrix.fromArray( this.instanceMatrix.array, index * 16 );
  75. }
  76. getMorphAt( index, object ) {
  77. const objectInfluences = object.morphTargetInfluences;
  78. const array = this.morphTexture.source.data.data;
  79. const len = objectInfluences.length + 1; // All influences + the baseInfluenceSum
  80. const dataIndex = index * len + 1; // Skip the baseInfluenceSum at the beginning
  81. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  82. objectInfluences[ i ] = array[ dataIndex + i ];
  83. }
  84. }
  85. raycast( raycaster, intersects ) {
  86. const matrixWorld = this.matrixWorld;
  87. const raycastTimes = this.count;
  88. _mesh.geometry = this.geometry;
  89. _mesh.material = this.material;
  90. if ( _mesh.material === undefined ) return;
  91. // test with bounding sphere first
  92. if ( this.boundingSphere === null ) this.computeBoundingSphere();
  93. _sphere.copy( this.boundingSphere );
  94. _sphere.applyMatrix4( matrixWorld );
  95. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  96. // now test each instance
  97. for ( let instanceId = 0; instanceId < raycastTimes; instanceId ++ ) {
  98. // calculate the world matrix for each instance
  99. this.getMatrixAt( instanceId, _instanceLocalMatrix );
  100. _instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix );
  101. // the mesh represents this single instance
  102. _mesh.matrixWorld = _instanceWorldMatrix;
  103. _mesh.raycast( raycaster, _instanceIntersects );
  104. // process the result of raycast
  105. for ( let i = 0, l = _instanceIntersects.length; i < l; i ++ ) {
  106. const intersect = _instanceIntersects[ i ];
  107. intersect.instanceId = instanceId;
  108. intersect.object = this;
  109. intersects.push( intersect );
  110. }
  111. _instanceIntersects.length = 0;
  112. }
  113. }
  114. setColorAt( index, color ) {
  115. if ( this.instanceColor === null ) {
  116. this.instanceColor = new InstancedBufferAttribute( new Float32Array( this.instanceMatrix.count * 3 ), 3 );
  117. }
  118. color.toArray( this.instanceColor.array, index * 3 );
  119. }
  120. setMatrixAt( index, matrix ) {
  121. matrix.toArray( this.instanceMatrix.array, index * 16 );
  122. }
  123. setMorphAt( index, object ) {
  124. const objectInfluences = object.morphTargetInfluences;
  125. const len = objectInfluences.length + 1; // morphBaseInfluence + all influences
  126. if ( this.morphTexture === null ) {
  127. this.morphTexture = new DataTexture( new Float32Array( len * this.count ), len, this.count, RedFormat, FloatType );
  128. }
  129. const array = this.morphTexture.source.data.data;
  130. let morphInfluencesSum = 0;
  131. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  132. morphInfluencesSum += objectInfluences[ i ];
  133. }
  134. const morphBaseInfluence = this.geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
  135. const dataIndex = len * index;
  136. array[ dataIndex ] = morphBaseInfluence;
  137. array.set( objectInfluences, dataIndex + 1 );
  138. }
  139. updateMorphTargets() {
  140. }
  141. dispose() {
  142. this.dispatchEvent( { type: 'dispose' } );
  143. }
  144. }
  145. export { InstancedMesh };
粤ICP备19079148号