Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Box3.expandByObject #17940

Merged
merged 4 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -279,6 +280,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