euv-core 0.53.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
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
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
use super::*;

/// HTML tag name created when mounting a cross-platform canvas surface.
pub(crate) const WEB_CANVAS_TAG: &str = "canvas";

/// CSS custom property exposing `env(safe-area-inset-top)` without DOM mutation or layout reflow.
pub(crate) const WEB_CSS_SAFE_TOP: &str = "--euv-safe-top";

/// CSS custom property exposing `env(safe-area-inset-right)` without DOM mutation or layout reflow.
pub(crate) const WEB_CSS_SAFE_RIGHT: &str = "--euv-safe-right";

/// CSS custom property exposing `env(safe-area-inset-bottom)` without DOM mutation or layout reflow.
pub(crate) const WEB_CSS_SAFE_BOTTOM: &str = "--euv-safe-bottom";

/// CSS custom property exposing `env(safe-area-inset-left)` without DOM mutation or layout reflow.
pub(crate) const WEB_CSS_SAFE_LEFT: &str = "--euv-safe-left";

/// `window.getComputedStyle` property name.
pub(crate) const WEB_METHOD_GET_COMPUTED_STYLE: &str = "getComputedStyle";

/// `CSSStyleDeclaration.getPropertyValue` property name.
pub(crate) const WEB_METHOD_GET_PROPERTY_VALUE: &str = "getPropertyValue";

/// `navigator.gpu` WebGPU capability property name.
pub(crate) const WEB_PROP_GPU: &str = "gpu";

/// WebGL2 rendering context identifier passed to `HTMLCanvasElement.getContext`.
pub(crate) const WEB_CONTEXT_WEBGL2: &str = "webgl2";

/// WebGL2 context option key disabling alpha compositing overhead.
pub(crate) const WEB_GL_OPT_ALPHA: &str = "alpha";

/// WebGL2 context option key disabling MSAA overhead.
pub(crate) const WEB_GL_OPT_ANTIALIAS: &str = "antialias";

/// WebGL2 context option key disabling depth buffer allocation.
pub(crate) const WEB_GL_OPT_DEPTH: &str = "depth";

/// WebGL2 context option key requesting low-latency desynchronized presentation.
pub(crate) const WEB_GL_OPT_DESYNCHRONIZED: &str = "desynchronized";

/// WebGL2 context option key requesting discrete/high-performance GPU adapter.
pub(crate) const WEB_GL_OPT_POWER_PREF: &str = "powerPreference";

/// WebGL2 `powerPreference` value selecting the highest-throughput GPU.
pub(crate) const WEB_GL_OPT_HIGH_PERFORMANCE: &str = "high-performance";

/// WebGL2 context option key avoiding redundant drawing buffer copies.
pub(crate) const WEB_GL_OPT_PRESERVE_DRAWING: &str = "preserveDrawingBuffer";

/// Global browser `ResizeObserver` constructor name.
pub(crate) const WEB_CTOR_RESIZE_OBSERVER: &str = "ResizeObserver";

/// `ResizeObserver.observe` method name.
pub(crate) const WEB_METHOD_OBSERVE: &str = "observe";

/// `ResizeObserver.disconnect` method name.
pub(crate) const WEB_METHOD_DISCONNECT: &str = "disconnect";

/// WebGL2 `createTexture` method name.
pub(crate) const WEB_GL_CREATE_TEXTURE: &str = "createTexture";

/// WebGL2 `bindTexture` method name cached once at pipeline initialization.
pub(crate) const WEB_GL_BIND_TEXTURE: &str = "bindTexture";

/// `window.visualViewport` property name.
pub(crate) const WEB_PROP_VISUAL_VIEWPORT: &str = "visualViewport";

/// `VisualViewport.width` property name.
pub(crate) const WEB_PROP_WIDTH: &str = "width";

/// `VisualViewport.height` property name.
pub(crate) const WEB_PROP_HEIGHT: &str = "height";

/// `VisualViewport.scale` property name guarding pinch-zoom from shrinking the layout canvas backing store.
pub(crate) const WEB_PROP_SCALE: &str = "scale";

/// Minimum supported Web `devicePixelRatio` multiplier (supports `<100%` browser zoom cleanly).
pub(crate) const WEB_DPR_MIN: f32 = 0.25;

/// Maximum supported Web `devicePixelRatio` multiplier covering extreme Retina zoom factors losslessly.
pub(crate) const WEB_DPR_MAX: f32 = 16.0;

/// Soft physical Web `<canvas>` axis ceiling (`65536px`) removing the legacy `8192px` clamp on ultrawide/5K/8K viewports.
pub(crate) const WEB_CANVAS_MAX_PHYS_DIM: f64 = 65536.0;

/// Maximum 1:1 tile edge length in physical pixels for tiled lossless Canvas2D presentation above WebGL2 texture limits.
pub(crate) const WEB_TILED_BLIT_CHUNK_DIM: u32 = 4096;

/// Safe hardware upper bound in physical pixels for single-canvas WebGL2 presentation (`min(GL_MAX_TEXTURE_SIZE, 16384)`).
pub(crate) const WEB_GL2_MAX_SINGLE_SURFACE_DIM: u32 = 16384;

/// HTML `data-euv-tiled` attribute recording active lossless multi-tile WebGL2 grid geometry on the primary `<canvas>`.
pub(crate) const WEB_ATTR_DATA_TILED: &str = "data-euv-tiled";

/// Linear-filter scissor margin in physical pixels applied around partial-tile GPU scene passes to prevent seam artifacts.
pub(crate) const WEB_GL2_SCENE_SCISSOR_PAD_PX: i32 = 2;

/// Threshold above `1.0` indicating browser pinch-zoom is active on `window.visualViewport`.
pub(crate) const WEB_PINCH_ZOOM_THRESHOLD: f64 = 1.01;

/// `window.open` target name opening external links in a new browser tab.
pub(crate) const WEB_OPEN_TARGET_BLANK: &str = "_blank";

/// `window.open` feature flags preventing reverse tabnabbing on external links.
pub(crate) const WEB_OPEN_FEATURES_SAFE: &str = "noopener,noreferrer";

/// Uniform name binding sampler unit 0 in `WEB_GL2_BLIT_FRAG_SHADER`.
pub(crate) const WEB_GL2_UNIFORM_TEX: &str = "u_tex";

/// Uniform name carrying the physical drawing-buffer size (`vec2`) in `WEB_GL2_BLIT_FRAG_SHADER`.
pub(crate) const WEB_GL2_UNIFORM_VIEW: &str = "u_view";

/// Argument index key for single-argument `Reflect::construct` invocation.
pub(crate) const WEB_REFLECT_ARG0_KEY: &str = "0";

/// Inline style HTML attribute name.
pub(crate) const WEB_ATTR_STYLE: &str = "style";

/// DOM `id` attribute name used to deduplicate injected `<style>` elements.
pub(crate) const WEB_ATTR_ID: &str = "id";

/// HTML `<style>` tag name injected to suppress browser tap-highlight and focus-ring overlays.
pub(crate) const WEB_STYLE_TAG: &str = "style";

/// CSS selector locating the document `<head>` element without extra `web-sys` feature flags.
pub(crate) const WEB_HEAD_SELECTOR: &str = "head";

/// DOM id assigned to the injected zero-highlight browser reset stylesheet.
pub(crate) const WEB_RESET_STYLE_ID: &str = "euv-web-reset";

/// CSS rules suppressing full-canvas tap-highlight blue overlays, touch callouts, text selection, and focus outlines across Chrome, Safari, and Firefox.
pub(crate) const WEB_RESET_STYLE_CSS: &str = "html,body,canvas{-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;user-select:none;-webkit-user-select:none;outline:none;}canvas:focus,canvas:focus-visible{outline:none;}";

/// Tabindex HTML attribute name enabling keyboard focus on canvas.
pub(crate) const WEB_ATTR_TABINDEX: &str = "tabindex";

/// Default full-viewport body inline CSS eliminating scrollbars, tap highlights, and borders.
pub(crate) const WEB_BODY_STYLE: &str = "margin:0;padding:0;width:100vw;height:100vh;height:100dvh;overflow:hidden;background:#09090b;touch-action:none;user-select:none;-webkit-user-select:none;-webkit-touch-callout:none;-webkit-tap-highlight-color:transparent;outline:none;";

/// Inline CSS prefix for the full-viewport 1:1 physical canvas surface.
pub(crate) const WEB_CANVAS_STYLE_PREFIX: &str =
    "position:fixed;inset:0;width:100%;height:100%;display:block;outline:none;cursor:";

/// Inline CSS suffix disabling browser touch panning, tap-highlight overlays, callouts, and text selection on the active foreground canvas surface.
pub(crate) const WEB_CANVAS_STYLE_SUFFIX: &str = ";margin:0;touch-action:none;user-select:none;-webkit-user-select:none;-webkit-touch-callout:none;-webkit-tap-highlight-color:transparent;z-index:1;visibility:visible;opacity:1;pointer-events:auto;";

/// Inline CSS style keeping a pre-warming standby `<canvas>` hidden underneath the active canvas until atomic frame-boundary handoff.
pub(crate) const WEB_STANDBY_CANVAS_STYLE: &str = "position:fixed;inset:0;width:100%;height:100%;display:block;outline:none;margin:0;touch-action:none;user-select:none;-webkit-user-select:none;-webkit-touch-callout:none;-webkit-tap-highlight-color:transparent;z-index:0;visibility:hidden;opacity:0;pointer-events:none;";

/// HTML `data-euv-canvas-role` attribute name marking active vs standby presentation canvases.
pub(crate) const WEB_ATTR_CANVAS_ROLE: &str = "data-euv-canvas-role";

/// Role attribute value applied to the visible foreground `<canvas>`.
pub(crate) const WEB_CANVAS_ROLE_ACTIVE: &str = "active";

/// Role attribute value applied to a hidden pre-warming standby `<canvas>`.
pub(crate) const WEB_CANVAS_ROLE_STANDBY: &str = "standby";

/// Role attribute value applied to sibling tiled WebGL2 canvases for surfaces exceeding `GL_MAX_TEXTURE_SIZE`.
pub(crate) const WEB_CANVAS_ROLE_TILE: &str = "tile";

/// Fixed-position non-interactive inline CSS prefix for sibling tiled WebGL2 `<canvas>` slices.
pub(crate) const WEB_TILE_CANVAS_STYLE_PREFIX: &str =
    "position:fixed;display:block;outline:none;margin:0;pointer-events:none;z-index:2;";

/// Uniform name carrying the tile physical pixel origin (`vec2`) in `GLSL_ES3_UI_VERTEX_SHADER`.
pub(crate) const WEB_GL2_UBER_UNIFORM_TILE_ORIGIN: &str = "u_tile_origin";

/// Uniform name binding the Dual-Kawase blur source texture sampler in `GLSL_ES3_BACKDROP_BLUR_FRAGMENT_SHADER`.
pub(crate) const WEB_GL2_BLUR_UNIFORM_TEX: &str = "u_blur_tex";

/// Uniform name carrying the reciprocal source texture dimensions in `GLSL_ES3_BACKDROP_BLUR_FRAGMENT_SHADER`.
pub(crate) const WEB_GL2_BLUR_UNIFORM_TEXEL: &str = "u_texel_size";

