Shader "Course/Environment/Lakeside Water"
{
    Properties
    {
        _DeepColor("Water absorption", Color) = (.012, .036, .032, 1)
        _PlanarReflection("Planar reflection", 2D) = "black" {}
        _WaveStrength("Reflection distortion", Float) = .003
        _MotionTime("Motion time", Float) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Transparent-10" "RenderPipeline"="UniversalPipeline" }
        Pass
        {
            Tags { "LightMode"="UniversalForward" }
            ZWrite On
            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag
            #pragma multi_compile_fog
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"
            TEXTURE2D(_PlanarReflection); SAMPLER(sampler_PlanarReflection);
            struct Attributes { float4 positionOS : POSITION; };
            struct Varyings { float4 positionCS : SV_POSITION; float3 positionWS : TEXCOORD0; float4 screen : TEXCOORD1; float fog : TEXCOORD2; };
            CBUFFER_START(UnityPerMaterial)
                float4 _DeepColor;
                float _MotionTime;
                float _WaveStrength;
            CBUFFER_END
            Varyings Vert(Attributes input)
            {
                Varyings o;
                o.positionWS = TransformObjectToWorld(input.positionOS.xyz);
                o.positionCS = TransformWorldToHClip(o.positionWS);
                o.screen=ComputeScreenPos(o.positionCS);
                o.fog=ComputeFogFactor(o.positionCS.z);
                return o;
            }
            half4 Frag(Varyings input) : SV_Target
            {
                float2 p=input.positionWS.xz; float t=_MotionTime;
                // Long, low-amplitude ripples keep vertical reflections coherent.
                float2 ripple=float2(sin(p.x*.73+p.y*.91+t*.42)+.24*sin(p.x*2.1-p.y*1.7-t*.31),
                    cos(p.y*.67+p.x*.51-t*.27)+.19*cos(p.y*1.9-p.x*2.3+t*.38));
                float3 normal=normalize(float3(ripple.x*.012,1,ripple.y*.012));
                float3 view=GetWorldSpaceNormalizeViewDir(input.positionWS);
                float2 uv=input.screen.xy/input.screen.w;
                float depth=LinearEyeDepth(SampleSceneDepth(uv),_ZBufferParams);
                float thickness=max(0,depth-input.screen.w);
                float2 distortion=ripple*_WaveStrength*saturate(thickness);
                float3 transmission=SampleSceneColor(uv+distortion*.25);
                transmission=lerp(transmission,_DeepColor.rgb,1-exp(-thickness*.55));
                float reflectionRoughness=lerp(.8,1.65,saturate(length(input.positionWS-_WorldSpaceCameraPos)*.022));
                float3 reflection=SAMPLE_TEXTURE2D_LOD(_PlanarReflection,sampler_PlanarReflection,uv+distortion,reflectionRoughness).rgb;
                float fresnel=.025+.975*pow(1-saturate(dot(normal,view)),5);
                float3 color=lerp(transmission,reflection,saturate(fresnel*.94+.08));
                return half4(MixFog(color,input.fog),1);
            }
            ENDHLSL
        }
    }
}
