浏览代码

Merge pull request #17939 from zeux/gltf-bounds

GLTFLoader: Pre-compute geometry bounds using attribute min/max
Mr.doob 5 年之前
父节点
当前提交
fb405ea69e
共有 1 个文件被更改,包括 68 次插入0 次删除
  1. 68 0
      examples/js/loaders/GLTFLoader.js

+ 68 - 0
examples/js/loaders/GLTFLoader.js

@@ -2267,6 +2267,72 @@ THREE.GLTFLoader = ( function () {
 
 
 	};
 	};
 
 
+	/**
+	 * @param {THREE.BufferGeometry} geometry
+	 * @param {GLTF.Primitive} primitiveDef
+	 * @param {GLTFParser} parser
+	 */
+	function computeBounds( geometry, primitiveDef, parser ) {
+
+		var attributes = primitiveDef.attributes;
+
+		var box = new THREE.Box3();
+
+		if ( attributes.POSITION !== undefined ) {
+
+			var accessor = parser.json.accessors[ attributes.POSITION ];
+			var min = accessor.min;
+			var max = accessor.max;
+
+			box.set(
+				new THREE.Vector3( min[0], min[1], min[2] ),
+				new THREE.Vector3( max[0], max[1], max[2] ) );
+
+		} else {
+
+			return;
+		}
+
+		var targets = primitiveDef.targets;
+
+		if ( targets !== undefined ) {
+
+			var vector = new THREE.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 THREE.Sphere();
+
+		box.getCenter( sphere.center );
+		sphere.radius = box.min.distanceTo( box.max ) / 2;
+
+		geometry.boundingSphere = sphere;
+
+	}
+
 	/**
 	/**
 	 * @param {THREE.BufferGeometry} geometry
 	 * @param {THREE.BufferGeometry} geometry
 	 * @param {GLTF.Primitive} primitiveDef
 	 * @param {GLTF.Primitive} primitiveDef
@@ -2315,6 +2381,8 @@ THREE.GLTFLoader = ( function () {
 
 
 		assignExtrasToUserData( geometry, primitiveDef );
 		assignExtrasToUserData( geometry, primitiveDef );
 
 
+		computeBounds( geometry, primitiveDef, parser );
+
 		return Promise.all( pending ).then( function () {
 		return Promise.all( pending ).then( function () {
 
 
 			return primitiveDef.targets !== undefined
 			return primitiveDef.targets !== undefined