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 /
avatar image
1
Question by mealone · Sep 12, 2016 at 04:15 AM · meshcolortimechangegradient

change Mesh.colors over time help?

Hello all,

I have a mesh and I am using mesh.colors to get a gradient. I am trying to get the gradient colors to change over time. I tried this but I believe that I am completely off here, and in addition the code did not work. can someone help me to change the colors in the vertical gradient over a timeframe of 5 seconds? Here is the code that I am using:

     public Color palet;
     public Color palet1;
     public Color palet2;
     public Color palet3;
     public Color palet4;
     public Color palet5;
     public bool on = true;
     public float currentTime = 0f;
     public Mesh mesh;
     Vector3[] vertices;
     public Color[] colors;
 
     // Use this for initialization
     void Start()
     {
         mesh = GetComponent<MeshFilter>().mesh;
         vertices = mesh.vertices;
         colors = new Color[vertices.Length];
 
         for (int i = 0; i < vertices.Length; i++)
             colors[i] = Color.Lerp(palet1, palet, vertices[i].y);
 
         }
 
 
 // Update is called once per frame
 void Update () {
         mesh.colors = colors;
 
         if (on == false)
         {
             for (int i = 0; i < vertices.Length; i++)
                 colors[i] = Color.Lerp(Color.Lerp(palet1,palet3, currentTime), Color.Lerp(palet, palet2, currentTime), vertices[i].y);
         }
     }

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 Sajalsh25 · Sep 14, 2016 at 01:01 PM 0
Share

if this code works, i am gonna buy you a coffe! Thanks for sharing!

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Lilius · Sep 12, 2016 at 04:59 AM

currentTime is always the same. In Update you should modify it: currentTime -= Time.deltaTime;

The third value in your lerp needs to be between 0 and 1, so if you want currenTime to start from 5 you should change it to Color.Lerp(palet1,palet3, currentTime * 0.2f). Now your mesh should change color over time. You also need a shader that supports vertex colors, if you use any standard shaders the mesh will be just white.

edit: I tested it with plane first and got same color for whole mesh. Using a mesh that actually has different y coordinates for vertices gives gradient color.

Comment
Add comment · Show 1 · 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 · Sep 14, 2016 at 06:25 PM 0
Share

btw: you should assign the color array back to the mesh after you applied the changes, Otherwise all your changes will lag one frame behind.

avatar image
0

Answer by Sajalsh25 · Sep 14, 2016 at 02:55 PM

Here's The Code -

      Mesh gradientPlaneMesh;
      void Start()
      {
    //gameobject which has the mesh you want to lerp the color of
    gradientPlaneMesh = GameObject.Find("Gradient Plane").GetComponent<MeshFilter>().mesh;
     }
     void Update()
     {
   Color[] meshColor = gradientPlaneMesh.colors;
   Color[] newColor = new Color[4]{topColor,topColor,bottomColor,bottomColor};
 //MY MESH HAS 4 VERTICES, you can modify this number to your requirement
     gradientPlaneMesh.colors = new Color[4] {Color.Lerp(meshColor[0],newColor[0],Time.deltaTime*5f),Color.Lerp(meshColor[0],newColor[0],Time.deltaTime*5f),Color.Lerp(meshColor[2],newColor[2],Time.deltaTime*5f),Color.Lerp(meshColor[2],newColor[2],Time.deltaTime*5f)};
     }

i know i am changing only the first and third color of my mesh but i think it works for me you cna modify this code as you want, i just wanted to Make this clear that you can Change the mesh color as color.Lerp.

Happy Unitying!

ANY QUESTIONS ? FEEL FREE TO ASK!

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

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

61 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

Related Questions

Change colour of int when reaches specific number 1 Answer

Shrink and change color of GUI 0 Answers

Text Mesh, change color 1 Answer

Color lerp once? 2 Answers

Smooth gradient between colours? 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