Shader "URP/Effects/Particle/Test/Distort"
{
	Properties
	{
		[Enum(Both,0,Back,2)] _CullMode("Cull Mode", int) = 2 
		_NoiseTex("Noise", 2D) = "black" {}
		_MaskTex("Mask", 2D) = "white" {}
		_DistortTimeFactor("DistortTimeFactor", float) = 0
		_DistortStrength("DistortStrength", float) = 0
	}
 
	HLSLINCLUDE
	#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Particles.hlsl"
	uniform sampler2D _NoiseTex;
	float4 _NoiseTex_ST;
	uniform sampler2D _MaskTex;
	uniform float _DistortTimeFactor;
	uniform float _DistortStrength;
	struct Attributes
	{
			float4 vertex   : POSITION;
			float2 texcoord     : TEXCOORD0;
			float4 color:COLOR;
			UNITY_VERTEX_INPUT_INSTANCE_ID
	};
	struct v2f
	{
		float4 pos : SV_POSITION;
		float4 texcoord : TEXCOORD0;
		float2 uv : TEXCOORD1;
		float2 Grabuv : TEXCOORD2;
		float4 color:COLOR;
		UNITY_VERTEX_INPUT_INSTANCE_ID
		UNITY_VERTEX_OUTPUT_STEREO
	};
	v2f vert(Attributes 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.pos = vertexInput.positionCS;
		o.uv = v.texcoord;
		float4 screenUV = ComputeScreenPos(o.pos);
     	o.Grabuv = screenUV.xy/screenUV.w;
		o.color = clamp(v.color, 0.0, 1.0);
		return o;
	}
	half4 frag(v2f i) : SV_Target
	{
		UNITY_SETUP_INSTANCE_ID(i);
		UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
		float4 noise = tex2D(_NoiseTex, (i.Grabuv - _Time.xy * _DistortTimeFactor)*_NoiseTex_ST.xy);
		float2 offset = noise.xy * _DistortStrength;
		half4 factor = tex2D(_MaskTex, i.uv);
		float2 uv = offset * factor.r + i.Grabuv;
		float4 texCol = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, uv);
		texCol.a *= i.color.a;
		return texCol;
	}
 
	ENDHLSL
 
	SubShader
	{
	Tags{
		"RenderType" = "Transparent" 
		"IgnoreProjector" = "True" 
		"Queue" = "Transparent"
		}
		Pass
		{
			Cull[_CullMode]
			ZWrite On
			Blend SrcAlpha OneMinusSrcAlpha
			Fog{ Mode off }
 
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest 
			ENDHLSL
		}
	}
}