rae 0.1.2

Renderer-neutral widget, layout, state, and GLSL shader primitives for agentic Rust desktop tools.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShaderUniformKind {
    Float1,
    Float2,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ShaderUniform {
    pub name: &'static str,
    pub kind: ShaderUniformKind,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ShaderSource {
    pub name: &'static str,
    pub vertex: &'static str,
    pub fragment: &'static str,
    pub uniforms: &'static [ShaderUniform],
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShaderFamily {
    Background,
    Panel,
    Prompt,
    Interaction,
    Code,
    AgentFlow,
    Data,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ShaderDescriptor {
    pub source: ShaderSource,
    pub family: ShaderFamily,
    pub description: &'static str,
    pub complexity: u8,
    pub animated: bool,
}

pub const COMMON_UNIFORMS: &[ShaderUniform] = &[
    ShaderUniform {
        name: "u_time",
        kind: ShaderUniformKind::Float1,
    },
    ShaderUniform {
        name: "u_resolution",
        kind: ShaderUniformKind::Float2,
    },
    ShaderUniform {
        name: "u_intensity",
        kind: ShaderUniformKind::Float1,
    },
];

pub const VERTEX: &str = r#"#version 100
attribute vec3 position;
attribute vec2 texcoord;
attribute vec4 color0;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform mat4 Model;
uniform mat4 Projection;
void main() {
    gl_Position = Projection * Model * vec4(position, 1.0);
    uv = texcoord;
    color = color0;
}
"#;

pub const BACKGROUND_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float hash(vec2 p) {
    p = fract(p * vec2(123.34, 456.21));
    p += dot(p, p + 45.32);
    return fract(p.x * p.y);
}

float noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    return mix(
        mix(hash(i + vec2(0.0, 0.0)), hash(i + vec2(1.0, 0.0)), u.x),
        mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x),
        u.y
    );
}

float fbm(vec2 p) {
    float value = 0.0;
    float amplitude = 0.5;
    mat2 warp = mat2(1.62, 1.18, -1.18, 1.62);
    for (int i = 0; i < 6; i++) {
        value += amplitude * noise(p);
        p = warp * p + vec2(0.17, 0.31);
        amplitude *= 0.53;
    }
    return value;
}

vec2 curl(vec2 p) {
    float e = 0.035;
    float n1 = fbm(p + vec2(0.0, e));
    float n2 = fbm(p - vec2(0.0, e));
    float n3 = fbm(p + vec2(e, 0.0));
    float n4 = fbm(p - vec2(e, 0.0));
    return vec2(n1 - n2, n4 - n3) / (2.0 * e);
}

float rings(vec2 p, float t) {
    float d = length(p);
    float wave = sin(34.0 * d - t * 3.5 + fbm(p * 4.0) * 2.5);
    return smoothstep(0.84, 1.0, wave) * smoothstep(1.2, 0.12, d);
}

void main() {
    vec2 p = (gl_FragCoord.xy * 2.0 - u_resolution.xy) / max(u_resolution.x, u_resolution.y);
    float t = u_time * 0.18;
    vec2 flow = curl(p * 2.0 + t);
    vec2 q = p + flow * 0.22 + vec2(sin(t * 1.7), cos(t * 1.3)) * 0.08;
    float nebula = fbm(q * 2.6 + t * 0.7);
    float veins = fbm(q * 9.0 - flow * 1.4);
    float halo = rings(p - vec2(0.38, -0.2), u_time);
    float vignette = smoothstep(1.36, 0.18, length(p));
    vec3 ink = vec3(0.010, 0.014, 0.034);
    vec3 blue = vec3(0.09, 0.32, 0.78);
    vec3 cyan = vec3(0.12, 0.82, 1.0);
    vec3 violet = vec3(0.56, 0.14, 0.96);
    vec3 gold = vec3(1.0, 0.55, 0.17);
    vec3 col = ink;
    col += blue * nebula * 0.34;
    col += cyan * smoothstep(0.52, 0.96, veins) * 0.24;
    col += violet * pow(max(nebula, 0.0), 2.2) * 0.32;
    col += gold * halo * 0.36;
    col *= 0.55 + vignette * 0.75;
    col += vec3(hash(gl_FragCoord.xy + u_time) * 0.025);
    gl_FragColor = vec4(col * (0.72 + u_intensity * 0.42), 1.0) * color;
}
"#;

pub const PANE_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float hex(vec2 p) {
    p.x *= 0.57735 * 2.0;
    p.y += mod(floor(p.x), 2.0) * 0.5;
    p = abs(fract(p) - 0.5);
    return abs(max(p.x * 1.5 + p.y, p.y * 2.0) - 1.0);
}

float hash(vec2 p) {
    return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
}

float grid(vec2 p, float t) {
    vec2 id = floor(p);
    float h = hash(id);
    float cell = smoothstep(0.035, 0.0, hex(p));
    float sweep = smoothstep(0.98, 0.4, abs(fract(h + t * 0.08) - 0.5));
    return cell * sweep;
}

void main() {
    vec2 p = uv;
    vec2 centered = p - 0.5;
    float edge = smoothstep(0.5, 0.47, max(abs(centered.x), abs(centered.y)));
    float border = 1.0 - smoothstep(0.0, 0.018, min(min(p.x, p.y), min(1.0 - p.x, 1.0 - p.y)));
    float lattice = grid((p + vec2(u_time * 0.018, -u_time * 0.012)) * 18.0, u_time);
    float scan = sin((p.y + u_time * 0.11) * 420.0) * 0.5 + 0.5;
    vec3 base = vec3(0.018, 0.032, 0.070);
    vec3 glow = vec3(0.10, 0.54, 0.95) * lattice * 0.22;
    vec3 rim = vec3(0.36, 0.76, 1.0) * border * (0.32 + 0.22 * sin(u_time * 1.9));
    vec3 col = base + glow + rim + vec3(scan * 0.018 * u_intensity);
    gl_FragColor = vec4(col, 0.82 * edge) * color;
}
"#;

pub const PROMPT_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float line(vec2 p, float offset, float width) {
    return smoothstep(width, 0.0, abs(p.y - offset));
}

void main() {
    vec2 p = uv;
    float t = u_time;
    float beam = line(p, 0.5 + sin(t * 1.4 + p.x * 7.0) * 0.08, 0.018);
    float beam2 = line(p, 0.34 + cos(t * 1.1 + p.x * 9.0) * 0.05, 0.012);
    float edge = 1.0 - smoothstep(0.0, 0.026, min(min(p.x, p.y), min(1.0 - p.x, 1.0 - p.y)));
    float caret = smoothstep(0.015, 0.0, abs(fract(p.x * 34.0 - t * 0.9) - 0.5));
    vec3 base = vec3(0.020, 0.030, 0.070);
    vec3 cyan = vec3(0.16, 0.86, 1.0);
    vec3 violet = vec3(0.75, 0.24, 1.0);
    vec3 col = base + cyan * beam * 0.28 + violet * beam2 * 0.18;
    col += cyan * edge * 0.32;
    col += vec3(0.35, 0.85, 1.0) * caret * 0.035 * u_intensity;
    gl_FragColor = vec4(col, 0.90) * color;
}
"#;

pub const AURORA_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float hash(vec2 p) {
    return fract(sin(dot(p, vec2(41.0, 289.0))) * 45758.5453);
}

float noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    float a = hash(i);
    float b = hash(i + vec2(1.0, 0.0));
    float c = hash(i + vec2(0.0, 1.0));
    float d = hash(i + vec2(1.0, 1.0));
    return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}

float ridge(float v) {
    return 1.0 - abs(v * 2.0 - 1.0);
}

void main() {
    vec2 p = (gl_FragCoord.xy / u_resolution.xy);
    float t = u_time * 0.12;
    float curtain = 0.0;
    for (int i = 0; i < 5; i++) {
        float fi = float(i);
        vec2 q = vec2(p.x * (2.0 + fi * 0.65) + t * (0.7 + fi * 0.13), p.y * (7.0 + fi));
        float band = ridge(noise(q + vec2(fi * 8.7, -t * 2.0)));
        curtain += smoothstep(0.44, 1.0, band) * (0.18 + fi * 0.035);
    }
    float horizon = smoothstep(0.98, 0.16, p.y);
    float stars = step(0.996, hash(gl_FragCoord.xy + floor(u_time * 8.0))) * smoothstep(0.8, 0.05, p.y);
    vec3 deep = vec3(0.005, 0.015, 0.042);
    vec3 green = vec3(0.18, 1.00, 0.70);
    vec3 blue = vec3(0.10, 0.42, 1.00);
    vec3 magenta = vec3(0.85, 0.16, 1.00);
    vec3 col = deep + mix(blue, green, p.y) * curtain * horizon * u_intensity;
    col += magenta * pow(curtain, 3.0) * 0.18;
    col += vec3(stars) * 0.75;
    gl_FragColor = vec4(col, 1.0) * color;
}
"#;

pub const KNOT_FIELD_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float sdTorus(vec3 p, vec2 t) {
    vec2 q = vec2(length(p.xz) - t.x, p.y);
    return length(q) - t.y;
}

mat2 rot(float a) {
    float s = sin(a);
    float c = cos(a);
    return mat2(c, -s, s, c);
}

float field(vec3 p) {
    p.xz *= rot(u_time * 0.23);
    p.xy *= rot(sin(u_time * 0.17) * 0.6);
    float d = sdTorus(p, vec2(0.62, 0.055));
    vec3 q = p;
    q.xy *= rot(2.094);
    float d2 = sdTorus(q, vec2(0.62, 0.055));
    q = p;
    q.xy *= rot(-2.094);
    float d3 = sdTorus(q, vec2(0.62, 0.055));
    return min(d, min(d2, d3));
}

void main() {
    vec2 p = (gl_FragCoord.xy * 2.0 - u_resolution.xy) / max(u_resolution.x, u_resolution.y);
    vec3 ro = vec3(0.0, 0.0, 2.1);
    vec3 rd = normalize(vec3(p, -1.65));
    float t = 0.0;
    float glow = 0.0;
    float hit = 0.0;
    for (int i = 0; i < 58; i++) {
        vec3 pos = ro + rd * t;
        float d = field(pos);
        glow += 0.006 / (0.018 + abs(d));
        if (d < 0.003) {
            hit = 1.0;
            break;
        }
        t += max(d * 0.58, 0.012);
        if (t > 4.0) {
            break;
        }
    }
    vec3 base = vec3(0.005, 0.010, 0.025);
    vec3 cyan = vec3(0.10, 0.78, 1.00);
    vec3 violet = vec3(0.65, 0.14, 1.00);
    vec3 gold = vec3(1.00, 0.48, 0.10);
    vec3 col = base + cyan * glow * 0.24 + violet * pow(glow, 1.4) * 0.08 + gold * hit * 0.35;
    col *= 0.74 + u_intensity * 0.34;
    gl_FragColor = vec4(col, 0.96) * color;
}
"#;

pub const TERMINAL_RAIN_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float hash(vec2 p) {
    return fract(sin(dot(p, vec2(13.13, 91.77))) * 43758.5453);
}

void main() {
    vec2 cell = floor(gl_FragCoord.xy / vec2(10.0, 18.0));
    float column = hash(vec2(cell.x, 2.0));
    float speed = mix(0.8, 3.4, column);
    float trail = fract(cell.y * 0.071 - u_time * speed + column * 9.0);
    float glyph = hash(cell + floor(u_time * speed));
    float on = smoothstep(0.92, 1.0, trail) * step(0.22, glyph);
    float tail = smoothstep(0.45, 0.0, trail) * step(0.65, column);
    vec3 base = vec3(0.0, 0.012, 0.025);
    vec3 green = vec3(0.18, 1.0, 0.56);
    vec3 blue = vec3(0.1, 0.68, 1.0);
    vec3 col = base + green * on * u_intensity + blue * tail * 0.18;
    col += vec3(hash(gl_FragCoord.xy) * 0.018);
    gl_FragColor = vec4(col, 0.72) * color;
}
"#;

pub const AGENT_FLOW_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float capsule(vec2 p, vec2 a, vec2 b, float r) {
    vec2 pa = p - a;
    vec2 ba = b - a;
    float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
    return length(pa - ba * h) - r;
}

void main() {
    vec2 p = uv;
    float glow = 0.0;
    for (int i = 0; i < 7; i++) {
        float fi = float(i);
        vec2 a = vec2(0.08, 0.16 + fi * 0.115);
        vec2 b = vec2(0.92, 0.22 + sin(u_time * 0.7 + fi) * 0.24 + fi * 0.08);
        float d = capsule(p, a, b, 0.006 + 0.002 * sin(u_time + fi));
        float pulse = smoothstep(0.05, 0.0, abs(fract(u_time * 0.22 + fi * 0.17) - p.x));
        glow += smoothstep(0.028, 0.0, d) * (0.28 + pulse * 0.72);
    }
    float node = 0.0;
    for (int j = 0; j < 5; j++) {
        float fj = float(j);
        vec2 c = vec2(0.16 + fj * 0.18, 0.5 + sin(u_time * 0.5 + fj * 1.7) * 0.28);
        node += smoothstep(0.06, 0.0, length(p - c));
    }
    vec3 base = vec3(0.014, 0.020, 0.050);
    vec3 cyan = vec3(0.08, 0.80, 1.00);
    vec3 violet = vec3(0.62, 0.16, 1.00);
    vec3 col = base + cyan * glow * 0.45 * u_intensity + violet * node * 0.20;
    gl_FragColor = vec4(col, 0.86) * color;
}
"#;

pub const RIPPLE_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float ring(vec2 p, vec2 c, float radius, float width) {
    float d = length(p - c);
    return smoothstep(width, 0.0, abs(d - radius));
}

float disk(vec2 p, vec2 c, float radius) {
    return smoothstep(radius, 0.0, length(p - c));
}

void main() {
    vec2 p = uv;
    float t = fract(u_time * 0.62);
    vec2 c1 = vec2(0.30 + sin(u_time * 0.37) * 0.09, 0.50 + cos(u_time * 0.41) * 0.08);
    vec2 c2 = vec2(0.70 + cos(u_time * 0.29) * 0.11, 0.46 + sin(u_time * 0.53) * 0.10);
    float r1 = ring(p, c1, t * 0.72, 0.026) * (1.0 - t);
    float r2 = ring(p, c2, fract(t + 0.38) * 0.66, 0.020) * (1.0 - fract(t + 0.38));
    float ink = disk(p, c1, 0.12 + t * 0.5) * 0.055 + disk(p, c2, 0.10 + fract(t + 0.38) * 0.45) * 0.045;
    float border = 1.0 - smoothstep(0.0, 0.018, min(min(p.x, p.y), min(1.0 - p.x, 1.0 - p.y)));
    vec3 base = vec3(0.018, 0.026, 0.055);
    vec3 cyan = vec3(0.12, 0.82, 1.0);
    vec3 violet = vec3(0.72, 0.20, 1.0);
    vec3 col = base + cyan * r1 * u_intensity + violet * r2 * 0.72 * u_intensity + cyan * border * 0.22 + vec3(ink);
    gl_FragColor = vec4(col, 0.82) * color;
}
"#;

pub const BLOOM_GLOW_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float blob(vec2 p, vec2 c, float radius) {
    float d = length(p - c);
    return radius * radius / max(d * d, 0.001);
}

void main() {
    vec2 p = uv;
    float t = u_time;
    float glow = 0.0;
    glow += blob(p, vec2(0.22 + sin(t * 0.7) * 0.12, 0.34 + cos(t * 0.6) * 0.08), 0.045);
    glow += blob(p, vec2(0.76 + cos(t * 0.5) * 0.10, 0.52 + sin(t * 0.9) * 0.12), 0.060);
    glow += blob(p, vec2(0.50 + sin(t * 0.4) * 0.18, 0.78 + cos(t * 0.8) * 0.05), 0.038);
    glow = min(glow, 3.0);
    float scan = sin((p.y + t * 0.11) * 540.0) * 0.5 + 0.5;
    float vignette = smoothstep(0.82, 0.12, length(p - 0.5));
    vec3 base = vec3(0.010, 0.014, 0.030);
    vec3 hot = mix(vec3(0.10, 0.55, 1.0), vec3(0.95, 0.18, 1.0), p.x);
    vec3 col = base + hot * glow * 0.23 * u_intensity + vec3(scan * 0.018);
    col *= 0.62 + vignette * 0.72;
    gl_FragColor = vec4(col, 0.78) * color;
}
"#;

pub const CODE_LENS_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float hash(vec2 p) {
    return fract(sin(dot(p, vec2(101.7, 203.3))) * 43758.5453);
}

float line_y(float y, float target, float width) {
    return smoothstep(width, 0.0, abs(y - target));
}

void main() {
    vec2 p = uv;
    float lens = smoothstep(0.38, 0.0, length((p - vec2(0.5, 0.52)) * vec2(1.0, 1.8)));
    float code = 0.0;
    for (int i = 0; i < 18; i++) {
        float fi = float(i);
        float y = 0.08 + fi * 0.048;
        float len = 0.2 + hash(vec2(fi, 1.0)) * 0.58;
        float start = 0.08 + hash(vec2(fi, 2.0)) * 0.18;
        float seg = smoothstep(start, start + 0.018, p.x) * smoothstep(start + len, start + len - 0.018, p.x);
        code += line_y(p.y, y + sin(u_time * 0.9 + fi) * 0.003, 0.004 + lens * 0.006) * seg;
    }
    float cursor = smoothstep(0.015, 0.0, abs(p.x - (0.16 + fract(u_time * 0.11) * 0.68))) * line_y(p.y, 0.52, 0.022);
    vec3 base = vec3(0.012, 0.017, 0.034);
    vec3 text = vec3(0.52, 0.85, 1.0);
    vec3 lens_col = vec3(0.18, 0.58, 1.0) * lens * 0.18;
    vec3 col = base + text * code * (0.22 + lens * 0.50) * u_intensity + lens_col + vec3(0.9, 0.6, 0.2) * cursor * 0.6;
    gl_FragColor = vec4(col, 0.86) * color;
}
"#;

pub const REACTION_DIFFUSION_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float hash(vec2 p) {
    return fract(sin(dot(p, vec2(17.17, 313.13))) * 43758.5453);
}

float n(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}

void main() {
    vec2 p = (gl_FragCoord.xy * 2.0 - u_resolution.xy) / max(u_resolution.x, u_resolution.y);
    float t = u_time * 0.12;
    float a = n(p * 4.0 + t);
    float b = n(p * 9.0 - t * 1.7 + a);
    float c = n(p * 19.0 + vec2(a, b) * 3.0 - t);
    float cells = smoothstep(0.48, 0.54, abs(a - b) + c * 0.14);
    float veins = smoothstep(0.72, 0.98, sin((a - b + c) * 18.0 + u_time));
    vec3 base = vec3(0.008, 0.014, 0.028);
    vec3 acid = vec3(0.30, 1.0, 0.58);
    vec3 plasma = vec3(0.45, 0.18, 1.0);
    vec3 col = base + acid * cells * 0.32 * u_intensity + plasma * veins * 0.26;
    gl_FragColor = vec4(col, 0.80) * color;
}
"#;

pub const VORONOI_CRYSTAL_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

vec2 hash2(vec2 p) {
    p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
    return fract(sin(p) * 43758.5453);
}

void main() {
    vec2 p = uv * vec2(u_resolution.x / max(u_resolution.y, 1.0), 1.0) * 8.0;
    vec2 i = floor(p);
    vec2 f = fract(p);
    float min_d = 8.0;
    float second_d = 8.0;
    for (int y = -1; y <= 1; y++) {
        for (int x = -1; x <= 1; x++) {
            vec2 g = vec2(float(x), float(y));
            vec2 o = hash2(i + g);
            o = 0.5 + 0.5 * sin(u_time * 0.35 + 6.2831 * o);
            float d = length(g + o - f);
            if (d < min_d) {
                second_d = min_d;
                min_d = d;
            } else if (d < second_d) {
                second_d = d;
            }
        }
    }
    float edge = smoothstep(0.045, 0.0, second_d - min_d);
    float facet = smoothstep(0.9, 0.0, min_d);
    vec3 base = vec3(0.010, 0.012, 0.032);
    vec3 cyan = vec3(0.18, 0.82, 1.0);
    vec3 rose = vec3(1.0, 0.20, 0.62);
    vec3 col = base + cyan * edge * 0.58 * u_intensity + rose * facet * 0.10;
    gl_FragColor = vec4(col, 0.78) * color;
}
"#;

pub const PLASMA_GRAPH_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float line(vec2 p, vec2 a, vec2 b, float w) {
    vec2 pa = p - a;
    vec2 ba = b - a;
    float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
    return smoothstep(w, 0.0, length(pa - ba * h));
}

void main() {
    vec2 p = uv;
    vec2 nodes[6];
    nodes[0] = vec2(0.14, 0.25 + sin(u_time * 0.8) * 0.06);
    nodes[1] = vec2(0.32, 0.68 + cos(u_time * 0.5) * 0.07);
    nodes[2] = vec2(0.48, 0.36 + sin(u_time * 0.6 + 1.0) * 0.08);
    nodes[3] = vec2(0.64, 0.72 + cos(u_time * 0.7 + 2.0) * 0.06);
    nodes[4] = vec2(0.82, 0.30 + sin(u_time * 0.55 + 3.0) * 0.07);
    nodes[5] = vec2(0.90, 0.82 + cos(u_time * 0.45 + 4.0) * 0.05);
    float network = 0.0;
    float sparks = 0.0;
    for (int i = 0; i < 5; i++) {
        vec2 a = nodes[i];
        vec2 b = nodes[i + 1];
        network += line(p, a, b, 0.010);
        float pulse = fract(u_time * 0.22 + float(i) * 0.19);
        sparks += smoothstep(0.045, 0.0, length(p - mix(a, b, pulse)));
    }
    for (int j = 0; j < 6; j++) {
        network += smoothstep(0.055, 0.0, length(p - nodes[j])) * 0.9;
    }
    vec3 base = vec3(0.007, 0.010, 0.028);
    vec3 blue = vec3(0.10, 0.58, 1.0);
    vec3 hot = vec3(1.0, 0.36, 0.12);
    vec3 col = base + blue * network * 0.34 * u_intensity + hot * sparks * 0.42;
    gl_FragColor = vec4(col, 0.84) * color;
}
"#;

pub const SDF_CARD_MORPH_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float sdRoundBox(vec2 p, vec2 b, float r) {
    vec2 q = abs(p) - b + r;
    return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r;
}

float sdCapsuleBox(vec2 p, vec2 b) {
    float r = min(b.x, b.y);
    return sdRoundBox(p, b, r);
}

float blobWarp(vec2 p, float t) {
    float a = atan(p.y, p.x);
    return sin(a * 3.0 + t * 1.7) * 0.035 + sin(a * 7.0 - t * 1.2) * 0.018;
}

float sdTicket(vec2 p, vec2 b, float r) {
    float box = sdRoundBox(p, b, r);
    float left = length(p - vec2(-b.x, 0.0)) - 0.115;
    float right = length(p - vec2(b.x, 0.0)) - 0.115;
    return max(box, -min(left, right));
}

void main() {
    vec2 p = uv * 2.0 - 1.0;
    p.x *= u_resolution.x / max(u_resolution.y, 1.0);
    float t = u_time;
    float morph = 0.5 + 0.5 * sin(t * 0.55);
    float morph2 = 0.5 + 0.5 * sin(t * 0.37 + 1.8);
    vec2 b = mix(vec2(0.72, 0.38), vec2(0.58, 0.58), morph2);
    float card = sdRoundBox(p, b, mix(0.08, 0.24, morph));
    float capsule = sdCapsuleBox(p, b * vec2(1.08, 0.72));
    vec2 warped = p * (1.0 + blobWarp(p, t) * mix(0.0, 2.6, morph));
    float blob = sdRoundBox(warped, b * vec2(0.96, 0.94), 0.34);
    float ticket = sdTicket(p, b, 0.08);
    float d = mix(card, capsule, smoothstep(0.0, 0.36, morph));
    d = mix(d, blob, smoothstep(0.26, 0.76, morph));
    d = mix(d, ticket, smoothstep(0.70, 1.0, morph2) * 0.45);
    float fill = smoothstep(0.012, -0.006, d);
    float edge = smoothstep(0.018, 0.0, abs(d));
    float outer = smoothstep(0.18, 0.0, d) * smoothstep(-0.02, 0.08, d);
    float grid = (sin((p.x + t * 0.05) * 42.0) * sin((p.y - t * 0.04) * 34.0)) * 0.5 + 0.5;
    vec3 base = vec3(0.015, 0.020, 0.045);
    vec3 fill_col = mix(vec3(0.05, 0.18, 0.45), vec3(0.22, 0.08, 0.45), morph);
    vec3 rim = mix(vec3(0.10, 0.72, 1.0), vec3(0.95, 0.24, 1.0), morph2);
    vec3 col = base + fill_col * fill * 0.92 + rim * edge * u_intensity + rim * outer * 0.22;
    col += vec3(grid * fill * 0.025);
    gl_FragColor = vec4(col, 0.92 * max(fill, outer)) * color;
}
"#;

pub const METABALL_CARD_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float ball(vec2 p, vec2 c, float r) {
    float d = length(p - c);
    return r * r / max(d * d, 0.002);
}

void main() {
    vec2 p = uv * 2.0 - 1.0;
    p.x *= u_resolution.x / max(u_resolution.y, 1.0);
    float t = u_time;
    float f = 0.0;
    f += ball(p, vec2(-0.48 + sin(t * 0.7) * 0.18, -0.20 + cos(t * 0.5) * 0.14), 0.34);
    f += ball(p, vec2(0.30 + cos(t * 0.6) * 0.20, -0.04 + sin(t * 0.9) * 0.12), 0.38);
    f += ball(p, vec2(-0.08 + sin(t * 0.4 + 2.0) * 0.16, 0.32 + cos(t * 0.8) * 0.12), 0.32);
    f += ball(p, vec2(0.58 + sin(t * 0.55 + 1.1) * 0.10, 0.20 + cos(t * 0.45) * 0.14), 0.24);
    float surface = smoothstep(1.08, 1.16, f);
    float edge = smoothstep(1.05, 1.22, f) - smoothstep(1.24, 1.42, f);
    float core = smoothstep(1.8, 3.0, f);
    vec3 base = vec3(0.005, 0.010, 0.026);
    vec3 cyan = vec3(0.08, 0.82, 1.0);
    vec3 pink = vec3(1.0, 0.16, 0.72);
    vec3 amber = vec3(1.0, 0.58, 0.08);
    vec3 col = base + mix(cyan, pink, uv.x) * surface * 0.54 * u_intensity + amber * edge * 0.45 + vec3(core * 0.18);
    gl_FragColor = vec4(col, 0.88 * max(surface, edge)) * color;
}
"#;

pub const GOOGLE_AURA_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float hash(vec2 p) { return fract(sin(dot(p, vec2(37.2, 171.9))) * 43758.5453); }
float noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}

