开发者

How do i color key, in c#, in xna?

I've started on a small game and downloaded some great 2d sprites from http://reinerstileset.4players.de. However, his background (unknown) is a brown color that I want to remove.

Is there a way tha开发者_开发技巧t I can check pixel at (1,1) and set that pixel as the colorkey for removal so that I don't get the objects' background color?


There is a property for the texture processor specifying the color for background transparency.

Select your sprite in the solution explorer (it should be in your content project), then look up the property inspector. You should see 'Content Processor: Texture - XNA Framework'. Expand that section, change Color Key Color as needed. Default value is magenta/bright pink (0xFF00FF).


You can create a content processor that will do this for you. Here is an example of a simple texture processor. This is for premultiplied alpha (which XNA 4.0 already does, but it was useful in 3.1).

You could easily modify it to read in the first pixel and clear all the pixels with matching values. Something like this:

    Color m = bitmap.GetPixel(0, 0);
    for (int y = 0; y < bitmap.Height; y++)
    {
        for (int x = 0; x < bitmap.Width; x++)
        {
            if (m == bitmap.GetPixel(x, y))
                bitmap.SetPixel(x, y, Color.Transparent);
        }
    }

Put this in a new Windows Library project, add it as a reference to your content project, build it once, and you should be able to select it from the processor options on the properties pane (F4) for your textures.

Of course, if all your sprites have the same background colour, jv42's answer is easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