• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cap · Jun 04, 2010 at 11:34 AM · scalelightdistancelens-flare

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 t$$anonymous$$s is that my game supports camera dolly movements w$$anonymous$$ch 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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 t$$anonymous$$s to your lens flare object. It changes to value of Brightness as an inverse function of distance to the camera selected.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmsosullivan · May 02, 2011 at 01:04 AM 0
Share

Of course, change "value" as required for the intensity you want. The larger "value" is, the stronger the lens flare.

avatar image sathya · Aug 24, 2012 at 11:03 AM 0
Share

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 !

avatar image raziel_anarki · Jun 20, 2014 at 08:43 PM 0
Share

the code above did not work for me in unity 4.5 (the flare size was off when zooming), so i adjusted it a bit:

 using UnityEngine;
 using System.Collections;
 
 public class FixedFlare : MonoBehaviour
 {
     public float size = 3.0f;

     void Update ()
     {
         float ratio = Mathf.Sqrt (Vector3.Distance (transform.position, Camera.main.transform.position));
 
         GetComponent<LensFlare> ().brightness = size / ratio;
     }
 }
 
avatar image
0

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.", t$$anonymous$$s);
             Destroy(t$$anonymous$$s);
             return;
         }
 
         Size = Flare.brightness;
     }
 
     void Update()
     {        
         float ratio = Mathf.Sqrt(Vector3.Distance(transform.position, Camera.main.transform.position));
         Flare.brightness = Size / ratio;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to scale a object with distance from the center of the canvas? 1 Answer

End the Lense flair issue. 0 Answers

Creating a Mesh Inset by a Specified Distance 3 Answers

How to scale an object between two values over distance 1 Answer

Disappearing flares 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges