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


Don't miss the next tutorial!

There's a new post every Wednesday: leave your email to be notified!


Ways to Support

In the past months I've been dedicating more and more of my time to the creation of quality tutorials, mainly about game development and machine learning. If you think these posts have either helped or inspired you, please consider supporting this blog.

PatreonBecome a Patron Oo4th_patreon_name
PaypalDonate on PayPal
Twitter_logoFollow on Twitter

Write a Comment

Comment

Webmentions

  • LCD Display Shader Effect – Alan Zucconi March 11, 2017

    Glad you have solved it! 😀

  • Developer I/O » Impossible Geometry: Non-Euclidean Cubes March 11, 2017

    Glad you have solved it! 😀