# Stencil doesn't work when using RenderTarget with calling createDepthStencilTexture

**URL:** <https://forum.babylonjs.com/t/stencil-doesnt-work-when-using-rendertarget-with-calling-createdepthstenciltexture/38846>\
**Category:** Questions\
**Tags:** material\
**Created:** [March 8, 2023, 8:22am UTC](https://forum.babylonjs.com/t/stencil-doesnt-work-when-using-rendertarget-with-calling-createdepthstenciltexture/38846 "2023-03-08T08:22:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [March 8, 2023, 8:22am UTC](https://forum.babylonjs.com/t/stencil-doesnt-work-when-using-rendertarget-with-calling-createdepthstenciltexture/38846/1 "2023-03-08T08:22:26Z")

</div>

Hi guys  
I have two questions，thank you so much for any help  
Q1: I found that stencil doesn’t work when using RenderTarget with calling createDepthStencilTexture  
[render target stencil | Babylon.js Playground (babylonjs-playground.com)](https://www.babylonjs-playground.com/#B5P4DA#21)

uncomment createDepthStencilTexture, the stencil doesn’t work

Q2: I want render A in RTT-A，with stencil ref = 1，and render B in RTT-B with ref equal 1。how to share RTT-A’s stencil buffer  
[render target stencil | Babylon.js Playground (babylonjs-playground.com)](https://www.babylonjs-playground.com/#B5P4DA#23)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [March 8, 2023, 10:44am UTC](https://forum.babylonjs.com/t/stencil-doesnt-work-when-using-rendertarget-with-calling-createdepthstenciltexture/38846/2 "2023-03-08T10:44:12Z")

</div>

The default format (5th parameter of `createDepthStencilTexture`) is `TEXTUREFORMAT_DEPTH32_FLOAT`. You should pass a stencil capable format instead (like `TEXTUREFORMAT_DEPTH24_STENCIL8`):

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#B5P4DA%2326)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Regarding 2/, you can use the `RenderTargetWrapper._shareDepth` function to share the depth/stencil texture: you can’t share the stencil (or depth) only.

Here’s how to do it:

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#B5P4DA%2328)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

You need to override the RTT clearing (by adding an observer to `RTT.onClearObservable`) to clear the color buffer only, and not the depth/stencil.

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [March 8, 2023, 11:31am UTC](https://forum.babylonjs.com/t/stencil-doesnt-work-when-using-rendertarget-with-calling-createdepthstenciltexture/38846/3 "2023-03-08T11:31:46Z")

</div>

Awesome! You are incredibly professional! Thank you!!!

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [March 8, 2023, 12:25pm UTC](https://forum.babylonjs.com/t/stencil-doesnt-work-when-using-rendertarget-with-calling-createdepthstenciltexture/38846/4 "2023-03-08T12:25:10Z")

</div>

I have another problem when i use MultiRenderTarget, How are they different?

[render target stencil | Babylon.js Playground (babylonjs-playground.com)](https://www.babylonjs-playground.com/#B5P4DA#30)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [March 8, 2023, 2:01pm UTC](https://forum.babylonjs.com/t/stencil-doesnt-work-when-using-rendertarget-with-calling-createdepthstenciltexture/38846/5 "2023-03-08T14:01:57Z")

</div>

If you create a multi render target with 2 textures (as you do in the PG), you are supposed to write to the two textures in your fragment shader. Else an error is reported (see the browser console log):

```auto
[.WebGL-0000299008C47100] GL_INVALID_OPERATION: Active draw buffers with missing fragment shader outputs.

```

The standard/PBR materials write only to a single texture, so you will have to provide your own shader if you want to use a multi render target with a count \> 1.

---

<div class="post-metadata">

**Author:** ![newbeea](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/newbeea/32/29263_2.png) [@newbeea](https://forum.babylonjs.com/u/newbeea)\
**Post date:** [March 9, 2023, 2:12am UTC](https://forum.babylonjs.com/t/stencil-doesnt-work-when-using-rendertarget-with-calling-createdepthstenciltexture/38846/6 "2023-03-09T02:12:50Z")

</div>

My bad, I’m sorry I didn’t notice the console  
It’s working now [render target stencil | Babylon.js Playground (babylonjs-playground.com)](https://www.babylonjs-playground.com/#B5P4DA#31)

Thank you again~
