Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Spherical Fog Shader (shared project)

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

  1. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,193
    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
    twobob, Eric2241, chelnok and 3 others like this.
  2. id0

    id0

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

    Baxter900

    Joined:
    Mar 22, 2015
    Posts:
    7
    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:
    98
    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,448
    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?
    [​IMG]

    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

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,193
    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:
    105
    Can anyone confirm this is working with 5.5? This is what the fog looks like for me when testing the example scene: [​IMG]
     
  11. Samhayne

    Samhayne

    Joined:
    Jun 15, 2009
    Posts:
    9
    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:
    72
    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