Jump to content
• Advertisement

# Help, understanding Atmospheric and Volumetric Light Scattering

This topic is 2149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello, I have seen demos that uses Atmospheric and Volumetric Light Scattering and I wish to implement it on my own. Hopefully SM3.0 will suffice.

But, after reading the nvidia GPU Gems articles I am still in the dark on how this actually works. The Accurate Atmospheric Scattering article by Sean O'Neil shows two pictures of the end result, a drawing of the atmosphere with a line through it. Follwing this is some history on the subject, some formulas and other jargon that does not really add any understanding to how the reader can use it.

Is there a better article that describes exactly how this works? The same applies to the Volumetric Light Scattering as a Post-Process article by Kenny Mitchell. This article is better, but not enough for me to grasp the subject. I have read plenty of articles and tutorials, but these are in a class of their own. I can't recall if I have ever read such difficult articles before.

Any references is greatly appreciated.

#### Share this post

##### Share on other sites
Advertisement

Are you asking about the implementation, or the underlying physics? Do you mean standard atmospheric scattering such as Rayleigh scattering and so on, or more complex effects, or about volumetric scattering in general?

#### Share this post

##### Share on other sites

Atmospheric Scattering

- create a ray for every pixel of your framebuffer, with your eye being the source and the rayDirection*DepthOfPixel the orientation

- step along the ray (aka divide the ray orientation by 32 and step 32times)

- at every step

- create a ray from that particular position towards the sun (or any other light source you want) by taking that point as origin and the sun direction as orientation

- step along this ray

- at every step, check if you hit something (e.g. inside the earth sphere, inside a mountain, inside of a cloud)

- add some intensity to a temporal variable

- weight that temporal variable by the amount of steps you make (aka temp/=32;) and add it to your scattering color.

that's the basic of how it works. for the intensity value you can choose some small value to start with. to improve, you can look up in those papers for 'better approximations', those depend on the height of your sample points, on the angle between your view ray and the sun etc. you can probably go infinity detailed about it and make it take days per picture :)

for speed up you can pre-calculate the inner loop into a 2d look up table, based on the height and the angle between the view ray and the direction towards the sun, effectively reducing it from O(n*n) to O(n), while n being the amount of steps you wanna sample.

#### Share this post

##### Share on other sites

Are you asking about the implementation, or the underlying physics? Do you mean standard atmospheric scattering such as Rayleigh scattering and so on, or more complex effects, or about volumetric scattering in general?

I understand the physics, light comes in and scatters until it scatters away or towards the viewer. But how this is implemented in shaders is what I want to know.

create a ray for every pixel of your framebuffer, with your eye being the source and the rayDirection*DepthOfPixel the orientation

Does the vertex shader supply the rayDirection or is it calculated in the pixel shader?

at every step, check if you hit something (e.g. inside the earth sphere, inside a mountain, inside of a cloud)

This sounds hard, how can the pixel shader know if a ray it creates is at any point intersecting an object?

#### Share this post

##### Share on other sites

create a ray for every pixel of your framebuffer, with your eye being the source and the rayDirection*DepthOfPixel the orientation

Does the vertex shader supply the rayDirection or is it calculated in the pixel shader?

[/quote]

the vertexshader can provide the direction of every corner of the screen that gets interpolated and passed to the pixelshader.

at every step, check if you hit something (e.g. inside the earth sphere, inside a mountain, inside of a cloud)

This sounds hard, how can the pixel shader know if a ray it creates is at any point intersecting an object?

that's up to your implementation, in the simplest case you don't do any checks.

a little improvement is to check the intersection with the earth-sphere, which is especially important during dust and dawn. (that's just a ray-sphere intersection test, simple, but doing it 32x32 times is not cheap, of course).

next step might be a shadowmap that just includes clouds, that way you'll get those god-ray effects.

it's all approximation, simulating light participating medias gets insanely complex the more accurate you want to get it and while the improvement gets more and more subtle, it's still noticeable if you compare 1:1, but for games, it usually is a very very rough approximation. e.g. the god-ray effects in alan wake or crysis are just screenspace effects, sampling from the screenspace position of the light along the projected 2d rays, checking the depth buffer or some light mask to bleed the light source color along those rays.

#### Share this post

##### Share on other sites

For intersection along sun-ray: replace the second ray that's cast towards the sun with a shadow map depth test. Essentially the same thing but you can drop the second march (faster) and simply sample the shadow map. If you've already got sun shadows then you're golden.

The shader code supplied should be more than enough to work with IMO. The tricky bit is some of the inputs, there's some weird scale setting that's very very touchy.

Edited by Styves

#### Share this post

##### Share on other sites

I have no idea what people are talking about here. Krypt0n was almost there but I didn't manage to get the whole picture.

So, we have a shadow map, the using depths, ray from pixel to sun etc, we can in the pixel shader get scattering?

Please reiterate the steps, but make it believable and understandable.

#### Share this post

##### Share on other sites

It would help if we knew what you were having trouble with. The NVIDIA page has source code and all, so I'm a bit baffled by what's confusing you.

Here's the idea though:

Vertex Shader:

• In the vertex shader, compute the ray direction (VertexPos - CameraPos) and output to the pixel shader.

Pixel Shader:

• Setup the ray. In the NVIDIA sample, they're using sphere intersections to determine the length of the ray to get the best result from the lower sample count. In the "from space" version, it's from the front of the atmosphere to the back. In the "from earth" version, it's from the camera to the atmosphere back.
• March along your ray direction and compute the scattering at that point (essentially it's just an RGB attenuation function that takes depth and height as inputs). You could use anything here for different effects: reading a texture, adding color, etc.
• At each ray step, convert your ray position to shadow space (standard shadow map "mul(matrix, pos)"). Read the shadow map depth and compare it to the ray's shadow map depth (regular shadow depth test). Multiply this result onto your attenuation result.
• The loop adds the computed color to the final result and is rendered to the scene.

That's basically it. If you want to get the basics working, you can replace the atmospheric scattering with a basic depth-based color (use Depth*Color).

Edited by Styves

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Now

• 13
• 18
• 29
• 11
• 27
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!