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.
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.
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.