// Toon卡通渲染
uniform float _Alpha_Change;
uniform float _HighLightStrength;
uniform float _ShadowThreshold;
uniform float _MainAlpha;
uniform float2 _ColorationMask;
#include "HLSLToon_Common.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
half4 BasicFrag(v2f_basic i) : COLOR
{	
    UNITY_SETUP_INSTANCE_ID(i);
	Light light = MainLight(i.shadowCoord);
	CharDot chardot;
	Chardot(light,i.tangentTransform,i.worldNormal,i.worldPos,i.uv,chardot);
	float w = fwidth(chardot.ndhV)*2.0;

	half4 texCol = tex2D(_MainTex, i.uv);
	#ifdef _Change
	texCol = ChangeTex(texCol,_ChangeTex,i.uv,i.uv,i.worldPos.y,_EdgeColor);
	#endif
	half3 final = texCol.rgb;
	half4 lightMask = tex2D(_LightMaskTex, i.uv);
	lightMask = lerp(lightMask,half4(0.5,0,0,0),_LightMask_Off);
	half4 mask = tex2D(_MaskTex, i.uv);
	half lightAtten = Atten(chardot.ndh_Lut,lightMask.r);

	half v4 = 0.625 + mask.r * 0.25 - 0.5 * mask.b;
	half v10 = 0.45 + mask.r * 0.1 + mask.g * 0.4 - 0.2 * mask.b;
	half v20 = (_RampNum+1-mask.r*20)/_RampNum-1/_RampNum*0.5;
	half v = lerp(v10,v4,_Ramp_Num4);
	v = lerp(v,v20,_USE_GrayMask);
	//v = lerp(v,(_RampNum+1-mask.r*20)/_RampNum-1/_RampNum*0.5,_USE_GrayMask);

	//高光
	half3 SpecCol = RampCol(_RampTex,1, v);
	//half3 Smooth = RampCol(_RampTex,0.95, v);
	//float range = dot(Smooth.rgb, float3(0.299, 0.587, 0.114));
	float spec = pow(max(0, chardot.ndhV) ,_Shininess * 128.0)*2.0;
	spec = smoothstep(0.5 - _SpecSmooth * 0.5, 0.5 + _SpecSmooth * 0.5, spec);
	final.rgb += spec*_SpecColor.rgb * lightMask.g*SpecCol*_SpecStrength;

	// 高亮，把 _HighLight lightMask.b 易出现为0值放置前面
	half3 specColor = _HighLight * lightMask.b *lightMask.b *
		lightAtten * lightAtten * lightAtten *
		_HighLightStrength * _HighLightColor.rgb * 0.6;
	final += specColor*SpecCol*light.shadowAttenuation;

	//光照衰减强度
	RampResult(chardot,light,final,lightMask.rgb,mask,i.uv,v,i.worldPos);

	//Backrim
	// float BackRimStep = smoothstep(-w+1-_BackRimWidth, 1, 1-ndhV);
	// float diffStep = smoothstep(-w+_ShadowThreshold, w+_ShadowThreshold, -ndhL);
	// half3 Backrim = BackRimStep * 0.5 * _BackRimColor*(1-step(_BackRimWidth,0))*diffStep;
	// final.rgb += lerp(0,Backrim,step(_RimWidth,0));
	//half3 RimColor = RampCol(_RampTex,0.95, v);
	final += StylizedFresnel(light, chardot.ndhV, i.worldNormal, _BackRimMin,_BackRimMax);
	//FXrim
	half rimA;
	Rim(final,i.uv,1-chardot.ndhV,w,rimA);
	// 染色
	half Channel = lerp(texCol.a,lightMask.a,_Alpha_Change);

	final = Coloration(final,Channel);
	//自发光
	half Illumin_On = GIANT_ACCESS_INSTANCED_PROP(_Illumin_On);
	half TimeScale = GIANT_ACCESS_INSTANCED_PROP(_TimeScale);
	half Strength = GIANT_ACCESS_INSTANCED_PROP(_IlluminStrength);
	half3 IlluminColor = GIANT_ACCESS_INSTANCED_PROP(_IlluminColor);
	Strength = abs((( (sin((_Time.y*TimeScale)) + 1.0)/1.0) / 2.0))* Strength*Illumin_On;
	final += final * mask.a * Strength * IlluminColor;

	half alpha = lerp(lightMask.a,texCol.a,_Alpha_Change);
	 
	#if defined(DISSOLVE)  || defined(_GIANT_BAKED_ANIMATION_ON)
	 	float4 color = GIANT_ACCESS_INSTANCED_PROP( _Color);
	 	half4 dissolveFactor = GIANT_ACCESS_INSTANCED_PROP( _DissolveFactor);
	 	half3 dissColor = GIANT_ACCESS_INSTANCED_PROP( _DissColor);
		
		half dissolveDegree = tex2D(_DissolveTex, i.uv*dissolveFactor.zz).r;
		i.uv2.y = pow(i.uv2.y,dissolveFactor.x);
		dissolveDegree = dissolveFactor.w?dissolveDegree:lerp(dissolveDegree,1,i.uv2.y)*i.uv2.y;
		half dissMask = lerp(dissolveDegree - dissolveFactor.x,1,step(dissolveFactor.x,0));
		final *= lerp(color,(dissolveFactor.y / dissMask)*dissColor,dissolveFactor.y*(1-step(dissolveFactor.x,0)));
		clip(dissolveDegree-dissolveFactor.x);
	#endif
	#if defined(_ALPHATEST_ON) && !defined(DISSOLVE)
		clip(alpha-_Cutoff);
	#endif
	final.rgb *= saturate(_MainLightColor.w);
    final.rgb = i.fogCoord.w == 0 ? final.rgb : VolumetricFog_GetFog(final.rgb, i.fogCoord);

	float RimWidth = GIANT_ACCESS_INSTANCED_PROP( _RimWidth);
    return  half4(final, lerp(1, saturate(texCol.a*_MainAlpha+rimA+mask.a*Illumin_On*(1-step(RimWidth,0))) + _RT_Reflection, _AlphaClip + _RT_Reflection));
   
    
}