How to animate vertex color on a 3D GameObject ( say a cube)?
I am trying to animate Color from Bottom to Top on a cube. Below is my test code. I have Top face verts and bottom face vert indices stored in _topFaceVert and _bottomeFaceVert in Start().
void AnimateColor()
{
_mesh = GetComponent<MeshFilter>().mesh;
_vertices = _mesh.vertices;
Color[] meshColors = new Color[_mesh.vertices.Length];
int index;
for (int i=0; i< _topFaceVert.Count; i++)
{
index = _topFaceVert[i];
meshColors[index] = Color.red;
}
for (int j=0; j < _bottomeFaceVert.Count; j++)
{
index = _bottomeFaceVert[j];
meshColors[index] = Color.blue;
}
_mesh.colors = meshColors;
}
I am using Mobile/Particles/Vertex Blended shader. Here is the result:
The issue is, you still see inside of the cube. The final result should be The top face being solid Red, bottom face being solid Blue and side faces being colored with gradient effect which is achieved here. Here is a screen shot of the Inspector for the Cube
What am I doing wrong here? Is there any better alternative? Need a optimized solution as this needs to be deployed in VR so more performance restrictions.
I think the main reason is you're creating a color array but only write colors to certain indices. since colors are struct they get default values, hence 0,0,0,0. you're using an alpha blended shader so that means they are transparent. and from the looks of the visible faces, you picked too few vertices. remember, each corner has 3.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101252im_/https://answers.unity.com/themes/thub/images/avi.jpg)