TikZ: shaded cube

With just a few lines of code you can create a drawing of a cube with a 3D impression, though if it’s not really perspective or raytraced:

\begin{tikzpicture}[on grid]
  \shade[yslant=-0.5,right color=gray!10, left color=black!50]
    (0,0) rectangle +(3,3);
  \draw[yslant=-0.5] (0,0) grid (3,3);
  \shade[yslant=0.5,right color=gray!70,left color=gray!10]
    (3,-3) rectangle +(3,3);
  \draw[yslant=0.5] (3,-3) grid (6,0);
  \shade[yslant=0.5,xslant=-1,bottom color=gray!10,
    top color=black!80] (6,3) rectangle +(-3,-3);
  \draw[yslant=0.5,xslant=-1] (3,0) grid (6,3);

This example uses the positioning tikz library.

Output screenshot:

pgf/TikZ 3D cube

By adding some slanted numbers we will get a Sudoku 3D cube:

pgf/TikZ sudoku 3D

TeX source code and pdf output are provided.

I’ve programmed it to answer a question posted on matheplanet.de. At first I wanted to use the matrix tikz library but it seemed to me that matrices of nodes cannot be slanted, at least I didn’t find a way.

10. August 2008 by stefan
Categories: Uncategorized | 5 comments

Comments (5)

  1. Nice cube! Can I add it to the TikZ and PGF gallery?

  2. Of course, I’d be glad to contribute to your excellent gallery.

  3. Nice example, Stefan! It’s really amazing how you can get such fine graphics in TikZ with just a few lines of code. Now, the real challenge would be to solve this Sudoku puzzle in TikZ ;)

  4. Starting from your example (the idea of using yslant and xslant to achieve a 3D like appearance)
    I have developed an alternative visualization for Pasigraphy Rhapsody, https://gitlab.com/nbatfai/pasigraphy-rhapsody/blob/master/para/docs/prelpara.lua
    Some examples of usage can be found at https://tex.stackexchange.com/questions/29877/need-help-creating-a-3d-cube-from-a-2d-set-of-nodes-in-tikz/502070#502070

Leave a Reply

Required fields are marked *