Overriding Stencil Test With Depth Test

Hey there - I’m using stencils to make these purple particles render behind a quad, but I had to check the “Disable Depth Test” option within the particles’ material to get them to render below my floor (since otherwise, the depth test would cull them).

The problem is that now I’m seeing the purple particles even behind objects such as the sphere in the video.

I know this is caused precisely by disabling the depth test, but I would still like to see the particles behind the “stencil-quad” when there’s a clear line of sight to it.

Explicitly speaking, I would like to output:

  • Stencil buffer = 0 if the quad is not hit by the camera raycast;
  • Stencil Buffer = 1 if the quad is directly hit by the camera raycast, therefore passing both the stencil and depth test,;
  • and Stencil Buffer = 0 if the quad hit by the stencil test during the raycast, but is obstructed by another object in the depth test’s raycast.

Which I think sums up to:

  • I want the make the depth test for my particles to take priority over their stencil test.

How would I go about doing this? Or is there perhaps a simpler solution to my problem?


Here are two screenshots from the engine - one with my framebuffers and another with my material setup for the particles.

I hope this is enough information, otherwise, I’d gladly provide more insight.

I’m not sure if this is the best approach to solving it, but I added a custom depth write to my sphere, which makes it forcefully write “0” into the stencil buffer.

Now I don’t get the aforementioned bug with the particles being rendered behind other objects, but I feel like there should be a better/easier way to figure this out?

Write to stencil buffer only on passing depth test.
Use stencil value later with depth test enabled for selective rendering.

At Stencillo, we blend the beauty of nature with the precision of artistry. Our store features a wide range of stencils, calligraphy tools, and nature-themed canvases designed to inspire creativity. Whether you’re passionate about capturing the grace of birds or the serenity of trees, Stencillo provides the tools you need to create stunning works of art that resonate with the beauty of the natural world.

Discover the magic of art with Stencillo, your one-stop shop for exquisite stencils, elegant calligraphy sets, and breathtaking nature canvases. Our products are crafted to inspire and elevate your artistic journey, offering intricate designs of trees, birds, and natural landscapes. Perfect for artists of all levels, Stencillo helps you bring your imagination to life with ease and elegance. https://stencillo.com/