Lens flare assets: some requests and a possible bug

Hello

The Unity lens flares assets are great.

But could it be possible to:

1 - Make the speed of fade modifiable.

For now it is so slow that the flare appears behind the collidable object and fades away behind it.
A faster speed would make it more realistic.
Remember that light goes at the speed of … light. Hence the flare should disappear instantaneously behind an occluder.

2 - Make the flare ignore some colliders

Could you make the flare ignore some colliders tag?
This could permit to have an atmosphere, glass or shield force that doesn’t occlude the flare.

Seems this thread also mention it.

3 - Fade out flares with distance to camera

The flares should decrease (linear, exp, …) with distance
http://forum.unity3d.com/viewtopic.php?t=15269
With settable param of course

4 - The Fade bool parameter doesn’t seem to work.

Not sure about this one. Maybe I didn’t understand what this bool do.
Isn’t the Fade param intended to make the flare disappear instantaneously if false when passing behind a collider?

Setting it false or true doesn’t seem to change anything.
Is this a bug or am I mistaken something?

5 - In the editor the collider must be bigger than the mesh???

Strangely I have this only in the editor not in the standalone.

As you can see in the screenshot below the collider must be much bigger than the mesh to be realistic.
The collider radius must be nearly exactly 2.6 to make the flare appear at the mesh border

In fact the more the camera is away from the collider the bigger it must be relative to the mesh dimensions!
To make it realistic I will need to change the collider size depending on the distance from the camera… :frowning:

Of course this is also inconvenient because the collider doesn’t match the object.

Am I missing something here or is this also a “bug/feature”?

PS: Doest someone has a custom flare code that does all that? :smile:

Thanks

Im having some issues with lens flare too, as I reported in

http://forum.unity3d.com/viewtopic.php?t=44852

It would be nice if the lens flare use HW Occlusion Query (when available) instead of raycasts, so we could have partially visible flares

Here’s some code to alter the brightness of flares as a linear inverse function of distance from the camera. Just put the script on the main camera in the scene.

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 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;
  }
 }

Had a quick mo’ to convert this to C#. No real testing, but I thought I’d share, as the slow fade is annoying.

using UnityEngine;
using System.Collections;

public class FlareFade : MonoBehaviour {
	 //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 float coord1 = 1.2f; 
	 private float coord2 = -0.2f;
	
	
	 //The strength of the flare relative to it's distance from the camera ("brightness = strength/distance")
	 private int strength = 5;
	
	
	 //Simple counter to ensure that the flare is visible for a few frames before the layer is changed
	 int count= 0;
	
	
	 void  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;
	 }
	
	 void  Update (){
		 Vector3 heading = gameObject.transform.position - Camera.main.transform.position;
		 Vector3 heading2 = Camera.main.transform.position -gameObject.transform.position;
		 float dist = Vector3.Dot(heading, Camera.main.transform.forward);
		 Vector3 viewPos = 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.01f,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;
		}
	}
}

Let me know if there are any errors…

1 Like