Fork me on GitHub

Variance shadow maps in WebGL

Originally soft shadows in GLGE where implemented using PCF (Percentage Closer Filter) but the results were generally noisy and/or slow. As a result I thought I’d try implementing variance shadow maps in webgl. This technique is commonly used for soft shadows and has greatly improved the quality of shadows produced by GLGE. I’ve put together an example to demonstrate the new shadows:

The Demo

Quick Explanation of Variance Shadow Maps in WebGL

Variance shadow maps don’t use the depth to directly determine occlusion but instead represent a distribution of depths using the mean and variance. These can both be calculated over a region using a split kernel to sum the depth and depth squared. You can then obtain the mean and variance from these values and calculate the probability of occlusion at a given depth. The single biggest advantage of this technique is that it can be used with hardware filtering allowing you to get away with smaller shadow buffers without the resulting blockiness.

Using this technique in webgl was made difficult by the lack of a suitable texture format. GLGE gets round the issue by using an RGBA texture, and encoding the sum of depths in the R and G channels and the sum of depth2 in the B and A channels. This gives 16 bits for each value which has proved to be enough to produce a good results.

The biggest downside to this technique is light leakage (areas lit when they should be in shadow). This can sometimes happen when there are multiple occluders and the assumptions made about the distribution of depths don’t hold up. However, it can be greatly reduced by darkening the shadow penumbra and/or reducing the size of the blur.

Using in GLGE

The addition of variance shadow maps has led to the addition of two new configuration options for light sources, these are:
Light.setSpotSoftness(value); // Blur size in pixels
Light.getSpotSoftDistance(value); // Value between 0 and 1 the higher the value the darker the penumbra
Adjusting the second option is useful for eliminating light leakage, the higher the value the less things will leak.

Currently only spotlights are using the new variance shadow maps. I’ll hopefully add to directional lights at some point soon.

Tags: ,