Skip to content
On this page

Square

fwidth() and saturate() are used here to create a crisp, antialiased square mask. Good for stylized shapes, UI masks, scanlines, or procedural transitions.

half2 uvOffset = abs((uv * 2.0) - 1.0) - edgeLength;
half2 aaEdge = 1.0 - (uvOffset / fwidth(uvOffset));
half col_output = saturate(min(aaEdge.x, aaEdge.y));

Visual demo

This pattern defines a square shape by measuring the offset of each fragment from the UV center. Using fwidth() ensures the edges stay consistent across varying screen resolutions and scale.

Shader Story: Patterns - Square

Node logic (engine-agnostic)

Shader Story: Patterns - Square


Shader code
Shader "DecompiledArt/Patterns/Square/Square"
{
    Properties
    {
        _EdgeLength("EdgeLength", Range(0.0, 1.0)) = 0.5
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {            
            ...


            half4 frag(Varyings IN) : SV_Target
            {
                half2 uvs = abs((IN.uvs.xy * 2) - 1) - _EdgeLength;
                uvs = 1 - uvs/fwidth(uvs);

                half col_output = saturate(min(uvs.x, uvs.y));

                return half4(col_output.xxx, 1.0);
            }


            ENDHLSL
        }
    }
}

Clamp/Saturate • Smoothstep • Abs


❤️ Support Shader Story

If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

Your support helps keep this library open and free for everyone.