Search Unity

[URP] Custom shader not receiving shadows

Discussion in 'Universal Render Pipeline' started by imagoFX, Nov 24, 2019.

  1. imagoFX

    imagoFX

    Joined:
    Sep 19, 2011
    Posts:
    84
    Working on a simple custom shader and can't make it receiving shadows. I'm guessing something changed with the way URP does that but couldn't find an answer in the unity docs. I'm using 2019.3 beta

    Code (JavaScript):
    1. Shader "imagoFX/Vertex Colors"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (0.5, 0.5, 0.5, 0.5)
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags { "Queue" = "Geometry"}
    11.        
    12.         CGINCLUDE
    13.         #pragma vertex vert
    14.         #pragma fragment frag
    15.        
    16.         #include "UnityCG.cginc"
    17.         #include "AutoLight.cginc"
    18.        
    19.         uniform fixed4 _Color;
    20.         uniform float4 _LightColor0;
    21.        
    22.         struct v2f
    23.         {
    24.             float4 pos : SV_POSITION;
    25.             float4 color : COLOR;
    26.             float3 normal : NORMAL;
    27.             LIGHTING_COORDS(1,2)
    28.         };
    29.        
    30.         v2f vert (appdata_full v)
    31.         {
    32.             v2f o;
    33.            
    34.             o.pos = UnityObjectToClipPos(v.vertex);
    35.             o.color = v.color;
    36.  
    37.             float4x4 modelMatrix = unity_ObjectToWorld;
    38.             float4x4 modelMatrixInverse = unity_WorldToObject;
    39.  
    40.             float3 normalDirection = UnityObjectToWorldNormal(v.normal);
    41.             float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    42.            
    43.             float3 diffuseReflection = _LightColor0.rgb * max(0.0, dot(normalDirection, lightDirection));
    44.             o.color += float4(diffuseReflection, 1.0);
    45.             o.pos = UnityObjectToClipPos(v.vertex);
    46.  
    47.             TRANSFER_SHADOW(o)
    48.  
    49.             return o;
    50.         }
    51.         ENDCG
    52.    
    53.         Pass {
    54.             CGPROGRAM
    55.        
    56.             fixed4 frag (v2f i) : COLOR
    57.             {
    58.                 return _Color * i.color * SHADOW_ATTENUATION(i);
    59.             }
    60.             ENDCG
    61.         }
    62.     }
    63.     FallBack "VertexLit"
    64. }
     
  2. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    your shader is written for built in forward. if you want to make in run in urp including shadows you have to entirely rewrite the shader...
    have a look at the Lit shader for example that ships with urp.
     
  3. imagoFX

    imagoFX

    Joined:
    Sep 19, 2011
    Posts:
    84
    Isn't urp forward renderer?
    I did check the lit shader and didn't understand a thing but I'll check it again.
     
  4. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    it is. but a completely different forward renderer – especially when it comes to lighting.
    so yur shader will output something to screen using urp. but it does not get any lighting as "AutoLight.cginc" does not sample urp shadows.
    and i even doubt that using _LightColor0 or _WorldSpaceLightPos0 will contain any data.
     
  5. imagoFX

    imagoFX

    Joined:
    Sep 19, 2011
    Posts:
    84
    Thanks for the info
     
  6. SangYun_Yi

    SangYun_Yi

    Unity Technologies

    Joined:
    Dec 6, 2017
    Posts:
    5
    if you need to receive shadow on unlit shader, it can need to modified shadow pass.

    visit this link you can find answer.

    https://illu.tistory.com/1407