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?
1 Answer
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}]}]
Or you can use one texture with proper texture coordinates like in cube maps
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}]
There is no visible joints!
Related: Implementing a first person view of 3D objects in a scene
-
$\begingroup$ Lovely! Is there a way to efficiently re-use textures within an expression to avoid bloating by repetition? $\endgroup$ Oct 20, 2014 at 20:19
-
$\begingroup$ @ybeltukov what is vtccm doing in your code. I cant quite understand it. $\endgroup$ 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$ 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$ Oct 21, 2014 at 15:21
Texture
before each polygon:{Texture[t1], Polygon[pts1,VertexTextureCoordinates -> vtc1], Texture[t2], Polygon[pts2,VertexTextureCoordinates -> vtc2], ...}
$\endgroup$