I'm running into an issue trying to get the bounding box of the geometry of a model after it is loaded with the OBJLoader. So far I have:
var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {
for ( var i = 0, l = object.children.length; i < l; i ++ ) {
geometry = object.children[0].geometry;
bBox = geometry.computeBoundingBox();
console.log("have a box of "+bBox);
}
//...rest of function
But the bBox is undefined when I write it to the console. Is geometry not the right property to access in this case?
I think something like this should do the trick:
var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {
THREE.SceneUtils.traverseHierarchy( object, function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.geometry.computeBoundingBox();
}
}
}
This answer combines the last for the newest version of THREE.js
var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {
object.traverse(function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.geometry.computeBoundingBox();
object.bBox = child.geometry.boundingBox;//<-- Actually get the variable
}
});
};
Now you can access the dimensions of the mesh by typing object.bBox.max.x to get x for instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With