Skip to content

Commit 6ba07e3

Browse files
committed
Box3 now supports computing minimal bounds for setFromObject
1 parent 612f544 commit 6ba07e3

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

src/math/Box3.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class Box3 {
1919
setFromBufferAttribute( bufferAttribute: BufferAttribute ): this;
2020
setFromPoints( points: Vector3[] ): this;
2121
setFromCenterAndSize( center: Vector3, size: Vector3 ): this;
22-
setFromObject( object: Object3D ): this;
22+
setFromObject( object: Object3D, minimal?: boolean ): this;
2323
clone(): this;
2424
copy( box: Box3 ): this;
2525
makeEmpty(): this;
@@ -29,7 +29,7 @@ export class Box3 {
2929
expandByPoint( point: Vector3 ): this;
3030
expandByVector( vector: Vector3 ): this;
3131
expandByScalar( scalar: number ): this;
32-
expandByObject( object: Object3D ): this;
32+
expandByObject( object: Object3D, minimal?: boolean ): this;
3333
containsPoint( point: Vector3 ): boolean;
3434
containsBox( box: Box3 ): boolean;
3535
getParameter( point: Vector3, target: Vector3 ): Vector3;

src/math/Box3.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ Object.assign( Box3.prototype, {
144144

145145
},
146146

147-
setFromObject: function ( object ) {
147+
setFromObject: function ( object, minimal = false ) {
148148

149149
this.makeEmpty();
150150

151-
return this.expandByObject( object );
151+
return this.expandByObject( object, minimal );
152152

153153
},
154154

@@ -237,7 +237,7 @@ Object.assign( Box3.prototype, {
237237

238238
},
239239

240-
expandByObject: function ( object ) {
240+
expandByObject: function ( object, minimal = false ) {
241241

242242
// Computes the world-axis-aligned bounding box of an object (including its children),
243243
// accounting for both the object's, and children's, world transforms
@@ -248,24 +248,39 @@ Object.assign( Box3.prototype, {
248248

249249
if ( geometry !== undefined ) {
250250

251-
if ( geometry.boundingBox === null ) {
251+
if ( minimal && geometry.attributes != undefined && geometry.attributes.position !== undefined ) {
252252

253-
geometry.computeBoundingBox();
253+
const position = geometry.attributes.position;
254+
for ( let i = 0, l = position.count; i < l; i ++ ) {
254255

255-
}
256+
_vector.set( position.getX( i ), position.getY( i ), position.getZ( i ) );
257+
_vector.applyMatrix4( object.matrixWorld );
258+
this.expandByPoint( _vector );
259+
260+
}
261+
262+
} else {
263+
264+
if ( geometry.boundingBox === null ) {
256265

257-
_box.copy( geometry.boundingBox );
258-
_box.applyMatrix4( object.matrixWorld );
266+
geometry.computeBoundingBox();
259267

260-
this.union( _box );
268+
}
269+
270+
_box.copy( geometry.boundingBox );
271+
_box.applyMatrix4( object.matrixWorld );
272+
273+
this.union( _box );
274+
275+
}
261276

262277
}
263278

264279
const children = object.children;
265280

266281
for ( let i = 0, l = children.length; i < l; i ++ ) {
267282

268-
this.expandByObject( children[ i ] );
283+
this.expandByObject( children[ i ], minimal );
269284

270285
}
271286

test/unit/src/math/Box3.tests.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import { Mesh } from '../../../../src/objects/Mesh';
1010
import { BufferAttribute } from '../../../../src/core/BufferAttribute';
1111
import {
1212
BoxGeometry,
13-
BoxBufferGeometry
13+
BoxBufferGeometry,
1414
} from '../../../../src/geometries/BoxGeometry';
15+
import {
16+
SphereBufferGeometry,
17+
} from '../../../../src/geometries/SphereGeometry';
1518
import {
1619
negInf3,
1720
posInf3,
@@ -168,6 +171,30 @@ export default QUnit.module( 'Maths', () => {
168171

169172
} );
170173

174+
QUnit.test( "setFromObject/Minimal", ( assert ) => {
175+
176+
var a = new Box3( zero3.clone(), one3.clone() );
177+
var object = new Mesh( new SphereBufferGeometry( 1, 32, 32 ) );
178+
var child = new Mesh( new SphereBufferGeometry( 2, 32, 32 ) );
179+
object.add( child );
180+
181+
object.rotation.setFromVector3(new Vector3(0, 0, Math.PI / 4.0));
182+
183+
a.setFromObject( object );
184+
var rotatedBox = new Box3(
185+
new Vector3( - 2 * Math.SQRT2, - 2 * Math.SQRT2, - 2 ),
186+
new Vector3( 2 * Math.SQRT2, 2 * Math.SQRT2, 2 )
187+
);
188+
assert.ok( compareBox( a, rotatedBox ), "Passed!" );
189+
190+
a.setFromObject( object, true );
191+
var rotatedMinBox = new Box3(
192+
new Vector3( - 2, - 2, - 2 ),
193+
new Vector3( 2, 2, 2 )
194+
);
195+
assert.ok( compareBox( a, rotatedMinBox ), "Passed!" );
196+
} );
197+
171198
QUnit.test( "clone", ( assert ) => {
172199

173200

0 commit comments

Comments
 (0)