Color Keying in XNA

Started by
11 comments, last by Machaira 14 years, 7 months ago
Is there a way to color key(AKA one color in your sprite doesn't show up) in XNA? I don't like my sprites showing up as rectangles, lol.
Advertisement
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.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

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.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

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 colorkeytcp.Format = SurfaceFormat.Rgba1010102; //I tried different surfaceformat and this works wellTexture2D.FromFile(YOUR_GRAPHICSDEVICE, YOUR_PATH, tcp);


[Edited by - FOOLVER on September 14, 2009 5:13:10 PM]
-----I am working on: xna isometric online rpg
Note that using FromFile doesn't work on the Xbox 360.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Ah good to know, thanks, never coded for 360 :(.

I just used fromfile in my map editor.

[Edited by - FOOLVER on September 15, 2009 4:54:40 AM]
-----I am working on: xna isometric online rpg
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.
Stretching is going to cause problems unless you do it correctly. Using a rendertarget might help with that.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

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.

GDNet+. It's only $5 a month. You know you want it.

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
-----I am working on: xna isometric online rpg

This topic is closed to new replies.

Advertisement