让我吐血的难题——XNA 中的Alpha混合

在XNA中遇到PNG透明度问题,导致白色背景或线条异常。通过调整BlendState及内容处理器设置,解决了两个PNG重叠时的颜色混合问题,实现透明效果并避免像素数据异常叠加。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于对XNA 的颜色混合使用不太理解,所以上MSDN搜到我吐血,头痛,呕吐,经过一翻对生命的折腾,问题终于得到了解决。。。。。。! 原来是我对XNA 的颜色混合使用不太适合。


颜色合混本身并不难,难的是不会用XNA去表达!!!!!

透明混合公式如下:
最 终颜色 = 源颜色 X  源颜色透明值 + 目标颜色 X  (255 - 源颜色透明值)

要是在DirectX,我早就实现了,可是在XNA让我吐血,原因是,XNA导入到资源窗口的时候修改了一些参数设置,现在要使用带有Alpha通道的PNG图片,就要在导入后的PNG的右键——属性——Content Processer——Premultipy Alpha设置为false.

以下是解决我的问题的帖 子:

楼主提问

XNA BlendState with SpriteBatch


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=newBlendState(); _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:

由于对XNA 的颜色混合使用不太理解,所以上MSDN搜到我吐血,头痛,呕吐,经过一翻对生命的折腾,问题终于得到了解决。。。。。。! 原来是我对XNA 的颜色混合使用不太适合。


颜色合混本身并不难,难的是不会用XNA去表达!!!!!

透明混合公式如下:
最 终颜色 = 源颜色 X  源颜色透明值 + 目标颜色 X  (255 - 源颜色透明值)

要是在DirectX,我早就实现了,可是在XNA让我吐血,原因是,XNA导入到资源窗口的时候修改了一些参数设置,现在要使用带有Alpha通道的PNG图片,就要在导入后的PNG的右键——属性——Content Processer——Premultipy Alpha设置为false.

以下是解决我的问题的帖 子:

楼主提问

XNA BlendState with SpriteBatch


 

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=newBlendState(); _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:

enter image description here

Effectively, BlendState.AlphaBlend is this:

_blendState=newBlendState(); _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 asColor.Black there is another way to get it to work.

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


1楼回答:

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值