I’m not sure how obj supports alpha, but at least one of the problems is that the texture doesn’t have alpha because it’s a jpg which can’t store alpha information.
@bghgary i’ve downloaded the files from Turbosquid and tried to import them on babylon without changing anything. I don’t know which is the engine installed on the Turbosquid website but it seems to work with jpeg files and transparent textures.
If we look inside the .mtl file, it has lines like: map_d gleditsia triacanthos flowers mask.jpg
According to this MTL OBJ materials file (paulbourke.net) reference, map_d refers to a “dissolve texture”.
map_d -options args filename
Specifies that a scalar texture file or scalar procedural texture file
is linked to the dissolve of the material. During rendering, the map_d
value is multiplied by the d value.
As it turns out, we do support this in the .obj loader. When I open the tree model in the Sandbox, I can see the material has the correct opacity map
But the leaves are still fully opaque.
If I open it up with the texture inspector and paint transparent pixels, then the material becomes transparent in those places
So the root of the problem is that we expect opacity textures to hold the opacity data in the alpha channel, but this model is using black/white to represent opacity instead.
Not sure if this is something we would want to address on the .obj loader side, or on the material side. @bghgary what do you think? Worth nothing that this model is broken in the exact same way in Blender.
The opacity map as @DarraghBurke points out is an RGB mask. Since .mtl files don’t specify how this works (at least not from what I can find online), Babylon just treats these as a luminance texture expecting the value to come from the alpha channel of the texture and defaults to alpha blend. There is a flag on the texture to switch this to RGB, opacityTexture.getAlphaFromRGB = true. There is a mode on material to set it to alpha mask material.transparencyMode = BABYLON.Material.MATERIAL_ALPHATEST. These both need to be set for this model to look right.
If I do this manually in the sandbox, then it looks like this:
If you are getting the model from TurboSquid, there should be an option to download a glTF right? glTF doesn’t have these problems since the spec is much more clear about how to render it.