Create a mesh from a sub mesh

I have a mesh with 2 sub meshes inside. I want to create 2 different meshes one for each sub mesh. Is it possible from the information stores in Mesh?

Mesh myMesh; // Contains 2 sub meshes
Mesh subMesh0 = new Mesh( );
Mesh subMesh1 = new Mesh( );

subMesh0 = // create the mesh by accessing myMesh.GetTriangles( 0 ) and myMesh.GetIndexes( 0 ) ? Is it possible Why no GetVertices( 0 )
subMesh1 = // create the mesh by accessing myMesh.GetTriangles( 1 ) and myMesh.GetIndexes( 1 ) ? Is it possible, Why no GetVertices( 1 )

Yes, you can make a new mesh from any of the submeshes of a mesh using the information stored.

The way the mesh stores the triangles and verticies explains why you shouldn’t use the raw results from GetTriangles and why there is not GetVerticies method. The verticies of a mesh are all stored in one Vector3. The int you get when you use GetTriangles is the indices of verticies that make up the triangles (every 3 ints is the first, second and third vertex of a triangle).

So if you use the list of GetTriangles to set the vertices of a new mesh then you are forced to keep the verticies in the same position. Then you might as well copy all the verticies, even the ones not part of the submesh because you will be using that space anyways. The following will display one submesh as a mesh but it will also copy all the vertices not in the submesh.

subMesh.triangles=myMesh.GetTriangles(x);
subMesh.verticies=myMesh.vertices;

If you only want to copy the vertices in the submesh then you will need to code a bit. For instance

using UnityEngine;
using System.Collections.Generic;

public class NewBehaviourScript : MonoBehaviour {
	void Start () {
        Mesh myMesh=GetComponent<MeshFilter>().mesh;
        Mesh newMesh=new Mesh();

        int[] oldTrianges=myMesh.GetTriangles(0);

        int count=0;
        Dictionary<int, int> dictionary=new Dictionary<int, int>();
        for(int x=0; x<oldTrianges.Length; x++){
            int current=oldTrianges[x];

            if(!dictionary.ContainsKey(current)) {
                dictionary.Add(current, count);
                count=count+1;
            }
        }

        int[] newTriangles=new int[oldTrianges.Length];
        for(int x=0; x<oldTrianges.Length; x++) {
            newTriangles[x]=dictionary[oldTrianges[x]];
        }
        
        Vector3[] oldVerts=myMesh.vertices;
        Vector3[] newVerts=new Vector3[count];
        foreach(KeyValuePair<int, int> pair in dictionary) {
            int oldVertIndex=pair.Key;
            int newVertIndex=pair.Value;
            newVerts[newVertIndex]=oldVerts[oldVertIndex];
        }

        newMesh.vertices=newVerts;
        newMesh.triangles=newTriangles;
        newMesh.uv=new Vector2[newVerts.Length];
        newMesh.RecalculateNormals();
        newMesh.Optimize();
        GetComponent<MeshFilter>().mesh=newMesh;
	}
}

As you can see, the old submesh indices are linked to new indices so the number of verticies is the same as the number of unique indices in the submesh. Then it is just a matter of converting the information in the old indices to the new indices (including the actual indices stored in triangles).

You can use this code to get started, although you will probably have to modify it. For instance, if you want to use textures you will have to convert the old uvs to new uvs as well.

Yes, should be possible… Take at lok at the Scripting API for the Mesh class. Fro the top of my head, it should go like this:

For each submesh:
Declare a List() vertices and a List() triangles.
Declare an int vertexIndex.

Iterate through each list in the submesh list.
For each three vertex indices therein, create a new triangle by copying the Vector3s from the original mesh’s vertices array, and add the current vertexIndex as as well as vI +1 and vI +2 to the triangles list.
Assign the vertices and triangles to a new mesh’s .vertices and .triangles using .ToArray().
Assign the new mesh to a MeshFilter.