RenderTarget2D strange behavior

Hello everybody! I’m having a strange issue where my RenderTarget2D is completely black in the background, despite the code that should say otherwise (I’m using DirectX not OpenGL btw):

    protected override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);

        GraphicsDevice.Clear(Color.White);

        GraphicsDevice.SetRenderTarget(spriteRenderer.Scene);
        spriteRenderer.Begin(samplerState: SamplerState.PointClamp);
        spriteRenderer.Draw(shroomy);
        spriteRenderer.End();
        GraphicsDevice.SetRenderTarget(null);
        GraphicsDevice.Clear(Color.White);

        spriteRenderer.Begin();
        spriteRenderer.Draw(spriteRenderer.Scene, Vector2.Zero, Color.White);
        spriteRenderer.End();
    }

Here is an image of the game when I draw to the RenderTarget2D (The rotation is completely irrelevant to this issue):

And here is an image of the game when I draw directly to the backbuffer:

Am I missing a line somewhere? Sorry, I fairly new to coding with graphics.

Show us where you create the render target

    public SpriteRenderer(GraphicsDevice graphicsDevice) : base (graphicsDevice)
    {
        for (int i = 0; i < 3; i++)
        {
            layers[i] = new RenderTarget2D(graphicsDevice, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);
        }
    }

SpiteRenderer is just an extension of SpriteBatch I’m using for convenience sake.

try this

GraphicsDevice.SetRenderTarget(spriteRenderer.Scene);
GraphicsDevice.Clear(Color.Transparent);

and/or set the RenderState to Premultiplied or Alpha in spriteRenderer.Begin().

Clearing to transparent didn’t work. How do you change the RenderState?

ok . i think its the blend state, not renderate. it a parameter in spritebatch.begin.

I tried that. Still black.

Nevermind I figured it out. I had to clear after setting the render target.

1 Like

I have an additional question about render targets:
If I wanted my game to have a virtual resolution of say 480x270, for a pixelated look, but run in 1080p, would I have to run Vector2s and Rectangles through some sort of transform method, if I’m using them for logic rather than drawing, or is there an easier way to do that?