Howdy.
I am trying to write a C# script that could return the closest vertex point on a specified mesh within a set of Vector3 boundaries.
So forgive me while I spill out where my head is going with this.
I’d have an “objectPoint” variable that would act as the transform to compare vertex points.
Then I’d have a Mesh.mesh variable, which HAS to be the mesh, not collider.
Next, I’d like to be able to input a bounding box of vector3 points, so my calculation doesn’t run through the entire set of vertices on the mesh I input.
Lastly, I just need it to return the transform of the closest vertex to the “objectPoint”.
My big issue is I have no idea if Unity can do this kind of calculation. Everything I keep finding on forums for returning a vertex makes it run through every vert on the entire mesh. That would be really costly as geometry gets more complex.
Also I can NOT use the mesh collider. What I’m trying to do will not work with Unity’s collider system. It needs to use the rendered mesh.
I’ve seen some wave physics systems do something similar, but I’d like some build something on my own to understand it better, and not run the risk of frankesteining some bad scripts.
You can iterate it as you load or create it and spatially partition it so you can consider smaller pieces of it in a more performant way later.
Or you can iterate it all whenever you need this info to keep it simple at first.
Don’t get twitchy with optimization until you have an actual performance problem.
Meshes go into both MeshColliders and MeshFilters (for use in MeshRenderers).
They’re the same exact object: Mesh.
It sounds like you should just work through a few tutorials on procedural generation and manipulating meshes.
For your purposes you don’t even care about anything except the Vector3[ ] array of vertices, so just pull that out and use it directly, get a feel for what’s involved. Mesh vertices are always local space.
You’re welcome to look at my MakeGeo procedural generation package too, just for useful examples.
Follow up question for you.
What do you think an efficient way to pull the nearest vert information from the MeshFilters would be?
I’ll shelve optimization for a bit, and just focus on getting something functional first.
I basically have a wave shader that I want to pull vert information from, and so I’m pursuing this method, because it seems like it will fit my gameplay needs. Most of the water solutions I found were built around a single water body, and non-sloped water surfaces. Anyway, I don’t want to bog you down with redundant information. I appreciate the help!