I’m procedurally generating a simple mesh and I get this little color glitch. One of the vertices has a different vertex color than the rest. Which is weird to begin with because I don’t assign vertex colors to any of the vertices. Also, it doesn’t happen every time I create a GameObject with that script on it. And even vertex where color difference is on keeps changing, sometimes it’s on the second vertex, sometimes the third vertex of the array.
Here’s an image of the problem. I’ve used a different shader to better illustrate the problem
Here’s the code I’m using to update the mesh
Vector3[] vertices = new Vector3[waveSegmentCount * 2];
Vector2[] uvCoords = new Vector2[waveSegmentCount * 2];
int[] triangles = new int[waveSegmentCount * 6];
// Generate mesh data
for (int i = 0; i < waveSegmentCount; i++)
{
float rad = (float)i * (360.0f / (float)waveSegmentCount) * Mathf.Deg2Rad;
Vector3 v = new Vector3(Mathf.Cos(rad), Mathf.Sin(rad), 0.0f);
vertices[i * 2] = v * TimeAlive * propagationVelocity;
vertices[i * 2 + 1] = v * TimeAlive * propagationVelocity + v * waveThickness;
uvCoords[i * 2] = Vector2.up;
uvCoords[i * 2 + 1] = Vector2.zero;
triangles[i * 6] = i * 2;
triangles[i * 6 + 1] = i * 2 + 2;
triangles[i * 6 + 2] = i * 2 + 1;
triangles[i * 6 + 3] = i * 2 + 1;
triangles[i * 6 + 4] = i * 2 + 2;
triangles[i * 6 + 5] = i * 2 + 3;
}
// Merge the ends
triangles[waveSegmentCount * 6 - 5] = 0;
triangles[waveSegmentCount * 6 - 2] = 0;
triangles[waveSegmentCount * 6 - 1] = 1;
// Apply mesh data
mesh.Clear();
mesh.vertices = vertices;
mesh.uv = uvCoords;
mesh.triangles = triangles;
I have used the debugger and found that the glitch is indeed in the vertex color data of the mesh.
The behaviour is so unpredictable that I cannot imagine it could a problem with my code, but more likely a bug in underlying systems.
I can just assign null to mesh.colors to fix the problem, but I’d really like get to the bottom of this.
I had a graphics card a while ago which had a few bad bits in VRAM, which would cause things like this…depending on what exactly happened to be using that VRAM, it would randomly manifest as wrongly-colored pixels in textures, wrong vertex colors, or incorrectly positioned vertices. Try a different computer and see what happens.
It seems to be an issue with Mesh.Clear(), since when I removed that the effect stopped happening. You don’t need Clear in this case since you’re not resizing the mesh, but this still seems like a bug so I’d recommend reporting it with the bug reporter app.
On a tangential note, for the sake of efficiency, I’d suggest computing the UVs and triangles once, rather than recomputing them every frame, since the number of vertices seems to be constant. Only the vertex positions change, so the vertices are the only thing you need to update every frame. I’d also recommend reusing the mesh instead of destroying/recreating it every time.
Thanks Eric for being so helpful. It never occurred to me you wouldn’t need to call Clear(), and obviously there’s no reason for recalculating uvs or triangles in that case.