How to create an image of a 3D die from the 2D images?
There is an example in the Documentation Center
but it doesn't work.
Answer
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: https://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],
MapThread[sticker, {faces, polygons}]
},
Boxed -> False, Lighting -> "Neutral"
]
Comments
Post a Comment