Browse Source

BufferGeometry: Honor morph targets when computing bounding volumes.

Mugen87 6 năm trước cách đây
mục cha
commit
c04ee58d18
1 tập tin đã thay đổi với 80 bổ sung14 xóa
  1. 80 14
      src/core/BufferGeometry.js

+ 80 - 14
src/core/BufferGeometry.js

@@ -601,35 +601,59 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 
 
 	computeBoundingBox: function () {
 	computeBoundingBox: function () {
 
 
-		if ( this.boundingBox === null ) {
+		var box = new Box3();
 
 
-			this.boundingBox = new Box3();
+		return function computeBoundingBox() {
 
 
-		}
+			if ( this.boundingBox === null ) {
 
 
-		var position = this.attributes.position;
+				this.boundingBox = new Box3();
 
 
-		if ( position !== undefined ) {
+			}
+
+			var position = this.attributes.position;
+			var morphAttributesPosition = this.morphAttributes.position;
 
 
-			this.boundingBox.setFromBufferAttribute( position );
+			if ( position !== undefined ) {
 
 
-		} else {
+				this.boundingBox.setFromBufferAttribute( position );
 
 
-			this.boundingBox.makeEmpty();
+				// process morph attributes if present
 
 
-		}
+				if ( morphAttributesPosition ) {
 
 
-		if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
+					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
 
 
-			console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
+						var morphAttribute = morphAttributesPosition[ i ];
+						box.setFromBufferAttribute( morphAttribute );
 
 
-		}
+						this.boundingBox.expandByPoint( box.min );
+						this.boundingBox.expandByPoint( box.max );
 
 
-	},
+					}
+
+				}
+
+			} else {
+
+				this.boundingBox.makeEmpty();
+
+			}
+
+			if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
+
+				console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
+
+			}
+
+		};
+
+	}(),
 
 
 	computeBoundingSphere: function () {
 	computeBoundingSphere: function () {
 
 
 		var box = new Box3();
 		var box = new Box3();
+		var boxMorphTargets = new Box3();
 		var vector = new Vector3();
 		var vector = new Vector3();
 
 
 		return function computeBoundingSphere() {
 		return function computeBoundingSphere() {
@@ -641,15 +665,35 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 			}
 			}
 
 
 			var position = this.attributes.position;
 			var position = this.attributes.position;
+			var morphAttributesPosition = this.morphAttributes.position;
 
 
 			if ( position ) {
 			if ( position ) {
 
 
+				// first, find the center of the bounding sphere
+
 				var center = this.boundingSphere.center;
 				var center = this.boundingSphere.center;
 
 
 				box.setFromBufferAttribute( position );
 				box.setFromBufferAttribute( position );
+
+				// process morph attributes if present
+
+				if ( morphAttributesPosition ) {
+
+					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
+
+						var morphAttribute = morphAttributesPosition[ i ];
+						boxMorphTargets.setFromBufferAttribute( morphAttribute );
+
+						box.expandByPoint( boxMorphTargets.min );
+						box.expandByPoint( boxMorphTargets.max );
+
+					}
+
+				}
+
 				box.getCenter( center );
 				box.getCenter( center );
 
 
-				// hoping to find a boundingSphere with a radius smaller than the
+				// second, try to find a boundingSphere with a radius smaller than the
 				// boundingSphere of the boundingBox: sqrt(3) smaller in the best case
 				// boundingSphere of the boundingBox: sqrt(3) smaller in the best case
 
 
 				var maxRadiusSq = 0;
 				var maxRadiusSq = 0;
@@ -663,6 +707,28 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 
 
 				}
 				}
 
 
+				// process morph attributes if present
+
+				if ( morphAttributesPosition ) {
+
+					for ( var i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
+
+						var morphAttribute = morphAttributesPosition[ i ];
+
+						for ( var j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
+
+							vector.x = morphAttribute.getX( i );
+							vector.y = morphAttribute.getY( i );
+							vector.z = morphAttribute.getZ( i );
+
+							maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
+
+						}
+
+					}
+
+				}
+
 				this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
 				this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
 
 
 				if ( isNaN( this.boundingSphere.radius ) ) {
 				if ( isNaN( this.boundingSphere.radius ) ) {