# Creating 3D dice

How to create an image of a 3D die from the 2D images?

There is an example in the Documentation Center

I tried

but it doesn't work.

-
– Dr. belisarius Mar 17 at 16:55
You can find all you need in the documentation for Texture – Jens Mar 17 at 17:03
Related: 63677 – C. E. Mar 18 at 2:14

Image3D isn't what you're looking for. That actually generates a "3-dimensional image" with voxels instead of pixels (by using the given images as layers in the voxel grid). That's what you're seeing in the example you included.

What you want is render a bunch of (square) polygons and use your images as textures. To do so you'll need Texture and the VertexTextureCoordinates option of Polygons. You can get the faces of a cube from PolyhedronData, which you can destructure and then reassemble with the correct textures. (I wonder if there's a way to apply multiple textures to the single GraphicsComplex object returned by PolyhedronData instead.)

Here is some code:

faces = Import /@ {
"http://i.stack.imgur.com/FdfMj.png",
"http://i.stack.imgur.com/Qv7w6.png",
"http://i.stack.imgur.com/WayOQ.png",
"http://i.stack.imgur.com/mgjkA.png",
"http://i.stack.imgur.com/qa5bK.png",
"http://i.stack.imgur.com/T8szh.png"
};

(* standard texture coordinates for mapping a texture completely onto a quad *)
fullQuad = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};

(* get the cube's face polygons *)
cube = PolyhedronData["Cube", "Faces"];

(* pick out the individual faces and pair them with the corresponding texture *)
Graphics3D[
{
Texture[#],
} &,
{
faces,
cube[[2, 1]]
}
],
Lighting -> "Neutral",
Boxed -> False
]


As you can see, this isn't quite perfect, because it doesn't follow the rounded edges of your textures, but extracting the shape of those and using it to generate a model on the fly will be a lot trickier.

-

By using a cube with rounded corners, from this answer, and by applying the images as "stickers" onto the sides of this cube, the dice can be made to look a little bit more realistic. The black border does not translate well to a 3D visualization, so I hid it by fiddling with the vertex coordinates.

faces = Import /@ {
"http://i.stack.imgur.com/FdfMj.png",
"http://i.stack.imgur.com/Qv7w6.png",
"http://i.stack.imgur.com/WayOQ.png",
"http://i.stack.imgur.com/mgjkA.png",
"http://i.stack.imgur.com/qa5bK.png",
"http://i.stack.imgur.com/T8szh.png"
};

polygons = With[{eps = 0.01, k = 0.05}, {
{{k, k, 0 - eps}, {k, 1 - k, 0 - eps}, {1 - k, 1 - k, 0 - eps}, {1 - k, k, 0 - eps}},
{{k, 0 - eps, k}, {1 - k, 0 - eps, k}, {1 - k, 0 - eps, 1 - k}, {k, 0 - eps, 1 - k}},
{{1 + eps, k, k}, {1 + eps, 1 - k, k}, {1 + eps, 1 - k, 1 - k}, {1 + eps, k, 1 - k}},
{{1 - k, 1 + eps, k}, {k, 1 + eps, k}, {k, 1 + eps, 1}, {1 - k, 1 + eps, 1 - k}},
{{0 - eps, 1 - k, k}, {0 - eps, k, k}, {0 - eps, k, 1 - k}, {0 - eps, 1 - k, 1 - k}},
{{1 - k, k, 1 + eps}, {1 - k, 1 - k, 1 + eps}, {k, 1 - k, 1 + eps}, {k, k, 1 + eps}}
}];

(* roundedCuboid: http://mathematica.stackexchange.com/questions/49313/drawing-a-cuboid-with-rounded-corners *)
roundedCuboid[p0 : {x0_, y0_, z0_}, p1 : {x1_, y1_, z1_}, r_] := {
EdgeForm[None],
Cuboid[p0 + {0, r, r}, p1 - {0, r, r}],
Cuboid[p0 + {r, 0, r}, p1 - {r, 0, r}],
Cuboid[p0 + {r, r, 0}, p1 - {r, r, 0}],
Table[Cylinder[{{x0 + r, y, z}, {x1 - r, y, z}}, r], {y, {y0 + r, y1 - r}}, {z, {z0 + r, z1 - r}}],
Table[Cylinder[{{x, y0 + r, z}, {x, y1 - r, z}}, r], {x, {x0 + r, x1 - r}}, {z, {z0 + r, z1 - r}}],
Table[Cylinder[{{x, y, z0 + r}, {x, y, z1 - r}}, r], {x, {x0 + r, x1 - r}}, {y, {y0 + r, y1 - r}}],
Table[Sphere[{x, y, z}, r], {x, {x0 + r, x1 - r}}, {y, {y0 + r, y1 - r}}, {z, {z0 + r, z1 - r}}]
};

sticker[img_, coords_] := With[{eps = 0.92}, {
EdgeForm[None],
Texture[img],
Polygon[coords,
VertexTextureCoordinates -> {{eps, eps}, {1 - eps, eps}, {1 - eps, 1 - eps}, {eps, 1 - eps}}]
}]

Graphics3D[{
White, roundedCuboid[{0, 0, 0}, {1, 1, 1}, 1/20],

I wonder if it might be neater (and less error prone) to extract the geometry from PolyhedronData["Cube", ...] and modify it for the rounded edges. (e.g. turn the Lines in "Edges" into Tubes, and then offset all the faces along their normal). – Martin Ender Mar 18 at 9:20