Mesh.MinMax breaks bounding box data

Hi!

That’s totally weird.

After calling to Mesh.MinMax coordinates of bounding box of the very first mesh get distorted.

The console reads:

BJS - [13:31:59]: Babylon.js v8.31.0 - WebGL2
==== just created ==== 
box.0 Object { minimumWorld: "{X: 2.5 Y: -0.5 Z: 4.5}", maximumWorld: "{X: 3.5 Y: 0.5 Z: 5.5}", extendSizeWorld: "{X: 0.5 Y: 0.5 Z: 0.5}", sizeDiag: 1.7320508075688772 }
box.1 Object { minimumWorld: "{X: 0.5 Y: -0.5 Z: 1.5}", maximumWorld: "{X: 1.5 Y: 0.5 Z: 2.5}", extendSizeWorld: "{X: 0.5 Y: 0.5 Z: 0.5}", sizeDiag: 1.7320508075688772 }
box.2 Object { minimumWorld: "{X: 4.5 Y: -0.5 Z: 7.5}", maximumWorld: "{X: 5.5 Y: 0.5 Z: 8.5}", extendSizeWorld: "{X: 0.5 Y: 0.5 Z: 0.5}", sizeDiag: 1.7320508075688772 }
==== after Mesh.MinMax ====
box.0 Object { minimumWorld: "{X: 0.5 Y: -0.5 Z: 1.5}", maximumWorld: "{X: 5.5 Y: 0.5 Z: 8.5}", extendSizeWorld: "{X: 0.5 Y: 0.5 Z: 0.5}", sizeDiag: 8.660254037844387 }
box.1 Object { minimumWorld: "{X: 0.5 Y: -0.5 Z: 1.5}", maximumWorld: "{X: 1.5 Y: 0.5 Z: 2.5}", extendSizeWorld: "{X: 0.5 Y: 0.5 Z: 0.5}", sizeDiag: 1.7320508075688772 }
box.2 Object { minimumWorld: "{X: 4.5 Y: -0.5 Z: 7.5}", maximumWorld: "{X: 5.5 Y: 0.5 Z: 8.5}", extendSizeWorld: "{X: 0.5 Y: 0.5 Z: 0.5}", sizeDiag: 1.7320508075688772 }

Apparently, they are reused as accumulator.
:exploding_head: That’s :exploding_head: insane! :exploding_head:

Indeed!

public static MinMax(meshes: AbstractMesh[]): { min: Vector3; max: Vector3 } {
        let minVector: Nullable<Vector3> = null;
        let maxVector: Nullable<Vector3> = null;

        for (const mesh of meshes) {
            const boundingInfo = mesh.getBoundingInfo();

            const boundingBox = boundingInfo.boundingBox;
            if (!minVector || !maxVector) {
                minVector = boundingBox.minimumWorld; // <-- reference to the first bbox!
                maxVector = boundingBox.maximumWorld;  // <-- reference to the first bbox!
            } else {
                minVector.minimizeInPlace(boundingBox.minimumWorld); // <-- modifying in place!
                maxVector.maximizeInPlace(boundingBox.maximumWorld);  // <-- modifying in place!
            }
        }

        if (!minVector || !maxVector) {
            return {
                min: Vector3.Zero(),
                max: Vector3.Zero(),
            };
        }

        return {
            min: minVector,
            max: maxVector,
        };
 }

But why haven’t it broke everything all around in Babylon?

Is there some another secret method of calculating meshes bounds that everyone uses?

Ok,
I’ll use Scene.getWorldExtends with filter

Thanks for the investigation, that’s a bug indeed. Here’s the fix:

1 Like