13
$\begingroup$

Is there a way to color each face of a Cuboid with a different color?

I looked in the help but couldn't find anything. I found this Coloring the faces of a 3D object different colors (in Mathematica) which proposes building the cuboid with Polygon primitives; any other way?

$\endgroup$
2
  • 1
    $\begingroup$ Since you want to add a color to each face, You really do need each face to be a separate polygon. I don't think there is a better way to accomplish this. What in particular do you wish could be improved upon this approach? $\endgroup$
    – jVincent
    Commented Oct 22, 2012 at 20:47
  • $\begingroup$ Nothing special; I just wish to know if there is any other way of doing that. $\endgroup$
    – Renan
    Commented Oct 22, 2012 at 20:49

2 Answers 2

10
$\begingroup$

I don't think it is possible to color the faces of a single cuboid differently (other than the two sides of one face), though there are multiple methods to create multicolored cuboid-like objects.

Creating multiple polygons:

v = {{-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, {-1, -1, 
    1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1}};
idx = {{1, 2, 3, 4}, {1, 2, 6, 5}, {2, 3, 7, 6}, {3, 4, 8, 7}, {4, 1, 
    5, 8}, {5, 6, 7, 8}};
Graphics3D[Table[{Glow@Hue[i/6], Polygon[v[[idx[[i]]]]]}, {i, 6}], Lighting -> None]

Mathematica graphics

Using textures, from the help on Texture:

sides = Graphics[{Hue@#, Rectangle[]}, ImageSize -> 50] & /@ 
   Most@Range[0, 1, 1/6];
vtc = {{0.01, 0.01}, {0.99, 0.01}, {0.99, 0.99}, {0.01, 0.99}};
Graphics3D[{Black, EdgeForm[Black],
  Table[{Texture[sides[[i]]], 
    GraphicsComplex[v, Polygon[idx[[i]], VertexTextureCoordinates -> vtc]]}, {i, 6}]}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ This does nicely and can be easily adapted to other polyhedrons using PolyhedronData. Thanks! $\endgroup$
    – Renan
    Commented Oct 22, 2012 at 22:40
12
$\begingroup$

Just for completeness:

Graphics3D[{Lighting -> {
    {"Directional", Red, {.5, .5, 1}},
    {"Directional", Green, {1, .5, .5}},
    {"Directional", Blue, {0, 0, 0}}
    },
  Cuboid[]}, Boxed -> False]

cuboid

$\endgroup$
4
  • $\begingroup$ Ingenious. Wouldn't have though ot this. Though it only works if there are no other objects casting a shadow. Cannnot something like Glow be used in that case? $\endgroup$ Commented Oct 23, 2012 at 9:29
  • $\begingroup$ @IstvánZachar there are no shadows in Graphics3D! $\endgroup$ Commented Mar 18, 2013 at 10:20
  • $\begingroup$ @OleksandrR. Yes, silly me. But still adding other objects (like Cuboid[{1, 1, 1}]) messes up the colors, so my guess is this method is only applicable when there is only one object in the scene. $\endgroup$ Commented Mar 18, 2013 at 10:32
  • $\begingroup$ This seems to be less general than the accepted answer, since we can't easily apply GeometricTransformation to this: we'll get Graphics3DBox is not a Graphics3D primitive or directive error message and no output. Maybe I'm doing something wrong, though... $\endgroup$
    – Ruslan
    Commented Aug 22, 2019 at 9: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.