As we’ve seen, a shading model performs select lighting calculations to shade facets. For example, Lambert shading performs lighting calculations at the vertices and then interpolates colors across facet interiors. Phong shading performs a lighting calculation at every fragment of a facet. Whatever the shading model, the lighting calculation applies some lighting model.
A lighting calculation may take into account both local and global factors. Local factors include surface geometry and material, and surface orientation with respect to light sources and the viewer. In contrast, global factors account for interaction of light between objects in the scene graph: reflection and refraction between objects, casting of shadows, lensing effects, and the like. Accounting for global factors is computationally expensive, so real-time systems like three.js provide limited support for them 1. Here here we’ll consider a standard lighting model that accounts only for local factors.
Light strikes a surface and, through reflection, illuminates the surface. If the surface is rough, the reflected light scatters in all directions. If the surface is smooth, the reflected light is fairly focused in a certain direction. Surfaces such as dirt and carpets are very rough, and surfaces such as mirrors, water, and polished metals are very smooth, but most surfaces are a blend of rough and smooth. Here, ‘rough’ and ‘smooth’ refer to a surface’s features at microscopic level at which it interacts with light, which roughly correlates with the surface finish we see.
Diffuse reflection is reflection due to a material’s roughness. When we refer to the color of an object — brown wood, green grass, blue sky — we’re talking about its diffuse color. The amount of light a surface reflects diffusely depends on how much light energy falls on the surface. We break light into two components:
Ambient light, which has neither source nor direction, just fills space with a uniform intensity. There is ambient light at Earth’s surface because our atmosphere scatters light, whereas there’s no ambient light in outer space since there’s no atmosphere to scatter light.
Diffuse light strikes a surface from a certain direction. The amount of diffuse light that strikes a surface depends on its orientation to the light source: A lot of light if the light illuminates the surface from above, but little light if it just grazes the surface from an oblique angle. Surface orientation with respect to the light source is represented by the angle between the normal vector to the surface and the light vector that points to the light source. The amount of diffuse light that strikes the surface is approximated by . When is close to zero (the light source is overhead), ; and when is close to (the light grazes the surface), .
We’ll let denote the intensity of ambient light and the intensity of the diffuse light. We’ll also let denote the percentage of ambient light reflected by the surface, and the percentage of diffuse light reflected by the surface. ( and are called ambient and diffuse reflection coefficients respectively.) Then the intensity of light reflected due to surface roughness (scattering in all directions) is given by
Where the and are unit vectors (i.e., of length one), we have , so this simplifies to2
We turn to the intensity contribution due to specular reflection, that is, due to a material’s smoothness. This reflection appears as a shiny spot, or specular highlight, that reflects into eye. To account for specular reflection, the orientation of the surface with respect to the viewer’s position comes into play. Let be the reflection vector, the direction in which the light would be reflected if the surface were a perfect mirror. And let be the vector toward the viewer. The angle between and quantifies how far the viewer is from the center axis of specular highlight. We approximate the amount of specular light that the eye sees by where is the shininess exponent. The specular highlight is increasingly tight and well-defined with higher values of : A rough surface is modeled by whereas the surface becomes more mirrorlike as increases.
Let be the specular reflection coefficient for the material. Then the intensity contribution due to specular reflection is given by
Combining diffuse and specular contributions over multiple light sources yields the following equation for intensity at a point of a surface:
The following program can be used to experiment with this lighting model. Both surfaces are assigned the MeshPhongMaterial
.
The ambient light and spotlight both cast white light whose brightness you can adjust with the two intensity sliders. The diffuse color selector sets both the ambient and diffuse reflection coefficient. In effect, it controls the fraction of reflected light in the color components red, green, and blue. The specular color selector is used similarly to set the specular coefficient in each of the color components. Shininess controls the shininess exponent.