texture tex0;
texture masktex ;
texture emblemtex;

float4x4 WorldViewProjectionMatrix; 
float4	 FlagCoords;
float4	 EmblemTemplateCoords;
float4	 EmblemCoords;

float4	Color1;
float4	Color2;
float4	Color3;

sampler BaseTexture  =
sampler_state
{
    Texture = <tex0>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = None;
    AddressU = Clamp;
    AddressV = Clamp;
};

sampler MaskTexture  =
sampler_state
{
    Texture = <masktex>;
    MinFilter = Point;
    MagFilter = Point;
    MipFilter = None;
    AddressU = Clamp;
    AddressV = Clamp;
};

sampler EmblemTexture =
sampler_state
{
	Texture = <emblemtex>;
	MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

struct VS_INPUT
{
    float4 vPosition  : POSITION;
    float2 vTexCoord  : TEXCOORD0;
    float2 vMaskCoord  : TEXCOORD1;
};

struct VS_OUTPUT
{
    float4  vPosition : POSITION;
    float2  vTexCoord0 : TEXCOORD0;
    float2  vTexCoord1 : TEXCOORD1;
	float2  vTexCoord2 : TEXCOORD2;
};

VS_OUTPUT GeneralVertexShader(const VS_INPUT v )
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	Out.vPosition  = mul(v.vPosition, WorldViewProjectionMatrix );

	Out.vTexCoord1 = v.vMaskCoord;

	Out.vTexCoord0.x = v.vTexCoord.x / FlagCoords.x + FlagCoords.z;
	Out.vTexCoord0.y = v.vTexCoord.y / FlagCoords.y + FlagCoords.w;
	
	Out.vTexCoord2.x = (v.vTexCoord.x - EmblemTemplateCoords.x) / (EmblemTemplateCoords.z * EmblemCoords.y) + EmblemCoords.x / EmblemCoords.y;
	Out.vTexCoord2.y = (v.vTexCoord.y - EmblemTemplateCoords.y) / (EmblemTemplateCoords.w * EmblemCoords.w) + EmblemCoords.z / EmblemCoords.w ;

	return Out;
}

VS_OUTPUT GeneralVertexShaderFrame(const VS_INPUT v )
{
	VS_OUTPUT Out = (VS_OUTPUT)0;

	Out.vPosition  = mul(v.vPosition, WorldViewProjectionMatrix );
	Out.vTexCoord0 = v.vTexCoord;

	return Out;
}

float4 GeneralPixelShader( VS_OUTPUT v ) : COLOR
{
	float4 OutColor = tex2D( BaseTexture, v.vTexCoord0.xy );
	float4 EmblemColor = tex2D( EmblemTexture, v.vTexCoord2.xy );
	float MaskColor = tex2D( MaskTexture, v.vTexCoord1.xy ).r;
	OutColor = lerp( OutColor, EmblemColor, EmblemColor.a );
	OutColor.a = 1; //MaskColor;
	
	OutColor.r = 1;
	
	return OutColor;
}

float4 GeneralPixelShaderColor( VS_OUTPUT v ) : COLOR
{
	//float2 vEmb = v.vTexCoord2.xy;
	//vEmb.x = clamp(v.vTexCoord2.x, EmblemCoords.x / EmblemCoords.y, (EmblemCoords.x + 1) / EmblemCoords.y );
	//vEmb.y = clamp(v.vTexCoord2.y, EmblemCoords.z / EmblemCoords.w, (EmblemCoords.z + 1) / EmblemCoords.w );
	
	float4 OutColor = tex2D( BaseTexture, v.vTexCoord0.xy );
	//float4 EmblemColor = tex2D( EmblemTexture, vEmb );
	float MaskColor = tex2D( MaskTexture, v.vTexCoord1.xy ).r;
	OutColor = OutColor.r * Color1 + OutColor.g * Color2 + OutColor.b * Color3;
	//OutColor = lerp( OutColor, EmblemColor, EmblemColor.a );
	OutColor.a = MaskColor;
	//OutColor = EmblemColor;
	//OutColor.a = 1;
	return OutColor;
}


float4 GeneralPixelShaderFrame( VS_OUTPUT v ) : COLOR
{
	float4 OutColor = tex2D( BaseTexture, v.vTexCoord0.xy );
	
	return OutColor;
}


technique tech_standard
{
	pass p0
	{
		ALPHABLENDENABLE = True;
		SrcBlend = SRCALPHA;
		DestBlend = INVSRCALPHA;
		
		VertexShader = compile vs_2_0 GeneralVertexShader();
		PixelShader = compile ps_2_0 GeneralPixelShader();
	}
}

technique tech_color
{
	pass p0
	{
		ALPHABLENDENABLE = True;
		SrcBlend = SRCALPHA;
		DestBlend = INVSRCALPHA;
		
		VertexShader = compile vs_2_0 GeneralVertexShader();
		PixelShader = compile ps_2_0 GeneralPixelShaderColor();
	}
}

technique tech_standard_frame
{
	pass p0
	{
		ALPHABLENDENABLE = True;
		SrcBlend = SRCALPHA;
		DestBlend = INVSRCALPHA;
		
		VertexShader = compile vs_2_0 GeneralVertexShaderFrame();
		PixelShader = compile ps_2_0 GeneralPixelShaderFrame();
	}
}
