Is there a way to dynamically set a Texture2D's colorkey?

Started by
3 comments, last by SonicD007 11 years, 8 months ago
Hi, I've been working on a level editor and in that level editor, it allows you to change the color key of a system.drawing,image. Now I'm trying to move everything over to XNA and I was wondering if anyone knows of a way to set the color key for each texture2D during runtime? I was using imageattributes for the system drawing and each image would have it's own colorkey. How can I do this with XNA?
Advertisement
A Texture2D simply holds the data of the texture and knows nothing about the way it's going to be rendered. The texture's alpha channel is usually used to create transparency when using alpha blending. If you don't want to create your textures with an alpha channel, you can use the "Color Key Color" property of XNA's texture content processor to create the alpha channel at compile time.

If you absolutely want to define the key color at runtime, you could either use a custom pixel shader that compares the texture's color with your key color and discards the pixel if they are sufficiently similar or use GetData and SetData to create the alpha channel yourself.

current project: Roa

I've seen some code snippets with the setdata getdata method of doing this. Will this method be a costly process to do during runtime?

I've seen some code snippets with the setdata getdata method of doing this. Will this method be a costly process to do during runtime?


If you only use it once when loading the texture or changing the key color, it's probably going to be fast enough. You should avoid doing it every frame though.

current project: Roa

Alright thank you. I'm gonna try this approach at it.

This topic is closed to new replies.

Advertisement