Alpha蒙版边界框透明与其他alpha蒙版冲突/覆盖。 XNA

问题描述 投票:0回答:1

Transparency overwriting

以上是我的问题的一个例子。我有两个完全相同的alpha蒙版,只是一个带有透明背景的圆形白色渐变。

我正在绘制一个渲染到屏幕上方的RenderTexture2D来创建光照。它清除半透明的黑色,然后将alpha蒙版绘制在正确的位置,使其看起来像灯光。

它们本身运行良好,但如果两个冲突,就像下面的“火炬”对蓝色发光蘑菇,你可以看到边界框透明度覆盖已经绘制的橙色光芒。

这是我的方法:

这是创建渲染目标:

RenderTarget2D = new RenderTarget2D(Global.GraphicsDevice, Global.Resolution.X+4, Global.Resolution.Y+4);
SpriteBatch = new SpriteBatch(Global.GraphicsDevice);

这是绘制到渲染目标:

private void UpdateRenderTarget()
    {
        Global.GraphicsDevice.SetRenderTarget(RenderTarget2D);
        Global.GraphicsDevice.Clear(ClearColor);

        // Draw textures
        float i = 0;
        foreach (DrawableTexture item in DrawableTextures)
        {
            i += 0.1f;
            item.Update?.Invoke(item);

            SpriteBatch.Begin(SpriteSortMode.Immediate, item.Blend,
           SamplerState.PointClamp, DepthStencilState.Default,
           RasterizerState.CullNone);

            SpriteBatch.Draw(
                item.Texture,
                (item.Position - Position) + (item.Texture.Size() / 2 * (1 - item.Scale)),
                null,
                item.Color,
                0,
                Vector2.Zero,
                item.Scale,
                SpriteEffects.None,
                i
            );
            SpriteBatch.End();
        }

        Global.GraphicsDevice.SetRenderTarget(null);
    }

我听说过深度模板等等。我觉得我已经尝试了很多组合,但我仍然遇到问题。在我的游戏中构建所有其他图形时,我没有遇到任何麻烦。

非常感谢任何帮助! :)

xna monogame spritebatch
1个回答
0
投票

啊,这对于BlendState本身而不是SpriteBatch来说是一个问题。我创建了一个自定义的BlendState“Multiply”,我在网上选择了导致这个问题。

“什么导致”这个问题才是真正的问题。

这是获得我的效果而没有“重叠”的解决方案:

public static BlendState Lighting = new BlendState
{
        ColorSourceBlend = Blend.One,
        ColorDestinationBlend = Blend.One,
        AlphaSourceBlend = Blend.Zero,
        AlphaDestinationBlend = Blend.InverseSourceColor
};

这允许纹理重叠,并且还从“暗度”层“减去”。更容易看出黑暗是否更不透明。

我已经回答了这个问题,只是因为一些其他的傻瓜错误与sprite批次本身的混合状态问题。

© www.soinside.com 2019 - 2024. All rights reserved.