Shadows

BeginnerDesignerArtist

Shadows bring significant information and realism to a scene.

Shadows offShadows on
media/SceneNoShadows.pngmedia/SceneWithShadows.png

Only directional lights, point lights, and spot lights can cast shadows.

Shadow maps

Xenko uses shadow mapping to render shadows. To understand shadow maps, imagine a camera in the center of the sun, so you're looking down from the sun's perspective.

Light and shadow

Everything the sun sees is in light. Everything hidden from the sun (ie behind occluders) is in shadow.

From this perspective, Xenko creates a shadow map for each light that casts shadows. This tells us how far each visible pixel is from the light. When Xenko renders the scene, it checks the position of each pixel in the shadow map to learn if it can be "seen" by the light. If the light can see the pixel, the light is illuminated. If it can't, the pixel is in shadow.

For example, these are shadow maps from the first-person shooter sample included in Xenko, generated by a directional light.

FPS scene

FPS scene shadow map

Note

Note that the directional light in the example above creates four shadow maps, one for each cascade. For more information, see the Directional lights page.

The shadow atlas

Shadow maps for each light that casts a shadow are saved in a region of the shadow atlas texture. You can choose how much of the shadow atlas each light uses. The larger the shadow map, the better the shadow quality, but the less space you have for shadow maps from other light sources.

Higher-quality shadow (uses a large area of the shadow atlas)Lower-quality shadow (uses a smaller area of the shadow atlas)
High-resolution shadowLow-resolution shadow
FPS scene shadow mapFPS scene shadow map

Generally, you should give more space to light sources that cast the most visible shadows.

The size of each area in the shadow map depends on several factors:

  • the shadowMapSizeFactor based on the LightShadowMap.Size property (/8, /4, /2, x1, or x2)
  • the projected size of the light in screenspace (lightSize)
    • for directional lights, the lightSize is equal to the max (screenWidth, screenHeight)
    • for spot lights, the lightSize is equal to the projection of the projected sphere at the target spot light cone
  • the ShadowMapBaseSize equals 1024The final size of the shadow map is calculated like this:
  1. // Calculate the size factor
  2. var shadowMapSizeFinalFactor = shadowImportanceFactor * shadowMapSizeFactor;
  3. // Multiply the light projected size by the size factor
  4. var shadowMapSize = NextPowerOfTwo(lightSize * shadowSizeFinalFactor);
  5. // Clamp to a maximum size
  6. shadowMapSize = min(shadowMapSize, ShadowMapBaseSize * shadowSizeFinalFactor);

If you've enabled shadows on a light in your scene, but it isn't casting shadows, make sure there's enough space in the shadow atlas to create a shadow map for the light. For more information, see Troubleshooting — Lights don't cast shadows.

See also