The loader now measures throughput from whole tile batches, not just the two probe tiles, and after a session settles inside its deadline it tops up in the background: the needed level if the budget cut it short, then one level above it on capable devices with a fast connection (drawn downscaled), within time and memory caps and never under Save-Data. The chop in the album end card and the page footer is replaced by the LUCIO loader in smoke from lucioIP, falling back to the SVG loader without WebGL. sync-brand.sh now copies lucio-loaders.js and defaults to ../lucioIP. Co-Authored-By: Claude Opus 5.5 <[email protected]>
469 lines
26 KiB
JavaScript
469 lines
26 KiB
JavaScript
(function (global) {
|
|
const HB = 49.8, CR = 16.2, HW = 10.2, H = HB - CR, SL = 2 * H, CL = Math.PI / 2 * CR, PER = 4 * (SL + CL), Q = PER / 4;
|
|
const NPTS = 96, NPUFF = 64, W0 = HW * 0.85;
|
|
function pathP(s) {
|
|
s = (((s - CL / 2) % PER) + PER) % PER;
|
|
const arc = (cx, cy, a) => [cx + CR * Math.cos(a), cy + CR * Math.sin(a)];
|
|
if (s < SL) return [HB, -H + s]; s -= SL;
|
|
if (s < CL) return arc(H, H, s / CR); s -= CL;
|
|
if (s < SL) return [H - s, HB]; s -= SL;
|
|
if (s < CL) return arc(-H, H, Math.PI / 2 + s / CR); s -= CL;
|
|
if (s < SL) return [-HB, H - s]; s -= SL;
|
|
if (s < CL) return arc(-H, -H, Math.PI + s / CR); s -= CL;
|
|
if (s < SL) return [-H + s, -HB]; s -= SL;
|
|
return arc(H, -H, -Math.PI / 2 + s / CR);
|
|
}
|
|
const PRELUDE = `
|
|
precision highp float;
|
|
uniform vec2 uRes;
|
|
uniform float uPx, uTime, uDark;
|
|
uniform vec3 uColor;
|
|
#define PI 3.14159265
|
|
#define HPI 1.5707963
|
|
const float HB = ${HB.toFixed(4)}, CR = ${CR.toFixed(4)}, HW = ${HW.toFixed(4)}, H = ${H.toFixed(4)};
|
|
const float SL = ${SL.toFixed(4)}, CL = ${CL.toFixed(4)}, PER = ${PER.toFixed(4)}, Q = ${Q.toFixed(4)};
|
|
const vec2 L2 = vec2(0.6608, 0.7506); // the brand's one light: from the upper right
|
|
float sdRB(vec2 p) { vec2 q = abs(p) - vec2(H); return length(max(q, 0.)) + min(max(q.x, q.y), 0.) - CR; }
|
|
// arc length of the nearest centreline point, CCW, 0 at the bottom-right corner's 45-degree point
|
|
float pathS(vec2 p) {
|
|
if (abs(p.x) <= H && abs(p.y) <= H) { if (abs(p.x) > abs(p.y)) p.x = sign(p.x) * (H + 1e-3); else p.y = sign(p.y) * (H + 1e-3); }
|
|
float s;
|
|
if (p.x > H && p.y > H) s = SL + atan(p.y - H, p.x - H) * CR;
|
|
else if (p.x < -H && p.y > H) s = 2. * SL + CL + (atan(p.y - H, p.x + H) - HPI) * CR;
|
|
else if (p.x < -H && p.y < -H) s = 3. * SL + 2. * CL + atan(-(p.y + H), -(p.x + H)) * CR;
|
|
else if (p.x > H && p.y < -H) s = 4. * SL + 3. * CL + (atan(p.y + H, p.x - H) + HPI) * CR;
|
|
else if (p.x > H) s = p.y + H;
|
|
else if (p.y > H) s = SL + CL + (H - p.x);
|
|
else if (p.x < -H) s = 2. * SL + 2. * CL + (H - p.y);
|
|
else s = 3. * SL + 3. * CL + (p.x + H);
|
|
return mod(s + CL * 0.5, PER);
|
|
}
|
|
float hash11(float n) { return fract(sin(n * 127.1 + 11.7) * 43758.5453); }
|
|
float hash12(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * .1031); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.x + p3.y) * p3.z); }
|
|
vec2 hash22(vec2 p) { vec3 p3 = fract(vec3(p.xyx) * vec3(.1031, .1030, .0973)); p3 += dot(p3, p3.yzx + 33.33); return fract((p3.xx + p3.yz) * p3.zy); }
|
|
// simplex noise 3D (Ashima Arts / Stefan Gustavson, MIT)
|
|
vec3 mod289(vec3 x) { return x - floor(x * (1. / 289.)) * 289.; }
|
|
vec4 mod289(vec4 x) { return x - floor(x * (1. / 289.)) * 289.; }
|
|
vec4 permute(vec4 x) { return mod289(((x * 34.) + 1.) * x); }
|
|
vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }
|
|
float snoise(vec3 v) {
|
|
const vec2 C = vec2(1. / 6., 1. / 3.); const vec4 D = vec4(0., .5, 1., 2.);
|
|
vec3 i = floor(v + dot(v, C.yyy)); vec3 x0 = v - i + dot(i, C.xxx);
|
|
vec3 g = step(x0.yzx, x0.xyz); vec3 l = 1. - g; vec3 i1 = min(g.xyz, l.zxy); vec3 i2 = max(g.xyz, l.zxy);
|
|
vec3 x1 = x0 - i1 + C.xxx; vec3 x2 = x0 - i2 + C.yyy; vec3 x3 = x0 - D.yyy;
|
|
i = mod289(i);
|
|
vec4 p = permute(permute(permute(i.z + vec4(0., i1.z, i2.z, 1.)) + i.y + vec4(0., i1.y, i2.y, 1.)) + i.x + vec4(0., i1.x, i2.x, 1.));
|
|
float n_ = 0.142857142857; vec3 ns = n_ * D.wyz - D.xzx;
|
|
vec4 j = p - 49. * floor(p * ns.z * ns.z);
|
|
vec4 x_ = floor(j * ns.z); vec4 y_ = floor(j - 7. * x_);
|
|
vec4 x = x_ * ns.x + ns.yyyy; vec4 y = y_ * ns.x + ns.yyyy; vec4 h = 1. - abs(x) - abs(y);
|
|
vec4 b0 = vec4(x.xy, y.xy); vec4 b1 = vec4(x.zw, y.zw);
|
|
vec4 s0 = floor(b0) * 2. + 1.; vec4 s1 = floor(b1) * 2. + 1.; vec4 sh = -step(h, vec4(0.));
|
|
vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy; vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
|
|
vec3 p0 = vec3(a0.xy, h.x); vec3 p1 = vec3(a0.zw, h.y); vec3 p2 = vec3(a1.xy, h.z); vec3 p3 = vec3(a1.zw, h.w);
|
|
vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
|
|
p0 *= norm.x; p1 *= norm.y; p2 *= norm.z; p3 *= norm.w;
|
|
vec4 m = max(.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.); m = m * m;
|
|
return 42. * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));
|
|
}
|
|
// premultiplied "over": c with coverage a goes on top of dst
|
|
void over(inout vec4 dst, vec3 c, float a) { a = clamp(a, 0., 1.); dst = vec4(c * a, a) + dst * (1. - a); }
|
|
vec2 frag() { return (gl_FragCoord.xy - 0.5 * uRes) * uPx; }
|
|
`;
|
|
const WATER = `
|
|
uniform float uHead, uLen, uSurge, uMid, uFlowA, uFlowB, uFlowW;
|
|
uniform sampler2D uWet; // how recently each stretch of pipe was under water, 1 = now
|
|
const float RI = 0.84; // bore radius / outer radius: the rest is glass wall
|
|
const float MEN = 0.55; // meniscus depth, fraction of bore radius
|
|
// x: coverage, y: distance behind the head along the pipe, z: distance inside the nearest meniscus
|
|
vec3 slug(float s, float vi) {
|
|
float rel = mod(uHead - s + 24., PER) - 24.;
|
|
float gap = PER - uLen, full = 1. - smoothstep(0.3, 1.5, gap); // O: the two ends meet and the ring closes
|
|
float c = sqrt(max(0., 1. - vi * vi));
|
|
float m = MEN * HW * RI * smoothstep(0., 30., gap);
|
|
float front = m * c * (1. - 0.4 * uSurge); // concave: the water climbs the wall ahead of the centre
|
|
float back = uLen + m * (1. - c) * (1. + 0.4 * uSurge);
|
|
float dIn = mix(min(rel - front, back - rel), 60., full);
|
|
return vec3(clamp(dIn / uPx + 0.5, 0., 1.), rel, dIn);
|
|
}
|
|
// noise laid on a circle, so the texture wraps the loop without a seam
|
|
float ringNoise(float s, float vi, float freq, float z) {
|
|
float a = 6.2831853 * s / PER, R = PER / 6.2831853 * freq;
|
|
return snoise(vec3(R * cos(a) + uTime * 0.3, R * sin(a) - uTime * 0.2, vi * 1.25 + z));
|
|
}
|
|
float flowTex(float s, float vi, float z) {
|
|
return 0.5 + 0.5 * (0.6 * ringNoise(s, vi, 0.06, z) + 0.28 * ringNoise(s, vi * 1.8, 0.13, z + 3.1) + 0.12 * ringNoise(s, vi * 3.6, 0.27, z + 7.7));
|
|
}
|
|
void main() {
|
|
vec2 p = frag();
|
|
vec4 o = vec4(0.);
|
|
// contact shadow thrown down-left, and the light the water focuses into it
|
|
vec2 so = -L2 * 5.5;
|
|
vec2 ps = p - so;
|
|
float dsh = sdRB(ps);
|
|
float sr = abs(dsh) / HW; // glass passes most light: the shadow is darkest under the walls
|
|
o.a = (1. - smoothstep(-4., 10., abs(dsh) - HW)) * mix(0.05, 0.2, uDark) + exp(-pow((sr - 0.95) / 0.35, 2.)) * mix(0.08, 0.06, uDark);
|
|
{
|
|
vec3 Ws = slug(pathS(ps), clamp(dsh / (HW * RI), -1., 1.));
|
|
float cz = exp(-pow((dsh / HW + 0.2) / 0.3, 2.)) * Ws.x;
|
|
o.rgb += mix(uColor, vec3(1.), 0.25) * cz * mix(0.45, 0.12, uDark);
|
|
}
|
|
float dc = sdRB(p);
|
|
float v = dc / HW;
|
|
float tube = clamp((HW - abs(dc)) / uPx + 0.5, 0., 1.);
|
|
if (tube <= 0.) { gl_FragColor = o; return; }
|
|
vec2 n2 = normalize(p - clamp(p, -H, H) + 1e-4);
|
|
vec2 t2 = vec2(-n2.y, n2.x);
|
|
float s = pathS(p);
|
|
float vc = clamp(v, -1., 1.);
|
|
vec3 N = vec3(n2 * vc, sqrt(max(0., 1. - vc * vc)));
|
|
vec3 Ld = normalize(vec3(L2 * 0.78, 0.62));
|
|
vec3 Hh = normalize(Ld + vec3(0., 0., 1.));
|
|
vec3 gTint = mix(vec3(0.42, 0.50, 0.53), vec3(0.78, 0.88, 0.92), uDark);
|
|
vec3 hiC = vec3(1.0, 0.985, 0.95);
|
|
// glass: the walls are thicker to the eye near the silhouette
|
|
float gl = 2. * (sqrt(max(0., 1. - vc * vc)) - sqrt(max(0., RI * RI - vc * vc)));
|
|
float gA = 1. - exp(-gl * 0.42);
|
|
over(o, gTint, gA * 0.3 * tube);
|
|
float vi = clamp(v / RI, -1., 1.);
|
|
float bore = clamp((RI * HW - abs(dc)) / uPx + 0.5, 0., 1.);
|
|
vec3 W = slug(s, vi);
|
|
float cov = W.x * bore;
|
|
vec2 hl = vec2(dot(L2, -t2), dot(L2, n2)); // light direction in the slug's local frame
|
|
vec3 wetC = mix(uColor, vec3(1.), 0.35);
|
|
// where the water has just left: a film on the walls and beads that drain away
|
|
float wet = texture2D(uWet, vec2(s / PER, 0.5)).r * (1. - W.x);
|
|
if (wet > 0.01 && bore > 0.) {
|
|
over(o, wetC, wet * 0.22 * smoothstep(0.5, 0.95, abs(vi)) * bore);
|
|
float cs = PER / 110.;
|
|
float gx = floor(s / cs), gy = floor((vi + 1.) * 2.);
|
|
for (int a = -1; a <= 1; a++) for (int b = -1; b <= 1; b++) {
|
|
vec2 cell = vec2(gx + float(a), gy + float(b));
|
|
if (cell.y < 0. || cell.y > 3.) continue;
|
|
vec2 hc = vec2(mod(cell.x, 110.), cell.y);
|
|
vec2 h = hash22(hc);
|
|
float h3 = hash12(hc.yx + 17.);
|
|
float pres = smoothstep(h3, h3 + 0.3, wet * 1.2) * step(0.45, hash12(hc + 3.7));
|
|
if (pres <= 0.01) continue;
|
|
vec2 c = vec2((cell.x + h.x) * cs, clamp(-1. + (cell.y + h.y) * 0.5, -0.88, 0.88));
|
|
float r = mix(0.35, 1.3, h.x * h.y) * sqrt(pres);
|
|
vec2 dl = vec2(s - c.x, (vi - c.y) * RI * HW);
|
|
float d = length(dl) - r;
|
|
float fill = clamp(-d / uPx + 0.5, 0., 1.) * bore;
|
|
over(o, wetC * 0.75, fill * 0.45);
|
|
over(o, uColor * 0.4, exp(-pow(d / 0.25, 2.)) * 0.35 * bore);
|
|
over(o, hiC, exp(-pow(length(dl - vec2(-hl.x, hl.y) * r * 0.45) / (0.28 * r + 0.08), 2.)) * 0.85 * bore);
|
|
}
|
|
}
|
|
if (cov > 0.) {
|
|
float th = sqrt(max(0., 1. - vi * vi));
|
|
float kv = 1. + 0.4 * (1. - 2. * vi * vi); // the centre runs ahead of the walls
|
|
float tex = mix(flowTex(s - kv * uFlowA, vi, 0.), flowTex(s - kv * uFlowB, vi, 7.3), uFlowW);
|
|
float side = dot(n2 * vi, L2);
|
|
vec3 wc = mix(mix(uColor, vec3(1.), 0.55), uColor, smoothstep(0.05, 0.9, th)); // Beer-Lambert: thicker is deeper
|
|
wc *= 1. - 0.55 * smoothstep(0.62, 1.0, abs(vi)); // total internal reflection at the wall
|
|
wc += mix(uColor, vec3(1.), 0.65) * exp(-pow((side + 0.42) / 0.17, 2.)) * 0.42; // caustic on the far wall
|
|
wc *= 0.8 + 0.4 * tex;
|
|
float spA = smoothstep(0.62, 0.9, ringNoise(s - kv * uFlowA, vi * 4., 0.45, 11.));
|
|
float spB = smoothstep(0.62, 0.9, ringNoise(s - kv * uFlowB, vi * 4., 0.45, 19.));
|
|
wc += 0.25 * mix(spA, spB, uFlowW);
|
|
wc *= 1. - 0.6 * exp(-pow(W.z / 1.1, 2.)); // meniscus: dark refracted band
|
|
wc += 0.5 * exp(-pow((W.z - 2.1) / 0.7, 2.)) * mix(uColor, vec3(1.), 0.75); // and its bright inner lip
|
|
over(o, wc, (0.58 + 0.34 * th + 0.2 * exp(-pow(W.z / 1.5, 2.))) * cov);
|
|
// entrained bubbles ride the flow; they only move when the water does
|
|
float full = 1. - smoothstep(0.3, 1.5, PER - uLen);
|
|
for (int i = 0; i < 12; i++) {
|
|
float fi = float(i);
|
|
float bv = (hash11(fi + 1.3) * 2. - 1.) * 0.62;
|
|
float br = mix(0.5, 1.9, hash11(fi + 7.9) * hash11(fi + 3.1));
|
|
float bk = 1. + 0.35 * (1. - 2. * bv * bv) + 0.2 * (hash11(fi + 4.4) - 0.5);
|
|
float bs = mod(hash11(fi + 9.2) * PER + bk * uMid, PER);
|
|
float bx = mod(uHead - bs + 24., PER) - 24.;
|
|
float fade = mix(smoothstep(4., 9., bx) * smoothstep(uLen, uLen - 9., bx), 1., full);
|
|
vec2 dl = vec2(W.y - bx, (vi - bv) * RI * HW);
|
|
dl.x -= PER * floor(dl.x / PER + 0.5);
|
|
float d = length(dl) - br;
|
|
if (d < 1.2 && fade > 0.) {
|
|
float f = fade * cov;
|
|
over(o, mix(wc, vec3(1.), 0.5), clamp(-d / uPx + 0.5, 0., 1.) * 0.3 * f);
|
|
over(o, wc * 0.3, exp(-pow(d / 0.28, 2.)) * 0.6 * f);
|
|
over(o, hiC, exp(-pow(length(dl - hl * br * 0.42) / (0.22 * br + 0.12), 2.)) * 0.9 * f);
|
|
}
|
|
}
|
|
}
|
|
// front wall, reflections
|
|
over(o, gTint, gA * 0.3 * tube);
|
|
float ndh = max(dot(N, Hh), 0.);
|
|
float spec = pow(ndh, 110.) * 0.95 + pow(ndh, 14.) * 0.09;
|
|
spec += pow(max(dot(vec3(-N.xy, N.z), Hh), 0.), 70.) * 0.28; // the back inner surface
|
|
float fres = pow(1. - N.z, 3.) * mix(0.12, 0.3, uDark);
|
|
float wl = exp(-pow((abs(v) - RI) * HW / 0.3, 2.)) * 0.2; // bore wall seen edge-on
|
|
over(o, hiC, (spec + fres + wl) * tube);
|
|
over(o, gTint * 0.55, exp(-pow((abs(v) - 1.) * HW / 0.45, 2.)) * mix(0.45, 0.1, uDark) * tube);
|
|
gl_FragColor = o;
|
|
}`;
|
|
const SMOKE = `
|
|
uniform vec4 uPuff[${NPUFF}]; // one puff per stretch of the frame: centre xy, radius, weight
|
|
const float W0 = ${W0.toFixed(4)};
|
|
float fbm(vec3 p) { float f = 0., a = 0.55; for (int i = 0; i < 4; i++) { f += a * snoise(p); p = p * 2.03 + vec3(1.7, 9.2, 3.1); a *= 0.42; } return f; }
|
|
vec2 warpAt(vec2 p) { vec3 np = vec3(p * 0.022, uTime * 0.12); return vec2(snoise(np), snoise(np + vec3(19.1, 7.3, 3.7))); }
|
|
// x: density, y: mean growth of the puffs here (released smoke has grown)
|
|
vec2 puffs(vec2 p, vec2 warp) {
|
|
float D = 0., G = 0.;
|
|
for (int i = 0; i < ${NPUFF}; i++) {
|
|
vec4 f = uPuff[i];
|
|
float grow = f.z - W0;
|
|
vec2 q = p - warp * (1.5 + 0.6 * grow) - f.xy; // turbulence displaces older smoke further (kept below folding)
|
|
float g = f.w * exp(-dot(q, q) / (f.z * f.z));
|
|
D += g; G += g * grow;
|
|
}
|
|
return vec2(D, G / max(D, 1e-5));
|
|
}
|
|
void main() {
|
|
vec2 p = frag();
|
|
vec2 wp = warpAt(p);
|
|
vec2 P = puffs(p, wp);
|
|
float ext = min(0.5 * uRes.x * uPx - abs(p.x), 0.5 * uRes.y * uPx - abs(p.y));
|
|
P.x *= smoothstep(0., 16., ext);
|
|
if (P.x < 0.004) { gl_FragColor = vec4(0.); return; }
|
|
// billows: domain-warped noise evolving in place, eaten away as the smoke ages
|
|
vec2 pw = p - wp * (1.5 + 0.6 * P.y);
|
|
vec3 dp = vec3(pw * 0.035, uTime * 0.16);
|
|
vec2 q = vec2(snoise(dp), snoise(dp + vec3(5.2, 1.3, 2.8)));
|
|
float n = fbm(vec3(pw * 0.045 + 1.1 * q, uTime * 0.2 + 3.));
|
|
float bil = smoothstep(-0.7, 0.55, n + 0.1 - 0.04 * P.y);
|
|
float d = P.x * (0.5 + 0.75 * bil);
|
|
// light from the upper right: shade by how much smoke lies toward it
|
|
vec2 pl = p + L2 * 7.;
|
|
float dl = puffs(pl, warpAt(pl)).x;
|
|
float lit = clamp(mix(0.55, 0.85, uDark) - (dl - P.x) * mix(1.3, 0.9, uDark), 0., 1.) * (0.88 + 0.16 * bil);
|
|
float a = 1. - exp(-d * 1.9);
|
|
vec3 shade = uColor * mix(vec3(0.62, 0.64, 0.70), vec3(0.50, 0.53, 0.60), uDark);
|
|
vec3 c = mix(shade, uColor, clamp(lit, 0., 1.));
|
|
c += uColor * 0.18 * (1. - a) * lit; // thin wisps let light through
|
|
gl_FragColor = vec4(min(c, vec3(1.)) * a, a);
|
|
}`;
|
|
const NEON = `
|
|
uniform float uSegA, uLen, uMix, uMove, uHum, uTR;
|
|
uniform vec3 uColA, uColB;
|
|
uniform vec2 uPts[${NPTS}];
|
|
vec3 emit(float s) {
|
|
float x = mod(s - uSegA, PER); // distance ahead of the tail
|
|
float din = x <= uLen ? min(x, uLen - x) : -min(x - uLen, PER - x);
|
|
din = mix(din, 1e3, step(PER - 0.5, uLen));
|
|
float on = smoothstep(-0.6, 0.6, din);
|
|
float strike = uMove * exp(-max(uLen - x, 0.) / 18.) * on; // the end being struck stutters while it runs
|
|
float fl = mix(1., 0.15 + 0.85 * step(0.3, hash12(vec2(floor(x / 6.), floor(uTime * 30.)))), strike);
|
|
float after = uMove * exp(-mod(uSegA - s, PER) / 10.) * (1. - on) * 0.6; // the tail lets go with an afterglow
|
|
vec3 col = mix(uColB, uColA, uMix);
|
|
return (col * (on * fl * (1. + 0.6 * strike) + after) + vec3(0.35) * strike * fl) * uHum;
|
|
}
|
|
void main() {
|
|
vec2 p = frag();
|
|
float dc = sdRB(p), d = abs(dc), s = pathS(p);
|
|
vec3 e = emit(s);
|
|
// light spilling onto the wall: integrated over the whole tube so every lit stretch contributes
|
|
vec3 g = vec3(0.);
|
|
for (int i = 0; i < ${NPTS}; i++) {
|
|
vec2 dv = p - uPts[i];
|
|
float r2 = dot(dv, dv);
|
|
g += emit((float(i) + 0.5) * (PER / ${NPTS}.)) * (exp(-r2 / 60.) + 0.35 * exp(-r2 / 420.));
|
|
}
|
|
g *= (PER / ${NPTS}.) / 40.;
|
|
float vt = clamp(dc / uTR, -1., 1.);
|
|
float tm = clamp((uTR - d) / uPx + 0.5, 0., 1.);
|
|
float I = max(max(e.r, e.g), e.b);
|
|
float core = exp(-vt * vt * 3.2);
|
|
vec3 tubeL = e * (0.4 + 0.85 * core) + vec3(1.) * I * pow(core, 4.) * 0.55; // white-hot centre, saturated rim
|
|
vec3 nearL = e * exp(-max(d - uTR * 0.8, 0.) / 1.2) * 0.6;
|
|
vec3 L = g + nearL * (1. - tm) + tubeL * tm;
|
|
vec3 mapped = 1. - exp(-L * 1.25);
|
|
// the glass itself, visible where it is dark
|
|
vec2 n2 = normalize(p - clamp(p, -H, H) + 1e-4);
|
|
vec3 N = vec3(n2 * vt, sqrt(max(0., 1. - vt * vt)));
|
|
vec3 Hh = normalize(normalize(vec3(L2 * 0.78, 0.62)) + vec3(0., 0., 1.));
|
|
vec3 gTint = mix(vec3(0.22, 0.24, 0.27), vec3(0.62, 0.66, 0.72), uDark);
|
|
vec4 o = vec4(0.);
|
|
over(o, gTint, tm * (0.14 + 0.35 * pow(1. - N.z, 2.)) * (1. - clamp(I, 0., 1.) * 0.6));
|
|
if (uDark > 0.5) o.rgb += mapped;
|
|
else { // on paper light can't add; lay the hue down instead, keeping it saturated
|
|
vec3 Lc = g + nearL * (1. - tm) + e * (0.4 + 0.85 * core) * tm;
|
|
float m = max(max(Lc.r, Lc.g), Lc.b);
|
|
over(o, Lc / max(m, 1e-3), (1. - exp(-m * 1.4)) * mix(0.85, 1., tm));
|
|
over(o, vec3(1.), pow(core, 4.) * min(I, 1.) * 0.6 * tm);
|
|
}
|
|
over(o, vec3(1.), pow(max(dot(N, Hh), 0.), 60.) * 0.5 * tm);
|
|
gl_FragColor = o;
|
|
}`;
|
|
const SRC = { water: WATER, smoke: SMOKE, neon: NEON };
|
|
const PAD = { water: 1.3, smoke: 1.9, neon: 1.8 };
|
|
const DEFAULTS = {
|
|
water: { color: "#2FA8FF" },
|
|
smoke: { color: "#F5F5F2" },
|
|
neon: { colors: ["#FF2E88", "#2EF0FF", "#FFB020", "#7CFF4F", "#9B5CFF"], tube: 0.58 },
|
|
};
|
|
const COMMON = { sequence: "LUCIO", hold: 0.7, morph: 0.45, speed: 1, size: 96 };
|
|
const rgb = hex => { const h = hex.replace("#", ""), f = h.length === 3 ? h.split("").map(c => c + c).join("") : h; return [0, 2, 4].map(i => parseInt(f.slice(i, i + 2), 16) / 255); };
|
|
const reduced = () => global.matchMedia && global.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
const ease = f => f < 0.5 ? 4 * f * f * f : 1 - Math.pow(-2 * f + 2, 3) / 2;
|
|
const fract = x => x - Math.floor(x);
|
|
const hash = n => fract(Math.sin(n * 127.1 + 11.7) * 43758.5453);
|
|
const smooth = (a, b, x) => { const k = Math.min(1, Math.max(0, (x - a) / (b - a))); return k * k * (3 - 2 * k); };
|
|
function bgIsDark(el) {
|
|
for (let n = el; n && n.nodeType === 1; n = n.parentElement) {
|
|
const m = getComputedStyle(n).backgroundColor.match(/[\d.]+/g);
|
|
if (m && (m.length < 4 || +m[3] > 0.5)) return (0.2126 * m[0] + 0.7152 * m[1] + 0.0722 * m[2]) / 255 < 0.5;
|
|
}
|
|
return !!(global.matchMedia && global.matchMedia("(prefers-color-scheme: dark)").matches);
|
|
}
|
|
const LET = { C: [0, .75], L: [.25, .75], U: [.25, 1], I: [.75, 1], O: [.75, 1.75] };
|
|
function speller(o) {
|
|
const seq = () => { const s = (o.sequence || "LUCIO").toUpperCase().split("").filter(c => LET[c]); return s.length ? s : ["O"]; };
|
|
const target = (cur, ch) => { const [s2, e2] = LET[ch]; for (let q = -1; q < 6; q++) { if (s2 + q >= cur[0] - 1e-6 && e2 + q >= cur[1] - 1e-6) return [s2 + q, e2 + q]; } return [s2, e2]; };
|
|
const st = { i: 0, j: 0, seg: LET[seq()[0]].slice(), u: 1, f: 1, moving: false, travel: 0, vHead: 0 };
|
|
let from = st.seg, to = st.seg, timer = 0;
|
|
st.step = dt => {
|
|
const before = st.seg.slice();
|
|
timer += dt;
|
|
if (!st.moving && timer >= o.hold) {
|
|
const s = seq(); st.j = (st.i + 1) % s.length; from = st.seg.slice(); to = target(st.seg, s[st.j]);
|
|
st.moving = true; timer -= o.hold;
|
|
}
|
|
if (st.moving) {
|
|
const dur = reduced() ? 0 : o.morph;
|
|
st.f = dur > 0 ? Math.min(1, timer / dur) : 1; st.u = ease(st.f);
|
|
st.seg = [from[0] + (to[0] - from[0]) * st.u, from[1] + (to[1] - from[1]) * st.u];
|
|
if (st.f >= 1) { st.moving = false; st.i = st.j; timer = 0; }
|
|
} else { st.f = 1; st.u = 1; }
|
|
st.travel += ((st.seg[0] - before[0]) + (st.seg[1] - before[1])) / 2 * PER;
|
|
st.vHead = dt > 0 ? (st.seg[1] - before[1]) * PER / dt : 0;
|
|
if (!st.moving) { const n = Math.floor(st.seg[0]); st.seg[0] -= n; st.seg[1] -= n; }
|
|
};
|
|
st.units = () => ({ a: PER * (st.seg[0] + 0.25), len: Math.min(PER, (st.seg[1] - st.seg[0]) * PER) });
|
|
st.reset = () => { st.i = st.j = 0; st.seg = LET[seq()[0]].slice(); st.moving = false; timer = 0; };
|
|
return st;
|
|
}
|
|
const live = new Set();
|
|
let raf = 0, last = 0;
|
|
function loop(now) {
|
|
const dt = last ? Math.min(0.1, (now - last) / 1000) : 0; last = now;
|
|
for (const inst of live) inst.frame(dt);
|
|
raf = live.size ? requestAnimationFrame(loop) : 0;
|
|
if (!raf) last = 0;
|
|
}
|
|
function create(kind, container, opts) {
|
|
const o = Object.assign({}, COMMON, DEFAULTS[kind], opts || {});
|
|
const wrap = document.createElement("div");
|
|
wrap.className = "lucio-fx lucio-fx-" + kind;
|
|
wrap.setAttribute("role", "img"); wrap.setAttribute("aria-label", "Loading");
|
|
wrap.style.cssText = "position:relative;display:inline-block;vertical-align:middle;flex:none";
|
|
const canvas = document.createElement("canvas");
|
|
canvas.style.cssText = "position:absolute;pointer-events:none";
|
|
wrap.appendChild(canvas); container.appendChild(wrap);
|
|
const gl = canvas.getContext("webgl", { premultipliedAlpha: true, alpha: true, antialias: false });
|
|
if (!gl) { console.warn("LucioLoaders: WebGL unavailable"); return null; }
|
|
const sh = (type, src) => { const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s); if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(kind + ": " + gl.getShaderInfoLog(s)); return s; };
|
|
const prog = gl.createProgram();
|
|
gl.attachShader(prog, sh(gl.VERTEX_SHADER, "attribute vec2 a; void main(){ gl_Position = vec4(a, 0., 1.); }"));
|
|
gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, PRELUDE + SRC[kind]));
|
|
gl.linkProgram(prog);
|
|
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) throw new Error(kind + ": " + gl.getProgramInfoLog(prog));
|
|
gl.useProgram(prog);
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
|
|
gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
const U = {}; const u = n => n in U ? U[n] : (U[n] = gl.getUniformLocation(prog, n));
|
|
if (kind === "neon") { const pts = []; for (let i = 0; i < NPTS; i++) pts.push(...pathP((i + 0.5) * PER / NPTS)); gl.uniform2fv(u("uPts[0]"), new Float32Array(pts)); }
|
|
const BINS = 64, wetAt = new Float32Array(BINS).fill(-1e3), wetTex = new Uint8Array(BINS);
|
|
if (kind === "water") {
|
|
gl.bindTexture(gl.TEXTURE_2D, gl.createTexture());
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
gl.uniform1i(u("uWet"), 0);
|
|
}
|
|
const puff = new Float32Array(NPUFF * 4), pDen = new Float32Array(NPUFF), pAge = new Float32Array(NPUFF);
|
|
const sp = speller(o);
|
|
const inside = (s, a, len) => { if (len >= PER - 0.5) return 1e3; const x = ((s - a) % PER + PER) % PER; return x <= len ? Math.min(x, len - x) : -Math.min(x - len, PER - x); };
|
|
let t = 0, dark = o.theme === "light" ? false : o.theme === "dark" ? true : bgIsDark(container), frames = 0, visible = true;
|
|
const io = global.IntersectionObserver ? new IntersectionObserver(es => { visible = es[0].isIntersecting; }) : null;
|
|
if (io) io.observe(wrap);
|
|
function setSize(px) {
|
|
o.size = px;
|
|
const dpr = Math.min(global.devicePixelRatio || 1, 2), c = px * PAD[kind];
|
|
wrap.style.width = wrap.style.height = px + "px";
|
|
canvas.style.width = canvas.style.height = c + "px";
|
|
canvas.style.left = canvas.style.top = (-(c - px) / 2) + "px";
|
|
canvas.width = canvas.height = Math.round(c * dpr);
|
|
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
gl.uniform2f(u("uRes"), canvas.width, canvas.height);
|
|
gl.uniform1f(u("uPx"), 120 / (px * canvas.width / c));
|
|
}
|
|
setSize(o.size);
|
|
function frame(dt) {
|
|
if (o.theme === "auto" || !o.theme) { if (++frames % 30 === 0) dark = bgIsDark(container); }
|
|
if (!visible) return;
|
|
dt *= o.speed;
|
|
t += dt * (reduced() ? 0.35 : 1);
|
|
sp.step(dt);
|
|
const { a, len } = sp.units();
|
|
gl.uniform1f(u("uTime"), t);
|
|
gl.uniform1f(u("uDark"), dark ? 1 : 0);
|
|
if (kind === "water") {
|
|
const Df = 70, pa = fract(sp.travel / Df), pb = fract(sp.travel / Df + 0.5);
|
|
gl.uniform3fv(u("uColor"), rgb(o.color));
|
|
gl.uniform1f(u("uHead"), a + len); gl.uniform1f(u("uLen"), len);
|
|
gl.uniform1f(u("uSurge"), Math.min(1, Math.max(0, sp.vHead / 700)));
|
|
gl.uniform1f(u("uMid"), sp.travel);
|
|
gl.uniform1f(u("uFlowA"), pa * Df); gl.uniform1f(u("uFlowB"), pb * Df);
|
|
gl.uniform1f(u("uFlowW"), 1 - Math.abs(2 * pb - 1));
|
|
for (let i = 0; i < BINS; i++) {
|
|
if (inside((i + 0.5) * PER / BINS, a, len) > -2) wetAt[i] = t;
|
|
wetTex[i] = Math.round(255 * Math.exp(-(t - wetAt[i]) / 1.1));
|
|
}
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.LUMINANCE, BINS, 1, 0, gl.LUMINANCE, gl.UNSIGNED_BYTE, wetTex);
|
|
} else if (kind === "smoke") {
|
|
gl.uniform3fv(u("uColor"), rgb(o.color));
|
|
const kIn = 1 - Math.exp(-dt * 20), kOut = Math.exp(-dt * 4.2);
|
|
for (let i = 0; i < NPUFF; i++) {
|
|
const s = (i + 0.5) * PER / NPUFF, cover = smooth(-6, 6, inside(s, a, len));
|
|
if (cover >= pDen[i]) { pDen[i] += (cover - pDen[i]) * kIn; pAge[i] *= Math.exp(-dt * 8); }
|
|
else { pDen[i] = Math.max(cover, pDen[i] * kOut); pAge[i] += dt; }
|
|
const w = W0 + 10 * pAge[i], [x, y] = pathP(s);
|
|
puff.set([x, y + 9 * pAge[i], w, pDen[i] * (W0 * 1.6 / (w + 0.6 * W0)) * (PER / NPUFF) / (1.7725 * w)], i * 4);
|
|
}
|
|
gl.uniform4fv(u("uPuff[0]"), puff);
|
|
} else {
|
|
const pal = o.colors.map(rgb), n = pal.length;
|
|
gl.uniform1f(u("uTR"), HW * o.tube);
|
|
gl.uniform1f(u("uHum"), 0.975 + 0.025 * Math.sin(t * 46) * Math.sin(t * 7.3) + (hash(Math.floor(t * 9)) > 0.985 ? -0.2 : 0));
|
|
gl.uniform1f(u("uSegA"), a); gl.uniform1f(u("uLen"), len);
|
|
gl.uniform3fv(u("uColB"), pal[sp.i % n]); gl.uniform3fv(u("uColA"), pal[(sp.moving ? sp.j : sp.i) % n]);
|
|
gl.uniform1f(u("uMix"), sp.moving ? smooth(0.05, 0.35, sp.f) : 1);
|
|
gl.uniform1f(u("uMove"), sp.moving ? Math.sin(Math.PI * sp.f) : 0);
|
|
}
|
|
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
}
|
|
const inst = { frame };
|
|
live.add(inst);
|
|
if (!raf) raf = requestAnimationFrame(loop);
|
|
return {
|
|
el: wrap, canvas,
|
|
tick: frame,
|
|
setColor(c) { o.color = c; if (kind === "neon") o.colors = [c]; },
|
|
setColors(cs) { o.colors = cs.slice(); },
|
|
setTiming({ hold, morph }) { if (hold != null) o.hold = hold; if (morph != null) o.morph = morph; },
|
|
setSequence(seq) { o.sequence = seq; sp.reset(); },
|
|
setSpeed(s) { o.speed = s; },
|
|
setSize,
|
|
setTheme(th) { o.theme = th; dark = th === "light" ? false : th === "dark" ? true : bgIsDark(container); },
|
|
stop() { live.delete(inst); if (io) io.disconnect(); wrap.remove(); const ext = gl.getExtension("WEBGL_lose_context"); if (ext) ext.loseContext(); },
|
|
};
|
|
}
|
|
global.LucioLoaders = {
|
|
water: (el, opts) => create("water", el, opts),
|
|
smoke: (el, opts) => create("smoke", el, opts),
|
|
neon: (el, opts) => create("neon", el, opts),
|
|
geometry: { HB, CR, HW, PER, pathP, LET },
|
|
};
|
|
})(window);
|