Box3 from mesh has offset

Hi there!

I have a model with a lot of nested objects and their meshes in it. When I generate a boundingBox or a Box3 for one of the meshes, the box is totally offset from the position of the mesh.

boundingBox approach:

let mesh = scene.getObjectByName("Cube");
mesh.geometry.computeBoundingBox();
var boundingHelper = new THREE.Box3Helper(boundingBoxTest, 0x00ff00);
scene.add(boundingHelper);


Box3 approach:

let mesh = scene.getObjectByName("Cube");
boundingBoxTest = new THREE.Box3().setFromObject(mesh);
var boundingHelper = new THREE.Box3Helper(boundingBoxTest, 0x00ff00);
scene.add(boundingHelper);

Can someone help please? :raising_hand_man:

how did you position your Cube?

Thank you for you reply! :raised_hands:

I don’t change the position of the model, just load the GLB file via:

       loader = new THREE.GLTFLoader(manager);

        if (window.XMLHttpRequest) {
            // IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // IE5, IE6  - next line supports these dinosaurs
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
                txt = xmlhttp.responseText;
                if (txt.length == xmlhttp.responseText.length) {
                    loader.load(txt, function (gltf) { })
                    loader.load(txt, onLoadObjects);
                }
            }
        };
        xmlhttp.open("GET", 'singleModel.txt', true); //http://localhost/app/models.txt
        xmlhttp.send();

and

    function onLoadObjects(gltf) {

        var object = gltf.scene;

        objects.push(object);

        scene.add(gltf.scene);
    }

As the cube is only a substitute for a mesh in the actual model (which I sadly can’t show or share) the hierarchy looks like this:
Unbenannt3

I don’t know, i cant reproduce it.

1 Like

Thank you for trying! :slight_smile:

I found my mistake, I used the LoadingManager .onLoad function to compute the box and I’m guessing that it executed before the model was fully integrated into the DOM which led to the wrong positions.

If I now call the generation of the box via a button click after the DOM is set up it works! :raised_hands: