SceneUtils.js 5.6 KB

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