Multiple custom shaders overlap

Hi, I am working with multiple meshes (some instanced and some not) and I am using a shader which makes them visible behind objects.
Made a small playground that represents what im trying to do.

Would anyone know why the non-instance shader is showing under the instance one? I already know about rendering groups and utility layers, but I would like to have both objects on the same layer.

This is all related to your custom management of the depth buffer.

As you disable it, last one drawn will be the visible one on screen.

You should not disable depth writes an depth tests.

1 Like

You need to declare zmin and zfar in the list of the uniforms of the custom shaders:

Note also that you don’t need to add defines: ["#define INSTANCES"],, the system will automatically add this define for instanced meshes. It means you don’t need two shaders, you can use the shader with instances (the one with #include<instancesDeclaration> / #include<instancesVertex>) for both cases.


Thank you both for the reply. Very useful tips.

I was curious why if i use a non instace custom shader on the box then the hierarchy behaves as expected.

Why if I move the original instance behind the plane then it also behaves as expected?

One more note, I tried using only one shader for both the instance and the plane and it doesn´t seem to work outside of the playground.

In transparent mode, instances of a mesh can be sorted from back to front by using the BABYLON.Mesh.INSTANCEDMESH_SORT_TRANSPARENT flag but they are not sorted with other transparent meshes: only the source mesh of the instances is sorted with the other meshes and the instances of this source mesh are displayed at the same time the source mesh is displayed (or would be displayed if the source mesh is not visible as in your PG). That’s why your 2nd PG works, because you move the source mesh behind the plane, at the instance location. It also explains why it works in non instanced mode, because in that case the sorting is correctly done among all transparent meshes.

I’m afraid you can’t do anything to fix that (except by not using instances), it’s how instances are working.

Note that in custom2VertexShader you should use viewProjection and not worldViewProjection, as the world matrix is already accounted for by the multiplication by finalWorld.

You need to add the world matrix in the list of the uniforms of the shader for the shader to work with non instanced meshes.


I am looking into other options for my case.
Still, very useful information. Good to know for future cases.