123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- import {
- BufferAttribute,
- BufferGeometry,
- Color,
- Group,
- Matrix4,
- Mesh,
- Vector3
- } from 'three';
- import { mergeGroups, deepCloneAttribute } from './BufferGeometryUtils.js';
- /**
- * @module SceneUtils
- * @three_import import * as SceneUtils from 'three/addons/utils/SceneUtils.js';
- */
- const _color = /*@__PURE__*/new Color();
- const _matrix = /*@__PURE__*/new Matrix4();
- /**
- * This function creates a mesh for each instance of the given instanced mesh and
- * adds it to a group. Each mesh will honor the current 3D transformation of its
- * corresponding instance.
- *
- * @param {InstancedMesh} instancedMesh - The instanced mesh.
- * @return {Group} A group of meshes.
- */
- function createMeshesFromInstancedMesh( instancedMesh ) {
- const group = new Group();
- const count = instancedMesh.count;
- const geometry = instancedMesh.geometry;
- const material = instancedMesh.material;
- for ( let i = 0; i < count; i ++ ) {
- const mesh = new Mesh( geometry, material );
- instancedMesh.getMatrixAt( i, mesh.matrix );
- mesh.matrix.decompose( mesh.position, mesh.quaternion, mesh.scale );
- group.add( mesh );
- }
- group.copy( instancedMesh );
- group.updateMatrixWorld(); // ensure correct world matrices of meshes
- return group;
- }
- /**
- * This function creates a mesh for each geometry-group of the given multi-material mesh and
- * adds it to a group.
- *
- * @param {Mesh} mesh - The multi-material mesh.
- * @return {Group} A group of meshes.
- */
- function createMeshesFromMultiMaterialMesh( mesh ) {
- if ( Array.isArray( mesh.material ) === false ) {
- console.warn( 'THREE.SceneUtils.createMeshesFromMultiMaterialMesh(): The given mesh has no multiple materials.' );
- return mesh;
- }
- const object = new Group();
- object.copy( mesh );
- // merge groups (which automatically sorts them)
- const geometry = mergeGroups( mesh.geometry );
- const index = geometry.index;
- const groups = geometry.groups;
- const attributeNames = Object.keys( geometry.attributes );
- // create a mesh for each group by extracting the buffer data into a new geometry
- for ( let i = 0; i < groups.length; i ++ ) {
- const group = groups[ i ];
- const start = group.start;
- const end = start + group.count;
- const newGeometry = new BufferGeometry();
- const newMaterial = mesh.material[ group.materialIndex ];
- // process all buffer attributes
- for ( let j = 0; j < attributeNames.length; j ++ ) {
- const name = attributeNames[ j ];
- const attribute = geometry.attributes[ name ];
- const itemSize = attribute.itemSize;
- const newLength = group.count * itemSize;
- const type = attribute.array.constructor;
- const newArray = new type( newLength );
- const newAttribute = new BufferAttribute( newArray, itemSize );
- for ( let k = start, n = 0; k < end; k ++, n ++ ) {
- const ind = index.getX( k );
- if ( itemSize >= 1 ) newAttribute.setX( n, attribute.getX( ind ) );
- if ( itemSize >= 2 ) newAttribute.setY( n, attribute.getY( ind ) );
- if ( itemSize >= 3 ) newAttribute.setZ( n, attribute.getZ( ind ) );
- if ( itemSize >= 4 ) newAttribute.setW( n, attribute.getW( ind ) );
- }
- newGeometry.setAttribute( name, newAttribute );
- }
- const newMesh = new Mesh( newGeometry, newMaterial );
- object.add( newMesh );
- }
- return object;
- }
- /**
- * This function represents an alternative way to create 3D objects with multiple materials.
- * Normally, {@link BufferGeometry#groups} are used which might introduce issues e.g. when
- * exporting the object to a 3D format. This function accepts a geometry and an array of
- * materials and creates for each material a mesh that is added to a group.
- *
- * @param {BufferGeometry} geometry - The geometry.
- * @param {Array<Material>} materials - An array of materials.
- * @return {Group} A group representing a multi-material object.
- */
- function createMultiMaterialObject( geometry, materials ) {
- const group = new Group();
- for ( let i = 0, l = materials.length; i < l; i ++ ) {
- group.add( new Mesh( geometry, materials[ i ] ) );
- }
- return group;
- }
- /**
- * Executes a reducer function for each vertex of the given 3D object.
- * `reduceVertices()` returns a single value: the function's accumulated result.
- *
- * @param {Object3D} object - The 3D object that should be processed. It must have a
- * geometry with a `position` attribute.
- * @param {function(number,Vector3):number} func - The reducer function. First argument
- * is the current value, second argument the current vertex.
- * @param {any} initialValue - The initial value.
- * @return {any} The result.
- */
- function reduceVertices( object, func, initialValue ) {
- let value = initialValue;
- const vertex = new Vector3();
- object.updateWorldMatrix( true, true );
- object.traverseVisible( ( child ) => {
- const { geometry } = child;
- if ( geometry !== undefined ) {
- const { position } = geometry.attributes;
- if ( position !== undefined ) {
- for ( let i = 0, l = position.count; i < l; i ++ ) {
- if ( child.isMesh ) {
- child.getVertexPosition( i, vertex );
- } else {
- vertex.fromBufferAttribute( position, i );
- }
- if ( ! child.isSkinnedMesh ) {
- vertex.applyMatrix4( child.matrixWorld );
- }
- value = func( value, vertex );
- }
- }
- }
- } );
- return value;
- }
- /**
- * Sorts the instances of the given instanced mesh.
- *
- * @param {InstancedMesh} mesh - The instanced mesh to sort.
- * @param {function(number, number):number} compareFn - A custom compare function for the sort.
- */
- function sortInstancedMesh( mesh, compareFn ) {
- // store copy of instanced attributes for lookups
- const instanceMatrixRef = deepCloneAttribute( mesh.instanceMatrix );
- const instanceColorRef = mesh.instanceColor ? deepCloneAttribute( mesh.instanceColor ) : null;
- const attributeRefs = new Map();
- for ( const name in mesh.geometry.attributes ) {
- const attribute = mesh.geometry.attributes[ name ];
- if ( attribute.isInstancedBufferAttribute ) {
- attributeRefs.set( attribute, deepCloneAttribute( attribute ) );
- }
- }
- // compute sort order
- const tokens = [];
- for ( let i = 0; i < mesh.count; i ++ ) tokens.push( i );
- tokens.sort( compareFn );
- // apply sort order
- for ( let i = 0; i < tokens.length; i ++ ) {
- const refIndex = tokens[ i ];
- _matrix.fromArray( instanceMatrixRef.array, refIndex * mesh.instanceMatrix.itemSize );
- _matrix.toArray( mesh.instanceMatrix.array, i * mesh.instanceMatrix.itemSize );
- if ( mesh.instanceColor ) {
- _color.fromArray( instanceColorRef.array, refIndex * mesh.instanceColor.itemSize );
- _color.toArray( mesh.instanceColor.array, i * mesh.instanceColor.itemSize );
- }
- for ( const name in mesh.geometry.attributes ) {
- const attribute = mesh.geometry.attributes[ name ];
- if ( attribute.isInstancedBufferAttribute ) {
- const attributeRef = attributeRefs.get( attribute );
- attribute.setX( i, attributeRef.getX( refIndex ) );
- if ( attribute.itemSize > 1 ) attribute.setY( i, attributeRef.getY( refIndex ) );
- if ( attribute.itemSize > 2 ) attribute.setZ( i, attributeRef.getZ( refIndex ) );
- if ( attribute.itemSize > 3 ) attribute.setW( i, attributeRef.getW( refIndex ) );
- }
- }
- }
- }
- /**
- * Generator based alternative to {@link Object3D#traverse}.
- *
- * @param {Object3D} object - Object to traverse.
- * @yields {Object3D} Objects that passed the filter condition.
- */
- function* traverseGenerator( object ) {
- yield object;
- const children = object.children;
- for ( let i = 0, l = children.length; i < l; i ++ ) {
- yield* traverseGenerator( children[ i ] );
- }
- }
- /**
- * Generator based alternative to {@link Object3D#traverseVisible}.
- *
- * @param {Object3D} object Object to traverse.
- * @yields {Object3D} Objects that passed the filter condition.
- */
- function* traverseVisibleGenerator( object ) {
- if ( object.visible === false ) return;
- yield object;
- const children = object.children;
- for ( let i = 0, l = children.length; i < l; i ++ ) {
- yield* traverseVisibleGenerator( children[ i ] );
- }
- }
- /**
- * Generator based alternative to {@link Object3D#traverseAncestors}.
- *
- * @param {Object3D} object Object to traverse.
- * @yields {Object3D} Objects that passed the filter condition.
- */
- function* traverseAncestorsGenerator( object ) {
- const parent = object.parent;
- if ( parent !== null ) {
- yield parent;
- yield* traverseAncestorsGenerator( parent );
- }
- }
- export {
- createMeshesFromInstancedMesh,
- createMeshesFromMultiMaterialMesh,
- createMultiMaterialObject,
- reduceVertices,
- sortInstancedMesh,
- traverseGenerator,
- traverseVisibleGenerator,
- traverseAncestorsGenerator
- };
|