-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.js
1601 lines (1418 loc) · 57 KB
/
math.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
"use strict";
// Math extensions
Math.deg2rad = Math.PI/180.0;
//............................................
// Vector classes
class Vector2{
raw;
get x(){ return this.raw[0];}
get y(){ return this.raw[1];}
set x(x){this.raw[0] = x;}
set y(y){this.raw[1] = y;}
static get ZERO(){ return new Vector2( 0.0, 0.0)};
static get RIGHT(){return new Vector2( 1.0, 0.0)};
static get LEFT(){ return new Vector2(-1.0, 0.0)};
static get UP(){ return new Vector2( 0.0, 1.0)};
static get DOWN(){ return new Vector2( 0.0,-1.0)};
constructor(x,y){
this.raw = [0,0];
if(Array.isArray(x)){
this.x = x[0];
this.y = x[1];
} else {
if(!isNaN(x)){ this.x = x;}
if(!isNaN(y)){ this.y = y;}
}
}
/** returns the magnitude of this vector.
* @return {number} the magnitude of this vector.
*/
magnitude(){
return Math.sqrt(this.x * this.x + this.y * this.y);
}
/** returns the squared magnitude of this vector.
* @return {number} the squared magnitude of this vector.
*/
sqrMagnitude(){
return this.x * this.x + this.y * this.y;
}
/** returns the distance from this point to the given point v.
* @param {Vector2} v the point to valculate the distance to.
* @return {number} the distance to v.
*/
distanceTo(v){
let x = v.x - this.x;
let y = v.y - this.y;
return Math.sqrt(x * x + y * y);
}
/** normalizes this vector.
* @return {Vector2} this vector to chain up commands.
*/
normalize(){
let mag = this.magnitude();
this.x /= mag;
this.y /= mag;
return this;
}
/** sets the values x and y for this vector.
* @param {number} x the new x value.
* @param {number} y the new y value.
* @return {Vector2} this vector to chain up commands.
*/
set(x, y){
this.x = x;
this.y = y;
return this;
}
/** multiplies the scalar s on this vector
* @param {number} s value to multiply and scale the vector.
* @return {Vector2} this vector to chain up commands.
*/
multiplyScalar(s){
this.x *= s;
this.y *= s;
return this;
}
/** calculates and returns the dot product between this vector and the vector v.
* @param {Vector2} v vector to calculate the dot product with.
* @return {number} the result of the dot product.
*/
dot(v){
return this.x * v.x + this.y * v.y;
}
/** adds another vector v on top of this vector.
* @param {Vector2} v vector to add to this vector.
* @return {Vector2} this vector to chain up commands.
*/
addV(v){
this.x += v.x;
this.y += v.y;
return this;
}
/** adds the values x and y to this vector.
* @param {number} x x value to add.
* @param {number} y y value to add
* @return {Vector2} this vector to chain up commands.
*/
add(x, y){
this.x += x;
this.y += y;
return this;
}
/** subtracts another vector v from this vector.
* @param {Vector2} v vector to subtract from this vector.
* @return {Vector2} this vector to chain up commands.
*/
substractV(v){
this.x -= v.x;
this.y -= v.y;
return this;
}
/** subtracts the values x and y from this vector.
* @param {number} x x value to subtract.
* @param {number} y y value to subtract.
* @return {Vector2} this vector to chain up commands.
*/
substract(x, y){
this.x -= x;
this.y -= y;
return this;
}
/** inverts this vector (makes it face the opposite direction, but keep its length).
* @return {Vector2} this vector to chain up commands.
*/
invert(){
this.x = -this.x;
this.y = -this.y;
return this;
}
equals(v){
if(!(v instanceof Vector2))
return false;
return this.x === v.x && this.y === v.y;
}
clone(){ return new Vector2(this.x, this.y); }
toVector3(){ return new Vector3(this.x, this.y, 0); }
toVector4(){ return new Vector4(this.x, this.y, 0, 1); }
toString(){ return "[" + this.raw.toString() + "]"; }
toFloat32(){ return new Float32Array(this.raw);}
}
class Vector3{
raw;
get x(){ return this.raw[0];}
get y(){ return this.raw[1];}
get z(){ return this.raw[2];}
set x(x){this.raw[0] = x;}
set y(y){this.raw[1] = y;}
set z(z){this.raw[2] = z;}
static get ZERO(){ return new Vector3( 0.0, 0.0, 0.0)};
static get RIGHT(){ return new Vector3( 1.0, 0.0, 0.0)};
static get LEFT(){ return new Vector3(-1.0, 0.0, 0.0)};
static get UP(){ return new Vector3( 0.0, 1.0, 0.0)};
static get DOWN(){ return new Vector3( 0.0,-1.0, 0.0)};
static get FORWARD(){return new Vector3( 0.0, 0.0, 1.0)};
static get BACK(){ return new Vector3( 0.0, 0.0,-1.0)};
constructor(x,y,z){
this.raw = [0,0,0];
if(Array.isArray(x)){
this.x = x[0];
this.y = x[1];
this.z = x[2];
} else {
if(!isNaN(x)){ this.x = x;}
if(!isNaN(y)){ this.y = y;}
if(!isNaN(z)){ this.z = z;}
}
}
/** returns the magnitude of this vector.
* @return {number} the magnitude of this vector.
*/
magnitude(){
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
/** returns the squared magnitude of this vector.
* @return {number} the squared magnitude of this vector.
*/
sqrMagnitude(){
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/** returns the distance from this point to the given point v.
* @param {Vector3} v the point to valculate the distance to.
* @return {number} the distance to v.
*/
distanceTo(v){
let x = v.x - this.x;
let y = v.y - this.y;
let z = v.z - this.z;
return Math.sqrt(x * x + y * y + z * z);
}
/** normalizes this vector.
* @return {Vector3} this vector to chain up commands.
*/
normalize(){
let mag = this.magnitude();
this.x /= mag;
this.y /= mag;
this.z /= mag;
return this;
}
/** sets the values x and y for this vector.
* @param {number} x the new x value.
* @param {number} y the new y value.
* @param {number} z the new z value.
* @return {Vector3} this vector to chain up commands.
*/
set(x, y, z){
this.x = x;
this.y = y;
this.z = z;
return this;
}
/** multiplies the scalar s on this vector
* @param {number} s value to multiply and scale the vector.
* @return {Vector3} this vector to chain up commands.
*/
multiplyScalar(s){
this.x *= s;
this.y *= s;
this.z *= s;
return this;
}
/** calculates and returns the dot product between this vector and the vector v.
* @param {Vector3} v vector to calculate the dot product with.
* @return {number} the result of the dot product.
*/
dot(v){
return this.x * v.x + this.y * v.y + this.z * v.z;
}
/** calculates and returns the cross product between this vector and the vector v (this X v).
* @param {Vector3} v vector to calculate the cross product with.
* @return {Vector3} the result of the cross product.
*/
cross(v){
return new Vector3( this.y*v.z - this.z*v.y,
this.z*v.x - this.x*v.z,
this.x*v.y - this.y*v.x);
}
/** adds another vector v on top of this vector.
* @param {Vector3} v vector to add to this vector.
* @return {Vector3} this vector to chain up commands.
*/
addV(v){
this.x += v.x;
this.y += v.y;
if(v.z) this.z += v.z;
return this;
}
/** adds the values x, y and z to this vector.
* @param {number} x x value to add.
* @param {number} y y value to add
* @param {number} z z value to add
* @return {Vector3} this vector to chain up commands.
*/
add(x, y, z){
this.x += x;
this.y += y;
if(z) this.z += z;
return this;
}
/** subtracts another vector v from this vector.
* @param {Vector3} v vector to subtract from this vector.
* @return {Vector3} this vector to chain up commands.
*/
substractV(v){
this.x -= v.x;
this.y -= v.y;
if(v.z) this.z -= v.z;
return this;
}
/** subtracts the values x, y and z from this vector.
* @param {number} x x value to subtract.
* @param {number} y y value to subtract.
* @param {number} z z value to subtract.
* @return {Vector3} this vector to chain up commands.
*/
substract(x, y, z){
this.x -= x;
this.y -= y;
if(z) this.z -= z;
return this;
}
/** inverts this vector (makes it face the opposite direction, but keep its length).
* @return {Vector3} this vector to chain up commands.
*/
invert(){
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
}
equals(v){
if(!(v instanceof Vector3))
return false;
return this.x === v.x && this.y === v.y && this.z === v.z;
}
clone(){ return new Vector3(this.x, this.y, this.z); }
toVector2(){ return new Vector2(this.x, this.y); }
toVector4(){ return new Vector4(this.x, this.y, 0, 1); }
toString(){ return "[" + this.raw.toString() + "]"; }
toFloat32(){ return new Float32Array(this.raw);}
}
class Vector4{
raw;
get x(){ return this.raw[0];}
get y(){ return this.raw[1];}
get z(){ return this.raw[2];}
get w(){ return this.raw[3];}
set x(x){this.raw[0] = x;}
set y(y){this.raw[1] = y;}
set z(z){this.raw[2] = z;}
set w(w){this.raw[3] = w;}
static get ZERO(){ return new Vector4( 0.0, 0.0, 0.0, 0.0)};
static get RIGHT(){ return new Vector4( 1.0, 0.0, 0.0, 1.0)};
static get LEFT(){ return new Vector4(-1.0, 0.0, 0.0, 1.0)};
static get UP(){ return new Vector4( 0.0, 1.0, 0.0, 1.0)};
static get DOWN(){ return new Vector4( 0.0,-1.0, 0.0, 1.0)};
static get FORWARD(){return new Vector4( 0.0, 0.0, 1.0, 1.0)};
static get BACK(){ return new Vector4( 0.0, 0.0,-1.0, 1.0)};
constructor(x,y,z,w){
this.raw = [0,0,0,1];
if(Array.isArray(x)){
this.x = x[0];
this.y = x[1];
this.z = x[2];
this.w = x[3];
} else {
if(!isNaN(x)){ this.x = x;}
if(!isNaN(y)){ this.y = y;}
if(!isNaN(z)){ this.z = z;}
if(!isNaN(w)){ this.w = w;}
}
}
/** returns the magnitude of this vector.
* @return {number} the magnitude of this vector.
*/
magnitude(){
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
}
/** returns the squared magnitude of this vector.
* @return {number} the squared magnitude of this vector.
*/
sqrMagnitude(){
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/** returns the distance from this point to the given point v.
* @param {Vector4} v the point to valculate the distance to.
* @return {number} the distance to v.
*/
distanceTo(v){
let x = v.x - this.x;
let y = v.y - this.y;
let z = 0;
if(v.z)
z = v.z - this.z;
let w = 0;
if(v.w)
w = v.w - this.w;
return Math.sqrt(x * x + y * y + z * z + w * w);
}
/** normalizes this vector.
* @return {Vector4} this vector to chain up commands.
*/
normalize(){
let mag = this.magnitude();
this.x /= mag;
this.y /= mag;
this.z /= mag;
this.w /= mag;
return this;
}
/** sets the values x and y for this vector.
* @param {number} x the new x value.
* @param {number} y the new y value.
* @param {number} z the new z value.
* @param {number} w the new w value.
* @return {Vector4} this vector to chain up commands.
*/
set(x, y, z, w){
this.x = x;
this.y = y;
if(z) this.z = z;
if(w) this.w = w;
return this;
}
/** multiplies the scalar s on this vector
* @param {number} s value to multiply and scale the vector.
* @return {Vector4} this vector to chain up commands.
*/
multiplyScalar(s){
this.x *= s;
this.y *= s;
this.z *= s;
this.w *= s;
return this;
}
/** calculates and returns the dot product between this vector and the vector v.
* @param {Vector4} v vector to calculate the dot product with.
* @return {number} the result of the dot product.
*/
dot(v){
return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
}
/** adds another vector v on top of this vector.
* @param {Vector4} v vector to add to this vector.
* @return {Vector4} this vector to chain up commands.
*/
addV(v){
this.x += v.x;
this.y += v.y;
if(v.z) this.z += v.z;
if(v.w) this.w += v.w;
return this;
}
/** adds the values x, y, z and w to this vector.
* @param {number} x x value to add.
* @param {number} y y value to add
* @param {number} z z value to add
* @param {number} w w value to add
* @return {Vector3} this vector to chain up commands.
*/
add(x, y, z, w){
this.x += x;
this.y += y;
if(z) this.z += z;
if(w) this.w += w;
return this;
}
/** subtracts another vector v from this vector.
* @param {Vector4} v vector to subtract from this vector.
* @return {Vector4} this vector to chain up commands.
*/
substractV(v){
this.x -= v.x;
this.y -= v.y;
if(v.z) this.z -= v.z;
if(v.w) this.w -= v.w;
return this;
}
/** subtracts the values x, y, z and w from this vector.
* @param {number} x x value to subtract.
* @param {number} y y value to subtract.
* @param {number} z z value to subtract.
* @param {number} w w value to subtract.
* @return {Vector4} this vector to chain up commands.
*/
substract(x, y, z, w){
this.x -= x;
this.y -= y;
if(w) this.z -= z;
if(w) this.w -= w;
return this;
}
/** inverts this vector (makes it face the opposite direction, but keep its length).
* @return {Vector4} this vector to chain up commands.
*/
invert(){
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
this.w = -this.w;
return this;
}
equals(v){
if(!(v instanceof Vector4))
return false;
return this.x === v.x && this.y === v.y && this.z === v.z && this.w === v.w;
}
clone(){ return new Vector4(this.x, this.y, this.z, this.w); }
toVector2(){ return new Vector2(this.x, this.y); }
toVector3(){ return new Vector2(this.x, this.y, this.z); }
toString(){ return "[" + this.raw.toString() + "]"; }
toFloat32(){ return new Float32Array(this.raw);}
}
//............................................
// Matrix3x3 class
class Matrix3x3{
raw;
static get IDENTITY(){
return new Matrix3x3();
}
constructor(raw){
this.raw = [0,0,0,0,0,0,0,0,0];
if(raw === undefined){
this.raw[0] = this.raw[4] = this.raw[8] = 1;
} else {
for(let i = 0; i < 9; i++){
this.raw[i] = raw[i];
}
}
}
clone(){
return new Matrix3x3(this.raw);
}
//....................................................................
//Methods
/** reset data back to identity.
* @return {Matrix3x3} this matrix to chain up commands.
*/
reset(){
for(var i=0; i < 8; i++){
this.raw[i] = 0;
}
this.raw[0] = this.raw[4] = this.raw[8] = 1; //only positions 0,4,8 need to be 1 else 0.
return this;
}
/** Bring the matrix back to identity without changing the transform values.
* @return {Matrix3x3} this matrix to chain up commands.
*/
resetRotation(){
this.raw[1] = this.raw[2] = this.raw[3] = this.raw[5] = 0;
this.raw[0] = this.raw[4] = this.raw[8] = 1; //only positions 0,4,8 need to be 1 else 0.
return this;
}
/** Calculates determinant of this matrix
* @return {number} the determinant.
*/
determinant(){
let a = this.raw[0], b = this.raw[3], c = this.raw[6],
d = this.raw[1], e = this.raw[4], f = this.raw[7],
g = this.raw[2], h = this.raw[5], i = this.raw[8];
// Calculate the determinant
return a*(e*i - f*h) - b*(d*i - f*g) + c*(d*h - e*g);
//return a*e*i + b*f*g + c*d*h - c*e*g - a*f*h - b*d*i;
}
toString(){ return "[" + this.raw.toString() + "]";}
toFloat32(){ return new Float32Array(this.raw);}
//....................................................................
//Transformation Methods
//All methods can be called from an instance of Matrix4x4 and be performed on that instance,
//but simply forward the commands to the static methods with the actual implementation.
multM3(m){Matrix3x3.multM3(this, m, this); return this;}
multV2(v, out){Matrix3x3.multV2(this, v, out); return out;}
multV3(v, out){Matrix3x3.multV3(this, v, out); return out;}
transformV2(v, out){Matrix3x3.transformV2(this, v, out); return out;}
invert(){Matrix3x3.invert(this, this); return this;}
transpose(){Matrix3x3.transpose(this, this); return this;}
//....................................................................
// Static Methods
/** Creates a transformation matrix that translates by x, y.
* @param {number} x amount to translate on the x axis.
* @param {number} y amount to translate on the y axis.
* @param {Matrix3x3} out the matrix to store the resulting transformation matrix.
* @return {Matrix3x3} the resulting transformation matrix (out).
*/
static translationMatrix(x, y, out){
if(!out) out = Matrix3x3.IDENTITY;
out.raw[6] = x;
out.raw[7] = y;
return out;
}
/** Creates a transformation matrix that rotates by "angle" degrees.
* If the parameter out is not defined, a new matrix is created and returned
* @param {number} angle angle of rotion around in degrees.
* @param {Matrix3x3} out the matrix to store the resulting transformation matrix.
* @return {Matrix3x3} the resulting transformation matrix (out).
*/
static rotationMatrix(angle, out){
if(!out) out = Matrix3x3.IDENTITY;
angle *= Math.deg2rad;
out.raw[0] = Math.cos(angle);
out.raw[1] = Math.sin(angle);
out.raw[3] = -Math.sin(angle);
out.raw[4] = Math.cos(angle);
return out;
}
/** Creates a transformation matrix that scales by x, y.
* @param {number} x amount to scale on the x axis.
* @param {number} y amount to scale on the y axis.
* @param {Matrix3x3} out the matrix to store the resulting transformation matrix.
* @return {Matrix3x3} the resulting transformation matrix (out).
*/
static scaleMatrix(x, y, out){
if(!out) out = Matrix3x3.IDENTITY;
out.raw[0] = x;
out.raw[4] = y;
return out;
}
/** Multiplies two 3x3 matrices a and b (a * b). Sets out to be the result. A new Matrix3x3
* is created and returned if out is not defined.
* @param {Matrix3x3} a the matrix to use as multiplicand.
* @param {Matrix3x3} b the matrix to use as multiplier.
* @param {Matrix3x3} out the matrix to store the product.
* @return {Matrix3x3} the resulting product matrix (out).
*/
static multM3(a, b, out){
let a0 = a.raw[0];
let a1 = a.raw[1];
let a2 = a.raw[2];
let a3 = a.raw[3];
let a4 = a.raw[4];
let a5 = a.raw[5];
let a6 = a.raw[6];
let a7 = a.raw[7];
let a8 = a.raw[8];
let b0 = b.raw[0];
let b1 = b.raw[1];
let b2 = b.raw[2];
let b3 = b.raw[3];
let b4 = b.raw[4];
let b5 = b.raw[5];
let b6 = b.raw[6];
let b7 = b.raw[7];
let b8 = b.raw[8];
if(!out) out = Matrix3x3.IDENTITY;
out.raw[0] = a0 * b0 + a3 * b1 + a6 * b2;
out.raw[1] = a1 * b0 + a4 * b1 + a7 * b2;
out.raw[2] = a2 * b0 + a5 * b1 + a8 * b2;
out.raw[3] = a0 * b3 + a3 * b4 + a6 * b5;
out.raw[4] = a1 * b3 + a4 * b4 + a7 * b5;
out.raw[5] = a2 * b3 + a5 * b4 + a8 * b5;
out.raw[6] = a0 * b6 + a3 * b7 + a6 * b8;
out.raw[7] = a1 * b6 + a4 * b7 + a7 * b8;
out.raw[8] = a2 * b6 + a5 * b7 + a8 * b8;
return out;
}
/** Transforms given Vector3 v by given matrix m using the complete 3x3 matrix.
* If out is supplied, result is stored in out, else result is stored in v.
* @param {Matrix3x3} m matrix to transform with.
* @param {Vector3} v Vector3 to be transformed.
* @param {Vector3} out Vector3 object to store result in,
* can be undefiened. Then a v will recieve the result.
* @return {Vector3} the transformed vector.
*/
static multV3(m, v, out){
// transform all, rotation, scale and translation
m = m.raw;
if(!out) out = new Vector3();
out.x = m[0] * v.x + m[3] * v.y + m[6] * v.z;
out.y = m[1] * v.x + m[4] * v.y + m[7] * v.z;
out.z = m[2] * v.x + m[5] * v.y + m[8] * v.z;
return out;
}
/** Transforms given Vector2 v by given matrix m using the complete 3x3 matrix.
* If out is supplied, result is stored in out, else result is stored in v.
* @param {Matrix3x3} m matrix to transform with.
* @param {Vector2} v Vector2 to be transformed.
* @param {Vector2} out Vector2 object to store result in,
* can be undefiened. Then a v will recieve the result.
* @return {Vector2} the transformed vector.
*/
static multV2(m, v, out){
// transform all, rotation, scale and translation
m = m.raw;
if(!out) out = new Vector2();
out.x = m[0] * v.x + m[3] * v.y + m[6] * 1;
out.y = m[1] * v.x + m[4] * v.y + m[7] * 1;
return out;
}
/** Transforms given Vector2 v by given matrix m using ONLY the 2x2 submatrix of given 3x3 matrix (no translation).
* This will only transform the vectors scale and rotation, but not the position.
* @param {Matrix3x3} m matrix to transform with.
* @param {Vector2} v Vector2 to be transformed.
* @param {Vector2} out Vector2 object to store result in,
* can be undefiened. Then a v will recieve the result.
* @return {Vector2} the transformed vector.
*/
static transformV2(m, v, out){
// transform only rotation and scale, but not translation
m = m.raw;
if(!out) out = new Vector2();
out.x = m[0] * v.x + m[3] * v.y;
out.y = m[1] * v.x + m[4] * v.y;
return out;
}
/** Inverts the given matrix m. If out is defined the result is stored in out, else the
* given matrix m will be overwritten with its inverse.
* @param {Matrix3x3} m the matrix to be inverted, if out is not defined, will recieve the result.
* @param {Matrix3x3} out the matrix to store the outcome, if not defined, outcome will be stored in m.
* @return {Matrix3x3} the resulting inverted matrix.
*/
static invert(m, out){
let d = m.determinant();
if(!out) out = m;
m = m.raw;
// function for derterminant of 2x2 matrix
function det2(mat){ return mat[0]*mat[3]-mat[2]*mat[1]; }
// determinants (matrix of minors)
let a00 = [ m[4], m[7], m[5], m[8] ];
let a01 = [ m[1], m[7], m[2], m[8] ];
let a02 = [ m[1], m[4], m[2], m[5] ];
let a10 = [ m[3], m[6], m[5], m[8] ];
let a11 = [ m[0], m[6], m[2], m[8] ];
let a12 = [ m[0], m[3], m[2], m[5] ];
let a20 = [ m[3], m[6], m[4], m[7] ];
let a21 = [ m[0], m[6], m[1], m[7] ];
let a22 = [ m[0], m[3], m[1], m[4] ];
out.raw[0] = det2(a00)/d;
out.raw[1] = -det2(a01)/d;
out.raw[2] = det2(a02)/d;
out.raw[3] = -det2(a10)/d;
out.raw[4] = det2(a11)/d;
out.raw[5] = -det2(a12)/d;
out.raw[6] = det2(a20)/d;
out.raw[7] = -det2(a21)/d;
out.raw[8] = det2(a22)/d;
return out;
}
/** Transposes the given matrix m. If out is defined the result is stored in out, else the
* given matrix m will be overwritten with its transpose.
* @param {Matrix3x3} m the matrix to be transposed, if out is not defined, will recieve the result.
* @param {Matrix3x3} out the matrix to store the outcome, if not defined, outcome will be stored in m.
* @return {Matrix3x3} the resulting transposed matrix.
*/
static transpose(m, out){
if(!out) out = m;
let temp1 = m.raw[1];
let temp6 = m.raw[6];
let temp7 = m.raw[7];
out.raw[1] = m.raw[3];
out.raw[6] = m.raw[2];
out.raw[7] = m.raw[5];
out.raw[3] = temp1;
out.raw[2] = temp6;
out.raw[5] = temp7;
return out;
}
}
//............................................
// Matrix4x4 class
class Matrix4x4{
raw;
static get IDENTITY(){
return new Matrix4x4();
}
constructor(raw){
this.raw = new Array(16);
if(raw === undefined || raw.length !== 16){
this.raw = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];
} else {
for(let i = 0; i < 16; i++){
this.raw[i] = raw[i];
}
}
}
//....................................................................
//Methods
//reset data back to identity.
reset(){
for(var i=0; i < this.raw.length; i++){
this.raw[i] = (i % 5 == 0)? 1 : 0; //only positions 0,5,10,15 need to be 1 else 0.
}
return this;
}
//Bring the matrix back to identity without changing the transform values.
resetRotation(){
for(var i=0; i < 12; i++){
this.raw[i] = 0;
}
//only positions 0,5,10,15 need to be 1 else 0.
this.raw[0] = this.raw[5] = this.raw[10] = this.raw[15] = 1;
return this;
}
clone(){ return new Matrix4x4(this.raw);}
toString(){ return "[" + this.raw.toString() + "]";}
toFloat32(){ return new Float32Array(this.raw);}
//....................................................................
//Transformation Methods
//All methods can be called from an instance of Matrix4x4 and be performed on that instance,
//but simply forward the commands to the static methods with the actual implementation.
applyTranslationV(v){ return Matrix4x4.applyTranslation(v.x,v.y,v.z,this);}
applyTranslation(x,y,z){ return Matrix4x4.applyTranslation(x,y,z,this);}
applyRotationX(rad){ return Matrix4x4.applyRotationX(rad, this);}
applyRotationY(rad){ return Matrix4x4.applyRotationY(rad, this);}
applyRotationZ(rad){ return Matrix4x4.applyRotationZ(rad, this);}
applyScaleV(v){ return Matrix4x4.applyScale(v.x,v.y,v.z,this);}
applyScale(x,y,z){ return Matrix4x4.applyScale(x,y,z,this);}
multM4(m){ return Matrix4x4.multM4(this, m, this);} // multiplies: this*m
multV4(v, out) { return Matrix4x4.multV4(this, v, out);}
multV3(v, out) { return Matrix4x4.multV3(this, v, out);}
transformV3(v, out) { return Matrix4x4.transformV3(this, v, out);}
invert(){Matrix4x4.invert(this, this); return this;}
transpose(){Matrix4x4.transpose(this,this); return this;}
//....................................................................
//Static Data Methods to generate matrices
/** Creates and returns a perspective matrix. If a matrix object is given for out,
* that matrix will be changed to be a perspective matrix.
* @param {number} fovy field of view.
* @param {number} aspect aspect ratio of the screen.
* @param {number} near distance of camera to the near plane.
* @param {number} far distance of camera to the far plane.
* @param {Matrix4x4} out matrix object to be set as this perspective matrix,
* can be undefiened. Then a new Matrix4x4 object will be created.
* @return {Matrix4x4} the created perspective matrix.
*/
static perspective(fov, near, far, aspect, out) {
if(!out) out = new Matrix4x4();
let o_r = out.raw;
let f = 1.0 / Math.tan(fov / 2);
let nf = 1 / (near - far);
o_r[ 0] = f / aspect;
o_r[ 1] = 0;
o_r[ 2] = 0;
o_r[ 3] = 0;
o_r[ 4] = 0;
o_r[ 5] = f;
o_r[ 6] = 0;
o_r[ 7] = 0;
o_r[ 9] = 0;
o_r[ 8] = 0;
o_r[10] = (far + near) * nf;
o_r[11] = -1;
o_r[12] = 0;
o_r[13] = 0;
o_r[14] = (2 * far * near) * nf;
o_r[15] = 0;
return out;
}
/** Creates and returns a orthographic matrix. If a matrix object is given for out,
* that matrix will be changed to be a orthographic matrix.
* @param {number} left distance to left boarder of camera view.
* @param {number} right distance to right boarder of camera view.
* @param {number} bottom distance to bottom boarder of camera view.
* @param {number} top distance to top boarder of camera view.
* @param {number} near distance of camera to the near plane.
* @param {number} far distance of camera to the far plane.
* @param {Matrix4x4} out matrix object to be set as this orthographic matrix,
* can be undefiened. Then a new Matrix4x4 object will be created.
* @return {Matrix4x4} the created orthographic matrix.
*/
static ortho(left, right, bottom, top, near, far, out) {
if(!out) out = new Matrix4x4();
let o_r = out.raw;
let lr = 1 / (left - right);
let bt = 1 / (bottom - top);
let nf = 1 / (near - far);
o_r[ 0] = -2 * lr;
o_r[ 1] = 0;
o_r[ 2] = 0;
o_r[ 3] = 0;
o_r[ 4] = 0;
o_r[ 5] = -2 * bt;
o_r[ 6] = 0;
o_r[ 7] = 0;
o_r[ 8] = 0;
o_r[ 9] = 0;
o_r[10] = 2 * nf;
o_r[11] = 0;
o_r[12] = (left + right) * lr;
o_r[13] = (top + bottom) * bt;
o_r[14] = (far + near) * nf;
o_r[15] = 1;
return out;
};
/** Transposes the matrix. If a matrix object is given for out,
* that matrix will be changed to be the transposed matrix.
* @param {Matrix4x4} m the matrix to transpose.
* @param {Matrix4x4} out matrix object to set as the transpose of matrix m.
* If undefiened, a new matrix is created.
* @return {Matrix4x4} the transposed matrix.
*/
static transpose(m, out) {
if(!out) out = new Matrix4x4();
let o_r = out.raw;
let m_r = m.raw;
//If we are transposing ourselves we can skip a few steps but have to cache some values
if (out === m) {
let m01 = m_r[1], m02 = m_r[2], m03 = m_r[3], m12 = m_r[6], m13 = m_r[7], m23 = m_r[11];
o_r[ 1] = m_r[4];
o_r[ 2] = m_r[8];
o_r[ 3] = m_r[12];
o_r[ 4] = m01;
o_r[ 6] = m_r[ 9];
o_r[ 7] = m_r[13];
o_r[ 8] = m02;
o_r[ 9] = m12;
o_r[11] = m_r[14];
o_r[12] = m03;
o_r[13] = m13;
o_r[14] = m23;
}else{
o_r[ 0] = m_r[ 0];
o_r[ 1] = m_r[ 4];
o_r[ 2] = m_r[ 8];
o_r[ 3] = m_r[12];
o_r[ 4] = m_r[ 1];
o_r[ 5] = m_r[ 5];
o_r[ 6] = m_r[ 9];
o_r[ 7] = m_r[13];
o_r[ 8] = m_r[ 2];
o_r[ 9] = m_r[ 6];
o_r[10] = m_r[10];
o_r[11] = m_r[14];