SceneUtils.js 8.4 KB

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