Let’s say I have a GameObject that has a MeshFilter with this mesh data.

These shapes are generated at runtime and all considered 1 mesh under the MeshFilter. What I want to do is separate the two, store them in a mesh array, and then create individual GameObject for both of them with their own MeshFilter and MeshRenderer. How do I distinguish that the square circled in red is unique from the shape on the left?
I think that by looking at this on the MeshRenderer level you’re trying to over-generalize the problem. Looking at this, I’m guessing that the shapes seen here are three quads, in which case there is no particular connection between the “connected” ones - they just happen to share some space. (Normally, with a mesh, you’d detect parts of the mesh being connected by seeing if they share an edge, which these do not) So you’d have to implement some other logic to detect things that are connected, which would probably be checking to see if the rectangles overlap.
With that in mind… Why wait until this is part of the MeshRenderer to separate it out? When this mesh is generated, it clearly knows what these rectangles are at that point. You can use a rectangle overlapping check (check to ensure there is some overlap between them in both dimensions), and then just siphon rectangles into separate meshes at that point.
What about in this situation? I have a circle and destroy its triangles so that it makes a pizza slice.


How do I separate those 2 meshes so that they’re unique meshes instead of just 1 combined mesh and GameObject?
StarManta’s right. You should be deciding “separateness” before MeshFilter, as you’re generating the geometry.
There are two definitions to “separateness” which StarManta points out. You want to decide geometric islands.
If you want to consider two sub-meshes as “separate” when they share no edges or vertices, this pseudocode should be close to what you want. Untested, of course.
PSEUDOCODE, not C#
* define a simple entity called an island (a sub-mesh)
* has a set of vertices
* has a set of edges
* get started
* make an empty list of islands
* for every edge in original mesh
* this edge has vertex A and vertex B
* for every island in our list of islands [note]
* if this edge is already in this island,
* add vertex A to this island
* for all other islands besides the current one,
* if the other island contains vertex A,
* merge other island containing vertex A with this island
* destroy the other island
* add vertex B to this island
* for all other islands besides the current one,
* if the other island contains vertex B,
* merge other island containing vertex B with this island
* destroy the other island
* if this edge was not found in any existing island,
* create a new island
* add this edge to this new island
* add vertex A to this new island
* add vertex B to this new island
[note]: the list of islands may shrink while iterating
Since this code loops on every edge, and does quite a few checks on whether edges or vertices are present in a set, you can see that this will take an increasing amount of time to process as the number of edges and vertices increases. Usually code like this would be done in a native routine, not in C#, for performance reasons.
The other definition which StarManta mentioned is whether or not sub-meshes intersect in 2D or 3D space. This is even more computationally intense than the given algorithm.
Thanks for the reply. I’m doing this in 2D space and I think distinguishing meshes by edges and vertices should be enough. Is there any way to optimize this?