-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpugl.js
3203 lines (2965 loc) · 79.9 KB
/
pugl.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';
const APP_ID = 'dh3YgVZQdX1Q';
function generate_creation_id() {
const parts = new Uint16Array(6);
crypto.getRandomValues(parts);
return Array.from(parts)
.map((x) => {
x = x.toString(16);
while (x.length < 4) x = '0' + x;
return x;
})
.join('-');
}
function parse_json(s) {
try {
return JSON.parse(s);
} catch (e) {
return undefined;
}
}
let gl;
let program_main = null;
let program_post = null;
let vertex_buffer_rect;
let vertex_buffer_main;
let canvas_container;
let canvas;
let framebuffer;
let framebuffer_color_texture;
let sampler_texture;
let current_time = 0;
let ui_shown = true;
let ui_div;
let ui_resize;
let viewport_width, viewport_height;
let next_html_id = 1;
let next_widget_id = 1;
let widget_choices;
let widget_search;
let widgets_container;
let error_element;
let parsed_widgets;
let paused = false;
let pause_element;
let step_element;
let play_element;
let creation_metadata = {};
let creation_id;
let creation_title_element;
let auto_update_element;
const mouse_pos_ndc = Object.preventExtensions({ x: 0, y: 0 });
let render_width = 1080;
let render_height = 1080;
const GLSL_FLOAT_TYPES = ['float', 'vec2', 'vec3', 'vec4'];
const GLSL_FLOAT_TYPE_PAIRS = GLSL_FLOAT_TYPES.flatMap((x) =>
GLSL_FLOAT_TYPES.map((y) => [x, y])
);
const builtin_widgets = [
`
//! .name: Buffer
//! .category: basic
//! .description: outputs its input unaltered. useful for defining constants.
//! x.name: input
//! x.id: input
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} buffer(${type} x) {
return x;
}`
).join(''),
`
//! .name: Slider
//! .category: basic
//! .description: an adjustable slider between two values.
//! x.id: x
//! x.default: 0.5
//! x.control: slider
//! min_val.id: min
//! min_val.default: 0
//! max_val.id: max
//! max_val.default: 1
float slider(float x, float min_val, float max_val) {
return mix(min_val, max_val, x);
}
`,
`
//! .name: Mix
//! .alt: lerp
//! .category: basic
//! .id: mix
//! .description: weighted average of two inputs
//! a.name: source 1
//! a.default: 0
//! b.name: source 2
//! b.default: 1
//! x.name: mix
//! x.default: 0.5
//! c.name: clamp mix
//! c.control: checkbox
//! c.description: clamp the mix input to the [0, 1] range
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} mix_(${type} a, ${type} b, ${type} x, int c) {
if (c != 0) x = clamp(x, 0.0, 1.0);
return mix(a, b, x);
}
`
).join(''),
`
//! .name: Last frame
//! .category: basic
//! .id: prev
//! .description: sample from the previous frame
//! pos.description: position to sample — bottom-left corner is (−1, −1), top-right corner is (1, 1)
//! pos.default: .pos
//! wrap.name: wrap mode
//! wrap.control: select:clamp|wrap
//! wrap.description: how to deal with the input components if they go outside [−1, 1]
//! samp.id: sample
//! samp.name: sample mode
//! samp.control: select:linear|nearest
//! samp.description: how positions in between pixels should be sampled
vec3 last_frame(vec2 pos, int wrap, int samp) {
pos = pos * 0.5 + 0.5;
if (wrap == 0)
pos = clamp(pos, 0.0, 1.0);
else if (wrap == 1)
pos = mod(pos, 1.0);
if (samp == 1)
pos = floor(0.5 + pos * _texture_size) * (1.0 / _texture_size);
return texture(_texture, pos).xyz;
}
`,
`
//! .name: Weighted sum
//! .alt: weighted add
//! .category: arithmetic
//! .description: add two numbers or vectors with weights
//! aw.name: a weight
//! aw.default: 1
//! bw.name: b weight
//! bw.default: 1
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} wtadd(${type} a, float aw, ${type} b, float bw) {
return a * aw + b * bw;
}
`
).join(''),
`
//! .name: Add
//! .category: arithmetic
//! .description: add two numbers or vectors
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} add(${type} a, ${type} b) {
return a + b;
}
`
).join(''),
`
//! .name: Subtract
//! .category: arithmetic
//! .description: subtract one number or vector from another
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} sub(${type} a, ${type} b) {
return a - b;
}
`
).join(''),
`
//! .name: Multiply
//! .category: arithmetic
//! .description: multiply two numbers, scale a vector by a number, or perform component-wise multiplication between vectors
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} mul(${type} a, ${type} b) {
return a * b;
}
`
).join(''),
`
//! .name: Divide
//! .category: arithmetic
//! .description: divide one number or vector by another
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} div(${type} a, ${type} b) {
return a / b;
}
`
).join(''),
`
//! .name: Power
//! .category: arithmetic
//! .id: pow
//! .description: take one number to the power of another
//! a.id: a
//! b.default: 0.5
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} pow_(${type} a, ${type} b) {
return pow(a, b);
}
`
).join(''),
`
//! .name: Modulo
//! .category: arithmetic
//! .id: mod
//! .description: wrap a value at a certain limit
//! a.name: a
//! b.default: 1
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} mod_(${type} a, ${type} b) {
return mod(a, b);
}
`
).join(''),
`
//! .name: Square
//! .category: geometry
//! .description: select between two inputs depending on whether a point lies within a square (or cube in 3D)
//! pos.name: pos
//! pos.description: point to test
//! pos.default: .pos
//! inside.description: source to use if pos lies inside the square
//! inside.default: #f00
//! outside.description: source to use if pos lies outside the square
//! outside.default: #0f0
//! size.description: radius of the square
//! size.default: 0.5
` +
[
['float', 'a'],
['vec2', 'max(a.x, a.y)'],
['vec3', 'max(a.x, max(a.y, a.z))'],
['vec4', 'max(max(a.x, a.y), max(a.z, a.w))'],
]
.map((x) => {
const type = x[0];
const max = x[1];
return ['float', 'vec2', 'vec3', 'vec4']
.map(
(type2) => `
${type2} square(${type} pos, ${type2} inside, ${type2} outside, ${type} size) {
${type} a = abs(pos) / size;
return ${max} < 1.0 ? inside : outside;
}
`
)
.join('');
})
.join(''),
`
//! .name: Circle
//! .category: geometry
//! .description: select between two inputs depending on whether a point lies within a circle (or sphere in 3D)
//! pos.default: .pos
//! pos.description: point to test
//! inside.default: #f00
//! inside.description: source to use if pos lies inside the circle
//! outside.default: #0f0
//! outside.description: source to use if pos lies outside the circle
//! size.default: 0.5
//! size.description: radius of the circle
` +
GLSL_FLOAT_TYPE_PAIRS.map(
([type, type2]) => `
${type2} circle(${type} pos, ${type2} inside, ${type2} outside, ${type} size) {
pos /= size;
return dot(pos, pos) < 1.0 ? inside : outside;
}
`
).join(''),
`
//! .name: Comparator
//! .category: basic
//! .description: select between two inputs depending on a comparison between two values
//! .id: cmp
//! cmp1.name: compare 1
//! cmp1.description: input to compare against "Compare 2"
//! cmp2.name: compare 2
//! cmp2.default: 0
//! cmp2.description: input to compare against "Compare 1"
//! less.name: if less
//! less.default: 0
//! less.description: value to output if "Compare 1" < "Compare 2"
//! greater.name: if greater
//! greater.default: 1
//! greater.description: value to output if "Compare 1" ≥ "Compare 2"
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} compare(float cmp1, float cmp2, ${type} less, ${type} greater) {
return cmp1 < cmp2 ? less : greater;
}
`
).join(''),
`
//! .name: Sine wave
//! .category: curves
//! .description: sine, triangle, square, sawtooth waves
//! .id: sin
//! type.control: select:sin|tri|squ|saw
//! t.description: position in the wave
//! t.default: .time
//! period.description: period of the wave
//! period.default: 1
//! amp.name: amplitude
//! amp.default: 1
//! amp.description: amplitude (maximum value) of the wave
//! phase.default: 0
//! phase.description: phase of the wave (0.5 = phase by ½ period)
//! center.name: baseline
//! center.default: 0
//! center.description: this value is added to the output at the end
//! nonneg.name: non-negative
//! nonneg.description: make the wave go from baseline to baseline+amp, rather than baseline-amp to baseline+amp
//! nonneg.control: checkbox
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} sine_wave(int type, ${type} t, ${type} period, ${type} amp, ${type} phase, ${type} center, int nonneg) {
${type} v = ${type}(0.0);
t = t / period - phase;
if (type == 0) {
v = sin(t * 6.2831853);
} else if (type == 1) {
t = mod(t, 1.0);
${type} s = step(${type}(0.5), t);
v = mix(4.0 * t - 1.0, 3.0 - 4.0 * t, s);
} else if (type == 2) {
v = mod(floor(2.0 * t) + 1.0, 2.0) * 2.0 - 1.0;
} else if (type == 3) {
v = mod(t, 1.0) * 2.0 - 1.0;
}
if (nonneg != 0) v = v * 0.5 + 0.5;
return amp * v + center;
}
`
).join(''),
`
//! .name: Rotate 2D
//! .category: geometry
//! .id: rot2
//! .description: rotate a 2-dimensional vector
//! v.description: vector to rotate
//! theta.name: θ
//! theta.description: angle to rotate by (in radians)
//! dir.name: direction
//! dir.description: direction of rotation
//! dir.control: select:CCW|CW
vec2 rotate2D(vec2 v, float theta, int dir) {
if (dir == 1) theta = -theta;
float c = cos(theta), s = sin(theta);
return vec2(c*v.x - s*v.y, s*v.x + c*v.y);
}
`,
`
//! .name: Hue shift
//! .category: colors
//! .id: hue
//! .description: shift hue of color
//! color.description: input color
//! shift.description: how much to shift hue by (0.5 = shift halfway across the rainbow)
vec3 hue_shift(vec3 color, float shift) {
vec3 c = color;
// rgb to hsv
vec3 hsv;
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
hsv = vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
hsv.x = mod(hsv.x + shift, 1.0);
c = hsv;
// hsv to rgb
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
}
`,
`
//! .name: Saturate
//! .category: colors
//! .id: saturate
//! .description: change saturation of color
//! color.description: input color
//! amount.description: how much to change saturation by (−1 to 1 range)
vec3 saturate(vec3 color, float amount) {
vec3 c = color;
// rgb to hsv
vec3 hsv;
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
hsv = vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
hsv.y = clamp(hsv.y + amount, 0.0, 1.0);
c = hsv;
// hsv to rgb
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
}
`,
`
//! .name: Brightness-contrast
//! .category: colors
//! .description: change brightness/contrast of color
//! color.description: input color
//! brightness.description: how much to change brightness by (−1 to 1 range)
//! contrast.description: how much to change contrast by (−1 to 1 range)
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} brightcont(${type} color, ${type} brightness, ${type} contrast) {
brightness = clamp(brightness, -1.0, 1.0);
contrast = clamp(contrast, -1.0, 1.0);
return clamp((contrast + 1.0) / (1.0 - contrast) * (color - 0.5) + (brightness + 0.5), 0.0, 1.0);
}
`
).join(''),
`
//! .name: Clamp
//! .category: basic
//! .id: clamp
//! .description: clamp a value between a minimum and maximum
//! x.name: value
//! x.id: val
//! x.description: input value
//! minimum.name: min
//! minimum.id: min
//! maximum.name: max
//! maximum.id: max
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} clamp_(${type} x, ${type} minimum, ${type} maximum) {
return clamp(x, minimum, maximum);
}
`
).join(''),
`
//! .name: Rotate 3D
//! .id: rot3
//! .category: geometry
//! .description: rotate a 3D vector about an axis
//! v.description: the vector to rotate
//! axis.description: the axis to rotate around. the magnitude must be non-zero but otherwise is ignored.
//! axis.default: 0,1,0
//! angle.name: θ
//! angle.description: the angle in radians
//! angle.default: 0.57
vec3 rot3(vec3 v, vec3 axis, float angle) {
axis = normalize(axis);
float c = cos(angle);
float s = sin(angle);
// https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
return v * c + cross(axis, v) * s + axis * dot(axis, v) * (1.0 - c);
}
`,
`
//! .name: Remap
//! .id: remap
//! .category: basic
//! .description: linearly remap a value from one interval to another
//! x.id: x
//! a1.name: a₁
//! a1.default: 0
//! a1.description: negative endpoint of source interval
//! b1.name: b₁
//! b1.default: 1
//! b1.description: positive endpoint of source interval
//! a2.name: a₂
//! a2.default: -1
//! a2.description: positive endpoint of source interval
//! b2.name: b₂
//! b2.default: 1
//! b2.description: positive endpoint of destination interval
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} remap(${type} x, ${type} a1, ${type} b1, ${type} a2, ${type} b2) {
return (x - a1) / (b1 - a1) * (b2 - a2) + a2;
}
`
).join(''),
`
//! .name: Smoothstep
//! .id: smoothstep
//! .category: curves
//! .description: smoothly transition between two values (with Hermite interpolation)
//! t.id: t
//! t1.name: t₁
//! t1.description: first input point
//! t1.default: 0
//! t2.name: t₂
//! t2.description: second input point
//! t2.default: 1
//! out1.name: out₁
//! out1.description: output value when t ≤ t₁
//! out1.default: 0
//! out2.name: out₂
//! out2.description: output value when t ≥ t₂
//! out2.default: 1
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} smoothst(${type} t, ${type} t1, ${type} t2, ${type} out1, ${type} out2) {
return mix(out1, out2, smoothstep(t1, t2, t));
}
`
).join(''),
`
//! .name: Arctangent
//! .id: arctan2
//! .category: trigonometry
//! .description: The arctangent function (radians) with 2 parameters (set x = 1 for normal arctangent)
//! y.id: y
//! x.id: x
//! x.default: 1
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} arctan2(${type} y, ${type} x) {
return atan(y, x);
}
`
).join(''),
`
//! .name: Cosine
//! .id: cos
//! .category: trigonometry
//! .description: The cosine function (radians)
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} cos_(${type} x) {
return cos(x);
}
`
).join(''),
`
//! .name: Sine
//! .id: sine
//! .category: trigonometry
//! .description: The sine function (radians)
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} sin_(${type} x) {
return sin(x);
}
`
).join(''),
`
//! .name: Tangent
//! .id: tan
//! .category: trigonometry
//! .description: The tangent function (radians)
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} tan_(${type} x) {
return tan(x);
}
`
).join(''),
`
//! .name: Arcsine
//! .id: arcsin
//! .category: trigonometry
//! .description: The arcsine function (radians) — input will be clamped to [−1, 1]
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} arcsin(${type} x) {
return asin(clamp(x, -1.0, 1.0));
}
`
).join(''),
`
//! .name: Arccosine
//! .id: arccos
//! .category: trigonometry
//! .description: The arccosine function (radians) — input will be clamped to [−1, 1]
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} arccos(${type} x) {
return acos(clamp(x, -1.0, 1.0));
}
`
).join(''),
`
//! .name: Hyperbolic cosine
//! .alt: cosh
//! .id: cosh
//! .category: trigonometry
//! .description: The hyperbolic cosine function
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} cosh_(${type} x) {
return cosh(x);
}
`
).join(''),
`
//! .name: Hyperbolic sine
//! .alt: sinh
//! .id: sinh
//! .category: trigonometry
//! .description: The hyperbolic sine function
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} sinh_(${type} x) {
return sinh(x);
}
`
).join(''),
`
//! .name: Hyperbolic tangent
//! .alt: tanh
//! .id: tanh
//! .category: trigonometry
//! .description: The hyperbolic tangent function (radians)
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} tanh_(${type} x) {
return tanh(x);
}
`
).join(''),
`
//! .name: Hyperbolic arccosine
//! .alt: acosh
//! .alt: arccosh
//! .id: arccosh
//! .category: trigonometry
//! .description: The hyperbolic arccosine function (radians) — input will be clamped to [1, ∞]
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} arccosh(${type} x) {
return acosh(max(x, 1.0));
}
`
).join(''),
`
//! .name: Hyperbolic arcsine
//! .alt: asinh
//! .alt: arcsinh
//! .id: arcsinh
//! .category: trigonometry
//! .description: The hyperbolic arcsine function (radians)
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} arcsinh(${type} x) {
return asinh(x);
}
`
).join(''),
`
//! .name: Hyperbolic arctangent
//! .alt: atanh
//! .alt: arctanh
//! .id: arctanh
//! .category: trigonometry
//! .description: The hyperbolic arctangent function (radians) — input will be clamped to [-1, 1]
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} arctanh(${type} x) {
return atanh(clamp(x, -1.0, 1.0));
}
`
).join(''),
`
//! .name: Sigmoid
//! .id: sigmoid
//! .category: curves
//! .description: The sigmoid function — smoothly maps the interval (−∞, ∞) to (a, b)
//! x.description: input value
//! a.description: output value for very negative inputs
//! b.description: output value for very positive inputs
//! sharpness.description: scale factor for input value — higher = quicker transition from a to b
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} sigmoid(${type} x, ${type} a, ${type} b, ${type} sharpness) {
return mix(a, b, 1.0 / (1.0 + exp(-sharpness * x)));
}
`
).join(''),
`
//! .name: Staircase
//! .id: floor
//! .category: curves
//! .description: The floor function — largest integer less than x
//! x.description: input value
//! stepw.name: step w
//! stepw.description: step width
//! steph.name: step h
//! steph.description: step height
//! phase.description: proportion of a step to be added to input
//! phase.default: 0
` +
GLSL_FLOAT_TYPES.map(
(type) => `
${type} floorf(${type} x, ${type} stepw, ${type} steph, ${type} phase) {
return floor(x / stepw + phase) * steph;
}
`
).join(''),
`
//! .name: Sine noise
//! .category: noise
//! .description: Noise generated from sine waves
//! x.id: x
//! falloff.description: values closer to 0 will emphasize lower-frequency noise, values towards 1 will emphasize higher-frequency noise
//! falloff.default: 0.5
//! freqstep.description: ratio between successive frequencies of noise
//! freqstep.default: 2
//! levels.description: number of frequencies of noises to add together
//! levels.control: int:1|30
//! levels.default: 8
float noise_sin(float x, float falloff, float freqstep, int levels) {
float k = 1.0;
float phase = 2.45;
falloff = clamp(falloff, 0.0, 1.0);
float v = 0.0;
int i = 0;
for (i = 0; i < levels; i++) {
float s = sin(x + phase);
v += k * s * s;
x *= freqstep;
k *= falloff;
phase *= 1.7;
phase = mod(phase, 6.28);
}
return v * (1.0 - falloff);
}
float noise_sin(vec2 x, float falloff, float freqstep, int levels) {
float v = 0.0;
float k = 1.0;
vec2 phase = vec2(1.0, 3.6);
float theta = 2.7;
for (int i = 0; i < levels; i++) {
v += k * abs(sin(x.x + phase.x) * sin(x.y + phase.y));
phase *= 3.8;
phase = mod(phase, 6.28);
x *= freqstep;
x = mat2(cos(theta), sin(theta), -sin(theta), cos(theta)) * x;
k *= falloff;
theta *= 2.4;
theta = mod(theta, 6.28);
}
return v * (1.0 - falloff);
}
float noise_sin(vec3 x, float falloff, float freqstep, int levels) {
float v = 0.0;
float k = 1.0;
vec3 phase = vec3(1.0, 3.6, 2.2);
float theta = 2.7;
float phi = 4.6;
for (int i = 0; i < levels; i++) {
v += k * abs(sin(x.x + phase.x) * sin(x.y + phase.y) * sin(x.z + phase.z));
phase *= 4.7;
phase = mod(phase, 6.28);
x *= freqstep;
float ct = cos(theta), st = sin(theta);
float cp = cos(phi), sp = sin(phi);
x = mat3(st*cp, ct*cp, -sp, st*sp, ct*sp, cp, ct, -st, 0.0) * x;
k *= falloff;
theta *= 2.4;
theta = mod(theta, 6.28);
}
return v * (1.0 - falloff);
}
`,
`
//! .name: Norm
//! .alt: length/magnitude
//! .description: the Euclidean norm ("length") of a vector
//! .category: geometry
float norm(float x) { return x; }
float norm(vec2 x) { return length(x); }
float norm(vec3 x) { return length(x); }
float norm(vec4 x) { return length(x); }
`,
`
//! .name: Distance
//! .description: the Euclidean distance between two points
//! .category: geometry
` +
GLSL_FLOAT_TYPES.map(
(type) => `
float dist(${type} x, ${type} y) { return distance(x, y); }
`
).join(''),
`
//! .name: Dot product
//! .description: the dot product between two vectors
//! .category: geometry
//! .id: dot
` +
GLSL_FLOAT_TYPES.map(
(type) => `
float dot_prod(${type} x, ${type} y) { return dot(x, y); }
`
).join(''),
`
//! .name: Cross product
//! .description: the cross product between two 3D vectors
//! .category: geometry
//! .id: cross
vec3 cross_(vec3 x, vec3 y) {
return cross(x, y);
}
`,
`
//! .name: White noise
//! .description: Uniform distribution over [0, 1)
//! .category: noise
float wnoise(float x)
{
uint k = 134775813u;
uint u = floatBitsToUint(x) * k;
u = ((u >> 8) ^ u) * k;
u = ((u >> 8) ^ u) * k;
u = ((u >> 8) ^ u) * k;
return float(u) * (1.0 / 4294967296.0);
}
float wnoise(vec2 x)
{
uint k = 134775813u;
uvec2 u = floatBitsToUint(x) * k;
u = ((u >> 8) ^ u.yx) * k;
u = ((u >> 8) ^ u.yx) * k;
u = ((u >> 8) ^ u.yx) * k;
return float(u) * (1.0 / 4294967296.0);
}
float wnoise(vec3 x)
{
uint k = 134775813u;
uvec3 u = floatBitsToUint(x) * k;
u = ((u >> 8) ^ u.yzx) * k;
u = ((u >> 8) ^ u.yzx) * k;
u = ((u >> 8) ^ u.yzx) * k;
return float(u) * (1.0 / 4294967296.0);
}
float wnoise(vec4 x)
{
uint k = 134775813u;
uvec4 u = floatBitsToUint(x) * k;
u = ((u >> 8) ^ u.yzwx) * k;
u = ((u >> 8) ^ u.yzwx) * k;
u = ((u >> 8) ^ u.yzwx) * k;
return float(u) * (1.0 / 4294967296.0);
}
`,
`
//! .name: Perlin noise
//! .description: Perlin noise with range [0, 1]
//! .category: noise
//! .require: wnoise
//! x.default: .pos
//! freq.description: input is scaled by this
//! freq.default: 8
float perlin(float x, float freq) {
x *= freq;
float grid0 = floor(x);
float grid1 = grid0 + 1.0;
float d0 = x - grid0;
float d1 = x - grid1;
float grad0 = wnoise(grid0) < 0.5 ? -1.0 : 1.0;
float grad1 = wnoise(grid1) < 0.5 ? -1.0 : 1.0;
float n0 = dot(grad0, d0);
float n1 = dot(grad1, d1);
float s = smoothstep(0.0, 1.0, d0);
float p = mix(n0, n1, s);
return p * 0.5 + 0.5;
}
float perlin(vec2 x, vec2 freq) {
x *= freq;
vec2 grid00 = floor(x);
vec2 grid01 = grid00 + vec2(0.0, 1.0);
vec2 grid10 = grid00 + vec2(1.0, 0.0);
vec2 grid11 = grid00 + 1.0;
vec2 d00 = x - grid00;
vec2 d01 = x - grid01;
vec2 d10 = x - grid10;
vec2 d11 = x - grid11;
float twopi = 6.2831853;
float theta00 = wnoise(grid00) * twopi;
float theta01 = wnoise(grid01) * twopi;
float theta10 = wnoise(grid10) * twopi;
float theta11 = wnoise(grid11) * twopi;
vec2 grad00 = vec2(cos(theta00), sin(theta00));
vec2 grad01 = vec2(cos(theta01), sin(theta01));
vec2 grad10 = vec2(cos(theta10), sin(theta10));
vec2 grad11 = vec2(cos(theta11), sin(theta11));
float n00 = dot(grad00, d00);
float n01 = dot(grad01, d01);
float n10 = dot(grad10, d10);
float n11 = dot(grad11, d11);
vec2 s = smoothstep(0.0, 1.0, d00);
float n0 = mix(n00, n10, s.x);
float n1 = mix(n01, n11, s.x);
float p = mix(n0, n1, s.y);