-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnvqm.js
1959 lines (1777 loc) · 45.8 KB
/
nvqm.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
// (c) Copyright 2017, Sean Connelly (@voidqk), http://syntheti.cc
// MIT License
// Project Home: https://github.com/voidqk/nvqm
var TAU = 6.28318530717958647692528676655900576839433879875021164195;
//
// num (scalars)
//
function num_abs(n){
return n < 0 ? -n : n;
}
var num_acos = Math.acos;
var num_asin = Math.asin;
var num_atan2 = Math.atan2;
var num_atan = Math.atan;
var num_ceil = Math.ceil;
function num_clamp(n, min, max){
return n < min ? min : (n > max ? max : n);
}
var num_cos = Math.cos;
var num_floor = Math.floor;
function num_lerp(a, b, t){
return a + (b - a) * t;
}
var num_log = Math.log;
function num_max(a, b){
return a > b ? a : b;
}
function num_min(a, b){
return a > b ? b : a;
}
function num_mod(a, b){
return a % b;
}
var num_pow = Math.pow;
var num_round = Math.round;
var num_sin = Math.sin;
var num_sqrt = Math.sqrt;
var num_tan = Math.tan;
//
// vec2
//
function vec2_add(a, b){
return [a[0] + b[0], a[1] + b[1]];
}
function vec2_applymat2(a, b){
var ax = a[0], ay = a[1];
return [b[0] * ax + b[2] * ay, b[1] * ax + b[3] * ay];
}
function vec2_applymat3x2(a, b){
var ax = a[0], ay = a[1];
return [b[0] * ax + b[2] * ay + b[4], b[1] * ax + b[3] * ay + b[5]];
}
function vec2_applymat3(a, b){
var ax = a[0], ay = a[1];
return [b[0] * ax + b[3] * ay + b[6], b[1] * ax + b[4] * ay + b[7]];
}
function vec2_applymat4(a, b){
var ax = a[0], ay = a[1];
return [b[0] * ax + b[4] * ay + b[12], b[1] * ax + b[5] * ay + b[13]];
}
function vec2_clamp(a, min, max){
return [num_clamp(a[0], min[0], max[0]), num_clamp(a[1], min[1], max[1])];
}
function vec2_cross(a, b){
return a[0] * b[1] - a[1] * b[0];
}
function vec2_dist(a, b){
return num_sqrt(vec2_len2(vec2_sub(a, b)));
}
function vec2_dist2(a, b){
return vec2_len2(vec2_sub(b, a));
}
function vec2_div(a, b){
return [a[0] / b[0], a[1] / b[1]];
}
function vec2_dot(a, b){
return a[0] * b[0] + a[1] * b[1];
}
function vec2_inverse(a){
return [1 / a[0], 1 / a[1]];
}
function vec2_len(a){
return num_sqrt(vec2_len2(a));
}
function vec2_len2(a){
var ax = a[0], ay = a[1];
return ax * ax + ay * ay;
}
function vec2_lerp(a, b, t){
return [num_lerp(a[0], b[0], t), num_lerp(a[1], b[1], t)];
}
function vec2_max(a, b){
return [num_max(a[0], b[0]), num_max(a[1], b[1])];
}
function vec2_min(a, b){
return [num_min(a[0], b[0]), num_min(a[1], b[1])];
}
function vec2_mul(a, b){
return [a[0] * b[0], a[1] * b[1]];
}
function vec2_neg(a){
return [-a[0], -a[1]];
}
function vec2_normal(a){
var ax = a[0], ay = a[1],
len = ax * ax + ay * ay;
if (len > 0){
len = 1 / num_sqrt(len);
return [ax * len, ay * len];
}
return a;
}
function vec2_scale(a, s){
return [a[0] * s, a[1] * s];
}
function vec2_sub(a, b){
return [a[0] - b[0], a[1] - b[1]];
}
//
// vec3
//
function vec3_add(a, b){
return [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
}
function vec3_angle(a, b){
return vec3_nangle(vec3_normal(a), vec3_normal(b));
}
function vec3_applymat3x2(a, b){
var ax = a[0], ay = a[1], az = a[2];
return [
ax * b[0] + ay * b[2] + az * b[4],
ax * b[1] + ay * b[3] + az * b[5],
az
];
}
function vec3_applymat3(a, b){
var ax = a[0], ay = a[1], az = a[2];
return [
ax * b[0] + ay * b[3] + az * b[6],
ax * b[1] + ay * b[4] + az * b[7],
ax * b[2] + ay * b[5] + az * b[8]
];
}
function vec3_applymat4(a, b){
var ax = a[0], ay = a[1], az = a[2];
var w = b[ 3] * ax + b[ 7] * ay + b[11] * az + b[15];
if (w == 0)
w = 1;
return [
(b[0] * ax + b[4] * ay + b[ 8] * az + b[12]) / w,
(b[1] * ax + b[5] * ay + b[ 9] * az + b[13]) / w,
(b[2] * ax + b[6] * ay + b[10] * az + b[14]) / w
];
}
function vec3_applyquat(a, b){
var
ax = a[0], ay = a[1], az = a[2],
bx = b[0], by = b[1], bz = b[2], bw = b[3];
var
ix = bw * ax + by * az - bz * ay,
iy = bw * ay + bz * ax - bx * az,
iz = bw * az + bx * ay - by * ax,
iw = -bx * ax - by * ay - bz * az;
return [
ix * bw + iw * -bx + iy * -bz - iz * -by,
iy * bw + iw * -by + iz * -bx - ix * -bz,
iz * bw + iw * -bz + ix * -by - iy * -bx
];
}
function vec3_clamp(a, min, max){
return [
num_clamp(a[0], min[0], max[0]),
num_clamp(a[1], min[1], max[1]),
num_clamp(a[2], min[2], max[2])
];
}
function vec3_cross(a, b){
var
ax = a[0], ay = a[1], az = a[2],
bx = b[0], by = b[1], bz = b[2];
return [ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx];
}
function vec3_dist(a, b){
return num_sqrt(vec3_len2(vec3_sub(a, b)));
}
function vec3_dist2(a, b){
return vec3_len2(vec3_sub(b, a));
}
function vec3_div(a, b){
return [a[0] / b[0], a[1] / b[1], a[2] / b[2]];
}
function vec3_dot(a, b){
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
function vec3_inverse(a){
return [1 / a[0], 1 / a[1], 1 / a[2]];
}
function vec3_len(a){
return num_sqrt(vec3_len2(a));
}
function vec3_len2(a){
var ax = a[0], ay = a[1], az = a[2];
return ax * ax + ay * ay + az * az;
}
function vec3_lerp(a, b, t){
return [
num_lerp(a[0], b[0], t),
num_lerp(a[1], b[1], t),
num_lerp(a[2], b[2], t)
];
}
function vec3_max(a, b){
return [num_max(a[0], b[0]), num_max(a[1], b[1]), num_max(a[2], b[2])];
}
function vec3_min(a, b){
return [num_min(a[0], b[0]), num_min(a[1], b[1]), num_min(a[2], b[2])];
}
function vec3_mul(a, b){
return [a[0] * b[0], a[1] * b[1], a[2] * b[2]];
}
function vec3_nangle(a, b){ // a and b are normalized
var c = vec3_dot(a, b);
if (c < -1 || c > 1)
return 0;
return num_acos(c);
}
function vec3_neg(a){
return [-a[0], -a[1], -a[2]];
}
function vec3_normal(a){
var ax = a[0], ay = a[1], az = a[2];
var len = ax * ax + ay * ay + az * az;
if (len > 0){
len = 1 / num_sqrt(len);
return [ax * len, ay * len, az * len];
}
return a;
}
function vec3_orthogonal(a, b){
return vec3_normal(vec3_cross(a, b));
}
function vec3_scale(a, s){
return [a[0] * s, a[1] * s, a[2] * s];
}
function vec3_sub(a, b){
return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
}
//
// vec4
//
function vec4_add(a, b){
return [a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]];
}
function vec4_applymat4(a, b){
var ax = a[0], ay = a[1], az = a[2], aw = a[3];
return [
b[0] * ax + b[4] * ay + b[ 8] * az + b[12] * aw,
b[1] * ax + b[5] * ay + b[ 9] * az + b[13] * aw,
b[2] * ax + b[6] * ay + b[10] * az + b[14] * aw,
b[3] * ax + b[7] * ay + b[11] * az + b[15] * aw
];
}
function vec4_applyquat(a, b){
var
ax = a[0], ay = a[1], az = a[2], aw = a[3],
bx = b[0], by = b[1], bz = b[2], bw = b[3];
var
ix = bw * ax + by * az - bz * ay,
iy = bw * ay + bz * ax - bx * az,
iz = bw * az + bx * ay - by * ax,
iw = -bx * ax - by * ay - bz * az;
return [
ix * bw + iw * -bx + iy * -bz - iz * -by,
iy * bw + iw * -by + iz * -bx - ix * -bz,
iz * bw + iw * -bz + ix * -by - iy * -bx,
aw
];
}
function vec4_clamp(a, min, max){
return [
num_clamp(a[0], min[0], max[0]),
num_clamp(a[1], min[1], max[1]),
num_clamp(a[2], min[2], max[2]),
num_clamp(a[3], min[3], max[3])
];
}
function vec4_dist(a, b){
return num_sqrt(vec4_len2(vec4_sub(a, b)));
}
function vec4_dist2(a, b){
return vec4_len2(vec4_sub(b, a));
}
function vec4_div(a, b){
return [a[0] / b[0], a[1] / b[1], a[2] / b[2], a[3] / b[3]];
}
function vec4_dot(a, b){
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
}
function vec4_inverse(a){
return [1 / a[0], 1 / a[1], 1 / a[2], 1 / a[3]];
}
function vec4_len(a){
return num_sqrt(vec4_len2(a));
}
function vec4_len2(a){
var ax = a[0], ay = a[1], az = a[2], aw = a[3];
return ax * ax + ay * ay + az * az + aw * aw;
}
function vec4_lerp(a, b, t){
return [
num_lerp(a[0], b[0], t),
num_lerp(a[1], b[1], t),
num_lerp(a[2], b[2], t),
num_lerp(a[3], b[3], t)
];
}
function vec4_max(a, b){
return [
num_max(a[0], b[0]),
num_max(a[1], b[1]),
num_max(a[2], b[2]),
num_max(a[3], b[3])
];
}
function vec4_min(a, b){
return [
num_min(a[0], b[0]),
num_min(a[1], b[1]),
num_min(a[2], b[2]),
num_min(a[3], b[3])
];
}
function vec4_mul(a, b){
return [a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]];
}
function vec4_neg(a){
return [-a[0], -a[1], -a[2], -a[3]];
}
function vec4_normal(a){
var ax = a[0], ay = a[1], az = a[2], aw = a[3];
var len = ax * ax + ay * ay + az * az + aw * aw;
if (len > 0){
len = 1 / num_sqrt(len);
return [ax * len, ay * len, az * len, aw * len];
}
return a;
}
function vec4_scale(a, s){
return [a[0] * s, a[1] * s, a[2] * s, a[3] * s];
}
function vec4_sub(a, b){
return [a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]];
}
//
// quat
//
function quat_axisang(axis, ang){
return quat_naxisang(vec3_normal(axis), ang);
}
function quat_between(from, to){
return quat_nbetween(vec3_normal(from), vec3_normal(to));
}
function quat_dot(a, b){
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
}
function quat_euler_xyz(rot){
var a0 = rot[0] * 0.5;
var a1 = rot[1] * 0.5;
var a2 = rot[2] * 0.5;
var cx = num_cos(a0);
var cy = num_cos(a1);
var cz = num_cos(a2);
var sx = num_sin(a0);
var sy = num_sin(a1);
var sz = num_sin(a2);
return [
sx * cy * cz + cx * sy * sz,
cx * sy * cz - sx * cy * sz,
cx * cy * sz + sx * sy * cz,
cx * cy * cz - sx * sy * sz
];
}
function quat_euler_xzy(rot){
var a0 = rot[0] * 0.5;
var a1 = rot[1] * 0.5;
var a2 = rot[2] * 0.5;
var cx = num_cos(a0);
var cy = num_cos(a1);
var cz = num_cos(a2);
var sx = num_sin(a0);
var sy = num_sin(a1);
var sz = num_sin(a2);
return [
sx * cy * cz - cx * sy * sz,
cx * sy * cz - sx * cy * sz,
cx * cy * sz + sx * sy * cz,
cx * cy * cz + sx * sy * sz
];
}
function quat_euler_yxz(rot){
var a0 = rot[0] * 0.5;
var a1 = rot[1] * 0.5;
var a2 = rot[2] * 0.5;
var cx = num_cos(a0);
var cy = num_cos(a1);
var cz = num_cos(a2);
var sx = num_sin(a0);
var sy = num_sin(a1);
var sz = num_sin(a2);
return [
sx * cy * cz + cx * sy * sz,
cx * sy * cz - sx * cy * sz,
cx * cy * sz - sx * sy * cz,
cx * cy * cz + sx * sy * sz
];
}
function quat_euler_yzx(rot){
var a0 = rot[0] * 0.5;
var a1 = rot[1] * 0.5;
var a2 = rot[2] * 0.5;
var cx = num_cos(a0);
var cy = num_cos(a1);
var cz = num_cos(a2);
var sx = num_sin(a0);
var sy = num_sin(a1);
var sz = num_sin(a2);
return [
sx * cy * cz + cx * sy * sz,
cx * sy * cz + sx * cy * sz,
cx * cy * sz - sx * sy * cz,
cx * cy * cz - sx * sy * sz
];
}
function quat_euler_zxy(rot){
var a0 = rot[0] * 0.5;
var a1 = rot[1] * 0.5;
var a2 = rot[2] * 0.5;
var cx = num_cos(a0);
var cy = num_cos(a1);
var cz = num_cos(a2);
var sx = num_sin(a0);
var sy = num_sin(a1);
var sz = num_sin(a2);
return [
sx * cy * cz - cx * sy * sz,
cx * sy * cz + sx * cy * sz,
cx * cy * sz + sx * sy * cz,
cx * cy * cz - sx * sy * sz
];
}
function quat_euler_zyx(rot){
var a0 = rot[0] * 0.5;
var a1 = rot[1] * 0.5;
var a2 = rot[2] * 0.5;
var cx = num_cos(a0);
var cy = num_cos(a1);
var cz = num_cos(a2);
var sx = num_sin(a0);
var sy = num_sin(a1);
var sz = num_sin(a2);
return [
sx * cy * cz - cx * sy * sz,
cx * sy * cz + sx * cy * sz,
cx * cy * sz - sx * sy * cz,
cx * cy * cz + sx * sy * sz
];
}
function quat_identity(){
return [0, 0, 0, 1];
}
function quat_invert(a){
var ax = a[0], ay = a[1], az = a[2], aw = a[3];
var dot = ax * ax + ay * ay + az * az + aw * aw;
var invDot = 0;
if (dot != 0)
invDot = 1 / dot;
return [
-ax * invDot,
-ay * invDot,
-az * invDot,
aw * invDot
];
}
function quat_lerp(a, b, t){
return [
num_lerp(a[0], b[0], t),
num_lerp(a[1], b[1], t),
num_lerp(a[2], b[2], t),
num_lerp(a[3], b[3], t)
];
}
function quat_mul(a, b){
var
ax = a[0], ay = a[1], az = a[2], aw = a[3],
bx = b[0], by = b[1], bz = b[2], bw = b[3];
return [
ax * bw + aw * bx + ay * bz - az * by,
ay * bw + aw * by + az * bx - ax * bz,
az * bw + aw * bz + ax * by - ay * bx,
aw * bw - ax * bx - ay * by - az * bz
];
}
function quat_naxisang(axis, ang){ // axis is normalized
ang *= 0.5;
var s = num_sin(ang);
return [axis[0] * s, axis[1] * s, axis[2] * s, num_cos(ang)];
}
function quat_nbetween(from, to){ // from/to are normalized
var r = vec3_dot(from, to) + 1;
var cross;
if (r < 0.000001){
if (num_abs(from[0]) > num_abs(from[2]))
cross = [-from[1], from[0], 0];
else
cross = [0, -from[2], from[1]];
}
else
cross = vec3_cross(from, to);
return quat_normal([cross[0], cross[1], cross[2], r]);
}
function quat_neg(a){
return [-a[0], -a[1], -a[2], -a[3]];
}
function quat_nlerp(a, b, t){
return quat_normal(quat_lerp(a, b, t));
}
function quat_normal(a){
var ax = a[0], ay = a[1], az = a[2], aw = a[3];
var len = ax * ax + ay * ay + az * az + aw * aw;
if (len > 0){
len = 1 / num_sqrt(len);
return [ax * len, ay * len, az * len, aw * len];
}
return a;
}
function quat_slerp(a, b, t){
var ax = a[0], ay = a[1], az = a[2], aw = a[3];
var bx = b[0], by = b[1], bz = b[2], bw = b[3];
var omega, cosom, sinom, scale0, scale1;
cosom = ax * bx + ay * by + az * bz + aw * bw;
if (cosom < 0){
cosom = -cosom;
bx = -bx ;
by = -by ;
bz = -bz ;
bw = -bw ;
}
if ((1 - cosom) > 0.000001){
omega = num_acos(cosom);
sinom = num_sin(omega);
scale0 = num_sin((1 - t) * omega) / sinom;
scale1 = num_sin(t * omega) / sinom;
}
else {
scale0 = 1 - t;
scale1 = t;
}
return [
scale0 * ax + scale1 * bx,
scale0 * ay + scale1 * by,
scale0 * az + scale1 * bz,
scale0 * aw + scale1 * bw
];
}
//
// mat2
//
function mat2_add(a, b){
return [a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]];
}
function mat2_adjoint(a){
return [a[3], -a[1], -a[2], a[0]];
}
function mat2_compmul(a, b){
return [a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]];
}
function mat2_det(a){
return a[0] * a[3] - a[2] * a[1];
}
function mat2_identity(){
return [1, 0, 0, 1];
}
function mat2_invert(a){
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3];
var det = a0 * a3 - a2 * a1;
if (det == 0)
return [0, 0, 0, 0];
det = 1 / det;
return [a3 * det, -a1 * det, -a2 * det, a0 * det];
}
function mat2_mul(a, b){
var
a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
return [a0 * b0 + a2 * b1, a1 * b0 + a3 * b1, a0 * b2 + a2 * b3, a1 * b2 + a3 * b3];
}
function mat2_rotate(a, ang){
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], s = num_sin(ang), c = num_cos(ang);
return [a0 * c + a2 * s, a1 * c + a3 * s, a0 * -s + a2 * c, a1 * -s + a3 * c];
}
function mat2_rotation(ang){
var s = num_sin(ang), c = num_cos(ang);
return [c, s, -s, c];
}
function mat2_scale(a, b){
var
a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
b0 = b[0], b1 = b[1];
return [a0 * b0, a1 * b0, a2 * b1, a3 * b1];
}
function mat2_scaling(a){
return [a[0], 0, 0, a[1]];
}
function mat2_sub(a, b){
return [a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]];
}
function mat2_transpose(a){
return [a[0], a[2], a[1], a[3]];
}
//
// mat3x2
//
function mat3x2_add(a, b){
return [
a[0] + b[0], a[1] + b[1],
a[2] + b[2], a[3] + b[3],
a[4] + b[4], a[5] + b[5]
];
}
function mat3x2_compmul(a, b){
return [
a[0] * b[0], a[1] * b[1],
a[2] * b[2], a[3] * b[3],
a[4] * b[4], a[5] * b[5]
];
}
function mat3x2_det(a){
return a[0] * a[3] - a[2] * a[1];
}
function mat3x2_identity(){
return [1, 0, 0, 1, 0, 0];
}
function mat3x2_invert(a){
var
a00 = a[0], a01 = a[1],
a10 = a[2], a11 = a[3],
a20 = a[4], a21 = a[5];
var det = a00 * a11 - a01 * a10;
if (det == 0)
return [0, 0, 0, 0, 0, 0];
det = 1 / det;
return [
a11 * det, -a01 * det,
-a10 * det, a00 * det,
( a21 * a10 - a11 * a20) * det,
(-a21 * a00 + a01 * a20) * det
];
}
function mat3x2_mul(a, b){
var
a00 = a[0], a01 = a[1],
a10 = a[2], a11 = a[3],
a20 = a[4], a21 = a[5],
b00 = b[0], b01 = b[1],
b10 = b[2], b11 = b[3],
b20 = b[4], b21 = b[5];
return [
b00 * a00 + b01 * a10 , b00 * a01 + b01 * a11,
b10 * a00 + b11 * a10 , b10 * a01 + b11 * a11,
b20 * a00 + b21 * a10 + a20, b20 * a01 + b21 * a11 + a21
];
}
function mat3x2_rotate(a, ang){
var
a00 = a[0], a01 = a[1],
a10 = a[2], a11 = a[3],
s = num_sin(ang), c = num_cos(ang);
return [
c * a00 + s * a10, c * a01 + s * a11,
c * a10 - s * a00, c * a11 - s * a01,
a[4], a[5]
];
}
function mat3x2_rotation(ang){
var s = num_sin(ang), c = num_cos(ang);
return [c, s, -s, c, 0, 0];
}
function mat3x2_scale(a, b){
var bx = b[0], by = b[1];
return [
bx * a[0], bx * a[1],
by * a[2], by * a[3],
a[4], a[5],
];
}
function mat3x2_scaling(a){
return [a[0], 0, 0, a[1], 0, 0];
}
function mat3x2_sub(a, b){
return [
a[0] - b[0], a[1] - b[1],
a[2] - b[2], a[3] - b[3],
a[4] - b[4], a[5] - b[5]
];
}
function mat3x2_translate(a, b){
var
a00 = a[0], a01 = a[1],
a10 = a[2], a11 = a[3],
bx = b[0], by = b[1];
return [
a00, a01,
a10, a11,
bx * a00 + by * a10 + a[4],
bx * a01 + by * a11 + a[5]
];
}
function mat3x2_translation(a){
return [1, 0, 0, 1, a[0], a[1]];
}
//
// mat3
//
function mat3_add(out, a, b){
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
out[4] = a[4] + b[4];
out[5] = a[5] + b[5];
out[6] = a[6] + b[6];
out[7] = a[7] + b[7];
out[8] = a[8] + b[8];
return out;
}
function mat3_adjoint(out, a){
var
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];
out[0] = a11 * a22 - a12 * a21;
out[1] = a02 * a21 - a01 * a22;
out[2] = a01 * a12 - a02 * a11;
out[3] = a12 * a20 - a10 * a22;
out[4] = a00 * a22 - a02 * a20;
out[5] = a02 * a10 - a00 * a12;
out[6] = a10 * a21 - a11 * a20;
out[7] = a01 * a20 - a00 * a21;
out[8] = a00 * a11 - a01 * a10;
return out;
}
function mat3_compmul(out, a, b){
out[0] = a[0] * b[0];
out[1] = a[1] * b[1];
out[2] = a[2] * b[2];
out[3] = a[3] * b[3];
out[4] = a[4] * b[4];
out[5] = a[5] * b[5];
out[6] = a[6] * b[6];
out[7] = a[7] * b[7];
out[8] = a[8] * b[8];
return out;
}
function mat3_copy(out, a){
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 out;
}
function mat3_det(a){
var
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];
return (
a00 * ( a22 * a11 - a12 * a21) +
a01 * (-a22 * a10 + a12 * a20) +
a02 * ( a21 * a10 - a11 * a20)
);
}
function mat3_identity(out){
if (typeof out === 'undefined')
out = [];
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 out;
}
function mat3_invert(out, a){
var
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],
b01 = a22 * a11 - a12 * a21,
b11 = -a22 * a10 + a12 * a20,
b21 = a21 * a10 - a11 * a20;
var det = a00 * b01 + a01 * b11 + a02 * b21;
if (det == 0)
return null;
det = 1 / det;
out[0] = b01 * det;
out[1] = (-a22 * a01 + a02 * a21) * det;
out[2] = ( a12 * a01 - a02 * a11) * det;
out[3] = b11 * det;
out[4] = ( a22 * a00 - a02 * a20) * det;
out[5] = (-a12 * a00 + a02 * a10) * det;
out[6] = b21 * det;
out[7] = (-a21 * a00 + a01 * a20) * det;
out[8] = ( a11 * a00 - a01 * a10) * det;
return out;
}
function mat3_mul(out, a, b){
var
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],
b00 = b[0], b01 = b[1], b02 = b[2],
b10 = b[3], b11 = b[4], b12 = b[5],
b20 = b[6], b21 = b[7], b22 = b[8];
out[0] = b00 * a00 + b01 * a10 + b02 * a20;
out[1] = b00 * a01 + b01 * a11 + b02 * a21;
out[2] = b00 * a02 + b01 * a12 + b02 * a22;
out[3] = b10 * a00 + b11 * a10 + b12 * a20;
out[4] = b10 * a01 + b11 * a11 + b12 * a21;
out[5] = b10 * a02 + b11 * a12 + b12 * a22;
out[6] = b20 * a00 + b21 * a10 + b22 * a20;
out[7] = b20 * a01 + b21 * a11 + b22 * a21;
out[8] = b20 * a02 + b21 * a12 + b22 * a22;
return out;
}
function mat3_quat(out, a){
var ax = a[0], ay = a[1], az = a[2], aw = a[3],
ax2 = ax + ax,
ay2 = ay + ay,
az2 = az + az,
axx = ax * ax2,
ayx = ay * ax2,
ayy = ay * ay2,
azx = az * ax2,
azy = az * ay2,
azz = az * az2,
awx = aw * ax2,
awy = aw * ay2,
awz = aw * az2;
out[0] = 1 - ayy - azz;
out[1] = ayx + awz;
out[2] = azx - awy;
out[3] = ayx - awz;
out[4] = 1 - axx - azz;
out[5] = azy + awx;
out[6] = azx + awy;
out[7] = azy - awx;
out[8] = 1 - axx - ayy;
return out;
}
function mat3_rotate(out, a, ang){
var
a00 = a[0], a01 = a[1], a02 = a[2],
a10 = a[3], a11 = a[4], a12 = a[5],