6
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ You can set Texture before each polygon: {Texture[t1], Polygon[pts1,VertexTextureCoordinates -> vtc1], Texture[t2], Polygon[pts2,VertexTextureCoordinates -> vtc2], ...} $\endgroup$
    – ybeltukov
    Commented Oct 20, 2014 at 15:41
  • $\begingroup$ Thanks @ybeltukov. This is exactly what I needed. $\endgroup$
    – kafsinkaf
    Commented Oct 20, 2014 at 15:48
  • $\begingroup$ @ybeltukov answer? $\endgroup$
    – Yves Klett
    Commented Oct 20, 2014 at 15:48
  • $\begingroup$ @YvesKlett, I decided to expand this simple example :) $\endgroup$
    – ybeltukov
    Commented Oct 20, 2014 at 18:00

1 Answer 1

12
$\begingroup$

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.sstatic.net/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

$\endgroup$
4
  • $\begingroup$ Lovely! Is there a way to efficiently re-use textures within an expression to avoid bloating by repetition? $\endgroup$
    – Yves Klett
    Commented Oct 20, 2014 at 20:19
  • $\begingroup$ @ybeltukov what is vtccm doing in your code. I cant quite understand it. $\endgroup$ Commented Oct 21, 2014 at 9:12
  • 2
    $\begingroup$ @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}}}. $\endgroup$
    – ybeltukov
    Commented Oct 21, 2014 at 11:14
  • $\begingroup$ Nice shortcut! I can use that in a similar way for a problem of mine. Thank you! $\endgroup$ Commented Oct 21, 2014 at 15:21

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.