#ifndef HLSLTOON_COMMON
#define HLSLTOON_COMMON
#include "HLSLToon_Input.hlsl"
#include "../../Common/Function/URP_Function.hlsl"
uniform half _USE_GrayMask,_Ramp_Num4,_FACEDIR;
half3 RampCol(sampler2D Ramp,half UvX,half UvY)
{
	half2 RampUV = half2(UvX, UvY);
	half3 Col = tex2D(Ramp, RampUV);
	return Col;
}
Light MainLight(float4 shadowCoord)
{
	Light light;
	#ifdef Receive_Shadows
	light = GetMainLight(shadowCoord);
	#else
	light = GetMainLight();
	#endif
	return light;
}
struct CharDot
{
    float3 worldHalfDir;
    float ndhH;
    float ndhL;
    float ndh_Lut;
    float VdotH;
    float ndhV;
    float ViewZ;
};
void Chardot(Light light,float3x3 tangent,inout half3 worldNormal,float3 WPos,float2 uv, out CharDot chardot)
{
#ifdef _NORMALMAP
	half4 Normal = tex2D(_BumpMap,uv);
	half3 tangentNormal = UnpackNormal(Normal);
	worldNormal = normalize(mul( tangentNormal.rgb, tangent));
#else
	worldNormal = TransformObjectToWorldNormal(worldNormal);
#endif
	float3 lightDir = light.direction;
	float perDistanceAttenuation = light.distanceAttenuation;
	//#ifdef USE_MAIN_LIGHT_DIR
	#ifdef _ADDITIONAL_LIGHTS
		uint pixelLightCount = GetAdditionalLightsCount();
		for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex)
		{
			Light light = GetAdditionalLight(lightIndex, WPos);
			lightDir = lerp(light.direction, lightDir, step(light.distanceAttenuation, perDistanceAttenuation));
			perDistanceAttenuation = max(light.distanceAttenuation, perDistanceAttenuation);
		}
	#endif
	//#endif
	worldNormal.y = lerp(worldNormal.y,0,_FACEDIR);
	float3 worldViewDir = normalize(_WorldSpaceCameraPos.xyz - WPos.xyz);
	float3 worldLightDir = normalize(lightDir*perDistanceAttenuation);
	chardot.worldHalfDir = normalize(worldViewDir+worldLightDir);
	chardot.ndhH = dot(worldNormal, chardot.worldHalfDir);
	chardot.ndhL = dot(worldNormal, worldLightDir);
	half ndhH = dot(worldNormal, normalize(worldViewDir+worldLightDir));
	chardot.ndh_Lut = lerp(chardot.ndhL,ndhH,_Billboard*light.shadowAttenuation);
	//chardot.ndh_Lut = pow(chardot.ndh_Lut,1/_Gamma);
	chardot.ndhV = saturate(dot(worldNormal, worldViewDir));
	chardot.VdotH = saturate(dot(worldViewDir, chardot.worldHalfDir));
	chardot.ViewZ = abs(worldViewDir.z);
}
half Atten(float NL,float3 lightMask)
{
	half lightAtten = saturate(lerp(NL, 1, 0.5 * _NewRamp));
	lightAtten = lerp(lightMask.r * lightAtten, lightAtten, _LightMaskParam+0.3);
	return lerp(lightAtten,0.9,step(0.9,lightAtten));
}
void RampResult(CharDot chardot,Light light,inout float3 final,float3 lightMask,float3 mask,float2 uv,float RampV,half3 WPos)
{
	half lightAtten = Atten(chardot.ndh_Lut,lightMask.r);
	half2 rampUV = half2(lightAtten, RampV);
	// 采样Ramp贴图
	half4 ramp = tex2D(_RampTex, rampUV);
	half3 ShadowCol = 1;
	#ifdef Receive_Shadows
	light.shadowAttenuation = smoothstep(0.0h, _shadowFalloff, chardot.ndhL*light.shadowAttenuation);
	ShadowCol = RampCol(_RampTex,0, RampV);
	#endif
	//云影掠过效果
	half4 posW = VertCalaShadow(float4(WPos,0), _DirX, _DirY);
	light.shadowAttenuation =FragCalaShadowAtten(light.shadowAttenuation, posW, _Time.x, _Speed, _ShadowAlphaRatio, _ShadowTex);
	#ifdef _Change
	ramp = ChangeTex(ramp,_ChangeRamp,rampUV,uv,WPos.y,0);
	#endif
	// 取出纯白色值
	half rampWhiteColor = step(3, ramp.r + ramp.g + ramp.b);
	ramp.rgb = lerp(ramp.rgb * _RampColor, ramp.rgb, rampWhiteColor);
	ramp.rgb = lerp(ramp.rgb,ShadowCol,1 - light.shadowAttenuation);
	//#ifdef _USE_Ramp_Num4
	half3 ramp4 = ramp.rgb - (1.0 - ramp.rgb) * (1.0 - mask.g);
	ramp.rgb = lerp(ramp.rgb,ramp4,_Ramp_Num4*(1-_USE_GrayMask));
	//#endif
	// half isRamp = step(chardot.ndhV, 0.85 / sqrt(sqrt(sqrt(chardot.ViewZ))));
	// isRamp = 1 - (step(isRamp, 0) * step(1, lightMask.b));
	// final *= lerp(1, ramp.rgb, isRamp);
	final *= ramp.rgb;
	final += (ramp.rgb - 0.75) * _BrightnessRatio;
	final *= (1 + _BrightnessIncrease * 0.02);
	final *= 1-lerp(0, (1 - lightMask.rrr-final.rgb) * _LightMaskBackParam, max(0, -chardot.ndhL));
	final *= lerp(1,light.color,  _BrightnessFactor);
}
void Rim(inout half3 col,float2 uv,float nv,half Width,out half rimStep)
{
	float RimWidth = GIANT_ACCESS_INSTANCED_PROP( _RimWidth);
	float3 RimColor = GIANT_ACCESS_INSTANCED_PROP( _RimColor);	

	rimStep = smoothstep(-Width+1-RimWidth, 1, nv);
	rimStep = rimStep * 0.5*(1-step(RimWidth,0));
	half3 rim = rimStep * RimColor;
	#ifdef FLASH_SWITCH
	half4 FlashFactor = GIANT_ACCESS_INSTANCED_PROP( _FlashFactor);
	half FlashStrength = GIANT_ACCESS_INSTANCED_PROP( _FlashStrength);
	half2 flashuv = uv * FlashFactor.zw + FlashFactor.xy * _Time.y;
	half3 flash = tex2D(_FlashTex, flashuv);
	col += lerp(0,flash.r * FlashStrength,rim);
	#else
	col += rim;
	#endif
}
uniform float _BackRimMax;
uniform float _BackRimMin;
half3 StylizedFresnel(Light light , half nv, half3 normal, half rimMin, half rimMax)
{
	//float3 BackRimColor = GIANT_ACCESS_INSTANCED_PROP( _BackRimColor);
	half rim = 1-nv;
	rim = smoothstep(rimMin, rimMax, rim);
	return rim * saturate(dot(normal, light.direction))*_BackRimColor*light.shadowAttenuation;
}

half3 Coloration(half3 Col, half Mask)
{
	Mask *= _IsColoration;

	// Android图片压缩导致alpha不会精确等于1 (导致安卓上出现颜色方块),这里的阈值改为0.8
	half factor = step(0.8, Mask) * _WhiteChannel;

	half hueshift = lerp(_HueShiftG, _HueShift, factor);
	half sat = lerp(_SatG, _Sat, factor);
	half val = lerp(_ValG, _Val, factor);
	half col = lerp(_ColorationG, _Coloration, factor);

	return replace_color_with_hsv(Col, Mask, hueshift, sat, val, col);
}

#endif

