SceneUtils.js 4.6 KB

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