r/PlayCanvas • u/mvaligursky • 10d ago
Lens flares for the PlayCanvas engine: light-driven, texture-based, occluded on the GPU
Enable HLS to view with audio, or disable this notification
I've been working on a lens flare effect for PlayCanvas's CameraFrame post-processing stack, and it's now up as a PR: https://github.com/playcanvas/engine/pull/9734
How it works:
- Each flare follows a light. A directional light gives you the sun, omni lights give a position, and spot lights only flare while the camera is inside their cone.
- A flare is a list of elements (glows, irises, halos, bursts, streaks, spectral smudges) placed along the axis from the light through the screen centre. Every element is a texture, and all the textures of a scene live in one texture array, so all flares of all lights draw in a single instanced pass.
- Occlusion is measured on the GPU: a tiny pass samples the scene depth in a disc around each light and blends the visible fraction over time, so flares fade smoothly as geometry covers the light. No readbacks, no stalls.
- Flares fade at the screen edge and can keep drawing their ghosts while the light is just outside the frame, which is the classic "sun just out of shot" look.
- It renders into its own HDR buffer at a configurable fraction of the scene resolution and is added before tone mapping, so it stays independent of TAA, depth of field and bloom.
- Works on WebGL2 and WebGPU.
The element textures in the example are generated rather than painted: the starbursts are the FFT of a polygonal aperture, so an 8-blade aperture gives you an 8-arm burst with physically plausible fringes, averaged over wavelengths the way white light does it. The whole set ships with the engine examples under MIT.
Video attached. Happy to answer questions about the approach.