Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Sprites and Casting / Receiving Shadows

Discussion in '2D' started by Hyubusa, Jun 20, 2015.

  1. Hyubusa

    Hyubusa

    Joined:
    Jun 18, 2015
    Posts:
    5
    I am new to Unity; just picked it up this week; so please bear with me.

    I have been playing around with this all day and I still can't seem to get a Sprite to RECEIVE a shadow. I figured the best way to approach this would be to go step by step on what I did to get to where I am then hopefully you guys can tell me what I missed.
    1. I created a new scene in 2D mode.
    2. I added a Sprite "character", Sprite "wall", 3D cube and a Spotlight into the scene then positioned them in a way that could test shadows: upload_2015-6-19_20-25-25.png
    3. I created my custom Shader, applied it to a custom Material then added that Material to both of my Sprite elements, Then using the Inspectors Debug mode I enabled both Receive and Cast Shadows for both Sprite Elements. upload_2015-6-19_20-30-18.png
      Custom 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.         sampler2D _MainTex;
      32.         fixed4 _Color;
      33.  
      34.         struct Input
      35.         {
      36.             float2 uv_MainTex;
      37.             fixed4 color;
      38.         };
      39.        
      40.         void vert (inout appdata_full v, out Input o)
      41.         {
      42.             #if defined(PIXELSNAP_ON)
      43.             v.vertex = UnityPixelSnap (v.vertex);
      44.             #endif
      45.            
      46.             UNITY_INITIALIZE_OUTPUT(Input, o);
      47.             o.color = v.color * _Color;
      48.         }
      49.  
      50.         void surf (Input IN, inout SurfaceOutput o)
      51.         {
      52.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
      53.             o.Albedo = c.rgb * c.a;
      54.             o.Alpha = c.a;
      55.         }
      56.         ENDCG
      57.     }
      58.  
      59. Fallback "Transparent/Cutout/VertexLit"
      60. }
      61.  
    As you can see the Sprite "character" cast a shadow, the 3D Cube receives it but the Sprite "wall" does not. Any idea as to why?
    upload_2015-6-19_20-32-59.png
     
  2. tmkebr

    tmkebr

    Joined:
    Jun 14, 2015
    Posts:
    2
    Sorry for the old bump, but I'd also like to know this. I've got sprites set to cast and receive shadows from a spotlight, but it seems like only quads receive shadows cast from the sprites. Both sprites have a script attached to them to force them to cast and receive shadows, and I've even tried doing it through the debug menu. I've spent several hours researching, but haven't found a way to make this work.
     
    Last edited: Aug 28, 2015
  3. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    Anybody? This would really be helpful, many other shaders on google don`t work....
     
  4. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    up too ^^
     
  5. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    352
    I got the solution, the Sprite should have Default-material for it to receive shadows. So if you have correct, working casting shadow shader, it will work.