info = { type = "image", title = "Bamboo canopy", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Looking up through bamboo with drifting light", api = 44, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Morning"] = "Ráno", ["Noon"] = "Poludnie" }, cs = { ["Time of day"] = "Denní doba", ["Morning"] = "Ráno", ["Noon"] = "Poledne" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tod", label = T("Time of day"), type = "int", default = 0, opts = { T("Morning"), T("Noon") } }, } local blend, blend_mask, blit_rgb = gfx.blend, gfx.blend_mask, gfx.blit_rgb local floor, ceil, sqrt, sin, cos, atan, abs = math.floor, math.ceil, math.sqrt, math.sin, math.cos, math.atan, math.abs local hash32, schar, unpack, concat = math.hash32, string.char, table.unpack, table.concat local W, H = gfx.W, gfx.H local VPY = -8 local PAL = { { g = { { 255, 238, 180 }, { 178, 208, 92 }, { 70, 114, 60 }, { 22, 40, 36 } }, lt = { 170, 202, 112 }, md = { 70, 108, 54 }, ray = { 255, 242, 196 }, rs = 0.32, fog = 0.42 }, { g = { { 232, 255, 150 }, { 186, 224, 56 }, { 72, 136, 34 }, { 16, 40, 16 } }, lt = { 192, 216, 72 }, md = { 78, 118, 44 }, ray = { 244, 255, 176 }, rs = 0.2, fog = 0 }, } local DK = { 18, 32, 16 } local NS, A0 = 24, -1.2 local DA = -2 * A0 / NS local SPH, SHW, SKF, STONE = {}, {}, {}, {} for k = 1, NS do local h = hash32(k * 0x9E3779B1) SPH[k] = A0 + DA * (k - 1 + 0.5 + ((h & 255) / 255 - 0.5) * 0.7) SHW[k] = 0.022 + 0.02 * (((h >> 8) & 255) / 255) SKF[k] = 190 + 90 * (((h >> 16) & 255) / 255) STONE[k] = 0.8 + 0.4 * (((h >> 24) & 127) / 127) end local function clamp01(v) return v < 0 and 0 or (v > 1 and 1 or v) end local function lerp(a, b, f) return a + (b - a) * f end local GR, GG, GB, SR_, SG_, SB_ = {}, {}, {}, {}, {}, {} local function luts(P) local g = P.g for n = 0, 127 do local t = n / 127 local i = t < 0.3 and 1 or (t < 0.65 and 2 or 3) local f = i == 1 and t / 0.3 or (i == 2 and (t - 0.3) / 0.35 or (t - 0.65) / 0.35) GR[n], GG[n], GB[n] = lerp(g[i][1], g[i + 1][1], f), lerp(g[i][2], g[i + 1][2], f), lerp(g[i][3], g[i + 1][3], f) end for n = 0, 32 do local lit = n / 32 local lt, md = P.lt, P.md local q = lit < 0.5 and lit * 2 or (lit - 0.5) * 2 local lo, hi = lit < 0.5 and DK or md, lit < 0.5 and md or lt SR_[n], SG_[n], SB_[n] = lerp(lo[1], hi[1], q), lerp(lo[2], hi[2], q), lerp(lo[3], hi[3], q) end end local function bg_px(x, y, dy) local dx = x + 0.5 - 32 local rho = sqrt(dx * dx + dy * dy) local a = atan(dx, dy) local t = (rho - 8) / 70 t = t < 0 and 0 or (t > 1 and 1 or t) local n = (t * 127) // 1 local r, gg, b = GR[n], GG[n], GB[n] local k0 = ((a - A0) // DA) + 1 local cov, cr, cg, cb = 0, 0, 0, 0 for k = (k0 > 1 and k0 - 1 or 1), (k0 < NS and k0 + 1 or NS) do local d = a - SPH[k] local c = SHW[k] * rho - (d < 0 and -d or d) * rho + 0.5 if c > cov then cov = c > 1 and 1 or c local s = d / SHW[k] local lit = 0.5 - 0.5 * s * (SPH[k] < 0 and -1 or 1) local li = (lit < 0 and 0 or (lit > 1 and 1 or lit)) * 32 // 1 local fr = (SKF[k] / rho) % 1 local m = STONE[k] * (fr < 0.09 and 0.55 or (fr < 0.14 and 0.8 or 1)) * ((s > 0.85 or s < -0.85) and 0.72 or 1) local haze = 0.55 * (1 - t) * (1 - t) cr, cg, cb = SR_[li] * m + (r - SR_[li] * m) * haze, SG_[li] * m + (gg - SG_[li] * m) * haze, SB_[li] * m + (b - SB_[li] * m) * haze end end local dens = (36 - rho) / 28 dens = dens < 0 and 0 or (dens > 1 and 1 or dens) if dens > 0 then local cell = (hash32(((x + 0.5 * y) // 1.6 | 0) * 131 + (y // 1.6 | 0) * 7919) & 255) * 0.00392157 local lr, lg, lb if cell < 0.5 * dens then lr, lg, lb = 34 + 30 * cell, 76 + 50 * cell, 28 elseif cell < 0.62 * dens then lr, lg, lb = 150, 196, 64 elseif cell > 1 - 0.16 * dens then lr, lg, lb = 214, 238, 240 end if lr then r, gg, b = r + (lr - r) * dens, gg + (lg - gg) * dens, b + (lb - b) * dens cov = cov * (1 - dens * 0.85) end end r, gg, b = r + (cr - r) * cov, gg + (cg - gg) * cov, b + (cb - b) * cov return r, gg, b, t, a, cov, dens end local function row_px(y, P) local dy = y + 0.5 - VPY local row, rr, n = {}, {}, 0 for x = 0, W - 1 do local r, g, b, t, a, cov, dens = bg_px(x, y, dy) if P.fog > 0 and t > 0.5 then local fa = P.fog * (t - 0.5) * 1.4 r, g, b = r + (190 - r) * fa, g + (208 - g) * fa, b + (200 - b) * fa end local ray = (0.5 + 0.5 * sin(a * 19 + 0.8)) * (0.5 + 0.5 * sin(a * 47 + 2.0)) local u = 1 - t local fade = sqrt(dy * dy + (x - 31.5) * (x - 31.5)) / 16 - 0.625 fade = (fade < 0 and 0 or (fade > 1 and 1 or fade)) * u * u local rv = ray * ray * fade * (1 - cov) * (1 - dens * 0.4) row[n + 1], row[n + 2], row[n + 3] = (r + 0.5) // 1, (g + 0.5) // 1, (b + 0.5) // 1 rr[x + 1] = (rv * 255) // 1 n = n + 3 end return schar(unpack(row, 1, n)), schar(unpack(rr)) end local ROWS, RROWS, BG, RAYS, BUILT = {}, {}, nil, nil, 0 local RPC = 2 local function build_chunk(P) if BUILT == 0 then luts(P) end local part, rpart = {}, {} local n = BUILT + RPC > H and H - BUILT or RPC for y = BUILT, BUILT + n - 1 do part[#part + 1], rpart[#rpart + 1] = row_px(y, P) end local s = concat(part) ROWS[#ROWS + 1] = s RROWS[#RROWS + 1] = concat(rpart) blit_rgb(s, 0, BUILT, W, n) BUILT = BUILT + n if BUILT >= H then BG, RAYS = concat(ROWS), concat(RROWS) end end function on_settings() ROWS, RROWS, BG, RAYS, BUILT = {}, {}, nil, nil, 0 end local CL = {} do for k = 1, 18 do local h = hash32(k * 0x85EBCA6B) CL[k] = { x = -3 + ((h & 255) / 255) * 62, y = 1 + ((h >> 8) & 255) / 255 * 21, shape = 1 + (h >> 16) % 3, tone = (h >> 20) % 3, ph = ((h >> 24) & 127) / 127 } end end local TONES = { { 156, 204, 66 }, { 62, 116, 38 }, { 214, 236, 120 } } local MASKS = {} local built_now = false local function cmask(shape, q) local key = shape * 4 + q local m = MASKS[key] if m then return m end if built_now then return nil end built_now = true local rows = {} local rx, ry = 2.6 + shape * 0.7, 1.7 + shape * 0.4 for j = 0, 7 do local row = {} for i = 0, 11 do local dx, dy = (i + 0.5 - 6 - q / 4) / rx, (j + 0.5 - 4) / ry local jag = 1 + 0.25 * sin((i + j * 2 + shape) * 1.9) local d = sqrt(dx * dx + dy * dy) * jag row[i + 1] = floor(clamp01(1.4 - d) * 255) end rows[j + 1] = schar(unpack(row)) end m = concat(rows) MASKS[key] = m return m end local function draw_canopy(t) local sway = 1.0 * sin(t * 0.0349) + 0.6 * sin(t * 0.0816 + 1) for k = 1, 18 do local c = CL[k] local x = c.x + sway * (0.8 + 0.4 * c.ph) local ix = floor(x) local q = floor((x - ix) * 4) local m = cmask(c.shape, q) if not m then for kk = 0, 3 do m = m or MASKS[c.shape * 4 + kk] end end if m then local tc = TONES[c.tone + 1] blend_mask(m, tc[1], tc[2], tc[3], ix - 6, floor(c.y) - 4, 12, 8, 12, 8, 0.5) end end end local NL = 5 local LF = {} for k = 1, NL do local h = hash32(k * 0xC2B2AE35) LF[k] = { x = 6 + ((h & 255) / 255) * 52, vy = 0.13 + ((h >> 8) & 127) / 127 * 0.17, off = ((h >> 16) & 255) / 255 * 100, ph = ((h >> 24) & 127) / 127 * 6.28, near = k % 2 == 0 } end local function dot(x, y, r, g, b, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, r, g, b, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, r, g, b, a * fx * (1 - fy)) blend(ix, iy + 1, r, g, b, a * (1 - fx) * fy) blend(ix + 1, iy + 1, r, g, b, a * fx * fy) end local function draw_leaves(t) for k = 1, NL do local l = LF[k] local y = (t * l.vy + l.off) % (H + 24) - 12 local x = l.x + 4 * sin(t * 0.03 + l.ph) + t * 0.025 x = (x + 8) % (W + 16) - 8 local ang = 0.9 * sin(t * 0.05 + l.ph * 2) local ca, sa = sin(ang), cos(ang) local len = l.near and 3 or 2 for i = 0, len do local c = l.near and 0.9 or 0.65 dot(x + ca * (i - len / 2), y + sa * (i - len / 2), l.near and 214 or 176, l.near and 236 or 210, l.near and 92 or 70, c) end end end function render() local P = PAL[(cfg.tod or 0) + 1] or PAL[1] built_now = false if not BG then build_chunk(P); if not BG then gfx.ready(false) end return end blit_rgb(BG, 0, 0, W, H) local rs = P.rs * (0.75 + 0.2 * sin(frame * 0.021) + 0.08 * sin(frame * 0.047 + 1)) blend_mask(RAYS, P.ray[1], P.ray[2], P.ray[3], 0, 0, W, H, W, H, rs) draw_canopy(frame) draw_leaves(frame) end