- Home /
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.
Answer by jmsosullivan · May 01, 2011 at 06:15 PM
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.
Of course, change "value" as required for the intensity you want. The larger "value" is, the stronger the lens flare.
Works Fine but looks ugly. Is there any other way around. And it costs two draw calls each. Can't we just do it with shaders for mobile !
the code above did not work for me in unity 4.5 (the flare size was off when zoo$$anonymous$$g), so i adjusted it a bit:
using UnityEngine;
using System.Collections;
public class FixedFlare : $$anonymous$$onoBehaviour
{
public float size = 3.0f;
void Update ()
{
float ratio = $$anonymous$$athf.Sqrt (Vector3.Distance (transform.position, Camera.main.transform.position));
GetComponent<LensFlare> ().brightness = size / ratio;
}
}
Answer by emdzeyek · Feb 05, 2016 at 02:42 AM
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;
}
}
Answer by Lloyd_RedironLabs · Oct 13, 2016 at 01:31 AM
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;
}
}