# Shader material for instanced meshes drawing meshes depth wrong

**URL:** <https://forum.babylonjs.com/t/shader-material-for-instanced-meshes-drawing-meshes-depth-wrong/21399>\
**Category:** Questions\
**Tags:** shader\
**Created:** [June 4, 2021, 5:24am UTC](https://forum.babylonjs.com/t/shader-material-for-instanced-meshes-drawing-meshes-depth-wrong/21399 "2021-06-04T05:24:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcubed](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mcubed/32/15602_2.png) [@mcubed](https://forum.babylonjs.com/u/mcubed)\
**Post date:** [June 4, 2021, 5:24am UTC](https://forum.babylonjs.com/t/shader-material-for-instanced-meshes-drawing-meshes-depth-wrong/21399/1 "2021-06-04T05:24:11Z")

</div>

[https://playground.babylonjs.com/#GTRC2Q#61](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](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).

---

<div class="post-metadata">

**Author:** ![mcubed](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mcubed/32/15602_2.png) [@mcubed](https://forum.babylonjs.com/u/mcubed)\
**Post date:** [June 4, 2021, 5:32am UTC](https://forum.babylonjs.com/t/shader-material-for-instanced-meshes-drawing-meshes-depth-wrong/21399/2 "2021-06-04T05:32:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 4, 2021, 10:35am UTC](https://forum.babylonjs.com/t/shader-material-for-instanced-meshes-drawing-meshes-depth-wrong/21399/3 "2021-06-04T10:35:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mcubed](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mcubed/32/15602_2.png) [@mcubed](https://forum.babylonjs.com/u/mcubed)\
**Post date:** [June 4, 2021, 2:33pm UTC](https://forum.babylonjs.com/t/shader-material-for-instanced-meshes-drawing-meshes-depth-wrong/21399/4 "2021-06-04T14:33:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 4, 2021, 2:36pm UTC](https://forum.babylonjs.com/t/shader-material-for-instanced-meshes-drawing-meshes-depth-wrong/21399/5 "2021-06-04T14:36:39Z")

</div>

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

[https://playground.babylonjs.com/#GTRC2Q#64](https://playground.babylonjs.com/#GTRC2Q#64)

---

<div class="post-metadata">

**Author:** ![mcubed](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mcubed/32/15602_2.png) [@mcubed](https://forum.babylonjs.com/u/mcubed)\
**Post date:** [June 4, 2021, 3:24pm UTC](https://forum.babylonjs.com/t/shader-material-for-instanced-meshes-drawing-meshes-depth-wrong/21399/6 "2021-06-04T15:24:59Z")

</div>

oh, hah, duh! thank you, you would be correct 😃
