-
Notifications
You must be signed in to change notification settings - Fork 16
/
shadertoy.cxx
3584 lines (2850 loc) · 100 KB
/
shadertoy.cxx
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
#if __circle_build__ < 103
#error "Circle build 103 required to reliably compile this sample"
#endif
#include <imgui.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h>
#define GL_GLEXT_PROTOTYPES
#include <gl3w/GL/gl3w.h>
#include <GLFW/glfw3.h>
#include <cstdio>
#include <cstdlib>
#include <complex>
#include <memory>
#include <vector>
#include <atomic>
#include <thread>
#include <csignal>
// Interlacing
#include "adam7.hxx"
template<typename type_t>
const char* enum_to_string(type_t x) {
switch(x) {
@meta for enum(type_t e : type_t) {
case e:
return @enum_name(e);
}
default:
return "unknown";
}
}
namespace imgui {
// imgui attribute tags.
using color3 [[attribute]] = void;
using color4 [[attribute]] = void;
template<typename type_t>
struct minmax_t {
type_t min, max;
};
using range_float [[attribute]] = minmax_t<float>;
using range_int [[attribute]] = minmax_t<int>;
// Resolution for drag.
using drag_float [[attribute]] = float;
using title [[attribute]] = const char*;
using url [[attribute]] = const char*;
}
// Return true if any option has changed.
template<typename options_t>
bool render_imgui(options_t& options, const char* child_name = nullptr) {
ImGui::SetColorEditOptions(ImGuiColorEditFlags_Float);
bool changed = false;
if(!child_name ||
ImGui::TreeNodeEx(child_name, ImGuiTreeNodeFlags_DefaultOpen)) {
using namespace imgui;
if constexpr(@has_attribute(options_t, url))
ImGui::Text(@attribute(options_t, url));
@meta for(int i = 0; i < @member_count(options_t); ++i) {{
typedef @member_type(options_t, i) type_t;
const char* name = @member_name(options_t, i);
auto& value = @member_value(options, i);
if constexpr(@member_has_attribute(options_t, i, color4)) {
static_assert(std::is_same_v<type_t, vec4>);
changed |= ImGui::ColorEdit4(name, &value.x);
} else if constexpr(@member_has_attribute(options_t, i, color3)) {
static_assert(std::is_same_v<type_t, vec3>);
changed |= ImGui::ColorEdit3(name, &value.x);
} else if constexpr(@member_has_attribute(options_t, i, range_float)) {
auto range = @member_attribute(options_t, i, range_float);
if constexpr(std::is_same_v<type_t, vec4>) {
changed |= ImGui::SliderFloat4(name, &value.x, range.min, range.max);
} else if constexpr(std::is_same_v<type_t, vec3>) {
changed |= ImGui::SliderFloat3(name, &value.x, range.min, range.max);
} else if constexpr(std::is_same_v<type_t, vec2>) {
changed |= ImGui::SliderFloat2(name, &value.x, range.min, range.max);
} else {
static_assert(std::is_same_v<type_t, float>);
changed |= ImGui::SliderFloat(name, &value, range.min, range.max);
}
} else if constexpr(@member_has_attribute(options_t, i, drag_float)) {
auto drag = @member_attribute(options_t, i, drag_float);
if constexpr(std::is_same_v<type_t, vec4>) {
changed |= ImGui::DragFloat4(name, &value.x, drag);
} else if constexpr(std::is_same_v<type_t, vec3>) {
changed |= ImGui::DragFloat3(name, &value.x, drag);
} else if constexpr(std::is_same_v<type_t, vec2>) {
changed |= ImGui::DragFloat2(name, &value.x, drag);
} else {
static_assert(std::is_same_v<type_t, float>);
changed |= ImGui::DragFloat(name, &value, drag);
}
} else if constexpr(@member_has_attribute(options_t, i, range_int)) {
auto range = @member_attribute(options_t, i, range_int);
if constexpr(std::is_same_v<type_t, ivec4>) {
changed |= ImGui::SliderInt4(name, &value.x, range.min, range.max);
} else if constexpr(std::is_same_v<type_t, ivec3>) {
changed |= ImGui::SliderInt3(name, &value.x, range.min, range.max);
} else if constexpr(std::is_same_v<type_t, ivec2>) {
changed |= ImGui::SliderInt2(name, &value.x, range.min, range.max);
} else {
static_assert(std::is_same_v<type_t, int>);
changed |= ImGui::SliderInt(name, &value, range.min, range.max);
}
} else if constexpr(std::is_same_v<type_t, bool>) {
changed |= ImGui::Checkbox(name, &value);
} else if constexpr(std::is_same_v<type_t, vec4>) {
changed |= ImGui::DragFloat4(name, &value.x, .1f);
} else if constexpr(std::is_same_v<type_t, vec3>) {
changed |= ImGui::DragFloat3(name, &value.x, .1f);
} else if constexpr(std::is_same_v<type_t, vec2>) {
changed |= ImGui::DragFloat2(name, &value.x, .1f);
} else if constexpr(std::is_same_v<type_t, float>) {
changed |= ImGui::DragFloat(name, &value, .1f);
} else if constexpr(std::is_same_v<type_t, int>) {
changed |= ImGui::DragInt(name, &value);
} else if constexpr(std::is_enum_v<type_t>) {
if(ImGui::BeginCombo(name, enum_to_string(value))) {
type_t cur = value;
@meta for enum(type_t e : type_t) {
if(ImGui::Selectable(@enum_name(e), e == cur))
value = e;
if(e == cur)
ImGui::SetItemDefaultFocus();
}
changed |= cur != value;
ImGui::EndCombo();
}
} else if constexpr(std::is_same_v<type_t, std::complex<float>>) {
float data[2] { value.real(), value.imag() };
changed |= ImGui::DragFloat2(name, data, .01f);
value.real(data[0]); value.imag(data[1]);
} else if constexpr(std::is_class_v<type_t>) {
// Iterate over each data member.
changed |= render_imgui(value, name);
}
}}
if(child_name)
ImGui::TreePop();
}
return changed;
}
////////////////////////////////////////////////////////////////////////////////
[[using spirv: in, location(0)]]
vec4 vertex_in;
[[spirv::vert]]
void vert_main() {
glvert_Output.Position = vertex_in;
}
[[using spirv: out, location(0)]]
vec4 fragColor;
struct shadertoy_uniforms_t {
// shader-specific parameters.
vec4 mouse; // .xy is current or last drag position.
// .zw is current or last click.
// .zw is negative if mouse button is up.
vec2 resolution; // width and height of viewport in pixels.
float time; // seconds since simulation started.
};
[[using spirv: uniform, binding(0)]]
shadertoy_uniforms_t uniforms;
template<typename shader_t>
[[using spirv: uniform, binding(1)]]
shader_t shader_ubo;
template<typename shader_t>
[[spirv::frag(lower_left)]]
void frag_main() {
fragColor = shader_ubo<shader_t>.render(glfrag_FragCoord.xy, uniforms);
}
////////////////////////////////////////////////////////////////////////////////
inline vec2 rot(vec2 p, vec2 pivot, float a) {
p -= pivot;
p = vec2(
p.x * cos(a) - p.y * sin(a),
p.x * sin(a) + p.y * cos(a)
);
p += pivot;
return p;
}
inline vec2 rot(vec2 p, float a) {
return rot(p, vec2(), a);
}
// https://github.com/hughsk/glsl-hsv2rgb/blob/master/index.glsl
inline vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.x + K.xyz) * 6 - K.w);
return c.z * mix(K.x, saturate(p - K.x), c.y);
}
////////////////////////////////////////////////////////////////////////////////
struct [[
.imgui::title="Mandelbrot Orbit Trap Periods",
.imgui::url="https://www.shadertoy.com/view/Wl2Gz1"
]] fractal_traps_t {
// orion elenzil 20190521
// inspired by https://www.shadertoy.com/view/wtfGWS
typedef std::complex<float> complex_t;
// Point of interest.
struct poi_t {
complex_t center;
float range;
int max_iter;
};
struct result_t {
int iterations;
int cycle_len1;
int cycle_len2;
};
result_t mandel_escape_iters(complex_t c, complex_t offset, float angle) {
complex_t c1 = std::polar(magnitude, angle);
complex_t c2 = c1 / magnitude * .2f;
c1 += offset;
c2 += offset;
complex_t z = c;
int cycle_len1 = 0, cycle_len2 = 0;
int i = 0;
while(i < max_iter) {
// Advance to the next iteration.
z = z * z + c;
if(!cycle_len1 && abs(1 - abs(z - c1)) < .015f)
cycle_len1 = i;
if(!cycle_len2 && abs(.2f - abs(z - c2)) < .01f)
cycle_len2 = i;
if(norm(z) > 4)
break;
++i;
}
return { i, cycle_len1, cycle_len2 };
}
vec4 render(vec2 frag_coord, shadertoy_uniforms_t u) {
constexpr float PI2 = 2 * M_PIf32;
float short_len = min(u.resolution.x, u.resolution.y);
vec2 uv = (2 * frag_coord - u.resolution.xy) / short_len;
complex_t ocOff { };
if(any(u.mouse.xy > 50)) {
vec2 v = (2 * u.mouse.xy - u.resolution.xy) / short_len;
ocOff.real(v.x);
ocOff.imag(v.y);
}
vec3 color { };
complex_t c(uv.x, uv.y);
for(int m = 0; m < 2; ++m) {
for(int n = 0; n < 2; ++n) {
complex_t C = (c + complex_t(m, n) / complex_t(2.f * short_len)) *
range + poi;
result_t result = mandel_escape_iters(C, ocOff, u.time);
float f = result.iterations != max_iter ?
(float)result.iterations / max_iter :
0.f;
f = pow(f, .6f);
f *= .82f;
vec3 rgb = f * base_color;
if(result.cycle_len1 > 0) {
rgb += vec3(cos(PI2 * result.cycle_len1 / sample_count1) * .2f + magnitude);
}
if(result.cycle_len2 > 0) {
float hue = fract((float)result.cycle_len2 / sample_count2);
rgb += hsv2rgb(vec3(hue, .9, .8));
}
color += rgb;
}
}
color /= 4;
return vec4(color, 1);
}
// TODO: Expose complex_t to imgui?
complex_t poi = complex_t(-.2, 0);
[[.imgui::color3]] vec3 base_color = { .2, .6, 1. };
[[.imgui::range_float { .01, 2 }]] float range = 1.2;
[[.imgui::range_int { 4, 512 }]] int max_iter = 90;
[[.imgui::range_float { 0, 1 } ]] float magnitude = .3;
[[.imgui::range_int { 1, 50 } ]] int sample_count1 = 20;
[[.imgui::range_int { 1, 50 } ]] int sample_count2 = 30;
};
////////////////////////////////////////////////////////////////////////////////
struct [[
.imgui::title="2D Clouds",
.imgui::url="https://www.shadertoy.com/view/4tdSWr"
]] clouds_t {
vec2 hash(vec2 p) {
p = vec2(dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)));
return -1.0 + 2.0*fract(sin(p)*43758.5453123);
}
float noise(vec2 p) {
const float K1 = 0.366025404; // (sqrt(3)-1)/2;
const float K2 = 0.211324865; // (3-sqrt(3))/6;
vec2 i = floor(p + (p.x+p.y)*K1);
vec2 a = p - i + (i.x+i.y)*K2;
vec2 o = (a.x>a.y) ? vec2(1.0,0.0) : vec2(0.0,1.0); //vec2 of = 0.5 + 0.5*vec2(sign(a.x-a.y), sign(a.y-a.x));
vec2 b = a - o + K2;
vec2 c = a - 1 + 2 * K2;
vec3 h = max(0.5f - vec3(dot(a,a), dot(b,b), dot(c,c)), 0.f );
vec3 n = h*h*h*h*vec3(dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1)));
return dot(n, vec3(70.0));
}
float fbm(vec2 n) {
float total = 0.0, amplitude = 0.1;
for (int i = 0; i < 7; i++) {
total += noise(n) * amplitude;
n = m * n;
amplitude *= 0.4f;
}
return total;
}
vec4 render(vec2 frag_coord, shadertoy_uniforms_t u) {
vec2 p = frag_coord / u.resolution;
vec2 adjust(u.resolution.x / u.resolution.y, 1);
vec2 uv = p * adjust;
float q = fbm(uv * cloudscale * 0.5f);
// Don't make speed a tunable parameter, because we already use the
// host's speed setting.
const float speed = .03;
float time = u.time * speed;
//ridged noise shape
float r = 0.0;
uv *= cloudscale;
uv -= q - time;
float weight = 0.8;
for (int i=0; i<8; i++){
r += abs(weight*noise( uv ));
uv = m * uv + time;
weight *= 0.7f;
}
//noise shape
float f = 0.0;
uv = p * adjust;
uv *= cloudscale;
uv -= q - time;
weight = 0.7;
for (int i=0; i<8; i++){
f += weight * noise(uv);
uv = m * uv + time;
weight *= 0.6f;
}
f *= r + f;
//noise colour
float c = 0.0;
time = u.time * speed * 2;
uv = p* adjust;
uv *= cloudscale * 2;
uv -= q - time;
weight = 0.4;
for (int i=0; i<7; i++){
c += weight * noise(uv);
uv = m * uv + time;
weight *= 0.6f;
}
//noise ridge colour
float c1 = 0.0;
time = u.time * speed * 3;
uv = p * adjust;
uv *= cloudscale * 3;
uv -= q - time;
weight = 0.4;
for (int i=0; i<7; i++){
c1 += abs(weight*noise( uv ));
uv = m * uv + time;
weight *= 0.6f;
}
c += c1;
vec3 skycolour = mix(skycolour2, skycolour1, p.y);
vec3 cloudcolour = vec3(1.1, 1.1, 0.9) *
saturate(clouddark + cloudlight * c);
f = cloudcover + cloudalpha*f*r;
vec3 result = mix(skycolour, saturate(skytint * skycolour + cloudcolour),
saturate(f + c));
return vec4(result, 1);
}
[[.imgui::range_float { 0, 4 }]] float cloudscale = 1.1;
[[.imgui::range_float { 0, 1 }]] float clouddark = 0.5;
[[.imgui::range_float { 0, 1 }]] float cloudlight = 0.3;
[[.imgui::range_float { 0, 1 }]] float cloudcover = 0.6;
[[.imgui::range_float { 0, 10 }]] float cloudalpha = 8.0;
[[.imgui::range_float { 0, 1 }]] float skytint = 0.2;
[[.imgui::color3]] vec3 skycolour1 = vec3(0.2, 0.4, 0.6);
[[.imgui::color3]] vec3 skycolour2 = vec3(0.4, 0.7, 1.0);
mat2 m = mat2( 1.6, 1.2, -1.2, 1.6 );
};
////////////////////////////////////////////////////////////////////////////////
struct [[
.imgui::title="Devil Egg",
.imgui::url="https://www.shadertoy.com/view/3tXXRX"
]] devil_egg_t {
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yxz+19.19f);
return fract((p3.xxy+p3.yzz)*p3.zyx);
}
float opUnion(float d1, float d2) { return min(d1, d2); }
float opSubtraction(float d1, float d2) { return max(-d1, d2); }
float sdShape(vec2 p, float r) {
return length(p + .5f * r) - r;
}
vec2 egg(vec2 sd, vec2 uv, float t, float r, float a) {
if(r > 0) {
vec2 uv2 = rot(uv, a);
float yolk = sdShape(11 / sqrt(2.f) * uv2, r);
float white = sdShape(5.2f / sqrt(2.f) * uv2, r);
sd = vec2(opUnion(sd.x, white), opUnion(sd.y, yolk));
}
return sd;
}
float under(vec2 N, float t) {
float sz = .001;
float range = 1.8;
vec2 p1 = vec2(sin(-1.66666f * t), cos(t)) * range;
vec2 p2 = vec2(sin(t), sin(1.3333f * t)) * range;
float b = min(length(p1 - N) - sz, length(p2 - N) - sz);
return b;
}
vec2 smallEggField(vec2 sd, vec2 uv, vec2 uvbig, float t) {
vec2 uvsmall = mod(uv + vec2(.3 * t, 0), Span) - .5f * Span;
vec2 uvsmall_q = uv - uvsmall;
// Find the dist to the big egg, quantizing big egg's coords to small
// egg coords.
vec2 sdquant = egg(1e6, uvsmall_q, t, BigSize, under(uvsmall_q, t));
return egg(sd, uvsmall, t, SmallSize * smoothstep(0.f, .8f, sdquant.x - .5f),
under(10 * uvbig, t));
}
vec3 color(vec2 sd, float fact) {
vec3 o = 1 - smoothstep(0.f, fact * vec3(.06, .03, .02), sd.x);
o += BackgroundColor;
if(sd.x < 0)
o -= sd.x * .6f;
o = saturate(o);
vec3 ayolk = 1 - smoothstep(0, fact * vec3(.2, .1, .2), sd.yyy);
o = mix(o, YolkColor, ayolk);
if(sd.y < 0)
o -= sd.y * .1f;
o = saturate(o);
return o;
}
vec4 render(vec2 frag_coord, shadertoy_uniforms_t u) {
constexpr float PI2 = 2 * M_PIf32;
vec2 uv = frag_coord / u.resolution - .5f;
vec2 N = uv;
uv.x *= u.resolution.x / u.resolution.y;
uv.y -= .1f;
uv *= Zoom;
float t = u.time;
vec2 uvbig = uv;
// Big egg.
vec2 sd = egg(1.0e6, uv, t, BigSize, under(uvbig, t));
// Small eggs.
vec2 sdsmall = smallEggField(1.0e6, uv, uvbig, t);
uv -= Span * .5f;
sdsmall = smallEggField(sdsmall, uv, uvbig, t);
vec3 o = mix(color(sd, 2), color(sdsmall, .2f), vec3(step(.1f, sd.x)));
o = pow(o, .5f);
o += (hash32(frag_coord + t) - .5f) * FilmGrain;
o = saturate(o);
o *= 1 - length(13 * pow(abs(N), DarkEdge));
return vec4(o, 1);
}
[[.imgui::range_float { .5, 5 }]] float Zoom = 1;
[[.imgui::range_float { .2, 2 }]] float BigSize = .8;
[[.imgui::range_float { .05, 1 }]] float SmallSize = .09;
[[.imgui::range_float { .05, 2 }]] float Span = .1;
[[.imgui::range_float { .01, .5 }]] float FilmGrain = .1;
[[.imgui::range_float { 1, 5 }]] float DarkEdge = 4;
[[.imgui::color3]] vec3 YolkColor = vec3(.5, .5, 0);
[[.imgui::color3]] vec3 BackgroundColor = vec3(.1, .5, .1);
};
struct [[
.imgui::title="Neon Hypno Bands",
.imgui::url="https://www.shadertoy.com/view/MdcGW4"]]
hypno_bands_t {
float rand(float n){
return fract(cos(n * 89.42f) * 343.42f);
}
float roundx(float x, float p) {
return floor((x + (p * .5f)) / p) * p;
}
float dtoa(float d, float amount) {
return saturate(1 / (clamp(d, 1 / amount, 1.f) * amount));
}
float sdSegment1D(float uv, float a, float b) {
return max(max(a - uv, 0.f), uv - b);
}
vec4 render(vec2 frag_coord, shadertoy_uniforms_t u) {
constexpr float PI2 = 2 * M_PIf32;
vec2 uv = frag_coord / u.resolution - .5f;
uv.x *= u.resolution.x / u.resolution.y;
uv *= Zoom;
float t = u.time;
if(Warp) {
vec2 oldUV = uv;
uv = pow(abs(uv), sin(t * vec2(WarpX, WarpY)) * .35f + 1.2f) * sign(oldUV);
}
float bandRadius = roundx(length(uv), BandSpacing);
vec3 bandID = vec3(
rand(bandRadius + 0),
rand(bandRadius + 1),
rand(bandRadius + 2)
);
float distToLine = sdSegment1D(
length(uv),
bandRadius - (LineSize * .5f),
bandRadius + (LineSize * .5f)
);
float bandA = dtoa(distToLine, 400.f);// alpha around this band.
float bandSpeed = .1f / max(0.05f, bandRadius);// outside = slower
float r = -t + PI2 * bandID.x;
r *= bandSpeed;
uv *= rot(uv, r);
float angle = mod(atan2(uv.x, uv.y), PI2);// angle, animated
float arcLength = bandRadius * angle;
float color = sign(mod(arcLength, 2 * SegmentLength) - SegmentLength);
return vec4(vec3(bandID * color * bandA), 1);
}
[[.imgui::range_float { .5, 8 }]] float Zoom = 1.5;
[[.imgui::range_float { .1, .5 }]] float BandSpacing = .05;
[[.imgui::range_float { 0, .5 }]] float LineSize = .008;
[[.imgui::range_float { 0, 6 }]] float SegmentLength = .5;
[[.imgui::range_float { 0, 1 }]] float WarpX = .2;
[[.imgui::range_float { 0, 1 }]] float WarpY = .7;
bool Warp = true;
};
////////////////////////////////////////////////////////////////////////////////
// https://www.shadertoy.com/view/wsVyzw
struct [[
.imgui::title="Star Amplitude Modulation",
.imgui::url="https://www.shadertoy.com/view/wsVyzw"
]] modulation_t {
// Signed distance to a n-star polygon with external angle en.
float sdStar(vec2 p, float r, int n, float m) {
float an = M_PIf32 / n;
float en = M_PIf32 / m;
vec2 acs(cos(an), sin(an));
vec2 ecs(cos(en), sin(en));
// reduce to first sector.
float bn = mod(atan2(p.x, p.y), 2 * an) - an;
p = length(p) * vec2(cos(bn), abs(sin(bn)));
// line sdf
p -= r * acs;
p += ecs * clamp(-dot(p, ecs), 0.0f, r * acs.y / ecs.y);
return length(p) * sign(p.x);
}
float sdShape(vec2 uv, float t) {
float angle = -t * StarRotationSpeed;
return sdStar(rot(uv, angle), StarSize, StarPoints, StarWeight);
}
vec3 dtoa(float d, vec3 amount) {
return 1 / clamp(d * amount, 1, amount);
}
// https://www.shadertoy.com/view/3t23WG
// Distance to y(x) = a + b*cos(cx+d)
float udCos(vec2 p, float a, float b, float c, float d) {
p = c * (p - vec2(d, a));
// Reduce to principal half cycle.
p.x = mod(p.x, 2 * M_PIf32);
if(p.x > M_PIf32)
p.x = 2 * M_PIf32 - p.x;
// Fine zero of derivative (minimize distance).
float xa = 0, xb = 2 * M_PIf32;
for(int i = 0; i < 7; ++i) { // bisection, 7 bits more or less.
float x = .5f * (xa + xb);
float si = sin(x);
float co = cos(x);
float y = x - p.x + b * c * si * (p.y - b * c * co);
if(y < 0)
xa = x;
else
xb = x;
}
float x = .5f * (xa + xb);
for(int i = 0; i < 4; ++i) { // Newton-Raphson, 28 bits more or less.
float si = sin(x);
float co = cos(x);
float f = x - p.x + b * c * (p.y * si - b * c * si * co);
float df = 1 + b * c * (p.y * co - b * c * (2 * co * co - 1));
x = x - f / df;
}
// Compute distance.
vec2 q(x, b * c * cos(x));
return length(p - q) / c;
}
vec4 render(vec2 frag_coord, shadertoy_uniforms_t u) {
vec2 N = frag_coord / u.resolution - .5f;
vec2 uv = N;
uv.x *= u.resolution.x / u.resolution.y;
uv *= Zoom;
float t = u.time * PhaseSpeed;
vec2 uvsq = uv;
float a = sdShape(uv, t);
float sh = mix(100.f, 1000.f, Sharpness);
float a2 = 1.5;
for(int i = -3; i <= 3; ++i) {
vec2 uvwave(uv.x, uv.y + i * WaveSpacing);
float b = smoothstep(1.f, -1.f, a) * WaveAmp + WaveAmpOffset;
a2 = min(a2, udCos(uvwave, 0.f, b, WaveFreq, t));
}
vec3 o = dtoa(mix(a2, a-LineWeight + 4, .03f), sh * Tint);
if(!InvertColors)
o = 1 - o;
o *= 1 - dot(N, N * 2);
return vec4(saturate(o), 1);
}
[[.imgui::range_float { 1, 10 }]] float Zoom = 5;
[[.imgui::range_float { 0, 10 }]] float LineWeight = 4.3;
[[.imgui::range_int { 2, 10 }]] int StarPoints = 5;
[[.imgui::range_float { .1, 3 }]] float Sharpness = .2;
[[.imgui::range_float { -5, 5 }]] float StarRotationSpeed = -.5;
[[.imgui::range_float { 0, 5 }]] float StarSize = 2;
[[.imgui::range_float { 2, 10 }]] float StarWeight = 6;
[[.imgui::range_float { 0, 4 }]] float WaveSpacing = .8;
[[.imgui::range_float { 0, 5 }]] float WaveAmp = 2;
[[.imgui::range_float { 0, 50 }]] float WaveFreq = 25;
[[.imgui::range_float { -2, 2 }]] float PhaseSpeed = .33;
[[.imgui::range_float { -.1, .1 }]] float WaveAmpOffset = .01;
[[.imgui::color3]] vec3 Tint = vec3(.15, .5, .8);
bool InvertColors = true;
};
////////////////////////////////////////////////////////////////////////////////
// Keep up little square
struct [[
.imgui::title="Keep Up Little Square",
.imgui::url="https://www.shadertoy.com/view/wlsXD2"
]] keep_up_square_t {
vec3 hash32(vec2 p) {
vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yxz + 19.19f);
return fract((p3.xxy + p3.yzz) * p3.zyx);
}
float dtoa(float d, float amount) {
return 1 / clamp(d * amount, 1.f, amount);
}
float sdSquare(vec2 p, vec2 pos, vec2 origin, float a, float s) {
vec2 d = abs((rot(p - pos, -a)) - origin) - s;
return min(max(d.x, d.y), 0.f) + length(max(d, 0.f));
}
// returns { x:orig, y:height, z:position 0-1 within this cell }
vec3 cell(float x) {
float pos = fract(x / XScale);
float orig = (x / XScale - pos) * XScale;
float h = hash32(vec2(orig)).r;
return vec3(orig, h * YScale, pos);
}
vec3 scene(vec2 N, vec2 uv, float t, float xpos, float xcam) {
vec3 sqCellPrev = cell(xpos - XScale);
// Ground.
vec3 bigCellThis = cell(uv.x);
vec3 bigCellNext = cell(uv.x + XScale);
float bigHeightThis = mix(bigCellThis.y, bigCellNext.y, bigCellThis.z);
float sd = uv.y - bigHeightThis;
// Walking square; interpolate between positions.
vec3 sqCellThis = cell(xpos);
vec3 sqCellNext = cell(xpos + XScale);
float aThis = atan2(sqCellPrev.y - sqCellThis.y,
sqCellPrev.x - sqCellThis.x);
float aNext = atan2(sqCellThis.y - sqCellNext.y,
sqCellThis.x - sqCellNext.x) - M_PIf32 / 2;
if(aNext > aThis + M_PIf32) aNext -= 2 * M_PIf32;
if(aNext < aThis - M_PIf32) aNext += 2 * M_PIf32;
float szThis = distance(sqCellPrev.xy, sqCellThis.xy);
float szNext = distance(sqCellThis.xy, sqCellNext.xy);
float param = pow(sqCellNext.z, Sluggishness);
float asq = mix(aThis, aNext, param);
float sz = mix(szThis, szNext, param);
float sdsq = sdSquare(uv, sqCellThis.xy, sz * vec2(-.5f, .5f),
asq + M_PIf32, .5f * sz);
vec3 o { };
vec2 uvtemp = uv;
for(int i = 1; i <= 9; ++i) {
uvtemp.x -= xpos;
uvtemp *= vec2(2, 1.8);
uvtemp.y -= .3;
uvtemp.x += xpos + 1000;
vec3 cellThis = cell(uvtemp.x);
vec3 cellNext = cell(uvtemp.x + XScale);
float heightThis = mix(cellThis.y, cellNext.y, cellThis.z);
float sd = uvtemp.y - heightThis;
o = max(o, dtoa(sd, 1000) * .2f / i);
float amt = 25 + heightThis * 500;
o = max(o, dtoa(abs(sd) + .01f, amt) * .4f / i);
}
o += .8f - uv.y * 1.1f;
o.g *= o.r;
o.r *= .6f;
o.b += N.y;
// square
float alphasq = dtoa(sdsq, 1000);
o = mix(o, SquareColor, alphasq);
// ground
float alphagr = dtoa(sd, 300);
o = mix(o, GroundColor, alphagr);
// snow
float alphasn = dtoa(abs(sd + .01f), 25 + bigHeightThis * 500);
o = mix(o, SnowColor, alphasn);
return o;
}
vec4 render(vec2 frag_coord, shadertoy_uniforms_t u) {
vec2 N = frag_coord / u.resolution - .5f;
N.x *= u.resolution.x / u.resolution.y;
vec2 uv = N;
float t = .16f * u.time + 100;
float xpos = t * XSpeed;
float xcam = sin(9 * t) * CamShakiness;
uv = vec2(xpos + xcam, .3f) + Zoom * N;
// Calc scene twice and motion blur.
vec3 sqCellPrev = cell(xpos - XScale);
float bounce = abs(sin(sqCellPrev.z * 26)) *
pow(1 - sqCellPrev.z, .7f) * Bounce;
vec3 o1 = scene(N, uv + vec2(0, bounce), t, xpos, xcam);
vec3 o2 = scene(N, uv , t, xpos, xcam);
vec3 o = .5f * (o1 + o2);
o.b *= .9;
o += (hash32(frag_coord + t) - .5f) * FilmGrain;
o *= 1.1f - dot(N, N);
o = saturate(o);
return vec4(o, 1);
}
[[.imgui::range_float { .1, 5 }]] float Zoom = 1.5;
[[.imgui::range_float { .1, 1 }]] float XScale = .3;
[[.imgui::range_float { 0, .5 }]] float YScale = .2;
[[.imgui::range_float { 0, .1 }]] float Bounce = .01;
[[.imgui::range_float { -3, 3 }]] float XSpeed = 1;
[[.imgui::range_float { 0, .2 }]] float CamShakiness = .1;
[[.imgui::range_float { 1, 10 }]] float Sluggishness = 7;
[[.imgui::range_float { .01, .5 }]] float FilmGrain = .15;
[[.imgui::color3]] vec3 SquareColor = vec3(0.9, 0.1, 0.1);
[[.imgui::color3]] vec3 GroundColor = vec3(.3, .6, .1);
[[.imgui::color3]] vec3 SnowColor = vec3(0.8, 0.9, 1.0);
};
////////////////////////////////////////////////////////////////////////////////
struct [[
.imgui::title="Metallic Paint Stir",
.imgui::url="https://www.shadertoy.com/view/3sG3Rd"
]] paint_t {
vec4 render(vec2 frag_coord, shadertoy_uniforms_t u) {
constexpr float PI2 = 2 * M_PIf32;
vec2 N = frag_coord / u.resolution - .5f;
vec2 uv = N;
uv.x *= u.resolution.x / u.resolution.y;
uv *= Zoom;
float t = .2f * u.time;
// get radius and angle
float l = sqrt(length(uv));
float a = atan2(uv.x, uv.y) + sin(l * PI2) * PI2;
// distort uv by length, animated wave over time
float ex = mix(MinPow, MaxPow, .5f * sin(l*PI2+a+t*PI2) + .5f);
uv = sign(uv)*pow(abs(uv), vec2(ex));
float d = abs(fract(length(uv)-t) - .5f);// dist to ring centers
float c = 1 / max(((2 - l) * 6) * d, .1f);// dist to grayscale amt
vec4 o(c);
vec3 col = vec3(
// generate correlated colorants 0-1 from length, angle, exponent
saturate(l * l * l),
.5f * sin(a) + .5f,
(ex - MinPow) / (MaxPow - MinPow)
);
col = 1 - mix(vec3(col.r + col.g + col.b) / 3, col, Saturation);
o.rgb *= col;
o *= Fade - l;// fade edges (vignette)
return o;
}
[[.imgui::range_float { .1, 10 }]] float Zoom = 4.3;
[[.imgui::range_float { 0, 4 }]] float MinPow = .6;
[[.imgui::range_float { 0, 4 }]] float MaxPow = 2;
[[.imgui::range_float { 0, 4 }]] float Fade = 1.8;
[[.imgui::range_float { 0, 1 }]] float Saturation = .5;
};
////////////////////////////////////////////////////////////////////////////////
struct [[
.imgui::title="Menger Journey",
.imgui::url="https://www.shadertoy.com/view/Mdf3z7"
]] menger_journey_t {
vec2 rotate(vec2 v, float a) {
return vec2(cos(a) * v.x + sin(a) * v.y, -sin(a) * v.x + cos(a) * v.y);
}
// Two light sources. No specular
vec3 getLight(vec3 color, vec3 normal, vec3 dir) {
vec3 light_dir = normalize(vec3(1, 1, 1));
vec3 light_dir2 = normalize(vec3(1, -1, 1));
float diffuse = max(0.f, dot(-normal, light_dir));
float diffuse2 = max(0.f, dot(-normal, light_dir2));
return Diffuse * color * (diffuse * LightColor + diffuse2 * LightColor2);
}
// For more info on KIFS, see:
// http://www.fractalforums.com/3d-fractal-generation/kaleidoscopic-%28escape-time-ifs%29/
float DE(vec3 z, float angle) {
// Folding 'tiling' of 3D space;
z = abs(1 - mod(z, 2.f));
const vec3 Offset(0.92858,0.92858,0.32858);
float d = 1000.0;
for (int n = 0; n < NumIterations; n++) {
z.xy = rotate(z.xy, angle);
z = abs(z);
if(z.x < z.y) z.xy = z.yx;
if(z.x < z.z) z.xz = z.zx;
if(z.y < z.z) z.yz = z.zy;
z = Scale * z - Offset * (Scale - 1);
if(z.z < -0.5f * Offset.z * (Scale - 1))
z.z += Offset.z * (Scale - 1);
d = min(d, length(z) * pow(Scale, -n - 1));
}
return d - 0.001f;