Submesh creation from script

I’m learning to use Unity these days, so just a newbie question.

I’ve successfully built a mesh from a C# script, following this tutorial.

Basically I created the following class:

    [RequireComponent (typeof (MeshCollider))]
    [RequireComponent (typeof (MeshFilter))]
    [RequireComponent (typeof (MeshRenderer))]
    
    
    public class MyMesh : MonoBehaviour {
     void Start () 
     {
        MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
        this.mesh = new Mesh ();
        meshFilter.mesh = this.mesh;
    		
        //vertex, triangles and uvs generation....
    
    		
        this.mesh.RecalculateNormals();
        this.mesh.RecalculateBounds ();
        this.mesh.Optimize();
     }

}

Now, my mesh is becoming very complex and I need to organize it using sub meshes, but I don’t know how to do this in Unity using C#.

Basically I need to:

  • create submeshes
  • add submeshes as children of my mesh object

Can anyone put me in the right direction?

thanks

1 Like

Use Mesh.subMeshCount to set the number of submeshes, then SetTriangles to set the triangle list for each submesh. There isn’t a “main” mesh in this case. Then, make a Material array (where each material corresponds to each submesh), and set renderer.materials to that Material array.

You can’t have submeshes as children. The point of submeshes is that they are all part of the same mesh. If you want children, just make separate meshes.

can somebody give a code example for Submesh and the Materials for each Submesch ?

@Heisenbug

And hopefully I can use different transformations for each submesh, right?

you may be able to add a bone like in blender pragmatically if you can’t achieveit using SkinnedMeshRenderer.bones Unity - Scripting API: SkinnedMeshRenderer.bones
look at how blender does it (all in easy to read python) but it looks like unity imports the bone data to the public Transform SkinnedMeshRenderer.bones
my bet is that one bone is made up of a set of 2 transforms, i don’t know what other arrays you might need to populate to connect the vertices if that even how its done…
this is how blender would do some mesh setup bone set up…
https://wiki.blender.org/index.php/Dev:Py/Scripts/Cookbook/Code_snippets/Three_ways_to_create_objects
at worse this could also export it to your server via file transfer or even ftp, python is awesome, if you need a web hook just use yo the social network api
just stuff i ran into doing research to see how I would add some features im working on …