glmaths 0.0.1
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 +952 -0
- package/benchmark.js +747 -0
- package/dist/cjs/glmaths.d.ts +12053 -0
- package/dist/cjs/glmaths.js +6496 -0
- package/dist/cjs/glmaths.js.map +1 -0
- package/dist/cjs/glmaths.min.js +2 -0
- package/dist/cjs/glmaths.min.js.map +1 -0
- package/dist/esm/glmaths.d.ts +12053 -0
- package/dist/esm/glmaths.js +6453 -0
- package/dist/esm/glmaths.js.map +1 -0
- package/dist/esm/glmaths.min.js +2 -0
- package/dist/esm/glmaths.min.js.map +1 -0
- package/dist/glmaths.d.ts +12053 -0
- package/dist/glmaths.js +6501 -0
- package/dist/glmaths.js.map +1 -0
- package/dist/glmaths.min.js +2 -0
- package/dist/glmaths.min.js.map +1 -0
- package/docs.js +64 -0
- package/package.json +37 -0
- package/rollup.config.js +70 -0
- package/src/index.ts +19 -0
- package/src/internalUtils.ts +78 -0
- package/src/mat2.ts +324 -0
- package/src/mat2x3.ts +328 -0
- package/src/mat3.ts +629 -0
- package/src/mat4.ts +1319 -0
- package/src/quat.ts +819 -0
- package/src/quat2.ts +412 -0
- package/src/utils.ts +248 -0
- package/src/vec2.ts +798 -0
- package/src/vec3.ts +1069 -0
- package/src/vec4.ts +810 -0
- package/tests/mat2.test.ts +277 -0
- package/tests/mat2x3.test.ts +217 -0
- package/tests/mat3.test.ts +306 -0
- package/tests/mat4.test.ts +586 -0
- package/tests/quat.test.ts +418 -0
- package/tests/quat2.test.ts +222 -0
- package/tests/utils.test.ts +115 -0
- package/tests/vec2.test.ts +617 -0
- package/tests/vec3.test.ts +649 -0
- package/tests/vec4.test.ts +390 -0
- package/tsconfig.json +17 -0
- package/tsconfig.test.json +8 -0
package/src/mat3.ts
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
import glm from '.'
|
|
2
|
+
import { equals } from './internalUtils'
|
|
3
|
+
import { Vec2 } from './vec2'
|
|
4
|
+
import { Vec3 } from './vec3'
|
|
5
|
+
import { Quat } from './quat'
|
|
6
|
+
import { Mat2x3 } from './mat2x3'
|
|
7
|
+
import { Mat4 } from './mat4'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 3x3 Matrix in column-major order
|
|
11
|
+
* @extends Float32Array
|
|
12
|
+
*/
|
|
13
|
+
export class Mat3 extends Float32Array {
|
|
14
|
+
|
|
15
|
+
static get identity() { return new Mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
|
|
16
|
+
static get Identity() { return new Mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
|
|
17
|
+
static get IDENTITY() { return new Mat3(1, 0, 0, 0, 1, 0, 0, 0, 1) }
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a new mat3 with the given values
|
|
21
|
+
*
|
|
22
|
+
* @param {Number} m00 Component in column 0, row 0 position (index 0)
|
|
23
|
+
* @param {Number} m01 Component in column 0, row 1 position (index 1)
|
|
24
|
+
* @param {Number} m02 Component in column 0, row 2 position (index 2)
|
|
25
|
+
* @param {Number} m10 Component in column 1, row 0 position (index 3)
|
|
26
|
+
* @param {Number} m11 Component in column 1, row 1 position (index 4)
|
|
27
|
+
* @param {Number} m12 Component in column 1, row 2 position (index 5)
|
|
28
|
+
* @param {Number} m20 Component in column 2, row 0 position (index 6)
|
|
29
|
+
* @param {Number} m21 Component in column 2, row 1 position (index 7)
|
|
30
|
+
* @param {Number} m22 Component in column 2, row 2 position (index 8)
|
|
31
|
+
*/
|
|
32
|
+
constructor(m00 = 0, m01 = 0, m02 = 0, m10 = 0, m11 = 0, m12 = 0, m20 = 0, m21 = 0, m22 = 0) {
|
|
33
|
+
super(9)
|
|
34
|
+
this[0] = m00
|
|
35
|
+
this[1] = m01
|
|
36
|
+
this[2] = m02
|
|
37
|
+
this[3] = m10
|
|
38
|
+
this[4] = m11
|
|
39
|
+
this[5] = m12
|
|
40
|
+
this[6] = m20
|
|
41
|
+
this[7] = m21
|
|
42
|
+
this[8] = m22
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new mat3 initialized with values from a matrix
|
|
47
|
+
*
|
|
48
|
+
* @returns {Mat3} a new Mat3
|
|
49
|
+
*/
|
|
50
|
+
clone() {
|
|
51
|
+
return new Mat3(this[0], this[1], this[2], this[3], this[4], this[5], this[6], this[7], this[8])
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Transposes a mat3
|
|
56
|
+
*
|
|
57
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
58
|
+
* @returns {Mat3} out
|
|
59
|
+
*/
|
|
60
|
+
transpose(out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
61
|
+
if (out === this) {
|
|
62
|
+
const a01 = this[1], a02 = this[2], a12 = this[5]
|
|
63
|
+
out[1] = this[3]
|
|
64
|
+
out[2] = this[6]
|
|
65
|
+
out[3] = a01
|
|
66
|
+
out[5] = this[7]
|
|
67
|
+
out[6] = a02
|
|
68
|
+
out[7] = a12
|
|
69
|
+
} else {
|
|
70
|
+
out[0] = this[0]
|
|
71
|
+
out[1] = this[3]
|
|
72
|
+
out[2] = this[6]
|
|
73
|
+
out[3] = this[1]
|
|
74
|
+
out[4] = this[4]
|
|
75
|
+
out[5] = this[7]
|
|
76
|
+
out[6] = this[2]
|
|
77
|
+
out[7] = this[5]
|
|
78
|
+
out[8] = this[8]
|
|
79
|
+
}
|
|
80
|
+
return out
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Inverts a mat3
|
|
85
|
+
*
|
|
86
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
87
|
+
* @returns {Mat3|null} out, or null if not invertible
|
|
88
|
+
*/
|
|
89
|
+
invert(out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
90
|
+
const a00 = this[0], a01 = this[1], a02 = this[2]
|
|
91
|
+
const a10 = this[3], a11 = this[4], a12 = this[5]
|
|
92
|
+
const a20 = this[6], a21 = this[7], a22 = this[8]
|
|
93
|
+
|
|
94
|
+
const b01 = a22 * a11 - a12 * a21
|
|
95
|
+
const b11 = -a22 * a10 + a12 * a20
|
|
96
|
+
const b21 = a21 * a10 - a11 * a20
|
|
97
|
+
|
|
98
|
+
let det = a00 * b01 + a01 * b11 + a02 * b21
|
|
99
|
+
if (!det) return null
|
|
100
|
+
det = 1.0 / det
|
|
101
|
+
|
|
102
|
+
out[0] = b01 * det
|
|
103
|
+
out[1] = (-a22 * a01 + a02 * a21) * det
|
|
104
|
+
out[2] = (a12 * a01 - a02 * a11) * det
|
|
105
|
+
out[3] = b11 * det
|
|
106
|
+
out[4] = (a22 * a00 - a02 * a20) * det
|
|
107
|
+
out[5] = (-a12 * a00 + a02 * a10) * det
|
|
108
|
+
out[6] = b21 * det
|
|
109
|
+
out[7] = (-a21 * a00 + a01 * a20) * det
|
|
110
|
+
out[8] = (a11 * a00 - a01 * a10) * det
|
|
111
|
+
return out
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Calculates the adjugate of a mat3
|
|
116
|
+
*
|
|
117
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
118
|
+
* @returns {Mat3} out
|
|
119
|
+
*/
|
|
120
|
+
adjoint(out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
121
|
+
const a00 = this[0], a01 = this[1], a02 = this[2]
|
|
122
|
+
const a10 = this[3], a11 = this[4], a12 = this[5]
|
|
123
|
+
const a20 = this[6], a21 = this[7], a22 = this[8]
|
|
124
|
+
|
|
125
|
+
out[0] = a11 * a22 - a12 * a21
|
|
126
|
+
out[1] = a02 * a21 - a01 * a22
|
|
127
|
+
out[2] = a01 * a12 - a02 * a11
|
|
128
|
+
out[3] = a12 * a20 - a10 * a22
|
|
129
|
+
out[4] = a00 * a22 - a02 * a20
|
|
130
|
+
out[5] = a02 * a10 - a00 * a12
|
|
131
|
+
out[6] = a10 * a21 - a11 * a20
|
|
132
|
+
out[7] = a01 * a20 - a00 * a21
|
|
133
|
+
out[8] = a00 * a11 - a01 * a10
|
|
134
|
+
return out
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Calculates the determinant of this mat3
|
|
139
|
+
*
|
|
140
|
+
* @returns {Number} determinant of this mat3
|
|
141
|
+
*/
|
|
142
|
+
determinant() {
|
|
143
|
+
const a00 = this[0], a01 = this[1], a02 = this[2]
|
|
144
|
+
const a10 = this[3], a11 = this[4], a12 = this[5]
|
|
145
|
+
const a20 = this[6], a21 = this[7], a22 = this[8]
|
|
146
|
+
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Multiplies two mat3's
|
|
151
|
+
*
|
|
152
|
+
* @param {Mat3} b the second operand
|
|
153
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
154
|
+
* @returns {Mat3} out
|
|
155
|
+
*/
|
|
156
|
+
multiply(b: Vec2): Vec2
|
|
157
|
+
multiply(b: Vec3): Vec3
|
|
158
|
+
multiply(b: Mat3, out?: Mat3): Mat3
|
|
159
|
+
multiply(b: Mat3 | Vec3 | Vec2, out: Mat3 = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
160
|
+
if (b instanceof Vec2)
|
|
161
|
+
return b.transformMat3(this)
|
|
162
|
+
if (b instanceof Vec3)
|
|
163
|
+
return b.transformMat3(this)
|
|
164
|
+
|
|
165
|
+
const a00 = this[0], a01 = this[1], a02 = this[2]
|
|
166
|
+
const a10 = this[3], a11 = this[4], a12 = this[5]
|
|
167
|
+
const a20 = this[6], a21 = this[7], a22 = this[8]
|
|
168
|
+
|
|
169
|
+
const b00 = b[0], b01 = b[1], b02 = b[2]
|
|
170
|
+
const b10 = b[3], b11 = b[4], b12 = b[5]
|
|
171
|
+
const b20 = b[6], b21 = b[7], b22 = b[8]
|
|
172
|
+
|
|
173
|
+
out[0] = b00 * a00 + b01 * a10 + b02 * a20
|
|
174
|
+
out[1] = b00 * a01 + b01 * a11 + b02 * a21
|
|
175
|
+
out[2] = b00 * a02 + b01 * a12 + b02 * a22
|
|
176
|
+
out[3] = b10 * a00 + b11 * a10 + b12 * a20
|
|
177
|
+
out[4] = b10 * a01 + b11 * a11 + b12 * a21
|
|
178
|
+
out[5] = b10 * a02 + b11 * a12 + b12 * a22
|
|
179
|
+
out[6] = b20 * a00 + b21 * a10 + b22 * a20
|
|
180
|
+
out[7] = b20 * a01 + b21 * a11 + b22 * a21
|
|
181
|
+
out[8] = b20 * a02 + b21 * a12 + b22 * a22
|
|
182
|
+
return out
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Translates a mat3 by the given vector
|
|
187
|
+
*
|
|
188
|
+
* @param {Vec2} v vector to translate by
|
|
189
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
190
|
+
* @returns {Mat3} out
|
|
191
|
+
*/
|
|
192
|
+
translate(v: Vec2, out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
193
|
+
const a00 = this[0], a01 = this[1], a02 = this[2]
|
|
194
|
+
const a10 = this[3], a11 = this[4], a12 = this[5]
|
|
195
|
+
const a20 = this[6], a21 = this[7], a22 = this[8]
|
|
196
|
+
const x = v[0], y = v[1]
|
|
197
|
+
|
|
198
|
+
out[0] = a00
|
|
199
|
+
out[1] = a01
|
|
200
|
+
out[2] = a02
|
|
201
|
+
out[3] = a10
|
|
202
|
+
out[4] = a11
|
|
203
|
+
out[5] = a12
|
|
204
|
+
out[6] = x * a00 + y * a10 + a20
|
|
205
|
+
out[7] = x * a01 + y * a11 + a21
|
|
206
|
+
out[8] = x * a02 + y * a12 + a22
|
|
207
|
+
return out
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Rotates a mat3 by the given angle
|
|
212
|
+
*
|
|
213
|
+
* @param {Number} rad the angle to rotate the matrix by
|
|
214
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
215
|
+
* @returns {Mat3} out
|
|
216
|
+
*/
|
|
217
|
+
rotate(rad: number, out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
218
|
+
const a00 = this[0], a01 = this[1], a02 = this[2]
|
|
219
|
+
const a10 = this[3], a11 = this[4], a12 = this[5]
|
|
220
|
+
const a20 = this[6], a21 = this[7], a22 = this[8]
|
|
221
|
+
const s = Math.sin(rad), c = Math.cos(rad)
|
|
222
|
+
|
|
223
|
+
out[0] = c * a00 + s * a10
|
|
224
|
+
out[1] = c * a01 + s * a11
|
|
225
|
+
out[2] = c * a02 + s * a12
|
|
226
|
+
out[3] = c * a10 - s * a00
|
|
227
|
+
out[4] = c * a11 - s * a01
|
|
228
|
+
out[5] = c * a12 - s * a02
|
|
229
|
+
out[6] = a20
|
|
230
|
+
out[7] = a21
|
|
231
|
+
out[8] = a22
|
|
232
|
+
return out
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Scales a mat3 by the given vector
|
|
237
|
+
*
|
|
238
|
+
* @param {Vec2} v the vector to scale by
|
|
239
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
240
|
+
* @returns {Mat3} out
|
|
241
|
+
*/
|
|
242
|
+
scale(v: Vec2, out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
243
|
+
const x = v[0], y = v[1]
|
|
244
|
+
out[0] = x * this[0]
|
|
245
|
+
out[1] = x * this[1]
|
|
246
|
+
out[2] = x * this[2]
|
|
247
|
+
out[3] = y * this[3]
|
|
248
|
+
out[4] = y * this[4]
|
|
249
|
+
out[5] = y * this[5]
|
|
250
|
+
out[6] = this[6]
|
|
251
|
+
out[7] = this[7]
|
|
252
|
+
out[8] = this[8]
|
|
253
|
+
return out
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Creates a matrix from a translation vector
|
|
258
|
+
*
|
|
259
|
+
* @param {Vec2} v translation vector
|
|
260
|
+
* @param {Mat3} out the receiving matrix, defaults to new Mat3()
|
|
261
|
+
* @returns {Mat3} out
|
|
262
|
+
*/
|
|
263
|
+
static fromTranslation(v: Vec2, out = new Mat3()) {
|
|
264
|
+
out[0] = 1
|
|
265
|
+
out[1] = 0
|
|
266
|
+
out[2] = 0
|
|
267
|
+
out[3] = 0
|
|
268
|
+
out[4] = 1
|
|
269
|
+
out[5] = 0
|
|
270
|
+
out[6] = v[0]
|
|
271
|
+
out[7] = v[1]
|
|
272
|
+
out[8] = 1
|
|
273
|
+
return out
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Creates a matrix from a given angle
|
|
278
|
+
*
|
|
279
|
+
* @param {Number} rad the angle to rotate the matrix by
|
|
280
|
+
* @param {Mat3} out the receiving matrix, defaults to new Mat3()
|
|
281
|
+
* @returns {Mat3} out
|
|
282
|
+
*/
|
|
283
|
+
static fromRotation(rad: number, out = new Mat3()) {
|
|
284
|
+
const s = Math.sin(rad), c = Math.cos(rad)
|
|
285
|
+
out[0] = c
|
|
286
|
+
out[1] = s
|
|
287
|
+
out[2] = 0
|
|
288
|
+
out[3] = -s
|
|
289
|
+
out[4] = c
|
|
290
|
+
out[5] = 0
|
|
291
|
+
out[6] = 0
|
|
292
|
+
out[7] = 0
|
|
293
|
+
out[8] = 1
|
|
294
|
+
return out
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Creates a matrix from a scaling vector
|
|
299
|
+
*
|
|
300
|
+
* @param {Vec2} v scaling vector
|
|
301
|
+
* @param {Mat3} out the receiving matrix, defaults to new Mat3()
|
|
302
|
+
* @returns {Mat3} out
|
|
303
|
+
*/
|
|
304
|
+
static fromScaling(v: Vec2, out = new Mat3()) {
|
|
305
|
+
out[0] = v[0]
|
|
306
|
+
out[1] = 0
|
|
307
|
+
out[2] = 0
|
|
308
|
+
out[3] = 0
|
|
309
|
+
out[4] = v[1]
|
|
310
|
+
out[5] = 0
|
|
311
|
+
out[6] = 0
|
|
312
|
+
out[7] = 0
|
|
313
|
+
out[8] = 1
|
|
314
|
+
return out
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Creates a mat3 from a Mat2x3
|
|
319
|
+
*
|
|
320
|
+
* @param {Mat2x3} a the Mat2x3 to convert
|
|
321
|
+
* @param {Mat3} out the receiving matrix, defaults to new Mat3()
|
|
322
|
+
* @returns {Mat3} out
|
|
323
|
+
*/
|
|
324
|
+
static fromMat2x3(a: Mat2x3, out = new Mat3()) {
|
|
325
|
+
out[0] = a[0]
|
|
326
|
+
out[1] = a[1]
|
|
327
|
+
out[2] = 0
|
|
328
|
+
out[3] = a[2]
|
|
329
|
+
out[4] = a[3]
|
|
330
|
+
out[5] = 0
|
|
331
|
+
out[6] = a[4]
|
|
332
|
+
out[7] = a[5]
|
|
333
|
+
out[8] = 1
|
|
334
|
+
return out
|
|
335
|
+
}
|
|
336
|
+
static fromMat2d: (a: Mat2x3, out?: Mat3) => Mat3
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Calculates a mat3 from the given quaternion
|
|
340
|
+
*
|
|
341
|
+
* @param {Quat} q quaternion to create matrix from
|
|
342
|
+
* @param {Mat3} out the receiving matrix, defaults to new Mat3()
|
|
343
|
+
* @returns {Mat3} out
|
|
344
|
+
*/
|
|
345
|
+
static fromQuat(q: Quat, out = new Mat3()) {
|
|
346
|
+
const x = q[0], y = q[1], z = q[2], w = q[3]
|
|
347
|
+
const x2 = x + x, y2 = y + y, z2 = z + z
|
|
348
|
+
const xx = x * x2, yx = y * x2, yy = y * y2
|
|
349
|
+
const zx = z * x2, zy = z * y2, zz = z * z2
|
|
350
|
+
const wx = w * x2, wy = w * y2, wz = w * z2
|
|
351
|
+
|
|
352
|
+
out[0] = 1 - yy - zz
|
|
353
|
+
out[3] = yx - wz
|
|
354
|
+
out[6] = zx + wy
|
|
355
|
+
|
|
356
|
+
out[1] = yx + wz
|
|
357
|
+
out[4] = 1 - xx - zz
|
|
358
|
+
out[7] = zy - wx
|
|
359
|
+
|
|
360
|
+
out[2] = zx - wy
|
|
361
|
+
out[5] = zy + wx
|
|
362
|
+
out[8] = 1 - xx - yy
|
|
363
|
+
return out
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Calculates a mat3 normal matrix (transpose inverse) from a mat4
|
|
368
|
+
*
|
|
369
|
+
* @param {Mat4} a the source mat4 to derive the normal matrix from
|
|
370
|
+
* @param {Mat3} out the receiving matrix, defaults to new Mat3()
|
|
371
|
+
* @returns {Mat3|null} out, or null if not invertible
|
|
372
|
+
*/
|
|
373
|
+
static normalFromMat4(a: Mat4, out = new Mat3()) {
|
|
374
|
+
const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]
|
|
375
|
+
const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]
|
|
376
|
+
const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]
|
|
377
|
+
const a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]
|
|
378
|
+
|
|
379
|
+
const b00 = a00 * a11 - a01 * a10
|
|
380
|
+
const b01 = a00 * a12 - a02 * a10
|
|
381
|
+
const b02 = a00 * a13 - a03 * a10
|
|
382
|
+
const b03 = a01 * a12 - a02 * a11
|
|
383
|
+
const b04 = a01 * a13 - a03 * a11
|
|
384
|
+
const b05 = a02 * a13 - a03 * a12
|
|
385
|
+
const b06 = a20 * a31 - a21 * a30
|
|
386
|
+
const b07 = a20 * a32 - a22 * a30
|
|
387
|
+
const b08 = a20 * a33 - a23 * a30
|
|
388
|
+
const b09 = a21 * a32 - a22 * a31
|
|
389
|
+
const b10 = a21 * a33 - a23 * a31
|
|
390
|
+
const b11 = a22 * a33 - a23 * a32
|
|
391
|
+
|
|
392
|
+
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06
|
|
393
|
+
if (!det) return null
|
|
394
|
+
det = 1.0 / det
|
|
395
|
+
|
|
396
|
+
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det
|
|
397
|
+
out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det
|
|
398
|
+
out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det
|
|
399
|
+
out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det
|
|
400
|
+
out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det
|
|
401
|
+
out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det
|
|
402
|
+
out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det
|
|
403
|
+
out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det
|
|
404
|
+
out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det
|
|
405
|
+
return out
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Copies the upper-left 3x3 values of a mat4 into a mat3
|
|
410
|
+
*
|
|
411
|
+
* @param {Mat4} a the source mat4
|
|
412
|
+
* @param {Mat3} out the receiving matrix, defaults to new Mat3()
|
|
413
|
+
* @returns {Mat3} out
|
|
414
|
+
*/
|
|
415
|
+
static fromMat4(a: Mat4, out = new Mat3()) {
|
|
416
|
+
out[0] = a[0]
|
|
417
|
+
out[1] = a[1]
|
|
418
|
+
out[2] = a[2]
|
|
419
|
+
out[3] = a[4]
|
|
420
|
+
out[4] = a[5]
|
|
421
|
+
out[5] = a[6]
|
|
422
|
+
out[6] = a[8]
|
|
423
|
+
out[7] = a[9]
|
|
424
|
+
out[8] = a[10]
|
|
425
|
+
return out
|
|
426
|
+
}
|
|
427
|
+
static fromMat4x4: (a: Mat4, out?: Mat3) => Mat3
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Generates a 2D projection matrix with the given bounds
|
|
431
|
+
*
|
|
432
|
+
* @param {Number} width width of the projection
|
|
433
|
+
* @param {Number} height height of the projection
|
|
434
|
+
* @param {Mat3} out the receiving matrix, defaults to new Mat3()
|
|
435
|
+
* @returns {Mat3} out
|
|
436
|
+
*/
|
|
437
|
+
static projection(width: number, height: number, out = new Mat3()) {
|
|
438
|
+
out[0] = 2 / width
|
|
439
|
+
out[1] = 0
|
|
440
|
+
out[2] = 0
|
|
441
|
+
out[3] = 0
|
|
442
|
+
out[4] = -2 / height
|
|
443
|
+
out[5] = 0
|
|
444
|
+
out[6] = -1
|
|
445
|
+
out[7] = 1
|
|
446
|
+
out[8] = 1
|
|
447
|
+
return out
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Returns Frobenius norm of this mat3
|
|
452
|
+
*
|
|
453
|
+
* @returns {Number} Frobenius norm
|
|
454
|
+
*/
|
|
455
|
+
frob() {
|
|
456
|
+
return Math.sqrt(
|
|
457
|
+
this[0] * this[0] + this[1] * this[1] + this[2] * this[2] +
|
|
458
|
+
this[3] * this[3] + this[4] * this[4] + this[5] * this[5] +
|
|
459
|
+
this[6] * this[6] + this[7] * this[7] + this[8] * this[8]
|
|
460
|
+
)
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Adds two mat3's
|
|
465
|
+
*
|
|
466
|
+
* @param {Mat3} b the second operand
|
|
467
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
468
|
+
* @returns {Mat3} out
|
|
469
|
+
*/
|
|
470
|
+
plus(b: Mat3, out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
471
|
+
out[0] = this[0] + b[0]
|
|
472
|
+
out[1] = this[1] + b[1]
|
|
473
|
+
out[2] = this[2] + b[2]
|
|
474
|
+
out[3] = this[3] + b[3]
|
|
475
|
+
out[4] = this[4] + b[4]
|
|
476
|
+
out[5] = this[5] + b[5]
|
|
477
|
+
out[6] = this[6] + b[6]
|
|
478
|
+
out[7] = this[7] + b[7]
|
|
479
|
+
out[8] = this[8] + b[8]
|
|
480
|
+
return out
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Subtracts matrix b from this
|
|
485
|
+
*
|
|
486
|
+
* @param {Mat3} b the second operand
|
|
487
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
488
|
+
* @returns {Mat3} out
|
|
489
|
+
*/
|
|
490
|
+
minus(b: Mat3, out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
491
|
+
out[0] = this[0] - b[0]
|
|
492
|
+
out[1] = this[1] - b[1]
|
|
493
|
+
out[2] = this[2] - b[2]
|
|
494
|
+
out[3] = this[3] - b[3]
|
|
495
|
+
out[4] = this[4] - b[4]
|
|
496
|
+
out[5] = this[5] - b[5]
|
|
497
|
+
out[6] = this[6] - b[6]
|
|
498
|
+
out[7] = this[7] - b[7]
|
|
499
|
+
out[8] = this[8] - b[8]
|
|
500
|
+
return out
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Multiplies each element of a mat3 by a scalar number
|
|
505
|
+
*
|
|
506
|
+
* @param {Number} b amount to scale the matrix's elements by
|
|
507
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
508
|
+
* @returns {Mat3} out
|
|
509
|
+
*/
|
|
510
|
+
scaleScalar(b: number, out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
511
|
+
out[0] = this[0] * b
|
|
512
|
+
out[1] = this[1] * b
|
|
513
|
+
out[2] = this[2] * b
|
|
514
|
+
out[3] = this[3] * b
|
|
515
|
+
out[4] = this[4] * b
|
|
516
|
+
out[5] = this[5] * b
|
|
517
|
+
out[6] = this[6] * b
|
|
518
|
+
out[7] = this[7] * b
|
|
519
|
+
out[8] = this[8] * b
|
|
520
|
+
return out
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Adds two mat3's after multiplying each element of the second operand by a scalar value
|
|
525
|
+
*
|
|
526
|
+
* @param {Mat3} b the second operand
|
|
527
|
+
* @param {Number} scale the amount to scale b's elements by before adding
|
|
528
|
+
* @param {Mat3} out the receiving matrix, defaults to this
|
|
529
|
+
* @returns {Mat3} out
|
|
530
|
+
*/
|
|
531
|
+
multiplyScalarAndAdd(b: Mat3, scale: number, out = glm.ALWAYS_COPY ? new Mat3() : this) {
|
|
532
|
+
out[0] = this[0] + b[0] * scale
|
|
533
|
+
out[1] = this[1] + b[1] * scale
|
|
534
|
+
out[2] = this[2] + b[2] * scale
|
|
535
|
+
out[3] = this[3] + b[3] * scale
|
|
536
|
+
out[4] = this[4] + b[4] * scale
|
|
537
|
+
out[5] = this[5] + b[5] * scale
|
|
538
|
+
out[6] = this[6] + b[6] * scale
|
|
539
|
+
out[7] = this[7] + b[7] * scale
|
|
540
|
+
out[8] = this[8] + b[8] * scale
|
|
541
|
+
return out
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Returns a string representation of a mat3
|
|
546
|
+
*
|
|
547
|
+
* @returns {String} string representation of the matrix
|
|
548
|
+
*/
|
|
549
|
+
toString() {
|
|
550
|
+
return `mat3(${this[0]}, ${this[1]}, ${this[2]},\t${this[3]}, ${this[4]}, ${this[5]},\t${this[6]}, ${this[7]}, ${this[8]})`
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Returns whether two mat3's have exactly equal elements
|
|
555
|
+
*
|
|
556
|
+
* @param {Mat3} b the second matrix
|
|
557
|
+
* @returns {Boolean} true if the matrices are exactly equal
|
|
558
|
+
*/
|
|
559
|
+
exactEquals(b: Mat3) {
|
|
560
|
+
return (
|
|
561
|
+
this[0] === b[0] &&
|
|
562
|
+
this[1] === b[1] &&
|
|
563
|
+
this[2] === b[2] &&
|
|
564
|
+
this[3] === b[3] &&
|
|
565
|
+
this[4] === b[4] &&
|
|
566
|
+
this[5] === b[5] &&
|
|
567
|
+
this[6] === b[6] &&
|
|
568
|
+
this[7] === b[7] &&
|
|
569
|
+
this[8] === b[8]
|
|
570
|
+
)
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Returns whether two mat3's have approximately equal elements
|
|
575
|
+
*
|
|
576
|
+
* @param {Mat3} b the second matrix
|
|
577
|
+
* @returns {Boolean} true if the matrices are approximately equal
|
|
578
|
+
*/
|
|
579
|
+
equals(b: Mat3) {
|
|
580
|
+
return (
|
|
581
|
+
equals(this[0], b[0]) && equals(this[1], b[1]) && equals(this[2], b[2]) &&
|
|
582
|
+
equals(this[3], b[3]) && equals(this[4], b[4]) && equals(this[5], b[5]) &&
|
|
583
|
+
equals(this[6], b[6]) && equals(this[7], b[7]) && equals(this[8], b[8])
|
|
584
|
+
)
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export interface Mat3 {
|
|
589
|
+
add: (b: Mat3, out?: Mat3) => Mat3
|
|
590
|
+
sub: (b: Mat3, out?: Mat3) => Mat3
|
|
591
|
+
subtract: (b: Mat3, out?: Mat3) => Mat3
|
|
592
|
+
mul(b: Vec2): Vec2
|
|
593
|
+
mul(b: Vec3): Vec3
|
|
594
|
+
mul(b: Mat3, out?: Mat3): Mat3
|
|
595
|
+
mult(b: Vec2): Vec2
|
|
596
|
+
mult(b: Vec3): Vec3
|
|
597
|
+
mult(b: Mat3, out?: Mat3): Mat3
|
|
598
|
+
times(b: Vec2): Vec2
|
|
599
|
+
times(b: Vec3): Vec3
|
|
600
|
+
times(b: Mat3, out?: Mat3): Mat3
|
|
601
|
+
str: () => string
|
|
602
|
+
multiplyScalar: (b: number, out?: Mat3) => Mat3
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// @aliases
|
|
606
|
+
Mat3.fromMat2d = Mat3.fromMat2x3
|
|
607
|
+
Mat3.fromMat4x4 = Mat3.fromMat4
|
|
608
|
+
Mat3.prototype.add = Mat3.prototype.plus
|
|
609
|
+
Mat3.prototype.sub = Mat3.prototype.minus
|
|
610
|
+
Mat3.prototype.subtract = Mat3.prototype.minus
|
|
611
|
+
Mat3.prototype.mul = Mat3.prototype.multiply
|
|
612
|
+
Mat3.prototype.mult = Mat3.prototype.multiply
|
|
613
|
+
Mat3.prototype.times = Mat3.prototype.multiply
|
|
614
|
+
Mat3.prototype.str = Mat3.prototype.toString
|
|
615
|
+
Mat3.prototype.multiplyScalar = Mat3.prototype.scaleScalar
|
|
616
|
+
|
|
617
|
+
export const mat3 = Object.assign(
|
|
618
|
+
(...args: (number | Float32Array)[]): Mat3 => {
|
|
619
|
+
const out = new Mat3()
|
|
620
|
+
let i = 0
|
|
621
|
+
for (const a of args) {
|
|
622
|
+
if (typeof a === 'number') out[i++] = a
|
|
623
|
+
else for (const v of a) out[i++] = v
|
|
624
|
+
}
|
|
625
|
+
return out
|
|
626
|
+
},
|
|
627
|
+
Mat3
|
|
628
|
+
)
|
|
629
|
+
export const mat3x3 = mat3
|