Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by WinterboltGames · Feb 28, 2020 at 05:25 PM · color changegradientcolor.lerp

How does Gradient.Evaluate work internally?

I have been trying to make a structure similar to Unity's built-in Gradient.

What I'm stuck at getting my head around is the Gradient.Evaluate function, how does it work internally?

Any code snippets will be really helpful!

Comment
Add comment
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

1 Reply

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

Answer by Namey5 · Feb 28, 2020 at 09:51 PM

I would imagine it's a combination of lerps between colours based on their time values, but for simplicity's sake it can be done with one lerp, i.e;

 Color EvaluateGradient (float t, GradientColorKey[] keys)
 {
     //We can early exit if t is at the boundaries
     if (t <= keys[0].time)
         return keys[0].color;
     if (t >= keys[keys.Length - 1].time)
         return keys[keys.Length - 1].color;
 
     //Loop through each gradient segment
     for (int i = 0; i < keys.Length - 1; i++)
     {
         //Find the start and end keys of this segment
         GradientColorKey startKey = keys[i];
         GradientColorKey endKey = keys[i + 1];
 
         //We only need to do any work if t is in the current segment
         if (t < endKey.time)
         {
             //Find t in relation to the current segment
             float localTime = Mathf.Clamp01 ((t - startKey.time) / (endKey.time - startKey.time));
             return Color.Lerp (startKey.color, endKey.color, localTime);
         }
     }
 
     //We will only get here if the last key is placed early, so return that
     return keys[keys.Length - 1].color;
 }
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 Namey5 · Feb 28, 2020 at 10:55 PM 0
Share

I should also note that the above will only work if the keys are in order (ascending based on time). If they aren't, you can sort them;

 public static void Sort$$anonymous$$eys (ref GradientColor$$anonymous$$ey[] keys)
 {
     for (int i = 0; i < keys.Length; i++)
     {
         for (int j = i + 1; j < keys.Length; j++)
         {
             if (keys[j].time < keys[i].time)
             {
                 GradientColor$$anonymous$$ey tmp = keys[i];
                 keys[i] = keys[j];
                 keys[j] = tmp;
             }
         }
     }
 }
avatar image WinterboltGames · Feb 29, 2020 at 01:20 PM 0
Share

Absolutely fantastic! Thanks a lot!

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

121 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I loop through all colours in code? 1 Answer

smoothly switch between 2 colors that are using lerp 2 Answers

How do I get a color gradient on a voxel terrain? 0 Answers

Apply gradient between two colors on x amount of objects 1 Answer

Unable to Color.Lerp in two directions (Solved) 1 Answer


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