/// Uniform name carrying the Dual-Kawase blur tap radius in `GLSL_ES3_BACKDROP_BLUR_FRAGMENT_SHADER`.
pub(crate) const WEB_GL2_BLUR_UNIFORM_OFFSET: &str = "u_blur_offset";

/// Uniform name selecting Dual-Kawase downsample (`0`) or upsample (`1`) pass.
pub(crate) const WEB_GL2_BLUR_UNIFORM_MODE: &str = "u_blur_pass_mode";

/// Uniform name for packed ocean timing, agitation, horizon, and aspect ratio.
pub(crate) const WEB_GL2_SCENE_UNIFORM_OCEAN_TIME: &str = "u_time_agit_hor_aspect";

/// Uniform name for packed ocean roll cosines and boat waterline UV.
pub(crate) const WEB_GL2_SCENE_UNIFORM_OCEAN_ROLL: &str = "u_roll_boat_uv";

/// Uniform name for packed ocean sun direction and active wake flag.
pub(crate) const WEB_GL2_SCENE_UNIFORM_OCEAN_SUN: &str = "u_sun_dir_wake";

/// Uniform name for ocean PBF interactive wake RGBA8 sampler.
pub(crate) const WEB_GL2_SCENE_UNIFORM_WAKE_TEX: &str = "u_wake_tex";

/// Uniform name for 3D column-major `projection * view` matrix.
pub(crate) const WEB_GL2_SCENE_UNIFORM_VIEW_PROJ: &str = "u_view_proj";

/// Uniform name for 3D camera world position and exposure.
pub(crate) const WEB_GL2_SCENE_UNIFORM_CAM_POS: &str = "u_camera_pos";

/// Uniform name for 3D sun direction and exponential fog density.
pub(crate) const WEB_GL2_SCENE_UNIFORM_SUN_FOG: &str = "u_sun_dir_fog";

/// Uniform name for 3D sun linear radiance and ambient strength.
pub(crate) const WEB_GL2_SCENE_UNIFORM_SUN_COL: &str = "u_sun_color_ambient";

/// Uniform name for 3D atmospheric horizon RGB and time-of-day.
pub(crate) const WEB_GL2_SCENE_UNIFORM_SKY_HOR: &str = "u_sky_horizon_tod";

/// Uniform name for 3D atmospheric zenith RGB and aspect ratio.
pub(crate) const WEB_GL2_SCENE_UNIFORM_SKY_ZEN: &str = "u_sky_zenith_aspect";

/// Fullscreen triangle vertex shader outputting top-down `v_uv` (`0.0` at top, `1.0` at bottom) for WebGL2 GPU ocean and 3D sky passes.
pub(crate) const WEB_GL2_SCENE_FULLSCREEN_VERT: &str = "#version 300 es\n\
precision highp float;\n\
precision highp int;\n\
out highp vec2 v_uv;\n\
void main() {\n\
    vec2 p = vec2((gl_VertexID == 1) ? 3.0 : -1.0, (gl_VertexID == 2) ? 3.0 : -1.0);\n\
    gl_Position = vec4(p, 0.0, 1.0);\n\
    v_uv = vec2(p.x * 0.5 + 0.5, 0.5 - p.y * 0.5);\n\
}\n";

