1use std::ops::{Add, Div, Mul, Neg, Sub};
4
5#[repr(C)]
7#[derive(Debug, Clone, Copy, PartialEq, Default)]
8pub struct Vec3 {
9 pub x: f32,
10 pub y: f32,
11 pub z: f32,
12}
13
14impl Vec3 {
15 pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
16 pub const ONE: Self = Self::new(1.0, 1.0, 1.0);
17 pub const UP: Self = Self::new(0.0, 1.0, 0.0);
18 pub const RIGHT: Self = Self::new(1.0, 0.0, 0.0);
19 pub const FORWARD: Self = Self::new(0.0, 0.0, -1.0);
20
21 #[inline(always)]
22 pub const fn new(x: f32, y: f32, z: f32) -> Self {
23 Self { x, y, z }
24 }
25
26 #[inline(always)]
27 pub fn dot(self, other: Self) -> f32 {
28 self.x * other.x + self.y * other.y + self.z * other.z
29 }
30
31 #[inline(always)]
32 pub fn cross(self, other: Self) -> Self {
33 Self {
34 x: self.y * other.z - self.z * other.y,
35 y: self.z * other.x - self.x * other.z,
36 z: self.x * other.y - self.y * other.x,
37 }
38 }
39
40 #[inline(always)]
41 pub fn length_squared(self) -> f32 {
42 self.dot(self)
43 }
44
45 #[inline(always)]
46 pub fn length(self) -> f32 {
47 self.length_squared().sqrt()
48 }
49
50 #[inline(always)]
51 pub fn normalize(self) -> Self {
52 let len = self.length();
53 if len > 1e-6 {
54 self / len
55 } else {
56 Self::ZERO
57 }
58 }
59
60 #[inline(always)]
61 pub fn lerp(self, other: Self, t: f32) -> Self {
62 self + (other - self) * t
63 }
64}
65
66impl Add for Vec3 {
67 type Output = Self;
68 #[inline(always)]
69 fn add(self, rhs: Self) -> Self::Output {
70 Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
71 }
72}
73
74impl Sub for Vec3 {
75 type Output = Self;
76 #[inline(always)]
77 fn sub(self, rhs: Self) -> Self::Output {
78 Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
79 }
80}
81
82impl Mul<f32> for Vec3 {
83 type Output = Self;
84 #[inline(always)]
85 fn mul(self, rhs: f32) -> Self::Output {
86 Self::new(self.x * rhs, self.y * rhs, self.z * rhs)
87 }
88}
89
90impl Div<f32> for Vec3 {
91 type Output = Self;
92 #[inline(always)]
93 fn div(self, rhs: f32) -> Self::Output {
94 let inv = 1.0 / rhs;
95 Self::new(self.x * inv, self.y * inv, self.z * inv)
96 }
97}
98
99impl Neg for Vec3 {
100 type Output = Self;
101 #[inline(always)]
102 fn neg(self) -> Self::Output {
103 Self::new(-self.x, -self.y, -self.z)
104 }
105}
106
107#[repr(C)]
109#[derive(Debug, Clone, Copy, PartialEq, Default)]
110pub struct Vec4 {
111 pub x: f32,
112 pub y: f32,
113 pub z: f32,
114 pub w: f32,
115}
116
117impl Vec4 {
118 pub const ZERO: Self = Self::new(0.0, 0.0, 0.0, 0.0);
119
120 #[inline(always)]
121 pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
122 Self { x, y, z, w }
123 }
124
125 #[inline(always)]
126 pub const fn from_vec3(v: Vec3, w: f32) -> Self {
127 Self {
128 x: v.x,
129 y: v.y,
130 z: v.z,
131 w,
132 }
133 }
134
135 #[inline(always)]
136 pub const fn xyz(self) -> Vec3 {
137 Vec3::new(self.x, self.y, self.z)
138 }
139
140 #[inline(always)]
141 pub fn dot(self, other: Self) -> f32 {
142 self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
143 }
144}
145
146impl Add for Vec4 {
147 type Output = Self;
148 #[inline(always)]
149 fn add(self, rhs: Self) -> Self::Output {
150 Self::new(
151 self.x + rhs.x,
152 self.y + rhs.y,
153 self.z + rhs.z,
154 self.w + rhs.w,
155 )
156 }
157}
158
159impl Sub for Vec4 {
160 type Output = Self;
161 #[inline(always)]
162 fn sub(self, rhs: Self) -> Self::Output {
163 Self::new(
164 self.x - rhs.x,
165 self.y - rhs.y,
166 self.z - rhs.z,
167 self.w - rhs.w,
168 )
169 }
170}
171
172impl Mul<f32> for Vec4 {
173 type Output = Self;
174 #[inline(always)]
175 fn mul(self, rhs: f32) -> Self::Output {
176 Self::new(self.x * rhs, self.y * rhs, self.z * rhs, self.w * rhs)
177 }
178}
179
180impl Div<f32> for Vec4 {
181 type Output = Self;
182 #[inline(always)]
183 fn div(self, rhs: f32) -> Self::Output {
184 let inv = 1.0 / rhs;
185 Self::new(self.x * inv, self.y * inv, self.z * inv, self.w * inv)
186 }
187}
188
189#[repr(C)]
191#[derive(Debug, Clone, Copy, PartialEq)]
192pub struct Mat4 {
193 pub m: [f32; 16],
199}
200
201impl Default for Mat4 {
202 fn default() -> Self {
203 Self::IDENTITY
204 }
205}
206
207impl Mat4 {
208 pub const IDENTITY: Self = Self {
209 m: [
210 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, ],
215 };
216
217 pub const fn from_cols(c0: Vec4, c1: Vec4, c2: Vec4, c3: Vec4) -> Self {
218 Self {
219 m: [
220 c0.x, c0.y, c0.z, c0.w, c1.x, c1.y, c1.z, c1.w, c2.x, c2.y, c2.z, c2.w, c3.x, c3.y,
221 c3.z, c3.w,
222 ],
223 }
224 }
225
226 pub fn from_translation(v: Vec3) -> Self {
227 let mut mat = Self::IDENTITY;
228 mat.m[12] = v.x;
229 mat.m[13] = v.y;
230 mat.m[14] = v.z;
231 mat
232 }
233
234 pub fn from_scale(s: Vec3) -> Self {
235 let mut mat = Self::IDENTITY;
236 mat.m[0] = s.x;
237 mat.m[5] = s.y;
238 mat.m[10] = s.z;
239 mat
240 }
241
242 pub fn from_rotation_x(radians: f32) -> Self {
243 let (sin, cos) = radians.sin_cos();
244 let mut mat = Self::IDENTITY;
245 mat.m[5] = cos;
246 mat.m[6] = sin;
247 mat.m[9] = -sin;
248 mat.m[10] = cos;
249 mat
250 }
251
252 pub fn from_rotation_y(radians: f32) -> Self {
253 let (sin, cos) = radians.sin_cos();
254 let mut mat = Self::IDENTITY;
255 mat.m[0] = cos;
256 mat.m[2] = -sin;
257 mat.m[8] = sin;
258 mat.m[10] = cos;
259 mat
260 }
261
262 pub fn from_rotation_z(radians: f32) -> Self {
263 let (sin, cos) = radians.sin_cos();
264 let mut mat = Self::IDENTITY;
265 mat.m[0] = cos;
266 mat.m[1] = sin;
267 mat.m[4] = -sin;
268 mat.m[5] = cos;
269 mat
270 }
271
272 pub fn from_euler(rx: f32, ry: f32, rz: f32) -> Self {
273 let mx = Self::from_rotation_x(rx);
275 let my = Self::from_rotation_y(ry);
276 let mz = Self::from_rotation_z(rz);
277 my.multiply(&mz).multiply(&mx)
278 }
279
280 pub fn from_trs(translation: Vec3, rotation: Vec3, scale: Vec3) -> Self {
281 let t = Self::from_translation(translation);
282 let r = Self::from_euler(rotation.x, rotation.y, rotation.z);
283 let s = Self::from_scale(scale);
284 t.multiply(&r).multiply(&s)
285 }
286
287 pub fn multiply(&self, rhs: &Self) -> Self {
288 let mut out = [0.0f32; 16];
289 for col in 0..4 {
290 for row in 0..4 {
291 let mut sum = 0.0f32;
292 for k in 0..4 {
293 sum += self.m[k * 4 + row] * rhs.m[col * 4 + k];
294 }
295 out[col * 4 + row] = sum;
296 }
297 }
298 Self { m: out }
299 }
300
301 pub fn transform_vec4(&self, v: Vec4) -> Vec4 {
302 Vec4::new(
303 self.m[0] * v.x + self.m[4] * v.y + self.m[8] * v.z + self.m[12] * v.w,
304 self.m[1] * v.x + self.m[5] * v.y + self.m[9] * v.z + self.m[13] * v.w,
305 self.m[2] * v.x + self.m[6] * v.y + self.m[10] * v.z + self.m[14] * v.w,
306 self.m[3] * v.x + self.m[7] * v.y + self.m[11] * v.z + self.m[15] * v.w,
307 )
308 }
309
310 pub fn transform_point(&self, p: Vec3) -> Vec3 {
311 let v = self.transform_vec4(Vec4::from_vec3(p, 1.0));
312 if v.w.abs() > 1e-6 {
313 Vec3::new(v.x / v.w, v.y / v.w, v.z / v.w)
314 } else {
315 Vec3::new(v.x, v.y, v.z)
316 }
317 }
318
319 pub fn transform_vector(&self, v: Vec3) -> Vec3 {
320 let res = self.transform_vec4(Vec4::from_vec3(v, 0.0));
321 Vec3::new(res.x, res.y, res.z)
322 }
323
324 pub fn transpose(&self) -> Self {
325 let mut out = [0.0f32; 16];
326 for r in 0..4 {
327 for c in 0..4 {
328 out[c * 4 + r] = self.m[r * 4 + c];
329 }
330 }
331 Self { m: out }
332 }
333
334 pub fn determinant(&self) -> f32 {
335 let m = self.m;
336 let s0 = m[0] * m[5] - m[1] * m[4];
337 let s1 = m[0] * m[6] - m[2] * m[4];
338 let s2 = m[0] * m[7] - m[3] * m[4];
339 let s3 = m[1] * m[6] - m[2] * m[5];
340 let s4 = m[1] * m[7] - m[3] * m[5];
341 let s5 = m[2] * m[7] - m[3] * m[6];
342
343 let c5 = m[10] * m[15] - m[11] * m[14];
344 let c4 = m[9] * m[15] - m[11] * m[13];
345 let c3 = m[9] * m[14] - m[10] * m[13];
346 let c2 = m[8] * m[15] - m[11] * m[12];
347 let c1 = m[8] * m[14] - m[10] * m[12];
348 let c0 = m[8] * m[13] - m[9] * m[12];
349
350 s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0
351 }
352
353 pub fn inverse(&self) -> Option<Self> {
354 let det = self.determinant();
355 if det.abs() < 1e-7 {
356 return None;
357 }
358
359 let inv_det = 1.0 / det;
360 let m = self.m;
361 let mut inv = [0.0f32; 16];
362
363 inv[0] = (m[5] * (m[10] * m[15] - m[11] * m[14]) - m[9] * (m[6] * m[15] - m[7] * m[14])
364 + m[13] * (m[6] * m[11] - m[7] * m[10]))
365 * inv_det;
366 inv[1] = -(m[1] * (m[10] * m[15] - m[11] * m[14]) - m[9] * (m[2] * m[15] - m[3] * m[14])
367 + m[13] * (m[2] * m[11] - m[3] * m[10]))
368 * inv_det;
369 inv[2] = (m[1] * (m[6] * m[15] - m[7] * m[14]) - m[5] * (m[2] * m[15] - m[3] * m[14])
370 + m[13] * (m[2] * m[7] - m[3] * m[6]))
371 * inv_det;
372 inv[3] = -(m[1] * (m[6] * m[11] - m[7] * m[10]) - m[5] * (m[2] * m[11] - m[3] * m[10])
373 + m[9] * (m[2] * m[7] - m[3] * m[6]))
374 * inv_det;
375
376 inv[4] = -(m[4] * (m[10] * m[15] - m[11] * m[14]) - m[8] * (m[6] * m[15] - m[7] * m[14])
377 + m[12] * (m[6] * m[11] - m[7] * m[10]))
378 * inv_det;
379 inv[5] = (m[0] * (m[10] * m[15] - m[11] * m[14]) - m[8] * (m[2] * m[15] - m[3] * m[14])
380 + m[12] * (m[2] * m[11] - m[3] * m[10]))
381 * inv_det;
382 inv[6] = -(m[0] * (m[6] * m[15] - m[7] * m[14]) - m[4] * (m[2] * m[15] - m[3] * m[14])
383 + m[12] * (m[2] * m[7] - m[3] * m[6]))
384 * inv_det;
385 inv[7] = (m[0] * (m[6] * m[11] - m[7] * m[10]) - m[4] * (m[2] * m[11] - m[3] * m[10])
386 + m[8] * (m[2] * m[7] - m[3] * m[6]))
387 * inv_det;
388
389 inv[8] = (m[4] * (m[9] * m[15] - m[11] * m[13]) - m[8] * (m[5] * m[15] - m[7] * m[13])
390 + m[12] * (m[5] * m[11] - m[7] * m[9]))
391 * inv_det;
392 inv[9] = -(m[0] * (m[9] * m[15] - m[11] * m[13]) - m[8] * (m[1] * m[15] - m[3] * m[13])
393 + m[12] * (m[1] * m[11] - m[3] * m[9]))
394 * inv_det;
395 inv[10] = (m[0] * (m[5] * m[15] - m[7] * m[13]) - m[4] * (m[1] * m[15] - m[3] * m[13])
396 + m[12] * (m[1] * m[7] - m[3] * m[5]))
397 * inv_det;
398 inv[11] = -(m[0] * (m[5] * m[11] - m[7] * m[9]) - m[4] * (m[1] * m[11] - m[3] * m[9])
399 + m[8] * (m[1] * m[7] - m[3] * m[5]))
400 * inv_det;
401
402 inv[12] = -(m[4] * (m[9] * m[14] - m[10] * m[13]) - m[8] * (m[5] * m[14] - m[6] * m[13])
403 + m[12] * (m[5] * m[10] - m[6] * m[9]))
404 * inv_det;
405 inv[13] = (m[0] * (m[9] * m[14] - m[10] * m[13]) - m[8] * (m[1] * m[14] - m[2] * m[13])
406 + m[12] * (m[1] * m[10] - m[2] * m[9]))
407 * inv_det;
408 inv[14] = -(m[0] * (m[5] * m[14] - m[6] * m[13]) - m[4] * (m[1] * m[14] - m[2] * m[13])
409 + m[12] * (m[1] * m[6] - m[2] * m[5]))
410 * inv_det;
411 inv[15] = (m[0] * (m[5] * m[10] - m[6] * m[9]) - m[4] * (m[1] * m[10] - m[2] * m[9])
412 + m[8] * (m[1] * m[6] - m[2] * m[5]))
413 * inv_det;
414
415 Some(Self { m: inv })
416 }
417
418 pub fn look_at(eye: Vec3, target: Vec3, up: Vec3) -> Self {
420 let f = (target - eye).normalize();
421 let s = f.cross(up).normalize();
422 let u = s.cross(f);
423
424 Self {
425 m: [
426 s.x,
427 u.x,
428 -f.x,
429 0.0,
430 s.y,
431 u.y,
432 -f.y,
433 0.0,
434 s.z,
435 u.z,
436 -f.z,
437 0.0,
438 -s.dot(eye),
439 -u.dot(eye),
440 f.dot(eye),
441 1.0,
442 ],
443 }
444 }
445
446 pub fn perspective(fov_y_rad: f32, aspect: f32, near: f32, far: f32) -> Self {
448 let f = 1.0 / (fov_y_rad * 0.5).tan();
449 let range_inv = 1.0 / (near - far);
450
451 Self {
452 m: [
453 f / aspect,
454 0.0,
455 0.0,
456 0.0,
457 0.0,
458 f,
459 0.0,
460 0.0,
461 0.0,
462 0.0,
463 far * range_inv,
464 -1.0,
465 0.0,
466 0.0,
467 near * far * range_inv,
468 0.0,
469 ],
470 }
471 }
472
473 pub fn orthographic(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Self {
475 let r_l = 1.0 / (right - left);
476 let t_b = 1.0 / (top - bottom);
477 let f_n = 1.0 / (near - far);
478
479 Self {
480 m: [
481 2.0 * r_l,
482 0.0,
483 0.0,
484 0.0,
485 0.0,
486 2.0 * t_b,
487 0.0,
488 0.0,
489 0.0,
490 0.0,
491 f_n,
492 0.0,
493 -(right + left) * r_l,
494 -(top + bottom) * t_b,
495 near * f_n,
496 1.0,
497 ],
498 }
499 }
500}
501
502#[derive(Debug, Clone, Copy, PartialEq)]
504pub struct Ray3D {
505 pub origin: Vec3,
506 pub direction: Vec3,
507}
508
509impl Ray3D {
510 pub fn new(origin: Vec3, direction: Vec3) -> Self {
511 Self {
512 origin,
513 direction: direction.normalize(),
514 }
515 }
516
517 pub fn unproject(
519 screen_x: f32,
520 screen_y: f32,
521 viewport_w: f32,
522 viewport_h: f32,
523 inv_proj: &Mat4,
524 inv_view: &Mat4,
525 ) -> Self {
526 let ndc_x = (2.0 * screen_x / viewport_w) - 1.0;
528 let ndc_y = 1.0 - (2.0 * screen_y / viewport_h);
529
530 let eye = inv_view.transform_point(Vec3::ZERO);
532
533 let far_ndc = Vec4::new(ndc_x, ndc_y, 1.0, 1.0);
535 let far_view = inv_proj.transform_vec4(far_ndc);
536 let far_world = inv_view.transform_vec4(far_view);
537 let p_far = far_world.xyz() / far_world.w;
538
539 let dir = (p_far - eye).normalize();
540 Self::new(eye, dir)
541 }
542}