God Ray
2019-12-13
Light scattering
This effect is showing the light scattering. It’s implemented by Light Volume, not ray matching.
Light goes through the window.
- Render scene object. I create an object like a window, then use a simple pass to render it on a buffer and also get the camera depth buffer.
- Render light depth buffer. Just like a shadow map generation, I use a pass to generate a depth buffer from light.
- Create a special mesh than represent the light volume, then render it. This mesh is generated from a bottom grid mesh and surrounding meshes. The grid size depends on the size of depth buffer of light. Each vertex will invoke a vertex shader, in the vertex shader we can recalculate the vertex position of the light volume from light depth buffer. For cutting out the deviation of depth buffer, I move the light volume vertex closer to light source slightly. In fragment shader, I used the camera depth buffer to clamp the depth of current light volume pixel in camera space, because if the depth of the pixel is greater than current scene pixel, that means the light don’t contribute to the scene pixel. Then check the side of the pixel to determine the pixel is an unlit to lit pixel or lit to unlit pixel. Use the equation to add the contribution to the final color.