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 Stampyturtle · Oct 30, 2013 at 03:24 AM · shadercgfragment

What is the proper way of calculating attentuation in Unity3D non-surface shaders?

In a CG vert/frag shader (NOT a surface shader), what is the proper way of calculating attenuation such that point lights with a small range do not cause hard edges to appear when the whole or partial light range is in view?

I have seen most examples of calculating light attenuation in Unity based on whether there is a directional light (1.0 attenuation) or point lights (subtracting _WorldSpaceLightPos0.xyz from world position and using the magnitude as distance). However, calculating attenuation for point lights in this way seems to have some strange behavior when fully or partially enclosed in view and stops flat instead of smoothly disappearing. Is there a better way of calculating it or perhaps a lighting macro in Lighting.cginc or another unity include I'm not aware of?

 if(_WorldSpaceLightPos0.w == 0.0){ //directional light
    atten = 1.0;
    lightDirection = normalize(_WorldSpaceLightPos0.xyz);
 }
 else{
    float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - IN.posWorld.xyz;
    float distance = length(fragmentToLightSource);
    atten = 1.0/distance;
    lightDirection = normalize(fragmentToLightSource);
 }

alt text

hard edge error.png (69.8 kB)
Comment
Add comment · Show 4
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 Owen-Reynolds · Oct 30, 2013 at 04:35 PM 0
Share

Have you looked at the standard attenuation math? It's been pretty well set for a decade, and not too difficult to read. The 1/(1+AD+BD^2) formula, where you tweak A and B.

I'd guess that just 1/D isn't giving enough fall-off. Then the "box" is Unity saying "O$$anonymous$$, the light is done for sure by now -- stop using it."

avatar image Stampyturtle · Oct 31, 2013 at 12:26 AM 0
Share

What factors do A and B represent in that equation? I've looked at some of the standard math, but this is what I was presented in the overwhel$$anonymous$$g majority of the tutorials I've seen for Unity CG shaders as how to calculate attenuation. Thank you for the plug on better attenuation formulas though, I've found better research for them since you posted. I've found since then that its not simply an attenuation problem anymore. See the updated question from my profile in just a moment.

avatar image Owen-Reynolds · Oct 31, 2013 at 03:55 PM 0
Share

A and B are just tweaks. Dividing by D^2 is real-world correct, but can give too small a circle. Dividing by D makes the light go a little too far. Dividing by D^1.8 might work, but a pain to compute. So, use a mix of /D and /D^2, deter$$anonymous$$ed by A and B.

1/(0.2D+0.8D^2) will be a little more puffed-out that just 1/D^2. If they don't add to one, no problem, but it just cancels out the brightness on top and makes it harder to read.

avatar image Karnsteiner · Nov 01, 2013 at 10:39 AM 0
Share

So A and B are user-set constants set to achieve a certain type of fallof then? I saw this equation on a few sites referencing OpenGL's default light attenuation, but I guess I didn't understand that the linear and quadratic factors were actually user manipulated.

0 Replies

· Add your reply
  • Sort: 

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

CG Programming, Points Lights attenuation and light range 3 Answers

Shader that renders fragment behind 0 Answers

Need help with this Shader; camera Solid color is being rendered on skybox. 0 Answers

Find out if an object is in front of a target in fragment shader? 0 Answers

Blend textures in shader 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