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
2
Question by Jaap Kreijkamp · Jan 27, 2012 at 04:24 AM · camerafogformula

Formula to calculate Far Plane from Fog Exp2 Density?

Would anyone know the formula to calculate the maximum distance you can see with a certain fog density, in fog exp2 mode? Thanks.

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
4

Answer by FishSpeaker · Jun 06, 2012 at 07:32 PM

You want to put your far clip plane at Sqrt( Log( 1/0.0019 ) )/density for Exp2 fog.

A general solution for any type of fog would be:

     if( RenderSettings.fogMode == FogMode.Linear ) {
         camera.farClipPlane = RenderSettings.fogEndDistance;
     } else if( RenderSettings.fogMode == FogMode.Exponential ) {
         camera.farClipPlane = Mathf.Log( 1f / 0.0019f ) / RenderSettings.fogDensity;
     } else if( RenderSettings.fogMode == FogMode.ExponentialSquared ) {
         camera.farClipPlane = Mathf.Sqrt( Mathf.Log( 1f / 0.0019f ) ) / RenderSettings.fogDensity;
     }

The Fog Formulas are detailed here.

For Exp2 fog, that's f = 1/e^((d * density)^2), where:

  • f=0 is all fog and f=1 is no fog.

  • d is distance from the camera to the pixel being rendered.

  • density is the fog density set in Unity, limited between 0.0 and 1.0.

So we want to calculate d, such that f will be 0. However, for f to be zero, e^((d density)^2) would have to be infinity, so instead we can just solve for a sufficiently small f of 0.0019* (0.0019 is just under 0.5/255, so the contribution of the scene should round down to 0 at this point, assuming an LDR pixel).

Solving for d gives you this, as noted above:

 d = Sqrt( Log( 1/0.0019 ) )/density

Note that the OpenGL docs regarding this can be confusing for a number of reasons, mostly because of bad formatting on many of the web pages I've seen.

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
1

Answer by Steven-1 · Apr 15, 2012 at 08:48 AM

the fog calculation for exp2 is: F = e^((-D * z)^2) (F=1 means no fog, and F=0 is complete fog) see: http://forum.unity3d.com/threads/28812-Fog-falloff-quot-equation-quot

which means the distance has to be infinte for the fog to be completely opaque

but almost opaque should be good enough, so use:

 float getFogDistance(float D, float F=0.005f) //0.005 is almost completely opaque fog
 {
     return Mathf.Sqrt(Mathf.Log(F))/-D; //note that for F=0, it will throw an error (i think)
 }
Comment
Add comment · Show 2 · 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 FishSpeaker · Jun 06, 2012 at 06:45 PM 0
Share

The formula for fog referenced from the thread, which is in turn referenced from an OpenGL tutorial, is wrong. It's listed as F = e^((-D z)^2), but the actual formula is F = e^(-(D z)^2). The negative sign is outside the square, and the formula is more clearly stated as F = 1 / e^((D * z)^2).

It's actually really confusing to try and look this up on the web, because the OpenGL doc sources seem to get formatted differently on different sites.

On this page (http://www.khronos.org/opengles/documentation/opengles1_0/html/glFog.html), it looks like a $$anonymous$$us sign between density and z.

This page (http://www.opengl.org/sdk/docs/man/xhtml/glFog.xml), which is xml, gets formatted all kinds of wonky depending on which browser you're using.

The Direct3D page does a much better job: http://msdn.microsoft.com/en-us/library/windows/desktop/bb324452(v=vs.85).aspx

avatar image metaleap · Nov 22, 2013 at 04:32 AM 0
Share

Note, $$anonymous$$athf.Sqrt($$anonymous$$athf.Log(0.005f))/-D gives NaN for typical fog densities whether D=0.0015 or D=0.025...

avatar image
0

Answer by jakovd · Jan 27, 2012 at 10:45 AM

I'm not a fog expert but doesn't fog setup work in a way where you set fogStartDistance and fogEndDistance? fogEndDistance being that maximum distance where your object is completely not visible. fog density is just a function that determines how much fog is applied on objects between fogStartDistance and fogEndDistance.

Comment
Add comment · Show 1 · 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 Jaap Kreijkamp · Jan 28, 2012 at 01:48 AM 0
Share

Only when you're using linear fog, you have three types, linear, that uses start and end distance, exp and exp2 where you use a density value. I use exp2 fog.

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

8 People are following this question.

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

Related Questions

Strange lighting on large-scale objects when using fog in VR 2 Answers

Global Fog, rotating the camera by itself 0 Answers

Unity - How to hide the Far Clipping Planes of camera? 1 Answer

Implement fog settings independent of the Scene Render settings. 4 Answers

Fog Effect Horizon Flicker 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