XNA 中的 Alpha 混合——我做错了什么?
我试图在 XNA 4.0 中实现与 这个 stackoverflow 提问者 相同的效果试图进入 XNA 3.1:即绘制背景,并在其顶部绘制带有透明“切口”的黑色蒙版像这样。
i.imgur.com/V4yK6.jpg 的屏幕截图是我所了解的我尝试根据其他提问者的答案,使用相同的着色器代码,在 XNA 4.0 中获得类似的效果。显然我做错了什么,因为我希望它看起来像这样: i.imgur.com/ZgD3l .jpg。谁能看一下我下面的代码并给我一些关于我可能做错了什么的线索?这在背景绘制后被调用:
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.NonPremultiplied;
我现在测试的glow3纹理就是透明背景上的黑色形状:i.imgur.com/3t9gf.png。
(注意:我最初只是制作了一个具有黑色背景和透明中心的简单纹理,并将其绘制在顶部,但现在我需要多个具有可能重叠的发光区域的对象,这种方法将不再起作用;因此我需要弄清楚阿尔法混合...)
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.NonPremultiplied;
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...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一些关于制作《战争迷雾》的好资源,这似乎正是您的目标。
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/
使用 Alpha 混合也会给你带来想要的效果。第二个链接使用 Alpha 混合和您尝试应用的相同技术。但是,示例代码适用于 XNA 3.1,而不是 4.0。
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.