top of page

Creating Unity lens-flares in 2D


If you haven't got great art, then use great lighting to make a good looking game! The addition of a reactive lens flare can add a lot to scene believability. Unity makes this easy with a drag-and-drop lens-flare solution...except it only works in 3D. Unity Technologies are seriously leaving their mobile devs behind in my opinion as all their work seems focussed on AAA PC and console games, and realtime artworks.

Anyway, a lens flare solution is needed that reacts to 2D objects covering the light source, and it's not hard to create one. In my case, because the light position is static, I ended up being able to use a static prefab containing a bunch of flare sprites lifted from the Unity lens-flare texture (85 mm) by turning the 85mm lens flare texture into a multisprite. I created a Unity lens flare and then roughly copied the positions and sizes for each element with a sprite from this new sprite sheet.

The only 'clever' bit is adding 8 child objects each with a trigger that tells the parent object when this trigger is covered. These triggers are on their own layer, called 'flareTriggers', and 2D physics properties are set so only objects like ships, rocks and scenery can trigger them.

On the parent Lens Flare game object is a flare controller script where I crudely assign each trigger to an array. Every update I count how many triggers are set (OnTriggerStay2D) and fade and shrink each flare element accordingly. Every late update I clear the number of occluded triggers. I thought I'd need to blend smoothly between occlusion states, but actually 8 levels is pretty smooth in motion as you can see in this test video; it nicely brightens/darkens based on how much of the star is visible.

2D Lens flare test

The star is a bit 'floaty' and that's where volumetric shadow trails are needed. This is proving a little trickier than expected because my normal-mapped sprites aren't drawing to a rendertexture, Unity don't provide a normal-mapped sprite shader to use, and no-one's answering my questions on the Unity help places. :( But it'll look good once I've cracked it!

Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Recent Posts
Archive
Search By Tags
No tags yet.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page