Search Unity

Custom sprite surface shader not receiving shadows

Discussion in 'Shaders' started by trevex, Jan 20, 2014.

  1. trevex

    trevex

    Joined:
    Apr 10, 2013
    Posts:
    17
    As far as I am aware semi-transparent objects can not receive shadows, but alpha tested objects can.

    So I tried to adapt the default sprite diffuse shader to include the necessary changes:

    renderer.receiveShadows is set to true.

    The shader looks as following:

    Code (csharp):
    1. Shader "Sprites/Normal"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _BumpMap ("Normalmap", 2D) = "bump" {}
    7.         _Color ("Tint", Color) = (1,1,1,1)
    8.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    9.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.  
    15.         Tags
    16.         {
    17.             "Queue"="Geometry+1"
    18.             "IgnoreProjector"="True"
    19.             "RenderType"="TransparentCutout"
    20.             "PreviewType"="Plane"
    21.         }
    22.                    
    23.         Cull Off
    24.         Lighting On
    25.         ZWrite On
    26.         Fog { Mode Off }
    27.         Blend SrcAlpha OneMinusSrcAlpha
    28.  
    29.         CGPROGRAM
    30.         #pragma target 2.0
    31.         #pragma surface surf SimpleLambert alphatest:_Cutoff addshadow vertex:vert
    32.         #pragma multi_compile DUMMY PIXELSNAP_ON
    33.  
    34.         sampler2D _MainTex;
    35.         sampler2D _BumpMap;
    36.  
    37.         fixed4 _Color;
    38.  
    39.         struct Input
    40.         {
    41.             float2 uv_MainTex;
    42.             float2 uv_BumpMap;
    43.             fixed4 color;
    44.         };
    45.                    
    46.  
    47.         half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
    48.             half NdotL = dot (s.Normal, lightDir);
    49.             half4 c;
    50.                 c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten * 2);
    51.             c.a = s.Alpha;
    52.             return c;
    53.         }
    54.        
    55.         void vert (inout appdata_full v, out Input o)
    56.         {
    57.             #if defined(PIXELSNAP_ON)  !defined(SHADER_API_FLASH)
    58.             v.vertex = UnityPixelSnap (v.vertex);
    59.             #endif
    60.             v.normal = float3(0,0,-1);
    61.             v.tangent = float4(1, 0, 0, -1);
    62.            
    63.             UNITY_INITIALIZE_OUTPUT(Input, o);
    64.             o.color = _Color;
    65.         }
    66.  
    67.         void surf (Input IN, inout SurfaceOutput o)
    68.         {
    69.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
    70.             o.Albedo = c.rgb;
    71.             o.Alpha = c.a;
    72.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    73.         }
    74.         ENDCG
    75.     }
    76. Fallback "Transparent/Cutout/VertexLit"
    77. }
    As far as I am aware all the necessities are there:
    The sprite is rendered after the geometry pass and the surface shader uses alphatesting and specifies addshadow.

    My current assumption is that it has to do with the custom vertex shader, because it is the only major difference to the original cutout shader?

    Let me know if you need further informations let me know!

    Thanking you in anticipation,
    Nik / trevex
     
  2. Stevepunk

    Stevepunk

    Joined:
    Oct 20, 2013
    Posts:
    205
    I'm also having problems with a sprite flip shader not receiving shadows.