- Home /
Color different Mesh vertices
I have a human mesh connected with an fbx animation file using Humanoid Mecanim. What I want to do is how to know the vertex groups, meaning the group of different vertices assigned to different limbs. Then I want to color each group of vertices with a certain color. For example, right hand will be red, left hand will be blue, head will be yellow,....etc. If anyone could please advise how this can be done in Unity using coding?
Answer by Ravart · May 28, 2015 at 09:46 AM
Since you kindly asked inside an old thread of mine, I will give you a straight answer, but it's not that easy ;)
Firstly you require a Shader that shows VertexColors, otherwise you wont see a thing!
Secondly, to color a Vertex (Each point in your Mesh is a Vertex), you have to access the Mesh (GetCompenent().mesh) and get the Colors (mesh.colors[], is an array). Get the index of the vertex and set color to an color of your choice at picked index.
For detecting submeshes (== vertex groups!!! what you called vertex groups is called "weights") inside your mesh you have to check the Triangles (mesh.triangles[]). Its a bit tricky since Triangles look different, depending if it's just one single submesh (therefore just one mesh) or contains many submeshes. Best google for Triangles⋐meshes.
Lastly, to gain information about your limbs(referred to as Joints), you access the Bones (mesh.bones or something like that). Inside a Bone is a Joint-Index and a Bone-Weight. Remember, one vertex can be affected by four joints, therefore you have the check the value of the weight (descripes how strongly the Joint affects the Vertex). Pick the index of the strongest weight and you have your limb.
Be careful, you could modify your mesh, therefore I recommend using the PlayMode.
Cheers
Thanks for your answer, I'm really thankful for your kind assistance. I already made a shader that shows the vertexcolors and I can color the whole mesh. I'm asking if you could assist me on how I can detect submeshes, as I only have one mesh (representing the human body)?
I can send you my code snippet regarding triangles and submeshes, though it's on my home-computer.
To check the number of submeshes, do "int i = mesh.sub$$anonymous$$eshCount;"
$$anonymous$$esh is just an container, therefore you never know if its one single submesh or many. Just imagine, you split any face and save it as own submesh, you won't notice a difference compared to a solid mesh. The reason for submeshes, they are important for tiled textures, otherwise you encounter problems with the UVs or if you require two different materials. Guess there are a lot of reasons to use submeshes. But you never know, therefore best check and store the triangles.
I re$$anonymous$$d you, to get your bones/limbs you don't require triangles, unless you want to get the polygroups/submesh (for example you wish to swap the head, taken your head is a polygroup/submesh)!