info = { type = "image", title = "Backlit wheat", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Wheat ears sway against a low sun, fluff drifts", api = 44, } local tr = ({ sk = { ["Time"] = "Čas", ["Sunset"] = "Západ", ["Blue hour"] = "Modrá hodinka" }, cs = { ["Time"] = "Čas", ["Sunset"] = "Západ", ["Blue hour"] = "Modrá hodinka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Time"), type = "int", default = "sunset", opts = { keys = { "sunset", "bluehour" }, T("Sunset"), T("Blue hour") } }, } local rect, blend, blend_rect, blend_mask = gfx.rect, gfx.blend, gfx.blend_rect, gfx.blend_mask local floor, sin, sqrt, abs, min, max = math.floor, math.sin, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local PI, TAU = math.pi, 6.2831853 local HZ = 34 local SUN_X, SUN_Y = 42, 31 local TIMES = { sunset = { sky = { { 0, { 62, 82, 150 } }, { 14, { 150, 104, 150 } }, { 22, { 222, 124, 120 } }, { 29, { 255, 170, 76 } }, { 33, { 255, 216, 134 } } }, glow = { 255, 176, 90 }, glow_a = 0.55, sun = true, gy = SUN_Y, far = { { 212, 140, 66 }, { 88, 62, 34 } }, blob = { 70, 46, 40 }, haze = { 255, 170, 80 }, mid = { { 70, 48, 26 }, { 24, 16, 10 } }, ear = { 40, 26, 14 }, rim = { 255, 200, 100 }, rima = 0.95, mear = { 74, 50, 26 }, back = { 128, 88, 40 }, backrim = { 255, 196, 96 }, stem = { 44, 30, 16 }, fluff = { 255, 238, 190 }, }, bluehour = { sky = { { 0, { 20, 32, 82 } }, { 14, { 50, 78, 148 } }, { 24, { 104, 136, 190 } }, { 30, { 196, 168, 160 } }, { 33, { 238, 184, 128 } } }, glow = { 236, 150, 104 }, glow_a = 0.42, sun = false, gy = 35, far = { { 42, 58, 78 }, { 18, 26, 38 } }, blob = { 20, 28, 44 }, haze = { 236, 150, 104 }, mid = { { 26, 38, 52 }, { 8, 12, 20 } }, ear = { 16, 20, 36 }, rim = { 150, 174, 224 }, rima = 0.6, mear = { 36, 48, 70 }, back = { 44, 60, 86 }, backrim = { 130, 154, 206 }, stem = { 14, 18, 30 }, fluff = { 206, 224, 255 }, }, } local TM = TIMES.sunset local function lerp(a, b, t) return a + (b - a) * t end local function lerp3(a, b, t) return floor(lerp(a[1], b[1], t) + 0.5), floor(lerp(a[2], b[2], t) + 0.5), floor(lerp(a[3], b[3], t) + 0.5) end local AWN = { { 1.0, 0, 1.0 }, { 0.95, 0.15, 0.95 }, { 0.95, -0.15, 0.95 }, { 0.85, 0.30, 0.85 }, { 0.85, -0.30, 0.85 }, { 0.7, 0.42, 0.7 }, { 0.7, -0.42, 0.7 } } local TYPES = { big = { Le = 13, La = 10, HW = 2.4, w = 15, h = 25, np = 4, bend = 0.012 }, mid = { Le = 9, La = 7, HW = 1.8, w = 11, h = 18, np = 4, bend = 0.012 }, back = { Le = 6, La = 4, HW = 1.3, w = 9, h = 12, np = 1, bend = 0.02 }, } local MK = { big = {}, mid = {}, back = {} } local function make_phase(T, off) local w, h, Le, La = T.w, T.h, T.Le, T.La local cxc = w // 2 local g = {} for i = 1, w * h do g[i] = 0 end local function axis(v) return cxc + off + T.bend * v * v end for cy = 0, h - 1 do local v = h - 1 - cy if v <= Le then local hw = T.HW * (0.15 + 0.85 * sin(PI * (v / Le) ^ 0.8)) * (1 + 0.16 * sin(v * 2.1)) local ax = axis(v) for cx = max(0, floor(ax - hw - 1)), min(w - 1, floor(ax + hw + 1)) do local s = hw - abs(cx - ax) + 0.5 if s > 0 then g[cy * w + cx + 1] = s > 1 and 1 or s end end end end for _, a in ipairs(AWN) do local v0, len = a[1] * Le, La * a[3] local sx, dx, dv = axis(v0), sin(a[2]), math.cos(a[2]) for k = 0, floor(len * 2) do local st = k * 0.5 local x, cy = sx + dx * st, h - 1 - floor(v0 + dv * st + 0.5) if cy >= 0 then local c0 = floor(x) local f = x - c0 local cov = 0.9 * (1 - 0.35 * st / len) local i0 = cy * w + c0 + 1 if c0 >= 0 and c0 < w and g[i0] < cov * (1 - f) then g[i0] = cov * (1 - f) end if c0 >= -1 and c0 + 1 < w and g[i0 + 1] < cov * f and c0 + 1 >= 0 then g[i0 + 1] = cov * f end end end end local D, RR, RL = {}, {}, {} local d, rr, rl = {}, {}, {} for cy = 0, h - 1 do for cx = 0, w - 1 do local i = cy * w + cx + 1 local s = g[i] d[cx + 1] = floor(s * 255 + 0.5) rr[cx + 1] = floor(s * (1 - (cx + 1 < w and g[i + 1] or 0)) * 255 + 0.5) rl[cx + 1] = floor(s * (1 - (cx > 0 and g[i - 1] or 0)) * 255 + 0.5) end D[cy + 1] = string.char(table.unpack(d, 1, w)) RR[cy + 1] = string.char(table.unpack(rr, 1, w)) RL[cy + 1] = string.char(table.unpack(rl, 1, w)) end return { table.concat(D), table.concat(RR), table.concat(RL) } end local JOBS = { { "back", 0 } } for ph = 0, 3 do JOBS[#JOBS + 1] = { "big", ph }; JOBS[#JOBS + 1] = { "mid", ph } end local JOB_DONE, JOBS_PER_FRAME = 0, 2 local BG, GLOW local BG_STAGE = 0 local function sky_row(y) local st = TM.sky for i = 2, #st do if y <= st[i][1] then return lerp3(st[i - 1][2], st[i][2], (y - st[i - 1][1]) / (st[i][1] - st[i - 1][1])) end end local c = st[#st][2] return c[1], c[2], c[3] end local function ear_draw(tp, hx, by, rim_c, rim_a, ear_c) local T = TYPES[tp] local ix = floor(hx) local m = MK[tp][floor((hx - ix) * T.np) + 1] local ox, oy = ix - T.w // 2, by - (T.h - 1) blend_mask(m[1], ear_c[1], ear_c[2], ear_c[3], ox, oy, T.w, T.h, T.w, T.h, 1) blend_mask(hx < SUN_X and m[2] or m[3], rim_c[1], rim_c[2], rim_c[3], ox, oy, T.w, T.h, T.w, T.h, rim_a) end local function build_glow() local rows, row = {}, {} for y = 0, 40 do for x = 0, 40 do local d = sqrt((x - 20) ^ 2 + (y - 20) ^ 2) / 20.5 row[x + 1] = d >= 1 and 0 or floor((1 - d) * 255 + 0.5) end rows[y + 1] = string.char(table.unpack(row, 1, 41)) end GLOW = table.concat(rows) end local function build_bg() for y = 0, HZ - 1 do rect(0, y, W, 1, sky_row(y)) end if not TM.sun then for i = 1, 9 do local h = hash32(i * 0x9E3779B1) blend(umod(h, W), umod(h >> 8, 16), 200, 210, 250, 0.35 + umod(h >> 16, 5) / 10) end end blend_mask(GLOW, TM.glow[1], TM.glow[2], TM.glow[3], SUN_X - 20, TM.gy - 20, 41, 41, 41, 41, TM.glow_a) if TM.sun then for _, s in ipairs({ { 6, 255, 222, 150 }, { 4, 255, 248, 224 } }) do for dy = -s[1], s[1] do local y = SUN_Y + dy if y < HZ then local hw = floor(sqrt(s[1] * s[1] + s[1] - dy * dy) + 0.5) rect(SUN_X - hw, y, 2 * hw + 1, 1, s[2], s[3], s[4]) end end end end for y = HZ, 45 do rect(0, y, W, 1, lerp3(TM.far[1], TM.far[2], (y - HZ) / 11)) end for y = HZ, HZ + 3 do blend_rect(0, y, W, 1, TM.haze[1], TM.haze[2], TM.haze[3], 0.2 * (1 - (y - HZ) / 4)) end for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) local x, w = umod(h, 70) - 6, 8 + umod(h >> 8, 10) blend_rect(x, HZ - 1, w, 4, TM.blob[1], TM.blob[2], TM.blob[3], 0.5) blend_rect(x + 2, HZ - 2, w - 4, 1, TM.blob[1], TM.blob[2], TM.blob[3], 0.3) blend_rect(x + 4, HZ - 3, w - 8, 1, TM.blob[1], TM.blob[2], TM.blob[3], 0.18) end for y = 46, H - 1 do rect(0, y, W, 1, lerp3(TM.mid[1], TM.mid[2], (y - 46) / 17)) end end local function build_bg2() for i = 1, 22 do local h = hash32(i * 0x27D4EB2F) local x, by = (i - 1) * 3 + umod(h, 3), 52 + umod(h >> 8, 5) rect(x, by, 1, H - by, TM.stem[1] + 18, TM.stem[2] + 12, TM.stem[3] + 6) ear_draw("back", x, by, TM.backrim, 0.8, TM.back) end for i = 1, 44 do local h = hash32(i * 0xC2B2AE35) local bh = 5 + umod(h >> 8, 10) local k = umod(h >> 16, 3) / 3 rect(umod(h, W), H - bh, 1, bh, lerp3(TM.stem, TM.mid[1], k)) end BG = gfx.snapshot(0, 0, W, H) end local BIG = { { 5, 40 }, { 16, 36 }, { 27, 42 }, { 50, 38 }, { 60, 44 } } local MIDE = { { 10, 47 }, { 21, 49 }, { 33, 46 }, { 44, 50 }, { 55, 47 } } local function gust(t) return 0.7 + 0.9 * sin(TAU * t / 360) + 0.35 * sin(TAU * t / 97) end local function stem(bx, hy, dx, c) local ad = abs(dx) local n = floor(ad + 0.5) local sg = dx < 0 and -1 or 1 local ytop, span = H, H - 1 - hy for k = 0, n do local yb = k < n and floor(H - 1 - sqrt((k + 0.5) / ad) * span + 0.5) or hy rect(bx + sg * k, yb, 1, ytop - yb, c[1], c[2], c[3]) ytop = yb end end local function ears(list, tp, amp, ear_c, rim_c, rim_a) for i, e in ipairs(list) do local ph = i * 1.7 + (tp == "mid" and 1 or 0) local hx = e[1] + amp * sin(TAU * frame / (150 + (i * 37) % 60) + ph) + gust(frame) * (0.8 + (i % 3) * 0.2) stem(e[1], e[2], hx - e[1], TM.stem) ear_draw(tp, hx, e[2], rim_c, rim_a, ear_c) end end local FLUFF = {} for i = 1, 6 do local h = hash32(i * 0x9E3779B1) FLUFF[i] = { umod(h, 70), umod(h >> 8, 50) + 6, 0.12 + umod(h >> 16, 8) / 60, -0.04 - umod(h >> 20, 5) / 80 } end local function fluff() local c = TM.fluff for i = 1, #FLUFF do local f = FLUFF[i] local x = (f[1] + f[3] * frame) % 70 - 3 local y = (f[2] + f[4] * frame + 2.2 * sin(frame * 0.03 + i)) % H local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, c[1], c[2], c[3], 0.85 * (1 - fx) * (1 - fy)) blend(ix + 1, iy, c[1], c[2], c[3], 0.85 * fx * (1 - fy)) blend(ix, iy + 1, c[1], c[2], c[3], 0.85 * (1 - fx) * fy) blend(ix + 1, iy + 1, c[1], c[2], c[3], 0.85 * fx * fy) end end function on_settings() TM = TIMES[cfg.time] or TIMES.sunset BG_STAGE = 0 end function render() if JOB_DONE < #JOBS then for _ = 1, JOBS_PER_FRAME do if JOB_DONE >= #JOBS then break end JOB_DONE = JOB_DONE + 1 local j = JOBS[JOB_DONE] local T = TYPES[j[1]] MK[j[1]][j[2] + 1] = make_phase(T, j[2] / T.np) end gfx.ready(false) return end if BG_STAGE == 0 then if not GLOW then build_glow(); gfx.ready(false) return end build_bg(); BG_STAGE = 1; gfx.ready(false) return end if BG_STAGE == 1 then build_bg2(); BG_STAGE = 2 end gfx.blit_rgb(BG, 0, 0, W, H) fluff() ears(MIDE, "mid", 1.0, TM.mear, TM.rim, TM.rima * 0.8) ears(BIG, "big", 1.5, TM.ear, TM.rim, TM.rima) end