Get wrong position center of mesh in gltf

I get center of mesh from gltf scene and use result to set position for my tree object. I don’t understand why my position is wrong with original file?
Here is my code get center of mesh:

    var boundingBox = mesh.geometry.boundingBox;
    var position = new THREE.Vector3();
    position.subVectors( boundingBox.max, boundingBox.min );
    position.multiplyScalar( 0.5 );
    position.add( boundingBox.min );
    position.applyMatrix4( mesh.matrixWorld );

I think you could try something a bit simpler like the following?

const box = new THREE.Box3().setFromObject(mesh);
const center = box.getCenter(new THREE.Vector3());

  mesh.position.x - center.x,
  mesh.position.y - center.y,
  mesh.position.z - center.z

If that doesn’t help, are you able to produce a live demo?

I tried simpler like your method but result is same :frowning:

Are you able to share the model and/or a larger code sample of what you are trying?

Here is demo
Thanks you =))

By adding the following code, I got the result shown in the image.


Oh no, it isn’t my problem.
My original model is white model. I get position of mesh from white model and set it for tree gltf model and result as image. I need my tree has same position with white model. Sorry because my English is bad.

Ah I see, took me a while to understand.

Each tree is a separate model and you are trying to place one tree on each of the white cubes? If so the problem here is that the tree model itself is offset, so you are giving them the position of the cubes and then they are offsetting themselves. You need to counter the offset by subtracting this vector. Its a similar thing to my first example, but rather than positioning to the center of the world you are setting the tree’s local center.

index.html (41.7 KB)

Is this what you were after?

1 Like

Thank you very much. That’s what I need