Compute surface/faceId world position from pickWithRay

Hi,

I’m trying to create some pin point on a mesh. I create this pin point from the pickWithRay method.
Here is an example, double click on the mesh to pick a point.
On simple use cases it works well : https://playground.babylonjs.com/#F9NNOF#15

I’m mainly trying to create that for animated models. So I try to reuse to faceId return by pickWithRay to compute the current world position of the face.

And here is my problems.
On some meshes, the points seems to be offset. Again double click, the blue point is the hit.pickedPoint and the red point is the computed point from the faceId.
On the Skull, it is offset : https://playground.babylonjs.com/#F9NNOF#12
On the Alien, it is offset at the base and offset/inverted on the head : https://playground.babylonjs.com/#F9NNOF#11
On the Khronos Helmet it is always on the ground : https://playground.babylonjs.com/#F9NNOF#10
On the Khronos animated Fox it is inverted : https://playground.babylonjs.com/#F9NNOF#16

I’m quite a newbie when it comes to 3D stuff, so I’m probably missing something obvious.
If you would be kind enough to help me :slight_smile:

Thanks

Hello,

What you was missing is taking into account the World Transform of the meshes (Position, Rotation, Scale) It worked by chance on the Dude because it was all 0,0,0 (and 1,1,1 for scale)

After mesh load, compute matrices :

meshes.forEach(function(mesh) {
    mesh.computeWorldMatrix(true);
})

And before projecting from 3D to 2D, transform your point :

const worldMatrix = pickedMesh.getWorldMatrix();
worldPosition = BABYLON.Vector3.TransformCoordinates(worldPosition, worldMatrix);

And here you go : this Playground works on all the examples you provide :slight_smile:

++
Tricotou

2 Likes

Thanks, I get it now !

@Tricotou striked again :slight_smile: