Skip to main content

bevy_pbr/volumetric_fog/
mod.rs

1//! Volumetric fog and volumetric lighting, also known as light shafts or god
2//! rays.
3//!
4//! This module implements a more physically-accurate, but slower, form of fog
5//! than the [`crate::fog`] module does. Notably, this *volumetric fog* allows
6//! for light beams from directional lights to shine through, creating what is
7//! known as *light shafts* or *god rays*.
8//!
9//! To add volumetric fog to a scene, add [`bevy_light::VolumetricFog`] to the
10//! camera, and add [`bevy_light::VolumetricLight`] to directional lights that you wish to
11//! be volumetric. [`bevy_light::VolumetricFog`] feature numerous settings that
12//! allow you to define the accuracy of the simulation, as well as the look of
13//! the fog. Currently, only interaction with directional lights that have
14//! shadow maps is supported. Note that the overhead of the effect scales
15//! directly with the number of directional lights in use, so apply
16//! [`bevy_light::VolumetricLight`] sparingly for the best results.
17//!
18//! The overall algorithm, which is implemented as a postprocessing effect, is a
19//! combination of the techniques described in [Scratchapixel] and [this blog
20//! post]. It uses raymarching in screen space, transformed into shadow map
21//! space for sampling and combined with physically-based modeling of absorption
22//! and scattering. Bevy employs the widely-used [Henyey-Greenstein phase
23//! function] to model asymmetry; this essentially allows light shafts to fade
24//! into and out of existence as the user views them.
25//!
26//! [Scratchapixel]: https://www.scratchapixel.com/lessons/3d-basic-rendering/volume-rendering-for-developers/intro-volume-rendering.html
27//!
28//! [this blog post]: https://www.alexandre-pestana.com/volumetric-lights/
29//!
30//! [Henyey-Greenstein phase function]: https://www.pbr-book.org/4ed/Volume_Scattering/Phase_Functions#TheHenyeyndashGreensteinPhaseFunction
31
32use bevy_app::{App, Plugin};
33use bevy_asset::{embedded_asset, Assets, Handle};
34use bevy_core_pipeline::{
35    core_3d::prepare_core_3d_depth_textures,
36    schedule::{Core3d, Core3dSystems},
37};
38use bevy_ecs::{resource::Resource, schedule::IntoScheduleConfigs as _};
39use bevy_light::FogVolume;
40use bevy_math::{Vec2, Vec3};
41use bevy_mesh::{Mesh, Meshable};
42use bevy_render::{
43    render_resource::SpecializedRenderPipelines,
44    sync_component::{SyncComponent, SyncComponentPlugin},
45    ExtractSchedule, GpuResourceAppExt, Render, RenderApp, RenderStartup, RenderSystems,
46};
47use bevy_shape::{Cuboid, Plane3d};
48use render::{volumetric_fog, VolumetricFogPipeline, VolumetricFogUniformBuffer};
49
50use crate::{volumetric_fog::render::init_volumetric_fog_pipeline, MeshPipelineSystems};
51
52pub mod render;
53
54/// A plugin that implements volumetric fog.
55pub struct VolumetricFogPlugin;
56
57#[derive(Resource)]
58pub struct FogAssets {
59    plane_mesh: Handle<Mesh>,
60    cube_mesh: Handle<Mesh>,
61}
62
63impl Plugin for VolumetricFogPlugin {
64    fn build(&self, app: &mut App) {
65        embedded_asset!(app, "volumetric_fog.wesl");
66
67        let mut meshes = app.world_mut().resource_mut::<Assets<Mesh>>();
68        let plane_mesh = meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE).mesh());
69        let cube_mesh = meshes.add(Cuboid::new(1.0, 1.0, 1.0).mesh());
70
71        app.add_plugins(SyncComponentPlugin::<FogVolume, Self>::default());
72
73        let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
74            return;
75        };
76
77        render_app
78            .insert_resource(FogAssets {
79                plane_mesh,
80                cube_mesh,
81            })
82            .init_gpu_resource::<SpecializedRenderPipelines<VolumetricFogPipeline>>()
83            .init_gpu_resource::<VolumetricFogUniformBuffer>()
84            .add_systems(
85                RenderStartup,
86                init_volumetric_fog_pipeline.after(MeshPipelineSystems),
87            )
88            .add_systems(ExtractSchedule, render::extract_volumetric_fog)
89            .add_systems(
90                Render,
91                (
92                    render::prepare_volumetric_fog_pipelines.in_set(RenderSystems::Prepare),
93                    render::prepare_volumetric_fog_uniforms.in_set(RenderSystems::Prepare),
94                    render::prepare_view_depth_textures_for_volumetric_fog
95                        .in_set(RenderSystems::Prepare)
96                        .before(prepare_core_3d_depth_textures),
97                ),
98            )
99            .add_systems(
100                Core3d,
101                volumetric_fog
102                    .after(Core3dSystems::MainPass)
103                    .before(Core3dSystems::EarlyPostProcess),
104            );
105    }
106}
107
108impl SyncComponent<RenderApp, VolumetricFogPlugin> for FogVolume {
109    type Target = Self;
110}