Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/3d/scrolling_fog.rs
6592 views
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
shadows_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.load_with_settings("volumes/fog_noise.ktx2", |settings: &mut _| {
93
*settings = ImageLoaderSettings {
94
sampler: ImageSampler::Descriptor(ImageSamplerDescriptor {
95
address_mode_u: ImageAddressMode::Repeat,
96
address_mode_v: ImageAddressMode::Repeat,
97
address_mode_w: ImageAddressMode::Repeat,
98
mag_filter: ImageFilterMode::Linear,
99
min_filter: ImageFilterMode::Linear,
100
mipmap_filter: ImageFilterMode::Linear,
101
..default()
102
}),
103
..default()
104
}
105
});
106
107
// Spawn a FogVolume and use the repeating noise texture as its density texture.
108
commands.spawn((
109
Transform::from_xyz(0.0, 32.0, 0.0).with_scale(Vec3::splat(64.0)),
110
FogVolume {
111
density_texture: Some(noise_texture),
112
density_factor: 0.05,
113
..default()
114
},
115
));
116
}
117
118
/// Moves fog density texture offset every frame.
119
fn scroll_fog(time: Res<Time>, mut query: Query<&mut FogVolume>) {
120
for mut fog_volume in query.iter_mut() {
121
fog_volume.density_texture_offset += Vec3::new(0.0, 0.0, 0.04) * time.delta_secs();
122
}
123
}
124
125