Why do my mesh colors blend sometimes and sometimes not?

Hi Everyone,

at this moment i’m experimenting with vertex coloring and creating raycasts that change the color of vertices of an object. So far i’m doing quite well, but i still have one problem. Every time i use the shader and the rayvast the triangles that get hit are getting colored properly. I change the vertex color of the each of the 3 vertices of a triangle and the triangle changes color. As i would expect 2 triangles connected to each other are influenced by the same vertex so if 1 triangle is hit it will blend out its color to the triangle next to it. Sometimes however it doesn’t. In the picture below i have created an example where one can see what i mean.

At circle 1 it is clear that the top vertex has a color around bluegreen and it perfectly blends to the color blue int he vertex below. However at circle 2 the top vertex should be somewhat bluegreen as well but below the vertex it suddenly is a lot less green. Doe someone have any clues how i can fix this or where the problem comes from?

To me it looks like a problem with the mesh:

  • it either has normal smoothing issues => in this case if you have 3ds max you can apply a smoothing modifier with only one smoothing group
  • or it has a duplicate vertex somewhere and the triangles don’t share the actual same edge (there are two edges overlapped) => if you have 3ds max you can check topology using X view or the STL check modifier.

Actually pretty much any model needs some seams. Either because of vertex normals (seam required for hard edges like in a cube mesh) or because of UV seams. If the object is UV mapped you will need seams.

You can use my [UVViewer][1] to see the UV map of a mesh inside Unity. Just open the UVViewer window from the tools menu and select an object in the scene that has a MeshRenderer. Keep in mind that a mesh can even have multiple UV channels.

If you don’t need any texture / lightmapping you could theoritically create a fully shared mesh with no duplicates, but such a case is extremely rare.

As actual solution you might want to setup “vertex groups” so you can easily find the other vertices which belong to the same position. To setup such a structure you can simply use an array of a small custom class like this:

public class Vertex
{
    public int index; // a vertex index
    public Vertex next = null; // next vertex that belongs to the same group.
}

public Vertex[] vertexGroups = new Vertex[vertices.Length];

void AddVertex(int aIndex1, int aIndex2)
{
    vertexGroups[aIndex1] = new Vertex { index = aIndex2, next = vertexGroups[aIndex1]};
    var n = vertexGroups[aIndex2];
    while (n != null)
    {
        vertexGroups[n.index] = new Vertex { index = aIndex1, next = vertexGroups[n.index] };
        vertexGroups[aIndex1] = new Vertex { index = n.index, next = vertexGroups[aIndex1] };
        n = n.next;
    }
    vertexGroups[aIndex2] = new Vertex { index = aIndex1, next = vertexGroups[aIndex2]};
}

for (int i = 1; i < vertices.Length; i++)
{
    Vector3 pos = vertices*;*

for(int n = 0; n < i; n++)
{
if (vertices[n] == pos)
{
AddVertex(i, n);
break;
}
}
}
Haven’t tested that code for errors but it should work. You’ll end up with an array where each element represents a simple linked list. If the vertex is unique, the element in the “vertexGroups” array will be “null”. It’s only set to something if there are “paired” vertices. The vertexGroups element for a certain vertex index only contains the “other” vertices, not itself.
So once you setup that array in Start you can simply iterate through all paired vertices.
Say you picked the vertex with index 25. You would simply set the color of vertex 25 and in addition do this:
int index = 25;
colors[25] = YourColor;
for(var n = vertexGroups[index]; n != null; n = n.next)
colors[n.index] = YourColor;
If the vertex is a unique vertex the for loop will be skipped since n is null in that case.
This will set the color of all vertices which are at the same position.
ps: Just leave a comment if you want a more detailed description of what AddVertex does. I Thought about writing some comments, but then it just gets messy ^^.
[1]: https://dl.dropboxusercontent.com/u/7761356/UnityAnswers/Code/UVViewer.cs

Hey, guys. I encountered the same problem when using Mesh.colors array:
the color change is not smooth.


Is your problem solved? Can you share your solution? Thanks a lot :slight_smile:

I have browsed many posts and it seems that using shader instead of Mesh.color can solve this problem.