# Attach mesh on closest point from another mesh

Hi, I was looking for a way to automatically position markers on a mesh.
I’ve tried an approach suggested in another question, but it’s indeed slow on mobile.
I would like to use ray-casts to improve performance, but cant get to make it work.
Can you give me a hint or make it work? Any idea, much will be much appreciated.
There are two point finding modes, only the “vertices” works, “ray” doesn’t.

Here a sandbox: https://www.babylonjs-playground.com/#H8GZIG#25
Working with vertices: https://www.babylonjs-playground.com/#H8GZIG#26

Thanks!

Hi @CornelioD, looping in @Evgeni_Popov who may be able to help

1 Like

You can make your first method a little faster by not creating a `Vector3` for each vertex:
https://www.babylonjs-playground.com/#H8GZIG#27

Regarding the ray method, it seems to me that what you are doing does work. But you don’t get the same result because the vertex method is finding the closest point on the mesh whereas the ray method find the intersection between the mesh and the ray going from (0,0,0) to the point.

Thank, will look more into the ray method. Another problem with vertex lookup method is in case of large triangles, for example, considering a cube, I wont be able to find a point in the middle of the square but only in one of 4 vertices, which is a problem.
Yes, using a longer ray seems to be ok https://www.babylonjs-playground.com/#H8GZIG#31
but it points to the center, is there a way to find the closest triangle? The ray method doesnt work if it just points toward the center. For example this point should snap around eye bone, but due to the fact that the ray points towards the center, it ends inside the skull, which is wrong.

Maybe the best approach would be, correct if I’m wrong:

1. find the closest 3 vertices (or triangle)
2. cast a ray in the middle of that triangle
3. pick location intersection from ray (or calculate triangle middle point)

or if there is a method to find the adiacent triangles of a point

1. find closest point
3. cast ray for each of them and measure distance
4. return the triangle intersection with shorter ray distance

If you really want to find the closest point (minimal distance) from point P to a mesh, I think you have to loop over all triangles and for each one find the closest point on the triangle (Closest point on triangle - Math and Physics - GameDev.net or GeometricTools/DistPointTriangle.h at master · davideberly/GeometricTools · GitHub), then keep the minimal distance. Using an octree may help by not looping through the triangles inside a cube of the octree if this cube is farther than the distance already found (see mathematics - Fastest way to find closest triangle of mesh from specified point - Game Development Stack Exchange).

All in all, it’s not very straightforward…

1 Like

Hi, just noticed that the vertex data seems to be relative to the model, so if the model is scaled or rotated it wont match anymore. How to get the absolute position of that every single vertex relative to world?

Just found the solution, seems like there is a method to manually update vertices.
Partially fixed: https://www.babylonjs-playground.com/#H8GZIG#33

You need to transform the vertex by the mesh world matrix (`mesh.getWorldMatrix()`). Use `BABYLON.Vector3.TransformCoordinates` or related functions for that.

1 Like

Like so? https://www.babylonjs-playground.com/#H8GZIG#34
The other method has a problem too, seems like calling bakeCurrentTransformIntoVertices alters the mesh position.

Yes, but the mesh you want to get the world matrix from is `target`, not `mesh`.

2 Likes

Final https://www.babylonjs-playground.com/#H8GZIG#35
Thanks a lot! Babylon.js is just amazing, as well the community!