How to clear depth buffer to 0 in the middle of a multipass rendering pipeline?

Hello, I have a multipass rendering pipeline setup in Three.js, and you can easily clear the depth in the middle of rendering into new render target by calling gRenderer.state.buffers.depth.setClear(0); I am wondering how do I achieve the same thing in Babylonjs? I tried calling gl.clearDepth(0.0) but it doesn’t work.

To put it in context, I want to render a mesh with depthFunction=GEQUAL. Currently nothing gets drawn as I think depth is default to be 1.0 so everything is smaller than 1.0 and gets rejected, which is why I need to clear depth to be 0.0 for depth testing.

Welcome aboard!

We currently have no way of defining the value used to clear the depth buffer.

However, if you want to use 1 as the clear value, perhaps you’re using a reverse depth buffer and want to set engine.useReverseDepthBuffer = true?

When the reverse depth buffer is used, the default depth function is set to GEQUAL and the depth buffer is cleared with 1.

2 Likes

Thank you for response! I actually managed to do it by calling mesh.onBeforeRenderObservable.add(() => {
const gl = scene.getEngine()._gl;
gl.clearDepth(1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
});
when the mesh’s material has a depthFunction = GEQUAL.

I have another question with this multipass pipeline. It’s a bit unrelated to the current question so please let me know if you want me to open another topic.

Question One: Is there a way to assign render target to shader material uniforms as the final pass with multipass rendering? Or do we have to use the PostProcess to do the final rendering pass. When I use shader material, I couldn’t get it to show up, the screen always shows me the render target output.

Question Two: Currently I’m using the PostProcess, and I assign uniform by calling postprocess.onApply(effect) => {effect.setXXX()}. This so far works for most of the uniforms I need, but shaderMaterial has a setUniformBuffer() function that works for the non-multipass rendering. I tried to use effect.bindUniformBuffer() but it seems to not be equivalent and I get a “It is undefined behaviour to use a uniform buffer that is too small.” error.

Any insight into the above questions are appreciated!

I’m not sure to understand question 1, but maybe using another camera and setting camera2.outputRenderTarget = yourRenderTarget is what you want? Else, a repro in the Playground would help.

Regarding the second question, doing shaderMat.setUniformBuffer(name, ubo) should work, you should not need to bind it in onApply.