- Home /
Changing alpha on specific vertexes?
I'm trying to make a type of fog of war where I raycast towards a plane and change the alpha of vertexes on that plane. I'm using this shader. and this is the code I tried to change the alphas with:
void Start ()
{
Mesh mesh = GetComponent<MeshFilter>().mesh;
Color[] vertexColors = mesh.colors;
int i = 0;
while (i < vertexColors.Length)
{
vertexColors[i].a = 0.0f;
print(vertexColors[i].a);
i++;
}
}
I check the alpha as I set them and I get the output 0 but when I later check in Update, the alpha is back to 1. Also, in the editor there is no visual change on the plane. What am I doing wrong. Keep in mind that I'm using Unity free. Thanks.
Answer by troien · Nov 10, 2014 at 01:51 PM
I'm not sure if this if this fixes it, because I didn't test it yet. But I believe you should add the following line after your while loop:
mesh.colors = vertexColors;
Because you are probably just modifying a copy of the colors array.
Also, reading the docs, they advise you to use mesh.colors32 instead. (note that colors32 uses bytes instead of floats and therefore values between 0-255 instead of 0-1)
Wow, thanks a ton, it's kinda embarassing that I forgot something that obvious... Thanks for the preformance tips as well! Very much appreciated!