Search Unity

How to get 2D sprite renderers to cast shadows woth 3D lighting in a 2.5D game?

Discussion in 'General Graphics' started by KqGMRPFkAeESzF8, Sep 26, 2021.

  1. KqGMRPFkAeESzF8

    KqGMRPFkAeESzF8

    Joined:
    Nov 20, 2018
    Posts:
    53
    I tried adding using the folowing shader and I get lighting on the sprite itself, but it doesn't cast shadows on anything else.

    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Sprites/Diffuse"
    4. {
    5.     Properties
    6.     {
    7.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    8.         _Color ("Tint", Color) = (1,1,1,1)
    9.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    10.         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
    11.         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
    12.         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
    13.         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
    14.         _Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.5
    15.     }
    16.  
    17.     SubShader
    18.     {
    19.         Tags
    20.         {
    21.             "Queue"="Transparent"
    22.             "IgnoreProjector"="True"
    23.             "RenderType"="Transparent"
    24.             "PreviewType"="Plane"
    25.             "CanUseSpriteAtlas"="True"
    26.         }
    27.  
    28.         Cull Off
    29.         Lighting Off
    30.         ZWrite Off
    31.         Blend One OneMinusSrcAlpha
    32.  
    33.         CGPROGRAM
    34.         #pragma surface surf Lambert alpha addshadow vertex:vert nofog nolightmap nodynlightmap keepalpha noinstancing
    35.         #pragma multi_compile_local _ PIXELSNAP_ON
    36.         #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    37.         #include "UnitySprites.cginc"
    38.  
    39.         struct Input
    40.         {
    41.             float2 uv_MainTex;
    42.             fixed4 color;
    43.         };
    44.  
    45.         void vert (inout appdata_full v, out Input o)
    46.         {
    47.             v.vertex = UnityFlipSprite(v.vertex, _Flip);
    48.  
    49.             #if defined(PIXELSNAP_ON)
    50.             v.vertex = UnityPixelSnap (v.vertex);
    51.             #endif
    52.  
    53.             UNITY_INITIALIZE_OUTPUT(Input, o);
    54.             o.color = v.color * _Color * _RendererColor;
    55.         }
    56.  
    57.         void surf (Input IN, inout SurfaceOutput o)
    58.         {
    59.             fixed4 c = SampleSpriteTexture (IN.uv_MainTex) * IN.color;
    60.             o.Albedo = c.rgb * c.a;
    61.             o.Alpha = c.a;
    62.         }
    63.         ENDCG
    64.     }
    65.  
    66. Fallback "Transparent/Cutout/VertexLit"
    67. }