// Made with Amplify Shader Editor
// Available at the Unity Asset Store - http://u3d.as/y3X 
Shader "URP/Effects/Particle/Test/Rongjieshader"
{
	Properties
	{
		[RongjieCustom1X]_MainTex("MainTex", 2D) = "white" {}
		_RongjieTex("RongjieTex", 2D) = "white" {}
		_Cutoff( "Mask Clip Value", Float ) = 0.5
	}

	SubShader
		{

			Tags{ "RenderType" = "Transparent"  "Queue" = "Overlay+0" "IgnoreProjector" = "True" "IsEmissive" = "true"  }
			Cull Back
			Blend SrcAlpha OneMinusSrcAlpha

			CGINCLUDE
			#include "UnityPBSLighting.cginc"
			#include "Lighting.cginc"
			#pragma target 3.0
			#undef TRANSFORM_TEX
			#define TRANSFORM_TEX(tex,name) float4(tex.xy * name##_ST.xy + name##_ST.zw, tex.z, tex.w)
			struct Input
			{
				float4 vertexColor : COLOR;
				float4 uv_tex4coord;
				float2 uv_texcoord;
			};

			uniform sampler2D _MainTex;
			uniform sampler2D _RongjieTex;
			uniform float4 _RongjieTex_ST;
			uniform float _Cutoff = 0.5;

			inline half4 LightingUnlit(SurfaceOutput s, half3 lightDir, half atten)
			{
				return half4 (0, 0, 0, s.Alpha);
			}

			void surf(Input i , inout SurfaceOutput o)
			{
				o.Emission = (i.vertexColor * tex2D(_MainTex, i.uv_tex4coord.xy) * i.uv_tex4coord.w).rgb;
				o.Alpha = i.vertexColor.a;
				float2 uv_RongjieTex = i.uv_texcoord * _RongjieTex_ST.xy + _RongjieTex_ST.zw;
				clip((tex2D(_RongjieTex, uv_RongjieTex).r + (-1.0 + (i.uv_tex4coord.z - 0.0) * (1.0 - -1.0) / (1.0 - 0.0))) - _Cutoff);
			}

			ENDCG

			Pass
			{
				CGPROGRAM
				//#pragma surface surf Unlit keepalpha fullforwardshadows 

				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"

				float4 _MainTex_ST;

				#define TRANSFORM_TEX(tex,name) float4(tex.xy * name##_ST.xy + name##_ST.zw, tex.z, tex.w)
				struct Attributes
				{
					float4 positionOS   : POSITION;
					float4 vertexColor : COLOR;
					float4 texcoord     : TEXCOORD0;
					UNITY_VERTEX_INPUT_INSTANCE_ID
				};

				struct v2f
				{
					float4 vertex : SV_POSITION;
					float4 vertexColor : COLOR;
					float4 uv : TEXCOORD2;
					float2 uv2 : TEXCOORD3;
					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);

					o.vertex = UnityObjectToClipPos(v.positionOS);
					o.vertexColor = v.vertexColor;

					o.uv.xyzw = TRANSFORM_TEX(v.texcoord, _MainTex);
					o.uv2 = TRANSFORM_TEX(v.texcoord, _RongjieTex);

					//VertexPositionInputs vertexInput = GetVertexPositionInputs(v.positionOS.xyz);
					//o.vertex = vertexInput.positionCS;

					return o;
				}

				float4 frag(v2f i) : SV_Target
				{
					UNITY_SETUP_INSTANCE_ID(i);
					UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

					//float2 uv_MainTex = i.uv_texcoord * _MainTex_ST.xy + _MainTex_ST.zw;

					float4 col = (i.vertexColor * tex2D(_MainTex, i.uv.xy) * i.uv.w);
					col.a = i.vertexColor.a;
					float2 uv_RongjieTex = i.uv2 * _RongjieTex_ST.xy + _RongjieTex_ST.zw;
					clip((tex2D(_RongjieTex, uv_RongjieTex).r + (-1.0 + (i.uv.z - 0.0) * (1.0 - -1.0) / (1.0 - 0.0))) - _Cutoff);
					return col;
				}
				ENDCG
			}
			
			Pass
			{
				Name "ShadowCaster"
				Tags{ "LightMode" = "ShadowCaster" }
				ZWrite On
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#pragma target 3.0
				#pragma multi_compile_shadowcaster
				#pragma multi_compile UNITY_PASS_SHADOWCASTER
				#pragma skip_variants FOG_LINEAR FOG_EXP FOG_EXP2
				#include "HLSLSupport.cginc"
				#if ( SHADER_API_D3D11 || SHADER_API_GLCORE || SHADER_API_GLES || SHADER_API_GLES3 || SHADER_API_METAL || SHADER_API_VULKAN )
					#define CAN_SKIP_VPOS
				#endif
				#include "UnityCG.cginc"
				#include "Lighting.cginc"
				#include "UnityPBSLighting.cginc"
				sampler3D _DitherMaskLOD;

				struct v2f
				{
					V2F_SHADOW_CASTER;
					float4 customPack1 : TEXCOORD1;
					float2 customPack2 : TEXCOORD2;
					float3 worldPos : TEXCOORD3;
					half4 color : COLOR0;
					UNITY_VERTEX_INPUT_INSTANCE_ID
				};
				v2f vert(appdata_full v)
				{
					v2f o;
					UNITY_SETUP_INSTANCE_ID(v);
					UNITY_INITIALIZE_OUTPUT(v2f, o);
					UNITY_TRANSFER_INSTANCE_ID(v, o);
					Input customInputData;
					float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
					half3 worldNormal = UnityObjectToWorldNormal(v.normal);
					o.customPack1.xyzw = customInputData.uv_tex4coord;
					o.customPack1.xyzw = v.texcoord;
					o.customPack2.xy = customInputData.uv_texcoord;
					o.customPack2.xy = v.texcoord;
					o.worldPos = worldPos;
					TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
					o.color = v.color;
					return o;
				}
				half4 frag(v2f IN
				#if !defined( CAN_SKIP_VPOS )
				, UNITY_VPOS_TYPE vpos : VPOS
				#endif
				) : SV_Target
				{
					UNITY_SETUP_INSTANCE_ID(IN);
					Input surfIN;
					UNITY_INITIALIZE_OUTPUT(Input, surfIN);
					surfIN.uv_tex4coord = IN.customPack1.xyzw;
					surfIN.uv_texcoord = IN.customPack2.xy;
					float3 worldPos = IN.worldPos;
					half3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
					surfIN.vertexColor = IN.color;
					SurfaceOutput o;
					UNITY_INITIALIZE_OUTPUT(SurfaceOutput, o)
					surf(surfIN, o);
					#if defined( CAN_SKIP_VPOS )
					float2 vpos = IN.pos;
					#endif
					half alphaRef = tex3D(_DitherMaskLOD, float3(vpos.xy * 0.25, o.Alpha * 0.9375)).a;
					clip(alphaRef - 0.01);
					SHADOW_CASTER_FRAGMENT(IN)
				}
				ENDCG
			}
	}
	Fallback "Diffuse"
}