Having issues loading textures with alpha channels from numpy array

I’m layering an alpha texture on top of non-alpha texture om my custom mesh. I have no issues with the texture with no alpha channel, but I can’t quite get the other one to work.

This is how I try to load it.

png_array = cv2.imread(path)

texture = Texture()
w, h, *_ = png_array.shape

texture.setup2dTexture(h, w, Texture.T_unsigned_byte, Texture.F_rgba)

buffer = png_array.astype(np.uint8)
texture.setRamImage(buffer, "RGBA")

This keeps crashing.
I’ve attached my test texture, just in case.

edge_template

There is no string argument as second argument to setRamImage. Did you mean setRamImageAs?

1 Like

Oops. Deleted the other post when i meant to edit my post, and cant see a way to restore.

Anyhow, I got it to load the image, but it isn’t loading on top of each other, rather it ignore the alpha all together. Did I do something wrong?

Screenshot from 2024-05-15 18-00-27

1 Like

How are you applying your texture?

And are you using the built-in shader-generator? (Or even the fixed-function pipeline?) Or are you using a PBR module, like simplepbr? (Or something else entirely…?)

If you’re using either the shader-generator or the fixed-function pipeline, and you’re not already doing the following, then I’d suggest looking at applying an appropriate texture mode.

See the manual for more, here:
https://docs.panda3d.org/1.10/python/programming/texturing/texture-modes

1 Like

Ah, right, everything is “right-out-of-the-box” shaderwise, but the mesh uses two sets of uv coordinates.
One for the base texture, and one for the edge texture. The edge texture is the texture attached at the beginning.

Here is how the texture stages look.

    def base_textue_stage() -> TextureStage:
        tx = TextureStage("base_tex")
        tx.setTexcoordName("base_tex")
        tx.set_priority(1)
        return tx
    def edge_textue_stage() -> TextureStage:
        tx = TextureStage("edge_tex")
        tx.setTexcoordName("edge_tex")
        tx.setMode(TextureStage.MDecal)
        tx.set_priority(0)
        return tx

The textures and the TextureStages works individually. Just not combined.

Edit:
Hmh, I’m reading it also ruins lightning data using blend or decal mode? Is that fixable via shader or similar? Would not be right for there to be no lighting on the edges of each tile.

Hmm… I’m not sure of why the textures so applied don’t show transparency, then.

That said, using a custom shader would… pretty much allow you to have the textures blend however you wanted. (Within the bounds of shaders, of course.) Complete with normal-mapping, should you so desire.

1 Like

Do you actually have transparency enabled on the object it is applied to?

1 Like

As in .setTransparency(TransparencyAttrib.MAlpha)? Thought it was just for if you wanted the object to be transparent.

Either way, No, but just tried setting it, doesn’t seem to work.

Edit:
Could be something to do with the texture loading. Will have to try with a texture loaded the conventional way.

Edit 2:
Yes, it was opencv2 that was the culprit. After following all of your advice AND changing to the way of loading textures from the manual, this was my result.

Which, is what I was looking for.

1 Like