title: Remove Duplicate Vertices at Runtime in Unity
body: <p>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). </p>
  
  <p>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 <code>Vector3</code> 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.</p>
  
  <p>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. </p>
  
  <p>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? </p>