forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volumetric_fog.rs
285 lines (262 loc) · 8.76 KB
/
volumetric_fog.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Demonstrates volumetric fog and lighting (light shafts or god rays).
use bevy::{
color::palettes::css::RED,
core_pipeline::{bloom::Bloom, tonemapping::Tonemapping, Skybox},
math::vec3,
pbr::{FogVolumeBundle, VolumetricFog, VolumetricLight},
prelude::*,
};
const DIRECTIONAL_LIGHT_MOVEMENT_SPEED: f32 = 0.02;
/// The current settings that the user has chosen.
#[derive(Resource)]
struct AppSettings {
/// Whether volumetric spot light is on.
volumetric_spotlight: bool,
/// Whether volumetric point light is on.
volumetric_pointlight: bool,
}
impl Default for AppSettings {
fn default() -> Self {
Self {
volumetric_spotlight: true,
volumetric_pointlight: true,
}
}
}
// Define a struct to store parameters for the point light's movement.
#[derive(Component)]
struct MoveBackAndForthHorizontally {
min_x: f32,
max_x: f32,
speed: f32,
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::Srgba(Srgba {
red: 0.02,
green: 0.02,
blue: 0.02,
alpha: 1.0,
})))
.insert_resource(AmbientLight::NONE)
.init_resource::<AppSettings>()
.add_systems(Startup, setup)
.add_systems(Update, tweak_scene)
.add_systems(Update, (move_directional_light, move_point_light))
.add_systems(Update, adjust_app_settings)
.run();
}
/// Initializes the scene.
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, app_settings: Res<AppSettings>) {
// Spawn the glTF scene.
commands.spawn(SceneBundle {
scene: asset_server.load(
GltfAssetLabel::Scene(0)
.from_asset("models/VolumetricFogExample/VolumetricFogExample.glb"),
),
..default()
});
// Spawn the camera.
commands
.spawn(Camera3dBundle {
transform: Transform::from_xyz(-1.7, 1.5, 4.5)
.looking_at(vec3(-1.5, 1.7, 3.5), Vec3::Y),
camera: Camera {
hdr: true,
..default()
},
..default()
})
.insert(Tonemapping::TonyMcMapface)
.insert(Bloom::default())
.insert(Skybox {
image: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
brightness: 1000.0,
..default()
})
.insert(VolumetricFog {
// This value is explicitly set to 0 since we have no environment map light
ambient_intensity: 0.0,
..default()
});
// Add the point light
commands
.spawn((
PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
range: 150.0,
color: RED.into(),
intensity: 1000.0,
..default()
},
transform: Transform::from_xyz(-0.4, 1.9, 1.0),
..default()
},
MoveBackAndForthHorizontally {
min_x: -1.93,
max_x: -0.4,
speed: -0.2,
},
))
.insert(VolumetricLight);
// Add the spot light
commands
.spawn(SpotLightBundle {
transform: Transform::from_xyz(-1.8, 3.9, -2.7).looking_at(Vec3::ZERO, Vec3::Y),
spot_light: SpotLight {
intensity: 5000.0, // lumens
color: Color::WHITE,
shadows_enabled: true,
inner_angle: 0.76,
outer_angle: 0.94,
..default()
},
..default()
})
.insert(VolumetricLight);
// Add the fog volume.
commands.spawn(FogVolumeBundle {
transform: Transform::from_scale(Vec3::splat(35.0)),
..default()
});
// Add the help text.
commands.spawn(
TextBundle {
text: create_text(&app_settings),
..default()
}
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
}),
);
}
fn create_text(app_settings: &AppSettings) -> Text {
Text::from_section(
format!(
"{}\n{}\n{}",
"Press WASD or the arrow keys to change the direction of the directional light",
if app_settings.volumetric_pointlight {
"Press P to turn volumetric point light off"
} else {
"Press P to turn volumetric point light on"
},
if app_settings.volumetric_spotlight {
"Press L to turn volumetric spot light off"
} else {
"Press L to turn volumetric spot light on"
}
),
TextStyle::default(),
)
}
/// A system that makes directional lights in the glTF scene into volumetric
/// lights with shadows.
fn tweak_scene(
mut commands: Commands,
mut lights: Query<(Entity, &mut DirectionalLight), Changed<DirectionalLight>>,
) {
for (light, mut directional_light) in lights.iter_mut() {
// Shadows are needed for volumetric lights to work.
directional_light.shadows_enabled = true;
commands.entity(light).insert(VolumetricLight);
}
}
/// Processes user requests to move the directional light.
fn move_directional_light(
input: Res<ButtonInput<KeyCode>>,
mut directional_lights: Query<&mut Transform, With<DirectionalLight>>,
) {
let mut delta_theta = Vec2::ZERO;
if input.pressed(KeyCode::KeyW) || input.pressed(KeyCode::ArrowUp) {
delta_theta.y += DIRECTIONAL_LIGHT_MOVEMENT_SPEED;
}
if input.pressed(KeyCode::KeyS) || input.pressed(KeyCode::ArrowDown) {
delta_theta.y -= DIRECTIONAL_LIGHT_MOVEMENT_SPEED;
}
if input.pressed(KeyCode::KeyA) || input.pressed(KeyCode::ArrowLeft) {
delta_theta.x += DIRECTIONAL_LIGHT_MOVEMENT_SPEED;
}
if input.pressed(KeyCode::KeyD) || input.pressed(KeyCode::ArrowRight) {
delta_theta.x -= DIRECTIONAL_LIGHT_MOVEMENT_SPEED;
}
if delta_theta == Vec2::ZERO {
return;
}
let delta_quat = Quat::from_euler(EulerRot::XZY, delta_theta.y, 0.0, delta_theta.x);
for mut transform in directional_lights.iter_mut() {
transform.rotate(delta_quat);
}
}
// Toggle point light movement between left and right.
fn move_point_light(
timer: Res<Time>,
mut objects: Query<(&mut Transform, &mut MoveBackAndForthHorizontally)>,
) {
for (mut transform, mut move_data) in objects.iter_mut() {
let mut translation = transform.translation;
let mut need_toggle = false;
translation.x += move_data.speed * timer.delta_seconds();
if translation.x > move_data.max_x {
translation.x = move_data.max_x;
need_toggle = true;
} else if translation.x < move_data.min_x {
translation.x = move_data.min_x;
need_toggle = true;
}
if need_toggle {
move_data.speed = -move_data.speed;
}
transform.translation = translation;
}
}
// Adjusts app settings per user input.
fn adjust_app_settings(
mut commands: Commands,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut app_settings: ResMut<AppSettings>,
mut point_lights: Query<Entity, With<PointLight>>,
mut spot_lights: Query<Entity, With<SpotLight>>,
mut text: Query<&mut Text>,
) {
// If there are no changes, we're going to bail for efficiency. Record that
// here.
let mut any_changes = false;
// If the user pressed P, toggle volumetric state of the point light.
if keyboard_input.just_pressed(KeyCode::KeyP) {
app_settings.volumetric_pointlight = !app_settings.volumetric_pointlight;
any_changes = true;
}
// If the user pressed L, toggle volumetric state of the spot light.
if keyboard_input.just_pressed(KeyCode::KeyL) {
app_settings.volumetric_spotlight = !app_settings.volumetric_spotlight;
any_changes = true;
}
// If there were no changes, bail out.
if !any_changes {
return;
}
// Update volumetric settings.
for point_light in point_lights.iter_mut() {
if app_settings.volumetric_pointlight {
commands.entity(point_light).insert(VolumetricLight);
} else {
commands.entity(point_light).remove::<VolumetricLight>();
}
}
for spot_light in spot_lights.iter_mut() {
if app_settings.volumetric_spotlight {
commands.entity(spot_light).insert(VolumetricLight);
} else {
commands.entity(spot_light).remove::<VolumetricLight>();
}
}
// Update the help text.
for mut text in text.iter_mut() {
*text = create_text(&app_settings);
}
}