How can I tint a sprite to white in XNA?

泪湿孤枕 提交于 2019-12-05 06:10:57

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.

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

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

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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!