info = { type = "image", title = "Maple leaf macro", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Dewy maple leaf on wet stone, forest bokeh", api = 44, } local tr = ({ sk = { ["Dew drops"] = "Kvapky rosy", ["Glints"] = "Trblietanie", ["Falling drop"] = "Padajúca kvapka" }, cs = { ["Dew drops"] = "Kapky rosy", ["Glints"] = "Třpyt", ["Falling drop"] = "Padající kapka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "drops", label = T("Dew drops"), type = "int", min = 0, max = 8, default = 6 }, { key = "glint", label = T("Glints"), type = "int", min = 0, max = 5, default = 3 }, { key = "drip", label = T("Falling drop"), type = "bool", default = true }, } 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, abs, max, min, PI = math.floor, math.sin, math.cos, math.sqrt, math.atan, math.abs, math.max, math.min, math.pi local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local LCX, LCY, S, ROT = 25, 54, 11.5, 0.32 local BX0, BX1, BY0, BY1 = 3, 47, 24, 62 local PUD = { x = 49, y = 53, rx = 14, ry = 6.2 } local DPX = 52 local RT = {} local function tabulate() for i = 0, 719 do local th = (i + 0.5) * PI / 360 - PI RT[i + 1] = (1 + sin(th)) * (0.68 + 0.32 * cos(8 * th)) * (1 + 0.05 * cos(40 * th)) end end local function cover(x, y) local dx, dy = x - LCX, LCY - y local th = atan(dy, dx) + ROT if th > PI then th = th - 2 * PI end local idx = floor((th + PI) * 114.59) if idx > 719 then idx = 719 end return RT[idx + 1] * S - sqrt(dx * dx + dy * dy), th end local NG, NS = {}, 40 local function nz(x, y) local xi, yi = floor(x), floor(y) local fx, fy = x - xi, y - yi fx = fx * fx * (3 - 2 * fx); fy = fy * fy * (3 - 2 * fy) local a = NG[yi * NS + xi + 1] local b = NG[yi * NS + xi + 2] local c = NG[yi * NS + NS + xi + 1] local d = NG[yi * NS + NS + xi + 2] return (a + (b - a) * fx) * (1 - fy) + (c + (d - c) * fx) * fy end local function lerp(a, b, t) return a + (b - a) * t end local DROPS, ND = {}, 0 local SPARK = { { 42, 51 }, { 56, 56 }, { 47, 58 } } local BOKEH = { { 240, 172, 52 }, { 222, 112, 40 }, { 156, 192, 72 }, { 255, 222, 124 }, { 118, 172, 92 }, { 232, 140, 48 }, } local function stage_bg() tabulate() for i = 0, NS * NS - 1 do NG[i + 1] = (hash32(i * 0x9E3779B1 + 7) & 0x7FFF) / 32767 end for y = 0, 26 do local t = y / 26 rect(0, y, W, 1, floor(lerp(18, 74, t)), floor(lerp(34, 86, t)), floor(lerp(28, 40, t))) end for i = 1, 14 do local h = hash32(i * 0x85EBCA6B) local r = 3 + umod(h, 5) local cx, cy = umod(h >> 6, W + 8) - 4, 2 + umod(h >> 14, 22) local c = BOKEH[umod(h >> 20, #BOKEH) + 1] local a = 0.26 + umod(h >> 24, 20) / 100 for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3], a) blend_rect(cx - hw, cy + dy, 1, 1, c[1], c[2], c[3], a * 0.8) blend_rect(cx + hw, cy + dy, 1, 1, c[1], c[2], c[3], a * 0.8) end end end local function line(x0, y0, x1, y1, r, g, b, a) local n = max(abs(x1 - x0), abs(y1 - y0)) for i = 0, floor(n) do local t = n > 0 and i / n or 0 blend(floor(x0 + (x1 - x0) * t + 0.5), floor(y0 + (y1 - y0) * t + 0.5), r, g, b, a) end end local function stage_stone() for y = 14, H - 1 do local t = (y - 14) / 49 local a = min(1, (y - 14) / 9) blend_rect(0, y, W, 1, floor(lerp(104, 58, t)), floor(lerp(108, 62, t)), floor(lerp(122, 76, t)), a) end for i = 1, 38 do local h = hash32(i * 0xC2B2AE35) local cx, cy = umod(h, W), 22 + umod(h >> 8, 41) local rx, ry = 3 + umod(h >> 14, 7), 1 + umod(h >> 18, 3) local light = umod(h >> 22, 2) == 0 local r, g, b, a = 150, 158, 176, 0.11 if not light then r, g, b, a = 30, 34, 46, 0.2 end for dy = -ry, ry do local hw = floor(rx * sqrt(1 - (dy / (ry + 0.5)) ^ 2) + 0.5) blend_rect(cx - hw, cy + dy, 2 * hw + 1, 1, r, g, b, a) end end for i = 1, 9 do local h = hash32(i * 0x2545F491) blend_rect(umod(h, W - 6), 26 + umod(h >> 8, 36), 2 + umod(h >> 16, 4), 1, 255, 255, 255, 0.3) end line(2, 44, 12, 48, 40, 44, 56, 0.5); line(12, 48, 14, 55, 40, 44, 56, 0.4) line(58, 28, 50, 34, 40, 44, 56, 0.45) local P = PUD for y = P.y - 7, P.y + 7 do local dy = (y - P.y) / (P.ry + 0.3) if dy * dy < 1 then local hw = P.rx * sqrt(1 - dy * dy) local t = (dy + 1) / 2 rect(floor(P.x - hw + 0.5), y, floor(2 * hw + 0.5), 1, floor(lerp(182, 114, t)), floor(lerp(200, 138, t)), floor(lerp(222, 172, t))) blend(floor(P.x - hw + 0.5), y, 44, 50, 64, 0.75); blend(floor(P.x + hw - 0.5), y, 44, 50, 64, 0.75) if t < 0.25 or t > 0.78 then blend_rect(floor(P.x - hw + 1.5), y, floor(2 * hw - 2), 1, 44, 50, 64, 0.25) end end end blend_rect(P.x - 11, P.y - 1, 11, 3, 236, 130, 50, 0.14) blend_rect(P.x + 2, P.y - 3, 8, 1, 255, 255, 255, 0.4) end local V0, V1, V2 = { 252, 214, 70 }, { 238, 122, 28 }, { 176, 30, 18 } local function stage_leaf(ya, yb) for y = ya, yb do for x = BX0, BX1 do local d, th = cover(x, y) if d > -0.5 then local dx, dy = x - LCX, LCY - y local rho = sqrt(dx * dx + dy * dy) / max(0.5, d + sqrt(dx * dx + dy * dy)) local q = rho * 0.95 + (nz(x * 0.3, y * 0.3) - 0.5) * 0.36 q = q < 0 and 0 or (q > 1 and 1 or q) local r, g, b if q < 0.5 then local t = q * 2 r, g, b = lerp(V0[1], V1[1], t), lerp(V0[2], V1[2], t), lerp(V0[3], V1[3], t) else local t = q * 2 - 1 r, g, b = lerp(V1[1], V2[1], t), lerp(V1[2], V2[2], t), lerp(V1[3], V2[3], t) end local vein = max(0, (cos(8 * th) - 0.9) / 0.1) * (1 - rho * rho) + 0.45 * max(0, (cos(24 * th) - 0.94) / 0.06) * (1 - rho) if vein > 0 then vein = min(1, vein) * 0.55 r, g, b = lerp(r, 255, vein), lerp(g, 224, vein), lerp(b, 110, vein) end local sh = 0.9 + 0.12 * sin(8 * th) + (nz(x * 0.6, y * 0.6) - 0.5) * 0.12 if d < 2.2 then sh = sh * (0.8 + 0.09 * d) end local spec = 0.26 * max(0, 1 - ((x - 17) ^ 2 + (y - 39) ^ 2) / 160) r = max(0, min(255, floor(r * sh + (255 - r * sh) * spec))) g = max(0, min(255, floor(g * sh + (255 - g * sh) * spec))) b = max(0, min(255, floor(b * sh + (255 - b * sh) * spec))) if d >= 0.5 then px(x, y, r, g, b) else blend(x, y, r * 0.85, g * 0.85, b * 0.85, d + 0.5) end else local d1 = cover(x - 1.5, y - 2) local a = d1 > -0.5 and min(1, d1 + 0.5) * 0.34 or 0 local d2 = cover(x - 3, y - 4) if d2 > -0.5 then a = a + min(1, d2 + 0.5) * 0.22 end if a > 0.01 then blend(x, y, 6, 8, 14, min(0.55, a)) end end end end end local function stage_finish() for i = 0, 7 do local x, y = LCX - 0.317 * i, LCY + 0.948 * i px(floor(x + 0.5), floor(y + 0.5), 156, 74, 30); px(floor(x + 0.5) + 1, floor(y + 0.5), 112, 48, 22) end DROPS, ND = {}, 0 local want = cfg.drops or 6 for i = 1, 120 do if ND >= want then break end local h = hash32(i * 0x9E3779B1) local x, y = BX0 + 6 + umod(h, BX1 - BX0 - 12), BY0 + 4 + umod(h >> 8, BY1 - BY0 - 14) local d = cover(x, y) local ok = d > 4 and (x - LCX) ^ 2 + (y - LCY) ^ 2 > 100 for j = 1, ND do if (DROPS[j].cx - x) ^ 2 + (DROPS[j].cy - y) ^ 2 < 36 then ok = false end end if ok then ND = ND + 1 local r = ND <= 2 and 2.2 or 1.5 DROPS[ND] = { cx = x, cy = y, off = umod(h >> 14, 200), per = 160 - 20 * (cfg.glint or 3) + umod(h >> 20, 60), hx = floor(x - r * 0.45), hy = floor(y - r * 0.45) } for oy = -2, 2 do for ox = -2, 2 do local u, v = ox / r, oy / r local rr = u * u + v * v if rr <= 1.15 then blend(x + ox, y + oy, 255, 214, 150, 0.32) if u + v > 0.45 then blend(x + ox, y + oy, 84, 26, 8, 0.42) end if rr > 0.6 and u + v < 0 then blend(x + ox, y + oy, 255, 244, 214, 0.4) end end end end blend(x + floor(r * 0.5), y + floor(r * 0.5), 255, 206, 140, 0.5) px(DROPS[ND].hx, DROPS[ND].hy, 255, 252, 240) end end end local STAGES = { stage_bg, stage_stone } for ya = BY0, BY1, 4 do STAGES[#STAGES + 1] = function() stage_leaf(ya, min(BY1, ya + 3)) end end STAGES[#STAGES + 1] = stage_finish local STAGE, PART, BG = 1, nil, nil function on_settings() STAGE, BG = 1, nil end local function build() if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() if STAGE < #STAGES then PART = snapshot(0, 0, W, H) else BG = snapshot(0, 0, W, H) end STAGE = STAGE + 1 end local function star(x, y, a, r, g, b) blend(x, y, r, g, b, a) if a > 0.3 then blend(x - 1, y, r, g, b, a * 0.6); blend(x + 1, y, r, g, b, a * 0.6) blend(x, y - 1, r, g, b, a * 0.6); blend(x, y + 1, r, g, b, a * 0.6) end if a > 0.75 then blend(x - 2, y, r, g, b, 0.35); blend(x + 2, y, r, g, b, 0.35) blend(x, y - 2, r, g, b, 0.35); blend(x, y + 2, r, g, b, 0.35) end end local function glints() local g = cfg.glint or 3 if g == 0 then return end for i = 1, ND do local d = DROPS[i] local ph = (frame + d.off) % d.per if ph < 14 then star(d.hx, d.hy, 1 - abs(ph - 7) / 7, 255, 248, 230) end end for i = 1, #SPARK do local ph = (frame + i * 53) % (200 - 25 * g) if ph < 12 then star(SPARK[i][1], SPARK[i][2], 0.9 * (1 - abs(ph - 6) / 6), 255, 255, 255) end end end local DP = 210 local TFALL = 25 local function ripples(f) local age = (f - TFALL) / FPS if age < 0 or age > 1.6 then return end local cx, cy = DPX, PUD.y if age < 0.2 then for s = -1, 1, 2 do blend(floor(cx + s * (1 + age * 14) + 0.5), floor(cy - 1 - 12 * age + 28 * age * age + 0.5), 236, 244, 255, 0.8 * (1 - age * 4)) end end for k = 0, 1 do local a = age - k * 0.28 if a > 0 then local r = 1.5 + a * 9 local al = 0.7 * (1 - a / 1.4) if al > 0 then local n = floor(r * 4) + 8 local ry = r * 0.42 for i = 0, n - 1 do local p = 6.2832 * i / n blend(floor(cx + r * cos(p) + 0.5), floor(cy + ry * sin(p) + 0.5), 236, 244, 255, al) end end end end end function render() if not BG then build() if not BG then gfx.ready(false) end return end blit_rgb(BG, 0, 0, W, H) glints() if cfg.drip ~= false then local f = frame % DP if f < TFALL then local t = f / FPS local y = -3 + 80 * t * t blend_rect(DPX, floor(y) - 2, 1, 3, 200, 224, 248, 0.6) px(DPX, floor(y) + 1, 240, 248, 255) end ripples(f) end end