Position-dependent lens flare scale?

So, lens flares scale themselves up in terms of the visible world when the camera moves away from them and down when it moves toward them so they always render with (almost) the same scale on screen. Is there any way to make them act like standard objects, so they scale up as you get closer and down as you move away? I've already altered the various settings (zoom, etc) for the flare material's layers but these only affect the flare when it's partially obscured by geometry.

The reason I'm after this is that my game supports camera dolly movements which vary between very close and very distant, and the lens flares tend to obscure the objects they're attached to when moving the camera out to show more of the environment. I'd rather not just turn the lights off once the camera reaches a certain distance as a small flare could aid in visibility of tiny objects.

var main : Camera;

var value = 5;

function Update () 
{

var heading: Vector3 = gameObject.transform.position - main.transform.position;
var dist: float = Vector3.Dot(heading, main.transform.forward);
gameObject.GetComponent(LensFlare).brightness = value/dist;

}

Attach this to your lens flare object. It changes to value of Brightness as an inverse function of distance to the camera selected.

For Uniy 5.1

using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class LensFlareBrightness : MonoBehaviour {
public Camera camera;
public LensFlare lf;
public Transform tr;
public Vector3 cam;
public float size = 3.0f;

void Start()
{
    cam = camera.gameObject.transform.position;
}

void Update()
{
    cam = camera.gameObject.transform.position;  
    float ratio = Mathf.Sqrt(Vector3.Distance(transform.position, cam));
    GetComponent<LensFlare>().brightness = size / ratio;
}

}

Simple solution, thanks guys.

Just wanted to contribute my version back. Tested U5.4.

  • Automatically uses the existing lens flares brightness as the “default” (close up) value
  • Can drag and drop a LensFlare onto the script, OR, it will check the current GameObject for a LensFlare for assignment
  • Can just be dragged and dropped on a GameObject using a dedicated LensFlare (not the Camera LensFlare)

Save the following as LensFlareFixedDistance.CS

using UnityEngine;

public class LensFlareFixedDistance : MonoBehaviour
{
    private float Size;
    public LensFlare Flare;
    void Start()
    {
        if (Flare == null)
            Flare = GetComponent<LensFlare>();

        if (Flare == null)
        {
            Debug.LogWarning("No LensFlare on " + name + ", destroying.", this);
            Destroy(this);
            return;
        }

        Size = Flare.brightness;
    }

    void Update()
    {        
        float ratio = Mathf.Sqrt(Vector3.Distance(transform.position, Camera.main.transform.position));
        Flare.brightness = Size / ratio;
    }
}