Shader "URP/Effects/Particle/Partice_Distortion"
{
	Properties
	{
		//////////////////////////////////////////////////////////////------------------参数列表
		[Header(Mask)]
		_MaskTex("遮罩纹理" , 2D) = "white"{}
		[KeywordEnum(R , G , B)]_Channel("遮罩纹理通道选择" , float) = 0
		_MaskSpeedU("遮罩 U方向速度" , float) = 0
		_MaskSpeedV("遮罩 V方向速度" , float) = 0

		[Main(g2 , _ , 2)]_NoiseGroup("Noise Group" , float) = 1
		[Sub(g2)]_NoiseTex("扰动纹理" , 2D) = "white"{}
		[Sub(g2)]_NoiseScale("扰动强度" , Range(0.0 , 1.0)) = 0
		[Sub(g2)]_NoiseOffset("扰动偏移" , Range(-1.0 , 1.0)) = 0
		[Sub(g2)]_NoiseSpeedU("扰动 U方向速度" , float) = 0
		[Sub(g2)]_NoiseSpeedV("扰动 V方向速度" , float) = 0

		[Main(g1 , _ , 2)]_PolarGroup("Polar Group" , float) = 1
		[SubToggle(g1)]_ToggleSwitch("切换极坐标UV" , int) = 0
		[Sub(g1)]_Rotator("UV 旋转" , float) = 0
		[Sub(g1)]_ScaleOffset("UV 大小与偏移" , Vector) = (1,1,0,0)
		[Sub(g1)]_PolarSpeedU("极坐标 U方向速度" , float) = 0
		[Sub(g1)]_PolarSpeedV("极坐标 V方向速度" , float) = 0
		[Sub(g1)]_Power("UV power", float) = 1
		[Sub(g1)]_PolarMask("极坐标遮罩" , 2D) = "white"{}

		[Header(CullMode)]
		[Enum(UnityEngine.Rendering.CullMode)] _CullMode("剔除模式" , int) = 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
		{
			"RenderType" = "Transparent" 
			"IgnoreProjector" = "True" 
			"Queue" = "Transparent"
		}
		Cull[_CullMode]
		ZWrite [_ZWrite]
		Blend [_Src] [_Dst]
		//BlendOp [_BlendOp]
		Fog{ Mode off }
		
		HLSLINCLUDE
		#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Particles.hlsl"
		#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Macros.hlsl"
		
		CBUFFER_START(UnityPerMaterial)
		//////////////////////////////////////////////////////////////------------------遮罩纹理参数
		uniform float4 _MaskTex_ST;
		uniform float _MaskSpeedU;
		uniform float _MaskSpeedV;
		//////////////////////////////////////////////////////////////------------------扰动纹理
		uniform float4 _NoiseTex_ST;
		uniform float _NoiseOffset;
		uniform float _NoiseScale;
		uniform float _NoiseSpeedU;
		uniform float _NoiseSpeedV;
		//////////////////////////////////////////////////////////////------------------极坐标参数
		uniform bool _ToggleSwitch;
		uniform float _Rotator;
		uniform float4 _ScaleOffset;
		uniform float _PolarSpeedU;
		uniform float _PolarSpeedV;
		uniform float _Power;
		uniform float4 _PolarMask_ST;
		CBUFFER_END
		ENDHLSL

		Pass
		{
			Tags{"LightMode" = "UniversalForward"}
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_instancing
			#pragma shader_feature _CHANNEL_R _CHANNEL_G _CHANNEL_B
			#pragma shader_feature _TOGGLE_OFF
			//#define PI 3.14

			//////////////////////////////////////////////////////////////------------------顶点数据
			struct a2v
			{
				float4 vertex : POSITION;
				float2 texcoord : TEXCOORD0;
				float4 color : COLOR;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};
			//////////////////////////////////////////////////////////////------------------顶点到片元数据		
			struct v2f
			{
				float4 positionCS : SV_POSITION;
				float4 uv : TEXCOORD0;
				float2 PolarMaskUV : TEXCOORD1;
				float4 color:COLOR;
				UNITY_VERTEX_INPUT_INSTANCE_ID
				UNITY_VERTEX_OUTPUT_STEREO
			};
			//////////////////////////////////////////////////////////////------------------纹理声明
			TEXTURE2D(_CameraColorTexture); 
			SAMPLER(sampler_CameraColorTexture);
			TEXTURE2D(_MaskTex);
			SAMPLER(sampler_MaskTex);
			TEXTURE2D(_NoiseTex);
			SAMPLER(sampler_NoiseTex);
			TEXTURE2D(_PolarMask);
			SAMPLER(sampler_PolarMask);
			//////////////////////////////////////////////////////////////------------------顶点着色器
			v2f vert(a2v v)
			{
				v2f o;
				UNITY_SETUP_INSTANCE_ID(v);
				UNITY_TRANSFER_INSTANCE_ID(v, o);
				UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
				VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
				o.positionCS = vertexInput.positionCS;
				o.color = v.color;
				o.uv.xy = v.texcoord;
				o.uv.zw = v.texcoord;
				o.PolarMaskUV = TRANSFORM_TEX(v.texcoord , _PolarMask);
				return o;
			}
			//////////////////////////////////////////////////////////////------------------片元着色器
			half4 frag(v2f i) : SV_Target
			{
				UNITY_SETUP_INSTANCE_ID(i);
				UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
				float2 masktexcoord = (i.uv.xy * _MaskTex_ST.xy + _MaskTex_ST.zw) +  + float2(_MaskSpeedU, _MaskSpeedV) * _Time.y;
				float2 noisetexcoord = (i.uv.zw * _NoiseTex_ST.xy + _NoiseTex_ST.zw) + float2(_NoiseSpeedU, _NoiseSpeedV) * _Time.y;
				noisetexcoord += _NoiseOffset;
				//////////////////////////////////////////////////////////////------------------极坐标
				float2 uv01_g3 = i.uv.xy * float2( 1,1 ) + float2( 0,0 );
				float cos8_g3 = cos( ( ( ( 1.0 - length( (uv01_g3*2.0 + -1.0) ) ) * _Rotator ) * PI ) );
				float sin8_g3 = sin( ( ( ( 1.0 - length( (uv01_g3*2.0 + -1.0) ) ) * _Rotator ) * PI ) );
				float2 rotator8_g3 = mul( uv01_g3 - float2( 0.5,0.5 ) , float2x2( cos8_g3 , -sin8_g3 , sin8_g3 , cos8_g3 )) + float2( 0.5,0.5 );
				float2 temp_output_9_0_g3 = (rotator8_g3 * 2.0 + -1.0);
				float2 break10_g3 = temp_output_9_0_g3;
				float2 appendResult22_g3 = (float2(pow( length( temp_output_9_0_g3 ) , _Power ) , ( ( atan2( break10_g3.y , break10_g3.x ) / ( 2.0 * PI ) ) + 0.5 )));
				float4 break25_g3 = _ScaleOffset;
				float2 appendResult20_g3 = (float2(break25_g3.x , break25_g3.y));
				float2 appendResult21_g3 = (float2(break25_g3.z , break25_g3.w));
				float2 polarUV = (appendResult22_g3 * appendResult20_g3 + appendResult21_g3) + float2(_PolarSpeedU, _PolarSpeedV) * _Time.y;
				float2 polarNoiseUV = (appendResult22_g3 * appendResult20_g3 + appendResult21_g3) + float2(_NoiseSpeedU, _NoiseSpeedV) * _Time.y;
				half4 PolarMask = SAMPLE_TEXTURE2D(_PolarMask, sampler_PolarMask, i.PolarMaskUV);
				float2 MaskUV = _ToggleSwitch ? polarUV : masktexcoord ;
				float2 NoiseUV = _ToggleSwitch ? polarNoiseUV : noisetexcoord;
				//////////////////////////////////////////////////////////////------------------扰动纹理
				half4 NoiseTex = SAMPLE_TEXTURE2D(_NoiseTex, sampler_NoiseTex, NoiseUV);
				half2 Noise = NoiseTex.rg *_NoiseScale;
				//////////////////////////////////////////////////////////////------------------遮罩纹理通道选择
				half4 MaskTex = SAMPLE_TEXTURE2D(_MaskTex , sampler_MaskTex , MaskUV + Noise);
				MaskTex *= PolarMask.r;
				#if _CHANNEL_R
					half mask = MaskTex.r;
				#elif _CHANNEL_G
					half mask = MaskTex.g;
				#else
					half mask = MaskTex.b;
				#endif
				//////////////////////////////////////////////////////////////------------------计算屏幕坐标
				float2 ScreenUV=(i.positionCS.xy / _ScreenParams.xy) + (Noise * mask);
				float4 ColorTex = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, ScreenUV);
				//ColorTex.rgb *= 1.5;
				//////////////////////////////////////////////////////////////------------------乘上顶点色
				ColorTex.a *= i.color.a;
				return 0;
				return ColorTex;
			}
			ENDHLSL
		}
	}
	CustomEditor "FZDL.ShaderDrawer.LWGUI"			
}
