问题
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?
回答1:
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.
回答2:
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
回答3:
If you want to use custom shaders with SpriteBatch, check out this sample:
http://creators.xna.com/en-us/sample/spriteeffects
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/82914/how-can-i-tint-a-sprite-to-white-in-xna