docs/bevy/examples/3d/scrolling_fog.rs 4.3 K · 126 lines · raw · history

1 //! Showcases a `FogVolume`'s density texture being scrolled over time to create
2 //! the effect of fog moving in the wind.
3 //!
4 //! The density texture is a repeating 3d noise texture and the `density_texture_offset`
5 //! is moved every frame to achieve this.
6 //!
7 //! The example also utilizes the jitter option of `VolumetricFog` in tandem
8 //! with temporal anti-aliasing to improve the visual quality of the effect.
9 //!
10 //! The camera is looking at a pillar with the sun peaking behind it. The light
11 //! interactions change based on the density of the fog.
12
13 use bevy::{
14 anti_alias::taa::TemporalAntiAliasing,
15 image::{
16 ImageAddressMode, ImageFilterMode, ImageLoaderSettings, ImageSampler,
17 ImageSamplerDescriptor,
18 },
19 light::{DirectionalLightShadowMap, FogVolume, VolumetricFog, VolumetricLight},
20 post_process::bloom::Bloom,
21 prelude::*,
22 };
23
24 /// Initializes the example.
25 fn main() {
26 App::new()
27 .add_plugins(DefaultPlugins.set(WindowPlugin {
28 primary_window: Some(Window {
29 title: "Bevy Scrolling Fog".into(),
30 ..default()
31 }),
32 ..default()
33 }))
34 .insert_resource(DirectionalLightShadowMap { size: 4096 })
35 .add_systems(Startup, setup)
36 .add_systems(Update, scroll_fog)
37 .run();
38 }
39
40 /// Spawns all entities into the scene.
41 fn setup(
42 mut commands: Commands,
43 mut meshes: ResMut<Assets<Mesh>>,
44 mut materials: ResMut<Assets<StandardMaterial>>,
45 assets: Res<AssetServer>,
46 ) {
47 // Spawn camera with temporal anti-aliasing and a VolumetricFog configuration.
48 commands.spawn((
49 Camera3d::default(),
50 Transform::from_xyz(0.0, 2.0, 0.0).looking_at(Vec3::new(-5.0, 3.5, -6.0), Vec3::Y),
51 Msaa::Off,
52 TemporalAntiAliasing::default(),
53 Bloom::default(),
54 VolumetricFog {
55 ambient_intensity: 0.0,
56 jitter: 0.5,
57 ..default()
58 },
59 ));
60
61 // Spawn a directional light shining at the camera with the VolumetricLight component.
62 commands.spawn((
63 DirectionalLight {
64 shadow_maps_enabled: true,
65 ..default()
66 },
67 Transform::from_xyz(-5.0, 5.0, -7.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
68 VolumetricLight,
69 ));
70
71 // Spawn ground mesh.
72 commands.spawn((
73 Mesh3d(meshes.add(Cuboid::new(64.0, 1.0, 64.0))),
74 MeshMaterial3d(materials.add(StandardMaterial {
75 base_color: Color::BLACK,
76 perceptual_roughness: 1.0,
77 ..default()
78 })),
79 Transform::from_xyz(0.0, -0.5, 0.0),
80 ));
81
82 // Spawn pillar standing between the camera and the sun.
83 commands.spawn((
84 Mesh3d(meshes.add(Cuboid::new(2.0, 9.0, 2.0))),
85 MeshMaterial3d(materials.add(Color::BLACK)),
86 Transform::from_xyz(-10.0, 4.5, -11.0),
87 ));
88
89 // Load a repeating 3d noise texture. Make sure to set ImageAddressMode to Repeat
90 // so that the texture wraps around as the density texture offset is moved along.
91 // Also set ImageFilterMode to Linear so that the fog isn't pixelated.
92 let noise_texture = assets
93 .load_builder()
94 .with_settings(|settings: &mut _| {
95 *settings = ImageLoaderSettings {
96 sampler: ImageSampler::Descriptor(ImageSamplerDescriptor {
97 address_mode_u: ImageAddressMode::Repeat,
98 address_mode_v: ImageAddressMode::Repeat,
99 address_mode_w: ImageAddressMode::Repeat,
100 mag_filter: ImageFilterMode::Linear,
101 min_filter: ImageFilterMode::Linear,
102 mipmap_filter: ImageFilterMode::Linear,
103 ..default()
104 }),
105 ..default()
106 }
107 })
108 .load("volumes/fog_noise.ktx2");
109
110 // Spawn a FogVolume and use the repeating noise texture as its density texture.
111 commands.spawn((
112 Transform::from_xyz(0.0, 32.0, 0.0).with_scale(Vec3::splat(64.0)),
113 FogVolume {
114 density_texture: Some(noise_texture),
115 density_factor: 0.05,
116 ..default()
117 },
118 ));
119 }
120
121 /// Moves fog density texture offset every frame.
122 fn scroll_fog(time: Res<Time>, mut query: Query<&mut FogVolume>) {
123 for mut fog_volume in query.iter_mut() {
124 fog_volume.density_texture_offset += Vec3::new(0.0, 0.0, 0.04) * time.delta_secs();
125 }
126 }