Search Unity

Adding shadows to the detail mesh terrain shader in Unity3D

Discussion in 'Shaders' started by SirWhiteBeard, Sep 18, 2019.

  1. SirWhiteBeard

    SirWhiteBeard

    Joined:
    Feb 21, 2014
    Posts:
    8
    I'm trying to add a shadow pass (I think it's called a pass) so that my detail meshes (vegetation) will cast and receive shadows. I managed to overwrite the default terrain shader by coloring the detail meshes red, but I'm unsure how to add the shadow pass to this shader. I see different approaches of adding shadows to different kinds of shaders, and I'm unsure what the difference is between such shaders. (fragment, vertic, surface). I hope someone can help with adding the shadow pass and explaining how it works.

    Here is the code for the detail mesh shader in it's current state;

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Hidden/TerrainEngine/Details/WavingDoublePass" {
    4. Properties {
    5.     _WavingTint ("Fade Color", Color) = (.7,.6,.5, 0)
    6.     _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    7.     _WaveAndDistance ("Wave and distance", Vector) = (12, 3.6, 1, 1)
    8.     _Cutoff ("Cutoff", float) = 0.5
    9. }
    10.  
    11. SubShader {
    12.     Tags {
    13.         "Queue" = "Geometry+200"
    14.         "IgnoreProjector"="True"
    15.         "RenderType"="Grass"
    16.         "DisableBatching"="True"
    17.     }
    18.     Cull Off
    19.     LOD 200
    20.     ColorMask RGB
    21.  
    22. CGPROGRAM
    23. #pragma surface surf Lambert vertex:WavingGrassVert addshadow fullforwardshadows exclude_path:deferred
    24. #include "TerrainEngine.cginc"
    25.  
    26. sampler2D _MainTex;
    27. fixed _Cutoff;
    28.  
    29. struct Input {
    30.     float2 uv_MainTex;
    31.     fixed4 color : COLOR;
    32. };
    33.  
    34. void surf (Input IN, inout SurfaceOutput o) {
    35.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
    36.     //o.Albedo = c.rgb;
    37.     // Testing
    38.     o.Albedo = float3(1, 0, 0);
    39.     o.Alpha = c.a;
    40.     clip (o.Alpha - _Cutoff);
    41.     o.Alpha *= IN.color.a;
    42. }
    43. ENDCG
    44. }
    45.  
    46.     SubShader {
    47.         Tags {
    48.             "Queue" = "Geometry+200"
    49.             "IgnoreProjector"="True"
    50.             "RenderType"="Grass"
    51.         }
    52.         Cull Off
    53.         LOD 200
    54.         ColorMask RGB
    55.  
    56.         Pass {
    57.             Tags { "LightMode" = "Vertex" }
    58.             Material {
    59.                 Diffuse (1,1,1,1)
    60.                 Ambient (1,1,1,1)
    61.             }
    62.             Lighting On
    63.             ColorMaterial AmbientAndDiffuse
    64.             AlphaTest Greater [_Cutoff]
    65.             SetTexture [_MainTex] { combine texture * primary DOUBLE, texture }
    66.         }
    67.  
    68.         // Lightmapped
    69.         Pass
    70.         {
    71.             Tags{ "LIGHTMODE" = "VertexLM" "RenderType" = "Opaque" }
    72.  
    73.             CGPROGRAM
    74.  
    75.             #pragma vertex vert
    76.             #pragma fragment frag
    77.             #pragma target 2.0
    78.             #include "UnityCG.cginc"
    79.             #pragma multi_compile_fog
    80.             #define USING_FOG (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
    81.  
    82.             float4 _MainTex_ST;
    83.  
    84.             struct appdata
    85.             {
    86.                 float3 pos : POSITION;
    87.                 float3 uv1 : TEXCOORD1;
    88.                 float3 uv0 : TEXCOORD0;
    89.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    90.             };
    91.  
    92.             struct v2f
    93.             {
    94.                 float2 uv0 : TEXCOORD0;
    95.                 float2 uv1 : TEXCOORD1;
    96.             #if USING_FOG
    97.                 fixed fog : TEXCOORD2;
    98.             #endif
    99.                 float4 pos : SV_POSITION;
    100.  
    101.                 UNITY_VERTEX_OUTPUT_STEREO
    102.             };
    103.  
    104.             v2f vert(appdata IN)
    105.             {
    106.                 v2f o;
    107.                 UNITY_SETUP_INSTANCE_ID(IN);
    108.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    109.  
    110.                 o.uv0 = IN.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    111.                 o.uv1 = IN.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    112.  
    113.             #if USING_FOG
    114.                 float3 eyePos = UnityObjectToViewPos(IN.pos);
    115.                 float fogCoord = length(eyePos.xyz);
    116.                 UNITY_CALC_FOG_FACTOR_RAW(fogCoord);
    117.                 o.fog = saturate(unityFogFactor);
    118.             #endif
    119.  
    120.                 o.pos = UnityObjectToClipPos(IN.pos);
    121.                 return o;
    122.             }
    123.  
    124.             sampler2D _MainTex;
    125.  
    126.             fixed4 frag(v2f IN) : SV_Target
    127.             {
    128.                 fixed4 col;
    129.                 fixed4 tex = UNITY_SAMPLE_TEX2D(unity_Lightmap, IN.uv0.xy);
    130.                 half3 bakedColor = DecodeLightmap(tex);
    131.  
    132.                 tex = tex2D(_MainTex, IN.uv1.xy);
    133.                 col.rgb = tex.rgb * bakedColor;
    134.                 col.a = 1.0f;
    135.  
    136.                 #if USING_FOG
    137.                 col.rgb = lerp(unity_FogColor.rgb, col.rgb, IN.fog);
    138.                 #endif
    139.  
    140.             return col;
    141.  
    142.             }
    143.  
    144.         ENDCG
    145.         }
    146.     }
    147.  
    148.     Fallback Off
    149. }
    150.