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
0
Question by Youngapprentice · Feb 14, 2012 at 02:33 AM · colorcubesimpleeasy

Changing Color Based on Z position

Hi, all! I have a cube, and I would like to change its renderer.material.color from one RGB to another RGB In between two points on the Z-axis in space. For instance, if my start parameter is 1.0 on the Z axis and my starting color is yellow, and my ending color is red at 2.0 on the Z-axis, when the cube is at the start (1.0), it is yellow. At 1.5 on the Z-axis the cube will be orange, and at 2.0 on the Z-axis the cube will be red. So I am asking if anyone knows how to link an RGB value to a position and turn it into a percentile and plug it into the RGB value.

Thanks! Happy gaming!- YA

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

2 Replies

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

Answer by Peter G · Feb 14, 2012 at 03:48 AM

You could do it in a shader.... if you want really accurate gradients on the object itself, and are willing to put excessive amounts of processing power into such a menial task.

Or, you could do it in a script and get a good looking result that's considerably cheaper. Every Update() you want to get the objects position and use that to interpolate from one color to the other.

  1. Create a shader that takes an extra color parameter or just use the MainColor of a built-in one.

  2. Create a simple script that does something like this:

       var leftBoundCol : Color;
          var rightBoundCol : Color;
     
          var leftBound : float;
          var rightBound : float;
     
          function Update () {
                  var lerpParam = Mathf.InverseLerp(leftBound, rightBound , transform.position.z);
                  renderer.material.color = Color.Lerp(leftBoundCol , rightBoundCol , lerpParam);
          }
     I think everything in this is pretty self-explanatory.  You set the endpoints, and the endpoint-colors.  Then, based on the objects, position between the two endpoints, it assigns the main color of the object to be something in-between the two.
    
    

This in turn changes the color of the entire object. If you were to do it in a shader, you could get per vertex or even per fragment changes in color, but as I mentioned earlier, that is considerably more expensive.

Doing it via script is one lerp per frame. Doing it in a shader is at least as many vertices as you have per frame. So unless its a cube, assume a few hundred to a few thousand lerps per second.

Comment
Add comment · Show 3 · 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 Youngapprentice · Feb 14, 2012 at 09:10 PM 0
Share

Thank you. Haha luckily I am using cubes. I am going for a retro stye so they will only be one color. :) Last thing: What is a Lerp?

avatar image Peter G · Feb 15, 2012 at 01:14 AM 0
Share

Lerp = Linear intER$$anonymous$$te. It linearly moves from the start value to the end value. There are a few ways of doing it, but here's and easy to understand way of explaining it:

 Actual Value = (1 - param) * initialValue + param * finalValue

When the parameter is 0, we are at the initial value. When the parameter is 1, we are at the final value. and When its somewhere in-between, we're somewhere in-between.

avatar image Youngapprentice · Feb 15, 2012 at 01:33 AM 0
Share

Thank you very much. You have no idea how much help you have been! Thank you!

avatar image
0

Answer by akekajoo · Feb 14, 2012 at 04:23 AM

Off the top of my head, I think you would do something like this to blend between yellow and red based on a z-position between 1 and 2:

     renderer.material.color = Color.Lerp(
         Color.red, 
         Color.yellow, 
         2.0f - transform.position.z);
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

7 People are following this question.

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

Related Questions

How can i make my cube not be a 3D parallelogram when i rotate it under its parent? 1 Answer

Quick Custom Inspector Question 1 Answer

Temporary Rotation 2 Answers

How to toggle a box color on & Off? 1 Answer

How do I make a grey cube transparent? 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