1use bevy::{
4 anti_alias::taa::TemporalAntiAliasing,
5 math::ops,
6 pbr::{ScreenSpaceAmbientOcclusion, ScreenSpaceAmbientOcclusionQualityLevel},
7 prelude::*,
8 render::{camera::TemporalJitter, view::Hdr},
9};
10use std::f32::consts::PI;
11
12fn main() {
13 App::new()
14 .insert_resource(AmbientLight {
15 brightness: 1000.,
16 ..default()
17 })
18 .add_plugins(DefaultPlugins)
19 .add_systems(Startup, setup)
20 .add_systems(Update, update)
21 .run();
22}
23
24fn setup(
25 mut commands: Commands,
26 mut meshes: ResMut<Assets<Mesh>>,
27 mut materials: ResMut<Assets<StandardMaterial>>,
28) {
29 commands.spawn((
30 Camera3d::default(),
31 Transform::from_xyz(-2.0, 2.0, -2.0).looking_at(Vec3::ZERO, Vec3::Y),
32 Hdr,
33 Msaa::Off,
34 ScreenSpaceAmbientOcclusion::default(),
35 TemporalAntiAliasing::default(),
36 ));
37
38 let material = materials.add(StandardMaterial {
39 base_color: Color::srgb(0.5, 0.5, 0.5),
40 perceptual_roughness: 1.0,
41 reflectance: 0.0,
42 ..default()
43 });
44 commands.spawn((
45 Mesh3d(meshes.add(Cuboid::default())),
46 MeshMaterial3d(material.clone()),
47 Transform::from_xyz(0.0, 0.0, 1.0),
48 ));
49 commands.spawn((
50 Mesh3d(meshes.add(Cuboid::default())),
51 MeshMaterial3d(material.clone()),
52 Transform::from_xyz(0.0, -1.0, 0.0),
53 ));
54 commands.spawn((
55 Mesh3d(meshes.add(Cuboid::default())),
56 MeshMaterial3d(material),
57 Transform::from_xyz(1.0, 0.0, 0.0),
58 ));
59 commands.spawn((
60 Mesh3d(meshes.add(Sphere::new(0.4).mesh().uv(72, 36))),
61 MeshMaterial3d(materials.add(StandardMaterial {
62 base_color: Color::srgb(0.4, 0.4, 0.4),
63 perceptual_roughness: 1.0,
64 reflectance: 0.0,
65 ..default()
66 })),
67 SphereMarker,
68 ));
69
70 commands.spawn((
71 DirectionalLight {
72 shadows_enabled: true,
73 ..default()
74 },
75 Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, PI * -0.15, PI * -0.15)),
76 ));
77
78 commands.spawn((
79 Text::default(),
80 Node {
81 position_type: PositionType::Absolute,
82 bottom: px(12),
83 left: px(12),
84 ..default()
85 },
86 ));
87}
88
89fn update(
90 camera: Single<
91 (
92 Entity,
93 Option<&ScreenSpaceAmbientOcclusion>,
94 Option<&TemporalJitter>,
95 ),
96 With<Camera>,
97 >,
98 mut text: Single<&mut Text>,
99 mut sphere: Single<&mut Transform, With<SphereMarker>>,
100 mut commands: Commands,
101 keycode: Res<ButtonInput<KeyCode>>,
102 time: Res<Time>,
103) {
104 sphere.translation.y = ops::sin(time.elapsed_secs() / 1.7) * 0.7;
105
106 let (camera_entity, ssao, temporal_jitter) = *camera;
107 let current_ssao = ssao.cloned().unwrap_or_default();
108
109 let mut commands = commands.entity(camera_entity);
110 commands
111 .insert_if(
112 ScreenSpaceAmbientOcclusion {
113 quality_level: ScreenSpaceAmbientOcclusionQualityLevel::Low,
114 ..current_ssao
115 },
116 || keycode.just_pressed(KeyCode::Digit2),
117 )
118 .insert_if(
119 ScreenSpaceAmbientOcclusion {
120 quality_level: ScreenSpaceAmbientOcclusionQualityLevel::Medium,
121 ..current_ssao
122 },
123 || keycode.just_pressed(KeyCode::Digit3),
124 )
125 .insert_if(
126 ScreenSpaceAmbientOcclusion {
127 quality_level: ScreenSpaceAmbientOcclusionQualityLevel::High,
128 ..current_ssao
129 },
130 || keycode.just_pressed(KeyCode::Digit4),
131 )
132 .insert_if(
133 ScreenSpaceAmbientOcclusion {
134 quality_level: ScreenSpaceAmbientOcclusionQualityLevel::Ultra,
135 ..current_ssao
136 },
137 || keycode.just_pressed(KeyCode::Digit5),
138 )
139 .insert_if(
140 ScreenSpaceAmbientOcclusion {
141 constant_object_thickness: (current_ssao.constant_object_thickness * 2.0).min(4.0),
142 ..current_ssao
143 },
144 || keycode.just_pressed(KeyCode::ArrowUp),
145 )
146 .insert_if(
147 ScreenSpaceAmbientOcclusion {
148 constant_object_thickness: (current_ssao.constant_object_thickness * 0.5)
149 .max(0.0625),
150 ..current_ssao
151 },
152 || keycode.just_pressed(KeyCode::ArrowDown),
153 );
154 if keycode.just_pressed(KeyCode::Digit1) {
155 commands.remove::<ScreenSpaceAmbientOcclusion>();
156 }
157 if keycode.just_pressed(KeyCode::Space) {
158 if temporal_jitter.is_some() {
159 commands.remove::<TemporalJitter>();
160 } else {
161 commands.insert(TemporalJitter::default());
162 }
163 }
164
165 text.clear();
166
167 let (o, l, m, h, u) = match ssao.map(|s| s.quality_level) {
168 None => ("*", "", "", "", ""),
169 Some(ScreenSpaceAmbientOcclusionQualityLevel::Low) => ("", "*", "", "", ""),
170 Some(ScreenSpaceAmbientOcclusionQualityLevel::Medium) => ("", "", "*", "", ""),
171 Some(ScreenSpaceAmbientOcclusionQualityLevel::High) => ("", "", "", "*", ""),
172 Some(ScreenSpaceAmbientOcclusionQualityLevel::Ultra) => ("", "", "", "", "*"),
173 _ => unreachable!(),
174 };
175
176 if let Some(thickness) = ssao.map(|s| s.constant_object_thickness) {
177 text.push_str(&format!(
178 "Constant object thickness: {thickness} (Up/Down)\n\n"
179 ));
180 }
181
182 text.push_str("SSAO Quality:\n");
183 text.push_str(&format!("(1) {o}Off{o}\n"));
184 text.push_str(&format!("(2) {l}Low{l}\n"));
185 text.push_str(&format!("(3) {m}Medium{m}\n"));
186 text.push_str(&format!("(4) {h}High{h}\n"));
187 text.push_str(&format!("(5) {u}Ultra{u}\n\n"));
188
189 text.push_str("Temporal Antialiasing:\n");
190 text.push_str(match temporal_jitter {
191 Some(_) => "(Space) Enabled",
192 None => "(Space) Disabled",
193 });
194}
195
196#[derive(Component)]
197struct SphereMarker;