import functools import math import os import sys import numpy as np from PIL import Image sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import vox from vox import P, hash3 G = 'env' PREVIEW = '/tmp/claude-1000/-home-tahinli-Documents-Code-Rust/edd994b8-9b3e-47f3-95c7-9404536bd326/scratchpad/env' FIRE = P['fire'] SLAG = [(28, 22, 22), (52, 40, 36), (78, 60, 50), (116, 76, 56)] EMBER = [(60, 20, 14), (110, 34, 20), (150, 50, 26), (180, 70, 34)] BIRCH = [(70, 66, 60), (150, 144, 136), (200, 194, 182), (232, 228, 216)] BG = (34, 32, 38, 255) RES = {} MC = {} def mt(ramp, pat=None, em=False): if isinstance(ramp, str): ramp = P[ramp] key = (tuple(ramp), em, pat) if key not in MC: assert len(vox._MATS) < 255, 'vox material ids exhausted' MC[key] = vox.mat(list(ramp), pat, em) return MC[key] @functools.lru_cache(None) def const(k): return lambda cx, cy, cz, f: k @functools.lru_cache(None) def sprinkle(s, p, a): return lambda cx, cy, cz, f: np.where(hash3(cx, cy, 0, s) < p, a, 0) def ring(pat=None): return pat R0 = ring() def gh(s, n=32): gx, gy = np.indices((n, n)) return hash3(gx, gy, 0, s) def wrap_disc(gx, gy, centers, shrink=0.0): out = np.zeros(gx.shape, bool) for px, py, r in centers: for ox in (-32, 0, 32): for oy in (-32, 0, 32): out |= (gx - px - ox) ** 2 + (gy - py - oy) ** 2 <= (r - shrink) ** 2 return out def top(f): return (f == 2).astype(np.int64) class Cv: def __init__(self, sx, sy, sz, off=(0, 0)): self.m = vox.Model(sx, sy, sz) self.o = off def p(self, a): return (a[0] + self.o[0], a[1] + self.o[1], a[2]) def box(self, a, b, m): pa, pb = self.p(a), self.p(b) self.m.box(pa[0], pa[1], pa[2], pb[0], pb[1], pb[2], m) def ell(self, c, r, m): pc = self.p(c) self.m.ellipsoid(pc[0], pc[1], pc[2], r[0], r[1], r[2], m) def cap(self, a, b, r, m): self.m.capsule(self.p(a), self.p(b), r, m) def cyl(self, a, b, r, m): self.m.cylinder(self.p(a), self.p(b), r, m) def gable(self, x0, y0, x1, y1, z0, z1, axis, m, ov=0): ox, oy = self.o self.m.gable(x0 + ox, y0 + oy, x1 + ox, y1 + oy, z0, z1, axis, m, ov) def finish(name, cv, outline=True, edges=True): img, em, org = vox.render(cv.m, outline, edges) anchor = [int(round(a)) for a in vox.project(cv.o[0], cv.o[1], 0, org)] if not edges: bb = img.getbbox() img = img.crop(bb) anchor = [anchor[0] - bb[0], anchor[1] - bb[1]] anchor = tuple(anchor) vox.save(name, img, anchor, emissive=em, group=G) RES[name] = (img, anchor) def add(name, img, origin): vox.save(name, img, origin, group=G) RES[name] = (img, origin) def tile(name, base, fix=None, h=3): cv = Cv(32, 32, h) cv.m.v[:, :, 0] = base if fix: fix(cv.m.v) finish(name, cv, outline=False, edges=False) def brick(cx, cy, w, h, stag): row = cy // h x = cx + (row * stag) % w return x % w, cy % h, (x // w) % (32 // w), row % (32 // h) def paving(name, ramp, joint, w, h, stag, s, var): def pat(cx, cy, cz, f): lx, ly, sx, row = brick(cx, cy, w, h, stag) sid = hash3(sx, row, 0, s) v = np.floor(sid * (2 * var + 1)).astype(np.int64) - var return v + ((lx == 1) | (ly == 1)) - ((lx == w - 1) | (ly == h - 1)) gx, gy = np.indices((32, 32)) lx, ly, _, _ = brick(gx, gy, w, h, stag) jm = (lx == 0) | (ly == 0) jmat = mt(joint, ring(sprinkle(s + 3, 0.12, -1))) def fix(v): v[:, :, 0][jm] = jmat tile(name, mt(ramp, ring(pat)), fix) def grass(i): def pat(cx, cy, cz, f): return sprinkle(20 + i, 0.03, -1)(cx, cy, cz, f) + sprinkle(30 + i, 0.02, 1)(cx, cy, cz, f) tuft = mt('grass', const(1)) cream = mt('cloth_cream') wine = mt('cloth_wine') def fix(v): m = gh(40 + i) < 0.012 v[:, :, 1][m] = tuft if i == 1: v[:, :, 2][m & (gh(49) < 0.5)] = tuft if i == 3: fl = gh(41) < 0.012 c = gh(42) < 0.5 v[:, :, 1][fl & c] = cream v[:, :, 1][fl & ~c] = wine tile(f'grass{i}', mt('grass', ring(pat)), fix) def road(i): def pat(cx, cy, cz, f): return np.where(hash3(cx // 4, cy // 4, 0, 30 + i) < 0.25, -1, 0) + sprinkle(40 + i, 0.03, 1)(cx, cy, cz, f) stone = mt('stone') def fix(v): v[:, :, 1][gh(50 + i) < 0.01] = stone tile(f'road{i}', mt('dirt', pat), fix) def soot(i): def pat(cx, cy, cz, f): p = sprinkle(40 + i, 0.05, 1)(cx, cy, cz, f) if i == 1: p = p + np.where(hash3(cx // 8, cy // 8, 0, 44) < 0.4, -1, 0) return p mud = mt('mud', R0) ember = mt(EMBER, R0) lump = mt('coal', const(2)) def fix(v): gx, gy = np.indices((32, 32)) if i == 2: v[:, :, 0][hash3(gx // 4, gy // 4, 0, 45) < 0.2] = mud v[:, :, 0][gh(50 + i) < 0.004] = ember if i == 3: v[:, :, 1][gh(60) < 0.012] = lump tile(f'soot{i}', mt('coal', ring(pat)), fix) def mud_pat(s): def f(cx, cy, cz, face): return np.where(hash3(cx // 4, cy // 4, 0, s) < 0.35, -1, 0) + sprinkle(s + 9, 0.05, 1)(cx, cy, cz, face) return f def mud(i): cen = [(6, 8, 4), (22, 20, 5), (12, 27, 3)] if i == 0 else [(20, 6, 3), (6, 21, 4)] pud = mt('water', ring(const(-1))) rim = mt('water', R0) base = mt('mud', ring(mud_pat(50 + i))) def fix(v): gx, gy = np.indices((32, 32)) + 0.5 v[:, :, 0][wrap_disc(gx, gy, cen)] = rim v[:, :, 0][wrap_disc(gx, gy, cen, 1.2)] = pud tile(f'mud{i}', base, fix) def farm(i): gx, gy = np.indices((32, 32)) along = gy per = 4 if i == 0 else 8 ridge = (along % per) < per // 2 rmat = mt('dirt', R0) sprout = mt('grass', const(1)) def fix(v): v[:, :, 1][ridge] = rmat v[:, :, 1][(~ridge) & (gh(90 + i) < 0.08 + 0.08 * i)] = sprout tile(f'farmland{i}', mt('dirt', ring(const(-1))), fix) def water(i): def pat(cx, cy, cz, f): rip = (cx % 4 != 3) & (hash3(cx // 4, cy, 0, 70 + i) < 0.1) return rip.astype(np.int64) + sprinkle(80 + i, 0.04, -1)(cx, cy, cz, f) tile(f'water{i}', mt('water', ring(pat))) def rail(): gx, gy = np.indices((32, 32)) cv = Cv(32, 32, 3) cv.m.v[:, :, 0] = mt('coal', ring(sprinkle(95, 0.1, 1))) cv.m.v[:, :, 0][(gx % 8 < 2) & (gy >= 7) & (gy < 25)] = mt('wood_dark', R0) cv.m.v[:, 11, 1] = mt('metal') cv.m.v[:, 20, 1] = mt('metal') finish('rail_x', cv, outline=False, edges=False) cv2 = Cv(32, 32, 3) cv2.m.v = np.ascontiguousarray(cv.m.v.transpose(1, 0, 2)) finish('rail_y', cv2, outline=False, edges=False) def tiles(): for i in range(4): grass(i) road(i) soot(i) paving('cobble0', 'cobble', 'stone_dark', 8, 4, 4, 11, 1) paving('cobble1', 'cobble', 'stone_dark', 8, 8, 4, 12, 1) paving('cobble2', 'cobble', 'stone_dark', 4, 4, 2, 13, 1) paving('cobble3', 'cobble', 'stone_dark', 8, 4, 0, 14, 1) paving('flag0', 'stone', 'stone_dark', 8, 8, 0, 21, 0) paving('flag1', 'stone', 'stone_dark', 16, 8, 8, 22, 1) paving('flag2', 'stone', 'stone_dark', 8, 8, 4, 23, 1) paving('flag3', 'stone', 'stone_dark', 16, 16, 8, 24, 1) mud(0) mud(1) farm(0) farm(1) water(0) water(1) tile('planks', mt('wood', ring(vox.planks(4, 0)))) rail() def weave(cx, cy, cz, f): return np.where(((cx + cz) % 4 == 0) | ((cy + cz) % 4 == 0), -1, 0) def leaf_pat(s): def f(cx, cy, cz, face): blob = np.where(hash3(cx // 3, cy // 3, cz // 3, s) < 0.3, -1, 0) return blob + top(face) return f def bark_pat(s): def f(cx, cy, cz, face): return np.where(hash3(cx // 2, cy // 2, cz // 3, s) < 0.4, -1, 0) + top(face) return f def rock_pat(s): def f(cx, cy, cz, face): return sprinkle(s, 0.06, -1)(cx, cy, cz, face) + sprinkle(s + 1, 0.05, 1)(cx, cy, cz, face) + top(face) return f def crate_pat(x0, y0, w, d): def f(cx, cy, cz, face): edge = (cx == x0) | (cx == x0 + w - 1) | (cy == y0) | (cy == y0 + d - 1) return edge.astype(np.int64) - (cz % 6 == 0).astype(np.int64) return f def crate(cv, x0, y0, z0, w, d, h): cv.box((x0, y0, z0), (x0 + w, y0 + d, z0 + h), mt('wood', crate_pat(x0, y0, w, d))) def barrel(cv, cx, cy, h=22, r=6.5): cv.ell((cx, cy, h / 2), (r, r, h / 2), mt('wood', vox.rows(4))) for z in (4, h - 6): cv.cyl((cx, cy, z), (cx, cy, z + 1), r + 0.7, mt('metal')) cv.cyl((cx, cy, h - 1), (cx, cy, h), r * 0.6, mt('wood_dark')) def sack(cv, cx, cy, sc): m = mt('canvas', weave) cv.ell((cx, cy, 7 * sc), (8 * sc, 6.5 * sc, 7 * sc), m) cv.cyl((cx, cy, 13 * sc), (cx, cy, 15 * sc), 2.4 * sc, m) def bale_pat(cx, cy, cz, f): rope = (cx == 9) | (cx == 22) | (cz == 4) | (cz == 10) return np.where(rope, -2, 0) + sprinkle(7, 0.08, 1)(cx, cy, cz, f) def coal_pat(cx, cy, cz, f): return sprinkle(111, 0.08, 1)(cx, cy, cz, f) + sprinkle(112, 0.08, -1)(cx, cy, cz, f) + top(f) def slag_pat(cx, cy, cz, f): return sprinkle(120, 0.02, 1)(cx, cy, cz, f) + sprinkle(121, 0.1, -1)(cx, cy, cz, f) + top(f) def cross_pat(cx, cy, cz, f): c = ((cx == 15) & (cz >= 7) & (cz <= 11)) | ((cz == 9) & (cx >= 14) & (cx <= 17)) return np.where(c, -1, 0) + top(f) def birch_pat(cx, cy, cz, f): return np.where(hash3(cx, cy, cz // 2, 311) < 0.15, -2, 0) + top(f) def p_crate(): cv = Cv(32, 32, 16) crate(cv, 5, 5, 0, 22, 22, 14) finish('crate', cv) def p_crates(): cv = Cv(32, 32, 28) crate(cv, 2, 4, 0, 14, 14, 12) crate(cv, 16, 14, 0, 14, 14, 12) crate(cv, 4, 6, 12, 11, 11, 10) finish('crates', cv) def p_barrel(): cv = Cv(32, 32, 24) barrel(cv, 16, 16) finish('barrel', cv) def p_barrels(): cv = Cv(32, 32, 24) barrel(cv, 9, 9) barrel(cv, 23, 10) barrel(cv, 16, 23) finish('barrels', cv) def p_sack(): cv = Cv(32, 32, 18) sack(cv, 16, 16, 1.0) finish('sack', cv) def p_sacks(): cv = Cv(32, 32, 18) sack(cv, 8, 9, 1.0) sack(cv, 22, 10, 1.0) sack(cv, 15, 22, 0.8) finish('sacks', cv) def p_bale(): cv = Cv(32, 32, 20) cv.box((3, 7, 0), (29, 25, 15), mt('cloth_cream', bale_pat)) finish('bale', cv) def p_haystack(): cv = Cv(32, 32, 28) cv.ell((16, 16, 0), (14, 14, 13), mt('thatch', lambda cx, cy, cz, f: np.where((cx + 2 * cz) % 5 == 0, -1, 0) + sprinkle(8, 0.06, 1)(cx, cy, cz, f))) finish('haystack', cv) def p_logs(): cv = Cv(32, 32, 22) bark = mt('bark', bark_pat(600)) cut = mt('wood', R0) for y, z in ((7, 3.5), (15, 3.5), (23, 3.5), (11, 9.6), (19, 9.6), (27, 9.6), (15, 15.7)): cv.cyl((2, y, z), (30, y, z), 3.5, bark) cv.cyl((2, y, z), (3.6, y, z), 2.6, cut) cv.cyl((28.4, y, z), (30.5, y, z), 2.6, cut) finish('logs', cv) def fence_cv(): cv = Cv(32, 32, 22) cv.box((0, 27, 0), (2, 31, 20), mt('wood_dark')) cv.box((0, 28, 6), (32, 30, 9), mt('wood', vox.rows(4))) cv.box((0, 28, 13), (32, 30, 16), mt('wood', vox.rows(4))) return cv def p_fences(): cv = fence_cv() finish('fence_x', cv) cv2 = Cv(32, 32, 22) cv2.m.v = np.ascontiguousarray(cv.m.v.transpose(1, 0, 2)) finish('fence_y', cv2) def p_lamp(): cv = Cv(32, 32, 94) iron = mt('slate') cv.cyl((16, 16, 0), (16, 16, 4), 3.6, iron) cv.cyl((16, 16, 4), (16, 16, 8), 2.4, iron) cv.cyl((16, 16, 8), (16, 16, 76), 1.2, iron) cv.cyl((16, 16, 76), (16, 16, 78), 2.2, iron) cv.box((11, 11, 78), (21, 21, 80), iron) cv.box((12, 12, 80), (20, 20, 88), mt('window', None, True)) for x, y in ((12, 12), (20, 12), (12, 20), (20, 20)): cv.cyl((x, y, 80), (x, y, 88), 0.7, iron) cv.box((9, 9, 88), (23, 23, 90), iron) cv.box((12, 12, 90), (20, 20, 92), iron) cv.cyl((16, 16, 92), (16, 16, 94), 0.9, iron) finish('lamp', cv) def p_well(): cv = Cv(32, 32, 46) cv.cyl((16, 16, 0), (16, 16, 12), 9.5, mt('stone', vox.bricks(4, 8))) cv.cyl((16, 16, 8), (16, 16, 13), 7.0, 0) cv.cyl((16, 16, 7), (16, 16, 8), 6.5, mt('water')) cv.box((6, 15, 12), (8, 17, 32), mt('wood')) cv.box((24, 15, 12), (26, 17, 32), mt('wood')) cv.cyl((6, 16, 24), (26, 16, 24), 1.0, mt('wood_dark')) cv.gable(4, 6, 28, 26, 30, 38, 'x', mt('wood_dark', vox.rows(3)), 1) finish('well', cv) def p_bench(): cv = Cv(32, 32, 26) cv.box((4, 11, 11), (28, 21, 13), mt('wood', vox.planks(3, 2))) cv.box((4, 11, 0), (6, 21, 11), mt('wood_dark')) cv.box((26, 11, 0), (28, 21, 11), mt('wood_dark')) cv.box((4, 9, 13), (28, 11, 22), mt('wood', vox.rows(4))) finish('bench', cv) def p_table(): cv = Cv(32, 32, 24) cv.box((7, 9, 17), (25, 23, 19), mt('wood', vox.planks(4, 2))) cv.cyl((16, 16, 0), (16, 16, 17), 1.6, mt('wood_dark')) cv.cyl((16, 16, 0), (16, 16, 1), 6.0, mt('wood_dark')) for x in (5, 27): cv.cyl((x, 16, 0), (x, 16, 9), 3.0, mt('wood')) cv.cyl((x, 16, 9), (x, 16, 10), 3.4, mt('wood_dark')) finish('table', cv) def p_signpost(): cv = Cv(32, 32, 34) cv.box((15, 15, 0), (17, 17, 34), mt('wood_dark')) cv.box((17, 14, 22), (28, 18, 27), mt('wood', vox.rows(4))) cv.box((4, 14, 12), (15, 18, 17), mt('wood', vox.rows(4))) finish('signpost', cv) def p_stall(): cv = Cv(64, 32, 42) for x in (2, 59): for y in (2, 26): cv.box((x, y, 0), (x + 3, y + 3, 33), mt('wood_dark')) cv.box((5, 6, 10), (59, 24, 15), mt('wood', vox.planks(4, 2))) cv.box((12, 10, 15), (20, 20, 20), mt('cloth_cream', weave)) cv.box((0, 0, 33), (64, 30, 36), mt('cloth_wine')) cream = mt('cloth_cream') for x in range(64): if (x // 8) % 2: cv.m.v[x, 0:30, 33:36] = cream finish('stall', cv) def p_coalpile(): cv = Cv(32, 32, 20) coal = mt('coal', coal_pat) cv.ell((16, 16, 0), (14, 12, 9), coal) cv.ell((9, 20, 0), (7, 6, 6), coal) cv.ell((23, 10, 0), (6, 6, 5), coal) finish('coalpile', cv) def p_slag(): cv = Cv(64, 64, 34) m = mt(SLAG, slag_pat) cv.ell((32, 32, 0), (28, 25, 15), m) cv.ell((14, 44, 0), (10, 9, 8), m) cv.ell((50, 14, 0), (9, 10, 8), m) finish('slag', cv) def p_tub(): cv = Cv(32, 32, 26) cv.box((5, 9, 6), (27, 23, 18), mt('slate')) cv.box((5, 9, 17), (27, 23, 19), mt('metal')) cv.box((7, 11, 19), (25, 21, 22), mt('coal', coal_pat)) for x in (8, 24): for y in (11, 20): cv.cyl((x, y - 0.5, 5), (x, y + 1.5, 5), 3.0, mt('metal')) finish('tub', cv) def p_flowers(i): rng = np.random.default_rng(3 + i) cv = Cv(32, 32, 6) cv.box((2, 2, 0), (30, 30, 4), mt('dirt', sprinkle(70 + i, 0.1, 1))) cols = [mt('cloth_wine'), mt('cloth_cream'), mt('window')] if i == 0 else [mt('cloth_navy'), mt('cloth_cream'), mt('cloth_wine')] leaf = mt('grass') for _ in range(70): x, y = rng.integers(3, 29, 2) cv.m.v[x, y, 4] = leaf for _ in range(26): x, y = rng.integers(3, 29, 2) cv.m.v[x, y, 4] = leaf cv.m.v[x, y, 5] = cols[rng.integers(3)] finish(f'flowers{i}', cv) def p_bush(i): cv = Cv(32, 32, 24) leaf = mt('foliage' if i == 0 else 'foliage_dark', leaf_pat(130 + i)) for x, y, z, r in ((16, 16, 9, 9), (10, 14, 7, 6), (22, 17, 7, 6), (15, 22, 6, 5)): cv.ell((x, y, z), (r, r, r * 0.8), leaf) finish(f'bush{i}', cv) def p_rock(i): cv = Cv(32, 32, 16) m = mt('stone', rock_pat(140 + i)) if i == 0: cv.ell((16, 16, 6), (10, 8, 6.5), m) else: cv.ell((12, 14, 5), (8, 7, 5.5), m) cv.ell((22, 20, 3.5), (6, 5, 3.5), m) finish(f'rock{i}', cv) def p_grave(): cv = Cv(32, 32, 22) cv.ell((16, 16, 0), (12, 9, 2.5), mt('mud', mud_pat(160))) cv.box((12, 13, 0), (20, 19, 4), mt('stone_dark')) head = mt('stone', cross_pat) cv.box((12, 14, 4), (20, 18, 15), head) cv.ell((16, 16, 15), (4, 2, 3.5), head) finish('grave', cv) def p_campfire(): cv = Cv(32, 32, 8) stone = mt('stone', rock_pat(150)) cv.ell((16, 16, 0.5), (4, 4, 1), mt('coal')) for k in range(12): a = 2 * math.pi * k / 12 cv.ell((16 + 9 * math.cos(a), 16 + 9 * math.sin(a), 1.5), (3.2, 2.8, 2.0), stone) cv.cyl((8, 12, 2.5), (24, 20, 2.5), 1.6, mt('bark')) cv.cyl((8, 20, 2.5), (24, 12, 2.5), 1.6, mt('bark')) finish('campfire', cv) def p_flame(i): cv = Cv(32, 32, 22) ms = [mt([c] * 4, None, True) for c in ((255, 120, 30), (255, 170, 50), (255, 226, 120), (255, 250, 210))] gx, gy, gz = np.meshgrid(np.arange(32) + 0.5, np.arange(32) + 0.5, np.arange(22) + 0.5, indexing='ij') ph = 1.9 * i cxf = 16 + 0.7 * np.sin(gz * 0.5 + ph) * gz / 22 cyf = 16 + 0.5 * np.cos(gz * 0.4 + ph) * gz / 22 r = 5.6 * np.clip(1 - gz / 22, 0, 1) ** 0.55 * (1 + 0.2 * np.sin(gz * 1.1 + 2 * ph)) + 0.4 d = np.hypot(gx - cxf, gy - cyf) / r v = cv.m.v v[d < 1] = ms[0] v[d < 0.65] = ms[1] v[d < 0.35] = ms[2] v[(d < 0.5) & (gz > 13)] = ms[3] finish(f'flame{i}', cv, outline=False) def p_tent(): cv = Cv(64, 32, 44) canvas = mt('canvas', vox.rows(6)) cv.gable(4, 3, 60, 29, 0, 40, 'x', canvas) cv.m.v[26:38, 18:29, 0:15] = 0 cv.m.v[26:38, 12:18, 0:15] = mt('cloth_black') cv.cap((4, 16, 41), (60, 16, 41), 0.9, mt('wood')) leather = mt('leather') cv.cap((20, 16, 40), (14, 1, 0), 0.5, leather) cv.cap((44, 16, 40), (50, 1, 0), 0.5, leather) finish('tent', cv) def tree_cv(): return Cv(128, 128, 176, (48, 48)) def p_oak(i): rng = np.random.default_rng(200 + i) cv = tree_cv() bark = mt('bark', bark_pat(210 + i)) cv.cap((16, 16, 0), (16, 16, 62), 3.4, bark) for _ in range(5): a = rng.uniform(0, 2 * math.pi) cv.cap((16, 16, 48 + rng.uniform(0, 10)), (16 + 24 * math.cos(a), 16 + 24 * math.sin(a), 92 + rng.uniform(0, 14)), 2.0, bark) light = mt('foliage', leaf_pat(220 + i)) dark = mt('foliage_dark', leaf_pat(230 + i)) for _ in range(14 + 4 * i): a = rng.uniform(0, 2 * math.pi) r = 28 * math.sqrt(rng.uniform()) rad = rng.uniform(12, 18) c = (16 + r * math.cos(a), 16 + r * math.sin(a), 102 + rng.normal(0, 12)) cv.ell(c, (rad, rad, rad * 0.85), light if rng.random() < 0.6 else dark) finish(f'oak{i}', cv) def p_birch(): rng = np.random.default_rng(301) cv = tree_cv() cv.cap((16, 16, 0), (16, 16, 95), 2.3, mt(BIRCH, birch_pat)) light = mt('foliage') dark = mt('foliage_dark') for _ in range(34): a = rng.uniform(0, 2 * math.pi) r = 22 * math.sqrt(rng.uniform()) rad = rng.uniform(6, 10) c = (16 + r * math.cos(a), 16 + r * math.sin(a), 110 + rng.normal(0, 14)) cv.ell(c, (rad, rad, rad * 1.1), light if rng.random() < 0.7 else dark) finish('birch', cv) def p_pine(): cv = tree_cv() cv.cap((16, 16, 0), (16, 16, 110), 2.6, mt('bark')) dark = mt('foliage_dark', leaf_pat(402)) for t in range(8): r = 36 - 4.6 * t cv.ell((16, 16, 24 + 15 * t), (r, r, 5.5 + 0.3 * (7 - t)), dark) cv.cap((16, 16, 120), (16, 16, 148), 1.8, dark) finish('pine', cv) def p_dead(): rng = np.random.default_rng(501) cv = tree_cv() bark = mt('bark', bark_pat(502)) def br(p, d, L, r, depth): q = p + d * L cv.cap(p, q, r, bark) if depth > 0: for _ in range(int(rng.integers(2, 4))): nd = d + rng.normal(0, 0.8, 3) nd /= np.linalg.norm(nd) br(q, nd, L * 0.68, r * 0.7, depth - 1) br(np.array([16.0, 16.0, 0.0]), np.array([0.0, 0.0, 1.0]), 46, 3.0, 4) finish('dead', cv) def small(): n = 12 yy, xx = np.mgrid[0:n, 0:n] + 0.5 a = np.zeros((n, n)) for dx, dy, r in ((-1.8, 0.8, 4.2), (1.6, -0.6, 4.6), (0.0, 2.2, 3.6), (0.0, -1.8, 3.2)): d = np.hypot(xx - n / 2 - dx, yy - n / 2 - dy) / r a = np.maximum(a, np.clip(1 - d, 0, 1) ** 0.6) g = (120 + 60 * a).astype(np.uint8) add('puff', Image.fromarray(np.dstack([g, g, g, (a * 230).astype(np.uint8)])), (n // 2, n // 2)) W, H = 96, 48 yy, xx = np.mgrid[0:H, 0:W] + 0.5 d = np.hypot((xx - W / 2) / (W / 2), (yy - H / 2) / (H / 2)) al = np.clip(1 - d, 0, 1) ** 2 rgba = np.dstack([np.full((H, W), 255.0), np.full((H, W), 236.0), np.full((H, W), 190.0), al * 255]).astype(np.uint8) add('light', Image.fromarray(rgba), (W // 2, H // 2)) e = np.zeros((1, 5, 4), np.uint8) e[0, 0] = e[0, 3] = (255, 214, 40, 255) add('eyes', Image.fromarray(e), (2, 0)) def props(): p_crate() p_crates() p_barrel() p_barrels() p_sack() p_sacks() p_bale() p_haystack() p_logs() p_fences() p_lamp() p_well() p_bench() p_table() p_signpost() p_stall() p_coalpile() p_slag() p_tub() p_flowers(0) p_flowers(1) p_bush(0) p_bush(1) p_rock(0) p_rock(1) p_grave() p_campfire() for i in range(4): p_flame(i) p_tent() for i in range(3): p_oak(i) p_birch() p_pine() p_dead() small() def save_scaled(img, path, k=2): bg = Image.new('RGBA', img.size, BG) bg.alpha_composite(img.convert('RGBA')) bg.resize((bg.width * k, bg.height * k), Image.NEAREST).save(path) def tile_block(names): cv = Image.new('RGBA', (260, 150), BG) for s in range(7): for i in range(4): j = s - i if not 0 <= j < 4: continue img, org = RES[names[(i + j) % len(names)]] cv.alpha_composite(img, (130 + 32 * (i - j) - org[0], 8 + 16 * (i + j) - org[1])) return cv def preview(): os.makedirs(PREVIEW, exist_ok=True) items = list(RES.values()) rows = [items[k:k + 9] for k in range(0, len(items), 9)] out, _, _, _ = vox.atlas(rows) save_scaled(out, os.path.join(PREVIEW, 'sheet.png')) fams = [ ['grass0', 'grass1', 'grass2', 'grass3'], ['road0', 'road1', 'road2', 'road3'], ['cobble0', 'cobble1', 'cobble2', 'cobble3'], ['flag0', 'flag1', 'flag2', 'flag3'], ['soot0', 'soot1', 'soot2', 'soot3'], ['mud0', 'mud1'], ['farmland0', 'farmland1'], ['water0', 'water1'], ['planks'], ['rail_x'], ['rail_y'], ] per = 6 sheet = Image.new('RGBA', (260 * per, 150 * ((len(fams) + per - 1) // per)), BG) for k, names in enumerate(fams): sheet.paste(tile_block(names), ((k % per) * 260, (k // per) * 150)) save_scaled(sheet, os.path.join(PREVIEW, 'tiles4.png')) def main(): tiles() props() preview() if __name__ == '__main__': main()