void main() {
    vec2 p = uv - 0.5;
    p.x *= u_resolution.x / max(u_resolution.y, 1.0);
    float t = u_time * 0.16;
    float n = noise(p * 3.0 + t) * 0.5 + noise(p * 7.0 - t * 1.7) * 0.3 + noise(p * 15.0 + t * 2.1) * 0.2;
    float angle = atan(p.y, p.x) / 6.2831853 + 0.5 + n * 0.18 + t * 0.25;
    vec3 blue = vec3(0.10, 0.42, 1.0);
    vec3 red = vec3(1.0, 0.20, 0.12);
    vec3 yellow = vec3(1.0, 0.74, 0.08);
    vec3 green = vec3(0.10, 0.72, 0.28);
    vec3 a = mix(blue, red, smoothstep(0.0, 0.35, fract(angle)));
    vec3 b = mix(yellow, green, smoothstep(0.35, 1.0, fract(angle)));
    vec3 hue = mix(a, b, smoothstep(0.25, 0.8, n));
    float d = length(p);
    float aura = smoothstep(0.72, 0.08, d + n * 0.12);
    float ring = smoothstep(0.018, 0.0, abs(d - (0.26 + sin(u_time * 0.8) * 0.035 + n * 0.06)));
    vec3 col = hue * aura * (0.36 + u_intensity * 0.44) + vec3(ring) * 0.32;
    gl_FragColor = vec4(col, 0.78 * aura) * color;
}
"#;

