• 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
Question by thenachotech1113 · Nov 09, 2014 at 06:51 PM · planetlinearcolor.lerp

interpolate color depending on distance from planet

hello everyone, I want to make it so the cameras bacground color changed gradualy as I got further or closer to the planet. i decided to go with color.Lerp that allows me to interpolate between 2 colors based on an number between 0 and 1. I have tried using linear functions but it is not getting me anywhere. here is what i`ve got so far:

 //set atmosphere 
         if (closestPlanet != null){
             float plntDist = Vector3.Distance (transform.position, closestPlanet.transform.position);
             float planetRadius = (closestPlanet.transform.localScale.x/2);
             float yAxisPoint = -(planetRadius/(planetRadius - planetRadius/3));
             if (plntDist <  planetRadius + planetRadius / 3){
                 colInterpolationT = (1/(planetRadius - planetRadius / 3)) * plntDist + yAxisPoint; // t$$anonymous$$s is an attempt of a linear function (explained at the end)
             }
             else colInterpolationT = 0;
 
             currSkyColor = Color.Lerp(PossibleSkyColor[1], PossibleSkyColor[2], colInterpolationT);
             cam.backgroundColor = currSkyColor;
         }

any ideas or suggestions are very much apreciated. thank you all.

  • the linear function worked the following way

i used 2 points to get the formula: (0, planetRadius)(1, atmosphere radius) (atmosphere radius is a tird of the planets radius btw)

out of t$$anonymous$$s i got m = 1/(r - atmos) = 1 / ( planetRadius + planetRadius/3 - planetRadius)

finaly b = 0-planetRadius/ planetRadius -atmosphere radius)

Comment

People who like this

0 Show 4
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 fafase · Nov 09, 2014 at 06:55 PM 1
Share

But what is the problem?

Out of nothing, I would take the max distance to the planet and the min distance. With this, I can normalize the position and use it in the lerp.

avatar image Scribe · Nov 09, 2014 at 06:58 PM 1
Share

Is there a maximum distance you can be from the planet? What's not working for you? Are you getting errors?! PossibleSkyColor[1], PossibleSkyColor[2] <- are you sure this is correct, arrays are normally 0 indexed

avatar image thenachotech1113 · Nov 09, 2014 at 07:14 PM 0
Share

dammit, sry, so worried in explaining how it worked i forgot to add the problem, basicaly it was the index was out of range, thanks and sorry

avatar image devluz · Nov 09, 2014 at 10:46 PM 0
Share

where/how do you initialize PossibleSkyColor? You create it wrong or use it wrong. Color[] PossibleSkyColor = new Color[2]; will allow you to use PossibleSkyColor[0] and PossibleSkyColor[1] but not PossibleSkyColor[2]!

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

planetary gravity: falling "off" 3 Answers

Cube2Sphere 2 Answers

Space and Planets 1 Answer

Perlin Noise Spherical Terrain 2 Answers

Can someone add a 2D texture alpha mask to this planet shader 0 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