Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three JS - BoundingBox not being updated after performing rotations

I need to find out the bounding box of a geometry after applying rotations on it.

Code to rotate - taken from sample editor of Three JS

object.rotation.x = xRadians;
object.rotation.y = yRdians;
object.rotation.z = zRadians

This rotates the object just fine.

Now I need to get the updated bounding box

Code to get the bounding Box

var minX = parseFloat(object.boundingBox.min.x);
var minY = parseFloat(object.boundingBox.min.y);
var minZ = parseFloat(object.boundingBox.min.z);

I keep getting the same values in minX-Z no matter what the rotation is. What is the right way of getting the updated bounding box?

I am using r-66.

I also tried using:

var radians = x * Math.PI / 180;
var axisX = new THREE.Vector3(1, 0, 0);
var matrix = new THREE.Matrix4().makeRotationAxis(axisX, radians);
geometry.applyMatrix(matrix);

This method performs the relative rotation and also updates the bounding box correctly but I do not want relative rotation. The first approach is what I am looking for but that does not update the bounding box of the object.

Any ideas? Thanks!

like image 208
user3472487 Avatar asked Feb 14 '23 09:02

user3472487


2 Answers

Box3.setFromObject( object ) computes the world-axis-aligned bounding box of an object (including its children), accounting for both the object's, and childrens', world transforms.

var box = new THREE.Box3().setFromObject( object );

three.js r.66

like image 155
WestLangley Avatar answered Feb 16 '23 04:02

WestLangley


var box = new THREE.Box3().setFromObject( object );

will return the world coordinates, so you need to subtract them from it:

var bbox = new THREE.Box3().setFromObject(object);
bbox.min.sub(object.position);
bbox.max.sub(object.position);

This will give you the correct bounding box relative to the object.

like image 32
Super Unicorn Avatar answered Feb 16 '23 03:02

Super Unicorn