How to get the whole scene bounding box

Hi,

I have a scene that might contain multiple children / meshes. How can I get the pivot point and center of the scene? Currently I’m using getWorldExtends() which returns the min and max of the scene world but I need more data from the inner BoundingInfo like the centerWorld and more data about the meshes in relative to the world like the pivot point but the total pivot.

I’ve created a box with the vector the scene.getWorldExtends() has returened but it’s not positioned correctly:

This is how:

    getBoundingBoxByObject(scene) {
        let minmax = scene.getWorldExtends();
        return {
            x: Math.abs(minmax.max.x - minmax.min.x),
            y: Math.abs(minmax.max.y - minmax.min.y),
            z: Math.abs(minmax.max.z - minmax.min.z)
        };
    }

    addBoundingBix() {
        const vector = this.getBoundingBoxByObject(this.scene);
        const box = BABYLON.MeshBuilder.CreateBox('box', {
                width: vector.x,
                height: vector.y,
                length: vector.z
        }, this.scene);
    }

I need this box to be in the same place as the model. I’ve tried to do this:

box.position.y = minmax.max.y / 2;

And it looks good but axis x and z aren’t aligned with the model when applying the same logic.

Thanks.

Hello and welcome to the Babylon community!

getWorldExtends returns the minimum and maximum positions occupied by the objects from the scene. When you have this information, you can get the center position by doing: (min + max) / 2, which is just averaging these positions.

Bounding Box Example 1 | Babylon.js Playground (babylonjs.com)

Some more techniques are described here: Drawing Bounding Boxes | Babylon.js Documentation (babylonjs.com)

4 Likes

Thanks @carolhmj for the excellent solution and the warm welcome!

1 Like