Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 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.

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 this 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 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;
     }
 }
 
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.", 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;
     }
 }
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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Disappearing flares 0 Answers

Point lights disabled after 10 000 units 1 Answer

Render 3D object to a 2D texture at runtime 1 Answer

Large scales, increased clipping planes, and performance 1 Answer

How to scale a blob shadow plane smoothly as distance from ground changes 1 Answer


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