info = { type = "image", title = "Soap bubbles", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Iridescent soap bubbles drift and pop on dark", api = 33, } local tr = ({ sk = { ["Bubbles"] = "Bubliny", ["Background"] = "Pozadie", ["Black"] = "Čierne", ["Midnight"] = "Polnočná modrá", ["Teal"] = "Tyrkysové" }, cs = { ["Bubbles"] = "Bubliny", ["Background"] = "Pozadí", ["Black"] = "Černé", ["Midnight"] = "Půlnoční modrá", ["Teal"] = "Tyrkysové" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Bubbles"), type = "int", min = 3, max = 12, default = 8 }, { key = "bg", label = T("Background"), type = "int", default = "midnight", opts = { keys = { "black", "midnight", "teal" }, T("Black"), T("Midnight"), T("Teal") } }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, atan, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.atan, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local KR, KG, KB = 255, 0, 255 local function ramp(s) if s < 0.5 then local t = s * 2 return 70 + 185 * t, 200 - 80 * t, 255 - 45 * t end local t = s * 2 - 1 return 255, 120 + 94 * t, 210 - 110 * t end local CLS_R = { 4, 6, 8, 11, 15 } local CLS_K = { 0.5, 0.65, 0.8, 1.0, 1.0 } local PH = { { 0.25, 0.25 }, { 0.75, 0.25 }, { 0.25, 0.75 }, { 0.75, 0.75 } } local SP, PARTS = { {}, {}, {}, {}, {} }, {} local NBAND = { 1, 1, 2, 3, 4 } local JOBS = {} for pass = 1, 4 do for c = 1, 5 do for bd = 1, NBAND[c] do JOBS[#JOBS + 1] = { c, pass, bd } end end end local NEXT = 1 local function clamp255(v) return v > 255 and 255 or v end local function bake(c, p, band) local R, k = CLS_R[c], CLS_K[c] local S = 2 * R + 4 local nb = NBAND[c] local j0, j1 = (band - 1) * S // nb, band * S // nb - 1 local cx, cy = R + 2 + PH[p][1], R + 2 + PH[p][2] rect(0, j0, S, j1 - j0 + 1, KR, KG, KB) local Rr, hw = R - 0.6, 0.75 + R * 0.03 local GA = -2.356 local core = 0.45 * R for j = j0, j1 do for i = 0, S - 1 do local dx, dy = i + 0.5 - cx, j + 0.5 - cy local d = sqrt(dx * dx + dy * dy) local r, g, b = 0, 0, 0 local ring = 1 - abs(d - Rr) / hw if d < Rr + hw and (d > core or R < 8) then local ang = atan(dy, dx) if ring > 0 then local l1 = 0.5 + 0.5 * cos(ang - GA) local l2 = 0.5 + 0.5 * cos(ang - 0.79) local br = 0.32 + 0.68 * max(l1 * l1, 0.55 * l2 * l2) local cr, cg, cb = ramp(0.5 + 0.5 * sin(1.5 * ang + 0.4)) local w = ring * br * k r, g, b = cr * w, cg * w, cb * w end if R >= 9 and d < Rr - hw then local t = (Rr - hw - d) / R if t < 0.38 and dy > -0.1 * R then local low = min(1, (dy / R + 0.1) / 0.7) local a = 0.3 * (1 - t / 0.38) * low * k local cr, cg, cb = ramp(0.5 + 0.5 * sin(ang * 1.2 + t * 12)) r, g, b = r + cr * a, g + cg * a, b + cb * a end end local da = ang - GA if da > 3.1416 then da = da - 6.2832 elseif da < -3.1416 then da = da + 6.2832 end local wd = 1 - abs(d - 0.64 * R) / (0.1 * R + 0.55) local wa = 1 - abs(da) / (0.2 + 1.6 / R) if wd > 0 and wa > 0 then local w = wd * wa * k r, g, b = r + 236 * w, g + 246 * w, b + 255 * w end end local ex, ey = i + 0.5 - (cx + 0.58 * R), j + 0.5 - (cy + 0.58 * R) if ex * ex + ey * ey < (0.09 * R + 0.4) ^ 2 then r, g, b = r + 160 * k, g + 120 * k, b + 170 * k end local lim = ring > 0 and 7 or 15 if r > lim or g > lim or b > lim then px(i, j, floor(clamp255(r)), floor(clamp255(g)), floor(clamp255(b))) end end end local parts = PARTS[c * 8 + p] or {} PARTS[c * 8 + p] = parts parts[band] = snapshot(0, j0, S, j1 - j0 + 1) if band == nb then SP[c][p] = parts; PARTS[c * 8 + p] = nil end end local BGS = { black = { base = { 2, 2, 4 }, a = { 24, 28, 44 }, b = { 40, 34, 28 } }, midnight = { base = { 3, 6, 18 }, a = { 18, 36, 84 }, b = { 52, 34, 82 } }, teal = { base = { 2, 10, 14 }, a = { 12, 66, 78 }, b = { 58, 64, 34 } }, } local BG local PBG = BGS.midnight local function bake_bg() local b = PBG.base rect(0, 0, W, H, b[1], b[2], b[3]) for i = 1, 11 do local h = hash32(i * 0x9E3779B1) local R = 3 + umod(h, 6) local cx, cy = umod(h >> 8, W + 8) - 4, umod(h >> 14, H + 8) - 4 local c = (i % 3 == 0) and PBG.b or PBG.a local f = 0.45 + umod(h >> 20, 40) / 100 local fr, fg, fb = floor(b[1] + (c[1] - b[1]) * f), floor(b[2] + (c[2] - b[2]) * f), floor(b[3] + (c[3] - b[3]) * f) local er, eg, eb = floor(b[1] + (c[1] - b[1]) * min(1, f + 0.3)), floor(b[2] + (c[2] - b[2]) * min(1, f + 0.3)), floor(b[3] + (c[3] - b[3]) * min(1, f + 0.3)) for dy = -R, R do local hw = floor(sqrt(R * R + 0.5 - dy * dy) + 0.5) rect(cx - hw, cy + dy, 2 * hw + 1, 1, fr, fg, fb) if dy == -R or dy == R then rect(cx - hw, cy + dy, 2 * hw + 1, 1, er, eg, eb) else px(cx - hw, cy + dy, er, eg, eb); px(cx + hw, cy + dy, er, eg, eb) end end end BG = snapshot(0, 0, W, H) end function on_settings() PBG = BGS[cfg.bg] or BGS.midnight BG = nil end local NB = 12 local B = {} for i = 1, NB do local h = hash32(i * 0x85EBCA6B) local cls = ({ 4, 2, 5, 3, 1, 4, 3, 2, 5, 3, 1, 4 })[i] B[i] = { cls = cls, life = (H + 2 * CLS_R[cls] + 8) / (2.6 + 0.9 * cls + umod(h, 10) / 10), off = umod(h >> 8, 100) / 100, f = 0.7 + umod(h >> 14, 60) / 100, ph = umod(h >> 20, 628) / 100, wf = 0.7 + umod(h >> 4, 8) / 10, } end local function wind(t) return 1.8 * t + 4 * sin(0.27 * t) end local function where(i, t, k, h, u) local b = B[i] local R = CLS_R[b.cls] local t0 = (k - b.off) * b.life local tt = t0 + u * b.life local y = H + R + 3 - u * (H + 2 * R + 6) local x = -6 + umod(h, 52) + (wind(tt) - wind(t0)) * b.f + 1.3 * sin(tt * b.wf + b.ph) return x, y end local POP_PCT = 45 local function burst(x, y, age, h, R) if age < 0.14 then local a = 0.8 * (1 - age / 0.14) for s = 0, 9 do local an = s * 0.6283 + umod(h, 60) / 100 local cr, cg, cb = ramp((s % 3) / 2) local r1, r2 = R * (1.0 + age * 3), R * (1.2 + age * 5) for q = 0, 1 do local rr = r1 + (r2 - r1) * q blend(floor(x + cos(an) * rr), floor(y + sin(an) * rr), cr, cg, cb, a) end end end for d = 1, 9 do local hh = hash32(h + d * 0xC2B2AE35) local an = umod(hh, 628) / 100 local sp = 6 + umod(hh >> 10, 10) + R * 0.4 local px_, py_ = x + cos(an) * sp * age, y + sin(an) * sp * age * 0.8 - 3 * age + 19 * age * age local cr, cg, cb = ramp(umod(hh >> 14, 100) / 100) blend(floor(px_), floor(py_), cr, cg, cb, 1 - age / 0.7) end end function render() if not BG then bake_bg() blit_rgb(BG, 0, 0, W, H) return end local j = JOBS[NEXT] if j then bake(j[1], j[2], j[3]) NEXT = NEXT + 1 end blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local n = cfg.n or 8 for i = 1, n do local b = B[i] local R = CLS_R[b.cls] local s = t / b.life + b.off local k = floor(s) local u = s - k local h = hash32(i * 0x9E3779B1 + k * 0x85EBCA6B) local pops = umod(h >> 8, 100) < POP_PCT local upop = 0.3 + umod(h >> 12, 45) / 100 if pops and u > upop then local age = (u - upop) * b.life if age < 0.7 then local x, y = where(i, t, k, h, upop) burst(x, y, age, h, R) end else local x, y = where(i, t, k, h, u) local ix, iy = floor(x), floor(y) local ph = (x - ix >= 0.5 and 1 or 0) + 2 * (y - iy >= 0.5 and 1 or 0) + 1 local sp = SP[b.cls][ph] or SP[b.cls][1] if sp then local S, nb = 2 * R + 4, NBAND[b.cls] for bd = 1, nb do local j0, j1 = (bd - 1) * S // nb, bd * S // nb blit_rgb(sp[bd], ix - R - 2, iy - R - 2 + j0, S, j1 - j0, KR, KG, KB) end end end end end