- Home /
Shader shadow anti-aliasing problem
If i write a shader code for a sprite as below, the edges of the shadow appear as dots. Setting the Shadow option of Directional Light and QualitySettings does not show any change. I think there is a problem with the shader code, what should I do? To briefly describe the code below, i put shadows in the sprite, turn off lighting to prevent lighting from affecting the color, and display the color with Emission.
Shader "Custom/Sprites/Shadow"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
_Cutoff ("Shadow alpha cutoff", Range(0, 1)) = 0.5
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"RenderType" = "Transparent"
"IgnoreProjector" = "True"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma surface surf Lambert alpha addshadow
sampler2D _MainTex;
fixed4 _Color;
struct Input
{
float2 uv_MainTex;
fixed4 color;
};
void vert (inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color = _Color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * IN.color;
o.Emission = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/Cutout/VertexLit"
}
You're definitely going to need to include some screenshots...
I uploaded it. The edge of the shadow looks like that.
Answer by FortisVenaliter · Jul 17, 2017 at 08:59 PM
There's no problem with the shader. It's using bilinear filtering to determine which points are transparent enough to cast a shadow. The pixellated nature of the target is causing the uneven edges.
If you want to remove those artifacts, increase the source resolution of the circle image.
Thanks! Definitely improve the resolution, the shadow quality improved.
If i increase the resolution and reduce it to fit the existing size with 'Pixel Per Unit', the image itself is broken. Is this unavoidable? Left Image Resolution: 32 x 32, Pixel Per Unit: 100. Right Image Resolution: 128 x 128, Pixel Per Unit: 400. If zoom out, the edges are not smooth. Do i have to choose between image quality and shadow? Or is there another way to solve this?
Oh. I solved it by activating 'Generate $$anonymous$$ip $$anonymous$$aps'.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
No shadows when building with WebGL? 1 Answer