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

struct PS_Input
{
	float3 chunkPos : POSITION;
	float4 position : SV_Position;

#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;
};

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

#if USE_TEXEL_AA
	float4 diffuse = texture2D_AA(TEXTURE_0, TextureSampler0, PSInput.uv0 );
#else
	float4 diffuse = TEXTURE_0.Sample(TextureSampler0, PSInput.uv0);
#endif

#ifdef SEASONS_FAR
	diffuse.a = 1.0;
#endif

#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)
	diffuse = diffuse * TEXTURE_1.Sample(TextureSampler1, PSInput.uv1);
#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.0, TEXTURE_2.Sample(TextureSampler2, uv).rgb*2.0, PSInput.color.b);
	diffuse.rgb *= PSInput.color.aaa;
	diffuse.a = 1.0;
#endif
	float3 cp = frac(PSInput.chunkPos.xyz);

	/*Main Chunks*/
	if      ((PSInput.chunkPos.x < 0.0625 || PSInput.chunkPos.x > 15.9375) && (PSInput.chunkPos.z < 0.0625 || PSInput.chunkPos.z > 15.9375)){
		diffuse.rgb *= float3(0.0, 255.0, 0.0);

	}else if((PSInput.chunkPos.x < 0.0625 || PSInput.chunkPos.x > 15.9375) && (PSInput.chunkPos.y < 0.0625 || PSInput.chunkPos.y > 15.9375)){
		diffuse.rgb *= float3(255.0, 0.0, 0.0);

	}else if((PSInput.chunkPos.y < 0.0625 || PSInput.chunkPos.y > 15.9375) && (PSInput.chunkPos.z < 0.0625 || PSInput.chunkPos.z > 15.9375)){
		diffuse.rgb *= float3(0.0, 0.0, 255.0);

	}
	
	/*Block Drawer*/
	else if (
		((cp.x < 0.02 || cp.x > 0.98) && (cp.y < 0.02 || cp.y > 0.98)) ||
		((cp.x < 0.02 || cp.x > 0.98) && (cp.z < 0.02 || cp.z > 0.98)) ||
		((cp.y < 0.02 || cp.y > 0.98) && (cp.z < 0.02 || cp.z > 0.98))
	){
		if      ((PSInput.chunkPos.y < 0.02 || PSInput.chunkPos.y > 15.98) || (PSInput.chunkPos.y < 0.02 || PSInput.chunkPos.y > 15.98)){
			diffuse.rgb *= float3(0.9, 0.9, 0.9);

		}else if((PSInput.chunkPos.x < 0.02 || PSInput.chunkPos.x > 15.98) || (PSInput.chunkPos.x < 0.02 || PSInput.chunkPos.x > 15.98)){
			diffuse.rgb *= float3(0.9, 0.9, 0.9);

		}else if((PSInput.chunkPos.z < 0.02 || PSInput.chunkPos.z > 15.98) || (PSInput.chunkPos.z < 0.02 || PSInput.chunkPos.z > 15.98)){
			diffuse.rgb *= float3(0.9, 0.9, 0.9);
		}
		diffuse.a = 0.4;
	}

#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.0);
#endif

#endif // BYPASS_PIXEL_SHADER
}