Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Spherical Fog Shader (shared project)

Discussion in 'Shaders' started by runevision, Sep 21, 2014.

  1. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Hi!

    (Note, I'm posting this in my spare time as a regular Unity user, not representing Unity Technologies in this particular post.)

    I wanted to replace skybox + standard fog with a spherical fog volume in my game that would serve as both fog and atmosphere.

    I searched around and found this thread: http://forum.unity3d.com/threads/share-volume-fog-shader.147323/

    This was a nice starting point, but I found that the fog effect in that thread didn't actually behave as fog - things didn't get more occluded by fog with distance from camera.

    I created a heavily modified version which I've included in the attached project. Things I changed:
    • Fog occlusion is dependent on volume of fog a pixel is seen through.
    • Fog doesn't add up linearly, but increases inversely exponentially. That's very similar to a bunch of alpha blended planes behind each other: If they all have 50% opacity, the first will occlude up to 50%, the next up to 75%, the next up to 87.5% and so on.
    • I changed the spherical volume to be entirely calculated with formulas in the shader. The shader can be attached to just a quad and still fully simulate a spherical volume.
    • There was some weird mirroring artifacts I got rid of.
    • I wanted to have greater artistic control over coloring, so I exposed settings to have the most dense fog have a different color than less dense fog. This great for controlling the gradient better when the edge of a spherical fog volume is used to create a sky gradient.
    • Added a setting to specify at which ratio the max density is reached. So instead of max density being in the center, the volume could have max density 90% out from the center towards the sphere radius, with all of the inside using a constant density.
    Some issues and limitations:
    • Effect is not reliable outside of play mode. Sometimes there's weird artifacts in Scene View, or the effect doesn't show up at all. I don't know why.
    • I think the effect requires Unity Pro in 4.x (for 5.x any version will work).
    • I'm by no means a shader expert, so I could be doing something stupid or highly inefficient in the shader.
    • Seems to work well for me on desktop but I haven't tested on mobile at all and suspect it's too heavy for mobile if it works at all.
    Use at your own risk!

    Also, if you make improvements, please let the rest of us know. :)

    Screenshot from example scene in the attached project - note the spherical volume is a single quad:
    SphericalFogEditor.png

    Screenshot from example scene in attached project with an object halfway submerged into spherical fog: SphericalFogExample.png

    Screenshots of spherical fog used for fog/atmoshere in my game The Cluster (not included in attached project). There is no standard fog, only a huge spherical fog: SphericalFogInGame.png
     

    Attached Files:

    Last edited: Oct 19, 2015
  2. id0

    id0

    Joined:
    Nov 23, 2012
    Posts:
    455
    Cool stuff, runevision, but is there a way to make a fog square?
     
  3. Baxter900

    Baxter900

    Joined:
    Mar 22, 2015
    Posts:
    8
    Hey, just wanted to let you know I found this really useful. I was struggling through writing a basic volume shader on my own, and I was able to hack this into mostly working for my purposes. It has a bunch of artifacts I still have to work out, so I don't think it's worth sharing yet, but it gave me a simple volume shader when all I really need is a semi-transparent object that doesn't look hollow when your inside it.

    Anyway, for my purposes, I can argue that the artifacts are on purpose, or even use them to my advantage.

    Here's essentially what I did, though I ran through a few tweaks in the shader itself to customize it for my specific needs after following these steps:

    Apply the shader to a regular object, then in the C# code change it so the radius is equal to the lossyScale of the biggest dimension of your object, and change the scaleFactor to something larger, I found 1.4 to be best. A tweak that I did which is really kind of necessary is to allow the Inner Ratio to be negative (Change it at the top of the shader file). Doing that allows you to control transparency without changing density.

    Here's the artifacts that will appear though, if I ever remove these I'll update this with the code to do that.

    • For the distance of your radius*scaleFactor, the object won't be occluded in any way, though it will fade at the edges of that.
    • From the inside of the object, you won't be able to tell where the edge is (In fact, it just applies a fog-like effect to everything).
    • The inside of the object looks slightly different than looking into it, it's almost unnoticeable though.
    • When looking through the object, all Non-transparent objects will occlude transparent ones, regardless of the distance, or the opacity of the transparent object. I think this is due to the new Unity 5 shader.
    • In the editor, the shader let's itself be occluded by everything, no clue why, just know that it's there, even if it looks like it isn't.


    Anyway, hope that helps someone else who, like me, is just starting in shaders and requires a volume shader (which Unity should really include!!!!) I'll probably buy one from the Asset store or learn more shaders and code my own if I really ever need it for much.
     
  4. Teejay5

    Teejay5

    Joined:
    Feb 26, 2010
    Posts:
    106
    This is so awesome, thanks for sharing.
    I did notice that it doesn't play well with transparent objects, the fog will always render on top of other transparent objects (particles, even the standard shader in Transparency mode). Does this have something to do with depth textures or whatnot? Can anyone think of a workaround other than not using materials that use alpha?
     
  5. Sasstraliss

    Sasstraliss

    Joined:
    Feb 24, 2013
    Posts:
    10
    It's a shame this requires unity pro :( Even for Unity 5?
     
    Last edited: May 30, 2015
  6. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Sasstraliss, just use Unity 5 personal edition.
     
  7. Spencer-Evans

    Spencer-Evans

    Joined:
    Feb 26, 2016
    Posts:
    1
    I'm getting a fatal error when I try to load up the project. Anyone else getting this?


    OS X El Capitan v 10.11.3
    Unity 5.3.0f4 Personal Edition
     
    Last edited: Feb 26, 2016
  8. rain330047

    rain330047

    Joined:
    Jun 3, 2016
    Posts:
    10
    Hi, i want to have a look at this one, however the link to download seems failed. Can u update the link plsz?
     
  9. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    The link works for me. Try the usual things like clearing your browser cache and then try again.
     
  10. BadSeedProductions

    BadSeedProductions

    Joined:
    Dec 26, 2014
    Posts:
    144
    Can anyone confirm this is working with 5.5? This is what the fog looks like for me when testing the example scene:
     
  11. Samhayne

    Samhayne

    Joined:
    Jun 15, 2009
    Posts:
    45
    Hmmm.
    Anybody got an idea why the square area around the fog is black when testing it on my old intel machine?
    Transparency doesn't seem to work there.

    The middle area (with the fog) looks fine.

    And no problem at all with my NVidia card.
    Hm.
     
  12. Gray_Master

    Gray_Master

    Joined:
    Jun 20, 2010
    Posts:
    152
    if color fallof set to maximum & allways black arround then (may be) your Intel Graphycs not support Shader Model 3.0 ?
    Ps 2 BadSeedProductions, - in my Unity 5.5 (b11) work correctly
     
  13. eisensafran

    eisensafran

    Joined:
    Oct 4, 2017
    Posts:
    1
    Although the original post is over three years old: I found this solution very helpful. Thank you very much runevision!

    Since I would like to implement this fog in a testing setup I am looking for a solution to implement an alpha noise to it in order to distribute the fog more "natural" (unevenly). Does somebody have some hints for me how to achieve this without loosing the overall aesthetics of the runevision fog?
     
  14. unity_B26573D76D106785FD79

    unity_B26573D76D106785FD79

    Joined:
    Dec 4, 2023
    Posts:
    1
    Bit late, but I found when trying the test scene, that the fog was on planes. If you replicate the process of adding the Fog script to a sphere and using an existing fog material you can get a spherical fog.
    Hope this helped!