4.3. Realtime Shadow Techniques Used By Grit

4.3.1. Depth Shadow Mapping

Grit has fully dynamic shadows that are calculated in real time on the GPU. The basic technique is called depth shadow mapping. It involves rendering the scene from the light (the sun or the moon) into a texture, called a depth shadow map. The shadow map is updated every frame, because objects move and so does the light. The colour of the scene is not computed, as we are only interested in the distance to all the surfaces that the light can 'see' (these are the occluders).

When the scene is rendered from the player's point of view, this shadow map is used as a reference to help decide if a given pixel is the closest one to the light (in which case it is not in shadow) or whether there is something else that is closer (in which case it is rendered darker because it is in shadow).

4.3.2. Perspective Transform

The depth shadow map has a limited resolution, so in order to increase the apparent fidelity (and avoid blocky artefacts) there is a perspective transform applied in order to concentrate as many as possible of the shadow map's texels close to the player. There are many techniques but the one used in Grit is called LiSPSM (LIght Space Perspective Shadow Mapping). The worst case is when the sun is directly behind you, in which case no perspective transform can be applied, and the shadow is very low detail and noisy. However, if you look 90 degrees to the sun, the shadows will be a lot crisper due to the use of LiSPSM. Note that increasing the resolution of the shadow map texture will also make the shadows crisper, but will cost memory and performance.

The perspective transform changes every frame depending on the light direction and the chase cam's direction. Sometimes the changes can be quite severe. This causes an unavoidable 'crawling' effect in the shadows.

4.3.3. Covering Larger Distances

There are in fact 3 shadow maps used. One for the area closest to the player, one to cover the area further away, and the 3rd one for the furthest reach of the shadow (200 metres). They are all the same size textures, but the one nearest to the camera covers a much smaller area and thus the shadows are better defined. Another way of looking at this is that it allows shadows to appear much further from the player, without compromising the quality of shadows near the player. The exact technique used in Grit is called PSSM (Parallel Split Shadow Mapping). Sometimes you can see the transition from one shadow map to the next, as a sudden decrease in shadow quality.

4.3.4. Soft Shadows

If each screen pixel was merely tested for being in shadow or not, the shadows would be very hard-edged because of the sudden transition from 'in shadow' to 'not in shadow'. To avoid this, we soften the shadows using a technique called PCF (Percentage Closer Filtering) . This boils down to testing the shadow map several times per screen pixel, and taking the average. The appearance is that several faint shadows have been overlaid in slightly different positions, to produce a blurred effect. It can get very slow but there is hardware support that we are currently not using that can help, see issue 125.