Game Development Community

dev|Pro Game Development Curriculum

Volumetric Fog object

by Richard Marrevee · 08/12/2014 (6:24 am) · 13 comments

Looking at some of the missions in my game The Master's Eye I felt that they were missing something related to the ambience of this missions. In one case it was a room where a furnace was spitting out smoke, but the room was clear and not filled with dust/smoke, in another one there is a graveyard which needed a more spooky ambience. The common factor here was that both missions needed a little bit of fog, but only in the assigned areas. The common fog in T3D doesn't let you assign an area whit fog, so I decided to create a Volumetric Fog object! Browsing the internet I stumbled upon a document describing how this could be achieved with multiple rendertargets and some shaders. The only little problem was: I didn't know much about multiple rendertargets and shaders, so the dreaded learning curve was very steep.

But now, after a few weeks (holiday not included), I have something that works!!

At first it was only capable of rendering a box-shaped volume hardcoded in the object, but now it is possible to render almost any shape as a fogvolume, you can even use an apple as a volume as you can see in the following image.

games.marrevee.com/blogpics/apple.png

So how does it work?

It is a 3 step rendering using multiple rendertargets and 2 shaders. The first step is rendering the depth values of the backside of the shape to a rendertarget using some kind of a prepass shader. The next step is rendering the depth values of the front of the shape to another rendertarget using the same prepass shader. Finally, render the fog in the scene by subtracting the 2 rendertargets to get the thickness of the fog.

This works great if there is only fog. Unfortunately it could be that there are some objects in the fog. So to catch this it uses the prepass buffer to determine the depth value at the current screen position. If this value is within the front to back range than this pixel is in the fog and we will use this value to determine the thickness of the fog. If it is beyond the back depth then this pixel is completely fogged, if it is less than the front depth than it is not fogged.
Another case is when the camera is in the fog, but that was fairly easy to solve by taking the camera position and check if it was within bounds, if so than act accordingly.

A few other things appeared along the way. There was no good way to clip the volume to the surrounding environment, so I used the prepassbuffer again to clip the volume. Only the depth values of the front and back targets didn't seem to be compatible with the depths achieved from the prepassbuffer. I tried some kind of multiplier on the values and that did seem to work well until I tried it in another mission :( .
At last I figured out what the problem was :) . The visible distance has an effect on the depth values of the prepassbuffer (all has to be in the 0.0 to 1.0 range) so having a visible distance of 1000 gives a much lower precision than a visible distance of 250. So taking this value in account it solved the clipping problem.

There are still some issues with it, like handling concave shapes and overlapping volumes, but for now it is what I need.

I guess you are curious to see it work, so here is a vid of it.



So what's next?

There is some kind of modulation of the thickness of the fog using a texture, which can be animated, but it doesn't work that great, so it needs some more work on it. Also fixing the issue with overlapping volumes and being able to use concave shapes would be nice, but for now I focus on releasing the playable demo of The Master's Eye which is delayed over and over again, and starting a funding campaign.

Should I expose this object to the Community?

Mmmmmmmm, good question. First of all I don't know if there is much interest in it ;P. On the other hand, it took some time to create this and delayed the demo and campaign of my game (it is time to generate some income from this) and giving it all away for nothing is something of a bridge to far at the moment. If I decide to sell it, it would be a very reasonable price though. Another thought that crossed my mind about this is if people wanting the code make a donation of a self-determined amount of money here at my website and choose Donations.

I hope you have enjoyed this blog and I thank you for reading it.

Edit:

I just made a vid on how to add this object to your missions by using the world editor (view it in HD if you can).


Edit 2:

Blog continues here.

Richard

About the author

Started programming in 1984 on an Oric, when time progressed switched to MSX, Amiga and finally the Windows PC with T3D. Now developing an epic fantasy game: The Master's Eye. Creator of the DoorClass pack and VolumetricFog pack @ richardsgamestudio.com


#1
08/12/2014 (7:01 am)
Cool stuff, Richard! :)
#2
08/12/2014 (7:05 am)
Richard, this is something Ive been looking for. Can you please contact me at pyoskowitz@winterleafentertainment.com
#3
08/12/2014 (12:01 pm)
And it looks like it performs pretty well, too!
#4
08/12/2014 (2:07 pm)
Very nice effect. This would be a great addition to the engine if you decide to release it. Then everyone can look at the code and make improvements :D so you end up with a better version.

Even if you don't release it, I appreciate the write-up on how you did it. If someone was so inclined it shouldn't be too hard to replicate what you did by following your post.

#5
08/12/2014 (3:56 pm)
Looking great, as mentioned above it should be apart of the engine maybe a community buyout may be a good idea. Nonetheless it's still a great feature and good luck with your game!
#6
08/12/2014 (5:37 pm)
Wow, this really would improve the engine. You did a great work!
If you don't want share it for free, i would make a Add On so ppl can buy it. Such nice fog is much need!
#7
08/12/2014 (9:18 pm)
Awsome that would be nice to Have :)

terry
#8
08/13/2014 (12:31 pm)
@all:
Thank you for the positive reactions. Today I've tested it on a clean install (of the DX9-DX11 version) and it worked as expected. The next thing I will do is make the animated modulation of the fog density/thickness work which shouldn't take longer than a week. In the mean time I will make up my mind on how I will expose this thing to the community.

For those who are really interested in using this:

I will post regularly on Twitter about this and other projects (Twitter: @RichardsGame) or you might drop me an e-mail: richard at marrevee dot com, without the blanks, or contact me through my website.
#9
08/13/2014 (1:12 pm)
Nice work Richard. This was one of the things that fell out of the engine way back in the TSE days. It is cool to see someone finally managed to handle the beast ;)
#10
08/14/2014 (2:21 am)
Indeed this game is nice to behold. In fact the game and its look reminds me of Oblivion in many ways. That Volumetric Fog object feature is opening new possibilities for level designs.

That I call innovation and creative thinking! :O)

#11
08/14/2014 (5:02 am)
I agree with Dwarf, the game looks nice. Great job!
#12
08/14/2014 (6:09 am)
Richard,

Great work! If you decide to release it as a resource, a pay product or a write up on how to add to torque, please let me know as I can really use something like this for my levels I'm designing. Please put me on your email list: info AT hallowscreams DOT com if you would. Thanks so much man!

Will
#13
08/16/2014 (12:44 am)
Thank you all for the positve responses. I've added a new blog with a status update here.

@Will Zettler:
Added