SceneUtils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. Color,
  5. Group,
  6. Matrix4,
  7. Mesh,
  8. Vector3
  9. } from 'three';
  10. import { mergeGroups, deepCloneAttribute } from './BufferGeometryUtils.js';
  11. const _color = /*@__PURE__*/new Color();
  12. const _matrix = /*@__PURE__*/new Matrix4();
  13. function createMeshesFromInstancedMesh( instancedMesh ) {
  14. const group = new Group();
  15. const count = instancedMesh.count;
  16. const geometry = instancedMesh.geometry;
  17. const material = instancedMesh.material;
  18. for ( let i = 0; i < count; i ++ ) {
  19. const mesh = new Mesh( geometry, material );
  20. instancedMesh.getMatrixAt( i, mesh.matrix );
  21. mesh.matrix.decompose( mesh.position, mesh.quaternion, mesh.scale );
  22. group.add( mesh );
  23. }
  24. group.copy( instancedMesh );
  25. group.updateMatrixWorld(); // ensure correct world matrices of meshes
  26. return group;
  27. }
  28. function createMeshesFromMultiMaterialMesh( mesh ) {
  29. if ( Array.isArray( mesh.material ) === false ) {
  30. console.warn( 'THREE.SceneUtils.createMeshesFromMultiMaterialMesh(): The given mesh has no multiple materials.' );
  31. return mesh;
  32. }
  33. const object = new Group();
  34. object.copy( mesh );
  35. // merge groups (which automatically sorts them)
  36. const geometry = mergeGroups( mesh.geometry );
  37. const index = geometry.index;
  38. const groups = geometry.groups;
  39. const attributeNames = Object.keys( geometry.attributes );
  40. // create a mesh for each group by extracting the buffer data into a new geometry
  41. for ( let i = 0; i < groups.length; i ++ ) {
  42. const group = groups[ i ];
  43. const start = group.start;
  44. const end = start + group.count;
  45. const newGeometry = new BufferGeometry();
  46. const newMaterial = mesh.material[ group.materialIndex ];
  47. // process all buffer attributes
  48. for ( let j = 0; j < attributeNames.length; j ++ ) {
  49. const name = attributeNames[ j ];
  50. const attribute = geometry.attributes[ name ];
  51. const itemSize = attribute.itemSize;
  52. const newLength = group.count * itemSize;
  53. const type = attribute.array.constructor;
  54. const newArray = new type( newLength );
  55. const newAttribute = new BufferAttribute( newArray, itemSize );
  56. for ( let k = start, n = 0; k < end; k ++, n ++ ) {
  57. const ind = index.getX( k );
  58. if ( itemSize >= 1 ) newAttribute.setX( n, attribute.getX( ind ) );
  59. if ( itemSize >= 2 ) newAttribute.setY( n, attribute.getY( ind ) );
  60. if ( itemSize >= 3 ) newAttribute.setZ( n, attribute.getZ( ind ) );
  61. if ( itemSize >= 4 ) newAttribute.setW( n, attribute.getW( ind ) );
  62. }
  63. newGeometry.setAttribute( name, newAttribute );
  64. }
  65. const newMesh = new Mesh( newGeometry, newMaterial );
  66. object.add( newMesh );
  67. }
  68. return object;
  69. }
  70. function createMultiMaterialObject( geometry, materials ) {
  71. const group = new Group();
  72. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  73. group.add( new Mesh( geometry, materials[ i ] ) );
  74. }
  75. return group;
  76. }
  77. function reduceVertices( object, func, initialValue ) {
  78. let value = initialValue;
  79. const vertex = new Vector3();
  80. object.updateWorldMatrix( true, true );
  81. object.traverseVisible( ( child ) => {
  82. const { geometry } = child;
  83. if ( geometry !== undefined ) {
  84. const { position } = geometry.attributes;
  85. if ( position !== undefined ) {
  86. for ( let i = 0, l = position.count; i < l; i ++ ) {
  87. if ( child.isMesh ) {
  88. child.getVertexPosition( i, vertex );
  89. } else {
  90. vertex.fromBufferAttribute( position, i );
  91. }
  92. if ( ! child.isSkinnedMesh ) {
  93. vertex.applyMatrix4( child.matrixWorld );
  94. }
  95. value = func( value, vertex );
  96. }
  97. }
  98. }
  99. } );
  100. return value;
  101. }
  102. /**
  103. * @param {InstancedMesh}
  104. * @param {function(int, int):int}
  105. */
  106. function sortInstancedMesh( mesh, compareFn ) {
  107. // store copy of instanced attributes for lookups
  108. const instanceMatrixRef = deepCloneAttribute( mesh.instanceMatrix );
  109. const instanceColorRef = mesh.instanceColor ? deepCloneAttribute( mesh.instanceColor ) : null;
  110. const attributeRefs = new Map();
  111. for ( const name in mesh.geometry.attributes ) {
  112. const attribute = mesh.geometry.attributes[ name ];
  113. if ( attribute.isInstancedBufferAttribute ) {
  114. attributeRefs.set( attribute, deepCloneAttribute( attribute ) );
  115. }
  116. }
  117. // compute sort order
  118. const tokens = [];
  119. for ( let i = 0; i < mesh.count; i ++ ) tokens.push( i );
  120. tokens.sort( compareFn );
  121. // apply sort order
  122. for ( let i = 0; i < tokens.length; i ++ ) {
  123. const refIndex = tokens[ i ];
  124. _matrix.fromArray( instanceMatrixRef.array, refIndex * mesh.instanceMatrix.itemSize );
  125. _matrix.toArray( mesh.instanceMatrix.array, i * mesh.instanceMatrix.itemSize );
  126. if ( mesh.instanceColor ) {
  127. _color.fromArray( instanceColorRef.array, refIndex * mesh.instanceColor.itemSize );
  128. _color.toArray( mesh.instanceColor.array, i * mesh.instanceColor.itemSize );
  129. }
  130. for ( const name in mesh.geometry.attributes ) {
  131. const attribute = mesh.geometry.attributes[ name ];
  132. if ( attribute.isInstancedBufferAttribute ) {
  133. const attributeRef = attributeRefs.get( attribute );
  134. attribute.setX( i, attributeRef.getX( refIndex ) );
  135. if ( attribute.itemSize > 1 ) attribute.setY( i, attributeRef.getY( refIndex ) );
  136. if ( attribute.itemSize > 2 ) attribute.setZ( i, attributeRef.getZ( refIndex ) );
  137. if ( attribute.itemSize > 3 ) attribute.setW( i, attributeRef.getW( refIndex ) );
  138. }
  139. }
  140. }
  141. }
  142. export {
  143. createMeshesFromInstancedMesh,
  144. createMeshesFromMultiMaterialMesh,
  145. createMultiMaterialObject,
  146. reduceVertices,
  147. sortInstancedMesh
  148. };