/// Hardware WebGL2 photorealistic 8-wave isotropic Nyquist-AA Gerstner ocean, sunlit coastal golden-day sky, Toksvig dual-lobe GGX sun glitter, PBF wake, and hull foam fragment shader.
pub(crate) const WEB_GL2_SCENE_OCEAN_FRAG: &str = r#"#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
in highp vec2 v_uv;
uniform highp vec4 u_time_agit_hor_aspect;
uniform highp vec4 u_roll_boat_uv;
uniform highp vec4 u_sun_dir_wake;
uniform highp sampler2D u_wake_tex;
out vec4 out_color;
const vec4 EUV_OCEAN_WAVES[8] = vec4[8](
    vec4( 0.2419,  0.9703,  0.56, 0.205),
    vec4(-0.5592,  0.8290,  0.94, 0.145),
    vec4( 0.7986,  0.6018,  1.58, 0.092),
    vec4(-0.9455,  0.3256,  2.63, 0.056),
    vec4( 0.5299,  0.8480,  4.38, 0.034),
    vec4(-0.7880, -0.6157,  7.21, 0.021),
    vec4( 0.7314, -0.6820, 11.85, 0.013),
    vec4(-0.3746,  0.9272, 19.30, 0.0078)
);
const float EUV_OCEAN_SPEEDS[8] = float[8](0.84, 1.12, 1.48, 1.92, 2.46, 3.08, 3.82, 4.68);
vec3 euv_aces(vec3 hdr) {
    vec3 x = max(hdr, vec3(0.0));
    float luma = dot(x, vec3(0.2126, 0.7152, 0.0722));
    float sat_roll = 1.0 / (1.0 + luma * 0.14);
    vec3 agx_in = mix(vec3(luma), x, 0.78 + 0.22 * sat_roll);
    vec3 num = agx_in * (2.51 * agx_in + 0.03);
    vec3 den = agx_in * (2.43 * agx_in + 0.59) + 0.14;
    return pow(clamp(num / max(den, vec3(0.0001)), vec3(0.0), vec3(1.0)), vec3(1.0 / 2.2));
}
float euv_hash_i2(ivec2 cell) {
    uint n = uint(cell.x) * 374761393u + uint(cell.y) * 668265263u;
    n = (n ^ (n >> 13u)) * 1274126177u;
    n ^= n >> 16u;
    return float(n & 0x00FFFFFFu) * (1.0 / 16777215.0);
}
float euv_noise(vec2 p) {
    vec2 fl = floor(p);
    vec2 f = p - fl;
    vec2 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
    ivec2 i = ivec2(fl);
    float a = euv_hash_i2(i);
    float b = euv_hash_i2(i + ivec2(1, 0));
    float c = euv_hash_i2(i + ivec2(0, 1));
    float d = euv_hash_i2(i + ivec2(1, 1));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
float euv_cloud_fbm(vec2 p, int octs) {
    float val = 0.0;
    float amp = 0.54;
    vec2 q = p;
    for (int i = 0; i < 4; ++i) {
        if (i >= octs) {
            break;
        }
        val += euv_noise(q) * amp;
        q = vec2(q.x * 1.92 - q.y * 0.58 + 3.1, q.x * 0.58 + q.y * 1.92 - 1.7);
        amp *= 0.50;
    }
    return clamp(val, 0.0, 1.0);
}
vec3 euv_sky(vec3 ray, vec3 sun_dir, vec2 sun_az_dir, float sim_time, bool high_detail) {
    float ry = max(ray.y, 0.0);
    float sdot = clamp(dot(ray, sun_dir), 0.0, 1.0);
    float sp2 = sdot * sdot;
    float sp4 = sp2 * sp2;
    float sp16 = sp4 * sp4 * sp4 * sp4;
    float sp64 = sp16 * sp16 * sp16 * sp16;
    float az_d = clamp(dot(normalize(vec2(ray.x, ray.z)), sun_az_dir), 0.0, 1.0);
    float az2 = az_d * az_d;
    float sun_az_glow = az2 * az2;
    float disk = smoothstep(0.99910, 0.99968, sdot);
    float horiz_band = exp(-ry * 7.5);
    vec3 horiz_mist = vec3(0.56, 0.70, 0.76)
        + vec3(0.62, 0.34, 0.08) * (sun_az_glow * (0.45 + 0.55 * horiz_band))
        + vec3(0.85, 0.58, 0.24) * sp16
        + vec3(1.25, 0.96, 0.58) * sp64;
    vec3 mid_sky = high_detail ? vec3(0.11, 0.30, 0.42) : vec3(0.12, 0.32, 0.70);
    vec3 zenith = high_detail ? vec3(0.042, 0.165, 0.275) : vec3(0.050, 0.175, 0.52);
    float sky_t1 = smoothstep(0.0, 0.22, ry);
    float sky_t2 = smoothstep(0.14, 0.68, ry);
    vec3 base_sky = mix(mix(horiz_mist, mid_sky, sky_t1), zenith, sky_t2);
    base_sky += vec3(0.32, 0.22, 0.11) * (sp4 * (0.30 + 0.70 * horiz_band));
    float horiz_clear = smoothstep(0.014, 0.115, ry);
    float cloud_alpha = 0.0;
    if (horiz_clear > 0.001) {
        float t_shell = (sqrt(ry * ry + 0.08) - ry) * 25.0;
        vec2 base_c = ray.xz * (t_shell * 0.46) + vec2(sim_time * 0.016, -sim_time * 0.009);
        float warp_a = euv_noise(base_c * 0.48 + vec2(3.1, -1.4));
        float warp_b = high_detail ? euv_noise(base_c * 1.35 + vec2(-2.3, 5.7)) : 0.5;
        vec2 c_uv = base_c + vec2((warp_a - 0.5) * 0.72 + (warp_b - 0.5) * 0.24, (warp_a - 0.5) * 0.72 - (warp_b - 0.5) * 0.24);
        float cloud_raw = euv_cloud_fbm(c_uv, high_detail ? 4 : 2);
        float puff_a = high_detail ? (1.0 - abs(euv_noise(c_uv * 2.4 + vec2(8.4, -3.9)) * 2.0 - 1.0)) : 0.0;
        float puff_b = high_detail ? (1.0 - abs(euv_noise(c_uv * 5.2 + vec2(1.9, -2.6)) * 2.0 - 1.0)) : 0.0;
        float cloud_field = clamp(cloud_raw * 0.76 + puff_a * 0.16 + puff_b * 0.08, 0.0, 1.0);
        float clearing = sun_az_glow * exp(-ry * 5.5) * 0.20;
        float cloud_cov = smoothstep(0.50, 0.76, cloud_field - clearing);
        float light_sample = high_detail ? euv_cloud_fbm(c_uv + sun_dir.xz * 0.22, 3) : (cloud_field * 0.92);
        float sun_lit = clamp((cloud_field - light_sample) * 3.1 + 0.54, 0.0, 1.0);
        float puff_top = smoothstep(0.48, 0.84, cloud_field);
        float edge_thin = (1.0 - cloud_cov * 0.55) * smoothstep(0.04, 0.42, cloud_cov);
        float silver_gold = (edge_thin * (0.34 + 0.90 * sp4) + (1.0 - cloud_cov) * sp16 * 0.35) * (0.40 + 0.60 * sun_lit);
        vec3 cloud_shadow = vec3(0.32, 0.44, 0.58);
        vec3 cloud_bright = vec3(1.22, 1.18, 1.08);
        vec3 cloud_col = mix(cloud_shadow, cloud_bright, clamp(sun_lit * 0.72 + puff_top * 0.28, 0.0, 1.0));
        cloud_col += vec3(1.18, 0.94, 0.62) * (silver_gold * 0.56);
        float dist_aerial = smoothstep(0.015, 0.17, ry);
        cloud_col = mix(horiz_mist, cloud_col, dist_aerial);
        cloud_alpha = clamp(cloud_cov * 0.90 * horiz_clear, 0.0, 0.92);
        base_sky = mix(base_sky, cloud_col, cloud_alpha);
    }
    if (high_detail) {
        float sun_trans = 1.0 - cloud_alpha * 0.45;
        float corona_inner = exp(-(1.0 - sdot) * 420.0);
        float corona_outer = exp(-(1.0 - sdot) * 42.0);
        base_sky += (vec3(4.6, 3.95, 2.95) * disk + vec3(1.35, 1.02, 0.60) * corona_inner + vec3(0.32, 0.24, 0.13) * corona_outer) * sun_trans;
    }
    return base_sky;
}
void main() {
    const float INV_TAU = 0.15915494;
    const float TAU = 6.283185307;
    float sim_time = u_time_agit_hor_aspect.x;
    float agitation = max(u_time_agit_hor_aspect.y, 1.0);
    float horizon_v = u_time_agit_hor_aspect.z;
    float aspect = max(u_time_agit_hor_aspect.w, 0.25);
    float roll_cos = u_roll_boat_uv.x;
    float roll_sin = u_roll_boat_uv.y;
    vec2 boat_uv = u_roll_boat_uv.zw;
    vec3 sun_dir = normalize(u_sun_dir_wake.xyz);
    vec2 sun_az_dir = normalize(vec2(sun_dir.x, sun_dir.z));
    float wake_active = u_sun_dir_wake.w;
    float raw_dx = (v_uv.x - 0.5) * 2.0 * aspect;
    float raw_dy = (horizon_v - v_uv.y) * 2.0;
    float rx_u = raw_dx * roll_cos + raw_dy * roll_sin;
    float ry_u = -raw_dx * roll_sin + raw_dy * roll_cos;
    vec3 ray_u = vec3(rx_u, ry_u, 1.62);
    float inv_rlen = inversesqrt(dot(ray_u, ray_u));
    vec3 ray = ray_u * inv_rlen;
    float inv_res_y = max(abs(dFdy(v_uv.y)), 1.0 / 16384.0);
    float d_ry = (2.0 * inv_res_y) * inv_rlen * max(1.0 - ray.y * ray.y, 0.15);
    float d_rx = (2.0 * inv_res_y) * inv_rlen;
    vec3 hdr;
    if (ray.y >= 0.0) {
        hdr = euv_sky(ray, sun_dir, sun_az_dir, sim_time, true);
    } else {
        float neg_ry_true = max(-ray.y, 1.0e-4);
        float dist_true = 1.42 / neg_ry_true;
        float dist = min(dist_true, 600.0);
        float px_span_z = dist_true * (d_ry / neg_ry_true);
        float px_span_x = dist_true * d_rx;
        float px_span = max(px_span_z, px_span_x);
        vec2 wp0 = ray.xz * dist;
        if (dist < 18.0) {
            float p_fade = 1.0 - smoothstep(10.0, 18.0, dist);
            float t0 = fract(sim_time * 0.84 * INV_TAU);
            float ph0 = (fract(dot(wp0, vec2(0.2419, 0.9703)) * 0.56 * INV_TAU) + t0) * TAU;
            float h_p = 0.205 * agitation * (exp(sin(ph0) - 1.0) - 0.42) * p_fade;
            dist = clamp((1.42 - h_p) / neg_ry_true, 0.5, 600.0);
            wp0 = ray.xz * dist;
        }
        vec2 fwd_xz = normalize(ray.xz);
        vec2 right_xz = vec2(fwd_xz.y, -fwd_xz.x);
        vec2 wp = wp0;
        float h = 0.0;
        vec2 grad = vec2(0.0);
        float crest_acc = 0.0;
        float normal_var = 0.0009;
        for (int wi = 0; wi < 8; ++wi) {
            vec4 wave = EUV_OCEAN_WAVES[wi];
            vec2 wdir = wave.xy;
            float freq = wave.z;
            float amp = wave.w * agitation;
            float w_span = max(max(abs(dot(wdir, fwd_xz)) * px_span_z, abs(dot(wdir, right_xz)) * px_span_x), px_span_z * 0.55);
            float nyq = freq * w_span * 2.25;
            float aa_w = (nyq < 4.5) ? exp(-0.5 * nyq * nyq) : 0.0;
            float rms_s = wave.w * min(agitation, 1.45) * freq;
            normal_var += 0.10 * rms_s * rms_s * (1.0 - aa_w * aa_w);
            if (aa_w > 0.0005) {
                float t_ph = fract(sim_time * EUV_OCEAN_SPEEDS[wi] * INV_TAU);
                float s_ph = fract(dot(wp, wdir) * freq * INV_TAU);
                float ph = (s_ph + t_ph) * TAU;
                float sp = sin(ph);
                float cp = cos(ph);
                float steep_k = 0.42 + 0.58 * aa_w;
                float sharp_e = exp(-(1.0 - sp) * steep_k);
                h += amp * (sharp_e - 0.42) * aa_w;
                float slope = amp * freq * steep_k * sharp_e * cp * aa_w;
                grad += wdir * slope;
                crest_acc += sharp_e * amp * aa_w;
                wp += wdir * (cp * amp * 0.78 * aa_w);
            }
        }
        float near_steep = 1.0 + 0.36 * exp(-dist * 0.082);
        grad *= near_steep;
        float cap_nyq1 = px_span * 14.5 * 2.25;
        float cap_aa1 = (cap_nyq1 < 4.5) ? exp(-0.5 * cap_nyq1 * cap_nyq1) : 0.0;
        normal_var += 0.0012 * (1.0 - cap_aa1 * cap_aa1) * min(agitation, 1.45);
        if (cap_aa1 > 0.001) {
            float p1a = (fract(dot(wp0, vec2( 0.8572,  0.5150)) * 14.2 * INV_TAU) + fract(sim_time * 3.7 * INV_TAU)) * TAU;
            float p1b = (fract(dot(wp0, vec2(-0.4226,  0.9063)) * 18.7 * INV_TAU) + fract(sim_time * 4.4 * INV_TAU)) * TAU;
            float p1c = (fract(dot(wp0, vec2( 0.9613, -0.2756)) * 23.4 * INV_TAU) + fract(sim_time * 5.1 * INV_TAU)) * TAU;
            float g1a = cos(p1a) * 0.044 * cap_aa1 * agitation;
            float g1b = cos(p1b) * 0.036 * cap_aa1 * agitation;
            float g1c = cos(p1c) * 0.028 * cap_aa1 * agitation;
            grad += vec2(0.8572, 0.5150) * g1a + vec2(-0.4226, 0.9063) * g1b + vec2(0.9613, -0.2756) * g1c;
        }
        float cap_nyq2 = px_span * 34.0 * 2.25;
        float cap_aa2 = (cap_nyq2 < 4.5) ? exp(-0.5 * cap_nyq2 * cap_nyq2) : 0.0;
        normal_var += 0.0006 * (1.0 - cap_aa2 * cap_aa2) * min(agitation, 1.45);
        if (cap_aa2 > 0.001) {
            float p2a = (fract(dot(wp0, vec2(-0.7547, -0.6561)) * 31.8 * INV_TAU) + fract(sim_time * 6.2 * INV_TAU)) * TAU;
            float p2b = (fract(dot(wp0, vec2( 0.2924, -0.9563)) * 41.3 * INV_TAU) + fract(sim_time * 7.3 * INV_TAU)) * TAU;
            float g2a = cos(p2a) * 0.022 * cap_aa2 * agitation;
            float g2b = cos(p2b) * 0.016 * cap_aa2 * agitation;
            grad += vec2(-0.7547, -0.6561) * g2a + vec2(0.2924, -0.9563) * g2b;
        }
        normal_var = clamp(normal_var + min(px_span * 0.006, 0.016), 0.0005, 0.036);
        float wake_foam = 0.0;
        if (wake_active > 0.001) {
            vec2 wake_uv = clamp(v_uv, vec2(0.5 / 96.0, 0.5 / 72.0), vec2(1.0 - 0.5 / 96.0, 1.0 - 0.5 / 72.0));
            vec4 wk = texture(u_wake_tex, wake_uv);
            vec2 edge_d = min(v_uv, vec2(1.0) - v_uv);
            float edge_win = smoothstep(0.0, 0.045, edge_d.x) * smoothstep(0.0, 0.045, edge_d.y);
            float w_gain = clamp(wake_active, 0.0, 1.0) * edge_win;
            vec2 flow = clamp((wk.rg - vec2(128.0 / 255.0)) * 4.0 * 0.22, vec2(-0.20), vec2(0.20)) * w_gain;
            grad += flow;
            wake_foam = wk.b * w_gain;
        }
        vec2 raw_bd = vec2((v_uv.x - boat_uv.x) * aspect, v_uv.y - boat_uv.y);
        vec2 rot_bd = vec2(raw_bd.x * roll_cos - raw_bd.y * roll_sin, raw_bd.x * roll_sin + raw_bd.y * roll_cos);
        vec2 boat_local = vec2(rot_bd.x / 0.145, rot_bd.y / 0.036);
        float boat_ring_d2 = dot(boat_local, boat_local);
        if (boat_ring_d2 > 0.30 && boat_ring_d2 < 12.8) {
            float r_hull = sqrt(boat_ring_d2);
            float rip_env = exp(-max(r_hull - 0.85, 0.0) * 1.08) * smoothstep(0.55, 0.92, r_hull);
            float rip_nyq = px_span * 16.5 * 1.8;
            float rip_aa = (rip_nyq < 4.5) ? exp(-0.5 * rip_nyq * rip_nyq) : 0.0;
            float rip_ph = (fract(r_hull * 16.5 * INV_TAU) - fract(sim_time * 4.2 * INV_TAU)) * TAU;
            float rip_wave = cos(rip_ph) * 0.34 * rip_env * rip_aa;
            float inv_rh = 1.0 / max(r_hull, 0.1);
            grad += vec2(boat_local.x * inv_rh * rip_wave, boat_local.y * inv_rh * rip_wave * 1.38);
        }
        vec3 norm = normalize(vec3(-grad.x, 1.0, -grad.y));
        vec3 dndx = dFdx(norm);
        vec3 dndy = dFdy(norm);
        float dn_sq = dot(dndx, dndx) + dot(dndy, dndy);
        float screen_nvar = (neg_ry_true > d_ry * 2.5 && dn_sq < 64.0)
            ? min(dn_sq * 0.48, 0.16) * smoothstep(d_ry * 2.5, d_ry * 5.0, neg_ry_true)
            : 0.0;
        float eff_nvar = clamp(normal_var + screen_nvar, 0.0012, 0.18);
        vec3 view_dir = -ray;
        float ndv = clamp(dot(norm, view_dir), 0.005, 1.0);
        float om_nv = 1.0 - ndv;
        float om_nv2 = om_nv * om_nv;
        float fresnel = clamp(0.0204 + (0.94 - min(eff_nvar * 1.6, 0.34)) * (om_nv2 * om_nv2 * om_nv), 0.022, 0.90);
        vec3 refl_dir = reflect(ray, norm);
        refl_dir.y = max(refl_dir.y, 0.008);
        vec3 sky_refl = euv_sky(normalize(refl_dir), sun_dir, sun_az_dir, sim_time, false);
        vec3 half_vec = normalize(view_dir + sun_dir);
        float ndl = max(dot(norm, sun_dir), 0.0);
        float ndh = max(dot(norm, half_vec), 0.0);
        float vdh = clamp(dot(view_dir, half_vec), 0.0, 1.0);
        float om_vh = 1.0 - vdh;
        float om_vh2 = om_vh * om_vh;
        float fresnel_h = 0.0204 + 0.9796 * (om_vh2 * om_vh2 * om_vh);
        float a1_sq = 0.0012 + eff_nvar * 0.56;
        float d1 = ndh * ndh * (a1_sq - 1.0) + 1.0;
        float ndf1 = a1_sq / max(3.14159265 * d1 * d1, 1.0e-5);
        float k1 = sqrt(a1_sq) * 0.5;
        float vis1 = 0.25 / max((ndv * (1.0 - k1) + k1) * (ndl * (1.0 - k1) + k1), 0.014);
        float a2_sq = 0.0068 + eff_nvar * 0.72;
        float d2 = ndh * ndh * (a2_sq - 1.0) + 1.0;
        float ndf2 = a2_sq / max(3.14159265 * d2 * d2, 1.0e-5);
        float k2 = sqrt(a2_sq) * 0.5;
        float vis2 = 0.25 / max((ndv * (1.0 - k2) + k2) * (ndl * (1.0 - k2) + k2), 0.034);
        float core_e = min(ndf1 * vis1 * ndl * fresnel_h, 11.0);
        float sheen_e = min(ndf2 * vis2 * ndl * fresnel_h, 2.0);
        vec3 spec = vec3(1.52, 1.38, 1.12) * (core_e * 0.76) + vec3(1.28, 0.96, 0.60) * (sheen_e * 0.20);
        float sss_dot = clamp(dot(-view_dir, sun_dir), 0.0, 1.0);
        float sss = clamp(h * 2.35 + 0.28, 0.0, 1.0) * exp(-dist * 0.045) * (0.48 + 0.52 * (sss_dot * sss_dot * sss_dot));
        vec3 deep_body = vec3(0.004, 0.145, 0.54) + vec3(0.018, 0.52, 0.98) * sss;
        float refl_weight = clamp(fresnel * 0.54 + 0.035, 0.035, 0.72);
        hdr = mix(deep_body, sky_refl, refl_weight) + spec;
        if (rot_bd.y > -0.01 && rot_bd.y < 0.24 && abs(rot_bd.x) < 0.22) {
            float ripple = sin(rot_bd.y * 95.0 - sim_time * 4.5 + grad.x * 6.0) * 0.012;
            float rx_b = abs(rot_bd.x + ripple);
            float y_fade = smoothstep(-0.01, 0.032, rot_bd.y) * smoothstep(0.24, 0.10, rot_bd.y);
            float tri_mask = smoothstep(0.14, 0.02, rx_b + max(rot_bd.y, 0.0) * 0.55) * y_fade;
            hdr = mix(hdr, vec3(0.55, 0.52, 0.48), tri_mask * 0.32);
        }
        float crest_norm = crest_acc / max(agitation, 1.0);
        float steep_foam = smoothstep(0.54, 0.74, crest_norm) * clamp(agitation - 1.08, 0.0, 1.0);
        float total_foam = clamp(steep_foam * 0.45 + wake_foam * 0.28, 0.0, 0.54);
        hdr = mix(hdr, vec3(0.78, 0.90, 0.96), total_foam);
        if (boat_ring_d2 < 4.5) {
            if (boat_ring_d2 < 1.12) {
                float contact_ao = (1.0 - smoothstep(0.18, 1.06, boat_ring_d2)) * 0.44;
                hdr *= vec3(1.0 - contact_ao, 1.0 - contact_ao * 0.82, 1.0 - contact_ao * 0.72);
            }
            float foam_ny = boat_local.y - 0.16;
            float foam_d2 = boat_local.x * boat_local.x * 1.16 + foam_ny * foam_ny * 1.08;
            float rim_band = smoothstep(0.42, 0.82, foam_d2) * (1.0 - smoothstep(0.88, 1.54, foam_d2)) * smoothstep(-0.45, 0.24, boat_local.y);
            float aft_wake = smoothstep(0.22, 0.82, boat_local.x) * (1.0 - smoothstep(0.88, 1.62, boat_local.x)) * (1.0 - smoothstep(0.18, 1.20, abs(boat_local.y - 0.26))) * 0.46;
            float collar_mask = clamp(rim_band + aft_wake, 0.0, 1.0);
            if (collar_mask > 0.02) {
                float lacy_a = euv_noise(wp0 * 8.2 + vec2(2.3, -1.7));
                float lacy_b = euv_noise(wp0 * 21.0 + vec2(-sim_time * 0.85, 4.1));
                float flecks = euv_noise(wp0 * 42.0 + vec2(7.1, -sim_time * 1.35));
                float foam_pat = smoothstep(0.46, 0.78, lacy_a * 0.42 + lacy_b * 0.36 + flecks * 0.22);
                float spray_dots = smoothstep(0.74, 0.88, flecks) * rim_band * 0.72 * smoothstep(0.08, 0.015, px_span);
                float hull_foam = clamp(collar_mask * foam_pat * 0.78 + spray_dots, 0.0, 0.86);
                hdr = mix(hdr, vec3(0.92, 0.97, 1.0), hull_foam);
            }
        }
        float horiz_blend = exp(-neg_ry_true * 56.0);
        float dist_haze = 1.0 - exp(-dist * 0.0020);
        float pixel_hor_aa = 1.0 - smoothstep(0.0, d_ry * 1.75, neg_ry_true);
        float atm_mix = clamp(max(max(horiz_blend, dist_haze), pixel_hor_aa), 0.0, 1.0);
        float atm_w = clamp((atm_mix - 0.002) * (1.0 / 0.998), 0.0, 1.0);
        if (atm_w > 0.0) {
            vec3 fog_col = euv_sky(normalize(vec3(ray.x, max(0.0005, d_ry * 0.5), ray.z)), sun_dir, sun_az_dir, sim_time, false);
            hdr = mix(hdr, fog_col, atm_w);
        }
    }
    out_color = vec4(euv_aces(hdr), 1.0);
}
"#;

/// Hardware WebGL2 origami paper boat vertex shader.
pub(crate) const WEB_GL2_SCENE_BOAT_VERT: &str = "#version 300 es\n\
precision highp float;\n\
layout(location = 0) in vec4 a_pos_ndc;\n\
layout(location = 1) in vec4 a_color;\n\
out vec4 v_color;\n\
void main() {\n\
    gl_Position = vec4(a_pos_ndc.xy, 0.0, 1.0);\n\
    v_color = a_color;\n\
}\n";

/// Hardware WebGL2 origami paper boat fragment shader.
pub(crate) const WEB_GL2_SCENE_BOAT_FRAG: &str = "#version 300 es\n\
precision highp float;\n\
in vec4 v_color;\n\
out vec4 out_color;\n\
void main() {\n\
    out_color = v_color;\n\
}\n";

/// Hardware WebGL2 world-space 3D atmospheric sky gradient and sun fragment shader.
pub(crate) const WEB_GL2_SCENE_3D_SKY_FRAG: &str = r#"#version 300 es
precision highp float;
in highp vec2 v_uv;
uniform highp mat4 u_view_proj;
uniform highp vec4 u_camera_pos;
uniform highp vec4 u_sun_dir_fog;
uniform highp vec4 u_sun_color_ambient;
uniform highp vec4 u_sky_horizon_tod;
uniform highp vec4 u_sky_zenith_aspect;
out vec4 out_color;
vec3 euv_aces(vec3 hdr) {
    vec3 x = max(hdr, vec3(0.0));
    vec3 num = x * (2.51 * x + 0.03);
    vec3 den = x * (2.43 * x + 0.59) + 0.14;
    return pow(clamp(num / max(den, vec3(0.0001)), vec3(0.0), vec3(1.0)), vec3(1.0 / 2.2));
}
float euv_sky_hash(vec2 cell) {
    vec2 p = fract(cell * vec2(0.1031, 0.1030));
    p += dot(p, p.yx + 33.33);
    return fract((p.x + p.y) * p.x);
}
float euv_sky_noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = euv_sky_hash(i);
    float b = euv_sky_hash(i + vec2(1.0, 0.0));
    float c = euv_sky_hash(i + vec2(0.0, 1.0));
    float d = euv_sky_hash(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}
void main() {
    vec2 ndc = vec2(v_uv.x * 2.0 - 1.0, 1.0 - v_uv.y * 2.0);
    vec3 r0 = vec3(u_view_proj[0].x, u_view_proj[1].x, u_view_proj[2].x);
    vec3 r1 = vec3(u_view_proj[0].y, u_view_proj[1].y, u_view_proj[2].y);
    vec3 r3 = vec3(u_view_proj[0].w, u_view_proj[1].w, u_view_proj[2].w);
    vec3 c0 = cross(r1, r3);
    vec3 c1 = cross(r3, r0);
    vec3 c2 = cross(r0, r1);
    float det = dot(r0, c0);
    float sgn = (det < 0.0) ? -1.0 : 1.0;
    vec3 world_ray = normalize((c0 * ndc.x + c1 * ndc.y + c2) * sgn);
    vec3 sun_dir = normalize(u_sun_dir_fog.xyz);
    float ry = max(world_ray.y, 0.0);
    float sky_t = smoothstep(0.0, 0.62, pow(ry, 0.72));
    vec3 col = mix(u_sky_horizon_tod.rgb, u_sky_zenith_aspect.rgb, sky_t);
    if (world_ray.y < 0.0) {
        col = mix(u_sky_horizon_tod.rgb, u_sky_horizon_tod.rgb * 0.52, smoothstep(0.0, 0.35, -world_ray.y));
    }
    float sdot = clamp(dot(world_ray, sun_dir), 0.0, 1.0);
    float sp2 = sdot * sdot;
    float sp4 = sp2 * sp2;
    float sp8 = sp4 * sp4;
    float horiz_band = exp(-ry * 6.5);
    float sun_aureole = exp(-(1.0 - sdot) * 28.0);
    float sun_corona = exp(-(1.0 - sdot) * 420.0);
    float sun_disk = smoothstep(0.99925, 0.99975, sdot);
    float above_hor = smoothstep(-0.02, 0.03, world_ray.y);
    col += u_sun_color_ambient.rgb * (sp8 * 0.36 * horiz_band + sun_aureole * 0.42 + (sun_corona * 0.95 + sun_disk * 3.4) * above_hor);
    if (ry > 0.015) {
        vec2 dome_uv = world_ray.xz / (ry + 0.16);
        vec2 cloud_p = dome_uv * 0.85 + vec2(u_sky_horizon_tod.w * 0.04, -u_sky_horizon_tod.w * 0.02);
        float wisp_n = euv_sky_noise(cloud_p * 1.45) * 0.65 + euv_sky_noise(cloud_p * 3.10 + vec2(4.2, -2.7)) * 0.35;
        float wisp_mask = smoothstep(0.42, 0.78, wisp_n) * smoothstep(0.015, 0.16, ry) * (1.0 - smoothstep(0.55, 0.92, ry));
        vec3 wisp_col = u_sky_horizon_tod.rgb * 1.15 + u_sun_color_ambient.rgb * (0.14 + 0.34 * sp4);
        col = mix(col, wisp_col, wisp_mask * 0.28);
    }
    out_color = vec4(euv_aces(col * u_camera_pos.w), 1.0);
}
"#;

/// Hardware WebGL2 instanced 3D PBR open-world vertex shader.
pub(crate) const WEB_GL2_SCENE_3D_MESH_VERT: &str = r#"#version 300 es
precision highp float;
layout(location = 0) in vec4 a_pos;
layout(location = 1) in vec4 a_norm;
layout(location = 2) in vec4 a_submat;
layout(location = 3) in vec4 a_m0;
layout(location = 4) in vec4 a_m1;
layout(location = 5) in vec4 a_m2;
layout(location = 6) in vec4 a_m3;
layout(location = 7) in vec4 a_albedo_met;
layout(location = 8) in vec4 a_emissive_rough;
uniform highp mat4 u_view_proj;
out highp vec3 v_world_pos;
out highp vec3 v_world_norm;
out highp vec4 v_base_met;
out highp vec4 v_emissive_rough;
out highp float v_submat_mode;
out highp float v_detail_mode;
void main() {
    float detail_mode = a_m3.w;
    vec4 m3_fixed = vec4(a_m3.xyz, 1.0);
    mat4 model = mat4(a_m0, a_m1, a_m2, m3_fixed);
    vec4 wp = model * vec4(a_pos.xyz, 1.0);
    gl_Position = u_view_proj * wp;
    vec3 c0 = a_m0.xyz;
    vec3 c1 = a_m1.xyz;
    vec3 c2 = a_m2.xyz;
    mat3 cof = mat3(cross(c1, c2), cross(c2, c0), cross(c0, c1));
    v_world_pos = wp.xyz;
    v_world_norm = normalize(cof * a_norm.xyz);
    v_base_met = (a_submat.w > 0.5)
        ? vec4(a_submat.rgb, a_albedo_met.w)
        : vec4(a_albedo_met.rgb * a_submat.rgb, a_albedo_met.w);
    v_emissive_rough = a_emissive_rough;
    v_submat_mode = a_submat.w;
    v_detail_mode = detail_mode;
}
"#;

/// Hardware WebGL2 instanced 3D PBR open-world fragment shader (analytical ray-traced canyon & clearcoat reflections, soft contact AO, multi-bounce GI, glowing skyscraper facades, wet boulevard puddles, streetlights, and volumetric haze + ACES).
pub(crate) const WEB_GL2_SCENE_3D_MESH_FRAG: &str = r#"#version 300 es
precision highp float;
in highp vec3 v_world_pos;
in highp vec3 v_world_norm;
in highp vec4 v_base_met;
in highp vec4 v_emissive_rough;
in highp float v_submat_mode;
in highp float v_detail_mode;
uniform highp vec4 u_camera_pos;
uniform highp vec4 u_sun_dir_fog;
uniform highp vec4 u_sun_color_ambient;
uniform highp vec4 u_sky_horizon_tod;
uniform highp vec4 u_sky_zenith_aspect;
out vec4 out_color;
vec3 euv_aces(vec3 hdr) {
    vec3 x = max(hdr, vec3(0.0));
    vec3 num = x * (2.51 * x + 0.03);
    vec3 den = x * (2.43 * x + 0.59) + 0.14;
    return pow(clamp(num / max(den, vec3(0.0001)), vec3(0.0), vec3(1.0)), vec3(1.0 / 2.2));
}
float euv_hash(vec2 cell) {
    vec2 p = fract(cell * vec2(0.1031, 0.1030));
    p += dot(p, p.yx + 33.33);
    return fract((p.x + p.y) * p.x);
}
float euv_noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    vec4 px = fract(vec4(i.x, i.x + 1.0, i.x, i.x + 1.0) * 0.1031);
    vec4 py = fract(vec4(i.y, i.y, i.y + 1.0, i.y + 1.0) * 0.1030);
    vec4 d = px * (py + 33.33) + py * (px + 33.33);
    px += d;
    py += d;
    vec4 h = fract((px + py) * px);
    vec2 m = mix(h.xz, h.yw, u.x);
    return mix(m.x, m.y, u.y);
}
float euv_box_pulse(float x, float a, float b, float fw) {
    float w = b - a;
    float f = max(fw, 1.0e-4);
    float x0 = x - 0.5 * f;
    float x1 = x + 0.5 * f;
    float i0 = floor(x0) * w + clamp(fract(x0) - a, 0.0, w);
    float i1 = floor(x1) * w + clamp(fract(x1) - a, 0.0, w);
    return clamp((i1 - i0) / f, 0.0, 1.0);
}
float euv_box_band(float x, float lo, float hi, float fw) {
    float f = max(fw, 1.0e-4);
    float x0 = x - 0.5 * f;
    float x1 = x + 0.5 * f;
    return clamp((min(x1, hi) - max(x0, lo)) / f, 0.0, 1.0);
}
float euv_box_step(float x, float edge, float fw) {
    float f = max(fw, 1.0e-4);
    return clamp((x - edge) / f + 0.5, 0.0, 1.0);
}
vec3 euv_trace_city_reflection(
    vec3 pos,
    vec3 refl_dir,
    vec3 sun_dir,
    vec3 sky_hor,
    vec3 sky_zen,
    vec3 sun_col,
    float roughness
) {
    vec3 env_col = mix(sky_hor, sky_zen, clamp(refl_dir.y * 0.5 + 0.5, 0.0, 1.0));
    float s_dot = max(dot(refl_dir, sun_dir), 0.0);
    float s2 = s_dot * s_dot;
    float s4 = s2 * s2;
    float s16 = s4 * s4 * s4 * s4;
    env_col += sun_col * (s4 * 0.22 + s16 * s16 * 1.45);
    if (abs(refl_dir.x) > 0.04 && abs(pos.x) < 13.5) {
        float wall_x = (refl_dir.x > 0.0) ? 12.6 : -12.6;
        float t_wall = (wall_x - pos.x) / refl_dir.x;
        if (t_wall > 0.25 && t_wall < 95.0) {
            float hit_y = pos.y + refl_dir.y * t_wall;
            if (hit_y > 0.0 && hit_y < 44.0) {
                float hit_z = pos.z + refl_dir.z * t_wall;
                float atten = exp(-t_wall * 0.022) * (1.0 - smoothstep(32.0, 44.0, hit_y));
                vec3 facade_col = sky_hor * 0.24;
                float refl_blur = clamp(roughness * 1.8 + t_wall * 0.012, 0.08, 1.25);
                if (hit_y < 3.45) {
                    float su_s = hit_z * 0.26;
                    float pane = euv_box_pulse(su_s, 0.10, 0.90, refl_blur);
                    float sh = euv_hash(vec2(floor(su_s), 7.0));
                    vec3 shop = (sh < 0.40) ? vec3(1.25, 0.80, 0.36) : ((sh < 0.62) ? vec3(0.28, 0.92, 1.24) : ((sh < 0.80) ? vec3(1.18, 0.34, 0.72) : vec3(0.05)));
                    float shop_coh = 1.0 - smoothstep(0.22, 0.80, refl_blur);
                    facade_col += mix(vec3(0.76, 0.58, 0.54), shop, shop_coh) * (pane * 0.95);
                } else {
                    float su = hit_z * 0.48;
                    float sy = hit_y * 0.33;
                    float pane = euv_box_pulse(su, 0.14, 0.86, refl_blur) * euv_box_pulse(sy, 0.18, 0.84, refl_blur);
                    float ch = euv_hash(vec2(floor(su), floor(sy)));
                    vec3 win_glow = (ch < 0.22) ? vec3(1.24, 0.86, 0.42)
                        : ((ch < 0.33) ? vec3(0.28, 0.86, 1.24)
                        : ((ch < 0.42) ? vec3(0.96, 0.94, 0.88)
                        : ((ch < 0.50) ? vec3(1.06, 0.34, 0.72) : vec3(0.04, 0.06, 0.10))));
                    float win_coh = 1.0 - smoothstep(0.20, 0.75, refl_blur);
                    facade_col += mix(vec3(0.49, 0.40, 0.36), win_glow, win_coh) * (pane * 0.92);
                }
                float lamp_z = abs(fract((hit_z + 12.0) / 24.0) - 0.5) * 24.0;
                float lamp_dy = abs(hit_y - 6.4);
                float lamp_glow = exp(-(lamp_z * lamp_z * 0.28 + lamp_dy * lamp_dy * 0.65));
                facade_col += vec3(1.65, 1.22, 0.68) * lamp_glow;
                env_col = mix(env_col, facade_col, clamp(atten * 0.86, 0.0, 0.88));
            }
        }
    }
    return env_col;
}
void main() {
    vec3 norm = normalize(v_world_norm);
    vec3 to_cam = u_camera_pos.xyz - v_world_pos;
    float dist = max(length(to_cam), 0.0001);
    vec3 view_dir = to_cam / dist;
    vec3 sun_dir = normalize(u_sun_dir_fog.xyz);
    vec3 fw_p = fwidth(v_world_pos);
    vec3 base_col = v_base_met.rgb;
    float metallic = clamp(v_base_met.w, 0.0, 1.0);
    float roughness = clamp(v_emissive_rough.w, 0.05, 1.0);
    vec3 emissive = (v_submat_mode > 1.5) ? base_col * 1.55 : v_emissive_rough.rgb;
    float env_boost = 0.92;
    float clearcoat = 0.0;
    float contact_ao = 1.0;
    vec3 lamp_gi = vec3(0.0);
    if (v_detail_mode > 0.5 && v_detail_mode < 1.5 && norm.y > 0.82) {
        float wpx = v_world_pos.x;
        float wpz = v_world_pos.z;
        float abs_x = abs(wpx);
        float fw_x = max(fw_p.x, 0.015);
        float fw_z = max(fw_p.z, 0.015);
        float curb_w = euv_box_step(abs_x, 9.9, fw_x);
        float walk_w = euv_box_step(abs_x, 10.6, fw_x);
        float micro_fade = (1.0 - smoothstep(14.0, 48.0, dist)) * (1.0 - smoothstep(0.06, 0.22, max(fw_x, fw_z)));
        float grain_n = (micro_fade > 0.005) ? (euv_hash(floor(vec2(wpx * 4.3, wpz * 4.3))) - 0.5) : 0.0;
        float om_curb = 1.0 - curb_w;
        float inner_lin = 1.0 - clamp(abs(abs_x - 3.65) * 0.85, 0.0, 1.0);
        float outer_lin = 1.0 - clamp(abs(abs_x - 6.85) * 0.95, 0.0, 1.0);
        float tire_wear = clamp(inner_lin * inner_lin + outer_lin * outer_lin, 0.0, 1.0) * om_curb;
        float gutter_band = (1.0 - clamp(abs(abs_x - 9.75) * 2.8, 0.0, 1.0)) * om_curb;
        float puddle_fade = 1.0 - smoothstep(48.0, 120.0, dist);
        float puddle = 0.0;
        if (puddle_fade > 0.01 && om_curb > 0.01) {
            float p_coarse = euv_noise(vec2(wpx * 0.14 + 3.1, wpz * 0.08 - 1.7));
            float p_fine = euv_noise(vec2(wpx * 0.38 - 5.4, wpz * 0.24 + 2.9));
            float center_crown = (1.0 - clamp(abs_x * 0.65, 0.0, 1.0)) * om_curb;
            float puddle_raw = p_coarse * 0.66 + p_fine * 0.34 + tire_wear * 0.14 + gutter_band * 0.16 - center_crown * 0.24;
            puddle = smoothstep(0.44, 0.90, puddle_raw) * om_curb * puddle_fade;
        }
        float tarmac_scale = (1.0 + grain_n * 0.24 * micro_fade * (1.0 - puddle)) * (1.0 - 0.14 * tire_wear) * (1.0 - 0.38 * puddle) * (1.0 - 0.30 * gutter_band);
        vec3 road_col = base_col * tarmac_scale;
        float yellow_w = euv_box_band(abs_x, 0.135, 0.305, fw_x) * om_curb;
        if (yellow_w > 0.005) {
            float pv = (1.0 - 0.14 * puddle) * (0.92 + grain_n * 0.14 * micro_fade);
            road_col = mix(road_col, vec3(0.88, 0.62, 0.10) * pv, yellow_w * 0.94);
        }
        float dash_on = euv_box_pulse(wpz * 0.125, 0.0, 0.42, fw_z * 0.125);
        float lane_w = euv_box_band(abs_x, 4.565, 4.735, fw_x) * dash_on * om_curb;
        float shoulder_w = euv_box_band(abs_x, 8.85, 9.05, fw_x) * om_curb;
        float cross_z = euv_box_pulse((wpz + 18.0) * 0.025, 0.455, 0.545, fw_z * 0.025);
        float cross_x_mask = euv_box_band(abs_x, 0.55, 8.40, fw_x);
        float cross_bars = euv_box_pulse(wpx * 0.85, 0.0, 0.50, fw_x * 0.85);
        float cross_w = cross_z * cross_x_mask * cross_bars * 0.85 * om_curb;
        float white_w = max(max(shoulder_w, lane_w), cross_w);
        if (white_w > 0.005) {
            float pv = (1.0 - 0.16 * puddle) * (0.90 + grain_n * 0.12 * micro_fade);
            road_col = mix(road_col, vec3(0.78, 0.81, 0.86) * pv, white_w * 0.90);
        }
        vec3 curb_col = vec3(0.36, 0.38, 0.42) * (1.0 + grain_n * 0.10 * micro_fade);
        float seam_u = euv_box_pulse(wpx * 0.45, 0.0, 0.045, fw_x * 0.45);
        float seam_v = euv_box_pulse(wpz * 0.45, 0.0, 0.045, fw_z * 0.45);
        float tile_seam = mix(1.0, 0.72, clamp(seam_u + seam_v - seam_u * seam_v, 0.0, 1.0));
        vec3 walk_col = vec3(0.24, 0.26, 0.30) * tile_seam * (1.0 + grain_n * 0.10 * micro_fade);
        base_col = mix(mix(road_col, curb_col, curb_w), walk_col, walk_w);
        roughness = mix(mix(roughness, 0.055, puddle), 0.78, walk_w);
        env_boost = mix(0.92, 2.05, puddle);
        clearcoat = puddle * 0.85;
        float curb_crevice = exp(-abs(abs_x - 10.15) * 4.5);
        float wall_ao = smoothstep(12.8, 10.8, abs_x);
        contact_ao = (1.0 - curb_crevice * 0.28) * mix(0.72, 1.0, wall_ao);
        float lamp_dz = abs(fract((wpz + 12.0) / 24.0) - 0.5) * 24.0;
        float lamp_dx = abs(abs_x - 9.0);
        float lamp_pool = exp(-(lamp_dz * lamp_dz * 0.032 + lamp_dx * lamp_dx * 0.048));
        lamp_gi = vec3(0.92, 0.64, 0.32) * (lamp_pool * (0.34 + 0.36 * puddle));
    } else if (v_detail_mode > 1.5 && v_detail_mode < 2.5 && abs(norm.y) < 0.35 && v_submat_mode < 0.5) {
        float dist_fade = 1.0 - smoothstep(130.0, 320.0, dist);
        contact_ao = mix(0.68, 1.0, smoothstep(0.0, 2.4, v_world_pos.y));
        if (dist_fade > 0.001) {
            float u_coord = (abs(norm.x) > abs(norm.z)) ? v_world_pos.z : v_world_pos.x;
            float fw_u_w = (abs(norm.x) > abs(norm.z)) ? fw_p.z : fw_p.x;
            float world_y = v_world_pos.y;
            float fw_y_w = fw_p.y;
            if (world_y < 3.45) {
                float su_s = u_coord * 0.26;
                float fw_su_s = max(fw_u_w * 0.26, 0.005);
                float fy_s = clamp(world_y / 3.45, 0.0, 1.0);
                float fw_fy_s = max(fw_y_w / 3.45, 0.005);
                float pane_w = euv_box_pulse(su_s, 0.11, 0.89, fw_su_s) * euv_box_band(fy_s, 0.18, 0.89, fw_fy_s) * dist_fade;
                float sh = euv_hash(vec2(floor(su_s), 7.0));
                vec3 shop_glow = (sh < 0.40) ? vec3(1.18, 0.78, 0.36) : ((sh < 0.62) ? vec3(0.28, 0.88, 1.18) : ((sh < 0.80) ? vec3(1.12, 0.32, 0.68) : vec3(0.0)));
                float shop_coh = 1.0 - smoothstep(0.28, 0.78, fw_su_s);
                vec3 shop_eff = mix(vec3(0.74, 0.56, 0.52), shop_glow, shop_coh);
                base_col = mix(base_col * 0.64, base_col * 0.40, pane_w);
                emissive += shop_eff * (pane_w * (0.55 + 0.55 * (1.0 - fy_s)) * 0.96);
                roughness = mix(roughness, 0.10, pane_w);
                env_boost = mix(0.92, 1.55, pane_w);
            } else {
                float su = u_coord * 0.48;
                float sy = world_y * 0.33;
                float fw_su = max(fw_u_w * 0.48, 0.005);
                float fw_sy = max(fw_y_w * 0.33, 0.005);
                float fy_win = fract(sy);
                float pane_u = euv_box_pulse(su, 0.14, 0.86, fw_su);
                float pane_v = euv_box_pulse(sy, 0.18, 0.84, fw_sy);
                float pane_w = clamp(pane_u * pane_v * dist_fade, 0.0, 1.0);
                float spandrel = mix(0.70, 0.84, euv_box_pulse(sy, 0.14, 0.88, fw_sy));
                vec3 wall_base = base_col * spandrel;
                float ch = euv_hash(vec2(floor(su), floor(sy)));
                float room_lvl = 0.68 + fract(ch * 17.3) * 0.45;
                float blind_grad = clamp(0.48 + 0.66 * (1.0 - fy_win), 0.38, 1.14);
                float cell_detail = 1.0 - smoothstep(0.22, 0.72, max(fw_su, fw_sy));
                vec3 cell_em = vec3(0.0);
                float cell_glass_mul = clamp(0.36 + 0.22 * fy_win, 0.32, 0.62);
                if (ch < 0.22) {
                    cell_glass_mul = 0.86;
                    cell_em = vec3(1.24, 0.86, 0.42) * (room_lvl * blind_grad);
                } else if (ch < 0.33) {
                    cell_glass_mul = 0.84;
                    cell_em = vec3(0.28, 0.86, 1.24) * (room_lvl * blind_grad);
                } else if (ch < 0.42) {
                    cell_glass_mul = 0.88;
                    cell_em = vec3(0.96, 0.94, 0.88) * (room_lvl * blind_grad * 0.82);
                } else if (ch < 0.50) {
                    cell_glass_mul = 0.84;
                    cell_em = vec3(1.06, 0.34, 0.72) * (room_lvl * blind_grad * 0.86);
                }
                vec3 mean_em = vec3(0.35, 0.28, 0.25);
                base_col = mix(wall_base, base_col * mix(0.65, cell_glass_mul, cell_detail), pane_w);
                emissive += mix(mean_em, cell_em, cell_detail) * pane_w;
                roughness = mix(roughness, mix(0.24, 0.09, cell_detail), pane_w);
                env_boost = mix(0.92, 1.55, pane_w);
            }
        }
    } else if (v_detail_mode > 2.5 && norm.y > 0.80) {
        float fw_xz = length(fw_p.xz);
        vec2 wp = v_world_pos.xz;
        float aa_w = 1.0 / (1.0 + fw_xz * 0.85);
        float w_a = cos(wp.x * 0.42 + wp.y * 0.64 + u_sky_horizon_tod.w * 2.4) * 0.08 * aa_w;
        float w_b = cos(-wp.x * 0.78 + wp.y * 0.52 + u_sky_horizon_tod.w * 3.1) * 0.06 * aa_w;
        norm = normalize(vec3(w_a - w_b, 1.0, w_a + w_b));
        base_col = vec3(0.015, 0.095, 0.145);
        roughness = 0.07;
        metallic = 0.02;
        env_boost = 1.85;
        clearcoat = 0.65;
    } else if (v_detail_mode < 0.5 && v_submat_mode < 1.5) {
        contact_ao = mix(0.62, 1.0, smoothstep(0.05, 0.72, v_world_pos.y));
        if (metallic > 0.20 || roughness < 0.38) {
            clearcoat = 0.88;
            env_boost = 1.65;
        }
    }
    vec3 dndx = dFdx(norm);
    vec3 dndy = dFdy(norm);
    float norm_var = clamp(dot(dndx, dndx) + dot(dndy, dndy), 0.0, 0.50);
    float rough_aa = sqrt(clamp(roughness * roughness + min(norm_var * 0.50, 0.18), 0.0025, 1.0));
    vec3 half_vec = normalize(view_dir + sun_dir);
    float ndl = max(dot(norm, sun_dir), 0.0);
    float ndv = max(dot(norm, view_dir), 0.002);
    float ndh = max(dot(norm, half_vec), 0.0);
    float hdv = max(dot(half_vec, view_dir), 0.0);
    float r2 = rough_aa * rough_aa;
    float a2 = max(r2 * r2, 0.0004);
    float d_den = ndh * ndh * (a2 - 1.0) + 1.0;
    float ndf = a2 / max(3.14159265 * d_den * d_den, 0.0001);
    vec3 f0 = mix(vec3(0.04), base_col, metallic);
    float om_hdv = 1.0 - hdv;
    float om_hdv2 = om_hdv * om_hdv;
    float f_pow5 = om_hdv2 * om_hdv2 * om_hdv;
    vec3 fresnel = f0 + (vec3(1.0) - f0) * f_pow5;
    float k_g = (rough_aa + 1.0);
    k_g = (k_g * k_g) * 0.125;
    float g_v = ndv / (ndv * (1.0 - k_g) + k_g);
    float g_l = ndl / (ndl * (1.0 - k_g) + k_g);
    vec3 spec = (ndf * g_v * g_l * fresnel) / max(4.0 * ndv * ndl, 0.001);
    if (clearcoat > 0.01) {
        float cc_a2 = clamp(0.0045 + norm_var * 0.45, 0.0045, 0.25);
        float cc_den = ndh * ndh * (cc_a2 - 1.0) + 1.0;
        float cc_ndf = min(cc_a2 / max(3.14159265 * cc_den * cc_den, 0.0001), 24.0);
        float cc_f = 0.04 + 0.96 * f_pow5;
        spec += vec3(cc_ndf * cc_f * clearcoat * 0.28);
    }
    vec3 kd = (vec3(1.0) - fresnel) * (1.0 - metallic);
    vec3 direct = (kd * base_col / 3.14159265 + spec) * u_sun_color_ambient.rgb * ndl * 1.48;
    float sky_w = norm.y * 0.5 + 0.5;
    vec3 ground_bounce = u_sky_horizon_tod.rgb * 0.36 + vec3(0.05, 0.045, 0.065);
    vec3 hemi = mix(ground_bounce, u_sky_zenith_aspect.rgb * 0.92 + u_sky_horizon_tod.rgb * 0.38, sky_w);
    vec3 refl_dir = reflect(-view_dir, norm);
    vec3 env_refl = (env_boost > 1.10 || clearcoat > 0.10)
        ? euv_trace_city_reflection(
            v_world_pos,
            refl_dir,
            sun_dir,
            u_sky_horizon_tod.rgb,
            u_sky_zenith_aspect.rgb,
            u_sun_color_ambient.rgb,
            rough_aa
        )
        : mix(u_sky_horizon_tod.rgb, u_sky_zenith_aspect.rgb, clamp(refl_dir.y * 0.5 + 0.5, 0.0, 1.0));
    float om_ndv = 1.0 - ndv;
    float om_ndv2 = om_ndv * om_ndv;
    float ndv_pow5 = om_ndv2 * om_ndv2 * om_ndv;
    vec3 f_env = f0 + (max(vec3(1.0 - rough_aa), f0) - f0) * ndv_pow5;
    vec3 cc_env = env_refl * (0.04 + 0.96 * ndv_pow5) * (clearcoat * 0.72);
    vec3 ambient = (base_col * (1.0 - metallic * 0.65) * (hemi + lamp_gi) + (env_refl * f_env * env_boost + cc_env)) * (0.46 + u_sun_color_ambient.w) * contact_ao;
    vec3 hdr = direct + ambient + emissive;
    float fog_t = 1.0 - exp(-dist * u_sun_dir_fog.w);
    float sun_scatter = max(dot(-view_dir, sun_dir), 0.0);
    float ss2 = sun_scatter * sun_scatter;
    vec3 fog_col = mix(u_sky_horizon_tod.rgb, u_sun_color_ambient.rgb * 0.85, ss2 * ss2 * 0.38);
    hdr = mix(hdr, fog_col, clamp(fog_t, 0.0, 0.86));
    out_color = vec4(euv_aces(hdr * u_camera_pos.w), 1.0);
}
"#;

/// Error message when WebGL2 direct GPU presentation is requested without a live WebGL2 direct pipeline.
pub(crate) const ERR_WEB_GL2_DIRECT_MISSING: &str =
    "WebGL2 direct GPU primitive pipeline unavailable on active canvas";

/// Error message when a WebGL2 context cannot be acquired on a fresh independent canvas.
pub(crate) const ERR_WEB_GL2_CONTEXT_MISSING: &str =
    "failed to acquire independent webgl2 canvas rendering context";

/// WebGL2 direct GPU primitive vertex shader (`#version 300 es`, `precision highp float`) drawing screen-space rectangles without CPU framebuffer upload.
pub(crate) const WEB_GL2_DIRECT_VERT_SHADER: &str = "#version 300 es\n\
precision highp float;\n\
precision highp int;\n\
uniform highp vec2 u_view;\n\
uniform highp vec4 u_rect;\n\
void main() {\n\
    vec2 corner = vec2((gl_VertexID == 1 || gl_VertexID == 4 || gl_VertexID == 5) ? 1.0 : 0.0,\n\
                       (gl_VertexID == 2 || gl_VertexID == 3 || gl_VertexID == 5) ? 1.0 : 0.0);\n\
    vec2 px = u_rect.xy + corner * u_rect.zw;\n\
    vec2 ndc = vec2((px.x / max(u_view.x, 1.0)) * 2.0 - 1.0, 1.0 - (px.y / max(u_view.y, 1.0)) * 2.0);\n\
    gl_Position = vec4(ndc, 0.0, 1.0);\n\
}\n";

