info = { type = "image", title = "Sunflower glow", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Sunflower field in warm evening light", api = 33, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Noon"] = "Poludnie", ["Golden hour"] = "Zlatá hodina", ["Dusk"] = "Súmrak" }, cs = { ["Time of day"] = "Denní doba", ["Noon"] = "Poledne", ["Golden hour"] = "Zlatá hodinka", ["Dusk"] = "Soumrak" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tod", label = T("Time of day"), type = "int", default = "golden", opts = { keys = { "noon", "golden", "dusk" }, T("Noon"), T("Golden hour"), T("Dusk") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, abs, sqrt, atan = math.floor, math.ceil, math.sin, math.cos, math.abs, math.sqrt, math.atan local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local MODES = { noon = { sky = { 62, 134, 228, 120, 184, 244, 188, 226, 250 }, sun = { 50, 8, 4, 255, 250, 214 }, hill1 = { 124, 164, 184 }, hill2 = { 92, 146, 100 }, field = { 70, 138, 52 }, cl = { 252, 254, 255, 206, 226, 244 }, tint = { 1.0, 1.0, 1.0 } }, golden = { sky = { 104, 140, 206, 238, 176, 150, 255, 198, 120 }, sun = { 46, 34, 6, 255, 238, 176 }, hill1 = { 190, 128, 126 }, hill2 = { 120, 108, 70 }, field = { 88, 100, 44 }, cl = { 255, 214, 178, 226, 144, 132 }, tint = { 1.0, 0.88, 0.7 } }, dusk = { sky = { 38, 46, 112, 150, 84, 128, 240, 132, 100 }, sun = { 46, 41, 6, 236, 150, 96 }, hill1 = { 124, 72, 104 }, hill2 = { 78, 62, 78 }, field = { 62, 66, 52 }, cl = { 226, 130, 140, 118, 64, 104 }, tint = { 0.76, 0.54, 0.58 } }, } local M = MODES.golden local TR, TG, TB = 1, 0.88, 0.7 local function tc(r, g, b) return floor(r * TR + 0.5), floor(g * TG + 0.5), floor(b * TB + 0.5) end local SPEC = { { R = 11, sq = 1.0, N = 17 }, { R = 8, sq = 0.88, N = 15 }, { R = 5, sq = 0.92, N = 11 }, { R = 3, sq = 1.0, N = 9 }, } for i = 1, 4 do SPEC[i].S = 2 * ceil(SPEC[i].R) + 3; SPEC[i].id = i end local HEADS = { { s = 4, cx = 14, cy = 49, bx = 15, A = 0.8, P = 2.8, ph = 0.6, sw = 1 }, { s = 4, cx = 51, cy = 51, bx = 50, A = 0.8, P = 2.9, ph = 2.4, sw = 1 }, { s = 3, cx = 6, cy = 43, bx = 7, A = 1.0, P = 3.2, ph = 1.1, sw = 1 }, { s = 3, cx = 35, cy = 45, bx = 36, A = 1.0, P = 3.5, ph = 4.0, sw = 1 }, { s = 3, cx = 58, cy = 47, bx = 57, A = 1.0, P = 3.0, ph = 5.1, sw = 1 }, { s = 2, cx = 47, cy = 38, bx = 44, A = 1.6, P = 4.0, ph = 3.0, sw = 2 }, { s = 1, cx = 21, cy = 30, bx = 25, A = 2.0, P = 4.6, ph = 0.2, sw = 2 }, } local RAS, ROWS = {}, {} local function head_pixel(sp, x, y, cx, cy) local R = sp.R local dx, dy = (x + 0.5 - cx) / sp.sq, y + 0.5 - cy local r2 = dx * dx + dy * dy if r2 > R * R then return nil end local r = sqrt(r2) local th = atan(dy, dx) local N = sp.N local rc = R * 0.42 local lit = (-dx * 0.55 - dy * 0.83) / R local s = abs(sin(N * th * 0.5)) local ro = R * (0.86 + 0.14 * (1 - s)) local cr, cg, cb if r < rc then local t = r / rc local hs = hash32(floor((th + 3.1416) * R * 0.8) * 7919 + floor(r * 1.4) * 104729 + sp.id) & 15 cr, cg, cb = 70 + 62 * t + hs - 8 + lit * 14, 38 + 40 * t + (hs >> 1) - 4 + lit * 8, 14 + 12 * t if t > 0.86 then cr, cg, cb = cr * 0.7, cg * 0.7, cb * 0.7 end elseif r < ro then local u = (r - rc) / (ro - rc) local k = 0.8 + 0.2 * (1 - s) if u < 0.4 then local v = u / 0.4 cr, cg, cb = 214 + 30 * v, 108 + 58 * v, 10 + 8 * v else local v = (u - 0.4) / 0.6 cr, cg, cb = 244 + 11 * v, 166 + 56 * v, 18 + 74 * v end if r < rc * 1.25 then k = k * 0.82 end cr, cg, cb = cr * k + lit * 16, cg * k + lit * 14, cb * k + lit * 6 elseif r < R * (0.94 + 0.06 * (1 - abs(cos(N * th * 0.5)))) then cr, cg, cb = 190 + lit * 14, 104 + lit * 10, 10 else return nil end cr, cg, cb = cr * TR, cg * TG, cb * TB if cr > 255 then cr = 255 elseif cr < 1 then cr = 1 end if cg > 255 then cg = 255 elseif cg < 1 then cg = 1 end if cb > 255 then cb = 255 elseif cb < 1 then cb = 1 end return floor(cr), floor(cg), floor(cb) end local function build_rows(sid, ph, y0, y1) local sp = SPEC[sid] local S = sp.S local key = sid * 10 + ph local rows = ROWS[key] or {} ROWS[key] = rows local cx, cy = S / 2 + ph / 4, S / 2 for y = y0, y1 do local b = {} for x = 0, S - 1 do local r, g, bl = head_pixel(sp, x, y, cx, cy) if r then b[x * 3 + 1], b[x * 3 + 2], b[x * 3 + 3] = r, g, bl else b[x * 3 + 1], b[x * 3 + 2], b[x * 3 + 3] = 255, 0, 255 end end rows[y + 1] = string.char(table.unpack(b, 1, S * 3)) end if y1 == S - 1 then RAS[key] = table.concat(rows); ROWS[key] = nil end end local BG = nil local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local function leaf(bx, by, dir, len, th) for i = 0, len do local t = i / len local d = floor(th * sin(3.14159 * t) ^ 0.8 + 0.5) local yc = by + floor(0.45 * i) if d > 0 then local h1 = d // 2 rect(bx + dir * i, yc - h1, 1, h1, tc(86, 150, 60)) rect(bx + dir * i, yc, 1, d - h1, tc(44, 98, 40)) end end end local function build_bg() local S = M.sky for y = 0, 41 do local r, g, b if y < 22 then local t = y / 22 r, g, b = lerp(S[1], S[4], t), lerp(S[2], S[5], t), lerp(S[3], S[6], t) else local t = (y - 22) / 19 r, g, b = lerp(S[4], S[7], t), lerp(S[5], S[8], t), lerp(S[6], S[9], t) end rect(0, y, W, 1, r, g, b) end local sn = M.sun for dy = -sn[3], sn[3] do local hw = floor(sqrt(sn[3] * sn[3] + 0.5 - dy * dy)) rect(sn[1] - hw, sn[2] + dy, 2 * hw + 1, 1, sn[4], sn[5], sn[6]) end for x = 0, W - 1 do local a = floor(37 + 3 * sin(x * 0.07 + 0.5) + 2 * sin(x * 0.19) + 0.5) rect(x, a, 1, 30, M.hill1[1], M.hill1[2], M.hill1[3]) local b = floor(41 + 2 * sin(x * 0.11 + 2.0) + 0.5) rect(x, b, 1, 30, M.hill2[1], M.hill2[2], M.hill2[3]) end local F = M.field for y = 43, H - 1 do local t = (y - 43) / 20 rect(0, y, W, 1, lerp(F[1], F[1] * 0.62, t), lerp(F[2], F[2] * 0.66, t), lerp(F[3], F[3] * 0.7, t)) end local rows = { { 44, 2, 5 }, { 47, 2, 7 }, { 51, 3, 9 }, { 56, 4, 11 } } for j = 1, 4 do local y, sz, sp = rows[j][1], rows[j][2], rows[j][3] for x = (j * 3) % sp - sp, W, sp do local h = hash32(x * 31 + j * 977) & 0x7FFF local xx = x + h % 3 local haze = (5 - j) * 0.1 local pr, pg, pb = tc(238, 186, 30) rect(xx, y, sz, sz - (sz > 3 and 1 or 0), lerp(pr, F[1], haze), lerp(pg, F[2], haze), lerp(pb, F[3], haze)) rect(xx + sz // 2, y + sz // 2 - (sz > 3 and 1 or 0), 1, 1, tc(78, 48, 20)) rect(xx + sz // 2, y + sz, 1, 3, tc(54, 96, 40)) end end leaf(25, 55, -1, 11, 5); leaf(25, 57, 1, 10, 5) leaf(44, 56, -1, 8, 4); leaf(44, 58, 1, 9, 4) BG = gfx.snapshot(0, 0, W, H) end local STAGES, stage = {}, 1 function on_settings() M = MODES[cfg.tod or "golden"] or MODES.golden TR, TG, TB = M.tint[1], M.tint[2], M.tint[3] BG, RAS, ROWS = nil, {}, {} STAGES = { build_bg } local chunks = { 3, 2, 1, 1 } for sid = 1, 4 do local S = SPEC[sid].S for ph = 0, 3 do local n = chunks[sid] for c = 0, n - 1 do local y0, y1 = c * S // n, (c + 1) * S // n - 1 STAGES[#STAGES + 1] = function() build_rows(sid, ph, y0, y1) end end end end stage = 1 end local function bar(x0, x1, y, r, g, b, al) local i0, i1 = floor(x0), floor(x1) if i0 == i1 then blend_rect(i0, y, 1, 1, r, g, b, (x1 - x0) * al); return end blend_rect(i0, y, 1, 1, r, g, b, (1 - (x0 - i0)) * al) if i1 - i0 > 1 then blend_rect(i0 + 1, y, i1 - i0 - 1, 1, r, g, b, al) end if x1 > i1 then blend_rect(i1, y, 1, 1, r, g, b, (x1 - i1) * al) end end local CROWS = { { 6, 20, 0.5 }, { 2, 26, 0.8 }, { 0, 30, 0.85 }, { 3, 25, 0.6 }, { 8, 18, 0.35 } } local CLOUDS = { { y = 5, x = 10, v = 1.4 }, { y = 17, x = 47, v = 0.9 }, { y = 26, x = 80, v = 0.6 } } local function draw_clouds(t) local c = M.cl for i = 1, 3 do local cl = CLOUDS[i] local cx = (cl.x + t * cl.v) % (W + 40) - 34 for r = 1, 5 do local s = CROWS[r] local lo = r >= 4 bar(cx + s[1], cx + s[2], cl.y + r - 1, lo and c[4] or c[1], lo and c[5] or c[2], lo and c[6] or c[3], s[3]) end end end local function draw_visitor(t) local cyc = floor(t / 22) local u = t - cyc * 22 if cyc % 2 == 0 then if u < 7 then local x = -6 + u * 10.5 local y = 34 + floor(3 * sin(u * 2.3) + 0.5) bar(x, x + 3, y, 250, 206, 40, 1) bar(x, x + 3, y + 1, 70, 52, 24, 1) local up = (frame // 2) % 2 == 0 blend_rect(floor(x) + 1, y - (up and 2 or 1), 2, 1, 255, 255, 255, 0.7) end elseif u < 12 then local x = 70 - u * 6.3 local y = 9 + floor(2 * sin(u * 0.9) + 0.5) local ix = floor(x) local c1, c2, c3 = 52, 40, 48 if (frame // 5) % 2 == 0 then px(ix - 2, y - 1, c1, c2, c3); px(ix - 1, y, c1, c2, c3); px(ix, y, c1, c2, c3); px(ix + 1, y - 1, c1, c2, c3) else px(ix - 2, y, c1, c2, c3); px(ix - 1, y, c1, c2, c3); px(ix, y + 1, c1, c2, c3); px(ix + 1, y, c1, c2, c3) px(ix + 2, y, c1, c2, c3) end end end function render() if stage <= #STAGES then STAGES[stage](); stage = stage + 1 end if not BG then return end local t = frame / FPS gfx.blit_rgb(BG, 0, 0, W, H) draw_clouds(t) draw_visitor(t) local w = 0.75 + 0.25 * sin(TAU * t / 9) local sr, sg, sb = tc(80, 124, 52) local lr, lg, lb = tc(112, 152, 72) local dr, dg, db = tc(56, 92, 40) local D = {} for i = 1, 7 do local h = HEADS[i] local d = h.A * w * sin(TAU * t / h.P + h.ph) + 0.35 * h.A * sin(TAU * t / (h.P * 0.37) + h.ph * 2) D[i] = d local sp = SPEC[h.s] local ytop = h.cy + sp.R * 0.8 local n = 63 - floor(ytop) for y = floor(ytop), 63 do local s = (63 - y) / n local x = floor(h.bx + (h.cx - h.bx) * s + d * s * s + 0.5) if h.sw == 2 then rect(x - 1, y, 1, 1, lr, lg, lb); rect(x, y, 1, 1, dr, dg, db) else rect(x, y, 1, 1, sr, sg, sb) end end end for i = 1, 7 do local h = HEADS[i] local sp = SPEC[h.s] local x0 = h.cx - sp.S / 2 + D[i] local ix = floor(x0) local ras = RAS[h.s * 10 + floor((x0 - ix) * 4)] if ras then gfx.blit_rgb(ras, ix, floor(h.cy - sp.S / 2 + 0.5), sp.S, sp.S, 255, 0, 255) end end end