Hi all,

given a mesh and a free point in space, is there a way to detect the closest point on the mesh surface from the free point? Also the nearest face or nearest vertex can be useful, but the nearest point is better.

Yep you could for example check the distance from the point to a line segment defined by the ray.

@carolhmj you suggest to check the distance from the free point to every vertex of the mesh? Is there a more performant way?

You could have a look at Optimizing With Octrees | Babylon.js Documentation

I believe you could then check out sections of the mesh first then dive into a closer area.

Never tried it so not sure but might be worth a go.

Your original post mentioned normals, but if you only need the closest point on the mesh’s surface then JohnK’s suggestion of using octrees is a very good approach.

Ok thanks a lot. I will try Octrees soon and give a feedback.

@carolhmj sorry, I realized that the original post was not quite accurate and I had to edit it…

No problem, let us know if octrees work for you!

I think i understand what an octree is, but i have some difficulty to understand what i have to with it in my specific case.

I have a mesh and a free point.

I have subdivided the mesh with the method subdivide(), passing 100 as value for testing.

I checked that after subdivide(100) the mesh has 100 submeshes.

Now what i have to do to detect the nearest face/vertex/point on mesh from the free point?

Thanks a lot for your help

In the case of an octree, you would have to find which octree block your free point falls into, and then find which submesh(es) fall into this same block. With this, you reduce your search space.

I remembered there’s another, simpler way, and that is using `facetData`

: Facet Data | Babylon.js Documentation. You can use `getClosestFacetAtCoordinates(x, y, z)`

to find the closest face to that free point, and then you just need to find the closest point on the face to the free point.

@carolhmj i also found the mesh.getClosestFacetAtCoordinates method, it should be just what i need. But I don’t understand why the result is always null. This is a simple example: https://playground.babylonjs.com/#P4C55L#4

As you can see, i placed a little sphere at (3,3,3) that represents the free point i’m talking about.

I seem to remember having problems with this a few years back. I think it only works if the free point is inside the partitioning.

The question was unfortunatey on the old forum.

This may be of interest

This is something I played around with not sure how useful it will be for you.

Whether the mesh is simple/complex and convex/concave will also make a huge difference