Atmospheric scattering explained

An atmosphere shader in GLSL

Shader is here on shadertoy!

A sunset From space


This is classic volumetric raymarching, but I wrote the shader in a way to try and explain it as clearly as possible. It's not the most advanced shader ever, with the more notable feature of this shader being it's ozone layer. More modern approaches precompute part of the optical depth, to replace a number of raymarching steps with texture lookups to improve performance, as well as adding multiple scattering.

I did try to experiment with hacking in multiple scattering with rough approximations, but it didn't work


I've also experimented with trying to find an approximation to the optical depth, or calculating the lookup textures for the precomputed ones directly instead of using raymarching.

In the book GPU Pro 3, there's an article on atmospheric scattering that replaces the optical depth calulation with the chapman function, which means there's only one loop instead of two nested loops in the shader.

I'm interested in also trying to remove that loop, and simply getting one formula to calculate the result directly. I believe it is possible as I've managed to find a few more approximations to the optical depth.

A promising one here is 2 * exp(-x) * (2 - exp(-x)), where x is the distance of the ray to the center of the atmosphere. This approximation works best for the full ray, so doesn't work nicely from the surface, but it's quite accurate.

Machine learning

In the end, I think it would be worth it to write a reference path tracer, and use symbolic regression, or some other ML tool to figure out a more accurate formula to the entire calculation.

I want to do this eventually, but I'd first need to write a proper volumetric path tracer. It will likely also be hard to figure out how the resulting formula actually comes to the results.