info = { type = "image", title = "Bokeh", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "Soft blurred lights drifting and glowing", api = 33, } local STEP_HZ = 30 local FR = 0 local ltr = ({ sk = { ["Density"] = "Hustota", ["Palette"] = "Paleta", ["Dream"] = "Sen", ["Twilight"] = "Súmrak", ["Forest"] = "Les", ["Speed"] = "Rýchlosť" }, cs = { ["Density"] = "Hustota", ["Palette"] = "Paleta", ["Dream"] = "Sen", ["Twilight"] = "Soumrak", ["Forest"] = "Les", ["Speed"] = "Rychlost" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "dens", label = LT("Density"), type = "int", min = 1, max = 5, default = 3 }, { key = "pal", label = LT("Palette"), type = "int", default = "dream", opts = { keys = { "dream", "twilight", "forest" }, LT("Dream"), LT("Twilight"), LT("Forest") } }, { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local floor, sin, sqrt = math.floor, math.sin, math.sqrt local blend_mask = gfx.blend_mask local hash32, umod = math.hash32, math.umod local TAU = 6.2831853 local LOOP = 3600 local W = 64 local PALS = { dream = { bg = { 40, 18, 74 }, hues = { 318, 272, 188, 222, 340 }, s = 0.62 }, twilight = { bg = { 34, 20, 70 }, hues = { 125, 70, 330, 285, 175 }, s = 0.68 }, forest = { bg = { 14, 44, 40 }, hues = { 95, 55, 150, 175, 40 }, s = 0.7 }, } local CLASSES = { { 33, 0.26, 2, 0.6, 0.0 }, { 25, 0.34, 3, 0.8, 0.1 }, { 17, 0.46, 4, 1.0, 0.2 }, { 11, 0.62, 4, 1.2, 0.3 }, { 7, 0.78, 4, 1.4, 0.45 }, { 3, 0.95, 5, 1.6, 0.9 }, } local function hsv(h, s, v) h = (h % 360) / 60 local i = floor(h) local f = h - i local p, q, t = v * (1 - s), v * (1 - s * f), v * (1 - s * (1 - f)) local r, g, b if i == 0 then r, g, b = v, t, p elseif i == 1 then r, g, b = q, v, p elseif i == 2 then r, g, b = p, v, t elseif i == 3 then r, g, b = p, q, v elseif i == 4 then r, g, b = t, p, v else r, g, b = v, p, q end return floor(r * 255 + 0.5), floor(g * 255 + 0.5), floor(b * 255 + 0.5) end local MASKS = {} local function getmask(S) local m = MASKS[S] if m then return m end local hh = (S - 1) // 2 local n = hh + 1 local R = S / 2 - 0.25 local T = {} for a = 0, hh do for b = 0, a do local d = sqrt(a * a + b * b) local c = R - d + 0.5 if c > 1 then c = 1 elseif c < 0 then c = 0 end local rim = S >= 11 and (d - (R - 2.2)) / 1.6 or 0 if rim < 0 then rim = 0 elseif rim > 1 then rim = 1 end local v = floor(c * (0.6 + 0.4 * rim) * 255 + 0.5) T[a * n + b] = v T[b * n + a] = v end end local rows, row = {}, {} for dy = -hh, hh do local a = dy < 0 and -dy or dy for dx = -hh, hh do row[dx + hh + 1] = T[a * n + (dx < 0 and -dx or dx)] end rows[dy + hh + 1] = string.char(table.unpack(row, 1, S)) end m = table.concat(rows) MASKS[S] = m return m end local TW, TX, TY = { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } local function light(m, S, cx, cy, r, g, b, a) local o = (S - 1) * 0.5 local x, y = cx - o, cy - o if S >= 25 then blend_mask(m, r, g, b, floor(x + 0.5), floor(y + 0.5), S, S, S, S, a) return end local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy TW[1], TX[1], TY[1] = (1 - fx) * (1 - fy), ix, iy TW[2], TX[2], TY[2] = fx * (1 - fy), ix + 1, iy TW[3], TX[3], TY[3] = (1 - fx) * fy, ix, iy + 1 TW[4], TX[4], TY[4] = fx * fy, ix + 1, iy + 1 for i = 2, 4 do local w, xx, yy = TW[i], TX[i], TY[i] local j = i - 1 while j >= 1 and TW[j] < w do TW[j + 1], TX[j + 1], TY[j + 1] = TW[j], TX[j], TY[j] j = j - 1 end TW[j + 1], TX[j + 1], TY[j + 1] = w, xx, yy end local tot = 0 for i = 1, 4 do if TW[i] > 0.04 then tot = tot + TW[i] end end local acc = 0 for i = 1, 4 do local w = TW[i] if w > 0.04 then w = w / tot local al = a * w / (1 - a * acc) if al > 1 then al = 1 end blend_mask(m, r, g, b, TX[i], TY[i], S, S, S, S, al) acc = acc + w end end end local BG, SPD = PALS.dream.bg, 3 local NL = 0 local LS, LX, LY, LVX, LVY, LWX, LWY, LHX, LHY, LA, LBP, LBH, LH, LR, LG, LB, LSP = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} local READY = false local function h3(a, b, c) return hash32(a * 7919 + b * 104729 + c * 15485863) & 0x7FFF end function on_settings() local P = PALS[cfg.pal or "dream"] or PALS.dream BG = P.bg SPD = cfg.speed or 3 local dens = cfg.dens or 3 NL = 0 for ci = 1, #CLASSES do local c = CLASSES[ci] local cnt = floor(c[3] * dens / 3 + 0.5) if cnt < 1 then cnt = 1 end for k = 1, cnt do NL = NL + 1 local seed = ci * 131 + k local S = c[1] local rng = W + S LS[NL] = S LSP[NL] = ci LX[NL] = h3(seed, 1, 1) / 0x7FFF * rng LY[NL] = h3(seed, 2, 1) / 0x7FFF * rng LVX[NL] = (h3(seed, 3, 1) % 2 == 0 and 1 or -1) * (S <= 11 and 1 + h3(seed, 13, 1) % 2 or 1) LVY[NL] = h3(seed, 4, 1) % 3 - 1 LWX[NL] = 1.5 + h3(seed, 5, 1) % 30 / 10 LWY[NL] = 1.5 + h3(seed, 6, 1) % 30 / 10 LHX[NL] = 1 + h3(seed, 7, 1) % 3 LHY[NL] = 2 + h3(seed, 8, 1) % 3 LA[NL] = c[2] LBP[NL] = h3(seed, 9, 1) / 0x8000 LBH[NL] = 4 + h3(seed, 10, 1) % 6 local hue = P.hues[h3(seed, 11, 1) % #P.hues + 1] + (h3(seed, 12, 1) % 21 - 10) LH[NL] = hue local v = ci <= 2 and 0.85 or 1.0 LR[NL], LG[NL], LB[NL] = hsv(hue, P.s, v) end end READY = true end function render() FR = frame * STEP_HZ // gfx.FPS if not READY then on_settings() end local u = ((FR * SPD) % LOOP) / LOOP gfx.clear(BG[1], BG[2], BG[3]) for i = 1, NL do local S = LS[i] local rng = W + S local x = (LX[i] + LVX[i] * u * rng) % rng - S * 0.5 + LWX[i] * sin(TAU * (LHX[i] * u + LBP[i])) local y = (LY[i] + LVY[i] * u * rng) % rng - S * 0.5 + LWY[i] * sin(TAU * (LHY[i] * u + LBP[i] * 2)) local twink = (LSP[i] == 6) and 0.5 or 0.2 local a = LA[i] * (1 - twink + twink * sin(TAU * (LBH[i] * u + LBP[i]))) if a > 0.02 then light(getmask(S), S, x, y, LR[i], LG[i], LB[i], a) end end end