Version: 2018.3 (switch to 2019.1 )
Shadows
Directional light shadows
Other Versions

Shadows

Unity’s lights can cast ShadowsA UI component that adds a simple outline effect to graphic components such as Text or Image. It must be on the same GameObject as the graphic component. More info
See in Glossary
from a GameObject onto other parts of itself or onto other nearby GameObjectsThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
. Shadows add a degree of depth and realism to a SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
, because they bring out the scale and position of GameObjects that can otherwise look flat.

Scene with GameObjects casting shadows
Scene with GameObjects casting shadows

How do Shadows work?

Consider a simple Scene with a single light source. Light rays travel in straight lines from that source, and may eventually hit GameObjects in the Scene. Once a ray has hit a GameObject, it can’t travel any further to illuminate anything else (that is, it “bounces” off the first GameObject and doesn’t pass through). The shadows cast by the GameObject are simply the areas that are not illuminated because the light couldn’t reach them.

Another way to look at this is to imagine a CameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary
at the same position as the light. The areas of the Scene that are in shadow are precisely those areas that the Camera can’t see.

A lights eye view of the same Scene
A “light’s eye view” of the same Scene

In fact, this is exactly how Unity determines the positions of shadows from a light. The light uses the same principle as a Camera to “render” the Scene internally from its point of view. A depth bufferA memory store that holds the z-value depth of each pixel in an image, where the z-value is the depth for each rendered pixel from the projection plane. More info
See in Glossary
system, as used by Scene Cameras, keeps track of the surfaces that are closest to the light; surfaces in a direct line of sight receive illumination but all the others are in shadow. The depth map in this case is known as a Shadow Map (you may find the Wikipedia Page on shadow mapping useful for further information).

Enabling Shadows

Use the Shadow Type property in the InspectorA Unity window that displays information about the currently selected GameObject, Asset or Project Settings, alowing you to inspect and edit the values. More info
See in Glossary
to enable and define shadows for an individual light.

Property: Function:
Shadow Type The Hard Shadows setting produces shadows with a sharp edge. Hard shadows are not particularly realistic compared to Soft Shadows but they involve less processing, and are acceptable for many purposes. Soft shadows also tend to reduce the “blocky” aliasing effect from the shadow map.
Strength This determines how dark the shadows are. In general, some light is scattered by the atmosphere and reflected off other GameObjects, so you usually don’t want shadows to be set to maximum strength.
Resolution This sets the renderingThe process of drawing graphics to the screen (or to a render texture). By default, the main camera in Unity renders its view to the screen. More info
See in Glossary
resolution for the shadow map’s “Camera” mentioned above. If your shadows have very visible edges, then you might want to increase this value.
Bias Use this to fine-tune the position and definition of your shadow. See Shadow mapping and the Bias property, below, for more information.
Normal Bias Use this to fine-tune the position and definition of your shadow. See Shadow mapping and the Bias property, below, for more information.
Shadow Near Plane This allows you to choose the value for the near plane when rendering shadows. GameObjects closer than this distance to the light do not cast any shadows.

Each Mesh RendererA mesh component that takes the geometry from the Mesh Filter and renders it at the position defined by the object’s Transform component. More info
See in Glossary
in the Scene also has a Cast Shadows and a Receive Shadows property, which must be enabled as appropriate.

Enable Cast Shadows by selecting On from the drop-down menu to enable or disable shadow casting for the meshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info
See in Glossary
. Alternatively, select Two Sided to allow shadows to be cast by either side of the surface (so backface culling is ignored for shadow casting purposes), or Shadows Only to allow shadows to be cast by an invisible GameObject.

Shadow mapping and the Bias property

The shadows for a given Light are determined during the final Scene rendering. When the Scene is rendered to the main Camera view, each pixelThe smallest unit in a computer image. Pixel size depends on your screen resolution. Pixel lighting is calculated at every screen pixel. More info
See in Glossary
position in the view is transformed into the coordinate system of the Light. The distance of a pixel from the Light is then compared to the corresponding pixel in the shadow map. If the pixel is more distant than the shadow map pixel, then it is presumably obscured from the Light by another GameObject and it obtains no illumination.

Correct shadowing
Correct shadowing

A surface directly illuminated by a Light sometimes appears to be partly in shadow. This is because pixels that should be exactly at the distance specified in the shadow map are sometimes calculated as being further away (this is a consequence of using shadow filtering, or a low-resolution image for the shadow map). The result is arbitrary patterns of pixels in shadow when they should really be lit, giving a visual effect known as “shadow acne”.

Shadow acne in the form of false self-shadowing artifacts
Shadow acne in the form of false self-shadowing artifacts

To prevent shadow acne, a Bias value can be added to the distance in the shadow map to ensure that pixels on the borderline definitely pass the comparison as they should, or to ensure that while rendering into the shadow map, GameObjects can be inset a little bit along their normals. These values are set by the Bias and Normal Bias properties in the Light Inspector window when shadows are enabled.

Do not set the Bias value too high, because areas around a shadow near the GameObject casting it are sometimes falsely illuminated. This results in a disconnected shadow, making the GameObject look as if it is flying above the ground.

A high Bias value makes the shadow appear disconnected from the GameObject
A high Bias value makes the shadow appear “disconnected” from the GameObject

Likewise, setting the Normal Bias value too high makes the shadow appear too narrow for the GameObject:

A high Normal Bias value makes the shadow shape too narrow
A high Normal Bias value makes the shadow shape too narrow

In some situations, Normal Bias can cause an unwanted effect called “light bleeding”, where light bleeds through from nearby geometry into areas that should be shadowed. A potential solution is to open the GameObject’s Mesh Renderer and change the Cast Shadows property to Two Sided. This can sometimes help, although it can be more resource-instensive and increase performance overhead when rendering the Scene.

The bias values for a Light may need tweaking to make sure that unwanted effects occur. It is generally easier to gauge the right value by eye rather than attempting to calculate it.

To further prevent shadow acne we are using a technique known as Shadow pancaking (see Directional light shadows: Shadow pancaking). This generally works well, but can create visual artifacts for very large triangles.

A low Shadow near plane offset value create the appearance of holes in shadows
A low Shadow near plane offset value create the appearance of holes in shadows

Tweak the Shadow Near Plane Offset property to troubleshoot this problem. Setting this value too high introduces shadow acne.

Correct shadowing
Correct shadowing

Did you find this page useful? Please give it a rating:

Shadows
Directional light shadows