/** * Vertex + fragment shader sources lifted from `image.html` (lines 961–1548). * All effect branches are kept in the source so future presets can select any * of them without re-touching the shader; the renderer compiles one effect per * program (see `specializeEffect`). The mosaic stage after `computeEffect` is * split into the `imgfx_*` helpers so it can also run as two passes. */ export const VERT_SRC = /* glsl */ ` void main() { gl_Position = vec4(position.xy, 0.0, 1.0); } `; export const FRAG_SRC = /* glsl */ ` uniform vec2 u_resolution; uniform float u_dpr; uniform float u_time; uniform vec3 u_color1, u_color2, u_color3, u_color4, u_color5, u_color6, u_color7, u_cardBg; uniform float u_alpha1, u_alpha2, u_alpha3, u_alpha4, u_alpha5, u_alpha6, u_alpha7; uniform float u_speed, u_intensity, u_scale, u_direction; uniform float u_softness, u_distortion, u_complexity, u_shape, u_flicker; uniform float u_vignette, u_vigOpacity, u_blur, u_highlight, u_shaderOpacity; uniform float u_cellSize, u_gap, u_dotSize, u_dotSoftness, u_dotOpacity, u_hlScale, u_fillOpacity, u_edgeFade, u_fadeStr; uniform float u_dotMode; uniform int u_effect; uniform int u_sweepEase; // Two-pass mosaic (see the mosaic stages above main). Unused by IMGFX_PASS 0. uniform vec2 u_grid; uniform vec2 u_fieldSize; uniform highp sampler2D u_field; #ifndef IMGFX_PASS #define IMGFX_PASS 0 #endif // Reference card edge length (CSS px) at which the original preset cellSize // gives the canonical cell count. Cell PIXEL size stays constant across card // sizes by scaling gridSize proportionally to (currentCssDim / REF_DIM). const float REF_DIM = 320.0; /** Anisotropic cell count: returns the number of cells along x and y so that * each cell stays SQUARE in screen space regardless of the card's aspect * ratio. A 600×300 card gets twice as many cells horizontally as vertically; * cells stay the same physical size as on a 300×300 card. */ vec2 gridCounts(float baseCount) { vec2 cssRes = u_resolution / max(u_dpr, 0.0001); return max(vec2(2.0), floor(baseCount * cssRes / REF_DIM)); } vec3 mod289(vec3 x) { return x - floor(x * (1.0/289.0)) * 289.0; } vec2 mod289v2(vec2 x) { return x - floor(x * (1.0/289.0)) * 289.0; } vec3 permute(vec3 x) { return mod289((x * 34.0 + 1.0) * x); } float snoise(vec2 v) { const vec4 C = vec4(0.211324865405187, 0.366025403784439, -0.577350269189626, 0.024390243902439); vec2 i = floor(v + dot(v, C.yy)); vec2 x0 = v - i + dot(i, C.xx); vec2 i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0); vec4 x12 = x0.xyxy + C.xxzz; x12.xy -= i1; i = mod289v2(i); vec3 p = permute(permute(i.y + vec3(0.0, i1.y, 1.0)) + i.x + vec3(0.0, i1.x, 1.0)); vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0); m = m * m; m = m * m; vec3 x_ = 2.0 * fract(p * C.www) - 1.0; vec3 h = abs(x_) - 0.5; vec3 ox = floor(x_ + 0.5); vec3 a0 = x_ - ox; m *= 1.79284291400159 - 0.85373472095314 * (a0*a0 + h*h); vec3 g; g.x = a0.x * x0.x + h.x * x0.y; g.yz = a0.yz * x12.xz + h.yz * x12.yw; return 130.0 * dot(m, g); } float fbm(vec2 p, float oct) { float val = 0.0, amp = 0.5; int n = int(oct); for (int i = 0; i < 4; i++) { if (i >= n) break; val += amp * snoise(p); p *= 2.0; amp *= 0.5; } return val; } float nfbm(vec2 p) { return fbm(p, 2.0 + u_complexity * 2.0); } vec3 palette(float t) { t = clamp(t, 0.0, 1.0); t = t * t * (3.0 - 2.0 * t); float k = 64.0; float w1 = u_alpha1 * exp(-k * t * t); float w2 = u_alpha2 * exp(-k * (t - 0.25) * (t - 0.25)); float w3 = u_alpha3 * exp(-k * (t - 0.5) * (t - 0.5)); float w4 = u_alpha4 * exp(-k * (t - 0.75) * (t - 0.75)); float w5 = u_alpha5 * exp(-k * (t - 1.0) * (t - 1.0)); float total = w1 + w2 + w3 + w4 + w5 + 0.0001; return (u_color1*w1 + u_color2*w2 + u_color3*w3 + u_color4*w4 + u_color5*w5) / total; } vec3 softBlend(float a, float b, float c) { a = clamp(a, 0.0, 1.0); a *= a; b = clamp(b, 0.0, 1.0); b *= b; c = clamp(c, 0.0, 1.0); c *= c; float d = clamp(a * 0.7 + c * 0.3, 0.0, 1.0); d *= d; float e = clamp(b * 0.5 + c * 0.5, 0.0, 1.0); e *= e; a *= u_alpha1; b *= u_alpha2; c *= u_alpha3; d *= u_alpha4; e *= u_alpha5; float total = a + b + c + d + e; float floorW = max(0.001 - total, 0.0); vec3 fallback = (u_color1 + u_color2 + u_color3 + u_color4 + u_color5) * 0.2; return (u_color1 * a + u_color2 * b + u_color3 * c + u_color4 * d + u_color5 * e + fallback * floorW) / (total + floorW); } vec2 warp(vec2 p, float t) { float str = u_distortion * 2.0; return vec2( nfbm(p + vec2(t * 0.1, 0.0)), nfbm(p + vec2(0.0, t * 0.12) + 5.0) ) * str; } float sweepEase(float x) { if (u_sweepEase == 1) return x * x * (3.0 - 2.0 * x); if (u_sweepEase == 2) { float p = 1.0 - x; return 1.0 - p * p * p; } if (u_sweepEase == 3) { return x < 0.5 ? 4.0 * x * x * x : 1.0 - pow(-2.0 * x + 2.0, 3.0) * 0.5; } if (u_sweepEase == 4) return 1.0 - pow(2.0, -10.0 * x) * (1.0 - x); return x; } float blob(vec2 p, vec2 center, float radius) { float r = radius * (0.5 + u_shape * 0.8); float soft = 0.05 + u_softness * 0.4; return smoothstep(r + soft, r - soft * 0.5, length(p - center)); } vec3 computeEffect(vec2 uv, float aspect, float t, float dist, float soft, float cpx, float shp) { vec2 p = (uv - 0.5) * u_scale; p.x *= aspect; p += vec2(cos(u_direction), sin(u_direction)) * t * 0.15; vec3 col = vec3(0.0); if (u_effect == 0) { float val = sin(p.x * 3.0 + t) * 0.5 + 0.5; val += sin(p.y * 2.0 + t * 0.7) * 0.3; val += sin((p.x + p.y) * (1.0 + cpx * 3.0) - t * 0.5) * 0.2; vec2 w = warp(p, t); val += (w.x + w.y) * 0.15; col = palette(clamp(val * u_intensity, 0.0, 1.0)); } else if (u_effect == 1) { float freq = 3.0 + cpx * 8.0; float val = 0.0; val += sin(p.x * freq + t); val += sin(p.y * freq + t * 1.3); val += sin((p.x + p.y) * freq * 0.7 + t * 0.7); val += sin(length(p) * freq * 0.8 - t * 1.5); vec2 w = warp(p, t); val += (w.x + w.y) * dist; val = val * 0.2 * u_intensity + 0.5; col = palette(clamp(val, 0.0, 1.0)); } else if (u_effect == 2) { vec2 q = vec2(nfbm(p + t * 0.3), nfbm(p + vec2(5.2, 1.3) + t * 0.2)); float val = nfbm(p + q * (1.0 + dist * 3.0) + t * 0.1); val = val * u_intensity * 0.5 + 0.5; col = palette(clamp(val, 0.0, 1.0)); } else if (u_effect == 3) { float d = length(p); float val = sin(d * (3.0 + cpx * 6.0) - t * 2.0) * 0.5 + 0.5; val *= exp(-d * (0.3 + shp * 1.0)); val += sin(atan(p.y, p.x) * (1.5 + cpx * 2.0) + t) * 0.15; col = palette(clamp(val * u_intensity, 0.0, 1.0)); } else if (u_effect == 4) { vec2 q = vec2(nfbm(p * (0.5 + shp * 0.6) + vec2(t * 0.12, t * 0.08)), nfbm(p * (0.5 + shp * 0.6) + vec2(t * 0.09, -t * 0.11))); vec2 r = vec2(nfbm(p + q * (1.0 + dist * 2.0) + vec2(1.7, 9.2) + t * 0.06), nfbm(p + q * (1.0 + dist * 2.0) + vec2(8.3, 2.8) - t * 0.08)); float val = nfbm(p + r * 2.0); float lo = -0.3 - soft * 0.5; float hi = 0.5 + soft * 0.5; val = smoothstep(lo, hi, val * u_intensity); col = palette(val); } else if (u_effect == 5) { float n1 = nfbm(vec2(p.x * 0.5 + t * 0.15, p.y * (1.0 + cpx * 1.5))); float n2 = nfbm(vec2(p.x * 0.3 - t * 0.1, p.y * (0.8 + cpx * 1.0) + 3.0)); float band = sin(p.y * 3.0 + n1 * (1.0 + dist * 2.0) + t * 0.3) * 0.5 + 0.5; float shimmer = sin(p.y * 4.0 + n2 * 1.5 - t * 0.2) * 0.5 + 0.5; float w1 = band * (0.5 + 0.5 * sin(p.x * 1.5 + t * 0.2 + n1)); float w2 = shimmer * (0.5 + 0.5 * cos(p.x * 1.0 - t * 0.15 + n2)); float w3 = nfbm(p * 0.5 + t * 0.05) * 0.5 + 0.5; col = softBlend(w1 * u_intensity, w2 * u_intensity, w3 * 0.6 * u_intensity); } else if (u_effect == 6) { vec2 wp = warp(p * 1.2, t); float blobR = 0.15 + shp * 0.2; float b1 = blob(p, vec2(sin(t * 0.3) * 0.3, cos(t * 0.2) * 0.4) + wp * 0.2, blobR); float b2 = blob(p, vec2(cos(t * 0.25) * 0.4, sin(t * 0.35) * 0.3 - 0.2) + wp * 0.15, blobR * 1.2); float b3 = blob(p, vec2(-sin(t * 0.2) * 0.3, -cos(t * 0.3) * 0.35) + wp * 0.18, blobR); float bg = nfbm(p * 0.5 + t * 0.05) * 0.3 + 0.15; col = softBlend((b1 + bg * 0.5) * u_intensity, (b2 + bg * 0.3) * u_intensity, (b3 + bg * 0.4) * u_intensity); } else if (u_effect == 7) { float sz = 0.4 + shp * 0.6; float sigma = sz * sz * 2.0; vec2 a1 = vec2(-0.45 + sin(t * 0.07) * 0.06, 0.45 + cos(t * 0.09) * 0.05); vec2 a2 = vec2(0.45 + cos(t * 0.08) * 0.06, 0.45 + sin(t * 0.06) * 0.05); vec2 a3 = vec2(0.0 + sin(t * 0.05) * 0.1, 0.0 + cos(t * 0.07) * 0.1); vec2 a4 = vec2(-0.4 + cos(t * 0.06) * 0.07, -0.3 + sin(t * 0.08) * 0.06); vec2 a5 = vec2(0.4 + sin(t * 0.07) * 0.06, -0.4 + cos(t * 0.05) * 0.06); float g1 = exp(-dot(p - a1, p - a1) / sigma); float g2 = exp(-dot(p - a2, p - a2) / sigma); float g3 = exp(-dot(p - a3, p - a3) / sigma); float g4 = exp(-dot(p - a4, p - a4) / sigma); float g5 = exp(-dot(p - a5, p - a5) / sigma); float nudge = dist > 0.01 ? snoise(p * (0.5 + cpx) + t * 0.04) * dist * 0.08 : 0.0; float w1 = (g1 + g4 + nudge) * u_intensity; float w2 = (g2 + g5 + nudge) * u_intensity; float w3 = (g3 + nudge) * u_intensity; col = softBlend(w1, w2, w3); } else if (u_effect == 8) { vec2 w1 = vec2(nfbm(p * (0.7 + cpx * 0.5) + t * 0.1), nfbm(p * (0.7 + cpx * 0.5) + vec2(3.3, 7.7) + t * 0.08)); vec2 w2 = vec2(nfbm(p * 0.6 + w1 * (1.0 + dist) + t * 0.06), nfbm(p * 0.6 + w1 * (1.0 + dist) + vec2(1.7, 4.2) - t * 0.07)); float f1 = nfbm(p + w2 * 1.5); float f2 = nfbm(p + w2 * 1.5 + vec2(4.1, 2.3)); float f3 = nfbm(p + w2 * 1.5 + vec2(7.5, 6.1)); col = softBlend((f1 * 0.5 + 0.5) * u_intensity, (f2 * 0.5 + 0.5) * u_intensity, (f3 * 0.5 + 0.5) * u_intensity); } else if (u_effect == 9) { vec2 sw = vec2(sin(p.y * 2.0 + t * 0.3) * 0.15 + snoise(p * 1.5 + t * 0.15) * dist * 0.3, cos(p.x * 1.8 + t * 0.25) * 0.15 + snoise(p * 1.5 + vec2(5.0, 0.0) + t * 0.12) * dist * 0.3); vec2 wp = p + sw; float caustic = (snoise(wp * (1.5 + cpx * 2.0) + t * 0.2) * 0.5 + 0.5) + (snoise(wp * (2.0 + cpx * 2.0) - t * 0.15) * 0.5 + 0.5) * 0.5; caustic = caustic / 1.5; float depth = nfbm(vec2(p.x * 0.3, p.y * 0.8) + t * 0.05) * 0.5 + 0.5; col = softBlend(depth * u_intensity, (1.0 - depth) * u_intensity, caustic * u_intensity); } else if (u_effect == 10) { float angle = 0.6 + shp * 1.2; float ca = cos(angle), sa = sin(angle); vec2 rp = vec2(p.x * ca - p.y * sa, p.x * sa + p.y * ca); float n1 = nfbm(rp * 0.8 + t * 0.12) * (1.0 + dist * 2.0); float n2 = nfbm(rp * 0.6 + vec2(3.0, 0.0) + t * 0.1) * (1.0 + dist * 1.5); float wave = sin(rp.x * (2.0 + cpx * 2.0) + n1 + t * 0.3); float wave2 = sin(rp.x * (1.5 + cpx * 1.5) + n2 - t * 0.2); float ribbon1 = exp(-2.0 * (rp.y - wave * 0.35) * (rp.y - wave * 0.35)) * u_intensity; float ribbon2 = exp(-2.0 * (rp.y - 0.15 - wave2 * 0.3) * (rp.y - 0.15 - wave2 * 0.3)) * u_intensity; float bg = nfbm(p * 0.4 + t * 0.03) * 0.5 + 0.5; col = softBlend(ribbon1, ribbon2, bg * 0.5 * u_intensity); } else if (u_effect == 11) { vec2 q = vec2(nfbm(p * 0.5 + vec2(t * 0.05, 0.0)), nfbm(p * 0.5 + vec2(0.0, t * 0.07))); vec2 r = vec2(nfbm(p * 0.6 + q * (1.0 + dist * 1.5) + vec2(1.7, 9.2) + t * 0.03), nfbm(p * 0.6 + q * (1.0 + dist * 1.5) + vec2(8.3, 2.8) + t * 0.04)); float f = nfbm(p + r * 1.5); float f2 = nfbm(p * 0.7 + r + vec2(3.0, 7.0)); col = softBlend((f * 0.5 + 0.5) * u_intensity, (f2 * 0.5 + 0.5) * u_intensity, (nfbm(p * 0.4 - t * 0.02) * 0.5 + 0.5) * u_intensity); } else if (u_effect == 12) { vec2 w = warp(p * 0.5, t * 0.7); float fold1 = sin(p.x * (1.5 + cpx * 2.0) + w.x * 1.5 + t * 0.2) * 0.5 + 0.5; float fold2 = sin(p.y * (1.2 + cpx * 1.5) + w.y * 1.5 - t * 0.15) * 0.5 + 0.5; float fold3 = sin((p.x - p.y) * (0.8 + cpx * 0.8) + (w.x + w.y) + t * 0.1) * 0.5 + 0.5; col = softBlend(fold1 * u_intensity, fold2 * u_intensity, fold3 * 0.7 * u_intensity); } else if (u_effect == 13) { float spread = 0.25 + shp * 0.35; vec2 w = warp(p, t * 0.5); vec2 c1 = vec2(sin(t * 0.08) * spread, cos(t * 0.11) * spread) + w * 0.15; vec2 c2 = vec2(cos(t * 0.09) * spread * 1.3, sin(t * 0.07) * spread) + w * 0.12; vec2 c3 = vec2(-sin(t * 0.1) * spread, -cos(t * 0.08) * spread * 1.2) + w * 0.1; float falloff = 0.3 + soft * 0.7; float d1 = 1.0 - smoothstep(0.0, falloff, length(p - c1 + w * dist * 0.3)); float d2 = 1.0 - smoothstep(0.0, falloff, length(p - c2 + w * dist * 0.25)); float d3 = 1.0 - smoothstep(0.0, falloff, length(p - c3 + w * dist * 0.2)); float detail = nfbm(p * 2.0 + t * 0.05) * cpx * 0.3; col = softBlend((d1 + detail) * u_intensity, (d2 + detail) * u_intensity, (d3 + detail) * u_intensity); } else if (u_effect == 14) { vec2 w = warp(p * 0.6, t * 0.6); float angle = atan(p.y + w.y * dist, p.x + w.x * dist); float radius = length(p); float field1 = sin(angle * (2.0 + cpx * 4.0) + radius * (3.0 + cpx * 3.0) + t * 0.4 + nfbm(p + t * 0.1) * dist * 2.0) * 0.5 + 0.5; float field2 = sin(angle * (1.5 + cpx * 2.5) - radius * 2.0 - t * 0.3 + nfbm(p * 0.6 + t * 0.08) * dist * 1.5) * 0.5 + 0.5; float bg = nfbm(p * 0.3 + t * 0.03) * 0.5 + 0.5; col = softBlend(field1 * u_intensity, field2 * u_intensity, bg * 0.5 * u_intensity); } else if (u_effect == 15) { vec2 drift = vec2(t * 0.06, t * 0.03); float c1 = nfbm((p + drift) * (0.4 + cpx * 0.5)) * 0.5 + 0.5; float c2 = nfbm((p + drift + vec2(3.7, 1.2)) * (0.35 + cpx * 0.4)) * 0.5 + 0.5; float c3 = nfbm((p + drift + vec2(7.1, 4.5)) * (0.3 + cpx * 0.35)) * 0.5 + 0.5; vec2 w = warp(p * 0.2, t * 0.4); c1 += w.x * dist * 0.3; c2 += w.y * dist * 0.25; col = softBlend(c1 * u_intensity, c2 * u_intensity, c3 * u_intensity); } else if (u_effect == 16) { vec2 w = warp(vec2(p.x * 0.3, p.y * 0.6), t * 0.5); float c1 = sin(p.x * (1.5 + cpx * 2.0) + w.x * (1.0 + dist * 2.0) + t * 0.15) * 0.5 + 0.5; float c2 = sin(p.x * (1.0 + cpx * 1.5) + w.y * (1.0 + dist * 1.5) - t * 0.12 + 2.0) * 0.5 + 0.5; float c3 = sin(p.x * (0.8 + cpx * 1.0) + (w.x + w.y) * 0.5 * (1.0 + dist) + t * 0.08 + 4.0) * 0.5 + 0.5; float fade = nfbm(vec2(p.x * 0.3, p.y * 0.5) + t * 0.03) * 0.5 + 0.5; col = softBlend(c1 * fade * u_intensity, c2 * fade * u_intensity, c3 * (1.0 - fade * 0.4) * u_intensity * 0.7); } else if (u_effect == 17) { vec2 w = warp(p * 0.8, t * 0.6); vec2 w2 = warp(p * 0.5 + w * 0.4, t * 0.4); float r1 = (snoise((p + w * dist * 0.5) * (1.5 + cpx * 2.0) + t * 0.1) * 0.5 + 0.5) * u_intensity; float r2 = (snoise((p + w2 * dist * 0.4) * (1.2 + cpx * 1.5) + t * 0.08 + 3.0) * 0.5 + 0.5) * u_intensity; float r3 = (snoise((p + (w + w2) * dist * 0.3) * (0.8 + cpx * 1.0) - t * 0.06 + 7.0) * 0.5 + 0.5) * u_intensity; col = softBlend(r1, r2, r3); } else if (u_effect == 18) { vec2 w = warp(p * 0.5, t * 0.5); float blobSize = 0.2 + shp * 0.3; float total1 = 0.0, total2 = 0.0; for (int i = 0; i < 5; i++) { float fi = float(i); vec2 c1 = vec2(sin(t * 0.1 + fi * 2.1) * 0.4, cos(t * 0.13 + fi * 1.7) * 0.35) + w * dist * 0.15; vec2 c2 = vec2(cos(t * 0.12 + fi * 1.9) * 0.35, sin(t * 0.09 + fi * 2.3) * 0.4) + w * dist * 0.12; total1 += blobSize * blobSize / (dot(p - c1, p - c1) + 0.02); total2 += blobSize * blobSize / (dot(p - c2, p - c2) + 0.02); } total1 = clamp(total1 * 0.25, 0.0, 1.0); total2 = clamp(total2 * 0.25, 0.0, 1.0); float total3 = nfbm(p + w * dist * 0.3 + t * 0.05) * 0.5 + 0.5; col = softBlend(total1 * u_intensity, total2 * u_intensity, total3 * 0.7 * u_intensity); } else if (u_effect == 19) { vec2 w = warp(p * 0.4, t * 0.4); float angle = atan(p.y, p.x); float radius = length(p); float s1 = sin(angle * (1.5 + cpx * 2.0) + radius * (3.0 + cpx * 3.0) + t * 0.3 + w.x * dist * 1.5) * 0.5 + 0.5; float s2 = sin(angle * (1.2 + cpx * 1.5) - radius * (2.5 + cpx * 2.5) - t * 0.25 + w.y * dist * 1.5 + 1.5) * 0.5 + 0.5; float s3 = sin((angle + 3.14) * (0.8 + cpx) + radius * (2.0 + cpx * 2.0) + t * 0.15 + (w.x + w.y) * dist) * 0.5 + 0.5; float fade = exp(-radius * (0.5 - shp * 0.3)); col = softBlend(s1 * fade * u_intensity, s2 * fade * u_intensity, s3 * fade * 0.7 * u_intensity); } else if (u_effect == 20) { vec2 w = warp(p * 0.5, t * 0.4); vec2 wp = p + w * (0.4 + dist * 0.6); float scale = 0.6 + cpx * 0.8; float h = nfbm(wp * scale + t * 0.08); float eps = 0.06; float hx = nfbm((wp + vec2(eps, 0.0)) * scale + t * 0.08) - h; float hy = nfbm((wp + vec2(0.0, eps)) * scale + t * 0.08) - h; vec3 n = normalize(vec3(-hx * 6.0, -hy * 6.0, 1.0)); vec3 lightDir = normalize(vec3(0.55, 0.65, 0.8)); float light = max(dot(n, lightDir), 0.0); float spec = pow(light, 6.0 + shp * 26.0); float diffuse = light * 0.6 + 0.35; float fres = pow(1.0 - max(n.z, 0.0), 2.0); float w1 = (diffuse + spec * 0.5) * u_intensity; float w2 = (h * 0.5 + 0.5 + spec * 0.3 + fres * 0.3) * u_intensity; float w3 = (spec * 1.4 + fres * 0.5) * u_intensity; col = softBlend(w1, w2, w3); } else if (u_effect == 21) { vec2 w = warp(p * 0.4, t * 0.3); float angle = 0.2 + shp * 1.3; float ca = cos(angle), sa = sin(angle); vec2 rp = vec2(p.x * ca - p.y * sa, p.x * sa + p.y * ca); float band = sin(rp.y * (3.0 + cpx * 5.0) + w.x * (1.0 + dist * 2.5) + t * 0.35); float ridge = 1.0 - abs(band); ridge = pow(ridge, 5.0 + shp * 10.0); float band2 = sin(rp.y * (2.0 + cpx * 3.0) + w.y * (0.8 + dist * 2.0) - t * 0.22 + 1.4); float ridge2 = 1.0 - abs(band2); ridge2 = pow(ridge2, 3.0 + shp * 8.0); float bg = nfbm(p * 0.45 + t * 0.05) * 0.5 + 0.5; float w1 = (ridge * 1.4 + bg * 0.25) * u_intensity; float w2 = (ridge2 * 1.0 + bg * 0.45) * u_intensity; float w3 = (ridge * 0.5 + ridge2 * 0.5) * u_intensity * 0.8; col = softBlend(w1, w2, w3); } else if (u_effect == 22) { vec2 w = warp(p * 0.7, t * 0.5); vec2 w2 = warp(p * 0.4 + w * 0.3, t * 0.3); vec2 wp = p + w * (0.4 + dist * 0.6); float n1 = snoise(wp * (1.4 + cpx * 1.6) + t * 0.14); float n2 = snoise((wp + w2 * dist * 0.4) * (2.0 + cpx * 2.0) + vec2(3.0, 7.0) - t * 0.1); float ridge1 = 1.0 - abs(n1); ridge1 = pow(ridge1, 5.0 + shp * 12.0); float ridge2 = 1.0 - abs(n2); ridge2 = pow(ridge2, 4.0 + shp * 10.0); float base = (n1 + n2) * 0.25 + 0.5; float w1 = (base * 0.6 + ridge1 * 1.2) * u_intensity; float w2c = ((1.0 - base) * 0.6 + ridge2 * 1.0) * u_intensity; float w3 = (ridge1 * 0.8 + ridge2 * 0.6) * u_intensity; col = softBlend(w1, w2c, w3); } else if (u_effect == 23) { float angle = 0.1 + shp * 1.4; float ca = cos(angle), sa = sin(angle); vec2 rp = vec2(p.x * ca - p.y * sa, p.x * sa + p.y * ca); vec2 stretch = vec2(rp.x * (5.0 + cpx * 4.0), rp.y * (0.35 + cpx * 0.3)); vec2 sw = warp(stretch * 0.3, t * 0.3) * dist; float n1 = snoise(stretch + sw + t * 0.08); float n2 = snoise(stretch * 1.4 + vec2(2.0, 5.0) + sw - t * 0.06); float streak = 1.0 - abs(n1); streak = pow(streak, 6.0 + shp * 12.0); float streak2 = 1.0 - abs(n2); streak2 = pow(streak2, 4.0 + shp * 8.0); float bg = nfbm(p * 0.4 + t * 0.04) * 0.4 + 0.4; float w1 = (streak * 1.4 + bg * 0.3) * u_intensity; float w2 = (streak2 * 0.9 + bg * 0.5) * u_intensity; float w3 = (streak * 0.7 + streak2 * 0.4) * u_intensity * 0.8; col = softBlend(w1, w2, w3); } else if (u_effect == 24) { vec2 w1 = warp(p * 0.55, t * 0.4); vec2 w2 = warp(p * 0.7 + w1 * 0.4, t * 0.3); vec2 wp = p + (w1 + w2) * (0.4 + dist * 0.7); float scale = 0.65 + cpx * 0.7; float n1 = nfbm(wp * scale + vec2(0.0, 0.0) + t * 0.10); float n2 = nfbm(wp * scale + vec2(3.7, 5.2) - t * 0.07); float n3 = nfbm(wp * scale + vec2(7.1, 2.3) + t * 0.06); float n4 = nfbm(wp * scale + vec2(1.8, 8.4) - t * 0.08); float n5 = nfbm(wp * scale + vec2(4.9, 1.1) + t * 0.05); float n6 = nfbm(wp * scale + vec2(6.3, 7.8) - t * 0.09); float n7 = nfbm(wp * scale + vec2(2.4, 4.6) + t * 0.04); float pw = 2.5 + shp * 5.0; n1 = pow(clamp(n1, 0.0, 1.0), pw); n2 = pow(clamp(n2, 0.0, 1.0), pw); n3 = pow(clamp(n3, 0.0, 1.0), pw); n4 = pow(clamp(n4, 0.0, 1.0), pw); n5 = pow(clamp(n5, 0.0, 1.0), pw); n6 = pow(clamp(n6, 0.0, 1.0), pw); n7 = pow(clamp(n7, 0.0, 1.0), pw); float intens = 0.5 + u_intensity * 0.9; float a1 = n1 * u_alpha1 * intens; float a2 = n2 * u_alpha2 * intens; float a3 = n3 * u_alpha3 * intens; float a4 = n4 * u_alpha4 * intens; float a5 = n5 * u_alpha5 * intens; float a6 = n6 * u_alpha6 * intens; float a7 = n7 * u_alpha7 * intens; float total = a1 + a2 + a3 + a4 + a5 + a6 + a7 + 0.001; col = (u_color1 * a1 + u_color2 * a2 + u_color3 * a3 + u_color4 * a4 + u_color5 * a5 + u_color6 * a6 + u_color7 * a7) / total; } else if (u_effect == 25) { float d = (uv.x + (1.0 - uv.y)) * 0.5; float w = 0.9 / max(u_scale, 0.25); float cyc = t * 0.08; float pA = mix(-w, 1.0 + w, sweepEase(fract(cyc))); float pB = mix(-w, 1.0 + w, sweepEase(fract(cyc + 0.5))); float band = max( clamp(1.0 - abs(d - pA) / w, 0.0, 1.0), clamp(1.0 - abs(d - pB) / w, 0.0, 1.0) ); float v = band * u_intensity; vec2 ggs = gridCounts(6.0 + u_cellSize * 74.0); if (u_dotMode > 1.5) { ggs = max(vec2(2.0), floor(ggs * (1.0 - u_gap * 0.8))); } vec2 cell = floor(uv * ggs); float clk = t * 1.6; // Wrap the stepped clock to keep sin() arguments small. u_time grows // unbounded over a session; on mediump-float GPUs (older Android, some // iOS) large hash inputs lose precision and the flicker bands/freezes. // mod(x, 1024) keeps the crossfade continuous across the wrap // (step 1023 fades into step 0, whose hash is the next s0). float step0 = mod(floor(clk), 1024.0); float step1 = mod(step0 + 1.0, 1024.0); float fz = smoothstep(0.0, 1.0, fract(clk)); float cellSeed = dot(cell, vec2(127.1, 311.7)); float r1 = fract(sin(cellSeed + step0 * 17.23) * 43758.5453); float r2 = fract(sin(cellSeed + step1 * 17.23) * 43758.5453); float rnd = mix(r1, r2, fz); v += (rnd - 0.5) * u_flicker * 0.9 * (0.15 + band * 0.85); col = palette(clamp(v, 0.0, 1.0)); } return col; } // ── Mosaic stages ───────────────────────────────────────────────────────── // In the pixel / dot mosaic everything except the per-pixel cell mask is // constant across a cell: the effect colour, its blur taps and the // highlight wave are all sampled at the cell centre. The renderer uses that // for a two-pass path — IMGFX_PASS 1 evaluates the cell field once per cell // into a tiny gridX×gridY target, IMGFX_PASS 2 shades every pixel from it — // which draws the same image as the single pass (IMGFX_PASS 0) with a few // hundred noise evaluations per frame instead of hundreds of thousands. /** Mosaic grid (cells along x / y) of the active mosaic mode. */ vec2 imgfx_mosaicGrid() { vec2 gs = gridCounts(6.0 + u_cellSize * 74.0); if (u_dotMode > 1.5) { gs = max(vec2(2.0), floor(gs * (1.0 - u_gap * 0.8))); } return gs; } /** Effect colour at sampleUV, with the optional 5-tap blur. */ vec3 imgfx_effectColor(vec2 sampleUV, float aspect, float t) { float dist = u_distortion; float soft = u_softness; float cpx = u_complexity; float shp = u_shape; if (u_blur < 0.01) { return computeEffect(sampleUV, aspect, t, dist, soft, cpx, shp); } float r = u_blur * 0.02; vec3 col = computeEffect(sampleUV, aspect, t, dist, soft, cpx, shp) * 0.4; col += computeEffect(sampleUV + vec2( r, 0.0), aspect, t, dist, soft, cpx, shp) * 0.15; col += computeEffect(sampleUV + vec2(-r, 0.0), aspect, t, dist, soft, cpx, shp) * 0.15; col += computeEffect(sampleUV + vec2( 0.0, r), aspect, t, dist, soft, cpx, shp) * 0.15; col += computeEffect(sampleUV + vec2( 0.0, -r), aspect, t, dist, soft, cpx, shp) * 0.15; return col; } /** Highlight wave weight of the cell centred at cellCenter. */ float imgfx_cellHighlight(vec2 cellCenter, float aspect, float t) { if (u_highlight <= 0.01 && u_hlScale <= 0.01) return 0.0; vec2 cp2 = (cellCenter - 0.5) * u_scale; cp2.x *= aspect; float lw = sin(cp2.x * 3.0 + t * 1.5) * 0.5 + 0.5; lw *= sin(cp2.y * 2.5 - t * 1.1) * 0.5 + 0.5; lw += (snoise(cp2 * 2.0 + t * 0.6) * 0.5 + 0.5) * 0.3; float hlFactor = clamp(lw, 0.0, 1.0); return hlFactor * hlFactor; } /** Per-pixel stage: vignette, colour alpha and — in mosaic modes — the * cell mask, highlight and edge fade. col is the effect colour of the * pixel's cell (or of the pixel itself in raw mode). */ vec4 imgfx_shade(vec2 uv, vec3 col, float hlFactor, vec2 gridSize) { vec3 baseCol = col; if (u_dotMode < 0.5) { col = pow(col, vec3(1.3)); } // CSS-pixel distance to the nearest edge — keeps the vignette / edge-fade // bands a consistent physical width on every side of any aspect ratio. vec2 cssRes = u_resolution / max(u_dpr, 0.0001); vec2 cssCoord = uv * cssRes; float edgeDistPx = min( min(cssCoord.x, cssRes.x - cssCoord.x), min(cssCoord.y, cssRes.y - cssCoord.y) ); float vigRangePx = 40.0 * (1.0 + u_vignette * 3.0); float vig = (edgeDistPx * edgeDistPx) / (vigRangePx * vigRangePx); vig = smoothstep(0.0, 1.0, vig); col *= mix(1.0, vig, u_vignette * u_vigOpacity); float colorAlpha = (u_alpha1 + u_alpha2 + u_alpha3 + u_alpha4 + u_alpha5) / 5.0; if (colorAlpha < 0.999) { vec3 c1d = col - u_color1, c2d = col - u_color2, c3d = col - u_color3, c4d = col - u_color4, c5d = col - u_color5; float prox1 = exp(-8.0 * dot(c1d, c1d)); float prox2 = exp(-8.0 * dot(c2d, c2d)); float prox3 = exp(-8.0 * dot(c3d, c3d)); float prox4 = exp(-8.0 * dot(c4d, c4d)); float prox5 = exp(-8.0 * dot(c5d, c5d)); float pTotal = prox1 + prox2 + prox3 + prox4 + prox5 + 0.0001; colorAlpha = (prox1*u_alpha1 + prox2*u_alpha2 + prox3*u_alpha3 + prox4*u_alpha4 + prox5*u_alpha5) / pTotal; } float alpha = colorAlpha; if (u_dotMode > 0.5) { // cellLocal is in [0,1] within each cell. Because gridSize was chosen so // that cell PIXEL size is square, distance / mask math here works in // screen-square units even though we're operating in normalised cell uv. vec2 cellLocal = fract(uv * gridSize); float scaleBoost = 1.0 + smoothstep(0.2, 0.8, hlFactor) * u_hlScale * 1.2; float mask = 1.0; if (u_dotMode < 1.5) { float gapW = u_gap * 0.35 / scaleBoost; if (gapW > 0.003) { mask = step(gapW, cellLocal.x) * step(gapW, 1.0 - cellLocal.x) * step(gapW, cellLocal.y) * step(gapW, 1.0 - cellLocal.y); } } else { // Render the circular dot mask in screen-pixel space rather than // cell-local UV. We map the cell-local offset to actual pixels // (cellPx = u_resolution / gridSize), then apply a 1-pixel AA // floor to the smoothstep edge so the dot rim is crisp and // properly anti-aliased even at u_dotSoftness near 0. The user // softness slider still scales linearly on top of the floor. // No fwidth() / GL_OES_standard_derivatives needed - dPx is // already in pixel units, so a fixed 1-px edge IS pixel-perfect. // gridCounts() already keeps cells square in screen space, so // pxOffset traces true circles (not ellipses) on any aspect. vec2 cellPx = u_resolution / gridSize; vec2 pxOffset = (cellLocal - 0.5) * cellPx; float dPx = length(pxOffset); float minCellPx = min(cellPx.x, cellPx.y); float radiusPx = u_dotSize * 0.5 * minCellPx * scaleBoost; // 0.5-px AA floor (1-px total smoothstep ramp) keeps the rim // pixel-perfect at u_dotSoftness=0 while letting the user softness // value dominate at the bundled preset defaults (e.g. softness=0.1 // on a ~28-px cell yields softPx=0.56 -> ~1.1-px ramp, matching // the original cell-local behaviour). A larger floor (e.g. 1.0) // would widen low-softness dots and visually lighten dot presets. float aaPx = 0.5; float softPx = u_dotSoftness * 0.2 * minCellPx; float edgePx = max(aaPx, softPx); mask = 1.0 - smoothstep(radiusPx - edgePx, radiusPx + edgePx, dPx); } if (u_highlight > 0.01) { float hl = hlFactor * u_highlight; col = col * (1.0 + hl * 2.5) + vec3(hl * hl * 0.3); } if (u_edgeFade > 0.5 && u_fadeStr > 0.005) { float ef = smoothstep(0.0, u_edgeFade, edgeDistPx); mask *= mix(1.0, ef, u_fadeStr); } float baseOpacity = (u_dotMode < 1.5) ? u_fillOpacity : 0.0; alpha = colorAlpha * mix(baseOpacity, u_dotOpacity, mask); float bgLum = dot(u_cardBg, vec3(0.299, 0.587, 0.114)); float colLum = dot(baseCol, vec3(0.299, 0.587, 0.114)); alpha *= smoothstep(0.0, 0.33, abs(colLum - bgLum)); } return vec4(col, alpha * u_shaderOpacity); } void main() { float aspect = u_resolution.x / u_resolution.y; float t = u_time * u_speed; #if IMGFX_PASS == 1 // Cell field: one fragment per cell (viewport = u_grid), so the fragment // coordinate is the cell centre in grid units. vec2 cellCenter = gl_FragCoord.xy / u_grid; gl_FragColor = vec4(imgfx_effectColor(cellCenter, aspect, t), imgfx_cellHighlight(cellCenter, aspect, t)); #elif IMGFX_PASS == 2 // Composite: every pixel reads its cell's field texel. vec2 uv = gl_FragCoord.xy / u_resolution; vec4 field = texture2D(u_field, (floor(uv * u_grid) + 0.5) / u_fieldSize); gl_FragColor = imgfx_shade(uv, field.rgb, field.a, u_grid); #else vec2 uv = gl_FragCoord.xy / u_resolution; vec2 gridSize = imgfx_mosaicGrid(); vec2 sampleUV = uv; float hlFactor = 0.0; if (u_dotMode > 0.5) { sampleUV = (floor(uv * gridSize) + vec2(0.5)) / gridSize; hlFactor = imgfx_cellHighlight(sampleUV, aspect, t); } gl_FragColor = imgfx_shade(uv, imgfx_effectColor(sampleUV, aspect, t), hlFactor, gridSize); #endif } `; /** Names of every uniform sampled by the fragment shader. */ export const UNIFORM_NAMES: readonly string[] = [ 'u_resolution', 'u_dpr', 'u_time', 'u_color1', 'u_color2', 'u_color3', 'u_color4', 'u_color5', 'u_color6', 'u_color7', 'u_cardBg', 'u_alpha1', 'u_alpha2', 'u_alpha3', 'u_alpha4', 'u_alpha5', 'u_alpha6', 'u_alpha7', 'u_speed', 'u_intensity', 'u_scale', 'u_direction', 'u_softness', 'u_distortion', 'u_complexity', 'u_shape', 'u_flicker', 'u_vignette', 'u_vigOpacity', 'u_blur', 'u_highlight', 'u_shaderOpacity', 'u_cellSize', 'u_gap', 'u_dotSize', 'u_dotSoftness', 'u_dotOpacity', 'u_hlScale', 'u_fillOpacity', 'u_edgeFade', 'u_fadeStr', 'u_dotMode', 'u_effect', 'u_sweepEase', 'u_grid', 'u_fieldSize', 'u_field' ]; const EFFECT_BRANCH = /\bif\s*\(\s*u_effect\s*==\s*(\d+)\s*\)\s*\{/; const EFFECT_ELSE_BRANCH = /^\s*else\s+if\s*\(\s*u_effect\s*==\s*(\d+)\s*\)\s*\{/; /** Index of the `}` closing the `{` at `open`, or -1. */ function matchingBrace(src: string, open: number): number { let depth = 0; for (let i = open; i < src.length; i++) { const ch = src[i]; if (ch === '{') depth++; else if (ch === '}' && --depth === 0) return i; } return -1; } /** * Specialise a fragment source to a single effect. `u_effect` becomes a * compile-time constant and the `if (u_effect == N) {…} else if …` chain is * cut down to the one branch that runs, so the driver compiles one effect * instead of all of them — the full chain is a large program once the noise * loops are unrolled, and compiling it can stall the page for a long time on * Windows (ANGLE → D3D), where the first card mounts. * * Works on any source that declares `uniform int u_effect;` (the bundled * shader and custom ones built from its prelude); anything else is returned * unchanged. */ export function specializeEffect(src: string, effect: number): string { const decl = /uniform\s+int\s+u_effect\s*;/; if (!decl.test(src)) return src; let out = src.replace(decl, `const int u_effect = ${effect | 0};`); const head = EFFECT_BRANCH.exec(out); if (!head) return out; const start = head.index; let open = start + head[0].length - 1; let body = '{}'; let end = -1; let n = Number(head[1]); for (;;) { const close = matchingBrace(out, open); if (close < 0) return out; if (n === effect) body = out.slice(open, close + 1); end = close + 1; const next = EFFECT_ELSE_BRANCH.exec(out.slice(end)); if (!next) break; n = Number(next[1]); open = end + next[0].length - 1; } // A trailing plain `else` can't be cut cleanly — the constant alone still // lets the compiler fold the chain. if (/^\s*else\b/.test(out.slice(end))) return out; return out.slice(0, start) + body + out.slice(end); }