I've read a lot of tutorials, but it just doesn't seem to work. Here's the code as suggested by a tutorial, using the built-in AlphaTestEffect class:

Matrix projection = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
alphaTest = new AlphaTestEffect(GraphicsDevice);
alphaTest.VertexColorEnabled = true;
alphaTest.DiffuseColor = Color.White.ToVector3();
alphaTest.AlphaFunction = CompareFunction.Greater;
alphaTest.ReferenceAlpha = 128;
alphaTest.World = Matrix.Identity;
alphaTest.View = Matrix.Identity;
alphaTest.Projection = halfPixelOffset * projection;

Then this object is just passed in to the SpriteBatch.Begin method. The result: Nothing appears on the screen, whereas the exact same code that doesn't pass in the alphaTest object renders correctly.

Note that this is the case even with CompareFunction.Always! (I tried CompareFunction.Never too in case I misunderstand the comparison order).

I am following this tutorial, but I've also seen that same code elsewhere. The tutorial has a downloadable file for XNA 4.0 to demonstrate it. I downloaded and tried it, and - It doesn't work. No craters show up. So what the heck is going on?


1 Answer 1


I followed your link and checked out the tutorial, your code seemed a little strange after looking at crater's texture which had alpha pixels as mask, so I downloaded tutorial's source. It's working fine for me, and there are differences from your code in these lines:

  alphaTestEffect.AlphaFunction = CompareFunction.Equal;
  alphaTestEffect.ReferenceAlpha = 0;

These code lines mean pixels whose alpha is equal to zero will be kicked out. In the context of this tutorial it means only actual transparent crater with it's half-transparent edges will be drawn to the stencil buffer setting it to 1, and solid white background will be discarded.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .