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

Box3: optimized matrix transform - ApplyMatrix4 #28348

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 16 additions & 22 deletions src/math/Box3.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,22 @@ class Box3 {
// transform of empty box is an empty box.
if ( this.isEmpty() ) return this;

// NOTE: I am using a binary pattern to specify all 2^3 combinations below
_points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
_points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
_points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
_points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
_points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
_points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
_points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
_points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111

this.setFromPoints( _points );
// compute box center and extents
this.getCenter( _center );
_extents.subVectors( this.max, _center );

// transform center
_center.applyMatrix4( matrix );

// transform extents as direction with abs matrix
const e = matrix.elements, n = _v0.copy( _extents );
_extents.x = Math.abs( e[ 0 ] ) * n.x + Math.abs( e[ 4 ] ) * n.y + Math.abs( e[ 8 ] ) * n.z;
_extents.y = Math.abs( e[ 1 ] ) * n.x + Math.abs( e[ 5 ] ) * n.y + Math.abs( e[ 9 ] ) * n.z;
_extents.z = Math.abs( e[ 2 ] ) * n.x + Math.abs( e[ 6 ] ) * n.y + Math.abs( e[ 10 ] ) * n.z;

// reconstruct bounds
this.min.copy( _center ).sub( _extents );
this.max.copy( _center ).add( _extents );

return this;

Expand All @@ -473,17 +478,6 @@ class Box3 {

}

const _points = [
/*@__PURE__*/ new Vector3(),
/*@__PURE__*/ new Vector3(),
/*@__PURE__*/ new Vector3(),
/*@__PURE__*/ new Vector3(),
/*@__PURE__*/ new Vector3(),
/*@__PURE__*/ new Vector3(),
/*@__PURE__*/ new Vector3(),
/*@__PURE__*/ new Vector3()
];

const _vector = /*@__PURE__*/ new Vector3();

const _box = /*@__PURE__*/ new Box3();
Expand Down