Shader "P33EffectsShader/P33_EffectGrab"
{
	Properties
	{
		[Toggle(_BLACKWHITEFLASHSWITCH_ON)] _BlackWhiteFlashSwitch("黑白闪开启", Float) = 0
		_BlackWhiteFlash("黑白闪对比度", Float) = 1.5
		_Color1("黑白闪颜色1", Color) = (1,1,1,0)
		_Color2("黑白闪颜色2", Color) = (0,0,0,0)
		_BlackWhiteFlashBlend("黑白闪和原图混合度", Range( 0 , 1)) = 0
		_GrabNoise("扭曲贴图", 2D) = "white" {}
		[Enum(RGB,0,R,1,G,2,B,3)]_NoiseTexCutRgba("扭曲贴图通道切换", Float) = 0
		_space_rd("扭曲强度", Range( 0 , 1)) = 0
		[Toggle(_POLARSWITCH_ON)] _PolarSwitch("极坐标开启", Float) = 0
		_RadialScale("径向缩放", Float) = 1
		_LengthScale("长度缩放", Float) = 1
		_U_Speed("X速度", Float) = 0
		_V_Speed("Y速度", Float) = 0
		_space_rd_mask("遮罩贴图", 2D) = "white" {}
		[Enum(RGB,0,R,1,G,2,B,3)]_MaskTexCutRgba2("遮罩贴图通道切换", Float) = 0
		[Enum(UnityEngine.Rendering.CompareFunction)]_ZTestMode("深度测试", Float) = 4
		[HideInInspector] _texcoord( "", 2D ) = "white" {}

	}
	
	SubShader
	{
		
		
		Tags { "RenderType"="Opaque" "Queue"="Transparent" }
	LOD 100

		CGINCLUDE
		#pragma target 3.0
		ENDCG
		Blend SrcAlpha OneMinusSrcAlpha
		AlphaToMask Off
		Cull Off
		ColorMask RGBA
		ZWrite Off
		ZTest [_ZTestMode]
		
		
		GrabPass{ }

		Pass
		{
			Name "Unlit"

			CGPROGRAM

			#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
			#define ASE_DECLARE_SCREENSPACE_TEXTURE(tex) UNITY_DECLARE_SCREENSPACE_TEXTURE(tex);
			#else
			#define ASE_DECLARE_SCREENSPACE_TEXTURE(tex) UNITY_DECLARE_SCREENSPACE_TEXTURE(tex)
			#endif


			#ifndef UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX
			//only defining to not throw compilation error over Unity 5.5
			#define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
			#endif
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_instancing
			#include "UnityCG.cginc"
			#include "UnityShaderVariables.cginc"
			#pragma shader_feature_local _BLACKWHITEFLASHSWITCH_ON
			#pragma shader_feature_local _POLARSWITCH_ON


			struct appdata
			{
				float4 vertex : POSITION;
				float4 color : COLOR;
				float4 ase_texcoord : TEXCOORD0;
				UNITY_VERTEX_INPUT_INSTANCE_ID
			};
			
			struct v2f
			{
				float4 vertex : SV_POSITION;
				#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
				float3 worldPos : TEXCOORD0;
				#endif
				float4 ase_texcoord1 : TEXCOORD1;
				float4 ase_texcoord2 : TEXCOORD2;
				float4 ase_color : COLOR;
				UNITY_VERTEX_INPUT_INSTANCE_ID
				UNITY_VERTEX_OUTPUT_STEREO
			};

			uniform half _ZTestMode;
			ASE_DECLARE_SCREENSPACE_TEXTURE( _GrabTexture )
			uniform sampler2D _GrabNoise;
			uniform half4 _GrabNoise_ST;
			uniform half _RadialScale;
			uniform half _LengthScale;
			uniform half _U_Speed;
			uniform half _V_Speed;
			uniform half _NoiseTexCutRgba;
			uniform half _space_rd;
			uniform half _BlackWhiteFlash;
			uniform half4 _Color1;
			uniform half4 _Color2;
			uniform half _BlackWhiteFlashBlend;
			uniform sampler2D _space_rd_mask;
			uniform half4 _space_rd_mask_ST;
			uniform half _MaskTexCutRgba2;
			inline float4 ASE_ComputeGrabScreenPos( float4 pos )
			{
				#if UNITY_UV_STARTS_AT_TOP
				float scale = -1.0;
				#else
				float scale = 1.0;
				#endif
				float4 o = pos;
				o.y = pos.w * 0.5f;
				o.y = ( pos.y - o.y ) * _ProjectionParams.x * scale + o.y;
				return o;
			}
			
			float4 CalculateContrast( float contrastValue, float4 colorTarget )
			{
				float t = 0.5 * ( 1.0 - contrastValue );
				return mul( float4x4( contrastValue,0,0,t, 0,contrastValue,0,t, 0,0,contrastValue,t, 0,0,0,1 ), colorTarget );
			}

			
			v2f vert ( appdata v )
			{
				v2f o;
				UNITY_SETUP_INSTANCE_ID(v);
				UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
				UNITY_TRANSFER_INSTANCE_ID(v, o);

				float4 ase_clipPos = UnityObjectToClipPos(v.vertex);
				float4 screenPos = ComputeScreenPos(ase_clipPos);
				o.ase_texcoord1 = screenPos;
				
				o.ase_texcoord2.xy = v.ase_texcoord.xy;
				o.ase_color = v.color;
				
				//setting value to unused interpolator channels and avoid initialization warnings
				o.ase_texcoord2.zw = 0;
				float3 vertexValue = float3(0, 0, 0);
				#if ASE_ABSOLUTE_VERTEX_POS
				vertexValue = v.vertex.xyz;
				#endif
				vertexValue = vertexValue;
				#if ASE_ABSOLUTE_VERTEX_POS
				v.vertex.xyz = vertexValue;
				#else
				v.vertex.xyz += vertexValue;
				#endif
				o.vertex = UnityObjectToClipPos(v.vertex);

				#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				#endif
				return o;
			}
			
			fixed4 frag (v2f i ) : SV_Target
			{
				UNITY_SETUP_INSTANCE_ID(i);
				UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
				fixed4 finalColor;
				#ifdef ASE_NEEDS_FRAG_WORLD_POSITION
				float3 WorldPosition = i.worldPos;
				#endif
				float4 screenPos = i.ase_texcoord1;
				float4 ase_grabScreenPos = ASE_ComputeGrabScreenPos( screenPos );
				half4 ase_grabScreenPosNorm = ase_grabScreenPos / ase_grabScreenPos.w;
				half2 uv_GrabNoise = i.ase_texcoord2.xy * _GrabNoise_ST.xy + _GrabNoise_ST.zw;
				half2 CenteredUV15_g1 = ( uv_GrabNoise - float2( 0.5,0.5 ) );
				half2 break17_g1 = CenteredUV15_g1;
				half2 appendResult23_g1 = (half2(( length( CenteredUV15_g1 ) * _RadialScale * 2.0 ) , ( atan2( break17_g1.x , break17_g1.y ) * ( 1.0 / 6.28318548202515 ) * _LengthScale )));
				#ifdef _POLARSWITCH_ON
				half2 staticSwitch63 = appendResult23_g1;
				#else
				half2 staticSwitch63 = uv_GrabNoise;
				#endif
				half2 appendResult32 = (half2(( _Time.y * _U_Speed ) , ( _Time.y * _V_Speed )));
				half4 tex2DNode1 = tex2D( _GrabNoise, ( staticSwitch63 + appendResult32 ) );
				half3 temp_cast_1 = (tex2DNode1.r).xxx;
				half temp_output_19_0_g2 = _NoiseTexCutRgba;
				half3 lerpResult10_g2 = lerp( tex2DNode1.rgb , temp_cast_1 , saturate( floor( temp_output_19_0_g2 ) ));
				half3 temp_cast_2 = (tex2DNode1.g).xxx;
				half3 lerpResult12_g2 = lerp( lerpResult10_g2 , temp_cast_2 , saturate( floor( (0.0 + (temp_output_19_0_g2 - 1.0) * (1.0 - 0.0) / (2.0 - 1.0)) ) ));
				half3 temp_cast_3 = (tex2DNode1.b).xxx;
				half3 lerpResult15_g2 = lerp( lerpResult12_g2 , temp_cast_3 , saturate( floor( (0.0 + (temp_output_19_0_g2 - 2.0) * (1.0 - 0.0) / (3.0 - 2.0)) ) ));
				half3 temp_cast_4 = (tex2DNode1.a).xxx;
				half3 lerpResult17_g2 = lerp( lerpResult15_g2 , temp_cast_4 , saturate( floor( (0.0 + (temp_output_19_0_g2 - 3.0) * (1.0 - 0.0) / (4.0 - 3.0)) ) ));
				half4 screenColor46 = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_GrabTexture,( ase_grabScreenPosNorm + half4( ( lerpResult17_g2 * ( _space_rd * 0.1 * i.ase_color.a ) ) , 0.0 ) ).xy);
				half3 desaturateInitialColor75 = screenColor46.rgb;
				half desaturateDot75 = dot( desaturateInitialColor75, float3( 0.299, 0.587, 0.114 ));
				half3 desaturateVar75 = lerp( desaturateInitialColor75, desaturateDot75.xxx, 1.0 );
				half4 temp_output_132_0 = round( CalculateContrast(_BlackWhiteFlash,half4( desaturateVar75 , 0.0 )) );
				half4 temp_output_83_0 = ( ( 1.0 - temp_output_132_0 ) * _Color2 );
				half4 lerpResult94 = lerp( ( temp_output_132_0 * _Color1 ) , temp_output_83_0 , temp_output_83_0);
				#ifdef _BLACKWHITEFLASHSWITCH_ON
				half4 staticSwitch98 = lerpResult94;
				#else
				half4 staticSwitch98 = screenColor46;
				#endif
				half4 lerpResult101 = lerp( staticSwitch98 , screenColor46 , _BlackWhiteFlashBlend);
				float2 uv_space_rd_mask = i.ase_texcoord2.xy * _space_rd_mask_ST.xy + _space_rd_mask_ST.zw;
				half4 tex2DNode51 = tex2D( _space_rd_mask, uv_space_rd_mask );
				half3 temp_cast_11 = (tex2DNode51.r).xxx;
				half temp_output_19_0_g3 = _MaskTexCutRgba2;
				half3 lerpResult10_g3 = lerp( tex2DNode51.rgb , temp_cast_11 , saturate( floor( temp_output_19_0_g3 ) ));
				half3 temp_cast_12 = (tex2DNode51.g).xxx;
				half3 lerpResult12_g3 = lerp( lerpResult10_g3 , temp_cast_12 , saturate( floor( (0.0 + (temp_output_19_0_g3 - 1.0) * (1.0 - 0.0) / (2.0 - 1.0)) ) ));
				half3 temp_cast_13 = (tex2DNode51.b).xxx;
				half3 lerpResult15_g3 = lerp( lerpResult12_g3 , temp_cast_13 , saturate( floor( (0.0 + (temp_output_19_0_g3 - 2.0) * (1.0 - 0.0) / (3.0 - 2.0)) ) ));
				half3 temp_cast_14 = (tex2DNode51.a).xxx;
				half3 lerpResult17_g3 = lerp( lerpResult15_g3 , temp_cast_14 , saturate( floor( (0.0 + (temp_output_19_0_g3 - 3.0) * (1.0 - 0.0) / (4.0 - 3.0)) ) ));
				half4 appendResult23 = (half4(lerpResult101.rgb , ( screenColor46.a * lerpResult17_g3 ).x));
				
				
				finalColor = appendResult23;
				return finalColor;
			}
			ENDCG
		}
	}
	CustomEditor "P33_EffectGrab_GUI"
	
	Fallback Off
}