Skip to main content

three_rs/math/
matrix4.rs

1//! Port of `three.js/src/math/Matrix4.js` (rung 1 subset).
2//!
3//! Column-major, same element order as three.js (`elements[0..4]` is the first
4//! column), so the `f32` narrowing at upload time is a straight copy and
5//! matches what WebGPURenderer writes into its uniform buffers.
6
7use super::{Quaternion, Vector3};
8
9/// three.js' `WebGLCoordinateSystem` / `WebGPUCoordinateSystem`: clip space
10/// depth is -1..1 for the former and 0..1 for the latter. `Matrix4`'s
11/// projection builders default to `WebGl`; `WebGPURenderer` always passes
12/// `WebGpu`.
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
14pub enum CoordinateSystem {
15    /// `WebGLCoordinateSystem`.
16    #[default]
17    WebGl,
18    /// `WebGPUCoordinateSystem`.
19    WebGpu,
20}
21
22/// Port of three.js' `Matrix4`: a 4x4 matrix.
23#[derive(Clone, Copy, Debug, PartialEq)]
24pub struct Matrix4 {
25    /// A column-major list of matrix values.
26    pub elements: [f64; 16],
27}
28
29impl Default for Matrix4 {
30    fn default() -> Self {
31        Self::identity()
32    }
33}
34
35impl Matrix4 {
36    /// The identity matrix.
37    pub const fn identity() -> Self {
38        Self {
39            elements: [
40                1.0, 0.0, 0.0, 0.0, //
41                0.0, 1.0, 0.0, 0.0, //
42                0.0, 0.0, 1.0, 0.0, //
43                0.0, 0.0, 0.0, 1.0,
44            ],
45        }
46    }
47
48    /// The 16 elements narrowed to `f32`, ready for a uniform buffer.
49    #[allow(clippy::wrong_self_convention)] // a 128-byte matrix is cheaper borrowed
50    pub(crate) fn to_f32_array(&self) -> [f32; 16] {
51        let mut out = [0.0f32; 16];
52        for (o, e) in out.iter_mut().zip(self.elements.iter()) {
53            *o = *e as f32;
54        }
55        out
56    }
57
58    /// `Matrix4.makeScale()`.
59    pub fn make_scale(&mut self, x: f64, y: f64, z: f64) -> &mut Self {
60        self.elements = [
61            x, 0.0, 0.0, 0.0, //
62            0.0, y, 0.0, 0.0, //
63            0.0, 0.0, z, 0.0, //
64            0.0, 0.0, 0.0, 1.0,
65        ];
66        self
67    }
68
69    /// `Matrix4.compose()`.
70    pub fn compose(
71        &mut self,
72        position: &Vector3,
73        quaternion: &Quaternion,
74        scale: &Vector3,
75    ) -> &mut Self {
76        let te = &mut self.elements;
77
78        let (x, y, z, w) = (quaternion.x, quaternion.y, quaternion.z, quaternion.w);
79        let (x2, y2, z2) = (x + x, y + y, z + z);
80        let (xx, xy, xz) = (x * x2, x * y2, x * z2);
81        let (yy, yz, zz) = (y * y2, y * z2, z * z2);
82        let (wx, wy, wz) = (w * x2, w * y2, w * z2);
83
84        let (sx, sy, sz) = (scale.x, scale.y, scale.z);
85
86        te[0] = (1.0 - (yy + zz)) * sx;
87        te[1] = (xy + wz) * sx;
88        te[2] = (xz - wy) * sx;
89        te[3] = 0.0;
90
91        te[4] = (xy - wz) * sy;
92        te[5] = (1.0 - (xx + zz)) * sy;
93        te[6] = (yz + wx) * sy;
94        te[7] = 0.0;
95
96        te[8] = (xz + wy) * sz;
97        te[9] = (yz - wx) * sz;
98        te[10] = (1.0 - (xx + yy)) * sz;
99        te[11] = 0.0;
100
101        te[12] = position.x;
102        te[13] = position.y;
103        te[14] = position.z;
104        te[15] = 1.0;
105
106        self
107    }
108
109    /// `Matrix4.lookAt()`: writes only the upper 3x3 rotation.
110    pub fn look_at(&mut self, eye: &Vector3, target: &Vector3, up: &Vector3) -> &mut Self {
111        let mut z = Vector3::ZERO;
112        let mut x = Vector3::ZERO;
113        let mut y = Vector3::ZERO;
114
115        z.sub_vectors(eye, target);
116
117        if z.length_sq() == 0.0 {
118            z.z = 1.0;
119        }
120
121        z.normalize();
122        x.cross_vectors(up, &z);
123
124        if x.length_sq() == 0.0 {
125            // up and z are parallel
126            if up.z.abs() == 1.0 {
127                z.x += 0.0001;
128            } else {
129                z.z += 0.0001;
130            }
131
132            z.normalize();
133            x.cross_vectors(up, &z);
134        }
135
136        x.normalize();
137        y.cross_vectors(&z, &x);
138
139        let te = &mut self.elements;
140        te[0] = x.x;
141        te[4] = y.x;
142        te[8] = z.x;
143        te[1] = x.y;
144        te[5] = y.y;
145        te[9] = z.y;
146        te[2] = x.z;
147        te[6] = y.z;
148        te[10] = z.z;
149
150        self
151    }
152
153    /// `Matrix4.multiplyMatrices()`.
154    pub fn multiply_matrices(&mut self, a: &Self, b: &Self) -> &mut Self {
155        let ae = &a.elements;
156        let be = &b.elements;
157
158        let (a11, a12, a13, a14) = (ae[0], ae[4], ae[8], ae[12]);
159        let (a21, a22, a23, a24) = (ae[1], ae[5], ae[9], ae[13]);
160        let (a31, a32, a33, a34) = (ae[2], ae[6], ae[10], ae[14]);
161        let (a41, a42, a43, a44) = (ae[3], ae[7], ae[11], ae[15]);
162
163        let (b11, b12, b13, b14) = (be[0], be[4], be[8], be[12]);
164        let (b21, b22, b23, b24) = (be[1], be[5], be[9], be[13]);
165        let (b31, b32, b33, b34) = (be[2], be[6], be[10], be[14]);
166        let (b41, b42, b43, b44) = (be[3], be[7], be[11], be[15]);
167
168        let te = &mut self.elements;
169
170        te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
171        te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
172        te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
173        te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
174
175        te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
176        te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
177        te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
178        te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
179
180        te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
181        te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
182        te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
183        te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
184
185        te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
186        te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
187        te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
188        te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
189
190        self
191    }
192
193    /// `Matrix4.invert()`.
194    pub fn invert(&mut self) -> &mut Self {
195        let te = &mut self.elements;
196
197        let (n11, n21, n31, n41) = (te[0], te[1], te[2], te[3]);
198        let (n12, n22, n32, n42) = (te[4], te[5], te[6], te[7]);
199        let (n13, n23, n33, n43) = (te[8], te[9], te[10], te[11]);
200        let (n14, n24, n34, n44) = (te[12], te[13], te[14], te[15]);
201
202        let t1 = n11 * n22 - n21 * n12;
203        let t2 = n11 * n32 - n31 * n12;
204        let t3 = n11 * n42 - n41 * n12;
205        let t4 = n21 * n32 - n31 * n22;
206        let t5 = n21 * n42 - n41 * n22;
207        let t6 = n31 * n42 - n41 * n32;
208        let t7 = n13 * n24 - n23 * n14;
209        let t8 = n13 * n34 - n33 * n14;
210        let t9 = n13 * n44 - n43 * n14;
211        let t10 = n23 * n34 - n33 * n24;
212        let t11 = n23 * n44 - n43 * n24;
213        let t12 = n33 * n44 - n43 * n34;
214
215        let det = t1 * t12 - t2 * t11 + t3 * t10 + t4 * t9 - t5 * t8 + t6 * t7;
216
217        if det == 0.0 {
218            *te = [0.0; 16];
219            return self;
220        }
221
222        let det_inv = 1.0 / det;
223
224        te[0] = (n22 * t12 - n32 * t11 + n42 * t10) * det_inv;
225        te[1] = (n31 * t11 - n21 * t12 - n41 * t10) * det_inv;
226        te[2] = (n24 * t6 - n34 * t5 + n44 * t4) * det_inv;
227        te[3] = (n33 * t5 - n23 * t6 - n43 * t4) * det_inv;
228
229        te[4] = (n32 * t9 - n12 * t12 - n42 * t8) * det_inv;
230        te[5] = (n11 * t12 - n31 * t9 + n41 * t8) * det_inv;
231        te[6] = (n34 * t3 - n14 * t6 - n44 * t2) * det_inv;
232        te[7] = (n13 * t6 - n33 * t3 + n43 * t2) * det_inv;
233
234        te[8] = (n12 * t11 - n22 * t9 + n42 * t7) * det_inv;
235        te[9] = (n21 * t9 - n11 * t11 - n41 * t7) * det_inv;
236        te[10] = (n14 * t5 - n24 * t3 + n44 * t1) * det_inv;
237        te[11] = (n23 * t3 - n13 * t5 - n43 * t1) * det_inv;
238
239        te[12] = (n22 * t8 - n12 * t10 - n32 * t7) * det_inv;
240        te[13] = (n11 * t10 - n21 * t8 + n31 * t7) * det_inv;
241        te[14] = (n24 * t2 - n14 * t4 - n34 * t1) * det_inv;
242        te[15] = (n13 * t4 - n23 * t2 + n33 * t1) * det_inv;
243
244        self
245    }
246
247    /// `Matrix4.makePerspective()`, `reversedDepth = false`.
248    #[allow(clippy::too_many_arguments)] // mirrors three.js's `makePerspective(left, right, top, bottom, near, far, coordinateSystem)`; public API, batched separately (#9/#38/#39)
249    pub fn make_perspective(
250        &mut self,
251        left: f64,
252        right: f64,
253        top: f64,
254        bottom: f64,
255        near: f64,
256        far: f64,
257        coordinate_system: CoordinateSystem,
258    ) -> &mut Self {
259        let x = 2.0 * near / (right - left);
260        let y = 2.0 * near / (top - bottom);
261
262        let a = (right + left) / (right - left);
263        let b = (top + bottom) / (top - bottom);
264
265        let (c, d) = match coordinate_system {
266            CoordinateSystem::WebGl => (
267                -(far + near) / (far - near),
268                (-2.0 * far * near) / (far - near),
269            ),
270            CoordinateSystem::WebGpu => (-far / (far - near), (-far * near) / (far - near)),
271        };
272
273        let te = &mut self.elements;
274        te[0] = x;
275        te[4] = 0.0;
276        te[8] = a;
277        te[12] = 0.0;
278        te[1] = 0.0;
279        te[5] = y;
280        te[9] = b;
281        te[13] = 0.0;
282        te[2] = 0.0;
283        te[6] = 0.0;
284        te[10] = c;
285        te[14] = d;
286        te[3] = 0.0;
287        te[7] = 0.0;
288        te[11] = -1.0;
289        te[15] = 0.0;
290
291        self
292    }
293
294    /// `Matrix4.set( n11, n12, ... )` — row-order arguments into a
295    /// column-major store.
296    #[allow(clippy::too_many_arguments)]
297    pub fn set(
298        &mut self,
299        n11: f64,
300        n12: f64,
301        n13: f64,
302        n14: f64,
303        n21: f64,
304        n22: f64,
305        n23: f64,
306        n24: f64,
307        n31: f64,
308        n32: f64,
309        n33: f64,
310        n34: f64,
311        n41: f64,
312        n42: f64,
313        n43: f64,
314        n44: f64,
315    ) -> &mut Self {
316        self.elements = [
317            n11, n21, n31, n41, //
318            n12, n22, n32, n42, //
319            n13, n23, n33, n43, //
320            n14, n24, n34, n44,
321        ];
322        self
323    }
324
325    /// `new Matrix4( n11, ... )`.
326    #[allow(clippy::too_many_arguments)]
327    pub fn from_rows(
328        n11: f64,
329        n12: f64,
330        n13: f64,
331        n14: f64,
332        n21: f64,
333        n22: f64,
334        n23: f64,
335        n24: f64,
336        n31: f64,
337        n32: f64,
338        n33: f64,
339        n34: f64,
340        n41: f64,
341        n42: f64,
342        n43: f64,
343        n44: f64,
344    ) -> Self {
345        let mut m = Self::identity();
346        m.set(
347            n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44,
348        );
349        m
350    }
351
352    /// `Matrix4.identity()`.
353    pub fn set_identity(&mut self) -> &mut Self {
354        self.elements = Self::identity().elements;
355        self
356    }
357
358    /// `Matrix4.copy()`.
359    pub fn copy(&mut self, m: &Self) -> &mut Self {
360        self.elements = m.elements;
361        self
362    }
363
364    /// `Matrix4.copyPosition()`.
365    pub fn copy_position(&mut self, m: &Self) -> &mut Self {
366        let (te, me) = (&mut self.elements, &m.elements);
367        te[12] = me[12];
368        te[13] = me[13];
369        te[14] = me[14];
370        self
371    }
372
373    /// `Matrix4.setFromMatrix3()`.
374    pub fn set_from_matrix3(&mut self, m: &crate::math::Matrix3) -> &mut Self {
375        let me = m.elements;
376        self.set(
377            me[0], me[3], me[6], 0.0, //
378            me[1], me[4], me[7], 0.0, //
379            me[2], me[5], me[8], 0.0, //
380            0.0, 0.0, 0.0, 1.0,
381        )
382    }
383
384    /// `Matrix4.extractBasis()`.
385    pub fn extract_basis(&self, x_axis: &mut Vector3, y_axis: &mut Vector3, z_axis: &mut Vector3) {
386        if self.determinant_affine() == 0.0 {
387            x_axis.set(1.0, 0.0, 0.0);
388            y_axis.set(0.0, 1.0, 0.0);
389            z_axis.set(0.0, 0.0, 1.0);
390            return;
391        }
392
393        x_axis.set_from_matrix_column(self, 0);
394        y_axis.set_from_matrix_column(self, 1);
395        z_axis.set_from_matrix_column(self, 2);
396    }
397
398    /// `Matrix4.makeBasis()`.
399    pub fn make_basis(
400        &mut self,
401        x_axis: &Vector3,
402        y_axis: &Vector3,
403        z_axis: &Vector3,
404    ) -> &mut Self {
405        self.set(
406            x_axis.x, y_axis.x, z_axis.x, 0.0, //
407            x_axis.y, y_axis.y, z_axis.y, 0.0, //
408            x_axis.z, y_axis.z, z_axis.z, 0.0, //
409            0.0, 0.0, 0.0, 1.0,
410        )
411    }
412
413    /// `Matrix4.extractRotation()`: the upper 3x3 of `m` with its scale divided
414    /// out. A singular matrix yields the identity.
415    pub fn extract_rotation(&mut self, m: &Self) -> &mut Self {
416        if m.determinant_affine() == 0.0 {
417            return self.set_identity();
418        }
419
420        let mut v1 = Vector3::ZERO;
421
422        let scale_x = 1.0 / v1.set_from_matrix_column(m, 0).length();
423        let scale_y = 1.0 / v1.set_from_matrix_column(m, 1).length();
424        let scale_z = 1.0 / v1.set_from_matrix_column(m, 2).length();
425
426        let me = m.elements;
427        let te = &mut self.elements;
428
429        te[0] = me[0] * scale_x;
430        te[1] = me[1] * scale_x;
431        te[2] = me[2] * scale_x;
432        te[3] = 0.0;
433
434        te[4] = me[4] * scale_y;
435        te[5] = me[5] * scale_y;
436        te[6] = me[6] * scale_y;
437        te[7] = 0.0;
438
439        te[8] = me[8] * scale_z;
440        te[9] = me[9] * scale_z;
441        te[10] = me[10] * scale_z;
442        te[11] = 0.0;
443
444        te[12] = 0.0;
445        te[13] = 0.0;
446        te[14] = 0.0;
447        te[15] = 1.0;
448
449        self
450    }
451
452    /// `Matrix4.makeRotationFromEuler()`.
453    pub fn make_rotation_from_euler(&mut self, euler: &crate::math::Euler) -> &mut Self {
454        use crate::math::EulerOrder;
455
456        let (x, y, z) = (euler.x, euler.y, euler.z);
457        let (a, b) = (x.cos(), x.sin());
458        let (c, d) = (y.cos(), y.sin());
459        let (e, f) = (z.cos(), z.sin());
460
461        let te = &mut self.elements;
462
463        match euler.order {
464            EulerOrder::XYZ => {
465                let (ae, af, be, bf) = (a * e, a * f, b * e, b * f);
466
467                te[0] = c * e;
468                te[4] = -c * f;
469                te[8] = d;
470
471                te[1] = af + be * d;
472                te[5] = ae - bf * d;
473                te[9] = -b * c;
474
475                te[2] = bf - ae * d;
476                te[6] = be + af * d;
477                te[10] = a * c;
478            }
479            EulerOrder::YXZ => {
480                let (ce, cf, de, df) = (c * e, c * f, d * e, d * f);
481
482                te[0] = ce + df * b;
483                te[4] = de * b - cf;
484                te[8] = a * d;
485
486                te[1] = a * f;
487                te[5] = a * e;
488                te[9] = -b;
489
490                te[2] = cf * b - de;
491                te[6] = df + ce * b;
492                te[10] = a * c;
493            }
494            EulerOrder::ZXY => {
495                let (ce, cf, de, df) = (c * e, c * f, d * e, d * f);
496
497                te[0] = ce - df * b;
498                te[4] = -a * f;
499                te[8] = de + cf * b;
500
501                te[1] = cf + de * b;
502                te[5] = a * e;
503                te[9] = df - ce * b;
504
505                te[2] = -a * d;
506                te[6] = b;
507                te[10] = a * c;
508            }
509            EulerOrder::ZYX => {
510                let (ae, af, be, bf) = (a * e, a * f, b * e, b * f);
511
512                te[0] = c * e;
513                te[4] = be * d - af;
514                te[8] = ae * d + bf;
515
516                te[1] = c * f;
517                te[5] = bf * d + ae;
518                te[9] = af * d - be;
519
520                te[2] = -d;
521                te[6] = b * c;
522                te[10] = a * c;
523            }
524            EulerOrder::YZX => {
525                let (ac, ad, bc, bd) = (a * c, a * d, b * c, b * d);
526
527                te[0] = c * e;
528                te[4] = bd - ac * f;
529                te[8] = bc * f + ad;
530
531                te[1] = f;
532                te[5] = a * e;
533                te[9] = -b * e;
534
535                te[2] = -d * e;
536                te[6] = ad * f + bc;
537                te[10] = ac - bd * f;
538            }
539            EulerOrder::XZY => {
540                let (ac, ad, bc, bd) = (a * c, a * d, b * c, b * d);
541
542                te[0] = c * e;
543                te[4] = -f;
544                te[8] = d * e;
545
546                te[1] = ac * f + bd;
547                te[5] = a * e;
548                te[9] = ad * f - bc;
549
550                te[2] = bc * f - ad;
551                te[6] = b * e;
552                te[10] = bd * f + ac;
553            }
554        }
555
556        // bottom row
557        te[3] = 0.0;
558        te[7] = 0.0;
559        te[11] = 0.0;
560
561        // last column
562        te[12] = 0.0;
563        te[13] = 0.0;
564        te[14] = 0.0;
565        te[15] = 1.0;
566
567        self
568    }
569
570    /// `Matrix4.makeRotationFromQuaternion()`.
571    pub fn make_rotation_from_quaternion(&mut self, q: &Quaternion) -> &mut Self {
572        self.compose(&Vector3::ZERO, q, &Vector3::new(1.0, 1.0, 1.0))
573    }
574
575    /// `Matrix4.multiply()`.
576    pub fn multiply(&mut self, m: &Self) -> &mut Self {
577        let a = *self;
578        self.multiply_matrices(&a, m)
579    }
580
581    /// `Matrix4.premultiply()`.
582    pub fn premultiply(&mut self, m: &Self) -> &mut Self {
583        let b = *self;
584        self.multiply_matrices(m, &b)
585    }
586
587    /// `Matrix4.multiplyScalar()`.
588    pub fn multiply_scalar(&mut self, s: f64) -> &mut Self {
589        for e in self.elements.iter_mut() {
590            *e *= s;
591        }
592        self
593    }
594
595    /// `Matrix4.determinant()`.
596    pub fn determinant(&self) -> f64 {
597        let te = &self.elements;
598
599        let (n11, n12, n13, n14) = (te[0], te[4], te[8], te[12]);
600        let (n21, n22, n23, n24) = (te[1], te[5], te[9], te[13]);
601        let (n31, n32, n33, n34) = (te[2], te[6], te[10], te[14]);
602        let (n41, n42, n43, n44) = (te[3], te[7], te[11], te[15]);
603
604        let t11 = n23 * n34 - n24 * n33;
605        let t12 = n22 * n34 - n24 * n32;
606        let t13 = n22 * n33 - n23 * n32;
607        let t21 = n21 * n34 - n24 * n31;
608        let t22 = n21 * n33 - n23 * n31;
609        let t23 = n21 * n32 - n22 * n31;
610
611        n11 * (n42 * t11 - n43 * t12 + n44 * t13) - n12 * (n41 * t11 - n43 * t21 + n44 * t22)
612            + n13 * (n41 * t12 - n42 * t21 + n44 * t23)
613            - n14 * (n41 * t13 - n42 * t22 + n43 * t23)
614    }
615
616    /// `Matrix4.determinantAffine()` — the determinant of the upper 3x3.
617    pub fn determinant_affine(&self) -> f64 {
618        let te = &self.elements;
619
620        let (n11, n12, n13) = (te[0], te[4], te[8]);
621        let (n21, n22, n23) = (te[1], te[5], te[9]);
622        let (n31, n32, n33) = (te[2], te[6], te[10]);
623
624        n11 * (n22 * n33 - n23 * n32) - n12 * (n21 * n33 - n23 * n31)
625            + n13 * (n21 * n32 - n22 * n31)
626    }
627
628    /// `Matrix4.transpose()`.
629    pub fn transpose(&mut self) -> &mut Self {
630        let te = &mut self.elements;
631        te.swap(1, 4);
632        te.swap(2, 8);
633        te.swap(6, 9);
634        te.swap(3, 12);
635        te.swap(7, 13);
636        te.swap(11, 14);
637        self
638    }
639
640    /// `Matrix4.setPosition()`.
641    pub fn set_position(&mut self, x: f64, y: f64, z: f64) -> &mut Self {
642        let te = &mut self.elements;
643        te[12] = x;
644        te[13] = y;
645        te[14] = z;
646        self
647    }
648
649    /// `Matrix4.scale( v )`.
650    pub fn scale(&mut self, v: &Vector3) -> &mut Self {
651        let (x, y, z) = (v.x, v.y, v.z);
652        let te = &mut self.elements;
653
654        te[0] *= x;
655        te[4] *= y;
656        te[8] *= z;
657        te[1] *= x;
658        te[5] *= y;
659        te[9] *= z;
660        te[2] *= x;
661        te[6] *= y;
662        te[10] *= z;
663        te[3] *= x;
664        te[7] *= y;
665        te[11] *= z;
666
667        self
668    }
669
670    /// `Matrix4.getMaxScaleOnAxis()`.
671    pub fn get_max_scale_on_axis(&self) -> f64 {
672        let te = &self.elements;
673
674        let scale_x_sq = te[0] * te[0] + te[1] * te[1] + te[2] * te[2];
675        let scale_y_sq = te[4] * te[4] + te[5] * te[5] + te[6] * te[6];
676        let scale_z_sq = te[8] * te[8] + te[9] * te[9] + te[10] * te[10];
677
678        let max = crate::math::math_utils::js_max(
679            crate::math::math_utils::js_max(scale_x_sq, scale_y_sq),
680            scale_z_sq,
681        );
682
683        max.sqrt()
684    }
685
686    /// `Matrix4.makeTranslation()`.
687    pub fn make_translation(&mut self, x: f64, y: f64, z: f64) -> &mut Self {
688        self.set(
689            1.0, 0.0, 0.0, x, //
690            0.0, 1.0, 0.0, y, //
691            0.0, 0.0, 1.0, z, //
692            0.0, 0.0, 0.0, 1.0,
693        )
694    }
695
696    /// `Matrix4.makeRotationX()`.
697    pub fn make_rotation_x(&mut self, theta: f64) -> &mut Self {
698        let (c, s) = (theta.cos(), theta.sin());
699
700        self.set(
701            1.0, 0.0, 0.0, 0.0, //
702            0.0, c, -s, 0.0, //
703            0.0, s, c, 0.0, //
704            0.0, 0.0, 0.0, 1.0,
705        )
706    }
707
708    /// `Matrix4.makeRotationY()`.
709    pub fn make_rotation_y(&mut self, theta: f64) -> &mut Self {
710        let (c, s) = (theta.cos(), theta.sin());
711
712        self.set(
713            c, 0.0, s, 0.0, //
714            0.0, 1.0, 0.0, 0.0, //
715            -s, 0.0, c, 0.0, //
716            0.0, 0.0, 0.0, 1.0,
717        )
718    }
719
720    /// `Matrix4.makeRotationZ()`.
721    pub fn make_rotation_z(&mut self, theta: f64) -> &mut Self {
722        let (c, s) = (theta.cos(), theta.sin());
723
724        self.set(
725            c, -s, 0.0, 0.0, //
726            s, c, 0.0, 0.0, //
727            0.0, 0.0, 1.0, 0.0, //
728            0.0, 0.0, 0.0, 1.0,
729        )
730    }
731
732    /// `Matrix4.makeRotationAxis()`. `axis` is assumed to be normalized.
733    pub fn make_rotation_axis(&mut self, axis: &Vector3, angle: f64) -> &mut Self {
734        let c = angle.cos();
735        let s = angle.sin();
736        let t = 1.0 - c;
737        let (x, y, z) = (axis.x, axis.y, axis.z);
738        let (tx, ty) = (t * x, t * y);
739
740        self.set(
741            tx * x + c,
742            tx * y - s * z,
743            tx * z + s * y,
744            0.0, //
745            tx * y + s * z,
746            ty * y + c,
747            ty * z - s * x,
748            0.0, //
749            tx * z - s * y,
750            ty * z + s * x,
751            t * z * z + c,
752            0.0, //
753            0.0,
754            0.0,
755            0.0,
756            1.0,
757        )
758    }
759
760    /// `Matrix4.makeShear()`.
761    pub fn make_shear(
762        &mut self,
763        xy: f64,
764        xz: f64,
765        yx: f64,
766        yz: f64,
767        zx: f64,
768        zy: f64,
769    ) -> &mut Self {
770        self.set(
771            1.0, yx, zx, 0.0, //
772            xy, 1.0, zy, 0.0, //
773            xz, yz, 1.0, 0.0, //
774            0.0, 0.0, 0.0, 1.0,
775        )
776    }
777
778    /// `Matrix4.decompose()`.
779    pub fn decompose(
780        &self,
781        position: &mut Vector3,
782        quaternion: &mut Quaternion,
783        scale: &mut Vector3,
784    ) {
785        let te = &self.elements;
786
787        position.x = te[12];
788        position.y = te[13];
789        position.z = te[14];
790
791        let det = self.determinant_affine();
792
793        if det == 0.0 {
794            scale.set(1.0, 1.0, 1.0);
795            quaternion.identity();
796            return;
797        }
798
799        let mut v1 = Vector3::ZERO;
800
801        let mut sx = v1.set(te[0], te[1], te[2]).length();
802        let sy = v1.set(te[4], te[5], te[6]).length();
803        let sz = v1.set(te[8], te[9], te[10]).length();
804
805        // if determinant is negative, we need to invert one scale
806        if det < 0.0 {
807            sx = -sx;
808        }
809
810        // scale the rotation part
811        let mut m1 = *self;
812
813        let inv_sx = 1.0 / sx;
814        let inv_sy = 1.0 / sy;
815        let inv_sz = 1.0 / sz;
816
817        m1.elements[0] *= inv_sx;
818        m1.elements[1] *= inv_sx;
819        m1.elements[2] *= inv_sx;
820
821        m1.elements[4] *= inv_sy;
822        m1.elements[5] *= inv_sy;
823        m1.elements[6] *= inv_sy;
824
825        m1.elements[8] *= inv_sz;
826        m1.elements[9] *= inv_sz;
827        m1.elements[10] *= inv_sz;
828
829        quaternion.set_from_rotation_matrix(&m1);
830
831        scale.x = sx;
832        scale.y = sy;
833        scale.z = sz;
834    }
835
836    /// `Matrix4.makeOrthographic()`, `reversedDepth = false`.
837    #[allow(clippy::too_many_arguments)]
838    pub fn make_orthographic(
839        &mut self,
840        left: f64,
841        right: f64,
842        top: f64,
843        bottom: f64,
844        near: f64,
845        far: f64,
846        coordinate_system: CoordinateSystem,
847    ) -> &mut Self {
848        let x = 2.0 / (right - left);
849        let y = 2.0 / (top - bottom);
850
851        let a = -(right + left) / (right - left);
852        let b = -(top + bottom) / (top - bottom);
853
854        let (c, d) = match coordinate_system {
855            CoordinateSystem::WebGl => (-2.0 / (far - near), -(far + near) / (far - near)),
856            CoordinateSystem::WebGpu => (-1.0 / (far - near), -near / (far - near)),
857        };
858
859        let te = &mut self.elements;
860        te[0] = x;
861        te[4] = 0.0;
862        te[8] = 0.0;
863        te[12] = a;
864        te[1] = 0.0;
865        te[5] = y;
866        te[9] = 0.0;
867        te[13] = b;
868        te[2] = 0.0;
869        te[6] = 0.0;
870        te[10] = c;
871        te[14] = d;
872        te[3] = 0.0;
873        te[7] = 0.0;
874        te[11] = 0.0;
875        te[15] = 1.0;
876
877        self
878    }
879
880    /// `Matrix4.equals()`.
881    pub fn equals(&self, m: &Self) -> bool {
882        self.elements == m.elements
883    }
884
885    /// `Matrix4.fromArray()`.
886    pub fn from_array(&mut self, array: &[f64], offset: usize) -> &mut Self {
887        self.elements.copy_from_slice(&array[offset..offset + 16]);
888        self
889    }
890
891    /// `Matrix4.toArray()`.
892    pub fn to_array(&self) -> [f64; 16] {
893        self.elements
894    }
895}