Publishers of technology books, eBooks, and videos for creative people

Home > Articles

Shadows and Occlusion

This chapter is from the book

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.

dir2_03_12.jpg

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.

dir2_03_13.jpg

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.

dir2_03_14.jpg

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.

dir2_03_15.jpg

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.

dir2_03_16.jpg

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.
    dir2_03_17.jpg

    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.

dir2_03_18.jpg

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.

dir2_03_19.jpg

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.

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020