• Create Account

## Help, understanding Atmospheric and Volumetric Light Scattering

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1Tispe  Members

1421
Like
1Likes
Like

Posted 02 September 2013 - 01:19 AM

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.

### #2Bacterius  Members

13102
Like
0Likes
Like

Posted 02 September 2013 - 01:50 AM

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?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #3Krypt0n  Members

4529
Like
0Likes
Like

Posted 02 September 2013 - 02:04 AM

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.

### #4Tispe  Members

1421
Like
1Likes
Like

Posted 02 September 2013 - 02:19 AM

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?

### #5Krypt0n  Members

4529
Like
0Likes
Like

Posted 02 September 2013 - 06:09 AM

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.

[quote]

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.

### #6Styves  Members

1630
Like
0Likes
Like

Posted 02 September 2013 - 06:18 AM

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, 02 September 2013 - 06:19 AM.

### #7Tispe  Members

1421
Like
0Likes
Like

Posted 02 September 2013 - 03:03 PM

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.

### #8Styves  Members

1630
Like
0Likes
Like

Posted 02 September 2013 - 04:20 PM

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:

• In the vertex shader, compute the ray direction (VertexPos - CameraPos) and output to the 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.
• 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, 02 September 2013 - 04:21 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.