info = { type = "image", title = "Cloud ridge", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Timelapse cloud pouring over a snowy ridge", api = 44, } local tr = ({ sk = { ["Light"] = "Svetlo", ["Noon"] = "Poludnie", ["Golden hour"] = "Zlatá hodinka", ["Dusk"] = "Súmrak", ["Speed"] = "Rýchlosť", ["Clouds"] = "Oblaky" }, cs = { ["Light"] = "Světlo", ["Noon"] = "Poledne", ["Golden hour"] = "Zlatá hodinka", ["Dusk"] = "Soumrak", ["Speed"] = "Rychlost", ["Clouds"] = "Mraky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "light", label = T("Light"), type = "int", default = "noon", opts = { keys = { "noon", "golden", "dusk" }, T("Noon"), T("Golden hour"), T("Dusk") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "amount", label = T("Clouds"), type = "int", min = 1, max = 5, default = 3 }, } local rect, blend_rect, blit_rgb, blend_mask = gfx.rect, gfx.blend_rect, gfx.blit_rgb, gfx.blend_mask local floor, sin, exp, sqrt = math.floor, math.sin, math.exp, math.sqrt local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PALS = { noon = { top = { 46, 98, 208 }, low = { 172, 206, 250 }, sl = { 244, 248, 255 }, ss = { 150, 172, 232 }, rl = { 122, 136, 196 }, rs = { 44, 58, 138 }, vl = { 40, 56, 124 }, vs = { 22, 34, 90 }, cl = { 255, 255, 255 }, cs = { 176, 196, 238 } }, golden = { top = { 70, 96, 188 }, low = { 255, 196, 140 }, sl = { 255, 238, 216 }, ss = { 172, 150, 206 }, rl = { 196, 140, 128 }, rs = { 84, 66, 130 }, vl = { 74, 58, 120 }, vs = { 36, 30, 84 }, cl = { 255, 246, 232 }, cs = { 214, 172, 190 } }, dusk = { top = { 26, 36, 104 }, low = { 168, 120, 196 }, sl = { 214, 204, 250 }, ss = { 100, 104, 190 }, rl = { 120, 112, 184 }, rs = { 40, 44, 118 }, vl = { 38, 42, 112 }, vs = { 20, 22, 70 }, cl = { 240, 226, 255 }, cs = { 130, 128, 204 } }, } local P = PALS.noon local SKYH = 46 local function mix(a, b, t) return a + (b - a) * t end local function skyc(y) local t = y / SKYH if t > 1 then t = 1 end local a, b = P.top, P.low return floor(mix(a[1], b[1], t) + 0.5), floor(mix(a[2], b[2], t) + 0.5), floor(mix(a[3], b[3], t) + 0.5) end local CREST = { 0, 43, 8, 37, 17, 30, 26, 23, 35, 17, 42, 12, 46, 9.5, 50, 14, 56, 18, 63, 21 } local RIDGE, NZ = {}, {} local function init_terrain() local k = 1 for x = 0, W - 1 do while CREST[k + 2] < x do k = k + 2 end local x0, y0, x1, y1 = CREST[k], CREST[k + 1], CREST[k + 2], CREST[k + 3] local y = mix(y0, y1, (x - x0) / (x1 - x0)) local rough = (x < 40 or x > 52) and 1 or 0.3 RIDGE[x] = y + rough * (0.9 * sin(x * 0.83 + 1) + 0.6 * sin(x * 2.1) + (umod(hash32(x * 0x9E3779B1), 100) - 50) / 90) end for i = 1, 256 do NZ[i] = (hash32(i * 0x85EBCA6B) & 0xFF) / 255 end end local function vn(x, y) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy fx = fx * fx * (3 - 2 * fx); fy = fy * fy * (3 - 2 * fy) local x0, x1 = ix % 16, (ix + 1) % 16 local r0, r1 = (iy % 16) * 16, ((iy + 1) % 16) * 16 local a = NZ[r0 + x0 + 1] + (NZ[r0 + x1 + 1] - NZ[r0 + x0 + 1]) * fx local b = NZ[r1 + x0 + 1] + (NZ[r1 + x1 + 1] - NZ[r1 + x0 + 1]) * fx return a + (b - a) * fy end local function mountain(x, y, d) local n1 = vn(x / 6.5, y / 6.5) local n2 = vn(x / 2.7 + 3, y / 2.7 + 5) if y > 44 + 0.72 * x then local t = 0.25 + (n2 - 0.5) * 0.9 + (n1 - 0.5) * 0.6 if y < 45.5 + 0.72 * x then t = t + 0.5 end if t < 0 then t = 0 elseif t > 1 then t = 1 end return mix(P.vs[1], P.vl[1], t), mix(P.vs[2], P.vl[2], t), mix(P.vs[3], P.vl[3], t) end local rib = 0.5 + 0.5 * sin((x + y * 0.4) * 0.75 + n1 * 3.5) local lit = (x - 42) / 8 if lit < 0 then lit = 0 elseif lit > 1 then lit = 1 end local L = 0.2 + 0.45 * lit + 0.42 * (rib - 0.5) + 0.2 * (n2 - 0.5) + 0.22 * exp(-d / 2.5) if L < 0 then L = 0 elseif L > 1 then L = 1 end local S = ((1 - y / 58) * 0.9 + (n1 - 0.5) * 0.9 + (n2 - 0.5) * 0.3 + 0.45 * lit - 0.78) * 4 if S < 0 then S = 0 elseif S > 1 then S = 1 end local sl, ss, rl, rs = P.sl, P.ss, P.rl, P.rs local sr, sg, sb = mix(ss[1], sl[1], L), mix(ss[2], sl[2], L), mix(ss[3], sl[3], L) local rr, rg, rb = mix(rs[1], rl[1], L), mix(rs[2], rl[2], L), mix(rs[3], rl[3], L) return mix(rr, sr, S), mix(rg, sg, S), mix(rb, sb, S) end local BAND = 2 local function build_band(y0) local rows = {} for y = y0, y0 + BAND - 1 do local t, n = {}, 0 local qr, qg, qb = skyc(y) for x = 0, W - 1 do local ry = RIDGE[x] local r, g, b if y + 0.5 < ry then r, g, b = qr, qg, qb else r, g, b = mountain(x, y, y + 0.5 - ry) r, g, b = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end t[n + 1], t[n + 2], t[n + 3] = r, g, b n = n + 3 end rows[#rows + 1] = char(unpack(t, 1, n)) end return concat(rows) end local MS = 21 local LOBES = { { { 0, 0, 9 }, { -4, 2, 6 }, { 4, 3, 6 }, { 1, -4, 5 } }, { { 0, 1, 8 }, { -5, 0, 5 }, { 5, 1, 5 }, { 0, -3, 5 }, { 2, 4, 5 } }, { { -3, 0, 7 }, { 3, 0, 7 }, { 0, -2, 6 } }, } local MASKS = {} local function build_mask(k) local rows = {} for j = 0, MS - 1 do local t = {} for i = 0, MS - 1 do local a = 0 for _, l in ipairs(LOBES[k]) do local dx, dy = i - 10 - l[1], j - 10 - l[2] local v = 1 - sqrt(dx * dx + dy * dy) / l[3] if v > a then a = v end end t[i + 1] = floor(a * 255 + 0.5) end rows[j + 1] = char(unpack(t, 1, MS)) end MASKS[k] = concat(rows) end local NP = 40 local PUF = {} for i = 1, NP do local h = hash32(i * 0xC2B2AE35) PUF[i] = { ph = ((i * 0.618034) % 1), sx = 6 + umod(h, 34), shape = 1 + umod(h >> 8, 3), size = 12 + umod(h >> 12, 9), asp = 1.0 + umod(h >> 16, 5) / 10, drift = 0.35 + umod(h >> 20, 4) / 10, } end local BG, PIECES, STAGE = nil, {}, 1 local NBANDS = H // BAND local COUNT, PERIOD = 24, 250 function on_settings() P = PALS[cfg.light] or PALS.noon COUNT = 10 + 6 * (cfg.amount or 3) PERIOD = 480 - 70 * (cfg.speed or 3) BG, PIECES, STAGE = nil, {}, 1 end local function step() local s = STAGE if s == 1 then init_terrain() elseif s <= 4 then build_mask(s - 1) else local k = s - 5 PIECES[k + 1] = build_band(k * BAND) if k == NBANDS - 1 then BG = concat(PIECES) end end STAGE = s + 1 end local function draw_partial() for y = 0, H - 1 do local k = y // BAND if not PIECES[k + 1] then rect(0, y, W, 1, skyc(y)) end end for k = 1, #PIECES do blit_rgb(PIECES[k], 0, (k - 1) * BAND, W, BAND) end end local GL = { { 50, 20 }, { 55, 23 }, { 47, 15 }, { 58, 25 }, { 52, 18 } } function render() if not BG then step() draw_partial() if not BG then gfx.ready(false) end return end blit_rgb(BG, 0, 0, W, H) local cl, cs = P.cl, P.cs local f = frame / PERIOD for i = 1, COUNT do local q = PUF[i] local p = (f + q.ph) % 1 local a = p < 0.18 and p / 0.18 or (p > 0.5 and (1 - p) * 2 or 1) local d = 30 * p * (0.6 + 0.8 * p) local cx = q.sx - q.drift * d local ry = RIDGE[floor(q.sx)] local cy = ry - 3 + d * 0.95 local size = q.size + 6 * p local w, h = floor(size * q.asp), floor(size) local t = p * 1.4 if t > 1 then t = 1 end local m = MASKS[q.shape] blend_mask(m, floor(mix(cl[1], cs[1], t)), floor(mix(cl[2], cs[2], t)), floor(mix(cl[3], cs[3], t)), floor(cx - w / 2), floor(cy - h / 2), w, h, MS, MS, 0.5 * a) local w2, h2 = floor(w * 0.6), floor(h * 0.6) blend_mask(m, cl[1], cl[2], cl[3], floor(cx - w2 / 2 + 2), floor(cy - h2 / 2 - 2), w2, h2, MS, MS, 0.42 * a * (1 - t * 0.7)) end local sl = P.sl local ts = frame / FPS for i = 1, #GL do local v = sin(ts * 1.3 + i * 2.1) if v > 0.55 then blend_rect(GL[i][1], GL[i][2], 1, 1, 255, 255, 255, (v - 0.55) * 2) end end end