Skip to content

Commit

Permalink
added js version
Browse files Browse the repository at this point in the history
  • Loading branch information
elalish committed Apr 4, 2022
1 parent 234e947 commit c2c28c3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
50 changes: 50 additions & 0 deletions examples/js/utils/SceneUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,55 @@

}

function reduceVertices( func, initialValue ) {

let value = initialValue;
const vertex = new THREE.Vector3();
this.traverseVisible( object => {

object.updateWorldMatrix( false, false );
const {
geometry
} = object;

if ( geometry !== undefined ) {

const {
position
} = geometry.attributes;

if ( position !== undefined ) {

const scale = THREE.getNormalizedComponentScale( position );

for ( let i = 0, l = position.count; i < l; i ++ ) {

vertex.fromBufferAttribute( position, i );
vertex.multiplyScalar( scale );

if ( object.isSkinnedMesh ) {

object.boneTransform( i, vertex );

} else {

vertex.applyMatrix4( object.matrixWorld );

}

value = func( value, vertex );

}

}

}

} );
return value;

}

function detach( child, parent, scene ) {

console.warn( 'THREE.SceneUtils: detach() has been deprecated. Use scene.attach( child ) instead.' );
Expand All @@ -115,5 +164,6 @@
THREE.SceneUtils.createMeshesFromMultiMaterialMesh = createMeshesFromMultiMaterialMesh;
THREE.SceneUtils.createMultiMaterialObject = createMultiMaterialObject;
THREE.SceneUtils.detach = detach;
THREE.SceneUtils.reduceVertices = reduceVertices;

} )();
6 changes: 4 additions & 2 deletions examples/jsm/utils/SceneUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {
Group,
Mesh,
BufferAttribute,
BufferGeometry
BufferGeometry,
Vector3
} from 'three';

import { mergeGroups } from './BufferGeometryUtils.js';
import { getNormalizedComponentScale } from '../loaders/GLTFLoader.js'

function createMeshesFromInstancedMesh( instancedMesh ) {

Expand Down Expand Up @@ -123,7 +125,7 @@ function reduceVertices( func, initialValue ) {
let value = initialValue;
const vertex = new Vector3();

this.traverse( ( object ) => {
this.traverseVisible( ( object ) => {

object.updateWorldMatrix( false, false );

Expand Down

0 comments on commit c2c28c3

Please sign in to comment.