/
math.n
1911 lines (1579 loc) · 51.9 KB
/
math.n
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
// Copyright (C) 2014 OneJS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// OneJS math types
define out(x) console.log(x)
define out(x,y) console.log(x, y)
define out(x,y,z) console.log(x, y, z)
define out(x,y,z,w) console.log(x, y, z, w)
define sin(vec2 v) vec2.sin(v)
define sin(vec3 v) vec3.sin(v)
define sin(vec4 v) vec4.sin(v)
define sin(v) Math.sin(v)
define cos(vec2 v) vec2.cos(v)
define cos(vec3 v) vec3.cos(v)
define cos(vec4 v) vec4.cos(v)
define cos(v) Math.cos(v)
define tan(vec2 v) vec2.tan(v)
define tan(vec3 v) vec3.tan(v)
define tan(vec4 v) vec4.tan(v)
define tan(v) Math.tan(v)
define asin(vec2 v) vec2.asin(v)
define asin(vec3 v) vec3.asin(v)
define asin(vec4 v) vec4.asin(v)
define asin(v) Math.asin(v)
define acos(vec2 v) vec2.acos(v)
define acos(vec3 v) vec3.acos(v)
define acos(vec4 v) vec4.acos(v)
define acos(v) Math.acos(v)
define atan(vec2 v) vec2.atan(v)
define atan(vec3 v) vec3.atan(v)
define atan(vec4 v) vec4.atan(v)
define atan(v) Math.atan(v)
define atan(vec2 x, vec2 y) vec2.atan(x, y)
define atan(vec3 x, vec3 y) vec3.atan(x, y)
define atan(vec4 x, vec4 y) vec4.atan(x, y)
define atan(x, y) Math.atan2(x, y)
define pow(vec2 v, vec2 w) vec2.pow(v, w)
define pow(vec3 v, vec3 w) vec3.pow(v, w)
define pow(vec4 v, vec3 w) vec4.pow(v, w)
define pow(v, w) Math.pow(v, w)
define exp(vec2 v) vec2.exp(vec2 v)
define exp(vec3 v) vec3.exp(vec3 v)
define exp(vec4 v) vec4.exp(vec4 v)
define exp(v) Math.exp(v)
define log(vec2 v) vec2.log(v)
define log(vec3 v) vec3.log(v)
define log(vec4 v) vec4.log(v)
define log(v) Math.log(v)
define exp2(vec2 v) vec2.exp2(v)
define exp2(vec3 v) vec3.exp2(v)
define exp2(vec4 v) vec4.exp2(v)
define exp2(v) Math.pow(2, v)
define log2(vec2 v) vec2.log2(v)
define log2(vec3 v) vec3.log2(v)
define log2(vec4 v) vec4.log2(v)
define log2(v) Math.log2(v)
define sqrt(vec2 v) vec2.sqrt(v)
define sqrt(vec3 v) vec3.sqrt(v)
define sqrt(vec4 v) vec4.sqrt(v)
define sqrt(v) Math.sqrt(v)
define inversesqrt(vec2 v) vec2.inversesqrt(v)
define inversesqrt(vec3 v) vec3.inversesqrt(v)
define inversesqrt(vec4 v) vec4.inversesqrt(v)
define inversesqrt(v) 1/Math.sqrt(v)
define abs(vec2 v) vec2.abs(v)
define abs(vec3 v) vec3.abs(v)
define abs(vec4 v) vec4.abs(v)
define abs(v) Math.abs(v)
define sign(vec2 v) vec2.sign(v)
define sign(vec3 v) vec3.sign(v)
define sign(vec4 v) vec4.sign(v)
define sign(v) Math._sign(v)
define floor(vec2 v) vec2.floor(v)
define floor(vec3 v) vec3.floor(v)
define floor(vec4 v) vec4.floor(v)
define floor(v) Math.floor(v)
define ceil(vec2 v) vec2.ceil(v)
define ceil(vec3 v) vec3.ceil(v)
define ceil(vec4 v) vec4.ceil(v)
define ceil(v) Math.ceil(v)
define fract(vec2 v) vec2.fract(v)
define fract(vec3 v) vec3.fract(v)
define fract(vec4 v) vec4.fract(v)
define fract(v) Math._fract(v)
define mod(vec2 a, vec2 b) vec2.mod2(a, b)
define mod(vec3 a, vec3 b) vec3.mod2(a, b)
define mod(vec4 a, vec4 b) vec4.mod2(a, b)
define mod(vec2 a, float b) vec2.mod(a, b)
define mod(vec3 a, float b) vec3.mod(a, b)
define mod(vec4 a, float b) vec4.mod(a, b)
define mod(a, b) Math._mod(a, b)
define min(vec2 a, vec2 b) vec2.min2(a, b)
define min(vec3 a, vec3 b) vec3.min2(a, b)
define min(vec4 a, vec4 b) vec4.min2(a, b)
define min(vec2 a, float b) vec2.min(a, b)
define min(vec3 a, float b) vec3.min(a, b)
define min(vec4 a, float b) vec4.min(a, b)
define min(a, b) Math.min(a, b)
define max(vec2 a, vec2 b) vec2.max2(a, b)
define max(vec3 a, vec3 b) vec3.max2(a, b)
define max(vec4 a, vec4 b) vec4.max2(a, b)
define max(vec2 a, float b) vec2.max(a, b)
define max(vec3 a, float b) vec3.max(a, b)
define max(vec4 a, float b) vec4.max(a, b)
define max(a, b) Math.max(a, b)
define clamp(vec2 x, vec2 mi, vec2 ma) vec2.clamp2(x, mi, ma)
define clamp(vec3 x, vec3 mi, vec3 ma) vec3.clamp2(x, mi, ma)
define clamp(vec4 x, vec4 mi, vec4 ma) vec4.clamp2(x, mi, ma)
define clamp(vec2 x, float mi, float ma) vec2.clamp(x, mi, ma)
define clamp(vec3 x, float mi, float ma) vec3.clamp(x, mi, ma)
define clamp(vec4 x, float mi, float ma) vec4.clamp(x, mi, ma)
define clamp(x, mi, ma) Math._clamp(x, mi, ma)
define mix(vec2 a, vec2 b, vec2 f) vec2.mix2(a, b, f)
define mix(vec3 a, vec3 b, vec3 f) vec3.mix2(a, b, f)
define mix(vec4 a, vec4 b, vec4 f) vec4.mix2(a, b, f)
define mix(vec2 a, vec2 b, f) vec2.mix(a, b, f)
define mix(vec3 a, vec3 b, f) vec3.mix(a, b, f)
define mix(vec4 a, vec4 b, f) vec4.mix(a, b, f)
define mix(a, b, f) Math._mix(a, b, f)
define step(vec2 e, vec2 v) vec2.step2(e, v)
define step(vec3 e, vec3 v) vec3.step2(e, v)
define step(vec4 e, vec4 v) vec4.step2(e, v)
define step(float e, vec2 v) vec2.step(e, v)
define step(float e, vec3 v) vec3.step(e, v)
define step(float e, vec4 v) vec4.step(e, v)
define step(e, v) Math._step(e, v)
define smoothstep(vec2 e0, vec2 e1, vec2 v) vec2.smoothstep2(e0, e1, v)
define smoothstep(vec3 e0, vec2 e1, vec3 v) vec3.smoothstep2(e0, e1, v)
define smoothstep(vec4 e0, vec2 e1, vec4 v) vec4.smoothstep2(e0, e1, v)
define smoothstep(float e0, float e1, vec2 v) vec2.smoothstep(e0, e1, v)
define smoothstep(float e0, float e1, vec3 v) vec3.smoothstep(e0, e1, v)
define smoothstep(float e0, float e1, vec4 v) vec4.smoothstep(e0, e1, v)
define smoothstep(e0, e1, v) Math._smoothstep(e0, e1, v)
define length(v) v.len()
// TODO fix this!
//define distance(T a, T b) a.distance(b)
define distance(a, b) float.distance(a, b)
define dot(a, b) float.dot(a, b)
define cross(vec3 a, vec3 b) vec3.cross(a, b)
define normalize(T v) T.normalize(v)
define faceforward(T n, T i, T r) T.faceforward(n, i, r)
define reflect(T i, T n) T.reflect(i, n)
define refract(T i, T n, float a) T.refract(i, n, a)
define outerProduct(vec2 c, vec2 r) mat2.outerProduct(c, r)
define outerProduct(vec3 c, vec3 r) mat3.outerProduct(c, r)
define outerProduct(vec4 c, vec4 r) mat4.outerProduct(c, r)
define outerProduct(vec3 c, vec2 r) mat2x3.outerProduct(c, r)
define outerProduct(vec2 c, vec3 r) mat3x2.outerProduct(c, r)
define outerProduct(vec4 c, vec2 r) mat2x4.outerProduct(c, r)
define outerProduct(vec2 c, vec4 r) mat4x2.outerProduct(c, r)
define outerProduct(vec4 c, vec3 r) mat3x4.outerProduct(c, r)
define outerProduct(vec3 c, vec4 r) mat4x3.outerProduct(c, r)
define transpose(mat2 m) mat2.transpose(m)
define transpose(mat3 m) mat3.transpose(m)
define transpose(mat4 m) mat4.transpose(m)
define transpose(mat2x3 m) mat3x2.transpose(m)
define transpose(mat3x2 m) mat2x3.transpose(m)
define transpose(mat2x4 m) mat4x2.transpose(m)
define transpose(mat4x2 m) mat2x4.transpose(m)
define transpose(mat3x4 m) mat4x3.transpose(m)
define transpose(mat4x3 m) mat3x4.transpose(m)
define matrixCompMult(T a, T b) T.matrixCompMult(a, b)
define lessThan(a, b) a.lessThan(b)
define lessThanEqual(a, b) a.lessThanEqual(b)
define greaterThan(a, b) a.greaterThan(b)
define greaterThanEqual(a, b) a.greaterThanEqual(b)
define equal(a, b) a.equal(b)
define notEqual(a, b) a.notEqual(b)
define any(v) a.any()
define all(v) v.all()
define not(v) v.not()
define rad 1
define deg 0.017453292519943295
define PI 3.141592653589793
define E 2.718281828459045
define LN2 0.6931471805599453
define LN10 2.302585092994046
define LOG2E 1.4426950408889634
define LOG10E 0.4342944819032518
define SQRT_1_2 0.70710678118654757
define SQRT2 1.4142135623730951
define mix(T a, T b, float v) T.mix(a, b, v)
define radians(vec2 v) vec2.radians(x)
define radians(vec3 v) vec3.radians(x)
define radians(vec4 v) vec4.radians(x)
define radians(d) d/deg
define degrees(vec2 v) vec2.degrees(x)
define degrees(vec3 v) vec3.degrees(x)
define degrees(vec4 v) vec4.degrees(x)
define degrees(d) d*deg
define dFdy(vec2 v) vec2.dFdy(v)
define dFdy(vec3 v) vec3.dFdy(v)
define dFdy(vec4 v) vec4.dFdy(v)
define dFdy(v) d
define dFdx(vec2 v) vec2.dFdx(v)
define dFdx(vec3 v) vec3.dFdx(v)
define dFdx(vec4 v) vec4.dFdx(v)
define dFdx(v) v
define mix3(a, b, c, d, f){
if(f<.5) return mix(a, b, f*2.)
return mix(c, d, (f-.5)*2.)
}
define mix4(a, b, c, d, f){
if(f<1./3.) return mix(a, b, f*3.)
if(f<2./3.) return mix(b, c, (f-(1./3.))*3.)
return mix(c, d, (f-2./3.)*3.)
}
define mix5(a, b, c, d, e, f){
if(f<.25) return mix(a, b, f*4.)
if(f<.5) return mix(b, c, (f-.25)*4.)
if(f<.75) return mix(c, d, (f-.5)*4.)
return mix(d, e, (f-.75)*4.)
}
define easeLinear(t){
return t
}
define easeInQuad(t){
return t*t
}
define easeOutQuad(t){
return -t*(t-2.)
}
define easeInOutQuad(t){
return (t/=0.5) < 1. ? 0.5*t*t : -0.5 * ((--t)*(t-2.) - 1.)
}
define easeInCubic(t){
return t*t*t
}
define easeOutCubic(t){
return ((t=t-1)*t*t + 1)
}
define easeInOutCubic(t){
return (t/=0.5) < 1. ? 0.5*t*t*t : 1. /2.*((t-=2.)*t*t + 2.)
}
define easeInQuart(t){
return t*t*t*t
}
define easeOutQuart(t){
return -((t=t-1.)*t*t*t - 1.)
}
define easeInOutQuart(t){
return (t/=0.5) < 1. ? 0.5*t*t*t*t : -0.5 * ((t-=2.)*t*t*t - 2.)
}
define easeInQuint(t){
return t*t*t*t*t
}
define easeOutQuint(t){
return ((t=t-1.)*t*t*t*t + 1.)
}
define easeInOutQuint(t){
return (t/=0.5) < 1. ? 0.5*t*t*t*t*t : 0.5*((t-=2.)*t*t*t*t + 2.)
}
define easeInSine(t){
return -cos(t * (PI/2.)) + 1.
}
define easeOutSine(t){
return sin(t * (PI/2.))
}
define easeInOutSine(t){
return -0.5 * (cos(PI*t) - 1.)
}
define easeInExpo(t){
return (t==0.)? 0.: pow(2., 10. * (t - 1.))
}
define easeOutExp(t){
return (t==1.)? 1.: (-pow(2., -10. * t) + 1.)
}
define easeInCirc(t){
return - (sqrt(1. - t*t) - 1.)
}
define easeOutCirc(t){
return sqrt(1. - (t=t-1.)*t)
}
define easeInOutCirc(t){
return (t/=0.5) < 1.? -0.5 * (sqrt(1. - t*t) - 1.): 0.5 * (sqrt(1. - (t-=2.)*t) + 1.)
}
define easeInOutExpo(t){
if (t==0.) return 0.
if (t==1.) return 1.
if ((t/=0.5) < 1.) return 0.5 * Math.pow(2., 10. * (t - 1.))
return 0.5 * (-Math.pow(2., -10. * --t) + 2.)
}
define easeInElastic(t){
var s=1.70158, p=0., a=1.;
if (t==0.) return 0.
if (t==1.) return 1.
if (!p) p=0.3
if (a < 1.) { a=1.; var s=p/4. }
else var s = p/(2.*PI) * asin(1./a)
return -(a*pow(2.,10.*(t-=1.)) * sin( (t*1.-s)*(2.*PI)/p ))
}
define easeOutElastic(t){
var s=1.70158, p=0., a=1.
if (t==0.) return 0.
if (t==1.) return 1.
if (!p) p=1.*0.3
if (a < 1.) { a=1.; var s=p/4.; }
else var s = p/(2.*PI) * asin (1./a)
return a*pow(2.,-10.*t) * sin( (t*1.-s)*(2.*PI)/p ) + 1.
}
define easeInOutElastic(t){
var s=1.70158, p=0., a=1.
if (t==0.) return 0.
if ((t/=0.5)==2.) return 1.
if (!p) p=(0.3*1.5)
if (a < 1.) { a=1.; var s=p/4.; }
else var s = p/(2.*PI) * asin (1./a)
if (t < 1.) return -.5*(a*pow(2.,10.*(t-=1.)) * sin( (t*1.-s)*(2.*PI)/p ))
return a*pow(2.,-10.*(t-=1.)) * sin( (t*1.-s)*(2.*PI)/p )*0.5 + 1.
}
define easeInBack(t){
var s = 1.70158
return (t/=1.)*t*((s+1.)*t - s)
}
define easeInBack(t, s){
return (t/=1.)*t*((s+1.)*t - s)
}
define easeOutBack(t){
var s = 1.70158
return ((t=t/1-1)*t*((s+1)*t + s) + 1)
}
define easeOutBack(t, s){
return ((t=t/1-1)*t*((s+1)*t + s) + 1)
}
define easeInOutBack(t)->{
var s = 1.70158
if ((t/=0.5) < 1.) return 0.5*(t*t*(((s*=(1.525))+1.)*t - s))
return 0.5*((t-=2.)*t*(((s*=(1.525))+1.)*t + s) + 2.)
}
define easeInOutBack(t, s)->{
if ((t/=0.5) < 1.) return 0.5*(t*t*(((s*=(1.525))+1.)*t - s))
return 0.5*((t-=2.)*t*(((s*=(1.525))+1.)*t + s) + 2.)
}
define easeInBounce(t){
return 1. - easeOutBounce(1.-t)
}
define easeOutBounce(t){
if (t < (1./2.75)) return (7.5625*t*t)
else if (t < (2./2.75)) return (7.5625*(t-=(1.5/2.75))*t + 0.75)
else if (t < (2.5/2.75)) return (7.5625*(t-=(2.25/2.75))*t + 0.9375)
return (7.5625*(t-=(2.625/2.75))*t + .984375)
}
define easeInOutBounce(t){
if (t < 0.5) return easeInBounce (t*2.) * 0.5
return easeOutBounce (t*2.-1.) * 0.5 + 0.5
}
define easeQuad(t){
return easeOutQuad(t)
}
define easeCubic(t){
return easeInOutCubic(t)
}
define easeQuart(t){
return easeOutQuart(t)
}
define easeQuint(t){
return easeOutQuint(t)
}
define easeSine(t){
return easeOutSine(t)
}
define easeExpo(t){
return easeOutExpo(t)
}
define easeElastic(t){
return easeOutElastic(t)
}
define easeCirc(t){
return easeOutCirc(t)
}
define easeBack(t){
return easeInOutBack(t)
}
define easeBounce(t){
return easeOutBounce(t)
}
struct geom_t{
// mappings
tri = 3,0,1,2
tri_a = 3,0
tri_b = 3,1
tri_c = 3,2
quad = 6,0,1,2,3,4,5
quad_tl = 6,0
quad_tr = 6,1,4
quad_bl = 6,2,3
quad_br = 6,5
}
struct vec_t extends geom_t{
zero(){
_[#] = 0
}
distance( vec v ){
var d = 0
d += (v[#] - _[#]) ** 2
return sqrt(d)
}
len(){
float d = 0
d += _[#] * _[#]
return sqrt(d)
}
negate( vec v ){
_[#] = -v[#]
}
inverse( vec v ){
_[#] = 1 / v[#]
}
mix( vec a, vec b, f ){
_[#] = a[#] + f * (b[#] - a[#])
}
mix2( vec a, vec b, vec f ){
_[#] = a[#] + f[#] * (b[#] - a[#])
}
greater( vec v ){
if( _[#] < v[#] ) return false
return true
}
abs( vec v ){
_[#] = abs(v[#])
}
sin( vec v ){
_[#] = sin(v[#])
}
cos( vec v ){
_[#] = cos(v[#])
}
tan( vec v ){
_[#] = tan(v[#])
}
asin( vec v){
_[#] = asin(v[#])
}
acos( vec v){
_[#] = asin(v[#])
}
atan( vec v){
_[#] = asin(v[#])
}
pow( vec v, vec w){
_[#] = pow(v[#], w[#])
}
exp( vec v ){
_[#] = exp(v[#])
}
log( vec v ){
_[#] = log(v[#])
}
exp2( vec v ){
_[#] = exp2(v[#])
}
log2( vec v ){
_[#] = log2(v[#])
}
sqrt( vec v ){
_[#] = sqrt(v[#])
}
inversesqrt( vec v ){
_[#] = inversesqrt(v[#])
}
abs( vec v ){
_[#] = abs(v[#])
}
sign( vec v ){
_[#] = sign(v[#])
}
sign( vec v ){
_[#] = sign(v[#])
}
floor( vec v ){
_[#] = floor(v[#])
}
ceil( vec v ){
_[#] = ceil(v[#])
}
fract( vec v ){
_[#] = fract(v[#])
}
mod2( vec a, vec b ){
_[#] = mod(a[#], b[#])
}
mod( vec a, float b ){
_[#] = mod(a[#], b)
}
min2( vec a, vec b ){
_[#] = max(a[#], b[#])
}
min( vec a, float b ){
_[#] = max(a[#], b)
}
max2( vec a, vec b ){
_[#] = max(a[#], b[#])
}
max( vec a, float b ){
_[#] = max(a[#], b)
}
clamp( vec v, float min, float max ){
_[#] = v[#] < min? min: v[#] > max? max: v[#]
}
clamp2( vec v, vec min, vec max ){
_[#] = v[#] < min[#]? min[#]: v[#] > max[#]? max[#]: v[#]
}
normalize( vec v ){
var d = 0
d += v[#] ** 2
d = sqrt(d)
if(d == 0){
_[#] = 0
}
else {
_[#] = v[#] / d
}
}
step( vec v, float s ){
_[#] = v[#] < s? 0: 1
}
step2( vec v, vec s ){
_[#] = v[#] < s[#]? 0: 1
}
smoothstep2( vec e0, vec e1, vec v){
_[#] = clamp((v[#] - e0[#])/(e1[#]-e0[#]), 0, 1)
_[#] = _[#] * _[#] * (3 - 2 * _[#])
}
smoothstep( float e0, float e1, vec v){
var e2 = e1 - e0
_[#] = clamp((v[#] - e0)/e2, 0, 1)
_[#] = _[#] * _[#] * (3 - 2 * _[#])
}
sign( vec v ){
_[#] = v[#] < 0? -1: v[#] > 0? 1: 0
}
dot( vec v ){
float d = 0
d += _[#] * v[#]
return d
}
mul( vec a, vec b ){
_[#] = a[#] * b[#]
}
}
struct bvec2{
bool x, y
}
struct bvec3{
bool x, y, z
}
struct bvec4{
bool x, y, z, w
}
struct ivec2{
int x, y
}
struct ivec3{
int x, y, z
}
struct ivec4{
int x, y, z, w
}
struct vec2 extends vec_t{
float x, y
random( float scale = 1 ){
var r = 2PI * random()
x = cos(r) * scale
y = sin(r) * scale
}
vec2_min_vec2( vec2 a, vec2 b ){
x = a[0] - b[0]
y = a[1] - b[1]
}
vec2_add_vec2( vec2 a, vec2 b ){
x = a[0] + b[0]
y = a[1] + b[1]
}
vec2_div_vec2( vec2 a, vec2 b ){
x = a[0] / b[0]
y = a[1] / b[1]
}
vec2_mul_vec2( vec2 a, vec2 b ){
x = a[0] * b[0]
y = a[1] * b[1]
}
vec2_mul_mat2( mat3 m, vec2 v ){
var vx = v.x, vy = v.y
x = m[0] * vx + m[2] * vy
y = m[1] * vx + m[3] * vy
}
vec2_mul_mat3( mat3 m, vec2 v ){
var vx = v.x, vy = v.y
x = m[0] * vx + m[2] * vy + m[4]
y = m[1] * vx + m[3] * vy + m[5]
}
vec2_mul_mat4( mat4 m, vec2 v ){
var vx = v.x, vy = v.y
x = m[0] * vx + m[4] * vy + m[12]
y = m[1] * vx + m[5] * vy + m[13]
}
vec2_mul_float( vec2 m, float f ){
x = m[0] * f
y = m[1] * f
}
lessThan( vec2 v ){
return bvec( x < v.x, y < v.y )
}
lessThanEqual( vec2 v ){
return bvec( x <= v.x, y <= v.y )
}
greaterThan( vec2 v ){
return bvec( x > v.x, y > v.y )
}
greaterThanEqual( vec2 v ){
return bvec( x >= b.x, y >= b.y )
}
equal( vec2 c ){
return bvec( x == b.x, y == b.y )
}
notEqual( vec2 c ){
return bvec( x != b.x, y != b.y )
}
}
struct vec3 extends vec_t{
float x, y, z
random( float scale = 1 ){
var r = 2PI * random()
var zr = (random() * 2.0) - 1.0
var zs = sqrt(1.0 - zr*zr) * scale
x = cos(r) * zs
y = sin(r) * zs
z = z * scale
}
vec3_min_vec3( vec3 a, vec3 b ){
x = a[0] - b[0]
y = a[1] - b[1]
z = a[2] - b[2]
}
vec3_add_vec3( vec3 a, vec3 b ){
x = a[0] + b[0]
y = a[1] + b[1]
z = a[2] + b[2]
}
vec3_div_vec3( vec3 a, vec3 b ){
x = a[0] / b[0]
y = a[1] / b[1]
z = a[2] / b[2]
}
vec3_mul_vec3( vec3 a, vec3 b ){
x = a[0] * b[0]
y = a[1] * b[1]
z = a[2] * b[2]
}
vec3_mul_mat3( vec3 v, mat3 m ){
var vx = v.x, vy = v.y, vz = v.z
x = vx * m[0] + vy * m[1] + vz * m[2]
y = vx * m[3] + vy * m[4] + vz * m[5]
z = vx * m[6] + vy * m[7] + vz * m[8]
}
vec3_mul_mat4( vec3 v, mat4 m ){
var vx = v.x, vy = v.y, vz = v.z, vw =
m[12] * vx + m[13] * vy + m[14] * vz + m[15]
vw = vw || 1.0
x = (m[0] * vx + m[1] * vy + m[2] * vz + m[3]) / vw
y = (m[4] * vx + m[5] * vy + m[6] * vz + m[7]) / vw
z = (m[8] * vx + m[9] * vy + m[10] * vz + m[11]) / vw
}
mulminor( vec3 v, mat4 m ){
var vx = v.x, vy = v.y, vz = v.z
x = vx * m[0] + vy * m[1] + vz * m[2]
y = vx * m[4] + vy * m[5] + vz * m[6]
z = vx * m[8] + vy * m[9] + vz * m[10]
}
cross( vec3 a, vec3 b ){
var ax = a.x, ay = a.y, az = a.z,
bx = b.x, by = b.y, bz = b.z
x = ay * bz - az * by
y = az * bx - ax * bz
z = ax * by - ay * bx
}
vec3_add_vec3( vec3 a, vec3 b ){
x = a.x + b.x
y = a.y + b.y
z = a.z + b.z
}
vec3_mul_float( vec3 a, float v ){
x = a.x * v
y = a.y * v
z = a.z * v
}
unproject(vec4mat4 world, mat4 project){
mat4 nproj = world * mat4.inverse(project)
return this *
project()
}
}
struct vec4 extends vec_t{
float x, y, z, w
random( float scale = 1 ){
x = random()
y = random()
z = random()
w = random()
_.normalize(_)
}
vec4_mul_mat4( mat4 m, vec4 v ){
var vx = v.x, vy = v.y, vz = v.z, vw = v.w
x = m[0] * vx + m[1] * vy + m[2] * vz + m[3] * vw
y = m[4] * vx + m[5] * vy + m[6] * vz + m[7] * vw
z = m[8] * vx + m[9] * vy + m[10] * vz + m[11] * vw
w = m[12] * vx + m[13] * vy + m[14] * vz + m[15] * vw
}
vec4_mul_quat( quat q, vec4 v ){
var vx = v.x, vy = v.y, vz = v.z,
qx = q.a, qy = q.b, qz = q.c, qw = q.d,
// calculate quat * vec
ix = qw * vx + qy * vz - qz * vy,
iy = qw * vy + qz * vx - qx * vz,
iz = qw * vz + qx * vy - qy * vx,
iw = -qx * vx - qy * vy - qz * vz
// calculate result * inverse quat
x = ix * qw + iw * -qx + iy * -qz - iz * -qy
y = iy * qw + iw * -qy + iz * -qx - ix * -qz
z = iz * qw + iw * -qz + ix * -qy - iy * -qx
}
}
struct mat{
zero(){
_[#] = 0
}
matrixCompMult(mat a, mat b){
_[#] = a[#] * b[#]
}
}
struct quat extends vec_t{
float x,y,z,w
identity(){
x = y = z = 0, w = 1
}
// Shortest rotation path from quat A to quat B
rotationTo( vec3 a, vec3 b ) {
var dot = vec3.dot( A, B )
if (dot < -0.999999) {
vec3 t = vec3.cross( vec3(1,0,0), a )
if (t.length() < 0.000001) t.cross(vec3(0,1,0), a)
t.normalize(t)
quat.setAxisAngle(t, PI)
}
else if (dot > 0.999999) {
x = 0, y = 0, z = 0, w = 1
}
else {
vec3 t = vec3.cross(a, b)
x = t.x, y = t.y, z = t.z, w = 1 + dot
_.normalize(_)
}
}
setAxes( vec3 dir, vec3 right, vec3 up ) {
_.fromMat3(mat3(
right.x, up.x, -dir.x,
right.y, up.y, -dir.y,
right.z, up.z, -dir.z
))
_.normalize(_)
}
// quaternion around aXis, with rotation Angle
setAxisAngle( vec3 v, float angle ) {
angle *= 0.5
var s = sin(angle)
x = s * X[0], y = s * X[1], z = s * X[2], w = cos(angle)
}
quat_mul_quat( quat a, quat b ){
var ax = a.x, ay = a.y, az = a.z, aw = a.w,
bx = b.x, by = b.y, bz = b.z, bw = b.w
x = ax * bw + aw * bx + ay * bz - az * by
y = ay * bw + aw * by + az * bx - ax * bz
z = az * bw + aw * bz + ax * by - ay * bx
w = aw * bw - ax * bx - ay * by - az * bz
}
// rotate quaternion Q with angle A around x axis
rotateX( quat q, float angle ){
angle *= 0.5
var ax = q.x, ay = q.y, az = q.z, aw = q.w,
bx = sin(angle), bw = cos(angle)
x = ax * bw + aw * bx
y = ay * bw + az * bx
z = az * bw - ay * bx
w = aw * bw - ax * bx
}
// rotate quaternion Q with angle A around y axis
rotateY( quat q, float angle ){
angle *= 0.5
var ax = q.x, ay = q.y, az = q.z, aw = q.w,
by = sin(angle), bw = cos(angle)
m00 = ax * bw - az * by
m01 = ay * bw + aw * by
m02 = az * bw + ax * by
m03 = aw * bw - ay * by
}
// rotate quaternion Q with angle A around z axis
rotateZ( quat q, float angle ){
angle *= 0.5
var ax = q.x, ay = q.y, az = q.z, aw = q.w,
bz = sin(angle), bw = cos(angle)
m00 = ax * bw + ay * bz
m01 = ay * bw - ax * bz
m02 = az * bw + aw * bz
m03 = aw * bw - az * bz
}