Lens-flare Shader for Cycles with real time preview.

I have recently been working on an OSL lens-flare shader for Cycles that is highly customisable and offers real time preview in the 3D viewport.

This shader has been ported from an original version written for the Renderman shading language by Larry Gritz and Tony Apodaca.

In the transfer to OSL a few new features, such as user definable lens-flare elements based on image textures, have been added.


The images above shows a hexagonal lensflare render. The setup is shown below.


The ‘Empty’ visible at the top right may be moved to change the position of the lens flare - the resulting image can be seen to update in the 3D viewport in real time.


The shader can be directly connected to the material output node of a plane parented to the front of a camera.

An example scene set up is available on BlendSwap here:

  • http://www.blendswap.com/blends/view/68257
    This blend file also contains a compositing ‘noodle’ that allows the lens-flare to automatically attenuate in intensity as it passes behind objects. The method for achieving this will be the subject of a future video and blog post (the noodle is available from the blog site too).

An example video is here:

Tutorials are available here:

A note not presently in the video
Set the aspect ration of the rendered camera viewport to be approximately the same as for your final image to get the best preview match (this does not affect the final render however).

Blog notes are available here:

Features of the lens-flare shader include the following.

  • Real time 3D viewport preview.
  • Shader output may be connected directly to a Material’s “Surface” node connection (easy node setup).
  • Lens-flare light source position co-ordinates may be set by the user.
  • Highly customisable Bloom, Starburst, Rainbow, Disk, RIng,Blot and Hole elements.
  • Number of spots, and the distribution of spot type, may be varied by the user.
  • Optional user definable images for all lens-flare elements (new feature).
  • Full control to mix between synthesised lens-flare and user image based lens flare.
  • Lens flare elements may be drawn in GIMP - Photoshop - Downloaded - or other.
  • Easy color tinting.
  • Simple random color variation user settings.
  • Automatic or user definable aspect ratio setting (e.g. for anamorphic lens-flare).
  • An Extensive selection of Color outputs allow all of the lens flare elements to be further proceed in the node editor if desired.

I hope you enjoy this shader. I look forward to seeing user images - and hopefully some tutorials from people better at making them than me!

:slight_smile:

Dylan

Nice one. Good to see some non texture scripts.

Nice, i will be testing this out on some projects later today

Just had a quick play, seems like there isnt any motion blur? is there motionblur avaliable to materials? or is it just object motionblur?

I gather that the plane carrying the flare material isn’t moving relative to the camera so wouldn’t accrue any blur. I was going to suggest gathering motion vectors from a sphere surrounding the camera but on another layer but motion blur on a flare would be quite different as it occurs within the lens. The only part that should streak is the source lamp I think.

Yeh i am guessing that only bits of geo would get motion blur… all good i will try some other work arounds

Also, a real flare appears in frame before the source, because it enters a round lens and causes refraction and stuff. Then the source lands on the rectangular image field. Would it make sense to make the material plane circular? Could you attenuate the effect close to that circular edge? So that it doesn’t just blink into being.

I agree, great shader. Thanks for it. :slight_smile:

I belive that it does take that into account, i just loaded up the example and moved the empty driver offscreen, and loaded up rendered view and it still has the circular effects going on without the bloom visible.

Funny thing I was just through some old photos and noticed a long exposure of the sun (vari nd almost opaque). It was a long exposure hand held, so it had a streak of light in the sun ghost image, half way down the flare sequence. So yeah flares do have motion blur but it’s track must be quite short for 180 deg shutter of most recordings.

Hi everyone – thanks for the comments and feedback.

I’m just off to work - so will respond to the motion blur idea tonight. I have not tried motion blur - but it sounds like an excellent idea - and there are probably several ways to achieve this.

I’ve just re-loaded the .zip file to BlenderSwap if anyone was having trouble with the down load – the .osl file was missing (although the shader script could still be loaded via the “Internal” setting in the script node).

have not been able to confirm the BlendSwap upload however – as the site has just stopped responding for me!

Cheers

Dylan

Hi guys,

Yes – the shader looks up the camera’s field of view and fades out the lens-flare after the “lightPosition” exits this cone using cosine roll off (line 293 of the code if any-one wants a play)

:slight_smile:

D

Thanks to you too for all the work with OSL for Blender and publicising its use. I found you tutorials very helpful.

Hi I had a quick look this evening at a few motion blur options. There is an OSL global vector called dPdtime that according to the OSL specification should give information about how the pixel being shaded is moving - but when I wrote a quick script - it did not seem to be giving any values for moving objects (I may have made errors though).

