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 tophatGrizzly9 · Nov 01, 2017 at 01:17 AM · shader programmingcustom shader

Color of vertex based on player distance

Hello, I am trying to figure out how to program a shader to change colors based on the player's position, similar to the picture I've attached.

alt text

What I am envisioning is the player moves around the game, and the GameObjects that have this shader will change colors based on how far away the player is from it. For this custom shader, each vertex would calculate the distance from the player, and then set the vertex to a specified color. Once all of the vertices have a color, it would rasterize the polygons. I am currently going over catlike coding tutorials to try and get an idea (http://catlikecoding.com/unity/tutorials/rendering/part-2/), but so far no success. Any reference material, advice, or examples would be much appreciated!

question.png (311.7 kB)
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
0

Answer by tophatGrizzly9 · Nov 25, 2017 at 04:02 AM

FIgure out how to do it with a friends help. It is a nieve way of doing it, but it works for the time being.

 Shader "Unlit/Distance"
 {
     Properties
     {
          _MainTex ("Base (RGB)", 2D) = "white" {}
          _FirstColor ("Color in First", Color) = (1, 0, 0, 1)
          _SecondColor("Color in Second", Color) = (1, 1, 0, 1)
          _ThirdColor ("Color in Third", Color) = (0, 1, 0, 1)
          _FourthColor ("Color in Fourth", Color) = (0, 1, 1, 1)
          _FifthColor("Color in Fifth", Color) = (0, 0, 1, 1)
          _SixthColor ("Color in Sixth", Color) = (1, 0, 1, 1)
          
          
          _FirstDistance ("First Distance", Float) = 1
          _SecondDistance ("Second Distance", Float) = 2
          _ThirdDistance ("Third Distance", Float) = 4
          _FourthDistance ("Fourth Distance", Float) = 8
          _FifthDistance ("Fifth Distance", Float) = 16
          _SixthDistance ("Sixth Distance", Float) = 32
          
         _Threshold ("Threshold", Range(0.0, 1.0)) = 1.
     }
      SubShader 
      {
          Tags { "RenderType"="Opaque" }
 
          
          CGPROGRAM
          #pragma surface surf Lambert
  
          sampler2D _MainTex;
  
          struct Input 
          {
              float2 uv_MainTex;
              float3 worldPos;
          };
  
          float _FirstDistance;
          float _SecondDistance;
          float _ThirdDistance;
          float _FourthDistance;
          float _FifthDistance;
          float _SixthDistance;
          half4 _FirstColor;
          half4 _SecondColor;
          half4 _ThirdColor;
          half4 _FourthColor;
          half4 _FifthColor;
          half4 _SixthColor;
          half4 _distanceColor;
          float _Threshold;
         
          void surf (Input IN, inout SurfaceOutput o) 
          {
              half4 c = tex2D (_MainTex, IN.uv_MainTex);
              float dist = distance(_WorldSpaceCameraPos, IN.worldPos);
                          
             if(dist < _FirstDistance){
                 //half weight = 1.0;
                 _distanceColor = _FirstColor;
             }
             
             if(dist >= _FirstDistance && dist < _SecondDistance){
                 half weight = (dist - _FirstDistance) / (_SecondDistance - _FirstDistance);
                 _distanceColor = lerp(_FirstColor, _SecondColor, weight);
             }
                 
             if(dist >= _SecondDistance && dist < _ThirdDistance){
                 half weight = (dist - _SecondDistance) / (_ThirdDistance - _SecondDistance);
                 _distanceColor = lerp(_SecondColor, _ThirdColor, weight);
             }
                 
             if(dist >= _ThirdDistance && dist < _FourthDistance){
                 half weight = (dist - _ThirdDistance) / (_FourthDistance - _ThirdDistance);
                 _distanceColor = lerp(_ThirdColor, _FourthColor, weight);
             }
             
             if(dist >= _FourthDistance && dist < _FifthDistance){
                 half weight = (dist - _FourthDistance) / (_FifthDistance - _FourthDistance);
                 _distanceColor = lerp(_FourthColor, _FifthColor, weight);
             }
             
             if(dist >= _FifthDistance && dist < _SixthDistance){
                 half weight = (dist - _FifthDistance) / (_SixthDistance - _FifthDistance);
                 _distanceColor = lerp(_FifthColor, _SixthColor, weight);
             }
             
             if(dist >= _SixthDistance){
                 //half weight = 1;
                 _distanceColor = _SixthColor;
             }
             
              o.Albedo = c.rgb * _distanceColor.rgb;
              o.Alpha = c.a;
              o.Emission = c.rgb * pow(_distanceColor.rgb, _Threshold);
          }
         
         ENDCG
      } 
      FallBack "Diffuse"
 }

alt text


rambow-shader-2.png (458.4 kB)
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

72 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

Related Questions

How to achieve shadows in custom Vertex/Frag shaders? 0 Answers

Find position of pixel that's currently being calculated in shadergraph custom function 0 Answers

Soft-Edge transparent Diffuse/Bump Shader WITH Lightning 0 Answers

How can I draw a X*X pixels rect to fill a X*X RenderTexture? 1 Answer

Problem with alpha blending on shader Unity 5 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