/
game_camera.rs
305 lines (274 loc) · 10.6 KB
/
game_camera.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use crate::{assets, inspect, mesh};
use bevy::input::mouse::{MouseMotion, MouseWheel};
use bevy::prelude::*;
use bevy::render::camera::PerspectiveProjection;
use noise::{NoiseFn, Perlin};
pub struct GameCameraPlugin;
impl Plugin for GameCameraPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(Perlin::new())
.insert_resource(CameraSettings::default())
//.add_system(handle_camera_shake)
.add_system(update_camera)
//.add_system(pan_orbit_camera);
;
}
}
#[derive(Component)]
pub struct PanOrbitCamera {
pub focus: Vec3,
pub radius: f32,
pub upside_down: bool,
}
impl Default for PanOrbitCamera {
fn default() -> Self {
PanOrbitCamera {
focus: Vec3::ZERO,
radius: 5.0,
upside_down: false,
}
}
}
#[derive(Default)]
pub struct CameraSettings {
look_at: Vec3,
height: f32,
orbit: bool,
speed: f32,
distance: f32,
target_distance: f32,
}
impl CameraSettings {
pub fn set_camera(
&mut self,
height: f32,
look_at: Vec3,
speed: f32,
orbit: bool,
distance: f32,
target_distance: f32,
) {
self.height = height;
self.look_at = look_at;
self.speed = speed;
self.orbit = orbit;
self.distance = distance;
self.target_distance = target_distance;
}
}
fn update_camera(
mut cameras: Query<&mut Transform, With<PanOrbitCamera>>,
mut camera_settings: ResMut<CameraSettings>,
time: Res<Time>,
) {
let mut c = camera_settings;
if (c.distance - c.target_distance).abs() > 0.1 {
if c.distance > c.target_distance {
c.distance -= time.delta_seconds() * 8.0;
} else {
c.distance += time.delta_seconds() * 8.0;
}
}
for mut transform in cameras.iter_mut() {
if transform.translation.is_nan() {
transform.translation = Vec3::new(0.1, 0.1, 0.1);
}
let height_difference = transform.translation.y - c.height;
if height_difference.abs() > 0.1 {
transform.translation.y +=
(c.height - transform.translation.y) * c.speed * time.delta_seconds();
// if height_difference > 0.0 {
// transform.translation.y -=
// (c.height - transform.translation.y)
// * c.speed
// * time.delta_seconds();
// } else {
// transform.translation.y +=
// (c.height - transform.translation.y)
// * c.speed
// * time.delta_seconds();
// }
}
if c.orbit {
let yaw = Quat::from_rotation_y(time.delta_seconds() as f32 * 0.3);
transform.rotation *= yaw; // rotate around global y axis
} else {
transform.rotation = Quat::from_rotation_y((3.0 * std::f32::consts::PI) / 2.0);
}
let rot_matrix = Mat3::from_quat(transform.rotation);
let new_translation = c.look_at + rot_matrix.mul_vec3(Vec3::new(0.0, 0.0, c.distance));
transform.translation.x = new_translation.x;
transform.translation.z = new_translation.z;
transform.look_at(c.look_at, Vec3::Y);
}
}
fn handle_camera_shake(
time: Res<Time>,
perlin: Res<Perlin>,
mut query: Query<(&mut PanOrbitCamera, &mut Transform, &PerspectiveProjection)>,
inspector_data: Res<inspect::InspectorData>,
) {
let delta_seconds = time.delta_seconds() as f64;
let perlin_value_1 = perlin.get([delta_seconds, 37.7, 2.8]) as f32;
let perlin_value_2 = perlin.get([delta_seconds, 38.7, 2.8]) as f32;
let perlin_value_3 = perlin.get([delta_seconds, 39.7, 2.8]) as f32;
let shake = inspector_data.camera_shake;
let max_yaw = inspector_data.max_camera_yaw;
let max_pitch = inspector_data.max_camera_pitch;
let max_roll = inspector_data.max_camera_roll;
let yaw = Quat::from_rotation_y(max_yaw * shake * perlin_value_1);
let pitch = Quat::from_rotation_x(max_pitch * shake * perlin_value_2);
let roll = Quat::from_rotation_z(max_roll * shake * perlin_value_3);
for (_, mut transform, _) in query.iter_mut() {
// transform.rotation = yaw;
// transform.rotation = pitch;
// transform.rotation = roll;
let perlin_value = ((perlin.get([-delta_seconds, delta_seconds]) as f32) + 0.05) * 0.0001;
// println!("{}", perlin_value);
// transform.translation += Vec3::new(perlin_value, 0.0, 0.0) * 0.5;
transform.rotation *= Quat::from_rotation_y(perlin_value);
}
}
pub fn pan_orbit_camera(
windows: Res<Windows>,
mut ev_motion: EventReader<MouseMotion>,
mut ev_scroll: EventReader<MouseWheel>,
input_mouse: Res<Input<MouseButton>>,
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&mut PanOrbitCamera, &mut Transform, &PerspectiveProjection)>,
camera_settings: Res<CameraSettings>,
time: Res<Time>,
) {
// change input mapping for orbit and panning here
let orbit_button = MouseButton::Right;
let orbit_key = KeyCode::LShift;
let pan_button = MouseButton::Middle;
let pan_key = KeyCode::LAlt;
let mut pan = Vec2::ZERO;
let mut rotation_move = Vec2::ZERO;
let mut scroll = 0.0;
let mut orbit_button_changed = false;
if camera_settings.orbit {
rotation_move = Vec2::new(2.0, 0.0);
}
if input_mouse.pressed(orbit_button) || keyboard_input.pressed(orbit_key) {
for ev in ev_motion.iter() {
rotation_move += ev.delta;
}
} else if input_mouse.pressed(pan_button) || keyboard_input.pressed(pan_key) {
// Pan only if we're not rotating at the moment
for ev in ev_motion.iter() {
pan += ev.delta;
}
}
for ev in ev_scroll.iter() {
scroll += ev.y;
}
if input_mouse.just_released(orbit_button)
|| input_mouse.just_pressed(orbit_button)
|| keyboard_input.just_released(orbit_key)
|| keyboard_input.just_pressed(orbit_key)
{
orbit_button_changed = true;
}
for (mut pan_orbit, mut transform, projection) in query.iter_mut() {
if orbit_button_changed {
// only check for upside down when orbiting started or ended this frame
// if the camera is "upside" down, panning horizontally would be inverted, so invert the input to make it correct
let up = transform.rotation * Vec3::Y;
pan_orbit.upside_down = up.y <= 0.0;
}
let mut any = false;
if rotation_move.length_squared() > 0.0 {
any = true;
let window = get_primary_window_size(&windows);
let delta_x = {
let delta = rotation_move.x / window.x * std::f32::consts::PI * 2.0;
if pan_orbit.upside_down {
-delta
} else {
delta
}
};
let delta_y = rotation_move.y / window.y * std::f32::consts::PI;
let yaw = Quat::from_rotation_y(-delta_x);
let pitch = Quat::from_rotation_x(-delta_y);
transform.rotation = yaw * transform.rotation; // rotate around global y axis
transform.rotation *= pitch; // rotate around local x axis
} else if pan.length_squared() > 0.0 {
any = true;
// make panning distance independent of resolution and FOV,
let window = get_primary_window_size(&windows);
pan *= Vec2::new(projection.fov * projection.aspect_ratio, projection.fov) / window;
// translate by local axes
let right = transform.rotation * Vec3::X * -pan.x;
let up = transform.rotation * Vec3::Y * pan.y;
// make panning proportional to distance away from focus point
let translation = (right + up) * pan_orbit.radius;
pan_orbit.focus += translation;
} else if scroll.abs() > 0.0 {
any = true;
pan_orbit.radius -= scroll * pan_orbit.radius * 0.2;
// dont allow zoom to reach zero or you get stuck
pan_orbit.radius = f32::max(pan_orbit.radius, 0.05);
}
if any {
// emulating parent/child to make the yaw/y-axis rotation behave like a turntable
// parent = x and y rotation
// child = z-offset
let rot_matrix = Mat3::from_quat(transform.rotation);
let new_translation =
pan_orbit.focus + rot_matrix.mul_vec3(Vec3::new(0.0, 0.0, pan_orbit.radius));
transform.translation.x = new_translation.x;
transform.translation.z = new_translation.z;
}
}
}
fn get_primary_window_size(windows: &Res<Windows>) -> Vec2 {
let window = windows.get_primary().unwrap();
Vec2::new(window.width() as f32, window.height() as f32)
}
pub fn spawn_camera(mut commands: Commands, game_assets: Res<assets::GameAssets>) {
//let translation = Vec3::new(-25.0, 25.0, 0.0);
let translation = Vec3::new(0.1, 0.1, 0.1);
let radius = translation.length();
commands
.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_translation(translation).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
})
.with_children(|parent| {
const HALF_SIZE: f32 = 100.0;
parent.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
// Configure the projection to better fit the scene
illuminance: 10000.0,
shadow_projection: OrthographicProjection {
left: -HALF_SIZE,
right: HALF_SIZE,
bottom: -HALF_SIZE,
top: HALF_SIZE,
near: -10.0 * HALF_SIZE,
far: 10.0 * HALF_SIZE,
..Default::default()
},
shadows_enabled: true,
..Default::default()
},
transform: Transform {
rotation: Quat::from_rotation_x(-std::f32::consts::FRAC_PI_4),
..Default::default()
},
..Default::default()
});
})
.insert(PanOrbitCamera {
radius,
..Default::default()
});
}
pub fn despawn_camera(mut commands: Commands, cameras: Query<Entity, With<PanOrbitCamera>>) {
for entity in cameras.iter() {
commands.entity(entity).despawn_recursive();
}
}