in C#, Shaders, Tutorial, Unity

Impossible Geometry: Non-Euclidean Cubes

This tutorial will teach you how to create non euclidean cubes in Unity, giving the illusion that each face is a door onto another dimension. This post is part of a series of tutorials on impossible geometries.


This effect can be seen in many game, most notoriously Antichamber which uses it extensively.

You can download the Unity package here.

Step 1. Stencil Theory

For this example, all the geometries will be contained within the cube, at the same time. This particular effect is achieved using stencil buffer. The GPU offers a special buffer which can be used to mask and discard pixels. The idea behind non-euclidean cubes is strikingly simple and it requires two steps:

  1. Stencil mask: each face of the cube writes a specific value to the stencil buffer (yellow and green, in this example).
  2. Stencil geometry: the geometry that has to be shown in the yellow face only draws pixels where the stencil buffer is yellow.

All the objects exists at the same time, but their pixels are only drawn in the respective faces.

Step 2. The stencil mask

The first step is to create a material that initialises the stencil buffer with a specific value. To do this, we use a simple vertex and fragment shader (learn how to use them: Vertex and Fragment Shader). Even if in front of the geometry, is essential that this mask is drawn before the content of the cube. To do this, we change the Queue value to Geometry-100.

Lines 10-16 write into the stencil buffer. The line  Ref[_StencilMask] indicates that the value we want to write is provided in the property _StencilMask. Comp always indicates that the value has to be written regardless of what was on the stencil buffer already.

Step 3. The stencil geometry

What we need now is a material which draws only if the stencil buffer has been initialised accordingly to a specific value. This is done using the Comp equal condition.

The Stencil block in lines 13-18 can be easily added to many existing shaders, allowing to use virtually any material you want.

Step 4. Putting all together

To recap:

  • A standard cube structure;
  • Each face of the cube is a quad has a different material, which uses the StencilMask shader with a unique value for _StencilMask;
  • All the geometry pieces are inside the cube;
  • Each geometry piece has it’s own material, derived from StencilGeometry and initialised to match the  _StencilMask property of its respective face.

You can download the full Unity package here.


Become a Patron!

Non-euclidean cubes are always intriguing and can add an interesting twist to your game.

Despite the name, these cubes are actually euclidean. This technique works nicely for decorative elements, but it fails to provide an efficient solution to the portal effect seen in (guess!) Portal or Parallax. Future tutorials will cover other impossible geometries.

  • Part 1: Non-Euclidean Cubes
  • Part 2: The Portal Effect
  • Part 3: The Physics of Monument Valley

This tutorial is inspired by Noisecrime’s post called “Stencils for portal rendering“.

💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.


📧 Stay updated

You will be notified when a new tutorial is relesed!

📝 Licensing

You are free to use, adapt and build upon this tutorial for your own projects (even commercially) as long as you credit me.

You are not allowed to redistribute the content of this tutorial on other platforms. Especially the parts that are only available on Patreon.

If the knowledge you have gained had a significant impact on your project, a mention in the credit would be very appreciated. ❤️🧔🏻

Write a Comment



  1. I was trying to run it on mobile(Android) but it doesn’t work. I’ve searched for “Disable the Stencil and Depth” but it was unchecked and used OpenGLES3 as an API and deleted #pragma target 3.0. Do you know were I can search further?
    I’ve tested on Samsung Galsy S6 and LG G6.

  2. I’m using this trick to great effect already but I started wondering what I would do if I wanted to also render bits poking out of the square from angles other than directly in front. I thought it would be nice to somehow support this, but the more I look at it, the harder it seems.

    It’s like all I’d want to do is to also paint the stencil over bits of the model geometry which are a positive distance from a plane defined by that side of the cube (vertices behind would have negative distance), and there is a shader specifically solving that problem, but which requires knowing the plane in advance, which in my case I don’t because it’s always moving. So it’s like I’d have to have a place to stash the plane location between the runs of two different shaders. 🙁

    At the moment I’m compromising and have a manual switch where I have an additional cube of stencil mask textures in a cubic region in front, and try to turn it on manually, in a very careful way ensuring that nobody will notice how I’m cheating. (Basically, their eyes have to be within the cuboid formed by extending the normals of that face infinitely outwards. If they are not, they will see stencil over the edges of the frame, and bits of the object they shouldn’t be able to see.)

    • Hey!
      I don’t know if I’m understanding correctly. But what you can do is probably possible with a very simple NORMAL EXTRUSION shader.

      Basically, you can have a shader with two passes. The first one extrudes the geometry based on its normal, and does the stencil pass. If you want to learn more about normal extrusion, I’ve been talking about it here: .

      Alternatively, you can attach some quads 90 degress inside the faces. Perhaps rotating them with a script based on LookAt?


  • Tutorial Series - Alan Zucconi April 8, 2019

    So, for example, how would I use what you have shown me to create something like this ?
    As in, going through a “gate” can take you to another area. You can find this on 0:45 and 0:50 on the youtube video.

  • LCD Display Shader Effect – Alan Zucconi April 8, 2019

    So, for example, how would I use what you have shown me to create something like this ?
    As in, going through a “gate” can take you to another area. You can find this on 0:45 and 0:50 on the youtube video.

  • Developer I/O » Impossible Geometry: Non-Euclidean Cubes April 8, 2019

    So, for example, how would I use what you have shown me to create something like this ?
    As in, going through a “gate” can take you to another area. You can find this on 0:45 and 0:50 on the youtube video.