Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any way to enable a temporary clipping mask in XNA while using a SpriteBatch? I'm drawing a bunch of Texture2D objects and Sprites in a given rectangle, and I want to make sure that said objects aren't drawn outside of that rectangle. If it's of any help, I'm already drawing my content within a Viewport, I'm not sure if I can draw, for instance, in another Viewport within that Viewport.

Illustrative image of what I'm trying to accomplish:

share|improve this question
up vote 10 down vote accepted

The scissor rect is a bit easier to use than the stencil buffer. You need to set ScissorRect to your Rectangle and set ScissorTestEnable to true.

        // In XNA < 4.0...
        spriteBatch.GraphicsDevice.RenderState.ScissorTestEnable = true;
        spriteBatch.GraphicsDevice.ScissorRectangle = clipRect;

        // In XNA 4.0, the ScissorTestEnable has moved, so do this instead...
        spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = true;
        spriteBatch.GraphicsDevice.ScissorRectangle = clipRect;
share|improve this answer
Thanks a ton! I wasn't able to get it working at first, but then I realized it wasn't XNA 4.0 compatible. I modified it and it worked awesomely. – ThatsGobbles Oct 16 '10 at 4:35
Hmm... GraphicsDevice doesn't seem to have a render state anymore. Where did this get moved to? – BrainSlugs83 Aug 25 '14 at 7:28
To follow up on the other two comments, in XNA 4.0, GraphicsDevice.RenderState doesn't exist anymore. In 4.0, to access the ScissorTestEnable, use: spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = true; – Sean Colombo Mar 15 '15 at 19:42

You need to use a Stencil Buffer. This is specified by passing a DepthStencilState into your SpriteBatch.Begin method call.

For example, to remove an object from a scene, fill a stencil buffer with a cut out pattern (using zeros) for each pixel where the object is visible. This is done by setting the reference stencil value to 0, clearing the stencil buffer, and rendering the object. Then set the reference stencil value to 1, set the compare function to CompareFunction.LessEqual, and render again. The stencil data masks those pixels whose value is non zero but less than 1, resulting in drawing over (or removing) the object.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.