问题描述
我们都需要一个BlendState充当如下:
We are needing a BlendState to act as the following:
- 在透明的PNG绘制预期,在他们身后什么preserved
- 我们用
Color.White
来画一个PNG原样 - 我们将改变颜色的alpha通道,以改变纹理 的不透明度
- 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
要获得这种效果, BlendState.AlphaBlend
接近,但平局白色为透明的部分,如果我们设置阿尔法100或任意数量超过255个等。
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;
这工作,但我们现在可以得到意想不到的效果如果两个PNG图像都在彼此顶部。基本上,我们得到了一些奇怪的线条它看起来像像素的数据被添加(或其它)。
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).
例如:
实际上, BlendState.AlphaBlend
是这样的:
_blendState = new BlendState();
_blendState.AlphaSourceBlend = Blend.SourceAlpha;
_blendState.AlphaDestinationBlend = Blend.InverseSourceAlpha;
_blendState.ColorSourceBlend = Blend.One;
_blendState.ColorDestinationBlend = Blend.InverseSourceAlpha;
图片看起来比上面的好:
Image looks better than above:
但随后阿尔法不起作用,使用100作为阿尔法将取代白色背景。
But then alpha doesn't work, using 100 as alpha would replace the background with white.
什么 BlendState
我们应该用它来获得SpriteBatch我们所期望的效果?我们即可使用不同的颜色,如 Color.Black
有另一种方式来得到它的工作。
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 - 另一个不错的功能是,如果我们可以使用 Color.Red
来色调纹理,但我们希望它在一般先工作
*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.
推荐答案
尝试 premultipliedAlpha
设置为假
对于那些巴纽在你的项目内容。
Try setting premultipliedAlpha
to false
for those .png's in your Content Project.
不幸的是,我不知道如何使用时要解决此问题 Texture2d.FromStream()
。
Unfortunately, I don't know how to resolve this issue when using Texture2d.FromStream()
.
这篇关于新华社BlendState与SpriteBatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!