Volumetric Lighting in BGE

Is it possible to do realtime volumetric lighting in the BGE? I looked up a bit and found most people do fake volumetric lighting and some say that a realtime one would take too much GPU to be processed, however they do not point a way of doing it, despite the huge amount of GPU needed. So I wondered if it is possible at all to do, since it would be a huge improvement to many games to have such a resource. If it is possible, can anyone one point out how to do a quality realtime volumetric lighting in BGE?

not until we get eevee, i think

You need to code a glsl shader and cache it to be faster. That means you need some knowledge of c++ and ogl glsl.

The Naive Approach:
‘real’ volumetreics requires a scene representation in volumes. So instead of polygons, you have to use voxels.
Let’s take a “small” area. It’s 1000x1000x1000 volume units. Each unit is represented by 4 bytes: rgb and alpha. Ooops, we just used up a whopping 4GB of ram! And because you need it on the GPU, it’s 4GB of VRAM. That’s more than most low-end systems have.
How big is 1000x1000x1000 volume units? If they’re 10cm, it’s a 100m x 100m area - or a single medium building. If your player walks in a straight line for a minute he’ll have left the volume area.

Then then there’s the sampling cost. You have to sample each voxel between you and each light source (assuming no scattering). For a 1000x1000x1000 unit cube, this is 1000 samples worst case - per pixel on your screen. So with three lights and an average distance of 500 units, and a 1920x1080 screen, you need to perform over 3000 million volume samples. Needless to say, you just completely cache-thrashed your GPU meaning performance slowed to a crawl. There’s no way (that I can think of) that you can cleanly access a 4GB volume texture.

By the time you add in scattering, your GPU has melted and burned a hole through the floor.
So that is why we don’t “brute force” volumetrics in that way. It’s unfeasible enough that I don’t think anyone’s actually tried.

Fake Method 1:
Why don’t we fill an area with really lightly textured planes that fade out when you are near. I mean, doesn’t this look great:
That ran at 5 FPS on GT 610 (60FPX on a GTX970 though). If the planes were more opaque you could see them, if there were fewer, you could see them.

At this point, it’s easier to just drop in a plane in about the direction of te light source, apply some clever fading tricks, and call it done (2004 style - and current indies) or hire a GLSL wizard to write a screen-space raytracer of some sort (AAA style)

^Ditto about the GLSL wizard - only I recommend that you try your hand at it yourself! : D
It’s pretty daunting at first, but the results are worth it.

Thank you all for your replies.
I’m gonna go with your Fake Method sdfgeoof, it looks really smooth and up to what I want. Your explanation on why a realtime volumetric lighting isn’t worth it is quite clearing and I thank you for that as well.
About the GLSL wizardry, I agree with you John_tgh. Since I started to work with blender and python, about a year and a half ago, I realised that it’s much more fun, and much more valuable to just learn how to do things yourself and do them the way you want. As you said, it’s a bit hard at first but when you get your things going it’s just so pleasant that you don’t care at all for the problems you had at the begginning. Having said that, I’m in a two-man game project right now and, given the amount of stuff we need to do I’m still a bit away from working with shaders, which I still find complicated. But for sure, sometime I’ll need to take a look at that and start working with it as well. Thank you for the link and the supporting words.
I’ll close this as solved since I’m satisfied with the answers.

Yeah, GLSL has a pretty steep learning curve.

If you add a subtle grainy texture to the volume planes then you don’t need quite as many. But it may not fit the style you’re going for. I’ll also warn you that it’s not a very performant solution as there is a lot of overdraw - but you do get a good approximation of a volume, you have lots of control over it and it does look good.

eevee has it I think

More hints - see how they do it in different games. :slight_smile: