bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Implement volumetric fog and volumetric lighting, also known as light shafts or god rays.

Open pcwalton opened this issue 1 year ago • 6 comments

This commit implements a more physically-accurate, but slower, form of fog than the bevy_pbr::fog module does. Notably, this volumetric fog allows for light beams from directional lights to shine through, creating what is known as light shafts or god rays.

To add volumetric fog to a scene, add VolumetricFogSettings to the camera, and add VolumetricLight to directional lights that you wish to be volumetric. VolumetricFogSettings has numerous settings that allow you to define the accuracy of the simulation, as well as the look of the fog. Currently, only interaction with directional lights that have shadow maps is supported. Note that the overhead of the effect scales directly with the number of directional lights in use, so apply VolumetricLight sparingly for the best results.

The overall algorithm, which is implemented as a postprocessing effect, is a combination of the techniques described in Scratchapixel and this blog post. It uses raymarching in screen space, transformed into shadow map space for sampling and combined with physically-based modeling of absorption and scattering. Bevy employs the widely-used Henyey-Greenstein phase function to model asymmetry; this essentially allows light shafts to fade into and out of existence as the user views them.

Volumetric rendering is a huge subject, and I deliberately kept the scope of this commit small. Possible follow-ups include:

  1. Raymarching at a lower resolution.

  2. A post-processing blur (especially useful when combined with (1)).

  3. Supporting point lights and spot lights.

  4. Supporting lights with no shadow maps.

  5. Supporting irradiance volumes and reflection probes.

  6. Voxel components that reuse the volumetric fog code to create voxel shapes.

  7. Horizon: Zero Dawn-style clouds.

These are all useful, but out of scope of this patch for now, to keep things tidy and easy to review.

A new example, volumetric_fog, has been added to demonstrate the effect.

Changelog

Added

  • A new component, VolumetricFog, is available, to allow for a more physically-accurate, but more resource-intensive, form of fog.

  • A new component, VolumetricLight, can be placed on directional lights to make them interact with VolumetricFog. Notably, this allows such lights to emit light shafts/god rays.

Screenshot 2024-04-21 162808

Screenshot 2024-04-21 132005

pcwalton avatar Apr 21 '24 23:04 pcwalton

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar Apr 21 '24 23:04 github-actions[bot]

I think the CI failure is spurious.

pcwalton avatar May 02 '24 09:05 pcwalton

I think the CI failure is spurious.

Yup, feel free to rebase / merge from main to get the check started again. :)

BD103 avatar May 02 '24 10:05 BD103

Closing as I am burned out and have no motivation to get this landed anymore.

pcwalton avatar May 03 '24 07:05 pcwalton

Note to maintainers. I extracted the mesh view layouts change to it's own PR since those changes make sense outside of this PR. #13266

That PR should be merged first, but it's not a blocker if we don't do that.

IceSentry avatar May 06 '24 20:05 IceSentry

@pcwalton since https://github.com/bevyengine/bevy/pull/13266 was merged this needs a merge. There shouldn't be conflicts, at least nothing complicated, but you'll be able to remove the .view_layouts.get_view_layout that you added in a few places.

IceSentry avatar May 08 '24 19:05 IceSentry

Once merge conflicts are resolved I'll merge this in :) Do let us know if you'd like a hand.

alice-i-cecile avatar May 12 '24 19:05 alice-i-cecile

I'm not sure I did it correctly, but I merged main and fixed the confict in this PR to your PR https://github.com/pcwalton/bevy/pull/5

IceSentry avatar May 12 '24 19:05 IceSentry

This should be ready now.

pcwalton avatar May 16 '24 16:05 pcwalton

Thank you to everyone involved with the authoring or reviewing of this PR! This work is relatively important and needs release notes! Head over to https://github.com/bevyengine/bevy-website/issues/1318 if you'd like to help out.

alice-i-cecile avatar Jun 03 '24 20:06 alice-i-cecile