Using C# (VS2010) with XNA 4.0, I have a terrain layer (RenderTarget2D) in a 2D side-scroller. My visual system tracks updates to redraw individual tiles, but I am having trouble finding a way to clear out the rectangle where the tile will be drawn, which I must do because A) there may no longer be a tile or B) the tile may itself contain transparency.

How can I draw a rectangle of transparency onto an existing RenderTarget2D? I essentially want to clear just that rectangular portion of it.

My Google searches have not yielded anything relevant.


1 Answer 1


Gustavo Maciel provided a link about BlendState, but it did not contain any information about the function of each BlendState option or how to use them. I have had quite a bit of time to play around with XNA now, and I have found the answer to this question through trial and error.

BlendState.Opaque will cause each color channel to overwrite the current render target, rather than alpha blending it in. For the example in my question, I could have created a small texture cleared with new Color(0, 0, 0, 0), and then drawn it onto my terrain layer after beginning the sprite batch as follows:

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque);

This could have been done even more efficiently through a .fx file, in a pixel shader, sourcing from a 2D texture with point sampling. Each pixel in the source texture would represent a single tile, and empty pixels would represent "no tile". Tiling this way is considerably easier and more efficient, as only 1 draw call is required to build the entire terrain layer, versus one for each tile if you rebuild every frame. It also prevents you from having to deal with preserving the terrain layer's state if you only update when tiles are actually changed.

  • \$\begingroup\$ This was useful for me, thanks. Now I have properly trimmed shadows! \$\endgroup\$
    – DrZ214
    Oct 31, 2017 at 12:22

You must log in to answer this question.

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