Reputation: 525
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?
Upvotes: 2
Views: 7807
Reputation: 6089
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.
Upvotes: 4
Reputation: 162
@mrdoob's answer was correct but due to three.js API changes (as of r52), it should look like this now:
var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {
object.traverse(function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.geometry.computeBoundingBox();
}
});
};
Upvotes: 1
Reputation: 19602
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();
}
}
}
Upvotes: 5
Reputation: 525
Based on mrdoob's traverse function, I was able to get to a bounding box with the following:
var loader = new THREE.OBJLoader();
loader.load( mURL, function ( object ) {
THREE.SceneUtils.traverseHierarchy(object, function(child){
if(child instanceof THREE.Mesh){
for (var i in child) {
if(i == "geometry"){
var geo = child[i];
geo.computeBoundingBox();
var bBox = geo.boundingBox;
}
}
}
});
///rest of loading function here
});
Upvotes: 0