From 426c4ba044b0bff637a3931e42678eedfa12d52e Mon Sep 17 00:00:00 2001 From: Emmett Lalish Date: Mon, 24 Oct 2022 11:23:03 -0700 Subject: [PATCH] SceneUtils: Add reduceVertices(). (#22742) * updated * Update SceneUtils.js * Update SceneUtils.js Fix import. Co-authored-by: Michael Herzog --- examples/jsm/utils/SceneUtils.js | 49 +++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/examples/jsm/utils/SceneUtils.js b/examples/jsm/utils/SceneUtils.js index 86a098019839a0..1548ea35b63225 100644 --- a/examples/jsm/utils/SceneUtils.js +++ b/examples/jsm/utils/SceneUtils.js @@ -4,7 +4,8 @@ import { Color, Group, Matrix4, - Mesh + Mesh, + Vector3 } from 'three'; import { mergeGroups, deepCloneAttribute } from './BufferGeometryUtils.js'; @@ -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} @@ -199,5 +245,6 @@ export { createMeshesFromInstancedMesh, createMeshesFromMultiMaterialMesh, createMultiMaterialObject, + reduceVertices, sortInstancedMesh };