Detect RGB vs RGBA in loaded texture

How can i find out if a texture loaded from an PNG via the asset loader is in RGB or RGBA format?

I tried looking at the “textureFormat” (material.opacityTexture.textureFormat) but it seems to always return -1 in my use case. Should this work? Is there any other way to check?

The use case is to support both grayscale RGB images and proper RGBA opacity maps by automatically setting the getAlphaFromRGB when there isn’t any alpha channel in the image.

I don’t see a way to get this information and I’m not sure why the format and type are both -1 in my testings (Babylon.js Playground)…

Maybe @sebavan or @Deltakosh will know more…

A big thanks for verifying.

Perhaps I should mention that this is again about legacy data coming from an API, hence why I can’t be sure of the actual format on beforehand.


The only idea that I have is to draw the texture on the canvas, and then you can read pixel data
HTML canvas getImageData() Method and figure out some logic what to do with that.

I did not test this, but I assume that if there is no alpha channel in the image[3]
would return undefined or something

It is not ideal solution performance-wise, but it might work.

Thanks for the idea Nogalo. But if I will go that route I can also access the raw data, either via Babylon if possible, or load the image via a side channel, and check the actual pixel values that way.

I just find it odd that the information from the PNG decoder isn’t exposed via textureFormat, so I thought I might be doing something wrong.

@sebavan or @Deltakosh, did you have any idea on why the textureFormat is returning -1?

png will always load as RGBA in babylon by default :slight_smile:

except if you force the format manually in the texture constuctor.

Thank you for clearing it up @sebavan. :+1:t2: