#include "../ShadowMap/pcf1.hls"

#ifdef USE_TERRAIN_BLEND
    #include "../TerrainBlending/TerrainBlending.hls"
#endif

#include "../DeathScreen.hls"
#include "../MipLevels.hls"
#include "../FogOfWar/FogOfWar_PS.hls"
#include "lit_common.hls"


sampler DIFFUSE_MAP             : register(s0);
sampler SAMPLER_1               : register(s1);
sampler EMISSIVE_MAP            : register(s2);
sampler SAMPLER_3               : register(s3);
sampler SAMPLER_4               : register(s4);
sampler SHADOW_MAP              : register(s5);

sampler TERRAIN_BLEND : register(s4);

#ifdef MOD2X_COLORMAP
    sampler COLORMAP            : register(s8);
#endif
#ifdef FOG_PIXELSHADER_BLEND
    sampler FOGLIGHTING         : register(s9);
#endif

#ifndef VS_LIGHT_INTERPOLATE
    float3   AMBIENT_COLOR      : register(c0);
#endif // VS_LIGHT_INTERPOLATE

// Fog Color.
#ifdef FOG_PIXELSHADER_BLEND
    float4 FOG_COLOR            : register(c28);    // R3D_SHADER_PS_CONSTANT_FOG_COLOR
#endif
#ifdef USE_TERRAIN_BLEND
    float4 TERRAIN_BLEND_LAYER_SCALE : register(c23);
#endif
float4 TERRAIN_BLEND_DATA         : register(c25);
float4 BRUSH_DATA               : register(c26);    //u,v,r2,1/lineWidth


// Functions to calculate appropriat4e Four Blend mix

#ifdef USE_TERRAIN_BLEND
// blend by map texture
float4 FourBlendByTexture( float2 uv0, float2 mapUV )
{   
    float4 weight_scale = tex2D(TERRAIN_BLEND, mapUV) * TERRAIN_BLEND_LAYER_SCALE;
    float4 texel_color0 = tex2D(DIFFUSE_MAP,       uv0);
    float4 texel_color1 = tex2D(SAMPLER_1,         uv0);
    float4 texel_color2 = tex2D(EMISSIVE_MAP,      uv0);
    float4 texel_color3 = tex2D(SAMPLER_3,         uv0);

    float4 texelColor = ApplyTerrainBlend(weight_scale, texel_color0, texel_color1, texel_color2, texel_color3, TERRAIN_BLEND_DATA.a);
    return texelColor;

}
#else
// blend with vertex specified uv into the blending map
float4 FourBlendByVertexUV( float2 uv0, float2 blendUV )
{
    float4 texelColor;
    float4 texel_blend = tex2D(TERRAIN_BLEND, blendUV);
    float4 texel_color0 = tex2D(DIFFUSE_MAP, uv0);
    texelColor.xyz = texel_color0.xyz;

    float4 texel_color1 = tex2D(SAMPLER_1, uv0);
    texelColor.xyz = lerp(texelColor.xyz, texel_color1.xyz, texel_blend.r);

    float4 texel_color2 = tex2D(EMISSIVE_MAP, uv0);
    texelColor.xyz = lerp(texelColor.xyz, texel_color2.xyz, texel_blend.g);

    float4 texel_color3 = tex2D(SAMPLER_3, uv0);
    texelColor.xyz = lerp(texelColor.xyz, texel_color3.xyz, texel_blend.b);

    texelColor.a = 1.f;
    return texelColor;
}
#endif // USE_TERRAIN_BLEND

#if !defined(QUALITY_LOW) || !defined(MOD2X_COLORMAP)
//////////////////////////////////////////////////////////////
// NORMAL version of the shader.
//////////////////////////////////////////////////////////////

struct FRAGMENT
{
    float4 m_Color0 : COLOR0;
};