/// WebGL2 direct GPU primitive fragment shader (`#version 300 es`, `precision highp float`).
pub(crate) const WEB_GL2_DIRECT_FRAG_SHADER: &str = "#version 300 es\n\
precision highp float;\n\
uniform highp vec4 u_color;\n\
out highp vec4 out_color;\n\
void main() {\n\
    out_color = u_color;\n\
}\n";

/// Uniform name carrying the primitive bounds `[x, y, w, h]` in `WEB_GL2_DIRECT_VERT_SHADER`.
pub(crate) const WEB_GL2_DIRECT_UNIFORM_RECT: &str = "u_rect";

/// Uniform name carrying the normalized RGBA primitive color in `WEB_GL2_DIRECT_FRAG_SHADER`.
pub(crate) const WEB_GL2_DIRECT_UNIFORM_COLOR: &str = "u_color";

/// Uniform name carrying physical viewport size in `GLSL_ES3_UI_VERTEX_SHADER` and `GLSL_ES3_UI_FRAGMENT_SHADER`.
pub(crate) const WEB_GL2_UBER_UNIFORM_VIEWPORT: &str = "u_viewport";

/// Uniform name carrying GPU scroll offset in `GLSL_ES3_UI_VERTEX_SHADER`.
pub(crate) const WEB_GL2_UBER_UNIFORM_SCROLL: &str = "u_scroll_offset";

