Mesh not importing in the center of scene

Hello everyone,
My model is loading 1000 meters (I think 1 unit in my scene is 1 meter) away from the (0,0,0) point and it’s rotated 90 degres. So I hardcoded it and when I’m loading model I am doing those fixes:

Promise.all(
      tilesArray.map((asset) =>
        BABYLON.SceneLoader.ImportMeshAsync(
          "",
          `${MESH_BASE_URL}/${asset}/`,
          "model.glb",
          scene,
          handleMeshLoading
        ).then((mesh) => {
          // FIRST FIX
          // First rotate tower 90 degrees, model originally is set horizontally
          mesh.meshes[0].rotate(
            BABYLON.Axis.X,
            Math.PI / 2,
            BABYLON.Space.LOCAL
          );
          // SECOND FIX
          // Then move it to (0,0,0) position (kind of hardcode here - different models can be in different positions at start)
           mesh.meshes[0].translate(BABYLON.Axis.Y, -1007, BABYLON.Space.WORLD);

          return mesh;
        })
      )
    )
      .then(this.handleComplete)

Is there any way to move the camera to the model so I shouldn’t move it with given offset (-1007)? I’ll have to handle multiple model’s which can have differet offsets.

When I do something like this:

camera.position = mesh.meshes[0].position

it doesn’t make anything because mesh.meshes[0].position shows the LOCAL position of this mesh and it’s (0,0,0) but in global axis it’s (0, -1007, 0) .

you can set the camera’s target using camera.setTarget(meshOrVector);

Would changing the pivot point work for you? https://www.babylonjs-playground.com/#8LFTCH#184

You could also re-center the mesh with the same boundingbox data and then bake the transformation
https://www.babylonjs-playground.com/#8LFTCH#197

This solution works the best

Would changing the pivot point work for you? https://www.babylonjs-playground.com/#8LFTCH#184

But I changed it to

let a2 = mesh.meshes[1];
freeCamera.position = a2.getBoundingInfo().boundingBox.centerWorld
arcRotateCamera.position = a2.getBoundingInfo().boundingBox.centerWorld
arcRotateCamera.target = a2.getBoundingInfo().boundingBox.centerWorld

It’s good bcuz a2.getBoundingInfo().boundingBox.centerWorld takes global position of mesh

Cameras now jumps from mesh to mesh (till every mesh is loaded) so I guess what I’m going to do is just take first mesh position and then stop taking new positions. And move it back a lil bit from it.

Thanks.

1 Like

Here’s a function to center mesh content in a node:

    function centerMeshContent(node: TransformNode) {
        const { min, max } = node.getHierarchyBoundingVectors();
        const halfRange = max.subtract(min).scaleInPlace(0.5);
        const currentCenter = min.add(halfRange);
        const offset = currentCenter.negate();

        traverseNodes(node, (n) => {
            if (n instanceof Mesh) {
                n.position.addInPlace(offset);

                // The bounding box is world-based, so re-adjust back into local space.
                n.position.addInPlace(node.getAbsolutePosition());
            }
        });
    }

    function traverseNodes(node: Node | Scene, visit: (node: Node) => void) {
        const children = node instanceof Node ? node.getChildren() : node.rootNodes;

        for (const child of children) {
            visit(child);
            traverseNodes(child, visit);
        }
    }