Shader "URP/Postprocess/Mirror_mask"
{
    Properties
    { 
 
        _BrushedNormalMap("_BrushedNormalMap",2D) = "white" {}
        _NormalOffset("_NormalOffset",Range(0,1)) = 0.24
        _ReflectionTex("_ReflectionTex",2D) = "white" {}
        // _ShadowTex("_ShadowTex",2D) = "white" {}
        _MainTex ("Texture", 2D) = "white" {}
        _BrushScaleX("_BrushScaleX",float) = 0.1
        _BrushScaleY("_BrushScaleY",float) = 0.1
        _ReflectionEmissionAdjust("_ReflectionEmissionAdjust",float) = 10
        _FresnelParams("_FresnelParams",Vector) = (1.2, 0.14, 0.01, 0)

         _MaskMap("Mask",2D) = "white" {}
    }
    SubShader
    {
        //Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
             Cull Off
             Lighting Off
              ZWrite Off
            Blend SrcAlpha  OneMinusSrcAlpha
            
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag 
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 in_NORMAL0: NORMAL;
                half2 in_TEXCOORD0 : TEXCOORD0;
                half2 in_TEXCOORD1 : TEXCOORD1;
            };

            struct v2f
            {
                // float2 uv : TEXCOORD0; 
                float4 vertex : SV_POSITION; 
                half2 vs_TEXCOORD0: TEXCOORD0; 
                half2 vs_TEXCOORD1: TEXCOORD1; 
                half4 vs_TEXCOORD2: TEXCOORD2;
                half2 vs_mask: TEXCOORD3;
            };


            sampler2D _MaskMap;
            half4 _MaskMap_ST;

            half4 _Color;
            uniform 	float _BrushScaleX;
            uniform 	float _BrushScaleY;
            half _ReflectionEmissionAdjust;
            half _NormalOffset;
        
            sampler2D _BrushedNormalMap;
            half4 _BrushedNormalMap_ST;
            half4 _BrushedNormalMap_TexelSize;
            sampler2D _ReflectionTex;
            half4 _ReflectionTex_TexelSize;
            // sampler2D _ShadowTex;
            // half4 _ShadowTex_ST;
            sampler2D _MainTex;
            half4 _FresnelParams;
            half4 _MainTex_ST;
 
            v2f vert (appdata v)
            {
                v2f o;
                o.vs_TEXCOORD0 = v.in_TEXCOORD0 * _MainTex_ST.xy +_MainTex_ST.zw; 
                float4 worldPos = mul(unity_ObjectToWorld,v.vertex);//u_xlat0
                o.vs_TEXCOORD1 = v.vertex.xz * _BrushedNormalMap_ST.xy + _BrushedNormalMap_ST.zw;
                o.vertex = mul(UNITY_MATRIX_VP,worldPos);  
                o.vs_TEXCOORD2 = ComputeScreenPos( o.vertex);
                o.vs_mask = v.in_TEXCOORD0 * _MaskMap_ST.xy + _MaskMap_ST.zw;
                return o;
            }

            float4 blur(sampler2D tex, float2 uv, float4 dir, float4 tex_TexelSize)
            {
                float4 c = 0;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * .75)) * 126;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * 2.25)) * 84;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * 3.75)) * 36;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * 5.25)) * 9;
                c += tex2D(tex, uv + (tex_TexelSize.xy * dir * 6.75)) * 1;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * .75)) * 126;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * 2.25)) * 84;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * 3.75)) * 36;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * 5.25)) * 9;
                c += tex2D(tex, uv - (tex_TexelSize.xy * dir * 6.75)) * 1;
                return c / 512;
            }

            half4 frag(v2f i) : SV_Target
            {

                half2 normalTex = tex2D(_BrushedNormalMap, i.vs_TEXCOORD1.xy).xy;//  
                normalTex -= half2(1,1) * _NormalOffset;
                half2 noiseUV =  (normalTex.xy * 2 + 0.5*half2(-1,-1) ) * half2(_BrushScaleX, _BrushScaleY) ;
          
                half2 reflectUV = noiseUV + i.vs_TEXCOORD2.xy / i.vs_TEXCOORD2.w;

                float4 reflectTex = blur(_ReflectionTex, reflectUV, float4(_FresnelParams.z, 0, 0, 0), _ReflectionTex_TexelSize);
                reflectTex += blur(_ReflectionTex, reflectUV, float4(0, _FresnelParams.z, 0, 0), _ReflectionTex_TexelSize);
                reflectTex *= 0.5f * _ReflectionEmissionAdjust;
    
                half4 mainTexVar = tex2D(_MainTex, i.vs_TEXCOORD0.xy) ; //
                half4 maskCol = tex2D(_MaskMap, i.vs_mask.xy);

                half4 reflectColor ;
                reflectColor.xyz = reflectTex.xyz;
                reflectColor.w = reflectTex.w;  
                             
                half4 reflectAndShadowColor = lerp(mainTexVar, reflectColor, _FresnelParams.w) ;
                reflectAndShadowColor.a = maskCol.a;
                return reflectAndShadowColor;
            }
            ENDCG
        }
    }
}
