Shader material for instanced meshes drawing meshes depth wrong

https://playground.babylonjs.com/#GTRC2Q#61

I have this playground setup with a Fragment/Vertex shader, the vertex shader appears to be working as desired. However, for some reason, some of the boxes are being drawn behind the others when their positions in the world are in fact in front of others. I’m not sure what is going on here.

the same example using a NodeMaterial is working https://playground.babylonjs.com/#M38M3I#20

I would like to use the NodeMaterial but I need to adjust the texture per instance so I need to rely on a raw shader and instance buffers to pass in the scale and position of the texture (unless there is a way to do that with the NodeMaterial but I was not able to figure it out).

OH looks like I just needed to add shaderMaterial.needDepthPrePass = true and now everything is working as desired!

Welcome aboard!

You should set needAlphaBlending to false as your material is not transparent. Setting it to true will lead to the rendering problems you experienced because the objects are simply sorted and don’t use the zbuffer anymore.

I ended up needing alphaMode = 0 to disable the alpha OR the needDepthPrePass = false the needAlphaBlending looks to be just a getter function on the ShaderMaterial.

I was speaking of the needAlphaBlending property you pass to the ShaderMaterial constructor:

https://playground.babylonjs.com/#GTRC2Q#64

oh, hah, duh! thank you, you would be correct :smiley: