- Home /
Is there a way to cast (maybe recieve) sprite shadows without discarding alpha pixels with cutout shader?
Hi , everybody !
I´m working on a 2.5D game that has movement on all axes and uses hand drawn sprites ( not pixel art , this is very important).
I managed to make our sprites cast an recieve shadows by making our shader render in the Geometry queue and using an alpha cutoff parameter so that the shadows show the correct sillouete of the sprite. This worked and generated a really cool level of polish in our project but it has one big disadvantage : The alpha pixels that are below the cutout threshold are not rendered at all. Since our art is hand drawn and has alpha gradients , this is not aceptable because we lose quality , sprites look jagged and pixelated on the edges due to the pixels getting discarded. Here is an image explainng the problem
I would really like to know if there is any way to cast shadows without discarding those alpha pixels. In my complete ingnorance of shaders I was thinking of a shader with multiple passes or using a second sprite renderer in each object to with the cutout shader but not rendering anything at all so that it only casts shadows but that seems really overkill. The ability to cast shadows is really cool and I don't want to lose it.
The project uses forward renderer and uses de default/basic render from Unity ( not using URP or HDRP)
Also : Is there any way my sprites could also recieve shadows without having to use the geometry queue? Recieving shadows would be the extra quality touch the game would need to look really good.
Here is the shader I´m currently using
Shader "Custom/CharactersV4Players"{
Properties
{
[PerRendererData] _MainTex("Texture", 2D) = "white" {}
_EffectColor1("Effect Color", Color) = (1,1,1,1)
_Crossfade("Fade", float) = 0
_FlashColor("Flash Color", Color) = (1,1,1,1)
_FlashAmount("Flash Amount",Range(0.0,1.0)) = 0
_Cutoff("Alpha Cutoff", Range(0,1)) = 0.9
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "TransparentCutOut"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf Lambert alpha:blend fullforwardshadows addshadow alphatest:_Cutoff
#pragma target 3.0
struct Input {
fixed2 uv_MainTex;
fixed4 color : COLOR;
};
sampler2D _MainTex;
fixed4 _EffectColor1;
fixed _Crossfade;
fixed4 _FlashColor;
float _FlashAmount;
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 col = tex2D(_MainTex, IN.uv_MainTex);
fixed4 returnColor = lerp(col, col * _EffectColor1, _Crossfade) * _EffectColor1.a + col * (1.0 - _EffectColor1.a);
o.Albedo = returnColor.rgb * IN.color.rgb;
o.Alpha = col.a * IN.color.a;
o.Albedo = lerp(o.Albedo,_FlashColor.rgb,_FlashAmount);
}
ENDCG
}
Fallback "Legacy Shaders/Transparent/Cutout/VertexLit"
}
Your answer
Follow this Question
Related Questions
Adjusting alpha cutoff during run time doesn't work 1 Answer
which shader to use for dualsided alpha cutout with light cookie enabled? 1 Answer
Adding shadows to transparent shader? 0 Answers
Distance in the shadows of the sprites and quads. 0 Answers
Mesh transparency halfway cutoff script 0 Answers