Calculate position of object based on center

Hello there.

Does anyone know a good way to calculate the position of a 3d object to make it appear exactly centered on the scene?

Take the next image as example

I have to load different objects and I want every one of them as centered as possible and for now I’m just passing the position for each model but it’s just not very optimal.

I appreciate any help and sorry if this is too basic.

You can try something like this:

  1. Create a Group or Object3D.
  2. Load your model, use Box3.setFromObject (or model.geometry.computeBoundingBox if the model is super simple) to calculate it’s bounding box.
  3. Get the default center point from the Box3 using getCenter.
  4. Add the model to the group / object from point 1.
  5. Translate the added model by inverted values of the Box3 center, new Three.Vector3(-box3.x, -box3.y, -box3.z).

Thank you so much, it worked

For reference here is the code I used:
const box3 = new Box3().setFromObject(model);
const vector = new Vector3();
box3.getCenter(vector);
model.position.set(-vector.x, -vector.y, -vector.z);

1 Like