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
0
Question by LinkUpGames · Jan 02, 2017 at 07:19 PM · c#meshshaderscolor.lerpcolor32

Spawned Object Gradient Mesh Colors?

Hi there, so I am trying to make a game similar to the stack game by Ketchapp based on this tutorial: https://www.youtube.com/watch?v=I19cC_HcyC0

I've already finished the tutorial and am now just making my own changes to make it my own project. One of the things the creator doesn't go over is a gradual change in color for each of the objects in the stack.

The version in the tutorial uses this code here:

 public Color32[] gameColors = new Color32[4];
 
  private void ColorMesh(Mesh mesh)
     {
         Vector3[] vertices = mesh.vertices;
         Color32[] colors = new Color32[vertices.Length];
         float f = Mathf.Sin(scoreCount * 0.25f);
 
         for (int i = 0; i < vertices.Length; i++)
             colors[i] = Lerp4(gameColors[0], gameColors[1], gameColors[2], gameColors[3], f);
 
         mesh.colors32 = colors;
     }
 
 private Color32 Lerp4(Color32 a, Color32 b, Color32 c, Color32 d, float t)
     {
         if (t < 0.33f)
         {
             return Color.Lerp(a, b, t / 0.33f);
         }
         else if (t < 0.33f)
         {
             return Color.Lerp(b, c, (t - 0.33f) / 0.33f);
         }
         else
             return Color.Lerp(c, d, (t - 0.66f) / 0.66f);
     }

I thought by increasing the number of colors within the array and adding that to the Lerp would solve this. Only to realize this is in fact randomly chosen instead of a set change. I don't really have any experience with Mesh Color related coding so I'm wondering how I could go about changing this to just consistently change the color overtime?

Any help would be appreciated, Thank you!

Comment
Add comment · Show 3
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 RobAnthem · Jan 02, 2017 at 07:23 PM 0
Share

Shouldn't you be altering the texture color, not the mesh color?

avatar image LinkUpGames RobAnthem · Jan 02, 2017 at 07:28 PM 0
Share

Not really sure, like I said I've never worked with altering colors in this way. The tutorial changed the $$anonymous$$esh which does seem odd now that I think about it but I'm not sure how to make a gradient using textures either.

avatar image Fluffy_Kaeloky · Jan 03, 2017 at 09:47 PM 0
Share

Did you see my answer ? Did it help you ? If this resolved your issue, mark it as accepted and close the thread.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Fluffy_Kaeloky · Jan 02, 2017 at 10:10 PM

Are you trying to do something similar to this ? (For the colors I mean) : https://www.youtube.com/watch?v=ad5HoAUyJao