FRAGMENT main( INTERPOLANTS interpolants )
{
    FRAGMENT fragment;

    // Compute color
    
    // Dynamic incident light
#ifndef MOD2X_COLORMAP
    float3 dynamicIncidentLight = interpolants.m_Color0.rgb;
#else
    float3 dynamicIncidentLight = 1.0f;
#endif

#ifndef DISABLE_SHADOWS
    // Mask dynamic light by shadow texture
    #ifdef NOLIGHTING   //disable shadow fading when using "no lighting" since that produces a more visually pleasing result.
        float shadowMask = saturate(SampleShadowMap(SHADOW_MAP, interpolants.m_TexCoord3.xyz)+0.4f);
    #else
        float shadowMask = saturate(SampleShadowMap(SHADOW_MAP, interpolants.m_TexCoord3.xyz)*interpolants.m_TexCoord3.w+0.4f);
    #endif
    float3 shadowModulate = shadowMask*SHADOW_COLOR_COMPLEMENT + SHADOW_COLOR; // i.e. lerp(SHADOW_COLOR, white, sampledValue
    dynamicIncidentLight *= shadowModulate;
#else
    float3 shadowModulate = 1.0f;
#endif

// extract appropriate uv coordinates for this shader permutation
#if NEED_MAP_UV
    // we need texture coordinates that give us our relative location on the entire map
    #if FOUR_BLEND_USES_VERTEX_UV
        float2 mapUV        = interpolants.mapUV_vertexUV1.xy;
        float2 vertexUV1    = interpolants.mapUV_vertexUV1.zw;
    #else
        float2 mapUV        = interpolants.mapUV;
    #endif
#elif FOUR_BLEND_USES_VERTEX_UV
    float2 vertexUV1        = interpolants.vertexUV1;
#endif

    // Add together baked and dynamic incident light
    float3 lighting = 1.0f;
#ifdef MOD2X_COLORMAP
    #if defined(READ_COLORMAP)
        float4 colorMap = tex2D(COLORMAP, mapUV);
    #endif
    //Lighting comes from the lightmap/colormap or the vertex colors, 
    //depending on shader defines.
    #if defined(FOUR_BLEND) || defined(DECAL)
        lighting = colorMap.rgb;
    #else
        lighting = interpolants.m_Color1.rgb;
    #endif
    //Mod4X lighting and shadowMask.
    lighting *= ( 4.0f*(shadowModulate*0.5f+0.5f) );
#else //!defined(MOD2X_COLORMAP)
    #if defined(VS_LIGHT_INTERPOLATE)
        lighting = dynamicIncidentLight + interpolants.m_Color1.rgb;
    #else
        lighting = dynamicIncidentLight + interpolants.m_Color1.rgb * 4 + AMBIENT_COLOR;
    #endif // VS_LIGHT_INTERPOLATE
#endif //MOD2X_COLORMAP

    // Compute final color
    float3 finalColor;
  
    // UV for the color look-up
    float2 uvColor = interpolants.m_TexCoord0.xy;
    float4 texelColor;
    {
#ifdef FOUR_BLEND
    #ifdef USE_TERRAIN_BLEND
        texelColor = FourBlendByTexture(interpolants.m_TexCoord0.xy, mapUV );
    #else
        texelColor = FourBlendByVertexUV(interpolants.m_TexCoord0.xy, vertexUV1 );
    #endif
#else // FOUR_BLEND
    // no blend, simply read the color texture   
    texelColor = tex2D(DIFFUSE_MAP, uvColor);
#endif // FOUR_BLEND
    }
    
#ifndef DISABLE_EMISSIVE
    // Apply strange emissive texture
    float4 texelEmissive = tex2D(EMISSIVE_MAP, uvColor);    
    finalColor = lighting*texelColor.xyz * (1 - texelEmissive.g) + texelEmissive.g * texelColor.xyz;
#else
    // No emissive, so just simple modulate
    finalColor = lighting*texelColor.xyz;
#endif

#ifdef NOLIGHTING
    finalColor.rgb = texelColor.rgb *  shadowModulate;
#elif defined(FULLBRIGHT)
    finalColor.rgb = texelColor.rgb;
#endif

    finalColor = saturate(finalColor);
    
#if !defined(DISABLE_FOW)
    float fowFadeInFactorByHeight = interpolants.fowUV.w;
    float4 fowFactors = GetFogOfWarFactorsWithFadeTowardFogged(interpolants.fowUV.xy, fowFadeInFactorByHeight);
    finalColor.rgb = lerp(fowFactors.rgb, finalColor.rgb, fowFactors.a);
#endif

    // Output
    fragment.m_Color0.rgb = finalColor;
#ifdef VERTEX_ALPHA
    fragment.m_Color0.a = texelColor.a * interpolants.m_Color0.a;
#else
    fragment.m_Color0.a = texelColor.a;
#endif
    
//Handle the high-end per-pixel fogging.
#if defined(FOG_PIXELSHADER_BLEND) && !defined(FULLBRIGHT) && !defined(NOLIGHTING)
    //modify the fog color based on the fog of war...
    #ifndef FOG_QUALITY_LOW //High quality dual layer colored height fog.
        float4 fogColor = tex2D(FOGLIGHTING, interpolants.fowUV.xy);
        #ifndef DISABLE_FOW
            float4 fogColorTop = lerp(fowFactors, fogColor, fowFactors.a);
        #endif
        float fogOpacity = interpolants.m_Color1.a;
        
        #ifdef MOD2X_COLORMAP
            fogOpacity = saturate( fogOpacity*colorMap.a*2.0f );
        #endif
        
        //lerp towards the pure fog color and fully opaque towards the second layer of the volumetric fog.
        float fogFarLerp = saturate( interpolants.m_TexCoord0.z );
        
        fogOpacity = lerp( fogOpacity, 1.0f, fogFarLerp );
        fogColor   = lerp( fogColorTop, fogColor, fogFarLerp );
        
        //final fog blend.
        fragment.m_Color0.rgb = lerp( fragment.m_Color0.rgb, fogColor.rgb, fogOpacity );
    #else           //Lower quality single layer singe-color height fog.
        //lerp towards the pure fog color and fully opaque towards the second layer of the volumetric fog.
        float fogOpacity = saturate( interpolants.m_TexCoord0.z );

        //final fog blend.
        fragment.m_Color0.rgb = lerp( fragment.m_Color0.rgb, FOG_COLOR.rgb, fogOpacity );
    #endif
#endif

    //Editor / Debugging features.
    #if defined(SHOW_LIGHTING) && defined(MOD2X_COLORMAP)
        fragment.m_Color0.rgb = lighting;
    #elif defined(GRAYSCALE)
        float3 LUMINANCE_CONVERSION = float3(0.299f, 0.587f, 0.114f);
        fragment.m_Color0.rgb = dot( fragment.m_Color0.rgb, LUMINANCE_CONVERSION );
    #endif  

    fragment.m_Color0.rgb = ApplyMipColorsVisualization(fragment.m_Color0.rgb, uvColor);

    return fragment;
}

