In my Unity project I have a dynamic mesh system which needs to be heavily optimized. Currently I'm looking for an algorithm to optimize the meshes by removing duplicate vertices at runtime (the meshes are combined into one giant mesh). Removing duplicate faces that occupied the same space was fairly easy, since they can both safely be removed if they shared the same space within a threshold (because they won't be showing).
I'm having a bit more trouble with duplicate vertices, as they need to be removed in a certain way or the mesh is completely messed up. After my initial try at it, I got an array of Vector3 objects that contain the vertices, and I was able to manipulate that to a certain extent. It seems that removing is only half of it though, as after looking more into mesh topology it seems I need to do more of a merge after removing the other one.
This is the part where I'm essentially stuck. Seemingly, I thought it would be fairly simple. Get all the triangles a vertex that needs to be removed is attached to and reassign the triangles to the vertex that stays. It isn't that easy in practice though.
First of all, is there an easier way? I'm very surprised Unity doesn't have a merge vertices script somewhere, or at least an example. Secondly, is there a better way, and how? I came across spatial hashing, however, I'm still stuck at the part where I somehow merge the vertices. Third of all, merging the actual vertices. How should I go about this in the most efficient way possible?
Would using a HashSet be of use in this situation? For those that don't know, a HashSet is like a list, but is much much faster when searching for elements. Due to the nature of HashSets, they cannot store duplicates. Storing the vertices in a HashSet could mean that any duplicates are simply not added, taking little to no processing power.
I know this question is old, but I thought I would try my best to help anyone else looking for this answer - I'm not a programming expert at all, just figured that writing this couldn't hurt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With