Search Unity

No Shadows on Colour by Height Shader

Discussion in 'Shaders' started by BratwurstZ, Nov 27, 2019.

  1. BratwurstZ

    BratwurstZ

    Joined:
    Jul 23, 2014
    Posts:
    2
    Hello,

    to preface this, I'm rather new to Unity and have no experience with shaders so far.

    So far I've got a game that basically generates a 100x100 world with random heights.

    It looks like this:
    As you can see, there are shadows under every tree.

    Now I've got a shader by Jason Beetham that colors the ground based on height. Sadly this shader outputs only flat colors:


    Now I wanted to "combine" my ground grass texture with this shader so it looks something like this:

    What I did was just fade the Standard shader with the grass texture on the Height Shader. But as you can see there are no shadows.

    Is there a simple way to get shadows to work? Is there maybe another way of doing this?

    Here's the shader code:
    Code (CSharp):
    1. Shader "Lit/Smooth Shaded Colour By Height"
    2. {
    3.     //Created By Jason Beetham
    4.     Properties
    5.     {
    6.         _First("Highest Point",float) = 1
    7.         _Second("Second Heighest",float) = 1
    8.         _Third("Third Heighest",float) = 1
    9.         _Fourth("Fourth Heighest",float) = 1
    10.         _FirstC("Highest Point Colour", Color) = (0,0,0,1)
    11.         _SecondC("Second Heighest Colour",Color) = (0,0,0,1)
    12.         _ThirdC("Third Colour",Color) = (0,0,0,1)
    13.         _FourthC("Fourth Colour",Color) = (0,0,0,1)
    14.         _FifthC("Fifth Colour",Color) = (0,0,0,1)
    15.     }
    16.         SubShader
    17.         {
    18.         Lighting on
    19.         Pass
    20.             {
    21.             Cull Back
    22.                 CGPROGRAM
    23.                 #pragma vertex vert
    24.                 #pragma fragment frag
    25.                 #include "UnityCG.cginc"
    26.                 #include "Lighting.cginc"
    27.                 #include "AutoLight.cginc"
    28.                 #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    29.                struct appdata
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 fixed4 diff : COLOR;
    33.                 float3 normal : NORMAL;
    34.                 float4 vertex : POSITION;
    35.             }
    36.  
    37.  
    38.  
    39.  
    40. ;
    41.             struct v2f
    42.             {
    43.                 float2 uv : TEXCOORD0;
    44.                 fixed4 diff : COLOR;
    45.                 float3 normal : NORMAL;
    46.                 float4 vertex : POSITION;
    47.             }
    48.  
    49.  
    50.  
    51.  
    52. ;
    53.             float4 _FirstC;
    54.             float4 _SecondC;
    55.             float4 _ThirdC;
    56.             float4 _FourthC;
    57.             float4 _FifthC;
    58.             float _Scale;
    59.             float _First;
    60.             float _Second;
    61.             float _Third;
    62.             float _Fourth;
    63.             float _Fifth;
    64.             float4 _Position;
    65.             v2f vert(appdata v)
    66.             {
    67.                 float4 tempPos = mul(unity_ObjectToWorld, float4(0,0,0,1));
    68.                 _Position = mul( unity_WorldToObject,tempPos);
    69.                 v2f OUT;
    70.                 OUT.normal = v.normal;
    71.                 OUT.vertex = UnityObjectToClipPos(v.vertex);
    72.                 if (v.vertex.y >= _First) {
    73.                     OUT.diff = lerp(_FirstC,_SecondC,(_First /v.vertex.y) +.1f);
    74.                 }
    75.  
    76.  
    77.                 else if (v.vertex.y >= _Second && v.vertex.y < _First) {
    78.                     OUT.diff = lerp(_ThirdC,_SecondC, (v.vertex.y / _First));
    79.  
    80.                 }
    81.  
    82.  
    83.                 else if (v.vertex.y >= _Third && v.vertex.y < _Second) {
    84.                    OUT.diff = lerp(_FourthC,_ThirdC, (v.vertex.y / _Second));
    85.                 }
    86.  
    87.  
    88.                 else if (v.vertex.y <= _Third && v.vertex.y > _Fourth){
    89.                      OUT.diff = lerp(_ThirdC,_FourthC, (v.vertex.y / _Third));
    90.                 }
    91.  
    92.                 else {
    93.                     OUT.diff = _FifthC;
    94.                 }
    95.  
    96.                 return OUT;
    97.             }
    98.  
    99.  
    100.  
    101.  
    102.  
    103.                 fixed4 frag(v2f i) : SV_Target
    104.                 {
    105.                 float4 col = i.diff;
    106.                 half3 lightColor = ShadeVertexLights(i.vertex, i.normal);
    107.                 col.rgb *= lightColor;
    108.                 return col;
    109.             }
    110.  
    111. ENDCG
    112. }
    113. }
    114. }
    Any help is appreciated. Thanks!
     
  2. BratwurstZ

    BratwurstZ

    Joined:
    Jul 23, 2014
    Posts:
    2
    Okay, maybe I need to ask a different question.

    Is there a way to add the option for a texture to that shader code? Basically all I want is to use this shader on a grass texture. That way I wouldn't have to use two materials and combine them by making the grass texture transparent or fade, which makes shadows disappear.

    Or is there another way of doing this?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,683