pub const ORBITAL_MESH_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float seg(vec2 p, vec2 a, vec2 b, float w) {
    vec2 pa = p - a;
    vec2 ba = b - a;
    float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
    return smoothstep(w, 0.0, length(pa - ba * h));
}

vec2 orbit(float i, float t, float r) {
    return vec2(cos(t + i * 1.047), sin(t * 0.83 + i * 1.618)) * r;
}

void main() {
    vec2 p = uv * 2.0 - 1.0;
    p.x *= u_resolution.x / max(u_resolution.y, 1.0);
    float t = u_time * 0.55;
    vec2 n0 = orbit(0.0, t, 0.52);
    vec2 n1 = orbit(1.0, t, 0.48);
    vec2 n2 = orbit(2.0, t, 0.56);
    vec2 n3 = orbit(3.0, t, 0.42);
    vec2 n4 = orbit(4.0, t, 0.50);
    float network = 0.0;
    network += seg(p, n0, n1, 0.009);
    network += seg(p, n1, n2, 0.009);
    network += seg(p, n2, n3, 0.009);
    network += seg(p, n3, n4, 0.009);
    network += seg(p, n4, n0, 0.009);
    network += smoothstep(0.055, 0.0, length(p - n0));
    network += smoothstep(0.055, 0.0, length(p - n1));
    network += smoothstep(0.055, 0.0, length(p - n2));
    network += smoothstep(0.055, 0.0, length(p - n3));
    network += smoothstep(0.055, 0.0, length(p - n4));
    float halo = smoothstep(0.75, 0.10, length(p));
    vec3 col = vec3(0.006, 0.012, 0.032) + vec3(0.10, 0.72, 1.0) * network * 0.38 * u_intensity + vec3(0.75, 0.2, 1.0) * halo * 0.08;
    gl_FragColor = vec4(col, 0.84) * color;
}
"#;

