0
\$\begingroup\$

I have a mesh and I want to color each group of vertices with a certain color. If anyone could please advise how this can be done in Unity using coding?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Read up on the Mesh class. You can use this to read & modify any information you want about mesh assets in your project, or even create whole new meshes procedurally.

You can access and iterate over mesh information like this:

void AssignBoneColors(Mesh mesh)
{
   Color32[] colors = mesh.colors32;
   // Ready an array large enough for all vertices if the mesh didn't already have one.
   if(colors.Length == 0)
       colors = new Color32[mesh.vertexCount];

   BoneWeight[] weights = mesh.boneWeights;

   // If the mesh has no skinning weights, this will be zero, 
   // which skips the loop below.
   int numVertices = weights.Length;

   for(int i = 0; i < numVertices; i++)
   {
       colors[i] = GetColorForBoneWeights(weights[i]);
   }

   mesh.colors32 = colors;
}

GetColorForBoneWeights() is where you'll implement your colour selection based on which limb the vertex is skinned to. Note that boneWeight.boneIndex0 should contain the index of the bone with the highest weight for each vertex, so you could colour based on that bone alone, or navigate to its first parent to which you've assigned a color, or write a colour-weighting function that considers multiple bones. These implementation details are up to you.

Note that the colours will only be visible if you render the mesh with a shader that uses vertex colours, or use additional editor extensions/gizmos to help visualize vertices.

\$\endgroup\$
9
  • \$\begingroup\$ Thank you very much for your answer and kind assistance. Could you please give me a hint on how GetColorForBoneWeights will look like? \$\endgroup\$ Commented May 29, 2015 at 0:30
  • \$\begingroup\$ That will depend a lot on what you want to do. For starters, you could prepare an array of unique colours as long as your bone list, then return boneColors[weights.boneIndex0]; \$\endgroup\$ Commented May 29, 2015 at 0:32
  • \$\begingroup\$ okay, what I want to do is this. I want unique colors for different vertices of each bone, so vertices assigned with highest weight to bone 1 will have color red, and so on. Sorry for disturbing you. \$\endgroup\$ Commented May 29, 2015 at 0:48
  • 1
    \$\begingroup\$ @shepherd If you read above, that's exactly what my suggestion will give you. The colours I describe are unique per bone, not per vertex. \$\endgroup\$ Commented May 29, 2015 at 0:56
  • 1
    \$\begingroup\$ mesh.bindposes.Length; Comments aren't really meant for extended discussion, so if you need more help implementing this then you may want to create a new question. You're also welcome to hit me up on the StackExchange chat feature or the social network of your choice to discuss further. \$\endgroup\$ Commented May 29, 2015 at 1:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.