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
|
use super::{gpu::Gpu, util::MatrixUniform};
use cgmath::{prelude::*, Deg, Euler, Matrix3, Matrix4, Point3, Rad, Vector3};
use collision::Frustum;
use std::time::Duration;
#[derive(Default)]
pub struct CameraInput {
pub forward: bool,
pub left: bool,
pub backward: bool,
pub right: bool,
pub jump: bool,
pub sneak: bool,
pub mouse_x: f32,
pub mouse_y: f32,
}
pub struct Camera {
pub pos: Point3<f32>,
pub rot: Euler<Deg<f32>>,
pub speed: f32,
pub fov: Rad<f32>,
pub view: Matrix4<f32>,
pub proj: Matrix4<f32>,
pub frustum: Frustum<f32>,
pub uniform: MatrixUniform,
pub layout: wgpu::BindGroupLayout,
pub input: CameraInput,
}
pub trait ToNative {
fn to_native(self) -> Self;
}
impl<T> ToNative for Euler<T>
where
T: From<Deg<f32>> + std::ops::Add<T, Output = T> + std::ops::Neg<Output = T>,
{
fn to_native(mut self) -> Self {
self.y = -self.y + Deg(270.0).into();
self.z = -self.z;
self
}
}
impl Camera {
pub fn new(gpu: &Gpu) -> Self {
let layout = MatrixUniform::layout(&gpu.device, "camera");
let uniform = MatrixUniform::new(&gpu.device, &layout, Matrix4::identity(), "camera", true);
Self {
pos: Point3::new(0.0, 0.0, 0.0),
rot: Euler {
x: Deg(0.0),
y: Deg(0.0),
z: Deg(0.0),
},
speed: 0.0,
fov: Deg(90.0).into(),
proj: Matrix4::identity(),
view: Matrix4::identity(),
frustum: Frustum::from_matrix4(Matrix4::identity()).unwrap(),
uniform,
layout,
input: Default::default(),
}
}
pub fn update(&mut self, gpu: &Gpu, dt: Duration) {
let dt = dt.as_secs_f32();
let sensitivity = dt * 2.0;
self.rot.y += Deg(sensitivity * self.input.mouse_x);
self.rot.z += Deg(sensitivity * self.input.mouse_y);
self.rot.z.0 = self.rot.z.0.min(89.9).max(-89.9);
self.input.mouse_x = 0.0;
self.input.mouse_y = 0.0;
let rot = Matrix3::from(self.rot.to_native());
let forward = rot * Vector3::unit_x();
let up = rot * Vector3::unit_y();
{
let mut forward = forward;
let mut up = up;
let mut right = forward.cross(up);
let pitch_move = false;
if !pitch_move {
forward.y = 0.0;
right.y = 0.0;
up = Vector3::unit_y();
}
let mut hdir = Vector3::zero();
let mut vdir = Vector3::zero();
if self.input.forward {
hdir += forward;
}
if self.input.backward {
hdir -= forward;
}
if self.input.right {
hdir += right;
}
if self.input.left {
hdir -= right;
}
if self.input.jump {
vdir += up;
}
if self.input.sneak {
vdir -= up;
}
self.pos += self.speed
* dt
* (vdir
+ if hdir.is_zero() {
hdir
} else {
hdir.normalize()
});
}
self.view = Matrix4::look_at_dir(self.pos, forward, up);
self.uniform.set(&gpu.queue, self.proj * self.view);
}
pub fn resize(&mut self, size: winit::dpi::PhysicalSize<u32>) {
self.proj = cgmath::perspective(
self.fov,
size.width as f32 / size.height as f32,
0.1,
100000.0,
);
self.frustum = Frustum::from_matrix4(self.proj).unwrap();
}
}
|