in c#, shader, tutorial, Unity3D

Impossible Geometry: Non-Euclidean Cubes

Share Button

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.

antichamber

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).
    s1
  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.

Conclusion

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“.

📧 Stay updated

A new tutorial is released every week.

💖 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.

Paypal
Ko-fi
Twitter_logo

Write a Comment

Comment

11 Comments

  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: https://www.alanzucconi.com/2015/06/17/surface-shaders-in-unity3d/ .

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

Webmentions

  • Tutorial Series - Alan Zucconi February 7, 2018

    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: https://www.alanzucconi.com/2015/06/17/surface-shaders-in-unity3d/ .

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

  • LCD Display Shader Effect – Alan Zucconi February 7, 2018

    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: https://www.alanzucconi.com/2015/06/17/surface-shaders-in-unity3d/ .

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

  • Developer I/O » Impossible Geometry: Non-Euclidean Cubes February 7, 2018

    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: https://www.alanzucconi.com/2015/06/17/surface-shaders-in-unity3d/ .

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