- Home /
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
@Fattie: thanks for the help. So the submesh index 0 is the first submesh? Or it's the main mesh?
this is what all of you are looking for:
https://answers.unity.com/questions/1436857/small-submesh-example.html
don't forget to add the Mesh.Clear(); at the top and not accidentally erase the Mesh.subMeshCount; like I was doing.
Answer by Eric5h5 · Sep 03, 2012 at 04:32 PM
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.
Yes, the only purpose of using submeshes is to be able to use different materials on each submesh. There's no other advantage.
If you have a mesh that is too big for a single mesh, split it into multiple objects. That means a new Gameobject, a new mesh, a new $$anonymous$$eshRenderer / $$anonymous$$eshFilter. This new object can be the child of your first object by setting it's transform.parent to your parent object's transform.
Well, yes I have to use different materials on submesh. And hopefully I can use different transformations for each submesh, right?
Definitely not. As I mentioned, if you want the meshes to be separate like that, you should just make them as separate meshes and attach them to child GameObjects. In this case submeshes are not what you want at all.
@Eric5h5: thanks for clarify. $$anonymous$$aybe you have a link to some references on how building game objects hierarchies from script?
Answer by AllOfM · Jan 27, 2016 at 03:17 PM
can somebody give a code example for Submesh and the Materials for each Submesch ?
this is what all of you are looking for:
https://answers.unity.com/questions/1436857/small-submesh-example.html
don't forget to add the Mesh.Clear(); at the top and not accidentally erase the Mesh.subMeshCount; like I was doing.
Answer by fenderrex · Dec 21, 2016 at 02:55 PM
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 https://docs.unity3d.com/ScriptReference/SkinnedMeshRenderer-bones.html 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 ....
Your answer
Follow this Question
Related Questions
Few problems with arrays 1 Answer
Distribute terrain in zones 3 Answers
Where is the submesh Reference of vertex/triangle? 1 Answer
Drawing emojis with Unity's Dynamic Font 1 Answer
Multiple Cars not working 1 Answer