Stencil testing

I’ve been playing about with stencil testing, but can’t seem to get it to work properly when not clearing the stencil buffer between frames (I’m trying to use the stencil buffer from the previous frame, and then clearing it in the middle of a frame).

https://www.babylonjs-playground.com/debug.html#LT9C8Q#2

This pg works fine if the render order is switched, but causes the plane to rapidly flash as is.

@sebavan would you be able to answer this?

This is expected as if you draw out of order, the cube can not write anymore to the depth and stencil info where the plane has been drawn so the frame after will not have stencil info and will not be able to draw the stickers then the cube can fill the buffers and the stickers appears preventing the cube to draw and so on…

You need to render in order for this use case.

1 Like

Thanks for the explanation, I knew there must be something I was overlooking.

Given that the cube is failing the depth test every second frame, I can achieve what I was going for by setting:
e.setStencilOperationDepthFail(BABYLON.Engine.REPLACE); as here.
The stencil is obviously a frame behind, but that can’t be helped without drawing the meshes in order.

1 Like