Moving centre of geometry to 0,0,0

Hi,

I am new to ThreeJS.

My issue is the following: I am loading an object( using the FBX loader), that has the geometry displaced by a locator.
I am trying to bring the geometry to the centre of my viewport but what I am currently doing is bringing the centre of a bounding box of the geometry but offset by the transformations of the locator.

I want to know how to calculate the true world space of the geometry, basically I want to find out the offset between the locator and the geometry.

In Maya or other 3D applications I would go up the hierarchy and calculate the world transforms based on the object local transforms and its parents transforms. I don’t know how to work my way up the hierarchy here(that’s one problem), when I try to access the geometry’s parents I don’t seem to be able to get their transformations.

When geometries are not displace I use the following to centre the geometry:

        // Compute object dimensions
        const matrix = new three.Vector3();
        child.geometry.computeBoundingBox();
        const offset = child.geometry.boundingBox.getCenter(matrix);
        child.geometry.applyMatrix(
          new three.Matrix4().makeTranslation(
            -offset.x,
            -offset.y,
            -offset.z,
          ),
        );

        child.geometry.computeBoundingBox();

    const box = new three.Box3().setFromObject(object);
    objectDimension = box.getSize(new three.Vector3()).length();
    objectPosition = box.getCenter(new three.Vector3());

    object.position.x += object.position.x - objectPosition.x;
    object.position.y += object.position.y - objectPosition.y;
    object.position.z += object.position.z - objectPosition.z;

    // Adding loaded model to the scene object
    scene.add(object);

I tried getting the world position using this method ’ getWorldPosition’ but I still get the world position of the locator, so not helpful at all.

Going back to the 3D package and altering the model is not an option, I need to figure out how to find all the information I need while in ThreeJS.

Thanks!

const offset = child.geometry.boundingBox.getCenter(matrix);

getCenter expects a Vector3, you probably just missed putting that offset variable instead matrix there, you can also then offset/translate the geometry with geometry.translate( x, y ,z ) instead that matrix conversion.

Hi, thank for you answer. Changed the code but I am still getting the same displacement/offset is present.

        // Compute object dimensions
        child.geometry.computeBoundingBox();
        const offset = child.geometry.boundingBox.getCenter(
          new three.Vector3(),
        );
        child.geometry.translate(-offset.x, -offset.y, -offset.z);

How can I find an object parent transformation if I know the object? I was hoping that I I knew the parent world transforms the offset that I need would be the geometry local transforms but I struggle to get the parent transforms for some reason. Do I need to first load, then traverse the scene again? IS the problem I am having due to the fact that I am trying to calculate the geometry world position during load?

Thanks

You don’t need to know it’s parent if it’s a child of it, just force-update the world matrix and then extract the world position.

child.updateMatrixWorld( true );
offset.setFromMatrixPosition( child.matrixWorld )

The focus point of camera is the center of your viewport. Make sure, it is looking at (0,0,0).

Geometry.center()
Get the World Coords

If the object has parent/s with transformation, use Maya method or localToWorldl to get the offset.

Still having problems, come up with a demo.