3
$\begingroup$

From this code, how could I animate (speed given by the user) this cube at random so that each face shows me a different image.

EDIT(The idea is that the cube rotates at random and shows different images on each face, it is understood now :(

 $Graphics3D[
 GraphicsComplex[
 Tuples[{0, 1}, 3],
 MapThread[
 {Texture[
 Graphics[ImportString[ExportString[#1, "PDF"], "PDF"][[1, 1]],       Background -> #3]],
 Polygon[#2, VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]} &,
{Characters["ABCDEF"],
 {{1, 2, 4, 3}, {1, 5, 6, 2}, {1, 3, 7, 5}, {8, 6, 5, 7}, {8, 7, 
   3, 4}, {8, 4, 2, 6}},
 Table[Hue[i/6], {i, 6}]}
]
   ],
 Lighting -> "Neutral"]$

here some example image to put on the faces

enter link description here enter link description here enter link description here enter link description here enter link description here enter link description here

$\endgroup$
1

1 Answer 1

4
$\begingroup$

Update 5: In newer versions (e.g., version 13.3.1) replace ImportString[ExportString[#1, "PDF"], "PDF"][[1, 1]] below with

ImportString[ExportString[#, "PDF"], {"PDF", "PageGraphics"}, 
  "TextMode" -> "Outlines"][[1, 1]]

Update 4: Rotations around x axis followed by rotations around y axis followed by rotations around z axis:

faces = Polygon[#, VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0,  1}}] & /@ 
  {{1, 2, 4, 3}, {1, 5, 6, 2}, {1, 3, 7, 5}, {8, 6, 5, 7}, {8, 7, 3, 4}, {8, 4, 2, 6}};

images = Import /@ 
 {"https://images-na.ssl-images-amazon.com/images/I/51ZQiVNDYPL._SX425_.jpg",
  "https://images-na.ssl-images-amazon.com/images/I/51S7cvjbrqL._SX425_.jpg", 
  "https://images-na.ssl-images-amazon.com/images/I/61acUMNLNvL._SX425_.jpg", 
  "https://images-na.ssl-images-amazon.com/images/I/51xRMocaTKL._SX425_.jpg"};

{one, six} = Texture[Graphics[
      ImportString[ExportString[#1, "PDF"], "PDF"][[1, 1]], 
      Background -> #2]] & @@@ Transpose[{{"1", "6"}, {Red, Yellow}}];
textures2 = Join[Texture /@ images, {one, six}];

Animate[Graphics3D[
  Rotate[GraphicsComplex[Tuples[{0, 1}, 3],Transpose[{textures2, faces}]], t, 
   IdentityMatrix[3][[1 + Quotient[t, 2 Pi]]], {1, 1, 1}/2], 
  Lighting -> "Neutral", SphericalRegion -> True, 
  PlotRange -> {{-1/2, 3/2}, {-1/2, 3/2}, {-1/2, 3/2}}, 
  Boxed -> False], {t, Range[0, 6 Pi - Pi/120, Pi/120]}]

enter image description here

Update 3: Rotations by a random angle around a randomly selected axis:

Animate[Graphics3D[Rotate[GraphicsComplex[Tuples[{0, 1}, 3], 
     Transpose[{textures, faces}]], t, RandomChoice[IdentityMatrix[3]]], 
   Lighting -> "Neutral",  SphericalRegion -> True,  Boxed -> False],
 {t, RandomSample[Range[0, 2 Pi, Pi/60]]}]

enter image description here

frames2 = Table[Graphics3D[
    Rotate[GraphicsComplex[Tuples[{0, 1}, 3], Transpose[{textures, faces}]], t, 
     RandomChoice[IdentityMatrix[3]]], Lighting -> "Neutral", 
     SphericalRegion -> True,  Boxed -> False],
  {t, RandomSample[Range[0, 2 Pi, Pi/60]]}];

Export["cube2.gif", frames2]

enter image description here

Update 2: Rotating a cube with images on its faces:

faces = Polygon[#, VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}] & /@
  {{1, 2, 4, 3}, {1, 5, 6, 2}, {1, 3, 7, 5}, {8, 6, 5, 7}, {8, 7, 3, 4}, {8, 4, 2, 6}};

images = Import /@ 
  {"https://images-na.ssl-images-amazon.com/images/I/51ZQiVNDYPL._SX425_.jpg", 
 "https://images-na.ssl-images-amazon.com/images/I/51S7cvjbrqL._SX425_.jpg", 
 "https://images-na.ssl-images-amazon.com/images/I/61acUMNLNvL._SX425_.jpg", 
 "https://images-na.ssl-images-amazon.com/images/I/51xRMocaTKL._SX425_.jpg"};

{one, six} = Texture[Graphics[
      ImportString[ExportString[#1, "PDF"], "PDF"][[1, 1]], 
      Background -> #2]] & @@@ Transpose[{{"1", "6"}, {Red, Yellow}}];
textures2 = Join[Texture /@ images, {one, six}];

Animate[Graphics3D[
  GraphicsComplex[Tuples[{0, 1}, 3], Transpose[{textures2, faces}]], 
  Lighting -> "Neutral", 
  ViewVector -> 6 { Cos[t], Sin[t], 3 Sin[t]/6 + 1/12}, 
  SphericalRegion -> True], {t, 0, 2 Pi, Pi/60}]

enter image description here

You can control the speed using the second and third buttons.

See also: this answer by halirutan to a related question

Update: Using some of the images from the links in OP:

images = Import /@ 
  {"https://images-na.ssl-images-amazon.com/images/I/51ZQiVNDYPL._SX425_.jpg", 
 "https://images-na.ssl-images-amazon.com/images/I/51S7cvjbrqL._SX425_.jpg", 
 "https://images-na.ssl-images-amazon.com/images/I/61acUMNLNvL._SX425_.jpg", 
 "https://images-na.ssl-images-amazon.com/images/I/51xRMocaTKL._SX425_.jpg"};

{one, six} = Texture[Graphics[
      ImportString[ExportString[#1, "PDF"], "PDF"][[1, 1]], 
      Background -> #2]] & @@@ Transpose[{{"1", "6"}, {Red, Yellow}}];

Just wrap your images with Texture (and add the missing 1 and 6):

textures2 = Join[Texture /@ images, {one, six}];


frames = Table[Graphics3D[GraphicsComplex[Tuples[{0, 1}, 3], 
     Transpose[{RandomSample[textures2, 6], faces}]], Lighting -> "Neutral"], {30}];
ListAnimate[frames]

enter image description here

Original answer:

textures = Texture[Graphics[ImportString[ExportString[#1, "PDF"], "PDF"][[1, 1]], 
      Background -> #2]] & @@@ 
   Transpose[{CharacterRange["A", "Z"], Hue[#/26] & /@ Range[26]}];

faces = Polygon[#, VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}] & /@
  {{1, 2, 4, 3}, {1, 5, 6, 2}, {1, 3, 7, 5}, {8, 6, 5, 7}, {8, 7, 3, 4}, {8, 4, 2, 6}};

frames = Table[Graphics3D[GraphicsComplex[Tuples[{0, 1}, 3], 
    Transpose[{RandomSample[textures, 6], faces}]], Lighting -> "Neutral"], {30}]

ListAnimate[frames]

enter image description here

Export["cube.gif", frames]

enter image description here

$\endgroup$
14
  • $\begingroup$ @ kglr ,ok , thanks , the idea is correct, but as insert pictures on the cube faces no letters (for example the images of the links that you leave) $\endgroup$
    – zeros
    Commented Jul 15, 2019 at 18:50
  • $\begingroup$ @ kglr ,,first of all, thanks for your time, is much better, but it seems that you understood me the other way round, it is the cube that rotates at random with images on their faces $\endgroup$
    – zeros
    Commented Jul 15, 2019 at 22:05
  • $\begingroup$ @ kglr sorry It is in the title How to animate a cube with images on their faces? ,(maybe I express myself badly) From this code, how could I animate (speed given by the user) this cube at random so that each face shows me a different image $\endgroup$
    – zeros
    Commented Jul 15, 2019 at 22:14
  • $\begingroup$ @zeros, please see update 2. $\endgroup$
    – kglr
    Commented Jul 15, 2019 at 22:27
  • 1
    $\begingroup$ @Reb.Cabin, thank you for bringing this issue up. I updated the post with a fix. $\endgroup$
    – kglr
    Commented Sep 18, 2023 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.