33

TikZ shading=ball creates a perfect 3D ball perspective, but it has no option to change the lighting angle, the contrast, radius of light, etc (or I am not aware of). I tried to create a 3D ball by radial shading. However, I still do not know how to change the place of inner color.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}

\begin{tikzpicture}
\node[circle,shading=ball,minimum width=3cm] (ball) at (0,0) {};
\node[circle,outer color=blue!60!black,inner color=white,minimum width=3cm] (radial) at (4,0) {};
\end{tikzpicture}

\end{document}

enter image description here

Which is the standard method to customize the 3D lighting?

1

1 Answer 1

38

You should go to section 83.2.2 Radial Shadings of the pgfmanual (version October 25, 2010).

Here is a solution in which the color of that example is a bit changed and different center points are used:

\documentclass{article}
\usepackage{pgf}
\begin{document}

% spheres definitions

\pgfdeclareradialshading{sphere}{\pgfpoint{0cm}{0cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.7cm)=(0.7,0.1,0); rgb(1cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}

\pgfdeclareradialshading{sphere1}{\pgfpoint{0.5cm}{0cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.7cm)=(0.7,0.1,0); rgb(1cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}


\pgfdeclareradialshading{sphere2}{\pgfpoint{0.5cm}{0.5cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.7cm)=(0.7,0.1,0); rgb(1cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}


\pgfdeclareradialshading{sphere3}{\pgfpoint{0cm}{0.5cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.7cm)=(0.7,0.1,0); rgb(1cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}

\pgfdeclareradialshading{sphere4}{\pgfpoint{-0.2cm}{0.5cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.7cm)=(0.7,0.1,0); rgb(1cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}



\pgfuseshading{sphere}\pgfuseshading{sphere1}\pgfuseshading{sphere2}\pgfuseshading{sphere3}\pgfuseshading{sphere4}

\end{document}

This leads to:

enter image description here


To further customize the lighting you should play with:

  • the coloring;
  • the coloring position.

To be more clear, rgb(0.7cm)=(0.7,0.1,0); it's saying that you want the color (0.7,0.1,0) at 0.7cm.

Here is a slightly different example:

\documentclass{article}
\usepackage{pgf}
\begin{document}

% spheres definitions

\pgfdeclareradialshading{sphere}{\pgfpoint{0cm}{0cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.7cm)=(0.7,0.1,0); rgb(1cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}

\pgfdeclareradialshading{sphere1}{\pgfpoint{0.5cm}{0cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.4cm)=(0.7,0.1,0); rgb(0.6cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}


\pgfdeclareradialshading{sphere2}{\pgfpoint{0.5cm}{0.5cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.9cm)=(0.7,0.1,0); rgb(1cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}


\pgfdeclareradialshading{sphere3}{\pgfpoint{0cm}{0.5cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.5cm)=(0.7,0.1,0); rgb(0.75cm)=(0.6,0.075,0); rgb(1cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}

\pgfdeclareradialshading{sphere4}{\pgfpoint{-0.2cm}{0.5cm}}% 
{rgb(0cm)=(1,1,1);
rgb(0.2cm)=(0.7,0.1,0); rgb(0.9cm)=(0.5,0.05,0); rgb(1.05cm)=(1,1,1)}


\pgfuseshading{sphere}\pgfuseshading{sphere1}\pgfuseshading{sphere2}\pgfuseshading{sphere3}\pgfuseshading{sphere4}

\end{document}

which gives you:

enter image description here

3
  • Thanks, I found it and in the subsequent section 83.3. My bad! but it still has lesser options than what I expected to have for flexible 3D lighting.
    – Googlebot
    Commented Jun 5, 2012 at 12:00
  • 1
    I'll edit my question to further customize the lighting. Commented Jun 5, 2012 at 12:09
  • That's something useful that I was unable to find in the manual.
    – Googlebot
    Commented Jun 5, 2012 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .