Skip to main content
GameDev.net gamedev.net
🔒 Locked

Max Blend/offscreen RenderTarget(2D) in XNA?

Started by Verg Mar 31, 2009 at 6:13 PM 1 replies 3k views
Original Post
Verg
Verg
Hello, I've gone over Drilian's fantastic article on lightning, and am having a bit of trouble getting the same sort of effect in XNA. I've gotten this with anti-aliasing and alpha blended lightning segments: (everything looks even more obviously bad when I apply an additional glow texture to each segment) As you can see, the edges of the segments (when they overlap) are not blending as one might expect; they're doing additive blending instead of max blending. I'm just wondering if there's an easy way (using XNA) to perhaps render the lightning bolt offscreen using Blend.Max and then render that to the screen using Alpha. If so, might someone please suggest how? Could it be accomplished using a render target? Thanks and greetz Chad
MJP
MJP
Sure, you could do that with a RenderTarget2D. You basically just create it when you initialize your graphics stuff, set it as the current RenderTarget using GraphicsDevice.SetRenderTarget (use index 0), and then draw whatever you need drawn. When you're done, you call GetTexture to get a Texture2D that you can use normally.

Probably the easiest way to start would be to just create a RenderTarget2D that's the same size as your backbuffer. Then you can render your lighting exactly like you're doing it now (except with BlendFunction.Max) to the RenderTarget, and afterwards just use SpriteBatch to draw it on top of everything else.

Just be careful about the order in which you set your RenderTargets. If you target the 360 you'll want to do it in this order:

-Render lightning to RT
-Render everything else
-Render lightning on top

If you draw to the backbuffer, then to a RT, then back to the backbuffer, your backbuffer will be cleared when you switch to it the second time. If you want you can use RenderTargetUsage.PreserveContents in your PresentationParameters struct, and then the backbuffer won't be cleared. Just be aware that it's not too fast on the 360, so it's more desirable to use RenderTargetUsage.DiscardContents.
Verg
Verg
Quote:
Original post by MJP
Sure, you could do that with a RenderTarget2D. You basically just create it when you initialize your graphics stuff, set it as the current RenderTarget using GraphicsDevice.SetRenderTarget (use index 0), and then draw whatever you need drawn. When you're done, you call GetTexture to get a Texture2D that you can use normally.

Probably the easiest way to start would be to just create a RenderTarget2D that's the same size as your backbuffer. Then you can render your lighting exactly like you're doing it now (except with BlendFunction.Max) to the RenderTarget, and afterwards just use SpriteBatch to draw it on top of everything else.

Just be careful about the order in which you set your RenderTargets. If you target the 360 you'll want to do it in this order:

-Render lightning to RT
-Render everything else
-Render lightning on top

If you draw to the backbuffer, then to a RT, then back to the backbuffer, your backbuffer will be cleared when you switch to it the second time. If you want you can use RenderTargetUsage.PreserveContents in your PresentationParameters struct, and then the backbuffer won't be cleared. Just be aware that it's not too fast on the 360, so it's more desirable to use RenderTargetUsage.DiscardContents.


Oh... thanks, MJP for the extra info.

I actually did get this to work as expected (without looking here first :-)) but you've just confirmed that this was the way to go about it.

I think in our game we:

*render to a render target for the lightning bolt

*switch to a new render target for this particular scene

*render everything in the scene onto this render target, including the resulting Texture2D from the initial render target.

Everything seems to work smoothly on a PC... we'll see how it goes on the 360.


Thanks for the heads up


Chad

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.