A 3D model pipeline, pushed by Gemini 4 Argon
agent (cont.)
+1:18read_file{"Path":"~/work/lowmarch/review/vroid-02/build.py"}
input
{ "Path": "~/work/lowmarch/review/vroid-02/build.py" }result, 392 lines
Showing lines 1-391 of 391 1: """Build the model-viewer review page for the cast on stage, and their duel. 2: 3: python3 review/vroid-02/build.py every character, the ground and the page 4: python3 review/vroid-02/build.py elaine just Elaine (the others stay as last built) 5: 6: Writes to review/out/vroid-02/site/: 7: <name>.glb.json each character, skinned, in the reference (bust) pose, with animation clips: 8: idle, attack, clash (the two-character sword bind) and apose (arms out, for 9: checking a costume where hanging arms would hide it). Artifact hosting serves 10: .json but not .glb, so the model is base64-wrapped. 11: plains.glb.json the battle ground (static) 12: camera.json per character: the cameras for both views plus what the live renderer needs 13: (palette roles, lights, colliders, cloth grids); the duel's stage camera and 14: where each fighter stands 15: checks.json per character: the automatic checks (tools/checks.py) over every keyframe: arms 16: through the body, and garments showing through plate 17: and review/out/vroid-02/geom/<name>.npz (every piece as built), which tools/regress.py compares with the 18: last published version (snapshot.py) after each build. 19: then copies index.html next to them. The duel's poses are cached (they take about a minute to 20: solve) and re-solved when the rig, the duel or the cast entries change. 21: """ 22: import base64 23: import hashlib 24: import json 25: import os 26: import pickle 27: import shutil 28: import sys 29: from concurrent.futures import ProcessPoolExecutor 30: 31: import numpy as np 32: 33: HERE = os.path.dirname(os.path.abspath(__file__)) 34: ROOT = os.path.dirname(os.path.dirname(HERE)) 35: sys.path[:0] = [os.path.join(ROOT, "tools"), os.path.join(ROOT, "art", "cast"), HERE] 36: import vroid_portrait as vp # noqa: E402 37: import poses as P # noqa: E402 38: import terrain # noqa: E402 39: import export_glb # noqa: E402 40: import clips # noqa: E402 41: import duel # noqa: E402 42: import checks # noqa: E402 43: from duel_check import chars_for # noqa: E402 44: from cast import CAST # noqa: E402 45: 46: VROID = os.environ.get("LOWMARCH_VROID", os.path.expanduser("~/.local/share/lowmarch/vroid")) 47: # which review page: the cast viewer (vroid-02), or a workshop page on a smaller roster (its own artifact) 48: SITE = os.environ.get("LOWMARCH_SITE", "vroid-02") 49: WORKSHOPS = { 50: "mh-workshop": dict( 51: title="Body workshop", eyebrow="Lowmarch · MakeHuman bodies, VRoid heads", 52: roster=["mob_mh_youth", "mob_mh_veteran", "mob_mh_brute", "mob_mh_f_youth", "mob_mh_f_veteran", "mob_mh_f_strong"], 53: notes=os.path.join(HERE, "mh_workshop_notes.html")), 54: # the armory: bodies to assemble characters from, T-posed and bare (no clips, no checks): VRoid's base shaped by 55: # MakeHuman references (art/cast/cast.py ARMORY), which are built but never shown 56: "armory": dict( 57: title="Armory", eyebrow="Lowmarch · VRoid bodies shaped by MakeHuman, T-posed for inspection", 58: roster=[n for n in CAST if n.startswith("arm_")], armory=True, 59: notes=os.path.join(HERE, "armory_notes.html")), 60: "base-spike": dict( 61: title="Base spike", eyebrow="Lowmarch · which base model: MakeHuman, VRoid heads, Quaternius", 62: roster=[n for n in CAST if n.startswith("spk_")], 63: notes=os.path.join(HERE, "base_spike_notes.html")), 64: } 65: WORKSHOP_ONLY = {n for w in WORKSHOPS.values() for n in w["roster"]} - {"mob_mh_youth", "mob_mh_veteran", "mob_mh_brute"} 66: OUT = os.path.join(ROOT, "review", "out", SITE, "site") 67: CACHE = os.path.join(ROOT, "review", "out", "vroid-02", "cache") 68: GEOM = os.path.join(ROOT, "review", "out", "vroid-02", "geom") # per-piece geometry of the last build 69: ORDER = {"player": "aldric", "opponent": "elaine"} 70: # the armory's skin picker (cast.SKINS): people light to deep, then fantasy 71: SKIN_PICKER = [("People", [("porcelain", "Porcelain"), ("ivory", "Ivory"), ("peach", "Peach"), ("sand", "Sand"), 72: ("honey", "Honey"), ("olive_mid", "Olive"), ("amber", "Amber"), ("caramel", "Caramel"), 73: ("chestnut", "Chestnut"), ("umber", "Umber"), ("mahogany", "Mahogany"), ("ebony", "Ebony")]), 74: ("Fantasy", [("drow_ash", "Dark elf, ash"), ("drow_slate", "Dark elf, slate"), ("ashen", "Ashen"), 75: ("orc", "Orc"), ("orc_deep", "Orc, deep"), ("sea", "Sea"), ("infernal", "Infernal")])] 76: LABELS = {"aldric": "Aldric", "elaine": "Elaine", "saber": "Saber (study)"} # saber: a benchmark, not for the game 77: MOBS = [k for k in CAST if k.startswith("mob_") and k not in WORKSHOP_ONLY] # common enemies: built, stood in the lineup 78: # MakeHuman-based units stand first, next to the VRoid soldier they are compared with 79: MOBS = sorted(MOBS, key=lambda k: (0 if k == "mob_soldier" else 1 if CAST[k].get("mh") else 2)) 80: LABELS.update({k: CAST[k].get("label", k[4:].title()) for k in MOBS}) 81: if SITE in WORKSHOPS: # a workshop page: only its roster, in its order, no duel 82: MOBS = list(WORKSHOPS[SITE]["roster"]) 83: LABELS = {k: CAST[k].get("label", k[4:].title()) for k in MOBS} 84: # arms raised 35 degrees from the T-pose: shows the waist, the sides and the armpits 85: APOSE = P.merge(P.BUST, {"J_Bip_L_UpperArm": P.rot((0, 0, 1), -37), "J_Bip_R_UpperArm": P.rot((0, 0, 1), 37), 86: "J_Bip_L_Shoulder": P.rot((0, 0, 1), -6), "J_Bip_R_Shoulder": P.rot((0, 0, 1), 6)}) 87: 88: 89: def costume_of(c): 90: """The cast entry's costume, plus kit hair (hair_style) and facial hair (beard: a style, or 91: (style, length)) if it has them.""" 92: import outfit 93: import hair 94: base = outfit.COSTUMES[c.get("costume") or "lord"] 95: extra = [] 96: if c.get("hair_style"): 97: extra.append(lambda fit: getattr(hair, c["hair_style"])(fit)) 98: if c.get("beard"): 99: style, length = (c["beard"], 0.0) if isinstance(c["beard"], str) else c["beard"] 100: extra.append(lambda fit: hair.beard(fit, style, length=length)) 101: if not extra: 102: return base 103: return lambda fit: base(fit) + [pc for f in extra for pc in f(fit)] 104: 105: 106: def model_path(name, c): 107: """A VRoid model under LOWMARCH_VROID, or a converted MakeHuman or Quaternius body, or a reshaped VRoid one 108: (tools/makehuman.py, tools/quaternius.py, tools/mass.py, tools/cage.py, run first with `python3 -I`: they 109: read downloaded data).""" 110: if c.get("mass"): 111: import mass 112: p = mass.path_for(name, c["mass"]) 113: if not os.path.exists(p): 114: raise SystemExit(f"{name}: no massed VRoid body; run python3 -I tools/mass.py first") 115: return p 116: if c.get("cage"): 117: import cage 118: p = cage.path_for(name, c["cage"]) 119: if not os.path.exists(p): 120: raise SystemExit(f"{name}: no cage body; run python3 -I tools/cage.py first") 121: return p 122: if c.get("qt"): 123: import quaternius 124: p = quaternius.path_for(name, c["qt"]) 125: if not os.path.exists(p): 126: raise SystemExit(f"{name}: no converted Quaternius body; run python3 -I tools/quaternius.py first") 127: return p 128: if not c.get("mh"): 129: return os.path.join(VROID, c["model"]) 130: import makehuman 131: p = makehuman.path_for(name, c["mh"]) 132: if not os.path.exists(p): 133: raise SystemExit(f"{name}: no converted MakeHuman body; run python3 -I tools/makehuman.py first") 134: return p 135: 136: 137: def arm_room(name, c, need=0.006, most=16): 138: """The smallest outward arm angle (degrees, shape["arm_out"]) at which the hanging arms clear the 139: body by `need` in the reference pose and the idle: broad bodies otherwise sink their arms into 140: their sides. MakeHuman bodies only for now (the VRoid cast was posed by hand).""" 141: import checks 142: from vrm import VRM 143: v = VRM(model_path(name, c)) 144: limbs = [f"{s}_{a}" for s in "LR" for a in ("UpperArm", "LowerArm")] 145: for deg in range(0, most + 1, 2): 146: b = checks.Body(v, dict(c["shape"], arm_out=float(deg))) 147: if min(b.worst(pose, limbs) for pose in (P.BUST, P.BATTLE_IDLE)) >= need: 148: return float(deg) 149: return float(most) 150: 151: 152: ARMORY = bool(WORKSHOPS.get(SITE, {}).get("armory")) # T-posed and bare: the reference pose is the T-pose 153: 154: 155: def portrait(name, view): 156: c = CAST[name] 157: if (c.get("mh") or c.get("qt") or c.get("mass") or c.get("cage")) and "arm_out" not in c["shape"] and not ARMORY: 158: c["shape"]["arm_out"] = arm_room(name, c) 159: return vp.Portrait(model_path(name, c), shape=c["shape"], 160: hair_from=os.path.join(VROID, c["hair"]) if c.get("hair") else None, 161: costume=costume_of(c), pose={} if ARMORY else vp.BUST_POSE, hair_trim=c.get("hair_trim"), 162: world=terrain.plains() if view == "battle" else None), c 163: 164: 165: def springs(v): 166: """The model's spring bones (VRM 0.x secondaryAnimation: VRoid's women's base has its breasts', a hairstyle its 167: hair's, tools/heads.py): each group's root bones, settings and collision spheres (on a bone, offset in its own 168: frame, before the export's turn), for the viewer's physics.""" 169: sa = v.j.get("extensions", {}).get("VRM", {}).get("secondaryAnimation", {}) 170: nodes = v.j["nodes"] 171: spheres = [[dict(bone=nodes[cg["node"]]["name"], offset=[c["offset"]["x"], c["offset"]["y"], c["offset"]["z"]], 172: radius=c["radius"]) for c in cg.get("colliders", [])] for cg in sa.get("colliderGroups", [])] 173: out = [] 174: for g in sa.get("boneGroups", []): 175: gd = g.get("gravityDir", {"x": 0, "y": -1, "z": 0}) 176: out.append(dict(bones=[nodes[b]["name"] for b in g["bones"]], stiffness=g.get("stiffiness", 1.0), 177: gravity=g.get("gravityPower", 0.0), gravity_dir=[gd["x"], gd["y"], gd["z"]], 178: drag=g.get("dragForce", 0.4), radius=g.get("hitRadius", 0.02), comment=g.get("comment", ""), 179: colliders=[c for k in g.get("colliderGroups", []) if k < len(spheres) for c in spheres[k]])) 180: return out 181: 182: 183: def wrap_json(path): 184: json.dump({"glb": base64.b64encode(open(path, "rb").read()).decode()}, open(path + ".json", "w")) 185: os.remove(path) 186: 187: 188: def character(args): 189: """The rigged character (reference pose) with its clips, its cameras, the live renderer's data and 190: its checks.""" 191: name, clash = args 192: p, c = portrait(name, "bust") 193: p.paint = c.get("paint") 194: pal = dict(vp.PALETTE, **c["colors"]) 195: cam_bust, cam_battle = p.frame("bust"), p.frame("battle") 196: if ARMORY: # nothing moves: every clip holds the T-pose 197: cl = {k: [(0.0, {}), (1.0, {})] for k in ("idle", "attack", "apose")} 198: else: 199: cl = clips.clips(p.v, c["shape"], c.get("stance", "sword")) 200: if clash: 201: cl["clash"] = clash 202: cl["apose"] = [(0.0, APOSE), (1.0, APOSE)] 203: path = f"{OUT}/{name}.glb" 204: size = export_glb.export_skinned(p, pal, path, clips=cl) 205: wrap_json(path) 206: save_geometry(name, p) 207: if ARMORY: # the armory's checks are proportions (tools/proportions.py) 208: return dict(name=name, size=size, cams=dict(bust=cam_bust, battle=cam_battle), 209: info=dict(export_glb.scene_info(p, pal), springs=springs(p.v)), checks=proportion_checks(name, c)) 210: body = checks.Body(p.v, c["shape"]) 211: keyed = {k: v for k, v in cl.items() if k != "apose"} 212: head, bad, touch = checks.report(name, keyed, body) 213: layers = checks.layer_report(name, keyed, checks.Layers(p, body)) 214: return dict(name=name, size=size, cams=dict(bust=cam_bust, battle=cam_battle), info=export_glb.scene_info(p, pal), 215: checks=dict(summary=head, through=bad, touching=touch, layers=layers)) 216: 217: 218: def save_geometry(name, p): 219: """Every piece's vertices as built (reference pose), for tools/regress.py to compare builds.""" 220: os.makedirs(GEOM, exist_ok=True) 221: arrays, seen = {}, {} 222: for q in p.prims: 223: key = q["material"] 224: seen[key] = seen.get(key, 0) + 1 225: if seen[key] > 1: 226: key = f"{key}#{seen[key]}" 227: arrays[key] = q["pos"][np.unique(q["tri"])].astype(np.float32) if len(q["tri"]) else q["pos"].astype(np.float32) 228: np.savez_compressed(os.path.join(GEOM, f"{name}.npz"), **arrays) 229: 230: 231: def _fit_of(c): 232: """The fit a body was made to (its own, or its cage reference's): the ratios it skips (the heavy targets' 233: widths were measured on armour), ranges of its own, its figure.""" 234: spec = c.get("mh") or {} 235: ref = (c.get("cage") or {}).get("ref") 236: if isinstance(ref, str): 237: spec = CAST[ref].get("mh", {}) 238: elif isinstance(ref, (list, tuple)): 239: spec = ref[1] 240: return spec.get("fit", {}) 241: 242: 243: def _nth(p): 244: n = int(round(p)) 245: return f"{n}{'th' if 10 <= n % 100 <= 20 else {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th')}" 246: 247: 248: def proportion_checks(name, c): 249: """The model file against its archetype's targets (docs/proportions.md): a summary, and a line per ratio 250: outside its range (fails first); then its girths against real people of its height and waist 251: (tools/anthro.py), a line for each past the 5th or 95th percentile.""" 252: import anthro 253: import proportions as PR 254: path = model_path(name, c) 255: m = PR.measure(path) 256: arch = c.get("archetype") or PR.best_archetype(m, c.get("sex")) 257: t = PR.targets(arch, c.get("sex")) 258: want = _fit_of(c) 259: t.update({k: tuple(r) for k, r in want.get("ranges", {}).items()}) 260: t.update({k: tuple(r) for k, r in c.get("ranges", {}).items()}) # the body's own (cast.py CHECK_RANGES) 261: head = (c.get("cage") or {}).get("face") or {} 262: face = head.get("fit", {}).get("archetype") or head.get("archetype") 263: if face: # its face is judged by its face's archetype (tools/heads.py) 264: for k in ("eye_frac", "eye_open_CE", "iris_gap_CE", "face_w_CE"): 265: t.pop(k, None) 266: if k in PR.TARGETS[face]: 267: t[k] = PR.TARGETS[face][k] 268: # a face left as VRoid built it (no fit, 2026-10-13) has its ratios against the study's paintings reported, not 269: # judged: held to them, faces were stretched and read too sharp from the side 270: reported = ("H_CE", "eye_frac", "eye_open_CE", "iris_gap_CE", "face_w_CE") if head and "fit" not in head else () 271: for k in want.get("skip", ()): 272: t.pop(k, None) 273: for k in ("hand", "foot"): # judged against real hands and feet below instead 274: if want.get("anthro") and f"{k}_p" not in want.get("skip", ()): 275: t.pop(f"{k}_CE", None) 276: rows, faces = [], [] 277: for k, rng in t.items(): 278: v = m.get(k) 279: verdict = PR.judge(v, rng) 280: if verdict in ("warn", "FAIL") and k in reported: 281: faces.append(dict(verdict="face", text=f"{PR.RATIOS.get(k, k)}: {v:.2f}, the study's {rng[0]:g} to {rng[1]:g}")) 282: elif verdict in ("warn", "FAIL"): 283: rows.append(dict(verdict=verdict, text=f"{PR.RATIOS.get(k, k)}: {v:.2f}, target {rng[0]:g} to {rng[1]:g}" 284: + (" (fail)" if verdict == "FAIL" else " (near)"))) 285: rows.sort(key=lambda r: r["verdict"] != "FAIL") 286: n_fail = sum(r["verdict"] == "FAIL" for r in rows) 287: judged = [k for k in t if m.get(k) is not None and k not in reported] 288: summary = (f"{name}: {arch}, {len(judged) - len(rows)} of {len(judged)} ratios in range" 289: + (f", {n_fail} out" if n_fail else "") + (f", {len(rows) - n_fail} near" if len(rows) > n_fail else "")) 290: a = anthro.assess(path, c.get("sex"), name.endswith("_old")) 291: am = a.pop("_m") 292: who = f"{'women' if c.get('sex') == 'f' else 'men'} {am['stature'] / 100:.2f} m tall with a {am['waist']:.0f} cm waist" 293: real = [] 294: for k, (v, med, pct) in a.items(): 295: if k == "build": 296: continue 297: if np.isfinite(pct) and not 5 <= pct <= 95: 298: real.append(dict(verdict="real", text=f"Real bodies: {anthro.MEASURES[k][3]}: {v:.1f} cm, the {_nth(pct)} percentile" 299: f" of {who} (their median {med:.1f})")) 300: rows += real + faces 301: if real: 302: summary += f"; {len(real)} girths past the 5th to 95th percentile of real bodies" 303: m.update({f"real_{k}": v[0] for k, v in a.items()}) 304: m.update({f"real_{k}_pct": v[2] for k, v in a.items()}) 305: clean = {k: (None if v is None or not np.isfinite(v) else round(float(v), 4)) for k, v in m.items() 306: if isinstance(v, (int, float, np.floating))} # JSON has no NaN 307: return dict(summary=summary, through=[], touching=[], layers=None, props=rows, measures=clean) 308: 309: 310: def scenery(name): 311: p, c = portrait(name, "battle") 312: path = f"{OUT}/plains.glb" 313: size = export_glb.export(p, dict(vp.PALETTE, **c["colors"]), path, only_world=True) 314: wrap_json(path) 315: return size 316: 317: 318: def duel_cached(): 319: """The clash clip for both fighters, solved once per change to what it depends on.""" 320: src = "".join(open(os.path.join(ROOT, "tools", f)).read() for f in ("duel.py", "rig.py", "checks.py", "poses.py")) 321: key = hashlib.sha1((src + repr([CAST[n] for n in ORDER.values()])).encode()).hexdigest()[:16] 322: path = os.path.join(CACHE, f"duel_{key}.pkl") 323: if os.path.exists(path): 324: return pickle.load(open(path, "rb")) 325: print("solving the duel (about a minute)...", flush=True) 326: res = duel.duel_clips(chars_for(ORDER.values()), ORDER) 327: os.makedirs(CACHE, exist_ok=True) 328: pickle.dump(res, open(path, "wb")) 329: return res 330: 331: 332: def as_list(o): 333: return o.tolist() if isinstance(o, np.ndarray) else o 334: 335: 336: if __name__ == "__main__": 337: names = sys.argv[1:] or list(LABELS) 338: os.makedirs(OUT, exist_ok=True) 339: ws = WORKSHOPS.get(SITE) 340: clash, place = ({}, {}) if ws else duel_cached() 341: cam_path, chk_path = f"{OUT}/camera.json", f"{OUT}/checks.json" 342: old = json.load(open(cam_path)) if os.path.exists(cam_path) and sys.argv[1:] else {} 343: old_chk = json.load(open(chk_path)) if os.path.exists(chk_path) and sys.argv[1:] else {} 344: with ProcessPoolExecutor(4) as ex: 345: fc = [ex.submit(character, (n, clash.get(n))) for n in names] 346: fs = ex.submit(scenery, names[0]) if not sys.argv[1:] or not os.path.exists(f"{OUT}/plains.glb.json") else None 347: built = {r["name"]: r for r in (f.result() for f in fc)} 348: if fs: 349: print(f"plains.glb {fs.result() / 1e6:.2f} MB") 350: for n, r in built.items(): 351: print(f"{n}.glb {r['size'] / 1e6:.1f} MB") 352: print(" " + r["checks"]["summary"]) 353: if r["checks"]["layers"]: 354: print(" " + r["checks"]["layers"]["summary"]) 355: for item in r["checks"].get("props", []): 356: print(" " + {"FAIL": "! ", "real": "· "}.get(item["verdict"], "~ ") + item["text"]) 357: for item in r["checks"]["through"]: 358: print(" ! " + item["text"]) 359: prev = {c["name"]: c for c in old.get("characters", [])} 360: entries = [] 361: for n in LABELS: # keep the cast order; unbuilt characters as they were 362: if n in built: 363: r = built[n] 364: entries.append(dict(name=n, label=LABELS[n], info=r["info"], cams=r["cams"], 365: **({"group": CAST[n]["group"]} if CAST[n].get("group") else {}))) 366: elif n in prev: # as last built, with the cast's current label and group 367: e = {k: v for k, v in prev[n].items() if k != "group"} 368: e["label"] = LABELS[n] 369: if CAST[n].get("group"): 370: e["group"] = CAST[n]["group"] 371: entries.append(e) 372: cam_battle = entries[0]["cams"]["battle"] 373: duel_cam = dict(cam_battle, pivot_px=[cam_battle["w"] // 2, cam_battle["pivot_px"][1]]) 374: out = dict(characters=entries, duel=dict(order=ORDER, place=place, cam=duel_cam, clip="clash"), 375: lineup=dict(order=[] if ARMORY else [n for n in MOBS if any(e["name"] == n for e in entries)], spacing=0.95)) 376: if ws: # the page's title and notes; no duel view 377: out["page"] = dict(title=ws["title"], eyebrow=ws["eyebrow"], notes=open(ws["notes"]).read(), duel=False, 378: armory=bool(ws.get("armory"))) 379: if ws.get("armory"): # the skin picker: every tone of the range, with its ramp 380: import cast 381: out["page"]["skins"] = [dict(name=n, label=lab, group=grp, ramp=export_glb.skin_ramp(cast.SKINS[n])) 382: for grp, tones in SKIN_PICKER for n, lab in tones] 383: json.dump(out, open(cam_path, "w"), indent=1, default=as_list) 384: chk = dict(old_chk, **{n: r["checks"] for n, r in built.items()}) 385: json.dump(chk, open(chk_path, "w"), indent=1) 386: shutil.copy(os.path.join(HERE, "index.html"), OUT) 387: import regress # what changed since the last published version 388: for n in built: 389: for line in regress.report(n): 390: print(line) 391: print("->", OUT)
agent
+1:26reasoning
Initial investigation into the file system reveals several relevant locations for review data, including directories dedicated to outputs, local application shares, and specific project shots. It's necessary to examine the contents of these directories and files to understand the project's current state. Examining the contents of review/frame is necessary, particularly index.html, along with other relevant HTML indices in different subdirectories.
read_file{"Path":"~/work/lowmarch/review/frame/index.html"}
input
{ "Path": "~/work/lowmarch/review/frame/index.html" }result, 227 lines
Showing lines 1-226 of 226 1: <!doctype html> 2: <!-- The first-session frame (docs/handbook.md): a plain stand-in figure in the game's camera, with options that change 3: in seconds because nothing is rebuilt. Blocks and colours come from a JSON spec, drawn here at load. 4: Not the game's look: flat two-tone only. The painted renderer stays review/vroid-02/index.html. 5: tools/session/frame.py serves this page and shoots it headless; ?spec=<file.json> names the spec. --> 6: <html><head><meta charset="utf-8"><title>frame</title> 7: <style>html,body{margin:0;background:#222}canvas{display:block}#out{display:none}</style> 8: <script type="importmap"> 9: { "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js", 10: "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/" } } 11: </script></head><body> 12: <textarea id="out" aria-hidden="true"></textarea> 13: <script type="module"> 14: import * as THREE from "three"; 15: import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js"; 16: 17: THREE.ColorManagement.enabled = false; // colours are written as they are (portrait guide, lesson 08) 18: const params = new URLSearchParams(location.search); 19: const SPEC = await (await fetch(params.get("spec") || "spec.json")).json(); 20: // ground_y: where the feet stand, as a share of the frame's height from the bottom; horizon: top of the flat ground band 21: const F = { w: 960, h: 540, background: "#b9c4cf", ground: "#8d9186", ground_y: 0.16, horizon: 0.3, camera: {}, ...SPEC.frame }; 22: const C = { kind: "ortho", fov: 30, yaw: 0, pitch: 0, figure_h: 0.5, figure_x: 0.5, ...F.camera }; 23: const LOOK = { skin: "#b8b8bc", shade: "#8c8c94", hair: "#6d6d76", hair_shade: "#55555e", cloth: "#a2a2a8", 24: cloth_shade: "#7c7c86", ink: "#2c2438", outline: 0.004, threshold: 0.15, light: [-0.5, 0.75, 0.6], 25: features: false, hide: [], pose: "down", ...SPEC.look }; 26: const hex = s => new THREE.Color(s); 27: 28: // ---- two flat tones, split by the light; the mesh's own (baked) normals, skinned; an inverted copy for the outline ---- 29: const VS = `#include <common> 30: #include <skinning_pars_vertex> 31: uniform float uPush; varying vec3 vN; varying vec2 vUv; 32: void main() { 33: #include <beginnormal_vertex> 34: #include <skinbase_vertex> 35: #include <skinnormal_vertex> 36: #include <begin_vertex> 37: #include <skinning_vertex> 38: vN = normalize(mat3(modelMatrix) * objectNormal); vUv = uv; 39: transformed += normalize(objectNormal) * uPush; 40: gl_Position = projectionMatrix * modelViewMatrix * vec4(transformed, 1.0); 41: }`; 42: const FS = `uniform vec3 uLit, uShade, uL; uniform float uT, uFlat; uniform sampler2D uMask; uniform float uHasMask; 43: varying vec3 vN; varying vec2 vUv; 44: void main() { 45: if (uHasMask > 0.5 && texture2D(uMask, vUv).a < 0.5) discard; 46: float d = dot(normalize(vN), uL); 47: gl_FragColor = vec4(uFlat > 0.5 || d > uT ? uLit : uShade, 1.0); 48: }`; 49: function toon(lit, shade, look, mask, flat) { 50: return new THREE.ShaderMaterial({ vertexShader: VS, fragmentShader: FS, side: THREE.DoubleSide, uniforms: { 51: uLit: { value: hex(lit) }, uShade: { value: hex(shade) }, uL: { value: new THREE.Vector3(...look.light).normalize() }, 52: uT: { value: look.threshold }, uFlat: { value: flat ? 1 : 0 }, uPush: { value: 0 }, 53: uMask: { value: mask || null }, uHasMask: { value: mask ? 1 : 0 } } }); 54: } 55: const inkMat = look => new THREE.ShaderMaterial({ vertexShader: VS, side: THREE.BackSide, 56: fragmentShader: `uniform vec3 uInk; void main() { gl_FragColor = vec4(uInk, 1.0); }`, 57: uniforms: { uPush: { value: look.outline }, uInk: { value: hex(look.ink) } } }); 58: 59: // ---- the figure: role from the material's name ("body:face", "hair:hair", "underwear:cloth") ---- 60: function colours(role, look) { 61: if (role === "hair") return [look.hair, look.hair_shade]; 62: if (role === "body" || role === "face" || role === "shadow") return [look.skin, look.shade]; 63: if (role === "white") return ["#f4f2ee", "#f4f2ee"]; 64: if (["iris", "brow", "line", "mouth"].includes(role)) return [look.ink, look.ink]; 65: return [look.cloth, look.cloth_shade]; 66: } 67: const POSE = { down: 74, out: 0 }; 68: function posed(root, kind) { // from the T-pose: each upper arm turns about the body's front axis 69: root.updateMatrixWorld(true); 70: const at = n => root.getObjectByName(n).getWorldPosition(new THREE.Vector3()); 71: for (const s of ["L", "R"]) { 72: const b = root.getObjectByName(`J_Bip_${s}_UpperArm`); 73: const side = Math.sign(at(`J_Bip_${s}_Hand`).x - at(`J_Bip_${s}_UpperArm`).x) || 1; 74: const axis = new THREE.Vector3(0, 0, 1).applyQuaternion(b.parent.getWorldQuaternion(new THREE.Quaternion()).invert()); 75: b.quaternion.premultiply(new THREE.Quaternion().setFromAxisAngle(axis, THREE.MathUtils.degToRad(-side * (POSE[kind] || 0)))); 76: root.updateMatrixWorld(true); 77: } 78: } 79: const j = await (await fetch(`${SPEC.figure}.glb.json`)).json(); 80: const bin = Uint8Array.from(atob(j.glb), c => c.charCodeAt(0)); 81: const gltf = await new Promise((ok, bad) => new GLTFLoader().parse(bin.buffer, "", ok, bad)); 82: const figure = gltf.scene; 83: let skeleton = null; 84: figure.traverse(o => { if (o.isMesh) { o.frustumCulled = false; o.userData.base = o.material; if (o.isSkinnedMesh) skeleton = o.skeleton; } }); 85: // The skeleton's bind pose holds the arms down while the mesh stays T-posed (lesson 26): turning a bone from bind lowers 86: // the mesh's arm, so an arm bone's world position doesn't say where the visible arm is. See meshAt below. 87: skeleton.pose(); 88: posed(figure, LOOK.pose); 89: figure.updateMatrixWorld(true); 90: skeleton.update(); 91: const box = new THREE.Box3(); 92: figure.traverse(o => { if (o.isSkinnedMesh) { const p = o.geometry.attributes.position, v = new THREE.Vector3(); 93: for (let i = 0; i < p.count; i += 7) { o.getVertexPosition(i, v); box.expandByPoint(v); } } }); 94: const H = box.max.y - box.min.y; 95: function meshAt(name) { // where the visible part is: the mean of the skin that follows this bone 96: const k = skeleton.bones.findIndex(b => b.name === name), sum = new THREE.Vector3(), v = new THREE.Vector3(); 97: let n = 0; 98: figure.traverse(o => { if (!o.isSkinnedMesh || !o.geometry.attributes.skinIndex) return; 99: const I = o.geometry.attributes.skinIndex, W = o.geometry.attributes.skinWeight; 100: for (let i = 0; i < I.count; i++) if (I.getX(i) === k && W.getX(i) > 0.5) { o.getVertexPosition(i, v); sum.add(v); n++; } }); 101: return n ? sum.divideScalar(n) : null; 102: } 103: const LANDMARKS = {}; 104: for (const n of ["J_Bip_C_Head", "J_Bip_C_Neck", "J_Bip_C_Chest", "J_Bip_C_Hips", "J_Bip_L_Foot", "J_Bip_R_Foot"]) { 105: const b = figure.getObjectByName(n); if (b) LANDMARKS[n] = b.getWorldPosition(new THREE.Vector3()).toArray().map(x => +x.toFixed(3)); 106: } 107: for (const n of ["J_Bip_L_UpperArm", "J_Bip_R_UpperArm", "J_Bip_L_Hand", "J_Bip_R_Hand"]) { 108: const p = meshAt(n); if (p) LANDMARKS[n] = p.toArray().map(x => +x.toFixed(3)); 109: } 110: 111: // ---- rough blocks, positions in metres from a landmark (+x the model's left, +y up, +z front) ---- 112: // {"kind":"ball","at":"J_Bip_C_Head","c":[0,0.1,0],"r":[0.11,0.1,0.11],"colour":"#333","shade":"#222"} 113: // {"kind":"tube","at":...,"points":[[x,y,z],...],"radii":[0.03,0.02,0.001],"flat":0.4,...} 114: // {"kind":"loft","at":...,"rings":[{"y":0,"rx":0.1,"rz":0.08,"cx":0,"cz":0},...],"closed":true,...} 115: function at(name) { return new THREE.Vector3(...(LANDMARKS[name] || [0, 0, 0])); } 116: function ringMesh(rings, seg) { // rings: arrays of Vector3, all one length 117: const P = [], I = [], n = rings[0].length; 118: rings.forEach(r => r.forEach(v => P.push(v.x, v.y, v.z))); 119: for (let a = 0; a < rings.length - 1; a++) for (let b = 0; b < n; b++) { 120: const c = (b + 1) % n, q = [a * n + b, a * n + c, (a + 1) * n + c, (a + 1) * n + b]; 121: I.push(q[0], q[1], q[2], q[0], q[2], q[3]); 122: } 123: for (const [k, w] of [[0, 1], [rings.length - 1, -1]]) { // close both ends with a fan 124: const m = new THREE.Vector3(); rings[k].forEach(v => m.add(v)); m.divideScalar(n); 125: const i0 = P.length / 3; P.push(m.x, m.y, m.z); 126: for (let b = 0; b < n; b++) { const c = (b + 1) % n; w > 0 ? I.push(i0, k * n + c, k * n + b) : I.push(i0, k * n + b, k * n + c); } 127: } 128: const g = new THREE.BufferGeometry(); 129: g.setAttribute("position", new THREE.Float32BufferAttribute(P, 3)); 130: g.setAttribute("uv", new THREE.Float32BufferAttribute(new Array(P.length / 3 * 2).fill(0), 2)); 131: g.setIndex(I); g.computeVertexNormals(); 132: return g; 133: } 134: function blockGeometry(b) { 135: const o = at(b.at), seg = 24; 136: if (b.kind === "ball") { 137: const g = new THREE.SphereGeometry(1, 24, 16); g.scale(...b.r); g.translate(o.x + b.c[0], o.y + b.c[1], o.z + b.c[2]); return g; 138: } 139: if (b.kind === "loft") { 140: const R = b.rings.map(r => Array.from({ length: seg }, (_, i) => { const t = i / seg * 2 * Math.PI; 141: return new THREE.Vector3(o.x + (r.cx || 0) + r.rx * Math.sin(t), o.y + r.y, o.z + (r.cz || 0) + r.rz * Math.cos(t)); })); 142: return ringMesh(R, seg); 143: } 144: if (b.kind === "tube") { // swept along the points; "flat" squashes the section toward the head's centre 145: const curve = new THREE.CatmullRomCurve3(b.points.map(p => new THREE.Vector3(...p).add(o))); 146: const N = 18, R = [], flat = b.flat ?? 1, c0 = at(b.at); 147: for (let i = 0; i <= N; i++) { 148: const u = i / N, p = curve.getPoint(u), t = curve.getTangent(u); 149: const k = u * (b.radii.length - 1), a = Math.min(Math.floor(k), b.radii.length - 2), r = b.radii[a] + (b.radii[a + 1] - b.radii[a]) * (k - a); 150: const out = p.clone().sub(c0).setY(0); if (out.lengthSq() < 1e-6) out.set(0, 0, 1); out.normalize(); 151: const side = new THREE.Vector3().crossVectors(t, out).normalize(), up = new THREE.Vector3().crossVectors(side, t).normalize(); 152: R.push(Array.from({ length: 12 }, (_, s) => { const q = s / 12 * 2 * Math.PI; 153: return p.clone().addScaledVector(side, r * Math.cos(q)).addScaledVector(up, r * flat * Math.sin(q)); })); 154: } 155: return ringMesh(R, 12); 156: } 157: throw new Error("unknown block kind " + b.kind); 158: } 159: 160: // ---- the camera: figure_h is the figure's share of the frame's height; ortho or a lens ---- 161: const scene = new THREE.Scene(); 162: const center = new THREE.Vector3((box.min.x + box.max.x) / 2, (box.min.y + box.max.y) / 2, 0); 163: const span = H / C.figure_h, aspect = F.w / F.h; 164: const ry = THREE.MathUtils.degToRad(C.yaw), rp = THREE.MathUtils.degToRad(C.pitch); 165: const look = new THREE.Vector3(center.x - (C.figure_x - 0.5) * span * aspect * Math.cos(ry), 166: box.min.y + span * (0.5 - F.ground_y), 167: (C.figure_x - 0.5) * span * aspect * Math.sin(ry)); 168: let camera; 169: const dir = new THREE.Vector3(Math.sin(ry) * Math.cos(rp), Math.sin(rp), Math.cos(ry) * Math.cos(rp)); 170: if (C.kind === "ortho") { 171: camera = new THREE.OrthographicCamera(-span * aspect / 2, span * aspect / 2, span / 2, -span / 2, 0.1, 60); 172: camera.position.copy(look).addScaledVector(dir, 20); 173: } else { 174: camera = new THREE.PerspectiveCamera(C.fov, aspect, 0.05, 200); 175: camera.position.copy(look).addScaledVector(dir, span / 2 / Math.tan(THREE.MathUtils.degToRad(C.fov) / 2)); 176: } 177: camera.lookAt(look); 178: scene.add(figure); 179: 180: const renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true }); 181: renderer.outputColorSpace = THREE.LinearSRGBColorSpace; 182: renderer.setSize(F.w, F.h); renderer.setPixelRatio(1); 183: document.body.appendChild(renderer.domElement); 184: const backdrop = document.createElement("canvas"); // flat bands behind everything: a stand-in, not a scene 185: backdrop.width = F.w; backdrop.height = F.h; 186: const bg2d = backdrop.getContext("2d"); 187: bg2d.fillStyle = F.background; bg2d.fillRect(0, 0, F.w, F.h); 188: bg2d.fillStyle = F.ground; bg2d.fillRect(0, F.h * (1 - F.horizon), F.w, F.h * F.horizon); 189: scene.background = new THREE.CanvasTexture(backdrop); 190: 191: // ---- one image per option ---- 192: const shots = {}, extras = []; 193: function clearExtras() { for (const m of extras.splice(0)) { scene.remove(m); if (m.geometry !== undefined && !m.isSkinnedMesh) m.geometry.dispose(); } } 194: const options = SPEC.options && SPEC.options.length ? SPEC.options : [{ label: "frame" }]; 195: for (const opt of options) { 196: clearExtras(); 197: const L = { ...LOOK, ...(opt.look || {}) }; 198: const hideRoles = new Set(L.hide); 199: figure.traverse(o => { 200: if (!o.isMesh) return; 201: const [group, role] = (o.userData.base.name || "body:body").split(":"); 202: const feature = ["iris", "white", "brow", "line", "mouth", "highlight"].includes(role); 203: o.visible = !(hideRoles.has(group) || hideRoles.has(role)) && !(feature && !L.features) && role !== "highlight"; 204: if (!o.visible) return; 205: const [lit, shade] = colours(group === "hair" ? "hair" : group === "body" ? role : "cloth", L); 206: const mask = o.userData.base.alphaTest > 0 ? o.userData.base.map : null; 207: o.material = toon(lit, shade, L, mask, feature); 208: if (L.outline > 0 && !feature && role !== "shadow") { 209: const hull = new THREE.SkinnedMesh(o.geometry, inkMat(L)); 210: hull.bind(o.skeleton, o.bindMatrix); hull.frustumCulled = false; o.parent.add(hull); extras.push(hull); 211: hull.userData.owner = o.parent; 212: } 213: }); 214: for (const b of opt.blocks || []) { 215: const g = blockGeometry(b), m = new THREE.Mesh(g, toon(b.colour || "#777", b.shade || b.colour || "#555", L)); 216: scene.add(m); extras.push(m); 217: if (L.outline > 0) { const h = new THREE.Mesh(g, inkMat(L)); scene.add(h); extras.push(h); } 218: } 219: renderer.render(scene, camera); 220: shots[opt.label] = renderer.domElement.toDataURL("image/png"); 221: for (const m of extras) if (m.userData.owner) m.userData.owner.remove(m); 222: } 223: document.getElementById("out").textContent = JSON.stringify({ shots, landmarks: LANDMARKS, height: +H.toFixed(3), 224: box: [box.min.toArray().map(x => +x.toFixed(3)), box.max.toArray().map(x => +x.toFixed(3))] }); 225: document.title = "frame ready"; 226: </script></body></html>