Alpha blending in XNA -- what am I doing wrong?
I'm trying to achieve the same effect in XNA 4.0 that this stackoverflow questioner was trying to get in XNA 3.1: namely, drawing a background, and on top of that a black mask with see-through 'cutouts' like so.
The screencap at i.imgur.com/V4yK6.jpg is as far as I have gotten in my attempts to work from the other questioner's answers, using the same shader code, to get a similar effect in XNA 4.0. Clearly I'm doing something wrong, as I want it to look like this: i.imgur.com/ZgD3l.jpg. Can anyone take a peek at my code below and give me some clues as to what I might be doing wrong? This gets called after the background gets drawn:
Rectangle originRect = glowingObject.BoundingRectangle;
// Draw glow overlay
int glowSideLength = SideLength;
Rectangle glowRect = new Rectangle(originRect.X - ((glowSideLength - originRect.Width) / 2),
originRect.Y - ((glowSideLength - originRect.Height) / 2), glowSideLength, glowSideLength);
RenderTarget2D renderTarget = new RenderTarget2D(spriteBatch.GraphicsDevice,
800, 600, false, SurfaceFormat.Color, DepthFormat.None, 4, RenderTargetUsage.PreserveContents);
spriteBatch.GraphicsDevice.SetRenderTarget(renderTarget);
spriteBatch.GraphicsDevice.BlendState = BlendState.Additive;
spriteBatch.GraphicsDevice.Clear(Color.Black);
//Draw some lights and apply shader
lightEffect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(glowOverlay, glowRect, Color.White);
spriteBatch.GraphicsDevice.SetRenderTarget(null);
//Draw "light mask"
foreach (Gem gem in level.gems)
gem.Draw(gameTime, spriteBatch);
spriteBatch.Draw(renderTarget, Vector2.Zero, Color.White);
spriteBatch.GraphicsDevice.BlendState = BlendState.NonPremulti开发者_运维技巧plied;
The glow3 texture I'm testing with for now is just this black shape on a transparent background: i.imgur.com/3t9gf.png.
(Note: I originally just made a simple texture with a black background and a transparent center and drew it on top, but now that I need multiple objects with glow areas that may overlap, this approach will no longer work; hence why I need to figure out the alpha blending...)
Here are some good resources on doing Fog of War, which seems to be what you are aiming for.
https://gamedev.stackexchange.com/questions/13675/draw-real-time-fog-of-war-in-2d-game
http://jsedlak.org/2009/10/27/2d-fog-of-war/
Using Alpha Blending would also give you the desired effect. The second link uses alpha blending and the same technique you are trying to apply. However, the sample code is for XNA 3.1, not 4.0.
精彩评论