Instances depth/alphaIndex

Hi BJS team

I’m implementing a GIS (Geographic Information System).
In this GIS, there are several layers, each one using a unique depth. (ex: tiny roads, highway, railway line, …). A lot of work has already been done, BJS is a great tool :wink:

Each layer is represented by a single BJS Mesh. All of these meshes are rendered using custom GLSL shaders (custom AA lines, signed distance fields for texts, …). Some information is passed using VBOs (setVerticesData), other using TBOs (RawTexture in BJS). To order all of these layers, I modify the alphaIndex of the meshes and send appropriate uniform values. All of these stuff cohabit very well.

Now, I would like to duplicate part of my geometry using instances (for performance reason). For this, I’m using BJS 4.1.0 beta 1.

  1. To create instances, I call myMesh.createInstance().
  2. I fill some TBOs containing instance meshes related stuff (position, rotation, custom info, …)
  3. I fill an “instanced” VBO containing the instance ID using myMesh.instancedBuffers.objID (useful to fetch corresponding info in the TBOs).
  4. I configure meshes params:
    instance.alwaysSelectAsActiveMesh = true;
    instance.hasVertexAlpha = true;
    instance.doNotSyncBoundingInfo = true;

I managed to make instance rendering working. The instanciated meshes are drawn in the right place, with the right orientation, … However, I get some troubles with the depth of my instanciated meshes. Whatever the method I use, I can not set properly the depth of my instanciated meshes. I tried with uniforms, and even by forcing the depth in my shader.

I already use this way of thinking in OpenGL 3.3+ without any particular problem. Therefore, I’m wondering if there is a WebGL limitation or a BJS overlay that prevent me to do this kind of things ?


Well it seems to be defined correctly (but I would like to see a repro to be more helpful).

But keep in mind that instances are rendered in a batch so you cannot control the depth with alphaindex as they will be in the alphaindex of the root object

@Brett_Ridel - Welcome to the Babylon Family! Can’t wait to see your Babylon Creations!

Hi all

Thanks for the reply. I finally found the problems.

The first problem was in the computation of the alphaIndex of the meshes. The alphaIndex of one of the mesh was equal to nan, which disturbed the drawing order. Here is an result example of draw orders (using onBeforeDrawObservable). You can see that all meshes are well ordered locally before and after the mesh 08, but that the global order is wrong.

mesh 04 <==> alphaIndex = 31.12
mesh 06 <==> alphaIndex = 31.25
mesh 07 <==> alphaIndex = 31.29
mesh 09 <==> alphaIndex = 31.35
mesh 10 <==> alphaIndex = 31.38
mesh 11 <==> alphaIndex = 31.41
mesh 12 <==> alphaIndex = 31.45
mesh 13 <==> alphaIndex = 31.48
mesh 15 <==> alphaIndex = 31.54
mesh 08 <==> alphaIndex = NaN
mesh 01 <==> alphaIndex = 30.60
mesh 02 <==> alphaIndex = 30.73
mesh 03 <==> alphaIndex = 31.09
mesh 05 <==> alphaIndex = 31.22
mesh 14 <==> alphaIndex = 31.51
mesh 16 <==> alphaIndex = 31.61
mesh 17 <==> alphaIndex = 31.64

The second problem is that I can’t influence the depth of my meshes from the vertex shader if the attribute hasVertexAlpha is set to true. Here is a playground :

If you modify the z value (ex: 0.5 or -0.5) of the shader customGreenVertexShader, nothing happen. To make the red box in front or behind the green box, you have to modify the alphaIndex attributes. If you really want to influence the z order of the meshes, you have to comment the following lines.

  • redBox.hasVertexAlpha = true;
  • greenBox.hasVertexAlpha = true;

But transparency does not work anymore (I mean, I know that if the red box is draw after the green one, you can’t see the red box through the green one. This is not a problem. Here, transparency no longer works at all).


1 Like

I’m not sure if my previous message was clear, but the problem still remains. Is there a way to influence the z value directly from the vertex shader, without losing the alpha transparency ?


You can influence the depth in the vertex shader as you are responsible for writing the output
I will in this case recommend that you read the nme doc: