فهرست منبع

Merge pull request #18907 from zeux/gltf-smaller-bounds

GLTFLoader: Use a smaller bounding box for morph targets
Mr.doob 5 سال پیش
والد
کامیت
2480ed0aa5
2فایلهای تغییر یافته به همراه18 افزوده شده و 2 حذف شده
  1. 9 1
      examples/js/loaders/GLTFLoader.js
  2. 9 1
      examples/jsm/loaders/GLTFLoader.js

+ 9 - 1
examples/js/loaders/GLTFLoader.js

@@ -2225,6 +2225,7 @@ THREE.GLTFLoader = ( function () {
 
 		if ( targets !== undefined ) {
 
+			var maxDisplacement = new THREE.Vector3();
 			var vector = new THREE.Vector3();
 
 			for ( var i = 0, il = targets.length; i < il; i ++ ) {
@@ -2246,7 +2247,11 @@ THREE.GLTFLoader = ( function () {
 						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 );
+						// Note: this assumes that the sum of all weights is at most 1. This isn't quite correct - it's more conservative
+						// to assume that each target can have a max weight of 1. However, for some use cases - notably, when morph targets
+						// are used to implement key-frame animations and as such only two are active at a time - this results in very large
+						// boxes. So for now we make a box that's sometimes a touch too small but is hopefully mostly of reasonable size.
+						maxDisplacement.max( vector );
 
 					} else {
 
@@ -2258,6 +2263,9 @@ THREE.GLTFLoader = ( function () {
 
 			}
 
+			// As per comment above this box isn't conservative, but has a reasonable size for a very large number of morph targets.
+			box.expandByVector( maxDisplacement );
+
 		}
 
 		geometry.boundingBox = box;

+ 9 - 1
examples/jsm/loaders/GLTFLoader.js

@@ -2288,6 +2288,7 @@ var GLTFLoader = ( function () {
 
 		if ( targets !== undefined ) {
 
+			var maxDisplacement = new Vector3();
 			var vector = new Vector3();
 
 			for ( var i = 0, il = targets.length; i < il; i ++ ) {
@@ -2309,7 +2310,11 @@ var GLTFLoader = ( function () {
 						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 );
+						// Note: this assumes that the sum of all weights is at most 1. This isn't quite correct - it's more conservative
+						// to assume that each target can have a max weight of 1. However, for some use cases - notably, when morph targets
+						// are used to implement key-frame animations and as such only two are active at a time - this results in very large
+						// boxes. So for now we make a box that's sometimes a touch too small but is hopefully mostly of reasonable size.
+						maxDisplacement.max( vector );
 
 					} else {
 
@@ -2321,6 +2326,9 @@ var GLTFLoader = ( function () {
 
 			}
 
+			// As per comment above this box isn't conservative, but has a reasonable size for a very large number of morph targets.
+			box.expandByVector( maxDisplacement );
+
 		}
 
 		geometry.boundingBox = box;