From 0532a8326195a8b7486c780d6f917dca54b5be3b Mon Sep 17 00:00:00 2001 From: WestLangley Date: Sat, 16 Nov 2019 17:55:05 -0500 Subject: [PATCH] Clean up --- examples/js/loaders/GLTFLoader.js | 11 ++--- examples/jsm/loaders/GLTFLoader.js | 72 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 2b67c6eb8774b6..5b92a196cebccc 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -2285,12 +2285,13 @@ THREE.GLTFLoader = ( function () { var max = accessor.max; box.set( - new THREE.Vector3( min[0], min[1], min[2] ), - new THREE.Vector3( max[0], max[1], max[2] ) ); + new THREE.Vector3( min[ 0 ], min[ 1 ], min[ 2 ] ), + new THREE.Vector3( max[ 0 ], max[ 1 ], max[ 2 ] ) ); } else { return; + } var targets = primitiveDef.targets; @@ -2310,9 +2311,9 @@ THREE.GLTFLoader = ( function () { var max = accessor.max; // we need to get max of absolute components because target weight is [-1,1] - vector.setX( Math.max( Math.abs( min[0] ), Math.abs( max[0] ) ) ); - vector.setY( Math.max( Math.abs( min[1] ), Math.abs( max[1] ) ) ); - vector.setZ( Math.max( Math.abs( min[2] ), Math.abs( max[2] ) ) ); + vector.setX( Math.max( Math.abs( min[ 0 ] ), Math.abs( max[ 0 ] ) ) ); + vector.setY( Math.max( Math.abs( min[ 1 ] ), Math.abs( max[ 1 ] ) ) ); + vector.setZ( Math.max( Math.abs( min[ 2 ] ), Math.abs( max[ 2 ] ) ) ); box.expandByVector( vector ); diff --git a/examples/jsm/loaders/GLTFLoader.js b/examples/jsm/loaders/GLTFLoader.js index 9b21c073baa3a7..1b0ff9889100b9 100644 --- a/examples/jsm/loaders/GLTFLoader.js +++ b/examples/jsm/loaders/GLTFLoader.js @@ -9,6 +9,7 @@ import { AnimationClip, Bone, + Box3, BufferAttribute, BufferGeometry, ClampToEdgeWrapping, @@ -59,12 +60,14 @@ import { ShaderMaterial, Skeleton, SkinnedMesh, + Sphere, SpotLight, TextureLoader, TriangleFanDrawMode, TriangleStripDrawMode, UniformsUtils, Vector2, + Vector3, VectorKeyframeTrack, VertexColors, sRGBEncoding @@ -2331,6 +2334,73 @@ var GLTFLoader = ( function () { }; + /** + * @param {BufferGeometry} geometry + * @param {GLTF.Primitive} primitiveDef + * @param {GLTFParser} parser + */ + function computeBounds( geometry, primitiveDef, parser ) { + + var attributes = primitiveDef.attributes; + + var box = new Box3(); + + if ( attributes.POSITION !== undefined ) { + + var accessor = parser.json.accessors[ attributes.POSITION ]; + var min = accessor.min; + var max = accessor.max; + + box.set( + new Vector3( min[ 0 ], min[ 1 ], min[ 2 ] ), + new Vector3( max[ 0 ], max[ 1 ], max[ 2 ] ) ); + + } else { + + return; + + } + + var targets = primitiveDef.targets; + + if ( targets !== undefined ) { + + var vector = new Vector3(); + + for ( var i = 0, il = targets.length; i < il; i ++ ) { + + var target = targets[ i ]; + + if ( target.POSITION !== undefined ) { + + var accessor = parser.json.accessors[ target.POSITION ]; + var min = accessor.min; + var max = accessor.max; + + // we need to get max of absolute components because target weight is [-1,1] + vector.setX( Math.max( Math.abs( min[ 0 ] ), Math.abs( max[ 0 ] ) ) ); + vector.setY( Math.max( Math.abs( min[ 1 ] ), Math.abs( max[ 1 ] ) ) ); + vector.setZ( Math.max( Math.abs( min[ 2 ] ), Math.abs( max[ 2 ] ) ) ); + + box.expandByVector( vector ); + + } + + } + + } + + geometry.boundingBox = box; + + var sphere = new Sphere(); + + box.getCenter( sphere.center ); + sphere.radius = box.min.distanceTo( box.max ) / 2; + + geometry.boundingSphere = sphere; + + } + /** * @param {BufferGeometry} geometry * @param {GLTF.Primitive} primitiveDef @@ -2379,6 +2449,8 @@ var GLTFLoader = ( function () { assignExtrasToUserData( geometry, primitiveDef ); + computeBounds( geometry, primitiveDef, parser ); + return Promise.all( pending ).then( function () { return primitiveDef.targets !== undefined