Sign in to follow this  
REspawn

[.net] Create a texture using a seperate texture as the alpha

Recommended Posts

Hi, I have been trying to find out how i would create a texture with an alpha channel from two textures. Texture a would be the normal texture and texture b would be the alpha channel. A+B = C kinda thing. I tried locking the textures and placing them in the new texture but only ended with with a kinda ghost effect. Thanks for any help, -Dave

Share this post


Link to post
Share on other sites
So you want to keep them as separate textures and combine at rendering time, or do you want to actually replace the A's alpha channel with B's? The former can be done with pixel shaders (or the stencil buffer on some cards I think); for the latter the simplest way should be to lock the two surfaces and set A's alpha value = B's, pixel by pixel.

Share this post


Link to post
Share on other sites
Yea i have two textures, and i want to combine them into a third.
Texture one will the the image.
Texture two will be the alpha.
Texture three will be the combination of these.


Basically im trying to find out how i can add an alpha channel to an exsisting texture.

Thanks for the help, sorry if my explaination of what im trying to do is a bit left of center,
-Dave

Share this post


Link to post
Share on other sites
Create a 32-bit surface (Format.X8R8G8B8 or Format.A8R8G8B8).

Basically, for each pixel, you do
clr = surfaceA.GetPixel(x, y);
alpha = surfaceB.GetPixel(x, y).A;
clr = Color.FromArgb(alpha, clr.R, clr.G, clr.B);
surfaceC.SetPixel(x, y, clr);

This is very slow, though. It's better to use unsafe pointer operations but that code would be too long here. If you want, I've got wrapper classes that handle this (for both Bitmaps and Surfaces) - you use pretty much the same code as above but it's many times faster.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jonas B
Create a 32-bit surface (Format.X8R8G8B8 or Format.A8R8G8B8).

Basically, for each pixel, you do
clr = surfaceA.GetPixel(x, y);
alpha = surfaceB.GetPixel(x, y).A;
clr = Color.FromArgb(alpha, clr.R, clr.G, clr.B);
surfaceC.SetPixel(x, y, clr);

This is very slow, though. It's better to use unsafe pointer operations but that code would be too long here. If you want, I've got wrapper classes that handle this (for both Bitmaps and Surfaces) - you use pretty much the same code as above but it's many times faster.


You an avoid unsafe code by using locking the surface, getting the pointer, and then using Marshal.Copy() to copy into managed byte arrays. It's not as efficient of course, but is better overall.

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