XNA BlendState with SpriteBatch

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-15 09:18:29

问题


We are needing a BlendState to act as the following:

  • Transparent PNGs are drawn as expected, with anything behind them preserved
  • We use Color.White to draw a PNG as-is
  • We will change the alpha channel of the color to change the "opacity" of the texture

To get this effect, BlendState.AlphaBlend is close, but draws white as the transparent part if we set alpha to 100 or any number other than 255.

So we attempted this:

        _blendState = new BlendState();
        _blendState.AlphaSourceBlend = Blend.SourceAlpha;
        _blendState.AlphaDestinationBlend = Blend.InverseSourceAlpha;
        _blendState.ColorSourceBlend = Blend.SourceAlpha;
        _blendState.ColorDestinationBlend = Blend.InverseSourceAlpha;

This works, except we now get undesired effects if two PNGs are on top of one another. Basically we get some weird lines where it looks like pixel data is being added (or something).

Example:

Effectively, BlendState.AlphaBlend is this:

        _blendState = new BlendState();
        _blendState.AlphaSourceBlend = Blend.SourceAlpha;
        _blendState.AlphaDestinationBlend = Blend.InverseSourceAlpha;
        _blendState.ColorSourceBlend = Blend.One;
        _blendState.ColorDestinationBlend = Blend.InverseSourceAlpha;

Image looks better than above:

But then alpha doesn't work, using 100 as alpha would replace the background with white.

What BlendState should we use to get our desired effect from SpriteBatch? We are OK to use a different color such as Color.Black there is another way to get it to work.

*PS - another nice feature would be if we could use Color.Red to "tint" a texture, but we want it to work in general first.


回答1:


Try setting premultipliedAlpha to false for those .png's in your Content Project.

Unfortunately, I don't know how to resolve this issue when using Texture2d.FromStream().



来源:https://stackoverflow.com/questions/10450585/xna-blendstate-with-spritebatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!