ws-game: model/md3 (Quake III MD3 -> Scene: surfaces as primitives, frames as morph targets, tags as nodes with channels, shaders as URIs)
This commit is contained in:
1 parent
897a386c4f
commit
50eb827897
7 files changed
+500
-3
No files matched your search
@@ -1,7 +1,7 @@
|
||||
# Shared helpers for the Quake-family model fixtures (md2.py, md3.py, mdl.py): assimp's triangle soup.
|
||||
# assimp_soup(ctx, data, ext) -> { pos = [floats], uv = [floats], meshes, faces } | { failed = True }: every triangle
|
||||
# mesh of `assimp export -f assjson`, faces in order, each face's corners in assimp's index order (positions x3,
|
||||
# texture coordinates u, v of set 0)
|
||||
# texture coordinates u, v of set 0); per = [ { name, start } ]: each mesh's name and first face
|
||||
import json, os, shutil, subprocess, tempfile
|
||||
|
||||
|
||||
@@ -18,11 +18,12 @@ def assimp_soup(ctx, data, ext):
|
||||
j = json.load(open(out))
|
||||
finally:
|
||||
shutil.rmtree(tmp)
|
||||
pos, uv, faces, meshes = [], [], 0, 0
|
||||
pos, uv, faces, meshes, per = [], [], 0, 0, []
|
||||
for m in j.get("meshes", []):
|
||||
if m.get("primitivetypes") != 4:
|
||||
continue
|
||||
meshes += 1
|
||||
per.append({"name": m.get("name", ""), "start": faces})
|
||||
v = m["vertices"]
|
||||
t = (m.get("texturecoords") or [[]])[0]
|
||||
nc = (m.get("numuvcomponents") or [2])[0]
|
||||
@@ -32,4 +33,4 @@ def assimp_soup(ctx, data, ext):
|
||||
pos += v[i * 3:i * 3 + 3]
|
||||
if t:
|
||||
uv += t[i * nc:i * nc + 2]
|
||||
return {"pos": pos, "uv": uv, "faces": faces, "meshes": meshes}
|
||||
return {"pos": pos, "uv": uv, "faces": faces, "meshes": meshes, "per": per}
|
||||
@@ -0,0 +1,93 @@
|
||||
# Quake III MD3 fixtures (tools/fixtures.py): MD3 files written here from the md3Header_t / md3Surface_t layout
|
||||
# (several surfaces with shaders, frames of 1/64-unit i16 positions with lat / long normals, tags per frame);
|
||||
# references: assimp's triangle soup of frame 0 (every surface: positions, texture coordinates) and this writer's
|
||||
# exact positions and decoded normals of every frame (CRC-32 of the f32 soup in our corner order), tag origins.
|
||||
# tests/gen/md3.luau { { name, data, frames, surfaces = { { name, shader, tris, crcs, ncrcs } }, tags = { ... },
|
||||
# assimp = { pos, uv, meshes } } }
|
||||
import importlib.util, math, os, struct, zlib
|
||||
|
||||
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 f32(x):
|
||||
return struct.unpack("<f", struct.pack("<f", x))[0]
|
||||
|
||||
|
||||
def normal(n):
|
||||
lat = ((n >> 8) & 255) * math.pi / 128
|
||||
lng = (n & 255) * math.pi / 128
|
||||
return (math.cos(lat) * math.sin(lng), math.sin(lat) * math.sin(lng), math.cos(lng))
|
||||
|
||||
|
||||
def surface(rng, name, shader, nframes, nverts, ntris):
|
||||
tris = [tuple(rng.sample(range(nverts), 3)) for _ in range(ntris)]
|
||||
st = [(f32(rng.uniform(0, 1)), f32(rng.uniform(0, 1))) for _ in range(nverts)]
|
||||
xyz = [[(rng.randrange(-3000, 3000), rng.randrange(-3000, 3000), rng.randrange(-3000, 3000), rng.randrange(65536)) for _ in range(nverts)] for _ in range(nframes)]
|
||||
o_tri = 108
|
||||
o_sh = o_tri + 12 * ntris
|
||||
o_st = o_sh + 68
|
||||
o_xyz = o_st + 8 * nverts
|
||||
o_end = o_xyz + 8 * nverts * nframes
|
||||
out = struct.pack("<4s64siiiiiiiiii", b"IDP3", name.encode(), 0, nframes, 1, nverts, ntris, o_tri, o_sh, o_st, o_xyz, o_end)
|
||||
for t in tris:
|
||||
out += struct.pack("<3i", *t)
|
||||
out += struct.pack("<64si", shader.encode(), 0)
|
||||
for s in st:
|
||||
out += struct.pack("<2f", *s)
|
||||
for f in xyz:
|
||||
for x, y, z, n in f:
|
||||
out += struct.pack("<3hH", x, y, z, n)
|
||||
crcs, ncrcs = [], []
|
||||
for f in xyz:
|
||||
soup, nsoup = b"", b""
|
||||
for t in tris:
|
||||
for c in (2, 1, 0):
|
||||
x, y, z, n = f[t[c]]
|
||||
soup += struct.pack("<3f", x / 64, y / 64, z / 64)
|
||||
nsoup += struct.pack("<3f", *normal(n))
|
||||
crcs.append(zlib.crc32(soup))
|
||||
ncrcs.append(zlib.crc32(nsoup))
|
||||
return out, {"name": name, "shader": shader, "tris": ntris, "crcs": crcs, "ncrcs": ncrcs}
|
||||
|
||||
|
||||
def md3(rng, nframes, surfs, ntags):
|
||||
frames = b""
|
||||
for i in range(nframes):
|
||||
frames += struct.pack("<10f16s", *([0.0] * 10), b"frame%d" % i)
|
||||
tags, tagref = b"", []
|
||||
for f in range(nframes):
|
||||
for k in range(ntags):
|
||||
o = tuple(f32(rng.uniform(-10, 10)) for _ in range(3))
|
||||
ang = rng.uniform(0, math.pi)
|
||||
ax = ((math.cos(ang), math.sin(ang), 0.0), (-math.sin(ang), math.cos(ang), 0.0), (0.0, 0.0, 1.0))
|
||||
tags += struct.pack("<64s12f", b"tag_%d" % k, *o, *[c for a in ax for c in a])
|
||||
tagref.append({"name": "tag_%d" % k, "frame": f + 1, "origin": list(o), "angle": ang})
|
||||
body, info = b"", []
|
||||
for args in surfs:
|
||||
s, i = surface(rng, args[0], args[1], nframes, args[2], args[3])
|
||||
body += s
|
||||
info.append(i)
|
||||
o_frames = 108
|
||||
o_tags = o_frames + len(frames)
|
||||
o_surf = o_tags + len(tags)
|
||||
o_end = o_surf + len(body)
|
||||
head = struct.pack("<4si64siiiiiiiii", b"IDP3", 15, b"models/test/test.md3", 0, nframes, ntags, len(surfs), 0, o_frames, o_tags, o_surf, o_end)
|
||||
return head + frames + tags + body, info, tagref
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(63)
|
||||
R = ref(ctx)
|
||||
out = []
|
||||
for name, nframes, surfs, ntags in [("two-surfaces", 4, [("body", "models/test/body.tga", 30, 40), ("head", "models/test/head", 12, 10)], 2),
|
||||
("one-frame", 1, [("s", "textures/x.tga", 8, 6)], 0)]:
|
||||
data, info, tags = md3(rng, nframes, surfs, ntags)
|
||||
out.append({"name": name, "data": data, "frames": nframes, "surfaces": info, "tags": tags, "assimp": R.assimp_soup(ctx, data, "md3")})
|
||||
ctx.emit("md3", out)
|
||||
Reference in new issue
Block a user