Screen space outlines in orthographic view

I recently tried making a URP outline shader. For some reason, depth sampling doesn’t work in orthographic view.

Does anyone know how to fix it?

Certainly! Below is an example of a URP (Universal Render Pipeline) outline shader that uses depth sampling. I’ll also describe the expected results and the issue you’re experiencing with orthographic view.

Shader Code
Here’s a sample URP outline shader code in HLSL:

Shader "Custom/Outline"
{
    Properties
    {
        _OutlineColor ("Outline Color", Color) = (1,1,1,1)
        _OutlineWidth ("Outline Width", Range(0.0, 0.03)) = 0.005
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Pass
        {
            Tags { "LightMode" = "ForwardBase" }

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

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

            struct Attributes
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct Varyings
            {
                float4 position : SV_POSITION;
                float3 normal : NORMAL;
                float3 worldPos : TEXCOORD0;
            };

            float4 _OutlineColor;
            float _OutlineWidth;

            Varyings vert (Attributes v)
            {
                Varyings o;
                o.position = TransformObjectToHClip(v.vertex + v.normal * _OutlineWidth);
                o.normal = v.normal;
                o.worldPos = TransformObjectToWorld(v.vertex);
                return o;
            }

            half4 frag (Varyings i) : SV_Target
            {
                float3 worldNormal = normalize(i.normal);
                float3 viewDir = normalize(i.worldPos - _WorldSpaceCameraPos);
                float outline = dot(worldNormal, viewDir);
                outline = 1.0 - outline;
                return half4(_OutlineColor.rgb * outline, 1.0);
            }
            ENDHLSL
        }
    }
    FallBack "Diffuse"
}

Apply an outline effect to objects by offsetting vertices along their normals. The outline color and width are adjustable via shader properties. The outline mgh patient gateway should appear around the object consistently, giving it a highlighted border effect.