Search Unity

Adding shadows to this shader ?

Discussion in 'Shaders' started by Elzean, Jan 22, 2017.

  1. Elzean

    Elzean

    Joined:
    Nov 25, 2011
    Posts:
    584
    Hey here is a version off "Nature/Tree Soft Occlusion Leaves" with an addition to receive fog in differed rendering:

    Code (CSharp):
    1. Shader "Nature/Tree Soft Occlusion Leaves with fog" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Main Texture", 2D) = "white" {  }
    5.         _Cutoff ("Alpha cutoff", Range(0.25,0.9)) = 0.5
    6.         _BaseLight ("Base Light", Range(0, 1)) = 0.35
    7.         _AO ("Amb. Occlusion", Range(0, 10)) = 2.4
    8.         _Occlusion ("Dir Occlusion", Range(0, 20)) = 7.5
    9.        
    10.         // These are here only to provide default values
    11.         [HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1)
    12.         [HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1)
    13.         [HideInInspector] _SquashAmount ("Squash", Float) = 1
    14.     }
    15.    
    16.     SubShader {
    17.         Tags {
    18.             "Queue" = "AlphaTest"
    19.             "IgnoreProjector"="True"
    20.             "RenderType" = "TreeTransparentCutout"
    21.             "DisableBatching"="True"
    22.         }
    23.         Cull Off
    24.         ColorMask RGB
    25.        
    26.         Pass {
    27.             Lighting On
    28.        
    29.             CGPROGRAM
    30.             #pragma vertex leaves
    31.             #pragma fragment frag
    32.             #pragma multi_compile_fog
    33.             #include "UnityBuiltin2xTreeLibrary.cginc"
    34.            
    35.             sampler2D _MainTex;
    36.             fixed _Cutoff;
    37.            
    38.             fixed4 frag(v2f input) : SV_Target
    39.             {
    40.                 fixed4 c = tex2D( _MainTex, input.uv.xy);
    41.                 c.rgb *= input.color.rgb;
    42.                
    43.                 clip (c.a - _Cutoff);
    44.                 UNITY_APPLY_FOG(input.fogCoord, c);
    45.                 return c;
    46.             }
    47.             ENDCG
    48.         }
    49.        
    50.         Pass {
    51.             Name "ShadowCaster"
    52.             Tags { "LightMode" = "ShadowCaster" }
    53.            
    54.             CGPROGRAM
    55.             #pragma vertex vert
    56.             #pragma fragment frag
    57.             #pragma multi_compile_shadowcaster
    58.             #include "UnityCG.cginc"
    59.             #include "TerrainEngine.cginc"
    60.            
    61.             struct v2f {
    62.                 V2F_SHADOW_CASTER;
    63.                 float2 uv : TEXCOORD1;
    64.             };
    65.            
    66.             struct appdata {
    67.                 float4 vertex : POSITION;
    68.                 float3 normal : NORMAL;
    69.                 fixed4 color : COLOR;
    70.                 float4 texcoord : TEXCOORD0;
    71.             };
    72.             v2f vert( appdata v )
    73.             {
    74.                 v2f o;
    75.                 TerrainAnimateTree(v.vertex, v.color.w);
    76.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    77.                 o.uv = v.texcoord;
    78.                 return o;
    79.             }
    80.            
    81.             sampler2D _MainTex;
    82.             fixed _Cutoff;
    83.                    
    84.             float4 frag( v2f i ) : SV_Target
    85.             {
    86.                 fixed4 texcol = tex2D( _MainTex, i.uv );
    87.                 clip( texcol.a - _Cutoff );
    88.                 SHADOW_CASTER_FRAGMENT(i)
    89.             }
    90.             ENDCG  
    91.         }
    92.     }
    93.    
    94.     // This subshader is never actually used, but is only kept so
    95.     // that the tree mesh still assumes that normals are needed
    96.     // at build time (due to Lighting On in the pass). The subshader
    97.     // above does not actually use normals, so they are stripped out.
    98.     // We want to keep normals for backwards compatibility with Unity 4.2
    99.     // and earlier.
    100.     SubShader {
    101.         Tags {
    102.             "Queue" = "Transparent-99"
    103.             "IgnoreProjector"="True"
    104.             "RenderType" = "TransparentCutout"
    105.         }
    106.         Cull Off
    107.         ColorMask RGB
    108.         Pass {
    109.             Tags { "LightMode" = "Vertex" }
    110.             AlphaTest GEqual [_Cutoff]
    111.             Lighting On
    112.             Material {
    113.                 Diffuse [_Color]
    114.                 Ambient [_Color]
    115.             }
    116.             SetTexture [_MainTex] { combine primary * texture DOUBLE, texture }
    117.         }      
    118.     }
    119.  
    120.     Dependency "BillboardShader" = "Hidden/Nature/Tree Soft Occlusion Leaves Rendertex"
    121.     Fallback Off
    122. }
    123.  

    and this the default "Nature/Tree Soft Occlusion Bark" :

    Code (CSharp):
    1. Shader "Nature/Tree Soft Occlusion Bark" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,0)
    4.         _MainTex ("Main Texture", 2D) = "white" {}
    5.         _BaseLight ("Base Light", Range(0, 1)) = 0.35
    6.         _AO ("Amb. Occlusion", Range(0, 10)) = 2.4
    7.        
    8.         // These are here only to provide default values
    9.         [HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1)
    10.         [HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1)
    11.         [HideInInspector] _SquashAmount ("Squash", Float) = 1
    12.     }
    13.    
    14.     SubShader {
    15.         Tags {
    16.             "IgnoreProjector"="True"
    17.             "RenderType" = "TreeOpaque"
    18.             "DisableBatching"="True"
    19.         }
    20.  
    21.         Pass {
    22.             Lighting On
    23.        
    24.             CGPROGRAM
    25.             #pragma vertex bark
    26.             #pragma fragment frag
    27.             #pragma multi_compile_fog
    28.             #include "UnityBuiltin2xTreeLibrary.cginc"
    29.            
    30.             sampler2D _MainTex;
    31.            
    32.             fixed4 frag(v2f input) : SV_Target
    33.             {
    34.                 fixed4 col = input.color;
    35.                 col.rgb *= tex2D( _MainTex, input.uv.xy).rgb;
    36.                 UNITY_APPLY_FOG(input.fogCoord, col);
    37.                 UNITY_OPAQUE_ALPHA(col.a);
    38.                 return col;
    39.             }
    40.             ENDCG
    41.         }
    42.        
    43.         Pass {
    44.             Name "ShadowCaster"
    45.             Tags { "LightMode" = "ShadowCaster" }
    46.            
    47.             CGPROGRAM
    48.             #pragma vertex vert
    49.             #pragma fragment frag
    50.             #pragma multi_compile_shadowcaster
    51.             #include "UnityCG.cginc"
    52.             #include "TerrainEngine.cginc"
    53.            
    54.             struct v2f {
    55.                 V2F_SHADOW_CASTER;
    56.             };
    57.            
    58.             struct appdata {
    59.                 float4 vertex : POSITION;
    60.                 float3 normal : NORMAL;
    61.                 fixed4 color : COLOR;
    62.             };
    63.             v2f vert( appdata v )
    64.             {
    65.                 v2f o;
    66.                 TerrainAnimateTree(v.vertex, v.color.w);
    67.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    68.                 return o;
    69.             }
    70.            
    71.             float4 frag( v2f i ) : SV_Target
    72.             {
    73.                 SHADOW_CASTER_FRAGMENT(i)
    74.             }
    75.             ENDCG  
    76.         }
    77.     }
    78.    
    79.     Dependency "BillboardShader" = "Hidden/Nature/Tree Soft Occlusion Bark Rendertex"
    80.     Fallback Off
    81. }
    82.  

    Does anyone know how to modify those to add the possibility to receive shadows ?

    Thanks!