Average position and same rotation when merging two meshes

Hi everyone,

I’m working on merging two 3D meshes and I would like the local origin of the resulting mesh to be the average of the origins of the two original meshes, while keeping the same rotation. I think setPivotMatrix might be the right approach for this, but I’m not entirely sure how to use it properly.

Additionally, I’ve noticed that when I change the pivot point of a mesh, the position of the axes viewer doesn’t seem to update. Could this be because I need to parent the mesh to the pivot point instead of just modifying the pivot point of the mesh itself?

I’d greatly appreciate any advice or insights on how to achieve this correctly.

Thanks in advance!

When you call CreateBox you are generating geometry with vertex positions evenly distributed around the model space origin (0,0,0). When you set the position and rotation on the boxes, you are changing their world transform. When you merge meshes, it takes into account the current world transform for each mesh and transforms the vertex positions accordingly. This is why in your PG you end up with a final mesh that basically has no translation/rotation relative to the world space origin, but rather has vertices offset relative to the model space origin.

To get the behavior you are describing (if I understand correctly), you should set the position/rotation of the individual meshes such that the world space origin is how you want the model space origin after the merge, and then set the position/rotation of the final merged mesh however you want. For example: MergeMeshes | Babylon.js Playground (babylonjs-playground.com)

1 Like

Understood. I also rotate now the boxes before the merge, so the mergedmesh has the same rotation as the initial boxes.

Thank you!