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.
2 Likes
You can try something like this:
- Create a
Group
or Object3D
.
- Load your model, use Box3.setFromObject (or model.geometry.computeBoundingBox if the model is super simple) to calculate it’s bounding box.
- Get the default center point from the Box3 using getCenter.
- Add the model to the group / object from point 1.
- Translate the added model by inverted values of the Box3 center,
new Three.Vector3(-box3.x, -box3.y, -box3.z)
.
1 Like
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);
3 Likes