Skip to content
On this page

Edge Transition

screen-space derivatives via fwidth() combined with smoothstep() are used to create a clean, antialiased transition across a UV-defined edge. It’s a common technique for stylized outlines, highlight edges, and procedural UV masks.

float edgeWidth = fwidth(uv.y);
float mask = smoothstep(threshold - edgeWidth, threshold + edgeWidth, uv.y);

Visual demo

This shader creates a smooth vertical transition along the UV Y-axis. The fwidth() function ensures consistent edge thickness across screen resolutions and object scaling.

Shader Story: Derivatives - EdgeTransition

Node logic (engine-agnostic)

Shader Story: Derivatives - EdgeTransition


Shader code
Shader "DecompiledArt/Derivatives/EdgeTransition/EdgeTransition"
{
    Properties
    {
        _EdgeFactor("EdgeFactor", Range(0.0, 1.0)) = 0.0
    }

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

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
                half2 uvs : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionHCS : SV_POSITION;
                float3 positionWS: TEXCOORD1;
                float3 normalWS : NORMAL;
                half2 uvs : TEXCOORD0;
            };

            CBUFFER_START(UnityPerMaterial)
            half _EdgeFactor;
            CBUFFER_END

            Varyings vert (Attributes IN)
            {
                Varyings OUT;
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
                OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS);

                OUT.uvs = IN.uvs;

                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                half edgeWidth = fwidth(IN.uvs.y);
                half edge = smoothstep(_EdgeFactor - edgeWidth, _EdgeFactor + edgeWidth, IN.uvs.y);

                half4 col_output = half4(edge.xxx, 1.0);

                return col_output;
            }

            ENDHLSL
        }
    }
}

Smoothstep • Lerp • Step


❤️ 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.