8

Is there a way to define more than one "spot" of light for one shading? Two (default white highlight and a big pink reflected light spot) were good and three were just great.

photorealistic radial shading in TikZ

I'm using

\shadedraw[line width=0.15mm, white, ball color=black] (0, 0, 0) circle (1.75mm); 

This modification:

\makeatletter
\pgfdeclareradialshading[tikz@ball]{ball}{\pgfqpoint{-20bp}{20bp}}{%
color(0bp)=(tikz@ball!0!white);
color(17bp)=(tikz@ball!0!black);
color(21bp)=(tikz@ball!0!black);
color(25bp)=(black!70);
color(30bp)=(black!70)}
\makeatother

looks pretty good:

sphere

  • 1
    Is there a question here? – cfr Sep 19 '15 at 15:58
21

It can be done with fadings:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{fadings}
\pgfdeclareverticalshading{up}{100bp}
{color(0bp)=(pgftransparent!0); color(50bp)=(pgftransparent!0);
 color(75bp)=(pgftransparent!25); color(100bp)=(pgftransparent!25)}
\pgfdeclarefading{up}{\pgfuseshading{up}}    
\pgfdeclareradialshading{thick ring}{\pgfpointorigin}{
  color(0pt)=(pgftransparent!100); color(15pt)=(pgftransparent!100); 
  color(20)=(pgftransparent!0);
  color(25bp)=(pgftransparent!100); color(50bp)=(pgftransparent!100)}
\pgfdeclarefading{thick ring}{\pgfuseshading{thick ring}}    
\begin{tikzfadingfrompicture}[name=ball]
\shade [shading=ball, ball color=black] circle [radius=1];
\end{tikzfadingfrompicture} 
\begin{tikzfadingfrompicture}[name=ring]
\fill [white, path fading=thick ring] circle [radius=1];
\fill [black, path fading=up, fading angle=215] circle [radius=1];
\end{tikzfadingfrompicture}    

\tikzset{shiny ball/.style={
  fill=none, draw=none, shading=ball, shading angle=-15,
  postaction={fill=white, path fading=ball, opacity=0.75, fading angle=45},
  postaction={fill=white, path fading=ring}
}}
\begin{document}
\tikz\foreach \c [count=\i]
  in {red, yellow, green!50!black, brown, blue, pink!50!purple, black, white}
    \path [shiny ball, ball color=\c] (360/8*\i:3) circle [radius=1];
\end{document}

enter image description here

And it is rather hard to resist reusing the code above and adding this:

\pgfdeclareradialshading{shadow}{\pgfpointorigin}{
  color(0pt)=(pgftransparent!0); color(25bp)=(pgftransparent!100)}
\pgfdeclarefading{shadow}{\pgfuseshading{shadow}} 
\tikzset{pics/ball/.style args={#1 : #2}{code={
\fill [path fading=shadow, fit fading=true]
  (300:#2*1/3) ellipse [x radius=#2*2/3, y radius=#2*1/3];
\path [shiny ball, ball color=#1] circle [radius=#2/2];
}}}
\begin{tikzpicture}
\clip [preaction={top color=green!50!olive, bottom color=green!50!black}]
  (-12,-8) -- (-6,8) -- (6,8) -- (12,-8) -- cycle;
\draw [green!50!olive!75!white, ultra thick] 
  (-8,6-1/9) -- (8,6-1/9) (-2,6-1/9) arc (180:0:2 and 1.125);
\pic at (-2,6) {ball=yellow : 5/9};
\pic at (0,6)  {ball=green!50!black : 5/9};
\pic at (2,6)  {ball=brown!75!red : 5/9};
\pic at (0,3)  {ball=blue : 7/9};
\pic at (0,0)  {ball=pink : 1};
\foreach \i [evaluate={\k=1+\i/25;}] in {1,...,5}
   \foreach \j in {1,...,\i}
     \pic at ({\k*(-\i/2-1/2+\j)},-\k*\i/2) {ball=red : \k};
\pic at (0,-6) {ball=black : 10/9};
\pic at (3,3/2) {ball=white : 8/9};
\end{tikzpicture}

enter image description here

But as fadings are not very portable (i.e., pdf output only and even then fadings may only be visible if the pdf viewer supports them), here is an alternative which sort of does it without fadings. As long as you don't look too closely. Works better with some colors than others.

\documentclass[border=5]{standalone}
\usepackage{tikz}
\begin{document}
\foreach \c in {black, red, yellow, green, blue}{
\begin{tikzpicture}[ball color=\c]
\foreach \i in {0,0.25,...,10}
   \fill [black, opacity=1/50] (270:1) 
     ellipse [x radius=\i/10, y radius=\i/20];
\shade [shading=ball, shading angle=-15]
  circle [radius=1];
\foreach \i in {0,0.25,...,10}
   \fill [white, opacity=1/75] (180:0.5) circle [radius=\i/30];

\foreach \i [evaluate={\j=\i*3;}] in {0,0.25,...,10}
  \draw [white, opacity=1/200, line width=\i, line cap=round]
   (0+\j:0.875) arc (0+\j:-90-\j:0.875);
\end{tikzpicture}}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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