//! GLSL sources. The `#version` line is added at runtime (desktop GL 3.3 core or GLES 3.0). /// Fullscreen triangle without vertex buffers. pub const VERTEX: &str = r#" out vec2 vUv; void main() { vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2)); vUv = p; gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); } "#; /// A quad over `uBox` (x0, y0, x1, y1 in px, top-left origin), drawn as a /// 4-vertex triangle strip. Glass panels only shade the pixels they can reach. pub const QUAD_VERTEX: &str = r#" uniform vec4 uBox; uniform vec2 uRes; void main() { vec2 c = vec2(float(gl_VertexID & 1), float((gl_VertexID >> 1) & 1)); vec2 p = mix(uBox.xy, uBox.zw, c); gl_Position = vec4(p.x / uRes.x * 2.0 - 1.0, 1.0 - p.y / uRes.y * 2.0, 0.0, 1.0); } "#; /// Backdrop: a dark, slowly flowing mesh gradient (soft colour fields in the /// focused app's palette), like Apple's dark dynamic wallpapers. /// /// It is smooth by design, so it is rendered at low resolution and upscaled. /// Cost per pixel: a handful of exp() calls, no noise, no loops over shapes. pub const SCENE: &str = r#" in vec2 vUv; out vec4 fragColor; uniform vec2 uRes; uniform float uTime; uniform vec3 uPal[3]; // highlight, mid, deep uniform float uZoom; // launch fly-in uniform float uBright; // backdrop brightness: 1 = full, lower = calmer (launcher: shelf hidden) float field(vec2 d, float r) { return exp(-dot(d, d) / (r * r)); } // Rotates the hue around the grey axis (keeps brightness roughly the same). vec3 hueRot(vec3 c, float ang) { const vec3 k = vec3(0.57735); float ca = cos(ang); return max(c * ca + cross(k, c) * sin(ang) + k * dot(k, c) * (1.0 - ca), 0.0); } void main() { vec2 uv = vec2(vUv.x, 1.0 - vUv.y); // top-left origin uv = (uv - 0.5) / uZoom + 0.5; float aspect = uRes.x / uRes.y; float t = uTime; // Gentle flow: two layers of low-frequency sine warp make the fields drift // and bend like liquid without any visible shapes. vec2 w = uv; w += 0.050 * vec2(sin(uv.y * 3.1 + t * 0.110), sin(uv.x * 2.3 - t * 0.090)); w += 0.022 * vec2(sin(uv.y * 5.7 - t * 0.070 + 1.3), sin(uv.x * 4.9 + t * 0.130 + 0.7)); vec2 a = vec2(aspect, 1.0); vec3 hi = uPal[0]; vec3 mid = uPal[1]; vec3 deep = uPal[2]; // Near-black base; colour lives in a few soft fields with dark gaps between. vec3 col = deep * 0.35 + vec3(0.003, 0.003, 0.005); // Fields on slow Lissajous paths. Two are hue-shifted for depth, like // Apple's dark gradients (never a single flat hue). vec2 c0 = vec2(0.76 + 0.10 * sin(t * 0.050), 0.18 + 0.08 * cos(t * 0.043)); vec2 c1 = vec2(0.20 + 0.12 * cos(t * 0.037 + 1.0), 0.26 + 0.10 * sin(t * 0.041)); vec2 c2 = vec2(0.52 + 0.20 * sin(t * 0.029 + 2.0), 0.62 + 0.08 * cos(t * 0.033 + 0.5)); vec2 c3 = vec2(0.96 + 0.05 * cos(t * 0.045), 0.88 + 0.06 * sin(t * 0.052)); vec2 c4 = vec2(0.04 + 0.06 * sin(t * 0.031 + 4.0), 0.94 + 0.05 * cos(t * 0.027)); col += hi * 0.34 * field((w - c0) * a, 0.40); col += mid * 0.62 * field((w - c1) * a, 0.48); col += hueRot(mix(mid, hi, 0.4), 0.55) * 0.34 * field((w - c2) * a, 0.38); col += hueRot(mid, -0.70) * 0.40 * field((w - c3) * a, 0.36); col += mid * 0.30 * field((w - c4) * a, 0.34); // Soft shoulder so bright palettes stay "dark mode". col = col / (1.0 + col * 1.2); // Calmer at the bottom where the icons sit; dimmer when asked (uBright). col *= mix(1.0, 0.62, smoothstep(0.45, 1.0, uv.y)); col *= mix(0.42, 1.0, uBright); vec2 v = uv - 0.5; col *= 1.0 - 0.30 * dot(v, v); fragColor = vec4(col, 1.0); } "#; /// Upscales the backdrop to the window with dithering (no banding). pub const BLIT: &str = r#" in vec2 vUv; out vec4 fragColor; uniform sampler2D uScene; uniform float uTime; float hash12(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * 0.1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); } void main() { vec3 col = textureLod(uScene, vUv, 0.0).rgb; col += (hash12(gl_FragCoord.xy + fract(uTime) * 61.0) - 0.5) / 255.0; fragColor = vec4(col, 1.0); } "#; /// A backdrop picture (artwork), cover-fitted to the viewport and toned for /// text on top: darkened overall (`uDim`) and towards the left and bottom /// edges, where titles usually sit. Blended with alpha (crossfades). pub const IMAGE: &str = r#" in vec2 vUv; out vec4 fragColor; uniform sampler2D uImage; uniform vec2 uImg; // image size, px uniform vec2 uRes; // viewport size, px uniform vec2 uAlign; // which part stays visible when cropping (0..1, 0.5 = centre) uniform float uDim; uniform float uFadeLeft; uniform float uFadeBottom; uniform float uAlpha; void main() { vec2 uv = vec2(vUv.x, 1.0 - vUv.y); // top-left origin, like the image rows float va = uRes.x / uRes.y; float ia = uImg.x / max(uImg.y, 1.0); vec2 st = uv; if (ia > va) { float f = va / ia; st.x = uAlign.x * (1.0 - f) + uv.x * f; } else { float f = ia / va; st.y = uAlign.y * (1.0 - f) + uv.y * f; } vec3 col = texture(uImage, st).rgb; float kl = uFadeLeft * (1.0 - smoothstep(0.05, 0.85, uv.x)); float kb = uFadeBottom * smoothstep(0.30, 0.95, uv.y); float k = 1.0 - (1.0 - kl) * (1.0 - kb); col = mix(col, vec3(0.018, 0.018, 0.022), k) * (1.0 - uDim); fragColor = vec4(col, uAlpha); } "#; /// A video frame (NV12: full-size Y plane, half-size interleaved CbCr plane), /// contain-fitted with black bars, converted to RGB here. pub const VIDEO: &str = r#" in vec2 vUv; out vec4 fragColor; uniform sampler2D uY; uniform sampler2D uUV; uniform vec2 uRes; // viewport size, px uniform float uAspect; // display aspect of the video uniform int uMatrix; // 0 = BT.601, 1 = BT.709, 2 = BT.2020 uniform int uFull; // 1 = full range (0..255), else limited (16..235) uniform float uAlpha; void main() { vec2 uv = vec2(vUv.x, 1.0 - vUv.y); float va = uRes.x / uRes.y; vec2 st = uv; float inside = 1.0; if (uAspect > va) { float h = va / uAspect; st.y = (uv.y - 0.5) / h + 0.5; } else { float w = uAspect / va; st.x = (uv.x - 0.5) / w + 0.5; } if (st.x < 0.0 || st.x > 1.0 || st.y < 0.0 || st.y > 1.0) inside = 0.0; st = clamp(st, vec2(0.0), vec2(1.0)); float y = texture(uY, st).r; vec2 c = texture(uUV, st).rg; if (uFull == 1) { c -= 0.5; } else { y = (y - 16.0 / 255.0) * (255.0 / 219.0); c = (c - 128.0 / 255.0) * (255.0 / 224.0); } vec3 rgb; if (uMatrix == 0) { rgb = vec3(y + 1.402 * c.y, y - 0.344136 * c.x - 0.714136 * c.y, y + 1.772 * c.x); } else if (uMatrix == 2) { rgb = vec3(y + 1.4746 * c.y, y - 0.16455 * c.x - 0.57135 * c.y, y + 1.8814 * c.x); } else { rgb = vec3(y + 1.5748 * c.y, y - 0.1873 * c.x - 0.4681 * c.y, y + 1.8556 * c.x); } fragColor = vec4(clamp(rgb, 0.0, 1.0) * inside, uAlpha); } "#; /// One Liquid Glass panel (or one merged group of panels), drawn as a quad /// over its bounds plus the shadow margin. /// /// The model follows Apple's Liquid Glass: light is *bent*, not scattered. /// Each panel is a slab of glass (IOR ~1.5) whose edge is a convex squircle /// bevel. The backdrop seen through it is displaced by a real refraction ray /// (Snell), with per-channel IOR for dispersion. Bigger panels are thicker: /// wider bevel, stronger lensing, deeper shadow. On top: Fresnel reflection, /// a geometry-aware specular rim, light from within when focused, and /// "materialize" (alpha ramps the lensing, not just the opacity). /// /// The result is written as `dst * a + rgb` (blend ONE, SRC_ALPHA), so the /// shader never needs to read the framebuffer: shadows multiply, glass /// replaces, and nested glass (on another pane) lights what is already there. pub const GLASS: &str = r#" out vec4 fragColor; #define MAX_GROUP 4 uniform sampler2D uScene; uniform vec2 uRes; // physical px uniform float uScale; // physical px per design px uniform float uTime; uniform vec2 uLight; // direction towards the key light (screen space, y down) uniform int uCount; // members of this run (> 1 = merged group) uniform vec4 uRect[MAX_GROUP]; // x, y, w, h (px, top-left origin) uniform vec4 uParam[MAX_GROUP]; // radius, focus, alpha, frost (mip lod; < 0 = nested on other glass) uniform vec4 uGlow[MAX_GROUP]; // accent rgb, unused uniform vec4 uTint[MAX_GROUP]; // tint rgb, tint amount uniform int uMode; // 0 = full, 1 = light (frost, no lensing), 2 = flat (no frost) float sdRoundRect(vec2 p, vec2 b, float r) { vec2 q = abs(p) - b + r; return min(max(q.x, q.y), 0.0) + length(max(q, 0.0)) - r; } // Analytic outward gradient of sdRoundRect. vec2 gradRoundRect(vec2 p, vec2 b, float r) { vec2 w = abs(p) - b + r; vec2 q = max(w, 0.0); vec2 g = (max(w.x, w.y) > 0.0) ? q / max(length(q), 1e-5) : ((w.x > w.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0)); return sign(p) * g; } float sdPanel(int i, vec2 p) { vec4 R = uRect[i]; vec2 hs = R.zw * 0.5; float r = min(uParam[i].x, min(hs.x, hs.y)); return sdRoundRect(p - (R.xy + hs), hs, r); } // Polynomial smooth minimum: the "liquid bridge" between merged panels. float smin(float a, float b, float k) { float h = max(k - abs(a - b), 0.0) / k; return min(a, b) - h * h * k * 0.25; } float sdGroup(vec2 p, float k) { float d = 1e5; for (int i = 0; i < MAX_GROUP; i++) { if (i >= uCount) break; if (uParam[i].z <= 0.001) continue; d = smin(d, sdPanel(i, p), k); } return d; } // uv has a top-left origin; the scene texture is stored GL-style (bottom row first). vec3 sceneAt(vec2 uv, float lod) { uv = clamp(uv, vec2(0.001), vec2(0.999)); return textureLod(uScene, vec2(uv.x, 1.0 - uv.y), lod).rgb; } float luma(vec3 c) { return dot(c, vec3(0.2126, 0.7152, 0.0722)); } // Convex squircle bevel: height (0 at the rim, 1 on the flat top) and slope. // u = distance from the rim / bevel width. vec2 squircle(float u) { u = clamp(u, 0.0, 1.0); float a = 1.0 - u; float a4 = a * a * a * a; float h = pow(max(1.0 - a4, 1e-4), 0.25); float dh = a * a * a * pow(max(1.0 - a4, 1e-3), -0.75); return vec2(h, dh); } // Where a vertical ray through the glass lands on the backdrop (px offset). vec2 refractOffset(vec3 N, float height, float ior) { vec3 t = refract(vec3(0.0, 0.0, -1.0), N, 1.0 / ior); return t.xy / max(-t.z, 0.2) * height; } void main() { vec2 p = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y); // px, top-left float s = uScale; float mergeK = 30.0 * s; bool merged = uCount > 1; // Combined distance, and parameters blended towards the nearest member. float d = 1e5; float wsum = 0.0; vec4 P = vec4(0.0); vec3 accent = vec3(0.0); vec4 tint = vec4(0.0); vec2 center = vec2(0.0); vec2 halfSize = vec2(0.0); for (int k = 0; k < MAX_GROUP; k++) { if (k >= uCount) break; float a = uParam[k].z; if (a <= 0.001) continue; float dk = sdPanel(k, p); d = merged ? smin(d, dk, mergeK) : dk; float w = exp(-max(dk, 0.0) / (12.0 * s)) + 1e-4; wsum += w; P += uParam[k] * w; accent += uGlow[k].rgb * w; tint += uTint[k] * w; vec4 R = uRect[k]; center += (R.xy + R.zw * 0.5) * w; halfSize += R.zw * 0.5 * w; } if (wsum <= 0.0) discard; P /= wsum; accent /= wsum; tint /= wsum; center /= wsum; halfSize /= wsum; float focus = P.y; float alpha = P.z; float minHalf = min(halfSize.x, halfSize.y); bool nested = P.w < 0.0; // Size-dependent thickness: small controls are full domes (a drop of // liquid), big panes are thick slabs with a wide rounded edge. float bezel = min(minHalf * 0.80, 9.0 * s + minHalf * 0.10); bezel = clamp(bezel, 5.0 * s, 40.0 * s); float sizeK = clamp(minHalf / (240.0 * s), 0.0, 1.0); float small = 1.0 - clamp(minHalf / (90.0 * s), 0.0, 1.0); // Output is dst * mul + add. float mul = 1.0; vec3 add = vec3(0.0); // ---- outside: grounding shadow and a little accent light spill ---- if (d > 0.0) { vec2 off = vec2(0.0, (10.0 + 18.0 * focus + 14.0 * sizeK) * s); float ds = merged ? sdGroup(p - off, mergeK) : sdPanel(0, p - off); float spread = (34.0 + 34.0 * focus + 50.0 * sizeK) * s * (nested ? 0.5 : 1.0); float shadow = (1.0 - smoothstep(-spread * 0.5, spread, ds)) * (0.26 + 0.22 * focus + 0.18 * sizeK); shadow += (1.0 - smoothstep(0.0, 6.0 * s, d)) * 0.10; mul = 1.0 - shadow * alpha; add = accent * exp(-d / (22.0 * s)) * focus * 0.16 * alpha; if (d > 1.0) { if (mul > 0.998 && dot(add, vec3(1.0)) < 0.002) discard; fragColor = vec4(add, mul); return; } } // ---- surface geometry ---- vec2 n2; if (merged) { vec2 e = vec2(0.75, 0.0); vec2 g2 = vec2(sdGroup(p + e.xy, mergeK) - sdGroup(p - e.xy, mergeK), sdGroup(p + e.yx, mergeK) - sdGroup(p - e.yx, mergeK)); n2 = g2 / max(length(g2), 1e-5); } else { vec4 R = uRect[0]; vec2 hs = R.zw * 0.5; n2 = gradRoundRect(p - (R.xy + hs), hs, min(uParam[0].x, min(hs.x, hs.y))); } float inside = max(-d, 0.0); float u = inside / bezel; vec2 hp = squircle(u); float thick = bezel * (1.1 + 0.3 * focus); // glass height in px float height = thick * hp.x; float slope = (u < 1.0) ? hp.y * thick / bezel : 0.0; slope = min(slope, 6.0); vec3 N = normalize(vec3(n2 * slope, 1.0)); // Materialize: lensing and frost ramp up with alpha instead of a plain fade. float mat = smoothstep(0.0, 1.0, alpha); float lens = (1.25 + 0.35 * focus) * mat; vec2 mag = -(p - center) * (0.025 + 0.035 * focus) * mat; float lod = abs(P.w) * mix(0.35, 1.0, mat); // The glass colour is affine in what is underneath: g = dst * A + B. float A = 0.0; vec3 B; if (nested || uMode == 2) { A = 1.0; B = vec3(0.0); } else if (uMode == 1) { B = sceneAt(p / uRes, lod); } else if (slope > 0.02) { // Bevel: refract each channel with its own IOR (dispersion). B.r = sceneAt((p + refractOffset(N, height, 1.46) * lens + mag) / uRes, lod).r; B.g = sceneAt((p + refractOffset(N, height, 1.50) * lens + mag) / uRes, lod).g; B.b = sceneAt((p + refractOffset(N, height, 1.56) * lens + mag) / uRes, lod).b; } else { B = sceneAt((p + mag) / uRes, lod); } // ---- body ---- // Adaptive tone: lift glass over dark content, hold it back over bright content. float bgL = luma(sceneAt(center / uRes, 6.0)); float kb = 1.02 + 0.10 * focus; A *= kb; B = B * kb + mix(0.045, -0.01, clamp(bgL * 2.0, 0.0, 1.0)) + (nested ? 0.025 : 0.0); A *= 1.0 - tint.a; B = mix(B, tint.rgb, tint.a); // Light from within when focused: starts at the center, spreads to the rim. vec2 qn = (p - center) / max(halfSize, vec2(1.0)); float core = exp(-dot(qn, qn) * 1.3); vec3 inner = mix(vec3(1.0), accent, 0.45); B += inner * focus * (0.06 + 0.12 * core); // Focus sheen: a soft diagonal band of light glides across the glass now and then. float sweep = fract(uTime * 0.16 + center.x * 0.0007); float band = (qn.x * 0.8 + qn.y * 0.45) * 0.5 + 0.5; float sheen = exp(-pow((band - (sweep * 1.8 - 0.4)) * 7.0, 2.0)); B += vec3(1.0) * sheen * focus * 0.10 * mat; // Soft vertical sheen, as if lit from above. float vy = clamp((p.y - (center.y - halfSize.y)) / (2.0 * halfSize.y), 0.0, 1.0); B += vec3(0.06 + 0.05 * focus) * pow(1.0 - vy, 2.5); // ---- light ---- float wob = focus * 0.35 * sin(uTime * 0.9); vec2 L2 = mat2(cos(wob), sin(wob), -sin(wob), cos(wob)) * uLight; vec3 L = normalize(vec3(L2, 0.55)); vec3 V = vec3(0.0, 0.0, 1.0); // Fresnel: steep bevel reflects the (blurred, brightened) surroundings. float cosT = clamp(N.z, 0.0, 1.0); float F = 0.04 + 0.96 * pow(1.0 - cosT, 5.0); float Fm = uMode == 0 ? clamp(F, 0.0, 1.0) * 0.35 * mat : 0.0; vec3 env = mix(sceneAt((p + n2 * 60.0 * s) / uRes, 5.0) * 1.6, vec3(0.92), 0.35); A *= 1.0 - Fm; B = mix(B, env, Fm); // Specular on the bevel: key light, plus a weaker bounce on the far side. vec3 H = normalize(L + V); vec3 H2 = normalize(vec3(-L2, 0.55) + V); float spec = pow(max(dot(N, H), 0.0), 26.0); float spec2 = pow(max(dot(N, H2), 0.0), 26.0); float bevelMask = 1.0 - smoothstep(0.0, 1.0, u); B += vec3(1.0) * (spec * 0.60 + spec2 * 0.38) * bevelMask * (0.8 + 0.6 * focus) * mat; // Glossy highlight on small domes, like a water drop. vec2 hl = qn - uLight * 0.42; float gloss = exp(-dot(hl * vec2(1.4, 2.2), hl * vec2(1.4, 2.2)) * 3.0); B += vec3(1.0) * gloss * small * 0.14 * mat; // Crisp rim: a bright hairline all around, strongest towards the light // and again on the far side where light exits the glass. float facing = max(dot(n2, uLight), 0.0); float away = max(dot(n2, -uLight), 0.0); float rimLine = exp(-inside / (1.0 * s)); float rimGlow = exp(-inside / (6.0 * s)); B += rimLine * (0.22 + 0.80 * pow(facing, 2.0) + 0.55 * pow(away, 2.0) + 0.22 * focus) * mat; B += rimGlow * (0.06 + 0.10 * facing + 0.06 * away + 0.08 * focus) * mat; B += accent * rimGlow * focus * 0.10 * mat; // Subtle inner shade on the side away from the light gives the slab depth. float shade = 1.0 - 0.08 * bevelMask * away * mat; A *= shade; B *= shade; float cov = clamp(0.5 - d, 0.0, 1.0) * clamp(alpha * 2.0, 0.0, 1.0); float keep = 1.0 - cov + cov * A; fragColor = vec4(add * keep + cov * B, mul * keep); } "#;