/
Matrix4.js
1787 lines (1549 loc) · 44.7 KB
/
Matrix4.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var Vector3 = require('./Vector3');
/**
* @ignore
*/
var EPSILON = 0.000001;
/**
* @classdesc
* A four-dimensional matrix.
*
* Adapted from [gl-matrix](https://github.com/toji/gl-matrix) by toji
* and [vecmath](https://github.com/mattdesl/vecmath) by mattdesl
*
* @class Matrix4
* @memberof Phaser.Math
* @constructor
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} [m] - Optional Matrix4 to copy values from.
*/
var Matrix4 = new Class({
initialize:
function Matrix4 (m)
{
/**
* The matrix values.
*
* @name Phaser.Math.Matrix4#val
* @type {Float32Array}
* @since 3.0.0
*/
this.val = new Float32Array(16);
if (m)
{
// Assume Matrix4 with val:
this.copy(m);
}
else
{
// Default to identity
this.identity();
}
},
/**
* Make a clone of this Matrix4.
*
* @method Phaser.Math.Matrix4#clone
* @since 3.0.0
*
* @return {Phaser.Math.Matrix4} A clone of this Matrix4.
*/
clone: function ()
{
return new Matrix4(this);
},
/**
* This method is an alias for `Matrix4.copy`.
*
* @method Phaser.Math.Matrix4#set
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} src - The Matrix to set the values of this Matrix's from.
*
* @return {this} This Matrix4.
*/
set: function (src)
{
return this.copy(src);
},
/**
* Sets all values of this Matrix4.
*
* @method Phaser.Math.Matrix4#setValues
* @since 3.50.0
*
* @param {number} m00 - The m00 value.
* @param {number} m01 - The m01 value.
* @param {number} m02 - The m02 value.
* @param {number} m03 - The m03 value.
* @param {number} m10 - The m10 value.
* @param {number} m11 - The m11 value.
* @param {number} m12 - The m12 value.
* @param {number} m13 - The m13 value.
* @param {number} m20 - The m20 value.
* @param {number} m21 - The m21 value.
* @param {number} m22 - The m22 value.
* @param {number} m23 - The m23 value.
* @param {number} m30 - The m30 value.
* @param {number} m31 - The m31 value.
* @param {number} m32 - The m32 value.
* @param {number} m33 - The m33 value.
*
* @return {this} This Matrix4 instance.
*/
setValues: function (m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33)
{
var out = this.val;
out[0] = m00;
out[1] = m01;
out[2] = m02;
out[3] = m03;
out[4] = m10;
out[5] = m11;
out[6] = m12;
out[7] = m13;
out[8] = m20;
out[9] = m21;
out[10] = m22;
out[11] = m23;
out[12] = m30;
out[13] = m31;
out[14] = m32;
out[15] = m33;
return this;
},
/**
* Copy the values of a given Matrix into this Matrix.
*
* @method Phaser.Math.Matrix4#copy
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} src - The Matrix to copy the values from.
*
* @return {this} This Matrix4.
*/
copy: function (src)
{
var a = src.val;
return this.setValues(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
},
/**
* Set the values of this Matrix from the given array.
*
* @method Phaser.Math.Matrix4#fromArray
* @since 3.0.0
*
* @param {number[]} a - The array to copy the values from. Must have at least 16 elements.
*
* @return {this} This Matrix4.
*/
fromArray: function (a)
{
return this.setValues(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
},
/**
* Reset this Matrix.
*
* Sets all values to `0`.
*
* @method Phaser.Math.Matrix4#zero
* @since 3.0.0
*
* @return {Phaser.Math.Matrix4} This Matrix4.
*/
zero: function ()
{
return this.setValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
},
/**
* Generates a transform matrix based on the given position, scale and rotation.
*
* @method Phaser.Math.Matrix4#transform
* @since 3.50.0
*
* @param {Phaser.Math.Vector3} position - The position vector.
* @param {Phaser.Math.Vector3} scale - The scale vector.
* @param {Phaser.Math.Quaternion} rotation - The rotation quaternion.
*
* @return {this} This Matrix4.
*/
transform: function (position, scale, rotation)
{
var rotMatrix = _tempMat1.fromQuat(rotation);
var rm = rotMatrix.val;
var sx = scale.x;
var sy = scale.y;
var sz = scale.z;
return this.setValues(
rm[0] * sx,
rm[1] * sx,
rm[2] * sx,
0,
rm[4] * sy,
rm[5] * sy,
rm[6] * sy,
0,
rm[8] * sz,
rm[9] * sz,
rm[10] * sz,
0,
position.x,
position.y,
position.z,
1
);
},
/**
* Set the `x`, `y` and `z` values of this Matrix.
*
* @method Phaser.Math.Matrix4#xyz
* @since 3.0.0
*
* @param {number} x - The x value.
* @param {number} y - The y value.
* @param {number} z - The z value.
*
* @return {this} This Matrix4.
*/
xyz: function (x, y, z)
{
this.identity();
var out = this.val;
out[12] = x;
out[13] = y;
out[14] = z;
return this;
},
/**
* Set the scaling values of this Matrix.
*
* @method Phaser.Math.Matrix4#scaling
* @since 3.0.0
*
* @param {number} x - The x scaling value.
* @param {number} y - The y scaling value.
* @param {number} z - The z scaling value.
*
* @return {this} This Matrix4.
*/
scaling: function (x, y, z)
{
this.zero();
var out = this.val;
out[0] = x;
out[5] = y;
out[10] = z;
out[15] = 1;
return this;
},
/**
* Reset this Matrix to an identity (default) matrix.
*
* @method Phaser.Math.Matrix4#identity
* @since 3.0.0
*
* @return {this} This Matrix4.
*/
identity: function ()
{
return this.setValues(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
},
/**
* Transpose this Matrix.
*
* @method Phaser.Math.Matrix4#transpose
* @since 3.0.0
*
* @return {this} This Matrix4.
*/
transpose: function ()
{
var a = this.val;
var a01 = a[1];
var a02 = a[2];
var a03 = a[3];
var a12 = a[6];
var a13 = a[7];
var a23 = a[11];
a[1] = a[4];
a[2] = a[8];
a[3] = a[12];
a[4] = a01;
a[6] = a[9];
a[7] = a[13];
a[8] = a02;
a[9] = a12;
a[11] = a[14];
a[12] = a03;
a[13] = a13;
a[14] = a23;
return this;
},
/**
* Copies the given Matrix4 into this Matrix and then inverses it.
*
* @method Phaser.Math.Matrix4#getInverse
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} m - The Matrix4 to invert into this Matrix4.
*
* @return {this} This Matrix4.
*/
getInverse: function (m)
{
this.copy(m);
return this.invert();
},
/**
* Invert this Matrix.
*
* @method Phaser.Math.Matrix4#invert
* @since 3.0.0
*
* @return {this} This Matrix4.
*/
invert: function ()
{
var a = 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 this;
}
det = 1 / det;
return this.setValues(
(a11 * b11 - a12 * b10 + a13 * b09) * det,
(a02 * b10 - a01 * b11 - a03 * b09) * det,
(a31 * b05 - a32 * b04 + a33 * b03) * det,
(a22 * b04 - a21 * b05 - a23 * b03) * det,
(a12 * b08 - a10 * b11 - a13 * b07) * det,
(a00 * b11 - a02 * b08 + a03 * b07) * det,
(a32 * b02 - a30 * b05 - a33 * b01) * det,
(a20 * b05 - a22 * b02 + a23 * b01) * det,
(a10 * b10 - a11 * b08 + a13 * b06) * det,
(a01 * b08 - a00 * b10 - a03 * b06) * det,
(a30 * b04 - a31 * b02 + a33 * b00) * det,
(a21 * b02 - a20 * b04 - a23 * b00) * det,
(a11 * b07 - a10 * b09 - a12 * b06) * det,
(a00 * b09 - a01 * b07 + a02 * b06) * det,
(a31 * b01 - a30 * b03 - a32 * b00) * det,
(a20 * b03 - a21 * b01 + a22 * b00) * det
);
},
/**
* Calculate the adjoint, or adjugate, of this Matrix.
*
* @method Phaser.Math.Matrix4#adjoint
* @since 3.0.0
*
* @return {this} This Matrix4.
*/
adjoint: function ()
{
var a = 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];
return this.setValues(
(a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22)),
-(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22)),
(a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12)),
-(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12)),
-(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22)),
(a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22)),
-(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12)),
(a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12)),
(a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21)),
-(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21)),
(a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11)),
-(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11)),
-(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21)),
(a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21)),
-(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11)),
(a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11))
);
},
/**
* Calculate the determinant of this Matrix.
*
* @method Phaser.Math.Matrix4#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 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
return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
},
/**
* Multiply this Matrix by the given Matrix.
*
* @method Phaser.Math.Matrix4#multiply
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} src - The Matrix to multiply this Matrix by.
*
* @return {this} This Matrix4.
*/
multiply: function (src)
{
var a = 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 b = src.val;
// Cache only the current line of the second matrix
var b0 = b[0];
var b1 = b[1];
var b2 = b[2];
var b3 = b[3];
a[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
a[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
a[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
a[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[4];
b1 = b[5];
b2 = b[6];
b3 = b[7];
a[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
a[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
a[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
a[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[8];
b1 = b[9];
b2 = b[10];
b3 = b[11];
a[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
a[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
a[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
a[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[12];
b1 = b[13];
b2 = b[14];
b3 = b[15];
a[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
a[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
a[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
a[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
return this;
},
/**
* Multiply the values of this Matrix4 by those given in the `src` argument.
*
* @method Phaser.Math.Matrix4#multiplyLocal
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} src - The source Matrix4 that this Matrix4 is multiplied by.
*
* @return {this} This Matrix4.
*/
multiplyLocal: function (src)
{
var a = this.val;
var b = src.val;
return this.setValues(
a[0] * b[0] + a[1] * b[4] + a[2] * b[8] + a[3] * b[12],
a[0] * b[1] + a[1] * b[5] + a[2] * b[9] + a[3] * b[13],
a[0] * b[2] + a[1] * b[6] + a[2] * b[10] + a[3] * b[14],
a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3] * b[15],
a[4] * b[0] + a[5] * b[4] + a[6] * b[8] + a[7] * b[12],
a[4] * b[1] + a[5] * b[5] + a[6] * b[9] + a[7] * b[13],
a[4] * b[2] + a[5] * b[6] + a[6] * b[10] + a[7] * b[14],
a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7] * b[15],
a[8] * b[0] + a[9] * b[4] + a[10] * b[8] + a[11] * b[12],
a[8] * b[1] + a[9] * b[5] + a[10] * b[9] + a[11] * b[13],
a[8] * b[2] + a[9] * b[6] + a[10] * b[10] + a[11] * b[14],
a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11] * b[15],
a[12] * b[0] + a[13] * b[4] + a[14] * b[8] + a[15] * b[12],
a[12] * b[1] + a[13] * b[5] + a[14] * b[9] + a[15] * b[13],
a[12] * b[2] + a[13] * b[6] + a[14] * b[10] + a[15] * b[14],
a[12] * b[3] + a[13] * b[7] + a[14] * b[11] + a[15] * b[15]
);
},
/**
* Multiplies the given Matrix4 object with this Matrix.
*
* This is the same as calling `multiplyMatrices(m, this)`.
*
* @method Phaser.Math.Matrix4#premultiply
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} m - The Matrix4 to multiply with this one.
*
* @return {this} This Matrix4.
*/
premultiply: function (m)
{
return this.multiplyMatrices(m, this);
},
/**
* Multiplies the two given Matrix4 objects and stores the results in this Matrix.
*
* @method Phaser.Math.Matrix4#multiplyMatrices
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} a - The first Matrix4 to multiply.
* @param {Phaser.Math.Matrix4} b - The second Matrix4 to multiply.
*
* @return {this} This Matrix4.
*/
multiplyMatrices: function (a, b)
{
var am = a.val;
var bm = b.val;
var a11 = am[0];
var a12 = am[4];
var a13 = am[8];
var a14 = am[12];
var a21 = am[1];
var a22 = am[5];
var a23 = am[9];
var a24 = am[13];
var a31 = am[2];
var a32 = am[6];
var a33 = am[10];
var a34 = am[14];
var a41 = am[3];
var a42 = am[7];
var a43 = am[11];
var a44 = am[15];
var b11 = bm[0];
var b12 = bm[4];
var b13 = bm[8];
var b14 = bm[12];
var b21 = bm[1];
var b22 = bm[5];
var b23 = bm[9];
var b24 = bm[13];
var b31 = bm[2];
var b32 = bm[6];
var b33 = bm[10];
var b34 = bm[14];
var b41 = bm[3];
var b42 = bm[7];
var b43 = bm[11];
var b44 = bm[15];
return this.setValues(
a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41,
a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41,
a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41,
a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41,
a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42,
a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42,
a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42,
a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42,
a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43,
a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43,
a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43,
a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43,
a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44,
a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44,
a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44,
a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44
);
},
/**
* Translate this Matrix using the given Vector.
*
* @method Phaser.Math.Matrix4#translate
* @since 3.0.0
*
* @param {(Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to translate this Matrix with.
*
* @return {this} This Matrix4.
*/
translate: function (v)
{
return this.translateXYZ(v.x, v.y, v.z);
},
/**
* Translate this Matrix using the given values.
*
* @method Phaser.Math.Matrix4#translateXYZ
* @since 3.16.0
*
* @param {number} x - The x component.
* @param {number} y - The y component.
* @param {number} z - The z component.
*
* @return {this} This Matrix4.
*/
translateXYZ: function (x, y, z)
{
var a = this.val;
a[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
a[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
a[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
a[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
return this;
},
/**
* Apply a scale transformation to this Matrix.
*
* Uses the `x`, `y` and `z` components of the given Vector to scale the Matrix.
*
* @method Phaser.Math.Matrix4#scale
* @since 3.0.0
*
* @param {(Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to scale this Matrix with.
*
* @return {this} This Matrix4.
*/
scale: function (v)
{
return this.scaleXYZ(v.x, v.y, v.z);
},
/**
* Apply a scale transformation to this Matrix.
*
* @method Phaser.Math.Matrix4#scaleXYZ
* @since 3.16.0
*
* @param {number} x - The x component.
* @param {number} y - The y component.
* @param {number} z - The z component.
*
* @return {this} This Matrix4.
*/
scaleXYZ: function (x, y, z)
{
var a = this.val;
a[0] = a[0] * x;
a[1] = a[1] * x;
a[2] = a[2] * x;
a[3] = a[3] * x;
a[4] = a[4] * y;
a[5] = a[5] * y;
a[6] = a[6] * y;
a[7] = a[7] * y;
a[8] = a[8] * z;
a[9] = a[9] * z;
a[10] = a[10] * z;
a[11] = a[11] * z;
return this;
},
/**
* Derive a rotation matrix around the given axis.
*
* @method Phaser.Math.Matrix4#makeRotationAxis
* @since 3.0.0
*
* @param {(Phaser.Math.Vector3|Phaser.Math.Vector4)} axis - The rotation axis.
* @param {number} angle - The rotation angle in radians.
*
* @return {this} This Matrix4.
*/
makeRotationAxis: function (axis, angle)
{
// Based on http://www.gamedev.net/reference/articles/article1199.asp
var c = Math.cos(angle);
var s = Math.sin(angle);
var t = 1 - c;
var x = axis.x;
var y = axis.y;
var z = axis.z;
var tx = t * x;
var ty = t * y;
return this.setValues(
tx * x + c, tx * y - s * z, tx * z + s * y, 0,
tx * y + s * z, ty * y + c, ty * z - s * x, 0,
tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
0, 0, 0, 1
);
},
/**
* Apply a rotation transformation to this Matrix.
*
* @method Phaser.Math.Matrix4#rotate
* @since 3.0.0
*
* @param {number} rad - The angle in radians to rotate by.
* @param {Phaser.Math.Vector3} axis - The axis to rotate upon.
*
* @return {this} This Matrix4.
*/
rotate: function (rad, axis)
{
var a = this.val;
var x = axis.x;
var y = axis.y;
var z = axis.z;
var len = Math.sqrt(x * x + y * y + z * z);
if (Math.abs(len) < EPSILON)
{
return this;
}
len = 1 / len;
x *= len;
y *= len;
z *= len;
var s = Math.sin(rad);
var c = Math.cos(rad);
var t = 1 - c;
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];
// Construct the elements of the rotation matrix
var b00 = x * x * t + c;
var b01 = y * x * t + z * s;
var b02 = z * x * t - y * s;
var b10 = x * y * t - z * s;
var b11 = y * y * t + c;
var b12 = z * y * t + x * s;
var b20 = x * z * t + y * s;
var b21 = y * z * t - x * s;
var b22 = z * z * t + c;
// Perform rotation-specific matrix multiplication
return this.setValues(
a00 * b00 + a10 * b01 + a20 * b02,
a01 * b00 + a11 * b01 + a21 * b02,
a02 * b00 + a12 * b01 + a22 * b02,
a03 * b00 + a13 * b01 + a23 * b02,
a00 * b10 + a10 * b11 + a20 * b12,
a01 * b10 + a11 * b11 + a21 * b12,
a02 * b10 + a12 * b11 + a22 * b12,
a03 * b10 + a13 * b11 + a23 * b12,
a00 * b20 + a10 * b21 + a20 * b22,
a01 * b20 + a11 * b21 + a21 * b22,
a02 * b20 + a12 * b21 + a22 * b22,
a03 * b20 + a13 * b21 + a23 * b22,
a30, a31, a32, a33
);
},
/**
* Rotate this matrix on its X axis.
*
* @method Phaser.Math.Matrix4#rotateX
* @since 3.0.0
*
* @param {number} rad - The angle in radians to rotate by.
*
* @return {this} This Matrix4.
*/
rotateX: function (rad)
{
var a = this.val;
var s = Math.sin(rad);
var c = Math.cos(rad);
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];
// Perform axis-specific matrix multiplication
a[4] = a10 * c + a20 * s;
a[5] = a11 * c + a21 * s;
a[6] = a12 * c + a22 * s;
a[7] = a13 * c + a23 * s;
a[8] = a20 * c - a10 * s;
a[9] = a21 * c - a11 * s;
a[10] = a22 * c - a12 * s;
a[11] = a23 * c - a13 * s;
return this;
},
/**
* Rotate this matrix on its Y axis.
*
* @method Phaser.Math.Matrix4#rotateY
* @since 3.0.0
*
* @param {number} rad - The angle to rotate by, in radians.
*
* @return {this} This Matrix4.
*/
rotateY: function (rad)
{
var a = this.val;
var s = Math.sin(rad);
var c = Math.cos(rad);
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a03 = a[3];
var a20 = a[8];
var a21 = a[9];
var a22 = a[10];
var a23 = a[11];
// Perform axis-specific matrix multiplication
a[0] = a00 * c - a20 * s;
a[1] = a01 * c - a21 * s;
a[2] = a02 * c - a22 * s;
a[3] = a03 * c - a23 * s;
a[8] = a00 * s + a20 * c;