-
Notifications
You must be signed in to change notification settings - Fork 1
/
projection.rs
179 lines (153 loc) · 5.19 KB
/
projection.rs
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
use bevy::{
color::palettes::css::{DARK_GREEN, TAN},
core_pipeline::auto_exposure::AutoExposurePlugin,
prelude::*,
};
use bevy_map_camera::{CameraPerspectiveState, LookTransform, MapCameraBundle, MapCameraPlugin};
const ROTATION_ENABLED: &str = "Press Left Mouse Button to rotate";
const ROTATION_DISABLED: &str = "Rotation Disabled";
const PERSPECTIVE: &str = "Perspective Projection";
const ORTHOGRAPHIC: &str = "Orthographic Projection";
fn main() {
let mut app = App::new();
app.add_plugins((DefaultPlugins, AutoExposurePlugin, MapCameraPlugin));
app.add_systems(Startup, setup);
app.add_systems(Update, (trigger_perspective_change, projection_changed));
app.run();
}
#[derive(Component)]
struct ProjectionLabel;
#[derive(Component)]
struct RotationLabel;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(10., 10.)),
material: materials.add(Color::from(DARK_GREEN)),
..Default::default()
});
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Cuboid::default()),
material: materials.add(Color::from(TAN)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
..Default::default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
// camera
commands.spawn(MapCameraBundle::new_with_transform(LookTransform::new(
Vec3 {
x: 1.,
y: 4.5,
z: 15.0,
},
Vec3::ZERO,
Vec3::Y,
)));
// text
commands
.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
..Default::default()
},
..Default::default()
})
.with_children(|parent| {
parent.spawn((
TextBundle::from_section("Press Right Mouse Button to pan", TextStyle::default())
.with_style(Style {
margin: UiRect::all(Val::Px(5.0)),
..Default::default()
}),
Label,
));
parent.spawn((
TextBundle::from_section(ROTATION_ENABLED, TextStyle::default()).with_style(
Style {
margin: UiRect::all(Val::Px(5.0)),
align_self: AlignSelf::FlexStart,
..default()
},
),
Label,
RotationLabel,
));
parent.spawn((
TextBundle::from_section(
"Press Middle Mouse Button to toggle projection",
TextStyle::default(),
)
.with_style(Style {
margin: UiRect::all(Val::Px(5.0)),
align_self: AlignSelf::FlexStart,
..default()
}),
Label,
));
});
commands.spawn((
TextBundle::from_section(PERSPECTIVE, TextStyle::default()).with_style(Style {
margin: UiRect::all(Val::Px(5.0)),
position_type: PositionType::Absolute,
right: Val::Px(10.0),
..default()
}),
Label,
ProjectionLabel,
));
}
fn trigger_perspective_change(
button: Res<ButtonInput<MouseButton>>,
current_state: Res<State<CameraPerspectiveState>>,
mut next_state: ResMut<NextState<CameraPerspectiveState>>,
) {
if button.just_pressed(MouseButton::Middle) {
match current_state.get() {
CameraPerspectiveState::Orthographic => {
next_state.set(CameraPerspectiveState::Perspective)
}
CameraPerspectiveState::Perspective => {
next_state.set(CameraPerspectiveState::Orthographic)
}
};
}
}
fn projection_changed(
projection_query: Query<&Projection, (With<LookTransform>, Changed<Projection>, Without<Text>)>,
mut projection_text: Query<&mut Text, (With<ProjectionLabel>, Without<RotationLabel>)>,
mut rotation_text: Query<&mut Text, (With<RotationLabel>, Without<ProjectionLabel>)>,
) {
if let Ok(projection) = projection_query.get_single() {
let (p_text, r_text) = match projection {
Projection::Perspective(_) => (PERSPECTIVE, ROTATION_ENABLED),
Projection::Orthographic(_) => (ORTHOGRAPHIC, ROTATION_DISABLED),
};
//Projection text
{
let mut text = projection_text.single_mut();
let mut section = text.sections[0].clone();
section.value = String::from(p_text);
text.sections = vec![section];
}
//Rotation text
{
let mut text = rotation_text.single_mut();
let mut section = text.sections[0].clone();
section.value = String::from(r_text);
text.sections = vec![section];
}
}
}