Get the closest point of an object's surface from any Vector3

I need to get the closest point of an object’s surface from any Vector3.

It is basically this question :

https://codepen.io/maurizzzio/pen/pERqxV?editors=0010

Unfornunatly the algorythm in the answer is outdated because it uses legacy Geomerties

I need this to work with today’s BufferGeometry

Thank you.

see pathfinding3d
Are you seeking to identify the nearest triangle based on known coordinates? In pathfinding3d, the get_closest_node function might be of assistance. I recommend exploring this library; it is remarkably efficient.

The get_closest_node_id function can be of assistance. By setting the final parameter, check_polygon, to false, the returned result need not be restricted to a node on the navmesh; instead, it will identify the nearest node in the entire space. During the algorithm’s construction phase, a k-d tree is employed to optimize spatial searches, ensuring exceptional speed. Furthermore, as it is implemented in WebAssembly (WASM), its performance is 10 to 20 times faster than that of JavaScript.

Usually there is a reason you need this information, and the reason you need it, determines the best approach to getting it.

Why do you need the nearest point on an objects surface, to a given point?

If you just need that demo ported to modern threejs, here you go:

gemini-code-1778512855655.html (3.9 KB)

@ manthrax
I need the nearest point on an object surface exaclty for the case described in the first link : To get my characters to go back on the navigation mesh.

the code you provide would do the job. thank you.

@ MrYang614
This is very interesting.

Im trying to make it work but currently i struggle with the “Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of “application/wasm”. Strict MIME type checking is enforced for module scripts per HTML spec.”

is it possible to make it work in native js browser without rebuild myself ? (i dont know about rust environment)

That error is related to how you are hosting your project. It needs to be hosted via https for wasm to work.

If you’re using an agentic coding tool, you can tell it “Please make me run targets to serve my project via https.”

You will have to click through the page dialogs the first time unsafe/yadeyada but then you can run the fancy stuff.

Review your errors and consult the AI. I encountered a top-level await issue in the Vite environment, which can be resolved with proper configuration.

@ MrYang614

I can’t make pathfinding3d work at all…

I need it to work in vanilla and webpack dev and production.

You should provide a live example on github.

@ manthrax

I have some minimal wasm that work on localhost without https.

It is the import and instancing that gives the problem.

Thank you

你能提供你的错误,或者你的最基本的代码在github吗, /pkg/目录下的 wasm 应该是没有问题,可以当作es模块引入执行的,我在 vite 需要配置一下,在webpack 也需要配置一下,我仍然没有见到你的错误信息。

Could you share the error you’re encountering, or perhaps the most minimal reproducible code on GitHub? The WASM files under the /pkg/ directory should be functioning correctly and can be imported and executed as ES modules. I had to make some adjustments in both Vite and Webpack configurations. However, I still haven’t been able to reproduce your error.

I am unable to access this CodePen link, likely due to restrictions imposed by our company’s firewall.

I tried setting this up using an importmap and also got the error,
Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of “application/wasm

It’s to do with how the wasm is generated.

So you can use fetch to load the wasm instead. Here is my edited pathfinding3d.js in case you want to see it.

Anyway,
here is a working demo of the Pathfinding3d wasm working directly in the browser.

@ seanwasere
Thank you this is amazing. It works perfectly in browser.

Also it is loaded in both webpack dev and build. didn’t try more already but it seems ok.

Thank you !

I’m having fun with pathfinding3d. This is a very good project.

These pathfinding libraries should include a get_closest_point_on_navmesh_surface function.

@params : navmesh, world coordinates.
@return : the nearest point on the navmesh’s surface from world coordinates.

for getting a pathway to go back to the navmesh.

for getting the character position on the navmesh without raycast.

these libraries only propose to get the nearest poly center. this is not helpfull.

What is stopping you from making a helpful library that has these features?

Mathematics

A major reason why this might not be implemented out-of-the-box on these libraries, is that the library can’t know in advance what kind of structures you’re using to segment your scene.

For instance If you are using a BVH to speed up your raycasts, the pathfinding library wouldn’t know about it, so its “find nearest surface to point” would be much slower than if you did it yourself using your acceleration structures.. so they may choose to omit exposing that kind of functionality entirely.

Otherwise people might try to use it and then complain “This pathfinding library made my app slow”.

This way the burden is on the user to control the time complexity of how they do operations like raycasting.

So that’s one good reason I can think of why they don’t include that functionality.

If you want an acceleration structure to make that raycasting to find the point really fast.. look into the excellent “three-mesh-bvh” library.