Shader "Hidden/Underwater Filter"
{
    Properties
    {
        _MainTex("Main Texture", 2D) = "white" {}
        _FogColor("Fog Color", Color) = (1, 1, 1, 1)
        _FogStart("Fog Start", float) = 1
        _FogDistance("Fog Distance", float) = 1
    }

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

        HLSLINCLUDE
        #include "Packages\com.unity.render-pipelines.universal\ShaderLibrary\Core.hlsl"
        #include "Packages\com.unity.render-pipelines.universal\ShaderLibrary\DeclareDepthTexture.hlsl"

        CBUFFER_START(UnityPerMaterial)
        float4 _FogColor;
        float _FogStart;
        float _FogDistance;
        CBUFFER_END

        TEXTURE2D(_MainTex);
        SAMPLER(sampler_MainTex);

        struct VertexInput
        {
            float4 pos : POSITION;
        };

        struct VertexOutput
        {
            float4 pos : SV_POSITION;
            float4 screenPos : TEXCOORD0;
        };
        ENDHLSL

        Pass
        {
            Name "Deferred Pass"

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            VertexOutput vert(VertexInput i)
            {
                VertexOutput o;

                o.pos = TransformObjectToHClip(i.pos.xyz);
                o.screenPos = ComputeScreenPos(i.pos.xyzw);

                return o;
            }

            float4 frag(VertexOutput i) : SV_Target
            {
                float depth = Linear01Depth(tex2Dproj(sampler_CameraDepthTexture, i.screenPos).r, _ZBufferParams) * _ProjectionParams.z;
                depth = saturate((depth - _FogStart) / _FogDistance);

                float4 coloredDepth = _FogColor * depth;
                float4 projText = tex2Dproj(sampler_MainTex, i.screenPos);

                return lerp(projText, coloredDepth, depth);
            }
            ENDHLSL
        }
    }
}