Weird aliasing artifact on corners of a rectangle with SDF

25 views Asked by At

I am trying to implement local anti-aliasing for rectangles with signed distance fields. However the pixels at the corners does not look quite right. Below is a screenshot of one of the corners.

screenshot of one corner

Here's my shader (including the distance function):

Shader "IMShapes/Rectangle"
{
    Properties
    {
        _Colour("Colour", Color) = (1, 1, 1, 1)
    }
    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
        }
        ZWrite Off
        Cull Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            float4 _Colour;

            float RectangleSDF(float2 p, float2 b)
            {
                p = (p - 0.5) * 2;
                float2 d = abs(p) - b;
                return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0);
            }

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float delta = fwidth(RectangleSDF(i.uv, 1));
                float alpha = smoothstep(0 + delta, 0 - delta, RectangleSDF(i.uv, 1));
                return float4(_Colour.rgb, alpha);
            }
            ENDCG
        }
    }
}

Any ideas on what could be wrong?

0

There are 0 answers