Simple ring wipe shader works, but I don't understand why.

Hi all. I’ve been tackling shaders recently, and been doing ok. I wanted to do some screen transitions, and wrote a simple ring wipe shader. It has 3 parameters, a tex2D for the old screen, one for the new, and a float to indicate how far along the transition is. The shader itself works…but I am not understanding something.

Here is my drawing code inside Game1.cs

spriteBatch.Begin(0, null, null, null, null, effect);
//spriteBatch.Draw(_blankTexture, graphics.GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.Draw(newScreen, graphics.GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.End();
`

This code works, what I don’t understand is why the commented line doesn’t: _blankTexture is the classic 1 pixel white texture. If I use it, the color inside the ring is blank white like the unshaded texture. Shouldn’t the pixel shader return the color needed and draw it over the blank white? I’m happy my simple shader works, but I really would like to understand what I’m missing here.

Here is the code for the ring wipe shader.

float4 MainPS(VertexShaderOutput input) : COLOR
{
float2 center = float2 (0.5, 0.5);
float4 result;
float4 newColor = tex2D(NewSample, input.Texcoord);
float4 oldColor = tex2D(OldSample, input.Texcoord);
float2 p1 = input.Texcoord;
float dist = distance(p1, center);

if (dist <= Delta) {
    result = newColor;
}
else if (dist > Delta) {
    result = oldColor;
}
return result;

Probably has to do with how your textures are set up.

SpriteBatcher’s flush explicitly sets Texture[0] to the given texture which is likely overwriting anything you’ve previously setup in the effect.

The uncommented code works because texture unit 0 is the new screen, while texture unit 1 is the old one.

1 Like

Thank you! Knowing this explains alot of weird behaviors I’ve been getting playing around with 2D HLSL. Totally fixed it for me! You are awesome.