pub const GLASS_CAUSTIC_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float wave(vec2 p, float t) {
    return sin(p.x * 18.0 + t) + sin(p.y * 21.0 - t * 1.2) + sin((p.x + p.y) * 15.0 + t * 0.7);
}

void main() {
    vec2 p = uv;
    float t = u_time * 0.7;
    float c = wave(p, t);
    float c2 = wave(p + vec2(c * 0.018, -c * 0.014), -t * 0.8);
    float caustic = smoothstep(1.7, 2.7, c2);
    float edge = 1.0 - smoothstep(0.0, 0.018, min(min(p.x, p.y), min(1.0 - p.x, 1.0 - p.y)));
    float glare = smoothstep(0.02, 0.0, abs(p.y - (0.22 + sin(t) * 0.04))) * smoothstep(0.15, 0.75, p.x) * smoothstep(0.95, 0.45, p.x);
    vec3 glass = vec3(0.040, 0.070, 0.120);
    vec3 cyan = vec3(0.34, 0.88, 1.0);
    vec3 col = glass + cyan * caustic * 0.22 * u_intensity + vec3(glare * 0.22) + cyan * edge * 0.24;
    gl_FragColor = vec4(col, 0.62) * color;
}
"#;

pub const LIQUID_CHROME_FRAGMENT: &str = r#"#version 100
precision mediump float;
varying lowp vec2 uv;
varying lowp vec4 color;
uniform sampler2D Texture;
uniform float u_time;
uniform vec2 u_resolution;
uniform float u_intensity;

