Spherical Fog Shader (shared project)

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: Share: Volume Fog Shader - Unity Engine - Unity Discussions

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. :slight_smile:

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

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

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:

1782373–113211–SphericalFog.zip (148 KB)

7 Likes

Cool stuff, runevision, but is there a way to make a fog square?

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.

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?

It’s a shame this requires unity pro :frowning: Even for Unity 5?

Sasstraliss, just use Unity 5 personal edition.

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

Hi, i want to have a look at this one, however the link to download seems failed. Can u update the link plsz?

The link works for me. Try the usual things like clearing your browser cache and then try again.

Can anyone confirm this is working with 5.5? This is what the fog looks like for me when testing the example scene:

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.

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

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?

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!