Path: blob/main/crates/bevy_pbr/src/volumetric_fog/mod.rs
9472 views
//! Volumetric fog and volumetric lighting, also known as light shafts or god1//! rays.2//!3//! This module implements a more physically-accurate, but slower, form of fog4//! than the [`crate::fog`] module does. Notably, this *volumetric fog* allows5//! for light beams from directional lights to shine through, creating what is6//! known as *light shafts* or *god rays*.7//!8//! To add volumetric fog to a scene, add [`bevy_light::VolumetricFog`] to the9//! camera, and add [`bevy_light::VolumetricLight`] to directional lights that you wish to10//! be volumetric. [`bevy_light::VolumetricFog`] feature numerous settings that11//! allow you to define the accuracy of the simulation, as well as the look of12//! the fog. Currently, only interaction with directional lights that have13//! shadow maps is supported. Note that the overhead of the effect scales14//! directly with the number of directional lights in use, so apply15//! [`bevy_light::VolumetricLight`] sparingly for the best results.16//!17//! The overall algorithm, which is implemented as a postprocessing effect, is a18//! combination of the techniques described in [Scratchapixel] and [this blog19//! post]. It uses raymarching in screen space, transformed into shadow map20//! space for sampling and combined with physically-based modeling of absorption21//! and scattering. Bevy employs the widely-used [Henyey-Greenstein phase22//! function] to model asymmetry; this essentially allows light shafts to fade23//! into and out of existence as the user views them.24//!25//! [Scratchapixel]: https://www.scratchapixel.com/lessons/3d-basic-rendering/volume-rendering-for-developers/intro-volume-rendering.html26//!27//! [this blog post]: https://www.alexandre-pestana.com/volumetric-lights/28//!29//! [Henyey-Greenstein phase function]: https://www.pbr-book.org/4ed/Volume_Scattering/Phase_Functions#TheHenyeyndashGreensteinPhaseFunction3031use bevy_app::{App, Plugin};32use bevy_asset::{embedded_asset, Assets, Handle};33use bevy_core_pipeline::{34core_3d::prepare_core_3d_depth_textures,35schedule::{Core3d, Core3dSystems},36};37use bevy_ecs::{resource::Resource, schedule::IntoScheduleConfigs as _};38use bevy_light::FogVolume;39use bevy_math::{40primitives::{Cuboid, Plane3d},41Vec2, Vec3,42};43use bevy_mesh::{Mesh, Meshable};44use bevy_render::{45render_resource::SpecializedRenderPipelines,46sync_component::{SyncComponent, SyncComponentPlugin},47ExtractSchedule, Render, RenderApp, RenderStartup, RenderSystems,48};49use render::{volumetric_fog, VolumetricFogPipeline, VolumetricFogUniformBuffer};5051use crate::volumetric_fog::render::init_volumetric_fog_pipeline;5253pub mod render;5455/// A plugin that implements volumetric fog.56pub struct VolumetricFogPlugin;5758#[derive(Resource)]59pub struct FogAssets {60plane_mesh: Handle<Mesh>,61cube_mesh: Handle<Mesh>,62}6364impl Plugin for VolumetricFogPlugin {65fn build(&self, app: &mut App) {66embedded_asset!(app, "volumetric_fog.wgsl");6768let mut meshes = app.world_mut().resource_mut::<Assets<Mesh>>();69let plane_mesh = meshes.add(Plane3d::new(Vec3::Z, Vec2::ONE).mesh());70let cube_mesh = meshes.add(Cuboid::new(1.0, 1.0, 1.0).mesh());7172app.add_plugins(SyncComponentPlugin::<FogVolume, Self>::default());7374let Some(render_app) = app.get_sub_app_mut(RenderApp) else {75return;76};7778render_app79.insert_resource(FogAssets {80plane_mesh,81cube_mesh,82})83.init_resource::<SpecializedRenderPipelines<VolumetricFogPipeline>>()84.init_resource::<VolumetricFogUniformBuffer>()85.add_systems(RenderStartup, init_volumetric_fog_pipeline)86.add_systems(ExtractSchedule, render::extract_volumetric_fog)87.add_systems(88Render,89(90render::prepare_volumetric_fog_pipelines.in_set(RenderSystems::Prepare),91render::prepare_volumetric_fog_uniforms.in_set(RenderSystems::Prepare),92render::prepare_view_depth_textures_for_volumetric_fog93.in_set(RenderSystems::Prepare)94.before(prepare_core_3d_depth_textures),95),96)97.add_systems(98Core3d,99volumetric_fog100.after(Core3dSystems::MainPass)101.before(Core3dSystems::PostProcess),102);103}104}105106impl SyncComponent<VolumetricFogPlugin> for FogVolume {107type Out = Self;108}109110111