float hash(vec2 p) { return fract(sin(dot(p, vec2(93.1, 67.7))) * 43758.5453); }
float noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}

void main() {
    vec2 p = uv * 2.0 - 1.0;
    p.x *= u_resolution.x / max(u_resolution.y, 1.0);
    float t = u_time * 0.18;
    vec2 q = p;
    q += vec2(noise(p * 2.0 + t), noise(p * 2.0 - t)) * 0.22;
    float bands = sin(q.x * 12.0 + noise(q * 5.0) * 5.0 + u_time) * 0.5 + 0.5;
    float spec = pow(smoothstep(0.62, 1.0, bands), 5.0);
    float body = smoothstep(0.72, 0.12, length(p));
    vec3 cold = vec3(0.18, 0.42, 0.88);
    vec3 warm = vec3(1.0, 0.55, 0.22);
    vec3 chrome = mix(cold, warm, bands);
    vec3 col = chrome * body * (0.22 + u_intensity * 0.30) + vec3(spec * 0.52);
    gl_FragColor = vec4(col, 0.82 * body) * color;
}
"#;

pub fn background_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-background-curl-nebula",
        vertex: VERTEX,
        fragment: BACKGROUND_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn pane_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-pane-hex-lattice",
        vertex: VERTEX,
        fragment: PANE_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn prompt_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-prompt-beam-field",
        vertex: VERTEX,
        fragment: PROMPT_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn aurora_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-background-aurora-curtains",
        vertex: VERTEX,
        fragment: AURORA_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn knot_field_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-knot-raymarch-field",
        vertex: VERTEX,
        fragment: KNOT_FIELD_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn terminal_rain_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-terminal-rain",
        vertex: VERTEX,
        fragment: TERMINAL_RAIN_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn agent_flow_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-agent-flow-beams",
        vertex: VERTEX,
        fragment: AGENT_FLOW_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn ripple_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-interaction-ripples",
        vertex: VERTEX,
        fragment: RIPPLE_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn bloom_glow_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-bloom-glow-field",
        vertex: VERTEX,
        fragment: BLOOM_GLOW_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn code_lens_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-code-lens-scan",
        vertex: VERTEX,
        fragment: CODE_LENS_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn reaction_diffusion_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-reaction-diffusion",
        vertex: VERTEX,
        fragment: REACTION_DIFFUSION_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn voronoi_crystal_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-voronoi-crystal",
        vertex: VERTEX,
        fragment: VORONOI_CRYSTAL_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn plasma_graph_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-plasma-graph",
        vertex: VERTEX,
        fragment: PLASMA_GRAPH_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn sdf_card_morph_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-sdf-card-morph",
        vertex: VERTEX,
        fragment: SDF_CARD_MORPH_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn metaball_card_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-metaball-card",
        vertex: VERTEX,
        fragment: METABALL_CARD_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn google_aura_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-google-aura",
        vertex: VERTEX,
        fragment: GOOGLE_AURA_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn orbital_mesh_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-orbital-mesh",
        vertex: VERTEX,
        fragment: ORBITAL_MESH_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn glass_caustic_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-glass-caustic",
        vertex: VERTEX,
        fragment: GLASS_CAUSTIC_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn liquid_chrome_shader() -> ShaderSource {
    ShaderSource {
        name: "rae-liquid-chrome",
        vertex: VERTEX,
        fragment: LIQUID_CHROME_FRAGMENT,
        uniforms: COMMON_UNIFORMS,
    }
}

