Skip to content

Commit

Permalink
SceneUtils: Add reduceVertices(). (#22742)
Browse files Browse the repository at this point in the history
* updated

* Update SceneUtils.js

* Update SceneUtils.js

Fix import.

Co-authored-by: Michael Herzog <michael.herzog@human-interactive.org>
  • Loading branch information
elalish and Mugen87 authored Oct 24, 2022
1 parent a117613 commit 426c4ba
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion examples/jsm/utils/SceneUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Color,
Group,
Matrix4,
Mesh
Mesh,
Vector3
} from 'three';

import { mergeGroups, deepCloneAttribute } from './BufferGeometryUtils.js';
Expand Down Expand Up @@ -123,6 +124,51 @@ function createMultiMaterialObject( geometry, materials ) {

}

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 ++ ) {

vertex.fromBufferAttribute( position, i );

if ( child.isSkinnedMesh ) {

child.boneTransform( i, vertex );

} else {

vertex.applyMatrix4( child.matrixWorld );

}

value = func( value, vertex );

}

}

}

} );

return value;

}

/**
* @param {InstancedMesh}
* @param {function(int, int):int}
Expand Down Expand Up @@ -199,5 +245,6 @@ export {
createMeshesFromInstancedMesh,
createMeshesFromMultiMaterialMesh,
createMultiMaterialObject,
reduceVertices,
sortInstancedMesh
};

0 comments on commit 426c4ba

Please sign in to comment.