docs/bevy/examples/3d/post_processing.rs 10.7 K · 314 lines · raw · history

1 //! Demonstrates Bevy's built-in postprocessing features.
2 //!
3 //! Includes:
4 //!
5 //! - Chromatic Aberration
6 //! - Vignette
7 //! - Lens Distortion
8
9 use std::f32::consts::PI;
10
11 use bevy::{
12 camera::Hdr,
13 light::CascadeShadowConfigBuilder,
14 post_process::effect_stack::{ChromaticAberration, LensDistortion, Vignette},
15 prelude::*,
16 };
17
18 /// The number of units per frame to add to or subtract from intensity when the
19 /// arrow keys are held.
20 const ADJUSTMENT_SPEED: f32 = 0.005;
21
22 /// The maximum supported chromatic aberration intensity level.
23 const MAX_CHROMATIC_ABERRATION_INTENSITY: f32 = 0.4;
24
25 /// The settings that the user can control.
26 #[derive(Resource)]
27 struct AppSettings {
28 /// The index of the currently selected UI item.
29 selected: usize,
30 /// The intensity of the chromatic aberration effect.
31 chromatic_aberration_intensity: f32,
32 /// The intensity of the vignette effect.
33 vignette_intensity: f32,
34 /// The radius of the vignette effect.
35 vignette_radius: f32,
36 /// The smoothness of the vignette effect.
37 vignette_smoothness: f32,
38 /// The roundness of the vignette effect.
39 vignette_roundness: f32,
40 /// The edge compensation of the vignette effect.
41 vignette_edge_compensation: f32,
42 /// The intensity of the lens distortion effect.
43 lens_distortion_intensity: f32,
44 /// Distortion strength multiplier for the horizontal direction.
45 lens_distortion_multiplier_x: f32,
46 /// Distortion strength multiplier for the vertical direction.
47 lens_distortion_multiplier_y: f32,
48 }
49
50 /// The entry point.
51 fn main() {
52 App::new()
53 .init_resource::<AppSettings>()
54 .add_plugins(DefaultPlugins)
55 .add_systems(Startup, setup)
56 .add_systems(Update, handle_keyboard_input)
57 .add_systems(
58 Update,
59 (update_chromatic_aberration_settings, update_help_text)
60 .run_if(resource_changed::<AppSettings>)
61 .after(handle_keyboard_input),
62 )
63 .run();
64 }
65
66 /// Creates the example scene and spawns the UI.
67 fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
68 // Spawn the camera.
69 spawn_camera(&mut commands, &asset_server);
70
71 // Create the scene.
72 spawn_scene(&mut commands, &asset_server);
73
74 // Spawn the help text.
75 spawn_text(&mut commands);
76 }
77
78 /// Spawns the camera, including the [`ChromaticAberration`] component.
79 fn spawn_camera(commands: &mut Commands, asset_server: &AssetServer) {
80 commands.spawn((
81 Camera3d::default(),
82 Hdr,
83 Transform::from_xyz(0.7, 0.7, 1.0).looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
84 DistanceFog {
85 color: Color::srgb_u8(43, 44, 47),
86 falloff: FogFalloff::Linear {
87 start: 1.0,
88 end: 8.0,
89 },
90 ..default()
91 },
92 EnvironmentMapLight {
93 diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
94 specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
95 intensity: 2000.0,
96 ..default()
97 },
98 // Include the `ChromaticAberration` component.
99 ChromaticAberration::default(),
100 // Include the `Vignette` component.
101 Vignette::default(),
102 // Include the `LensDistortion` component.
103 LensDistortion::default(),
104 ));
105 }
106
107 /// Spawns the scene.
108 ///
109 /// This is just the tonemapping test scene, chosen for the fact that it uses a
110 /// variety of colors.
111 fn spawn_scene(commands: &mut Commands, asset_server: &AssetServer) {
112 // Spawn the main scene.
113 commands.spawn(WorldAssetRoot(asset_server.load(
114 GltfAssetLabel::Scene(0).from_asset("models/TonemappingTest/TonemappingTest.gltf"),
115 )));
116
117 // Spawn the flight helmet.
118 commands.spawn((
119 WorldAssetRoot(
120 asset_server
121 .load(GltfAssetLabel::Scene(0).from_asset("models/FlightHelmet/FlightHelmet.gltf")),
122 ),
123 Transform::from_xyz(0.5, 0.0, -0.5).with_rotation(Quat::from_rotation_y(-0.15 * PI)),
124 ));
125
126 // Spawn the light.
127 commands.spawn((
128 DirectionalLight {
129 illuminance: 15000.0,
130 shadow_maps_enabled: true,
131 ..default()
132 },
133 Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, PI * -0.15, PI * -0.15)),
134 CascadeShadowConfigBuilder {
135 maximum_distance: 3.0,
136 first_cascade_far_bound: 0.9,
137 ..default()
138 }
139 .build(),
140 ));
141 }
142
143 /// Spawns the help text.
144 fn spawn_text(commands: &mut Commands) {
145 commands.spawn((
146 Text::default(),
147 Node {
148 position_type: PositionType::Absolute,
149 top: px(12),
150 left: px(12),
151 ..default()
152 },
153 ));
154 }
155
156 impl Default for AppSettings {
157 fn default() -> Self {
158 let vignette_default = Vignette::default();
159 let lens_distortion = LensDistortion::default();
160 Self {
161 selected: 0,
162 chromatic_aberration_intensity: ChromaticAberration::default().intensity,
163 vignette_intensity: vignette_default.intensity,
164 vignette_radius: vignette_default.radius,
165 vignette_smoothness: vignette_default.smoothness,
166 vignette_roundness: vignette_default.roundness,
167 vignette_edge_compensation: vignette_default.edge_compensation,
168 lens_distortion_intensity: lens_distortion.intensity,
169 lens_distortion_multiplier_x: lens_distortion.multiplier.x,
170 lens_distortion_multiplier_y: lens_distortion.multiplier.y,
171 }
172 }
173 }
174
175 /// Handles requests from the user to change the chromatic aberration intensity.
176 fn handle_keyboard_input(mut app_settings: ResMut<AppSettings>, input: Res<ButtonInput<KeyCode>>) {
177 if input.just_pressed(KeyCode::ArrowUp) && app_settings.selected > 0 {
178 app_settings.selected -= 1;
179 } else if input.just_pressed(KeyCode::ArrowDown) && app_settings.selected < 8 {
180 app_settings.selected += 1;
181 }
182
183 let mut delta = 0.0;
184 if input.pressed(KeyCode::ArrowLeft) {
185 delta -= ADJUSTMENT_SPEED;
186 } else if input.pressed(KeyCode::ArrowRight) {
187 delta += ADJUSTMENT_SPEED;
188 }
189
190 // If no arrow key was pressed, just bail out.
191 if delta == 0.0 {
192 return;
193 }
194
195 match app_settings.selected {
196 0 => {
197 app_settings.chromatic_aberration_intensity =
198 (app_settings.chromatic_aberration_intensity + delta)
199 .clamp(0.0, MAX_CHROMATIC_ABERRATION_INTENSITY);
200 }
201 1 => {
202 app_settings.vignette_intensity =
203 (app_settings.vignette_intensity + delta).clamp(0.0, 1.0);
204 }
205 2 => app_settings.vignette_radius = (app_settings.vignette_radius + delta).clamp(0.0, 2.0),
206 3 => {
207 app_settings.vignette_smoothness = (app_settings.vignette_smoothness + delta).max(0.01);
208 }
209 4 => app_settings.vignette_roundness = (app_settings.vignette_roundness + delta).max(0.01),
210 5 => {
211 app_settings.vignette_edge_compensation =
212 (app_settings.vignette_edge_compensation + delta).clamp(0.0, 1.0);
213 }
214 6 => {
215 app_settings.lens_distortion_intensity =
216 (app_settings.lens_distortion_intensity + delta).clamp(-1.0, 1.0);
217 }
218 7 => {
219 app_settings.lens_distortion_multiplier_x =
220 (app_settings.lens_distortion_multiplier_x + delta).clamp(0.0, 1.0);
221 }
222 8 => {
223 app_settings.lens_distortion_multiplier_y =
224 (app_settings.lens_distortion_multiplier_y + delta).clamp(0.0, 1.0);
225 }
226 _ => {}
227 }
228 }
229
230 /// Updates the [`ChromaticAberration`] settings per the [`AppSettings`].
231 fn update_chromatic_aberration_settings(
232 mut chromatic_aberration: Query<&mut ChromaticAberration>,
233 mut vignette: Query<&mut Vignette>,
234 mut lens_distortion: Query<&mut LensDistortion>,
235 app_settings: Res<AppSettings>,
236 ) {
237 let intensity = app_settings.chromatic_aberration_intensity;
238
239 // Pick a reasonable maximum sample size for the intensity to avoid an
240 // artifact whereby the individual samples appear instead of producing
241 // smooth streaks of color.
242 //
243 // Don't take this formula too seriously; it hasn't been heavily tuned.
244 let max_samples = ((intensity - 0.02) / (0.20 - 0.02) * 56.0 + 8.0)
245 .clamp(8.0, 64.0)
246 .round() as u32;
247
248 for mut chromatic_aberration in &mut chromatic_aberration {
249 chromatic_aberration.intensity = intensity;
250 chromatic_aberration.max_samples = max_samples;
251 }
252
253 for mut vignette in &mut vignette {
254 vignette.intensity = app_settings.vignette_intensity;
255 vignette.radius = app_settings.vignette_radius;
256 vignette.smoothness = app_settings.vignette_smoothness;
257 vignette.roundness = app_settings.vignette_roundness;
258 vignette.edge_compensation = app_settings.vignette_edge_compensation;
259 }
260
261 for mut lens_distortion in &mut lens_distortion {
262 lens_distortion.intensity = app_settings.lens_distortion_intensity;
263 lens_distortion.multiplier.x = app_settings.lens_distortion_multiplier_x;
264 lens_distortion.multiplier.y = app_settings.lens_distortion_multiplier_y;
265 }
266 }
267
268 /// Updates the help text at the bottom of the screen to reflect the current
269 /// [`AppSettings`].
270 fn update_help_text(mut text: Single<&mut Text>, app_settings: Res<AppSettings>) {
271 text.clear();
272 let text_list = [
273 format!(
274 "Chromatic aberration intensity: {:.2}\n",
275 app_settings.chromatic_aberration_intensity
276 ),
277 format!(
278 "Vignette intensity: {:.2}\n",
279 app_settings.vignette_intensity
280 ),
281 format!("Vignette radius: {:.2}\n", app_settings.vignette_radius),
282 format!(
283 "Vignette smoothness: {:.2}\n",
284 app_settings.vignette_smoothness
285 ),
286 format!(
287 "Vignette roundness: {:.2}\n",
288 app_settings.vignette_roundness
289 ),
290 format!(
291 "Vignette edge_compensation: {:.2}\n",
292 app_settings.vignette_edge_compensation
293 ),
294 format!(
295 "Lens Distortion intensity: {:.2}\n",
296 app_settings.lens_distortion_intensity
297 ),
298 format!(
299 "Lens Distortion multiplier x: {:.2}\n",
300 app_settings.lens_distortion_multiplier_x
301 ),
302 format!(
303 "Lens Distortion multiplier y: {:.2}\n",
304 app_settings.lens_distortion_multiplier_y
305 ),
306 ];
307 for (i, val) in text_list.iter().enumerate() {
308 if i == app_settings.selected {
309 text.push_str("> ");
310 }
311 text.push_str(val);
312 }
313 text.push_str("\n(Press Up or Down to select)\n(Press Left or Right to change)");
314 }