Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Leroterr · Jul 07, 2014 at 09:47 AM · colorlerptime.deltatimemathf.lerpcolor.lerp

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Klarax · Jul 07, 2014 at 10:19 AM 1
Share

have you looked at the docs ?

http://docs.unity3d.com/ScriptReference/Light-color.html

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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:

gradient

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);
 


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 07, 2014 at 11:53 AM 1
Share

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:

colorwheel colors

avatar image Leroterr · Jul 07, 2014 at 12:54 PM 0
Share

Thanks! Gradients worked for me.

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 07, 2014 at 11:00 AM 0
Share

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.

avatar image Nerevar · Jul 07, 2014 at 12:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Controlling duration of Color.Lerp in seconds 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges