Stencil doesn't work when using RenderTarget with calling createDepthStencilTexture

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)

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)

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

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:

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.

1 Like

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

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

render target stencil | Babylon.js Playground (babylonjs-playground.com)

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

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

My bad, I’m sorry I didn’t notice the console
It’s working now render target stencil | Babylon.js Playground (babylonjs-playground.com)

Thank you again~

2 Likes