【问题标题】:Alpha masks bounding box transparency clashing / overwriting with other alpha masks. XNAAlpha 遮罩边界框透明度与其他 Alpha 遮罩发生冲突/覆盖。 XNA
【发布时间】:2018-09-17 17:04:43
【问题描述】:

上面是我的问题的一个例子。我有两个完全相同的 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】:

    啊,这原来是 BlendState 本身而不是 SpriteBatch 的问题。我创建了一个自定义的 BlendState“乘法”,我在网上找到了导致问题的原因。

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

    这是在不“重叠”的情况下获得我的效果的解决方案:

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

    这允许纹理重叠,并从“黑暗”层中“减去”。如果黑暗更不透明,会更容易看到。

    我已经回答了这个问题,以防其他一些傻瓜错误地处理精灵批次本身的混合状态问题。

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多