/// Uniform name binding R8 glyph coverage atlas sampler unit `0` in `GLSL_ES3_UI_FRAGMENT_SHADER`.
pub(crate) const WEB_GL2_UBER_UNIFORM_GLYPH_R8: &str = "u_glyph_r8";

/// Uniform name binding RGBA8 color emoji glyph atlas sampler unit `1` in `GLSL_ES3_UI_FRAGMENT_SHADER`.
pub(crate) const WEB_GL2_UBER_UNIFORM_GLYPH_RGBA8: &str = "u_glyph_rgba8";

/// Uniform name binding RGBA8 image atlas sampler unit `2` in `GLSL_ES3_UI_FRAGMENT_SHADER`.
pub(crate) const WEB_GL2_UBER_UNIFORM_IMAGE_TEX: &str = "u_image_tex";

/// Uniform name binding external/3D/fluid render target sampler unit `3` in `GLSL_ES3_UI_FRAGMENT_SHADER`.
pub(crate) const WEB_GL2_UBER_UNIFORM_EXTERNAL_TEX: &str = "u_external_tex";

/// Static 6-vertex unit quad corner coordinates (`a_corner` at location `0`) shared across all instanced WebGL2 UI draws.
pub(crate) const WEB_GL2_UNIT_QUAD_VERTS: [f32; 12] =
    [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0];

