I've written a pixel shader to mask 2 textures (both belong to a spritesheet). This works great but adding support to flip the resulting image horizontal is proving to be very difficult.
// Tile.
uniform extern texture ScreenTexture;
sampler screen : register(s0) = sampler_state
{
Texture = <ScreenTexture>;
MinFilter = POINT; // Stops pixels bleeding together.
MagFilter = POINT;
};
// Alpha map (mask) texture to blend with.
uniform extern texture maskTexture;
sampler mask : register(s1) = sampler_state
{
Texture = <maskTexture>;
MinFilter = POINT;
MagFilter = POINT;
};
float2 spritePos; // Screen coordinates of the tile.
float2 maskPos; // Screen coordinates of the mask.
bool flip;
float4 PixelShaderFunction(float2 inCoord: TEXCOORD0) : COLOR
{
// Convert the mask position to 0-1 texels.
float2 maskTexel = (maskPos - spritePos) / 512;
float2 pos = inCoord + maskTexel;
float x = (1 - inCoord.x) + maskTexel.x;
if (flip) pos = float2(x, pos.y);
// Blend!
float4 color = tex2D(screen, inCoord);
if (maskPos.x < 0) return color;
float4 maskColor = tex2D(mask, pos);
if (maskColor.a > 0) color.rgba = 0;
return color;
}
technique
{
pass P0
{
// The xbox can only run pixel shader 2.0 and for this purpose that is plenty for us too.
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
From what I've read, 1 - x is all that is required to flip horizontally so I'm guessing combining this with the spritesheet is what's causing the issue?
I've tried for several hours and I can't get it to anything apart from a black square, upside down and backwards or squashed!
Any suggestions?
Cheers,
John