How can I tint a sprite to white in XNA?
Asked Answered
B

5

7

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?

Barros answered 17/9, 2008 at 13:4 Comment(0)
I
3

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.

Internist answered 17/9, 2008 at 13:38 Comment(0)
E
1

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

Eutectoid answered 17/9, 2008 at 18:25 Comment(0)
I
1

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

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

Internist answered 17/9, 2008 at 19:55 Comment(0)
L
1

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.

Leeuwenhoek answered 23/9, 2008 at 18:20 Comment(0)
C
0

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.

Cigar answered 17/9, 2008 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.