pub fn all_shaders() -> Vec<ShaderSource> {
    vec![
        background_shader(),
        pane_shader(),
        prompt_shader(),
        aurora_shader(),
        knot_field_shader(),
        terminal_rain_shader(),
        agent_flow_shader(),
        ripple_shader(),
        bloom_glow_shader(),
        code_lens_shader(),
        reaction_diffusion_shader(),
        voronoi_crystal_shader(),
        plasma_graph_shader(),
        sdf_card_morph_shader(),
        metaball_card_shader(),
        google_aura_shader(),
        orbital_mesh_shader(),
        glass_caustic_shader(),
        liquid_chrome_shader(),
    ]
}

pub fn shader_catalog() -> Vec<ShaderDescriptor> {
    vec![
        ShaderDescriptor {
            source: background_shader(),
            family: ShaderFamily::Background,
            description: "Curl-noise nebula with ring interference for full-window depth.",
            complexity: 7,
            animated: true,
        },
        ShaderDescriptor {
            source: pane_shader(),
            family: ShaderFamily::Panel,
            description: "Hex lattice pane surface with scanning rim light.",
            complexity: 4,
            animated: true,
        },
        ShaderDescriptor {
            source: prompt_shader(),
            family: ShaderFamily::Prompt,
            description: "Prompt beam field with animated caret energy.",
            complexity: 3,
            animated: true,
        },
        ShaderDescriptor {
            source: aurora_shader(),
            family: ShaderFamily::Background,
            description: "Layered aurora curtains for context and reference views.",
            complexity: 5,
            animated: true,
        },
        ShaderDescriptor {
            source: knot_field_shader(),
            family: ShaderFamily::Data,
            description: "Raymarched torus-knot field for high-energy agent state.",
            complexity: 9,
            animated: true,
        },
        ShaderDescriptor {
            source: terminal_rain_shader(),
            family: ShaderFamily::Code,
            description: "Terminal rain data stream for run and log surfaces.",
            complexity: 3,
            animated: true,
        },
        ShaderDescriptor {
            source: agent_flow_shader(),
            family: ShaderFamily::AgentFlow,
            description: "Animated routed beam network for agent-flow lanes.",
            complexity: 5,
            animated: true,
        },
        ShaderDescriptor {
            source: ripple_shader(),
            family: ShaderFamily::Interaction,
            description: "Material-style click ripple field with two animated origins.",
            complexity: 4,
            animated: true,
        },
        ShaderDescriptor {
            source: bloom_glow_shader(),
            family: ShaderFamily::Interaction,
            description: "Soft bloom blobs for hover, focus, and elevated surfaces.",
            complexity: 4,
            animated: true,
        },
        ShaderDescriptor {
            source: code_lens_shader(),
            family: ShaderFamily::Code,
            description: "Code-line lens shimmer for transcript/code panes.",
            complexity: 5,
            animated: true,
        },
        ShaderDescriptor {
            source: reaction_diffusion_shader(),
            family: ShaderFamily::Background,
            description: "Procedural reaction-diffusion cells for organic loading states.",
            complexity: 6,
            animated: true,
        },
        ShaderDescriptor {
            source: voronoi_crystal_shader(),
            family: ShaderFamily::Panel,
            description: "Voronoi crystal facets for glassy command-palette surfaces.",
            complexity: 6,
            animated: true,
        },
        ShaderDescriptor {
            source: plasma_graph_shader(),
            family: ShaderFamily::AgentFlow,
            description: "Plasma graph nodes and sparks for validation/loop visualizers.",
            complexity: 6,
            animated: true,
        },
        ShaderDescriptor {
            source: sdf_card_morph_shader(),
            family: ShaderFamily::Panel,
            description:
                "SDF whole-card morph between rounded cards, capsules, blobs, and ticket notches.",
            complexity: 8,
            animated: true,
        },
        ShaderDescriptor {
            source: metaball_card_shader(),
            family: ShaderFamily::Panel,
            description: "Metaball card topology that merges moving lobes into one liquid surface.",
            complexity: 6,
            animated: true,
        },
        ShaderDescriptor {
            source: google_aura_shader(),
            family: ShaderFamily::Interaction,
            description: "Google-style multicolor aura for focus and assistant activity states.",
            complexity: 6,
            animated: true,
        },
        ShaderDescriptor {
            source: orbital_mesh_shader(),
            family: ShaderFamily::AgentFlow,
            description: "Orbital node mesh for delegated work and runtime flow maps.",
            complexity: 5,
            animated: true,
        },
        ShaderDescriptor {
            source: glass_caustic_shader(),
            family: ShaderFamily::Panel,
            description: "Glass caustic surface for translucent elevated cards.",
            complexity: 5,
            animated: true,
        },
        ShaderDescriptor {
            source: liquid_chrome_shader(),
            family: ShaderFamily::Data,
            description: "Liquid chrome spectral body for high-energy hero surfaces.",
            complexity: 6,
            animated: true,
        },
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn shader_catalog_names_are_unique() {
        let shaders = all_shaders();
        let mut names = shaders.iter().map(|shader| shader.name).collect::<Vec<_>>();
        names.sort_unstable();
        names.dedup();

        assert_eq!(names.len(), shaders.len());
    }

    #[test]
    fn all_shaders_expose_common_uniforms() {
        for shader in all_shaders() {
            assert_eq!(shader.uniforms, COMMON_UNIFORMS);
        }
    }

    #[test]
    fn descriptor_catalog_matches_sources() {
        let descriptors = shader_catalog();
        let sources = all_shaders();

        assert_eq!(descriptors.len(), sources.len());
        assert!(descriptors
            .iter()
            .all(|descriptor| descriptor.complexity > 0));
    }
}