0

我正在为Terraria开发某种 mod (用C#编写并使用XNA),其中我需要使用一些混合模式。我没有任何麻烦让添加剂混合工作,但减法混合给我带来了一些问题。
我设法用减法混合来显示东西,但它并不想回到标准模式。SpriteBatch.EndBegin根本没有帮助。

这是我的自定义BlendState

public readonly static BlendState
    bsSubtract = new BlendState{
        ColorSourceBlend = Blend.SourceAlpha,
        ColorDestinationBlend = Blend.One,
        ColorBlendFunction = BlendFunction.ReverseSubtract,
        AlphaSourceBlend = Blend.SourceAlpha,
        AlphaDestinationBlend = Blend.One,
        AlphaBlendFunction = BlendFunction.ReverseSubtract
    },

绘图代码:

sb.End();
sb.Begin(SpriteSortMode.Immediate,bsSubtract);
(...drawing drawing blah...)
sb.End();
sb.Begin(SpriteSortMode.Immediate,BlendState.Additive);

问题是,在这段代码之后绘制的所有内容似乎仍然使用一些旧选项(半透明、平淡)。我究竟做错了什么?

我什至尝试在设置混合状态之前只调用sb.End()sb.Begin(),或者使用另一个自定义混合状态,它是标准的添加状态,只是将BlendFunctions设置为Add,但无济于事。

编辑:似乎设置任何自定义 BlendState 都可以做到这一点......

EDIT2:似乎问题是我将绘图分成 3 个不同的地方:一个用于物品槽,一个用于瓷砖,一个用于一般世界。在其中一个(项目)中,我忘记在使用之前设置 SpriteBatch 并在之后重置它。我应该花更多时间查看我的代码。不过,感谢您的帮助!
(暂时无法关闭问题,在 StackOverflow 让我这样做后将其关闭)

4

2 回答 2

0

The default blending mode is BlendState.AlphaBlend.

Try replacing BlendState.Additive with BlendState.AlphaBlend in your code. Or possibly NonPremultiplied, depending on what Terraria is actually using.

Better yet, you could read out exactly the blend state that Terraria was using, as SpriteBatch sets it on the graphics card and simply leaves it there. Here is some untested code that should do exactly that:

sb.End(); // Sets blend state
BlendState previousState = GraphicsDevice.BlendState; // Retrieve it
sb.Begin(SpriteSortMode.Immediate, bsSubtract);
// (...drawing drawing blah...)
sb.End();
sb.Begin(SpriteSortMode.Immediate, previousState); // Re-use it
于 2013-06-17T13:18:28.793 回答
0

Seems like the problem was me splitting the drawing to 3 separate places: one for item slots, one for tiles and one for world in general. And in one of these (items) I forgot to set the SpriteBatch before using and reset it afterwards. I should have spent more time looking at my code. Still, thanks for trying to help!

于 2013-06-19T08:27:45.017 回答