Shader "MS_RPG/FullOutline/DepthDilate"
{
    Properties
    {
        _Offset("Dilation Offset", Float) = 1.0
    }

    SubShader
    {
        Tags
        {
            "RenderType" = "Opaque"
            "RenderPipeline" = "UniversalPipeline"
        }

        Cull Off
        ZWrite Off
        ZTest Always

        Pass
        {
            Name "DepthDilate"

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"

            float _Offset;

            half4 frag(Varyings input) : SV_Target
            {
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);

                float2 texelSize = _BlitTexture_TexelSize.xy * _Offset;

                // Sample center and 8 neighbors
                float center = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord).r;
                float s0 = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + float2(-texelSize.x, -texelSize.y)).r;
                float s1 = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + float2(0, -texelSize.y)).r;
                float s2 = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + float2(texelSize.x, -texelSize.y)).r;
                float s3 = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + float2(-texelSize.x, 0)).r;
                float s4 = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + float2(texelSize.x, 0)).r;
                float s5 = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + float2(-texelSize.x, texelSize.y)).r;
                float s6 = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + float2(0, texelSize.y)).r;
                float s7 = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord + float2(texelSize.x, texelSize.y)).r;

                // Take MINIMUM depth (closest to camera) - this expands the object's depth outward
                // We use min because we want to catch occluders that are in front of ANY part of the object
                float minDepth = min(center, min(s0, min(s1, min(s2, min(s3, min(s4, min(s5, min(s6, s7))))))));

                // Only dilate where there's actual depth data (not the cleared background at 1.0)
                // This prevents the far plane from overwriting nearby depth
                float hasDepth = center < 0.999 ? 1.0 : 0.0;
                float neighborHasDepth = (s0 < 0.999 || s1 < 0.999 || s2 < 0.999 || s3 < 0.999 ||
                                          s4 < 0.999 || s5 < 0.999 || s6 < 0.999 || s7 < 0.999) ? 1.0 : 0.0;

                // If this pixel or any neighbor has real depth, use the minimum; otherwise stay at far
                float result = (hasDepth > 0 || neighborHasDepth > 0) ? minDepth : 1.0;

                return half4(result, result, result, 1.0);
            }
            ENDHLSL
        }
    }
}
