Problem loading textures with alpha

Hey everyone,
I have a RGBA targa texture and for the life of me cant get it to load the alpha channel.

This doesnt appear to work:
tex = loader.loadTexture(texPath)

and neither does:
tex = Texture()
tex.setAlphaFilename(Filename(texPath))
tex.read(Filename(texPath))

Is there something i’m missing? I always end up with only 3 components…

From what I gather, you will need 2 textures. One has the RGB values, the other is a grayscale image that holds the alpha values.

try something like:
tex = loader.loadTexture(texPath)
tex.setAlphaFilename(alphaTexPath)

You might not have to load two textures, but that seems to be an available option. Try explicitly setting the number of channels to 4 when you call read().

Not sure, but I think the call would look like:
tex = Texture()
tex.read(Filename(texPath), primaryFileNumChannels = 4)

try this instead:


tex = loader.loadTexture(texPath)
ts = TextureStage.getDefault()
ts.setMode(TextureStage.MModulate)
yourmodel.setTexture(ts, tex)

…you have to set it’s TextureStage so that it actually uses the apha data you stored in the TGA file.

check http://panda3d.org/manual/index.php/Texture_Blend_Modes for more examples.

Normally, you shouldn’t have to go through gymnastics to get the alpha channel from a texture; it should just be there when you load the texture. (You might have to do model.setTransparency(TransparencyAttrib.MAlpha) to see the effects of alpha, however.)

You can query whether the texture has been successfully loaded along with its alpha channel by calling tex.getNumComponents(). It should return 4 if it has an alpha channel.

If your 4-channel TGA file is being loaded with only 3 channels (components), then try converting the TGA file to some other format that supports alpha, such as TIFF or PNG. I have recently seen evidence that the TGA decoder we are using sometimes has difficulty finding the alpha channel.

David

In my code, I had to specifically enable transparency for the render path during program initialization, similar to what David mentions. In the 2d case:


aspect2d.setTransparency(1)

Setting for aspect2d will specifically enable transparency on all images in the 2d HUD render path. You could set it for any node you want, such as render2d (the 2d screen), render (the 3d camera scene), or any model node for more fine-grained control.

I’m successfully using alpha channels on both TIF and PNG textures with no problems.

Note that David uses TransparencyAttrib.MAlpha, whereas I simply use 1. They are numerically equivalent, but I don’t know if there’s a chance the value might change some time in the future. It might be safer to use the named constant.