uniform sampler greyArea		: register(s0);
uniform sampler diffuseMap		: register(s1);

static const float LOS_THRESHOLD = 0.75f;

extern uniform float 	worldSize;
extern uniform float 	useAlpha;
extern uniform float4 	losTint;

struct VS_INPUT 
{
	float2 uv0							: TEXCOORD0;
	float2 terrainUV					: TEXCOORD1;
};

float4 main( VS_INPUT input ) : COLOR0
{
	float4 outColor = float4(1, 1, 1, 1);
	
	float offset = 1/worldSize;
	
	// number should be odd, since the current pixel is the center of the block
	float blockSide = 3.0f;
	
	// Ashwin please review : rakehsj
	float fOneTileOffset = 4*offset;
	
	float4 tempGrey = float4( 0.0f, 0.0f, 0.0f, 0.0f );
	float2 tempGreyAreaUV = float2( input.terrainUV.x-fOneTileOffset-blockSide*offset, input.terrainUV.y-blockSide*offset );
	
	for( int i = 0;i < blockSide;i ++ )
		for(int j = 0;j < blockSide;j ++ )
			tempGrey += tex2D( greyArea, float2(tempGreyAreaUV.x + j*offset, tempGreyAreaUV.y + i*offset) );
	
	tempGrey /= blockSide*blockSide;

#ifndef FUZZY_LOS 
	if( tempGrey.r < LOS_THRESHOLD )
	{
		outColor *= losTint;
	}
#else	
	// tempGrey:0.5->1.0
	outColor *= lerp( 2*losTint-1, 1, tempGrey.r);
#endif

	if( useAlpha == 1.0 )
	{
		float alpha = tex2D( diffuseMap, input.uv0 ).a;
		outColor = lerp( outColor, 1, 1-alpha );
	}
	
	return outColor;
}
