//===================================================================================================
// Uniform Variables
//===================================================================================================
float4x4 ViewProj;
float4x4 inverseViewProj;
float numberOfPlayers;

//-- aiVisionTexture --
texture aiVisionTexture; 
sampler aiVisionSampler = sampler_state 
{
	texture		= <aiVisionTexture>; 
	AddressU	= CLAMP; 
	AddressV	= CLAMP; 
	AddressW	= CLAMP; 
	MIPFILTER	= ANISOTROPIC;	
	MINFILTER	= ANISOTROPIC;	
	MAGFILTER	= ANISOTROPIC;
};


//===================================================================================================
// Uniform, Varying, and FrameBuffer Stream Structures - 
// Used to Communicate between CPU, VertexProcessor, FragmentProcessor, and FrameBuffer
//===================================================================================================
struct VertexInput 
{
    float4	position				: POSITION0;		// Position
    float4	texCoord				: TEXCOORD0;		// Texture coordinates
};

struct VertexOutput
{
	// These values are used directly by the rasterizer
	float4	position				: POSITION; 		// Position
	float4	color					: COLOR0; 			// Color (example: COLOR0)
	float4	texCoord				: TEXCOORD0; 		// Texture coordinates (example: TEXCOORD0)
};

struct FragmentInput
{
	float4	color					: COLOR0; 			// Color (example: COLOR0)
    float4	texCoord				: TEXCOORD0;		// Texture coordinates 
};

struct FragmentOutput
{
	float4	color				: COLOR0; 			// Color for render target n
};
 

//===================================================================================================
// Vertex and Fragment Shaders
//===================================================================================================

// AI Vision - Texture Scan Main
//--------------------------------------------------------------------------------------------------
VertexOutput AI_Vision_VertexTextureScan(VertexInput IN)
{
	VertexOutput OUT = (VertexOutput)0;
	
	OUT.position = IN.position;
	OUT.texCoord = IN.texCoord;
	
	float4 textureOut = tex2Dlod(aiVisionSampler, float4(IN.texCoord.x, IN.texCoord.y, 0, 0));
	
	if(textureOut.r >= 0.1f)
	{
		// player index
		int index = round(textureOut.a * numberOfPlayers);
	
		OUT.position.x = (index % 4) / 4.0f;
		OUT.position.y = round(index / 4) / 4.0f;
		
		OUT.color.a = 1.0f;
		OUT.color.rgb = mul(float2(IN.texCoord.x, IN.texCoord.y), inverseViewProj);
	}
	else
	{
		OUT.position.xy = -1.0f;
		OUT.color = 0.4f;
	}

	OUT.position = mul(OUT.position, ViewProj);

	return OUT;
}

FragmentOutput AI_Vision_FragmentTextureScan(FragmentInput IN)
{
	FragmentOutput OUT = (FragmentOutput)0;

	OUT.color = IN.color;
	
	return OUT;
}

// AI Vision - Scan AI Texture
//--------------------------------------------------------------------------------------------------
technique AI_Vision_TextureScan
{
	pass Pass0
	{
		VertexShader  = compile vs_3_0 AI_Vision_VertexTextureScan();
		PixelShader =  compile ps_3_0 AI_Vision_FragmentTextureScan();
		
		// Alpha blending
        AlphaBlendEnable = true; SrcBlend = SrcAlpha; DestBlend = InvSrcAlpha;
		FillMode = Solid;
	}
}