1use bevy::{
4 core_pipeline::tonemapping::{DebandDither, Tonemapping},
5 post_process::bloom::{Bloom, BloomCompositeMode},
6 prelude::*,
7};
8
9fn main() {
10 App::new()
11 .add_plugins(DefaultPlugins)
12 .add_systems(Startup, setup)
13 .add_systems(Update, update_bloom_settings)
14 .run();
15}
16
17fn setup(
18 mut commands: Commands,
19 mut meshes: ResMut<Assets<Mesh>>,
20 mut materials: ResMut<Assets<ColorMaterial>>,
21 asset_server: Res<AssetServer>,
22) {
23 commands.spawn((
24 Camera2d,
25 Camera {
26 clear_color: ClearColorConfig::Custom(Color::BLACK),
27 ..default()
28 },
29 Tonemapping::TonyMcMapface, Bloom::default(), DebandDither::Enabled, ));
33
34 commands.spawn(Sprite {
36 image: asset_server.load("branding/bevy_bird_dark.png"),
37 color: Color::srgb(5.0, 5.0, 5.0), custom_size: Some(Vec2::splat(160.0)),
39 ..default()
40 });
41
42 commands.spawn((
44 Mesh2d(meshes.add(Circle::new(100.))),
45 MeshMaterial2d(materials.add(Color::srgb(7.5, 0.0, 7.5))),
47 Transform::from_translation(Vec3::new(-200., 0., 0.)),
48 ));
49
50 commands.spawn((
52 Mesh2d(meshes.add(RegularPolygon::new(100., 6))),
53 MeshMaterial2d(materials.add(Color::srgb(6.25, 9.4, 9.1))),
55 Transform::from_translation(Vec3::new(200., 0., 0.)),
56 ));
57
58 commands.spawn((
60 Text::default(),
61 Node {
62 position_type: PositionType::Absolute,
63 top: px(12),
64 left: px(12),
65 ..default()
66 },
67 ));
68}
69
70fn update_bloom_settings(
73 camera: Single<(Entity, &Tonemapping, Option<&mut Bloom>), With<Camera>>,
74 mut text: Single<&mut Text>,
75 mut commands: Commands,
76 keycode: Res<ButtonInput<KeyCode>>,
77 time: Res<Time>,
78) {
79 let (camera_entity, tonemapping, bloom) = camera.into_inner();
80
81 match bloom {
82 Some(mut bloom) => {
83 text.0 = "Bloom (Toggle: Space)\n".to_string();
84 text.push_str(&format!("(Q/A) Intensity: {:.2}\n", bloom.intensity));
85 text.push_str(&format!(
86 "(W/S) Low-frequency boost: {:.2}\n",
87 bloom.low_frequency_boost
88 ));
89 text.push_str(&format!(
90 "(E/D) Low-frequency boost curvature: {:.2}\n",
91 bloom.low_frequency_boost_curvature
92 ));
93 text.push_str(&format!(
94 "(R/F) High-pass frequency: {:.2}\n",
95 bloom.high_pass_frequency
96 ));
97 text.push_str(&format!(
98 "(T/G) Mode: {}\n",
99 match bloom.composite_mode {
100 BloomCompositeMode::EnergyConserving => "Energy-conserving",
101 BloomCompositeMode::Additive => "Additive",
102 }
103 ));
104 text.push_str(&format!(
105 "(Y/H) Threshold: {:.2}\n",
106 bloom.prefilter.threshold
107 ));
108 text.push_str(&format!(
109 "(U/J) Threshold softness: {:.2}\n",
110 bloom.prefilter.threshold_softness
111 ));
112 text.push_str(&format!("(I/K) Horizontal Scale: {:.2}\n", bloom.scale.x));
113
114 if keycode.just_pressed(KeyCode::Space) {
115 commands.entity(camera_entity).remove::<Bloom>();
116 }
117
118 let dt = time.delta_secs();
119
120 if keycode.pressed(KeyCode::KeyA) {
121 bloom.intensity -= dt / 10.0;
122 }
123 if keycode.pressed(KeyCode::KeyQ) {
124 bloom.intensity += dt / 10.0;
125 }
126 bloom.intensity = bloom.intensity.clamp(0.0, 1.0);
127
128 if keycode.pressed(KeyCode::KeyS) {
129 bloom.low_frequency_boost -= dt / 10.0;
130 }
131 if keycode.pressed(KeyCode::KeyW) {
132 bloom.low_frequency_boost += dt / 10.0;
133 }
134 bloom.low_frequency_boost = bloom.low_frequency_boost.clamp(0.0, 1.0);
135
136 if keycode.pressed(KeyCode::KeyD) {
137 bloom.low_frequency_boost_curvature -= dt / 10.0;
138 }
139 if keycode.pressed(KeyCode::KeyE) {
140 bloom.low_frequency_boost_curvature += dt / 10.0;
141 }
142 bloom.low_frequency_boost_curvature =
143 bloom.low_frequency_boost_curvature.clamp(0.0, 1.0);
144
145 if keycode.pressed(KeyCode::KeyF) {
146 bloom.high_pass_frequency -= dt / 10.0;
147 }
148 if keycode.pressed(KeyCode::KeyR) {
149 bloom.high_pass_frequency += dt / 10.0;
150 }
151 bloom.high_pass_frequency = bloom.high_pass_frequency.clamp(0.0, 1.0);
152
153 if keycode.pressed(KeyCode::KeyG) {
154 bloom.composite_mode = BloomCompositeMode::Additive;
155 }
156 if keycode.pressed(KeyCode::KeyT) {
157 bloom.composite_mode = BloomCompositeMode::EnergyConserving;
158 }
159
160 if keycode.pressed(KeyCode::KeyH) {
161 bloom.prefilter.threshold -= dt;
162 }
163 if keycode.pressed(KeyCode::KeyY) {
164 bloom.prefilter.threshold += dt;
165 }
166 bloom.prefilter.threshold = bloom.prefilter.threshold.max(0.0);
167
168 if keycode.pressed(KeyCode::KeyJ) {
169 bloom.prefilter.threshold_softness -= dt / 10.0;
170 }
171 if keycode.pressed(KeyCode::KeyU) {
172 bloom.prefilter.threshold_softness += dt / 10.0;
173 }
174 bloom.prefilter.threshold_softness = bloom.prefilter.threshold_softness.clamp(0.0, 1.0);
175
176 if keycode.pressed(KeyCode::KeyK) {
177 bloom.scale.x -= dt * 2.0;
178 }
179 if keycode.pressed(KeyCode::KeyI) {
180 bloom.scale.x += dt * 2.0;
181 }
182 bloom.scale.x = bloom.scale.x.clamp(0.0, 16.0);
183 }
184
185 None => {
186 text.0 = "Bloom: Off (Toggle: Space)\n".to_string();
187
188 if keycode.just_pressed(KeyCode::Space) {
189 commands.entity(camera_entity).insert(Bloom::default());
190 }
191 }
192 }
193
194 text.push_str(&format!("(O) Tonemapping: {tonemapping:?}\n"));
195 if keycode.just_pressed(KeyCode::KeyO) {
196 commands
197 .entity(camera_entity)
198 .insert(next_tonemap(tonemapping));
199 }
200}
201
202fn next_tonemap(tonemapping: &Tonemapping) -> Tonemapping {
204 match tonemapping {
205 Tonemapping::None => Tonemapping::AcesFitted,
206 Tonemapping::AcesFitted => Tonemapping::AgX,
207 Tonemapping::AgX => Tonemapping::BlenderFilmic,
208 Tonemapping::BlenderFilmic => Tonemapping::Reinhard,
209 Tonemapping::Reinhard => Tonemapping::ReinhardLuminance,
210 Tonemapping::ReinhardLuminance => Tonemapping::SomewhatBoringDisplayTransform,
211 Tonemapping::SomewhatBoringDisplayTransform => Tonemapping::TonyMcMapface,
212 Tonemapping::TonyMcMapface => Tonemapping::None,
213 }
214}