# Unable to use stencil buffer in combination with DepthTexture

**URL:** <https://discourse.threejs.org/t/unable-to-use-stencil-buffer-in-combination-with-depthtexture/74072>\
**Category:** Questions\
**Tags:** shaders\
**Created:** [November 16, 2024, 12:03am UTC](https://discourse.threejs.org/t/unable-to-use-stencil-buffer-in-combination-with-depthtexture/74072 "2024-11-16T00:03:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maikeio](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/maikeio/32/22948_2.png) [@Maikeio](https://discourse.threejs.org/u/Maikeio)\
**Post date:** [November 16, 2024, 12:03am UTC](https://discourse.threejs.org/t/unable-to-use-stencil-buffer-in-combination-with-depthtexture/74072/1 "2024-11-16T00:03:18Z")

</div>

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).

> **[DepthTexture disables stencil buffer - JSFiddle - Code Playground](https://jsfiddle.net/c814ogab/2/)**
>
> JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.

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](https://discourse.threejs.org/t/apply-depth-buffer-from-render-target-to-the-next-render/37276/3) 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.

---

<div class="post-metadata">

**Author:** ![TarczaBoga](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@TarczaBoga](https://discourse.threejs.org/u/TarczaBoga)\
**Post date:** [January 9, 2025, 1:31am UTC](https://discourse.threejs.org/t/unable-to-use-stencil-buffer-in-combination-with-depthtexture/74072/2 "2025-01-09T01:31:57Z")

</div>

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:

```javascript
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.
