Lights and Shadows

From C4 Engine Wiki

Jump to: navigation, search

The C4 Engine has six distinct types of light sources that can be used to illuminate a scene. Some of the differences among these types are whether the light is a point source or an infinite directional light and whether the light projects a texture map onto the scene. Lights are placed into a scene by using the tools in the Lights page in the World Editor.

Contents

Infinite Light

An infinite light illuminates a scene with a constant brightness from a single direction. When it is placed in a world, only the direction in which it is pointing matters, not its position.

Depth Light

Figure 1. Shadows produced by a depth light.
Figure 1. Shadows produced by a depth light.
A depth light is an extension of the infinite light that uses an orthographically-projected 2D shadow map (containing depth information) to apply shadows to the environment. When rendering, it uses shadow mapping hardware to perform per-pixel depth comparisons, causing correct shadows to be applied to any objects within the light's volume of influence. Unlike the infinite light, the depth light's position in a scene matters. A depth light's position corresponds to the (x, y) center of the shadow map that it projects and to the front of the range of depth values that its shadow map covers.

The shadow map for a depth light is completely dynamic and updated every frame. In order for a geometry node to cast a shadow in a depth light's shadow map (as opposed to casting a stencil shadow), it must have the “Render in shadow map” box checked in the Node Info window.

Landscape Light

The landscape light is a special kind of infinite light that can be used to cast multiresolution shadows over a large area. For more information, See Landscape Light.

Point Light

A point light emits light from a single point in space and influences a spherical region of a given radius. The intensity of the light gradually falls off to zero at the outer boundary of the light's sphere.

To create a point light, select the point light tool from the Lights page and use it to place a light in your level at the desired position. Hold the mouse button down and drag away from the light position to set the radius for the point light. Alternatively, you could later set the radius manually in the Transform page by entering a positive value in the X size field.

Cube Light

Figure 2. A projected shadow produced by a cube light.
Figure 2. A projected shadow produced by a cube light.
A cube light is a special type of point light that projects an ordinary CUBE texture map into the environment. The texture can contain grayscale shadows or any kind of full-color projected image. It does not contain any depth information for shadow mapping, so it's only intended to be used for projecting shadows from geometry immediately surrounding a light source. When the light source moves, the projected shadow moves with it, providing a very efficient way of projecting shadows from geometry surrounding a light, such as a cage, onto the environment.

The texture used by a cube light can be imported or generated by the engine. To generate a cube map for shadow casting, follow these steps:

  1. Create a new world and place the cube light and any geometry that will participate in the projected shadow into this world.
  2. Open the Get Info dialog for the infinite root zone by selecting it in the scene graph view and hitting Ctrl-I. Set the clear color to white (255, 255, 255), and set the ambient color to black (0, 0, 0).
  3. Open the Get Info dialog for the cube light, and set its color to black (0, 0, 0). Also, set the name of the texture that will be generated and its size. Be sure to check the “Generate shadow texture” box.
  4. Save the world containing the cube light, and then start a new game with that world.
  5. Type gentex in the command console, and a small developer window will appear.
  6. Make sure the “Light projections” box is checked, and click the Generate button. This will cause the cube light in the world to generate its CUBE texture map and write it to the disk.

Once these steps have been followed, the resulting CUBE texture map can be used on any depth light to project a shadow texture. When used in conjunction with the original geometry that generated the texture, the projected cube map can be cast onto the geometry that is actually casting the shadows in the map, which doesn't look right. To prevent this from happening, set the “Don't render cube light projection” flag under the Rendering pane for each of the geometries surrounding the light source.

Spot Light

Figure 3. A projected shadow produced by a spot light.
Figure 3. A projected shadow produced by a spot light.
A spot light is a special type of point light that shines light in one primary direction. It has the same type of attenuation that a point light has, but it is also restricted to a pyramid of influence centered on the directional axis. Spot lights always project a 2D texture map which can be imported or generated just like the texture for the cube light. The wrap modes for the texture should be "clamp to border" so that the light intensity falls off to zero at the boundary of the pyramid of influence (because texture border colors are always black in C4).


Stencil Shadows

The C4 Engine uses three distinct types of shadows: stencil shadows, shadow maps, and projected shadows. Shadow maps and projected shadows are those that are described under Depth Light, Cube Light, and Spot Light above. Stencil shadows are the primary dynamic shadowing technique used by the engine.

By default, almost all geometries created in the World Editor cast stencil shadows automatically. The exceptions are the planar geometry primitives, such as the plate and disk, because they don't form closed meshes by themselves. The stencil shadow algorithm requires that each shadow-casting object be a completely closed mesh.

Optimization Tips

The stencil shadow algorithm produces very accurate shadows, but it is known for eating lots of polygon-filling power on lower-end GPUs. The C4 Engine uses several advanced techniques to optimize stencil shadows as much as possible. Here are some tips for getting the best results.

  • First, it is essential that you use zones and portals to partition your world. The engine can use this information to determine which objects really need to cast shadows and eliminate those objects whose shadows couldn't possibly be visible.
  • Be sure to turn off shadow casting for objects that can't actually cast shadows on anything else. In most rooms, for example, the floor, ceiling, and some walls never cast visible shadows. Turn shadow casting off by unchecking the “Render stencil shadow” box in the Node Info dialog for a geometry.
  • Make the range for point, cube, and spot lights as small as possible. This will allow the engine to restrict the screen area covered by shadows cast by those lights. If you need to make a light brighter, try increasing the “Brightness scale” in its Get Info dialog before increasing the light's range.
  • Mark lights as static whenever you know that they aren't going to move. This allows the engine to cache shadow volumes so that they don't need to be recomputed every frame. To make a light static, check the “Light is static” box in its Get Info dialog.
  • Use shadow spaces when appropriate. These will limit the screen area to which shadow volumes are rendered. Be aware, however, that even though shadow spaces decrease GPU load, they increase CPU computation, so you should time your scenes with and without each shadow space to see if you've really gained an overall speedup.
Personal tools