SceneUtils.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /** @module SceneUtils */
  12. const _color = /*@__PURE__*/new Color();
  13. const _matrix = /*@__PURE__*/new Matrix4();
  14. /**
  15. * This function creates a mesh for each instance of the given instanced mesh and
  16. * adds it to a group. Each mesh will honor the current 3D transformation of its
  17. * corresponding instance.
  18. *
  19. * @param {InstancedMesh} instancedMesh - The instanced mesh.
  20. * @return {Group} A group of meshes.
  21. */
  22. function createMeshesFromInstancedMesh( instancedMesh ) {
  23. const group = new Group();
  24. const count = instancedMesh.count;
  25. const geometry = instancedMesh.geometry;
  26. const material = instancedMesh.material;
  27. for ( let i = 0; i < count; i ++ ) {
  28. const mesh = new Mesh( geometry, material );
  29. instancedMesh.getMatrixAt( i, mesh.matrix );
  30. mesh.matrix.decompose( mesh.position, mesh.quaternion, mesh.scale );
  31. group.add( mesh );
  32. }
  33. group.copy( instancedMesh );
  34. group.updateMatrixWorld(); // ensure correct world matrices of meshes
  35. return group;
  36. }
  37. /**
  38. * This function creates a mesh for each geometry-group of the given multi-material mesh and
  39. * adds it to a group.
  40. *
  41. * @param {Mesh} mesh - The multi-material mesh.
  42. * @return {Group} A group of meshes.
  43. */
  44. function createMeshesFromMultiMaterialMesh( mesh ) {
  45. if ( Array.isArray( mesh.material ) === false ) {
  46. console.warn( 'THREE.SceneUtils.createMeshesFromMultiMaterialMesh(): The given mesh has no multiple materials.' );
  47. return mesh;
  48. }
  49. const object = new Group();
  50. object.copy( mesh );
  51. // merge groups (which automatically sorts them)
  52. const geometry = mergeGroups( mesh.geometry );
  53. const index = geometry.index;
  54. const groups = geometry.groups;
  55. const attributeNames = Object.keys( geometry.attributes );
  56. // create a mesh for each group by extracting the buffer data into a new geometry
  57. for ( let i = 0; i < groups.length; i ++ ) {
  58. const group = groups[ i ];
  59. const start = group.start;
  60. const end = start + group.count;
  61. const newGeometry = new BufferGeometry();
  62. const newMaterial = mesh.material[ group.materialIndex ];
  63. // process all buffer attributes
  64. for ( let j = 0; j < attributeNames.length; j ++ ) {
  65. const name = attributeNames[ j ];
  66. const attribute = geometry.attributes[ name ];
  67. const itemSize = attribute.itemSize;
  68. const newLength = group.count * itemSize;
  69. const type = attribute.array.constructor;
  70. const newArray = new type( newLength );
  71. const newAttribute = new BufferAttribute( newArray, itemSize );
  72. for ( let k = start, n = 0; k < end; k ++, n ++ ) {
  73. const ind = index.getX( k );
  74. if ( itemSize >= 1 ) newAttribute.setX( n, attribute.getX( ind ) );
  75. if ( itemSize >= 2 ) newAttribute.setY( n, attribute.getY( ind ) );
  76. if ( itemSize >= 3 ) newAttribute.setZ( n, attribute.getZ( ind ) );
  77. if ( itemSize >= 4 ) newAttribute.setW( n, attribute.getW( ind ) );
  78. }
  79. newGeometry.setAttribute( name, newAttribute );
  80. }
  81. const newMesh = new Mesh( newGeometry, newMaterial );
  82. object.add( newMesh );
  83. }
  84. return object;
  85. }
  86. /**
  87. * This function represents an alternative way to create 3D objects with multiple materials.
  88. * Normally, {@link BufferGeometry#groups} are used which might introduce issues e.g. when
  89. * exporting the object to a 3D format. This function accepts a geometry and an array of
  90. * materials and creates for each material a mesh that is added to a group.
  91. *
  92. * @param {BufferGeometry} geometry - The geometry.
  93. * @param {Array<Material>} materials - An array of materials.
  94. * @return {Group} A group representing a multi-material object.
  95. */
  96. function createMultiMaterialObject( geometry, materials ) {
  97. const group = new Group();
  98. for ( let i = 0, l = materials.length; i < l; i ++ ) {
  99. group.add( new Mesh( geometry, materials[ i ] ) );
  100. }
  101. return group;
  102. }
  103. /**
  104. * Executes a reducer function for each vertex of the given 3D object.
  105. * `reduceVertices()` returns a single value: the function's accumulated result.
  106. *
  107. * @param {Object3D} object - The 3D object that should be processed. It must have a
  108. * geometry with a `position` attribute.
  109. * @param {function(number,Vector3):number} func - The reducer function. First argument
  110. * is the current value, second argument the current vertex.
  111. * @param {any} initialValue - The initial value.
  112. * @return {any} The result.
  113. */
  114. function reduceVertices( object, func, initialValue ) {
  115. let value = initialValue;
  116. const vertex = new Vector3();
  117. object.updateWorldMatrix( true, true );
  118. object.traverseVisible( ( child ) => {
  119. const { geometry } = child;
  120. if ( geometry !== undefined ) {
  121. const { position } = geometry.attributes;
  122. if ( position !== undefined ) {
  123. for ( let i = 0, l = position.count; i < l; i ++ ) {
  124. if ( child.isMesh ) {
  125. child.getVertexPosition( i, vertex );
  126. } else {
  127. vertex.fromBufferAttribute( position, i );
  128. }
  129. if ( ! child.isSkinnedMesh ) {
  130. vertex.applyMatrix4( child.matrixWorld );
  131. }
  132. value = func( value, vertex );
  133. }
  134. }
  135. }
  136. } );
  137. return value;
  138. }
  139. /**
  140. * Sorts the instances of the given instanced mesh.
  141. *
  142. * @param {InstancedMesh} mesh - The instanced mesh to sort.
  143. * @param {function(number, number):number} compareFn - A custom compare function for the sort.
  144. */
  145. function sortInstancedMesh( mesh, compareFn ) {
  146. // store copy of instanced attributes for lookups
  147. const instanceMatrixRef = deepCloneAttribute( mesh.instanceMatrix );
  148. const instanceColorRef = mesh.instanceColor ? deepCloneAttribute( mesh.instanceColor ) : null;
  149. const attributeRefs = new Map();
  150. for ( const name in mesh.geometry.attributes ) {
  151. const attribute = mesh.geometry.attributes[ name ];
  152. if ( attribute.isInstancedBufferAttribute ) {
  153. attributeRefs.set( attribute, deepCloneAttribute( attribute ) );
  154. }
  155. }
  156. // compute sort order
  157. const tokens = [];
  158. for ( let i = 0; i < mesh.count; i ++ ) tokens.push( i );
  159. tokens.sort( compareFn );
  160. // apply sort order
  161. for ( let i = 0; i < tokens.length; i ++ ) {
  162. const refIndex = tokens[ i ];
  163. _matrix.fromArray( instanceMatrixRef.array, refIndex * mesh.instanceMatrix.itemSize );
  164. _matrix.toArray( mesh.instanceMatrix.array, i * mesh.instanceMatrix.itemSize );
  165. if ( mesh.instanceColor ) {
  166. _color.fromArray( instanceColorRef.array, refIndex * mesh.instanceColor.itemSize );
  167. _color.toArray( mesh.instanceColor.array, i * mesh.instanceColor.itemSize );
  168. }
  169. for ( const name in mesh.geometry.attributes ) {
  170. const attribute = mesh.geometry.attributes[ name ];
  171. if ( attribute.isInstancedBufferAttribute ) {
  172. const attributeRef = attributeRefs.get( attribute );
  173. attribute.setX( i, attributeRef.getX( refIndex ) );
  174. if ( attribute.itemSize > 1 ) attribute.setY( i, attributeRef.getY( refIndex ) );
  175. if ( attribute.itemSize > 2 ) attribute.setZ( i, attributeRef.getZ( refIndex ) );
  176. if ( attribute.itemSize > 3 ) attribute.setW( i, attributeRef.getW( refIndex ) );
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * Generator based alternative to {@link Object3D#traverse}.
  183. *
  184. * @param {Object3D} object - Object to traverse.
  185. * @yields {Object3D} Objects that passed the filter condition.
  186. */
  187. function* traverseGenerator( object ) {
  188. yield object;
  189. const children = object.children;
  190. for ( let i = 0, l = children.length; i < l; i ++ ) {
  191. yield* traverseGenerator( children[ i ] );
  192. }
  193. }
  194. /**
  195. * Generator based alternative to {@link Object3D#traverseVisible}.
  196. *
  197. * @param {Object3D} object Object to traverse.
  198. * @yields {Object3D} Objects that passed the filter condition.
  199. */
  200. function* traverseVisibleGenerator( object ) {
  201. if ( object.visible === false ) return;
  202. yield object;
  203. const children = object.children;
  204. for ( let i = 0, l = children.length; i < l; i ++ ) {
  205. yield* traverseVisibleGenerator( children[ i ] );
  206. }
  207. }
  208. /**
  209. * Generator based alternative to {@link Object3D#traverseAncestors}.
  210. *
  211. * @param {Object3D} object Object to traverse.
  212. * @yields {Object3D} Objects that passed the filter condition.
  213. */
  214. function* traverseAncestorsGenerator( object ) {
  215. const parent = object.parent;
  216. if ( parent !== null ) {
  217. yield parent;
  218. yield* traverseAncestorsGenerator( parent );
  219. }
  220. }
  221. export {
  222. createMeshesFromInstancedMesh,
  223. createMeshesFromMultiMaterialMesh,
  224. createMultiMaterialObject,
  225. reduceVertices,
  226. sortInstancedMesh,
  227. traverseGenerator,
  228. traverseVisibleGenerator,
  229. traverseAncestorsGenerator
  230. };
粤ICP备19079148号