I have a mesh and I am using mesh.colors to get a gradient. I am trying to get the gradient colors to change over time. I tried this but I believe that I am completely off here, and in addition the code did not work. can someone help me to change the colors in the vertical gradient over a timeframe of 5 seconds?
Here is the code that I am using:
public Color palet;
public Color palet1;
public Color palet2;
public Color palet3;
public Color palet4;
public Color palet5;
public bool on = true;
public float currentTime = 0f;
public Mesh mesh;
Vector3[] vertices;
public Color[] colors;
// Use this for initialization
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
colors = new Color[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
colors _= Color.Lerp(palet1, palet, vertices*.y);*_
}
// Update is called once per frame void Update () { mesh.colors = colors;
if (on == false) { for (int i = 0; i < vertices.Length; i++) colors = Color.Lerp(Color.Lerp(palet1,palet3, currentTime), Color.Lerp(palet, palet2, currentTime), vertices*.y);* } }
currentTime is always the same. In Update you should modify it: currentTime -= Time.deltaTime;
The third value in your lerp needs to be between 0 and 1, so if you want currenTime to start from 5 you should change it to Color.Lerp(palet1,palet3, currentTime * 0.2f). Now your mesh should change color over time. You also need a shader that supports vertex colors, if you use any standard shaders the mesh will be just white.
edit: I tested it with plane first and got same color for whole mesh. Using a mesh that actually has different y coordinates for vertices gives gradient color.
Mesh gradientPlaneMesh;
void Start()
{
//gameobject which has the mesh you want to lerp the color of
gradientPlaneMesh = GameObject.Find("Gradient Plane").GetComponent<MeshFilter>().mesh;
}
void Update()
{
Color[] meshColor = gradientPlaneMesh.colors;
Color[] newColor = new Color[4]{topColor,topColor,bottomColor,bottomColor};
//MY MESH HAS 4 VERTICES, you can modify this number to your requirement
gradientPlaneMesh.colors = new Color[4] {Color.Lerp(meshColor[0],newColor[0],Time.deltaTime*5f),Color.Lerp(meshColor[0],newColor[0],Time.deltaTime*5f),Color.Lerp(meshColor[2],newColor[2],Time.deltaTime*5f),Color.Lerp(meshColor[2],newColor[2],Time.deltaTime*5f)};
}
i know i am changing only the first and third color of my mesh but i think it works for me you cna modify this code as you want, i just wanted to Make this clear that you can Change the mesh color as color.Lerp.