/
deco-effects.cpp
2619 lines (2025 loc) · 89.3 KB
/
deco-effects.cpp
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Scott Moreau <oreaus@gmail.com>
* - Ported weston-smoke to compute shader set
* Copyright (c) 2024 Ilia Bozhinov <ammen99@gmail.com>
* - Awesome optimizations
* Copyright (c) 2024 Andrew Pliatsikas <futurebytestore@gmail.com>
* - Ported effect shaders to compute
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <wayfire/debug.hpp>
#include "deco-effects.hpp"
#include "smoke-shaders.hpp"
namespace wf
{
namespace pixdecor
{
static std::string stitch_smoke_shader(const std::string& source)
{
return smoke_header + source + effect_run_for_region_main;
}
// ported from https://www.shadertoy.com/view/WdXBW4
static const char *render_source_clouds =
R"(
#version 320 es
precision highp float;
precision highp image2D;
layout(binding = 0, rgba32f) uniform writeonly image2D out_tex;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(location = 1) uniform int title_height;
layout(location = 2) uniform int border_size;
layout(location = 5) uniform int width;
layout(location = 6) uniform int height;
layout(location = 7) uniform int radius;
layout(location = 9) uniform float current_time;
float cloudscale=2.1; // Added cloudscale parameter
const mat2 m = mat2(1.6, 1.2, -1.2, 1.6);
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 b = a - o + K2;
vec2 c = a - 1.0 + 2.0 * K2;
vec3 h = max(0.5 - vec3(dot(a, a), dot(b, b), dot(c, c)), 0.0);
vec3 n = h * h * h * h * vec3(dot(a, hash(i + 0.0)), dot(b, hash(i + o)), dot(c, hash(i + 1.0)));
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.4;
}
return total;
}
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
int x = pos.x;
int y = pos.y;
// Check if the pixel should be drawn
if (x >= border_size && x <= (width - 1) - border_size && y >= title_height && y <= (height - 1) - border_size)
{
return;
}
vec2 uv = vec2(pos) / vec2(gl_NumWorkGroups.x * gl_WorkGroupSize.x, gl_NumWorkGroups.y * gl_WorkGroupSize.y);
float time = 0.003 * current_time; // Time variable for animation
float cloudPattern1 = fbm(uv * 10.0 * cloudscale + time);
float cloudPattern2 = fbm(uv * 5.0 * cloudscale + time);
float cloudPattern3 = fbm(uv * 3.0 * cloudscale + time);
// Combine different cloud patterns with different weights
float cloudPattern = 0.5 * cloudPattern1 + 0.3 * cloudPattern2 + 0.2 * cloudPattern3;
// Ridge noise shape
float ridgeNoiseShape = 0.0;
uv *= cloudscale * 1.1; // Adjust scale
float weight = 0.8;
for (int i = 0; i < 8; i++) {
ridgeNoiseShape += abs(weight * noise(uv));
uv = m * uv + time;
weight *= 0.7;
}
// Noise shape
float noiseShape = 0.0;
uv = vec2(pos) / vec2(gl_NumWorkGroups.x * gl_WorkGroupSize.x, gl_NumWorkGroups.y * gl_WorkGroupSize.y);
uv *= cloudscale * 1.1; // Adjust scale
weight = 0.7;
for (int i = 0; i < 8; i++) {
noiseShape += weight * noise(uv);
uv = m * uv + time;
weight *= 0.6;
}
noiseShape *= ridgeNoiseShape + noiseShape;
// Noise color
float noiseColor = 0.0;
uv = vec2(pos) / vec2(gl_NumWorkGroups.x * gl_WorkGroupSize.x, gl_NumWorkGroups.y * gl_WorkGroupSize.y);
uv *= 2.0 * cloudscale; // Adjust scale
weight = 0.4;
for (int i = 0; i < 7; i++) {
noiseColor += weight * noise(uv);
uv = m * uv + time;
weight *= 0.6;
}
// Noise ridge color
float noiseRidgeColor = 0.0;
uv = vec2(pos) / vec2(gl_NumWorkGroups.x * gl_WorkGroupSize.x, gl_NumWorkGroups.y * gl_WorkGroupSize.y);
uv *= 3.0 * cloudscale; // Adjust scale
weight = 0.4;
for (int i = 0; i < 7; i++) {
noiseRidgeColor += abs(weight * noise(uv));
uv = m * uv + time;
weight *= 0.6;
}
noiseColor += noiseRidgeColor;
// Sky tint
float skytint = 0.5;
vec3 skyColour1 = vec3(0.1, 0.2, 0.3);
vec3 skyColour2 = vec3(0.4, 0.7, 1.0);
vec3 skycolour = mix(skyColour1, skyColour2, smoothstep(0.4, 0.6, uv.y));
// Cloud darkness
float clouddark = 0.5;
// Cloud Cover, Cloud Alpha
float cloudCover = 0.01;
float cloudAlpha = 2.0;
// Movement effect
uv = uv + time;
// Use a bright color for clouds
vec3 cloudColor = vec3(1.0, 1.0, 1.0); ; // Bright white color for clouds
// Mix the cloud color with the background, considering darkness, cover, and alpha
vec3 finalColor = mix(skycolour, cloudColor * clouddark, cloudPattern + noiseShape + noiseColor) * (1.0 - cloudCover) + cloudColor * cloudCover;
finalColor = mix(skycolour, finalColor, cloudAlpha);
imageStore(out_tex, pos, vec4(finalColor, 1.0));
}
)";
// ported from https://github.com/keijiro/ShaderSketches/blob/master/Fragment/Dots3.glsl
static const char *render_source_halftone =
R"(
#version 310 es
precision highp float;
precision highp image2D;
layout(binding = 0, rgba32f) writeonly uniform highp image2D out_tex;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(location = 1) uniform int title_height;
layout(location = 2) uniform int border_size;
layout(location = 5) uniform int width;
layout(location = 6) uniform int height;
layout(location = 7) uniform int radius;
layout(location = 9) uniform float time;
const vec2 resolution = vec2(1280.0, 720.0);
const float timeFactor = 0.025;
float rand(vec2 uv) {
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453);
}
vec2 rotate(vec2 p, float theta) {
vec2 sncs = vec2(sin(theta), cos(theta));
return vec2(p.x * sncs.y - p.y * sncs.x, p.x * sncs.x + p.y * sncs.y);
}
float swirl(vec2 coord, float t) {
float l = length(coord) / resolution.x;
float phi = atan(coord.y, coord.x + 1e-6);
return sin(l * 10.0 + phi - t * 4.0) * 0.5 + 0.5;
}
float halftone(vec2 coord, float angle, float t, float amp) {
coord -= resolution * 0.5;
float size = resolution.x / (60.0 + sin(time * timeFactor * 0.5) * 50.0);
vec2 uv = rotate(coord / size, angle / 180.0 * 3.14);
vec2 ip = floor(uv); // column, row
vec2 odd = vec2(0.5 * mod(ip.y, 2.0), 0.0); // odd line offset
vec2 cp = floor(uv - odd) + odd; // dot center
float d = length(uv - cp - 0.5) * size; // distance
float r = swirl(cp * size, t) * size * 0.5 * amp; // dot radius
return 1.0 - clamp(d - r, 0.0, 1.0);
}
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
// Extract x and y coordinates
int x = pos.x;
int y = pos.y;
// Check if the pixel should be drawn
if (x >= border_size && x <= (width - 1) - border_size && y >= title_height && y <= (height - 1) - border_size)
{
return;
}
vec3 c1 = 1.0 - vec3(1.0, 0.0, 0.0) * halftone(vec2(pos), 0.0, time * timeFactor * 1.00, 0.7);
vec3 c2 = 1.0 - vec3(0.0, 1.0, 0.0) * halftone(vec2(pos), 30.0, time * timeFactor * 1.33, 0.7);
vec3 c3 = 1.0 - vec3(0.0, 0.0, 1.0) * halftone(vec2(pos), -30.0, time * timeFactor * 1.66, 0.7);
vec3 c4 = 1.0 - vec3(1.0, 1.0, 1.0) * halftone(vec2(pos), 60.0, time * timeFactor * 2.13, 0.4);
// Output the final color
imageStore(out_tex, pos, vec4(c1 * c2 * c3 * c4, 1.0));
}
)";
// ported from https://www.shadertoy.com/view/WdjGRc
static const char *render_source_lava =
R"(
#version 320 es
precision highp float;
precision highp image2D;
layout(binding = 0, rgba32f) uniform readonly image2D in_tex;
layout(binding = 0, rgba32f) uniform writeonly image2D out_tex;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(location = 1) uniform int title_height;
layout(location = 2) uniform int border_size;
layout(location = 5) uniform int width;
layout(location = 6) uniform int height;
layout(location = 7) uniform int radius;
layout(location = 9) uniform float current_time;
vec3 effect(float speed, vec2 uv, float time, float scale) {
float t = mod(time * 0.005, 6.0);
float rt = 0.00000000000001 * sin(t * 0.45);
mat2 m1 = mat2(cos(rt), -sin(rt), -sin(rt), cos(rt));
vec2 uva = uv * m1 * scale;
float irt = 0.005 * cos(t * 0.05);
mat2 m2 = mat2(sin(irt), cos(irt), -cos(irt), sin(irt));
for (int i = 1; i < 40; i += 1) {
float it = float(i);
uva *= m2;
uva.y += -1.0 + (0.6 / it) * cos(t + it * uva.x + 0.5 * it) * float(mod(it, 0.5) == 0.0);
uva.x += 1.0 + (0.5 / it) * cos(t + it * uva.y * 0.1 / 5.0 + 0.5 * (it + 15.0)); // Adjust the scaling factor for y-coordinate
}
float n = 0.5;
float r = n + n * sin(4.0 * uva.x + t);
float gb = n + n * sin(3.0 * uva.y);
return vec3(r, gb * 0.8 * r, gb * r);
}
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
vec2 uv = vec2(pos) / vec2(1000, 2000);
uv = 2.0 * uv - 1.0;
uv *= (10.3 + 0.1 * sin(current_time * 0.01));
// Extract x and y coordinates
int x = pos.x;
int y = pos.y;
// Check if the pixel should be drawn
if (x >= border_size && x <= (width - 1) - border_size && y >= title_height && y <= (height - 1) - border_size)
{
return;
}
// uv.y -= current_time * 0.013;
//uv.x -= current_time * 0.026;
vec3 col = effect(0.001, uv, current_time, 0.5);
// col += effect(0.5, uv * 3.0, 2.0 * current_time + 10.0, 0.5) * 0.3;
// col += effect(0.5, sin(current_time * 0.01) * uv * 2.0, 2.0 * current_time + 10.0, 0.5) * 0.1;
// Output the final color to the output texture
imageStore(out_tex, pos, vec4(col, 1.0));
}
)";
// ported from https://github.com/keijiro/ShaderSketches/blob/master/Fragment/Eyes2.glsl
static const char *render_source_pattern =
R"(
#version 310 es
precision highp float;
precision highp image2D;
layout(binding = 0, rgba32f) writeonly uniform highp image2D out_tex;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(location = 1) uniform int title_height;
layout(location = 2) uniform int border_size;
layout(location = 5) uniform int width;
layout(location = 6) uniform int height;
layout(location = 7) uniform int radius;
layout(location = 9) uniform float time;
float rand(vec2 uv)
{
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453);
}
vec3 hue2rgb(float h)
{
h = fract(h) * 6.0 - 2.0;
return clamp(vec3(abs(h - 1.0) - 1.0, 2.0 - abs(h), 2.0 - abs(h - 2.0)), 0.0, 1.0);
}
vec3 eyes(vec2 coord, vec2 resolution)
{
const float pi = 3.141592;
float t = 0.4 * time * 0.05;
float aspectRatio = resolution.x / resolution.y;
float div = 20.0;
float sc = 30.0;
vec2 p = (coord - resolution / 2.0) / sc - 0.5;
// center offset
float dir = floor(rand(floor(p) + floor(t) * 0.11) * 4.0) * pi / 2.0;
vec2 offs = vec2(sin(dir), cos(dir)) * 0.6;
offs *= smoothstep(0.0, 0.1, fract(t));
offs *= smoothstep(0.4, 0.5, 1.0 - fract(t));
// circles
float l = length(fract(p) + offs - 0.5);
float rep = sin((rand(floor(p)) * 2.0 + 2.0) * t) * 4.0 + 5.0;
float c = (abs(0.5 - fract(l * rep + 0.5)) - 0.25) * sc / rep;
// grid lines
vec2 gr = (abs(0.5 - fract(p + 0.5)) - 0.05) * sc;
c = clamp(min(min(c, gr.x), gr.y), 0.0, 1.0);
return hue2rgb(rand(floor(p) * 0.3231)) * c;
}
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
vec2 resolution = vec2(float(width), float(height));
int x = pos.x;
int y = pos.y;
// Check if the pixel should be drawn
if (x >= border_size && x <= (width - 1) - border_size && y >= title_height && y <= (height - 1) - border_size)
{
return;
}
vec3 color = eyes(vec2(pos), resolution);
// Output the final color
imageStore(out_tex, pos, vec4(color, 1.0));
}
)";
// original (by phodius)
static const char *render_source_hex =
R"(
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(binding = 0, rgba32f) writeonly uniform highp image2D OutputImage;
layout(location = 1) uniform int title_height;
layout(location = 2) uniform int border_size;
layout(location = 5) uniform int width;
layout(location = 6) uniform int height;
layout(location = 7) uniform int radius;
layout(location = 9) uniform float iTime;
vec2 iResolution;
float rand(vec2 co) {
return fract(sin(dot(co.xy, vec2(12.9898, 4.1414))) * 43758.5453);
}
// Hexagon function
vec4 hexagon(vec2 p)
{
vec2 q = vec2(p.x * 2.0 * 0.5773503, p.y + p.x * 0.5773503);
vec2 pi = floor(q);
vec2 pf = fract(q);
float v = mod(pi.x * 9.0, 0.0);
float ca = step(1.0, v);
float cb = step(2.0, v);
vec2 ma = step(pf.xy, pf.yx);
float e = 0.0;
float f = length((fract(p) - 10.5) * vec2(1.0, 0.85));
return vec4(pi + ca - cb * ma, e, f);
}
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
int x = pos.x;
int y = pos.y;
// Check if the pixel should be drawn
if (x >= border_size && x <= (width - 1) - border_size && y >= title_height && y <= (height - 1) - border_size)
{
return;
}
vec2 uv = (vec2(pos) + 0.5) / float(width);
// Generate a random value
float randVal = rand(uv);
// Apply hexagon logic
vec2 p = (-float(width) + 2.0 * vec2(pos)) / float(height);
vec4 h = hexagon(40.0 * p + vec2(0.5 * iTime * 0.125));
float col = 0.01 + 0.15 * rand(vec2(h.xy)) * 1.0;
col *= 4.3 + 0.15 * sin(10.0 * h.z);
// Use hardcoded colors for shades with gradient
vec3 shadeColor1 = vec3(0.1, 0.1, 0.1) + 1.1 * col * h.z; // Dark gray with gradient
vec3 shadeColor2 = vec3(0.4, 0.4, 0.4) + 1.1 * col; // Gray with gradient
vec3 shadeColor3 = vec3(0.9, 0.9, 0.9) + 0.1 * col; // Light gray with gradient
// Use different hardcoded colors based on the random value
vec3 finalColor = mix(shadeColor1, mix(shadeColor2, shadeColor3, randVal), col);
// Use imageStore instead of writing to buffer
imageStore(OutputImage, pos, vec4(finalColor, 1.0));
}
)";
// ported from https://github.com/keijiro/ShaderSketches/blob/master/Fragment/Zebra.glsl
static const char *render_source_zebra =
R"(
#version 310 es
precision highp float;
precision highp image2D;
layout(binding = 0, rgba32f) writeonly uniform highp image2D out_tex;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(location = 1) uniform int title_height;
layout(location = 2) uniform int border_size;
layout(location = 5) uniform int width;
layout(location = 6) uniform int height;
layout(location = 7) uniform int radius;
layout(location = 9) uniform float time;
const float resolutionY = 720.0; // Set to constant resolution of 720p
const float pi = 3.14159265359;
const float timeFactor = 0.05; // Adjust this factor to control the speed of the animation
float rand(vec2 uv)
{
return fract(sin(dot(uv, vec2(12.9898, 78.233))) * 43758.5453);
}
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
int x = pos.x;
int y = pos.y;
// Check if the pixel should be drawn
if (x >= border_size && x <= (width - 1) - border_size && y >= title_height && y <= (height - 1) - border_size)
{
return;
}
float size = resolutionY / 10.0; // cell size in pixel
vec2 p1 = vec2(pos) / size; // normalized pos
vec2 p2 = fract(p1) - 0.5; // relative pos from cell center
// random number
float rnd = dot(floor(p1), vec2(12.9898, 78.233));
rnd = fract(sin(rnd) * 43758.5453);
// rotation matrix
float phi = rnd * pi * 2.0 + time * 0.4 * timeFactor;
mat2 rot = mat2(cos(phi), -sin(phi), sin(phi), cos(phi));
vec2 p3 = rot * p2; // apply rotation
p3.y += sin(p3.x * 5.0 + time * 2.0 * timeFactor) * 0.12; // wave
float rep = fract(rnd * 13.285) * 8.0 + 2.0; // line repetition
float gr = fract(p3.y * rep + time * 0.8 * timeFactor); // repeating gradient
// make antialiased line by saturating the gradient
float c = clamp((0.25 - abs(0.5 - gr)) * size * 0.75 / rep, 0.0, 1.0);
c *= max(0.0, 1.0 - length(p2) * 0.6); // darken corners
vec2 bd = (0.5 - abs(p2)) * size - 2.0; // border lines
c *= clamp(min(bd.x, bd.y), 0.0, 1.0);
// Output the final color
imageStore(out_tex, pos, vec4(c, c, c, 1.0));
}
)";
// ported from https://www.shadertoy.com/view/dlGfWV
static const char *render_source_neural_network =
R"(
#version 310 es
precision highp float;
precision highp image2D;
//layout(binding = 0, rgba32f) readonly uniform highp image2D neural_network_tex; // Use binding point 0
layout(binding = 0, rgba32f) writeonly uniform highp image2D out_tex; // Use binding point 1
//layout(binding = 0, rgba32f) writeonly uniform highp image2D out_tex;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(location = 1) uniform int title_height;
layout(location = 2) uniform int border_size;
layout(location = 5) uniform int width;
layout(location = 6) uniform int height;
layout(location = 7) uniform int radius;
layout(location = 9) uniform float time;
uniform vec2 iResolution;
uniform float iTime;
mat2 rotate2D(float r) {
float c = cos(r);
float s = sin(r);
return mat2(c, -s, s, c);
}
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
int x = pos.x;
int y = pos.y;
// Check if the pixel should be drawn
if (x >= border_size && x <= (width - 1) - border_size && y >= title_height && y <= (height - 1) - border_size)
{
return;
}
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = (vec2(pos) - 0.5 * float(gl_NumWorkGroups.x)) / float(gl_WorkGroupSize.x);
// Scale the uv coordinates by a factor of 8
uv *= 0.05;
vec3 col = vec3(0);
float t = 0.05 * time;
vec2 n = vec2(0), q;
vec2 N = vec2(0);
vec2 p = uv + sin(t * 0.1) / 10.0;
float S = 10.0;
mat2 m = rotate2D(1.0);
for (float j = 0.0; j < 30.0; j++) {
p *= m;
n *= m;
q = p * S + j + n + t;
n += sin(q);
N += cos(q) / S;
S *= 1.2;
}
col = vec3(1, 2, 4) * pow((N.x + N.y + 0.2) + 0.005 / length(N), 2.1);
imageStore(out_tex, pos, vec4(col, 1.0));
}
)";
// Ported from https://www.shadertoy.com/view/llSyDh
static const char *render_source_hexagon_maze =
R"(
#version 320 es
precision highp float;
precision highp int;
precision highp image2D;
//layout (location = 0) out vec4 fragColor;
layout (binding = 0, rgba32f) writeonly uniform image2D fragColor;
layout(location = 1) uniform int title_height;
layout(location = 2) uniform int border_size;
layout(location = 5) uniform int width;
layout(location = 6) uniform int height;
layout(location = 7) uniform int radius;
layout(location = 9) uniform float iTime;
uniform vec2 iResolution;
layout(local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
// Interlaced variation - Interesting, but patched together in a hurry.
//#define INTERLACING
// A quick hack to get rid of the winding overlay - in order to show the maze only.
//#define MAZE_ONLY
// Helper vector. If you're doing anything that involves regular triangles or hexagons, the
// 30-60-90 triangle will be involved in some way, which has sides of 1, sqrt(3) and 2.
const vec2 s = vec2(1, 1.7320508);
// Standard vec2 to float hash - Based on IQ's original.
float hash21(vec2 p){ return fract(sin(dot(p, vec2(141.173, 289.927)))*43758.5453); }
// Standard 2D rotation formula.
mat2 r2(in float a){ float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
// The 2D hexagonal isosuface function: If you were to render a horizontal line and one that
// slopes at 60 degrees, mirror, then combine them, you'd arrive at the following.
float hex(in vec2 p){
p = abs(p);
// Below is equivalent to:
//return max(p.x*.5 + p.y*.866025, p.x);
return max(dot(p, s*.5), p.x); // Hexagon.
}
// This function returns the hexagonal grid coordinate for the grid cell, and the corresponding
// hexagon cell ID - in the form of the central hexagonal point. That's basically all you need to
// produce a hexagonal grid.
//
// When working with 2D, I guess it's not that important to streamline this particular function.
// However, if you need to raymarch a hexagonal grid, the number of operations tend to matter.
// This one has minimal setup, one "floor" call, a couple of "dot" calls, a ternary operator, etc.
// To use it to raymarch, you'd have to double up on everything - in order to deal with
// overlapping fields from neighboring cells, so the fewer operations the better.
vec4 getHex(vec2 p){
// The hexagon centers: Two sets of repeat hexagons are required to fill in the space, and
// the two sets are stored in a "vec4" in order to group some calculations together. The hexagon
// center we'll eventually use will depend upon which is closest to the current point. Since
// the central hexagon point is unique, it doubles as the unique hexagon ID.
vec4 hC = floor(vec4(p, p - vec2(.5, 1))/s.xyxy) + .5;
// Centering the coordinates with the hexagon centers above.
vec4 h = vec4(p - hC.xy*s, p - (hC.zw + .5)*s);
// Nearest hexagon center (with respect to p) to the current point. In other words, when
// "h.xy" is zero, we're at the center. We're also returning the corresponding hexagon ID -
// in the form of the hexagonal central point. Note that a random constant has been added to
// "hC.zw" to further distinguish it from "hC.xy."
//
// On a side note, I sometimes compare hex distances, but I noticed that Iomateron compared
// the Euclidian version, which seems neater, so I've adopted that.
return dot(h.xy, h.xy)<dot(h.zw, h.zw) ? vec4(h.xy, hC.xy) : vec4(h.zw, hC.zw + vec2(.5, 1));
}
// Dot pattern.
float dots(in vec2 p){
p = abs(fract(p) - .5);
return length(p); // Circles.
//return (p.x + p.y)/1.5 + .035; // Diamonds.
//return max(p.x, p.y) + .03; // Squares.
//return max(p.x*.866025 + p.y*.5, p.y) + .01; // Hexagons.
//return min((p.x + p.y)*.7071, max(p.x, p.y)) + .08; // Stars.
}
// Distance field for the arcs. I think it's called poloidal rotation, or something like that.
float dfPol(vec2 p){
return length(p); // Circular arc.
// There's no rule that says the arcs have to be rounded. Here's a hexagonal one.
//return hex(p);
// Dodecahedron.
//return max(hex(p), hex(r2(3.14159/6.)*p));
// Triangle.
//return max(abs(p.x)*.866025 - p.y, p.y);
}
// Truchet pattern distance field.
float df(vec2 p, float dir){
// Weird UV coordinates. The first entry is the Truchet distance field itself,
// and the second is the polar angle of the arc pixel. The extra ".1" is just a bit
// of mutational scaling, or something... I can't actually remember why it's there. :)
vec2 uv = vec2(p.x + .1, p.y);//*vec2(1, 1); // Scaling.
// A checkered dot pattern. At present the pattern needs to have flip symmetry about
// the center, but I'm pretty sure regular textures could be applied with a few
// minor changes. Due to the triangular nature of the Truchet pattern, factors of "3"
// were necessary, but factors of "1.5" seemed to work too. Hence the "4.5."
return min(dots(uv*4.5), dots(uv*4.5 + .5)) - .3;
}
// Polar coordinate of the arc pixel.
float getPolarCoord(vec2 q, float dir){
// The actual animation. You perform that before polar conversion.
q = r2((iTime*0.05)*dir)*q;
// Polar angle.
const float aNum = 1.;
float a = atan(q.y, q.x);
// Wrapping the polar angle.
return mod(a/3.14159, 2./aNum) - 1./aNum;
}
void main() {
vec2 iResolution = vec2(width, height);
ivec2 id = ivec2(gl_GlobalInvocationID.xy);
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
int x = pos.x;
int y = pos.y;
// Check if the pixel should be drawn
if (x >= border_size && x <= (width - 1) - border_size && y >= title_height && y <= (height - 1) - border_size)
{
return;
}
vec2 fragCoord = vec2(id);
float res = clamp(iResolution.y, 300.0, 600.0);
vec2 u = (fragCoord - iResolution.xy*.5)/res;
vec2 sc = u*4. + s.yx*(iTime*0.05)/12.;
vec4 h = getHex(sc);
vec4 h2 = getHex(sc - 1.0 / s);
vec4 h3 = getHex(sc + 1.0 / s);
vec2 p = h.xy;
float eDist = hex(p);
float cDist = dot(p, p);
float rnd = hash21(h.zw);
//float aRnd = sin(rnd*6.283 + (iTime*0.05)*1.5)*.5 + .5; // Animating the random number.
#ifdef INTERLACING
// Random vec3 - used for some overlapping.
//vec3 lRnd = vec3(rnd*14.4 + .81, fract(rnd*21.3 + .97), fract(rnd*7.2 + .63));
vec3 lRnd = vec3(hash21(h.zw + .23), hash21(h.zw + .96), hash21(h.zw + .47));
#endif
// It's possible to control the randomness to form some kind of repeat pattern.
//rnd = mod(h.z + h.w, 2.);
// Redundant here, but I might need it later.
float dir = 1.;
// Storage vector.
vec2 q;
// If the grid cell's random ID is above a threshold, flip the Y-coordinates.
if(rnd>.5) p.y = -p.y;
// Determining the closest of the three arcs to the current point, the keeping a copy
// of the vector used to produce it. That way, you'll know just to render that particular
// decorated arc, lines, etc - instead of all three.
const float r = 1.;
const float th = .2; // Arc thickness.
// Arc one.
q = p - vec2(0, r)/s;
vec3 da = vec3(q, dfPol(q));
// Arc two. "r2" could be hardcoded, but this is a relatively cheap 2D example.
q = r2(3.14159*2./3.)*p - vec2(0, r)/s;
vec3 db = vec3(q, dfPol(q));
// Arc three.
q = r2(3.14159*4./3.)*p - vec2(0, r)/s;
vec3 dc = vec3(q, dfPol(q));
// Compare distance fields, and return the vector used to produce the closest one.
vec3 q3 = da.z<db.z && da.z<dc.z? da : db.z<dc.z ? db : dc;
// TRUCHET PATTERN
//
// Set the poloidal arc radius: You can change the poloidal distance field in
// the "dfPol" function to a different curve shape, but you'll need to change
// the radius to one of the figures below.
//
q3.z -= .57735/2. + th/2.; // Circular and dodecahedral arc/curves.
//q3.z -= .5/2. + th/2.; // Hexagon curve.
//q3.z -= .7071/2. + th/2.; // Triangle curve.
q3.z = max(q3.z, -th - q3.z); // Chop out the smaller radius. The result is an arc.
// Store the result in "d" - only to save writing "q3.z" everywhere.
float d = q3.z;
// If you'd like to see the maze by itself.
#ifdef MAZE_ONLY
d += 1e5;
#endif
// Truchet border.
float dBord = max(d - .015, -d);
// MAZE BORDERS
// Producing the stright-line arc borders. Basically, we're rendering some hexagonal borders around
// the arcs. The result is the hexagonal maze surrounding the Truchet pattern.
q = q3.xy;
const float lnTh = .05;
q = abs(q);
float arcBord = hex(q);
//float arcBord = length(q); // Change arc length to ".57735."
//float arcBord = max(hex(q), hex(r2(3.14159/6.)*q)); // Change arc length to ".57735."
// Making the hexagonal arc.
float lnOuter = max(arcBord - .5, -(arcBord - .5 + lnTh)); //.57735
#ifdef INTERLACING
float ln = min(lnOuter, (q.y*.866025 + q.x*.5, q.x) - lnTh);
#else
float ln = min(lnOuter, arcBord - lnTh);
#endif
float lnBord = ln - .03; // Border lines to the maze border, if that makes any sense. :)
///////
// The moving Truchet pattern. The polar coordinates consist of a wrapped angular coordinate,
// and the distance field itself.
float a = getPolarCoord(q3.xy, dir);
float d2 = df(vec2(q3.z, a), dir);
float dMask = smoothstep(0., .015, d);
vec3 bg = mix(vec3(0, .0, .6), vec3(0, .9, .0), dot(sin(u*6. - cos(u*3.)), vec2(.4/2.)) + .4);
bg = mix(bg, bg.xzy, dot(sin(u*6. - cos(u*3.)), vec2(.4/2.)) + .4);
bg = mix(bg, bg.zxy, dot(sin(u*3. + cos(u*3.)), vec2(.1/2.)) + .1);
#ifdef INTERLACING
// Putting in background cube lines for the interlaced version.
float hLines = smoothstep(0., .02, eDist - .5 + .02);
bg = mix(bg, vec3(0), smoothstep(0., .02, ln)*dMask*hLines);
#endif
// Lines over the maze lines. Applying difference logic, depending on whether the
// pattern is interlaced or not.
const float tr = 1.;
float eDist2 = hex(h2.xy);
float hLines2 = smoothstep(0., .02, eDist2 - .5 + .02);
#ifdef INTERLACING
if(rnd>.5 && lRnd.x<.5) hLines2 *= smoothstep(0., .02, ln);
if(lRnd.x>.5) hLines2 *= dMask;
#else
if(rnd>.5) hLines2 *= smoothstep(0., .02, ln);
hLines2 *= dMask;
#endif
bg = mix(bg, vec3(0), hLines2*tr);
float eDist3 = hex(h3.xy);
float hLines3 = smoothstep(0., .02, eDist3 - .5 + .02);
#ifdef INTERLACING
if(rnd<=.5 && lRnd.x>.5) hLines3 *= smoothstep(0., .02, ln);
if(lRnd.x>.5) hLines3 *= dMask;
#else
if(rnd<=.5) hLines3 *= smoothstep(0., .02, ln);
hLines3 *= dMask;
#endif
bg = mix(bg, vec3(0), hLines3*tr);
// Using the two off-centered hex coordinates to give the background a bit of highlighting.
float shade = max(1.25 - dot(h2.xy, h2.xy)*2., 0.);
shade = min(shade, max(dot(h3.xy, h3.xy)*3. + .25, 0.));
bg = mix(bg, vec3(0), (1.-shade)*.5);
// I wanted to change the colors of everything at the last minute. It's pretty hacky, so
// when I'm feeling less lazy, I'll tidy it up. :)
vec3 dotCol = bg.zyx*vec3(1.5, .4, .4);
vec3 bCol = mix(bg.zyx, bg.yyy, .25);
bg = mix(bg.yyy, bg.zyx, .25);
// Under the random threshold, and we draw the lines under the Truchet pattern.
#ifdef INTERLACING
if(lRnd.x>.5){
bg = mix(bg, vec3(0), (1. - smoothstep(0., .015, lnBord)));
bg = mix(bg, bCol, (1. - smoothstep(0., .015, ln)));
// Center lines.
bg = mix(bg, vec3(0), smoothstep(0., .02, eDist3 - .5 + .02)*tr);
}
#else
bg = mix(bg, vec3(0), (1. - smoothstep(0., .015, lnBord)));
bg = mix(bg, bCol, (1. - smoothstep(0., .015, ln)));
#endif
// Apply the Truchet shadow to the background.
bg = mix(bg, vec3(0), (1. - smoothstep(0., .07, d))*.5);
// Place the Truchet field to the background, with some additional shading to give it a
// slightly rounded, raised feel.
//vec3 col = mix(bg, vec3(1)*max(-d*3. + .7, 0.), (1. - dMask)*.65);
// Huttarl suggest slightly more shading on the snake-like pattern edges, so I added just a touch.
vec3 col = mix(bg, vec3(1)*max(-d*9. + .4, 0.), (1. - dMask)*.65);
// Apply the moving dot pattern to the Truchet.