浏览代码

Merge pull request #5109 from shapespark/BufferGeometry-dedup

Remove duplicated bounding box computation from the BufferGeometry
Mr.doob 11 年之前
父节点
当前提交
784dd66fd0
共有 1 个文件被更改,包括 19 次插入51 次删除
  1. 19 51
      src/core/BufferGeometry.js

+ 19 - 51
src/core/BufferGeometry.js

@@ -230,80 +230,48 @@ THREE.BufferGeometry.prototype = {
 
 	computeBoundingBox: function () {
 
-		if ( this.boundingBox === null ) {
-
-			this.boundingBox = new THREE.Box3();
-
-		}
+		var vector = new THREE.Vector3();
 
-		var positions = this.attributes[ 'position' ].array;
+		return function () {
 
-		if ( positions ) {
+			if ( this.boundingBox === null ) {
 
-			var bb = this.boundingBox;
+				this.boundingBox = new THREE.Box3();
 
-			if ( positions.length >= 3 ) {
-				bb.min.x = bb.max.x = positions[ 0 ];
-				bb.min.y = bb.max.y = positions[ 1 ];
-				bb.min.z = bb.max.z = positions[ 2 ];
 			}
 
-			for ( var i = 3, il = positions.length; i < il; i += 3 ) {
-
-				var x = positions[ i ];
-				var y = positions[ i + 1 ];
-				var z = positions[ i + 2 ];
-
-				// bounding box
-
-				if ( x < bb.min.x ) {
-
-					bb.min.x = x;
-
-				} else if ( x > bb.max.x ) {
-
-					bb.max.x = x;
-
-				}
-
-				if ( y < bb.min.y ) {
-
-					bb.min.y = y;
-
-				} else if ( y > bb.max.y ) {
-
-					bb.max.y = y;
-
-				}
+			var positions = this.attributes[ 'position' ].array;
 
-				if ( z < bb.min.z ) {
+			if ( positions ) {
 
-					bb.min.z = z;
+				var bb = this.boundingBox;
+				bb.makeEmpty();
 
-				} else if ( z > bb.max.z ) {
+				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
 
-					bb.max.z = z;
+					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
+					bb.expandByPoint( vector );
 
 				}
 
 			}
 
-		}
+			if ( positions === undefined || positions.length === 0 ) {
 
-		if ( positions === undefined || positions.length === 0 ) {
+				this.boundingBox.min.set( 0, 0, 0 );
+				this.boundingBox.max.set( 0, 0, 0 );
 
-			this.boundingBox.min.set( 0, 0, 0 );
-			this.boundingBox.max.set( 0, 0, 0 );
+			}
 
-		}
+			if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
 
-		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.' );
 
-			console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.' );
+			}
 
 		}
 
-	},
+	}(),
 
 	computeBoundingSphere: function () {