Search Unity

How to make Receive shadow shader in URP

Discussion in 'Shaders' started by EternalMe, Mar 18, 2020.

  1. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    So I fallowed the Unity manual https://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html , section "Receiving shadows". And it works well.

    But I want TODO the same thing in URP, but it simply not working with this code. Here is my try until now:

    Code (CSharp):
    1. Shader "Unlit/Kaka"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             Tags{ "LightMode" = "UniversalForward" }
    15.  
    16.             CGPROGRAM
    17.  
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #include "UnityCG.cginc"
    21.             #include "Lighting.cginc"
    22.  
    23.             #pragma multi_compile_fwdbase// nolightmap nodirlightmap nodynlightmap novertexlight
    24.             #include "AutoLight.cginc"
    25.  
    26.             /*struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };*/
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 //UNITY_FOG_COORDS(1)
    36.                 SHADOW_COORDS(1)
    37.                 float4 pos : SV_POSITION;                              
    38.                 fixed3 diff : COLOR0;
    39.                 fixed3 ambient : COLOR1;
    40.             };
    41.  
    42.             sampler2D _MainTex;
    43.             float4 _MainTex_ST;
    44.  
    45.             v2f vert (appdata_base v)
    46.             {
    47.                 v2f o;
    48.                 o.pos = UnityObjectToClipPos(v.vertex);
    49.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    50.  
    51.                 //UNITY_TRANSFER_FOG(o,o.vertex);
    52.                
    53.                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    54.                 half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    55.                 o.diff = nl * _LightColor0.rgb;
    56.                 o.ambient = ShadeSH9(half4(worldNormal, 1));
    57.                
    58.                 TRANSFER_SHADOW(o)
    59.  
    60.                 return o;
    61.             }
    62.  
    63.             fixed4 frag (v2f i) : SV_Target
    64.             {
    65.                 // sample the texture
    66.                 fixed4 col = tex2D(_MainTex, i.uv);
    67.                 // apply fog
    68.                 //UNITY_APPLY_FOG(i.fogCoord, col);
    69.  
    70.                 fixed shadow = SHADOW_ATTENUATION(i);
    71.                 fixed3 lighting = i.diff * shadow + i.ambient;
    72.  
    73.                 col.rgb *= lighting;
    74.  
    75.                 return col;
    76.             }
    77.             ENDCG
    78.         }
    79.  
    80.         // shadow casting support
    81.         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    82.     }
    83. }
    84.  
    I hope somebody can help :)
     
  2. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    So my new version:

    Code (CSharp):
    1. Shader "Universal Render Pipeline/Unlit2"
    2. {
    3.     Properties
    4.     {
    5.         _BaseMap("Texture", 2D) = "white" {}
    6.         _BaseColor("Color", Color) = (1, 1, 1, 1)
    7.         _Cutoff("AlphaCutout", Range(0.0, 1.0)) = 0.5
    8.  
    9.             // BlendMode
    10.             [HideInInspector] _Surface("__surface", Float) = 0.0
    11.             [HideInInspector] _Blend("__blend", Float) = 0.0
    12.             [HideInInspector] _AlphaClip("__clip", Float) = 0.0
    13.             [HideInInspector] _SrcBlend("__src", Float) = 1.0
    14.             [HideInInspector] _DstBlend("__dst", Float) = 0.0
    15.             [HideInInspector] _ZWrite("__zw", Float) = 1.0
    16.             [HideInInspector] _Cull("__cull", Float) = 2.0
    17.  
    18.             // Editmode props
    19.             [HideInInspector] _QueueOffset("Queue offset", Float) = 0.0
    20.  
    21.             // ObsoleteProperties
    22.             [HideInInspector] _MainTex("BaseMap", 2D) = "white" {}
    23.             [HideInInspector] _Color("Base Color", Color) = (0.5, 0.5, 0.5, 1)
    24.             [HideInInspector] _SampleGI("SampleGI", float) = 0.0 // needed from bakedlit
    25.     }
    26.         SubShader
    27.             {
    28.                 Tags { "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
    29.                 LOD 300
    30.  
    31.                 Pass
    32.                 {
    33.                
    34.                 Name "Unlit"
    35.                 Tags { "LightMode" = "UniversalForward" }
    36.  
    37.                 // Use same blending / depth states as Standard shader
    38.                 Blend[_SrcBlend][_DstBlend]
    39.                 ZWrite[_ZWrite]
    40.                 Cull[_Cull]
    41.  
    42.                 HLSLPROGRAM
    43.                 // Required to compile gles 2.0 with standard srp library
    44.                 #pragma prefer_hlslcc gles
    45.                 #pragma exclude_renderers d3d11_9x
    46.                 #pragma target 2.0
    47.  
    48.                 #pragma vertex vert
    49.                 #pragma fragment frag
    50.                 #pragma shader_feature _ALPHATEST_ON
    51.                 #pragma shader_feature _ALPHAPREMULTIPLY_ON
    52.  
    53.                 // -------------------------------------
    54.                 // Unity defined keywords
    55.                 #pragma multi_compile_fog
    56.                 #pragma multi_compile_instancing
    57.  
    58.                 #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
    59.                 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    60.  
    61.                 struct Attributes
    62.                 {
    63.                     float4 positionOS       : POSITION;
    64.                     float2 uv               : TEXCOORD0;
    65.                     float3 normalOS     : NORMAL;
    66.                     float4 tangentOS    : TANGENT;
    67.                     UNITY_VERTEX_INPUT_INSTANCE_ID
    68.                 };
    69.  
    70.                 struct Varyings
    71.                 {
    72.                     float2 uv        : TEXCOORD0;
    73.                     float fogCoord : TEXCOORD1;
    74.                     float4 vertex : SV_POSITION;                  
    75.                     half3  normalWS : TEXCOORD2;
    76.                     float4 shadowCoord : TEXCOORD3;
    77.  
    78.                     UNITY_VERTEX_INPUT_INSTANCE_ID
    79.                     UNITY_VERTEX_OUTPUT_STEREO
    80.                 };
    81.  
    82.                 Varyings vert(Attributes input)
    83.                 {
    84.                     Varyings output = (Varyings)0;
    85.  
    86.                     UNITY_SETUP_INSTANCE_ID(input);
    87.                     UNITY_TRANSFER_INSTANCE_ID(input, output);
    88.                     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    89.  
    90.                     VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
    91.                     output.vertex = vertexInput.positionCS;
    92.                     output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
    93.                     output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
    94.                     output.shadowCoord = GetShadowCoord(vertexInput);
    95.                    
    96.                     VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
    97.                     output.normalWS = vertexNormalInput.normalWS;
    98.  
    99.                     return output;
    100.                 }
    101.  
    102.                 half4 frag(Varyings input) : SV_Target
    103.                 {
    104.                     UNITY_SETUP_INSTANCE_ID(input);
    105.                     UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    106.  
    107.                                    
    108.  
    109.                     half2 uv = input.uv;
    110.                     half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
    111.                     half3 color = texColor.rgb * _BaseColor.rgb;
    112.                     half alpha = texColor.a * _BaseColor.a;
    113.                     AlphaDiscard(alpha, _Cutoff);
    114.  
    115.     #ifdef _ALPHAPREMULTIPLY_ON
    116.                     color *= alpha;
    117.     #endif
    118.                     Light mainLight = GetMainLight(input.shadowCoord);
    119.  
    120.                     half3 attenuatedLightColor = mainLight.color * (mainLight.distanceAttenuation * mainLight.shadowAttenuation);
    121.                     half3 diffuseColor = LightingLambert(attenuatedLightColor, mainLight.direction, input.normalWS);            
    122.  
    123.                     color *= diffuseColor; //mainLight.shadowAttenuation;
    124.                     color = MixFog(color, input.fogCoord);
    125.  
    126.                     return half4(color, alpha);
    127.                 }
    128.                 ENDHLSL
    129.             }
    130.             Pass
    131.             {
    132.                 Tags{"LightMode" = "DepthOnly"}
    133.  
    134.                 ZWrite On
    135.                 ColorMask 0
    136.  
    137.                 HLSLPROGRAM
    138.                     // Required to compile gles 2.0 with standard srp library
    139.                     #pragma prefer_hlslcc gles
    140.                     #pragma exclude_renderers d3d11_9x
    141.                     #pragma target 2.0
    142.  
    143.                     #pragma vertex DepthOnlyVertex
    144.                     #pragma fragment DepthOnlyFragment
    145.  
    146.                     // -------------------------------------
    147.                     // Material Keywords
    148.                     #pragma shader_feature _ALPHATEST_ON
    149.  
    150.                     //--------------------------------------
    151.                     // GPU Instancing
    152.                     #pragma multi_compile_instancing
    153.  
    154.                     #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
    155.                     #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
    156.                     ENDHLSL
    157.                 }
    158.  
    159.                     // This pass it not used during regular rendering, only for lightmap baking.
    160.                     Pass
    161.                     {
    162.                         Name "Meta"
    163.                         Tags{"LightMode" = "Meta"}
    164.  
    165.                         Cull Off
    166.  
    167.                         HLSLPROGRAM
    168.                     // Required to compile gles 2.0 with standard srp library
    169.                     #pragma prefer_hlslcc gles
    170.                     #pragma exclude_renderers d3d11_9x
    171.                     #pragma vertex UniversalVertexMeta
    172.                     #pragma fragment UniversalFragmentMetaUnlit
    173.  
    174.                     #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
    175.                     #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitMetaPass.hlsl"
    176.  
    177.                     ENDHLSL
    178.                 }
    179.  
    180.                 UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    181.             }
    182.                 FallBack "Hidden/Universal Render Pipeline/FallbackError"
    183. }
    184.  
    Problem is that `mainLight.shadowAttenuation` is always 1,1,1. Why?
     
  3. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    Solved by adding
    Code (CSharp):
    1. #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    2.                 #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
     
    GrossPaul and valarnur like this.
  4. Spettro22

    Spettro22

    Joined:
    Apr 22, 2016
    Posts:
    4
    Hi!
    Did it works?
    Thank you for sharing, but where did you add the la piece of code? Can you post the complete Shadar?
    Thank you!!!
     
  5. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    Sorry I discarded it and started to work with Shader graph.
     
  6. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    I am actually in the same situation...
    Had a receiver shadow only shader working with default render pipeline as below :
    Code (CSharp):
    1.  
    2.  
    3. Shader "ShadowOnly" {
    4.     Properties{
    5.         _Color("Shadow Color", Color) = (1,1,1,1)
    6.         _ShadowInt("Shadow Intensity", Range(0,1)) = 1.0
    7.         _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    8.     }
    9.         SubShader{
    10.             Tags
    11.             {
    12.                 "Queue" = "AlphaTest"
    13.                 "IgnoreProjector" = "True"
    14.                 "RenderType" = "Transparent"
    15.             }
    16.             LOD 200
    17.             ZWrite Off
    18.             Blend zero SrcColor
    19.         CGPROGRAM
    20.         //        #pragma surface surf ShadowOnly alphatest:_Cutoff noambient approxview halfasview novertexlights nolightmap nodynlightmap nodirlightmap nofog nometa noforwardadd nolppv noshadowmask
    21.                 #pragma surface surf ShadowOnly alphatest:_Cutoff NoLighting noambient
    22.                 fixed4 _Color;
    23.                 float _ShadowInt;
    24.                 struct Input {
    25.                     float2 uv_MainTex;
    26.                 };
    27.                 inline fixed4 LightingShadowOnly(SurfaceOutput s, fixed3 lightDir, fixed atten)
    28.                 {
    29.                     fixed4 c;
    30.                     c.rgb = lerp(s.Albedo, float3(1.0,1.0,1.0), atten);
    31.                     c.a = 1.0 - atten;
    32.                     return c;
    33.                 }
    34.                 void surf(Input IN, inout SurfaceOutput o) {
    35.                     o.Albedo = lerp(float3(1.0,1.0,1.0), _Color.rgb, _ShadowInt);
    36.                     o.Alpha = 1.0;
    37.                 }
    38.                 ENDCG
    39.     }
    40.         Fallback "Transparent/Cutout/VertexLit"
    41. }
    42.  
    43.  
    44.  
    And then showing pink with URP...
    I am searching/testing....
     
    Last edited: Apr 7, 2020
  7. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734