Why does one assignment method to Vertex colours work and not another?

So I’ve been trying to change the colours on a background gradient which is in the wiki, it has a vertex color only shader. I looked everywhere for information and tried everything I could. I wanted to change specific verts to specific colours. But none of the below works. I’ve checked that they are assigned to specific verts. I can read the colour from them but I cant write to them in the below way.

mash.SetColors = new Color(0.1, 0.2, 0.3);
mash.colors[0] = new Color(255, 0, 0);
mash.colors[1] = new Color(0.0f, 0.0f, 0.0f);
mash.colors[2] = new Color(0.5F, 1, 0.5F, 1);
mash.colors[3] = Color.green;

Till eventually I just tried this which works. But I dont understand why it works.

mash.colors = new Color[4] { topColor, topColor, bottomColor, bottomColor };

I dont understand why I can change them all in that way but not specific ones. Is that the way it works? I have to change them all and I’m not able to change them individually?

All of the mesh arrays are secretly fetching and saving copies of the entire array. In other words, when you see theMesh.vertices it’s really Vector3[ ] theMesh.getVertexArrayCopy();. And when you use theMesh.colors[2]=red it’s really (theMesh.getColorArrayCopy())[2]=red;. You’re getting a copy of the real array.

The only way that works with any of them is the way the examples show: you have to get or create Color[ ] A;, fill or change it, then use theMesh.color=A. Assigning an entire array does areal copy back to the real mesh values.

If you know graphics cards (most people using the Mesh class already know it all very well, or can learn it from standard sources) it makes some sense. The arrays are really stored in the GPU, and you can’t just poke around in there directly. All you can do is copy entire arrays back and forth, which is slow.

1 Like

Thanks for the detailed explanation. I think I get it now.