/// Fallback initial instance VBO capacity in bytes (`256 KiB` = 4096 primitives) before dynamic growth.
pub(crate) const WEB_GL2_INITIAL_INSTANCE_VBO_BYTES: usize = 256 * 1024;

/// Default canvas tabindex value enabling keyboard focus.
pub(crate) const WEB_ATTR_TABINDEX_ZERO: &str = "0";

/// 2D rendering context identifier passed to `HTMLCanvasElement.getContext`.
pub(crate) const WEB_CONTEXT_2D: &str = "2d";

/// Media query string used to detect browser dark mode preference.
pub(crate) const WEB_DARK_MEDIA_QUERY: &str = "(prefers-color-scheme: dark)";

/// Error message when browser `window` or `document` is unavailable.
pub(crate) const ERR_WEB_WINDOW_MISSING: &str = "browser window or document unavailable";

/// Error message when `HTMLCanvasElement` creation or casting fails.
pub(crate) const ERR_WEB_CANVAS_CREATE: &str = "failed to create HTMLCanvasElement";

/// Error message when `CanvasRenderingContext2d` cannot be acquired.
pub(crate) const ERR_WEB_CONTEXT_2D_MISSING: &str = "failed to acquire 2d canvas rendering context";

/// Error message when `ImageData` creation or `putImageData` fails.
pub(crate) const ERR_WEB_PRESENT_FAILED: &str = "failed to blit ImageData to canvas";

/// CSS cursor keyword for default arrow.
pub(crate) const WEB_CURSOR_DEFAULT: &str = "default";

/// CSS cursor keyword for clickable hand pointer.
pub(crate) const WEB_CURSOR_POINTER: &str = "pointer";

/// CSS cursor keyword for text selection I-beam.
pub(crate) const WEB_CURSOR_TEXT: &str = "text";

/// CSS cursor keyword for precision crosshair.
pub(crate) const WEB_CURSOR_CROSSHAIR: &str = "crosshair";

/// CSS cursor keyword for disabled control.
pub(crate) const WEB_CURSOR_NOT_ALLOWED: &str = "not-allowed";

/// CSS cursor keyword for open grab hand.
pub(crate) const WEB_CURSOR_GRAB: &str = "grab";

/// CSS cursor keyword for active grabbing hand.
pub(crate) const WEB_CURSOR_GRABBING: &str = "grabbing";

/// CSS cursor keyword for omnidirectional move.
pub(crate) const WEB_CURSOR_MOVE: &str = "move";

/// CSS cursor keyword for horizontal resize.
pub(crate) const WEB_CURSOR_EW_RESIZE: &str = "ew-resize";

/// CSS cursor keyword for vertical resize.
pub(crate) const WEB_CURSOR_NS_RESIZE: &str = "ns-resize";

/// DOM Element method name entering browser fullscreen.
pub(crate) const WEB_METHOD_REQUEST_FULLSCREEN: &str = "requestFullscreen";

/// WebKit-prefixed DOM Element method name entering browser fullscreen on Safari.
pub(crate) const WEB_METHOD_WEBKIT_REQUEST_FULLSCREEN: &str = "webkitRequestFullscreen";

/// DOM Document method name exiting browser fullscreen.
pub(crate) const WEB_METHOD_EXIT_FULLSCREEN: &str = "exitFullscreen";

/// WebKit-prefixed DOM Document method name exiting browser fullscreen on Safari.
pub(crate) const WEB_METHOD_WEBKIT_EXIT_FULLSCREEN: &str = "webkitExitFullscreen";

/// WebKit-prefixed DOM Document property reporting the active fullscreen element on Safari.
pub(crate) const WEB_PROP_WEBKIT_FULLSCREEN_ELEMENT: &str = "webkitFullscreenElement";

/// DOM `devicemotion` window event name.
pub(crate) const WEB_EVENT_DEVICE_MOTION: &str = "devicemotion";

/// DOM `deviceorientation` window event name.
pub(crate) const WEB_EVENT_DEVICE_ORIENTATION: &str = "deviceorientation";

