info = { type = "image", title = "Sea stacks at dusk", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Long exposure of sea stacks in dusk haze", api = 44, } local ltr = ({ sk = { ["Afterglow"] = "Večerná zora", ["Rose"] = "Ružová", ["Amber"] = "Jantárová", ["Violet"] = "Fialová", ["Haze"] = "Opar", ["Cloud speed"] = "Rýchlosť mrakov" }, cs = { ["Afterglow"] = "Večerní záře", ["Rose"] = "Růžová", ["Amber"] = "Jantarová", ["Violet"] = "Fialová", ["Haze"] = "Opar", ["Cloud speed"] = "Rychlost mraků" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "tone", label = LT("Afterglow"), type = "int", default = "rose", opts = { keys = { "rose", "amber", "violet" }, LT("Rose"), LT("Amber"), LT("Violet") } }, { key = "haze", label = LT("Haze"), type = "int", min = 0, max = 5, default = 3 }, { key = "drift", label = LT("Cloud speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, min, max = math.floor, math.sin, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ = 26 local RX = 48 local TONES = { rose = { skyT = { 58, 82, 172 }, skyM = { 140, 118, 196 }, skyH = { 250, 168, 170 }, glow = { 255, 204, 176 }, cT = { 104, 96, 176 }, cM = { 232, 150, 176 }, cB = { 255, 196, 176 }, seaH = { 196, 196, 216 }, seaN = { 16, 172, 190 }, refl = { 250, 176, 186 }, rockD = { 34, 30, 52 }, rockL = { 170, 108, 128 }, rim = { 255, 176, 160 }, mist = { 236, 238, 248 }, }, amber = { skyT = { 40, 84, 170 }, skyM = { 150, 130, 170 }, skyH = { 255, 190, 110 }, glow = { 255, 224, 150 }, cT = { 96, 100, 170 }, cM = { 240, 160, 120 }, cB = { 255, 206, 130 }, seaH = { 206, 206, 190 }, seaN = { 14, 164, 178 }, refl = { 255, 196, 120 }, rockD = { 34, 32, 48 }, rockL = { 176, 120, 100 }, rim = { 255, 196, 120 }, mist = { 255, 244, 226 }, }, violet = { skyT = { 44, 50, 140 }, skyM = { 116, 92, 178 }, skyH = { 226, 150, 206 }, glow = { 255, 192, 222 }, cT = { 80, 70, 160 }, cM = { 200, 130, 200 }, cB = { 250, 170, 214 }, seaH = { 176, 180, 220 }, seaN = { 22, 150, 186 }, refl = { 230, 160, 214 }, rockD = { 30, 26, 56 }, rockL = { 150, 100, 150 }, rim = { 250, 170, 210 }, mist = { 232, 230, 250 }, }, } local P = TONES.rose local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function noise(x, seed) local i = floor(x) local f = x - i f = f * f * (3 - 2 * f) return vn(i, seed) * (1 - f) + vn(i + 1, seed) * f end local function poly(p, x) for i = 1, #p - 2, 2 do if x <= p[i + 2] then return p[i + 1] + (p[i + 3] - p[i + 1]) * (x - p[i]) / (p[i + 2] - p[i]) end end return p[#p] end local function mix(a, b, t) return a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local STACKS = { { xa = 5, xb = 20, base = 45, seed = 11, p = { 5,45, 6,33, 8,28, 11,25, 14,24, 17,27, 19,32, 20,45 } }, { xa = 21, xb = 32, base = 46, seed = 23, p = { 21,46, 22,37, 25,32, 28,31, 31,34, 32,46 } }, { xa = 45, xb = 58, base = 56, seed = 37, p = { 45,56, 47,52, 50,50, 54,51, 57,53, 58,56 } }, } local TOPY = {} for si, s in ipairs(STACKS) do local t = {} for x = s.xa, s.xb do t[x] = poly(s.p, x) + (noise(x / 1.9, s.seed) - 0.5) * 2.2 end TOPY[si] = t end local PART, BASE = nil, nil local function sky() for y = 0, HZ - 1 do local t = y / (HZ - 1) local r, g, b if t < 0.55 then r, g, b = mix(P.skyT, P.skyM, t / 0.55) else r, g, b = mix(P.skyM, P.skyH, ((t - 0.55) / 0.45) ^ 0.9) end rect(0, y, W, 1, rgb(r, g, b)) end for k = 0, 9 do local a = 0.5 * (1 - k / 10) local hw = 34 - k * 2.4 blend_rect(floor(RX - hw), HZ - 1 - k, floor(hw * 2), 1, P.glow[1], P.glow[2], P.glow[3], a) end end local function sea() for y = HZ, H - 1 do local t = ((y - HZ) / (H - 1 - HZ)) ^ 0.8 rect(0, y, W, 1, rgb(mix(P.seaH, P.seaN, t))) end for y = HZ, H - 1 do local d = (y - HZ) / (H - 1 - HZ) local hw = 3 + (y - HZ) * 0.34 local a = 0.36 * (1 - d) * (0.55 + 0.45 * noise(y / 1.3, 5)) blend_rect(floor(RX - hw), y, floor(hw * 2), 1, P.refl[1], P.refl[2], P.refl[3], a) end for i = 1, 34 do local h = hash32(i * 0x85EBCA6B) local y = HZ + 1 + umod(h, H - HZ - 2) local len = 8 + umod(h >> 8, 22) local x = umod(h >> 14, W + 8) - 8 local light = umod(h >> 20, 2) == 0 if light then blend_rect(x, y, len, 1, 255, 255, 255, 0.07) else blend_rect(x, y, len, 1, 0, 40, 70, 0.08) end end end local function stack(si) local s = STACKS[si] local T = TOPY[si] for x = s.xa + 1, s.xb - 1 do local y0 = floor(T[x] + 0.5) local h = s.base - y0 if h > 0 then local slope = (T[x + 1] - T[x - 1]) * 0.5 local lit0 = max(0, min(1, 0.06 + slope * 0.5)) local nb = min(7, h) for k = 0, nb - 1 do local ya, yb = y0 + h * k // nb, y0 + h * (k + 1) // nb local lit = max(0, min(1, lit0 + (noise((x + k * 1.7) / 2.1, 41 + si) - 0.5) * 0.3)) local r, g, b = mix(P.rockD, P.rockL, lit) local foot = max(0, (k + 0.5) / nb - 0.7) * 1.4 r, g, b = r + (P.mist[1] - r) * foot * 0.5, g + (P.mist[2] - g) * foot * 0.5, b + (P.mist[3] - b) * foot * 0.5 rect(x, ya, 1, yb - ya, rgb(r, g, b)) end if lit0 > 0.45 then px(x, y0, P.rim[1], P.rim[2], P.rim[3]) end end end local w = s.xb - s.xa for k = 0, 3 do blend_rect(s.xa - 1 - k, s.base - k + 1, w + 2 + 2 * k, 1, P.mist[1], P.mist[2], P.mist[3], 0.5 - k * 0.11) end for k = 1, 14 do local a = 0.5 * (1 - k / 15) blend_rect(s.xa + 1 + k // 4, s.base + k, w - 2 - k // 3, 1, P.rockD[1], P.rockD[2], P.rockD[3], a) end end local STAGES = { sky, sea, function() stack(1) end, function() stack(2) end, function() stack(3) end, } local STAGE = 1 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 BASE = snapshot(0, 0, W, H) end STAGE = STAGE + 1 end function on_settings() P = TONES[cfg.tone] or TONES.rose BASE, STAGE = nil, 1 end local CL, HB, GL, FS = {}, {}, {}, {} for i = 1, 9 do local h = hash32(i * 0x27D4EB2F) CL[i] = { y = 2 + (i - 1) * 2 + umod(h, 3), L = 18 + umod(h >> 4, 26), x0 = umod(h >> 9, 130), v = 1.2 + umod(h >> 15, 14) / 5 + (i - 1) * 0.3 } end for i = 1, 5 do local h = hash32(i * 0xC2B2AE35) HB[i] = { y = 27 + umod(h, 10), L = 18 + umod(h >> 6, 16), x0 = umod(h >> 10, 100), v = 0.9 + umod(h >> 16, 10) / 6 } end for i = 1, 12 do local h = hash32(i * 0x165667B1) GL[i] = { y = HZ + 2 + umod(h, 30), dx = umod(h >> 7, 100) / 100 - 0.5, w = 1.2 + umod(h >> 12, 8) * 0.3, ph = umod(h >> 18, 628) / 100 } end for i = 1, 6 do local h = hash32(i * 0x9E3779B1 + 7) FS[i] = { y = HZ + 6 + umod(h, 30), L = 10 + umod(h >> 6, 14), x0 = umod(h >> 10, 100), v = 1.4 + umod(h >> 16, 10) / 5 } end local function row(xl, xr, y, a, c) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, c[1], c[2], c[3], a) blend_rect(il, y, 1, 1, c[1], c[2], c[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, c[1], c[2], c[3], a * (xr - ir)) end function render() if not BASE then build() gfx.ready(false) return end local t = frame / FPS local k = 0.35 + 0.3 * (cfg.drift or 3) blit_rgb(BASE, 0, 0, W, H) for i = 1, 9 do local c = CL[i] local L = c.L local x = (c.x0 + t * c.v * k) % (W + 2 * L) - L row(x + L * 0.2, x + L * 0.8, c.y, 0.34, P.cT) row(x, x + L, c.y + 1, 0.4, P.cM) row(x + L * 0.1, x + L * 0.9, c.y + 2, 0.36, P.cB) if i > 4 then row(x + L * 0.3, x + L * 0.7, c.y + 3, 0.2, P.cB) end end for i = 1, 6 do local f = FS[i] local x = (f.x0 + t * f.v * k) % (W + 2 * f.L) - f.L row(x, x + f.L, f.y, 0.1, P.mist) end for i = 1, 10 do local g = GL[i] local a = sin(t * g.w + g.ph) if a > 0.2 then local hw = 1 + (g.y - HZ) * 0.34 blend_rect(floor(RX + g.dx * hw * 2 + 0.5), g.y, 2, 1, 255, 244, 232, (a - 0.2) * 0.5) end end local m = P.mist for i = 1, cfg.haze or 3 do local b = HB[i] local L = b.L local x = (b.x0 + t * b.v * k) % (W + 2 * L) - L row(x + L * 0.25, x + L * 0.75, b.y, 0.15, m) row(x, x + L, b.y + 1, 0.2, m) row(x - L * 0.1, x + L * 1.1, b.y + 2, 0.18, m) row(x + L * 0.15, x + L * 0.85, b.y + 3, 0.11, m) end end