Search Unity

2D Sprite can't receive Shadows

Discussion in 'Shaders' started by anlev, Jan 28, 2016.

  1. anlev

    anlev

    Joined:
    Jun 14, 2015
    Posts:
    9
    hi guys, i have a problem with shadows receiving on sprite
    well i have some cute shadow-cast shader:

    Code (CSharp):
    1. Shader "Sprites/Diffuse/Shadows"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    6.         _Color("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
    8.         _Cutoff("Shadow alpha cutoff", Range(0,1)) = 0.5
    9.     }
    10.  
    11.         SubShader
    12.     {
    13.         Tags
    14.     {
    15.         "Queue" = "Transparent"
    16.         "IgnoreProjector" = "True"
    17.         "RenderType" = "Transparent"
    18.         "PreviewType" = "Plane"
    19.         "CanUseSpriteAtlas" = "True"
    20.     }
    21.  
    22.         Cull Off
    23.         Lighting Off
    24.         ZWrite Off
    25.         Blend One OneMinusSrcAlpha
    26.  
    27.         CGPROGRAM
    28. #pragma surface surf Lambert vertex:vert nofog keepalpha addshadow
    29. #pragma multi_compile _ PIXELSNAP_ON
    30.  
    31.  
    32.     sampler2D _MainTex;
    33.     fixed4 _Color;
    34.  
    35.     struct Input
    36.     {
    37.         float2 uv_MainTex;
    38.         fixed4 color;
    39.     };
    40.  
    41.     void vert(inout appdata_full v, out Input o)
    42.     {
    43. #if defined(PIXELSNAP_ON)
    44.         v.vertex = UnityPixelSnap(v.vertex);
    45. #endif
    46.  
    47.         UNITY_INITIALIZE_OUTPUT(Input, o);
    48.         o.color = v.color * _Color;
    49.      
    50.     }
    51.  
    52.  
    53.     fixed _Cutoff;
    54.     void surf(Input IN, inout SurfaceOutput o)
    55.     {
    56.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
    57.         o.Albedo = c.rgb;
    58.         o.Alpha = c.a;
    59.         clip(o.Alpha - _Cutoff);
    60.     }
    61.  
    62.  
    63.     ENDCG
    64.     }
    65.         Fallback "Transparent/Cutout/VertexLit"
    66. }
    this shader allows my sprites to cast some shadows but they cant be received by them.
    how can i add the possibility of receiving shadows in this shader?
    spriteRenderer.receiveShadows is ON
    spriteRenderer.castShadows is ON too.

    P.S. yes, i know that meshRenderer could receive any shadow, but i need to attach this possibility to sprites.
    please help!)
     
    Last edited: Jan 29, 2016
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    if you are using desktop then this shader is built into unity? see standard shader drop down for blend modes like Transparent, Fade, Cutout...
     
  3. anlev

    anlev

    Joined:
    Jun 14, 2015
    Posts:
    9
    did anyone received shadow by sprite?
     
    Last edited: Jan 29, 2016
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Anything with a transparency queue will not receive shadows.

    Try "Queue"="AlphaTest" "RenderType"="TransparentCutout" ZWrite On and remove the blend line.
     
  5. anlev

    anlev

    Joined:
    Jun 14, 2015
    Posts:
    9
    i tried now, it does not work :X
    here is some explanation about my problem:

    bg is mesh with standart shader, it receive shadows easily
    if sprite's shadow comes on other sprite, it isnt receiving by it
    i turned off bg "receiveShadows":

    which part of standart(specular) shader allows it to receive shadows?
    must i mix it with my shader or something?
     
    Last edited: Jan 29, 2016
    IgorUnity3D likes this.
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    You might try adding fullforwardshadows to the #pragma surface line, along with replacing keepalpha with alphatest:_Cutoff
     
  7. anlev

    anlev

    Joined:
    Jun 14, 2015
    Posts:
    9
    nope :(
    which code allows to receive shadows in Unity?
     
    Last edited: Jan 29, 2016
  8. anlev

    anlev

    Joined:
    Jun 14, 2015
    Posts:
    9
  9. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
  10. _Keith

    _Keith

    Joined:
    Nov 7, 2015
    Posts:
    27