Why Color.Lerp or Color32.Lerp doesnt work properly?
i have made a simple script to test Color32.Lerp to use it on my Directional Light Color Changing, but it seems like that Color32.Lerp or Color.Lerp only works downwards(backwards), i mean it Interpolates all the RGBA values only when they're higher than the Interpolated Color Variable, here is an example:
var damp : float = 0.005;
var first : Color32;
var second : Color32;
function Update ()
{
first = Color32.Lerp(first,second,damp);
}
and my "first" variable is black for example, and my "second" one is white, and it wont interpolate, it will be very dark grey (looks like it interpolates like that: RGBA from 255 * damp = 1.275 RGBA) and when "first" is white and "second" is black, it interpolates perfectly, so can someone tell me why this happens?
Answer by FlaSh-G · Jun 23, 2017 at 07:45 AM
when "first" is white and "second" is black, it interpolates perfectly
I am 100% sure this is not the case - it's still very bright grey, but you don't notice it. The reason you're not reaching your destination color is because you're using Lerp in a special way. Lerp is really simple to implement:
lerp(a, b, t) = a + (b - a) * t
It means that you get a as result when t is 0, and b when t is 1. And of course, a proportionally interpolated value inbetween when t is between 0 and 1, too. So if you want a proper linear interpolation, you would have to define a set a and a set b, then increase the value of t from 0 to 1 (or 1 to 0) over time.
What you did instead is to put in a constant t and use the updated a every frame. This leads to behaviour that does not match the original definition of what lerp is for - even though it is of course okay to use it that way if the result is what you want.
What happens is that you always go a set percentage of your remaining way to the destination. There is that Meg Ryan movie "I.Q." where she explains pretty much the same thing. Freely quoted: "When I go half of the way towards you, then again half of the remaining way, and again and again, I will never reach you because I will always leave the other half of the remaining way between us."
So since the (b - a) term becomes smaller with every iteration (because a gets closer to b), (b - a) * t becomes smaller as well. So in the beginning, your color value changes faster and then the change becomes smaller and smaller. As long as a is not equal to b and t is not 1, a will never become equal to b.
And that is why you never reach your "second" color. No matter which way you put in the parameters.
thanks for your explanation, i expected it to work like $$anonymous$$athf.Lerp but it seems to be different, for example:
var damp : float = 0.005;
var first : float;
var second : float;
function Update ()
{
first = $$anonymous$$athf.Lerp(first,second,damp);
}
Works but this one doesnt work:
var damp : float = 0.005;
var first : Color32;
var second : Color32;
function Update ()
{
first = Color32.Lerp(first,second,damp);
}
Which doesnt make any sense why Color32.Lerp works different :/
It should absolutely work the same. It's even implemented the same.
Haha, wait, you're using Color32? Use Color ins$$anonymous$$d maybe, it might be due to rounding the numbers every time.
thanks so much, it seems like Color32.Lerp has problems, not Color.Lerp, but Color's values are between 0 to 1, but Color32 from 0 to 255, i want Color32, but anyways thanks :) EDIT: nope, Color can be converted to Color32 like:
var first : Color = Color32(10,50,120,75)
And Color.Lerp works for both Color and Color32
Your answer
Follow this Question
Related Questions
Get color between 2 colors 0 Answers
Is there any way to make a gradient within unity?,Is there any way to make a gradient? 1 Answer
Why does my random colour generator always spit out the same colour? 0 Answers
How Do I Import A model with Colors? 0 Answers
how to change object's B color when object A is in range? 2D 1 Answer