Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to create a figure illustrating a cylinder filled with gas. And I saw a very good looking similar one at here. The shape is polygon and by using the VertexColors option, it creates a impression of gas inside the building, with high density near the floor and low density near the roof. However, I would like to plot a cylinder, and for a cylinder, there seems no option of VertexColors. So how can I create the same shading effect for a cylinder?

Code copied from here

w = 100;
l = 200;
h = 30;
m = 70;
backwall = {{0, l, 0}, {w, l, 0}, {w, l, h}, {0, l, h}};
side1 = {{0, 0, 0}, {0, 0, h}, {0, l, h}, {0, l, 0}};
side2 = {{w, 0, 0}, {w, 0, h}, {w, l, h}, {w, l, 0}};
floor = {{0, 0, 0}, {w, 0, 0}, {w, l, 0}, {0, l, 0}};
top = {{0, 0, h}, {w, 0, h}, {w, l, h}, {0, l, h}};
front = {{0, 0, 0}, {w, 0, 0}, {w, 0, h}, {0, 0, h}};
leftRoof = {{0, 0, h}, {w/2, 0, m}, {w/2, l, m}, {0, l, h}};
rightRoof = {{w, 0, h}, {w/2, 0, m}, {w/2, l, m}, {w, l, h}};
roofBack = {{w, l, h}, {w/2, l, m}, {0, l, h}};
roofFront = {{w, 0, h}, {w/2, 0, m}, {0, 0, h}};
building = {backwall, side1, side2, floor, front, leftRoof, rightRoof,
    roofBack, roofFront};
figure = Graphics3D[{Opacity[0.5], 
   Style[Polygon[building, 
     VertexColors -> Map[0.5 + #[[3]]/80 &, building, {2}]], 
    Lighting -> {{"Ambient", White}}]}, Boxed -> False, 
  RotationAction -> "Clip"]

enter image description here

share|improve this question
1  
This question is answered by @J.M. in a response to Extruding along a path. That answer is much more general, but a cylinder is a special case of the problem addressed there. –  Jens May 20 at 3:36

2 Answers 2

Tube takes a VertexColorsoption. In fact you can even combine this with a couple of Cylinders to a nice effect IMHO:

Graphics3D[{CapForm["Butt"], 
  Tube[{{0, 0, 0}, {0, 0, 2.5}, {0, 0, 5}}, 1, 
   VertexColors -> {Directive[Black, Opacity[.95]], 
     Directive[Gray, Opacity[.25]], Directive[White, Opacity[0.25]]}],
   Opacity[0.2, LightBlue], Cylinder[{{0, 0, 0}, {0, 0, 5}}, 1.05], 
  Opacity[1, Black], Specularity[White, 3], 
  Cylinder[{{0, 0, 5}, {0, 0, 5.1}}, 1.05]}, Lighting -> "Neutral", 
 Boxed -> False, SphericalRegion -> True]

enter image description here

Here is some fun with colors!

 GraphicsGrid[
   Map[Graphics3D[{CapForm["Butt"], 
     Tube[{{0, 0, 0}, {0, 0, 2.5}, {0, 0, 5}}, 1, 
      VertexColors -> {Directive[Black, Opacity[.95]], 
        Directive[Gray, Opacity[.25]], 
        Directive[White, Opacity[0.25]]}], Opacity[0.2, #], 
     Cylinder[{{0, 0, 0}, {0, 0, 5}}, 1.05], Opacity[1, Black], 
     Specularity[White, 3], 
     Cylinder[{{0, 0, 5}, {0, 0, 5.1}}, 1.05]}, 
    Lighting -> "Neutral", Boxed -> False, 
    SphericalRegion -> True] &, {{Red, Green, Blue}, {Brown, Purple, 
    Orange}, {Cyan, Yellow, Magenta}}, {2}], ImageSize -> 800]

enter image description here

And why not a lava lamp? Reverse the gradient! (This could likely be greatly improved with a bit more time and effort, especially with some dynamic content and randomly generated blobs).

Graphics3D[{CapForm["Butt"], 
  Tube[{{0, 0, 1.5}, {0, 0, 7}}, {2, 1/2}, 
   VertexColors -> {Directive[White, Opacity[.25]], 
     Directive[Black, Opacity[0.65]]}], Opacity[0.2, Purple], 
  Tube[{{0, 0, 1.5}, {0, 0, 7}}, {2.05, 0.55}], Opacity[1, Black], 
  Specularity[White, 5], Cylinder[{{0, 0, 7}, {0, 0, 7.1}}, 0.55], 
  Tube[{{0, 0, 0}, {0, 0, 1.5}}, {1.05, 2.05}], 
  Tube[{{0, 0, -1.5}, {0, 0, 0}}, {2.05, 1.05}], Orange, 
  Style[Sphere[{0, 0, 4}, 0.3], 
   Lighting -> {{"Point", Orange, {0, 0, 0}}}], 
  Style[Sphere[{0.1, 0.3, 3}, 0.5], 
   Lighting -> {{"Point", Orange, {0.1, 0.2, 0}}}], 
  Style[Sphere[{0, 0, 1}, 1], 
   Lighting -> {{"Point", Orange, {0, 0, 5}}}], 
  Style[Sphere[{-0.5, -0.4, 2}, 0.4], 
   Lighting -> {{"Point", Orange, {1, 1, 1}}}]}, 
 Lighting -> "Neutral", Boxed -> False, SphericalRegion -> True]

enter image description here

share|improve this answer
2  
Nice! Can you also do a lava-lamp? –  Yves Klett May 20 at 15:24
    
That looks beautiful. Thanks! –  xslittlegrass May 20 at 16:58
    
+1 Very cool solution! –  Vitaliy Kaurov May 20 at 17:03
    
My crystal lava lamp predicts a hot network Q... –  Yves Klett May 20 at 17:20

Approximate by polygons:

bottom = Table[{50 Cos[n], 50 Sin[n], 0}, {n, 0, 2 Pi, 2 Pi/50}];
top = Table[{50 Cos[n], 50 Sin[n], 80}, {n, 0, 2 Pi, 2 Pi/50}];
sides = Flatten[#, 1] & /@ Thread[{Partition[bottom, 2, 1], 
         Reverse /@ Partition[top, 2, 1]}];

Graphics3D[{
  {EdgeForm[Directive[Opacity[0.2], Thick]], Opacity[0.7], Polygon[bottom]},
  {EdgeForm[Directive[Opacity[0.2], Thick]], Opacity[0.7], Polygon[top]},
  {EdgeForm[], Opacity[0.7], 
   Polygon[sides, VertexColors -> Map[0.5 + #[[3]]/80 &, sides, {2}]]}},
 Boxed -> False, RotationAction -> "Clip", 
 Lighting -> {{"Ambient", White}}]

enter image description here

share|improve this answer
    
Thanks a lot :) –  xslittlegrass May 20 at 16:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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