Skip to content

Commit

Permalink
Merge pull request #17940 from zeux/faster-expand
Browse files Browse the repository at this point in the history
Optimize Box3.expandByObject
  • Loading branch information
mrdoob authored Nov 20, 2019
2 parents ad47577 + 57dab2c commit 4a2e622
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
2 changes: 2 additions & 0 deletions docs/api/en/math/Box3.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ <h3>[method:Box3 expandByObject]( [param:Object3D object] )</h3>

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>

Expand Down Expand Up @@ -280,6 +281,7 @@ <h3>[method:Box3 setFromObject]( [param:Object3D object] )</h3>

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>

Expand Down
35 changes: 9 additions & 26 deletions src/math/Box3.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function Box3( min, max ) {

}

var _box = new Box3();

Object.assign( Box3.prototype, {

isBox3: true,
Expand Down Expand Up @@ -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 );

}

Expand Down

0 comments on commit 4a2e622

Please sign in to comment.