2
$\begingroup$

I would like to apply gradient coloring lengthwise to a Cuboid. The coloring can be within the volume or one of its long faces. I'm not sure which would look better. Here's what I have so far:

Graphics3D[{
EdgeForm[{Thickness[0.0015], Black}],
GrayLevel[0.8, 0.4],
Opacity[0.8],
Cuboid[{-6, -50, -4}, {6, 70, 3}, 
VertexColors -> ColorData["DarkRainbow"]],
}, Boxed -> False, ViewPoint -> {0.2, -0.8, 0.3}, 
ViewVertical -> {0., 0., 1.}, Lighting -> "Neutral", 
ImageSize -> 500]

enter image description here

My failed attempt was to use VertexColoring but that doesn't work with Cuboid. I would prefer to stay with Cuboid.

$\endgroup$
  • 2
    $\begingroup$ Cuboid[] does not support VertexColors; you'll need to use Polygon[]. $\endgroup$ – J. M. will be back soon Aug 11 '17 at 4:42
5
$\begingroup$

As @JM points out, you can't use VertexColors with Cuboid, only Polygon, Line, Tube, Point, and GraphicsComplex (from the VertexColors documentation). GraphicsComplex is probably what you want here.

vertices = Tuples[Transpose[{{-6, -50, -4}, {6, 70, 3}}]];
vc = ColorData["DarkRainbow"][#] & /@ {0, 0, 1, 1, 0, 0, 1, 1}; 
Graphics3D[{EdgeForm[{Thickness[0.0015], Black}], GrayLevel[0.8, 0.4],
   Opacity[0.8], 
  GraphicsComplex[vertices, 
   Polygon[{{1, 2, 4, 3}, {1, 2, 6, 5}, {1, 3, 7, 5}, {8, 6, 2, 
      4}, {8, 7, 5, 6}, {8, 7, 3, 4}}], VertexColors -> vc]}, 
 Boxed -> False, ViewPoint -> {0.2, -0.8, 0.3}, 
 ViewVertical -> {0., 0., 1.}, Lighting -> "Neutral", 
 ImageSize -> 500]

enter image description here

So the colouring is down the length on all sides. You can of course mess around with how I've set the colours to the vertices in vc.

If, instead, you just want the colouring on one face, you can draw that polygon separately:

vertices = Tuples[Transpose[{{-6, -50, -4}, {6, 70, 3}}]];
vc = ColorData["DarkRainbow"][#] & /@ {1, 1, 0, 0};
Graphics3D[{EdgeForm[{Thickness[0.0015], Black}], GrayLevel[0.8, 0.4],
   Opacity[0.8], 
  GraphicsComplex[vertices, 
   Polygon[{{1, 2, 4, 3}, {1, 2, 6, 5}, {1, 3, 7, 5}, {8, 7, 5, 
      6}, {8, 7, 3, 4}}]], 
  Polygon[vertices[[{4, 8, 6, 2}]], VertexColors -> vc]}, 
 Boxed -> False, ViewPoint -> {0.2, -0.8, 0.3}, 
 ViewVertical -> {0., 0., 1.}, Lighting -> "Neutral", 
 ImageSize -> 500]

enter image description here

$\endgroup$
  • $\begingroup$ I run your code but mine appears opaque. I am using 11.0 student version. $\endgroup$ – BeauGeste Aug 14 '17 at 13:20
  • $\begingroup$ I have 10.4. I can't find anything on SE or Google about an Opacity bug in 11. Does changing the value affect anything at all? $\endgroup$ – aardvark2012 Aug 14 '17 at 13:41
  • $\begingroup$ No, same thing no matter what I set for Opacity. $\endgroup$ – BeauGeste Aug 14 '17 at 15:28
  • $\begingroup$ That's weird. Sorry about that. There's not much I can do from this end, but you could play around with Opacity in some other Graphics3D constructions and see if you can find what breaks it. Then post another question specifically about this issue. $\endgroup$ – aardvark2012 Aug 15 '17 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.