6

I am creating a Graphics3D object for a box using 6 Polygons (1 per side). I can use one single Texture on one or more sides but I can not find a way to use different Textures for different sides of the box. Is there any way to achieve this?

kafsinkaf
  • 63
  • 2

1 Answers1

12

You can set Texture before each polygon

t = ImageResize[ExampleData@#, {100, 100}] & /@ 
   ExampleData["ColorTexture"][[;; 6]];

vtc = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
coords = {{{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}}, {{0, 0, 
     0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1}}, {{1, 0, 0}, {1, 1, 0}, {1, 
     1, 1}, {1, 0, 1}}, {{1, 1, 0}, {0, 1, 0}, {0, 1, 1}, {1, 1, 
     1}}, {{0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1}}, {{1, 0, 
     1}, {1, 1, 1}, {0, 1, 1}, {0, 0, 1}}};

Graphics3D[{Table[{Texture@t[[i]], 
     Polygon[coords[[i]], VertexTextureCoordinates -> vtc]}, {i, 6}]}]

enter image description here

Or you can use one texture with proper texture coordinates like in cube maps

enter image description here

cubemap = ImageResize[#, Scaled[0.5]] &@ Import@"https://i.stack.imgur.com/hSCfz.jpg";

vtccm = {{##}, {# + 1, #2}, {# + 1, #2 + 1}, {#, #2 + 1}} & @@@ {{1, 
      1}, {0, 2}, {1, 2}, {2, 2}, {3, 2}, {1, 3}}/4;

α = 0.;
Graphics3D[{EdgeForm[], Texture[cubemap], 
  Polygon[coords, VertexTextureCoordinates -> vtccm]}, 
 Lighting -> {{"Ambient", White}}, Boxed -> False, ViewAngle -> 1, 
 ViewCenter -> {1, 1, 1}/2, 
 ViewVector -> {1, 1, 1}/2 + {Cos[α], Sin[α], 0}/8, 
 RotationAction -> "Clip", ImageSize -> 500, 
 ViewVertical -> {0, 0, 1}]

enter image description here

There is no visible joints!

Related: Implementing a first person view of 3D objects in a scene

ybeltukov
  • 43,673
  • 5
  • 108
  • 212
  • Lovely! Is there a way to efficiently re-use textures within an expression to avoid bloating by repetition? – Yves Klett Oct 20 '14 at 20:19
  • @ybeltukov what is vtccm doing in your code. I cant quite understand it. – Gordon Coale Oct 21 '14 at 09:12
  • 2
    @GordonCoale it is "Vertex Texture Coordinates of Cube Map". It is a short form of {{{1/4, 1/4}, {1/2, 1/4}, {1/2, 1/2}, {1/4, 1/2}}, {{0, 1/2}, {1/4, 1/2}, {1/4, 3/4}, {0, 3/4}}, {{1/4, 1/2}, {1/2, 1/2}, {1/2, 3/4}, {1/4, 3/4}}, {{1/2, 1/2}, {3/4, 1/2}, {3/4, 3/4}, {1/2, 3/4}}, {{3/4, 1/2}, {1, 1/2}, {1, 3/4}, {3/4, 3/4}}, {{1/4, 3/4}, {1/2, 3/4}, {1/2, 1}, {1/4, 1}}}. – ybeltukov Oct 21 '14 at 11:14
  • Nice shortcut! I can use that in a similar way for a problem of mine. Thank you! – Gordon Coale Oct 21 '14 at 15:21