info = { type = "image", title = "Matcha bowl", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "Whisked matcha in a glazed bowl, soft light", api = 44, } local tr = ({ sk = { ["Bowl colour"] = "Farba misky", ["Celadon"] = "Celadón", ["Indigo"] = "Indigo", ["Sakura"] = "Sakura", ["Rust"] = "Hrdzavá", ["Steam"] = "Para", ["Whisking"] = "Šľahanie" }, cs = { ["Bowl colour"] = "Barva misky", ["Celadon"] = "Celadon", ["Indigo"] = "Indigo", ["Sakura"] = "Sakura", ["Rust"] = "Rezavá", ["Steam"] = "Pára", ["Whisking"] = "Šlehání" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bowl", label = T("Bowl colour"), type = "int", default = "celadon", opts = { keys = { "celadon", "indigo", "sakura", "rust" }, T("Celadon"), T("Indigo"), T("Sakura"), T("Rust") } }, { key = "steam", label = T("Steam"), type = "int", min = 0, max = 12, default = 8 }, { key = "whisk", label = T("Whisking"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit, blit_rgb, snapshot = gfx.blit_indexed, gfx.blit_rgb, gfx.snapshot local floor, ceil, sin, cos, sqrt, min, max, abs = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.min, math.max, math.abs local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H = gfx.W, gfx.H local TAU = 6.28318 local BOWLS = { celadon = { { 0, 34, 86, 70 }, { 0.4, 98, 160, 134 }, { 0.75, 170, 214, 186 }, { 1, 238, 252, 240 } }, indigo = { { 0, 12, 22, 70 }, { 0.4, 36, 62, 142 }, { 0.75, 98, 132, 212 }, { 1, 214, 228, 255 } }, sakura = { { 0, 150, 64, 86 }, { 0.4, 232, 138, 160 }, { 0.75, 250, 196, 208 }, { 1, 255, 240, 240 } }, rust = { { 0, 62, 26, 16 }, { 0.4, 150, 70, 36 }, { 0.75, 214, 130, 72 }, { 1, 255, 220, 170 } }, } local TEA = { { 0, 24, 72, 20 }, { 0.5, 80, 150, 36 }, { 0.85, 150, 196, 60 }, { 1, 206, 228, 120 } } local BAMBOO = { { 0, 110, 72, 24 }, { 0.5, 196, 150, 64 }, { 0.85, 240, 206, 110 }, { 1, 255, 236, 170 } } local CLAY = { { 0, 80, 50, 38 }, { 1, 206, 160, 120 } } local function ramp(keys, t) for i = 1, #keys - 1 do local a, b = keys[i], keys[i + 1] if t <= b[1] then local u = (t - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * u, a[3] + (b[3] - a[3]) * u, a[4] + (b[4] - a[4]) * u end end local l = keys[#keys] return l[2], l[3], l[4] end local PAL = {} local function build_pal() PAL = { 0, 0, 0 } local function add(keys, n) for k = 0, n - 1 do local r, g, b = ramp(keys, k / (n - 1)) PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end end add(BOWLS[cfg.bowl] or BOWLS.celadon, 20) add(TEA, 12); add(BAMBOO, 12); add(CLAY, 8) PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = 70, 44, 30 end local LX, LY, LZ = 0.5, -0.55, 0.67 local HX, HY, HZ = 0.27, -0.32, 0.91 local function shade(nx, ny, nz, spec) local d = (nx * LX + ny * LY + nz * LZ + 0.3) / 1.3 if d < 0 then d = 0 end local t = 0.18 + 0.62 * d local s = nx * HX + ny * HY + nz * HZ if s > 0.8 then local s2 = s * s; s2 = s2 * s2; s2 = s2 * s2; s2 = s2 * s2 t = t + spec * 0.6 * s2 end t = t * (0.74 + 0.26 * nz) if t > 1 then t = 1 elseif t < 0 then t = 0 end return floor(t * 19 + 0.5) end local function clamp(v, a, b) return v < a and a or (v > b and b or v) end local BW, BH = 36, 30 local BGRID, BOWL = {}, nil local function bowl_rows(y0, y1) for y = y0, y1 do local yy = y + 0.5 for x = 0, BW - 1 do local dx = x + 0.5 - 18 local u = dx / 17.5 local c = 0 local fx, fy = dx / 8.2, (yy - 27.4) / 2.3 if fx * fx + fy * fy <= 1 then c = 45 + clamp(floor(3.5 + 3.5 * fx - 1.5 * fy), 0, 7) end local v = (yy - 6) / 21 if v >= 0 and abs(u) ^ 2.6 + v ^ 2.6 <= 1 then local nz = sqrt(max(0, 1 - u * u - 0.6 * v * v)) c = 1 + shade(u, v * 0.7, nz, 1) if v > 0.5 and v < 0.56 then c = max(1, c - 4) end end local vr = (yy - 6) / 5.2 if u * u + vr * vr <= 1 then local ri, vi = dx / 16, (yy - 6) / 4.7 if ri * ri + vi * vi > 1 then c = 1 + clamp(floor(14 + 5 * u + (yy < 6 and 1 or 0)), 0, 19) else local te, ve = dx / 16, (yy - 7.6) / 4.7 if te * te + ve * ve <= 1 then local n = umod(hash32(x * 73 + y * 1009), 5) - 2 c = 21 + clamp(floor(3 + 7 * clamp((yy - 4) / 6.5, 0, 1) + 1.5 * u) + (n > 1 and 1 or 0), 0, 11) else local yt = (yy - 0.8) / 6.8 c = 1 + clamp(floor(15 - 8 * yt + 3 * u), 3, 18) end end end BGRID[y * BW + x + 1] = c end end end local function bowl_done() local rows = {} for y = 0, BH - 1 do rows[y + 1] = char(unpack(BGRID, y * BW + 1, y * BW + BW)) end BOWL = concat(rows) end local KW, KH = 26, 34 local WHISK = {} local function whisk_pose(k) local phi = (k - 5) * 3.5 * 0.01745 local c, s = cos(phi), sin(phi) local g = {} for i = 1, KW * KH do g[i] = 0 end for ti = 0, 42 do local t = ti * 0.5 local hw = t < 8 and (2.2 + (8 - t) * 0.3) or 2.2 for j = 0, floor(hw * 4) do local u = -hw + j * 0.5 local rel = u / hw local idx if t >= 8 and t < 9.5 then idx = 53 elseif t < 8 then idx = 33 + clamp(4 + floor(5 * (0.5 + 0.5 * rel)) - (floor(u * 1.5) % 2 == 0 and 2 or 0), 0, 11) elseif t > 20 then idx = 34 else idx = 33 + clamp(floor(2 + 8 * (0.5 + 0.5 * rel) ^ 1.3) + (abs(rel - 0.4) < 0.15 and 2 or 0), 0, 11) end local x, y = floor(13 + u * c + t * s), floor(32 - t * c + u * s) if x >= 0 and x < KW and y >= 0 and y < KH then g[y * KW + x + 1] = idx end end end local rows = {} for y = 0, KH - 1 do rows[y + 1] = char(unpack(g, y * KW + 1, y * KW + KW)) end WHISK[k] = concat(rows) end local function blob(cx, cy, rx, ry, r, g, b, a) for y = floor(cy - ry), ceil(cy + ry) - 1 do local v = (y + 0.5 - cy) / ry if v > -1 and v < 1 then local hw = rx * sqrt(1 - v * v) local x0 = floor(cx - hw + 0.5) local w = floor(cx + hw + 0.5) - x0 if a then blend_rect(x0, y, w, 1, r, g, b, a) else rect(x0, y, w, 1, r, g, b) end end end end local STAGES = { function() for y = 0, 23 do local t = y / 23 rect(0, y, W, 1, floor(86 + 24 * t), floor(66 + 20 * t), floor(56 + 14 * t)) end rect(31, 0, 33, 24, 120, 84, 56) for y = 2, 21 do local t = (y - 2) / 19 rect(33, y, 30, 1, 255, floor(244 - 18 * t), floor(218 - 38 * t)) end for _, x in ipairs({ 40, 47, 54 }) do rect(x, 2, 1, 20, 150, 108, 70) end for _, y in ipairs({ 9, 15 }) do rect(33, y, 30, 1, 150, 108, 70) end blend_rect(28, 0, 36, 25, 255, 226, 176, 0.06) rect(0, 24, W, 1, 70, 44, 30) for y = 25, H - 1 do local t = (y - 25) / 38 rect(0, y, W, 1, floor(150 - 50 * t), floor(104 - 38 * t), floor(66 - 22 * t)) end for k = 1, 16 do local h = hash32(k * 0x9E3779B1) blend_rect(umod(h, 50), 26 + umod(h >> 8, 36), 8 + umod(h >> 16, 18), 1, 56, 30, 18, 0.14) end end, function() blob(21, 59, 21, 4.5, 20, 10, 6, 0.14) blob(21, 59, 17, 3.5, 20, 10, 6, 0.14) blob(21, 59, 12, 2.6, 20, 10, 6, 0.14) rect(2, 46, 44, 16, 206, 170, 98) for x = 3, 43, 4 do rect(x, 46, 2, 16, 190, 150, 80) end rect(2, 46, 44, 1, 238, 206, 138) rect(2, 61, 44, 1, 150, 112, 60) rect(2, 46, 2, 16, 46, 70, 52) rect(44, 46, 2, 16, 46, 70, 52) rect(4, 50, 40, 1, 110, 80, 46) rect(4, 58, 40, 1, 110, 80, 46) blend_rect(2, 62, 44, 1, 20, 10, 6, 0.3) for x = 46, 62 do local y = floor(55 - (x - 46) * 0.37) rect(x, y + 2, 1, 1, 110, 70, 30) rect(x, y, 1, 2, 214, 170, 84) rect(x, y, 1, 1, 250, 220, 140) end blob(47, 56, 2.2, 1.6, 214, 170, 84) blob(53, 47, 8, 3.2, 28, 20, 26) blob(53, 46.2, 7, 2.5, 52, 38, 46) blob(53, 44.2, 4.8, 3.6, 214, 112, 144) blob(54, 43.4, 3.6, 2.7, 250, 164, 188) blob(55, 42.6, 1.8, 1.2, 255, 226, 232) rect(52, 40, 3, 1, 70, 150, 70) rect(54, 39, 2, 1, 96, 180, 80) end, function() for y = 26, 62 do local t = (y - 26) / 36 local a = 0.2 * (1 - 0.8 * t) * min(1, (y - 25) / 5) local xl = 40 - (y - 26) * 0.5 for j = 0, 3 do local x = floor(xl + j * 1.6) blend_rect(x, y, W - x, 1, 255, 226, 168, a / 4) end for _, xb in ipairs({ 40, 47, 54 }) do blend(floor(xb - (y - 26) * 0.5), y, 60, 30, 10, 0.16 * (1 - 0.7 * t)) end end for _, y in ipairs({ 32, 40 }) do blend_rect(floor(40 - (y - 26) * 0.5), y, 30, 1, 60, 30, 10, 0.1) end end, function() bowl_rows(0, 7) end, function() bowl_rows(8, 14) end, function() bowl_rows(15, 22) end, function() bowl_rows(23, 29); bowl_done() end, } local SNAP, STAGE, WN = nil, 1, 0 function on_settings() build_pal() SNAP, STAGE = nil, 1 end local CYCLE = 600 local function foam_at(t) if t < 60 then return 0.18 end if t < 360 then return 0.18 + 0.82 * (t - 60) / 300 end if t < 450 then return 1 end if t < 540 then return 1 - 0.82 * (t - 450) / 90 end return 0.18 end local function amp_at(t) if t < 60 or t >= 360 then return 0 end if t < 90 then return (t - 60) / 30 end if t >= 330 then return (360 - t) / 30 end return 1 end local function splat(x, y, r, g, b, a) local x0, y0 = floor(x), floor(y) local fx, fy = x - x0, y - y0 blend(x0, y0, r, g, b, a * (1 - fx) * (1 - fy)) blend(x0 + 1, y0, r, g, b, a * fx * (1 - fy)) blend(x0, y0 + 1, r, g, b, a * (1 - fx) * fy) blend(x0 + 1, y0 + 1, r, g, b, a * fx * fy) end local STEAM = {} for i = 1, 12 do local h = hash32(i * 0x85EBCA6B) STEAM[i] = { x = 15 + umod(h, 18), ph = umod(h >> 6, 628) / 100, off = umod(h >> 12, 120), w = 0.04 + umod(h >> 20, 3) / 100 } end local MOTES = {} for i = 1, 6 do local h = hash32(i * 0xC2B2AE35) MOTES[i] = { x = 36 + umod(h, 26), y = 22 + umod(h >> 8, 26), ph = umod(h >> 16, 628) / 100, w = 0.025 + umod(h >> 22, 3) / 200 } end function render() if STAGE <= #STAGES then if SNAP then blit_rgb(SNAP, 0, 0, W, H) end STAGES[STAGE]() if STAGE <= 3 then SNAP = snapshot(0, 0, W, H) end STAGE = STAGE + 1 gfx.ready(false) return end if WN < 9 then WN = WN + 1; whisk_pose(WN); if WN < 5 then gfx.ready(false) end end local t = frame % CYCLE blit_rgb(SNAP, 0, 0, W, H) for i = 1, #MOTES do local m = MOTES[i] splat(m.x + 3 * sin(frame * m.w + m.ph), m.y + 4 * sin(frame * m.w * 0.7 + m.ph * 2), 255, 240, 200, 0.35 + 0.3 * sin(frame * 0.05 + m.ph)) end blit(BOWL, PAL, 6, 32, BW, BH, BW, BH, true) local F = foam_at(t) blob(24, 39.4, 13.6, 3, 196, 226, 112, 0.5 * F) blob(23, 39, 9, 2, 220, 240, 150, 0.4 * F) for k = 1, floor(12 * F) do local h = hash32(k * 977 + (frame // 10) * 31) local a = umod(h, 628) / 100 local r = umod(h >> 8, 100) / 100 px(floor(24 + 12 * r * cos(a)), floor(39.4 + 2.6 * r * sin(a)), 236, 250, 190) end local A = cfg.whisk and amp_at(t) or 0 local k = 5 + floor(4 * A * (sin(frame * TAU * 3.2 / 30) + 0.25 * sin(frame * TAU * 7.1 / 30)) / 1.25 + 0.5) local wk = WHISK[clamp(k, 1, 9)] or WHISK[5] if wk then blit(wk, PAL, 11, 9, KW, KH, KW, KH, true) end for i = 1, cfg.steam or 8 do local s = STEAM[i] local age = (frame + s.off * 7) % 130 local u = age / 130 local y = 37 - 28 * u local x = s.x + 4 * sin(frame * s.w + s.ph) * (0.4 + u) splat(x, y, 250, 252, 246, 0.34 * sin(u * 3.14159) ^ 1.3) splat(x + 1, y, 250, 252, 246, 0.16 * sin(u * 3.14159)) end end