Surface Shading
|
|
Creating Fake Lighting Effects
A surface shader generally calculates the apparent color of a surface (Ci) by combining the true color (Cs) and opacity (Os) of a surface with the various effects of the illumination a surface receives. This tutorial shows how it is possible to create the illusion of soft rim lighting without the use of any light sources. |
|||||||||||
|
Basic Code
To keep the code developed in this tutorial as simple as possible the
shader shown below does not calculate the true values of the ambient,
diffuse and specular lighting components. Instead, it will apply a fake
(diffuse) rim lighting effect based on the angle between the viewing
vector and the surface normal.
|
Fig 1 |
||||||||||
|
Using the Viewing Vector and Surface Normal
The following illustration shows how the angle between the surface normal and light-of-sight of the camera ie. the viewing vector, changes from 0.0 degrees at the center of the object (approximated by location A) to 90.0 degrees at the rim (location B).
|
|
The apparent brightness of a surface can be based on the way this angle (shown in blue) changes across an object. A convenient method of calculating this angle is via the vector multiplication known as the dot product (also called the scalar or inner product) ie.
The snippet of code shown above uses normalized copies of the surface normal (nf) and the (reversed) viewing vector (i). The dot product yields the cosine of the angle between the two vectors. The shading effect is shown in fig 2. Inverting the values by subtracting the dot product from 1.0 gives the shading effect shown in fig 3.
The important point to note is that because the normal used in these calculations has been forced to face the camera, and hence we are only dealing with angles between 0.0 and 90 degrees, the cosines of this range of angles, calculated by the dot product, is in the rangle 0.0 to 1.0. In other words we are applying a fake lighting effect. |
Fig 2
Fig 3 |
||||||||
|
Controlling the Width of the Rim Effect
Naturally, the shader should allow an artist to control the width of the rim effect. By providing an instance variable, say, rim_width we can use the smoothstep() function to modify the range of values across a surface. For example,
Using a rim_width of 0.6 gives the effect shown in fig 4.
Fig 5
|
Fig 4 |
|||||||||||
|
Fake Lighting Direction
The chair in fig 6 has been rendered using a modified version of the shader - listing 4. Notice that step 2 consists of two parts. In step 2.1 a uniform value of Oi is obtained. In step 2.2 Oi is scaled by the dot product of the surface normal and instance variable direction - this provides an illusion of a (fake) lighting direction.
|
Fig 6 |
||||||
© 2002-5 Malcolm Kesson. All rights reserved.