-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathui_aa.rs
More file actions
207 lines (196 loc) · 6.26 KB
/
Copy pathui_aa.rs
File metadata and controls
207 lines (196 loc) · 6.26 KB
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
use bevy::{
anti_alias::{
fxaa::Fxaa,
smaa::{Smaa, SmaaPreset},
taa::TemporalAntiAliasing,
},
prelude::*,
};
use bevy_mod_outline::*;
#[bevy_main]
fn main() {
App::new()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins((DefaultPlugins, OutlinePlugin))
.insert_state(AAMode::NoAA)
.add_systems(Startup, setup)
.add_systems(Update, (bounce, highlight, interaction))
.add_systems(
OnEnter(AAMode::MSAAx4),
|mut query: Query<Entity, With<TheCamera>>, mut commands: Commands| {
commands
.entity(query.single().unwrap())
.insert(Msaa::Sample4);
},
)
.add_systems(
OnExit(AAMode::MSAAx4),
|mut query: Query<Entity, With<TheCamera>>, mut commands: Commands| {
commands.entity(query.single().unwrap()).insert(Msaa::Off);
},
)
.add_systems(
OnEnter(AAMode::FXAA),
|mut query: Query<Entity, With<TheCamera>>, mut commands: Commands| {
commands
.entity(query.single().unwrap())
.insert(Fxaa::default());
},
)
.add_systems(
OnExit(AAMode::FXAA),
|mut query: Query<Entity, With<TheCamera>>, mut commands: Commands| {
commands.entity(query.single().unwrap()).remove::<Fxaa>();
},
)
.add_systems(
OnEnter(AAMode::SMAA),
|mut query: Query<Entity, With<TheCamera>>, mut commands: Commands| {
commands.entity(query.single().unwrap()).insert(Smaa {
preset: SmaaPreset::Ultra,
});
},
)
.add_systems(
OnExit(AAMode::SMAA),
|mut query: Query<Entity, With<TheCamera>>, mut commands: Commands| {
commands.entity(query.single().unwrap()).remove::<Smaa>();
},
)
.add_systems(
OnEnter(AAMode::TAA),
|mut query: Query<Entity, With<TheCamera>>, mut commands: Commands| {
commands
.entity(query.single().unwrap())
.insert(TemporalAntiAliasing::default());
},
)
.add_systems(
OnExit(AAMode::TAA),
|mut query: Query<Entity, With<TheCamera>>, mut commands: Commands| {
commands
.entity(query.single().unwrap())
.remove::<TemporalAntiAliasing>();
},
)
.run();
}
#[derive(Component)]
struct Bounce;
#[derive(Component)]
struct TheCamera;
#[derive(Copy, Clone, States, Component, Debug, PartialEq, Eq, Hash)]
enum AAMode {
NoAA,
MSAAx4,
FXAA,
SMAA,
TAA,
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Add spheres
commands.spawn((
Mesh3d(meshes.add(Sphere::new(1.0).mesh().ico(9).unwrap())),
MeshMaterial3d(materials.add(StandardMaterial::from(Color::srgb(0.5, 0.5, 0.5)))),
Transform::from_translation(Vec3::new(-1.5, 0.0, 0.0)),
OutlineVolume {
visible: true,
width: 25.0,
colour: Color::srgb(1.0, 1.0, 0.0),
},
Bounce,
));
commands.spawn((
Mesh3d(meshes.add(Sphere::new(1.0).mesh().ico(20).unwrap())),
MeshMaterial3d(materials.add(StandardMaterial::from(Color::srgb(0.5, 0.5, 0.5)))),
Transform::from_translation(Vec3::new(1.5, 0.0, 0.0)),
Bounce,
));
// Add buttons
commands
.spawn(Node {
flex_direction: FlexDirection::Column,
..default()
})
.with_children(|parent| {
for mode in [
AAMode::NoAA,
AAMode::MSAAx4,
AAMode::FXAA,
AAMode::SMAA,
AAMode::TAA,
] {
parent
.spawn((
Button,
Node {
margin: UiRect::all(Val::Px(5.0)),
padding: UiRect::all(Val::Px(10.0)),
border: UiRect::all(Val::Px(5.0)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
border_radius: BorderRadius::MAX,
..default()
},
BorderColor::all(Color::BLACK),
BackgroundColor(Color::srgb(0.2, 0.2, 0.2)),
mode,
))
.with_children(|parent| {
parent
.spawn(Text::new(format!("{:?}", mode)))
.insert(TextColor(Color::WHITE));
});
}
});
// Add light source and camera
commands.spawn((
PointLight {
shadow_maps_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 0.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
Msaa::Off,
TheCamera,
));
}
fn bounce(mut query: Query<&mut Transform, With<Bounce>>, timer: Res<Time>, mut t: Local<f32>) {
*t = (*t + timer.delta_secs()) % 4.0;
let y = (*t - 2.0).abs() - 1.0;
for mut transform in query.iter_mut() {
transform.translation.y = y;
}
}
fn highlight(mut query: Query<(&mut BorderColor, &AAMode)>, state: Res<State<AAMode>>) {
for (mut border, m) in query.iter_mut() {
*border = if m == state.get() {
BorderColor::all(Color::srgb(0.0, 0.0, 1.0))
} else {
BorderColor::all(Color::BLACK)
};
}
}
fn interaction(
query: Query<(&Interaction, &AAMode)>,
mut armed: Local<bool>,
mut state: ResMut<NextState<AAMode>>,
) {
let mut any_pressed = false;
for (i, m) in query.iter() {
if *i == Interaction::Pressed {
any_pressed = true;
if *armed {
state.set(*m);
}
}
}
*armed = !any_pressed;
}