Click or drag to resize
DigitalRuneLens Flares

A lens flare is an effect caused by a camera lens when looking at bright light. A lens flare usually consists of multiple elements which move across the screen depending on the position of the light source: bloom and halo around the light source, light streaks, secondary rings, hexagonal patterns caused by the lens' aperture blades.

This topic contains the following sections:

Defining lens flares

A lens flare effect is defined by creating a LensFlare. The elements of a lens flare (e.g. rings, halos, ...) are defined using the LensFlareElement. The LensFlareElements need to be added to the Elements collection. The LensFlareNode positions a lens flare within a scene. (Lens flare effects in DigitalRune Graphics are not bound to cameras or light sources - they can be placed anywhere within a scene.)

DigitalRune.Graphics (Lens Flares)
Class Diagram: Lens Flares
Rendering lens flares

This section explains how to render lens flare effects using the LensFlareRenderer.

LensFlareRenderer

The LensFlareRenderer is SceneNodeRenderer that handles LensFlareNodes. The renderer performs hardware occlusion queries to determine the visibility of the lens flares. The query results are delayed by one or more frames, which means that lens flares need at least two frames to become visible.

Lens flares need to be rendered in two passes:

  1. Occlusion Query: The method UpdateOcclusion needs to be called after the scene is rendered. The Z-buffer of the current render target needs to contain the depth information of the scene. The method performs a hardware occlusion query to check the visibility of the light source. In a deferred lighting renderer the method can be called at the end of the "Material" pass. No pixels are rendered into the current render target during the occlusion queries.

  2. Rendering: The method Render needs to be called to render the lens flares into the current render target. The method can be called at any point after the occlusion queries.

The following code illustrates the usage within the rendering pipeline.

C#
// Here: Render all opaque objects... 
// At this point the Z-buffer should contain the depth information of the scene.

// Perform occlusion queries to determine the visibility of the lens flare nodes. 
lensFlareRenderer.UpdateOcclusion(query.SceneNodes, context);

// Render other stuff...

// Render lens flares.
lensFlareRenderer.Render(query.SceneNodes, context);

Limitations

The LensFlareRenderer has certain limitations:

  • Does not work with orthographic projections. Lens flares for local light sources require a camera with perspective projection.
  • Hardware occlusion queries are not support in Reach profile. The method UpdateOcclusion has no effect in Reach profile. That means lens flares in front of the camera are always visible.
    As a workaround: Override the method OnGetSizeAndIntensity (as described below) and perform a custom visibility check.

Custom size and intensity

It is further possible to customize the size and intensity of a lens-flare effect by deriving a custom lens-flare type from LensFlare and overriding the method OnGetSizeAndIntensity. This method is called by the LensFlareRenderer during Render.