A mech game prototype, pushed by Gemini 4 Argon
agent (cont.)
+1:14read_file{"Path":"~/work/sortie/kit/light.py"}
input
{ "Path": "~/work/sortie/kit/light.py" }result, 604 lines
Partial read (lines 1-800 of 908). Showing bytes 0-46080 of 61570 in this line range, truncated at the 46080-byte limit. To read the rest of this line range, call this tool again with the same StartLine and EndLine and ByteOffset=46080. 1: """Light frames: 6 m machines in the knightmare class (docs/sizes.md, docs/light-frames.md). Same joint 2: names as the striker (src/render/mech.ts JOINTS) plus their own: core (the ejectable cockpit block, 3: with the cockpit seat inside, parent of the backpack and the harkens), harken.L/R (wire anchor 4: launchers at the muzzles) and spinner.L/R (heel wheel axles). 5: 6: Proportions from the knightmare study: hip at 54% of the height, a small head with the crest 7: reaching the full height, long arms (hands hang at about 0.42), a narrow waist. The core block is a 8: quarter of the height tall, as wide as the chest and tops out behind the head. At this size the 9: armour is fewer, larger plates: sculpted shells (two_view cages smoothed with creases) split by real 10: seams, over a dark inner frame with the hip, knee and elbow mechanics showing. 11: 12: Variants: 13: knight the lead frame: armet helm with one visor slit and a raked crest, upswept pauldrons with 14: three lames, long pointed tassets, fenders over the heel wheels, broad float blades 15: line the mass-produced grunt in the accent colour: rounded factsphere helm (domed lids that 16: swing open on hinges), slab chest and pauldrons with louvres, a wide boxy block, stub blades 17: lancer the fast assault frame: swept wedge helm with a long blade crest, prow chest, calf 18: thrusters, folding forearm blades, a long block and swept delta blades 19: 20: Left is +X, the model faces -Y, z up, authored in world rest-pose coordinates. 21: """ 22: from mathutils import Matrix, Vector 23: 24: import bpy 25: from lib import (attach, boolean, circle, clip, copy, cylinder, empty, halves, loft, merge, mirror_x, nozzle, octo, panels, piston, prism, rot, 26: solid, 27: subd, transform, two_view) 28: from figure import pilot 29: from mech import gltf_dir, nozzle_pair, place 30: 31: H = 6.0 32: 33: 34: # ---- helpers ---------------------------------------------------------------------------------------------- 35: def sc(front, side, top=None, sharp=40, levels=2): 36: """A sculpted shell: a two_view cage, smoothed, creased where its faces meet sharper than `sharp`.""" 37: return subd(two_view(front, side, top), levels, sharp) 38: 39: 40: SWAP = {} # role remap for the frame being built: the grunt wears the accent as its body colour 41: 42: 43: def put(name, bm, role, j, **kw): 44: kw.setdefault("smooth", True) 45: return solid(name, bm, SWAP.get(role, role), j, **kw) 46: 47: 48: def two(name, bm, role, jl, jr, **kw): 49: """The left part and its mirror on the right.""" 50: kw.setdefault("smooth", True) 51: r = mirror_x(bm) 52: role = SWAP.get(role, role) 53: return solid(f"{name}.L", bm, role, jl, **kw), solid(f"{name}.R", r, role, jr, **kw) 54: 55: 56: def centre(bm): 57: return sum((v.co for v in bm.verts), Vector()) / max(len(bm.verts), 1) 58: 59: 60: def split(bm, cuts, gap=.025): 61: return panels(bm, [(Vector(c), Vector(n)) for c, n in cuts], gap) 62: 63: 64: def box(x0, x1, y0, y1, z0, z1): 65: rect = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)] 66: return loft([(z0, rect), (z1, rect)]) 67: 68: 69: def sphere(r, at, n=16, rings=8): 70: import math 71: pts = [] 72: for i in range(1, rings): 73: a = math.pi * i / rings 74: pts.append((at[2] - r * math.cos(a), circle(r * math.sin(a), n, at[0], at[1]))) 75: return subd(loft(pts), 1, 90) 76: 77: 78: def lr(J, part): 79: return J[part, "L"], J[part, "R"] 80: 81: 82: def mat(at, rx=0.0, ry=0.0, rz=0.0): 83: return Matrix.Translation(Vector(at)) @ rot("Z", rz) @ rot("Y", ry) @ rot("X", rx) 84: 85: 86: # ---- skeleton --------------------------------------------------------------------------------------------- 87: def joints(variant): 88: J = {"frame": empty("frame", (0, 0, 0), frame=f"light_{variant}", height=H)} 89: J["pelvis"] = empty("pelvis", (0, 0, 3.3), J["frame"]) 90: J["waist"] = empty("waist", (0, 0, 3.48), J["pelvis"]) 91: J["torso"] = empty("torso", (0, 0, 3.78), J["waist"]) 92: J["neck"] = empty("neck", (0, 0, 5.02), J["torso"]) 93: J["head"] = empty("head", (0, 0, 5.2), J["neck"]) 94: back = CORE[variant]["back"] 95: J["core"] = empty("core", (0, .65, 5.05), J["torso"], ejectable=True) 96: J["cockpit"] = empty("cockpit", (0, .62, 5.05), J["core"]) 97: J["backpack"] = empty("backpack", (0, back + .1, 5.35), J["core"]) 98: for s, sx in (("L", 1), ("R", -1)): 99: J["shoulder", s] = empty(f"shoulder.{s}", (.62 * sx, 0, 4.8), J["torso"]) 100: J["upperarm", s] = empty(f"upperarm.{s}", (.82 * sx, 0, 4.74), J["shoulder", s]) 101: J["forearm", s] = empty(f"forearm.{s}", (.84 * sx, 0, 3.92), J["upperarm", s]) 102: J["hand", s] = empty(f"hand.{s}", (.85 * sx, 0, 3.04), J["forearm", s]) 103: J["grip", s] = empty(f"grip.{s}", (.83 * sx, -.01, 2.86), J["hand", s]) 104: J["thigh", s] = empty(f"thigh.{s}", (.36 * sx, 0, 3.24), J["pelvis"]) 105: J["shin", s] = empty(f"shin.{s}", (.4 * sx, 0, 1.78), J["thigh", s]) 106: J["foot", s] = empty(f"foot.{s}", (.4 * sx, .03, .4), J["shin", s]) 107: J["spinner", s] = empty(f"spinner.{s}", (.4 * sx, .42, .31), J["foot", s]) 108: J["binder", s] = empty(f"binder.{s}", (.42 * sx, back + .14, 5.52), J["backpack"]) 109: return J 110: 111: 112: # ---- the dark inner frame, shared -------------------------------------------------------------------------- 113: def inner(J, n): 114: P, W, T, N = J["pelvis"], J["waist"], J["torso"], J["neck"] 115: put(f"{n}_pelvis_core", loft([(2.98, octo(.4, .38, .1)), (3.2, octo(.64, .48, .14)), (3.46, octo(.56, .44, .12))]), "frame", P, 116: smooth=False, bevel=.015) 117: two(f"{n}_hip_ball", cylinder(.13, .18, .5, 12, axis="x", at=(0, 3.24)), "joint", *lr(J, "thigh")) 118: put(f"{n}_abdomen", loft([(3.36, octo(.42, .36, .1)), (3.6, octo(.36, .32, .09)), (3.92, octo(.46, .4, .11))]), "joint", W, 119: smooth=False, bevel=.012) 120: put(f"{n}_chest_core", loft([(3.8, octo(.5, .42, .12, y=.02)), (4.1, octo(.8, .56, .16)), (4.55, octo(1.0, .64, .18)), 121: (4.92, octo(.92, .58, .16)), (5.06, octo(.46, .36, .1))]), "frame", T, smooth=False, bevel=.015) 122: put(f"{n}_neck_core", cylinder(.085, 4.96, 5.34, 10), "joint", N) 123: put(f"{n}_neck_ring", loft([(5.04, octo(.26, .26, .07)), (5.1, octo(.24, .24, .06))]), "frame", N, smooth=False) 124: # arms 125: two(f"{n}_shoulder_block", loft([(.42, octo(.3, .3, .08, y=4.8)), (.74, octo(.28, .28, .08, y=4.8))], axis="x"), "frame", 126: *lr(J, "shoulder"), smooth=False, bevel=.012) 127: two(f"{n}_trunnion", cylinder(.12, .7, .9, 12, axis="x", at=(0, 4.76)), "joint", *lr(J, "shoulder")) 128: two(f"{n}_upperarm_core", loft([(4.0, octo(.18, .2, .05, x=.83)), (4.66, octo(.2, .22, .055, x=.82))]), "joint", 129: *lr(J, "upperarm"), smooth=False, bevel=.01) 130: two(f"{n}_elbow", cylinder(.11, .73, .93, 12, axis="x", at=(0, 3.92)), "joint", *lr(J, "upperarm")) 131: two(f"{n}_forearm_core", loft([(3.08, octo(.15, .17, .04, x=.85)), (3.86, octo(.18, .2, .05, x=.84))]), "frame", 132: *lr(J, "forearm"), smooth=False, bevel=.01) 133: two(f"{n}_wrist", cylinder(.065, 2.96, 3.1, 10, at=(.85, 0)), "joint", *lr(J, "hand")) 134: # legs 135: two(f"{n}_thigh_core", loft([(1.94, octo(.2, .24, .06, x=.4)), (2.75, octo(.27, .3, .08, x=.4)), (3.12, octo(.25, .27, .07, x=.38))]), 136: "frame", *lr(J, "thigh"), smooth=False, bevel=.012) 137: two(f"{n}_knee", cylinder(.125, .3, .5, 12, axis="x", at=(0, 1.78)), "joint", *lr(J, "shin")) 138: two(f"{n}_shin_core", loft([(.45, octo(.17, .2, .05, x=.4, y=.03)), (1.7, octo(.21, .25, .06, x=.4))]), "frame", 139: *lr(J, "shin"), smooth=False, bevel=.01) 140: two(f"{n}_ankle", cylinder(.09, .33, .47, 10, axis="x", at=(.03, .4)), "joint", *lr(J, "foot")) 141: 142: 143: def hands(J, n, guard="main"): 144: jl, jr = lr(J, "hand") 145: two(f"{n}_palm", loft([(2.86, octo(.12, .2, .03, x=.85)), (3.0, octo(.11, .19, .03, x=.85))]), "frame", jl, jr, smooth=False, 146: bevel=.008) 147: # four fingers in two pairs, slightly curled, a thumb across the front 148: for i, y in enumerate((-.06, .04)): 149: f = loft([(2.88, octo(.07, .08, .02, x=.82, y=y)), (2.78, octo(.068, .075, .02, x=.815, y=y - .005)), 150: (2.68, octo(.06, .07, .018, x=.8, y=y - .02))]) 151: two(f"{n}_fingers{i}", f, "frame", jl, jr, smooth=False, bevel=.006) 152: two(f"{n}_thumb", loft([(2.92, octo(.06, .06, .015, x=.82, y=-.12)), (2.78, octo(.05, .05, .012, x=.81, y=-.15))]), "frame", 153: jl, jr, smooth=False, bevel=.006) 154: two(f"{n}_hand_guard", sc([(.89, 2.82), (.93, 2.84), (.94, 3.02), (.9, 3.04)], [(-.1, 2.82), (.1, 2.82), (.11, 3.02), (-.11, 3.04)]), 155: guard, jl, jr) 156: 157: 158: def spinners(J, n, v, r=.3): 159: """Magnetic hull wheels at the heels, 0.1 of the height across: a dark tyre on a bright rim, 160: carried on one arm from the inner heel so the hub shows outboard. The lead tucks the top of the 161: wheel under a blade-shaped heel.""" 162: for s, sx in (("L", 1), ("R", -1)): 163: sp, F = J["spinner", s], J["foot", s] 164: c = Vector(sp.matrix_world.translation) 165: x, y, z = c 166: put(f"{n}_tyre.{s}", cylinder(r, x - .17, x + .17, 24, axis="x", at=(y, z)), "joint", sp) 167: put(f"{n}_rim.{s}", cylinder(r * .72, x - .178, x + .178, 20, axis="x", at=(y, z)), "frame", sp) 168: put(f"{n}_hub.{s}", cylinder(r * .42, x - .188, x + .188, 12, axis="x", at=(y, z)), "metal", sp) 169: put(f"{n}_hubcap.{s}", cylinder(r * .16, x - .2, x + .2, 8, axis="x", at=(y, z)), "trim", sp) 170: xi = x - .215 * sx 171: put(f"{n}_wheel_arm.{s}", sc([(xi - .035, .2), (xi + .035, .2), (xi + .035, .44), (xi - .035, .44)], 172: [(y - .36, .16), (y - .02, .22), (y + .08, .32), (y, .42), (y - .34, .4)], 173: top=[(xi - .035, y - .38), (xi + .035, y - .38), (xi + .035, y + .1), (xi - .035, y + .1)]), "frame", F) 174: put(f"{n}_wheel_strut.{s}", piston((x - .215 * sx, .06, .44), (x - .215 * sx, y, z), r=.04), "metal", F) 175: if v == "line": 176: put(f"{n}_fender.{s}", sc([(x - .19, .46), (x + .19, .46), (x + .19, .66), (x - .19, .66)], 177: [(y - .3, .54), (y - .12, .66), (y + .2, .64), (y + .34, .44), (y + .22, .46), (y + .06, .56), (y - .2, .5)], 178: top=[(x - .19, y - .3), (x + .19, y - .3), (x + .19, y + .34), (x - .19, y + .34)], sharp=50), "accent", F) 179: if v == "knight": 180: fender = sc([(x - .19, .4), (x + .19, .4), (x + .18, .7), (x - .18, .7)], 181: [(y - .4, .5), (y - .26, .68), (y + .06, .7), (y + .3, .56), (y + .42, .34), (y + .34, .28), (y + .2, .5), (y - .04, .6), 182: (y - .3, .46)], 183: top=[(x - .19, y - .4), (x + .19, y - .4), (x + .17, y + .44), (x - .17, y + .44)], sharp=32) 184: for i, p in enumerate(split(fender, [((0, y + .14, 0), (0, 1, -.6))])): 185: put(f"{n}_fender{i}.{s}", p, "main" if centre(p).y < y + .14 else "accent", F) 186: elif v == "lancer": 187: put(f"{n}_fender.{s}", sc([(x - .19, .44), (x + .19, .44), (x + .18, .66), (x - .18, .66)], 188: [(y - .36, .5), (y - .1, .66), (y + .16, .64), (y + .3, .48), (y + .1, .56), (y - .2, .48)], 189: top=[(x - .19, y - .36), (x + .19, y - .36), (x + .17, y + .3), (x - .17, y + .3)], sharp=32), 190: "accent", F) 191: 192: 193: def harken(J, n, at, pod_role="main", back=.75): 194: """Harken launchers on the core block sides: an armoured pod, a cable reel showing outboard and a 195: clawed anchor seated in the muzzle, firing forward. `at` is the left muzzle. harken.L/R sit at 196: the muzzles and move with the block.""" 197: for s, sx in (("L", 1), ("R", -1)): 198: x, y, z = at[0] * sx, at[1], at[2] 199: C = J["core"] 200: pod = sc([(x - .07 * sx, z - .11), (x + .07 * sx, z - .1), (x + .075 * sx, z + .1), (x - .07 * sx, z + .12)], 201: [(y + .06, z - .1), (back, z - .13), (back, z + .13), (y + .1, z + .12), (y, z + .01)], 202: top=[(x - .07 * sx, y), (x + .05 * sx, y), (x + .075 * sx, y + .12), (x + .075 * sx, back), (x - .07 * sx, back)], sharp=36) 203: put(f"{n}_harken_pod.{s}", pod, pod_role, C) 204: ry = (y + back) / 2 + .08 205: put(f"{n}_reel.{s}", cylinder(.115, x + .05 * sx, x + .12 * sx, 16, axis="x", at=(ry, z)), "joint", C) 206: put(f"{n}_reel_cap.{s}", cylinder(.06, x + .07 * sx, x + .135 * sx, 10, axis="x", at=(ry, z)), "metal", C) 207: p = Vector((x, y, z)) 208: d = Vector((0, -1, 0)) 209: j = empty(f"harken.{s}", p, C, dir=gltf_dir(d)) 210: J["harken", s] = j 211: put(f"{n}_harken_ring.{s}", place(loft([(0, circle(.1, 14)), (-.05, circle(.094, 14))]), p + Vector((0, .02, 0)), d), "frame", j) 212: put(f"{n}_harken_spike.{s}", place(loft([(0, circle(.055, 8)), (-.09, circle(.06, 8)), (-.22, circle(.008, 8))]), p, d), "metal", j) 213: for k in range(3): 214: prong = loft([(0, octo(.04, .026, .008, y=.06)), (-.08, octo(.034, .024, .007, y=.1)), (-.16, octo(.01, .008, .003, y=.066))]) 215: transform(prong, rot("Z", 120 * k + 90)) 216: put(f"{n}_harken_claw{k}.{s}", place(prong, p, d), "trim", j, smooth=False) 217: 218: 219: # ---- the core block (cockpit), shared shape with variant detail --------------------------------------------- 220: CORE = {"knight": dict(w=1.06, top=5.78, back=1.06, ch=.26, sharp=46), 221: "line": dict(w=1.3, top=5.7, back=1.0, ch=.1, sharp=30), 222: "lancer": dict(w=.96, top=5.66, back=1.2, ch=.28, sharp=55)} 223: 224: 225: def core_block(J, n, v): 226: """The cockpit: a module behind the chest as wide as the shoulders and topping out over the head, 227: a notch at its front for the head. The pilot sits on a bike-style seat inside. The rear face is 228: the hatch, the whole block ejects (the core joint) and carries the harkens and the float pack.""" 229: C = J["core"] 230: k = CORE[v] 231: w, top, back, ch = k["w"], k["top"], k["back"], k["ch"] 232: hw = w / 2 233: body = sc([(-hw * .8, 4.38), (hw * .8, 4.38), (hw, 4.62), (hw, 5.36), (hw * .7, top), (-hw * .7, top), (-hw, 5.36), (-hw, 4.62)], 234: [(.08, 4.4), (.08, 5.04), (.27, 5.14), (.27, top - .1), (.42, top), (back - .4, top - .02), (back, top - .42), (back + .04, 4.72), 235: (back - .14, 4.4), (.3, 4.36)], 236: top=[(-hw + ch, .0), (hw - ch, 0), (hw, ch), (hw, back - ch), (hw - ch, back + .1), (-hw + ch, back + .1), (-hw, back - ch), 237: (-hw, ch)], sharp=k["sharp"]) 238: cuts = [((.2, 0, 0), (1, 0, 0)), ((-.2, 0, 0), (1, 0, 0)), ((0, 0, 5.08), (0, -.15, 1)), ((0, back - .08, 0), (0, 1, 0))] 239: hi, lo, spine = {"knight": ("hull", "accent", "main"), "line": ("hull", "main", "accent"), "lancer": ("main", "hull", "accent")}[v] 240: for i, p in enumerate(split(body, cuts, gap=.03)): 241: c = centre(p) 242: if c.y > back - .1: 243: role = "frame" 244: elif abs(c.x) < .2: 245: role = spine if c.z > 5.08 else "frame" 246: else: 247: role = hi if c.z > 5.08 else lo 248: put(f"{n}_core{i}", p, role, C) 249: # the rear hatch: a dark plate, four ejection motors round it, a handle and one status lamp 250: yb = back + .02 251: put(f"{n}_hatch", prism(octo(w * .5, .42, .1, x=0, y=4.78), yb, yb + .05, "xz"), "joint", C, smooth=False) 252: put(f"{n}_hatch_bar", prism(octo(w * .3, .05, .02, x=0, y=4.86), yb + .05, yb + .09, "xz"), "metal", C, smooth=False) 253: for kk, (x, z) in enumerate(((-.36, 4.62), (.36, 4.62), (-.36, 4.96), (.36, 4.96))): 254: put(f"{n}_eject{kk}", cylinder(.045, yb - .02, yb + .07, 10, axis="y", at=(x * w / 1.06, z)), "trim", C) 255: put(f"{n}_core_lamp", cylinder(.025, yb, yb + .07, 8, axis="y", at=(.18, 4.66)), "lamp_green", C) 256: # side armour standing proud of the hull, a vent behind the harken, a ridge along the top 257: plate_role = {"knight": "main", "line": "accent", "lancer": "accent"}[v] 258: plate = sc([(hw - .02, 4.7), (hw + .035, 4.73), (hw + .045, 5.4), (hw - .02, 5.46)], 259: [(.34, 4.7), (back - .22, 4.68), (back - .08, 4.92), (back - .22, 5.34), (.46, 5.46), (.34, 5.28)], 260: top=[(hw - .02, .34), (hw + .045, .4), (hw + .045, back - .1), (hw - .02, back - .06)], sharp=40) 261: for i, p in enumerate(split(plate, [((0, .78, 5.1), (0, 1, -.5))], gap=.025)): 262: two(f"{n}_core_side{i}", p, plate_role if centre(p).y < .78 + (centre(p).z - 5.1) * .5 else "frame", C, C) 263: two(f"{n}_core_vent", merge(*[box(hw - .01, hw + .03, back - .36 + .06 * i, back - .325 + .06 * i, 4.46, 4.62) for i in range(4)]), 264: "frame", C, C, smooth=False) 265: ridge = sc([(-.07, top - .05), (.07, top - .05), (.05, top + .035), (-.05, top + .035)], 266: [(.44, top - .05), (back - .34, top - .07), (back - .4, top + .02), (.52, top + .035)], sharp=40) 267: put(f"{n}_core_ridge", ridge, "trim" if v == "knight" else "frame", C) 268: if v == "knight": 269: # a dark sensor housing across the top front edge, a sensor strip in its face, one blade antenna 270: pod = sc([(-.3, top - .12), (.3, top - .12), (.26, top + .05), (-.26, top + .05)], 271: [(.24, top - .12), (.5, top - .1), (.56, top + .04), (.3, top + .05), (.22, top - .02)], 272: top=[(-.3, .22), (.3, .22), (.3, .56), (-.3, .56)], sharp=36) 273: put(f"{n}_sensor_pod", pod, "frame", C) 274: put(f"{n}_sensor_strip", box(-.2, .2, .21, .235, top - .05, top - .02), "sensor", C, smooth=False) 275: ant = transform(two_view([(-.012, 0), (.012, 0), (.006, .42), (-.006, .42)], [(-.03, 0), (.05, 0), (.08, .42), (.06, .42)]), 276: mat((.2, .45, top + .03), rx=-35)) 277: put(f"{n}_sensor_blade", ant, "frame", C, smooth=False) 278: # rails where the block docks into the torso 279: for s, sx in (("L", 1), ("R", -1)): 280: put(f"{n}_core_rail.{s}", box(sx * .2 - .03, sx * .2 + .03, -.02, .12, 4.5, 5.0), "joint", C, smooth=False) 281: empty(f"{n}_seat", (0, .62, 4.75), J["cockpit"]) 282: 283: 284: # ---- heads -------------------------------------------------------------------------------------------------- 285: def _cranium(front, side, top, sharp): 286: return sc(front, side, top, sharp) 287: 288: 289: def head_knight(J, n): 290: """A small crested helm: a narrow T-slit under a brow that dips to the centre, a raked blade crest 291: and two horns swept back along the crown. The rear of the helm is its own plate.""" 292: Hd = J["head"] 293: cage = two_view([(-.15, 5.25), (.15, 5.25), (.19, 5.43), (.18, 5.6), (.1, 5.7), (-.1, 5.7), (-.18, 5.6), (-.19, 5.43)], 294: [(-.22, 5.3), (-.23, 5.47), (-.19, 5.62), (-.05, 5.72), (.13, 5.69), (.22, 5.5), (.21, 5.3), (.1, 5.23), (-.1, 5.24)], 295: top=[(-.07, -.27), (.07, -.27), (.18, -.15), (.19, .15), (.1, .24), (-.1, .24), (-.19, .15), (-.18, -.15)]) 296: helm = subd(cage, 2, 50) 297: fr, rear = halves(helm, (0, .06, 5.5), (0, 1, -.4), .022) 298: o = put(f"{n}_helm", fr, "main", Hd) 299: boolean(o, two_view([(-.125, 5.4), (.125, 5.4), (.135, 5.52), (-.135, 5.52)], [(-1, 5.4), (-.1, 5.4), (-.1, 5.52), (-1, 5.52)]), 300: role="frame") 301: put(f"{n}_helm_rear", rear, "accent", Hd) 302: put(f"{n}_skull", loft([(5.22, octo(.26, .3, .08)), (5.58, octo(.3, .36, .09))]), "frame", Hd, smooth=False) 303: put(f"{n}_visor", two_view([(-.13, 5.405), (.13, 5.405), (.14, 5.515), (-.14, 5.515)], [(-.17, 5.4), (-.12, 5.4), (-.12, 5.52), (-.17, 5.52)]), 304: "visor", Hd, smooth=False) 305: put(f"{n}_slit", two_view([(-.12, 5.458), (.12, 5.458), (.12, 5.476), (-.12, 5.476)], 306: [(-.185, 5.4), (-.17, 5.4), (-.17, 5.52), (-.185, 5.52)]), "glow_eye", Hd, smooth=False) 307: brow = sc([(-.18, 5.49), (.18, 5.49), (.2, 5.55), (.03, 5.6), (-.03, 5.6), (-.2, 5.55)], 308: [(-.3, 5.485), (-.06, 5.485), (-.06, 5.62), (-.22, 5.61)], 309: top=[(-.21, -.05), (.21, -.05), (.21, -.19), (0, -.32), (-.21, -.19)], sharp=30) 310: put(f"{n}_brow", brow, "main", Hd) 311: put(f"{n}_crest", sc([(-.025, 5.56), (.025, 5.56), (.008, 6.02), (-.008, 6.02)], 312: [(-.2, 5.58), (-.32, 6.0), (-.21, 5.86), (-.02, 5.76), (.1, 5.75), (.08, 5.67), (-.05, 5.6)], sharp=30), 313: "trim", Hd) 314: put(f"{n}_crest_shoe", sc([(-.05, 5.6), (.05, 5.6), (.04, 5.72), (-.04, 5.72)], [(-.2, 5.6), (.02, 5.66), (.02, 5.74), (-.16, 5.72)]), 315: "accent", Hd) 316: for s, sx in (("L", 1), ("R", -1)): 317: horn = loft([(0, octo(.05, .09, .02)), (.2, octo(.04, .06, .015)), (.38, octo(.01, .016, .004))], axis="y") 318: transform(horn, mat((.17 * sx, -.1, 5.58), rx=16, rz=-7 * sx)) 319: put(f"{n}_horn.{s}", horn, "main", Hd, smooth=False, bevel=.005) 320: put(f"{n}_cheek.{s}", sc([(.13 * sx, 5.28), (.18 * sx, 5.32), (.19 * sx, 5.5), (.15 * sx, 5.52)], 321: [(-.14, 5.28), (.06, 5.28), (.08, 5.5), (-.14, 5.52)], 322: top=[(.15 * sx, -.16), (.2 * sx, -.12), (.21 * sx, .08), (.15 * sx, .08)]), "accent", Hd) 323: put(f"{n}_bevor", sc([(-.15, 5.27), (.15, 5.27), (.175, 5.44), (-.175, 5.44)], 324: [(-.2, 5.27), (-.06, 5.27), (-.06, 5.445), (-.29, 5.445), (-.27, 5.36)], 325: top=[(-.16, -.06), (.16, -.06), (.16, -.17), (0, -.27), (-.16, -.17)], sharp=30), "main", Hd) 326: 327: 328: def head_line(J, n, shutters_open=False): 329: """The factsphere head: a rounded helm with a round socket in its face, the sensor sphere inside 330: behind two domed lids. Open, the lids swing out on hinges above and below like a visor and the 331: sphere shows its lens cluster.""" 332: Hd = J["head"] 333: helm = clip(subd(loft([(5.22, octo(.36, .42, .12, y=.02)), (5.4, octo(.48, .52, .16)), (5.58, octo(.44, .5, .16, y=.02)), 334: (5.69, octo(.28, .36, .1, y=.04))]), 2, 60), x=(-.19, .19)) 335: # flat side plates make it a cowl, not a ball 336: for s_, sx in (("L", 1), ("R", -1)): 337: put(f"{n}_cowl.{s_}", sc([(.18 * sx, 5.26), (.215 * sx, 5.28), (.215 * sx, 5.58), (.18 * sx, 5.62)], 338: [(-.16, 5.3), (.12, 5.26), (.2, 5.4), (.16, 5.6), (-.12, 5.62), (-.2, 5.48)], sharp=36), "accent", Hd) 339: parts = split(helm, [((0, 0, 5.6), (0, 0, 1)), ((0, .1, 0), (0, 1, .2))], gap=.02) 340: for i, p in enumerate(parts): 341: c = centre(p) 342: if c.z < 5.6 and c.y < .1: 343: o = put(f"{n}_helm{i}", p, "main", Hd) 344: boolean(o, prism(circle(.15, 16, 0, 5.44), -1, -.05, "xz"), role="frame") 345: else: 346: put(f"{n}_helm{i}", p, "accent" if c.z >= 5.6 else "main", Hd) 347: put(f"{n}_socket", prism(circle(.15, 16, 0, 5.44), -.16, -.04, "xz"), "joint", Hd) 348: put(f"{n}_sphere", sphere(.125, (0, -.1, 5.44)), "sensor", Hd) 349: put(f"{n}_sphere_band", cylinder(.128, 5.43, 5.45, 24, at=(0, -.1)), "frame", Hd, smooth=False) 350: put(f"{n}_lens_ring", cylinder(.055, -.235, -.2, 16, axis="y", at=(0, 5.47)), "frame", Hd) 351: for k, (x, z, r) in enumerate(((0, 5.47, .038), (.06, 5.405, .016), (-.06, 5.405, .016))): 352: put(f"{n}_lens{k}", cylinder(r, -.245, -.22, 14, axis="y", at=(x, z)), "glow_eye" if k == 0 else "glow_hot", Hd, smooth=False) 353: # two domed lids cut from one shell, hinged at the top and bottom of the socket 354: cap = clip(sphere(.17, (0, -.1, 5.44), n=20, rings=10), y=(-1, -.2)) 355: up, dn = halves(cap, (0, 0, 5.44), (0, 0, 1), .02) 356: if shutters_open: 357: transform(up, rot("X", -72, (0, -.2, 5.6))) 358: transform(dn, rot("X", 72, (0, -.2, 5.28))) 359: put(f"{n}_shutter_hi", up, "main", Hd) 360: put(f"{n}_shutter_lo", dn, "main", Hd) 361: for k, z in (("hi", 5.6), ("lo", 5.28)): 362: put(f"{n}_hinge_{k}", cylinder(.018, -.1, .1, 8, axis="x", at=(-.2, z)), "joint", Hd) 363: put(f"{n}_ridge", sc([(-.22, 5.57), (.22, 5.57), (.2, 5.67), (-.2, 5.67)], [(-.32, 5.56), (-.08, 5.56), (-.06, 5.69), (-.24, 5.68)], 364: top=[(-.22, -.05), (.22, -.05), (.22, -.2), (0, -.33), (-.22, -.2)], sharp=30), "accent", Hd) 365: for s, sx in (("L", 1), ("R", -1)): 366: vents = merge(*[box(.235 * sx - .02, .235 * sx + .02, -.06, .1, z, z + .022) for z in (5.32, 5.36, 5.4)]) 367: put(f"{n}_vent.{s}", vents, "frame", Hd, smooth=False) 368: # one whip antenna, off to the left 369: ant = transform(cylinder(.012, 0, .42, 6, r1=.005), mat((.15, .12, 5.62), rx=-28, ry=-12)) 370: put(f"{n}_antenna", ant, "frame", Hd, smooth=False) 371: put(f"{n}_antenna_base", cylinder(.035, 5.6, 5.68, 8, at=(.15, .12)), "frame", Hd, smooth=False) 372: 373: 374: def head_lancer(J, n): 375: """A swept wedge: narrow, pointed in plan, one visor slit deep under a hard brow, a long blade crest 376: raked back over the helm.""" 377: Hd = J["head"] 378: cage = two_view([(-.11, 5.24), (.11, 5.24), (.2, 5.4), (.18, 5.62), (.08, 5.7), (-.08, 5.7), (-.18, 5.62), (-.2, 5.4)], 379: [(-.27, 5.32), (-.27, 5.5), (-.16, 5.66), (.08, 5.72), (.3, 5.6), (.34, 5.42), (.2, 5.24), (-.12, 5.24)], 380: top=[(0, -.36), (.14, -.28), (.21, -.06), (.18, .24), (.08, .34), (-.08, .34), (-.18, .24), (-.21, -.06), (-.14, -.28)]) 381: helm = subd(cage, 2, 36) 382: fr, rear = halves(helm, (0, .02, 5.5), (0, 1, -.6), .022) 383: o = put(f"{n}_helm", fr, "main", Hd) 384: boolean(o, two_view([(-.15, 5.4), (.15, 5.4), (.16, 5.52), (-.16, 5.52)], [(-1, 5.4), (-.1, 5.4), (-.1, 5.52), (-1, 5.52)]), role="frame") 385: put(f"{n}_helm_rear", rear, "accent", Hd) 386: put(f"{n}_skull", loft([(5.22, octo(.28, .32, .08)), (5.56, octo(.32, .38, .1))]), "frame", Hd, smooth=False) 387: put(f"{n}_visor", two_view([(-.15, 5.41), (.15, 5.41), (.16, 5.51), (-.16, 5.51)], [(-.2, 5.41), (-.12, 5.41), (-.12, 5.51), (-.2, 5.51)]), 388: "visor", Hd, smooth=False) 389: put(f"{n}_slit", two_view([(-.13, 5.455), (.13, 5.455), (.13, 5.472), (-.13, 5.472)], [(-.215, 5.43), (-.2, 5.43), (-.2, 5.51), (-.215, 5.51)]), 390: "glow_eye", Hd, smooth=False) 391: brow = sc([(-.19, 5.5), (.19, 5.5), (.2, 5.54), (0, 5.59), (-.2, 5.54)], [(-.34, 5.5), (-.08, 5.5), (-.08, 5.6), (-.24, 5.59)], 392: top=[(-.21, -.05), (.21, -.05), (.21, -.18), (0, -.4), (-.21, -.18)], sharp=28) 393: put(f"{n}_brow", brow, "accent", Hd) 394: put(f"{n}_crest", sc([(-.028, 5.58), (.028, 5.58), (.012, 6.0), (-.012, 6.0)], 395: [(-.32, 5.6), (-.22, 5.7), (.12, 5.86), (.5, 5.98), (.72, 5.99), (.5, 5.9), (.22, 5.76), (.04, 5.66), (-.12, 5.6)], 396: sharp=28), "trim", Hd) 397: put(f"{n}_crest_shoe", sc([(-.045, 5.58), (.045, 5.58), (.035, 5.7), (-.035, 5.7)], [(-.24, 5.58), (.06, 5.62), (.08, 5.72), (-.16, 5.7)]), 398: "frame", Hd) 399: for s, sx in (("L", 1), ("R", -1)):204 more lines not shown.
read_file{"Path":"~/work/sortie/src/render/pipeline.ts"}
input
{ "Path": "~/work/sortie/src/render/pipeline.ts" }result, 259 lines
Showing lines 1-258 of 258 1: import * as THREE from 'three'; 2: import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'; 3: import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js'; 4: import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'; 5: import { Pass } from 'three/addons/postprocessing/Pass.js'; 6: import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js'; 7: import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js'; 8: import { DetailShader, GradeShader, look, type Look } from './look'; 9: 10: /** Meshes on this layer get surface detail (edge wear, grime, ink); glows, effects and sky stay off it. */ 11: export const DETAIL_LAYER = 1; 12: 13: /** 14: * Rendering tiers. high: everything. medium: no multisampling, a smaller shadow map. low, for weak 15: * or software rendering: no multisampling, no shadows, no surface detail, bloom or grain, and at most 16: * 0.6 resolution. Multisampling the half-float buffers is by far the dearest part on a weak GPU. 17: */ 18: export type Tier = 'high' | 'medium' | 'low'; 19: export const TIERS: readonly Tier[] = ['high', 'medium', 'low']; 20: 21: export interface Quality { 22: /** Render resolution relative to CSS pixels (1 ignores high-DPI scaling). */ 23: scale: number; 24: ao: boolean; 25: shadows: boolean; 26: tier: Tier; 27: } 28: 29: export const DEFAULT_QUALITY: Quality = { scale: 1, ao: false, shadows: true, tier: 'high' }; 30: /** The low tier's resolution cap. */ 31: const LOW_SCALE = 0.6; 32: 33: /** Clamp before the bloom: one infinite or NaN pixel would otherwise blur over the whole frame. */ 34: const Sanitize = { 35: uniforms: { tDiffuse: { value: null } }, 36: vertexShader: 'varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }', 37: fragmentShader: ` 38: uniform sampler2D tDiffuse; varying vec2 vUv; 39: void main() { 40: vec4 c = texture2D(tDiffuse, vUv); 41: if (any(isnan(c)) || any(isinf(c))) c = vec4(0.0, 0.0, 0.0, 1.0); 42: gl_FragColor = vec4(min(c.rgb, vec3(48.0)), c.a); 43: }`, 44: }; 45: 46: /** 47: * Frame order: the backdrop at infinity (stars, nebula, planet), the world, ambient occlusion on the 48: * world, then the cockpit in its own pass (it sits centimetres from the eye, the world runs to tens 49: * of kilometres, so they don't share a depth range), then bloom and filmic tone mapping. 50: */ 51: class ScenePass extends Pass { 52: private bc = new THREE.PerspectiveCamera(60, 1, 1, 1e7); 53: 54: constructor( 55: public scene: THREE.Scene, 56: public camera: THREE.PerspectiveCamera, 57: public backdrop: THREE.Scene | null, 58: private clearFirst: boolean, 59: ) { 60: super(); 61: this.needsSwap = false; 62: } 63: 64: render(renderer: THREE.WebGLRenderer, _write: THREE.WebGLRenderTarget, read: THREE.WebGLRenderTarget) { 65: renderer.setRenderTarget(read); 66: if (this.clearFirst) { 67: renderer.setClearColor(0x000000, 1); 68: renderer.clear(); 69: } else renderer.clearDepth(); 70: if (this.backdrop) { 71: const bc = this.bc; 72: bc.fov = this.camera.fov; 73: bc.aspect = this.camera.aspect; 74: bc.updateProjectionMatrix(); 75: this.camera.getWorldQuaternion(bc.quaternion); 76: bc.updateMatrixWorld(); 77: renderer.render(this.backdrop, bc); 78: renderer.clearDepth(); 79: } 80: renderer.render(this.scene, this.camera); 81: } 82: } 83: 84: /** Size the composer's buffers for `w` x `h` CSS pixels at the renderer's pixel ratio. The composer 85: * keeps its own copy of the ratio from construction, so a tier change has to hand it the new one. */ 86: export function fitComposer(composer: Pick<EffectComposer, 'setPixelRatio' | 'setSize'>, w: number, h: number, pr: number) { 87: composer.setPixelRatio(pr); 88: composer.setSize(w, h); 89: } 90: 91: export class Pipeline { 92: readonly renderer: THREE.WebGLRenderer; 93: readonly composer: EffectComposer; 94: readonly bloom: UnrealBloomPass; 95: readonly ao: GTAOPass; 96: private detail: ShaderPass; 97: private grade: ShaderPass; 98: private sanitize: ShaderPass; 99: /** The look's own choice for the detail pass, before the tier. */ 100: private detailWanted = true; 101: private sun: THREE.DirectionalLight | null = null; 102: private normals: THREE.WebGLRenderTarget; 103: private normalMat = new THREE.MeshNormalMaterial(); 104: private world: ScenePass; 105: private overlay: ScenePass | null = null; 106: 107: quality: Quality; 108: 109: constructor(canvas: HTMLCanvasElement, world: THREE.Scene, camera: THREE.PerspectiveCamera, backdrop: THREE.Scene | null, 110: quality: Quality = DEFAULT_QUALITY) { 111: this.quality = { ...quality }; 112: const renderer = new THREE.WebGLRenderer({ canvas, antialias: false, powerPreference: 'high-performance' }); 113: renderer.setPixelRatio(this.quality.scale); 114: renderer.toneMapping = THREE.AgXToneMapping; 115: renderer.toneMappingExposure = 1.15; 116: renderer.shadowMap.enabled = true; 117: renderer.shadowMap.type = THREE.PCFShadowMap; 118: renderer.autoClear = false; 119: this.renderer = renderer; 120: 121: const target = new THREE.WebGLRenderTarget(1, 1, { type: THREE.HalfFloatType, samples: 4 }); 122: this.composer = new EffectComposer(renderer, target); 123: this.world = new ScenePass(world, camera, backdrop, true); 124: this.composer.addPass(this.world); 125: this.ao = new GTAOPass(world, camera, 1, 1); 126: this.ao.blendIntensity = 0.9; 127: this.ao.updateGtaoMaterial({ radius: 1.6, distanceExponent: 1.4, thickness: 2, scale: 1.2, samples: 8 }); 128: this.composer.addPass(this.ao); 129: this.normals = new THREE.WebGLRenderTarget(1, 1, { type: THREE.HalfFloatType }); 130: this.normals.depthTexture = new THREE.DepthTexture(1, 1, THREE.FloatType); 131: this.detail = new ShaderPass(DetailShader); 132: this.detail.uniforms.tNormal.value = this.normals.texture; 133: this.detail.uniforms.tDepth.value = this.normals.depthTexture; 134: this.composer.addPass(this.detail); 135: this.sanitize = new ShaderPass(Sanitize); 136: this.composer.addPass(this.sanitize); 137: this.bloom = new UnrealBloomPass(new THREE.Vector2(256, 256), 0.32, 0.22, 1.6); 138: this.composer.addPass(this.bloom); 139: this.composer.addPass(new OutputPass()); 140: this.grade = new ShaderPass(GradeShader); 141: this.composer.addPass(this.grade); 142: this.applyLook(look()); 143: this.setQuality({}); 144: } 145: 146: /** The GPU the browser gave us, and whether it's a software rasteriser (very slow). */ 147: gpu(): { name: string; software: boolean } { 148: const gl = this.renderer.getContext(); 149: const ext = gl.getExtension('WEBGL_debug_renderer_info'); 150: const name = String(ext ? gl.getParameter(ext.UNMASKED_RENDERER_WEBGL) : gl.getParameter(gl.RENDERER)); 151: return { name, software: /swiftshader|llvmpipe|softpipe|software|basic render/i.test(name) }; 152: } 153: 154: setQuality(q: Partial<Quality>) { 155: Object.assign(this.quality, q); 156: const { tier } = this.quality; 157: const low = tier === 'low'; 158: this.renderer.setPixelRatio(low ? Math.min(LOW_SCALE, this.quality.scale) : this.quality.scale); 159: this.ao.enabled = this.quality.ao && !low; 160: this.renderer.shadowMap.enabled = this.quality.shadows && !low; 161: const samples = tier === 'high' ? 4 : 0; 162: for (const t of [this.composer.renderTarget1, this.composer.renderTarget2]) { 163: if (t.samples === samples) continue; 164: t.samples = samples; 165: t.dispose(); 166: } 167: this.detail.enabled = this.detailWanted && !low; 168: this.bloom.enabled = this.sanitize.enabled = this.grade.enabled = !low; 169: this.shadowSize(); 170: this.resize(); 171: } 172: 173: /** The sun whose shadow map follows the tier. */ 174: set shadowLight(sun: THREE.DirectionalLight) { 175: this.sun = sun; 176: this.shadowSize(); 177: } 178: 179: private shadowSize() { 180: const s = this.quality.tier === 'high' ? 2048 : 1024; 181: const sh = this.sun?.shadow; 182: if (!sh || sh.mapSize.x === s) return; 183: sh.mapSize.set(s, s); 184: sh.map?.dispose(); 185: sh.map = null; 186: } 187: 188: applyLook(l: Look) { 189: const u = this.detail.uniforms; 190: u.ridgeK.value = l.ridge; 191: u.valleyK.value = l.valley; 192: u.inkK.value = l.ink; 193: u.chipped.value = l.chipped ? 1 : 0; 194: u.hatch.value = l.hatch ?? 0; 195: this.grade.uniforms.paper.value = l.paper ?? 0; 196: this.detailWanted = l.ridge + l.valley + l.ink > 0; 197: this.detail.enabled = this.detailWanted && this.quality?.tier !== 'low'; 198: this.grade.uniforms.grain.value = l.grain; 199: this.grade.uniforms.vignette.value = l.vignette; 200: } 201: 202: /** A scene drawn over the world with its own depth range (the cockpit), after the world's detail pass. */ 203: setOverlay(scene: THREE.Scene, camera: THREE.PerspectiveCamera) { 204: this.overlay = new ScenePass(scene, camera, null, false); 205: this.composer.insertPass(this.overlay, this.composer.passes.indexOf(this.detail) + 1); 206: this.resize(); 207: } 208: 209: set backdrop(scene: THREE.Scene | null) { 210: this.world.backdrop = scene; 211: } 212: 213: set overlayVisible(v: boolean) { 214: if (this.overlay) this.overlay.enabled = v; 215: } 216: 217: get camera() { 218: return this.world.camera; 219: } 220: 221: resize(w = window.innerWidth, h = window.innerHeight) { 222: this.renderer.setSize(w, h, false); 223: const pr = this.renderer.getPixelRatio(); 224: fitComposer(this.composer, w, h, pr); 225: this.normals?.setSize(Math.round(w * pr), Math.round(h * pr)); 226: this.detail?.uniforms.px.value.set(1 / (w * pr), 1 / (h * pr)); 227: for (const c of [this.world.camera, this.overlay?.camera]) { 228: if (!c) continue; 229: c.aspect = w / h; 230: c.updateProjectionMatrix(); 231: } 232: } 233: 234: render() { 235: if (this.detail.enabled) { 236: // normals and depth of detailed meshes only, for the detail pass 237: const { renderer } = this; 238: const scene = this.world.scene, camera = this.world.camera; 239: camera.layers.set(DETAIL_LAYER); 240: const bg = scene.background; 241: scene.background = null; 242: scene.overrideMaterial = this.normalMat; 243: renderer.setRenderTarget(this.normals); 244: renderer.setClearColor(0x000000, 0); 245: renderer.clear(); 246: renderer.render(scene, camera); 247: scene.overrideMaterial = null; 248: scene.background = bg; 249: camera.layers.set(0); 250: this.detail.uniforms.near.value = camera.near; 251: this.detail.uniforms.far.value = camera.far; 252: this.detail.uniforms.projInv.value.copy(camera.projectionMatrixInverse); 253: this.detail.uniforms.viewInv.value.copy(camera.matrixWorld); 254: } 255: this.grade.uniforms.time.value = (performance.now() % 10000) / 1000; 256: this.composer.render(); 257: } 258: }