#include "../DeathScreen.hls"
#include "../MipLevels.hls"

// Diffuse map
sampler DIFFUSE_MAP :           register (s0);

// Funky emissive map which contains diffuse/emissive slider and team color mask
sampler EMISSIVE_MAP :          register (s1);

// Shadow map
sampler SHADOW_MAP  :           register (s5);

// Fog of war
sampler FOW_MAP     : register (s6);

sampler FOW_OVERLAY_MAP     : register(s10);


#ifdef REFLECTIVE
    samplerCUBE REFLECTION_MAP : register (s2);
    sampler GLOSS_MAP          : register (s11);
#endif

// Highlight the character when selected
float4 kGrassFade : register(c23);

// Ambient color
float4  AMBIENT_COLOR  :        register(c21);

// Self illumination value
float3 SELF_ILLUMINATION : register(c24);

#include "../ShadowMap/pcf1.hls"

///////////////////////////////////////////////////////////////////////////////////////
///
///
///
///////////////////////////////////////////////////////////////////////////////////////
float4
    main
    (
        float2  uv1 :           TEXCOORD0, 
        float4  in_texcoord4 :  TEXCOORD1,
#ifndef DISABLE_SHADOWS
        float3  uvSM :          TEXCOORD2, 
#endif
#ifdef  SEPARATE_ALPHA_UV
        float2  alphaUV:        TEXCOORD3,
#endif
#ifdef REFLECTIVE
        float4  reflectRay  : TEXCOORD4,
        float3  normal  : TEXCOORD5,
            float3  reflTint: TEXCOORD6,
#endif

        float4  sunColor :      COLOR0,
        float4  envColor :      COLOR1
    ) : COLOR0
{
    // Incident sun light
    float4 incidentSunLight = sunColor;

#ifndef DISABLE_SHADOWS
    // Modulate the incident sun light by the shadow
    float shadowMask = saturate(SampleShadowMap(SHADOW_MAP, uvSM)+0.4f);
    incidentSunLight *= shadowMask;
#endif

    // Sum the incident sun light with contributions from environmental and ambient
    float4 incidentLight = incidentSunLight + envColor * 4 +AMBIENT_COLOR;

    // Read emissive texture which contains emissive/diffuse factor
    float4 texelEmissive = tex2D(EMISSIVE_MAP, uv1.xy);
    float emissiveFactor = texelEmissive.g;                 /// Amount of color which is emissive
    float diffuseFactor = saturate(1.f - emissiveFactor);   /// Amount of color which is diffuse

    // Read the diffuse texture
    float4 texelDiffuse = tex2D(DIFFUSE_MAP, uv1.xy);

#ifdef SEPARATE_ALPHA_UV
    texelDiffuse.a = tex2D(DIFFUSE_MAP, alphaUV).a;
#endif

    // Compute albedo color using a combination of diffuse and emissive values
    float4 albedoColor = texelDiffuse * saturate(diffuseFactor * incidentLight + emissiveFactor + float4(SELF_ILLUMINATION, 0.f));

    // Calculate final color
    float4 finalColor;
    finalColor.rgb = saturate(albedoColor).rgb;
    finalColor.a   = texelDiffuse.a * kGrassFade.a;  // apply any global alpha factor (e.g., the fading associated with being in the grass)


#ifdef REFLECTIVE
    float3 gloss = tex2D(GLOSS_MAP, uv1.xy).rgb;  // sample per pixel tinting and attenuation of reflections
    
    //This reflection vector is used to look up the Reflection color in the cube map.
    //This color is masked and tinted by the gloss map and also the opacity stored in eyeRay.w (computed in the vertex shader).
    float3 reflectionColor = gloss * texCUBE(REFLECTION_MAP, reflectRay.xyz).rgb * reflectRay.w;
    
    // The reflection color is modified by the RGB Reflection Tint (computed in the vertex shader)
    // and added to the pixel color (finalColor).
    finalColor.rgb +=reflectionColor * reflTint;
    
    // TODO
    // Why doesn't this shader variant have support for the 'fresnel rolling' effect?
    // This makes characters like Zac look noticeably different.
    // This variant is currently used for quality settings above 'low' when MRT is not available, or outline 'inking' is disabled.
    // If added, it should go here
#endif


#ifndef DISABLE_FOW
    // Apply FOW to final result
    float4 fowOverlay = tex2D(FOW_OVERLAY_MAP, in_texcoord4.xy);
    float texelFOW = tex2D(FOW_MAP, in_texcoord4.xy).r;
    texelFOW = fowOverlay.a+texelFOW*(1.0f-fowOverlay.a);
    finalColor.rgb = lerp(fowOverlay.rgb, finalColor.rgb, texelFOW);
#endif

    finalColor.rgb = ApplyMipColorsVisualization(finalColor.rgb, uv1);

    // Return final color
    return finalColor;
}
