#include "CharacterProperties.hlsl"
#include "../../Common/Template Shader/Shadow.hlsl"
#include "../../Common/Function/URP_Function.hlsl"
#include "CharacterFunction.hlsl"
struct appdata {
	float4 vertex : POSITION;
	float3 normal : NORMAL;
	half4 color : COLOR;
	float2 uv : TEXCOORD0;
	float2 uv2 : TEXCOORD1;
	float2 uv3 : TEXCOORD2;
	float4 tangent : TANGENT;
};

struct v2f_basic
{
	float4 pos:SV_POSITION;
	half4 color:COLOR;
	half2 uv : TEXCOORD0;
	half2 uv2 : TEXCOORD6;
	float3 worldPos : TEXCOORD1;
	float3 worldNormal : TEXCOORD2;
	float4 shadowCoord : TEXCOORD3;
	float3 normalWS : TEXCOORD4;
    float4 fogCoord : TEXCOORD5;
	float3x3 tangentTransform:TEXCOORD7;
};
v2f_basic BasicVert(appdata v)
{
	v2f_basic o;
	VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
	o.pos = vertexInput.positionCS;
	o.worldPos = vertexInput.positionWS;

	o.uv = v.uv;
	o.uv2 = v.uv2;
	o.color = v.color;
	VertexNormalInputs normalInput = GetVertexNormalInputs(v.normal, v.tangent);
	o.normalWS = normalInput.normalWS;
	o.tangentTransform = float3x3(normalInput.tangentWS, normalInput.bitangentWS, normalInput.normalWS);	
 	o.worldNormal = normalize(v.normal);
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
	o.shadowCoord = GetShadowCoord(vertexInput);
#endif
    o.fogCoord = VolumetricFog_GetFogCoord(vertexInput.positionNDC, vertexInput.positionVS.z);
	
	return o;
}
half4 BasicFrag(v2f_basic i) : COLOR
{	
	Light light = MainLight(i.worldPos);
	CharDot chardot;
	Chardot(light,i.tangentTransform,i.worldNormal,i.worldPos,i.uv,chardot);
	//Color
	half4 texCol = tex2D(_MainTex, i.uv);
	half3 final = texCol.rgb;
#ifdef DISSOLVE 
	half dissolveDegree = tex2D(_DissolveTex, i.uv*_DissolveFactor.zz).r;
	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
	half4 lightMask = tex2D(_LightMaskTex, i.uv);
	lightMask.r = lerp(lightMask.r,1,_LightMask_Off);
	//Ramp
	half RampId = pow(lightMask.a, 1/2.2);
	half v = (_RampNum+1-RampId*20)/_RampNum-1/_RampNum*0.5;
	//云影掠过效果
	half4 posW = VertCalaShadow(float4(i.worldPos,0), _DirX, _DirY);
	light.shadowAttenuation =FragCalaShadowAtten(light.shadowAttenuation, posW, _Time.x, _Speed, _ShadowAlphaRatio, _ShadowTex);
#ifdef USE_FACE_LIGHT
	RampResultFace(light,light.shadowAttenuation,chardot.ndhL,final,lightMask.rgb,i.uv,v,i.worldPos);
#else
	half lightAtten = Atten(chardot.ndhL,lightMask.r);
#ifdef HAIR_LIGHT
	final += HairHighLight(i.worldNormal,chardot.worldHalfDir,lightMask.b)*light.shadowAttenuation;// hair高亮
#else
	half3 SpecMask = RampCol(_RampTex,1, v);
	SpecMask = RGBToHSV(SpecMask);
	//Pbr
	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);
	half3 diffusecolor = CustGlobalIllumination(brdfData, i.worldNormal, viewDir,(1-SpecMask.z)*_envir_brightness);
	half3 PBRSpec = PBRResult.rgb+diffusecolor;
	//Npr
	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);
	half3 NPRSpec = final + spec*_SpecColor.rgb * lightMask.g*_SpecStrength;
	final = lerp(NPRSpec,PBRSpec,_Specular_Mode);

	final += _HighLight * lightMask.b * lightAtten *_HighLightColor.rgb;// normal高亮
#endif
	//final = lerp(NormalL,HairL,_DynHLight);
	RampResult(light,chardot.ndhL*light.shadowAttenuation,chardot.ndhL,final,lightMask.rgb,i.uv,v,i.worldPos);//光照衰减强度
#endif
	//Backrim
	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 *= _Color.rgb;
	//自发光
#if !defined(_ALPHATEST_ON)
	_IlluminStrength = abs(((sin((_Time.y*_TimeScale)) + 1.0) / 2.0))* _IlluminStrength*_Illumin_On;
	final += texCol.a * _IlluminStrength * _IlluminColor;
	half alpha = 1;
#else
	half alpha = saturate(texCol.a*_MainAlpha+rimA);
#endif
	final.rgb *= saturate(_MainLightColor.w);
    final.rgb = i.fogCoord.w == 0 ? final.rgb : VolumetricFog_GetFog(final.rgb, i.fogCoord);
    return  half4(final, alpha);
}