float4x4 wMat : WORLD;
float fColorScale;
float4x4 viewProj : ViewProjection;
float4 vPosition : vPosition;

texture diffuseTexture : DiffuseTexture;
float4x4 VPInv : VPInv;
float4x4 View : View;

float4x4 Proj : Projection;
float4x4 ProjInv : ProjInv;


sampler DiffuseTextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

struct vertexInput
{
    float4 pos : POSITION;
    float4 tint : COLOR0;
    float2 coord : TEXCOORD0;
};

struct vertexOutput
{
    float4 hPosition		: POSITION;
    float4 tint             : COLOR0;
    float2 texCoordDiffuse	: TEXCOORD0;
};

// Used when coords are XYZW, not XYZRHW
vertexOutput VS_PassThru(vertexInput vi)
{
    vertexOutput OUT;
     
    float4 textInHSpace = mul(vi.pos, wMat);
    float4 textInVSpace = mul(textInHSpace, ProjInv);
        
    // add origin + vertex offsets in viewspace:
	float4 originInView = mul(vPosition, View);
	float4 combinedView = originInView + (vi.pos * float4(1,-1,1,1));;
	
	OUT.hPosition = mul( combinedView, Proj); 
	//OUT.hPosition = mul(vi.pos, wMat);
	  
    OUT.tint = vi.tint;
    OUT.texCoordDiffuse = vi.coord;
    return OUT;
};

float4 PS_Textured( vertexOutput IN) : COLOR0
{
    float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    float4 mix = diffuseTexture.b * IN.tint;
    mix.rgb *= fColorScale;
    return mix;
}

float4 PS_Textured2( vertexOutput IN) : COLOR0
{
    float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    diffuseTexture.rgb *= fColorScale;
    float4 mix = diffuseTexture * IN.tint;
//    clip (mix.a - 0.1);
    return mix;
}

//-----------------------------------

technique Regular
{
    pass p0 
    {
        VertexShader = compile vs_1_1 VS_PassThru();
#ifndef FixedPipe
		PixelShader  = compile ps_2_0 PS_Textured();
#else
        PixelShader = 0;
        texturefactor = 0x80FF8080;
        sampler[0] = <DiffuseTextureSampler>;
        colorop[0]   = MultiplyAdd;	               alphaop[0]   = SelectArg1;
        colorarg1[0] = Texture;                    alphaarg1[0] = Diffuse;
        colorarg2[0] = TFactor|alphareplicate;
        colorarg0[0] = TFactor|alphareplicate;

        colorop[1]   = DotProduct3;	alphaop[1]   = DotProduct3;
        colorarg1[1] = current;     alphaarg1[1] = current;
        colorarg2[1] = TFactor;     alphaarg2[1] = TFactor;

        colorop[2]   = modulate;    alphaop[2]   = modulate;
        colorarg1[2] = current;     alphaarg1[2] = current;
        colorarg2[2] = diffuse;     alphaarg2[2] = diffuse;

        colorop[3]   = disable;     alphaop[3]   = disable;
#endif
		alphatestenable = true;
		alpharef = 8;
		alphafunc = greaterequal;
		cullmode = none;
		fillmode = solid;
		zenable = false;
#ifndef _X360
		fogenable = false;
#endif
		stencilenable = false;
    }
    pass RestoreState
    {
        alphatestenable = false;
        cullmode = ccw;
        zenable = true;
    }
}

technique BlendedRegular
{
    pass p0 
    {
        VertexShader = compile vs_1_1 VS_PassThru();
#ifndef FixedPipe
		PixelShader  = compile ps_2_0 PS_Textured();
#else
        PixelShader = 0;
        texturefactor = 0x80FF8080;
        sampler[0] = <DiffuseTextureSampler>;
        colorop[0]   = MultiplyAdd;	               alphaop[0]   = SelectArg1;
        colorarg1[0] = Texture;                    alphaarg1[0] = Diffuse;
        colorarg2[0] = TFactor|alphareplicate;
        colorarg0[0] = TFactor|alphareplicate;

        colorop[1]   = DotProduct3;	alphaop[1]   = DotProduct3;
        colorarg1[1] = current;     alphaarg1[1] = current;
        colorarg2[1] = TFactor;     alphaarg2[1] = TFactor;

        colorop[2]   = modulate;    alphaop[2]   = modulate;
        colorarg1[2] = current;     alphaarg1[2] = current;
        colorarg2[2] = diffuse;     alphaarg2[2] = diffuse;

        colorop[3]   = disable;     alphaop[3]   = disable;
#endif
		alphatestenable = true;
		alpharef = 8;
		alphafunc = greaterequal;
		alphablendenable = true;
		srcblend = srcalpha;
		destblend = invsrcalpha;
		cullmode = none;
		fillmode = solid;
		zenable = false;
#ifndef _X360
		fogenable = false;
#endif
		stencilenable = false;
    }
    pass RestoreState
    {
        alphatestenable = false;
        alphablendenable = false;
        cullmode = ccw;
        zenable = true;
    }
}

technique Regular2
{
    pass p0 
    {
        VertexShader = compile vs_1_1 VS_PassThru();
#ifndef FixedPipe
		PixelShader  = compile ps_2_0 PS_Textured2();
#else
        sampler[0] = <DiffuseTextureSampler>;
        colorop[0]   = modulate;	alphaop[0]   = modulate;
        colorarg1[0] = Texture;     alphaarg1[0] = Texture;
        colorarg2[0] = diffuse;     alphaarg2[0] = diffuse;

        colorop[1]   = disable;     alphaop[1]   = disable;
#endif
		alphatestenable = true;
		alpharef = 8;
		alphafunc = greaterequal;
		cullmode = none;
		fillmode = solid;
		zenable = false;
#ifndef _X360
		fogenable = false;
#endif
		stencilenable = false;
    }
    pass RestoreState
    {
        alphatestenable = false;
        cullmode = ccw;
        zenable = true;
    }
}

technique BlendedRegular2
{
    pass p0 
    {
        VertexShader = compile vs_1_1 VS_PassThru();
#ifndef FixedPipe
		PixelShader  = compile ps_2_0 PS_Textured2();
#else
        sampler[0] = <DiffuseTextureSampler>;
        colorop[0]   = modulate;	alphaop[0]   = modulate;
        colorarg1[0] = Texture;     alphaarg1[0] = Texture;
        colorarg2[0] = diffuse;     alphaarg2[0] = diffuse;

        colorop[1]   = disable;     alphaop[1]   = disable;
#endif
		alphatestenable = true;
		alpharef = 8;
		alphafunc = greaterequal;
		alphablendenable = true;
		srcblend = srcalpha;
		destblend = invsrcalpha;
		cullmode = none;
		fillmode = solid;
		zenable = false;
#ifndef _X360
		fogenable = false;
#endif
		stencilenable = false;
    }
    pass RestoreState
    {
        alphablendenable = false;
        alphatestenable = false;
        cullmode = ccw;
        zenable = true;
    }
}
