forked from BlackPhlox/bevy_dolly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbit.rs
283 lines (258 loc) · 8.57 KB
/
orbit.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#![allow(clippy::type_complexity)]
use bevy::input::mouse::{MouseMotion, MouseWheel};
use bevy::prelude::*;
use bevy::render::camera::ScalingMode;
use bevy_dolly::prelude::*;
#[derive(Component)]
struct MainCamera;
#[derive(Component)]
struct SecondCamera;
fn main() {
App::new()
.insert_resource(Msaa::default())
.add_plugins((DefaultPlugins, DollyPosCtrl, DollyCursorGrab))
.insert_resource(DollyPosCtrlConfig {
..Default::default()
})
.init_state::<ProjectionType>()
.init_state::<Pan>()
.init_state::<ZoomType>()
.add_systems(Startup, setup)
.add_systems(
Update,
(
Dolly::<MainCamera>::update_active,
update_camera,
swap_camera,
handle_mouse_scroll,
),
)
.run();
}
#[derive(States, Default, PartialEq, Eq, Debug, Clone, Copy, Hash)]
enum ProjectionType {
#[default]
Orthographic,
Perspective,
}
#[derive(States, Default, PartialEq, Eq, Debug, Clone, Copy, Hash)]
enum Pan {
#[default]
Mouse,
Keys,
}
#[derive(States, Default, PartialEq, Eq, Debug, Clone, Copy, Hash)]
enum ZoomType {
#[default]
Arm,
Fov,
}
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
startup_perspective: Res<State<ProjectionType>>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(5., 5.)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
..default()
});
commands.spawn(Transform::from_xyz(0., 0., 0.));
commands.spawn((
SceneBundle {
scene: asset_server.load("poly_dolly.gltf#Scene0"),
transform: Transform {
translation: Vec3::new(0., 0.2, 0.),
..default()
},
..default()
},
DollyPosCtrlMove,
));
commands.spawn((
MainCamera,
Rig::builder()
.with(Position::new(Vec3::ZERO))
.with(YawPitch::new().yaw_degrees(45.0).pitch_degrees(-30.0))
.with(Smooth::new_position(0.3))
.with(Smooth::new_rotation(0.3))
.with(Arm::new(Vec3::Z * 4.0))
.build(),
));
let orth = Camera3dBundle {
projection: OrthographicProjection {
scale: 3.0,
scaling_mode: ScalingMode::FixedVertical(2.0),
..default()
}
.into(),
transform: Transform::from_xyz(10.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
};
let pers = Camera3dBundle {
projection: PerspectiveProjection {
..Default::default()
}
.into(),
transform: Transform::from_xyz(10.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
camera: Camera {
is_active: false,
..Default::default()
},
..Default::default()
};
if *startup_perspective == ProjectionType::Orthographic {
commands.spawn((MainCamera, orth));
commands.spawn((SecondCamera, pers));
} else {
commands.spawn((MainCamera, pers));
commands.spawn((SecondCamera, orth));
}
// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
info!("Use W, A, S, D for movement");
info!("Use Space and Shift for going up and down");
info!("Use Z and X to orbit the sheep");
info!("Or press E to toggle to use the mouse to orbit the sheep");
info!("Press T to toggle between orthographic and perspective camera");
info!("Scroll to Zoom (Press G to switch between changing arm length and fov for perspective and scale for orthographic)");
info!("Press P to toggle pinned to entity with DollyPosCtrlMove component");
info!("Press Esc to toggle cursor focus");
}
#[allow(clippy::too_many_arguments)]
fn swap_camera(
keys: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
perspective: Res<State<ProjectionType>>,
mut next_perspective: ResMut<NextState<ProjectionType>>,
zoom: Res<State<ZoomType>>,
mut next_zoom: ResMut<NextState<ZoomType>>,
mut q_main: Query<(Entity, &mut Camera), (With<MainCamera>, Without<SecondCamera>)>,
mut q_sec: Query<(Entity, &mut Camera), (With<SecondCamera>, Without<MainCamera>)>,
) {
if keys.just_pressed(KeyCode::KeyT) {
if let Ok((e_main, cam_main)) = &mut q_main.get_single_mut() {
if let Ok((e_sec, cam_sec)) = &mut q_sec.get_single_mut() {
commands
.entity(*e_main)
.remove::<MainCamera>()
.insert(SecondCamera);
commands
.entity(*e_sec)
.remove::<SecondCamera>()
.insert(MainCamera);
cam_sec.is_active = true;
cam_main.is_active = false;
next_perspective.set(if *perspective == ProjectionType::Orthographic {
ProjectionType::Perspective
} else {
ProjectionType::Orthographic
});
println!("Perspective: {:?}", perspective);
}
}
} else if keys.just_pressed(KeyCode::KeyG) {
// Arm doesn't make a difference for Orthographic projection
next_zoom.set(
if *zoom == ZoomType::Arm && *perspective == ProjectionType::Perspective {
ZoomType::Fov
} else {
ZoomType::Arm
},
);
println!("ZoomType: {:?}", zoom);
}
}
fn handle_mouse_scroll(
mut mouse_wheel_events: EventReader<MouseWheel>,
mut q_main: Query<&mut Projection, With<MainCamera>>,
zoom: Res<State<ZoomType>>,
mut rig_q: Query<&mut Rig>,
) {
for mouse_wheel_event in mouse_wheel_events.read() {
for mut projection in &mut q_main.iter_mut() {
match &mut projection.as_mut() {
Projection::Perspective(pers) => {
if *zoom == ZoomType::Fov {
pers.fov = (pers.fov - mouse_wheel_event.y * 0.01).abs();
} else if let Ok(mut rig) = rig_q.get_single_mut() {
if let Some(arm) = rig.try_driver_mut::<Arm>() {
let mut xz = arm.offset;
xz.z = (xz.z - mouse_wheel_event.y * 0.5).abs();
arm.offset = xz;
}
}
}
Projection::Orthographic(orth) => {
orth.scale = (orth.scale - mouse_wheel_event.y * 0.1).abs();
}
}
}
}
}
#[allow(clippy::too_many_arguments)]
fn update_camera(
keys: Res<ButtonInput<KeyCode>>,
pan: Res<State<Pan>>,
mut next_pan: ResMut<NextState<Pan>>,
mut mouse_motion_events: EventReader<MouseMotion>,
mut rig_q: Query<&mut Rig>,
trans: Query<&Transform, With<DollyPosCtrlMove>>,
mut config: ResMut<DollyPosCtrlConfig>,
grab_config: Res<DollyCursorGrabConfig>,
) {
let mut rig = rig_q.single_mut();
let camera_yp = rig.driver_mut::<YawPitch>();
let sensitivity = Vec2::splat(2.0);
let mut delta = Vec2::ZERO;
for event in mouse_motion_events.read() {
delta += event.delta;
}
config.transform.rotation = Quat::from_rotation_y(delta.x);
if *pan == Pan::Keys {
if keys.just_pressed(KeyCode::KeyZ) {
camera_yp.rotate_yaw_pitch(-90.0, 0.0);
}
if keys.just_pressed(KeyCode::KeyX) {
camera_yp.rotate_yaw_pitch(90.0, 0.0);
}
} else if !grab_config.visible {
camera_yp.rotate_yaw_pitch(
-0.1 * delta.x * sensitivity.x,
-0.1 * delta.y * sensitivity.y,
);
}
if keys.just_pressed(KeyCode::KeyE) {
let result = if *pan == Pan::Keys {
Pan::Mouse
} else {
Pan::Keys
};
next_pan.set(result);
println!("PanType: {result:?}");
}
if keys.just_pressed(KeyCode::KeyP) {
config.pin = !config.pin;
println!(
"Camera: {}",
match config.pin {
true => "Pinned",
false => "Static",
}
);
}
if config.pin {
if let Some(camera_pos) = rig.try_driver_mut::<Position>() {
for pos in trans.iter() {
camera_pos.position = pos.translation + Vec3::new(0., 1., 0.);
}
}
}
}