且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XNA 4.0 中的设备 AlphaBlend 状态

更新时间:2023-12-06 14:26:46

首先,您想要对几何图形进行排序或使用 alpha 测试,因为您无法将基于 Z 缓冲区(又名:深度缓冲区)的渲染与透明度.无法混合"Z 缓冲区值 - 因此它始终使用最高值,阻挡后面的东西,即使您渲染的颜色是透明的.

First of all you want to either sort your geometry or use alpha-testing, because you cannot combine Z-buffer (aka: depth-buffer) based rendering with transparency. There is no way to "blend" a Z-buffer value - so it always uses the topmost value, blocking off the things behind, even when the colour you are rendering is transparent.

看看这篇文章的解释(这是一个关于质量的词).这将解决您的几何问题有时不会显示在一些透明几何后面.

Take a look this article for an explanation (and this one for a word on quality). This will solve your problem of geometry sometimes not showing up behind some transparent geometry.

您的混合状态的问题在于您正在对尚未预乘的数据使用预乘混合.

The problem with your blend state is that you're using premultiplied blending with data that has not been premultiplied.

有关预乘的解释,请参阅本文阿尔法.它是 在 XNA 4.0 中设为默认值.

当您预乘时,在混合它们(即:pre)之前,您将通过 alpha 值缩放颜色值 (RGB)(即:乘以).

When you premultiply, you scale your colour values (RGB) by your alpha value (ie: you multiply them) before you blend them (ie: pre).

因此有两种可能的解决方案:您可以使用 BlendState.NonPremultiplied.但是预乘有好处(如该文章中所述),因此更好的解决方案是在您的着色器中执行乘法,如下所示:

So there are two possible solutions: you could use BlendState.NonPremultiplied. But premultiplying has benefits (as explained in that aritcle), so the better solution is to perform the multiplication in your shader like so:

return float4(result.xyz * alpha, alpha);

当然,如果可能的话,***简单地使用带有 Alpha 通道的单个纹理.如果您无法使用艺术工具组合 alpha 通道,您可以使用 XNA Content Pipeline 组合两个纹理并在构建期间执行预乘.默认的 Content Pipeline 纹理导入器默认进行预乘.

Of course, it is preferable to simply use a single texture with an alpha channel, if that is at all possible. If you can't combine the alpha channel using your art tools, you could use the XNA Content Pipeline to combine the two textures and perform premultiplication during your build. The default Content Pipeline texture importer does premultiplication by default.