- Home /
change Mesh.colors over time help?
Hello all,
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[i] = Color.Lerp(palet1, palet, vertices[i].y);
}
// Update is called once per frame
void Update () {
mesh.colors = colors;
if (on == false)
{
for (int i = 0; i < vertices.Length; i++)
colors[i] = Color.Lerp(Color.Lerp(palet1,palet3, currentTime), Color.Lerp(palet, palet2, currentTime), vertices[i].y);
}
}
if this code works, i am gonna buy you a coffe! Thanks for sharing!
Answer by Lilius · Sep 12, 2016 at 04:59 AM
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.
btw: you should assign the color array back to the mesh after you applied the changes, Otherwise all your changes will lag one frame behind.
Answer by Sajalsh25 · Sep 14, 2016 at 02:55 PM
Here's The Code -
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.
Happy Unitying!
ANY QUESTIONS ? FEEL FREE TO ASK!
Your answer
Follow this Question
Related Questions
Change colour of int when reaches specific number 1 Answer
Shrink and change color of GUI 0 Answers
Text Mesh, change color 1 Answer
Color lerp once? 2 Answers
Smooth gradient between colours? 1 Answer