[<< wikibooks] Cg Programming/Unity/Soft Shadows of Spheres
This tutorial covers soft shadows of spheres.
It is one of several tutorials about lighting that go beyond the Phong reflection model, which is a local illumination model and therefore doesn't take shadows into account. The presented technique renders the soft shadow of a single sphere on any mesh and is somewhat related to a technique that was proposed by Orion Sky Lawlor (see the “Further Reading” section). The shader can be extended to render the shadows of a small number of spheres at the cost of rendering performance; however, it cannot easily be applied to any other kind of shadow caster. Potential applications are computer ball games (where the ball is often the only object that requires a soft shadow and the only object that should cast a dynamic shadow on all other objects), computer games with a spherical main character (e.g. “Marble Madness”), visualizations that consist only of spheres (e.g. planetary visualizations, ball models of small nuclei, atoms, or molecules, etc.), or test scenes that can be populated with spheres and benefit from soft shadows.


== Soft Shadows ==
While directional light sources and point light sources produce hard shadows, any area light source generates a soft shadow. This is also true for all real light sources, in particular the sun and any light bulb or lamp. From some points behind the shadow caster, no part of the light source is visible and the shadow is uniformly dark: this is the umbra. From other points, more or less of the light source is visible and the shadow is therefore less or more complete: this is the penumbra. Finally, there are points from where the whole area of the light source is visible: these points are outside of the shadow.
In many cases, the softness of a shadow depends mainly on the distance between the shadow caster and the shadow receiver: the larger the distance, the softer the shadow. This is a well known effect in art; see for example the painting by Caravaggio to the right.


== Computation ==
We are going to approximately compute the shadow of a point on a surface when a sphere of radius 
  
    
      
        
          r
          
            sphere
          
        
      
    
    {\displaystyle r_{\text{sphere}}}
   at S (relative to the surface point) is occluding a spherical light source of radius 
  
    
      
        
          r
          
            light
          
        
      
    
    {\displaystyle r_{\text{light}}}
   at L (again relative to the surface point); see the figure to the left.
To this end, we consider a tangent in direction T to the sphere and passing through the surface point. Furthermore, this tangent is chosen to be in the plane spanned by L and S, i.e. parallel to the view plane of the figure to the left. The crucial observation is that the minimum distance 
  
    
      
        d
      
    
    {\displaystyle d}
   of the center of the light source and this tangent line is directly related to the amount of shadowing of the surface point because it determines how large the area of the light source is that is visible from the surface point. More precisely spoken, we require a signed distance (positive if the tangent is on the same side of L as the sphere, negative otherwise) to determine whether the surface point is in the umbra (
  
    
      
        d
        <
        −
        
          r
          
            light
          
        
      
    
    {\displaystyle d<-r_{\text{light}}}
  ), in the penumbra (
  
    
      
        −
        
          r
          
            light
          
        
        <
        d
        <
        
          r
          
            light
          
        
      
    
    {\displaystyle -r_{\text{light}}