瀏覽代碼

Optimize Box3.expandByObject

Instead of recomputing the bounding box every time, we now use the
cached bounding box from the object, and transform it by the world
transform.

Note that this results in slightly less accurate bounding box compared
to the previous implementation. This seems like a fair tradeoff -
it's possible to get the precise box by using Box3.setFromBufferAttribute
if desired.

In addition to making the box more precise, this honors morph target
data and supports arbitrary future improvements to object bounding box
computation automatically.
Arseny Kapoulkine 5 年之前
父節點
當前提交
47630c07c3
共有 1 個文件被更改,包括 7 次插入26 次删除
  1. 7 26
      src/math/Box3.js

+ 7 - 26
src/math/Box3.js

@@ -251,36 +251,17 @@ Object.assign( Box3.prototype, {
 
 		if ( geometry !== undefined ) {
 
-			if ( geometry.isGeometry ) {
+			if ( geometry.boundingBox === null ) {
 
-				var vertices = geometry.vertices;
+				geometry.computeBoundingBox();
 
-				for ( i = 0, l = vertices.length; i < l; i ++ ) {
-
-					_vector.copy( vertices[ i ] );
-					_vector.applyMatrix4( object.matrixWorld );
-
-					this.expandByPoint( _vector );
-
-				}
-
-			} else if ( geometry.isBufferGeometry ) {
-
-				var attribute = geometry.attributes.position;
-
-				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 );
 
 		}