// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "URP/Effects/Particle/AdditiveOverlay" {
	Properties {
		_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
		_MainTex ("Particle Texture", 2D) = "white" {}
		_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0

		_RootPos("RootPos",Vector) = (0,0,0,1)
		[MaterialToggle] _isMirror("isMirror", Float) = 0
		_Normal("Normal",Vector) = (0,0,0,1)

		_DistanceFade("Distance Fade",Float) = 0

		[Enum(Add,1,Blend,11)]_SrcBlend("SrcBlend", Float) = 1
		[Enum(UnityEngine.Rendering.CullMode)] _CullMode("Cull Mode", float) = 0
	}
	SubShader {
		Tags { "Queue"="Transparent+100" "IgnoreProjector"="True" "RenderType"="Transparent" }
		CGINCLUDE
		#include "UnityCG.cginc"

		sampler2D _MainTex;
		fixed4 _TintColor;
		
		struct appdata_t {
			float4 vertex : POSITION;
			fixed4 color : COLOR;
			float2 texcoord : TEXCOORD0;
		};

		struct v2f {
			float4 vertex : SV_POSITION;
			fixed4 color : COLOR;
			float2 texcoord : TEXCOORD0;
			UNITY_FOG_COORDS(1)
			#ifdef SOFTPARTICLES_ON
				float4 projPos : TEXCOORD2;
			#endif
			float4 worldPos : TEXCOORD3;
		};
		
		float4 _MainTex_ST; 
		float4 _RootPos;
		float4 _Normal;
		float _isMirror;

		float _DistanceFade;


		v2f vert (appdata_t v)
		{
			v2f o;

			float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
			o.worldPos = worldPos;

			if (_isMirror > 0)
			{
				//float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
				float f = dot(_Normal.xyz, (_RootPos - worldPos).xyz);
				if (f < 0)
				{
					_Normal = -_Normal;
				}
				_Normal.xyz = _Normal.xyz * dot(_Normal.xyz, (_RootPos - worldPos).xyz);
				worldPos.xyz = worldPos.xyz + 2 * _Normal.xyz;
				v.vertex = mul(unity_WorldToObject, worldPos);
			}

			o.vertex = UnityObjectToClipPos(v.vertex);
			#ifdef SOFTPARTICLES_ON
				o.projPos = ComputeScreenPos (o.vertex);
				COMPUTE_EYEDEPTH(o.projPos.z);
			#endif
			o.color = v.color;
			o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
			UNITY_TRANSFER_FOG(o,o.vertex);
			return o;
		}

		sampler2D_float _CameraDepthTexture;
		float _InvFade;
		
		fixed4 frag (v2f i) : SV_Target
		{
			#ifdef SOFTPARTICLES_ON
				float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
				float partZ = i.projPos.z;
				float fade = saturate (_InvFade * (sceneZ-partZ));
				i.color.a *= fade;
			#endif
			
			float CameraFade = saturate(distance(i.worldPos,_WorldSpaceCameraPos.xyz) - _DistanceFade);
			i.color.a *= CameraFade;


			fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
			UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode
			return col;
		}
		ENDCG
		// Pass {
		// 	Tags{ "LightMode" = "UniversalForward" }
		// 	Blend SrcAlpha [_SrcBlend]
		// 	ColorMask RGB
		// 	Cull[_CullMode]
		// 	Lighting Off ZWrite Off
		// 	CGPROGRAM
		// 	#pragma vertex vert
		// 	#pragma fragment frag
		// 	#pragma multi_compile_particles
		// 	#pragma multi_compile_fog
			
		// 	ENDCG 
		// }
		Pass {
			Tags{ "LightMode" = "SRPDefaultUnlit" }
			Blend SrcAlpha [_SrcBlend]
			ColorMask RGB
			Cull[_CullMode]
			Lighting Off ZWrite Off
			ZTest Off
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_particles
			#pragma multi_compile_fog
			
			ENDCG 
		}
	}	
}