If the shader could get the past position of the camera and "LightObject’ then it could shade the blur in itself. I imagine this could be achieved with pynodes/python script that is called post frame render - so that the positions could be saved for the next frame.

I also tried a few tricks with motion blur enabled in Cycles (such as moving the lens flare plane in various ways - to see if this would ‘fool’ the shader into drawing the blur frames) - but to no avail.

Did you have any luck with a ‘get around’?

The idea of generating vectors from a previous frame could be useful in other areas too.

Quick off topic sorry:
When shooting chromakey video elements for compositing you need to minimize blur at the edges for a clean key. Best way is to decrease the shutter exposure time, but this gives a very strobe like appearance to movement. It would be nice to track the moving object and place a mask that moves around it. Then derive the vectors of that movement for blur in compositor (or OSL?)

Another scenario for Blender is it’s lack of slow motion generation for fixed frame rate assets. That is external footage used that needs to be slowed down (frames added) but cannot be re-rendered at alternate frame rate. You can interpolate by mixing between additional frames or a bit better you could blur the moving parts between frames via derived motion vectors. But getting them from the tracker I guess is the hard part :S

End Off Topic.

Hi David, Thanks for the comments.

You have a great set of VSE tutorials!
Can you recommend a good Render Output can Encoding Setting for uploads from Blender VSE to You Tube? I just use ‘plain’ old QuickTime to capture the screen (15FPS) - and then Blender VSE to edit.

I notice you’re in Bris - if you’d be keen to catch up sometime then please PM me.

:slight_smile:

Dylan

I was wondering about this tool, what would be the best way to get multiple source flares onto the material screen? Would I use multiple screens/planes?

Maybe try using the add shader node, and have two individual lensflare setups?

There are a couple of ways to add multiple flares. You can have as many as you want (if you really want to go all JJ Abrams!)
As doublebishop says:

  • You can use two flare scripts and an Add Shader node - as in the image below.

or

  • You can add multiple lens-flare planes -each one with its own shader.

This can give you a lot of control in post compositing - when you really want to tweak features to subtly suit the scene (that’s one reason why there are so many colour outputs – to allow each element that makes up the flare to be colour/value tweaked/distorted for the desired effect.

I plan to make shader tweaks and usage the subject of video tutorial 4/4.
Video 3/4 is on automated lens-flare masking and is rendering now :slight_smile:


Hello all,

I have just uploaded a tutorial on how to set up the compositor to automatically mask the lens flare when its ‘light source’ passes behind a solid object (like the Earth for example).

Here is an example scene showing the effect:

Here is the tutorial:
It’s pretty long – but there are ‘quick jump’ links in the “About” section.
This tutorial has enough information to set up a scene from scratch (hence the length – the compositing bit is only around 15 mins!)

- LensFlare Tutorial Part 3 of 4: Automatically Masking the lens flare using the compositor

There’s an entry on the BlendBits blog site that describes the general workings of the compositor set up which I’ll reproduce part of here. (The OSL shader is also available from this site - or from the BlendSwap link in the introductory post).

The actual node set up is reasonably simple - as can be seen from the noodle below.
The general idea works as follows:

  • Solid 3D objects in the scene that should block a lens flare are given a Pass Index.
    The IndexOB render-pass is enabled in the RenderLayer(s) for these objects.
  • A small emission source (e.g an icosphere) is placed on it’s own RenderLayer.
    The emission source is parented to the empty that defines the position of the lens flare bloom.
  • The emission source output image, from its RenderLayer is Multiplied by the output of an IDMask. The IDMask index is set to match the Pass Index from above.

a) Flare Blocked: When the emission source is behind a ‘solid’ 3D objects (white) mask – then its bright white image passes through the Multiply node. (i.e. 1 x 1 = 1 = small white dot)

b) Flare Not Blocked: If he emission source is not behind a white mask – then it does not appear at the out put of the Multiply node. (i.e. 1 x 0 = 0 = totally black image)

  • The output of the Multiply node is fed into a Dilate node. This will turn the small white dot from 3a into a fully white image that is the size of the render image.

a) Flare Blocked: Output = fully white image.
b) Flare not Blocked: Output = fully black image.

(In earlier version of this noodle - various blur functions were used to spread the dot ‘gating signal’ to cover the whole image.

  • The output of the Dilate node is inverted.

a) Flare Blocked: Output = fully black image.
b) Flare not Blocked: Output = fully white image.

Hence this signal can be fed directly to the Fac input off the ADD color mixer that mixes the output from the lens flare render layer with the output from the general 3D scene render layer.