scrolling_fog/
scrolling_fog.rs1use 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
24fn 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
40fn setup(
42 mut commands: Commands,
43 mut meshes: ResMut<Assets<Mesh>>,
44 mut materials: ResMut<Assets<StandardMaterial>>,
45 assets: Res<AssetServer>,
46) {
47 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 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 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 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 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 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
118fn 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}