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 /
  • Help Room /
avatar image
2
Question by rhysp · Jul 31, 2016 at 04:09 PM · shadersmobileshader writinglow poly

How do I adjust shader tint based on normal/face orientation?

Using a very handy shader reference from http://answers.unity3d.com/answers/882217/view.html, I adapted the solution to be used with flat shading/colours. I am new to shaders, so the solution is likely far from optimal, but available below.

This gets me mostly to the effect I am after, but as this shader is used on mesh that has faces strictly orient flat or vertical (like stairs), I have been trying to apply a darker tint to the vertical faces to achieve a pseudo-shadow effect.

I have struggled to learn how shaders can adapt to face orientation, and failed attempts result in an "invalid" pink shader that I'm failing to resolve. Any help is much appreciated, thank you.

 Shader "TintedHeight" {
 
    Properties 
    {
      _MainTex ("Base (RGB)", 2D) = "white" {}
      _HeightMin ("Height Min", Float) = -1
      _HeightMax ("Height Max", Float) = 1
      _ColorMin ("Tint Color At Min", Color) = (0,0,0,1)
      _ColorMax ("Tint Color At Max", Color) = (1,1,1,1)
    }
   
     SubShader {
         Pass {
 
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             float _HeightMax;
             float _HeightMin;
             fixed4 _ColorMin;
             fixed4 _ColorMax;
 
             struct Input
          {
            float3 worldPos;
          };
 
             struct v2f {
                 float4 pos : SV_POSITION;
                 fixed3 color : COLOR0;
             };
 
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = UnityObjectToClipPos(v.vertex);
                  float3 worldPos = mul (unity_ObjectToWorld, v.vertex).xyz;
                 float h = (_HeightMax - worldPos.y) / (_HeightMax - _HeightMin);
                 o.color = lerp (_ColorMax.rgba, _ColorMin.rgba, h);
                 return o;    
             }
 
             fixed4 frag (v2f i) : SV_Target
             {
                 return fixed4 (i.color, 1);
             }
             ENDCG
 
         }
     }
 }
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
2
Best Answer

Answer by Glurth · Jul 31, 2016 at 04:30 PM

I see that you are using the vertex position in your computation. This will adjust the color based upon the vertex location, rather than the face's direction. I suspect you want to use v.normal, which is a vector that defines the direction the face is pointing.

This page has an example, scroll down a bit to the "visualizing normals" section. https://docs.unity3d.com/Manual/SL-VertexProgramInputs.html

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 rhysp · Jul 31, 2016 at 04:51 PM 0
Share

Thank you for your suggestion, after investigation (and also referring to another question: http://answers.unity3d.com/questions/170589/shader-actual-view-directionnormal.html), my v2f solution had some adjustments using:

 float3 viewN = mul ((float3x3)UNITY_$$anonymous$$ATRIX_IT_$$anonymous$$V, v.normal);
 
 // Shade the vertical normals
 o.color = lerp (o.color.rgba, _ColorShadow.rgba, (1 - viewN.y) * 0.18f);
 
 // Curved shading
 o.color = lerp (o.color.rgba, _ColorShadow.rgba, viewN.x * 0.1f);
avatar image Noxury · Feb 08, 2018 at 07:29 PM 0
Share

I would like to know if this can be turned into a screen space camera shader and how, so the camera automaticly renders all it gets like this surface shader.

As an example, that will come in handy for a $$anonymous$$imap where the $$anonymous$$imap camera renders with height lerping as screen space effect and the environment that is rendered by the main camera should stay just like it is.

Hope anyone understand what I mean and thanks in advance.

avatar image astha7838 Noxury · Aug 19, 2020 at 08:37 PM 0
Share

@Noxury :were you able to apply the same concept in $$anonymous$$imap?

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

76 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

Related Questions

How to affect Shader _Color by the Tint Color of Button (Through Shader) 0 Answers

Standard shader or mobile/legacy shader for better performance on mobile? 0 Answers

Need help with projector shader 4 Answers

Is it possible to have a grid shader with gradient lines? (Without external assets) 0 Answers

Anisotropic Shader, How Does It Works? Help 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