Shadow Algorithms
Many rendering programs let you choose between two popular techniques to calculate shadows:
- Depth map (also called shadow map) shadows are typically the quickest and most efficient to render, but have a finite resolution and sometimes need to be adjusted (as described below) to avoid artifacts.
- Raytraced shadows are easy to use and accurate at any resolution, but usually take more rendering time to compute.
The following sections discuss how to use depth map shadows and raytraced shadows, along with their advantages, disadvantages, and options for adjusting their appearance.
Depth Map Shadows
Depth map shadows are currently the most popular kind of shadows used in professional lighting work for film. This kind of shadow works by precomputing a depth map to determine where shadows will be rendered.
A depth map (sometimes abbreviated dmap; also called a shadow map) is an array of numbers representing distances. Before the renderer even begins rendering the scene viewed by the camera, it computes a depth map from the point of view of each light that will cast depth mapped shadows. For each direction that the light shines, the depth map stores the distance from the light to the nearest shadow-casting object found in that direction, as shown in Figure 3.12.
Figure 3.12 A depth map shadow is based on an array of distance measurements from the light to the nearest visible geometry, shown here as white lines.
During the rendering, the light will be cut off at the distances specified by the depth map, so that it does not shine farther than the distance stored for each angle. When rendering a surface under the apple, for example, the renderer needs to check only the depth map to see which parts of the ground are shadowed and which aren't. This saves a tremendous amount of rendering time, because the renderer doesn't need to repeatedly check through the scene to verify where objects come between the ground and the light.
Figure 3.12 shows only one row of depth measurements, as would exist in one "slice" of a depth map. A depth map would actually run both horizontally and vertically over the area illuminated by the light. For example, a depth map with a resolution of 512 would actually be 512 distance measurements wide by 512 distance measurements high.
Resolution and Memory Use
A single distance measurement within a depth map is stored as a floating point value. Floating point values can store just about any number, from tiny fractions to huge distances, but use 4 bytes to store each value. The resolution of a shadow map is used for both the horizontal and vertical dimensions of the map, meaning the number of bytes used is actually 4*(resolution^2). Table 3.1 shows the memory used by common shadow map resolutions, in megabytes.
Table 3.1. Shadow Map Memory Use
Depth Map Resolution |
Memory Used |
128 |
0.06 MB |
256 |
0.25 MB |
512 |
1 MB |
1024 |
4 MB |
2048 |
16 MB |
4096 |
64 MB |
As Table 3.1 shows, increasing your shadow map resolution will rapidly deplete your system's memory. As general defaults to start with, on a well-framed spotlight, a shadow map resolution of 512 will usually be adequate for television, and a resolution for 1024 will usually be good for final shots in a feature film.
Depth Map Framing
To make shadow maps work efficiently, you need to frame your shadow map so that it covers the geometry in the shadow and not too much extra empty space around it.
Spotlights are the lighting tools of choice for most projects that use depth map shadows. You can aim and control a spotlight to put your shadow exactly where it's needed. If you used an omnidirectional (point) light, then the software would need to calculate multiple depth maps in order to calculate the shadows cast in each direction. If you used a directional (infinite) light, then the depth map shadow might be stretched over too broad an area to optimize its resolution.
If you are lighting with a spotlight, make sure that the cone angle is focused as tightly as possible around your subject, so that you don't waste samples in your depth map shooting off into empty space. Figure 3.13 shows how a shadow map works efficiently for a narrow cone angle, but becomes lower resolution at a wider cone angle, as its samples are wasted over a wider area. If the light is very far away from your subject, you may be able to use a very narrow cone angle. You can check your framing by viewing the subject from the light's point of view, to make sure there isn't too much empty space around the subject within your shadow map.
Figure 3.13 A spotlight that is poorly aimed, with its cone angle covering too wide an area, will waste most of the samples within a shadow map and produce inaccurate results (left), while a spotlight with a cone tightly focused around the subject uses the shadow map efficiently for an accurate shadow (right).
There may be situations when it seems impossible to properly frame a depth map for everything that needs to be shadowed. For example, if the sun were lighting a very large scene including a tree, a house, and an animated character, you'd really want a depth map framed neatly around each of these things. In programmable renderers such as Mental Ray and Renderman, studios can implement solutions providing control over which objects are in each shadow map, and which shadow maps a given light uses. In most off-the-shelf 3D software, however, there is no option to specify an arbitrary list of shadow maps that will be used by a light, nor is there an option that allows you to frame each of those shadow maps differently. When you are stuck with a shadow map covering too wide an area, and can't frame it any tighter because of the number of objects it needs to light, you have several choices:
- Replace your light with several spotlights, each covering a smaller area. This will add a little bit of natural variation to the lighting, which is often a good thing.
- Turn off shadows in the main light, and instead use shadows-only lights (described later in this chapter) to create shadows beneath each object.
- Use different well-focused spotlights for each shadow in a separate shadow pass, then use the shadow pass to darken the scene in compositing software. (Chapter 11, "Rendering Passes and Compositing," goes into detail about this.)
- Raise the resolution of the shadow map as high as necessary. Be mindful of the memory and performance hit that this solution will take, though. If test renders show that you need to go above 2048 or 4096, you may even find that a raytraced shadow (discussed below) could be more efficient in some cases.
Depth Map Bias and Self-Shadowing Artifacts
Artifacts like bands or grid patterns (as shown in Figure 3.14), are often caused by having a parameter called depth map bias set too low. For scenes built on a very large scale, you may need to raise the bias of some shadows to eliminate such artifacts.
Figure 3.14 A depth map bias that's too low causes artifacts that appear to be stripes, grids, or moiré patterns, shown at a bias of 0.005 (left), 0.02 (middle), and 0.25 (right).
Bias is a number that gets added to each distance measurement in the shadow map, pushing the shadow-start-distance further out from the light. Increasing the bias slides the shadowing effect out a little further away from the surface casting the shadow, so that it doesn't accidentally start too soon and cause artifacts. Artifacts such as banding or grid patterns appear because points on the surface essentially begin shadowing themselves, in areas where the limited number of depth samples have under-estimated the actual distance from the light to the surface.
The bias is set in the distance units of your software, so a scene built on a larger scale might need a higher bias, or a scene built on a very small scale might need a lower bias.
Fixing Light Leaks
Too high a bias can cause visible gaps between the object casting a shadow and the point where the shadow itself starts. A high bias can even let light "leak" through walls and corners that should be blocking the light, as shown in Figure 3.15.
Figure 3.15 A depth map bias that's too high, combined with thinly built geometry, can cause light leaks (left). Lowering the bias (right) is one way to fix this problem.
If you have light leaking through corners, there are some things you should do to isolate and fix the problem:
- Hide all the other lights in your scene, so that you are test-rendering only one light.
- If you are using a spotlight, make sure the cone is as narrow as possible to aim it just where it needs to shine.
- Reduce the depth map bias.
- Reduce the filtering or softness applied to the shadow, which extends light into shadow areas in much the same way as an increased bias.
A light leak is not always the fault of the lighting; often, it is a problem with your modeling. Here are some tips to fix your models to avoid light leaks:
- Build thicker geometry in your architecture, instead of using infinitely thin surfaces. Walls of real houses have a thickness, and yours should too.
- Add a polygon outside a building to block light where it is not needed.
- Make sure that all corners are properly beveled, not perfect 90-degree angles.
- If a part of your geometry isn't casting shadows, try adding another surface, such as a primitive shape, inside the part.
For some beginners, raising the shadow map resolution seems to be their first response to any shadow problem, even though this takes more memory and rendering time. If a problem with your shadows can be fixed by adjusting the bias, which does not add to your rendering time, then get the bias right first. If your shadows are not working well with a particular model, sometimes you need to fix the model.
Transparency Support
You expect a transparent object to cast a lighter shadow, and an opaque object to cast a darker shadow. However, conventional depth map shadows do not respond correctly to transparency, and are not any lighter when blocked by a transparent object For example, in Figure 3.16, the shadow becomes as dark under a transparent material as it is under an opaque material. Remember that each point in the depth map stores only one distance at which the light will be cut off, so it either stops at the glass or it doesn't, with no shades possible in between.
Figure 3.16 A limitation of conventional depth map shadows is that different levels of transparency in objects do not cause lighter shadows.
You may be able to work around the lack of transparency support in scenes that have a small amount of glass by setting glass objects not to cast shadows, or by using light-linking to remove them from some of the shadow-casting lights. However, in scenes with a large amount of glass (in which you will probably be raytracing already to achieve refraction), you may want to bite the bullet and switch to raytraced shadows.
Raytraced Shadows
Raytraced shadows are shadows computed by tracing rays of light between light sources and illuminated objects. Raytraced shadows are computed one pixel at a time as you render rather than being pre-computed and stored in shadow maps. Raytraced shadows have a number of advantages over shadow maps:
- Raytraced shadows become lighter when shining through transparent surfaces, and they can even pick up color from colored transparent surfaces, as shown in Figure 3.17.
Figure 3.17 Raytraced shadows can pick up different levels of color and opacity when shining through transparent surfaces.
- With raytraced shadows, you don't run into many of the problems associated with shadow maps, such as needing to adjust bias to prevent artifacts or fix light leaks.
- Raytraced shadows do not use a fixed-resolution map, so they can always be crisp and accurate in any resolution rendering.
- Raytraced shadows support higher quality, more realistic soft shadows, when used with area lights as described below.
- Raytraced shadows work equally well from most types of lights, with no efficiency advantage pushing you toward spotlights.
So, if raytraced shadows are so wonderful and easy to use, why didn't I write about them first? And why do most feature films still rely primarily on shadow maps? There are two simple answers:
- Raytraced shadows generally take longer to render than shadow maps. For complex scenes, the difference can be huge.
- Employing raytracing in your scene increases memory use, and effectively limits the complexity of the scenes you can render on your computer.
Thus, raytraced shadows may be the first choice of a beginner or someone working on simpler scenes, but currently they're less desirable for professionals working on larger, film-resolution projects or doing animation work on tight deadlines.
How Raytraced Shadows Work
Conventional raytracing works backwards in the sense that each ray is computed starting at the camera, instead of starting at the light source as in real life. For each pixel in your image, a ray is traced out from the camera at the appropriate angle until it hits part of a surface that needs to be rendered, as shown by the white lines in Figure 3.18.
Figure 3.18 Raytracing starts with primary rays fired from the camera (white). But for ray-traced shadows, rays need to be fired from each rendered point towards the light, to see if the path is clear (yellow) or blocked and requiring a shadow (red).
For each point on a surface, the renderer needs to determine which lights will be illuminating that point. If a light uses raytraced shadows, then the renderer needs to trace a path from the surface point to the light. If any polygons are found to be blocking that path, then the light will be blocked from the points on the surface. The area where the light is blocked from points on the surface forms the raytraced shadow. The yellow rays in Figure 3.18 show rays followed to the light; red rays show rays that were blocked by geometry, indicating that the point being rendered is in shadow.
If there is more than one light casting raytraced shadows, then the process of checking through space to see whether there are any polygons blocking a ray needs to be repeated for each light with raytraced shadows. All of this needs to be repeated at least once for each pixel, and usually more than once per pixel when rendering with anti-aliasing (anti-aliasing techniques will be discussed in Chapter 9, "Shaders and Rendering Algorithms.")
The result is that raytraced shadows slow down even the fastest computers. Furthermore, the time required to raytrace the shadow is only a part of the performance hit taken by raytracing. All of the polygons in your scene that might be casting a shadow need to be stored in memory in a way that can be searched through. Instead of allowing the rendering software to focus on a small part of your scene at one time, the raytracing process requires continual access to large amounts of data, usually stored in memory in many sorted lists of polygons. The result is that raytracing a large, complex scene uses up far more memory than rendering without raytracing.
Trace Depth
One concern of using raytraced shadows is trace depth, the idea that raytracing is always limited to a finite number of steps. Raytracing without limits could potentially get caught in an infinite loop—leaving your computer rendering a reflection of a reflection of a reflection—if it were not limited to a finite number of calculations. These limits can cause problems with missing raytraced shadows. If your raytraced shadows don't appear within a reflection, or don't appear when seen through layers of refractive glass, chances are you've run into a limited trace depth. The image on the right-hand side of Figure 3.19 shows the consequences of a shadow not appearing in a reflection.
Figure 3.19 With a ray depth limit (trace depth) of 1, the raytraced shadow appears in the rendering but is not reflected in the bottom of the ball (left). With a depth of 2, you can see a reflection of the ray-traced shadow (right).
Besides having a trace depth or ray depth limit setting on your light, trace depth may also be limited globally to your scene's render settings. Trace depth being set too low in either place can prevent your raytraced shadows from appearing in reflections or through refractive transparent surfaces.
Depth map shadows don't have a trace depth setting. A depth map shadow on a surface will tend to appear in any raytraced reflection of the surface, without regard for trace depth.