Function Repository Resource:

ChessVoxels

Source Notebook

Graphics3D primitives for a set of voxelated chess pieces

Contributed by: Bradley Klee

ResourceFunction["ChessVoxels"][piece]

represents a voxel chess piece centered at {x,y}={0,0} with base height z=0.

ResourceFunction["ChessVoxels"][piece,{x,y,z}]

represents a voxel chess piece centered at {x,y} with base height z.

ResourceFunction["ChessVoxels"][piece,{x,y,z},θ]

also rotates the piece counter-clockwise around the vertical by θ radians.

ResourceFunction["ChessVoxels"][piece,{x,y,z},θ,scale]

also scales linear dimensions by a factor scale.

ResourceFunction["ChessVoxels"][piece,{x,y,z},θ,scale,v]

also rotates the piece vertical vector from {0,0,1} to v.

ResourceFunction["ChessVoxels"][]

lists allowable values for the input piece.

ResourceFunction["ChessVoxels"]["StandardGame"]

gives inputs for setting up a standard chess game on an 8×8 board.

Details

Voxels are initially scaled to be size (1/8)3 so that each piece fits squarely on a unit square, by default[0,1]×[0,1] at vertical height 0. Setting scale=8 obtains unit-edge voxels if necessary.
Each piece is returned as a two-element list of graphics primitives, which allows pieces to be given two-tone coloring styles (see examples).
The "StandardGame" return separates pieces by player, i.e. has the form: {player1,player2}={{p11,p12,…},{p21,p22,…}} with pxy being the yth piece of player x.

Examples

Basic Examples (5) 

Obtain the primitive Polyhedron objects for a chess Knight:

In[1]:=
ResourceFunction["ChessVoxels"]["Knight"]
Out[1]=

Plot the Knight at the origin:

In[2]:=
Graphics3D[ResourceFunction["ChessVoxels"]["Knight"]]
Out[2]=

Add two-tone color style:

In[3]:=
Graphics3D[
 Riffle[{Orange, Yellow}, ResourceFunction["ChessVoxels"]["Knight"]]]
Out[3]=

Plot all pieces using isomorphic perspective:

In[4]:=
GraphicsRow[Graphics3D[Riffle[
     {Orange, Yellow}, ResourceFunction["ChessVoxels"][#]],
    PlotRange -> {{0, 1}, {0, 1}, {0, 2.5}},
    SphericalRegion -> False, Boxed -> False,
    ViewPoint -> {Infinity, Infinity, Infinity},
    ViewVertical -> {0, 0, 1}
    ] & /@ ResourceFunction["ChessVoxels"][]]
Out[4]=

Arrange the chess pieces on a board in the usual way:

In[5]:=
Graphics3D[Join[
  Table[{If[Mod[i + j, 2] == 1,
     Lighter[#, .6] &@Orange,
     Lighter[#, .6] &@Yellow],
    Cuboid[{i, j, -1/2}, {i + 1, j + 1, 0}]},
   {i, 0, 7}, {j, 0, 7}],
  {EdgeForm[Opacity[0]],
   MapThread[Function[{colors, pieces},
     Riffle[colors, #] & /@ pieces
     ], {{{Yellow, Orange}, {Orange, Yellow}},
     Map[ResourceFunction["ChessVoxels"] @@ # &,
      ResourceFunction["ChessVoxels"]["StandardGame"], {2}]
     }]
   }],
 ViewPoint -> {Infinity, Infinity, Infinity},
 ViewVertical -> {0, 0, 1}, Boxed -> False,
 ImageSize -> 500]
Out[5]=

Scope (2) 

Pieces can be scaled to smaller or larger sizes:

In[6]:=
Graphics3D[
 Riffle[{Orange, Yellow}, ResourceFunction["ChessVoxels"]["Knight", {0, 2 #, 0}, 0, #]] & /@ {1/2, 1, 2},
 Boxed -> False]
Out[6]=

Pieces can be rotated in three dimensions by setting a vertical vector:

In[7]:=
Graphics3D[{Lighter[Cyan, .7], Cuboid[{0, 0, -1/2}],
  Riffle[{Lighter[Cyan, .5], Lighter[Purple, .2]},
   ResourceFunction["ChessVoxels"]["Pawn", 1/2*{0, 0, -1}, Pi/2, -1]],
  Riffle[{Lighter[Cyan, .5], Lighter[Purple, .2]},
   ResourceFunction["ChessVoxels"]["Pawn", 1/2*{0, 0, 1}, 0, 1, {0, 0, 1}]],
  MapThread[Riffle[{Lighter[Cyan, .5], Lighter[Purple, .2]},
     ResourceFunction["ChessVoxels"]["Knight", 1/2*#2, #1, 1, #2]] &,
   {{0, Pi, -Pi/2, Pi/2}, {{0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}}}
   ]}, ViewPoint -> {Infinity, Infinity, Infinity}, ViewVertical -> {0, 0, 1},
 Boxed -> False]
Out[7]=

Neat Examples (1) 

Put the chess pieces in a scene with a non-trivial height map:

In[8]:=
With[{heightMap = Floor[{
       {3, 3, 2, 4, 4, 4, 4, 4}, {3, 2, 2, 2, 5, 5, 5, 6},
       {3, 3, 2, 5, 5, 6, 6, 6}, {7, 7, 8, 6, 6, 13, 13, 6},
       {7, 7, 8, 6, 6, 11, 13, 13}, {9, 7, 8, 11, 11, 11, 12, 13},
       {9, 8, 8, 11, 10, 12, 12, 12}, {9, 9, 9, 10, 10, 10, 10, 12}}/
      2]/2},
 Graphics3D[Join[Table[
    {If[Mod[i + j, 2] == 1, Lighter[#, .6] &@Orange, Lighter[#, .6] &@Yellow],
     Cuboid[{i, j, -1/2}, {i + 1, j + 1, heightMap[[i + 1, j + 1]]}]
     }, {i, 0, 7}, {j, 0, 7}], {EdgeForm[Opacity[0]],
    MapThread[
     Function[{colors, pieces}, Riffle[colors, #] & /@ pieces
      ], {{{Yellow, Orange}, {Orange, Yellow}},
      Map[
       ResourceFunction["ChessVoxels"] @@ ReplacePart[#, 2 -> Plus[
            #[[2]], {0, 0, heightMap[[Apply[Sequence,
                #[[2, 1 ;; 2]] + {1, 1}]]]}]] &, ResourceFunction["ChessVoxels"]["StandardGame"], {2}]}]}],
  ViewPoint -> {-Infinity, -Infinity, Infinity}, ViewVertical -> {0, 0, 1}, Boxed -> False,
  ImageSize -> 500]]
Out[8]=

Publisher

Brad Klee

Version History

  • 1.0.0 – 09 December 2022

Related Resources

License Information