Get boundingBox of a gltf loaded mesh incorrectly?

Hi, just begin to explore babylon.js.
Basically, I want to load mesh from gltf, and resize it according to its height.
A gltf file contains one root mesh which composed of some children meshes.
My solution is to find the root mesh by check if it has no parent, then apply scaling, it works.
However, I have to pick the correct factor by manually inspecting the mesh, not very convenient.
The better way is that get the height of mesh, say it’s 200, and expected height is 10, and the factor now is 10 / 200 = 0.05.
The problem is, after doing many researches on google, I still don’t know how to get the height of mesh.
I tried to use getBoundingInfo, but it gives me all zero result, here is a PG to show what I meant

Thanks in advance.

That’s because the root mesh is a simple dummy mesh without any real geometry.
I guess this is one way of doing it, just not sure it’s the best way:

Thanks, also root.getChildMeshes().map(…) can also get minimum and maximum of a mesh.

@Raggar, after more digging into the code, I notice that you put a line

m.position.y -= 0.22

if I commented this line, the box would be a little higher than the mesh.
I suspect the reason causing this is that mesh’s origin is not the center of the mesh’s geometry.
Check this PG

So how do we reset the origin to the center of mesh by code? Is it even possible to do this in babylon.js?

I got into this trouble as well, especially importing gltf mesh downloaded from sketchfab, most of them seem to have their origin in variouse positions, rather than the center of mesh.

You can use the boundingInfo to get the center of the boundingBox/Sphere in world space:

This is the center of the newly created mesh and all the imported model parts.
You can either use this mesh as your root-mesh, parenting other meshes to it(Or create a transform node). You can also take your original parent and change its pivot point to reflect this new position.

Just a note that if you want to reduce a little of the code you can use BABYLON.Vector3.Minimize/Maximize

Playground -

Also, another note is if you export your model from say blender and its scaling is not set to 1, you might get different values than you would expect. Say in blender you have a box with a scaling set to 0.5 and its dimensions are 10x10x10. The bounding info you get will be 2x that dimension, though the model will still show at that 0.5 scaling I believe.

1 Like