fade out flares with distance to camera

I think the flare behavior in unity is rather strange: instead of fading out over the distance (I cannot use fog for this) the flares get bigger and brighter with increasing distance to the camera.
Is there a way to invert this behavior and let flares be the strongest near the camera and become fainter as the camera gets farer away from them?

Thanks in advance!

the flare doesn’t actually become bigger as it moves further away, what is happening is that the flare is actually staying the same size… thus making it appear to grow when it moves into the distance. But yeah I second your question and would like to know if it is possible to scale lens flair based on distance from camera. :slight_smile:

Or use a plane and map a texture onto it and put a simple script on it that does something like:

function Update() {
  transform.LookAt(Camera.main);
}

There’s a clever way to have it point at each camera in turn for multi-camera setups, but I can’t remember it off the top of my head.

well, i suppose this would be a simple solution, but wouldn’t the performance advantage of using flares be completely lost when using textured planes instead?

There’s a performance advantage to using flares?

At any rate, if you check the LensFlare documentation, you’ll see there are properties for setting the colour and brightness of a flare. It would be simple to write a script which gets the LensFlare component and sets the brightness according to the distance from the camera. However, there doesn’t seem to be any way to set the visible size of the flare - maybe a feature request is in order.

The lens flares do not change size, other geometry just gets smaller in the distance. I agree that it would be nice to have the option to have them vary in size.

Space Navigator is used by about half a dozen users around here, it’s just not a priority.

If you’re waiting for Unity 2.2 you’re going to be waiting for a very long time as 2.5 will be the next release (the focus of that release being Windows and a completely rewritten GUI). Also a small pet peeve around the office here… it’s Unity, not Unity3D :wink:

Ethan

Are there any updates on that with the new unity 3.0 version?

Ditto.

Here’s some code to alter the brightness of flares as a linear inverse function of distance.

var light : Transform;
var lf : LensFlare;
var value = 1;

function Update () 
{
var heading: Vector3 = light.position - camera.transform.position;
var dist: float = Vector3.Dot(heading, camera.transform.forward);
lf.brightness = value/dist;
}

Attach it to your main camera

Have you noticed that Lens Flares work perfectly in Scene View, with regards to fading in and out behind colliders? Is there any way to have this functionality in Game View? The slow fading is very unrealistic.

I have written code to make flares disappear instantaneously using this code:

var lf : LensFlare;

function OnBecameVisible () 
{
lf.enabled = true;
}

function OnBecameInvisible ()
{
lf.enabled = false;
}

However, they still fade in unrealistically. Please Unity, is there any way to fix this problem?

Attach this script to any lens flare in your scene. It will now appear and disappear instantaneously when occluded by an object or when it’s outside of the camera’s frustrum. It will also get brighter or dimmer depending on the camera’s distance to it. Enjoy!

 //I actually don't know why this works, but it does! Some kind of glitch I presume. 
 //For some reason, when the flare is visible to the camera, and you then change it's layer to one that's on its ignore list,
 //the flare will then permanently stay on. (It doesn't work if you do this before the flare is visible; it will just never appear.)
 //By then changing its brightness to zero when it's outside of the camera's frustrum or when it's occluded by a collider, 
 //it acts like a more realistic flare.
 //
 //Note that the first time you look at the flare, it will fade in it's usual unrealistic manner, but will change subsequently.
 
 //Coordinates for the right and left hand sides of the screen respectively 
 //Change to "1" and "0" if you want the flare to disappear as soon as its invisible to the camera's frustrum.
 private var coord1 = 1.2;	
 private var coord2 = -0.2;
 
 //The strength of the flare relative to it's distance from the camera ("brightness = strength/distance")
 private var strength = 5;
 
 //Simple counter to ensure that the flare is visible for a few frames before the layer is changed
 var count = 0;
 
 function Start()
 {
 //Ensures that the flare's layer isn't part of its ingore list to begin with
 //Change to whatever you want, as long as it's not on the ignore list
 gameObject.layer= 0;
 }
 
 function Update () 
 {
 var heading: Vector3 = gameObject.transform.position - Camera.main.transform.position;
 var heading2: Vector3 = Camera.main.transform.position -gameObject.transform.position;
 var dist: float = Vector3.Dot(heading, Camera.main.transform.forward);
 var viewPos : Vector3 = Camera.main.WorldToViewportPoint (gameObject.transform.position);
 
 //Turns off the flare when it goes outside of the camera's frustrum
 if( viewPos.x > coord1 || viewPos.x < coord2 || viewPos.y < coord2 || viewPos.y > coord1)
 gameObject.GetComponent(LensFlare).brightness = 0;
 
 //Turns off the flare when it's occluded by a collider.
 else if (Physics.Raycast (gameObject.transform.position, heading2.normalized, Mathf.Clamp(dist,0.01,20)))
 gameObject.GetComponent(LensFlare).brightness = 0;
 
 else
 {
 //Sets the flares brightness to be an inverse function of distance from the camera
 gameObject.GetComponent(LensFlare).brightness = strength/dist;
 if(count<50)
 count = count+1;
 }
 
 //Changes the layer of the flare to be "Transparent FX"
 //Change it to whatever you want as long as you include that layer in your ignore list of the flare 
 if(count > 20)
  {
  gameObject.layer= 1;
  }
 }

@jmsosullivan

Looks like a nicely written script. However, it does nothing for flares attached to directional lights. Sadly, I think a true fix can only come from the Unity folks.

How does one attach a script to a lens flare? there is no option for this that I can see :S

Stumbled accross this post and thought I’d clear some of this info up…

@jmsosullivan Thanks for the script. Seems prone to some buggy behavior setting layers by ambigiuos numbers. Also i suggest the lens flare sets ignore layers to Everything, that way it might not perform the raycast, since you perform one already and it would be inefficient to double up such expensive calls.I’d make the Strength variable public so you can set it with the inspector too.

@Julien.Lynge Ummm of course it doesn’t affect Directional Flares… they’re directional, meaning infinitely far away. So it shouldn’t scale based on an infinite distance. Makes no sense.

@shi**er You attach a script to the game object, the same one that has the lens flare on it.