# Graphics3D: Opacity limitations

Trying to make a better-looking globe model, I start off with something like this

To enhance the 3D effect, I want the lines further from the eye to be "grayer" than the lines closer to the eye, as if there were a little fog inside the sphere. I thought I might get there by replacing the great-circles with semi-transparent wafers, but it didn't work. It seems as though I hit some kind of internal limit on the number of semi-transparent objects I can create before they become opaque.

Here is the original code with polygonal great circles (minus the RGB-XYZ axes):

ClearAll[e, o, g, ϵ];
e[1] = {1, 0, 0}; e[2] = {0, 1, 0}; e[3] = {0, 0, 1};
o = {0, 0, 0}; g = 1.25; ϵ = 1/10000;
ClearAll[polycircle];
polycircle[n_: 360] :=
Line@Table[{Cos[2 π i/n], Sin[2 π i/n], 0}, {i, n + 1}];
ClearAll[globeGrid];
globeGrid[bands_: 6, figure_: polycircle[]] :=
{Table[
Scale[Translate[figure,  Sin[(k π)/2] e[3]],
Cos[(k π)/2]]
, {k, -((bands - 1)/bands), (bands - 1)/bands, 1/bands}]
, Table[Rotate[Rotate[figure, π/2, e[1]], k π, e[3]]
, {k, 0, (bands - 1)/bands, 1/bands}]};
ClearAll[showFrames];
showFrames[figure_:polycircle[]] := Show[{
Graphics3D[{
Opacity[0.05], Sphere[], Opacity[1.0]
, globeGrid[6, figure]
}]   }
, Axes -> True
, PlotRange -> {{-g, g}, {-g, g}, {-g, g}}
, ImageSize -> Large];
showFrames[]


To improve it, I replace the polycircles with partially transparent wafers, which are very thin cylinders at low opacity:

ClearAll[wafer];
wafer[opacity_: 1/24] :=
{RGBColor[1, 0.71, 0]
, Opacity[opacity]
, Cylinder[ϵ {-e[3], e[3]}]
, Opacity[1]};


The results were disappointing

showFrames[wafer[]]


EDIT: I tried a tube

polytorus[n_: 100] := Tube[polycircle[n], 0.01];
showFrames[polytorus[48]]


it's too slow to be interactive, but it has a better 3D effect. Still looking for a better answer.

• oops, my bad. should have been the constant [3]. fixed in the edit (I snipped this code from another context). Jul 18, 2014 at 3:05
• Why not just ParametricPlot3D[{Cos[lat] Cos[lon], Cos[lat] Sin[lon], Sin[lat]}, {lat, -Pi/2, Pi/2}, {lon, -Pi, Pi}, PlotStyle -> {Opacity[0.7], White}, MeshStyle -> Black]? i.sstatic.net/pPKE6.png
– user484
Jul 18, 2014 at 4:00
• @Rahul That only gives two levels of effective line color: in front or behind. I believe Reb.Cabin wants to simulate a volumetric fill. Jul 18, 2014 at 4:59
• ... True, but Rahul's comment is excellent. I'll wait a few days to see whether something new comes up, then ask that @Rahul promote it to an answer. Jul 18, 2014 at 12:53
• Strange. For me, showFrames[wafer[]] gives this, and showFrames[{polycircle[],waver[]}] gives this. I think the latter is what you wanted, right? BTW, I'm using version 8.0.0.0. Jul 18, 2014 at 12:56

grid = First@
ParametricPlot3D[{Cos[u] Cos[v], Sin[u] Cos[v], Sin[v]}, {u, 0,
2 Pi}, {v, -Pi/2, Pi/2}, PlotStyle -> None];

