Sign in to follow this  
SonicD007

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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. Edited by lwm

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='SonicD007' timestamp='1344095398' post='4966120']
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?
[/quote]

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.

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