/// Global `DeviceMotionEvent` constructor property name on `window`.
pub(crate) const WEB_CTOR_DEVICE_MOTION_EVENT: &str = "DeviceMotionEvent";

/// Global `DeviceOrientationEvent` constructor property name on `window`.
pub(crate) const WEB_CTOR_DEVICE_ORIENTATION_EVENT: &str = "DeviceOrientationEvent";

/// iOS Safari sensor permission static method name on `DeviceMotionEvent` / `DeviceOrientationEvent`.
pub(crate) const WEB_METHOD_REQUEST_PERMISSION: &str = "requestPermission";

/// iOS Safari granted permission string value.
pub(crate) const WEB_PERMISSION_GRANTED: &str = "granted";

/// `window.screen` property name.
pub(crate) const WEB_PROP_SCREEN: &str = "screen";

/// `screen.orientation` / legacy `window.orientation` property name.
pub(crate) const WEB_PROP_ORIENTATION: &str = "orientation";

/// `screen.orientation.angle` property name.
pub(crate) const WEB_PROP_ANGLE: &str = "angle";

/// `DeviceMotionEvent.accelerationIncludingGravity` property name.
pub(crate) const WEB_PROP_ACCEL_INCLUDING_GRAVITY: &str = "accelerationIncludingGravity";

/// `DeviceMotionEvent.acceleration` property name.
pub(crate) const WEB_PROP_ACCELERATION: &str = "acceleration";

/// `DeviceMotionEvent.rotationRate` property name.
pub(crate) const WEB_PROP_ROTATION_RATE: &str = "rotationRate";

/// `DeviceOrientationEvent` / `rotationRate` alpha axis property name.
pub(crate) const WEB_PROP_ALPHA: &str = "alpha";

/// `DeviceOrientationEvent` / `rotationRate` beta axis property name.
pub(crate) const WEB_PROP_BETA: &str = "beta";

/// `DeviceOrientationEvent` / `rotationRate` gamma axis property name.
pub(crate) const WEB_PROP_GAMMA: &str = "gamma";

/// Cartesian `x` component property name.
pub(crate) const WEB_PROP_AXIS_X: &str = "x";

/// Cartesian `y` component property name.
pub(crate) const WEB_PROP_AXIS_Y: &str = "y";

/// Cartesian `z` component property name.
pub(crate) const WEB_PROP_AXIS_Z: &str = "z";

/// Degree-to-radian conversion constant (`PI / 180`).
pub(crate) const WEB_DEG_TO_RAD: f32 = PI / 180.0;

/// Exponential low-pass filter factor used to separate gravity when `DeviceMotionEvent.acceleration` is null.
pub(crate) const WEB_GRAVITY_LPF_ALPHA: f32 = 0.22;

/// Stationary linear acceleration noise deadzone in `m/s²` suppressing MEMS bias/jitter on untouched devices.
pub(crate) const WEB_LINEAR_ACCEL_DEADZONE_MPS2: f32 = 0.30;

/// Stationary gyroscope angular rate deadzone in `rad/s` suppressing zero-rate drift on untouched devices.
pub(crate) const WEB_GYRO_DEADZONE_RAD_S: f32 = 0.04;

/// Minimum dot-product magnitude between orientation gravity and accelerometer gravity to auto-detect Safari vs W3C sign convention.
pub(crate) const WEB_GRAVITY_SIGN_DOT_THRESHOLD: f32 = 12.0;

/// DOM element ID for the reusable hidden CSS `env(safe-area-inset-*)` length probe.
pub(crate) const WEB_SAFE_PROBE_ID: &str = "euv-safe-area-probe";

/// HTML `div` tag name used for the hidden safe-area CSS probe element.
pub(crate) const WEB_TAG_DIV: &str = "div";

/// HTML `img` tag name used for browser-native async image decoding.
pub(crate) const WEB_TAG_IMG: &str = "img";

/// Inline CSS rule mapping `env(safe-area-inset-*)` onto real box `padding-*` lengths so every browser resolves exact pixel values.
pub(crate) const WEB_SAFE_PROBE_STYLE: &str = "position:fixed;top:0;left:0;width:0;height:0;visibility:hidden;pointer-events:none;padding:env(safe-area-inset-top,0px) env(safe-area-inset-right,0px) env(safe-area-inset-bottom,0px) env(safe-area-inset-left,0px);";

/// CSS computed property name for top padding on the hidden safe-area probe.
pub(crate) const WEB_CSS_PADDING_TOP: &str = "padding-top";

/// CSS computed property name for right padding on the hidden safe-area probe.
pub(crate) const WEB_CSS_PADDING_RIGHT: &str = "padding-right";

/// CSS computed property name for bottom padding on the hidden safe-area probe.
pub(crate) const WEB_CSS_PADDING_BOTTOM: &str = "padding-bottom";

/// CSS computed property name for left padding on the hidden safe-area probe.
pub(crate) const WEB_CSS_PADDING_LEFT: &str = "padding-left";

/// DOM `HTMLImageElement.crossOrigin` property name.
pub(crate) const WEB_PROP_CROSS_ORIGIN: &str = "crossOrigin";

/// CORS anonymous attribute value enabling `<canvas>` pixel readback on cross-origin images.
pub(crate) const WEB_CORS_ANONYMOUS: &str = "anonymous";

/// DOM `HTMLImageElement.src` property name.
pub(crate) const WEB_PROP_SRC: &str = "src";

/// DOM `HTMLImageElement.naturalWidth` property name.
pub(crate) const WEB_PROP_NATURAL_WIDTH: &str = "naturalWidth";

/// DOM `HTMLImageElement.naturalHeight` property name.
pub(crate) const WEB_PROP_NATURAL_HEIGHT: &str = "naturalHeight";

/// DOM `CanvasRenderingContext2D.drawImage` method name.
pub(crate) const WEB_METHOD_DRAW_IMAGE: &str = "drawImage";

/// DOM `load` event name for image elements.
pub(crate) const WEB_EVENT_LOAD: &str = "load";

/// DOM `error` event name for image elements.
pub(crate) const WEB_EVENT_ERROR: &str = "error";

/// HTTP scheme prefix used to detect cross-origin image fetches.
pub(crate) const WEB_SCHEME_HTTP: &str = "http://";

/// HTTPS scheme prefix used to detect cross-origin image fetches.
pub(crate) const WEB_SCHEME_HTTPS: &str = "https://";

/// Maximum rasterized image dimension in pixels when decoding network images onto a 2D canvas.
pub(crate) const WEB_IMAGE_MAX_DIM: u32 = 2048;

/// Default fallback width in CSS pixels for vector SVG images reporting zero intrinsic width.
pub(crate) const WEB_SVG_FALLBACK_WIDTH: u32 = 240;

/// Default fallback height in CSS pixels for vector SVG images reporting zero intrinsic height.
pub(crate) const WEB_SVG_FALLBACK_HEIGHT: u32 = 48;

/// Maximum DPR multiplier applied when rasterizing vector SVG sources onto an offscreen 2D canvas.
pub(crate) const WEB_SVG_RASTER_MAX_DPR: f32 = 4.0;

/// `CanvasRenderingContext2D.imageSmoothingQuality` property name for crisp HiDPI image decoding.
pub(crate) const WEB_PROP_IMAGE_SMOOTHING_QUALITY: &str = "imageSmoothingQuality";

/// High-quality resampling mode for browser offscreen 2D canvas image rasterization.
pub(crate) const WEB_IMG_SMOOTHING_HIGH: &str = "high";

/// File extension marker identifying vector SVG image URLs.
pub(crate) const WEB_EXT_SVG: &str = ".svg";

/// Data URI prefix identifying inline vector SVG images.
pub(crate) const WEB_DATA_SVG_PREFIX: &str = "data:image/svg+xml";

/// HTML `<textarea>` tag name used for synchronous clipboard copy fallback.
pub(crate) const WEB_TAG_TEXTAREA: &str = "textarea";

/// HTML attribute tagging the temporary offscreen clipboard copy `<textarea>` so document `copy` listeners pass it through.
pub(crate) const WEB_ATTR_COPY_PROXY: &str = "data-euv-copy-proxy";

/// Attribute value identifying the temporary offscreen clipboard copy proxy.
pub(crate) const WEB_COPY_PROXY_FLAG: &str = "1";

/// HTML `readonly` attribute preventing mobile soft keyboards from flashing during offscreen clipboard copy.
pub(crate) const WEB_ATTR_READONLY: &str = "readonly";

/// Inline style positioning the offscreen clipboard copy proxy outside the viewport without shifting scroll.
pub(crate) const WEB_COPY_PROXY_STYLE: &str =
    "position:fixed;left:-9999px;top:0;width:1px;height:1px;opacity:0;pointer-events:none;";

/// `navigator.clipboard` property name.
pub(crate) const WEB_PROP_CLIPBOARD: &str = "clipboard";

/// `Clipboard.writeText` method name.
pub(crate) const WEB_METHOD_WRITE_TEXT: &str = "writeText";

/// `HTMLTextAreaElement.value` property name.
pub(crate) const WEB_PROP_VALUE: &str = "value";

/// `HTMLTextAreaElement.select` method name.
pub(crate) const WEB_METHOD_SELECT: &str = "select";

/// `HTMLTextAreaElement.setSelectionRange` method name.
pub(crate) const WEB_METHOD_SET_SELECTION_RANGE: &str = "setSelectionRange";

/// `Document.execCommand` method name for synchronous clipboard copy fallback.
pub(crate) const WEB_METHOD_EXEC_COMMAND: &str = "execCommand";

/// `Document.execCommand` copy command name.
pub(crate) const WEB_CMD_COPY: &str = "copy";

/// `Promise.catch` method name suppressing unhandled clipboard permission rejection logs.
pub(crate) const WEB_METHOD_CATCH: &str = "catch";

/// Empty JS no-op function body used as a `Promise.catch` handler.
pub(crate) const WEB_JS_NOOP_BODY: &str = "";

/// HTML `<iframe>` tag name used for embedded bottom-sheet WebView hosting on WebAssembly.
pub(crate) const WEB_TAG_IFRAME: &str = "iframe";

/// DOM id assigned to the active embedded WebView sheet `<iframe>` overlay.
pub(crate) const WEB_WEBVIEW_IFRAME_ID: &str = "euv-webview-sheet-frame";

/// HTML `src` attribute name for embedded WebView navigation.
pub(crate) const WEB_ATTR_SRC: &str = "src";

/// HTML `title` attribute name for accessible embedded WebView labeling.
pub(crate) const WEB_ATTR_TITLE: &str = "title";

/// HTML `referrerpolicy` attribute name applied to embedded WebView iframes.
pub(crate) const WEB_ATTR_REFERRER_POLICY: &str = "referrerpolicy";

/// Referrer policy value limiting cross-origin leakage from embedded WebView iframes.
pub(crate) const WEB_REFERRER_POLICY_STRICT: &str = "strict-origin-when-cross-origin";

/// Initial hidden inline style applied to an embedded WebView `<iframe>` before its first layout frame sync.
pub(crate) const WEB_WEBVIEW_HIDDEN_STYLE: &str = "position:fixed;left:0;top:0;width:0;height:0;border:0;z-index:20;background:#ffffff;display:none;pointer-events:none;opacity:0;";