如何在 XNA 中将精灵着色为白色?

发布于 2024-07-05 14:43:20 字数 154 浏览 16 评论 0原文

我认为仅使用 SpriteBatch 中的颜色设置是不可能的,因此我尝试设计一个简单的着色器,它可以获取每个像素并将其设为白色,同时尊重像素的 alpha 值。

Joel Martinez 给出的答案看起来是正确的,但是当我用 SpriteBatch 绘制精灵时如何合并呢?

I don't think this is possible just using the color setting in SpriteBatch, so I'm trying to work out a simple shader that would take every pixel and make it white, while respecting the alpha value of the pixel.

The answer Joel Martinez gave looks right, but how do I incorporate that when I draw the sprite with SpriteBatch?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゞ记忆︶ㄣ 2024-07-12 14:43:20

我附上了来自 MS 的文档页面,如果您遵循所有步骤,您应该可以立即启动并运行它。

http://msdn.microsoft.com/en-us /library/bb203872(MSDN.9).aspx

总结一下 - 您需要创建并效果文件(结合上面的代码,这确实适合您的目的),将其添加到您的项目中,然后然后在源文件中加载它并在渲染期间使用它,如链接中所述。

顺便说一句:我不太记得 SpriteBatch(因为我选择自己编写,所以限制太多),但我记得您可能需要在发送到渲染的材质中设置效果。
无论如何 - 也许你会在这里找到它:

http://creators.xna.com/ en-us/utilities/spritebatchshader

如果你想到达那里,还有一个高级代码:

http://creators.xna.com/en-us/sample/article3d

玩得开心

I attach the documentation page from MS, and if you follow all the steps you should get it up and running in no time.

http://msdn.microsoft.com/en-us/library/bb203872(MSDN.9).aspx

To sum it up - you need to create and effect file (combined of the code above which is indeed correct for your purposes), , add it to your project, and then in the source file load it and use it during the render as explained in the link.

BTW: I don't quite remember the SpriteBatch (since I chose to write my own, it's too restrictive), but as I recall you might need to set the effect in the material you send to the render.
Anyways - maybe you'll find it here:

http://creators.xna.com/en-us/utilities/spritebatchshader

And an advanced code if you want to get there:

http://creators.xna.com/en-us/sample/particle3d

Have fun

灰色世界里的红玫瑰 2024-07-12 14:43:20

我想这就是你正在寻找的

sampler2D baseMap;

struct PS_INPUT 
{
   float2 Texcoord : TEXCOORD0;

};

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

它非常简单,它只是从纹理中获取采样的颜色,然后使用纹理的 alpha 值返回全白色。

I think this is what you're looking for

sampler2D baseMap;

struct PS_INPUT 
{
   float2 Texcoord : TEXCOORD0;

};

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

It's very simple, it just takes the sampled color from the texture, and then returns an all white color using the texture's alpha value.

薄暮涼年 2024-07-12 14:43:20

我没有编写自己的像素着色器,大部分是从网络上修改的示例,你要做的就是分别增加像素中的 R、G、B 分量的值,只要它们低于 255,这会逐渐将精灵的颜色变为白色。 嘿,这押韵。

I haven't wrote my own pixel shaders, mostly modified samples from the net, what you would do is you would increase the value of the R,G,B components in the pixel respectively as long as they're under 255, this would gradually shift the color of the sprite towards white. Hey that rhymes.

三生路 2024-07-12 14:43:20

Joel Martinez 确实是对的,您可以像这样将它与 SpriteBatch 一起使用,将效果加载到tintWhiteEffect 中:

spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);

tintWhiteEffect.Begin();
tintWhiteEffect.CurrentTechnique.Passes[0].Begin();

   // DRAW SPRITES HERE USING SPRITEBATCH

tintWhiteEffect.CurrentTechnique.Passes[0].End();
tintWhiteEffect.End();

spriteBatch.End();

SpriteSortMode.Immediate 是这里的技巧,它允许您将 SpriteBatch 的默认着色器替换为您自己的着色器。 使用它会使精灵绘制速度慢一些,因为精灵不会在一次绘制调用中批量处理,但我认为您不会注意到其中的差异。

Joel Martinez is indeed right, and you use it like this with a SpriteBatch, having loaded the effect into tintWhiteEffect:

spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);

tintWhiteEffect.Begin();
tintWhiteEffect.CurrentTechnique.Passes[0].Begin();

   // DRAW SPRITES HERE USING SPRITEBATCH

tintWhiteEffect.CurrentTechnique.Passes[0].End();
tintWhiteEffect.End();

spriteBatch.End();

SpriteSortMode.Immediate is the trick here, it allows you to swap out SpriteBatch's default shader for your own. Using it will make sprite drawing a bit slower though, since sprites aren't batched up in a single draw call, but I don't think you will notice the difference.

吹梦到西洲 2024-07-12 14:43:20

如果您想在 SpriteBatch 中使用自定义着色器,请查看此示例:

http://creators .xna.com/en-us/sample/spriteeffects

If you want to use custom shaders with SpriteBatch, check out this sample:

http://creators.xna.com/en-us/sample/spriteeffects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文