Importing Larry Gritz’s lens flare shader into DAZ Studio

Some of you may have read my Shader Builder tutorial that deals with importing 3Delight’s envlight2 shader into DS. Here is a yet another walkthrough for another type of Renderman shader – a surface one.

It’s not going to be as detailed as the big PDF one, though, so I suggest you start with that one first. Test what you have learned; ask away if you get stuck.

In the end, you will have a working shader and a method to use it for getting neat lens flare – a phenomenon that is ubiquitous in real-life cameras, but sorely missing from DAZ Studio. This shader is fast, and often it will give you just the right “glow” around light sources you need, without expensive volumetrics: even in real life, that fuzzy halo you see around lights is often created in your eye, not in the atmosphere.

The lens flare shader was written years ago by Larry Gritz (a big name guy in the world of CG). It is actually a surface shader, so you’ll have to apply it to something mimicking a “lens” – but not in the geometrical sense thereof =) A flat plane will do.

As a teaser, here is a “toon-y” landscape scene I quickly threw together. The “sun” is a regular point light. Of course it’s not that physically correct, so won’t work for anything but a “toy” scene, but! There is a trick: you can place a point light that will not affect the rest of your scene, but will give you the lens flare so as to simulate the sun (or whatever). To do this, you either need to use a light shader that can have a light category assigned and set only the lens flare surface to “see” it, or just put it very close to your lens flare surface and adjust the intensity of the light to make it low enough not to influence the rest of the scene (fairly easy with physically-based square falloff that most point light shaders have – unless they are “linear point lights”, that is). The strength of lens flare effects can also be adjusted. Just remember: put the light “outside the kit” = the lens flare surface should be between the light and the camera.


Click the image, click the links, click the “read on” when you’re ready!

What we need:

– a working installation of DS (I’m using DS4.7 now, but unless the DAZ guys change everything, this should apply to subsequent versions as well);
– Larry Gritz’s source code, available here; scroll down and underneath the heading “Tar files to download”, get the one with shaders only;
– a good text editor like Notepad++.

1) Unzip the archive you got from Larry Gritz´s website. If your software can’t handle tars, get the free 7zip. Unzip until you get to the folder called “shaders”.
2) Find the source called “” and open it in your text editor (I am using Notepad++). You will see that these two awesome gentlemen Mr Gritz and Mr Apodaca have provided an explanation of what the shader is for and what its parameters do. You will want to refer to this info later when actually using the shader. Right now you need to scroll down and find this line:

#include "patterns.h"

Shader Builder in DS does not understand includes, so you will need to edit the .sl file. Save it under a different name (like “”).
If you’re using Notepad++, too, make sure you select “all types” in the save dialogue (scroll up one step from the default “txt” option).
3) Open the “shaders” folder again and locate the “patterns.h” file. Open it in the text editor. You will see it calls on a yet another file:

#include "filterwidth.h"

Locate this file and open it.
4) Select all in the “filterwidth.h”, copy and paste into the “” instead of the #include line.
5) Select all in the “patterns.h”, copy and paste into the “” underneath the code you have just copied from “filterwidth.h”. Find the

#include "filterwidth.h"

line and kill it.
6) Save the .sl file. You can close the .h files now.
7) Fire up DAZ Studio, go to the Shader Builder tab. It should open at the “Surface” category by default; if it does not, click on it in the tree. Create a new shader, name it something like “LensFlare”.
8) The Designer window will now open; right-click in there, select “New block” – “Macro from shader source”; do not accept adding it to the repository; navigate to your “” and select it.
Shader Builder will think a bit and import the code.
9) Now connect the Ci output of the macro with the Ci of the “LensFlare” main block. It’s a good time to hit “Accept” and select “save and compile” in the next dialogue; there may be an “error compiling shader”, but that’s okay. Just make sure the shader gets saved.
10) Now double-click your “LensFlare” icon again and start connecting the macro inputs to the user parameters (right-click each pink dot, select “Connect/create new”, refer to my envlight2 tutorial for extra info). I suggest creating five groups: Bloom, Starburst, Rainbow, Spots and Control. The last parameter, “seed”, should go into Control. The first four groups should be self-explanatory (read the shader intro comments and look at the parameter names).
11) Now let’s give it some more thought. Do we want our lens flare to affect each and every light in the scene? Probably not. Fortunately, we’re talking Renderman here, and it means we can use light categories (scroll a bit down for explanation and syntax)!
As of right now, you will need to have AoA’s Advanced Lights because they are the only light shaders on the market with built-in light category support, but when my lights get released, this will change.
12) To add light category support to the lens flare shader, we will need to create a string variable. Double-click the macro block; select any variable; change its type to “uniform string”, give it a new name like “lenscat” (copy this into “variable”, “name” and “label” fields) and assign a default value of “” (empty string). Click the “New” button on the right. You will have a new input created:
13) Now click on the “Initialisation code” tab and find this line:

 illuminance (P, vector "camera" (0,0,1), PI/2) {

Our input is numbered 22, so we need to make the illuminance() loop see and respect it; insert a 22 enclosed inside percent signs (this is Shader Builder syntax) right before the point P, and don’t forget the comma:

 illuminance (%22%, P, vector "camera" (0,0,1), PI/2) {

Click “Accept”.
14) You will see a new input named “lenscat”, a blue dot underneath all the others; right-click it and create a user parameter in the Control group.
Save network and compile shader. You should see an empty “Log” tab now.
15) Go back to the “Block Network” tab: we need to make sure our lens flare does not exist to any rays in the scene apart from camera rays (otherwise it may get reflected in surfaces or cast shadows etc, and this is not what we really want – because IRL, lens flare happens inside the camera). To achieve this, we need to set a few RiAttributes; in DS, this is done via rendertime scripts.
So, right-click the window, select “New block” – “Rendertime script” and connect its grey output with the corresponding grey input of the main “LensFlare” block.
16) Double-click the rendertime script block. A window will pop up with some code. Erase it and insert these lines:

