Unable to use stencil buffer in combination with DepthTexture

Hello everyone,

I am writing a post-processing shader that should be masked by a stencil buffer, but needs depth information. Unfortunately it seems impossible to combine these things on a render target. I created a fiddle for an example (line 50 to uncomment the DephTexture).

If it’s really impossible to combine the two, it should be stated somewhere.
And of course I’d be happy if someone could point me in the right direction to solve this problem.

Thanks in advance!

Edit:
I tried copying the depthBuffer directly instead of using a depthTexture.
But to do that I have to disable the dephtBuffer on the target I want to use the stencil on,
and it turns out that also brreaks the stencil buffer.

It seems like you need a dephtBuffer but no dephtTexture for the stencil to work.

2 Likes

I’m not sure if I understand your use-case completely, but you can use a stencil buffer along with a DepthTexture, as long as you use the DepthStencilFormat constant for the format value:

const depthTexture = new DepthTexture(width, height);
depthTexture.format = DepthStencilFormat;

This format requires 24 bits, so it won’t work with all ‘types’. However the default type of UnsignedIntType should work fine.

1 Like