November 9, 2025, 1:51pm 1
Hello! I’m new to rendering stuff and have programmed only basic shaders before, so please keep that in mind.
What I’m trying to achieve (In case stencil buffer is the completely wrong approach) Let’s say we have a Mesh A and it’s completely rendered in white. Now let’s say we have a Mesh B that is a bit closer. I want it to “see” what’s behind it and render in the opposite color. So, let’s say it completely overlaps with Mesh A. Thus, it renders completely black. And last but not least, I have Mesh C, which is the closest mesh. Mesh C overlaps a little bit with Mesh B as well as Mesh A. I want the parts that overlaps with A to be black, and the parts that overlap with B to be white.
What I tried so far
- Use `uniform sampler2D sc…
November 9, 2025, 1:51pm 1
Hello! I’m new to rendering stuff and have programmed only basic shaders before, so please keep that in mind.
What I’m trying to achieve (In case stencil buffer is the completely wrong approach) Let’s say we have a Mesh A and it’s completely rendered in white. Now let’s say we have a Mesh B that is a bit closer. I want it to “see” what’s behind it and render in the opposite color. So, let’s say it completely overlaps with Mesh A. Thus, it renders completely black. And last but not least, I have Mesh C, which is the closest mesh. Mesh C overlaps a little bit with Mesh B as well as Mesh A. I want the parts that overlaps with A to be black, and the parts that overlap with B to be white.
What I tried so far
- Use
uniform sampler2D screen_texture : hint_screen_texture;This didn’t work because thescreen_textureis only updated every frame. Because all the meshes render on the same frame, they’re not yet visible on thescreen_textureand all meshes get the samescreen_texture. - Use stencil buffer
This is the most promising option so far. As far as I understand it, I can edit the buffer each time a mesh is rendered, so unlike
screen_texture, each mesh will get the stencil buffer that was modified by the last mesh that was rendered. So, here’s what I came up with: Mesh A renders completely white and increments the buffer by 1. Mesh B renders the parts that have 1 written in the buffer black, the rest in white. Then, it increments the buffer again by 1. Mesh C renders the parts that have a 2 in white, the parts that have 1 in black, the parts that have 0 in white. This means we can just check whether the buffer is even or odd and go from there: 0->White 1-> Black 2->White 3->Black etc.
And finally, my question
After all this, I was stumped by one simple problem: Wait, how do I increment the buffer?
Because here’s the thing, as far as I understood everything, I interact with the buffer by writing stencil_mode at the beginning of my shader.
But stencil_mode doesn’t support incrementing. I also can’t read the buffer, and change it based on what I read. I can only either write to it, completely replacing what was there before, or compare it to some number. Which doesn’t help me as well because I need to check whether the buffer is even or odd, not whether it’s smaller or bigger than an arbitrary number.
I know this is quite a long text but I’ve been banging my head against this simple problem for quite a while now.