3

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

zeros
  • 2,263
  • 1
  • 14
  • 18

1 Answers1

4

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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • @ 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) – zeros Jul 15 '19 at 18:50
  • @ 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 – zeros Jul 15 '19 at 22:05
  • @ 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 – zeros Jul 15 '19 at 22:14
  • @zeros, please see update 2. – kglr Jul 15 '19 at 22:27
  • now works fine, thanks for your time – zeros Jul 15 '19 at 23:53
  • @zeros, my pleasure. Thank you for the accept. – kglr Jul 16 '19 at 00:01
  • how do I rotate more fluid, not jumps – zeros Jul 16 '19 at 00:03
  • @zeros, replace RandomSample[Range[0, 2 Pi, Pi/60]] with Range[0, 2 Pi, Pi/60] and RandomChoice[IdentityMatrix[3]] with {0,0,1} to rotated around the z axis ({0,1,0} for rotating around y axis and {1,0,0} for rotating around x axis). Alternatively, use the approach in update 2 to rotate along a path. – kglr Jul 16 '19 at 00:27
  • sorry , with so much change, now I do not work any, can the full final code please – zeros Jul 16 '19 at 00:38
  • @ kglr ,something happens and no update works, I could write (copy) the complete code with tosas its variables in your next answer (it tells me that there are some words undefined)
    – zeros Jul 16 '19 at 04:10
  • @zeros, posted the complete code in update 4. – kglr Jul 16 '19 at 04:19
  • @ kglr , now it works, the pc is locked, there was no way to calculate, I'm wondering if you can add an output to animated gif please, greetings and thank you – zeros Jul 16 '19 at 21:14
  • @kglr in Mathematica 13.3, I consistently get X[[1,1]] is longer than depth of the object, where x is an element of the list of character/color pairs rendered by ImportString. The result is the background with no characters. I'm not sure how to fix this. It's probably some breaking change in PDF importing ? but who knows. – Reb.Cabin Sep 18 '23 at 16:18
  • 1
    @Reb.Cabin, thank you for bringing this issue up. I updated the post with a fix. – kglr Sep 18 '23 at 17:29