Browse Source

Merge pull request #17940 from zeux/faster-expand

Optimize Box3.expandByObject
Mr.doob 5 years ago
parent
commit
4a2e622885
2 changed files with 11 additions and 26 deletions
  1. 2 0
      docs/api/en/math/Box3.html
  2. 9 26
      src/math/Box3.js

+ 2 - 0
docs/api/en/math/Box3.html

@@ -138,6 +138,7 @@
 
 		Expands the boundaries of this box to include [page:Object3D object] and its children,
 		accounting for the object's, and children's, world transforms.
+		The function may result in a larger box than strictly necessary.
 
 		</p>
 
@@ -280,6 +281,7 @@
 
 		Computes the world-axis-aligned bounding box of an [page:Object3D] (including its children),
 		accounting for the object's, and children's, world transforms.
+		The function may result in a larger box than strictly necessary.
 
 		</p>
 

+ 9 - 26
src/math/Box3.js

@@ -41,6 +41,8 @@ function Box3( min, max ) {
 
 }
 
+var _box = new Box3();
+
 Object.assign( Box3.prototype, {
 
 	isBox3: true,
@@ -251,36 +253,17 @@ Object.assign( Box3.prototype, {
 
 		if ( geometry !== undefined ) {
 
-			if ( geometry.isGeometry ) {
-
-				var vertices = geometry.vertices;
-
-				for ( i = 0, l = vertices.length; i < l; i ++ ) {
-
-					_vector.copy( vertices[ i ] );
-					_vector.applyMatrix4( object.matrixWorld );
-
-					this.expandByPoint( _vector );
-
-				}
-
-			} else if ( geometry.isBufferGeometry ) {
+			if ( geometry.boundingBox === null ) {
 
-				var attribute = geometry.attributes.position;
+				geometry.computeBoundingBox();
 
-				if ( attribute !== undefined ) {
-
-					for ( i = 0, l = attribute.count; i < l; i ++ ) {
-
-						_vector.fromBufferAttribute( attribute, i ).applyMatrix4( object.matrixWorld );
-
-						this.expandByPoint( _vector );
-
-					}
+			}
 
-				}
+			_box.copy( geometry.boundingBox );
+			_box.applyMatrix4( object.matrixWorld );
 
-			}
+			this.expandByPoint( _box.min );
+			this.expandByPoint( _box.max );
 
 		}