Sign in to follow this  
arbell

Using alpha blending and colorkey transparency

Recommended Posts

Hi, I've been trying to solve the following problem... I am trying to render x amount of trees onto terrain. I want the trees to fade in/out relative to their distance from the camera. The trees use a texture that has transparent pixels (using the colorkey parameter in the TextureLoader.FromFile method). From the outset, there would seem to be a problem, simply because each method uses the alpha channel to determine transparency. As it turns out, I can't get the desired effect - I can only get one thing working at a time, ie. the fade in/out or the transparent part of the texture, not both. I have been setting the TextureStage[0] properties which changes between the two results. I have also tried using an alpha channel within the image file (through the DirectX Texture Tool), but this also does not work. Any suggestions on how to get these two effects working together?

Share this post


Link to post
Share on other sites
You just want to blend two alpha states. When you use the color key parameter, it simply sets the alpha value for that color anywhere in the texture to 0. If the image format supports alpha information, that will also be stored in the texture.

Now, if you apply the texture to your tree, it will look like a fully visible tree. You want to make the whole tree fade as the camera moves further away. There are two ways you can do this.

Either set every vertex DIFFUSE color's alpha component to a number proportional to the camera distance, or use T Factor or a CONSTANT value in the texture stage state.

The primary thing to do is to setup the texture stages. You will modulate the alpha values from the DIFFUSE component of the vertex with the alpha values from the TEXTURE component. I take it that you probably know how to do this, but if not, let me know and I'll post some code (or someone else will).

Share this post


Link to post
Share on other sites
Or, you can simply set the diffuse color's alpha component instead. For texture stage 0, set one arg to Current, the other to Diffuse, and set the op to Modulate. That will blend the texture's alpha with the diffuse alpha. :)

Share this post


Link to post
Share on other sites
Not quite. Current on stage 0 is a well defined "invalid" state. Invalid in that "current" tends to mean "from the previous stage", and stage 0 has no previous stage. Well defined in that it seems to mean "diffuse" on stage 0. Thus, modulating "current" by "diffuse" will give you diffuse squared. You probably want to modulate texture and diffuse. Some older cards are quite picky and want texture as arg1. Some other older cards (Rage Pro circa. 1998) require the color and alpha ops to be identical if the alpha is a modulate.

Thus, you probably want:

0, colorop, top_modulate
0, colorarg1, ta_texture
0, colorarg2, ta_diffuse // color = Texture * lighting
0, alphaop, top_modulate
0, alphaarg1, ta_texture
0, alphaarg2, ta_diffuse // alpha = texture * (vertex or material alpha)

1, colorop, top_disable
1, alphaop, top_disable // end

If you want your alpha in tfactor, change the alphaarg2 to tfactor. Again, the Rage Pro type cards don't get along with TFACTOR very well.

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