#include "ShaderConstants.fxh"
#include "util.fxh"

struct PS_Input
{
	float4 position : Sposition;
	float3 wpos : WPOS;
	float4 posi : POSI;


#ifndef BYPASS_PIXEL_SHADER
	lpfloat4 color : COLOR;
	snorm float2 uv0 : TEXCOORD_0_FB_MSAA;
	snorm float2 uv1 : TEXCOORD_1_FB_MSAA;
#endif

#ifdef FOG
	float4 fogColor : FOG_COLOR;
#endif
};

struct PS_Output
{
	float4 color : SV_Target;
};

#include "dect.fxh"


ROOT_SIGNATURE
void main(in PS_Input PSInput, out PS_Output PSOutput)
{
#ifdef BYPASS_PIXEL_SHADER
    PSOutput.color = float4(0.0f, 0.0f, 0.0f, 0.0f);
    return;
#else

#if USE_TEXEL_AA
	float4 diffuse = texture2D_AA(TEXTURE_0, TextureSampler0, PSInput.uv0 );
#else
	float4 diffuse = TEXTURE_0.Sample(TextureSampler0, PSInput.uv0);
	float4 tex = TEXTURE_0.Sample(TextureSampler0, PSInput.uv0);
    float4 NC = TEXTURE_1.Sample(TextureSampler1, PSInput.uv1);
#endif
     
    float4 tep = FOG_COLOR;
	float4 temp = TEXTURE_1.Sample(TextureSampler1, float2(0.0, 1.0));
    float chuva = Rain(FOG_CONTROL);
    float dia = pow(max(min(tep.r * 1.5, 1.0), 0.0), 1.2);
    float diat = pow(max(min(temp.r * 1.5, 1.0), 0.0), 1.2);
    float noite =  lerp(0.0, 2.5, pow(max(min(1.0 - temp.r * 1.5, 1.0), 0.0), 1.2));
    
    float3 DC = lerp(float3(1.4,1.4,1.4),float3(FOG_COLOR.rgb) + pow(PSInput.uv1.x, 3.0),chuva);
    float fog_flag = 0.0;
    float3 SC = float3(0.768,0.758,0.858)+PSInput.uv1.x*float3(0.318,0.318,0.17);
    float3 La = float3(0.7,0.1,-0.3);
    float3 Lb = float3(0.7,0.1,-0.3) * 0.5;
    float pisc = 0.2 * cos(TIME * 0.3 * cos(0.3 * TIME));
    float3 L = lerp(La, Lb, pisc);
    float shdY = 0.87;
    float shdX = PSInput.color.g;

#if USE_ALPHA_TEST
	#ifdef ALPHA_TO_COVERAGE
		#define ALPHA_THRESHOLD 0.05
	#else
		#define ALPHA_THRESHOLD 0.5
	#endif
	if(diffuse.a < ALPHA_THRESHOLD)
		discard;
#endif

#if defined(BLEND)
	diffuse.a *= PSInput.color.a;
#endif

#if !defined(ALWAYS_LIT)
    NC.rgb = lerp(lerp(NC.rgb,float3(1.0,1.0,1.0),PSInput.uv1.y),NC.rgb *max(NC.rgb, float3(0.07,0.0,0.88)),noite);
	diffuse *= NC;
#endif

#ifndef SEASONS
	#if !USE_ALPHA_TEST && !defined(BLEND)
		diffuse.a = PSInput.color.a;
	#endif	

		diffuse.rgb *= PSInput.color.rgb;

#else
	float2 uv = PSInput.color.xy;
	diffuse.rgb *= lerp(1.0f, TEXTURE_2.Sample(TextureSampler2, uv).rgb*2.0f, PSInput.color.b);
	diffuse.rgb *= PSInput.color.aaa;
	diffuse.a = 1.0f;
#endif

if(End(FOG_COLOR)){
    
    diffuse.rgb *= float3(0.511,0.362,0.985);
    diffuse.rgb += diffuse.rgb * float3(0.511,0.362,0.985)*1.2 * pow(PSInput.uv1.x, 2.0);
   
}else{
//nether
if(Nether(FOG_CONTROL)){//nether
    //nether do fumaça
    diffuse.rgb += lerp(float3(0.0,0.0,0.0), lerp(La * 2.5, La * 6.0, pow(PSInput.uv1.x,0.8)), clamp(FOG_COLOR.x - 0.1, 0.0, 1.0));
    //nether gases de azul
    diffuse.rgb *= lerp(diffuse.rgb ,float3(-0.2,-0.2,4.0), clamp(FOG_COLOR.y - (FOG_COLOR.x - 0.1), 0.0, 1.0));
    diffuse.rgb += lerp(float3(0.0,0.0,0.0) ,(float3(0.0,0.0,diffuse.b) * 15.0) * pow(PSInput.uv1.x,5.0), clamp(FOG_COLOR.y - (FOG_COLOR.x - 0.1), 0.0, 1.0));
    diffuse.rgb += lerp(diffuse.rgb, lerp(-float3(0.0,0.1,0.1) * 3.5, La * 5.0, pow(PSInput.uv1.x,3.0)), clamp(FOG_CONTROL.x - FOG_COLOR.y, 0.0, 1.0));
    
}else{

if(Underwater(FOG_CONTROL)){
diffuse.r *= 0.3;
}else{

    if(PSInput.color.a==0.0){
        diffuse.rgb *= 1.25;
        shdY = 0.8749;
        shdX = PSInput.color.g*1.3;
    }
    if(PSInput.color.b > PSInput.color.r*2.0){
        shdX = 1.0;
        shdY = 0.9158;
    }

    #ifdef FOG
    fog_flag = 0.5;
    #endif
    diffuse.rgb *= DC;

    if(((PSInput.uv1.y<shdY-0.000)||(shdX<0.630))&&fog_flag==0.0)diffuse.rgb *= SC;
    
    if(((PSInput.uv1.y<shdY-0.001)||(shdX<0.625))&&fog_flag==0.0)diffuse.rgb *= SC;
    
    if(((PSInput.uv1.y<shdY-0.002)||(shdX<0.620))&&fog_flag==0.0)diffuse.rgb *= SC;
    
    if(((PSInput.uv1.y<shdY-0.003)||(shdX<0.615))&&fog_flag==0.0)diffuse.rgb *= SC;
    

diffuse.rgb += (diffuse.rgb*(L*1.5-0.5)*pow(PSInput.uv1.x,3.0)*1.5)*lerp(lerp(1.0,1.0-PSInput.uv1.y,diat),1.0,chuva);

}

}}

#ifdef FOG
	diffuse.rgb = lerp( diffuse.rgb, PSInput.fogColor.rgb, PSInput.fogColor.a );
#endif

	PSOutput.color = diffuse;

#ifdef VR_MODE
	// On Rift, the transition from 0 brightness to the lowest 8 bit value is abrupt, so clamp to 
	// the lowest 8 bit value.
	PSOutput.color = max(PSOutput.color, 1 / 255.0f);
#endif

#endif // BYPASS_PIXEL_SHADER
}