- Home /
How do you Lerp light.color into multiple colors?
How do you lerp into multiple colors? I can only seem to lerp from Point A to Point B. How do I make it so that when it reaches Point B, it will automatically go to Point C?
I've tried googling multiple lerping but found nothing.
Here's the code I'm currently using. This only lerps from Red to Blue, but I want it to lerp from Blue to Green after lerping from Red to Blue, then back to Green to Red.
using UnityEngine;
using System.Collections;
public class colorLerp : MonoBehaviour {
Color r = color.red;
Color b = color.blue;
Color g = color.green;
float duration = 3;
// Use this for initialization
void Start () {
light.color = color.red
}
// Update is called once per frame
void Update () {
float t = Mathf.PingPong (Time.time, duration) / duration;
//This only lerps from Red to Blue
light.color = Color.Lerp (r, b, t);
//What I want is, if the color is currently blue, then lerp to green
//light.color = Color.Lerp (b, g, t);
//If the color is now green, then lerp back to red
//light.color = Color.Lerp (g, r, t);
//And I want to repeat this cycle forever
}
}
Is there I can do this in a simple and easy way?
Thanks!
have you looked at the docs ?
Answer by Bunny83 · Jul 07, 2014 at 10:41 AM
It's easier when you "lerp" manually, First you need to calculate your 3 lerp factors
float t = 3 * Mathf.Repeat(Time.time, duration)/duration; // goes from 0 - 3
float t1 = 1.0f - Mathf.Clamp01(t) + Mathf.Clamp01(t-2);
float t2 = Mathf.Clamp01(t ) - Mathf.Clamp01(t-1);
float t3 = Mathf.Clamp01(t-1) - Mathf.Clamp01(t-2);
light.color = r*t1 + g*t2 + b*t3;
Here's the "transition" table in relation to t
t 0 -> 1 -> 2 -> 3
t1 1 0 0 1
t2 0 1 0 0
t3 0 0 1 0
Haven't tested but should work ;) Next step would be to generalize the algorithm with an array so it can be used for any number of colors. However the above should work for your problem.
public static Color AdvColorLerp(float t, params Color[] colors)
{
int c = colors.Length-1; // number of transitions
t = Mathf.Clamp01(t)*c; // expand t from 0-1 to 0-c
int index = Mathf.Clamp(Mathf.Floor(t),0,c-1); // get current index and clamp
t -= index; // subract the index to get back a value of 0-1
return Color.Lerp(colors[index],colors[index+1],t);
}
With that function you can do this:
float t = Mathf.Repeat(Time.time, duration) / duration;
light.color = AdvColorLerp(t,Color.red, Color.blue, Color.green, Color.red);
// or
Color[] colors = new Color[]{Color.red, Color.blue, Color.green, Color.red};
//[...]
float t = Mathf.Repeat(Time.time, duration) / duration;
light.color = AdvColorLerp(t,colors);
edit
However there's a simpler and more Unity-like way ;) The Gradient
While it is possible to setup the color and alpha values in a script, it's way easier to use the gradient editor. Just declare a public variables like this:
public Gradient gradient;
and it should show up in the inspector:
Inside the code you can use it's Evaluate method to sample a color value at the desired percentage:
float t = Mathf.Repeat(Time.time, duration) / duration;
light.color = gradient.Evaluate(t);
ps:
$$anonymous$$eep in $$anonymous$$d that lerping between red and green will be a bit darker in the middle since both colors are only half in. To keep them at more or less full brightness you need to insert the mix colors as well:
Answer by Nerevar · Jul 07, 2014 at 10:27 AM
Hello,
I would do like this:
using UnityEngine;
using System.Collections;
public class colorLerp : MonoBehaviour {
Color[] LerpTab = new Color[3];
public float ColorTransTime = 2f;
int looper = 0;
// Use this for initialization
void Start () {
LerpTab [0] = Color.red;
LerpTab [1] = Color.blue;
LerpTab [2] = Color.green;
light.color = Color.red;
}
// Update is called once per frame
void Update () {
float t = Time.deltaTime* ColorTransTime;
light.color = Color.Lerp (light.color, LerpTab[looper], t);
if(light.color == LerpTab[looper])
{
looper ++;
looper = looper%LerpTab.Length;
}
}
}
Not tested though, tell me if there is a bug and I will edit it :p
EDIT: I Modified the algorithm, I tested it and this works ! :p
From the description it seems he want a repeating pattern and not a pingpong ;) Also your condition will almost never be true since t is most likely never exactly 1.0.
Right i thought t was something else, but the ping pong was in his code from the start :p
replace t in update by :
float t = Time.deltaTime* ColorTransTime;
declare ColorTransTime and adjust the value to fit the speed of the lerping
cheers
Your answer
Follow this Question
Related Questions
Color.Lerp doesn't work 1 Answer
Lerping 2 values on a shader on button press. 1 Answer
lerp transparency back and forth, while cycling through rainbow? 0 Answers
Color.lerp not working properly 2 Answers