[Tex/LaTex] TikZ, 3d library and circle fading

3dpgfplotsshadingtikz-pgf

I'm using the 3d library to put a circle on the z=0 plane, and other things. I'd like to fade this circle radially, but the result isn't as I would expect because the fading is not proportional to the distance from the origin. How can I do?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{3d,fadings}
\tikzfading [name=radialfade, inner color=transparent!0, outer color=transparent!100]

\begin{document}
\begin{tikzpicture} [x={(-0.3535,-0.3535cm)}, y={(1cm,0cm)}, z={(0cm,1cm)}, scale=3]
\begin{scope} [canvas is xy plane at z=0]
\fill [blue, path fading=radialfade] circle (1);
\end{scope}
\draw[-latex] (-1,0,0) -- (1,0,0) node [left] {$x$};
\draw[-latex] (0,-1,0) -- (0,1,0) node [below] {$y$};
\draw[-latex] (0,0,-0.5) -- (0,0,0.5) node [left] {$z$};
\end{tikzpicture}
\end{document}

enter image description here

Best Answer

As already mentioned by Andrew Stacey in some comment, TikZ is not a real 3D system - and a radial fading is a two-dimensional construct.

You would need some 3d projection algorithm to compute the colors.

A possibility would be to sample such a projection and to interpolate between the sampled points. This is not directly supported by TikZ (because it involves more complicated shadings), but you can use pgfplots and its surf plot handler to get the effect:

EDIT: I did not realize that you asked explicitly for the axis direction vectors of your example. I added them to the answer.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=1.7} % *EDIT*: this improves scale uniformly.

\begin{document}
\begin{tikzpicture}
    \begin{axis}[axis on top,
        axis lines=center,
        min=-1,max=1,
        colormap={somename}{color=(white) color=(blue)},
        % *EDIT*: this here respects your choice of unit vectors.
        %         scale uniformly computes one common scaling factor
        %         and chooses limits such that the image fulfills the 
        %         prescribed width/height as best as possible.
        x={(-0.3535cm,-0.3535cm)}, y={(1cm,0cm)}, z={(0cm,1cm)}, scale mode=scale uniformly,
    ]

    \addplot3[surf,
        shader=interp,
        %z buffer=sort, % only for complete sphere
        samples=30,
        domain=-1:0, % -1:1 is a complete sphere. -1:0 half
        y domain=0:2*pi,
        variable=\t,
        point meta=t^2]
    ({sqrt(1-t^2) * cos(deg(y))},
     {sqrt( 1-t^2 ) * sin(deg(y))},
     0);% use 't' here to draw the sphere

    \end{axis}
\end{tikzpicture}
\end{document}

The idea is to use a sphere parameterization of the form (x(t,y), y(t,y), 0), i.e. to fix z=0. The key point meta defines the color data. I figured that t would be the correct choice, but t^2 looks better. The surf,shader=interp key draws the sampled parametric plot as surface with interpolated colors.

The colormap uses RGB color interpolation. What is transparent in your example? I defined a colormap using blue and white.

EDIT: just in case you are using pgfplots 1.7 or higher, you can use the new feature patch type sampling combined with a higher-order patch like patch type=bicubic. This allows to reconstruct the geometry with smooth(er) boundaries.

The following example has been generated with \usepgfplotslibrary{patchplots}

and

patch type sampling, patch type=bicubic,
samples=10,% CF reduced because bicubic is smooth anyway

enter image description here