# Gray shaded sphere with tikz-3dplot

How do I draw a grayscale shaded sphere with tikz-3dplot? I tried using \tdplotsphericalsurfaceplot with radius equal to 1, and parametricfill depending on the spherical angles. However, it looks like I can only prescribe the color's hue (in HSB space), according to the following post, which does not suit me. I could go all the way and redefine \tdplotdosurfaceplot, but that seems overkill.

Let me emphasize that I want true 3D drawing (the rest of my picture requires 3D coordinates: I need to draw arcs), so I cannot use 2D tricks such as

\draw [ball color=white] (0,0,0) circle (1) ;


so this is not a duplicate of how-to-draw-a-shaded-sphere. I explicitly want a 3d drawing, preferably with tikz-3dplot, though I'm open to other 3d packages.

Here are two MWEs: the first sphere uses uniform coloring, while the second uses a linear combination of the spherical angles:

\begin{tikzpicture}[tdplot_main_coords]
\tdplotsphericalsurfaceplot[parametricfill]{24}{24}{1} {black}{50}{}{}{}
\begin{scope}[xshift=4cm]
\tdplotsphericalsurfaceplot[parametricfill]{24}{24}{1} {black}{\tdplottheta+\tdplotphi}{}{}{}
\end{scope}
\end{tikzpicture}


I would also like to get rid of the parametric lines... Maybe this is not the right package to work with, or the right command in the package.

• pgfplots grant you full access by default. Keywords: surf and point meta – Symbol 1 Jul 13 '17 at 1:23
• If you want 'true 3D', PGF/TikZ is not the package to choose. You can use it to fake 3D in 2D, but it does not do real 3D at all. tikz-3dplot helps do calculations required to fake 3D in 2D. – cfr Jul 17 '17 at 15:28
• @cfr I'll be happy to know other options. My goal is to draw a circular arc on the sphere, given 3d coordinates of its endpoints. – Pascal Romon Jul 18 '17 at 12:48
• Thanks to all answers, I can now rephrase my question: I want to draw circular arcs on a gray shaded sphere, with actual 3d coordinates of the spherical points. tikz-3dplot is reasonably good at drawing circular arcs, but poor at shading the sphere. pgfplots excels at drawing graphs, including the sphere, but afaik does not know how to draw circular arcs (or in a very complicated way). Moreover the two do not work well together. Any idea of a package that would allow me to reach both? – Pascal Romon Jul 23 '17 at 18:30

Better use pgfplots (adapted from this post). The parametric fill with \theta+\phi looks pretty weird in my opinion.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
width=6cm,height=6cm,
axis equal,enlargelimits=false,
axis lines=none,
domain=0:180,samples=21,
y domain=0:360,samples y=21,
colormap/blackwhite,
view={100}{10},
]
[
surf,
z buffer=sort,
point meta={acos(z/sqrt(x*x+y*y+z*z)) + atan2(y,x)}
] (
{sin(x)*cos(y)},
{sin(x)*sin(y)},
{cos(x)}
);
\end{axis}
\end{tikzpicture}
\end{document}


• Thanks @henri-menke. Happy to learn about this package. It does what I wanted, especially with the shader=interp option. However, it does no play so well with the rest of my code in tikz-3dplot. The reason being (I suppose) that the axis environment defines its own 3d coordinates; hence any subsequent 3d instruction is drawn w.r.t. another one. I'll have to look it up. Btw, the choice of shading function \theta+\phi was just an example, not the real thing. – Pascal Romon Jul 18 '17 at 12:46
• My mistake: I only need to write the rest of the 3d code within the axis environment. – Pascal Romon Jul 18 '17 at 13:14

After trying bits here and there, I have come up with an acceptable solution, which does use tikz-3dplot (with shortcomings, see below). Here's a MWE, showing a spherical triangle drawn on the shaded sphere. For different shadings, simply go to how-to-draw-a-shaded-sphere.

The solution below cheats partially: it does not draw a shaded sphere but rather a shaded 2D disc in perspective; therefore one has to rotate it in 3-space to make it appear like a sphere (hence the rotated coordinates). However the spherical coordinates of the arcs are true.

\documentclass[11pt]{article}
\usepackage{tikz,tikz-3dplot}
\tdplotsetmaincoords{80}{110}

\begin{document}
\begin{figure}
\begin{center}
\begin{tikzpicture}[scale=3,tdplot_main_coords]
% spherical background
\tdplotsetrotatedcoords{20}{80}{0}
\draw [ball color=white,very thin,tdplot_rotated_coords] (0,0,0) circle (1) ;
% equator
\draw [dashed] (0,0,0) circle (1) ;
% spherical triangle
\tdplotdefinepoints(0,0,0)(0.8,-0.4,-0.4)(0.4,0.8,-0.4)
\tdplotdrawpolytopearc[thick]{1}{}{}
\tdplotdefinepoints(0,0,0)(0.4,0.8,-0.4)(0.45,0.22,0.9)
\tdplotdrawpolytopearc[thick]{1}{}{}
\tdplotdefinepoints(0,0,0)(0.45,0.22,0.9)(0.8,-0.4,-0.4)
\tdplotdrawpolytopearc[thick]{1}{}{}
\end{tikzpicture}
\end{center}
\end{figure}
\end{document}


Another shortcoming is the awkward syntax in tikz-3dplot for defining arcs, and the fact that you cannot mix it with the coordinate command.