#else

//////////////////////////////////////////////////////////////
// LOW QUALITY version of the shader, using the Mod2X Colormap.
//////////////////////////////////////////////////////////////

struct FRAGMENT
{
    float4 m_Color0 : COLOR0;
};

FRAGMENT main( in Interpolants_LowestQuality interpolants )
{
    FRAGMENT fragment;


// extract appropriate uv coordinates for this shader permutation
#if NEED_MAP_UV
    // we need texture coordinates that give us our relative location on the entire map
    #if FOUR_BLEND_USES_VERTEX_UV
        float2 mapUV        = interpolants.mapUV_vertexUV1.xy;
        float2 vertexUV1    = interpolants.mapUV_vertexUV1.zw;
    #else
        float2 mapUV        = interpolants.mapUV;
    #endif
#elif FOUR_BLEND_USES_VERTEX_UV
    float2 vertexUV1        = interpolants.vertexUV1;
#endif

    #if defined(READ_COLORMAP)
        float4 colorMap = tex2D(COLORMAP, mapUV);
    #endif

    //Lighting comes from the lightmap/colormap or the vertex colors, 
    //depending on shader defines.
    #if defined(FOUR_BLEND) || defined(DECAL)
        float3 lighting = colorMap.rgb;
    #else
        float3 lighting = interpolants.m_Color0.rgb;
    #endif
    //Mod4X lighting.
    lighting *= 4.0f;

    // Compute final color
    float3 finalColor;
    
    float2 uvColor = interpolants.m_TexCoord0.xy;
    float4 texelColor;
    
#ifdef FOUR_BLEND
    #ifdef USE_TERRAIN_BLEND
        texelColor = FourBlendByTexture(uvColor, mapUV );
    #else
        texelColor = FourBlendByVertexUV(uvColor, vertexUV1 );
    #endif
#else  // FOUR_BLEND
    // no blend, simply read the color texture   
    texelColor = tex2D(DIFFUSE_MAP, uvColor);
#endif // FOUR_BLEND
    
#if defined(NOLIGHTING) || defined(FULLBRIGHT)  //normally we'd multiply in the shadow value here but with the lowest detail settings, shadows are disabled.
    finalColor.rgb = texelColor.rgb;
#else
    // No emissive, so just simple modulate
    finalColor = saturate( lighting*texelColor.xyz );
#endif

#if !defined(DISABLE_FOW)
    float4 fowFactors = GetFogOfWarFactors(interpolants.m_TexCoord0.zw);
    finalColor.rgb = lerp(fowFactors.rgb, finalColor.rgb, fowFactors.a);
#endif

    // Output
    fragment.m_Color0.rgb = finalColor;
    fragment.m_Color0.a   = texelColor.a;
    
//Handle the high-end per-pixel fogging.
#if defined(FOG_PIXELSHADER_BLEND) && !defined(FULLBRIGHT) && !defined(NOLIGHTING)
    //lerp towards the pure fog color and fully opaque towards the second layer of the volumetric fog.
    float fogOpacity = saturate( interpolants.m_Color0.a );
    //final fog blend.
    fragment.m_Color0.rgb = lerp( fragment.m_Color0.rgb, FOG_COLOR.rgb, fogOpacity );
#endif

    fragment.m_Color0.rgb = ApplyMipColorsVisualization(fragment.m_Color0.rgb, uvColor);

    return fragment;
}

//LOW QUALITY
#endif
