Files
rosetta/tools/fixtures/robloxmesh_draco.py
4DBug 2768075add ws-3d3: model/robloxmesh: Draco-compressed COREMESH (6.00 / 7.00, chunk v2) through codecs/model/Draco
Parse (native) gets the decode as a pcall-guarded lim.core callback from init, so
nothing throws through a native frame. Tests: draco_encoder streams of a UV sphere
and a coloured PLY at three levels wrapped in v6 / v7 chunks, byte for byte against
libdraco and equal to the same mesh as COREMESH v1; round trips; fuzz. The old
UNSUPPORTED fixture (garbage bytes in a v2 chunk) now expects CORRUPT. roblox.md
updated.
2026-10-31 16:03:50 -05:00

71 lines
4.0 KiB
Python

# Roblox mesh 6.00 / 7.00 with a Draco-compressed COREMESH (v2: a u32 size, then the stream) (tools/fixtures.py). The
# Draco streams are made by draco_encoder 1.5.7 (nix) from a generated UV sphere (OBJ: positions, normals, uvs) and a
# coloured PLY, at two compression levels; each is wrapped with LODS (and for one an empty SKINNING) chunks, and the
# same mesh is also written with a plain COREMESH v1 from libdraco's own decode (tools/fixtures/_draco_dump.cc), so
# the test compares the two decodes and the values against libdraco.
# tests/gen/robloxmesh_draco.luau { { name, data, plain, points, faces, indices = crc, pos, nrm?, uv?, col? = crc } }
import importlib.util, os, struct, subprocess, tempfile, zlib
INPUTS = ["tools/fixtures/_draco_dump.cc", "tools/fixtures/draco.py"]
def chunk(name, version, body):
return name.ljust(8, b"\0") + struct.pack("<II", version, len(body)) + body
def generate(ctx):
spec = importlib.util.spec_from_file_location("draco_fx", os.path.join(ctx.root, "tools", "fixtures", "draco.py"))
dfx = importlib.util.module_from_spec(spec)
spec.loader.exec_module(dfx)
draco = dfx.store_path("draco")
tmp = tempfile.mkdtemp(prefix="formats-rbxdraco-")
dump = os.path.join(tmp, "dump")
ctx.nix("gcc", ["g++", "-O1", "-std=c++17", "-I" + os.path.join(draco, "include"), os.path.join(ctx.root, "tools", "fixtures", "_draco_dump.cc"),
"-L" + os.path.join(draco, "lib"), "-l:libdraco.a", "-o", dump])
rng = ctx.rng(7)
inputs = {"sphere.obj": dfx.uv_sphere(10, 16), "colored.ply": dfx.colored_ply(rng, 90)}
cases = []
for iname, text in inputs.items():
ipath = os.path.join(tmp, iname)
open(ipath, "w").write(text)
for cl in (0, 7, 10):
out = os.path.join(tmp, "%s.%d.drc" % (iname, cl))
subprocess.run([os.path.join(draco, "bin", "draco_encoder"), "-i", ipath, "-o", out, "-cl", str(cl)], capture_output=True, check=True)
drc = open(out, "rb").read()
lines = subprocess.run([dump, out], capture_output=True, check=True).stdout.decode().split("\n")
npts, faces, attrs = 0, b"", {}
for line in lines:
p = line.split(" ")
if p[0] == "POINTS":
npts = int(p[1])
elif p[0] == "FACES":
faces = bytes.fromhex(p[2])
elif p[0] == "ATTR":
t = int(p[1])
if t not in attrs:
attrs[t] = (int(p[2]), int(p[3]), bytes.fromhex(p[6]))
# the plain (v1) layout of the same mesh: 40-byte vertices (pos, normal, uv, tangent, colour)
def get(t, i, size):
a = attrs.get(t)
return a[2][i * size:(i + 1) * size] if a else bytes(size)
verts = b""
for i in range(npts):
col = attrs.get(2)
verts += get(0, i, 12) + get(1, i, 12) + get(3, i, 8) + bytes(4) + (col[2][i * 4:i * 4 + 4] if col else b"\xff\xff\xff\xff")
nf = len(faces) // 12
lods = chunk(b"LODS", 1, struct.pack("<HBI", 3, 1, 2) + struct.pack("<II", 0, nf))
for version in ("6.00", "7.00"):
head = ("version %s\n" % version).encode()
data = head + chunk(b"COREMESH", 2, struct.pack("<I", len(drc)) + drc) + lods
plain = head + chunk(b"COREMESH", 1, struct.pack("<I", npts) + verts + struct.pack("<I", nf) + faces) + lods
c = {"name": "%s cl%d v%s" % (iname, cl, version), "data": data, "plain": plain, "points": npts, "faces": nf,
"indices": zlib.crc32(faces), "pos": zlib.crc32(attrs[0][2])}
if 1 in attrs:
c["nrm"] = zlib.crc32(attrs[1][2])
if 3 in attrs:
c["uv"] = zlib.crc32(attrs[3][2])
if 2 in attrs and attrs[2][0] == 2 and attrs[2][1] == 4:
c["col"] = zlib.crc32(attrs[2][2])
cases.append(c)
ctx.emit("robloxmesh_draco", cases)