// Toon卡通渲染
uniform half _DynHLight;
uniform float _HighLightStrength;
uniform float _HLightLength;
uniform float _HLightFeather;
uniform float _AttenS;
uniform float _MetalStrength;
uniform half _spec_bloom;
uniform float _MainAlpha;
uniform float _envir_brightness;
#include "HLSLToon_Common.hlsl"
half StylizedSpecular(half specularTerm, half specSmoothness)
{
	return smoothstep(specSmoothness*0.5, 0.5 + specSmoothness*0.5, specularTerm);
}
half4 CustDirectBDRF(BRDFData brdfData, half3 normalWS, half3 lightDirectionWS, half3 viewDirectionWS,half3 SpecMask,half atten)
{
    float3 halfDir = SafeNormalize(float3(lightDirectionWS) + float3(viewDirectionWS));

    float NoH = saturate(dot(normalWS, halfDir));
    half LoH = saturate(dot(lightDirectionWS, halfDir));
    float d = NoH * NoH * brdfData.roughness2MinusOne + 1.00001f;

    half LoH2 = LoH * LoH;
    half specularTerm = brdfData.roughness2 / ((d * d) * max(0.1h, LoH2) * brdfData.normalizationTerm);
#if defined (SHADER_API_MOBILE) || defined (SHADER_API_SWITCH)
    specularTerm = specularTerm - HALF_MIN;
    specularTerm = clamp(specularTerm, 0.0, 100.0);
#endif
	half r = brdfData.roughness2*0.85 + (1e-4h);
	specularTerm = lerp(specularTerm,StylizedSpecular(specularTerm,SpecMask.y*0.96+0.04)*(1/r),step(0.04,SpecMask.y));
	half3 brdf = specularTerm * brdfData.specular * (lerp(_MetalStrength,_SpecStrength,step(1,SpecMask.z))+5*SpecMask.x);
    half3 color = brdf*atten + brdfData.diffuse;
    return half4(color,dot(brdf, half3(0.3, 0.59, 0.11)));
}
half3 CustGlobalIllumination(BRDFData brdfData, half3 normalWS, half3 viewDirectionWS, half RefIntensity)
{
    half3 reflectVector = reflect(-viewDirectionWS, normalWS);
    half fresnelTerm = Pow4(1.0 - saturate(dot(normalWS, viewDirectionWS)));
    half3 indirectSpecular = GlossyEnvironmentReflection(reflectVector, 1.0, RefIntensity);
    return EnvironmentBRDF(brdfData, half3(0.0,0.0,0.0), indirectSpecular, fresnelTerm);
 }
half3 HairHighLight(float3 N,float3 H,float lightMask)
{
	float3 NV = mul(UNITY_MATRIX_V, N);
	float3 HV = mul(UNITY_MATRIX_V, H);
	float NdotH = dot(normalize(NV.xz), normalize(HV.xz));
	NdotH = pow(NdotH, 6) * _HighLight;
	NdotH = pow(NdotH, 1 / _HLightLength);
	float lightFeather = _HLightFeather * NdotH;
	float lightStepMax = saturate(1 - NdotH + lightFeather);
	float lightStepMin = saturate(1 - NdotH - lightFeather);
	half3 HColor = smoothstep(lightStepMin, lightStepMax, clamp(lightMask, 0, 0.99))*_HighLightColor.rgb;
	return HColor;
}
half4 BasicFrag(v2f_basic i) : COLOR
{	
	Light light = MainLight(i.shadowCoord);
	CharDot chardot;
	Chardot(light,i.tangentTransform,i.worldNormal,i.worldPos,i.uv,chardot);
	//Color
	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 v = (_RampNum+1-mask.r*20)/_RampNum-1/_RampNum*0.5;
	//高光
	half3 SpecMask = RampCol(_RampTex,1, v);
	SpecMask = RGBToHSV(SpecMask);

	BRDFData brdfData;
	InitializeBRDFData(final, 1-SpecMask.z, half3(0.0h, 0.0h, 0.0h), _Shininess*lightMask.g, 1, brdfData);
	float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
	half4 PBRResult = CustDirectBDRF(brdfData, i.worldNormal, light.direction, viewDir,SpecMask,lightAtten*(1-step(lightMask.g,0)));
	half3 diffusecolor = CustGlobalIllumination(brdfData, i.worldNormal, viewDir,(1-SpecMask.z)*_envir_brightness);
	final = PBRResult.rgb+diffusecolor;
	half SpecBloom=PBRResult.a;
	// 高亮
	half3 HighLightColor = _HighLight * lightMask.b * lightAtten *_HighLightColor.rgb*texCol.rgb;
	#ifdef Receive_Shadows
	light.shadowAttenuation = smoothstep(0.0h, _shadowFalloff, chardot.ndhL*light.shadowAttenuation);
	#endif
	final += lerp(HighLightColor,HairHighLight(i.worldNormal,chardot.worldHalfDir,lightMask.b),_DynHLight)*light.shadowAttenuation;
	//光照衰减强度
	RampResult(chardot,light,final,lightMask.rgb,mask,i.uv,v,i.worldPos);
	//Backrim
	//half3 RimColor = RampCol(_RampTex,0.95, v);
	final += StylizedFresnel(light, chardot.ndhV, i.worldNormal, _BackRimMin,_BackRimMax);
	//FXrim
	half w = fwidth(chardot.ndhV)*2.0;
	half rimA;
	Rim(final,i.uv,1 - chardot.ndhV,w,rimA);
	// 染色
	final = Coloration(final,lightMask.a);
	//自发光
	_IlluminStrength = abs((( (sin((_Time.y*_TimeScale)) + 1.0)/1.0) / 2.0))* _IlluminStrength*_Illumin_On;
	final += final* mask.a * _IlluminStrength * _IlluminColor;
	#ifdef DISSOLVE 
		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(texCol.a-_Cutoff);
	#endif
	final.rgb *= saturate(_MainLightColor.w);
    final.rgb = i.fogCoord.w == 0 ? final.rgb : VolumetricFog_GetFog(final.rgb, i.fogCoord);
    return half4(final, lerp(1, saturate(texCol.a*_MainAlpha+rimA+mask.a*_Illumin_On*(1-step(_RimWidth,0))) + _RT_Reflection, _AlphaClip + _RT_Reflection));

}