- Home /
Mesh.colors doesn't work even though colors are passed correctly to the array
So iam creating a procedural terrain. The terrain is devided into chunks, which are instantiated in the Start function from a prefab. I am using a vertex color shader so that i can change the color of the meshe's vertices. During play mode the tarrain is perfectly generated, (vertices, triangles, uvs, texture, and normals). I also create a list of Colors and assign it to the meshe' s colors by typing:
mesh.colors = colors.ToArray();
No errors are showing in the editor on play mode, but the vertex colors do not change. On the other hand,when debugging the mesh.colors content the vertex colors are exactly what i want them to be, meaning that they were passed in the array successfully.
Also keep in mind that the color list that i create contains colors of type "new Color(5,5,5,255)" and not Color.black for example.
SO, what am i doing wrong? Thanks in advance!!
Edit!! I created a new scene, created a cube, added the shader and a script to change vertex color. What i noticed was that when i set the mesh.colors array with a color of type Color.black the vertices change to black, but when i set it to new Color(5,5,5,255) it actually makes the vertices whiter!! Am i missing something here??
Answer by Bunny83 · Jun 08, 2018 at 12:23 AM
What you seem to do wrong here is that the Color struct uses float values in the range of 0 to 1, just like in the shader. If you want to specify your colors with byte ranges you should use the Color32 struct. It can be implicitly converted to Color or vice versa. Though a Color32 value is 4 times smaller that a Color value. The mesh has a seperate colors32 property which you may want to use instead.
I hadn't realised that the Color struct takes values from 0 to 1. That's why when adding the color (5,5,5,255) the vertices would become white.
Thank you very much!! It makes sense now!
Your answer
Follow this Question
Related Questions
Vertex color emptied after instanciating mesh at runtime (build only) 1 Answer
Vertex Material add Burn Mark on Hit 0 Answers
Perlin Resources system 1 Answer
How to fill out a procedurally generated mesh with quads of varying sizes? 0 Answers
Circular Mesh UV interpolation for tileable texture 1 Answer