Monday, January 14, 2019

c# - How to Color.Lerp between multiple colors?


I have found it rather difficult to find a solution to this in the Unity documents.


Color.Lerp(Color a, Color b, float t) is a function that gradually changes a color according to a step t, giving it the final value of the Color b.


How do I Lerp between multiple colors one after another?



Answer



Let _colors be an array of colors let LENGHT be the number of colors in the array let t be the 0..1 float value


float scaledTime = t * (float) (LENGHT - 1);
Color oldColor = _colors[(int) scaledTime];
Color newColor = _colors[(int) (scaledTime + 1f)];

float newT = scaledTime - Mathf.Round(scaledTime);

finally you can use Lerp


Color.Lerp(oldColor, newColor, newT)

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...