I’m trying to change individual vertex colors on a simple plane mesh. I’m trying to read mesh.colors and it doesn’t seem to be working.
Here’s the basic code that i’m testing:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FogBehavior : MonoBehaviour
{
private Mesh fmesh;
void Start()
{
fmesh = GetComponent<MeshFilter>().mesh;
Color[] fcolors = fmesh.colors;
Debug.Log(fcolors.Length);
}
}
the debug always shows zero but if i return the length of the vertex array it is 121. Can anyone tell me why this isnt working? I have only seen examples that initialize the colors array based on the length of the vertex array, then assign all the vertex colors, then copy that to mesh.colors. Is there a special way to get the existing vertex colors?
I understand that colors32 is better and I will definitely be using that instead of colors…this was just for the test
I guess what I don’t understand is that if I have a shader that uses vertex colors, it should have a default color for each vertex supplied by the shader, right?
Why wouldn’t it be returning those?
Is there any better ways to adjust a single vertex color (or color32)?
Thanks
the shader does not modify the mesh data itself. if it creates custom vertex colors its only for internal use in the shader and the information is discarded after the shader has finished. next frame the creation is done again.
so if you want a mesh to contain vertex colors you must initially create it with an array of those or you can assign the array later also. but you must provide the mesh the vertex colors at some point in order to retitrieve/modify them.
btw: if you purchase eric’s vectrosity package you can learn quite alot about mesh manipulation. and probably you can use it in a convenient way for your problem.
No, the mesh needs vertex colors, the same way meshes need normals, tangents, uvs, etc., depending on what the shader requires…those don’t come from the shader, they come from the mesh.
No, you need to assign all of the vertex colors regardless of how many of them are being changed, same for vertices and the other stuff.
Ok so I need to first initialize the vertex colors and assign them to mesh.colors then I can either retrieve them or simply change the array I used for initialization and then reassign the modified array to mesh.colors … just one final question is that I know mesh.colors ( color32) contains the alpha channel…I guess this means that upon creation, regardless of what is in the inspector, I have to initialize the alpha value of all the verteces procedurally, is that true?
No, you can simply put in vertex colors in your 3D app. I don’t really understand the question about alpha since all color variables include alpha; you can’t have a Color or Color32 without it.