# Global Depth Buffer zOffset

**URL:** <https://forum.babylonjs.com/t/global-depth-buffer-zoffset/51292>\
**Category:** Questions\
**Created:** [June 13, 2024, 8:39am UTC](https://forum.babylonjs.com/t/global-depth-buffer-zoffset/51292 "2024-06-13T08:39:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dennemark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dennemark/32/10745_2.png) [@dennemark](https://forum.babylonjs.com/u/dennemark)\
**Post date:** [June 13, 2024, 8:39am UTC](https://forum.babylonjs.com/t/global-depth-buffer-zoffset/51292/1 "2024-06-13T08:39:40Z")

</div>

Hi,  
I have another webgl renderer on my canvas (maplibre) that is setting the depth buffer. afterwards I render babylonjs on top. However this leads to some z-fighting and I am wondering if I can avoid it by setting some kind of global zOffset on the depth buffer before rendering with babylonjs.  
I found engine.depthCullingState.zOffset, but this has no effect.

Moving the babylon camera a bit back helps, but I would still prefer a better way.  
Maybe @Evgeni_Popov knows this?

---

<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 13, 2024, 9:38am UTC](https://forum.babylonjs.com/t/global-depth-buffer-zoffset/51292/2 "2024-06-13T09:38:37Z")

</div>

We don’t have such function, but you can do something like this (line 2-5):

> **[Babylon.js Playground](https://playground.babylonjs.com/#JBO2C5)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![dennemark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dennemark/32/10745_2.png) [@dennemark](https://forum.babylonjs.com/u/dennemark)\
**Post date:** [June 13, 2024, 9:43am UTC](https://forum.babylonjs.com/t/global-depth-buffer-zoffset/51292/3 "2024-06-13T09:43:54Z")

</div>

Thanks for the reply!  
I don’t think that setting zOffset will move the depth buffer by a certain value. It does not have an effect on my z-fighting issue.  
Is there any way I could access the depth buffer and move try to increase it?  
Not sure if it is a good idea though…

---

<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 13, 2024, 9:53am UTC](https://forum.babylonjs.com/t/global-depth-buffer-zoffset/51292/4 "2024-06-13T09:53:29Z")

</div>

`zOffset` is meant to fix z-fighting issues. Try smaller values than -10 (-100 / -1000). You can also try to override `engine.setZOffsetUnits`. See [glPolygonOffset](https://registry.khronos.org/OpenGL-Refpages/gl4/html/glPolygonOffset.xhtml) for more information about `zOffset` / `zOffsetUnits` (the former corresponds to `factor` in this doc, and the latter to `units`). I don’t think you can access the depth buffer, as maplibre probably renders directly to the canvas, so the depth is a framebuffer and not a texture.

---

<div class="post-metadata">

**Author:** ![dennemark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dennemark/32/10745_2.png) [@dennemark](https://forum.babylonjs.com/u/dennemark)\
**Post date:** [June 13, 2024, 9:55am UTC](https://forum.babylonjs.com/t/global-depth-buffer-zoffset/51292/5 "2024-06-13T09:55:53Z")

</div>

Hm seems like I have to stick to moving the camera or its perspective matrix then.

The zOffsetUnits work well on materials, I can see how their meshes move away from the map. But if I do it globally there is no effect.

Thanks though!
