Render order and textures with alpha

Seeing some weirdness when using a custom render order with textures that have alpha. The PG above has two planar meshes with textures that are .001 units separated in z. As you move the smaller plane in front of the larger plane, note how it is initially occluded by and then occludes the larger plane. This isn’t z-fighting; if you leave the plane where it is you will not see flickering, nor does it flicker while moving. I was able to reproduce this using planar meshes (version 1 of the above PG) with diffuse only color in 4.2.1 and concluded that the default front to back sort for render order was using distance from the camera rather than z position to determine sort order. In 5.10.0, this does not occur so it appears something was fixed in the render order sort. I fixed the issue in 4.2.1 using a custom render order that relied on the z positions to determine order. The issue came back; however, when I added textures to the meshes, specifically textures with alpha. After investigating I realized that the custom render order function was never being called when meshes had diffuse textures with alpha, i.e. hasAlpha on the textures (just setting hasAlpha to true suffices, you do not actually need to have any alpha in the texture) and you set useAlphaFromDiffuseTexture on the material. As you can see in the PG, the render order issue is present when textures have alpha in 5.10.0.

It happens that I need a custom render order function in any case, because I have some depth masks, so my primary issue is that the render order function is not called when meshes have textures with alpha. Is there a reason the render order is not applied for meshes that have textures with alpha? If this is expected behavior, and I can’t use a custom render order, how can I ensure that the meshes with textures render consistent with their z position and ensure that my depth masks render first?

According to the docs here Transparent Rendering | Babylon.js Documentation, using both hasAlpha and useAlphaFromDiffuseTexture result in the mesh being treated as alpha blended. In the Things Not to Do section it even describes the potential for the problem I am seeing. So is it accurate to say the custom render order functions are not used for alpha-blended meshes?

Is a way to achieve what I want to put my depth masks in their own rendering group to make sure they render before everything else and sort properly amongst themselves and then to use alpha index to ensure that all my meshes with transparency order correctly based on z position and not distance to camera of the bounding box center?

in setRenderingOrder you only use the first param which is for opaque meshes. You need to enforce it in the third param for transparent order:

1 Like

Well that makes sense. Thanks.