 Alan Zucconi in maths, shader, tutorial, Unity3D | October 10, 2017

This tutorial finally concludes our journey to simulate Rayleigh Scattering for a planet’s atmosphere. The next (and final) part will show how to change the shader to also include an additional type of scattering, known as Mie Scattering.

You can find all the post in this series here:

You can refer to the Atmospheric Scattering Cheatsheet for a complete reference of all the equations used.

You can download the Unity package for this tutorial at the bottom of the page.

Sampling the View Ray

Let’s recall the equation for the atmospheric scattering that we have recently derived: The amount of light that we receive is equal to the amount of light emitted from the sun, , multiplied by the sum of the individual contributions from each point in the segment .

We could go one and implement that function directly in our shader. However, there are few optimisations that can be done. It was hinted, in a previous tutorial, that the expression could be simplified even further. The first step we can take is to decompose the scattering function into its two basic components: The phase function and the scattering coefficient at sea level are constant with respects to the summation, since the angle and the wavelength do not depend on the sampled point. Hence, they can be factored out: This new expression is mathematically equivalent to the previous one, but is more efficient to calculate since some of the most heavy parts have been taken out of the summation.

We are not ready to start implementing it. There are infinitely many points we should take into account. A reasonable approximation to is to split into several smaller segments of length , and to accumulate the contribution of each individual segment. In doing so, we assume that each segment is small enough to have a constant density. Generally speaking, that is not the case, but if the is small enough, we can still reach a reasonably good approximation. The number of segments in is referred to as the view samples, since all segments lie on the view ray. In the shader, this will be the _ViewSamples property. By having it as a property, it is accessible from the material inspector. This allows us to reduce the precision of the shader, in favour of its performance.

The following piece of code allows looping through all the segments in the atmosphere.

The variable time is used to keep track of how far we are from the origin point , and is incremented by ds after each iteration.

Optical Depth PA

Each point along the view ray brings its own contribution to the final colour of the pixel that we are drawing. That contribution is, mathematically speaking, the quantity inside the summation: Like we did in the previous paragraph, let’s try to simplify it. We can expand the expression above even further, by replacing with its actual definition: The product of the transmittance over and becomes:   The combined transmittance is modelled as an exponential decay with which coefficient is the sum of the optical depths over the path travelled by the light ( and ), multiplied by the scattering coefficient at sea level ( with ).

The first quantity that we start calculating is the optical depth for the segment , which goes from the point of entry through the atmosphere, up to the point we are currently sampling in the for loop. Let’s recall the definition of optical depth: If one had to implement this naively, we would create a function called opticalDepth that samples points between and in a loop. This is possible, but very inefficient. As a matter of fact, is the optical depth of the segment that we are already analysing the outermost for loop. We can save a lot of calculations if we calculate the optical depth the current segmenet centred at ( opticalDepthSegment), and keep accumulating it in the for loop ( opticalDepthPA).

Light Sampling

If we look back at the expression for the light contribution of , we see that the only quantity needed is the optical depth of the segment , We will move the code that calculates the optical depth of the segment into a function called lightSampling. The name comes from the light ray, which is the segment that starts at and is directed towards the sun. We have called the point where it exits the atmosphere is .

The  lightSampling function, however, will not just calculate the optical depth of . So far we have only considered the contribution of the atmosphere, ignoring the role of the actual planet. Our equations do not account for the possibility that the light ray that goes from towards the sun might hit the planet. If this happens, all calculations done so far must be discarded, as no light would actually reach the camera. In the diagram above, it’s easy to see that the light contribution of should be ignored, since no sun light is reaching . While looping through the points in between and , the  lightSampling function will also check if the planet has been hit. This can be done by checking if the altitude of a point is negative.

The function above first calculates the point using rayInstersect. It then divides the segment into _LightSamples segments of length ds. The calculation for the optical depth is the same used in the outermost loop.

The function returns false if the planet has been hit. We can use this to update the missing code the outermost loop, replacing the  ....

Now that we have taken into account all the elements, our shader is complete.

Coming Next…

This post (finally!) completes the volumetric shader that simulates atmospheric scattering. So far, we have only taken into account the contribution of the Rayleigh scattering. There are many optical phenomena that cannot be explained with Rayleigh scattering alone. The next post will introduce a second type of scattering, which is known as Mie scattering.

You can find all the post in this series here:

You can refer to the Atmospheric Scattering Cheatsheet for a complete reference of all the equations used.

You can download all the assets necessary to reproduce the volumetric atmospheric scattering presented in this tutorial.

 Feature Standard Premium Volumetric Shader ✅ ✅ Clouds Support ❌ ✅ Day & Night Cycle ❌ ✅ Earth 8K ✅ ✅ Mars 8K ❌ ✅ Venus 8K ❌ ✅ Neptune 2K ❌ ✅ Postprocessing ❌ ✅ Download Standard Premium

A new tutorial is released every week.

💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.  1. Steffen

How does this shader look like when inside the atmosphere?

• Alan Zucconi

Hey!
This particular version doesn’t really work inside the atmosphere.
But it can be changed very easily. The first step is to have a material that does not cull back faces.

2. Carl Davidson

Hey Alan, wanted to thank you for the great tutorial. I was able to create my own atmospheric renderer thanks in large part to this series (you can see it in action here: http://davidson16807.github.io/tectonics.js/).

I wanted to point out something I found: you can dramatically improve performance by using a closed form approximation for the “lightSampling()” function in your tutorial. I wrote up a blog post about this, which I hope will make sense: http://davidson16807.github.io/tectonics.js//2019/03/24/fast-atmospheric-scattering.html

• Alan Zucconi

Thank you, that is really interesting!
I hope other people will be able to use your implementation!

Webmentions

• Learning Shaders - Alan Zucconi March 25, 2019

Thank you, that is really interesting!
I hope other people will be able to use your implementation!

• Atmospheric Scattering Shader in Unity via @AlanZucconi | hanecci's blog : はねっちブログ March 25, 2019

Thank you, that is really interesting!
I hope other people will be able to use your implementation!