Search code examples
c#xnarendertarget

Getting rid of black background when using a render target


What I am trying to do is use a spritesheet to create a blended terrain, save that as a texture, and then pass it into a shader for additional effects. However, I am having some issues with the render target:

RenderTarget2D someNewTexture = new RenderTarget2D(GraphicsDevice, 256, 256);

GraphicsDevice.SetRenderTarget(someNewTexture);
GraphicsDevice.Clear(Color.Black);

spriteBatch.Begin();
{
    // draw some stuff
}
spriteBatch.End();

GraphicsDevice.SetRenderTarget(null);

Obviously, this creates a black background with my texture on top of it. However, when I actually draw that texture onto my main scene, I don't want the background, only the texture (if I don't use clear, I get the default purple color instead). How do I fix this?


Solution

  • Use a transparent clear color:

    GraphicsDevice.Clear(Color.TransparentBlack);
    

    If you handle blending correctly, it will result in transparent pixels not to be drawn.