Shader "Universal Render Pipeline/2D/Spine/Skeleton Lit"
{
    Properties
    {
        [NoScaleOffset] _MainTex ("Main Texture", 2D) = "black" {}

        [NoScaleOffset] _MainColor ("Main Color", Color) = (1,1,1,1)

        [NoScaleOffset] _MaskTex("Mask", 2D) = "white" {}
        _LiuGuang("LiuGuang", 2D) = "white" {}
        [Gamma][HDR] _LiuGuangColor ("LiuGuangColor", Color) = (1,1,1,1)
        [Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 0
        [MaterialToggle(_LIGHT_AFFECTS_ADDITIVE)] _LightAffectsAdditive("Light Affects Additive", Float) = 0
        [HideInInspector] _StencilRef("Stencil Reference", Float) = 1.0
        [Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Compare", Float) = 0.0 // Disabled stencil test by default
        _LiuGuangTime("LiuGuangTime", Float) = 0
        _ViewportPointY("屏幕视点Y坐标", Float) = 0
        _EnableFog("启用全局雾", Float) = 1
        _Fogstrength("强度", Float) = 0
        _FogColor("雾的颜色", Color) = (1,1,1,1)

        _Freeze("Freeze", Float) = 0

        _freezeNoises ("冰冻噪音图", 2D) = "black" {}
        _freezeSurface ("冰面贴图", 2D) = "black" {}
        _freezeTransition ("冰面过度图", 2D) = "black" {}
        _freezeAddColor ("冰冻叠加颜色", Color) = (0,0,0,0)
        _freezeAddStrength("冰冻强度", Float) = 0
        _freezeWorldX("冰冻起始的X值", Float) = 0
        _freezePow("冰冻Pow值", Float) = 8
        _dis("冰冻差值", Float) = 8

    }

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

    SubShader
    {
        // UniversalPipeline tag is required. If Universal render pipeline is not set in the graphics settings
        // this Subshader will fail.
        Tags
        {
            "Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True"
        }
        Cull Off
        ZWrite off

//        ZTest On
        Stencil
        {
            Ref[_StencilRef]
            Comp[_StencilComp]
            Pass Keep
        }

        Pass
        {
            Tags
            {
                "LightMode" = "Universal2D"
            }

            ZWrite off
            Cull Off
//            ZTest On
            Blend One OneMinusSrcAlpha

            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
            #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
            #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
            #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
            #pragma multi_compile _ _LIGHT_AFFECTS_ADDITIVE

            // Fog
            // #pragma multi_compile_local __ ENABLE_FOG_ON
            // #pragma multi_compile __ ENABLE_FOG_ON
            // #pragma multi_compile ENABLE_FOG_ON __
            // #pragma shader_feature _ ENABLE_FOG_ON
            // #pragma multi_compile __ ENABLE_FOG

            #include <HLSLSupport.cginc>

            struct Attributes
            {
                float3 positionOS : POSITION;
                half4 color : COLOR;
                float2 uv : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                half4 color : COLOR0;
                float2 uv : TEXCOORD0;
                float2 lightingUV : TEXCOORD1;
                float2 scenUV : TEXCOORD2;
                float3 objectPos : TEXCOORD3;
            };

            // Spine related keywords
            #pragma shader_feature _ _STRAIGHT_ALPHA_INPUT
            #pragma vertex CombinedShapeLightVertex
            #pragma fragment CombinedShapeLightFragment

            #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
            #define USE_URP
            #include "../Include/SpineCoreShaders/Spine-Common.cginc"

            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            TEXTURE2D(_MaskTex);
            SAMPLER(sampler_MaskTex);


            uniform half4 _ExploreFogColor;
            float _ViewportPointY;
            float _EnableFog;
            fixed _GrayFloat;
            float4 _MainColor;
            float4 _TextrueRect[3];
            sampler2D _LiuGuang;

            float4 _MainTex_TexelSize;

            float4 _LiuGuang_ST;
            float4 _LiuGuangColor;

            float4 _vertextPos;

            float _LiuGuangTime;
            float _Fogstrength;
            float4 _FogColor;

            float _Freeze;
            sampler2D _freezeNoises;
            // float4 _freezeNoises_TexelSize;
            // float4 _freezeNoises_ST;
            sampler2D _freezeSurface;
            sampler2D _freezeTransition;
            float4 _freezeAddColor;
            float _freezeAddStrength;
            float _freezePow;
            float _dis;
            float _freezeWorldX;


            #if USE_SHAPE_LIGHT_TYPE_0
            SHAPE_LIGHT(


                0
            )
            #endif

            #if USE_SHAPE_LIGHT_TYPE_1
            SHAPE_LIGHT(


                1
            )
            #endif

            #if USE_SHAPE_LIGHT_TYPE_2
            SHAPE_LIGHT(


                2
            )
            #endif

            #if USE_SHAPE_LIGHT_TYPE_3
            SHAPE_LIGHT(


                3
            )
            #endif


            Varyings CombinedShapeLightVertex(Attributes v)
            {
                Varyings o = (Varyings)0;
                o.objectPos = v.positionOS;
                o.positionCS = TransformObjectToHClip(v.positionOS);
                o.uv = v.uv;
                float4 clipVertex = o.positionCS / o.positionCS.w;
                o.scenUV = mul(unity_ObjectToWorld, v.positionOS);
                o.lightingUV = ComputeScreenPos(clipVertex).xy;
                o.color = PMAGammaToTargetSpace(v.color);
                return o;
            }

            #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"

            float GetGaoPing(float2 uv, half4 mainUV0Color)
            {
                float2 res = _MainTex_TexelSize.xy;
                half4 nostionColor = mainUV0Color;
                half4 nostionColor1 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(1, 0) * res);
                half4 nostionColor2 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(-1, 0) * res);
                half4 nostionColor3 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(0, 1) * res);
                half4 nostionColor4 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv + float2(0, -1) * res);
                half4 c = (nostionColor1 + nostionColor2 + nostionColor3 + nostionColor4) / 4;
                return distance(c, nostionColor);
            }


            half4 GetFreezeColor(half4 mainUV0Color, Varyings i)
            {
                // _freezePow = 8;
                float d = GetGaoPing(i.uv, mainUV0Color) * 10;
                half4 noisesColor = tex2D(_freezeNoises, i.objectPos.xy) * d;
                half4 surfaceColor = tex2D(_freezeSurface, i.objectPos.xy) * _freezeAddStrength * _freezeAddColor;
                half4 freezeColor = noisesColor + surfaceColor;

                half4 TransitionColor = tex2D(_freezeTransition, i.objectPos.xy);
                float worldX = i.objectPos.x + TransitionColor.x;
                float d1 = worldX - _freezeWorldX;
                d1 = max(d1, 0);
                float d1Pow = pow(d1, _freezePow);
                d1Pow = clamp(d1Pow, 0, 1);

                float d2 = worldX - _freezeWorldX - _dis;
                d2 = max(d2, 0);

                float d2Pow = pow(d2, _freezePow);
                d2Pow = clamp(d2Pow, 0, 1);
                float d1posSub = (d1Pow - d2Pow) * 10;

                float qianColor = pow(d1, _freezePow * -1);
                qianColor = clamp(qianColor, 0, 1);
                half4 c = freezeColor * qianColor;
                return c + d1posSub;
            }


            half4 CombinedShapeLightFragment(Varyings i) : SV_Target
            {
                half4 uv0Color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
                half4 tex = uv0Color * _MainColor;

                tex.rgb = lerp(tex.rgb, _FogColor.rgb, _Fogstrength);

                tex.rgb *= tex.a;
                #if defined(_STRAIGHT_ALPHA_INPUT)

                #endif

                half4 main = tex * i.color;
                #if !defined(_LIGHT_AFFECTS_ADDITIVE)
                if (i.color.a == 0)
                    return main;
                #endif
                half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);


                SurfaceData2D surfaceData;
                InputData2D inputData;
                surfaceData.albedo = main.rgb;
                surfaceData.alpha = 1;
                surfaceData.mask = mask;
                inputData.uv = i.uv;
                inputData.lightingUV = i.lightingUV;
                half4 color = half4(CombinedShapeLightShared(surfaceData, inputData).rgb, main.a);

                float gray = dot(color.rgb, float3(0.299, 0.587, 0.114));
                float3 grayColor = fixed3(gray, gray, gray);
                color.rgb = lerp(color.rgb, grayColor, _GrayFloat);


                float2 pointUV = _TextrueRect[0].xy;
                float2 maxUV = _TextrueRect[0].zw;
                if (i.uv.x > pointUV.x && i.uv.x < maxUV.x && i.uv.y > pointUV.y && i.uv.y < maxUV.y)
                {
                    float2 blUV = (i.scenUV - _vertextPos.xy) / (_vertextPos.zw - _vertextPos.xy);
                    float2 liuGuangUV = blUV * _LiuGuang_ST.xy + _LiuGuang_ST.zw - _LiuGuangTime;
                    liuGuangUV = float2(liuGuangUV.x, liuGuangUV.y);
                    color.rgb += tex2D(_LiuGuang, liuGuangUV).rgb * color.a * _LiuGuangColor;
                }

                pointUV = _TextrueRect[1].xy;
                maxUV = _TextrueRect[1].zw;
                if (i.uv.x > pointUV.x && i.uv.x < maxUV.x && i.uv.y > pointUV.y && i.uv.y < maxUV.y)
                {
                    float2 blUV = (i.scenUV - _vertextPos.xy) / (_vertextPos.zw - _vertextPos.xy);
                    float2 liuGuangUV = blUV * _LiuGuang_ST.xy + _LiuGuang_ST.zw - _LiuGuangTime;
                    liuGuangUV = float2(liuGuangUV.x, liuGuangUV.y);
                    color.rgb += tex2D(_LiuGuang, liuGuangUV).rgb * color.a * _LiuGuangColor;
                }
                if (_Freeze > 1)
                {
                    half4 freezeColor = GetFreezeColor(uv0Color, i) * color.a;
                    color.rgb += freezeColor.rgb;
                }

                return color;
            }
            ENDHLSL
        }
    }
    FallBack "Universal Render Pipeline/2D/Sprite-Lit-Default"
}