在带有简单着色器的 XNA 4.0 中制作白色精灵(alpha 问题) [英] Make a sprite white in XNA 4.0 w/ simple shader (alpha issues)

查看:67
本文介绍了在带有简单着色器的 XNA 4.0 中制作白色精灵(alpha 问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tint Sprite White<使用简单着色器/a> 和 Sprite Effects Sample 中的示例代码,我已将着色器的 .fx 文件加载到示例项目中,并替换了其中一个 Draw() 调用中的示例效果之一以使用白化效果.

Using the simple shader at Tint Sprite White and the sample code at Sprite Effects Sample, I've loaded the .fx file for the shader into the sample project and replaced one of the sample effects in one of the Draw() calls to use the whitening effect.

我得到的是一个与纹理本身尺寸相同的白色大方块.我显然只希望将 alpha 不是 0 的纹理区域设为白色.着色器似乎可以正常工作,因为我将默认白色更改为其他测试用例,并且正方形确实改变了颜色.

What I get is a big white square with the same dimensions as the texture itself. I obviously want only the regions of the texture where the alpha is not 0 to be made white. The shader appears to be working, as I altered the default white color to other test cases and the square does change color.

我在我自己的一个简单项目中尝试了这个并得到了相同的结果.如何确保只有纹理的可见部分被着色?

I tried this in a simple project of my own and got the same result. How can I make sure that only the visible part of the texture is shaded?

修改后的调用如下所示:

The modified call looks like this:

        spriteBatch.Begin(0, null, null, null, null, whiteEffect);

        spriteBatch.Draw(myTexture, CenterOnScreen(myTexture), Color.White);

        spriteBatch.End();

推荐答案

首先,仔细检查以确保图像中有 alpha.

First, double-check to make sure you have alpha in your image.

该着色器假定您使用的是非预乘 alpha:

That shader assumes you're using non-premultiplied alpha:

batch.Begin(0, BlendState.NonPremultiplied, null, null, null, whiteEffect);

着色器使用纹理的 alpha 值作为白化的基础;换句话说,alpha 越稳固,美白效果越稳固.如果您不想要任何中间"(例如部分白色),如果您的纹理中没有中间"alpha,它将起作用.

The shader uses the texture's alpha value as a base for the whitening; in other words, the more solid the alpha, the more solid the whitening effect. If you don't want any "in-betweens" (e.g. partially white), it will work if your texture has no "in-between" alpha in it.

   float4 ps_main( PS_INPUT Input ) : COLOR0
   {
       float4 color = tex2D( baseMap, Input.Texcoord );
       return float4(1.0f, 1.0f, 1.0f, color.a);
   }

由于询问了预乘版本,我将添加该版本并进行解释.

Since a premultiplied version was inquired about, I'll add that version in with an explanation.

来自这篇有用的文章:

要将非预乘颜色转换为预乘格式:

To convert a non premultiplied color into premultiplied format:

color.rgb *= color.a

color.rgb *= color.a

所以让我们在我们的着色器中做同样的事情:

So let's do the same here in our shader:

   float4 ps_main( PS_INPUT Input ) : COLOR0
   {
       float4 color = tex2D( baseMap, Input.Texcoord );
       return float4(color.a, color.a, color.a, color.a);
   }

现在您可以继续使用默认的 alpha 混合状态:

Now you can go ahead and use the default alpha blend state:

batch.Begin(0, BlendState.AlphaBlend, null, null, null, whiteEffect);

我仍然不确定你是否想要硬的白色或透明"......在这种情况下,你是否进行预乘并不重要,因为你可以只做一个布尔"风格返回纯白色(1.0f, 1.0f, 1.0f, 1.0f)或纯透明(0.0f, 0.0f, 0.0f, 0.0f);无论混合状态如何,这些特定颜色都将显示相同:

I'm still not really sure if you want the hard "white or transparent" ... in that case it doesn't really matter if you do pre-multiplied or not because you could just do a "boolean" style return of pure white (1.0f, 1.0f, 1.0f, 1.0f) or pure transparent (0.0f, 0.0f, 0.0f, 0.0f); these particular colors will both show up the same regardless of blend state:

float4 ps_main( PS_INPUT Input ) : COLOR0
{
   float4 color = tex2D( baseMap, Input.Texcoord );
   if (color.a > 0.0f)
   {
      return float4(1.0f, 1.0f, 1.0f, 1.0f);
   }
   else
   {
      return float4(0.0f, 0.0f, 0.0f, 0.0f);
   }
}

这篇关于在带有简单着色器的 XNA 4.0 中制作白色精灵(alpha 问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