1

我试图在 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

(注意:我最初只是用黑色背景和透明中心制作了一个简单的纹理并将其绘制在顶部,但现在我需要多个具有可能重叠的发光区域的对象,这种方法将不再有效;因此我需要找出阿尔法混合...)

4

1 回答 1

0

这里有一些关于制作战争迷雾的好资源,这似乎是你的目标。

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 Blending 也会给你想要的效果。第二个链接使用 alpha 混合和您尝试应用的相同技术。但是,示例代码适用于 XNA 3.1,而不是 4.0。

于 2011-06-17T15:43:05.010 回答