Sign in to follow this  
boehmz

Color Keying in XNA

Recommended Posts

2 options I can think of of the top of my head:

1) Copy the texture to another texture and while your doing it, set the chroma pixels alpha to zero.

2) do it in a shader, make sure to check the color before any lighting is applied.

There's also the option of saving your images in a format that supports alpha.

Share this post


Link to post
Share on other sites
Assuming you're adding your sprites into the Content project in your game (which you should be ;) ), you can set the color key in the sprite file's Properties under the Content Processor.

Share this post


Link to post
Share on other sites
Quote:
Original post by Machaira
Assuming you're adding your sprites into the Content project in your game (which you should be ;) ), you can set the color key in the sprite file's Properties under the Content Processor.


yep, and if you have to use Texture2D.FromFile() you can use a code like this:


TextureCreationParameters tcp = TextureCreationParameters.Default;
tcp.ColorKey = new Color(new Vector3(255, 0, 255)); // set here the color you want as your colorkey
tcp.Format = SurfaceFormat.Rgba1010102; //I tried different surfaceformat and this works well

Texture2D.FromFile(YOUR_GRAPHICSDEVICE, YOUR_PATH, tcp);


[Edited by - FOOLVER on September 14, 2009 5:13:10 PM]

Share this post


Link to post
Share on other sites
Didn't see that before, good to know it's there. Now when I use it, it seems only part of the color is keyed. The image still shows the color that is supposed to be keyed in jagged parts around it and sometimes in black. One problem might be the A value, I don't fully understand what it is. I'm also stretching the image(using a rectangle) so I'll try displaying at regular size to see if that changes anything.

Share this post


Link to post
Share on other sites
The texture filter is blending edge pixels with the keyed color. Disabling it (i.e., using a point filter) will eliminate this at the cost of making your sprites blocky when stretched. You may be better off using an alpha test instead of a color key and storing 1-bit alpha.

Share this post


Link to post
Share on other sites
Quote:
Original post by boehmz
Didn't see that before, good to know it's there. Now when I use it, it seems only part of the color is keyed. The image still shows the color that is supposed to be keyed in jagged parts around it and sometimes in black. One problem might be the A value, I don't fully understand what it is. I'm also stretching the image(using a rectangle) so I'll try displaying at regular size to see if that changes anything.



check your image with a program because it seems yor image have not clear border!

ps. avoid the use of jpg images, jpg makes artifacts!


http://www.polisportivasantachiara.it/fool/pablo/avoid.png

good on the left side, not good on the right side

Share this post


Link to post
Share on other sites
Seems to have been the file format. Was using jpg's, switched to bmp's and now there are no problems, even when scaling(not extensively tested though). I don't really know anything about the different image types. Is there any time that I should use a jpg(it seems the most common type to me)? Or do I just need to know that they have loss of information and the technical explanation of why would confuse me?

Share this post


Link to post
Share on other sites
Quote:
Original post by FOOLVER
you can use jpg for use like backgrounds for a menù or similar, I suggest you to use png instead of bmp for the in game sprites


Agreed. I never use bmps anymore.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this