Shader "FZDL_Shader/Particles/JR_Particle_2Noise_Dissolve"
{
    Properties
    {
        //////////////////////////////////////////////////////////////------------------参数列表
        _MainTex ("主纹理贴图", 2D) = "white" {}
        [KeywordEnum(R, G, B)]_Channel("选择主纹理贴图通道", float) = 0
        [HDR]_Color1("暗部颜色", Color) = (0,0,0,0)
        [HDR]_Color2("亮部颜色", Color) = (1,1,1,1)
        _ColorRange("颜色范围", Range(0.01, 5.0)) = 1
        _MainSpeedU("主纹理 U方向速度", float) = 0
        _MainSpeedV("主纹理 V方向速度", float) = 0
        _Clip("主纹理自身溶解", Range(0.0, 1.0)) = 0

        [Main(g1 , _ , 2)]_Noise01Group("Noise01 Group" , float) = 1
        [Sub(g1)]_Noise01("扰动纹理01", 2D) = "white"{}
        [Sub(g1)]_Noise01Scale("扰动01 强度", Range(0.0, 1.0)) = 0
        [Sub(g1)]_Noise01Offset("扰动01 偏移", Range(-1.0, 1.0)) = 0
        [Sub(g1)]_Noise01SpeedU("扰动01 U方向速度", float) = 0
        [Sub(g1)]_Noise01SpeedV("扰动01 V方向速度", float) = 0

        [Main(g2 , _ , 2)]_Noise02Group("Noise02 Group" , float) = 1
        [Sub(g2)]_Noise02("扰动纹理02", 2D) = "white"{}
        [Sub(g2)]_Noise02Scale("扰动02 强度", Range(0.0, 1.0)) = 0
        [Sub(g2)]_Noise02Offset("扰动02 偏移", Range(-1.0, 1.0)) = 0
        [Sub(g2)]_Noise02SpeedU("扰动02 U方向速度", float) = 0
        [Sub(g2)]_Noise02SpeedV("扰动02 V方向速度", float) = 0

        [Header(Advanced)]
        [Main(Soft Particle,_SOFTPARTICLES_ON,0)] FZDL_SOFT_PARTICLE("Soft Particle",float) = 0
        [Title(Soft Particle,Distance)]
        // [Sub(Soft Particle)] _SoftParticle_Near("Near",Range(0.0 , 5.0)) = 1 
        // [Sub(Soft Particle)] _SoftParticle_Far("far",Range(0.0 , 10.0)) = 1 
        [Sub(Soft Particle)] _DepthFade("Depth Fade",Range( 0.001 , 20)) = 0.1 
        [Sub(Soft Particle)] _DepthThresh("Depth Thresh",Range( 0.001 , 20)) = 2 

        [Header(CullMode)]
        [Enum(UnityEngine.Rendering.CullMode)]_Cull("剔除模式", float) = 2

        [Header(Depth)]
        [Enum(Off, 0, On, 1)]_ZWrite("是否写入深度", float) = 0

        [Header(BlendMode)]
        [Enum(UnityEngine.Rendering.BlendMode)]_Src("源颜色", float) = 5
        [Enum(UnityEngine.Rendering.BlendMode)]_Dst("目标颜色", float) = 10
        [Enum(UnityEngine.Rendering.BlendOp)]_BlendOp("混合模式", float) = 0
    }
    
    SubShader
    {
        Tags { 
            "RenderPipeline" = "UniversalPipeline"
            "RenderType" = "Transparent"
            "Queue" = "Transparent+100"
        }

        Pass
        {
            //Tags{"LightMode" = "TransparentDepthFog"}
			Name "FORWARD"
            Cull [_Cull]
            ZWrite [_ZWrite]
            Blend [_Src][_Dst]
            BlendOp [_BlendOp]

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

            CBUFFER_START(UnityPerMaterial)
//////////////////////////////////////////////////////////////------------------主纹理参数
            uniform float4 _MainTex_ST;
            uniform half4 _Color1;
            uniform half4 _Color2;
            uniform half _ColorRange;
            uniform float _MainSpeedU;
            uniform float _MainSpeedV;
            uniform half _Clip;
//////////////////////////////////////////////////////////////------------------扰动纹理 01
            uniform float4 _Noise01_ST;
            uniform float _Noise01Scale;
            uniform float _Noise01Offset;
            uniform float _Noise01SpeedU;
            uniform float _Noise01SpeedV;
//////////////////////////////////////////////////////////////------------------扰动纹理 02
            uniform float4 _Noise02_ST;
            uniform float _Noise02Scale;
            uniform float _Noise02Offset;
            uniform float _Noise02SpeedU;
            uniform float _Noise02SpeedV;
//////////////////////////////////////////////////////////////------------------软粒子
            uniform float _DepthFade;
            uniform float _DepthThresh;
            CBUFFER_END
            ENDHLSL

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0
            #pragma multi_compile_instancing
            #pragma shader_feature _CHANNEL_R _CHANNEL_G _CHANNEL_B
            #pragma shader_feature_local _SOFTPARTICLES_ON
//////////////////////////////////////////////////////////////------------------顶点数据
            struct appdata
            {
                float4 positionOS : POSITION;
                float2 uv : TEXCOORD0;
                half4 Color : COLOR;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };
//////////////////////////////////////////////////////////////------------------顶点到片元数据
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 NoiseUV : TEXCOORD1;
                float4 positionCS : SV_POSITION;
                half4 VertexColor : COLOR;
            #if defined(_SOFTPARTICLES_ON)
                float4 JRScreen        : TEXCOORD2;
            #endif
                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO
            };
//////////////////////////////////////////////////////////////------------------纹理声明
            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            TEXTURE2D(_Noise01);
            SAMPLER(sampler_Noise01);
            TEXTURE2D(_Noise02);
            SAMPLER(sampler_Noise02);
//////////////////////////////////////////////////////////////------------------顶点着色器
            v2f vert (appdata v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_TRANSFER_INSTANCE_ID(v, o);

                VertexPositionInputs PosIN = GetVertexPositionInputs(v.positionOS);
                o.positionCS = PosIN.positionCS;
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv += float2(_MainSpeedU, _MainSpeedV) * _Time.y;
                o.NoiseUV.xy = TRANSFORM_TEX(v.uv, _Noise01);
                o.NoiseUV.xy += float2(_Noise01SpeedU, _Noise01SpeedV) * _Time.y;
                o.NoiseUV.zw = TRANSFORM_TEX(v.uv, _Noise02);
                o.NoiseUV.zw += float2(_Noise02SpeedU, _Noise02SpeedV) * _Time.y;
                o.VertexColor = v.Color;
            #if defined(_SOFTPARTICLES_ON)
                o.JRScreen = ComputeScreenPos(PosIN.positionCS);
            #endif
                return o;
            }
//////////////////////////////////////////////////////////////------------------片元着色器
            half4 frag (v2f i) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(i);
                float4 Noise01 = SAMPLE_TEXTURE2D(_Noise01, sampler_Noise01, i.NoiseUV.xy) * _Noise01Scale;
                float4 Noise02 = SAMPLE_TEXTURE2D(_Noise02, sampler_Noise02, i.NoiseUV.zw) * _Noise02Scale;
                float2 MainUV = i.uv + Noise01.rg + Noise02.rg + _Noise01Offset + _Noise02Offset;
                half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, MainUV);
//////////////////////////////////////////////////////////////------------------主纹理通道选择 
                #if _CHANNEL_R
                    half SwitchChannel = col.r;
                #elif _CHANNEL_G
                    half SwitchChannel = col.g;
                #elif _CHANNEL_B
                    half SwitchChannel = col.b;
                #endif
//////////////////////////////////////////////////////////////------------------主纹理颜色
                SwitchChannel = saturate(pow(SwitchChannel, _ColorRange));
                half3 finalColor = lerp(_Color1, _Color2, SwitchChannel) * i.VertexColor.rgb;
//////////////////////////////////////////////////////////////------------------透明度
                half finalAlpha = saturate(SwitchChannel - _Clip) * i.VertexColor.a;
//////////////////////////////////////////////////////////////------------------软粒子
            #if defined(_SOFTPARTICLES_ON)
                float4 screenPos = i.JRScreen;
                float2 uv = UnityStereoTransformScreenSpaceTex(screenPos.xy / screenPos.w);
                //float eyeDepth = Linear01Depth(SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, screenPos).r, _ZBufferParams);
                float eyeDepth = LinearEyeDepth((SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, uv)).r,_ZBufferParams);
                float TempOutput = eyeDepth - screenPos.w;
                finalAlpha = lerp( saturate(_DepthFade * TempOutput) , 1 , saturate(TempOutput / _DepthThresh)) * finalAlpha;
                //fin_a *= SoftParticles(_SoftParticle_Near,1.0/(_SoftParticle_Far - _SoftParticle_Near),i.projectedPosition);
            #endif

				return half4(finalColor,finalAlpha);
                //return half4(finalColor, finalAlpha);
            }
            ENDHLSL
        }
    }
}
