11
$\begingroup$

I'm trying to make a ContourPlot3D that needs to be transparent. With transparency, however, something odd happens.

With PerformanceGoal->"Speed", the colors look fine.

With PerformanceGoal->"Quality", the colors are all mottled.

My actual code is more complicated than what I've shown here, but I've tried to reduce it to (nearly?) the simplest case that still produces the error.

I have a function:

my3DGauss[x_, y_, z_] := Exp[-(x^2 + y^2 + z^2)/2];

I try to make a transparent 3D contour plot of it:

ContourPlot3D[my3DGauss[x, y, z], {x, -3, 3}, {y, -3, 3}, {z, -3, 3}, 
 Contours -> {0.1, 0.2, 0.4, 0.7}, 
 RegionFunction -> Function[{x, y, z}, x > 0], Mesh -> None, 
 ColorFunction -> (ColorData[{"AlpineColors", "Reverse"}][#4] &), 
 ContourStyle -> Directive[Opacity[0.8], Specularity[White, 30]], 
 PerformanceGoal -> "Speed"]

This renders fine (if jagged).

Correct color rendering with PerformanceGoal->"Speed"

If I try to make a quality plot though, the results look horrible:

ContourPlot3D[my3DGauss[x, y, z], {x, -3, 3}, {y, -3, 3}, {z, -3, 3}, 
 Contours -> {0.1, 0.2, 0.4, 0.7}, 
 RegionFunction -> Function[{x, y, z}, x > 0], Mesh -> None, 
 ColorFunction -> (ColorData[{"AlpineColors", "Reverse"}][#4] &), 
 ContourStyle -> Directive[Opacity[0.8], Specularity[White, 30]], 
 PerformanceGoal -> "Quality"]

The colors are all mottled, with off-color artifacts reminiscent of what is presumably the underlying mesh (which has been suppressed in output).

Mottled color rendering with PerformanceGoal->"Quality"

Things I have tried:

  1. Increasing PlotPoints
  2. Increasing MaxRecursion
  3. Increasing MaxPoint
  4. Using an indexed list of colors instead of a ColorFunction
  5. Specifying the opacity various ways (e.g. in a list of colors, instead of ContourStyle Directive)
  6. Using LegacyAlphaChannel option under RenderingOptions
  7. Playing with 3DRenderingEngine: "Direct3D", "Mesa"
  8. Playing with 3DRenderingMethod: "HardwareDepthBuffer", "HardwareDepthPeeling", "BSPTree"
  9. Other things not worth mentioning here

Option (4) seemed like a no-fail work-around. It got rid of the mottling, but a new problem arose: the colors were all off (they all looked bizarrely orangey).

Fun irrelevant fact, I have been using Mathematica since 2.0, and this is my first SE question. :)

I decided this must be a bug, and so I was hoping that when I upgraded from 11.0 -> 12.0 it would fix it. No such luck.

System: Windows 10. NVIDIA Quadro P1000 graphics card.

Planning on trying this in Linux and/or cloud-based (?) Mathematica, which I haven't tried yet.

Thanks, Peter

$\endgroup$
1
  • $\begingroup$ I can confirm that this occurs on Mathematica 10.1 on Ubuntu 18.04 LTS. $\endgroup$ Mar 28, 2020 at 12:32

1 Answer 1

10
$\begingroup$

A work-around: (1) Remove the ColorFunction option and (2.a) use ContourStyle to specify the contour styles using the desired color function or (2.b) use the form $\{\ldots,\{contour_i, style_i\},\ldots\}$ to set the option Contours.

contours = {0.1, 0.2, 0.4, 0.7};
cstyles = Directive[Lighting -> "Neutral", Opacity[0.8], Specularity[White, 30], 
     ColorData[{"AlpineColors", "Reverse"}]@#] & /@ Rescale[contours];


cp1 = ContourPlot3D[my3DGauss[x, y, z], {x, -3, 3}, {y, -3, 3}, {z, -3, 3}, 
   Contours -> contours, RegionFunction -> Function[{x, y, z}, x > 0],
   Mesh -> None, ContourStyle -> cstyles, PerformanceGoal -> "Quality", 
   ImageSize -> Medium,  ViewPoint -> {-3, -2, 1}];

styledcontours = Thread[{contours, cstyles}];    

cp2 = ContourPlot3D[my3DGauss[x, y, z], {x, -3, 3}, {y, -3, 3}, {z, -3, 3}, 
   Contours -> styledcontours, RegionFunction -> Function[{x, y, z}, x > 0], 
   Mesh -> None, PerformanceGoal -> "Quality", ImageSize -> Medium, 
   ViewPoint -> {-3, -2, 1}];

Row[{cp1, cp2}, Spacer[10]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks, kglr. Notice how the colors have taken on an orange tint though? Any ideas how to get rid of that? Compare the colors with the colors in my posting. If I could get rid of that orange tint, this would be a good work-around. $\endgroup$ Mar 28, 2020 at 14:55
  • 1
    $\begingroup$ Adding Lighting->"Neutral" fixed the orange tint issues. Fantastic. Thanks! (Not sure why that is required when using ContourStyle instead of ColorFunction, but still, that fixes the orange-tint problem.) $\endgroup$ Mar 28, 2020 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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