WestLangley 5 år sedan
förälder
incheckning
0532a83261
2 ändrade filer med 78 tillägg och 5 borttagningar
  1. 6 5
      examples/js/loaders/GLTFLoader.js
  2. 72 0
      examples/jsm/loaders/GLTFLoader.js

+ 6 - 5
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 );
 

+ 72 - 0
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