Learning about the Stencil buffer

An experiment I was able to produce whilst learning about stencil buffers in the context of writing graphics shaders. The way this effect works is by having both the sphere and a cube at the same position in the world. Then two quads moving horizontally towards each other in front of the camera.

When the quads are on their own they present the camera with a view through a single stencil buffer layer (the sphere), but when they overlap, we start to see the next stencil buffer layer (the cube).

The rest is simply a bit of animation to make the effect a bit more dynamic 🙂

I’ve found this technique quite interesting, and I’m sure will be useful in game projects I make, such as portals, or optical illusions.



A further experiment allowed me to create this cube effect, which seems hollow with different content from each side, again this is produced through a quad in front of the cube’s face that draws an inner cube and object, that are only visible on a specific stencil buffer layer. I was inspired by a similar effect in the game Antichamber, which is one of my all time favourites.