Search Unity

Sprite 2D Shadow Shader

Discussion in '2D' started by meebou, May 8, 2019.

  1. meebou

    meebou

    Joined:
    Mar 6, 2017
    Posts:
    46
    Hello,
    maybe there are thousands solutions but i really can't find any. So i have this SOLID COLOR shader, that i am using right now to make 2D Shadows for my sprites. Example below. Now i want it to have transparency but i don't know how. Could someone give me a hint? To be clear. I want my sprite to be full black for example with transparency of 0.5. There are transparency shaders but they don't make the full sprite black at first, they just make everything transparent, but thats not how shadows work unfortunately...

    Code (CSharp):
    1. Shader "Sprites/SolidCol"
    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.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue"="Transparent"
    15.             "IgnoreProjector"="True"
    16.             "RenderType"="Transparent"
    17.             "PreviewType"="Plane"
    18.             "CanUseSpriteAtlas"="True"
    19.         }
    20.  
    21.         Cull Off
    22.         Lighting Off
    23.         ZWrite Off
    24.         Fog { Mode Off }
    25.         Blend One OneMinusSrcAlpha
    26.  
    27.         Pass
    28.         {
    29.         CGPROGRAM
    30.             #pragma vertex vert
    31.             #pragma fragment frag
    32.             #pragma multi_compile DUMMY PIXELSNAP_ON
    33.             #include "UnityCG.cginc"
    34.  
    35.             struct appdata_t
    36.             {
    37.                 float4 vertex   : POSITION;
    38.                 float4 color    : COLOR;
    39.                 float2 texcoord : TEXCOORD0;
    40.             };
    41.  
    42.             struct v2f
    43.             {
    44.                 float4 vertex   : SV_POSITION;
    45.                 fixed4 color    : COLOR;
    46.                 half2 texcoord  : TEXCOORD0;
    47.             };
    48.  
    49.             fixed4 _Color;
    50.  
    51.             v2f vert(appdata_t IN)
    52.             {
    53.                 v2f OUT;
    54.                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
    55.                 OUT.texcoord = IN.texcoord;
    56.                 OUT.color = IN.color * _Color;
    57.                 #ifdef PIXELSNAP_ON
    58.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    59.                 #endif
    60.  
    61.                 return OUT;
    62.             }
    63.  
    64.             sampler2D _MainTex;
    65.  
    66.             fixed4 frag(v2f IN) : SV_Target
    67.             {
    68.                 fixed4 c = tex2D(_MainTex, IN.texcoord);
    69.                 c.rgb = IN.color * c.a;
    70.                 return c;
    71.             }
    72.         ENDCG
    73.         }
    74.     }
    75. }