Skip to content
On this page

Silhouette

In this sample, screen-space derivatives (ddx, ddy) are used to estimate changes in surface normals and depth across neighboring pixels. These values detect edges or silhouettes: orientation and depth change rapidly there. This technique is useful for stylized outlines, non-photorealistic rendering (NPR), and post-process edge detection effects.

half normalDiff = length(ddx(normalWS) + ddy(normalWS));
half depthDiff = length(float2(ddx(depth), ddy(depth)));

Visual demo

This shader flags geometry edges and silhouettes by measuring changes in surface normals and depth across the screen. The stronger the difference, the brighter the output, making silhouettes pop naturally from the background.

Shader Story: Derivatives - Silhouette

Node logic (engine-agnostic)

Shader Story: Derivatives - Silhouette


Shader code
Shader "DecompiledArt/Derivatives/Silhouette/Silhouette"
{
    Properties
    {
        _EdgeFactor("EdgeFactor", Range(0.0, 0.1)) = 0.025
    }

    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;
            };

            struct Varyings
            {
                float4 positionHCS : SV_POSITION;
                float3 normalWS : NORMAL;
                float4 screenPos :TEXCOORD0;
            };

            CBUFFER_START(UnityPerMaterial)
            half _EdgeFactor;
            CBUFFER_END

            TEXTURE2D(_CameraDepthTexture);
            SAMPLER(sampler_CameraDepthTexture);

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

                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                float2 screenUV = IN.screenPos.xy / IN.screenPos.w;

                float rawDepth = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV).r;
                float linearDepth = Linear01Depth(rawDepth, _ZBufferParams);

                half depthDiff = length(float2(ddx(linearDepth), ddy(linearDepth)));
                half normalDiff = length(ddx(IN.normalWS) + ddy(IN.normalWS));

                half edge = smoothstep(_EdgeFactor - 0.01, _EdgeFactor + 0.01, normalDiff + depthDiff);

                return half4(edge.xxx, 1.0);
            }

            ENDHLSL
        }
    }
}

Length • Normalize • Smoothstep


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