Shader.setIntegerAttrib( "visibility", "integer diffuse", 0 );
Shader.setIntegerAttrib( "visibility", "integer specular", 0 );
Shader.setIntegerAttrib( "visibility", "integer transmission", 0 );
Shader.setIntegerAttrib( "visibility", "integer photon", 0 );

Click “Accept” and save network.
17) Let’s also make sure our lens flare surface won’t interfere with us actually seeing the scene while we work at it; click the User parameters block, find opacity strength and set its default to 0. We do not use opacity in this shader, so this parameter only influences the viewport opacity of the surface, not anything in the rendered image.
You may still have to set it to 0 manually, though, after applying the shader to your surface, since it will inherit the DS default shader opacity value which is 1 by default.
Save and compile.
18) Now we have a working surface shader that we can apply to surfaces and turn them into “lenses”. Here’s what I’d do to make sure I can just load a scene subset whenever I need a lens flare:

1. Create a new camera (give it a descriptive name like “LensFlareCam”).
2. Create a new primitive plane. It can be any size, and it’s best to set “divisions” to 1 (because we won’t bend it or anything). Select the plane in your Scene tab and go to “Edit – Object – Scene Identification” menu; rename the plane to something like “LensPlane”.
3. Now, parent your LensPlane to the LensFlareCam. With LensPlane selected, go to Parameters tab and from its menu, select “Zero – Zero Selected Item(s) Pose”. This will snap the plane to the camera.
4. With the plane still selected, input “90” in the X Rotation parameter. This will set the plane perpendicular to the camera axis. Now, move the plane a bit to be in front of the camera: either by moving it along the green axis of the Translate tool (supposing it’s still set to use local coordinates in your DS), or by typing something like “-5” into the Z Translation parameter.
5. Select the plane in the Surfaces tab, right-click the LensFlare shader icon in the Shader Builder and select “Apply to selected surfaces”. Check if viewport opacity needs to be set to 0. UPD: If you click on that tiny arrow-with-a-checkbox next to LensPlane in the Scene tab, the plane will turn non-selectable in the viewport, which is very helpful when you use the LensFlareCam as your scene setup camera (otherwise this transparent plane will be the only thing that gets selected when you click in the viewport, since it technically covers all of it).
6. Right-click your LensFlareCam, go to “File – Save As – Scene Subset” menu. You will be presented with a dialogue to specify a file name; do it (again, something like “LensFlareCamera” will do; you have to remember it, that´s all) and accept. Now you will see a dialogue like this:


By clicking the menu, you can choose “Scene selected only”, which will conveniently tick the camera only (supposing you still have it selected in your scene, of course), or the plane only, or whatever is selected in the scene tab. You should make sure both the camera and the plane are selected, and then click “Accept”.

7. Now you have a scene subset with a “lens flare-enabled” camera that you can merge with your scene any time quickly.

19) A word of caution: like volume shaders, the lens flare effect is composited over geometry; it does not influence the alpha of the render. It means that in order to see the effect, you need to have some “physical” object in the background of your scene, like a plane/cube, or a fully modeled skydome/building/whatever. Just a mesh object.

20) As in real life, the flare will be influenced by the position/direction of the lights (it may take some fiddling to get the particular effect you’re after!). However, since you can adjust the strengths of all the parts of the effect, you have more freedom than a real-world photographer =)


Comment here

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s