Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/3d/reflection_probes.rs
9350 views
1
//! This example shows how to place reflection probes in the scene.
2
//!
3
//! Press Space to cycle through the reflection modes:
4
//!
5
//! 1. A pre-generated [`EnvironmentMapLight`] acting as a reflection probe, with both the skybox and cubes
6
//! 2. A runtime-generated [`GeneratedEnvironmentMapLight`] acting as a reflection probe with just the skybox
7
//! 3. A pre-generated [`EnvironmentMapLight`] with just the skybox
8
//!
9
//! Press Enter to pause or resume rotation.
10
//!
11
//! Reflection probes don't work on WebGL 2 or WebGPU.
12
13
use bevy::{
14
camera::{Exposure, Hdr},
15
core_pipeline::tonemapping::Tonemapping,
16
light::{ParallaxCorrection, Skybox},
17
pbr::generate::generate_environment_map_light,
18
prelude::*,
19
render::render_resource::TextureUsages,
20
};
21
22
use std::{
23
f32::consts::PI,
24
fmt::{Display, Formatter, Result as FmtResult},
25
};
26
27
static STOP_ROTATION_HELP_TEXT: &str = "Press Enter to stop rotation";
28
static START_ROTATION_HELP_TEXT: &str = "Press Enter to start rotation";
29
30
static REFLECTION_MODE_HELP_TEXT: &str = "Press Space to switch reflection mode";
31
32
const ENV_MAP_INTENSITY: f32 = 5000.0;
33
34
// The mode the application is in.
35
#[derive(Resource)]
36
struct AppStatus {
37
// Which environment maps the user has requested to display.
38
reflection_mode: ReflectionMode,
39
// Whether the user has requested the scene to rotate.
40
rotating: bool,
41
// The current roughness of the central sphere
42
sphere_roughness: f32,
43
}
44
45
// Which environment maps the user has requested to display.
46
#[derive(Clone, Copy, PartialEq)]
47
enum ReflectionMode {
48
// Only a world environment map is shown.
49
EnvironmentMap = 0,
50
// Both a world environment map and a reflection probe are present. The
51
// reflection probe is shown in the sphere.
52
ReflectionProbe = 1,
53
// A generated environment map is shown.
54
GeneratedEnvironmentMap = 2,
55
}
56
57
// The various reflection maps.
58
#[derive(Resource)]
59
struct Cubemaps {
60
// The blurry diffuse cubemap that reflects the world, but not the cubes.
61
diffuse_environment_map: Handle<Image>,
62
63
// The specular cubemap mip chain that reflects the world, but not the cubes.
64
specular_environment_map: Handle<Image>,
65
66
// The specular cubemap mip chain that reflects both the world and the cubes.
67
specular_reflection_probe: Handle<Image>,
68
}
69
70
fn main() {
71
// Create the app.
72
App::new()
73
.add_plugins(DefaultPlugins)
74
.init_resource::<AppStatus>()
75
.init_resource::<Cubemaps>()
76
.add_systems(Startup, setup)
77
.add_systems(PreUpdate, add_environment_map_to_camera)
78
.add_systems(
79
Update,
80
change_reflection_type.before(generate_environment_map_light),
81
)
82
.add_systems(Update, toggle_rotation)
83
.add_systems(Update, change_sphere_roughness)
84
.add_systems(
85
Update,
86
rotate_camera
87
.after(toggle_rotation)
88
.after(change_reflection_type),
89
)
90
.add_systems(Update, update_text.after(rotate_camera))
91
.add_systems(Update, setup_environment_map_usage)
92
.run();
93
}
94
95
// Spawns all the scene objects.
96
fn setup(
97
mut commands: Commands,
98
mut meshes: ResMut<Assets<Mesh>>,
99
mut materials: ResMut<Assets<StandardMaterial>>,
100
asset_server: Res<AssetServer>,
101
app_status: Res<AppStatus>,
102
cubemaps: Res<Cubemaps>,
103
) {
104
spawn_camera(&mut commands);
105
spawn_sphere(&mut commands, &mut meshes, &mut materials, &app_status);
106
spawn_reflection_probe(&mut commands, &cubemaps);
107
spawn_scene(&mut commands, &asset_server);
108
spawn_text(&mut commands, &app_status);
109
}
110
111
// Spawns the cubes, light, and camera.
112
fn spawn_scene(commands: &mut Commands, asset_server: &AssetServer) {
113
commands.spawn((
114
SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/cubes/Cubes.glb"))),
115
CubesScene,
116
));
117
}
118
119
// Spawns the camera.
120
fn spawn_camera(commands: &mut Commands) {
121
commands.spawn((
122
Camera3d::default(),
123
Hdr,
124
Exposure { ev100: 11.0 },
125
Tonemapping::AcesFitted,
126
Transform::from_xyz(-3.883, 0.325, 2.781).looking_at(Vec3::ZERO, Vec3::Y),
127
));
128
}
129
130
// Creates the sphere mesh and spawns it.
131
fn spawn_sphere(
132
commands: &mut Commands,
133
meshes: &mut Assets<Mesh>,
134
materials: &mut Assets<StandardMaterial>,
135
app_status: &AppStatus,
136
) {
137
// Create a sphere mesh.
138
let sphere_mesh = meshes.add(Sphere::new(1.0).mesh().ico(7).unwrap());
139
140
// Create a sphere.
141
commands.spawn((
142
Mesh3d(sphere_mesh.clone()),
143
MeshMaterial3d(materials.add(StandardMaterial {
144
base_color: Srgba::hex("#ffffff").unwrap().into(),
145
metallic: 1.0,
146
perceptual_roughness: app_status.sphere_roughness,
147
..StandardMaterial::default()
148
})),
149
SphereMaterial,
150
));
151
}
152
153
// Spawns the reflection probe.
154
fn spawn_reflection_probe(commands: &mut Commands, cubemaps: &Cubemaps) {
155
commands.spawn((
156
LightProbe::default(),
157
EnvironmentMapLight {
158
diffuse_map: cubemaps.diffuse_environment_map.clone(),
159
specular_map: cubemaps.specular_reflection_probe.clone(),
160
intensity: ENV_MAP_INTENSITY,
161
..default()
162
},
163
// 2.0 because the sphere's radius is 1.0 and we want to fully enclose it.
164
Transform::from_scale(Vec3::splat(2.0)),
165
// Disable parallax correction because the reflected scene is quite
166
// distant.
167
ParallaxCorrection::None,
168
));
169
}
170
171
// Spawns the help text.
172
fn spawn_text(commands: &mut Commands, app_status: &AppStatus) {
173
// Create the text.
174
commands.spawn((
175
app_status.create_text(),
176
Node {
177
position_type: PositionType::Absolute,
178
bottom: px(12),
179
left: px(12),
180
..default()
181
},
182
));
183
}
184
185
// Adds a world environment map to the camera. This separate system is needed because the camera is
186
// managed by the scene spawner, as it's part of the glTF file with the cubes, so we have to add
187
// the environment map after the fact.
188
fn add_environment_map_to_camera(
189
mut commands: Commands,
190
query: Query<Entity, Added<Camera3d>>,
191
cubemaps: Res<Cubemaps>,
192
) {
193
for camera_entity in query.iter() {
194
commands
195
.entity(camera_entity)
196
.insert(create_camera_environment_map_light(&cubemaps))
197
.insert(Skybox {
198
image: cubemaps.specular_environment_map.clone(),
199
brightness: ENV_MAP_INTENSITY,
200
..default()
201
});
202
}
203
}
204
205
// A system that handles switching between different reflection modes.
206
fn change_reflection_type(
207
mut commands: Commands,
208
light_probe_query: Query<Entity, With<LightProbe>>,
209
cubes_scene_query: Query<Entity, With<CubesScene>>,
210
camera_query: Query<Entity, With<Camera3d>>,
211
keyboard: Res<ButtonInput<KeyCode>>,
212
mut app_status: ResMut<AppStatus>,
213
cubemaps: Res<Cubemaps>,
214
asset_server: Res<AssetServer>,
215
) {
216
// Only do anything if space was pressed.
217
if !keyboard.just_pressed(KeyCode::Space) {
218
return;
219
}
220
221
// Advance to the next reflection mode.
222
app_status.reflection_mode =
223
ReflectionMode::try_from((app_status.reflection_mode as u32 + 1) % 3).unwrap();
224
225
// Remove light probes
226
for light_probe in light_probe_query.iter() {
227
commands.entity(light_probe).despawn();
228
}
229
// Remove existing cube scenes
230
for scene_entity in cubes_scene_query.iter() {
231
commands.entity(scene_entity).despawn();
232
}
233
match app_status.reflection_mode {
234
ReflectionMode::EnvironmentMap | ReflectionMode::GeneratedEnvironmentMap => {}
235
ReflectionMode::ReflectionProbe => {
236
spawn_reflection_probe(&mut commands, &cubemaps);
237
spawn_scene(&mut commands, &asset_server);
238
}
239
}
240
241
// Update the environment-map components on the camera entity/entities
242
for camera in camera_query.iter() {
243
// Remove any existing environment-map components
244
commands
245
.entity(camera)
246
.remove::<(EnvironmentMapLight, GeneratedEnvironmentMapLight)>();
247
248
match app_status.reflection_mode {
249
// A baked or reflection-probe environment map
250
ReflectionMode::EnvironmentMap | ReflectionMode::ReflectionProbe => {
251
commands
252
.entity(camera)
253
.insert(create_camera_environment_map_light(&cubemaps));
254
}
255
256
// GPU-filtered environment map generated at runtime
257
ReflectionMode::GeneratedEnvironmentMap => {
258
commands
259
.entity(camera)
260
.insert(GeneratedEnvironmentMapLight {
261
environment_map: cubemaps.specular_environment_map.clone(),
262
intensity: ENV_MAP_INTENSITY,
263
..default()
264
});
265
}
266
}
267
}
268
}
269
270
// A system that handles enabling and disabling rotation.
271
fn toggle_rotation(keyboard: Res<ButtonInput<KeyCode>>, mut app_status: ResMut<AppStatus>) {
272
if keyboard.just_pressed(KeyCode::Enter) {
273
app_status.rotating = !app_status.rotating;
274
}
275
}
276
277
// A system that updates the help text.
278
fn update_text(mut text_query: Query<&mut Text>, app_status: Res<AppStatus>) {
279
for mut text in text_query.iter_mut() {
280
*text = app_status.create_text();
281
}
282
}
283
284
impl TryFrom<u32> for ReflectionMode {
285
type Error = ();
286
287
fn try_from(value: u32) -> Result<Self, Self::Error> {
288
match value {
289
0 => Ok(ReflectionMode::EnvironmentMap),
290
1 => Ok(ReflectionMode::ReflectionProbe),
291
2 => Ok(ReflectionMode::GeneratedEnvironmentMap),
292
_ => Err(()),
293
}
294
}
295
}
296
297
impl Display for ReflectionMode {
298
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
299
let text = match *self {
300
ReflectionMode::EnvironmentMap => "Environment map",
301
ReflectionMode::ReflectionProbe => "Reflection probe",
302
ReflectionMode::GeneratedEnvironmentMap => "Generated environment map",
303
};
304
formatter.write_str(text)
305
}
306
}
307
308
impl AppStatus {
309
// Constructs the help text at the bottom of the screen based on the
310
// application status.
311
fn create_text(&self) -> Text {
312
let rotation_help_text = if self.rotating {
313
STOP_ROTATION_HELP_TEXT
314
} else {
315
START_ROTATION_HELP_TEXT
316
};
317
318
format!(
319
"{}\n{}\nRoughness: {:.2}\n{}\nUp/Down arrows to change roughness",
320
self.reflection_mode,
321
rotation_help_text,
322
self.sphere_roughness,
323
REFLECTION_MODE_HELP_TEXT
324
)
325
.into()
326
}
327
}
328
329
// Creates the world environment map light, used as a fallback if no reflection
330
// probe is applicable to a mesh.
331
fn create_camera_environment_map_light(cubemaps: &Cubemaps) -> EnvironmentMapLight {
332
EnvironmentMapLight {
333
diffuse_map: cubemaps.diffuse_environment_map.clone(),
334
specular_map: cubemaps.specular_environment_map.clone(),
335
intensity: ENV_MAP_INTENSITY,
336
..default()
337
}
338
}
339
340
// Rotates the camera a bit every frame.
341
fn rotate_camera(
342
time: Res<Time>,
343
mut camera_query: Query<&mut Transform, With<Camera3d>>,
344
app_status: Res<AppStatus>,
345
) {
346
if !app_status.rotating {
347
return;
348
}
349
350
for mut transform in camera_query.iter_mut() {
351
transform.translation = Vec2::from_angle(time.delta_secs() * PI / 5.0)
352
.rotate(transform.translation.xz())
353
.extend(transform.translation.y)
354
.xzy();
355
transform.look_at(Vec3::ZERO, Vec3::Y);
356
}
357
}
358
359
// Loads the cubemaps from the assets directory.
360
impl FromWorld for Cubemaps {
361
fn from_world(world: &mut World) -> Self {
362
Cubemaps {
363
diffuse_environment_map: world
364
.load_asset("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
365
specular_environment_map: world
366
.load_asset("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
367
specular_reflection_probe: world
368
.load_asset("environment_maps/cubes_reflection_probe_specular_rgb9e5_zstd.ktx2"),
369
}
370
}
371
}
372
373
fn setup_environment_map_usage(cubemaps: Res<Cubemaps>, mut images: ResMut<Assets<Image>>) {
374
if let Some(image) = images.get_mut(&cubemaps.specular_environment_map)
375
&& !image
376
.texture_descriptor
377
.usage
378
.contains(TextureUsages::COPY_SRC)
379
{
380
image.texture_descriptor.usage |= TextureUsages::COPY_SRC;
381
}
382
}
383
384
impl Default for AppStatus {
385
fn default() -> Self {
386
Self {
387
reflection_mode: ReflectionMode::ReflectionProbe,
388
rotating: false,
389
sphere_roughness: 0.2,
390
}
391
}
392
}
393
394
#[derive(Component)]
395
struct SphereMaterial;
396
397
#[derive(Component)]
398
struct CubesScene;
399
400
// A system that changes the sphere's roughness with up/down arrow keys
401
fn change_sphere_roughness(
402
keyboard: Res<ButtonInput<KeyCode>>,
403
mut app_status: ResMut<AppStatus>,
404
mut materials: ResMut<Assets<StandardMaterial>>,
405
sphere_query: Query<&MeshMaterial3d<StandardMaterial>, With<SphereMaterial>>,
406
) {
407
let roughness_delta = if keyboard.pressed(KeyCode::ArrowUp) {
408
0.01 // Decrease roughness
409
} else if keyboard.pressed(KeyCode::ArrowDown) {
410
-0.01 // Increase roughness
411
} else {
412
0.0 // No change
413
};
414
415
if roughness_delta != 0.0 {
416
// Update the app status
417
app_status.sphere_roughness =
418
(app_status.sphere_roughness + roughness_delta).clamp(0.0, 1.0);
419
420
// Update the sphere material
421
for material_handle in sphere_query.iter() {
422
if let Some(material) = materials.get_mut(&material_handle.0) {
423
material.perceptual_roughness = app_status.sphere_roughness;
424
}
425
}
426
}
427
}
428
429