Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Extended Global Fog (Unity5)

Discussion in 'Works In Progress' started by Reanimate_L, Mar 24, 2015.

  1. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Hi Guys,
    I decide to share my modification of the global fog image effect that comes with Unity5, since unity lacking a free good looking fog.
    I extend it by adding some more option to control the global fog, also with fog scattering based from Preetham/Hoffman that scattered all across the net with all the parameter exposed in the inspector . Also keep in mind that i'm not a programmer so maybe my code are sucks or the result are incorrect :D
    Also i believe there's a better way to control the shader conditional, but yeah i'll try to update it if i made some changes :D.
    And if someone made a changes and willing to share it the community, just let me know i'll put it on the first post.
    Note : the built in fog Color also affecting the Final fog Color.
    Update: 08262016 you can get update for 5.4 in this post

    FogGUI.PNG
    PreethamFog3.PNG PreethamFog.jpg PreethamFog4.jpg
     

    Attached Files:

    Last edited: Aug 25, 2016
  2. Soul-Challenger

    Soul-Challenger

    Joined:
    Dec 30, 2010
    Posts:
    152
    This is looking great! Just one question: does the scattering work with multiple lights? I've seen a couple of similar approaches but it's always just for one directional light.
     
  3. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Sorry this one only support one directional light.
    And i believe it's possible to support multiple lights in Unity5 via Compute buffer.
     
  4. FreakForFreedom

    FreakForFreedom

    Joined:
    Jan 22, 2013
    Posts:
    154
    That's really beautiful! You, Sir, are amazing for sharing this with us! :)
     
  5. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    Hi rea, it includes only script file, no shader file?
    [Edit]
    I do have Preetham code in glsl, so you mean we can implement it by our own, right?

    By the way, I think the scattering fog supposes to support only 1 directional light as sun only.
     
    Last edited: Mar 24, 2015
  6. Soul-Challenger

    Soul-Challenger

    Joined:
    Dec 30, 2010
    Posts:
    152
    Some planets have more than one sun... :p;)
     
  7. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Damn....i'm being stupid....sorry guys,
    shader added.
    it's fixed now please redownload and let me know how it goes
     
  8. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    No problem rea. Thanks for sharing anyway :)

    I'm working my own version of scattering fog. Actually after i look through the code of Scrawk's Atmospheric image effect project, it is easier than i thought.
     
  9. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    you're welcome, btw why not use the same scattering that you used for uSky? it would make the fog match the sky color perfectly
     
  10. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Hmm it's possible but gonna be so expensive since you'll have to calculate the scattering twice from two suns, except if you implement a very simple and fast scattering for multiple lights
     
  11. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    Of course, I will implement it with uSky.
     
    Reanimate_L and SAOTA like this.
  12. sowatnow

    sowatnow

    Joined:
    Jun 12, 2014
    Posts:
    304
    Hi there,
    I am getting the following error:

    Assets/Standard Assets/Effects/ImageEffects/Scripts/GlobalFog.cs(9,23): error CS0246: The type or namespace name `PostEffectsBase' could not be found. Are you missing a using directive or an assembly reference?
     
  13. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    You need to import that file from Unity standard package "Effect", inside has PostEffectsBase.cs file.
     
  14. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    @rea,
    I think you try to modify Preetham/Hoffman code to get the effect you want. However I'm not quite understand it.
    I'm still grateful that you have shared it, I want to share something else here. I ported a 99.9% pure Preetham/Hoffman physical day sky codes directly for Unity 5 skybox shader, I think I got the default value pretty close to real sky color. This shader has not been optimised. People may check it out for learning purpose or implement it as a base for their own fog.
    Keep in mind that it is just for only day sky, if you want some sunset color, you may need to modify it.

    To use it, just create a new material and assign this shader, then apply it to Lighting skybox.
    [Edit]
    This skybox shader is default in linear color space.
    Preetham_Hoffman_Skybox.png
    [Edit]
    Fixed the shader for PC
     

    Attached Files:

    Last edited: Apr 1, 2015
  15. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Awesome luckymouse :),
    yeah actually there's still some stuff that confuse me in the code that i use, especially the world scale value
     
  16. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
  17. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    I have no problem, are using unity 5, right? does the scene have any directional light ?
     
  18. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    yes and yes
     
  19. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    Strange:confused:. I checked compiled code, seems everything fine. So should be working on window.(i'm on Mac)
    Any error message from shader?

    [Edit] Try this attached project file
    [Edit] Deleted
     
    Last edited: Apr 1, 2015
  20. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Nope no error, also got black sky with your project. This is weird....
     
  21. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,182
    Black everything here also.
     
  22. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    I found out one of the reason why this shader get black in PC, All the const need to change to static const .
    so It won't got pitch black, but the sky color is still very dark by the default value.
    Still checking whats wrong....
    However it works all fine on Mac, so strange.
     
  23. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    Ok, beside the issue above, also it seems the totalRayleigh(float3 lambda) function can not get the correct value on PC,
    I did a quick hack to return the final value. This attached shader will work on PC, Tested on PC. I replaced the early posted file as well.
     

    Attached Files:

  24. x_african_warrior_x

    x_african_warrior_x

    Joined:
    Jun 24, 2014
    Posts:
    80
    what happened to my skybox?
    before: scattering-off.jpg after
    scattering-on.jpg
     
  25. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    @rea in your pictures, the skybox seems visible through the fog, and your settings show that you checked "Affect Sky Box".
    For some reason, even if I mimic your settings, the skybox always get occluded by the fog. I can only see the fog, it's colors and scatering. Its beautifull but your pics are even more so, since you can actually see the clouds...
    Any ideas why my (and I supose the guy above) skybox gets occluded?
     
    x_african_warrior_x likes this.
  26. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    set the fog intensity in lighting tab to a very low value and disable distance fog
     
    odival likes this.
  27. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Thanks @rea, that solved it. I hadn't realized how much impact the built in fog settings had on your extended one. Sorry to bother again but one last question:
    The sunrises look just like they would on Earth, but say I want a more blue-ish or green-ish sunrise, as if it was a alien sky... can I do that just by messing with the settings of the Extended (or built in) fog? Or those colors are hard coded?
     
  28. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
    Thanks for sharing, but I am unable to reproduce the nice bluish mountains in the distance. Would you be so kind to post a simple example scene with some terrain (just a bump will do) which shows the same effects as in the screen shots you posted?
     
  29. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    The blue mountains are the fog. The fog is blue.
     
  30. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
    That is not the way it works, or should work anyway.

    Blue wavelengths travel gets filtered out less by the atmosphere than red and green. So the further away an object is, the more blue it appears. This phenomenon is called Rayleigh Scattering, and since that effect is included in this shader, I assume it makes a correct approximation of it and not just adds blue fog like you mentioned.
     
  31. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    So very true, the problem if i ignore the fog settings in the lighting tab you gonna get double fog. Since the default fog cannot be turned off if we want to use Global fog. Not sure if this behaviour are changed by unity in the latest version.
     
  32. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
    Interesting. This is a not-so-nice Unity limitation. Can someone from Unity jump in and provide a workaround for this, if possible?
     
  33. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    or....you can just set the fog color to black in the lighting tab (not sure if it's a good idea or not)

    it's possible just play around with the turbidity , world scale, and rayleigh settings. start with zero value would make it easier.
     
    odival likes this.
  34. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    :(
    upload_2015-4-23_10-49-33.png
     
  35. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
    Last edited: Apr 23, 2015
  36. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Yeah physical based fog should be an option in the built in fog settings, not as an image effects
     
  37. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
    @rea,
    Why not modify the build in fog shader also?
     
  38. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Ehh is that even possible?

    Well this is sucks @.@
    upload_2015-4-23_13-55-18.png
     
    Last edited: Apr 23, 2015
  39. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
  40. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Ah..for that you need to edit all the shaders in your project
     
  41. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
  42. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    well except if Unity built in Fog function are already exposed somewhere
     
  43. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
    Yeah, I tried looking for that but couldn't find anything.
     
  44. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
    Wait, if you turn off distance fog then it doesn't use the build in fog at all, right? If you then set the global fog high enough, you could achieve a similar effect. Then you have total control over the color, ditch the build in fog color fetch and use your own. Could that be implemented?
     
  45. antislash

    antislash

    Joined:
    Apr 23, 2015
    Posts:
    646
    Hi Rea,
    funny, i was exactly looking for that fog http://forum.unity3d.com/threads/he...g-shader-or-image-effect.320945/#post-2081589
    fortunately you did a good job, really...
    the only little improvement i could wish is a general shadowing of the scene and a desaturation due to ligh loss.
    looks better with a little DOF effect tough.

    i have a little problem with directionnal lights : they are visible through trees foliage , odd.

    thanks again for your great work, almost perfect
     
  46. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    can you explain more about this, some images and sample would help
    can you post some screenshot of this?
     
  47. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,334
    Is there a way to draw the scattering highlight behind objects instead of in front?

    scattering.jpg
     
  48. Luckymouse

    Luckymouse

    Joined:
    Jan 31, 2010
    Posts:
    481
    Yes, the Mie scattering can be block by the depth (Z-Buffer) value.

    modify the rea's shader line #263 :

    Code (CSharp):
    1.         float3 betaMTheta = float3(betaM * mPhase).xyz * saturate( dpth - 0.5 );
    (dpth - 0.5) mean blocked from half of the depth.
    (dpth - 1.0) mean blocked everything, no scattering behind the mountain.

    You can make a slider to control this value.
    Hope it helps.
     
    Last edited: Apr 23, 2015
    TakuanDaikon and Elecman like this.
  49. antislash

    antislash

    Joined:
    Apr 23, 2015
    Posts:
    646
    @rea
    ok, i just took a screenshot of a scene, then blended with the zbuffer (or kinda) to obtain my ideal fog.
    as you see in the attached image, my good fog looks almost like yours excepted that ideally, the fog should globally darken the scene and desaturate colors (as well as smooth shadows but that's another trick).
    fog also should blur shapes (i guess i can do that with DOF)

    the problem i have with lights is that they show though foliage instead of being altered by scattering. (see pic)

    thanks again for the good work
     

    Attached Files:

  50. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,382
    Oh hey i was looking for this, do you mind if i add it to the extended fog?

    The first one is easy actually but the blur pass would make the fog bit more expensive, the second one i'm still don't get it.
    You mean like all the light are scattered by the fog or glowing/blooming under the fog?
    Note : Image from Nvidia demo
    upload_2015-4-23_23-9-6.png
     
    Last edited: Apr 23, 2015