@predy-js/math 0.1.67

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/dist/index.js ADDED
@@ -0,0 +1,970 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const cos$2 = Math.cos;
6
+ const sin$2 = Math.sin;
7
+ const d2r$2 = Math.PI / 180;
8
+ const tempMat3FromRotationZ = [0, 0, 0, 0, 0, 0, 0, 0, 0];
9
+ function mat3create() {
10
+ return new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
11
+ }
12
+ function mat3MulMat3(out, a, b) {
13
+ const a00 = a[0], a01 = a[1], a02 = a[2];
14
+ const a10 = a[3], a11 = a[4], a12 = a[5];
15
+ const a20 = a[6], a21 = a[7], a22 = a[8];
16
+ const b00 = b[0], b01 = b[1], b02 = b[2];
17
+ const b10 = b[3], b11 = b[4], b12 = b[5];
18
+ const b20 = b[6], b21 = b[7], b22 = b[8];
19
+ out[0] = b00 * a00 + b01 * a10 + b02 * a20;
20
+ out[1] = b00 * a01 + b01 * a11 + b02 * a21;
21
+ out[2] = b00 * a02 + b01 * a12 + b02 * a22;
22
+ out[3] = b10 * a00 + b11 * a10 + b12 * a20;
23
+ out[4] = b10 * a01 + b11 * a11 + b12 * a21;
24
+ out[5] = b10 * a02 + b11 * a12 + b12 * a22;
25
+ out[6] = b20 * a00 + b21 * a10 + b22 * a20;
26
+ out[7] = b20 * a01 + b21 * a11 + b22 * a21;
27
+ out[8] = b20 * a02 + b21 * a12 + b22 * a22;
28
+ return out;
29
+ }
30
+ function mat3FromRotationZ(out, rad) {
31
+ if (!out) {
32
+ out = tempMat3FromRotationZ;
33
+ }
34
+ const s = sin$2(rad);
35
+ const c = cos$2(rad);
36
+ out[0] = c;
37
+ out[1] = s;
38
+ out[2] = 0;
39
+ out[3] = -s;
40
+ out[4] = c;
41
+ out[5] = 0;
42
+ out[6] = 0;
43
+ out[7] = 0;
44
+ out[8] = 1;
45
+ return out;
46
+ }
47
+ function mat3FromRotationZYX(ret, x, y, z) {
48
+ const cosX = cos$2(x * d2r$2);
49
+ const cosY = cos$2(y * d2r$2);
50
+ const cosZ = cos$2(z * d2r$2);
51
+ const sinX = sin$2(x * d2r$2);
52
+ const sinY = sin$2(y * d2r$2);
53
+ const sinZ = sin$2(z * d2r$2);
54
+ ret[0] = cosY * cosZ;
55
+ ret[1] = cosY * sinZ;
56
+ ret[2] = -sinY;
57
+ ret[3] = -cosX * sinZ + sinX * sinY * cosZ;
58
+ ret[4] = cosX * cosZ + sinX * sinY * sinZ;
59
+ ret[5] = sinX * cosY;
60
+ ret[6] = sinZ * sinX + cosX * sinY * cosZ;
61
+ ret[7] = -sinX * cosZ + cosX * sinY * sinZ;
62
+ ret[8] = cosX * cosY;
63
+ return ret;
64
+ }
65
+ function mat3FromRotation(ret, rotation) {
66
+ return mat3FromRotationZYX(ret, -rotation[0], -rotation[1], -rotation[2]);
67
+ }
68
+ function mat3NormalFromMat4(out, a) {
69
+ const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
70
+ const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
71
+ const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
72
+ const a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
73
+ const b00 = a00 * a11 - a01 * a10;
74
+ const b01 = a00 * a12 - a02 * a10;
75
+ const b02 = a00 * a13 - a03 * a10;
76
+ const b03 = a01 * a12 - a02 * a11;
77
+ const b04 = a01 * a13 - a03 * a11;
78
+ const b05 = a02 * a13 - a03 * a12;
79
+ const b06 = a20 * a31 - a21 * a30;
80
+ const b07 = a20 * a32 - a22 * a30;
81
+ const b08 = a20 * a33 - a23 * a30;
82
+ const b09 = a21 * a32 - a22 * a31;
83
+ const b10 = a21 * a33 - a23 * a31;
84
+ const b11 = a22 * a33 - a23 * a32;
85
+ // Calculate the determinant
86
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
87
+ if (!det) {
88
+ return null;
89
+ }
90
+ det = 1.0 / det;
91
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
92
+ out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
93
+ out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
94
+ out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
95
+ out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
96
+ out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
97
+ out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
98
+ out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
99
+ out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
100
+ return out;
101
+ }
102
+ function mat3Translate(out, a, v) {
103
+ const a00 = a[0], a01 = a[1], a02 = a[2], a10 = a[3], a11 = a[4], a12 = a[5], a20 = a[6], a21 = a[7], a22 = a[8], x = v[0], y = v[1];
104
+ out[0] = a00;
105
+ out[1] = a01;
106
+ out[2] = a02;
107
+ out[3] = a10;
108
+ out[4] = a11;
109
+ out[5] = a12;
110
+ out[6] = x * a00 + y * a10 + a20;
111
+ out[7] = x * a01 + y * a11 + a21;
112
+ out[8] = x * a02 + y * a12 + a22;
113
+ return out;
114
+ }
115
+ function mat3Rotate(out, a, rad) {
116
+ const a00 = a[0], a01 = a[1], a02 = a[2], a10 = a[3], a11 = a[4], a12 = a[5], a20 = a[6], a21 = a[7], a22 = a[8], s = sin$2(rad), c = cos$2(rad);
117
+ out[0] = c * a00 + s * a10;
118
+ out[1] = c * a01 + s * a11;
119
+ out[2] = c * a02 + s * a12;
120
+ out[3] = c * a10 - s * a00;
121
+ out[4] = c * a11 - s * a01;
122
+ out[5] = c * a12 - s * a02;
123
+ out[6] = a20;
124
+ out[7] = a21;
125
+ out[8] = a22;
126
+ return out;
127
+ }
128
+ function mat3Scale(out, a, v) {
129
+ const x = v[0], y = v[1];
130
+ out[0] = x * a[0];
131
+ out[1] = x * a[1];
132
+ out[2] = x * a[2];
133
+ out[3] = y * a[3];
134
+ out[4] = y * a[4];
135
+ out[5] = y * a[5];
136
+ out[6] = a[6];
137
+ out[7] = a[7];
138
+ out[8] = a[8];
139
+ return out;
140
+ }
141
+
142
+ const d2r$1 = Math.PI / 180;
143
+ const matrixRotation = new Float32Array(9);
144
+ const quatOut = [0, 0, 0, 1];
145
+ const temps = [0, 0, 0];
146
+ function mat4create() {
147
+ return new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
148
+ }
149
+ function mat4fromRotationTranslationScale(out, q, v, s) {
150
+ const x = q[0];
151
+ const y = q[1];
152
+ const z = q[2];
153
+ const w = q[3];
154
+ const x2 = x + x;
155
+ const y2 = y + y;
156
+ const z2 = z + z;
157
+ const xx = x * x2;
158
+ const xy = x * y2;
159
+ const xz = x * z2;
160
+ const yy = y * y2;
161
+ const yz = y * z2;
162
+ const zz = z * z2;
163
+ const wx = w * x2;
164
+ const wy = w * y2;
165
+ const wz = w * z2;
166
+ const sx = s[0];
167
+ const sy = s[1];
168
+ const sz = s[2];
169
+ out[0] = (1 - (yy + zz)) * sx;
170
+ out[1] = (xy + wz) * sx;
171
+ out[2] = (xz - wy) * sx;
172
+ out[3] = 0;
173
+ out[4] = (xy - wz) * sy;
174
+ out[5] = (1 - (xx + zz)) * sy;
175
+ out[6] = (yz + wx) * sy;
176
+ out[7] = 0;
177
+ out[8] = (xz + wy) * sz;
178
+ out[9] = (yz - wx) * sz;
179
+ out[10] = (1 - (xx + yy)) * sz;
180
+ out[11] = 0;
181
+ out[12] = v[0];
182
+ out[13] = v[1];
183
+ out[14] = v[2];
184
+ out[15] = 1;
185
+ return out;
186
+ }
187
+ function invertMat4(out, a) {
188
+ const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
189
+ const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
190
+ const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
191
+ const a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
192
+ const b00 = a00 * a11 - a01 * a10;
193
+ const b01 = a00 * a12 - a02 * a10;
194
+ const b02 = a00 * a13 - a03 * a10;
195
+ const b03 = a01 * a12 - a02 * a11;
196
+ const b04 = a01 * a13 - a03 * a11;
197
+ const b05 = a02 * a13 - a03 * a12;
198
+ const b06 = a20 * a31 - a21 * a30;
199
+ const b07 = a20 * a32 - a22 * a30;
200
+ const b08 = a20 * a33 - a23 * a30;
201
+ const b09 = a21 * a32 - a22 * a31;
202
+ const b10 = a21 * a33 - a23 * a31;
203
+ const b11 = a22 * a33 - a23 * a32;
204
+ // Calculate the determinant
205
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
206
+ if (!det) {
207
+ return null;
208
+ }
209
+ det = 1.0 / det;
210
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
211
+ out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
212
+ out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
213
+ out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
214
+ out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
215
+ out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
216
+ out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
217
+ out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
218
+ out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
219
+ out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
220
+ out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
221
+ out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
222
+ out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
223
+ out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
224
+ out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
225
+ out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
226
+ return out;
227
+ }
228
+ function mat4Determinate(a) {
229
+ const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
230
+ const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
231
+ const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
232
+ const a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
233
+ const b00 = a00 * a11 - a01 * a10;
234
+ const b01 = a00 * a12 - a02 * a10;
235
+ const b02 = a00 * a13 - a03 * a10;
236
+ const b03 = a01 * a12 - a02 * a11;
237
+ const b04 = a01 * a13 - a03 * a11;
238
+ const b05 = a02 * a13 - a03 * a12;
239
+ const b06 = a20 * a31 - a21 * a30;
240
+ const b07 = a20 * a32 - a22 * a30;
241
+ const b08 = a20 * a33 - a23 * a30;
242
+ const b09 = a21 * a32 - a22 * a31;
243
+ const b10 = a21 * a33 - a23 * a31;
244
+ const b11 = a22 * a33 - a23 * a32;
245
+ return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
246
+ }
247
+ function getMat4TR(mat4, translate, quat, scaling) {
248
+ const m11 = mat4[0];
249
+ const m12 = mat4[1];
250
+ const m13 = mat4[2];
251
+ const m21 = mat4[4];
252
+ const m22 = mat4[5];
253
+ const m23 = mat4[6];
254
+ const m31 = mat4[8];
255
+ const m32 = mat4[9];
256
+ const m33 = mat4[10];
257
+ if (quat) {
258
+ // copy from gl-matrix
259
+ const is1 = 1 / scaling[0];
260
+ const is2 = 1 / scaling[1];
261
+ const is3 = 1 / scaling[2];
262
+ matrixRotation[0] = m11 * is1;
263
+ matrixRotation[1] = m12 * is1;
264
+ matrixRotation[2] = m13 * is1;
265
+ matrixRotation[3] = m21 * is2;
266
+ matrixRotation[4] = m22 * is2;
267
+ matrixRotation[5] = m23 * is2;
268
+ matrixRotation[6] = m31 * is3;
269
+ matrixRotation[7] = m32 * is3;
270
+ matrixRotation[8] = m33 * is3;
271
+ const fTrace = matrixRotation[0] + matrixRotation[4] + matrixRotation[8];
272
+ let fRoot;
273
+ if (fTrace > 0.0) {
274
+ // |w| > 1/2, may as well choose w > 1/2
275
+ fRoot = Math.sqrt(fTrace + 1.0); // 2w
276
+ quatOut[3] = 0.5 * fRoot;
277
+ fRoot = 0.5 / fRoot; // 1/(4w)
278
+ quatOut[0] = (matrixRotation[5] - matrixRotation[7]) * fRoot;
279
+ quatOut[1] = (matrixRotation[6] - matrixRotation[2]) * fRoot;
280
+ quatOut[2] = (matrixRotation[1] - matrixRotation[3]) * fRoot;
281
+ }
282
+ else {
283
+ // |w| <= 1/2
284
+ let i = 0;
285
+ if (matrixRotation[4] > matrixRotation[0]) {
286
+ i = 1;
287
+ }
288
+ if (matrixRotation[8] > matrixRotation[i * 3 + i]) {
289
+ i = 2;
290
+ }
291
+ const j = (i + 1) % 3;
292
+ const k = (i + 2) % 3;
293
+ fRoot = Math.sqrt(matrixRotation[i * 3 + i] - matrixRotation[j * 3 + j] - matrixRotation[k * 3 + k] + 1.0);
294
+ quatOut[i] = 0.5 * fRoot;
295
+ fRoot = 0.5 / fRoot;
296
+ quatOut[3] = (matrixRotation[j * 3 + k] - matrixRotation[k * 3 + j]) * fRoot;
297
+ quatOut[j] = (matrixRotation[j * 3 + i] + matrixRotation[i * 3 + j]) * fRoot;
298
+ quatOut[k] = (matrixRotation[k * 3 + i] + matrixRotation[i * 3 + k]) * fRoot;
299
+ }
300
+ quat[0] = quatOut[0];
301
+ quat[1] = quatOut[1];
302
+ quat[2] = quatOut[2];
303
+ quat[3] = quatOut[3];
304
+ }
305
+ if (translate) {
306
+ translate[0] = mat4[12];
307
+ translate[1] = mat4[13];
308
+ translate[2] = mat4[14];
309
+ }
310
+ }
311
+ function getMat4TRS(mat4, translate, quat, scaling = temps) {
312
+ const m11 = mat4[0];
313
+ const m12 = mat4[1];
314
+ const m13 = mat4[2];
315
+ const m21 = mat4[4];
316
+ const m22 = mat4[5];
317
+ const m23 = mat4[6];
318
+ const m31 = mat4[8];
319
+ const m32 = mat4[9];
320
+ const m33 = mat4[10];
321
+ const det = mat4Determinate(mat4);
322
+ scaling[0] = Math.hypot(m11, m12, m13);
323
+ scaling[1] = Math.hypot(m21, m22, m23);
324
+ scaling[2] = Math.hypot(m31, m32, m33);
325
+ if (det < 0) {
326
+ scaling[0] = -scaling[0];
327
+ }
328
+ return getMat4TR(mat4, translate, quat, scaling);
329
+ }
330
+ function mat4multiply(out, a, b) {
331
+ const a00 = a[0];
332
+ const a01 = a[1];
333
+ const a02 = a[2];
334
+ const a03 = a[3];
335
+ const a10 = a[4];
336
+ const a11 = a[5];
337
+ const a12 = a[6];
338
+ const a13 = a[7];
339
+ const a20 = a[8];
340
+ const a21 = a[9];
341
+ const a22 = a[10];
342
+ const a23 = a[11];
343
+ const a30 = a[12];
344
+ const a31 = a[13];
345
+ const a32 = a[14];
346
+ const a33 = a[15];
347
+ // Cache only the current line of the second matrix
348
+ let b0 = b[0];
349
+ let b1 = b[1];
350
+ let b2 = b[2];
351
+ let b3 = b[3];
352
+ out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
353
+ out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
354
+ out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
355
+ out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
356
+ b0 = b[4];
357
+ b1 = b[5];
358
+ b2 = b[6];
359
+ b3 = b[7];
360
+ out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
361
+ out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
362
+ out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
363
+ out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
364
+ b0 = b[8];
365
+ b1 = b[9];
366
+ b2 = b[10];
367
+ b3 = b[11];
368
+ out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
369
+ out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
370
+ out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
371
+ out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
372
+ b0 = b[12];
373
+ b1 = b[13];
374
+ b2 = b[14];
375
+ b3 = b[15];
376
+ out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
377
+ out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
378
+ out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
379
+ out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
380
+ return out;
381
+ }
382
+ function mat4Clone(out, from) {
383
+ for (let i = 0; i < 16; i++) {
384
+ out[i] = from[i];
385
+ }
386
+ return out;
387
+ }
388
+ function mat4invert(out, a) {
389
+ const a00 = a[0];
390
+ const a01 = a[1];
391
+ const a02 = a[2];
392
+ const a03 = a[3];
393
+ const a10 = a[4];
394
+ const a11 = a[5];
395
+ const a12 = a[6];
396
+ const a13 = a[7];
397
+ const a20 = a[8];
398
+ const a21 = a[9];
399
+ const a22 = a[10];
400
+ const a23 = a[11];
401
+ const a30 = a[12];
402
+ const a31 = a[13];
403
+ const a32 = a[14];
404
+ const a33 = a[15];
405
+ const b00 = a00 * a11 - a01 * a10;
406
+ const b01 = a00 * a12 - a02 * a10;
407
+ const b02 = a00 * a13 - a03 * a10;
408
+ const b03 = a01 * a12 - a02 * a11;
409
+ const b04 = a01 * a13 - a03 * a11;
410
+ const b05 = a02 * a13 - a03 * a12;
411
+ const b06 = a20 * a31 - a21 * a30;
412
+ const b07 = a20 * a32 - a22 * a30;
413
+ const b08 = a20 * a33 - a23 * a30;
414
+ const b09 = a21 * a32 - a22 * a31;
415
+ const b10 = a21 * a33 - a23 * a31;
416
+ const b11 = a22 * a33 - a23 * a32;
417
+ // Calculate the determinant
418
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
419
+ if (!det) {
420
+ for (let i = 0; i < 16; i++) {
421
+ out[i] = NaN;
422
+ }
423
+ return out;
424
+ }
425
+ det = 1.0 / det;
426
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
427
+ out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
428
+ out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
429
+ out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
430
+ out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
431
+ out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
432
+ out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
433
+ out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
434
+ out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
435
+ out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
436
+ out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
437
+ out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
438
+ out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
439
+ out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
440
+ out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
441
+ out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
442
+ return out;
443
+ }
444
+ function mat4translate(out, a, v) {
445
+ const x = v[0];
446
+ const y = v[1];
447
+ const z = v[2];
448
+ let a00;
449
+ let a01;
450
+ let a02;
451
+ let a03;
452
+ let a10;
453
+ let a11;
454
+ let a12;
455
+ let a13;
456
+ let a20;
457
+ let a21;
458
+ let a22;
459
+ let a23;
460
+ if (a === out) {
461
+ out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
462
+ out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
463
+ out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
464
+ out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
465
+ }
466
+ else {
467
+ a00 = a[0];
468
+ a01 = a[1];
469
+ a02 = a[2];
470
+ a03 = a[3];
471
+ a10 = a[4];
472
+ a11 = a[5];
473
+ a12 = a[6];
474
+ a13 = a[7];
475
+ a20 = a[8];
476
+ a21 = a[9];
477
+ a22 = a[10];
478
+ a23 = a[11];
479
+ out[0] = a00;
480
+ out[1] = a01;
481
+ out[2] = a02;
482
+ out[3] = a03;
483
+ out[4] = a10;
484
+ out[5] = a11;
485
+ out[6] = a12;
486
+ out[7] = a13;
487
+ out[8] = a20;
488
+ out[9] = a21;
489
+ out[10] = a22;
490
+ out[11] = a23;
491
+ out[12] = a00 * x + a10 * y + a20 * z + a[12];
492
+ out[13] = a01 * x + a11 * y + a21 * z + a[13];
493
+ out[14] = a02 * x + a12 * y + a22 * z + a[14];
494
+ out[15] = a03 * x + a13 * y + a23 * z + a[15];
495
+ }
496
+ return out;
497
+ }
498
+ function mat4scale(out, a, v) {
499
+ const x = v[0];
500
+ const y = v[1];
501
+ const z = v[2];
502
+ out[0] = a[0] * x;
503
+ out[1] = a[1] * x;
504
+ out[2] = a[2] * x;
505
+ out[3] = a[3] * x;
506
+ out[4] = a[4] * y;
507
+ out[5] = a[5] * y;
508
+ out[6] = a[6] * y;
509
+ out[7] = a[7] * y;
510
+ out[8] = a[8] * z;
511
+ out[9] = a[9] * z;
512
+ out[10] = a[10] * z;
513
+ out[11] = a[11] * z;
514
+ out[12] = a[12];
515
+ out[13] = a[13];
516
+ out[14] = a[14];
517
+ out[15] = a[15];
518
+ return out;
519
+ }
520
+ function mat4rotate(out, a, rad, axis) {
521
+ let x = axis[0];
522
+ let y = axis[1];
523
+ let z = axis[2];
524
+ let len = Math.sqrt(x * x + y * y + z * z);
525
+ len = 1 / len;
526
+ x *= len;
527
+ y *= len;
528
+ z *= len;
529
+ const s = Math.sin(rad);
530
+ const c = Math.cos(rad);
531
+ const t = 1 - c;
532
+ const a00 = a[0];
533
+ const a01 = a[1];
534
+ const a02 = a[2];
535
+ const a03 = a[3];
536
+ const a10 = a[4];
537
+ const a11 = a[5];
538
+ const a12 = a[6];
539
+ const a13 = a[7];
540
+ const a20 = a[8];
541
+ const a21 = a[9];
542
+ const a22 = a[10];
543
+ const a23 = a[11];
544
+ // Construct the elements of the rotation matrix
545
+ const b00 = x * x * t + c;
546
+ const b01 = y * x * t + z * s;
547
+ const b02 = z * x * t - y * s;
548
+ const b10 = x * y * t - z * s;
549
+ const b11 = y * y * t + c;
550
+ const b12 = z * y * t + x * s;
551
+ const b20 = x * z * t + y * s;
552
+ const b21 = y * z * t - x * s;
553
+ const b22 = z * z * t + c;
554
+ // Perform rotation-specific matrix multiplication
555
+ out[0] = a00 * b00 + a10 * b01 + a20 * b02;
556
+ out[1] = a01 * b00 + a11 * b01 + a21 * b02;
557
+ out[2] = a02 * b00 + a12 * b01 + a22 * b02;
558
+ out[3] = a03 * b00 + a13 * b01 + a23 * b02;
559
+ out[4] = a00 * b10 + a10 * b11 + a20 * b12;
560
+ out[5] = a01 * b10 + a11 * b11 + a21 * b12;
561
+ out[6] = a02 * b10 + a12 * b11 + a22 * b12;
562
+ out[7] = a03 * b10 + a13 * b11 + a23 * b12;
563
+ out[8] = a00 * b20 + a10 * b21 + a20 * b22;
564
+ out[9] = a01 * b20 + a11 * b21 + a21 * b22;
565
+ out[10] = a02 * b20 + a12 * b21 + a22 * b22;
566
+ out[11] = a03 * b20 + a13 * b21 + a23 * b22;
567
+ if (a !== out) {
568
+ // If the source and destination differ, copy the unchanged last row
569
+ out[12] = a[12];
570
+ out[13] = a[13];
571
+ out[14] = a[14];
572
+ out[15] = a[15];
573
+ }
574
+ return out;
575
+ }
576
+ function mat4perspective(out, fovy, aspect, near, far, reverse) {
577
+ const f = 1.0 / Math.tan((fovy * d2r$1) / 2);
578
+ let nf;
579
+ out[0] = reverse ? f : f / aspect;
580
+ out[1] = 0;
581
+ out[2] = 0;
582
+ out[3] = 0;
583
+ out[4] = 0;
584
+ out[5] = reverse ? f * aspect : f;
585
+ out[6] = 0;
586
+ out[7] = 0;
587
+ out[8] = 0;
588
+ out[9] = 0;
589
+ out[11] = -1;
590
+ out[12] = 0;
591
+ out[13] = 0;
592
+ out[15] = 0;
593
+ if (far != null && far !== Infinity) {
594
+ nf = 1 / (near - far);
595
+ out[10] = (far + near) * nf;
596
+ out[14] = 2 * far * near * nf;
597
+ }
598
+ else {
599
+ out[10] = -1;
600
+ out[14] = -2 * near;
601
+ }
602
+ return out;
603
+ }
604
+ function mat4identity(out) {
605
+ out[0] = 1;
606
+ out[1] = 0;
607
+ out[2] = 0;
608
+ out[3] = 0;
609
+ out[4] = 0;
610
+ out[5] = 1;
611
+ out[6] = 0;
612
+ out[7] = 0;
613
+ out[8] = 0;
614
+ out[9] = 0;
615
+ out[10] = 1;
616
+ out[11] = 0;
617
+ out[12] = 0;
618
+ out[13] = 0;
619
+ out[14] = 0;
620
+ out[15] = 1;
621
+ }
622
+
623
+ const cos$1 = Math.cos;
624
+ const sin$1 = Math.sin;
625
+ function vecAdd(out, a, b) {
626
+ for (let i = 0, len = a.length; i < len; i++) {
627
+ out[i] = a[i] + b[i];
628
+ }
629
+ return out;
630
+ }
631
+ function vecFill(out, number) {
632
+ for (let i = 0, len = out.length; i < len; i++) {
633
+ out[i] = number;
634
+ }
635
+ return out;
636
+ }
637
+ function vecAddCombine(out, a, b) {
638
+ if (a && b) {
639
+ for (let i = 0, len = a.length; i < len; i++) {
640
+ out[i] = a[i] + b[i];
641
+ }
642
+ return out;
643
+ }
644
+ return a || b;
645
+ }
646
+ function vecAssign(out, a, count, start = 0) {
647
+ for (let i = 0; i < count; i++) {
648
+ out[i] = a[i + start];
649
+ }
650
+ return out;
651
+ }
652
+ function vecMulCombine(out, a, b) {
653
+ if (a && b) {
654
+ for (let i = 0, len = a.length; i < len; i++) {
655
+ out[i] = a[i] * b[i];
656
+ }
657
+ }
658
+ else if (a) {
659
+ if (out !== a) {
660
+ for (let i = 0; i < a.length; i++) {
661
+ out[i] = a[i];
662
+ }
663
+ }
664
+ }
665
+ else if (b) {
666
+ if (out !== b) {
667
+ for (let i = 0; i < b.length; i++) {
668
+ out[i] = b[i];
669
+ }
670
+ }
671
+ }
672
+ return out;
673
+ }
674
+ function vecMinus(out, v0, v1) {
675
+ for (let i = 0, len = v0.length; i < len; i++) {
676
+ out[i] = v0[i] - v1[i];
677
+ }
678
+ return out;
679
+ }
680
+ function vecSquareDistance(v0, v1) {
681
+ let sum = 0;
682
+ for (let i = 0, len = v0.length; i < len; i++) {
683
+ const d = v0[i] - v1[i];
684
+ sum += d * d;
685
+ }
686
+ return sum;
687
+ }
688
+ const NumberEpsilon = Number.EPSILON || Math.pow(2, -32);
689
+ function vecNormalize(out, a) {
690
+ if (arguments.length === 1) {
691
+ a = out;
692
+ out = [];
693
+ }
694
+ const sum = Math.hypot(...a);
695
+ if (sum === 0) {
696
+ return vecAssign(out, a, a.length);
697
+ }
698
+ for (let i = 0; i < a.length; i++) {
699
+ out[i] = a[i] / sum;
700
+ }
701
+ return out;
702
+ }
703
+ function vecMulScalar(out, vec, a) {
704
+ for (let i = 0, len = vec.length; i < len; i++) {
705
+ out[i] = vec[i] * a;
706
+ }
707
+ return out;
708
+ }
709
+ function vecDot(out, a, b) {
710
+ if (isNaN(b)) {
711
+ let sum = 0;
712
+ for (let i = 0, len = a.length; i < len; i++) {
713
+ sum += out[i] * a[i];
714
+ }
715
+ return sum;
716
+ }
717
+ for (let i = 0, len = a.length; i < len; i++) {
718
+ out[i] = a[i] * b;
719
+ }
720
+ return (out);
721
+ }
722
+ function vec2TransformByMat3(out, a, m) {
723
+ const x = a[0], y = a[1];
724
+ out[0] = m[0] * x + m[3] * y + m[6];
725
+ out[1] = m[1] * x + m[4] * y + m[7];
726
+ return out;
727
+ }
728
+ function isZeroVec(vec) {
729
+ for (let i = 0, len = vec.length; i < len; i++) {
730
+ if (Math.abs(vec[i]) > NumberEpsilon) {
731
+ return false;
732
+ }
733
+ }
734
+ return true;
735
+ }
736
+ function rotateVec2(out, vec2, angleInRad) {
737
+ const c = cos$1(angleInRad);
738
+ const s = sin$1(angleInRad);
739
+ const x = vec2[0];
740
+ const y = vec2[1];
741
+ out[0] = c * x + s * y;
742
+ out[1] = -s * x + c * y;
743
+ return out;
744
+ }
745
+ function clamp(v, min, max) {
746
+ return v > max ? max : (v < min ? min : v);
747
+ }
748
+
749
+ const r2d = 180 / Math.PI;
750
+ function vec3Create(v) {
751
+ if (v) {
752
+ return [v[0], v[1], v[2]];
753
+ }
754
+ return [0, 0, 0];
755
+ }
756
+ function vec3AddVec3(out, a, b) {
757
+ out[0] = a[0] + b[0];
758
+ out[1] = a[1] + b[1];
759
+ out[2] = a[2] + b[2];
760
+ return out;
761
+ }
762
+ function vec3Cross(out, a, b) {
763
+ const ax = a[0], ay = a[1], az = a[2];
764
+ const bx = b[0], by = b[1], bz = b[2];
765
+ out[0] = ay * bz - az * by;
766
+ out[1] = az * bx - ax * bz;
767
+ out[2] = ax * by - ay * bx;
768
+ return out;
769
+ }
770
+ function vec3MulMat4(out, vec3, mat4) {
771
+ const x = vec3[0], y = vec3[1], z = vec3[2];
772
+ const w = (mat4[3] * x + mat4[7] * y + mat4[11] * z + mat4[15]) || 1;
773
+ out[0] = (mat4[0] * x + mat4[4] * y + mat4[8] * z + mat4[12]) / w;
774
+ out[1] = (mat4[1] * x + mat4[5] * y + mat4[9] * z + mat4[13]) / w;
775
+ out[2] = (mat4[2] * x + mat4[6] * y + mat4[10] * z + mat4[14]) / w;
776
+ return out;
777
+ }
778
+ function vec3TranslateByMat4(out, vec3, matrix4) {
779
+ out[0] = vec3[0] + matrix4[12];
780
+ out[1] = vec3[1] + matrix4[13];
781
+ out[2] = vec3[2] + matrix4[14];
782
+ return out;
783
+ }
784
+ function vec3RotateByMat4(out, a, m) {
785
+ const x = a[0], y = a[1], z = a[2];
786
+ const w = (m[3] * x + m[7] * y + m[11] * z + m[15]) || 1;
787
+ out[0] = (m[0] * x + m[4] * y + m[8] * z) / w;
788
+ out[1] = (m[1] * x + m[5] * y + m[9] * z) / w;
789
+ out[2] = (m[2] * x + m[6] * y + m[10] * z) / w;
790
+ return out;
791
+ }
792
+ function vec3MulMat3(out, a, m) {
793
+ const x = a[0], y = a[1], z = a[2];
794
+ out[0] = x * m[0] + y * m[3] + z * m[6];
795
+ out[1] = x * m[1] + y * m[4] + z * m[7];
796
+ out[2] = x * m[2] + y * m[5] + z * m[8];
797
+ return out;
798
+ }
799
+ function ensureVec3(vec, defaultVec = [0, 0, 0]) {
800
+ const ret = [defaultVec[0], defaultVec[1], defaultVec[2]];
801
+ if (vec) {
802
+ for (let i = 0; i < 3; i++) {
803
+ if (!Number.isNaN(+vec[i])) {
804
+ ret[i] = +vec[i];
805
+ }
806
+ }
807
+ }
808
+ return ret;
809
+ }
810
+ function rotationZYXFromMat3(out, mat3) {
811
+ const te = mat3;
812
+ const m11 = te[0], m12 = te[3]; te[6];
813
+ const m21 = te[1], m22 = te[4]; te[7];
814
+ const m31 = te[2], m32 = te[5], m33 = te[8];
815
+ out[1] = Math.asin(clamp(-m31, -1, 1)) * r2d;
816
+ if (Math.abs(m31) < 0.9999999) {
817
+ out[0] = Math.atan2(m32, m33) * r2d;
818
+ out[2] = Math.atan2(m21, m11) * r2d;
819
+ }
820
+ else {
821
+ out[0] = 0;
822
+ out[2] = Math.atan2(-m12, m22) * r2d;
823
+ }
824
+ return out;
825
+ }
826
+ function vec3MulNum(out, v3, mul) {
827
+ out[0] = v3[0] * mul;
828
+ out[1] = v3[1] * mul;
829
+ out[2] = v3[2] * mul;
830
+ return out;
831
+ }
832
+ function rotationFromMat3(out, mat3) {
833
+ rotationZYXFromMat3(out, mat3);
834
+ return out;
835
+ }
836
+ const tempUV = vec3Create();
837
+ const tempUUV = vec3Create();
838
+ function vec3RotateByQuat(out, a, quat) {
839
+ const x = quat[0], y = quat[1], z = quat[2], w = quat[3];
840
+ const qvec = [x, y, z];
841
+ const uv = vec3Cross(tempUV, qvec, a);
842
+ const uuv = vec3Cross(tempUUV, qvec, uv);
843
+ vec3MulNum(uuv, uuv, 2);
844
+ vec3MulNum(uv, uv, 2 * w);
845
+ vec3AddVec3(uuv, uuv, uv);
846
+ vec3AddVec3(out, a, uuv);
847
+ return out;
848
+ }
849
+
850
+ const d2r = Math.PI / 180;
851
+ const cos = Math.cos;
852
+ const sin = Math.sin;
853
+ function quatCreate() {
854
+ return new Float32Array([0, 0, 0, 1]);
855
+ }
856
+ function quatMultiply(out, a, b) {
857
+ const ax = a[0], ay = a[1], az = a[2], aw = a[3];
858
+ const bx = b[0], by = b[1], bz = b[2], bw = b[3];
859
+ out[0] = ax * bw + aw * bx + ay * bz - az * by;
860
+ out[1] = ay * bw + aw * by + az * bx - ax * bz;
861
+ out[2] = az * bw + aw * bz + ax * by - ay * bx;
862
+ out[3] = aw * bw - ax * bx - ay * by - az * bz;
863
+ return out;
864
+ }
865
+ function mat3FromQuat(out, quat) {
866
+ const x = quat[0], y = quat[1], z = quat[2], w = quat[3];
867
+ const x2 = x + x;
868
+ const y2 = y + y;
869
+ const z2 = z + z;
870
+ const xx = x * x2;
871
+ const yx = y * x2;
872
+ const yy = y * y2;
873
+ const zx = z * x2;
874
+ const zy = z * y2;
875
+ const zz = z * z2;
876
+ const wx = w * x2;
877
+ const wy = w * y2;
878
+ const wz = w * z2;
879
+ out[0] = 1 - yy - zz;
880
+ out[3] = yx - wz;
881
+ out[6] = zx + wy;
882
+ out[1] = yx + wz;
883
+ out[4] = 1 - xx - zz;
884
+ out[7] = zy - wx;
885
+ out[2] = zx - wy;
886
+ out[5] = zy + wx;
887
+ out[8] = 1 - xx - yy;
888
+ return out;
889
+ }
890
+ function quatEquals(q1, q2) {
891
+ const tolerance = 1e-6;
892
+ return q1.length === q2.length && q1.every((component, index) => Math.abs(component - q2[index]) < tolerance ||
893
+ Math.abs(component + q2[index]) < tolerance);
894
+ }
895
+ function quatFromRotation(out, x, y, z) {
896
+ const c1 = cos((x * d2r) / 2);
897
+ const c2 = cos((y * d2r) / 2);
898
+ const c3 = cos((z * d2r) / 2);
899
+ const s1 = sin((x * d2r) / 2);
900
+ const s2 = sin((y * d2r) / 2);
901
+ const s3 = sin((z * d2r) / 2);
902
+ out[0] = s1 * c2 * c3 - c1 * s2 * s3;
903
+ out[1] = c1 * s2 * c3 + s1 * c2 * s3;
904
+ out[2] = c1 * c2 * s3 - s1 * s2 * c3;
905
+ out[3] = c1 * c2 * c3 + s1 * s2 * s3;
906
+ return out;
907
+ }
908
+ function quatStar(out, quat) {
909
+ const x = quat[0], y = quat[1], z = quat[2], w = quat[3];
910
+ out[0] = -x;
911
+ out[1] = -y;
912
+ out[2] = -z;
913
+ out[3] = w;
914
+ return out;
915
+ }
916
+
917
+ exports.NumberEpsilon = NumberEpsilon;
918
+ exports.clamp = clamp;
919
+ exports.ensureVec3 = ensureVec3;
920
+ exports.getMat4TR = getMat4TR;
921
+ exports.getMat4TRS = getMat4TRS;
922
+ exports.invertMat4 = invertMat4;
923
+ exports.isZeroVec = isZeroVec;
924
+ exports.mat3FromQuat = mat3FromQuat;
925
+ exports.mat3FromRotation = mat3FromRotation;
926
+ exports.mat3FromRotationZ = mat3FromRotationZ;
927
+ exports.mat3MulMat3 = mat3MulMat3;
928
+ exports.mat3NormalFromMat4 = mat3NormalFromMat4;
929
+ exports.mat3Rotate = mat3Rotate;
930
+ exports.mat3Scale = mat3Scale;
931
+ exports.mat3Translate = mat3Translate;
932
+ exports.mat3create = mat3create;
933
+ exports.mat4Clone = mat4Clone;
934
+ exports.mat4Determinate = mat4Determinate;
935
+ exports.mat4create = mat4create;
936
+ exports.mat4fromRotationTranslationScale = mat4fromRotationTranslationScale;
937
+ exports.mat4identity = mat4identity;
938
+ exports.mat4invert = mat4invert;
939
+ exports.mat4multiply = mat4multiply;
940
+ exports.mat4perspective = mat4perspective;
941
+ exports.mat4rotate = mat4rotate;
942
+ exports.mat4scale = mat4scale;
943
+ exports.mat4translate = mat4translate;
944
+ exports.quatCreate = quatCreate;
945
+ exports.quatEquals = quatEquals;
946
+ exports.quatFromRotation = quatFromRotation;
947
+ exports.quatMultiply = quatMultiply;
948
+ exports.quatStar = quatStar;
949
+ exports.rotateVec2 = rotateVec2;
950
+ exports.rotationFromMat3 = rotationFromMat3;
951
+ exports.vec2TransformByMat3 = vec2TransformByMat3;
952
+ exports.vec3AddVec3 = vec3AddVec3;
953
+ exports.vec3Create = vec3Create;
954
+ exports.vec3Cross = vec3Cross;
955
+ exports.vec3MulMat3 = vec3MulMat3;
956
+ exports.vec3MulMat4 = vec3MulMat4;
957
+ exports.vec3MulNum = vec3MulNum;
958
+ exports.vec3RotateByMat4 = vec3RotateByMat4;
959
+ exports.vec3RotateByQuat = vec3RotateByQuat;
960
+ exports.vec3TranslateByMat4 = vec3TranslateByMat4;
961
+ exports.vecAdd = vecAdd;
962
+ exports.vecAddCombine = vecAddCombine;
963
+ exports.vecAssign = vecAssign;
964
+ exports.vecDot = vecDot;
965
+ exports.vecFill = vecFill;
966
+ exports.vecMinus = vecMinus;
967
+ exports.vecMulCombine = vecMulCombine;
968
+ exports.vecMulScalar = vecMulScalar;
969
+ exports.vecNormalize = vecNormalize;
970
+ exports.vecSquareDistance = vecSquareDistance;