# Doom 3 MD5 fixtures (tools/fixtures.py): an .md5mesh (random joint hierarchy in model space, two meshes whose # vertices hang off 1-3 weights) and two .md5anim files (animated component flags, baseframe, frames) written here # from the format notes; references: assimp's triangle soup of the bind pose (positions, texture coordinates) and # this writer's own values (bind positions from the weights, joints, every frame's parent-relative pose). # tests/gen/md5.luau { mesh = { data, joints, positions = { per mesh: { x, y, z, ... } }, tris, assimp }, # anims = { { name, data, frames, rate, poses = { [frame] = { [joint] = { t, q } } } } } } import importlib.util, math, os, random INPUTS = ["tools/fixtures/_quake_ref.py"] def ref(ctx): spec = importlib.util.spec_from_file_location("_quake_ref", os.path.join(ctx.root, "tools", "fixtures", "_quake_ref.py")) m = importlib.util.module_from_spec(spec) spec.loader.exec_module(m) return m def r6(v): return float("%.6f" % v) def quat_w(x, y, z): t = 1 - x * x - y * y - z * z return 0.0 if t < 0 else -math.sqrt(t) def rand_q(rng): # a random unit quaternion with w <= 0 (stored as x, y, z) while True: q = [rng.uniform(-1, 1) for _ in range(4)] n = math.sqrt(sum(c * c for c in q)) if n > 0.1: q = [c / n for c in q] if q[3] > 0: q = [-c for c in q] return [r6(q[0]), r6(q[1]), r6(q[2])] def rotate(q, v): qx, qy, qz, qw = q vx, vy, vz = v tx, ty, tz = 2 * (qy * vz - qz * vy), 2 * (qz * vx - qx * vz), 2 * (qx * vy - qy * vx) return (vx + qw * tx + (qy * tz - qz * ty), vy + qw * ty + (qz * tx - qx * tz), vz + qw * tz + (qx * ty - qy * tx)) def mesh(rng, njoints=5): joints = [] lines = ["MD5Version 10", 'commandline "written by tools/fixtures/md5.py"', "", "numJoints %d" % njoints, "numMeshes 2", "", "joints {"] for i in range(njoints): parent = -1 if i == 0 else rng.randrange(i) p = [r6(rng.uniform(-30, 30)) for _ in range(3)] q = rand_q(rng) joints.append({"name": "joint%d" % i, "parent": parent + 1, "pos": p, "q": q + [quat_w(*q)]}) lines.append('\t"joint%d"\t%d ( %s ) ( %s )\t\t// comment' % (i, parent, " ".join("%.6f" % c for c in p), " ".join("%.6f" % c for c in q))) lines.append("}") positions, tris_all, uvs = [], [], [] for m in range(2): nverts, ntris = 8 + m * 4, 6 + m * 2 verts, weights = [], [] for v in range(nverts): k = rng.randrange(1, 4) ws = [rng.uniform(0.2, 1) for _ in range(k)] s = sum(ws) ws = [r6(w / s) for w in ws] start = len(weights) for j, w in zip(rng.sample(range(njoints), k), ws): # distinct joints per vertex (assimp 6 crashes on repeats) weights.append((j, w, [r6(rng.uniform(-5, 5)) for _ in range(3)])) verts.append(([r6(rng.random()), r6(rng.random())], start, k)) # every vertex in some triangle (assimp 6's MD5 loader corrupts memory on unused vertices) order = list(range(nverts)) rng.shuffle(order) tris = [(order[i % nverts], order[(i + 1) % nverts], order[(i + 2) % nverts]) for i in range(0, nverts, 3)] tris += [tuple(rng.sample(range(nverts), 3)) for _ in range(ntris - len(tris))] lines += ["", "mesh {", '\tshader "models/test/skin%d"' % m, "", "\tnumverts %d" % nverts] for i, (st, start, k) in enumerate(verts): lines.append("\tvert %d ( %.6f %.6f ) %d %d" % (i, st[0], st[1], start, k)) lines.append("\n\tnumtris %d" % ntris) for i, t in enumerate(tris): lines.append("\ttri %d %d %d %d" % (i, *t)) lines.append("\n\tnumweights %d" % len(weights)) for i, (j, b, p) in enumerate(weights): lines.append("\tweight %d %d %.6f ( %s )" % (i, j, b, " ".join("%.6f" % c for c in p))) lines.append("}") pos = [] for st, start, k in verts: acc = [0.0, 0.0, 0.0] for j, b, p in weights[start:start + k]: jt = joints[j] r = rotate(jt["q"], p) for c in range(3): acc[c] += (jt["pos"][c] + r[c]) * b pos += acc positions.append(pos) tris_all.append(len(tris)) uvs.append([c for st, s, k in verts for c in st]) return "\n".join(lines) + "\n", {"joints": joints, "positions": positions, "tris": tris_all, "uvs": uvs} def anim(rng, joints, nframes, rate, name): nj = len(joints) flags = [rng.randrange(64) for _ in range(nj)] base = [[r6(rng.uniform(-10, 10)) for _ in range(3)] + rand_q(rng) for _ in range(nj)] starts, n = [], 0 for f in flags: starts.append(n) n += bin(f).count("1") lines = ["MD5Version 10", 'commandline ""', "", "numFrames %d" % nframes, "numJoints %d" % nj, "frameRate %d" % rate, "numAnimatedComponents %d" % n, "", "hierarchy {"] for i, j in enumerate(joints): lines.append('\t"%s"\t%d %d %d' % (j["name"], j["parent"] - 1, flags[i], starts[i])) lines += ["}", "", "bounds {"] + ["\t( -1 -1 -1 ) ( 1 1 1 )"] * nframes + ["}", "", "baseframe {"] for b in base: lines.append("\t( %s ) ( %s )" % (" ".join("%.6f" % c for c in b[:3]), " ".join("%.6f" % c for c in b[3:]))) lines.append("}") poses = [] for f in range(nframes): vals, pose = [], [] for i in range(nj): v = list(base[i]) for bit in range(6): if flags[i] & (1 << bit): if bit < 3: x = r6(rng.uniform(-10, 10)) else: x = r6(rng.uniform(-0.5, 0.5)) v[bit] = x vals.append(x) pose.append({"t": v[:3], "q": v[3:] + [quat_w(*v[3:])]}) poses.append(pose) lines += ["", "frame %d {" % f, "\t" + " ".join("%.6f" % x for x in vals), "}"] return "\n".join(lines) + "\n", {"name": name, "frames": nframes, "rate": rate, "poses": poses} def generate(ctx): rng = ctx.rng(97) R = ref(ctx) text, info = mesh(rng) data = text.encode() info.update({"data": data, "assimp": R.assimp_soup(ctx, data, "md5mesh")}) anims = [] for name, nf, rate in (("idle", 4, 24), ("walk", 7, 30)): t, a = anim(rng, info["joints"], nf, rate, name) a["data"] = t.encode() anims.append(a) ctx.emit("md5", {"mesh": info, "anims": anims})