-
-
- (295)
-
premium membership
-
Posts
144
|
RenderTargets, SpriteBatch, and alpha blending...help!
|
So I draw my scene to a rendertarget. I plan to use the alpha values to determine how much I want to glow things. Everything my scene is being rendered with alphas set 0 for now. So my glow render targets end up being "empty" except for for some constant color. When I want to draw my original render target back to the screen, I use spritebatch.Draw with the following settings:
spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
At this point if I ran my code, everything from my scene shows up fine. But then I try to blend this with my glow render target. I use spritebatch.draw a second time using the following settings:
spriteBatch.Begin(SpriteBlendMode.Additive,
SpriteSortMode.Immediate,
SaveStateMode.None);
The glow render target ends up completely overwriting my texture that contains the original scene pixels. I'm guessing this is because my alphas were all 0. So I try writing the scene with all my alphas as 1. This of course "fixes" things, but I can't have my pixels with 0 alpha values being overwritten by something else with 0 alpha values, I want them to still blend with the glow texture. I should mention now that all the pixels in my blur render target also have 0 alpha values. So I try this:
When I draw my original render target to screen with spritebatch I pass it through an effect that sets all the alpha values to 1, but somehow my glow render target is still overwriting it! This doesn't make sense because when I write my original scene with alphas of 1, spritebatch blends the two render targets correctly... but if i start with a scene with alphas of 0 for each pixel and then then set the alphas to 1 as I am drawing back the scene the glow render target completely overwrites the original render target. I even tried creating an intermediate render target that grabs the scene and sets the alphas to 1, and it still overwrites that render target.
Can anyone explain this? I would be happy and amazed :p
|
|
-
-
- (295)
-
premium membership
-
Posts
144
|
Re: RenderTargets, SpriteBatch, and alpha blending...help!
|
Here is my code if my explanation is confusing. When I run this code, my screen shows up as black. When drawing the mesh my pixel shader just does flat shading and sets the alpha to 0. TextureEffect is just pixel shader the returns the color the texture and ensures it's alpha (w) is 0. I apologize for the somewhat sloppy code (I've been messing around with alot of things); protected override void Draw(GameTime gameTime) {
graphics.GraphicsDevice.SetRenderTarget(0, colorTarget); graphics.GraphicsDevice.Clear(Color.TransparentBlack); //blurManager.DrawFullscreenQuad(graphics.GraphicsDevice, bg, colorTarget.Width, colorTarget.Height, textureEffect); DrawSampleMesh(myModel); graphics.GraphicsDevice.SetRenderTarget(0, null);
blurManager.SetBlurEffectParameters(1.0f / blurTarget1.Width, 0); blurManager.DrawFullscreenQuad(graphics.GraphicsDevice, colorTarget, blurTarget1, blurEffect); blurManager.SetBlurEffectParameters(0, 1.0f / blurTarget1.Height); blurManager.DrawFullscreenQuad(graphics.GraphicsDevice, blurTarget1, blurTarget2, blurEffect); graphics.GraphicsDevice.SetRenderTarget(0, null);
graphics.GraphicsDevice.RenderState.DepthBufferWriteEnable = false; graphics.GraphicsDevice.RenderState.DepthBufferEnable = false;
graphics.GraphicsDevice.Clear(Color.Black);
blurManager.DrawFullscreenQuad(graphics.GraphicsDevice, colorTarget.GetTexture(), colorTarget.Width, colorTarget.Height, textureEffect); blurManager.DrawFullscreenQuad(graphics.GraphicsDevice,blurTarget2.GetTexture(), colorTarget.Width, colorTarget.Height); graphics.GraphicsDevice.RenderState.DepthBufferWriteEnable = true; graphics.GraphicsDevice.RenderState.DepthBufferEnable = true;
// TODO: Add your drawing code here
base.Draw(gameTime); }
///// Here are the blur manager functions
public void DrawFullscreenQuad(GraphicsDevice gd, RenderTarget2D textureTarget, RenderTarget2D renderTarget, Effect effect) { gd.SetRenderTarget(0, renderTarget); gd.Clear(Color.TransparentBlack); DrawFullscreenQuad(gd,textureTarget.GetTexture(), renderTarget.Width, renderTarget.Height, effect); }
public void DrawFullscreenQuad(GraphicsDevice gd, Texture2D texture, int width, int height, Effect effect) { spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);
effect.Begin(); effect.CurrentTechnique.Passes[0].Begin();
// Draw the quad. spriteBatch.Draw(texture, new Rectangle(0, 0, width, height), Color.White); spriteBatch.End();
effect.CurrentTechnique.Passes[0].End(); effect.End();
}
public void DrawFullscreenQuad(GraphicsDevice gd, Texture2D texture, int width, int height) { spriteBatch.Begin(SpriteBlendMode.Additive, SpriteSortMode.Immediate, SaveStateMode.None);
// Draw the quad. spriteBatch.Draw(texture, new Rectangle(0, 0, width, height), Color.White); spriteBatch.End();
}
|
|
-
-
- (0)
-
premium membership
-
Posts
72
|
Re: RenderTargets, SpriteBatch, and alpha blending...help!
|
In Immediate mode, you're going to want to select the blend operation that makes sense for you. Checkout "What Is Color Blending?" in the docs for an idea of what's going on behind the scenes. You're going to want to select RenderState.SourceBlend and RenderState.DestinationBlend values that don't rely on the Alpha value.
You can try Blend.One for Destination and Blend.SrcAlpha for Source, for example.
Ex-Programmer/Writer - XNA Game Studio
|
|
|