# Reusing depth buffer of a RenderTarget

**URL:** <https://discourse.threejs.org/t/reusing-depth-buffer-of-a-rendertarget/3904>\
**Category:** Questions\
**Tags:** render-target, depth-texture\
**Created:** [August 26, 2018, 5:00pm UTC](https://discourse.threejs.org/t/reusing-depth-buffer-of-a-rendertarget/3904 "2018-08-26T17:00:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramirofages](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ramirofages/32/4253_2.png) [@ramirofages](https://discourse.threejs.org/u/ramirofages)\
**Post date:** [August 26, 2018, 5:00pm UTC](https://discourse.threejs.org/t/reusing-depth-buffer-of-a-rendertarget/3904/1 "2018-08-26T17:00:27Z")

</div>

Hi, I’m trying to do some special effects by rendering a few things in a color buffer, and then draw other things on a new color buffer but using the original depth buffer from the first render. Is it possible to do this?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [August 27, 2018, 10:17am UTC](https://discourse.threejs.org/t/reusing-depth-buffer-of-a-rendertarget/3904/2 "2018-08-27T10:17:24Z")

</div>

You could save the depth information in a texture and then reuse it for an arbitrary amount of effects. Many post-processing effects work like that. The following example illustrates this approach by visualizing the depth of the original scene.

[https://threejs.org/examples/#webgl\_depth\_texture](https://threejs.org/examples/#webgl_depth_texture)

---

<div class="post-metadata">

**Author:** ![ramirofages](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/ramirofages/32/4253_2.png) [@ramirofages](https://discourse.threejs.org/u/ramirofages)\
**Post date:** [August 27, 2018, 10:18pm UTC](https://discourse.threejs.org/t/reusing-depth-buffer-of-a-rendertarget/3904/3 "2018-08-27T22:18:30Z")

</div>

Yeah I came across that…but I’m trying to make this as efficient as possible, and if I use that texture I would have to sample it and make an explicit discard operation comparing to the current depth value instead of taking advantage of the early ztest. Thanks for your time anyway 🙂

EDIT: is it possible to manipulate the depth buffer of a render target?

---

<div class="post-metadata">

**Author:** ![tentone](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@tentone](https://discourse.threejs.org/u/tentone)\
**Post date:** [August 30, 2018, 8:23am UTC](https://discourse.threejs.org/t/reusing-depth-buffer-of-a-rendertarget/3904/4 "2018-08-30T08:23:21Z")

</div>

Yes it is possible using the EXT\_frag\_depth extension you can write values to the depth buffer in the fragmentshader in gl\_FragDepthEXT.

([https://developer.mozilla.org/en-US/docs/Web/API/EXT\_frag\_depth](https://developer.mozilla.org/en-US/docs/Web/API/EXT_frag_depth))

---

<div class="post-metadata">

**Author:** ![pailhead](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pailhead/32/85_2.png) [@pailhead](https://discourse.threejs.org/u/pailhead)\
**Post date:** [December 16, 2018, 10:02pm UTC](https://discourse.threejs.org/t/reusing-depth-buffer-of-a-rendertarget/3904/5 "2018-12-16T22:02:16Z")

</div>

Hi, i was able to achieve this by calling

```auto
_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );

```

and was able to use the same stencil to draw into two different targets. It’s really neat! But i can’t make it work with three.js. Has anyone been able to achieve this?

---

<div class="post-metadata">

**Author:** ![DolphinIQ](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/dolphiniq/32/3510_2.png) [@DolphinIQ](https://discourse.threejs.org/u/DolphinIQ)\
**Post date:** [April 25, 2022, 3:06pm UTC](https://discourse.threejs.org/t/reusing-depth-buffer-of-a-rendertarget/3904/6 "2022-04-25T15:06:22Z")

</div>

In case anyone finds this topic, it seems it has been solved in [Apply depth buffer from render target to the next render](https://discourse.threejs.org/t/apply-depth-buffer-from-render-target-to-the-next-render/37276) 😊
