I’m having trouble figuring out an algorithm to do this. It also seems to be very performance heavy potentially. I’m currently using Unity’s Mesh component for this.
In this example, I want triangles 0,1,2 to be one separate mesh and triangles 3,4,5,6 to be the other separate mesh.
In this example, there should be 3 separate meshes as triangle 0, triangle 1, and triangle 2 should not be connected to each other.
However, if there was a 4th triangle that consisted of vertices 2,3, and 4, then there would be 2 meshes instead of 3 where triangles 1,2,3 make up 1 mesh and triangle 0 make up the other mesh.
Your definition of a mesh is a set of triangles that share an edge and not just a vertex. Another way of looking at it is a mesh is comprised of triangles that share two vertices.
Sounds like you can traverse the triangle indices performing this search. Making it efficient depends on a number of factors but to aid your search, I’d look at some basic graph theory (islands, components etc). Balancing performance and memory usage is always an issue in these kinds of problems.
There may well be an established simple algorithm to determine these mesh “islands” though honestly I’m not aware of one off hand.
You probably want to run a Depth-First search on all of the vertices in a loop so you can find out if there are any which aren’t connected (islands). Also you can run statistics on the shared edges. After you finished, you can check the shared edges, if a vertex doesn’t have shared edge, you found a candidate.
You can watch this course on the subject (I recommend the entire list if you have the time):