@lakuna/umath 1.3.9 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/algorithms/approx.d.ts +9 -0
- package/dist/algorithms/approx.d.ts.map +1 -0
- package/dist/algorithms/approx.js +12 -0
- package/dist/algorithms/approx.js.map +1 -0
- package/dist/algorithms/approxRelative.d.ts +9 -0
- package/dist/algorithms/approxRelative.d.ts.map +1 -0
- package/dist/algorithms/approxRelative.js +12 -0
- package/dist/algorithms/approxRelative.js.map +1 -0
- package/dist/algorithms/combinations.d.ts +2 -1
- package/dist/algorithms/combinations.d.ts.map +1 -1
- package/dist/algorithms/combinations.js +2 -1
- package/dist/algorithms/combinations.js.map +1 -1
- package/dist/algorithms/degreesToRadians.d.ts +3 -2
- package/dist/algorithms/degreesToRadians.d.ts.map +1 -1
- package/dist/algorithms/degreesToRadians.js +5 -3
- package/dist/algorithms/degreesToRadians.js.map +1 -1
- package/dist/algorithms/factorial.d.ts +2 -1
- package/dist/algorithms/factorial.d.ts.map +1 -1
- package/dist/algorithms/factorial.js +2 -1
- package/dist/algorithms/factorial.js.map +1 -1
- package/dist/algorithms/fibonacci.d.ts +2 -1
- package/dist/algorithms/fibonacci.d.ts.map +1 -1
- package/dist/algorithms/fibonacci.js +6 -2
- package/dist/algorithms/fibonacci.js.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.d.ts +4 -2
- package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -1
- package/dist/algorithms/greatestCommonDivisor.js +5 -2
- package/dist/algorithms/greatestCommonDivisor.js.map +1 -1
- package/dist/algorithms/hypergeometricPmf.d.ts +3 -2
- package/dist/algorithms/hypergeometricPmf.d.ts.map +1 -1
- package/dist/algorithms/hypergeometricPmf.js +3 -2
- package/dist/algorithms/hypergeometricPmf.js.map +1 -1
- package/dist/algorithms/isPrime.d.ts +2 -1
- package/dist/algorithms/isPrime.d.ts.map +1 -1
- package/dist/algorithms/isPrime.js +2 -1
- package/dist/algorithms/isPrime.js.map +1 -1
- package/dist/algorithms/permutations.d.ts +2 -1
- package/dist/algorithms/permutations.d.ts.map +1 -1
- package/dist/algorithms/permutations.js +2 -1
- package/dist/algorithms/permutations.js.map +1 -1
- package/dist/algorithms/primeFactorization.d.ts +2 -1
- package/dist/algorithms/primeFactorization.d.ts.map +1 -1
- package/dist/algorithms/primeFactorization.js +2 -1
- package/dist/algorithms/primeFactorization.js.map +1 -1
- package/dist/algorithms/radiansToDegrees.d.ts +3 -2
- package/dist/algorithms/radiansToDegrees.d.ts.map +1 -1
- package/dist/algorithms/radiansToDegrees.js +5 -3
- package/dist/algorithms/radiansToDegrees.js.map +1 -1
- package/dist/algorithms/summation.d.ts +2 -1
- package/dist/algorithms/summation.d.ts.map +1 -1
- package/dist/algorithms/summation.js +2 -1
- package/dist/algorithms/summation.js.map +1 -1
- package/dist/linalg/DualQuaternion.d.ts +102 -32
- package/dist/linalg/DualQuaternion.d.ts.map +1 -1
- package/dist/linalg/DualQuaternion.js +243 -272
- package/dist/linalg/DualQuaternion.js.map +1 -1
- package/dist/linalg/Matrix.d.ts +14 -10
- package/dist/linalg/Matrix.d.ts.map +1 -1
- package/dist/linalg/Matrix2.d.ts +74 -51
- package/dist/linalg/Matrix2.d.ts.map +1 -1
- package/dist/linalg/Matrix2.js +95 -171
- package/dist/linalg/Matrix2.js.map +1 -1
- package/dist/linalg/Matrix3.d.ts +122 -72
- package/dist/linalg/Matrix3.d.ts.map +1 -1
- package/dist/linalg/Matrix3.js +186 -355
- package/dist/linalg/Matrix3.js.map +1 -1
- package/dist/linalg/Matrix4.d.ts +266 -149
- package/dist/linalg/Matrix4.d.ts.map +1 -1
- package/dist/linalg/Matrix4.js +512 -852
- package/dist/linalg/Matrix4.js.map +1 -1
- package/dist/linalg/Quaternion.d.ts +252 -34
- package/dist/linalg/Quaternion.d.ts.map +1 -1
- package/dist/linalg/Quaternion.js +436 -166
- package/dist/linalg/Quaternion.js.map +1 -1
- package/dist/linalg/SlowMatrix.d.ts +10 -9
- package/dist/linalg/SlowMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowMatrix.js +10 -9
- package/dist/linalg/SlowMatrix.js.map +1 -1
- package/dist/linalg/SlowSquareMatrix.d.ts +10 -9
- package/dist/linalg/SlowSquareMatrix.d.ts.map +1 -1
- package/dist/linalg/SlowSquareMatrix.js +10 -9
- package/dist/linalg/SlowSquareMatrix.js.map +1 -1
- package/dist/linalg/SquareMatrix.d.ts +6 -5
- package/dist/linalg/SquareMatrix.d.ts.map +1 -1
- package/dist/linalg/Vector.d.ts +8 -4
- package/dist/linalg/Vector.d.ts.map +1 -1
- package/dist/linalg/Vector2.d.ts +82 -31
- package/dist/linalg/Vector2.d.ts.map +1 -1
- package/dist/linalg/Vector2.js +112 -154
- package/dist/linalg/Vector2.js.map +1 -1
- package/dist/linalg/Vector3.d.ts +93 -41
- package/dist/linalg/Vector3.d.ts.map +1 -1
- package/dist/linalg/Vector3.js +171 -282
- package/dist/linalg/Vector3.js.map +1 -1
- package/dist/linalg/Vector4.d.ts +71 -21
- package/dist/linalg/Vector4.d.ts.map +1 -1
- package/dist/linalg/Vector4.js +121 -195
- package/dist/linalg/Vector4.js.map +1 -1
- package/dist/types/AxisAngle.d.ts +4 -1
- package/dist/types/AxisAngle.d.ts.map +1 -1
- package/dist/types/FieldOfView.d.ts +4 -1
- package/dist/types/FieldOfView.d.ts.map +1 -1
- package/dist/utility/BigNumber.d.ts +4 -1
- package/dist/utility/BigNumber.d.ts.map +1 -1
- package/dist/utility/BigNumber.js +4 -1
- package/dist/utility/BigNumber.js.map +1 -1
- package/dist/utility/MagnitudeError.d.ts +4 -1
- package/dist/utility/MagnitudeError.d.ts.map +1 -1
- package/dist/utility/MagnitudeError.js +4 -1
- package/dist/utility/MagnitudeError.js.map +1 -1
- package/dist/utility/MatrixSizeError.d.ts +4 -1
- package/dist/utility/MatrixSizeError.d.ts.map +1 -1
- package/dist/utility/MatrixSizeError.js +4 -1
- package/dist/utility/MatrixSizeError.js.map +1 -1
- package/dist/utility/PartialMatrixError.d.ts +4 -1
- package/dist/utility/PartialMatrixError.d.ts.map +1 -1
- package/dist/utility/PartialMatrixError.js +4 -1
- package/dist/utility/PartialMatrixError.js.map +1 -1
- package/dist/utility/SingularMatrixError.d.ts +2 -1
- package/dist/utility/SingularMatrixError.d.ts.map +1 -1
- package/dist/utility/SingularMatrixError.js +2 -1
- package/dist/utility/SingularMatrixError.js.map +1 -1
- package/dist/utility/epsilon.d.ts +4 -1
- package/dist/utility/epsilon.d.ts.map +1 -1
- package/dist/utility/epsilon.js +4 -1
- package/dist/utility/epsilon.js.map +1 -1
- package/package.json +9 -9
- package/src/algorithms/approx.ts +12 -0
- package/src/algorithms/approxRelative.ts +12 -0
- package/src/algorithms/combinations.ts +2 -1
- package/src/algorithms/degreesToRadians.ts +6 -3
- package/src/algorithms/factorial.ts +3 -1
- package/src/algorithms/fibonacci.ts +7 -2
- package/src/algorithms/greatestCommonDivisor.ts +9 -4
- package/src/algorithms/hypergeometricPmf.ts +3 -2
- package/src/algorithms/isPrime.ts +2 -1
- package/src/algorithms/permutations.ts +2 -1
- package/src/algorithms/primeFactorization.ts +2 -1
- package/src/algorithms/radiansToDegrees.ts +6 -3
- package/src/algorithms/summation.ts +2 -1
- package/src/linalg/DualQuaternion.ts +424 -289
- package/src/linalg/Matrix.ts +14 -10
- package/src/linalg/Matrix2.ts +141 -188
- package/src/linalg/Matrix3.ts +400 -375
- package/src/linalg/Matrix4.ts +1083 -905
- package/src/linalg/Quaternion.ts +706 -188
- package/src/linalg/SlowMatrix.ts +10 -9
- package/src/linalg/SlowSquareMatrix.ts +10 -9
- package/src/linalg/SquareMatrix.ts +6 -5
- package/src/linalg/Vector.ts +8 -4
- package/src/linalg/Vector2.ts +146 -173
- package/src/linalg/Vector3.ts +293 -326
- package/src/linalg/Vector4.ts +227 -215
- package/src/types/AxisAngle.ts +4 -1
- package/src/types/FieldOfView.ts +4 -1
- package/src/utility/BigNumber.ts +6 -3
- package/src/utility/MagnitudeError.ts +4 -1
- package/src/utility/MatrixSizeError.ts +4 -1
- package/src/utility/PartialMatrixError.ts +4 -1
- package/src/utility/SingularMatrixError.ts +2 -1
- package/src/utility/epsilon.ts +4 -1
package/src/linalg/Matrix4.ts
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
import Quaternion, {
|
|
2
|
-
|
|
1
|
+
import Quaternion, {
|
|
2
|
+
type QuaternionLike,
|
|
3
|
+
fromValues as quaternionFromValues
|
|
4
|
+
} from "./Quaternion.js";
|
|
5
|
+
import Vector3, {
|
|
6
|
+
type Vector3Like,
|
|
7
|
+
createVector3Like,
|
|
8
|
+
fromValues as vector3FromValues
|
|
9
|
+
} from "./Vector3.js";
|
|
3
10
|
import type { DualQuaternionLike } from "./DualQuaternion.js";
|
|
4
11
|
import type FieldOfView from "../types/FieldOfView.js";
|
|
5
12
|
import MagnitudeError from "../utility/MagnitudeError.js";
|
|
6
13
|
import type { MatrixLike } from "./Matrix.js";
|
|
7
14
|
import SingularMatrixError from "../utility/SingularMatrixError.js";
|
|
8
15
|
import type SquareMatrix from "./SquareMatrix.js";
|
|
16
|
+
import approx from "../algorithms/approx.js";
|
|
17
|
+
import approxRelative from "../algorithms/approxRelative.js";
|
|
18
|
+
import degreesToRadians from "../algorithms/degreesToRadians.js";
|
|
9
19
|
import epsilon from "../utility/epsilon.js";
|
|
10
20
|
|
|
11
21
|
/**
|
|
12
22
|
* Numbers arranged into four columns and four rows.
|
|
13
|
-
* @see
|
|
23
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_(mathematics) | Matrix}
|
|
24
|
+
* @public
|
|
14
25
|
*/
|
|
15
26
|
export interface Matrix4Like extends MatrixLike {
|
|
16
27
|
/** The value in the first column and first row. */
|
|
@@ -63,109 +74,159 @@ export interface Matrix4Like extends MatrixLike {
|
|
|
63
74
|
}
|
|
64
75
|
|
|
65
76
|
/**
|
|
66
|
-
* Create a
|
|
67
|
-
* @returns A
|
|
77
|
+
* Create a four-by-four matrix-like object.
|
|
78
|
+
* @returns A four-by-four matrix-like object.
|
|
79
|
+
* @public
|
|
68
80
|
*/
|
|
69
|
-
export const createMatrix4Like = () => {
|
|
81
|
+
export const createMatrix4Like = (): Float32Array & Matrix4Like => {
|
|
70
82
|
return new Float32Array(16) as Float32Array & Matrix4Like;
|
|
71
83
|
};
|
|
72
84
|
|
|
73
85
|
/**
|
|
74
|
-
* Create a
|
|
86
|
+
* Create a four-by-four matrix with the given values.
|
|
87
|
+
* @param c0r0 - The value in the first column and first row.
|
|
88
|
+
* @param c0r1 - The value in the first column and second row.
|
|
89
|
+
* @param c0r2 - The value in the first column and third row.
|
|
90
|
+
* @param c0r3 - The value in the first column and fourth row.
|
|
91
|
+
* @param c1r0 - The value in the second column and first row.
|
|
92
|
+
* @param c1r1 - The value in the second column and second row.
|
|
93
|
+
* @param c1r2 - The value in the second column and third row.
|
|
94
|
+
* @param c1r3 - The value in the second column and fourth row.
|
|
95
|
+
* @param c2r0 - The value in the third column and first row.
|
|
96
|
+
* @param c2r1 - The value in the third column and second row.
|
|
97
|
+
* @param c2r2 - The value in the third column and third row.
|
|
98
|
+
* @param c2r3 - The value in the third column and fourth row.
|
|
99
|
+
* @param c3r0 - The value in the fourth column and first row.
|
|
100
|
+
* @param c3r1 - The value in the fourth column and second row.
|
|
101
|
+
* @param c3r2 - The value in the fourth column and third row.
|
|
102
|
+
* @param c3r3 - The value in the fourth column and fourth row.
|
|
103
|
+
* @param out - The matrix to store the result in.
|
|
104
|
+
* @returns The matrix.
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
107
|
+
export const fromValues = <T extends Matrix4Like>(
|
|
108
|
+
c0r0: number,
|
|
109
|
+
c0r1: number,
|
|
110
|
+
c0r2: number,
|
|
111
|
+
c0r3: number,
|
|
112
|
+
c1r0: number,
|
|
113
|
+
c1r1: number,
|
|
114
|
+
c1r2: number,
|
|
115
|
+
c1r3: number,
|
|
116
|
+
c2r0: number,
|
|
117
|
+
c2r1: number,
|
|
118
|
+
c2r2: number,
|
|
119
|
+
c2r3: number,
|
|
120
|
+
c3r0: number,
|
|
121
|
+
c3r1: number,
|
|
122
|
+
c3r2: number,
|
|
123
|
+
c3r3: number,
|
|
124
|
+
out: T
|
|
125
|
+
): T => {
|
|
126
|
+
out[0] = c0r0;
|
|
127
|
+
out[1] = c0r1;
|
|
128
|
+
out[2] = c0r2;
|
|
129
|
+
out[3] = c0r3;
|
|
130
|
+
out[4] = c1r0;
|
|
131
|
+
out[5] = c1r1;
|
|
132
|
+
out[6] = c1r2;
|
|
133
|
+
out[7] = c1r3;
|
|
134
|
+
out[8] = c2r0;
|
|
135
|
+
out[9] = c2r1;
|
|
136
|
+
out[10] = c2r2;
|
|
137
|
+
out[11] = c2r3;
|
|
138
|
+
out[12] = c3r0;
|
|
139
|
+
out[13] = c3r1;
|
|
140
|
+
out[14] = c3r2;
|
|
141
|
+
out[15] = c3r3;
|
|
142
|
+
return out;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Create a transformation matrix that represents a translation by the given vector. Equivalent to (but faster than) `translate(identity(out), vector, out)`.
|
|
75
147
|
* @param vector - The translation vector.
|
|
76
148
|
* @param out - The matrix to store the result in.
|
|
77
149
|
* @returns The transformation matrix.
|
|
78
|
-
* @see
|
|
150
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
151
|
+
* @public
|
|
79
152
|
*/
|
|
80
153
|
export const fromTranslation = <T extends Matrix4Like>(
|
|
81
154
|
vector: Vector3Like,
|
|
82
155
|
out: T
|
|
83
|
-
): T =>
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
156
|
+
): T =>
|
|
157
|
+
fromValues(
|
|
158
|
+
1,
|
|
159
|
+
0,
|
|
160
|
+
0,
|
|
161
|
+
0,
|
|
162
|
+
0,
|
|
163
|
+
1,
|
|
164
|
+
0,
|
|
165
|
+
0,
|
|
166
|
+
0,
|
|
167
|
+
0,
|
|
168
|
+
1,
|
|
169
|
+
0,
|
|
170
|
+
vector[0],
|
|
171
|
+
vector[1],
|
|
172
|
+
vector[2],
|
|
173
|
+
1,
|
|
174
|
+
out
|
|
175
|
+
);
|
|
102
176
|
|
|
103
177
|
/**
|
|
104
|
-
* Create a transformation matrix that represents a scaling by the given vector.
|
|
178
|
+
* Create a transformation matrix that represents a scaling by the given vector. This is equivalent to (but faster than) `scale(identity(out), vector, out)`.
|
|
105
179
|
* @param vector - The scaling vector.
|
|
106
180
|
* @param out - The matrix to store the result in.
|
|
107
181
|
* @returns The transformation matrix.
|
|
108
|
-
* @see
|
|
182
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
183
|
+
* @public
|
|
109
184
|
*/
|
|
110
185
|
export const fromScaling = <T extends Matrix4Like>(
|
|
111
186
|
vector: Vector3Like,
|
|
112
187
|
out: T
|
|
113
|
-
): T =>
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
188
|
+
): T =>
|
|
189
|
+
fromValues(
|
|
190
|
+
vector[0],
|
|
191
|
+
0,
|
|
192
|
+
0,
|
|
193
|
+
0,
|
|
194
|
+
0,
|
|
195
|
+
vector[1],
|
|
196
|
+
0,
|
|
197
|
+
0,
|
|
198
|
+
0,
|
|
199
|
+
0,
|
|
200
|
+
vector[2],
|
|
201
|
+
0,
|
|
202
|
+
0,
|
|
203
|
+
0,
|
|
204
|
+
0,
|
|
205
|
+
1,
|
|
206
|
+
out
|
|
207
|
+
);
|
|
132
208
|
|
|
133
209
|
/**
|
|
134
210
|
* Reset a matrix to identity.
|
|
135
211
|
* @param out - The matrix to store the result in.
|
|
136
212
|
* @returns The matrix.
|
|
137
|
-
* @see
|
|
213
|
+
* @see {@link https://en.wikipedia.org/wiki/Identity_matrix | Identity matrix}
|
|
214
|
+
* @public
|
|
138
215
|
*/
|
|
139
|
-
export const identity = <T extends Matrix4Like>(out: T): T =>
|
|
140
|
-
|
|
141
|
-
out[1] = 0;
|
|
142
|
-
out[2] = 0;
|
|
143
|
-
out[3] = 0;
|
|
144
|
-
out[4] = 0;
|
|
145
|
-
out[5] = 1;
|
|
146
|
-
out[6] = 0;
|
|
147
|
-
out[7] = 0;
|
|
148
|
-
out[8] = 0;
|
|
149
|
-
out[9] = 0;
|
|
150
|
-
out[10] = 1;
|
|
151
|
-
out[11] = 0;
|
|
152
|
-
out[12] = 0;
|
|
153
|
-
out[13] = 0;
|
|
154
|
-
out[14] = 0;
|
|
155
|
-
out[15] = 1;
|
|
156
|
-
return out;
|
|
157
|
-
};
|
|
216
|
+
export const identity = <T extends Matrix4Like>(out: T): T =>
|
|
217
|
+
fromValues(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, out);
|
|
158
218
|
|
|
159
219
|
/**
|
|
160
|
-
* Create a transformation matrix that represents a rotation by the given angle around the
|
|
161
|
-
* @param
|
|
220
|
+
* Create a transformation matrix that represents a rotation by the given angle around the given axis. This is equivalent to (but faster than) `rotate(identity(out), radians, axis, out)`.
|
|
221
|
+
* @param radians - The angle in radians.
|
|
162
222
|
* @param axis - The axis to rotate around.
|
|
163
223
|
* @param out - The matrix to store the result in.
|
|
164
224
|
* @returns The transformation matrix.
|
|
165
|
-
* @see
|
|
225
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
226
|
+
* @public
|
|
166
227
|
*/
|
|
167
228
|
export const fromRotation = <T extends Matrix4Like>(
|
|
168
|
-
|
|
229
|
+
radians: number,
|
|
169
230
|
axis: Vector3Like,
|
|
170
231
|
out: T
|
|
171
232
|
): T => {
|
|
@@ -183,128 +244,95 @@ export const fromRotation = <T extends Matrix4Like>(
|
|
|
183
244
|
y *= len;
|
|
184
245
|
z *= len;
|
|
185
246
|
|
|
186
|
-
const s = Math.sin(
|
|
187
|
-
const c = Math.cos(
|
|
247
|
+
const s = Math.sin(radians);
|
|
248
|
+
const c = Math.cos(radians);
|
|
188
249
|
const t = 1 - c;
|
|
189
250
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
251
|
+
return fromValues(
|
|
252
|
+
x * x * t + c,
|
|
253
|
+
y * x * t + z * s,
|
|
254
|
+
z * x * t - y * s,
|
|
255
|
+
0,
|
|
256
|
+
x * y * t - z * s,
|
|
257
|
+
y * y * t + c,
|
|
258
|
+
z * y * t + x * s,
|
|
259
|
+
0,
|
|
260
|
+
x * z * t + y * s,
|
|
261
|
+
y * z * t - x * s,
|
|
262
|
+
z * z * t + c,
|
|
263
|
+
0,
|
|
264
|
+
0,
|
|
265
|
+
0,
|
|
266
|
+
0,
|
|
267
|
+
1,
|
|
268
|
+
out
|
|
269
|
+
);
|
|
207
270
|
};
|
|
208
271
|
|
|
209
272
|
/**
|
|
210
|
-
* Create a transformation matrix that represents a rotation by the given angle around the X-axis.
|
|
211
|
-
* @param
|
|
273
|
+
* Create a transformation matrix that represents a rotation by the given angle around the X-axis. This is equivalent to (but faster than) `rotateX(identity(out), radians, out)`.
|
|
274
|
+
* @param radians - The angle in radians.
|
|
212
275
|
* @param out - The matrix to store the result in.
|
|
213
276
|
* @returns The transformation matrix.
|
|
214
|
-
* @see
|
|
277
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
278
|
+
* @public
|
|
215
279
|
*/
|
|
216
|
-
export const fromXRotation = <T extends Matrix4Like>(
|
|
217
|
-
|
|
218
|
-
|
|
280
|
+
export const fromXRotation = <T extends Matrix4Like>(
|
|
281
|
+
radians: number,
|
|
282
|
+
out: T
|
|
283
|
+
): T => {
|
|
284
|
+
const s = Math.sin(radians);
|
|
285
|
+
const c = Math.cos(radians);
|
|
219
286
|
|
|
220
|
-
|
|
221
|
-
out[1] = 0;
|
|
222
|
-
out[2] = 0;
|
|
223
|
-
out[3] = 0;
|
|
224
|
-
out[4] = 0;
|
|
225
|
-
out[5] = c;
|
|
226
|
-
out[6] = s;
|
|
227
|
-
out[7] = 0;
|
|
228
|
-
out[8] = 0;
|
|
229
|
-
out[9] = -s;
|
|
230
|
-
out[10] = c;
|
|
231
|
-
out[11] = 0;
|
|
232
|
-
out[12] = 0;
|
|
233
|
-
out[13] = 0;
|
|
234
|
-
out[14] = 0;
|
|
235
|
-
out[15] = 1;
|
|
236
|
-
return out;
|
|
287
|
+
return fromValues(1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, out);
|
|
237
288
|
};
|
|
238
289
|
|
|
239
290
|
/**
|
|
240
|
-
* Create a transformation matrix that represents a rotation by the given angle around the Y-axis.
|
|
241
|
-
* @param
|
|
291
|
+
* Create a transformation matrix that represents a rotation by the given angle around the Y-axis. This is equivalent to (but faster than) `rotateY(identity(out), radians, out)`.
|
|
292
|
+
* @param radians - The angle in radians.
|
|
242
293
|
* @param out - The matrix to store the result in.
|
|
243
294
|
* @returns The transformation matrix.
|
|
244
|
-
* @see
|
|
295
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
296
|
+
* @public
|
|
245
297
|
*/
|
|
246
|
-
export const fromYRotation = <T extends Matrix4Like>(
|
|
247
|
-
|
|
248
|
-
|
|
298
|
+
export const fromYRotation = <T extends Matrix4Like>(
|
|
299
|
+
radians: number,
|
|
300
|
+
out: T
|
|
301
|
+
): T => {
|
|
302
|
+
const s = Math.sin(radians);
|
|
303
|
+
const c = Math.cos(radians);
|
|
249
304
|
|
|
250
|
-
|
|
251
|
-
out[1] = 0;
|
|
252
|
-
out[2] = -s;
|
|
253
|
-
out[3] = 0;
|
|
254
|
-
out[4] = 0;
|
|
255
|
-
out[5] = 1;
|
|
256
|
-
out[6] = 0;
|
|
257
|
-
out[7] = 0;
|
|
258
|
-
out[8] = s;
|
|
259
|
-
out[9] = 0;
|
|
260
|
-
out[10] = c;
|
|
261
|
-
out[11] = 0;
|
|
262
|
-
out[12] = 0;
|
|
263
|
-
out[13] = 0;
|
|
264
|
-
out[14] = 0;
|
|
265
|
-
out[15] = 1;
|
|
266
|
-
return out;
|
|
305
|
+
return fromValues(c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1, out);
|
|
267
306
|
};
|
|
268
307
|
|
|
269
308
|
/**
|
|
270
|
-
* Create a transformation matrix that represents a rotation by the given angle around the Z-axis.
|
|
271
|
-
* @param
|
|
309
|
+
* Create a transformation matrix that represents a rotation by the given angle around the Z-axis. This is equivalent to (but faster than) `rotateZ(identity(out), radians, out)`.
|
|
310
|
+
* @param radians - The angle in radians.
|
|
272
311
|
* @param out - The matrix to store the result in.
|
|
273
312
|
* @returns The transformation matrix.
|
|
274
|
-
* @see
|
|
313
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
314
|
+
* @public
|
|
275
315
|
*/
|
|
276
|
-
export const fromZRotation = <T extends Matrix4Like>(
|
|
277
|
-
|
|
278
|
-
|
|
316
|
+
export const fromZRotation = <T extends Matrix4Like>(
|
|
317
|
+
radians: number,
|
|
318
|
+
out: T
|
|
319
|
+
): T => {
|
|
320
|
+
const s = Math.sin(radians);
|
|
321
|
+
const c = Math.cos(radians);
|
|
279
322
|
|
|
280
|
-
|
|
281
|
-
out[1] = s;
|
|
282
|
-
out[2] = 0;
|
|
283
|
-
out[3] = 0;
|
|
284
|
-
out[4] = -s;
|
|
285
|
-
out[5] = c;
|
|
286
|
-
out[6] = 0;
|
|
287
|
-
out[7] = 0;
|
|
288
|
-
out[8] = 0;
|
|
289
|
-
out[9] = 0;
|
|
290
|
-
out[10] = 1;
|
|
291
|
-
out[11] = 0;
|
|
292
|
-
out[12] = 0;
|
|
293
|
-
out[13] = 0;
|
|
294
|
-
out[14] = 0;
|
|
295
|
-
out[15] = 1;
|
|
296
|
-
return out;
|
|
323
|
+
return fromValues(c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, out);
|
|
297
324
|
};
|
|
298
325
|
|
|
299
326
|
/**
|
|
300
|
-
* Create a transformation matrix from the given rotation and translation.
|
|
327
|
+
* Create a transformation matrix from the given rotation and translation. This is equivalent to (but faster than) `multiply(translate(identity(out), translation, out), fromQuaternion(rotation, createMatrix4Like()), out)`.
|
|
301
328
|
* @param rotation - The rotation quaternion.
|
|
302
329
|
* @param translation - The translation vector.
|
|
303
330
|
* @param out - The matrix to store the result in.
|
|
304
331
|
* @returns The transformation matrix.
|
|
305
|
-
* @see
|
|
306
|
-
* @see
|
|
307
|
-
* @see
|
|
332
|
+
* @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
|
|
333
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
334
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
335
|
+
* @public
|
|
308
336
|
*/
|
|
309
337
|
export const fromRotationTranslation = <T extends Matrix4Like>(
|
|
310
338
|
rotation: QuaternionLike,
|
|
@@ -329,35 +357,38 @@ export const fromRotationTranslation = <T extends Matrix4Like>(
|
|
|
329
357
|
const wy = w * y2;
|
|
330
358
|
const wz = w * z2;
|
|
331
359
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
360
|
+
return fromValues(
|
|
361
|
+
1 - (yy + zz),
|
|
362
|
+
xy + wz,
|
|
363
|
+
xz - wy,
|
|
364
|
+
0,
|
|
365
|
+
xy - wz,
|
|
366
|
+
1 - (xx + zz),
|
|
367
|
+
yz + wx,
|
|
368
|
+
0,
|
|
369
|
+
xz + wy,
|
|
370
|
+
yz - wx,
|
|
371
|
+
1 - (xx + yy),
|
|
372
|
+
0,
|
|
373
|
+
translation[0],
|
|
374
|
+
translation[1],
|
|
375
|
+
translation[2],
|
|
376
|
+
1,
|
|
377
|
+
out
|
|
378
|
+
);
|
|
349
379
|
};
|
|
350
380
|
|
|
351
381
|
/**
|
|
352
|
-
* Create a transformation matrix from the given rotation, translation, and scale.
|
|
382
|
+
* Create a transformation matrix from the given rotation, translation, and scale. This is equivalent to (but faster than) `scale(multiply(translate(identity(out), translation, out), fromQuaternion(rotation, createMatrix4Like()), out), scaling, out)`.
|
|
353
383
|
* @param rotation - The rotation quaternion.
|
|
354
384
|
* @param translation - The translation vector.
|
|
355
385
|
* @param scaling - The scaling vector.
|
|
356
386
|
* @param out - The matrix to store the result in.
|
|
357
387
|
* @returns The transformation matrix.
|
|
358
|
-
* @see
|
|
359
|
-
* @see
|
|
360
|
-
* @see
|
|
388
|
+
* @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
|
|
389
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
390
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
391
|
+
* @public
|
|
361
392
|
*/
|
|
362
393
|
export const fromRotationTranslationScale = <T extends Matrix4Like>(
|
|
363
394
|
rotation: QuaternionLike,
|
|
@@ -387,36 +418,39 @@ export const fromRotationTranslationScale = <T extends Matrix4Like>(
|
|
|
387
418
|
const sy = scaling[1];
|
|
388
419
|
const sz = scaling[2];
|
|
389
420
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
421
|
+
return fromValues(
|
|
422
|
+
(1 - (yy + zz)) * sx,
|
|
423
|
+
(xy + wz) * sx,
|
|
424
|
+
(xz - wy) * sx,
|
|
425
|
+
0,
|
|
426
|
+
(xy - wz) * sy,
|
|
427
|
+
(1 - (xx + zz)) * sy,
|
|
428
|
+
(yz + wx) * sy,
|
|
429
|
+
0,
|
|
430
|
+
(xz + wy) * sz,
|
|
431
|
+
(yz - wx) * sz,
|
|
432
|
+
(1 - (xx + yy)) * sz,
|
|
433
|
+
0,
|
|
434
|
+
translation[0],
|
|
435
|
+
translation[1],
|
|
436
|
+
translation[2],
|
|
437
|
+
1,
|
|
438
|
+
out
|
|
439
|
+
);
|
|
407
440
|
};
|
|
408
441
|
|
|
409
442
|
/**
|
|
410
|
-
* Create a transformation matrix from the given rotation, translation, and scale around the given origin.
|
|
443
|
+
* Create a transformation matrix from the given rotation, translation, and scale around the given origin. This is equivalent to (but faster than) `translate(scale(multiply(translate(translate(identity(out), translation, out), origin, out), fromQuaternion(rotation, createMatrix4Like()), out), scaling, out), negate(origin), out)`.
|
|
411
444
|
* @param rotation - The rotation quaternion.
|
|
412
445
|
* @param translation - The translation vector.
|
|
413
446
|
* @param scaling - The scaling vector.
|
|
414
447
|
* @param origin - The origin vector.
|
|
415
448
|
* @param out - The matrix to store the result in.
|
|
416
449
|
* @returns The transformation matrix.
|
|
417
|
-
* @see
|
|
418
|
-
* @see
|
|
419
|
-
* @see
|
|
450
|
+
* @see {@link https://en.wikipedia.org/wiki/Quaternion | Quaternion}
|
|
451
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
452
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
453
|
+
* @public
|
|
420
454
|
*/
|
|
421
455
|
export const fromRotationTranslationScaleOrigin = <T extends Matrix4Like>(
|
|
422
456
|
rotation: QuaternionLike,
|
|
@@ -450,46 +484,102 @@ export const fromRotationTranslationScaleOrigin = <T extends Matrix4Like>(
|
|
|
450
484
|
const oy = origin[1];
|
|
451
485
|
const oz = origin[2];
|
|
452
486
|
|
|
453
|
-
const
|
|
454
|
-
const
|
|
455
|
-
const
|
|
456
|
-
const
|
|
457
|
-
const
|
|
458
|
-
const
|
|
459
|
-
const
|
|
460
|
-
const
|
|
461
|
-
const
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
487
|
+
const o0 = (1 - (yy + zz)) * sx;
|
|
488
|
+
const o1 = (xy + wz) * sx;
|
|
489
|
+
const o2 = (xz - wy) * sx;
|
|
490
|
+
const o4 = (xy - wz) * sy;
|
|
491
|
+
const o5 = (1 - (xx + zz)) * sy;
|
|
492
|
+
const o6 = (yz + wx) * sy;
|
|
493
|
+
const o8 = (xz + wy) * sz;
|
|
494
|
+
const o9 = (yz - wx) * sz;
|
|
495
|
+
const o10 = (1 - (xx + yy)) * sz;
|
|
496
|
+
|
|
497
|
+
return fromValues(
|
|
498
|
+
o0,
|
|
499
|
+
o1,
|
|
500
|
+
o2,
|
|
501
|
+
0,
|
|
502
|
+
o4,
|
|
503
|
+
o5,
|
|
504
|
+
o6,
|
|
505
|
+
0,
|
|
506
|
+
o8,
|
|
507
|
+
o9,
|
|
508
|
+
o10,
|
|
509
|
+
0,
|
|
510
|
+
translation[0] + ox - (o0 * ox + o4 * oy + o8 * oz),
|
|
511
|
+
translation[1] + oy - (o1 * ox + o5 * oy + o9 * oz),
|
|
512
|
+
translation[2] + oz - (o2 * ox + o6 * oy + o10 * oz),
|
|
513
|
+
1,
|
|
514
|
+
out
|
|
515
|
+
);
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Create a transformation matrix from a quaternion.
|
|
520
|
+
* @param quaternion - The quaternion.
|
|
521
|
+
* @param out - The matrix to store the result in.
|
|
522
|
+
* @returns The transformation matrix.
|
|
523
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
524
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
525
|
+
* @public
|
|
526
|
+
*/
|
|
527
|
+
export const fromQuaternion = <T extends Matrix4Like>(
|
|
528
|
+
quaternion: QuaternionLike,
|
|
529
|
+
out: T
|
|
530
|
+
): T => {
|
|
531
|
+
const x = quaternion[0];
|
|
532
|
+
const y = quaternion[1];
|
|
533
|
+
const z = quaternion[2];
|
|
534
|
+
const w = quaternion[3];
|
|
535
|
+
|
|
536
|
+
const x2 = x + x;
|
|
537
|
+
const y2 = y + y;
|
|
538
|
+
const z2 = z + z;
|
|
539
|
+
|
|
540
|
+
const xx = x * x2;
|
|
541
|
+
const yx = y * x2;
|
|
542
|
+
const yy = y * y2;
|
|
543
|
+
const zx = z * x2;
|
|
544
|
+
const zy = z * y2;
|
|
545
|
+
const zz = z * z2;
|
|
546
|
+
const wx = w * x2;
|
|
547
|
+
const wy = w * y2;
|
|
548
|
+
const wz = w * z2;
|
|
549
|
+
|
|
550
|
+
return fromValues(
|
|
551
|
+
1 - yy - zz,
|
|
552
|
+
yx + wz,
|
|
553
|
+
zx - wy,
|
|
554
|
+
0,
|
|
555
|
+
yx - wz,
|
|
556
|
+
1 - xx - zz,
|
|
557
|
+
zy + wx,
|
|
558
|
+
0,
|
|
559
|
+
zx + wy,
|
|
560
|
+
zy - wx,
|
|
561
|
+
1 - xx - yy,
|
|
562
|
+
0,
|
|
563
|
+
0,
|
|
564
|
+
0,
|
|
565
|
+
0,
|
|
566
|
+
1,
|
|
567
|
+
out
|
|
568
|
+
);
|
|
480
569
|
};
|
|
481
570
|
|
|
482
571
|
// Used to store intermediary values in some functions.
|
|
483
|
-
const
|
|
572
|
+
const iv3 = createVector3Like();
|
|
484
573
|
|
|
485
574
|
/**
|
|
486
575
|
* Create a transformation matrix from a dual quaternion.
|
|
487
576
|
* @param quaternion - The dual quaternion.
|
|
488
577
|
* @param out - The matrix to store the result in.
|
|
489
578
|
* @returns The transformation matrix.
|
|
490
|
-
* @see
|
|
491
|
-
* @see
|
|
492
|
-
* @see
|
|
579
|
+
* @see {@link https://en.wikipedia.org/wiki/Dual_quaternion | Dual quaternion}
|
|
580
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
581
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
582
|
+
* @public
|
|
493
583
|
*/
|
|
494
584
|
export const fromDualQuaternion = <T extends Matrix4Like>(
|
|
495
585
|
quaternion: DualQuaternionLike,
|
|
@@ -505,17 +595,23 @@ export const fromDualQuaternion = <T extends Matrix4Like>(
|
|
|
505
595
|
const aw = quaternion[7];
|
|
506
596
|
|
|
507
597
|
const magnitude = bx * bx + by * by + bz * bz + bw * bw;
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
598
|
+
return fromRotationTranslation(
|
|
599
|
+
quaternion,
|
|
600
|
+
magnitude > 0
|
|
601
|
+
? vector3FromValues(
|
|
602
|
+
((ax * bw + aw * bx + ay * bz - az * by) * 2) / magnitude,
|
|
603
|
+
((ay * bw + aw * by + az * bx - ax * bz) * 2) / magnitude,
|
|
604
|
+
((az * bw + aw * bz + ax * by - ay * bx) * 2) / magnitude,
|
|
605
|
+
iv3
|
|
606
|
+
)
|
|
607
|
+
: vector3FromValues(
|
|
608
|
+
(ax * bw + aw * bx + ay * bz - az * by) * 2,
|
|
609
|
+
(ay * bw + aw * by + az * bx - ax * bz) * 2,
|
|
610
|
+
(az * bw + aw * bz + ax * by - ay * bx) * 2,
|
|
611
|
+
iv3
|
|
612
|
+
),
|
|
613
|
+
out
|
|
614
|
+
);
|
|
519
615
|
};
|
|
520
616
|
|
|
521
617
|
/**
|
|
@@ -528,8 +624,9 @@ export const fromDualQuaternion = <T extends Matrix4Like>(
|
|
|
528
624
|
* @param far - The far bound of the frustum.
|
|
529
625
|
* @param out - The matrix to store the result in.
|
|
530
626
|
* @returns The frustum matrix.
|
|
531
|
-
* @see
|
|
532
|
-
* @see
|
|
627
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
628
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
629
|
+
* @public
|
|
533
630
|
*/
|
|
534
631
|
export const frustum = <T extends Matrix4Like>(
|
|
535
632
|
left: number,
|
|
@@ -542,7 +639,6 @@ export const frustum = <T extends Matrix4Like>(
|
|
|
542
639
|
): T => {
|
|
543
640
|
const rl = 1 / (right - left);
|
|
544
641
|
const tb = 1 / (top - bottom);
|
|
545
|
-
const nf = 1 / (near - far);
|
|
546
642
|
|
|
547
643
|
out[0] = near * 2 * rl;
|
|
548
644
|
out[1] = 0;
|
|
@@ -554,25 +650,34 @@ export const frustum = <T extends Matrix4Like>(
|
|
|
554
650
|
out[7] = 0;
|
|
555
651
|
out[8] = (right + left) * rl;
|
|
556
652
|
out[9] = (top + bottom) * tb;
|
|
557
|
-
out[10] = (far + near) * nf;
|
|
558
653
|
out[11] = -1;
|
|
559
654
|
out[12] = 0;
|
|
560
655
|
out[13] = 0;
|
|
561
|
-
out[14] = far * near * 2 * nf;
|
|
562
656
|
out[15] = 0;
|
|
657
|
+
|
|
658
|
+
if (far === Infinity) {
|
|
659
|
+
out[10] = -1;
|
|
660
|
+
out[14] = -2 * near;
|
|
661
|
+
return out;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const nf = 1 / (near - far);
|
|
665
|
+
out[10] = (far + near) * nf;
|
|
666
|
+
out[14] = 2 * far * near * nf;
|
|
563
667
|
return out;
|
|
564
668
|
};
|
|
565
669
|
|
|
566
670
|
/**
|
|
567
|
-
* Create a perspective projection matrix with the given bounds.
|
|
671
|
+
* Create a perspective projection matrix with the given bounds such that the near and far clip planes correspond to a normalized device coordinate Z range of `[-1, 1]` (OpenGL/WebGL).
|
|
568
672
|
* @param fov - The vertical field of view in radians.
|
|
569
673
|
* @param aspect - The aspect ratio (typically the width of the viewport divided by its height).
|
|
570
674
|
* @param near - The near bound of the frustum. Must be strictly positive.
|
|
571
675
|
* @param far - The far bound of the frustum.
|
|
572
676
|
* @param out - The matrix to store the result in.
|
|
573
677
|
* @returns The perspective projection matrix.
|
|
574
|
-
* @see
|
|
575
|
-
* @see
|
|
678
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
679
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
680
|
+
* @public
|
|
576
681
|
*/
|
|
577
682
|
export const perspective = <T extends Matrix4Like>(
|
|
578
683
|
fov: number,
|
|
@@ -610,6 +715,54 @@ export const perspective = <T extends Matrix4Like>(
|
|
|
610
715
|
return out;
|
|
611
716
|
};
|
|
612
717
|
|
|
718
|
+
/**
|
|
719
|
+
* Create a perspective projection matrix with the given bounds such that the near and far clip planes correspond to a normalized device coordinate Z range of `[0, 1]` (WebGPU/Vulkan/DirectX/Metal).
|
|
720
|
+
* @param fov - The vertical field of view in radians.
|
|
721
|
+
* @param aspect - The aspect ratio (typically the width of the viewport divided by its height).
|
|
722
|
+
* @param near - The near bound of the frustum. Must be strictly positive.
|
|
723
|
+
* @param far - The far bound of the frustum.
|
|
724
|
+
* @param out - The matrix to store the result in.
|
|
725
|
+
* @returns The perspective projection matrix.
|
|
726
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
727
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
728
|
+
* @public
|
|
729
|
+
*/
|
|
730
|
+
export const perspectiveGpu = <T extends Matrix4Like>(
|
|
731
|
+
fov: number,
|
|
732
|
+
aspect: number,
|
|
733
|
+
near: number,
|
|
734
|
+
far: number,
|
|
735
|
+
out: T
|
|
736
|
+
): T => {
|
|
737
|
+
const f = 1 / Math.tan(fov / 2);
|
|
738
|
+
|
|
739
|
+
out[0] = f / aspect;
|
|
740
|
+
out[1] = 0;
|
|
741
|
+
out[2] = 0;
|
|
742
|
+
out[3] = 0;
|
|
743
|
+
out[4] = 0;
|
|
744
|
+
out[5] = f;
|
|
745
|
+
out[6] = 0;
|
|
746
|
+
out[7] = 0;
|
|
747
|
+
out[8] = 0;
|
|
748
|
+
out[9] = 0;
|
|
749
|
+
out[11] = -1;
|
|
750
|
+
out[12] = 0;
|
|
751
|
+
out[13] = 0;
|
|
752
|
+
out[15] = 0;
|
|
753
|
+
|
|
754
|
+
if (far === Infinity) {
|
|
755
|
+
out[10] = -1;
|
|
756
|
+
out[14] = -near;
|
|
757
|
+
return out;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
const fnf = far * (1 / (near - far));
|
|
761
|
+
out[10] = fnf;
|
|
762
|
+
out[14] = near * fnf;
|
|
763
|
+
return out;
|
|
764
|
+
};
|
|
765
|
+
|
|
613
766
|
/**
|
|
614
767
|
* Create a perspective projection matrix from a field of view. Useful for generating projection matrices to be used with the WebXR API.
|
|
615
768
|
* @param fov - The field of view.
|
|
@@ -617,9 +770,10 @@ export const perspective = <T extends Matrix4Like>(
|
|
|
617
770
|
* @param far - The far bound of the frustum.
|
|
618
771
|
* @param out - The matrix to store the result in.
|
|
619
772
|
* @returns The perspective projection matrix.
|
|
620
|
-
* @see
|
|
621
|
-
* @see
|
|
622
|
-
* @see
|
|
773
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API | WebXR API}
|
|
774
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
775
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
776
|
+
* @public
|
|
623
777
|
*/
|
|
624
778
|
export const perspectiveFromFieldOfView = <T extends Matrix4Like>(
|
|
625
779
|
fov: FieldOfView,
|
|
@@ -627,34 +781,85 @@ export const perspectiveFromFieldOfView = <T extends Matrix4Like>(
|
|
|
627
781
|
far: number,
|
|
628
782
|
out: T
|
|
629
783
|
): T => {
|
|
630
|
-
const upTan = Math.tan((fov.upDegrees
|
|
631
|
-
const downTan = Math.tan((fov.downDegrees
|
|
632
|
-
const leftTan = Math.tan((fov.leftDegrees
|
|
633
|
-
const rightTan = Math.tan((fov.rightDegrees
|
|
634
|
-
const xScale = 2 / (leftTan + rightTan);
|
|
635
|
-
const yScale = 2 / (upTan + downTan);
|
|
784
|
+
const upTan = Math.tan(degreesToRadians(fov.upDegrees));
|
|
785
|
+
const downTan = Math.tan(degreesToRadians(fov.downDegrees));
|
|
786
|
+
const leftTan = Math.tan(degreesToRadians(fov.leftDegrees));
|
|
787
|
+
const rightTan = Math.tan(degreesToRadians(fov.rightDegrees));
|
|
788
|
+
const xScale = 2 / (leftTan + rightTan);
|
|
789
|
+
const yScale = 2 / (upTan + downTan);
|
|
790
|
+
const nf = near - far;
|
|
791
|
+
|
|
792
|
+
return fromValues(
|
|
793
|
+
xScale,
|
|
794
|
+
0,
|
|
795
|
+
0,
|
|
796
|
+
0,
|
|
797
|
+
0,
|
|
798
|
+
yScale,
|
|
799
|
+
0,
|
|
800
|
+
0,
|
|
801
|
+
-(((leftTan - rightTan) * xScale) / 2),
|
|
802
|
+
((upTan - downTan) * yScale) / 2,
|
|
803
|
+
far / nf,
|
|
804
|
+
-1,
|
|
805
|
+
0,
|
|
806
|
+
0,
|
|
807
|
+
(far * near) / nf,
|
|
808
|
+
0,
|
|
809
|
+
out
|
|
810
|
+
);
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Generate an orthogonal projection matrix with the given bounds such that the near and far clip planes correspond to a normalized device coordinate Z range of `[-1, 1]` (OpenGL/WebGL).
|
|
815
|
+
* @param left - The left bound of the frustum.
|
|
816
|
+
* @param right - The right bound of the frustum.
|
|
817
|
+
* @param bottom - The bottom bound of the frustum.
|
|
818
|
+
* @param top - The top bound of the frustum.
|
|
819
|
+
* @param near - The near bound of the frustum.
|
|
820
|
+
* @param far - The far bound of the frustum.
|
|
821
|
+
* @param out - The matrix to store the result in.
|
|
822
|
+
* @returns The frustum matrix.
|
|
823
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
824
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
825
|
+
* @public
|
|
826
|
+
*/
|
|
827
|
+
export const ortho = <T extends Matrix4Like>(
|
|
828
|
+
left: number,
|
|
829
|
+
right: number,
|
|
830
|
+
bottom: number,
|
|
831
|
+
top: number,
|
|
832
|
+
near: number,
|
|
833
|
+
far: number,
|
|
834
|
+
out: T
|
|
835
|
+
): T => {
|
|
836
|
+
const lr = 1 / (left - right);
|
|
837
|
+
const bt = 1 / (bottom - top);
|
|
838
|
+
const nf = 1 / (near - far);
|
|
636
839
|
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
840
|
+
return fromValues(
|
|
841
|
+
-2 * lr,
|
|
842
|
+
0,
|
|
843
|
+
0,
|
|
844
|
+
0,
|
|
845
|
+
0,
|
|
846
|
+
-2 * bt,
|
|
847
|
+
0,
|
|
848
|
+
0,
|
|
849
|
+
0,
|
|
850
|
+
0,
|
|
851
|
+
2 * nf,
|
|
852
|
+
0,
|
|
853
|
+
(left + right) * lr,
|
|
854
|
+
(top + bottom) * bt,
|
|
855
|
+
(far + near) * nf,
|
|
856
|
+
1,
|
|
857
|
+
out
|
|
858
|
+
);
|
|
654
859
|
};
|
|
655
860
|
|
|
656
861
|
/**
|
|
657
|
-
* Generate an orthogonal projection matrix with the given bounds.
|
|
862
|
+
* Generate an orthogonal projection matrix with the given bounds such that the near and far clip planes correspond to a normalized device coordinate Z range of `[0, 1]` (WebGPU/Vulkan/DirectX/Metal).
|
|
658
863
|
* @param left - The left bound of the frustum.
|
|
659
864
|
* @param right - The right bound of the frustum.
|
|
660
865
|
* @param bottom - The bottom bound of the frustum.
|
|
@@ -663,10 +868,11 @@ export const perspectiveFromFieldOfView = <T extends Matrix4Like>(
|
|
|
663
868
|
* @param far - The far bound of the frustum.
|
|
664
869
|
* @param out - The matrix to store the result in.
|
|
665
870
|
* @returns The frustum matrix.
|
|
666
|
-
* @see
|
|
667
|
-
* @see
|
|
871
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
872
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
873
|
+
* @public
|
|
668
874
|
*/
|
|
669
|
-
export const
|
|
875
|
+
export const orthoGpu = <T extends Matrix4Like>(
|
|
670
876
|
left: number,
|
|
671
877
|
right: number,
|
|
672
878
|
bottom: number,
|
|
@@ -679,23 +885,25 @@ export const ortho = <T extends Matrix4Like>(
|
|
|
679
885
|
const bt = 1 / (bottom - top);
|
|
680
886
|
const nf = 1 / (near - far);
|
|
681
887
|
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
888
|
+
return fromValues(
|
|
889
|
+
-2 * lr,
|
|
890
|
+
0,
|
|
891
|
+
0,
|
|
892
|
+
0,
|
|
893
|
+
0,
|
|
894
|
+
-2 * bt,
|
|
895
|
+
0,
|
|
896
|
+
0,
|
|
897
|
+
0,
|
|
898
|
+
0,
|
|
899
|
+
nf,
|
|
900
|
+
0,
|
|
901
|
+
(left + right) * lr,
|
|
902
|
+
(top + bottom) * bt,
|
|
903
|
+
near * nf,
|
|
904
|
+
1,
|
|
905
|
+
out
|
|
906
|
+
);
|
|
699
907
|
};
|
|
700
908
|
|
|
701
909
|
/**
|
|
@@ -705,7 +913,8 @@ export const ortho = <T extends Matrix4Like>(
|
|
|
705
913
|
* @param up - The local up direction.
|
|
706
914
|
* @param out - The matrix to store the result in.
|
|
707
915
|
* @returns The look-at matrix.
|
|
708
|
-
* @see
|
|
916
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
917
|
+
* @public
|
|
709
918
|
*/
|
|
710
919
|
export const lookAt = <T extends Matrix4Like>(
|
|
711
920
|
eye: Vector3Like,
|
|
@@ -725,18 +934,14 @@ export const lookAt = <T extends Matrix4Like>(
|
|
|
725
934
|
const centery = center[1];
|
|
726
935
|
const centerz = center[2];
|
|
727
936
|
|
|
728
|
-
if (
|
|
729
|
-
Math.abs(eyex - centerx) < epsilon &&
|
|
730
|
-
Math.abs(eyey - centery) < epsilon &&
|
|
731
|
-
Math.abs(eyez - centerz) < epsilon
|
|
732
|
-
) {
|
|
937
|
+
if (approx(eyex, centerx) && approx(eyey, centery) && approx(eyez, centerz)) {
|
|
733
938
|
return identity(out);
|
|
734
939
|
}
|
|
735
940
|
|
|
736
941
|
let z0 = eyex - centerx;
|
|
737
942
|
let z1 = eyey - centery;
|
|
738
943
|
let z2 = eyez - centerz;
|
|
739
|
-
let len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
|
|
944
|
+
let len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2); // `Math.hypot` is slower.
|
|
740
945
|
z0 *= len;
|
|
741
946
|
z1 *= len;
|
|
742
947
|
z2 *= len;
|
|
@@ -745,7 +950,7 @@ export const lookAt = <T extends Matrix4Like>(
|
|
|
745
950
|
let x1 = upz * z0 - upx * z2;
|
|
746
951
|
let x2 = upx * z1 - upy * z0;
|
|
747
952
|
len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
|
|
748
|
-
if (len
|
|
953
|
+
if (len) {
|
|
749
954
|
len = 1 / len;
|
|
750
955
|
x0 *= len;
|
|
751
956
|
x1 *= len;
|
|
@@ -760,7 +965,7 @@ export const lookAt = <T extends Matrix4Like>(
|
|
|
760
965
|
let y1 = z2 * x0 - z0 * x2;
|
|
761
966
|
let y2 = z0 * x1 - z1 * x0;
|
|
762
967
|
len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
|
|
763
|
-
if (len
|
|
968
|
+
if (len) {
|
|
764
969
|
len = 1 / len;
|
|
765
970
|
y0 *= len;
|
|
766
971
|
y1 *= len;
|
|
@@ -771,23 +976,25 @@ export const lookAt = <T extends Matrix4Like>(
|
|
|
771
976
|
y2 = 0;
|
|
772
977
|
}
|
|
773
978
|
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
979
|
+
return fromValues(
|
|
980
|
+
x0,
|
|
981
|
+
y0,
|
|
982
|
+
z0,
|
|
983
|
+
0,
|
|
984
|
+
x1,
|
|
985
|
+
y1,
|
|
986
|
+
z1,
|
|
987
|
+
0,
|
|
988
|
+
x2,
|
|
989
|
+
y2,
|
|
990
|
+
z2,
|
|
991
|
+
0,
|
|
992
|
+
-(x0 * eyex + x1 * eyey + x2 * eyez),
|
|
993
|
+
-(y0 * eyex + y1 * eyey + y2 * eyez),
|
|
994
|
+
-(z0 * eyex + z1 * eyey + z2 * eyez),
|
|
995
|
+
1,
|
|
996
|
+
out
|
|
997
|
+
);
|
|
791
998
|
};
|
|
792
999
|
|
|
793
1000
|
/**
|
|
@@ -797,7 +1004,8 @@ export const lookAt = <T extends Matrix4Like>(
|
|
|
797
1004
|
* @param up - The local up direction.
|
|
798
1005
|
* @param out - The matrix to store the result in.
|
|
799
1006
|
* @returns The transformation matrix.
|
|
800
|
-
* @see
|
|
1007
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
1008
|
+
* @public
|
|
801
1009
|
*/
|
|
802
1010
|
export const targetTo = <T extends Matrix4Like>(
|
|
803
1011
|
eye: Vector3Like,
|
|
@@ -817,7 +1025,7 @@ export const targetTo = <T extends Matrix4Like>(
|
|
|
817
1025
|
let z1 = eyey - target[1];
|
|
818
1026
|
let z2 = eyez - target[2];
|
|
819
1027
|
let len = z0 * z0 + z1 * z1 + z2 * z2;
|
|
820
|
-
if (len
|
|
1028
|
+
if (len) {
|
|
821
1029
|
len = 1 / Math.sqrt(len);
|
|
822
1030
|
z0 *= len;
|
|
823
1031
|
z1 *= len;
|
|
@@ -828,30 +1036,32 @@ export const targetTo = <T extends Matrix4Like>(
|
|
|
828
1036
|
let x1 = upz * z0 - upx * z2;
|
|
829
1037
|
let x2 = upx * z1 - upy * z0;
|
|
830
1038
|
len = x0 * x0 + x1 * x1 + x2 * x2;
|
|
831
|
-
if (len
|
|
1039
|
+
if (len) {
|
|
832
1040
|
len = 1 / Math.sqrt(len);
|
|
833
1041
|
x0 *= len;
|
|
834
1042
|
x1 *= len;
|
|
835
1043
|
x2 *= len;
|
|
836
1044
|
}
|
|
837
1045
|
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
1046
|
+
return fromValues(
|
|
1047
|
+
x0,
|
|
1048
|
+
x1,
|
|
1049
|
+
x2,
|
|
1050
|
+
0,
|
|
1051
|
+
z1 * x2 - z2 * x1,
|
|
1052
|
+
z2 * x0 - z0 * x2,
|
|
1053
|
+
z0 * x1 - z1 * x0,
|
|
1054
|
+
0,
|
|
1055
|
+
z0,
|
|
1056
|
+
z1,
|
|
1057
|
+
z2,
|
|
1058
|
+
0,
|
|
1059
|
+
eyex,
|
|
1060
|
+
eyey,
|
|
1061
|
+
eyez,
|
|
1062
|
+
1,
|
|
1063
|
+
out
|
|
1064
|
+
);
|
|
855
1065
|
};
|
|
856
1066
|
|
|
857
1067
|
/**
|
|
@@ -860,190 +1070,91 @@ export const targetTo = <T extends Matrix4Like>(
|
|
|
860
1070
|
* @param b - The addend.
|
|
861
1071
|
* @param out - The matrix to store the result in.
|
|
862
1072
|
* @returns The sum.
|
|
863
|
-
* @see
|
|
1073
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
1074
|
+
* @public
|
|
864
1075
|
*/
|
|
865
1076
|
export const add = <T extends Matrix4Like>(
|
|
866
1077
|
a: Matrix4Like,
|
|
867
1078
|
b: Matrix4Like,
|
|
868
1079
|
out: T
|
|
869
|
-
): T =>
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
/**
|
|
890
|
-
* Create a two-by-two matrix with the given values.
|
|
891
|
-
* @param c0r0 - The value in the first column and first row.
|
|
892
|
-
* @param c0r1 - The value in the first column and second row.
|
|
893
|
-
* @param c0r2 - The value in the first column and third row.
|
|
894
|
-
* @param c0r3 - The value in the first column and fourth row.
|
|
895
|
-
* @param c1r0 - The value in the second column and first row.
|
|
896
|
-
* @param c1r1 - The value in the second column and second row.
|
|
897
|
-
* @param c1r2 - The value in the second column and third row.
|
|
898
|
-
* @param c1r3 - The value in the second column and fourth row.
|
|
899
|
-
* @param c2r0 - The value in the third column and first row.
|
|
900
|
-
* @param c2r1 - The value in the third column and second row.
|
|
901
|
-
* @param c2r2 - The value in the third column and third row.
|
|
902
|
-
* @param c2r3 - The value in the third column and fourth row.
|
|
903
|
-
* @param c3r0 - The value in the fourth column and first row.
|
|
904
|
-
* @param c3r1 - The value in the fourth column and second row.
|
|
905
|
-
* @param c3r2 - The value in the fourth column and third row.
|
|
906
|
-
* @param c3r3 - The value in the fourth column and fourth row.
|
|
907
|
-
* @param out - The matrix to store the result in.
|
|
908
|
-
* @returns The matrix.
|
|
909
|
-
*/
|
|
910
|
-
export const fromValues = <T extends Matrix4Like>(
|
|
911
|
-
c0r0: number,
|
|
912
|
-
c0r1: number,
|
|
913
|
-
c0r2: number,
|
|
914
|
-
c0r3: number,
|
|
915
|
-
c1r0: number,
|
|
916
|
-
c1r1: number,
|
|
917
|
-
c1r2: number,
|
|
918
|
-
c1r3: number,
|
|
919
|
-
c2r0: number,
|
|
920
|
-
c2r1: number,
|
|
921
|
-
c2r2: number,
|
|
922
|
-
c2r3: number,
|
|
923
|
-
c3r0: number,
|
|
924
|
-
c3r1: number,
|
|
925
|
-
c3r2: number,
|
|
926
|
-
c3r3: number,
|
|
927
|
-
out: T
|
|
928
|
-
): T => {
|
|
929
|
-
out[0] = c0r0;
|
|
930
|
-
out[1] = c0r1;
|
|
931
|
-
out[2] = c0r2;
|
|
932
|
-
out[3] = c0r3;
|
|
933
|
-
out[4] = c1r0;
|
|
934
|
-
out[5] = c1r1;
|
|
935
|
-
out[6] = c1r2;
|
|
936
|
-
out[7] = c1r3;
|
|
937
|
-
out[8] = c2r0;
|
|
938
|
-
out[9] = c2r1;
|
|
939
|
-
out[10] = c2r2;
|
|
940
|
-
out[11] = c2r3;
|
|
941
|
-
out[12] = c3r0;
|
|
942
|
-
out[13] = c3r1;
|
|
943
|
-
out[14] = c3r2;
|
|
944
|
-
out[15] = c3r3;
|
|
945
|
-
return out;
|
|
946
|
-
};
|
|
1080
|
+
): T =>
|
|
1081
|
+
fromValues(
|
|
1082
|
+
a[0] + b[0],
|
|
1083
|
+
a[1] + b[1],
|
|
1084
|
+
a[2] + b[2],
|
|
1085
|
+
a[3] + b[3],
|
|
1086
|
+
a[4] + b[4],
|
|
1087
|
+
a[5] + b[5],
|
|
1088
|
+
a[6] + b[6],
|
|
1089
|
+
a[7] + b[7],
|
|
1090
|
+
a[8] + b[8],
|
|
1091
|
+
a[9] + b[8],
|
|
1092
|
+
a[10] + b[10],
|
|
1093
|
+
a[11] + b[11],
|
|
1094
|
+
a[12] + b[12],
|
|
1095
|
+
a[13] + b[13],
|
|
1096
|
+
a[14] + b[14],
|
|
1097
|
+
a[15] + b[15],
|
|
1098
|
+
out
|
|
1099
|
+
);
|
|
947
1100
|
|
|
948
1101
|
/**
|
|
949
1102
|
* Determine whether or not two matrices are roughly equivalent.
|
|
950
1103
|
* @param a - The first matrix.
|
|
951
1104
|
* @param b - The second matrix.
|
|
952
1105
|
* @returns Whether or not the matrices are equivalent.
|
|
1106
|
+
* @public
|
|
953
1107
|
*/
|
|
954
|
-
export const equals = (a: Matrix4Like, b: Matrix4Like): boolean =>
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
const b0 = b[0];
|
|
973
|
-
const b1 = b[1];
|
|
974
|
-
const b2 = b[2];
|
|
975
|
-
const b3 = b[3];
|
|
976
|
-
const b4 = b[4];
|
|
977
|
-
const b5 = b[5];
|
|
978
|
-
const b6 = b[6];
|
|
979
|
-
const b7 = b[7];
|
|
980
|
-
const b8 = b[8];
|
|
981
|
-
const b9 = b[9];
|
|
982
|
-
const b10 = b[10];
|
|
983
|
-
const b11 = b[11];
|
|
984
|
-
const b12 = b[12];
|
|
985
|
-
const b13 = b[13];
|
|
986
|
-
const b14 = b[14];
|
|
987
|
-
const b15 = b[15];
|
|
988
|
-
|
|
989
|
-
return (
|
|
990
|
-
Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0)) &&
|
|
991
|
-
Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1)) &&
|
|
992
|
-
Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2)) &&
|
|
993
|
-
Math.abs(a3 - b3) <= epsilon * Math.max(1, Math.abs(a3), Math.abs(b3)) &&
|
|
994
|
-
Math.abs(a4 - b4) <= epsilon * Math.max(1, Math.abs(a4), Math.abs(b4)) &&
|
|
995
|
-
Math.abs(a5 - b5) <= epsilon * Math.max(1, Math.abs(a5), Math.abs(b5)) &&
|
|
996
|
-
Math.abs(a6 - b6) <= epsilon * Math.max(1, Math.abs(a6), Math.abs(b6)) &&
|
|
997
|
-
Math.abs(a7 - b7) <= epsilon * Math.max(1, Math.abs(a7), Math.abs(b7)) &&
|
|
998
|
-
Math.abs(a8 - b8) <= epsilon * Math.max(1, Math.abs(a8), Math.abs(b8)) &&
|
|
999
|
-
Math.abs(a9 - b9) <= epsilon * Math.max(1, Math.abs(a9), Math.abs(b9)) &&
|
|
1000
|
-
Math.abs(a10 - b10) <=
|
|
1001
|
-
epsilon * Math.max(1, Math.abs(a10), Math.abs(b10)) &&
|
|
1002
|
-
Math.abs(a11 - b11) <=
|
|
1003
|
-
epsilon * Math.max(1, Math.abs(a11), Math.abs(b11)) &&
|
|
1004
|
-
Math.abs(a12 - b12) <=
|
|
1005
|
-
epsilon * Math.max(1, Math.abs(a12), Math.abs(b12)) &&
|
|
1006
|
-
Math.abs(a13 - b13) <=
|
|
1007
|
-
epsilon * Math.max(1, Math.abs(a13), Math.abs(b13)) &&
|
|
1008
|
-
Math.abs(a14 - b14) <=
|
|
1009
|
-
epsilon * Math.max(1, Math.abs(a14), Math.abs(b14)) &&
|
|
1010
|
-
Math.abs(a15 - b15) <= epsilon * Math.max(1, Math.abs(a15), Math.abs(b15))
|
|
1011
|
-
);
|
|
1012
|
-
};
|
|
1108
|
+
export const equals = (a: Matrix4Like, b: Matrix4Like): boolean =>
|
|
1109
|
+
approxRelative(a[0], b[0]) &&
|
|
1110
|
+
approxRelative(a[1], b[1]) &&
|
|
1111
|
+
approxRelative(a[2], b[2]) &&
|
|
1112
|
+
approxRelative(a[3], b[3]) &&
|
|
1113
|
+
approxRelative(a[4], b[4]) &&
|
|
1114
|
+
approxRelative(a[5], b[5]) &&
|
|
1115
|
+
approxRelative(a[6], b[6]) &&
|
|
1116
|
+
approxRelative(a[7], b[7]) &&
|
|
1117
|
+
approxRelative(a[8], b[8]) &&
|
|
1118
|
+
approxRelative(a[9], b[9]) &&
|
|
1119
|
+
approxRelative(a[10], b[10]) &&
|
|
1120
|
+
approxRelative(a[11], b[11]) &&
|
|
1121
|
+
approxRelative(a[12], b[12]) &&
|
|
1122
|
+
approxRelative(a[13], b[13]) &&
|
|
1123
|
+
approxRelative(a[14], b[14]) &&
|
|
1124
|
+
approxRelative(a[15], b[15]);
|
|
1013
1125
|
|
|
1014
1126
|
/**
|
|
1015
1127
|
* Determine whether or not two matrices are exactly equivalent.
|
|
1016
1128
|
* @param a - The first matrix.
|
|
1017
1129
|
* @param b - The second matrix.
|
|
1018
1130
|
* @returns Whether the matrices are equivalent.
|
|
1131
|
+
* @public
|
|
1019
1132
|
*/
|
|
1020
|
-
export const exactEquals = (a: Matrix4Like, b: Matrix4Like): boolean =>
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
a[15] === b[15]
|
|
1038
|
-
);
|
|
1039
|
-
};
|
|
1133
|
+
export const exactEquals = (a: Matrix4Like, b: Matrix4Like): boolean =>
|
|
1134
|
+
a[0] === b[0] &&
|
|
1135
|
+
a[1] === b[1] &&
|
|
1136
|
+
a[2] === b[2] &&
|
|
1137
|
+
a[3] === b[3] &&
|
|
1138
|
+
a[4] === b[4] &&
|
|
1139
|
+
a[5] === b[5] &&
|
|
1140
|
+
a[6] === b[6] &&
|
|
1141
|
+
a[7] === b[7] &&
|
|
1142
|
+
a[8] === b[8] &&
|
|
1143
|
+
a[9] === b[9] &&
|
|
1144
|
+
a[10] === b[10] &&
|
|
1145
|
+
a[11] === b[11] &&
|
|
1146
|
+
a[12] === b[12] &&
|
|
1147
|
+
a[13] === b[13] &&
|
|
1148
|
+
a[14] === b[14] &&
|
|
1149
|
+
a[15] === b[15];
|
|
1040
1150
|
|
|
1041
1151
|
/**
|
|
1042
1152
|
* Calculate the adjugate of a matrix.
|
|
1043
1153
|
* @param matrix - The matrix.
|
|
1044
1154
|
* @param out - The matrix to store the result in.
|
|
1045
1155
|
* @returns The adjugate of the matrix.
|
|
1046
|
-
* @see
|
|
1156
|
+
* @see {@link https://en.wikipedia.org/wiki/Adjugate_matrix | Adjugate matrix}
|
|
1157
|
+
* @public
|
|
1047
1158
|
*/
|
|
1048
1159
|
export const adjoint = <T extends Matrix4Like>(
|
|
1049
1160
|
matrix: Matrix4Like,
|
|
@@ -1066,79 +1177,38 @@ export const adjoint = <T extends Matrix4Like>(
|
|
|
1066
1177
|
const a32 = matrix[14];
|
|
1067
1178
|
const a33 = matrix[15];
|
|
1068
1179
|
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
);
|
|
1101
|
-
out[7] =
|
|
1102
|
-
a00 * (a12 * a23 - a13 * a22) -
|
|
1103
|
-
a10 * (a02 * a23 - a03 * a22) +
|
|
1104
|
-
a20 * (a02 * a13 - a03 * a12);
|
|
1105
|
-
out[8] =
|
|
1106
|
-
a10 * (a21 * a33 - a23 * a31) -
|
|
1107
|
-
a20 * (a11 * a33 - a13 * a31) +
|
|
1108
|
-
a30 * (a11 * a23 - a13 * a21);
|
|
1109
|
-
out[9] = -(
|
|
1110
|
-
a00 * (a21 * a33 - a23 * a31) -
|
|
1111
|
-
a20 * (a01 * a33 - a03 * a31) +
|
|
1112
|
-
a30 * (a01 * a23 - a03 * a21)
|
|
1113
|
-
);
|
|
1114
|
-
out[10] =
|
|
1115
|
-
a00 * (a11 * a33 - a13 * a31) -
|
|
1116
|
-
a10 * (a01 * a33 - a03 * a31) +
|
|
1117
|
-
a30 * (a01 * a13 - a03 * a11);
|
|
1118
|
-
out[11] = -(
|
|
1119
|
-
a00 * (a11 * a23 - a13 * a21) -
|
|
1120
|
-
a10 * (a01 * a23 - a03 * a21) +
|
|
1121
|
-
a20 * (a01 * a13 - a03 * a11)
|
|
1122
|
-
);
|
|
1123
|
-
out[12] = -(
|
|
1124
|
-
a10 * (a21 * a32 - a22 * a31) -
|
|
1125
|
-
a20 * (a11 * a32 - a12 * a31) +
|
|
1126
|
-
a30 * (a11 * a22 - a12 * a21)
|
|
1127
|
-
);
|
|
1128
|
-
out[13] =
|
|
1129
|
-
a00 * (a21 * a32 - a22 * a31) -
|
|
1130
|
-
a20 * (a01 * a32 - a02 * a31) +
|
|
1131
|
-
a30 * (a01 * a22 - a02 * a21);
|
|
1132
|
-
out[14] = -(
|
|
1133
|
-
a00 * (a11 * a32 - a12 * a31) -
|
|
1134
|
-
a10 * (a01 * a32 - a02 * a31) +
|
|
1135
|
-
a30 * (a01 * a12 - a02 * a11)
|
|
1180
|
+
const b00 = a00 * a11 - a01 * a10;
|
|
1181
|
+
const b01 = a00 * a12 - a02 * a10;
|
|
1182
|
+
const b02 = a00 * a13 - a03 * a10;
|
|
1183
|
+
const b03 = a01 * a12 - a02 * a11;
|
|
1184
|
+
const b04 = a01 * a13 - a03 * a11;
|
|
1185
|
+
const b05 = a02 * a13 - a03 * a12;
|
|
1186
|
+
const b06 = a20 * a31 - a21 * a30;
|
|
1187
|
+
const b07 = a20 * a32 - a22 * a30;
|
|
1188
|
+
const b08 = a20 * a33 - a23 * a30;
|
|
1189
|
+
const b09 = a21 * a32 - a22 * a31;
|
|
1190
|
+
const b10 = a21 * a33 - a23 * a31;
|
|
1191
|
+
const b11 = a22 * a33 - a23 * a32;
|
|
1192
|
+
|
|
1193
|
+
return fromValues(
|
|
1194
|
+
a11 * b11 - a12 * b10 + a13 * b09,
|
|
1195
|
+
a02 * b10 - a01 * b11 - a03 * b09,
|
|
1196
|
+
a31 * b05 - a32 * b04 + a33 * b03,
|
|
1197
|
+
a22 * b04 - a21 * b05 - a23 * b03,
|
|
1198
|
+
a12 * b08 - a10 * b11 - a13 * b07,
|
|
1199
|
+
a00 * b11 - a02 * b08 + a03 * b07,
|
|
1200
|
+
a32 * b02 - a30 * b05 - a33 * b01,
|
|
1201
|
+
a20 * b05 - a22 * b02 + a23 * b01,
|
|
1202
|
+
a10 * b10 - a11 * b08 + a13 * b06,
|
|
1203
|
+
a01 * b08 - a00 * b10 - a03 * b06,
|
|
1204
|
+
a30 * b04 - a31 * b02 + a33 * b00,
|
|
1205
|
+
a21 * b02 - a20 * b04 - a23 * b00,
|
|
1206
|
+
a11 * b07 - a10 * b09 - a12 * b06,
|
|
1207
|
+
a00 * b09 - a01 * b07 + a02 * b06,
|
|
1208
|
+
a31 * b01 - a30 * b03 - a32 * b00,
|
|
1209
|
+
a20 * b03 - a21 * b01 + a22 * b00,
|
|
1210
|
+
out
|
|
1136
1211
|
);
|
|
1137
|
-
out[15] =
|
|
1138
|
-
a00 * (a11 * a22 - a12 * a21) -
|
|
1139
|
-
a10 * (a01 * a22 - a02 * a21) +
|
|
1140
|
-
a20 * (a01 * a12 - a02 * a11);
|
|
1141
|
-
return out;
|
|
1142
1212
|
};
|
|
1143
1213
|
|
|
1144
1214
|
/**
|
|
@@ -1146,32 +1216,35 @@ export const adjoint = <T extends Matrix4Like>(
|
|
|
1146
1216
|
* @param matrix - The matrix to copy.
|
|
1147
1217
|
* @param out - The matrix to store the result in.
|
|
1148
1218
|
* @returns This matrix.
|
|
1219
|
+
* @public
|
|
1149
1220
|
*/
|
|
1150
|
-
export const copy = <T extends Matrix4Like>(matrix: Matrix4Like, out: T): T =>
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1221
|
+
export const copy = <T extends Matrix4Like>(matrix: Matrix4Like, out: T): T =>
|
|
1222
|
+
fromValues(
|
|
1223
|
+
matrix[0],
|
|
1224
|
+
matrix[1],
|
|
1225
|
+
matrix[2],
|
|
1226
|
+
matrix[3],
|
|
1227
|
+
matrix[4],
|
|
1228
|
+
matrix[5],
|
|
1229
|
+
matrix[6],
|
|
1230
|
+
matrix[7],
|
|
1231
|
+
matrix[8],
|
|
1232
|
+
matrix[9],
|
|
1233
|
+
matrix[10],
|
|
1234
|
+
matrix[11],
|
|
1235
|
+
matrix[12],
|
|
1236
|
+
matrix[13],
|
|
1237
|
+
matrix[14],
|
|
1238
|
+
matrix[15],
|
|
1239
|
+
out
|
|
1240
|
+
);
|
|
1169
1241
|
|
|
1170
1242
|
/**
|
|
1171
1243
|
* Calculate the Frobenius norm of a matrix.
|
|
1172
1244
|
* @param matrix - The matrix.
|
|
1173
1245
|
* @returns The Frobenius norm.
|
|
1174
|
-
* @see
|
|
1246
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_norm | Matrix norm}
|
|
1247
|
+
* @public
|
|
1175
1248
|
*/
|
|
1176
1249
|
export const frob = (matrix: Matrix4Like): number => {
|
|
1177
1250
|
const a00 = matrix[0];
|
|
@@ -1191,6 +1264,7 @@ export const frob = (matrix: Matrix4Like): number => {
|
|
|
1191
1264
|
const a32 = matrix[14];
|
|
1192
1265
|
const a33 = matrix[15];
|
|
1193
1266
|
|
|
1267
|
+
// `Math.hypot` is slower.
|
|
1194
1268
|
return Math.sqrt(
|
|
1195
1269
|
a00 * a00 +
|
|
1196
1270
|
a01 * a01 +
|
|
@@ -1213,11 +1287,12 @@ export const frob = (matrix: Matrix4Like): number => {
|
|
|
1213
1287
|
|
|
1214
1288
|
/**
|
|
1215
1289
|
* Multiply two matrices.
|
|
1216
|
-
* @param a - The
|
|
1217
|
-
* @param b - The
|
|
1290
|
+
* @param a - The multiplier.
|
|
1291
|
+
* @param b - The multiplicand.
|
|
1218
1292
|
* @param out - The matrix to store the result in.
|
|
1219
1293
|
* @returns The product.
|
|
1220
|
-
* @see
|
|
1294
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
1295
|
+
* @public
|
|
1221
1296
|
*/
|
|
1222
1297
|
export const multiply = <T extends Matrix4Like>(
|
|
1223
1298
|
a: Matrix4Like,
|
|
@@ -1241,39 +1316,42 @@ export const multiply = <T extends Matrix4Like>(
|
|
|
1241
1316
|
const a32 = a[14];
|
|
1242
1317
|
const a33 = a[15];
|
|
1243
1318
|
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1319
|
+
const b00 = b[0];
|
|
1320
|
+
const b01 = b[1];
|
|
1321
|
+
const b02 = b[2];
|
|
1322
|
+
const b03 = b[3];
|
|
1323
|
+
const b10 = b[4];
|
|
1324
|
+
const b11 = b[5];
|
|
1325
|
+
const b12 = b[6];
|
|
1326
|
+
const b13 = b[7];
|
|
1327
|
+
const b20 = b[8];
|
|
1328
|
+
const b21 = b[9];
|
|
1329
|
+
const b22 = b[10];
|
|
1330
|
+
const b23 = b[11];
|
|
1331
|
+
const b30 = b[12];
|
|
1332
|
+
const b31 = b[13];
|
|
1333
|
+
const b32 = b[14];
|
|
1334
|
+
const b33 = b[15];
|
|
1335
|
+
|
|
1336
|
+
return fromValues(
|
|
1337
|
+
b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30,
|
|
1338
|
+
b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31,
|
|
1339
|
+
b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32,
|
|
1340
|
+
b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33,
|
|
1341
|
+
b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30,
|
|
1342
|
+
b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31,
|
|
1343
|
+
b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32,
|
|
1344
|
+
b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33,
|
|
1345
|
+
b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30,
|
|
1346
|
+
b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31,
|
|
1347
|
+
b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32,
|
|
1348
|
+
b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33,
|
|
1349
|
+
b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30,
|
|
1350
|
+
b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31,
|
|
1351
|
+
b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32,
|
|
1352
|
+
b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33,
|
|
1353
|
+
out
|
|
1354
|
+
);
|
|
1277
1355
|
};
|
|
1278
1356
|
|
|
1279
1357
|
/**
|
|
@@ -1282,31 +1360,33 @@ export const multiply = <T extends Matrix4Like>(
|
|
|
1282
1360
|
* @param scalar - The multiplier.
|
|
1283
1361
|
* @param out - The matrix to store the result in.
|
|
1284
1362
|
* @returns The product.
|
|
1285
|
-
* @see
|
|
1363
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
1364
|
+
* @public
|
|
1286
1365
|
*/
|
|
1287
1366
|
export const multiplyScalar = <T extends Matrix4Like>(
|
|
1288
1367
|
matrix: Matrix4Like,
|
|
1289
1368
|
scalar: number,
|
|
1290
1369
|
out: T
|
|
1291
|
-
): T =>
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1370
|
+
): T =>
|
|
1371
|
+
fromValues(
|
|
1372
|
+
matrix[0] * scalar,
|
|
1373
|
+
matrix[1] * scalar,
|
|
1374
|
+
matrix[2] * scalar,
|
|
1375
|
+
matrix[3] * scalar,
|
|
1376
|
+
matrix[4] * scalar,
|
|
1377
|
+
matrix[5] * scalar,
|
|
1378
|
+
matrix[6] * scalar,
|
|
1379
|
+
matrix[7] * scalar,
|
|
1380
|
+
matrix[8] * scalar,
|
|
1381
|
+
matrix[9] * scalar,
|
|
1382
|
+
matrix[10] * scalar,
|
|
1383
|
+
matrix[11] * scalar,
|
|
1384
|
+
matrix[12] * scalar,
|
|
1385
|
+
matrix[13] * scalar,
|
|
1386
|
+
matrix[14] * scalar,
|
|
1387
|
+
matrix[15] * scalar,
|
|
1388
|
+
out
|
|
1389
|
+
);
|
|
1310
1390
|
|
|
1311
1391
|
/**
|
|
1312
1392
|
* Add a matrix to another after multiplying the other by a scalar.
|
|
@@ -1315,33 +1395,35 @@ export const multiplyScalar = <T extends Matrix4Like>(
|
|
|
1315
1395
|
* @param scalar - The multiplier.
|
|
1316
1396
|
* @param out - The matrix to store the result in.
|
|
1317
1397
|
* @returns The sum.
|
|
1318
|
-
* @see
|
|
1319
|
-
* @see
|
|
1398
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
1399
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
1400
|
+
* @public
|
|
1320
1401
|
*/
|
|
1321
1402
|
export const multiplyScalarAndAdd = <T extends Matrix4Like>(
|
|
1322
1403
|
a: Matrix4Like,
|
|
1323
1404
|
b: Matrix4Like,
|
|
1324
1405
|
scalar: number,
|
|
1325
1406
|
out: T
|
|
1326
|
-
): T =>
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1407
|
+
): T =>
|
|
1408
|
+
fromValues(
|
|
1409
|
+
a[0] + b[0] * scalar,
|
|
1410
|
+
a[1] + b[1] * scalar,
|
|
1411
|
+
a[2] + b[2] * scalar,
|
|
1412
|
+
a[3] + b[3] * scalar,
|
|
1413
|
+
a[4] + b[4] * scalar,
|
|
1414
|
+
a[5] + b[5] * scalar,
|
|
1415
|
+
a[6] + b[6] * scalar,
|
|
1416
|
+
a[7] + b[7] * scalar,
|
|
1417
|
+
a[8] + b[8] * scalar,
|
|
1418
|
+
a[9] + b[9] * scalar,
|
|
1419
|
+
a[10] + b[10] * scalar,
|
|
1420
|
+
a[11] + b[11] * scalar,
|
|
1421
|
+
a[12] + b[12] * scalar,
|
|
1422
|
+
a[13] + b[13] * scalar,
|
|
1423
|
+
a[14] + b[14] * scalar,
|
|
1424
|
+
a[15] + b[15] * scalar,
|
|
1425
|
+
out
|
|
1426
|
+
);
|
|
1345
1427
|
|
|
1346
1428
|
/**
|
|
1347
1429
|
* Subtract two matrices.
|
|
@@ -1349,38 +1431,41 @@ export const multiplyScalarAndAdd = <T extends Matrix4Like>(
|
|
|
1349
1431
|
* @param b - The subtrahend.
|
|
1350
1432
|
* @param out - The matrix to store the result in.
|
|
1351
1433
|
* @returns The difference.
|
|
1352
|
-
* @see
|
|
1434
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
1435
|
+
* @public
|
|
1353
1436
|
*/
|
|
1354
1437
|
export const subtract = <T extends Matrix4Like>(
|
|
1355
1438
|
a: Matrix4Like,
|
|
1356
1439
|
b: Matrix4Like,
|
|
1357
1440
|
out: T
|
|
1358
|
-
): T =>
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1441
|
+
): T =>
|
|
1442
|
+
fromValues(
|
|
1443
|
+
a[0] - b[0],
|
|
1444
|
+
a[1] - b[1],
|
|
1445
|
+
a[2] - b[2],
|
|
1446
|
+
a[3] - b[3],
|
|
1447
|
+
a[4] - b[4],
|
|
1448
|
+
a[5] - b[5],
|
|
1449
|
+
a[6] - b[6],
|
|
1450
|
+
a[7] - b[7],
|
|
1451
|
+
a[8] - b[8],
|
|
1452
|
+
a[9] - b[8],
|
|
1453
|
+
a[10] - b[10],
|
|
1454
|
+
a[11] - b[11],
|
|
1455
|
+
a[12] - b[12],
|
|
1456
|
+
a[13] - b[13],
|
|
1457
|
+
a[14] - b[14],
|
|
1458
|
+
a[15] - b[15],
|
|
1459
|
+
out
|
|
1460
|
+
);
|
|
1377
1461
|
|
|
1378
1462
|
/**
|
|
1379
1463
|
* Transpose a matrix.
|
|
1380
1464
|
* @param matrix - The matrix.
|
|
1381
1465
|
* @param out - The matrix to store the result in.
|
|
1382
1466
|
* @returns The transpose.
|
|
1383
|
-
* @see
|
|
1467
|
+
* @see {@link https://en.wikipedia.org/wiki/Transpose | Transpose}
|
|
1468
|
+
* @public
|
|
1384
1469
|
*/
|
|
1385
1470
|
export const transpose = <T extends Matrix4Like>(
|
|
1386
1471
|
matrix: Matrix4Like,
|
|
@@ -1408,63 +1493,61 @@ export const transpose = <T extends Matrix4Like>(
|
|
|
1408
1493
|
return out;
|
|
1409
1494
|
}
|
|
1410
1495
|
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1496
|
+
return fromValues(
|
|
1497
|
+
matrix[0],
|
|
1498
|
+
matrix[4],
|
|
1499
|
+
matrix[8],
|
|
1500
|
+
matrix[12],
|
|
1501
|
+
matrix[1],
|
|
1502
|
+
matrix[5],
|
|
1503
|
+
matrix[9],
|
|
1504
|
+
matrix[13],
|
|
1505
|
+
matrix[2],
|
|
1506
|
+
matrix[6],
|
|
1507
|
+
matrix[10],
|
|
1508
|
+
matrix[14],
|
|
1509
|
+
matrix[3],
|
|
1510
|
+
matrix[7],
|
|
1511
|
+
matrix[11],
|
|
1512
|
+
matrix[15],
|
|
1513
|
+
out
|
|
1514
|
+
);
|
|
1428
1515
|
};
|
|
1429
1516
|
|
|
1430
1517
|
/**
|
|
1431
1518
|
* Calculate the determinant of a matrix.
|
|
1432
1519
|
* @param matrix - The matrix.
|
|
1433
1520
|
* @returns The determinant.
|
|
1434
|
-
* @see
|
|
1521
|
+
* @see {@link https://en.wikipedia.org/wiki/Determinant | Determinant}
|
|
1522
|
+
* @public
|
|
1435
1523
|
*/
|
|
1436
1524
|
export const determinant = (matrix: Matrix4Like): number => {
|
|
1437
1525
|
const a00 = matrix[0];
|
|
1438
1526
|
const a01 = matrix[1];
|
|
1439
1527
|
const a02 = matrix[2];
|
|
1440
|
-
const a03 = matrix[3];
|
|
1441
1528
|
const a10 = matrix[4];
|
|
1442
1529
|
const a11 = matrix[5];
|
|
1443
1530
|
const a12 = matrix[6];
|
|
1444
|
-
const a13 = matrix[7];
|
|
1445
1531
|
const a20 = matrix[8];
|
|
1446
1532
|
const a21 = matrix[9];
|
|
1447
1533
|
const a22 = matrix[10];
|
|
1448
|
-
const a23 = matrix[11];
|
|
1449
1534
|
const a30 = matrix[12];
|
|
1450
1535
|
const a31 = matrix[13];
|
|
1451
1536
|
const a32 = matrix[14];
|
|
1452
|
-
const a33 = matrix[15];
|
|
1453
1537
|
|
|
1454
|
-
const
|
|
1455
|
-
const
|
|
1456
|
-
const
|
|
1457
|
-
const
|
|
1458
|
-
const
|
|
1459
|
-
const
|
|
1460
|
-
const b06 = a20 * a31 - a21 * a30;
|
|
1461
|
-
const b07 = a20 * a32 - a22 * a30;
|
|
1462
|
-
const b08 = a20 * a33 - a23 * a30;
|
|
1463
|
-
const b09 = a21 * a32 - a22 * a31;
|
|
1464
|
-
const b10 = a21 * a33 - a23 * a31;
|
|
1465
|
-
const b11 = a22 * a33 - a23 * a32;
|
|
1538
|
+
const b0 = a00 * a11 - a01 * a10;
|
|
1539
|
+
const b1 = a00 * a12 - a02 * a10;
|
|
1540
|
+
const b2 = a01 * a12 - a02 * a11;
|
|
1541
|
+
const b3 = a20 * a31 - a21 * a30;
|
|
1542
|
+
const b4 = a20 * a32 - a22 * a30;
|
|
1543
|
+
const b5 = a21 * a32 - a22 * a31;
|
|
1466
1544
|
|
|
1467
|
-
return
|
|
1545
|
+
return (
|
|
1546
|
+
matrix[7] * (a00 * b5 - a01 * b4 + a02 * b3) -
|
|
1547
|
+
matrix[3] * (a10 * b5 - a11 * b4 + a12 * b3) +
|
|
1548
|
+
matrix[15] * (a20 * b2 - a21 * b1 + a22 * b0) -
|
|
1549
|
+
matrix[11] * (a30 * b2 - a31 * b1 + a32 * b0)
|
|
1550
|
+
);
|
|
1468
1551
|
};
|
|
1469
1552
|
|
|
1470
1553
|
/**
|
|
@@ -1472,7 +1555,8 @@ export const determinant = (matrix: Matrix4Like): number => {
|
|
|
1472
1555
|
* @param matrix - The matrix.
|
|
1473
1556
|
* @param out - The matrix to store the result in.
|
|
1474
1557
|
* @returns The inverted matrix.
|
|
1475
|
-
* @see
|
|
1558
|
+
* @see {@link https://en.wikipedia.org/wiki/Invertible_matrix | Invertible matrix}
|
|
1559
|
+
* @public
|
|
1476
1560
|
*/
|
|
1477
1561
|
export const invert = <T extends Matrix4Like>(
|
|
1478
1562
|
matrix: Matrix4Like,
|
|
@@ -1515,23 +1599,25 @@ export const invert = <T extends Matrix4Like>(
|
|
|
1515
1599
|
}
|
|
1516
1600
|
det = 1 / det;
|
|
1517
1601
|
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1602
|
+
return fromValues(
|
|
1603
|
+
(a11 * b11 - a12 * b10 + a13 * b09) * det,
|
|
1604
|
+
(a02 * b10 - a01 * b11 - a03 * b09) * det,
|
|
1605
|
+
(a31 * b05 - a32 * b04 + a33 * b03) * det,
|
|
1606
|
+
(a22 * b04 - a21 * b05 - a23 * b03) * det,
|
|
1607
|
+
(a12 * b08 - a10 * b11 - a13 * b07) * det,
|
|
1608
|
+
(a00 * b11 - a02 * b08 + a03 * b07) * det,
|
|
1609
|
+
(a32 * b02 - a30 * b05 - a33 * b01) * det,
|
|
1610
|
+
(a20 * b05 - a22 * b02 + a23 * b01) * det,
|
|
1611
|
+
(a10 * b10 - a11 * b08 + a13 * b06) * det,
|
|
1612
|
+
(a01 * b08 - a00 * b10 - a03 * b06) * det,
|
|
1613
|
+
(a30 * b04 - a31 * b02 + a33 * b00) * det,
|
|
1614
|
+
(a21 * b02 - a20 * b04 - a23 * b00) * det,
|
|
1615
|
+
(a11 * b07 - a10 * b09 - a12 * b06) * det,
|
|
1616
|
+
(a00 * b09 - a01 * b07 + a02 * b06) * det,
|
|
1617
|
+
(a31 * b01 - a30 * b03 - a32 * b00) * det,
|
|
1618
|
+
(a20 * b03 - a21 * b01 + a22 * b00) * det,
|
|
1619
|
+
out
|
|
1620
|
+
);
|
|
1535
1621
|
};
|
|
1536
1622
|
|
|
1537
1623
|
/**
|
|
@@ -1540,7 +1626,8 @@ export const invert = <T extends Matrix4Like>(
|
|
|
1540
1626
|
* @param vector - The scaling vector.
|
|
1541
1627
|
* @param out - The matrix to store the result in.
|
|
1542
1628
|
* @returns The scaled matrix.
|
|
1543
|
-
* @see
|
|
1629
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
1630
|
+
* @public
|
|
1544
1631
|
*/
|
|
1545
1632
|
export const scale = <T extends Matrix4Like>(
|
|
1546
1633
|
matrix: Matrix4Like,
|
|
@@ -1551,23 +1638,25 @@ export const scale = <T extends Matrix4Like>(
|
|
|
1551
1638
|
const y = vector[1];
|
|
1552
1639
|
const z = vector[2];
|
|
1553
1640
|
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1641
|
+
return fromValues(
|
|
1642
|
+
matrix[0] * x,
|
|
1643
|
+
matrix[1] * x,
|
|
1644
|
+
matrix[2] * x,
|
|
1645
|
+
matrix[3] * x,
|
|
1646
|
+
matrix[4] * y,
|
|
1647
|
+
matrix[5] * y,
|
|
1648
|
+
matrix[6] * y,
|
|
1649
|
+
matrix[7] * y,
|
|
1650
|
+
matrix[8] * z,
|
|
1651
|
+
matrix[9] * z,
|
|
1652
|
+
matrix[10] * z,
|
|
1653
|
+
matrix[11] * z,
|
|
1654
|
+
matrix[12],
|
|
1655
|
+
matrix[13],
|
|
1656
|
+
matrix[14],
|
|
1657
|
+
matrix[15],
|
|
1658
|
+
out
|
|
1659
|
+
);
|
|
1571
1660
|
};
|
|
1572
1661
|
|
|
1573
1662
|
/**
|
|
@@ -1576,7 +1665,8 @@ export const scale = <T extends Matrix4Like>(
|
|
|
1576
1665
|
* @param vector - The translation vector.
|
|
1577
1666
|
* @param out - The matrix to store the result in.
|
|
1578
1667
|
* @returns The translated matrix.
|
|
1579
|
-
* @see
|
|
1668
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
1669
|
+
* @public
|
|
1580
1670
|
*/
|
|
1581
1671
|
export const translate = <T extends Matrix4Like>(
|
|
1582
1672
|
matrix: Matrix4Like,
|
|
@@ -1592,39 +1682,41 @@ export const translate = <T extends Matrix4Like>(
|
|
|
1592
1682
|
out[13] = matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13];
|
|
1593
1683
|
out[14] = matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14];
|
|
1594
1684
|
out[15] = matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15];
|
|
1595
|
-
|
|
1596
|
-
const a00 = matrix[0];
|
|
1597
|
-
const a01 = matrix[1];
|
|
1598
|
-
const a02 = matrix[2];
|
|
1599
|
-
const a03 = matrix[3];
|
|
1600
|
-
const a10 = matrix[4];
|
|
1601
|
-
const a11 = matrix[5];
|
|
1602
|
-
const a12 = matrix[6];
|
|
1603
|
-
const a13 = matrix[7];
|
|
1604
|
-
const a20 = matrix[8];
|
|
1605
|
-
const a21 = matrix[9];
|
|
1606
|
-
const a22 = matrix[10];
|
|
1607
|
-
const a23 = matrix[11];
|
|
1608
|
-
|
|
1609
|
-
out[0] = a00;
|
|
1610
|
-
out[1] = a01;
|
|
1611
|
-
out[2] = a02;
|
|
1612
|
-
out[3] = a03;
|
|
1613
|
-
out[4] = a10;
|
|
1614
|
-
out[5] = a11;
|
|
1615
|
-
out[6] = a12;
|
|
1616
|
-
out[7] = a13;
|
|
1617
|
-
out[8] = a20;
|
|
1618
|
-
out[9] = a21;
|
|
1619
|
-
out[10] = a22;
|
|
1620
|
-
out[11] = a23;
|
|
1621
|
-
out[12] = a00 * x + a10 * y + a20 * z + matrix[12];
|
|
1622
|
-
out[13] = a01 * x + a11 * y + a21 * z + matrix[13];
|
|
1623
|
-
out[14] = a02 * x + a12 * y + a22 * z + matrix[14];
|
|
1624
|
-
out[15] = a03 * x + a13 * y + a23 * z + matrix[15];
|
|
1685
|
+
return out;
|
|
1625
1686
|
}
|
|
1626
1687
|
|
|
1627
|
-
|
|
1688
|
+
const a00 = matrix[0];
|
|
1689
|
+
const a01 = matrix[1];
|
|
1690
|
+
const a02 = matrix[2];
|
|
1691
|
+
const a03 = matrix[3];
|
|
1692
|
+
const a10 = matrix[4];
|
|
1693
|
+
const a11 = matrix[5];
|
|
1694
|
+
const a12 = matrix[6];
|
|
1695
|
+
const a13 = matrix[7];
|
|
1696
|
+
const a20 = matrix[8];
|
|
1697
|
+
const a21 = matrix[9];
|
|
1698
|
+
const a22 = matrix[10];
|
|
1699
|
+
const a23 = matrix[11];
|
|
1700
|
+
|
|
1701
|
+
return fromValues(
|
|
1702
|
+
a00,
|
|
1703
|
+
a01,
|
|
1704
|
+
a02,
|
|
1705
|
+
a03,
|
|
1706
|
+
a10,
|
|
1707
|
+
a11,
|
|
1708
|
+
a12,
|
|
1709
|
+
a13,
|
|
1710
|
+
a20,
|
|
1711
|
+
a21,
|
|
1712
|
+
a22,
|
|
1713
|
+
a23,
|
|
1714
|
+
a00 * x + a10 * y + a20 * z + matrix[12],
|
|
1715
|
+
a01 * x + a11 * y + a21 * z + matrix[13],
|
|
1716
|
+
a02 * x + a12 * y + a22 * z + matrix[14],
|
|
1717
|
+
a03 * x + a13 * y + a23 * z + matrix[15],
|
|
1718
|
+
out
|
|
1719
|
+
);
|
|
1628
1720
|
};
|
|
1629
1721
|
|
|
1630
1722
|
/**
|
|
@@ -1634,7 +1726,8 @@ export const translate = <T extends Matrix4Like>(
|
|
|
1634
1726
|
* @param axis - The axis.
|
|
1635
1727
|
* @param out - The matrix to store the result in.
|
|
1636
1728
|
* @returns The rotated matrix.
|
|
1637
|
-
* @see
|
|
1729
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
1730
|
+
* @public
|
|
1638
1731
|
*/
|
|
1639
1732
|
export const rotate = <T extends Matrix4Like>(
|
|
1640
1733
|
matrix: Matrix4Like,
|
|
@@ -1646,7 +1739,7 @@ export const rotate = <T extends Matrix4Like>(
|
|
|
1646
1739
|
let y = axis[1];
|
|
1647
1740
|
let z = axis[2];
|
|
1648
1741
|
|
|
1649
|
-
let len = Math.sqrt(x * x + y * y + z * z);
|
|
1742
|
+
let len = Math.sqrt(x * x + y * y + z * z); // `Math.hypot` is slower.
|
|
1650
1743
|
if (len < epsilon) {
|
|
1651
1744
|
return out;
|
|
1652
1745
|
}
|
|
@@ -1660,6 +1753,14 @@ export const rotate = <T extends Matrix4Like>(
|
|
|
1660
1753
|
const c = Math.cos(radians);
|
|
1661
1754
|
const t = 1 - c;
|
|
1662
1755
|
|
|
1756
|
+
const xs = x * s;
|
|
1757
|
+
const ys = y * s;
|
|
1758
|
+
const zs = z * s;
|
|
1759
|
+
|
|
1760
|
+
const xt = x * t;
|
|
1761
|
+
const yt = y * t;
|
|
1762
|
+
const zt = z * t;
|
|
1763
|
+
|
|
1663
1764
|
const a00 = matrix[0];
|
|
1664
1765
|
const a01 = matrix[1];
|
|
1665
1766
|
const a02 = matrix[2];
|
|
@@ -1673,15 +1774,15 @@ export const rotate = <T extends Matrix4Like>(
|
|
|
1673
1774
|
const a22 = matrix[10];
|
|
1674
1775
|
const a23 = matrix[11];
|
|
1675
1776
|
|
|
1676
|
-
const b00 = x *
|
|
1677
|
-
const b01 = y *
|
|
1678
|
-
const b02 = z *
|
|
1679
|
-
const b10 = x *
|
|
1680
|
-
const b11 = y *
|
|
1681
|
-
const b12 = z *
|
|
1682
|
-
const b20 = x *
|
|
1683
|
-
const b21 = y *
|
|
1684
|
-
const b22 = z *
|
|
1777
|
+
const b00 = x * xt + c;
|
|
1778
|
+
const b01 = y * xt + zs;
|
|
1779
|
+
const b02 = z * xt - ys;
|
|
1780
|
+
const b10 = x * yt - zs;
|
|
1781
|
+
const b11 = y * yt + c;
|
|
1782
|
+
const b12 = z * yt + xs;
|
|
1783
|
+
const b20 = x * zt + ys;
|
|
1784
|
+
const b21 = y * zt - xs;
|
|
1785
|
+
const b22 = z * zt + c;
|
|
1685
1786
|
|
|
1686
1787
|
out[0] = a00 * b00 + a10 * b01 + a20 * b02;
|
|
1687
1788
|
out[1] = a01 * b00 + a11 * b01 + a21 * b02;
|
|
@@ -1695,12 +1796,14 @@ export const rotate = <T extends Matrix4Like>(
|
|
|
1695
1796
|
out[9] = a01 * b20 + a11 * b21 + a21 * b22;
|
|
1696
1797
|
out[10] = a02 * b20 + a12 * b21 + a22 * b22;
|
|
1697
1798
|
out[11] = a03 * b20 + a13 * b21 + a23 * b22;
|
|
1799
|
+
|
|
1698
1800
|
if (out !== matrix) {
|
|
1699
1801
|
out[12] = matrix[12];
|
|
1700
1802
|
out[13] = matrix[13];
|
|
1701
1803
|
out[14] = matrix[14];
|
|
1702
1804
|
out[15] = matrix[15];
|
|
1703
1805
|
}
|
|
1806
|
+
|
|
1704
1807
|
return out;
|
|
1705
1808
|
};
|
|
1706
1809
|
|
|
@@ -1710,7 +1813,8 @@ export const rotate = <T extends Matrix4Like>(
|
|
|
1710
1813
|
* @param r - The angle in radians.
|
|
1711
1814
|
* @param out - The matrix to store the result in.
|
|
1712
1815
|
* @returns The rotated matrix.
|
|
1713
|
-
* @see
|
|
1816
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
1817
|
+
* @public
|
|
1714
1818
|
*/
|
|
1715
1819
|
export const rotateX = <T extends Matrix4Like>(
|
|
1716
1820
|
matrix: Matrix4Like,
|
|
@@ -1757,7 +1861,8 @@ export const rotateX = <T extends Matrix4Like>(
|
|
|
1757
1861
|
* @param r - The angle in radians.
|
|
1758
1862
|
* @param out - The matrix to store the result in.
|
|
1759
1863
|
* @returns The rotated matrix.
|
|
1760
|
-
* @see
|
|
1864
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
1865
|
+
* @public
|
|
1761
1866
|
*/
|
|
1762
1867
|
export const rotateY = <T extends Matrix4Like>(
|
|
1763
1868
|
matrix: Matrix4Like,
|
|
@@ -1804,7 +1909,8 @@ export const rotateY = <T extends Matrix4Like>(
|
|
|
1804
1909
|
* @param r - The angle in radians.
|
|
1805
1910
|
* @param out - The matrix to store the result in.
|
|
1806
1911
|
* @returns The rotated matrix.
|
|
1807
|
-
* @see
|
|
1912
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
1913
|
+
* @public
|
|
1808
1914
|
*/
|
|
1809
1915
|
export const rotateZ = <T extends Matrix4Like>(
|
|
1810
1916
|
matrix: Matrix4Like,
|
|
@@ -1850,17 +1956,13 @@ export const rotateZ = <T extends Matrix4Like>(
|
|
|
1850
1956
|
* @param matrix - The matrix.
|
|
1851
1957
|
* @param out - The vector to store the result in.
|
|
1852
1958
|
* @returns The translation vector.
|
|
1853
|
-
* @see
|
|
1959
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
1960
|
+
* @public
|
|
1854
1961
|
*/
|
|
1855
1962
|
export const getTranslation = <T extends Vector3Like>(
|
|
1856
1963
|
matrix: Matrix4Like,
|
|
1857
1964
|
out: T
|
|
1858
|
-
): T =>
|
|
1859
|
-
out[0] = matrix[12];
|
|
1860
|
-
out[1] = matrix[13];
|
|
1861
|
-
out[2] = matrix[14];
|
|
1862
|
-
return out;
|
|
1863
|
-
};
|
|
1965
|
+
): T => vector3FromValues(matrix[12], matrix[13], matrix[14], out);
|
|
1864
1966
|
|
|
1865
1967
|
/**
|
|
1866
1968
|
* Set the translation vector component of a transformation matrix.
|
|
@@ -1868,7 +1970,8 @@ export const getTranslation = <T extends Vector3Like>(
|
|
|
1868
1970
|
* @param translation - The translation vector.
|
|
1869
1971
|
* @param out - The matrix to store the result in.
|
|
1870
1972
|
* @returns The transformation matrix.
|
|
1871
|
-
* @see
|
|
1973
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
1974
|
+
* @public
|
|
1872
1975
|
*/
|
|
1873
1976
|
export const setTranslation = <T extends Matrix4Like>(
|
|
1874
1977
|
matrix: Matrix4Like,
|
|
@@ -1902,7 +2005,8 @@ export const setTranslation = <T extends Matrix4Like>(
|
|
|
1902
2005
|
* @param matrix - The matrix.
|
|
1903
2006
|
* @param out - The vector to store the result in.
|
|
1904
2007
|
* @returns The scaling vector.
|
|
1905
|
-
* @see
|
|
2008
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2009
|
+
* @public
|
|
1906
2010
|
*/
|
|
1907
2011
|
export const getScaling = <T extends Vector3Like>(
|
|
1908
2012
|
matrix: Matrix4Like,
|
|
@@ -1918,10 +2022,13 @@ export const getScaling = <T extends Vector3Like>(
|
|
|
1918
2022
|
const m32 = matrix[9];
|
|
1919
2023
|
const m33 = matrix[10];
|
|
1920
2024
|
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
2025
|
+
// `Math.hypot` is slower.
|
|
2026
|
+
return vector3FromValues(
|
|
2027
|
+
Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13),
|
|
2028
|
+
Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23),
|
|
2029
|
+
Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33),
|
|
2030
|
+
out
|
|
2031
|
+
);
|
|
1925
2032
|
};
|
|
1926
2033
|
|
|
1927
2034
|
/**
|
|
@@ -1929,17 +2036,18 @@ export const getScaling = <T extends Vector3Like>(
|
|
|
1929
2036
|
* @param matrix - The matrix.
|
|
1930
2037
|
* @param out - The quaternion to store the result in.
|
|
1931
2038
|
* @returns The rotation.
|
|
1932
|
-
* @see
|
|
2039
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2040
|
+
* @public
|
|
1933
2041
|
*/
|
|
1934
2042
|
export const getRotation = <T extends QuaternionLike>(
|
|
1935
2043
|
matrix: Matrix4Like,
|
|
1936
2044
|
out: T
|
|
1937
2045
|
): T => {
|
|
1938
|
-
|
|
2046
|
+
getScaling(matrix, iv3);
|
|
1939
2047
|
|
|
1940
|
-
const is1 = 1 /
|
|
1941
|
-
const is2 = 1 /
|
|
1942
|
-
const is3 = 1 /
|
|
2048
|
+
const is1 = 1 / iv3[0];
|
|
2049
|
+
const is2 = 1 / iv3[1];
|
|
2050
|
+
const is3 = 1 / iv3[2];
|
|
1943
2051
|
|
|
1944
2052
|
const sm11 = matrix[0] * is1;
|
|
1945
2053
|
const sm12 = matrix[1] * is2;
|
|
@@ -1955,42 +2063,51 @@ export const getRotation = <T extends QuaternionLike>(
|
|
|
1955
2063
|
|
|
1956
2064
|
if (trace > 0) {
|
|
1957
2065
|
const s = Math.sqrt(trace + 1) * 2;
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
2066
|
+
return quaternionFromValues(
|
|
2067
|
+
(sm23 - sm32) / s,
|
|
2068
|
+
(sm31 - sm13) / s,
|
|
2069
|
+
(sm12 - sm21) / s,
|
|
2070
|
+
0.25 * s,
|
|
2071
|
+
out
|
|
2072
|
+
);
|
|
1963
2073
|
}
|
|
1964
2074
|
|
|
1965
2075
|
if (sm11 > sm22 && sm11 > sm33) {
|
|
1966
2076
|
const s = Math.sqrt(1 + sm11 - sm22 - sm33) * 2;
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
2077
|
+
return quaternionFromValues(
|
|
2078
|
+
0.25 * s,
|
|
2079
|
+
(sm12 + sm21) / s,
|
|
2080
|
+
(sm31 + sm13) / s,
|
|
2081
|
+
(sm23 - sm32) / s,
|
|
2082
|
+
out
|
|
2083
|
+
);
|
|
1972
2084
|
}
|
|
1973
2085
|
|
|
1974
2086
|
if (sm22 > sm33) {
|
|
1975
2087
|
const s = Math.sqrt(1 + sm22 - sm11 - sm33) * 2;
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
2088
|
+
return quaternionFromValues(
|
|
2089
|
+
(sm12 + sm21) / s,
|
|
2090
|
+
0.25 * s,
|
|
2091
|
+
(sm23 + sm32) / s,
|
|
2092
|
+
(sm31 - sm13) / s,
|
|
2093
|
+
out
|
|
2094
|
+
);
|
|
1981
2095
|
}
|
|
1982
2096
|
|
|
1983
2097
|
const s = Math.sqrt(1 + sm33 - sm11 - sm22) * 2;
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
2098
|
+
return quaternionFromValues(
|
|
2099
|
+
(sm31 + sm13) / s,
|
|
2100
|
+
(sm23 + sm32) / s,
|
|
2101
|
+
0.25 * s,
|
|
2102
|
+
(sm12 - sm21) / s,
|
|
2103
|
+
out
|
|
2104
|
+
);
|
|
1989
2105
|
};
|
|
1990
2106
|
|
|
1991
2107
|
/**
|
|
1992
2108
|
* A four-by-four matrix.
|
|
1993
|
-
* @see
|
|
2109
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_(mathematics) | Matrix}
|
|
2110
|
+
* @public
|
|
1994
2111
|
*/
|
|
1995
2112
|
export default class Matrix4
|
|
1996
2113
|
extends Float32Array
|
|
@@ -2001,7 +2118,7 @@ export default class Matrix4
|
|
|
2001
2118
|
* @param vector - The translation vector.
|
|
2002
2119
|
* @param out - The matrix to store the result in.
|
|
2003
2120
|
* @returns The transformation matrix.
|
|
2004
|
-
* @see
|
|
2121
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2005
2122
|
*/
|
|
2006
2123
|
public static fromTranslation<T extends Matrix4Like = Matrix4>(
|
|
2007
2124
|
vector: Vector3Like,
|
|
@@ -2015,7 +2132,7 @@ export default class Matrix4
|
|
|
2015
2132
|
* @param vector - The scaling vector.
|
|
2016
2133
|
* @param out - The matrix to store the result in.
|
|
2017
2134
|
* @returns The transformation matrix.
|
|
2018
|
-
* @see
|
|
2135
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2019
2136
|
*/
|
|
2020
2137
|
public static fromScaling<T extends Matrix4Like = Matrix4>(
|
|
2021
2138
|
vector: Vector3Like,
|
|
@@ -2030,7 +2147,7 @@ export default class Matrix4
|
|
|
2030
2147
|
* @param axis - The axis to rotate around.
|
|
2031
2148
|
* @param out - The matrix to store the result in.
|
|
2032
2149
|
* @returns The transformation matrix.
|
|
2033
|
-
* @see
|
|
2150
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2034
2151
|
*/
|
|
2035
2152
|
public static fromRotation<T extends Matrix4Like = Matrix4>(
|
|
2036
2153
|
r: number,
|
|
@@ -2045,7 +2162,7 @@ export default class Matrix4
|
|
|
2045
2162
|
* @param r - The angle in radians.
|
|
2046
2163
|
* @param out - The matrix to store the result in.
|
|
2047
2164
|
* @returns The transformation matrix.
|
|
2048
|
-
* @see
|
|
2165
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2049
2166
|
*/
|
|
2050
2167
|
public static fromXRotation<T extends Matrix4Like = Matrix4>(
|
|
2051
2168
|
r: number,
|
|
@@ -2059,7 +2176,7 @@ export default class Matrix4
|
|
|
2059
2176
|
* @param r - The angle in radians.
|
|
2060
2177
|
* @param out - The matrix to store the result in.
|
|
2061
2178
|
* @returns The transformation matrix.
|
|
2062
|
-
* @see
|
|
2179
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2063
2180
|
*/
|
|
2064
2181
|
public static fromYRotation<T extends Matrix4Like = Matrix4>(
|
|
2065
2182
|
r: number,
|
|
@@ -2073,7 +2190,7 @@ export default class Matrix4
|
|
|
2073
2190
|
* @param r - The angle in radians.
|
|
2074
2191
|
* @param out - The matrix to store the result in.
|
|
2075
2192
|
* @returns The transformation matrix.
|
|
2076
|
-
* @see
|
|
2193
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2077
2194
|
*/
|
|
2078
2195
|
public static fromZRotation<T extends Matrix4Like = Matrix4>(
|
|
2079
2196
|
r: number,
|
|
@@ -2088,8 +2205,8 @@ export default class Matrix4
|
|
|
2088
2205
|
* @param translation - The translation vector.
|
|
2089
2206
|
* @param out - The matrix to store the result in.
|
|
2090
2207
|
* @returns The transformation matrix.
|
|
2091
|
-
* @see
|
|
2092
|
-
* @see
|
|
2208
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2209
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2093
2210
|
*/
|
|
2094
2211
|
public static fromRotationTranslation<T extends Matrix4Like = Matrix4>(
|
|
2095
2212
|
rotation: QuaternionLike,
|
|
@@ -2106,8 +2223,8 @@ export default class Matrix4
|
|
|
2106
2223
|
* @param scaling - The scaling vector.
|
|
2107
2224
|
* @param out - The matrix to store the result in.
|
|
2108
2225
|
* @returns The transformation matrix.
|
|
2109
|
-
* @see
|
|
2110
|
-
* @see
|
|
2226
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2227
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2111
2228
|
*/
|
|
2112
2229
|
public static fromRotationTranslationScale<T extends Matrix4Like = Matrix4>(
|
|
2113
2230
|
rotation: QuaternionLike,
|
|
@@ -2126,8 +2243,8 @@ export default class Matrix4
|
|
|
2126
2243
|
* @param origin - The origin vector.
|
|
2127
2244
|
* @param out - The matrix to store the result in.
|
|
2128
2245
|
* @returns The transformation matrix.
|
|
2129
|
-
* @see
|
|
2130
|
-
* @see
|
|
2246
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2247
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2131
2248
|
*/
|
|
2132
2249
|
public static fromRotationTranslationScaleOrigin<
|
|
2133
2250
|
T extends Matrix4Like = Matrix4
|
|
@@ -2152,9 +2269,9 @@ export default class Matrix4
|
|
|
2152
2269
|
* @param quaternion - The dual quaternion.
|
|
2153
2270
|
* @param out - The matrix to store the result in.
|
|
2154
2271
|
* @returns The transformation matrix.
|
|
2155
|
-
* @see
|
|
2156
|
-
* @see
|
|
2157
|
-
* @see
|
|
2272
|
+
* @see {@link https://en.wikipedia.org/wiki/Dual_quaternion | Dual quaternion}
|
|
2273
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2274
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2158
2275
|
*/
|
|
2159
2276
|
public static fromDualQuaternion<T extends Matrix4Like = Matrix4>(
|
|
2160
2277
|
quaternion: DualQuaternionLike,
|
|
@@ -2163,6 +2280,21 @@ export default class Matrix4
|
|
|
2163
2280
|
return fromDualQuaternion(quaternion, out);
|
|
2164
2281
|
}
|
|
2165
2282
|
|
|
2283
|
+
/**
|
|
2284
|
+
* Create a transformation matrix from a quaternion.
|
|
2285
|
+
* @param quaternion - The quaternion.
|
|
2286
|
+
* @param out - The matrix to store the result in.
|
|
2287
|
+
* @returns The transformation matrix.
|
|
2288
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2289
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2290
|
+
*/
|
|
2291
|
+
public static fromQuaternion<T extends Matrix4Like = Matrix4>(
|
|
2292
|
+
quaternion: QuaternionLike,
|
|
2293
|
+
out: T = new Matrix4() as Matrix4 & T
|
|
2294
|
+
): T {
|
|
2295
|
+
return fromQuaternion(quaternion, out);
|
|
2296
|
+
}
|
|
2297
|
+
|
|
2166
2298
|
/**
|
|
2167
2299
|
* Generate a frustum matrix with the given bounds.
|
|
2168
2300
|
* @param left - The left bound of the frustum.
|
|
@@ -2173,8 +2305,8 @@ export default class Matrix4
|
|
|
2173
2305
|
* @param far - The far bound of the frustum.
|
|
2174
2306
|
* @param out - The matrix to store the result in.
|
|
2175
2307
|
* @returns The frustum matrix.
|
|
2176
|
-
* @see
|
|
2177
|
-
* @see
|
|
2308
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
2309
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
2178
2310
|
*/
|
|
2179
2311
|
public static frustum<T extends Matrix4Like = Matrix4>(
|
|
2180
2312
|
left: number,
|
|
@@ -2189,15 +2321,15 @@ export default class Matrix4
|
|
|
2189
2321
|
}
|
|
2190
2322
|
|
|
2191
2323
|
/**
|
|
2192
|
-
* Create a perspective projection matrix with the given bounds.
|
|
2324
|
+
* Create a perspective projection matrix with the given bounds such that the near and far clip planes correspond to a normalized device coordinate Z range of `[-1, 1]` (OpenGL/WebGL).
|
|
2193
2325
|
* @param fov - The vertical field of view in radians.
|
|
2194
2326
|
* @param aspect - The aspect ratio (typically the width of the viewport divided by its height).
|
|
2195
2327
|
* @param near - The near bound of the frustum.
|
|
2196
2328
|
* @param far - The far bound of the frustum.
|
|
2197
2329
|
* @param out - The matrix to store the result in.
|
|
2198
2330
|
* @returns The perspective projection matrix.
|
|
2199
|
-
* @see
|
|
2200
|
-
* @see
|
|
2331
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
2332
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
2201
2333
|
*/
|
|
2202
2334
|
public static perspective<T extends Matrix4Like = Matrix4>(
|
|
2203
2335
|
fov: number,
|
|
@@ -2209,6 +2341,27 @@ export default class Matrix4
|
|
|
2209
2341
|
return perspective(fov, aspect, near, far, out);
|
|
2210
2342
|
}
|
|
2211
2343
|
|
|
2344
|
+
/**
|
|
2345
|
+
* Create a perspective projection matrix with the given bounds such that the near and far clip planes correspond to a normalized device coordinate Z range of `[0, 1]` (WebGPU/Vulkan/DirectX/Metal).
|
|
2346
|
+
* @param fov - The vertical field of view in radians.
|
|
2347
|
+
* @param aspect - The aspect ratio (typically the width of the viewport divided by its height).
|
|
2348
|
+
* @param near - The near bound of the frustum.
|
|
2349
|
+
* @param far - The far bound of the frustum.
|
|
2350
|
+
* @param out - The matrix to store the result in.
|
|
2351
|
+
* @returns The perspective projection matrix.
|
|
2352
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
2353
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
2354
|
+
*/
|
|
2355
|
+
public static perspectiveGpu<T extends Matrix4Like = Matrix4>(
|
|
2356
|
+
fov: number,
|
|
2357
|
+
aspect: number,
|
|
2358
|
+
near: number,
|
|
2359
|
+
far: number,
|
|
2360
|
+
out: T = new Matrix4() as Matrix4 & T
|
|
2361
|
+
): T {
|
|
2362
|
+
return perspectiveGpu(fov, aspect, near, far, out);
|
|
2363
|
+
}
|
|
2364
|
+
|
|
2212
2365
|
/**
|
|
2213
2366
|
* Create a perspective projection matrix from a field of view. Useful for generating projection matrices to be used with the WebXR API.
|
|
2214
2367
|
* @param fov - The field of view.
|
|
@@ -2216,9 +2369,9 @@ export default class Matrix4
|
|
|
2216
2369
|
* @param far - The far bound of the frustum.
|
|
2217
2370
|
* @param out - The matrix to store the result in.
|
|
2218
2371
|
* @returns The perspective projection matrix.
|
|
2219
|
-
* @see
|
|
2220
|
-
* @see
|
|
2221
|
-
* @see
|
|
2372
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
2373
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
2374
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API | WebXR API}
|
|
2222
2375
|
*/
|
|
2223
2376
|
public static perspectiveFromFieldOfView<T extends Matrix4Like = Matrix4>(
|
|
2224
2377
|
fov: FieldOfView,
|
|
@@ -2230,7 +2383,7 @@ export default class Matrix4
|
|
|
2230
2383
|
}
|
|
2231
2384
|
|
|
2232
2385
|
/**
|
|
2233
|
-
* Generate an orthogonal projection matrix with the given bounds.
|
|
2386
|
+
* Generate an orthogonal projection matrix with the given bounds such that the near and far clip planes correspond to a normalized device coordinate Z range of `[-1, 1]` (OpenGL/WebGL).
|
|
2234
2387
|
* @param left - The left bound of the frustum.
|
|
2235
2388
|
* @param right - The right bound of the frustum.
|
|
2236
2389
|
* @param bottom - The bottom bound of the frustum.
|
|
@@ -2239,8 +2392,8 @@ export default class Matrix4
|
|
|
2239
2392
|
* @param far - The far bound of the frustum.
|
|
2240
2393
|
* @param out - The matrix to store the result in.
|
|
2241
2394
|
* @returns The frustum matrix.
|
|
2242
|
-
* @see
|
|
2243
|
-
* @see
|
|
2395
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
2396
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
2244
2397
|
*/
|
|
2245
2398
|
public static ortho<T extends Matrix4Like = Matrix4>(
|
|
2246
2399
|
left: number,
|
|
@@ -2254,6 +2407,31 @@ export default class Matrix4
|
|
|
2254
2407
|
return ortho(left, right, bottom, top, near, far, out);
|
|
2255
2408
|
}
|
|
2256
2409
|
|
|
2410
|
+
/**
|
|
2411
|
+
* Generate an orthogonal projection matrix with the given bounds such that the near and far clip planes correspond to a normalized device coordinate Z range of `[0, 1]` (WebGPU/Vulkan/DirectX/Metal).
|
|
2412
|
+
* @param left - The left bound of the frustum.
|
|
2413
|
+
* @param right - The right bound of the frustum.
|
|
2414
|
+
* @param bottom - The bottom bound of the frustum.
|
|
2415
|
+
* @param top - The top bound of the frustum.
|
|
2416
|
+
* @param near - The near bound of the frustum.
|
|
2417
|
+
* @param far - The far bound of the frustum.
|
|
2418
|
+
* @param out - The matrix to store the result in.
|
|
2419
|
+
* @returns The frustum matrix.
|
|
2420
|
+
* @see {@link https://en.wikipedia.org/wiki/Camera_matrix | Camera matrix}
|
|
2421
|
+
* @see {@link https://en.wikipedia.org/wiki/3D_projection | 3D projection}
|
|
2422
|
+
*/
|
|
2423
|
+
public static orthoGpu<T extends Matrix4Like = Matrix4>(
|
|
2424
|
+
left: number,
|
|
2425
|
+
right: number,
|
|
2426
|
+
bottom: number,
|
|
2427
|
+
top: number,
|
|
2428
|
+
near: number,
|
|
2429
|
+
far: number,
|
|
2430
|
+
out: T = new Matrix4() as Matrix4 & T
|
|
2431
|
+
): T {
|
|
2432
|
+
return orthoGpu(left, right, bottom, top, near, far, out);
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2257
2435
|
/**
|
|
2258
2436
|
* Generate a look-at matrix. If you want a matrix that actually makes an object look at another object, use `targetTo` instead.
|
|
2259
2437
|
* @param eye - The position of the viewer.
|
|
@@ -2261,7 +2439,7 @@ export default class Matrix4
|
|
|
2261
2439
|
* @param up - The local up direction.
|
|
2262
2440
|
* @param out - The matrix to store the result in.
|
|
2263
2441
|
* @returns The look-at matrix.
|
|
2264
|
-
* @see
|
|
2442
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2265
2443
|
*/
|
|
2266
2444
|
public static lookAt<T extends Matrix4Like = Matrix4>(
|
|
2267
2445
|
eye: Vector3Like,
|
|
@@ -2279,7 +2457,7 @@ export default class Matrix4
|
|
|
2279
2457
|
* @param up - The local up direction.
|
|
2280
2458
|
* @param out - The matrix to store the result in.
|
|
2281
2459
|
* @returns The transformation matrix.
|
|
2282
|
-
* @see
|
|
2460
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2283
2461
|
*/
|
|
2284
2462
|
public static targetTo<T extends Matrix4Like = Matrix4>(
|
|
2285
2463
|
eye: Vector3Like,
|
|
@@ -2353,7 +2531,7 @@ export default class Matrix4
|
|
|
2353
2531
|
|
|
2354
2532
|
/**
|
|
2355
2533
|
* Create a four-by-four identity matrix.
|
|
2356
|
-
* @see
|
|
2534
|
+
* @see {@link https://en.wikipedia.org/wiki/Identity_matrix | Identity matrix}
|
|
2357
2535
|
*/
|
|
2358
2536
|
public constructor() {
|
|
2359
2537
|
super(16);
|
|
@@ -2444,7 +2622,7 @@ export default class Matrix4
|
|
|
2444
2622
|
* @param matrix - The other matrix.
|
|
2445
2623
|
* @param out - The matrix to store the result in.
|
|
2446
2624
|
* @returns The sum of the matrices.
|
|
2447
|
-
* @see
|
|
2625
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
2448
2626
|
*/
|
|
2449
2627
|
public add<T extends Matrix4Like = Matrix4>(
|
|
2450
2628
|
matrix: Matrix4Like,
|
|
@@ -2457,7 +2635,7 @@ export default class Matrix4
|
|
|
2457
2635
|
* Calculate the adjugate of this matrix.
|
|
2458
2636
|
* @param out - The matrix to store the result in.
|
|
2459
2637
|
* @returns The adjugate of this matrix.
|
|
2460
|
-
* @see
|
|
2638
|
+
* @see {@link https://en.wikipedia.org/wiki/Adjugate_matrix | Adjugate matrix}
|
|
2461
2639
|
*/
|
|
2462
2640
|
public adjoint<T extends Matrix4Like = Matrix4>(
|
|
2463
2641
|
out: T = new Matrix4() as Matrix4 & T
|
|
@@ -2487,7 +2665,7 @@ export default class Matrix4
|
|
|
2487
2665
|
|
|
2488
2666
|
/**
|
|
2489
2667
|
* Get the Frobenius norm of this matrix.
|
|
2490
|
-
* @see
|
|
2668
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_norm | Matrix norm}
|
|
2491
2669
|
*/
|
|
2492
2670
|
public get frob(): number {
|
|
2493
2671
|
return frob(this);
|
|
@@ -2498,7 +2676,7 @@ export default class Matrix4
|
|
|
2498
2676
|
* @param matrix - The other matrix.
|
|
2499
2677
|
* @param out - The matrix to store the result in.
|
|
2500
2678
|
* @returns The product of the matrices.
|
|
2501
|
-
* @see
|
|
2679
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
2502
2680
|
*/
|
|
2503
2681
|
public multiply<T extends Matrix4Like = Matrix4>(
|
|
2504
2682
|
matrix: Matrix4Like,
|
|
@@ -2512,7 +2690,7 @@ export default class Matrix4
|
|
|
2512
2690
|
* @param scalar - The scalar value.
|
|
2513
2691
|
* @param out - The matrix to store the result in.
|
|
2514
2692
|
* @returns The product of the matrix and the scalar value.
|
|
2515
|
-
* @see
|
|
2693
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
2516
2694
|
*/
|
|
2517
2695
|
public multiplyScalar<T extends Matrix4Like = Matrix4>(
|
|
2518
2696
|
scalar: number,
|
|
@@ -2527,8 +2705,8 @@ export default class Matrix4
|
|
|
2527
2705
|
* @param scalar - The scalar.
|
|
2528
2706
|
* @param out - The matrix to store the result in.
|
|
2529
2707
|
* @returns The sum.
|
|
2530
|
-
* @see
|
|
2531
|
-
* @see
|
|
2708
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
2709
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_multiplication | Matrix multiplication}
|
|
2532
2710
|
*/
|
|
2533
2711
|
public multiplyScalarAndAdd<T extends Matrix4Like = Matrix4>(
|
|
2534
2712
|
matrix: Matrix4Like,
|
|
@@ -2543,7 +2721,7 @@ export default class Matrix4
|
|
|
2543
2721
|
* @param matrix - The other matrix.
|
|
2544
2722
|
* @param out - The matrix to store the result in.
|
|
2545
2723
|
* @returns The difference between the matrices.
|
|
2546
|
-
* @see
|
|
2724
|
+
* @see {@link https://en.wikipedia.org/wiki/Matrix_addition | Matrix addition}
|
|
2547
2725
|
*/
|
|
2548
2726
|
public subtract<T extends Matrix4Like = Matrix4>(
|
|
2549
2727
|
matrix: Matrix4Like,
|
|
@@ -2556,7 +2734,7 @@ export default class Matrix4
|
|
|
2556
2734
|
* Transpose this matrix.
|
|
2557
2735
|
* @param out - The matrix to store the result in.
|
|
2558
2736
|
* @returns The transpose of this matrix.
|
|
2559
|
-
* @see
|
|
2737
|
+
* @see {@link https://en.wikipedia.org/wiki/Transpose | Transpose}
|
|
2560
2738
|
*/
|
|
2561
2739
|
public transpose<T extends Matrix4Like = Matrix4>(
|
|
2562
2740
|
out: T = new Matrix4() as Matrix4 & T
|
|
@@ -2566,7 +2744,7 @@ export default class Matrix4
|
|
|
2566
2744
|
|
|
2567
2745
|
/**
|
|
2568
2746
|
* Get the determinant of this matrix.
|
|
2569
|
-
* @see
|
|
2747
|
+
* @see {@link https://en.wikipedia.org/wiki/Determinant | Determinant}
|
|
2570
2748
|
*/
|
|
2571
2749
|
public get determinant(): number {
|
|
2572
2750
|
return determinant(this);
|
|
@@ -2575,7 +2753,7 @@ export default class Matrix4
|
|
|
2575
2753
|
/**
|
|
2576
2754
|
* Reset this matrix to identity.
|
|
2577
2755
|
* @returns This matrix.
|
|
2578
|
-
* @see
|
|
2756
|
+
* @see {@link https://en.wikipedia.org/wiki/Identity_matrix | Identity matrix}
|
|
2579
2757
|
*/
|
|
2580
2758
|
public identity(): this {
|
|
2581
2759
|
return identity(this);
|
|
@@ -2585,7 +2763,7 @@ export default class Matrix4
|
|
|
2585
2763
|
* Invert this matrix.
|
|
2586
2764
|
* @param out - The matrix to store the result in.
|
|
2587
2765
|
* @returns The inverted matrix.
|
|
2588
|
-
* @see
|
|
2766
|
+
* @see {@link https://en.wikipedia.org/wiki/Invertible_matrix | Invertible matrix}
|
|
2589
2767
|
*/
|
|
2590
2768
|
public invert<T extends Matrix4Like = Matrix4>(
|
|
2591
2769
|
out: T = new Matrix4() as Matrix4 & T
|
|
@@ -2598,7 +2776,7 @@ export default class Matrix4
|
|
|
2598
2776
|
* @param vector - The scaling vector.
|
|
2599
2777
|
* @param out - The matrix to store the result in.
|
|
2600
2778
|
* @returns The scaled matrix.
|
|
2601
|
-
* @see
|
|
2779
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2602
2780
|
*/
|
|
2603
2781
|
public scale<T extends Matrix4Like = Matrix4>(
|
|
2604
2782
|
vector: Vector3Like,
|
|
@@ -2612,7 +2790,7 @@ export default class Matrix4
|
|
|
2612
2790
|
* @param vector - The translation vector.
|
|
2613
2791
|
* @param out - The matrix to store the result in.
|
|
2614
2792
|
* @returns The translated matrix.
|
|
2615
|
-
* @see
|
|
2793
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2616
2794
|
*/
|
|
2617
2795
|
public translate<T extends Matrix4Like = Matrix4>(
|
|
2618
2796
|
vector: Vector3Like,
|
|
@@ -2627,7 +2805,7 @@ export default class Matrix4
|
|
|
2627
2805
|
* @param axis - The axis.
|
|
2628
2806
|
* @param out - The matrix to store the result in.
|
|
2629
2807
|
* @returns The rotated matrix.
|
|
2630
|
-
* @see
|
|
2808
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2631
2809
|
*/
|
|
2632
2810
|
public rotate<T extends Matrix4Like = Matrix4>(
|
|
2633
2811
|
r: number,
|
|
@@ -2642,7 +2820,7 @@ export default class Matrix4
|
|
|
2642
2820
|
* @param r - The angle in radians.
|
|
2643
2821
|
* @param out - The matrix to store the result in.
|
|
2644
2822
|
* @returns The rotated matrix.
|
|
2645
|
-
* @see
|
|
2823
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2646
2824
|
*/
|
|
2647
2825
|
public rotateX<T extends Matrix4Like = Matrix4>(
|
|
2648
2826
|
r: number,
|
|
@@ -2656,7 +2834,7 @@ export default class Matrix4
|
|
|
2656
2834
|
* @param r - The angle in radians.
|
|
2657
2835
|
* @param out - The matrix to store the result in.
|
|
2658
2836
|
* @returns The rotated matrix.
|
|
2659
|
-
* @see
|
|
2837
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2660
2838
|
*/
|
|
2661
2839
|
public rotateY<T extends Matrix4Like = Matrix4>(
|
|
2662
2840
|
r: number,
|
|
@@ -2670,7 +2848,7 @@ export default class Matrix4
|
|
|
2670
2848
|
* @param r - The angle in radians.
|
|
2671
2849
|
* @param out - The matrix to store the result in.
|
|
2672
2850
|
* @returns The rotated matrix.
|
|
2673
|
-
* @see
|
|
2851
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2674
2852
|
*/
|
|
2675
2853
|
public rotateZ<T extends Matrix4Like = Matrix4>(
|
|
2676
2854
|
r: number,
|
|
@@ -2683,7 +2861,7 @@ export default class Matrix4
|
|
|
2683
2861
|
* Get the translation vector component of this transformation matrix.
|
|
2684
2862
|
* @param out - The vector to store the result in.
|
|
2685
2863
|
* @returns The translation.
|
|
2686
|
-
* @see
|
|
2864
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2687
2865
|
*/
|
|
2688
2866
|
public getTranslation<T extends Vector3Like = Vector3>(
|
|
2689
2867
|
out: T = new Vector3() as Vector3 & T
|
|
@@ -2696,7 +2874,7 @@ export default class Matrix4
|
|
|
2696
2874
|
* @param translation - The translation vector.
|
|
2697
2875
|
* @param out - The matrix to store the result in.
|
|
2698
2876
|
* @returns The transformation matrix.
|
|
2699
|
-
* @see
|
|
2877
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2700
2878
|
*/
|
|
2701
2879
|
public setTranslation<T extends Matrix4Like = Matrix4>(
|
|
2702
2880
|
translation: Vector3Like,
|
|
@@ -2709,7 +2887,7 @@ export default class Matrix4
|
|
|
2709
2887
|
* Get the scaling vector component of this transformation matrix.
|
|
2710
2888
|
* @param out - The vector to store the result in.
|
|
2711
2889
|
* @returns The scaling.
|
|
2712
|
-
* @see
|
|
2890
|
+
* @see {@link https://en.wikipedia.org/wiki/Transformation_matrix | Transformation matrix}
|
|
2713
2891
|
*/
|
|
2714
2892
|
public getScaling<T extends Vector3Like = Vector3>(
|
|
2715
2893
|
out: T = new Vector3() as Vector3 & T
|
|
@@ -2721,7 +2899,7 @@ export default class Matrix4
|
|
|
2721
2899
|
* Get the scaling vector component of this transformation matrix.
|
|
2722
2900
|
* @param out - The quaternion to store the result in.
|
|
2723
2901
|
* @returns The rotation.
|
|
2724
|
-
* @see
|
|
2902
|
+
* @see {@link https://en.wikipedia.org/wiki/Rotation_matrix | Rotation matrix}
|
|
2725
2903
|
*/
|
|
2726
2904
|
public getRotation<T extends QuaternionLike = Quaternion>(
|
|
2727
2905
|
out: T = new Quaternion() as Quaternion & T
|