info = { type = "image", title = "Buttercup macro", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Macro buttercup, drifting bokeh and pollen", api = 44, } local ltr = ({ sk = { ["Bokeh"] = "Bokeh", ["Pollen"] = "Peľ", ["Light"] = "Svetlo", ["Lime"] = "Limetkové", ["Gold"] = "Zlaté", ["White"] = "Biele" }, cs = { ["Bokeh"] = "Bokeh", ["Pollen"] = "Pyl", ["Light"] = "Světlo", ["Lime"] = "Limetkové", ["Gold"] = "Zlaté", ["White"] = "Bílé" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "bokeh", label = LT("Bokeh"), type = "int", min = 2, max = 10, default = 7 }, { key = "pollen", label = LT("Pollen"), type = "int", min = 0, max = 20, default = 10 }, { key = "tint", label = LT("Light"), type = "int", default = "lime", opts = { keys = { "lime", "gold", "white" }, LT("Lime"), LT("Gold"), LT("White") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, atan, exp, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.atan, math.exp, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TAU = 6.2831853 local function rnd(v) return floor(v + 0.5) end local BGS = { { 0, 186, 216, 108 }, { 22, 140, 190, 84 }, { 44, 88, 150, 60 }, { 63, 52, 112, 48 } } local function bgc(y) for i = 2, 4 do local a, b = BGS[i - 1], BGS[i] if y <= b[1] then local f = (y - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * f, a[3] + (b[3] - a[3]) * f, a[4] + (b[4] - a[4]) * f end end return 52, 112, 48 end local BG, FL = nil, nil local function bake_bg() for y = 0, H - 1 do local r, g, b = bgc(y) rect(0, y, W, 1, rnd(r), rnd(g), rnd(b)) end BG = snapshot(0, 0, W, H) end local RW, RH = 50, 36 local NP, R, CX, CY, RC = 7, 29, 18.0, 4.0, 7 local STOPS = { { 0, 168, 96, 6 }, { 0.35, 232, 160, 14 }, { 0.6, 255, 206, 34 }, { 0.85, 255, 232, 84 }, { 1, 255, 246, 160 } } local function ramp(s) if s <= 0 then return 168, 96, 6 end if s >= 1 then return 255, 246, 160 end for i = 2, 5 do local a, b = STOPS[i - 1], STOPS[i] if s <= b[1] then local f = (s - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * f, a[3] + (b[3] - a[3]) * f, a[4] + (b[4] - a[4]) * f end end end local function flower_px(x, y) local dx, dy = x + 0.5 - CX, y + 0.5 - CY local r = sqrt(dx * dx + dy * dy) local u = (atan(dy, dx) - 1.5708) / TAU * NP + 0.5 local k = floor(u) local fr = u - k local v = abs(fr - 0.5) * 2 local re = R * (0.8 + 0.2 * sqrt(1 - v * v)) local cov = re - r + 0.5 if cov <= 0.05 then return end local ak = 1.5708 + k * TAU / NP local lit = cos(ak + 0.7) local s = 0.62 - 0.30 * v * v - 0.38 * max(0, 1 - (r - 6.5) / 10) + 0.20 * lit + 0.04 * cos(fr * TAU * 9) if r > 11.5 then local g = max(0, (v - 0.84) / 0.16) s = s - 0.5 * g * sqrt(g) local w = sin(max(0, min(1, (r - 11.5) / (re - 11.5))) * 3.1416) local d = fr - 0.62 s = s + 0.38 * exp(-d * d / 0.008) * w * w * (0.55 + 0.45 * lit) end if re - r < 1.6 then s = s + 0.1 end local cr, cg, cb = ramp(s) if r < RC + 0.5 then local a = r / RC local q = a * sqrt(a) local dr, dg, db = 92 + 120 * q, 44 + 78 * q, 4 + 12 * q if r > 2.2 and r < RC - 0.5 and hash32(x * 131 + y * 7919) & 7 == 0 then dr, dg, db = 255, 226, 96 end local t = max(0, min(1, RC + 0.5 - r)) cr, cg, cb = cr + (dr - cr) * t, cg + (dg - cg) * t, cb + (db - cb) * t end if cov < 1 then local br, bg, bb = bgc(y) cr, cg, cb = br + (cr - br) * cov, bg + (cg - bg) * cov, bb + (cb - bb) * cov end px(x, y, rnd(cr), rnd(cg), rnd(cb)) end local DX, DY, DR = 28, 24, 3.2 local function bake_dew() for dy = -4, 4 do for dx = -4, 4 do local d = sqrt(dx * dx + dy * dy) if d <= DR then local t = (dy + DR) / (2 * DR) local r, g, b = 205 + 50 * t, 120 + 118 * t, 12 + 130 * t if d > DR - 1 then if dx + dy < 0 then r, g, b = r * 0.78, g * 0.7, b * 0.5 else b = b + 30 end end px(DX + dx, DY + dy, rnd(min(255, r)), rnd(min(255, g)), rnd(min(255, b))) elseif d <= DR + 1.2 and dx + dy > 0 then px(DX + dx, DY + dy, 176, 102, 10) end end end px(DX - 1, DY - 1, 255, 255, 240) end local function glint(t) local a = (t + 100) % 150 if a >= 22 then return end local i = sin(a / 22 * 3.1416) local x, y = DX - 1, DY - 1 blend(x, y, 255, 255, 240, i) if i > 0.45 then local s = (i - 0.45) * 1.4 blend(x - 1, y, 255, 255, 230, s); blend(x + 1, y, 255, 255, 230, s) blend(x, y - 1, 255, 255, 230, s); blend(x, y + 1, 255, 255, 230, s) blend(x - 2, y, 255, 255, 230, s * 0.5); blend(x + 2, y, 255, 255, 230, s * 0.5) blend(x, y - 2, 255, 255, 230, s * 0.5); blend(x, y + 2, 255, 255, 230, s * 0.5) end end local TINTS = { lime = { 232, 248, 140 }, gold = { 255, 222, 110 }, white = { 255, 255, 236 } } local NB = 10 local BK = {} for i = 1, NB do local h = hash32(i * 0x9E3779B1) local big = i % 3 == 0 BK[i] = { r = big and (7 + umod(h, 3)) or (4 + umod(h, 3)), x0 = umod(h >> 4, W + 16), y0 = 4 + umod(h >> 10, H - 8), vx = (0.03 + umod(h >> 16, 8) / 120) * ((h >> 3) & 1 == 1 and 1 or -1), ph = umod(h >> 20, 628) / 100, al = big and 0.16 or 0.24, rim = big and 0.06 or 0.14, } end local function disc(cx, cy, r, al, rim, cr, cg, cb) for y = floor(cy - r), floor(cy + r) do local d = y + 0.5 - cy local q = r * r - d * d if q > 0 then local hw = sqrt(q) local xl, xr = cx - hw, cx + hw local il, ir = floor(xl), floor(xr) local ai = (r - abs(d) < 1) and (al + rim * 0.7) or al if ir <= il then blend(il, y, cr, cg, cb, (xr - xl) * (al + rim)) else blend(il, y, cr, cg, cb, (il + 1 - xl) * (al + rim)) if ir - il > 1 then blend_rect(il + 1, y, ir - il - 1, 1, cr, cg, cb, ai) end blend(ir, y, cr, cg, cb, (xr - ir) * (al + rim)) end end end end local function bokeh(t) local c = TINTS[cfg.tint] or TINTS.lime for i = 1, min(cfg.bokeh or 7, NB) do local b = BK[i] local span = W + 2 * (b.r + 1) local x = (b.x0 + b.vx * t) % span - (b.r + 1) local y = b.y0 + 1.5 * sin(t * 0.012 + b.ph) local pulse = 0.85 + 0.15 * sin(t * 0.02 + b.ph * 2) disc(x, y, b.r, b.al * pulse, b.rim * pulse, c[1], c[2], c[3]) end end local NPOL = 20 local PO = {} for i = 1, NPOL do local h = hash32(i * 0x85EBCA6B) PO[i] = { x0 = umod(h, W), y0 = umod(h >> 8, H), vx = 0.03 + umod(h >> 16, 8) / 200, vy = -(0.02 + umod(h >> 20, 6) / 300), ph = umod(h >> 24, 628) / 100, } end local function pollen(t) for i = 1, min(cfg.pollen or 10, NPOL) do local p = PO[i] local x = (p.x0 + p.vx * t + 2.5 * sin(t * 0.03 + p.ph)) % (W + 4) - 2 local y = rnd((p.y0 + p.vy * t + 1.5 * cos(t * 0.045 + p.ph)) % (H + 4) - 2) local ix = floor(x) local f = x - ix local a = 0.55 + 0.35 * sin(t * 0.07 + p.ph * 3) blend(ix, y, 255, 244, 176, a * (1 - f)) blend(ix + 1, y, 255, 244, 176, a * f) end end local SLICE = 3 local NSL = (RH + SLICE - 1) // SLICE local STAGE = 0 function render() if STAGE == 0 then bake_bg() else gfx.clear(255, 0, 255) if FL then blit_rgb(FL, 0, 0, RW, RH, 255, 0, 255) end if STAGE <= NSL then for y = (STAGE - 1) * SLICE, min(STAGE * SLICE, RH) - 1 do for x = 0, RW - 1 do flower_px(x, y) end end FL = snapshot(0, 0, RW, RH) elseif STAGE == NSL + 1 then bake_dew() FL = snapshot(0, 0, RW, RH) end end if STAGE <= NSL + 1 then STAGE = STAGE + 1 if STAGE <= NSL + 1 then gfx.ready(false) end end blit_rgb(BG, 0, 0, W, H) bokeh(frame) if FL then blit_rgb(FL, 0, 0, RW, RH, 255, 0, 255) end glint(frame) pollen(frame) end