# Roblox FileMesh fixtures (tools/fixtures.py) for model/robloxmesh. No reference tool reads .mesh files, so this # generator is an independent writer + reader written from the spec (MaximumADHD's "Roblox Mesh Format" notes, # devforum 326114): it writes meshes of every version (1.00 / 1.01 ASCII, 2.00 with 36- and 40-byte vertices, 3.00 / # 3.01 with LODs, 4.00 / 4.01 with bones, skinning and several subsets, 5.00 with a FACS blob, 6.00 / 7.00 chunked # with LODS, SKINNING, FACS and HSRAVIS chunks, a 7.00 Draco COREMESH) with the values they hold, and reads our # encoder's output (tools/fixtures/_robloxmesh_encode.luau under the luau CLI) back, as verdicts. # tests/gen/robloxmesh.luau { cases = { { name, data, version, vertices, faces, pos, nrm, uv, tan?, col?, lods = # { idx crc ... }, joints?, weights?, bones? = { { name, parent, cf } }, facs?, chunks?, # unsupported? } }, ours = { { name, ok, detail } } } import json, math, os, random, shutil, struct, subprocess, zlib INPUTS = ["src/Rosetta/model/robloxmesh", "tools/fixtures/_robloxmesh_encode.luau"] def f32(x): return struct.unpack("= 128 else v # ---------------------------------------------------------------------------------------------------- geometry def sphere(rng, rings, segs, radius=2.0): verts, faces = [], [] for i in range(rings + 1): th = math.pi * i / rings for j in range(segs + 1): ph = 2 * math.pi * j / segs n = [math.sin(th) * math.cos(ph), math.cos(th), math.sin(th) * math.sin(ph)] verts.append({ "pos": [f32(c * radius + rng.uniform(-0.01, 0.01)) for c in n], "nrm": [f32(c) for c in n], "uv": [f32(j / segs), f32(i / rings)], "tan": [rng.randrange(-127, 128) for _ in range(3)] + [rng.choice((-127, 127))], "col": [rng.randrange(256) for _ in range(4)], }) for i in range(rings): for j in range(segs): a = i * (segs + 1) + j b = a + segs + 1 faces += [(a, b, a + 1), (a + 1, b, b + 1)] return verts, faces def vbytes(v, size): out = pk("f", v["pos"] + v["nrm"] + v["uv"]) + bytes(t & 255 for t in v["tan"]) if size >= 40: out += bytes(v["col"]) return out def fbytes(faces): return b"".join(pk("I", list(f)) for f in faces) def tangent_f32(t): return [f32(max(-1.0, min(1.0, c / 127))) for c in t] def expect_core(verts, faces, lods, colors, tangents=True): exp = { "vertices": len(verts), "faces": len(faces), "pos": crc(b"".join(pk("f", v["pos"]) for v in verts)), "nrm": crc(b"".join(pk("f", v["nrm"]) for v in verts)), "uv": crc(b"".join(pk("f", v["uv"]) for v in verts)), "lods": [crc(fbytes(faces[lods[k]:lods[k + 1]])) for k in range(len(lods) - 1)], } if tangents: exp["tan"] = crc(b"".join(pk("f", tangent_f32(v["tan"])) for v in verts)) if colors: exp["col"] = crc(b"".join(bytes(v["col"]) for v in verts)) return exp # ---------------------------------------------------------------------------------------------------- bones / skinning def rot_y(a): c, s = math.cos(a), math.sin(a) return [c, 0, s, 0, 1, 0, -s, 0, c] def make_bones(rng, n): bones = [] for i in range(n): parent = None if i == 0 else rng.randrange(i) r = rot_y(rng.uniform(-3, 3)) cf = [f32(x) for x in r + [rng.uniform(-2, 2), rng.uniform(-2, 2), rng.uniform(-2, 2)]] bones.append({"name": "Bone%02d" % i if i else "Root", "parent": parent, "cf": cf, "culling": f32(rng.uniform(0, 5)), "lodParent": 0xFFFF}) return bones def make_skin(rng, verts, faces, bones, segs=12): """influences per vertex (1..4 bones from a window that moves with the sphere ring, so a third of the faces needs at most ~20 bones), then subsets by face thirds; vertices are duplicated per subset (the format needs contiguous vertex ranges). Returns new verts, faces, subsets, envelopes, the expected global joints / weights""" nb = len(bones) infl = [] for v in range(len(verts)): ring = v // (segs + 1) window = [(ring * 4 + k) % nb for k in range(min(8, nb))] k = rng.randrange(1, 5) bs = rng.sample(window, min(k, len(window))) w = [rng.randrange(1, 100) for _ in bs] tot = sum(w) wb = [x * 255 // tot for x in w] wb[0] += 255 - sum(wb) infl.append(list(zip(bs, wb))) nverts, nfaces, subsets, env, joints, weights = [], [], [], [], [], [] chunk = max(1, len(faces) // 3) for s0 in range(0, len(faces), chunk): sub_faces = faces[s0:s0 + chunk] used = [] for f in sub_faces: for v in f: if v not in used: used.append(v) sub_bones = sorted({b for v in used for b, _ in infl[v]}) assert len(sub_bones) <= 26 loc = {b: i for i, b in enumerate(sub_bones)} remap = {} vb = len(nverts) for v in used: remap[v] = len(nverts) nverts.append(verts[v]) pairs = infl[v] + [(None, 0)] * (4 - len(infl[v])) env.append(bytes(loc[b] if b is not None else 0 for b, _ in pairs) + bytes(w for _, w in pairs)) joints.append([b if b is not None else 0 for b, _ in pairs]) weights.append([w for _, w in pairs]) for f in sub_faces: nfaces.append(tuple(remap[v] for v in f)) subsets.append({"facesBegin": s0, "facesLength": len(sub_faces), "vertsBegin": vb, "vertsLength": len(nverts) - vb, "bones": sub_bones}) return nverts, nfaces, subsets, env, joints, weights def bone_bytes(bones): names = b"" out = b"" for b in bones: idx = len(names) names += b["name"].encode() + b"\0" out += struct.pack("= 40 else [255] * 4 verts.append({"pos": list(v[:3]), "nrm": list(v[3:6]), "uv": list(v[6:8]), "tan": t, "col": col}) p += nv * vs env = [] if version == "4.00" and nb > 0: env = [rest[p + 8 * i:p + 8 * i + 8] for i in range(nv)] p += 8 * nv faces = [struct.unpack_from("<3I", rest, p + 12 * i) for i in range(nf)] p += 12 * nf if version == "4.00": lods = list(struct.unpack_from("<%dI" % nl, rest, p)) p += 4 * nl bones = [] for i in range(nb): ni, par, lp, cull = struct.unpack_from(" ok, detail""" a, b = src, out if len(b["verts"]) < 1: return False, "no vertices" if sorted(corners(a)) != sorted(corners(b)): return False, "LOD 0 triangles differ" if b["version"] == "4.00": if len(a["lods"]) != len(b["lods"]) and not b["bones"]: return False, "LOD count %d, want %d" % (len(b["lods"]), len(a["lods"])) if not b["bones"]: for k in range(len(a["lods"]) - 1): if sorted(corners(a, k)) != sorted(corners(b, k)): return False, "LOD %d differs" % k if a["bones"]: if [x["name"] for x in a["bones"]] != [x["name"] for x in b["bones"]] or [x["parent"] for x in a["bones"]] != [x["parent"] for x in b["bones"]]: return False, "bones differ" for x, y in zip(a["bones"], b["bones"]): if any(abs(p - q) > 1e-4 for p, q in zip(x["cf"], y["cf"])): return False, "bone %s transform differs" % x["name"] ia, ib = vertex_influences(a), vertex_influences(b) key = lambda m, i: tuple(m["verts"][i]["pos"] + m["verts"][i]["uv"]) want = {key(a, i): ia[i] for i in range(len(a["verts"]))} for i in range(len(b["verts"])): w = want.get(key(b, i)) if w is None or ib[i] is None: return False, "vertex %d has no influences" % i if set(w) != set(ib[i]) or any(abs(w[j] - ib[i][j]) > 1 for j in w): return False, "vertex %d influences %r, want %r" % (i, ib[i], w) return True, "%d vertices, %d faces, %d bones, %d subsets" % (len(b["verts"]), len(b["faces"]), len(b["bones"]), len(b["subsets"])) def luau_cmd(): exe = shutil.which("luau") return [exe] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau"] def generate(ctx): rng = random.Random(1337) cases = [] sv, sf = sphere(rng, 8, 12) for version in ("1.00", "1.01"): data, exp = write_v1(version, sv, sf[:60]) cases.append(dict(exp, name="v" + version, data=data, version=version)) for colors in (False, True): data, exp = write_v2(sv, sf, colors) cases.append(dict(exp, name="v2.00-%d" % (40 if colors else 36), data=data, version="2.00")) lods = [0, len(sf) // 2, len(sf) * 3 // 4, len(sf)] for version in ("3.00", "3.01"): data, exp = write_v3(version, sv, sf, lods) cases.append(dict(exp, name="v" + version, data=data, version=version)) # skinned: 6 bones (one subset of all of them) and 40 bones (several subsets of up to 26) bones6 = make_bones(rng, 6) bones40 = make_bones(rng, 40) for version in ("4.00", "4.01", "5.00"): for bones in (bones6, bones40): nv, nf, subs, env, joints, weights = make_skin(rng, sv, sf, bones) lods2 = [0, len(nf)] facs = bytes(rng.randrange(256) for _ in range(77)) if version == "5.00" else None data, exp = write_v45(version, nv, nf, lods2, bones, subs, env, facs) exp = skin_expect(exp, bones, joints, weights) if facs: exp["facs"] = crc(facs) cases.append(dict(exp, name="v%s-%dbones" % (version, len(bones)), data=data, version=version)) # unskinned 4.00 with LODs data, exp = write_v45("4.00", sv, sf, lods, [], [], []) cases.append(dict(exp, name="v4.00-lods", data=data, version="4.00")) for version in ("6.00", "7.00"): nv, nf, subs, env, joints, weights = make_skin(rng, sv, sf, bones40) facs = bytes(rng.randrange(256) for _ in range(40)) data, exp = write_v67(version, nv, nf, [0, len(nf) // 2, len(nf)], bones40, subs, env, facs, hsr=True) exp = skin_expect(exp, bones40, joints, weights) exp["facs"] = crc(facs) exp["chunks"] = ["HSRAVIS"] cases.append(dict(exp, name="v" + version, data=data, version=version)) data, _ = write_v67("7.00", sv, sf, [0, len(sf)], [], [], [], None, hsr=False, draco=True) cases.append({"name": "v7.00-draco", "data": data, "version": "7.00", "unsupported": True}) ctx.emit("robloxmesh", {"cases": cases}) # ours: every decodable case re-encoded as 2.00 and 4.00, read back here enc = subprocess.run(luau_cmd() + [os.path.join(ctx.root, "tools", "fixtures", "_robloxmesh_encode.luau")], capture_output=True, cwd=ctx.root) if enc.returncode != 0: raise RuntimeError("_robloxmesh_encode: " + enc.stderr.decode(errors="replace")[-3000:]) originals = {} byname = {c["name"]: c for c in cases} ours = [] for line in enc.stdout.decode().split("\n"): if not line.strip(): continue label, hexdata = line.split(" ", 1) name, version = label.rsplit("/", 1) try: out = read_mesh(bytes.fromhex(hexdata.strip())) if name not in originals: originals[name] = read_original(byname[name]) ok, detail = check_ours(originals[name], out) except Exception as e: # a reader failure is a verdict, not a fixture error ok, detail = False, "read: %r" % e ours.append({"name": label, "ok": ok, "detail": detail}) ctx.emit("robloxmesh", {"cases": cases, "ours": ours}) def read_original(case): """a fixture case as the record read_mesh gives (version 1 / 3 / 5 / 6 / 7 are rewritten through the 4.00 shape)""" data = case["data"] line, rest = data.split(b"\n", 1) version = line.decode().split()[1] if version in ("1.00", "1.01"): text = rest.decode().split("\n", 1)[1] vals = [float(x) for x in text.replace("][", ",").strip("[]").split(",")] scale = 0.5 if version == "1.00" else 1 verts = [] for i in range(0, len(vals), 9): verts.append({"pos": [f32(c * scale) for c in vals[i:i + 3]], "nrm": [f32(c) for c in vals[i + 3:i + 6]], "uv": [f32(vals[i + 6]), f32(1 - vals[i + 7])]}) nf = len(verts) // 3 return {"version": version, "verts": verts, "faces": [(3 * i, 3 * i + 1, 3 * i + 2) for i in range(nf)], "lods": [0, nf], "bones": [], "subsets": [], "env": []} if version == "2.00": return read_mesh(data) if version in ("3.00", "3.01"): hs, vs, fs, ls, nl, nv, nf = struct.unpack_from("