info = { type = "image", title = "Iso cube", category = "patterns", author = "BRRRO", version = "1.0", license = "MIT", desc = "An isometric cube flips as a ball orbits it", api = 33, } local tr = ({ sk = { ["Cube colour"] = "Farba kocky", ["Ball colour"] = "Farba lopty", ["Background drift"] = "Pohyb pozadia" }, cs = { ["Cube colour"] = "Barva kostky", ["Ball colour"] = "Barva míče", ["Background drift"] = "Pohyb pozadí" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "cube", label = T("Cube colour"), type = "color", default = "#FFB028" }, { key = "ball", label = T("Ball colour"), type = "color", default = "#FF5C8A" }, { key = "drift", label = T("Background drift"), type = "int", min = 1, max = 4, default = 2 }, } local rect, blend_rect = gfx.rect, gfx.blend_rect local floor, ceil, sin, cos, sqrt, abs = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local OX, OY = 32, 36 local S = 9 local SE, CE = 0.57735, 0.81650 local C45 = 0.70711 local RO = 24 local RB = 6 local PW, PH = 24, 12 local GW, GH = 64 + PW, 64 + PH local GRID = nil local PAL = { 0, 0, 0, 22, 128, 160, 8, 62, 100 } local function build_grid() local rows = {} for y = 0, PH - 1 do local row = {} for x = 0, GW - 1 do row[x + 1] = 1 + (((x + 2 * y) // PW) + ((x - 2 * y) // PW)) % 2 end rows[y + 1] = string.char(table.unpack(row, 1, GW)) end local all = {} for y = 0, GH - 1 do all[y + 1] = rows[y % PH + 1] end GRID = table.concat(all) end local function vi(x, y, z) return (x > 0 and 4 or 0) + (y > 0 and 2 or 0) + (z > 0 and 1 or 0) + 1 end local VX, VY, VZ = {}, {}, {} for x = -1, 1, 2 do for y = -1, 1, 2 do for z = -1, 1, 2 do local i = vi(x, y, z) VX[i], VY[i], VZ[i] = x * S, y * S, z * S end end end local FACES = { { 0, 1, 0, vi(-1, 1, -1), vi(1, 1, -1), vi(1, 1, 1), vi(-1, 1, 1) }, { 0, -1, 0, vi(-1, -1, -1), vi(1, -1, -1), vi(1, -1, 1), vi(-1, -1, 1) }, { 1, 0, 0, vi(1, -1, -1), vi(1, 1, -1), vi(1, 1, 1), vi(1, -1, 1) }, { -1, 0, 0, vi(-1, -1, -1), vi(-1, 1, -1), vi(-1, 1, 1), vi(-1, -1, 1) }, { 0, 0, 1, vi(-1, -1, 1), vi(1, -1, 1), vi(1, 1, 1), vi(-1, 1, 1) }, { 0, 0, -1, vi(-1, -1, -1), vi(1, -1, -1), vi(1, 1, -1), vi(-1, 1, -1) }, } local CXS = { -S, -S, S, S } local CZS = { -S, S, S, -S } local cS, sS = 1, 0 local fk, cF, sF = 0, 1, 0 local CX, CZ = 0, 0 local PX, PY, PZ = 0, 0, 0 local function xf(x, y, z, pos) local x1 = x * cS + z * sS local z1 = -x * sS + z * cS local y1 = y if pos then x1, z1 = x1 + CX, z1 + CZ if fk ~= 0 then x1, y1, z1 = x1 - PX, y1 - PY, z1 - PZ end end if fk == 1 then y1, z1 = y1 * cF - z1 * sF, y1 * sF + z1 * cF elseif fk == 2 then x1, y1 = x1 * cF - y1 * sF, x1 * sF + y1 * cF end if pos and fk ~= 0 then x1, y1, z1 = x1 + PX, y1 + PY, z1 + PZ end local cx = (x1 + z1) * C45 local cz = (z1 - x1) * C45 return OX + cx, OY - y1 * CE + cz * SE, y1 * SE + cz * CE, y1 * CE - cz * SE, cx, x1, z1 end local PX_, PY_ = {}, {} local T1, T2, T3 = { 255, 176, 40 }, { 184, 126, 28 }, { 118, 80, 18 } local QX, QY = { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } local function quad(a, b, c, d, col) local ax, ay, bx, by, cx_, cy_, dx, dy = PX_[a], PY_[a], PX_[b], PY_[b], PX_[c], PY_[c], PX_[d], PY_[d] local ymin = ay; if by < ymin then ymin = by end; if cy_ < ymin then ymin = cy_ end; if dy < ymin then ymin = dy end local ymax = ay; if by > ymax then ymax = by end; if cy_ > ymax then ymax = cy_ end; if dy > ymax then ymax = dy end local xs, ys = QX, QY xs[1], xs[2], xs[3], xs[4], xs[5] = ax, bx, cx_, dx, ax ys[1], ys[2], ys[3], ys[4], ys[5] = ay, by, cy_, dy, ay local r, g, bl = col[1], col[2], col[3] for y = ceil(ymin - 0.5), floor(ymax - 0.5) do local yc = y + 0.5 local lo, hi = 1e9, -1e9 for e = 1, 4 do local y0, y1 = ys[e], ys[e + 1] local x0, x1 = xs[e], xs[e + 1] if y0 > y1 then y0, y1, x0, x1 = y1, y0, x1, x0 end if y0 <= yc and yc < y1 then local x = x0 + (x1 - x0) * (yc - y0) / (y1 - y0) if x < lo then lo = x end if x > hi then hi = x end end end if hi >= lo then local xa, xb = ceil(lo - 0.5), floor(hi - 0.5) if xb >= xa then rect(xa, y, xb - xa + 1, 1, r, g, bl) end end end end local LX, LY, LZ = -0.5, 0.7, 0.5 local function smooth(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function cube_state(t) local seg = floor(t / 2.5) % 4 local u = t % 2.5 CX, CZ = CXS[seg + 1], CZS[seg + 1] local spin = (PI / 2) * smooth((u - 0.3) / 0.9) cS, sS = cos(spin), sin(spin) local a = (PI / 2) * smooth((u - 1.5) / 0.7) fk = 0 if a > 0 then local th if seg == 0 then fk, th, PX, PY, PZ = 1, a, CX, -S, CZ + S elseif seg == 1 then fk, th, PX, PY, PZ = 2, -a, CX + S, -S, CZ elseif seg == 2 then fk, th, PX, PY, PZ = 1, -a, CX, -S, CZ - S else fk, th, PX, PY, PZ = 2, a, CX - S, -S, CZ end cF, sF = cos(th), sin(th) end if u >= 2.2 then fk = 0 CX, CZ = CXS[(seg + 1) % 4 + 1], CZS[(seg + 1) % 4 + 1] cS, sS = 1, 0 end end local function draw_cube() for i = 1, 8 do PX_[i], PY_[i] = xf(VX[i], VY[i], VZ[i], true) end for f = 1, 6 do local F = FACES[f] local _, _, nz, nup, nx = xf(F[1], F[2], F[3], false) if nz > 0.02 then local d = nx * LX + nup * LY + nz * LZ local col = d > 0.6 and T1 or (d > -0.12 and T2 or T3) quad(F[4], F[5], F[6], F[7], col) end end end local B1, B2, B3, B4 = { 255, 92, 138 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } local SH = { 0.55, 0.35 } local function disc(cx, cy, r, col) local y0, y1 = floor(cy - r + 0.5), ceil(cy + r - 0.5) - 1 for y = y0, y1 do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = sqrt(q) local xa, xb = ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if xb >= xa then rect(xa, y, xb - xa + 1, 1, col[1], col[2], col[3]) end end end end local function draw_ball(sx, sy) local cx, cy = floor(sx + 0.5), floor(sy + 0.5) disc(cx, cy, RB, B1) disc(cx - 0.9, cy - 1.0, RB - 1.6, B2) disc(cx - 1.6, cy - 1.9, RB - 3.2, B3) rect(cx - 3, cy - 3, 1, 1, B4[1], B4[2], B4[3]) rect(cx - 2, cy - 4, 1, 1, B4[1], B4[2], B4[3]) end local function shadow(sx, sy, hw, hh) for i = -hh + 1, hh - 1 do local w = floor(hw * sqrt(1 - (i / hh) * (i / hh)) + 0.5) if w > 0 then blend_rect(floor(sx) - w, floor(sy) + i, 2 * w, 1, 4, 20, 36, 0.38) end end end function on_settings() local c = cfg.cube or { r = 255, g = 176, b = 40 } T1 = { c.r, c.g, c.b } T2 = { c.r * 7 // 10, c.g * 7 // 10, c.b * 7 // 10 } T3 = { c.r * 44 // 100, c.g * 44 // 100, c.b * 44 // 100 } local b = cfg.ball or { r = 255, g = 92, b = 138 } B1 = { b.r * 45 // 100, b.g * 45 // 100, b.b * 45 // 100 } B2 = { b.r * 75 // 100, b.g * 75 // 100, b.b * 75 // 100 } B3 = { b.r, b.g, b.b } B4 = { b.r + (255 - b.r) * 6 // 10, b.g + (255 - b.g) * 6 // 10, b.b + (255 - b.b) * 6 // 10 } end local RATE = { 5, 10, 15, 30 } function render() if not GRID then build_grid() end local t = frame / FPS local d = floor(t * RATE[cfg.drift or 2]) gfx.blit_indexed(GRID, PAL, d % PW - PW, (d // 2) % PH - PH, GW, GH, GW, GH) cube_state(t) local ccx, ccy, ccz, _, _, wx, wz = xf(0, 0, 0, true) shadow(OX + (wx + wz) * C45, OY + S * CE + (wz - wx) * C45 * SE, 12, 4) local th = t * (2 * PI / 5.5) local v = (th * 5 / (2 * PI)) % 1 local hop = 6 * 4 * v * (1 - v) local bx, bz = RO * cos(th), RO * sin(th) local x1 = (bx + bz) * C45 local z1 = (bz - bx) * C45 local by = -S + RB local bsx, bsy = OX + x1, OY - (by + hop) * CE + z1 * SE local bdepth = (by + hop) * SE + z1 * CE local gsy = OY - (-S) * CE + z1 * SE shadow(bsx, gsy, 6 - hop * 0.3, 3) if bdepth < ccz then draw_ball(bsx, bsy) end draw_cube() if bdepth >= ccz then draw_ball(bsx, bsy) end end