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 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 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
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 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
121fn 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}