Skip to content

2D Pixel Perfect Shadows

mattdesl edited this page Mar 31, 2013 · 22 revisions

Detailed here is an approach to 2D pixel-perfect shadows using shaders and the GPU. Because of the high fill rate and multiple passes involved, this is generally less performant than geometry shadows; however, it has a number of benefits. It will be implemented in LibGDX, although the concepts can be applied to any OpenGL/GLSL framework.

The basic steps involved:
Img

  1. Render occluders (shadow casters) to a FBO; if they cast a shadow, they will be opaque, otherwise transparent.

  2. Build a 1-dimensional "shadow map" from our occluders. The x-axis represents angle (theta), and the colour represents the minimum distance from light center to the nearest occluder.

  3. Using the 1D shadow map, render our blurred shadows. Then render our sprites on top.

Here is a visual demonstration of the technique:
Visual

The idea is an extension of my previous attempts at shader-based shadows, which combines ideas from various sources. However, "nego" on LibGDX forums suggested some great ideas to reduce the process into fewer passes.

Further Reading

Clone this wiki locally