If so, I used the HSB/HSV colors (https://en.wikipedia.org/wiki/HSL_and_HSV) and colorized the obstacles like this :

 go.GetComponentInChildren<Renderer>().material.color = new HSBColor(Mathf.PingPong(Time.time / 10.0f, 1.0f), 1.0f, 0.7f).ToColor();

An here's the content of HSBColors.cs (I edited the script, but it's not mine, I can't remember where I found it, sorry) :

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class HSBColor
 {
     public float h;
     public float s;
     public float b;
     public float a;
 
     public HSBColor(float h, float s, float b, float a)
     {
         this.h = h;
         this.s = s;
         this.b = b;
         this.a = a;
     }
 
     public HSBColor(float h, float s, float b)
     {
         this.h = h;
         this.s = s;
         this.b = b;
         this.a = 1f;
     }
 
     public HSBColor(Color col)
     {
         HSBColor temp = FromColor(col);
         h = temp.h;
         s = temp.s;
         b = temp.b;
         a = temp.a;
     }
 
     public static HSBColor FromColor(Color color)
     {
         HSBColor ret = new HSBColor(0f, 0f, 0f, color.a);
 
         float r = color.r;
         float g = color.g;
         float b = color.b;
 
         float max = Mathf.Max(r, Mathf.Max(g, b));
 
         if (max <= 0)
         {
             return ret;
         }
 
         float min = Mathf.Min(r, Mathf.Min(g, b));
         float dif = max - min;
 
         if (max > min)
         {
             if (g == max)
             {
                 ret.h = (b - r) / dif * 60f + 120f;
             }
             else if (b == max)
             {
                 ret.h = (r - g) / dif * 60f + 240f;
             }
             else if (b > g)
             {
                 ret.h = (g - b) / dif * 60f + 360f;
             }
             else
             {
                 ret.h = (g - b) / dif * 60f;
             }
             if (ret.h < 0)
             {
                 ret.h = ret.h + 360f;
             }
         }
         else
         {
             ret.h = 0;
         }
 
         ret.h *= 1f / 360f;
         ret.s = (dif / max) * 1f;
         ret.b = max;
 
         return ret;
     }
 
     public static Color ToColor(HSBColor hsbColor)
     {
         float r = hsbColor.b;
         float g = hsbColor.b;
         float b = hsbColor.b;
         if (hsbColor.s != 0)
         {
             float max = hsbColor.b;
             float dif = hsbColor.b * hsbColor.s;
             float min = hsbColor.b - dif;
 
             float h = hsbColor.h * 360f;
 
             if (h < 60f)
             {
                 r = max;
                 g = h * dif / 60f + min;
                 b = min;
             }
             else if (h < 120f)
             {
                 r = -(h - 120f) * dif / 60f + min;
                 g = max;
                 b = min;
             }
             else if (h < 180f)
             {
                 r = min;
                 g = max;
                 b = (h - 120f) * dif / 60f + min;
             }
             else if (h < 240f)
             {
                 r = min;
                 g = -(h - 240f) * dif / 60f + min;
                 b = max;
             }
             else if (h < 300f)
             {
                 r = (h - 240f) * dif / 60f + min;
                 g = min;
                 b = max;
             }
             else if (h <= 360f)
             {
                 r = max;
                 g = min;
                 b = -(h - 360f) * dif / 60 + min;
             }
             else
             {
                 r = 0;
                 g = 0;
                 b = 0;
             }
         }
 
         return new Color(Mathf.Clamp01(r), Mathf.Clamp01(g), Mathf.Clamp01(b), hsbColor.a);
     }
 
     public Color ToColor()
     {
         return ToColor(this);
     }
 
     public override string ToString()
     {
         return "H:" + h + " S:" + s + " B:" + b;
     }
 
     public static HSBColor Lerp(HSBColor a, HSBColor b, float t)
     {
         float h, s;
 
         if (a.b == 0)
         {
             h = b.h;
             s = b.s;
         }
         else if (b.b == 0)
         {
             h = a.h;
             s = a.s;
         }
         else
         {
             if (a.s == 0)
             {
                 h = b.h;
             }
             else if (b.s == 0)
             {
                 h = a.h;
             }
             else
             {
                 float angle = Mathf.LerpAngle(a.h * 360f, b.h * 360f, t);
                 while (angle < 0f)
                     angle += 360f;
                 while (angle > 360f)
                     angle -= 360f;
                 h = angle / 360f;
             }
             s = Mathf.Lerp(a.s, b.s, t);
         }
         return new HSBColor(h, s, Mathf.Lerp(a.b, b.b, t), Mathf.Lerp(a.a, b.a, t));
     }
 }
 
Comment
Add comment · 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
0

Answer by Palass64 · Aug 01, 2017 at 01:46 PM

I'm also having this kind of problem.

Any other option then using HSB/HSV colors ?

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

273 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 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 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

Multiple Cars not working 1 Answer

Making a mesh transparent 1 Answer

Distribute terrain in zones 3 Answers

How to Move two spheres over the surface of irregular shapes meshes towards the closest X and Y boundary of mesh without leaving its trigger till end.,? 0 Answers

Dymanic Mesh Hiding 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