I want to get the bounding box of a GLTF model that covers its root and its children.
This is what i did,
loader.load('assets/a1/A1.glb',
(gltf) => {
const root = gltf.scene;
const box = new THREE.Box3().setFromObject(root);
const boxSize = box.getSize(new THREE.Vector3()).length();
const boxCenter = box.getCenter(new THREE.Vector3());
});
But unfortunately, it doesnt compute or include the child meshes.
How to do this correctlt?
Hm I’m pretty sure that does include child meshes, I use the same code in my viewer:
… can you share an example where it’s broken? The only case I know of is with SkinnedMesh, it doesn’t including skinning transforms in the bounding box.
Here is the model i am loading
https://drive.google.com/drive/folders/1tXTW57mrLBLQ3T2JzA2lXLQjPqO7dkpN?usp=sharing
displaying the size of the box for root results to 0.0000022626528104191175
but displaying the box of each child mesh it displays
child 1 = 0.22626528322871423
child 2 = 0.057509041719473984
so if bounding box gets the larger size its it should be in the box, the value should be the 0.226 or around that
here is a strip version of the code
loader.load('assets/a1/A1.glb',
(gltf) => {
gltf.scene.traverse( function ( child )
{
if ( child.isMesh )
{
const box = new THREE.Box3().setFromObject(child);
const boxSize = box.getSize(new THREE.Vector3()).length();
console.log(boxSize);
}
} );
const root = gltf.scene;
const box = new THREE.Box3().setFromObject(root);
const boxSize = box.getSize(new THREE.Vector3()).length();
const boxCenter = box.getCenter(new THREE.Vector3());
});
I have a guess at what’s happening here… try calling gltf.scene.updateMatrixWorld( true )
before traversing and calculating sizes, does that change the result?
The armature has a 1/100
scale on it, and everything else is nested under that.