-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Matrix3.js
587 lines (501 loc) · 13.2 KB
/
Matrix3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
// Adapted from [gl-matrix](https://github.com/toji/gl-matrix) by toji
// and [vecmath](https://github.com/mattdesl/vecmath) by mattdesl
var Class = require('../utils/Class');
/**
* @classdesc
* A three-dimensional matrix.
*
* Defaults to the identity matrix when instantiated.
*
* @class Matrix3
* @memberof Phaser.Math
* @constructor
* @since 3.0.0
*
* @param {Phaser.Math.Matrix3} [m] - Optional Matrix3 to copy values from.
*/
var Matrix3 = new Class({
initialize:
function Matrix3 (m)
{
/**
* The matrix values.
*
* @name Phaser.Math.Matrix3#val
* @type {Float32Array}
* @since 3.0.0
*/
this.val = new Float32Array(9);
if (m)
{
// Assume Matrix3 with val:
this.copy(m);
}
else
{
// Default to identity
this.identity();
}
},
/**
* Make a clone of this Matrix3.
*
* @method Phaser.Math.Matrix3#clone
* @since 3.0.0
*
* @return {Phaser.Math.Matrix3} A clone of this Matrix3.
*/
clone: function ()
{
return new Matrix3(this);
},
/**
* This method is an alias for `Matrix3.copy`.
*
* @method Phaser.Math.Matrix3#set
* @since 3.0.0
*
* @param {Phaser.Math.Matrix3} src - The Matrix to set the values of this Matrix's from.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
set: function (src)
{
return this.copy(src);
},
/**
* Copy the values of a given Matrix into this Matrix.
*
* @method Phaser.Math.Matrix3#copy
* @since 3.0.0
*
* @param {Phaser.Math.Matrix3} src - The Matrix to copy the values from.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
copy: function (src)
{
var out = this.val;
var a = src.val;
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
out[6] = a[6];
out[7] = a[7];
out[8] = a[8];
return this;
},
/**
* Copy the values of a given Matrix4 into this Matrix3.
*
* @method Phaser.Math.Matrix3#fromMat4
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} m - The Matrix4 to copy the values from.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
fromMat4: function (m)
{
var a = m.val;
var out = this.val;
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[4];
out[4] = a[5];
out[5] = a[6];
out[6] = a[8];
out[7] = a[9];
out[8] = a[10];
return this;
},
/**
* Set the values of this Matrix from the given array.
*
* @method Phaser.Math.Matrix3#fromArray
* @since 3.0.0
*
* @param {array} a - The array to copy the values from.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
fromArray: function (a)
{
var out = this.val;
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
out[6] = a[6];
out[7] = a[7];
out[8] = a[8];
return this;
},
/**
* Reset this Matrix to an identity (default) matrix.
*
* @method Phaser.Math.Matrix3#identity
* @since 3.0.0
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
identity: function ()
{
var out = this.val;
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 1;
out[5] = 0;
out[6] = 0;
out[7] = 0;
out[8] = 1;
return this;
},
/**
* Transpose this Matrix.
*
* @method Phaser.Math.Matrix3#transpose
* @since 3.0.0
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
transpose: function ()
{
var a = this.val;
var a01 = a[1];
var a02 = a[2];
var a12 = a[5];
a[1] = a[3];
a[2] = a[6];
a[3] = a01;
a[5] = a[7];
a[6] = a02;
a[7] = a12;
return this;
},
/**
* Invert this Matrix.
*
* @method Phaser.Math.Matrix3#invert
* @since 3.0.0
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
invert: function ()
{
var a = this.val;
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a10 = a[3];
var a11 = a[4];
var a12 = a[5];
var a20 = a[6];
var a21 = a[7];
var a22 = a[8];
var b01 = a22 * a11 - a12 * a21;
var b11 = -a22 * a10 + a12 * a20;
var b21 = a21 * a10 - a11 * a20;
// Calculate the determinant
var det = a00 * b01 + a01 * b11 + a02 * b21;
if (!det)
{
return null;
}
det = 1 / det;
a[0] = b01 * det;
a[1] = (-a22 * a01 + a02 * a21) * det;
a[2] = (a12 * a01 - a02 * a11) * det;
a[3] = b11 * det;
a[4] = (a22 * a00 - a02 * a20) * det;
a[5] = (-a12 * a00 + a02 * a10) * det;
a[6] = b21 * det;
a[7] = (-a21 * a00 + a01 * a20) * det;
a[8] = (a11 * a00 - a01 * a10) * det;
return this;
},
/**
* Calculate the adjoint, or adjugate, of this Matrix.
*
* @method Phaser.Math.Matrix3#adjoint
* @since 3.0.0
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
adjoint: function ()
{
var a = this.val;
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a10 = a[3];
var a11 = a[4];
var a12 = a[5];
var a20 = a[6];
var a21 = a[7];
var a22 = a[8];
a[0] = (a11 * a22 - a12 * a21);
a[1] = (a02 * a21 - a01 * a22);
a[2] = (a01 * a12 - a02 * a11);
a[3] = (a12 * a20 - a10 * a22);
a[4] = (a00 * a22 - a02 * a20);
a[5] = (a02 * a10 - a00 * a12);
a[6] = (a10 * a21 - a11 * a20);
a[7] = (a01 * a20 - a00 * a21);
a[8] = (a00 * a11 - a01 * a10);
return this;
},
/**
* Calculate the determinant of this Matrix.
*
* @method Phaser.Math.Matrix3#determinant
* @since 3.0.0
*
* @return {number} The determinant of this Matrix.
*/
determinant: function ()
{
var a = this.val;
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a10 = a[3];
var a11 = a[4];
var a12 = a[5];
var a20 = a[6];
var a21 = a[7];
var a22 = a[8];
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
},
/**
* Multiply this Matrix by the given Matrix.
*
* @method Phaser.Math.Matrix3#multiply
* @since 3.0.0
*
* @param {Phaser.Math.Matrix3} src - The Matrix to multiply this Matrix by.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
multiply: function (src)
{
var a = this.val;
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a10 = a[3];
var a11 = a[4];
var a12 = a[5];
var a20 = a[6];
var a21 = a[7];
var a22 = a[8];
var b = src.val;
var b00 = b[0];
var b01 = b[1];
var b02 = b[2];
var b10 = b[3];
var b11 = b[4];
var b12 = b[5];
var b20 = b[6];
var b21 = b[7];
var b22 = b[8];
a[0] = b00 * a00 + b01 * a10 + b02 * a20;
a[1] = b00 * a01 + b01 * a11 + b02 * a21;
a[2] = b00 * a02 + b01 * a12 + b02 * a22;
a[3] = b10 * a00 + b11 * a10 + b12 * a20;
a[4] = b10 * a01 + b11 * a11 + b12 * a21;
a[5] = b10 * a02 + b11 * a12 + b12 * a22;
a[6] = b20 * a00 + b21 * a10 + b22 * a20;
a[7] = b20 * a01 + b21 * a11 + b22 * a21;
a[8] = b20 * a02 + b21 * a12 + b22 * a22;
return this;
},
/**
* Translate this Matrix using the given Vector.
*
* @method Phaser.Math.Matrix3#translate
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to translate this Matrix with.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
translate: function (v)
{
var a = this.val;
var x = v.x;
var y = v.y;
a[6] = x * a[0] + y * a[3] + a[6];
a[7] = x * a[1] + y * a[4] + a[7];
a[8] = x * a[2] + y * a[5] + a[8];
return this;
},
/**
* Apply a rotation transformation to this Matrix.
*
* @method Phaser.Math.Matrix3#rotate
* @since 3.0.0
*
* @param {number} rad - The angle in radians to rotate by.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
rotate: function (rad)
{
var a = this.val;
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a10 = a[3];
var a11 = a[4];
var a12 = a[5];
var s = Math.sin(rad);
var c = Math.cos(rad);
a[0] = c * a00 + s * a10;
a[1] = c * a01 + s * a11;
a[2] = c * a02 + s * a12;
a[3] = c * a10 - s * a00;
a[4] = c * a11 - s * a01;
a[5] = c * a12 - s * a02;
return this;
},
/**
* Apply a scale transformation to this Matrix.
*
* Uses the `x` and `y` components of the given Vector to scale the Matrix.
*
* @method Phaser.Math.Matrix3#scale
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to scale this Matrix with.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
scale: function (v)
{
var a = this.val;
var x = v.x;
var y = v.y;
a[0] = x * a[0];
a[1] = x * a[1];
a[2] = x * a[2];
a[3] = y * a[3];
a[4] = y * a[4];
a[5] = y * a[5];
return this;
},
/**
* Set the values of this Matrix from the given Quaternion.
*
* @method Phaser.Math.Matrix3#fromQuat
* @since 3.0.0
*
* @param {Phaser.Math.Quaternion} q - The Quaternion to set the values of this Matrix from.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
fromQuat: function (q)
{
var x = q.x;
var y = q.y;
var z = q.z;
var w = q.w;
var x2 = x + x;
var y2 = y + y;
var z2 = z + z;
var xx = x * x2;
var xy = x * y2;
var xz = x * z2;
var yy = y * y2;
var yz = y * z2;
var zz = z * z2;
var wx = w * x2;
var wy = w * y2;
var wz = w * z2;
var out = this.val;
out[0] = 1 - (yy + zz);
out[3] = xy + wz;
out[6] = xz - wy;
out[1] = xy - wz;
out[4] = 1 - (xx + zz);
out[7] = yz + wx;
out[2] = xz + wy;
out[5] = yz - wx;
out[8] = 1 - (xx + yy);
return this;
},
/**
* Set the values of this Matrix3 to be normalized from the given Matrix4.
*
* @method Phaser.Math.Matrix3#normalFromMat4
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} m - The Matrix4 to normalize the values from.
*
* @return {Phaser.Math.Matrix3} This Matrix3.
*/
normalFromMat4: function (m)
{
var a = m.val;
var out = this.val;
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a03 = a[3];
var a10 = a[4];
var a11 = a[5];
var a12 = a[6];
var a13 = a[7];
var a20 = a[8];
var a21 = a[9];
var a22 = a[10];
var a23 = a[11];
var a30 = a[12];
var a31 = a[13];
var a32 = a[14];
var a33 = a[15];
var b00 = a00 * a11 - a01 * a10;
var b01 = a00 * a12 - a02 * a10;
var b02 = a00 * a13 - a03 * a10;
var b03 = a01 * a12 - a02 * a11;
var b04 = a01 * a13 - a03 * a11;
var b05 = a02 * a13 - a03 * a12;
var b06 = a20 * a31 - a21 * a30;
var b07 = a20 * a32 - a22 * a30;
var b08 = a20 * a33 - a23 * a30;
var b09 = a21 * a32 - a22 * a31;
var b10 = a21 * a33 - a23 * a31;
var b11 = a22 * a33 - a23 * a32;
// Calculate the determinant
var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
if (!det)
{
return null;
}
det = 1 / det;
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
return this;
}
});
module.exports = Matrix3;