Graphics3D[
{grid,
{Red, Thick, Line[1.2 {{-1, 0, 0}, {1, 0, 0}}]},
{Blue, Thick, Line[1.2 {{0, -1, 0}, {0, 1, 0}}]}, {Green, Thick,
Line[1.2 {{0, 0, -1}, {0, 0, 1}}]},
Raster3D[Array[
UnitStep[1 - Norm[{##}]] &, {100, 100,
100}, {{-1., 1.}, {-1., 1.}, {-1., 1.}}], {{-1, -1, -1}, {1, 1,
1}}, ColorFunction -> (GrayLevel[0.8, .03 #] &)]
},
Boxed -> False]


## Edit by halirutan

Let me point out some flaws and probably improve the quality a bit. Therefore, blame me for everything that follows, not Szabolcs.

First of all, fog is usually a global thing which means that the impact on the visibility really depends on the distance to the viewer. If the fog is only visible inside the sphere, then we have things like the top of wireframe which is crystal clear (and an eye catcher because lines meet there) while it is farther away from the camera than the front wire.

Therefore, we might improve the effect by making a foggy cube that is larger than the wireframe so that at least it looks like fog is everywhere from the camera viewpoint.

Another detail seems to be that the gradient with which view is degrading doesn't seem to be enough. Therefore, one could make a cube that has not a constant visual density, but a density that is getting larger in one direction. If we then view the wireframe from the correct viewpoint, it might increase the fog-effect.

Last point, I think fog looks better when it is white, not gray. If we combine all these points and make the wireframe thicker then I get something like the following (I'll append the code at the end):

There are some points left: Most importantly, I don't think it is appropriate to try to achieve something like this in the Mathematica front end. If you need high quality, then create your model and render it with Blender (or whatever renderer you prefer).

Finally, I could not get antialiasing working when I combined a Raster3D with a normal 3d graphics in Mathematica. It seems that this is not working with Raster3D or Image3D.

grid = First@
ParametricPlot3D[{Cos[u] Cos[v], Sin[u] Cos[v], Sin[v]}, {u, 0,
2 Pi}, {v, -Pi/2, Pi/2}, PlotStyle -> None,
MeshStyle ->
Directive[Thickness[0.005], RGBColor[0, 43/255, 18/85]]];

Graphics3D[{grid, RGBColor[44/51, 10/51, 47/255], Thickness[.01],
Line[1.2 {{-1, 0, 0}, {1, 0, 0}}], {RGBColor[38/255, 139/255,
14/17], Line[1.2 {{0, -1, 0}, {0, 1, 0}}]}, {RGBColor[133/255,
3/5, 0], Line[1.2 {{0, 0, -1}, {0, 0, 1}}]},
White,
Raster3D[
Array[#3 &, {100, 100,
100}, {{0, 1}, {0, 1}, {0, 1}}], {{-1, -2, -2}, {1, 2, 2}},
ColorFunction -> (GrayLevel[1, .05 #] &)]}, Boxed -> False,
ViewPoint -> {-3.15, -0.89, 0.84}, ViewAngle -> 0.175]

• Cannot rotate it smoothly, it freezes the notebook :( Feb 12, 2015 at 21:44
• @SquareOne The first version I posted did have this problem, but the updated one doesn't, at least not on my machine. Since there was only 12 minutes between my post and your comment, could you reload the page and try again? Feb 12, 2015 at 22:31
• Yes, it's smooth now ... however there is a really strange and heavy slowdown on my machine paradoxically when I test arrays smaller than {100,100,100} ?? The problem seems to start and is the worst with {50,50,50} then it gets better when the array gets smaller and smaller -> {10,10,10}. It's 100 % reproducible (I start from fresh kernel). There seems to be a lot of disk caching then also ... (My config v10.0.2, OSX10.9.5). Do you get the same ? Feb 12, 2015 at 23:29
• I do not get much of a sense of depth out of this; nearly the entire mesh seems to have the same color rather than the farther part having a thicker "fog" -- only the very edges lighten. Can this be improved? Feb 13, 2015 at 10:20
• @Mr.Wizard Is it a bit better now? Feb 13, 2015 at 13:00

I have to admit, that I only copied your code and tried it without actually reading what you have done, but I guess I can help to fix at least the second image. What you are after is the "DepthPeelingLayers" settings that you can access with the option inspector:

When you raise this number to e.g. 32, the output looks like this

This can also be done on a per-object basis with BaseStyle:

Show[
showFrames[wafer[]],
BaseStyle -> RenderingOptions -> {"DepthPeelingLayers" -> 32}
]

• Wonderful! I knew someone would know what was going on. Feb 12, 2015 at 10:08

You just need to change the parameters before and after your Sphere[] like this:

{Graphics3D[{Opacity[0.6], GrayLevel[1], Sphere[], GrayLevel[0], Opacity[1.0], globeGrid[6, figure]}]}


Opacity[0.6] is about right to partly hide the lines around the back. GrayLevel[1] because you want the sphere to be coloured white/light. GrayLevel[0] because you want the globeGrid to be black.

If you don't like the default coloured sphere (I think it's nice) you can fiddle with the lighting, e.g. Lighting -> "Neutral".

• Really nice effect ! Feb 12, 2015 at 21:22
• and keeps the rotation smooth. (Opacity[1.0] could be omitted.) You should include a screenshot. Feb 12, 2015 at 21:40
• I agree, Opacity[1.0] can be omitted but it is useful to understand that it will be propagated.
– djp
Feb 12, 2015 at 21:49
• I'm afraid this isn't what OP wants, he wants "the lines further from the eye to be "grayer" than the lines closer to the eye, as if there were a little fog inside the sphere", while this only differs "front" and "behind". Feb 13, 2015 at 2:59
• This is not what I started the Bounty to address, and as @xzczd comments it is not exactly the effect that the OP was after, but +1 nevertheless for an interesting contribution. Feb 13, 2015 at 10:17