Weird shading artifacts in ContourPlot3D
For some reason, the VertexColors
of the recursively computed vertices are set incorrectly. I think this should be reported to Wolfram Support.
Anyways, here is a quick fix:
f = {x, y, z} \[Function] 1/Sqrt[x^2 + y^2 + z^2];
g = ContourPlot3D[
f[x, y, z], {x, -1, 1}, {y, 0, 1}, {z, -1, 1},
Ticks -> None,
Contours -> Range[5],
ColorFunction -> "TemperatureMap"
];
pos = Position[g, _GraphicsComplex][[1]];
GC = Extract[g, pos];
colpos = Append[#, 2] & /@ Position[GC, VertexColors -> _][[{-1}]];
GC = ReplacePart[GC,
colpos -> List @@@ (ColorData["TemperatureMap"] /@ Rescale[f @@@ GC[[1]]])
];
ReplacePart[g, pos -> GC]
Henrik Schumacher's answer is great and the basis of this extension. He deserves 100% of the credit for this (and no blame for any errors I've introduced below).
I am extending it merely because his answer fixes the MWE given in the OP, but I want now to automate it and generalize to predefined and user-defined color functions (of one variable... multivariable ones are left as an exercise to anybody interested ;) ). Here's my amateur attempt to do so:
PrettyContourPlot3D[o__]:=Module[
{g=ContourPlot3D[o],
f={o}[[1]],
args={o}[[2;;4,1]],
cfp=Position[{o},ColorFunction],
cf,gcp,GC,cp
},
If[Length[cfp]>0,
cf={o}[[cfp[[1,1]],2]];
If[StringQ[cf],cf=ColorData[cf]];
gcp=Position[g,_GraphicsComplex][[1]];
GC=Extract[g, gcp];
cp=Append[#,2]&/@Position[GC,VertexColors->_][[{-1}]];
GC=ReplacePart[
GC,
cp->List@@@(ColorConvert[#,"RGB"]&/@cf/@Rescale[
f/.{args[[1]]->#1,args[[2]]->#2,args[[3]]->#3}&@@@GC[[1]]])
];
ReplacePart[g, gcp -> GC],
g
]
]