#ifndef X2M_LWRP_PARTICLE_UNLIT_INCLUDED
#define X2M_LWRP_PARTICLE_UNLIT_INCLUDED


#include "Particle-Common.hlsl"

X2M_DECLARE_TEX2D(_MainTex);
X2M_DECLARE_TEX2D_DEPTH;

CBUFFER_START(ParticlePerMaterial)
	half4  _TintColor;
	float4 _MainTex_ST;
	float  _GlobalOpaque;
	float  _InvFade;
#if defined(_X2M_PARTICLE_UI_CLIP_RECT_ON)
	float4 _ClipRect; // ui clip rect(RectMask2D)
#endif

CBUFFER_END

struct ParticleVertexInput
{
	float4 vertex : POSITION;
	half4  color  : COLOR;
#if defined(_X2M_PARTICLE_RIM_ON) || defined(_X2M_PARTICLE_FLOW_ON)
	float3 normal : NORMAL;
#endif
	X2MParticleUV texcoord : TEXCOORD0;

	UNITY_VERTEX_INPUT_INSTANCE_ID
};

#if defined(_X2M_PARTICLE_FLOW_ON)
	#define _VERTEX_OUTPUT_WORLD_POSITION
#endif

#if defined(_X2M_PARTICLE_UI_CLIP_RECT_ON)
	#define _VERTEX_OUTPUT_BODY_POSITION
#endif

struct ParticleVertexOutput
{
	float4 vertex : SV_POSITION;
	half4  color : COLOR;
	float2 texcoord : TEXCOORD0;

#ifdef _X2M_PARTICLE_SOFTPARTICLES_ON
	float4 projPos : TEXCOORD2;
#endif

#if defined(_X2M_PARTICLE_RIM_ON) || defined(_X2M_PARTICLE_FLOW_ON)
	float3 normalWS : TEXCOORD3;
	float3 viewDirWS: TEXCOORD4;
#endif
#if defined(_VERTEX_OUTPUT_WORLD_POSITION)
	float3 positionWS: TEXCOORD5;
#endif
#if defined(_VERTEX_OUTPUT_BODY_POSITION)
	float3 positionOS: TEXCOORD6;
#endif

	X2M_PARTICLE_MASK_UV(7)
	X2M_PARTICLE_DISSOLVE_UV(8)

	UNITY_VERTEX_INPUT_INSTANCE_ID
	UNITY_VERTEX_OUTPUT_STEREO
};

ParticleVertexOutput ParticleUnlitVS(ParticleVertexInput input)
{
	ParticleVertexOutput output;

	UNITY_SETUP_INSTANCE_ID(input);
	UNITY_TRANSFER_INSTANCE_ID(input, output);
	UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

#if defined(_VERTEX_OUTPUT_BODY_POSITION)
	output.positionOS = input.vertex.xyz;
#endif

	VertexPositionInputs positionInput = GetVertexPositionInputs(input.vertex.xyz);

	output.vertex = positionInput.positionCS;
#ifdef _X2M_PARTICLE_SOFTPARTICLES_ON
	output.projPos = ComputeScreenPos(positionInput.positionCS);
	//COMPUTE_EYEDEPTH(output.projPos.z);
#endif
	output.color = input.color;

#if defined(_X2M_PARTICLE_RIM_ON) || defined(_X2M_PARTICLE_FLOW_ON)
	VertexNormalInputs normalInput = GetVertexNormalInputs(input.normal);
	output.normalWS  = normalInput.normalWS;
	output.viewDirWS = normalize(positionInput.positionWS - GetCameraPositionWS());
#if !defined(SHADER_HINT_NICE_QUALITY)
	output.viewDirWS = SafeNormalize(output.viewDirWS);
#endif
#endif

#if defined(_VERTEX_OUTPUT_WORLD_POSITION)
	output.positionWS = positionInput.positionWS;
#endif

//#ifdef _X2M_PARTICLE_DISTANCE_FADE_ON
//	output.color.a *= CalcFadeFactor(output.vertex.w);
//#endif
	output.texcoord = TRANSFORM_TEX(input.texcoord, _MainTex);

	X2M_PARTICLE_SCROLL_ROTATE_UV(output.texcoord);
	X2M_PARTICLE_MASK_TRANSFER_UV(input.texcoord, output);
	X2M_PARTICLE_DISSOLVE_TRANSFER_UV(output, input.texcoord);
	return output;
}
half4 ParticleUnlitPS(ParticleVertexOutput input) : SV_Target
{
	UNITY_SETUP_INSTANCE_ID(input);
	UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);

#ifdef _X2M_PARTICLE_SOFTPARTICLES_ON
	float sceneZ = X2M_LINEAR_EYE_DEPTH_FROM_DEPTH_TEXTURE(input.projPos);
	float thisZ  = X2M_LINEAR_EYE_DEPTH(input.projPos);
    //float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(CAMERA_DEPTH_TEXTURE, UNITY_PROJ_COORD(input.projPos)));
    //float thisZ  = input.projPos.z;// eye depth
    float fade = saturate(_InvFade * (sceneZ - thisZ));

	input.color.a *= fade;
#endif
#if defined(_X2M_PARTICLE_MUTIPLY2_ON)
	half4 col;
	half4 tex = X2M_SAMPLE_TEX2D(_MainTex, input.texcoord);
	col.rgb = tex.rgb * input.color.rgb * 2;
	col.a = input.color.a * tex.a;
	col = lerp(half4(0.5f, 0.5f, 0.5f, 0.5f), col, col.a);
#else
	half4 vColor = input.color;// +input.color;//unity 自带shader中的方法，提升了颜色的饱和度
	half4 tColor = X2M_PARTICLE_UVANIMATED_COLOR(_MainTex,input.texcoord.xy);
	half4 col = vColor * _TintColor * tColor;
	//float4 col = input.color * _TintColor *  X2M_PARTICLE_UVANIMATED_COLOR(_MainTex input.texcoord.xy);
#endif
	X2M_PARTICLE_RIM_APPLY(col, input);
	X2M_PARTICLE_VFLOW_RIM_NOALPHA_APPLY(col, input);
	X2M_PARTICLE_MASK_APPLY(col, input);

#if defined(_X2M_PARTICLE_ALPHA_AS_MASK_ON)
	col.rgba *=  tColor.aaaa;
#endif

	X2M_PARTICLE_DISSOLVE_APPLY(col, input);
	col.a = saturate(col.a * _GlobalOpaque);//避免alpha blend后出现负数的情况

#if defined(_X2M_PARTICLE_UI_CLIP_RECT_ON)
	col.a *= Get2DClipping(input.positionOS.xy, _ClipRect);
#endif

    return KillNAN(col);
}

#endif //X2M_LWRP_PARTICLE_UNLIT_INCLUDED