import math import os import sys import numpy as np from PIL import Image sys.path.insert(0, os.path.dirname(__file__)) from vox import P, Model, atlas, mat, planks, project, render, save PREV = '/tmp/claude-1000/-home-tahinli-Documents-Code-Rust/edd994b8-9b3e-47f3-95c7-9404536bd326/scratchpad/chr' D = math.radians TAU = 2 * math.pi ANK = 3.0 LEG = 10.5 HIP_Y = 3.0 ARM_U = 8.5 ARM_F = 8.0 N_H, NZ_H = 48, 56 N_W, NZ_W = 60, 36 N_C, NZ_C = 48, 36 BOOT_A = np.array([-1.4, 0.0, -1.6]) BOOT_B = np.array([3.2, 0.0, -1.6]) n1 = None pl = planks() MAT_DEFS = { 'skin': ('skin', None), 'coal': ('coal', None), 'hair_gray': ('hair_gray', None), 'hair_brown': ('hair_brown', None), 'hair_black': ('hair_black', None), 'cloth_gray': ('cloth_gray', n1), 'cloth_brown': ('cloth_brown', n1), 'cloth_navy': ('cloth_navy', n1), 'cloth_cream': ('cloth_cream', n1), 'cloth_green': ('cloth_green', n1), 'cloth_wine': ('cloth_wine', n1), 'cloth_black': ('cloth_black', n1), 'plaster': ('plaster', n1), 'leather': ('leather', None), 'metal': ('metal', None), 'wood': ('wood', pl), 'wood_dark': ('wood_dark', pl), 'canvas': ('canvas', n1), 'stone': ('stone', n1), 'stone_dark': ('stone_dark', n1), } M = {k: mat(P[r], pt) for k, (r, pt) in MAT_DEFS.items()} def Rz(a): c, s = math.cos(a), math.sin(a) return np.array([[c, -s, 0.0], [s, c, 0.0], [0.0, 0.0, 1.0]]) def Ry(a): c, s = math.cos(a), math.sin(a) return np.array([[c, 0.0, s], [0.0, 1.0, 0.0], [-s, 0.0, c]]) class Xf: def __init__(self, O=(0, 0, 0), R=None): self.O = np.asarray(O, float) self.R = np.eye(3) if R is None else R def p(self, q): return self.O + self.R @ np.asarray(q, float) def at(self, O, R): return Xf(self.p(O), self.R @ R) class Fr: def __init__(self, n, nz, yaw, s=1.0): self.m = Model(n, n, nz) self.n = n self.c = np.array([n / 2, n / 2, 0.0]) self.Rz = Rz(yaw) self.s = s def W(self, p): return self.c + self.s * (self.Rz @ np.asarray(p, float)) def cap(self, a, b, r, m): self.m.capsule(self.W(a), self.W(b), r * self.s, m) def cyl(self, a, b, r, m): self.m.cylinder(self.W(a), self.W(b), r * self.s, m) def solid(self, xf, c, h, m, box=False): ctr = self.W(xf.p(c)) Rw = self.Rz @ xf.R h = np.asarray(h, float) * self.s ext = np.abs(Rw) @ h if box else np.sqrt(((Rw * h) ** 2).sum(1)) def fn(X, Y, Z): d = [X - ctr[0], Y - ctr[1], Z - ctr[2]] q = [sum(Rw[i, j] * d[i] for i in range(3)) for j in range(3)] if box: return (abs(q[0]) <= h[0]) & (abs(q[1]) <= h[1]) & (abs(q[2]) <= h[2]) return sum((q[j] / h[j]) ** 2 for j in range(3)) <= 1 self.m._fill(ctr - ext, ctr + ext, fn, m) def ell(self, xf, c, r, m): self.solid(xf, c, r, m) def box(self, xf, c, h, m): self.solid(xf, c, h, m, True) def finish(fr, n): img, _, origin = render(fr.m) fx, fy = project(n / 2, n / 2, 0, origin) return img, (int(round(fx)), int(round(fy))) def yaw_of(d): a = math.radians((d - 5) * 45) sx, sy = math.cos(a), math.sin(a) wx, wy = (sx - 2 * sy) / 2, (-sx - 2 * sy) / 2 return math.atan2(wy, wx) def gait(ph, amp, kmax): if ph is None: return 0.0, 0.0 th = D(amp) * math.sin(ph) k = D(5) + (D(kmax) - D(5)) * max(math.cos(ph), 0.0) ** 1.5 return th, k def leg_pose(hip, th, k, l): a = np.array([math.sin(th), 0.0, -math.cos(th)]) b = np.array([math.sin(th - k), 0.0, -math.cos(th - k)]) knee = hip + l * a ankle = knee + l * b drop = l * (math.cos(th) + math.cos(th - k)) return knee, ankle, drop def ik(S, T, l1, l2, pole): v = T - S L = float(np.linalg.norm(v)) d = min(L, l1 + l2 - 1e-3) u = v / L a = (l1 * l1 - l2 * l2 + d * d) / (2 * d) h = math.sqrt(max(l1 * l1 - a * a, 0.0)) perp = pole - S perp = perp - (perp @ u) * u perp = perp / np.linalg.norm(perp) return S + a * u + h * perp STYLES = [ dict(mode='swing', amp=22, kmax=35, lean=0, arm=22, hp=0), dict(mode='carry', amp=16, kmax=25, lean=-3, arm=0, hp=0), dict(mode='push', amp=30, kmax=28, lean=12, arm=0, hp=0), ] DEFAULT = dict(hair='cloth_gray', vest=None, roll=False, skirt=None, short=False, coat=False, extras=(), scale=1.0, age=False, boots=True, legs='cloth_navy') LOOKS = { 'player': dict(head='cap', hair='cloth_gray', torso='cloth_cream', vest='cloth_brown', sleeve='cloth_cream', legs='cloth_navy', extras=('satchel',)), 'gent': dict(head='top', hair='cloth_black', torso='cloth_black', sleeve='cloth_black', legs='cloth_gray', coat=True), 'grocer': dict(head='bald', hair='hair_gray', torso='cloth_cream', vest='cloth_brown', sleeve='cloth_cream', roll=True, legs='cloth_brown', extras=('apron',)), 'woman': dict(head='bonnet', torso='cloth_wine', sleeve='cloth_wine', legs='cloth_wine', skirt='cloth_wine', boots=False, extras=('shawl',)), 'barmaid': dict(head='bun', hair='hair_brown', torso='cloth_green', sleeve='cloth_green', legs='cloth_green', skirt='cloth_green', boots=False, extras=('apron',)), 'teacher': dict(head='hair', hair='hair_gray', torso='cloth_brown', sleeve='cloth_brown', legs='cloth_gray', coat=True, extras=('spectacles',)), 'old': dict(head='hair', hair='hair_gray', torso='cloth_gray', sleeve='cloth_gray', legs='cloth_black', coat=True, age=True), 'worker': dict(head='cap', hair='cloth_brown', torso='cloth_cream', vest='cloth_gray', sleeve='cloth_cream', legs='cloth_brown', extras=('braces',)), 'miner': dict(head='helmet', hair='cloth_black', torso='cloth_black', sleeve='cloth_black', legs='cloth_black', extras=('dust',)), 'peddler': dict(head='hair', hair='hair_black', torso='cloth_brown', sleeve='cloth_brown', legs='cloth_gray', extras=('pack', 'patch')), 'bandit': dict(head='tricorn', hair='cloth_black', torso='cloth_black', sleeve='cloth_black', legs='cloth_navy', coat=True, extras=('scarf',)), 'boy': dict(head='cap', hair='cloth_brown', torso='cloth_cream', sleeve='cloth_cream', legs='cloth_brown', short=True, scale=0.68), } def human_skel(st, p): thL, kL = gait(p, st['amp'], st['kmax']) thR, kR = gait(None if p is None else p + math.pi, st['amp'], st['kmax']) dL = LEG * (math.cos(thL) + math.cos(thL - kL)) dR = LEG * (math.cos(thR) + math.cos(thR - kR)) hz = ANK + max(dL, dR) xf = Xf((0, 0, hz), Ry(D(st['lean']))) legs = [] for s, th, k in ((1, thL, kL), (-1, thR, kR)): hip = np.array([0.0, s * HIP_Y, hz]) knee, ankle, _ = leg_pose(hip, th, k, LEG) legs.append((hip, knee, ankle)) arms = [] for s in (1, -1): S = xf.p((0, s * 4.8, 9.5)) if st['mode'] == 'swing': if p is None: phi, e = D(2.5), D(6) else: phi, e = -s * D(st['arm']) * math.sin(p), D(15) E = S + ARM_U * np.array([math.sin(phi), 0.0, -math.cos(phi)]) H = E + ARM_F * np.array([math.sin(phi + e), 0.0, -math.cos(phi + e)]) else: if st['mode'] == 'carry': H = np.array([6.0, s * 5.0, hz + 3.0]) else: H = np.array([12.0, s * 4.0, hz + 1.0]) E = (S + H) / 2 + np.array([0.0, 0.0, -2.0]) arms.append((S, E, H)) return dict(hz=hz, xf=xf, legs=legs, arms=arms, p=p) def draw_skirt(fr, m, hz, p): sx = 0.5 * math.cos(p) if p is not None else 0.0 sy = 0.9 * math.sin(p) if p is not None else 0.0 z0, z1, n = 0.8, hz - 0.5, 6 for i in range(n): za = z0 + (z1 - z0) * i / n zb = z0 + (z1 - z0) * (i + 1) / n r = 7.2 - 2.9 * ((za + zb) / 2 - z0) / (z1 - z0) fr.cyl((sx, sy, za), (sx, sy, zb), r, m) def draw_head(fr, hx, L): hair = M[L['hair']] kind = L['head'] if kind == 'cap': fr.ell(hx, (-1.0, 0, 1.2), (4.3, 4.3, 1.4), hair) fr.cyl(hx.p((0, 0, 2.6)), hx.p((0, 0, 5.1)), 4.6, hair) fr.box(hx, (5.5, 0, 2.4), (1.5, 3.4, 0.3), hair) elif kind == 'top': fr.cyl(hx.p((0, 0, 3.9)), hx.p((0, 0, 4.6)), 5.2, hair) fr.cyl(hx.p((0, 0, 4.5)), hx.p((0, 0, 10.5)), 3.4, hair) elif kind in ('hair', 'bun'): fr.ell(hx, (-0.8, 0, 0.9), (3.8, 3.8, 4.1), hair) if kind == 'bun': fr.ell(hx, (-3.6, 0, 0.3), (1.9, 1.9, 1.9), hair) elif kind == 'bald': fr.ell(hx, (-1.9, 0, 2.8), (2.2, 2.9, 1.6), hair) elif kind == 'bonnet': fr.ell(hx, (-1.6, 0, 0.4), (4.6, 4.6, 4.4), M['plaster']) elif kind == 'helmet': fr.ell(hx, (-0.3, 0, 1.2), (4.0, 3.9, 3.7), M['leather']) fr.box(hx, (3.3, 0, 1.8), (1.5, 3.0, 0.3), M['leather']) elif kind == 'tricorn': fr.ell(hx, (0, 0, 2.8), (3.8, 3.8, 2.2), hair) fr.box(hx, (0, 0, 2.0), (4.8, 4.6, 0.35), hair) def draw_human(fr, L, sk, st): hz, xf, p = sk['hz'], sk['xf'], sk['p'] dress = L['skirt'] is not None lg = M[L['legs']] tor = M[L['torso']] sl = M[L['sleeve']] skin = M['skin'] ext = L['extras'] for hip, knee, ankle in sk['legs']: fr.cap(hip, knee, 2.0, lg) fr.cap(knee, ankle, 1.9, skin if L['short'] else lg) if L['boots'] and not dress: fr.cap(ankle + BOOT_A, ankle + BOOT_B, 1.4, M['leather']) fr.ell(Xf((0, 0, hz)), (0, 0, 1.0), (3.4, 4.6, 2.8), M[L['skirt']] if dress else lg) if dress: draw_skirt(fr, M[L['skirt']], hz, p) fr.ell(xf, (0, 0, 7.0), (3.6, 5.0, 7.0), tor) if L['coat']: fr.ell(xf, (-0.3, 0, -3.0), (3.9, 5.2, 5.8), tor) if L['vest']: fr.ell(xf, (0, 0, 6.8), (3.8, 5.2, 5.8), M[L['vest']]) if 'shawl' in ext: fr.ell(xf, (0.3, 0, 11.0), (4.6, 6.9, 2.8), M['cloth_cream']) if 'satchel' in ext: fr.box(xf, (-0.5, 6.9, 0.8), (1.8, 1.1, 3.0), M['leather']) fr.cap(xf.p((2.4, -5.8, 10.8)), xf.p((3.8, 0, 6.0)), 0.6, M['leather']) fr.cap(xf.p((3.8, 0, 6.0)), xf.p((2.4, 5.8, 1.5)), 0.6, M['leather']) if 'braces' in ext: for s in (1, -1): fr.cap(xf.p((3.0, s * 3.4, -1.0)), xf.p((0, s * 5.6, 10.8)), 0.45, M['leather']) fr.cap(xf.p((0, s * 5.6, 10.8)), xf.p((-3.0, s * 3.4, -1.0)), 0.45, M['leather']) if 'patch' in ext: fr.box(xf, (0.5, 5.5, 3.0), (1.5, 0.4, 1.4), M['cloth_cream']) fr.box(xf, (0.5, -5.2, 6.0), (1.2, 0.4, 1.2), M['cloth_gray']) if 'pack' in ext: fr.box(xf, (-5.6, 0, 6.5), (2.3, 4.6, 5.0), M['canvas']) if 'apron' in ext: fr.box(xf, (5.3 if dress else 4.4, 0, 2.5), (0.7, 4.3, 4.6), M['plaster']) for S, E, H in sk['arms']: fr.cap(S, E, 1.6, sl) fr.cap(E, H, 1.5, skin if L['roll'] else sl) fr.cap(H, H, 1.4, skin) fr.cap(xf.p((0, 0, 10.0)), xf.p((0, 0, 12.6)), 1.5, skin) hp = D(st['hp']) hx = xf.at(np.array([0.0, 0.0, 12.0]) + Ry(hp) @ np.array([0.0, 0.0, 6.0]), Ry(hp)) fr.ell(hx, (0, 0, 0), (4.2, 4.2, 4.8), skin) for y in (1.25, -1.25): fr.box(hx, (4.1, y, -0.2), (0.45, 0.45, 0.45), M['coal']) fr.box(hx, (4.3, 0, -0.9), (0.4, 0.35, 0.4), skin) draw_head(fr, hx, L) if 'spectacles' in ext: fr.box(hx, (4.3, 1.7, 0.0), (0.3, 0.6, 0.5), M['metal']) fr.box(hx, (4.3, -1.7, 0.0), (0.3, 0.6, 0.5), M['metal']) fr.box(hx, (4.4, 0, 0.3), (0.25, 1.0, 0.15), M['metal']) if 'dust' in ext: fr.box(hx, (3.8, 2.4, -1.6), (0.5, 0.7, 0.7), M['coal']) fr.box(hx, (3.8, -2.4, -1.6), (0.5, 0.7, 0.7), M['coal']) if 'scarf' in ext: fr.box(hx, (3.8, 0, -2.4), (1.0, 3.6, 1.2), M['cloth_wine']) def human_frame(look, pose, d, col): L = dict(DEFAULT) L.update(look) st = dict(STYLES[pose]) if L['age']: st['amp'] *= 0.7 st['kmax'] *= 0.8 st['arm'] *= 0.6 st['lean'] += 18 st['hp'] = 12 fr = Fr(N_H, NZ_H, yaw_of(d), L['scale']) p = None if col == 0 else TAU * (col - 1) / 8 sk = human_skel(st, p) if pose == 1: fr.box(Xf(), (8.0, 0.0, sk['hz'] + 6.0), (6.0, 5.0, 4.0), M['plaster']) draw_human(fr, L, sk, st) return finish(fr, N_H) def build_human(look): rows = [] for pose in range(3): for d in range(8): rows.append([human_frame(look, pose, d, c) for c in range(9)]) return atlas(rows) def wolf_frame(d, col): fr = Fr(N_W, NZ_W, yaw_of(d)) trot = col > 0 p = TAU * (col - 1) / 6 if trot else None phB = None if p is None else p + math.pi legs_spec = [(10.0, 2.5, p), (10.0, -2.5, phB), (-10.0, 2.5, phB), (-10.0, -2.5, p)] gaits = [gait(ph, 25, 40) for _, _, ph in legs_spec] drops = [7.0 * (math.cos(th) + math.cos(th - k)) for th, k in gaits] hz = 0.9 + max(drops) zc = hz + 0.9 body = M['stone'] dark = M['stone_dark'] fr.ell(Xf(), (0, 0, zc), (15.0, 5.0, 5.5), body) for (x, y, _), (th, k) in zip(legs_spec, gaits): hip = np.array([x, y, hz]) knee, ankle, _ = leg_pose(hip, th, k, 7.0) fr.cap(hip, knee, 1.1, body) fr.cap(knee, ankle, 0.9, dark) fr.cap(ankle + np.array([-0.5, 0, 0]), ankle + np.array([1.5, 0, 0]), 0.9, dark) sh = -1.0 if trot else 0.0 fr.cap((-14.0, 0, zc + 1.0), (-18.5, 0, zc - 1.5), 2.0, body) fr.cap((-18.5, 0, zc - 1.5), (-21.0, 0, zc - 3.5), 1.6, body) fr.cap((12.0, 0, zc + 1.5), (15.5, 0, zc + 4.0 + sh), 2.6, body) fr.ell(Xf(), (17.5, 0, zc + 4.5 + sh), (3.6, 2.7, 2.7), body) fr.cap((19.5, 0, zc + 4.0 + sh), (24.5, 0, zc + 3.4 + sh), 1.3, dark) for s in (1, -1): fr.cap((16.5, s * 1.4, zc + 6.8 + sh), (16.0, s * 1.3, zc + 9.2 + sh), 0.8, dark) fr.box(Xf(), (20.2, s * 1.4, zc + 4.6 + sh), (0.35, 0.35, 0.35), M['coal']) return finish(fr, N_W) def wheel(fr, y, phi): C = np.array([0.0, y, 10.6]) iron, wood = M['metal'], M['wood'] pts = [C + 9.2 * np.array([math.cos(TAU * k / 16), 0.0, math.sin(TAU * k / 16)]) for k in range(16)] for k in range(16): fr.cap(pts[k], pts[(k + 1) % 16], 0.9, iron) for k in range(6): t = phi + TAU * k / 6 u = np.array([math.cos(t), 0.0, math.sin(t)]) fr.cap(C + 1.5 * u, C + 8.3 * u, 0.55, wood) fr.cyl(C + np.array([0.0, -1.2, 0.0]), C + np.array([0.0, 1.2, 0.0]), 1.9, iron) def cart_frame(d, col, loaded): fr = Fr(N_C, NZ_C, yaw_of(d)) I = Xf() wood, dark, canvas, cream = M['wood'], M['wood_dark'], M['canvas'], M['cloth_cream'] phi = D(15 * col) for y in (-11.5, 11.5): wheel(fr, y, phi) fr.cyl((0, -11.5, 10.6), (0, 11.5, 10.6), 0.9, M['metal']) fr.box(I, (0, 0, 15.5), (14.0, 10.0, 1.0), wood) fr.box(I, (13.25, 0, 19.0), (0.75, 10.0, 3.0), wood) fr.box(I, (-13.25, 0, 19.0), (0.75, 10.0, 3.0), wood) for s in (-1, 1): fr.box(I, (0, s * 9.25, 19.0), (13.0, 0.75, 3.0), wood) fr.cap((-13.5, 3.0 * s, 17.0), (-19.0, 3.0 * s, 24.0), 0.9, wood) fr.cyl((-19.0, -3.0, 24.0), (-19.0, 3.0, 24.0), 1.0, wood) if not loaded: fr.cyl((-6.0, -9.0, 19.0), (-6.0, 9.0, 19.0), 2.6, canvas) else: fr.box(I, (-5.0, -2.0, 21.0), (5.0, 4.5, 4.5), dark) fr.box(I, (7.0, -3.0, 20.0), (4.0, 5.0, 3.5), canvas) fr.box(I, (7.0, -3.0, 23.9), (4.2, 0.45, 0.5), cream) fr.box(I, (7.0, -3.0, 23.9), (0.45, 5.2, 0.5), cream) fr.box(I, (-5.0, -2.0, 27.0), (3.5, 3.5, 1.5), canvas) fr.box(I, (-5.0, -2.0, 28.6), (3.6, 0.4, 0.4), cream) fr.ell(I, (-9.0, 6.0, 19.5), (3.6, 3.0, 3.0), canvas) return finish(fr, N_C) def on_bg(img): bg = Image.new('RGBA', img.size, (206, 200, 186, 255)) bg.alpha_composite(img.convert('RGBA')) return bg.convert('RGB') def write_sheet(name, out, cell, grid, origin): save(name, out, origin, cell=cell, grid=grid, group='chars') on_bg(out.resize((out.width * 3, out.height * 3), Image.NEAREST)).save(os.path.join(PREV, 'sheet_%s.png' % name)) def main(): os.makedirs(PREV, exist_ok=True) player = None for name, look in LOOKS.items(): out, cell, grid, origin = build_human(look) write_sheet(name, out, cell, grid, origin) if name == 'player': player = (out, cell) out, cell, grid, origin = atlas([[wolf_frame(d, c) for c in range(7)] for d in range(8)]) write_sheet('wolf', out, cell, grid, origin) out, cell, grid, origin = atlas([[cart_frame(d, c, ld) for c in range(4)] for ld in (0, 1) for d in range(8)]) write_sheet('cart', out, cell, grid, origin) out, (cw, ch) = player def crop(r, c): return out.crop((c * cw, r * ch, (c + 1) * cw, (r + 1) * ch)) walk = Image.new('RGBA', (8 * cw, 2 * ch), (0, 0, 0, 0)) for j, d in enumerate((3, 5)): for i in range(8): walk.paste(crop(d, i + 1), (i * cw, j * ch)) on_bg(walk.resize((walk.width * 4, walk.height * 4), Image.NEAREST)).save(os.path.join(PREV, 'walk.png')) poses = Image.new('RGBA', (9 * cw, 2 * ch), (0, 0, 0, 0)) for j, r in enumerate((8 + 4, 16 + 4)): for i in range(9): poses.paste(crop(r, i), (i * cw, j * ch)) on_bg(poses.resize((poses.width * 4, poses.height * 4), Image.NEAREST)).save(os.path.join(PREV, 'poses.png')) if __name__ == '__main__': main()