Albedo texture has alpha getting reset when texture is replaced

Playing around with loading higher res textures after initial load and ran across an issue as can be seen in this PG: Babylon.js Playground. When I replace a texture that has alpha, it looks like the “albedo texture has alpha” switch gets flipped to false. You can see this in the PG, if you comment out the call to replace texture, then the lenses have alpha, as you would expect. But when I replace the lens albedo texture with the same texture it was using, the “albedo has alpha” switch is set to false, and the lenses are opaque. Is this expected behavior when replacing textures, i.e. do I need to detect the material albedo alpha setting prior to replacing the texture and reset it?

Pinging @sebavan

The hasAlpha property of the Texture class is not updated based on the texture itself: it’s up to the user to set this property to true (it is false by default) if he wants to use the alpha channel of the texture (in case it has one).

So, it’s the expected behaviour, when you load a new texture, the hasAlpha property gets reset to false.

1 Like

Okay, we can grab the “albedo has alpha” state of the material before setting the texture and make sure it is set back. Are there any other properties on materials that are reset when a texture in albedo or other channels?

You should also look at “transparencyMode” which can also affect alpha.

transparencyMode is a property on the material itself, so is not changed if you change a texture.

@Eric_Wood All the texture properties are set to their default values if you replace a texture by another one (you create a new texture instance, so it’s expected that the values from the instance you are replacing are not retained). You should look at the doc of the Texture class to know the properties that could be changed by the user and that you would want to retain the value in your use case.

Gotcha. So it is safe to assume that none of the properties on Texture will get set when the texture is loaded via TextureAssetTask?

All properties that are writeable by the user are not set when loading / creating a texture, except that they still get default values (so, in fact, they are set…). Also, some of those properties can get non default values if you provide them when calling the constructor (for eg, you can pass a value for the samplingMode, and this will be the value of the corresponding property when the texture is created).