Home

Awesome

Occlusion Lens Flare for Unity

The Occlusion Lens Flare package provides the ability to use Unity's built-in Lens Flare Component without Physics raycasts. It was created to be used with Unity's built-in Render Pipeline.

Please see the following video for a quick introduction,

Unity's built-in Lens Flare requires the Physics engine by default, because it uses a Physics raycast to determine whether the sun is blocked by another object. Here is the relevant quote from the documentation:

Lens Flares are blocked by Colliders. A Collider in-between the Flare GameObject and the Camera will hide the Flare, even if the Collider does not have a Mesh Renderer.

There can be reasons why you might not want to use Unity's Physics engine and in this case, the built-in Lens Flare stops working.

This Occlusion Lens Flare package does not require Physics, it renders an occlusion texture instead. The occlusion texture contains the sun and any object in-between the sun and camera. The occlusion texture is then used to determine how many pixels are occluded from the sun, to control the Lens Flare opacity.

This approach comes with its own benefits and drawbacks:

Installation

As of Unity 2019.3, Unity supports to add packages from git through the Package Manager window. In Unity's Package Manager, choose "Add package from git URL" and insert one of the Package URL's you can find below.

Package URL's

VersionLink
1.1.0https://github.com/pschraut/UnityOcclusionLensFlare.git#1.1.0
1.0.0https://github.com/pschraut/UnityOcclusionLensFlare.git#1.0.0

Credits

If this package is useful to you, please mention my name in your credits screen. Something like "Occlusion Lens Flare by Peter Schraut" or "Thanks to Peter Schraut" would be very much appreciated.

Honorable mentions

Thanks to bgolus and KokkuHub from the Unity forums for helping me getting this working.

You can read the discussion here: https://forum.unity.com/threads/how-to-create-a-lens-flare-effect-without-physics.1053590/

Integration

Override the built-in Lens Flare shader

Open Edit > Project Settings... and select the Graphics tab. Scroll down to the Built-in Shader Settings section and change the Lens Flare setting from Built-in shader to Custom shader.

Drag&drop the Shaders/OcclusionLensFlare.shader asset from the Occlusion Lens Flare package folder in Lens Flare shader setting.

alt text

Example

Once you have overridden the built-in Lens Flare shader as mentioned in the Integration section, you can either just continue with Sample or follow all further integration steps below.

The Sample requires Unity's Cinemachine package, but the actual Occlusion Lens Flare does not.

You should see this now: alt text

Hold down the right mouse button and move the mouse to rotate the camera. When the sun goes behind one of those green boxes, the Lens Flare should fade out.

Integration step by step

In this section I describe how to replicate the Occlusion Lens Flare Sample.

TODO

How it works

TODO

In this video I go into more detail how it actually works.