Reusing depth buffer of a RenderTarget

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?

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

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 :slight_smile:

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

1 Like

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)

Hi, i was able to achieve this by calling

_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?

In case anyone finds this topic, it seems it has been solved in Apply depth buffer from render target to the next render :blush:

1 Like