--!strict -- model/md2: Quake II MD2 models ("IDP2" version 8), read into a Scene. Written from the MD2 format description in the -- Quake II source release (qfiles.h: dmdl_t) and the "MD2 file format" notes (David Henry); no code taken. -- Decode: one primitive: the triangles (welded on (vertex, texture coordinate) pairs; MD2 front faces are clockwise, -- the indices are reversed to counter-clockwise as glTF wants), POSITION of frame opts.frame (default 1), -- TEXCOORD_0 = s / skinwidth, t / skinheight (top-left origin), NORMAL computed from the geometry (the stored -- indexes into Quake's normal table are kept as meta.source.normalIndexes = false: that table is not -- redistributable here); every frame as a morph target (codecs/model/Rig: weight k = 1 shows frame k) and one -- animation per frame-name group ("run1".."run6" -> "run", 10 frames per second, LINEAR); the skins as texture -- URIs (the first is the material's base colour texture; mesh.extras.skins lists all). Units are Quake units, z up. -- meta.source = { format, frames, frameNames, skinWidth, skinHeight }. -- Not supported: the GL command list (the triangles carry the same geometry), writing. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") local Sniff = require("@formats/core/Sniff") local Types = require("@formats/Types") local Rig = require("@formats/codecs/model/Rig") -- ------------------------------------------------------------------------------------------------ contract local Md2 = { name = "md2", category = "model", extensions = { "md2" }, mime = { "model/x-md2" }, deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/model/Rig" }, caps = { decode = true, encode = false, morph = true, animation = true }, } -- ------------------------------------------------------------------------------------------------ types export type DecodeOptions = { frame: number?, -- the frame used as the base POSITION (1-based, default 1) morph: boolean?, -- every frame as a morph target + the animations (default true) fps: number?, -- animation frame rate (default 10) maxVertices: number?, -- frames x vertices (default 2^24) } -- ------------------------------------------------------------------------------------------------ constants local Scene = Types.Scene local readu8, readu16, readi16, readf32, writef32, writeu32 = buffer.readu8, buffer.readu16, buffer.readi16, buffer.readf32, buffer.writef32, buffer.writeu32 -- ------------------------------------------------------------------------------------------------ sniff function Md2.sniff(r: Bytes.Reader): number if r:remaining() < 68 then return 0 end return if Sniff.magic(r, "IDP2") == 1 and r:peekU8(4) == 8 then 1 else 0 end -- ------------------------------------------------------------------------------------------------ decode -- group frame names by their name without trailing digits, in file order local function groups(names: { string }, fps: number): { Rig.Range } local out: { Rig.Range } = {} for i, n in names do local stem = string.match(n, "^(.-)_?%d*$") or n if stem == "" then stem = n end local last = out[#out] if last and last.name == stem and last.last == i - 1 then last.last = i else table.insert(out, { name = stem, first = i, last = i, fps = fps }) end end return out end function Md2.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?) local size = r.limit - r.base if not r:match("IDP2") then return nil, Err.new(if size < 4 then Err.TRUNCATED else Err.CORRUPT, "bad magic", 0) end local h: { number } = {} for i = 1, 16 do h[i] = r:i32le() end if r.err then return nil, r.err end local version, sw, sh, frameSize, nSkins, nXyz, nSt, nTris, _, nFrames, oSkins, oSt, oTris, oFrames = table.unpack(h, 1, 14) if version ~= 8 then return nil, Err.new(Err.UNSUPPORTED, "MD2 version " .. version, 4) end if nSkins < 0 or nXyz <= 0 or nSt < 0 or nTris < 0 or nFrames <= 0 or sw < 0 or sh < 0 then return nil, Err.new(Err.CORRUPT, "bad counts", 8) end if nFrames * nXyz > (opts and opts.maxVertices or 2 ^ 24) then return nil, Err.new(Err.LIMIT, "too many vertices") end local function inside(o: number, n: number): boolean return o >= 0 and n >= 0 and o + n <= size end if not (inside(oSkins, nSkins * 64) and inside(oSt, nSt * 4) and inside(oTris, nTris * 12) and inside(oFrames, nFrames * frameSize)) then return nil, Err.new(Err.TRUNCATED, "a section runs past the end", 44) end if frameSize < 40 + nXyz * 4 then return nil, Err.new(Err.CORRUPT, "frame size too small", 16) end local b, base = r.buf, r.base -- skins local skins: { string } = {} for i = 0, nSkins - 1 do local s = buffer.readstring(b, base + oSkins + i * 64, 64) table.insert(skins, (string.match(s, "^[^%z]*")) :: string) end -- weld (xyz, st) pairs local key: { [number]: number } = {} local vx: { number }, vst: { number } = {}, {} local indices: { number } = {} for t = 0, nTris - 1 do local o = base + oTris + t * 12 local corner: { number } = {} for c = 0, 2 do local xi, si = readu16(b, o + c * 2), readu16(b, o + 6 + c * 2) if xi >= nXyz or (nSt > 0 and si >= nSt) then return nil, Err.new(Err.CORRUPT, "triangle " .. t .. " index out of range", oTris + t * 12) end local k = xi * 65536 + si local v = key[k] if not v then table.insert(vx, xi) table.insert(vst, si) v = #vx - 1 key[k] = v end corner[c + 1] = v end table.insert(indices, corner[3]) table.insert(indices, corner[2]) table.insert(indices, corner[1]) end local nv = #vx local uv = buffer.create(nv * 8) for i = 1, nv do if nSt > 0 then local o = base + oSt + vst[i] * 4 writef32(uv, (i - 1) * 8, readi16(b, o) / math.max(sw, 1)) writef32(uv, (i - 1) * 8 + 4, readi16(b, o + 2) / math.max(sh, 1)) end end -- frames local frames: { buffer } = {} local normals: { buffer } = {} local names: { string } = {} local morph = not (opts and opts.morph == false) local want = math.clamp(math.floor(opts and opts.frame or 1), 1, nFrames) for f = 1, nFrames do if morph or f == want then local o = base + oFrames + (f - 1) * frameSize local sx, sy, sz = readf32(b, o), readf32(b, o + 4), readf32(b, o + 8) local tx, ty, tz = readf32(b, o + 12), readf32(b, o + 16), readf32(b, o + 20) local pos = buffer.create(nv * 12) for i = 1, nv do local p = o + 40 + vx[i] * 4 writef32(pos, (i - 1) * 12, readu8(b, p) * sx + tx) writef32(pos, (i - 1) * 12 + 4, readu8(b, p + 1) * sy + ty) writef32(pos, (i - 1) * 12 + 8, readu8(b, p + 2) * sz + tz) end frames[f] = pos normals[f] = Rig.normals(pos, nv, indices) end local nm = buffer.readstring(b, base + oFrames + (f - 1) * frameSize + 24, 16) names[f] = (string.match(nm, "^[^%z]*")) :: string end local ib = buffer.create(#indices * 4) for i, v in indices do writeu32(ib, (i - 1) * 4, v) end local prim: Types.Primitive = { attributes = { POSITION = Scene.accessor(frames[want], "f32", 3, nv), NORMAL = Scene.accessor(normals[want], "f32", 3, nv), TEXCOORD_0 = Scene.accessor(uv, "f32", 2, nv), }, indices = Scene.accessor(ib, "u32", 1, #indices), mode = "triangles", } local scene = Scene.new() scene.meta.up = "z" if #skins > 0 then for _, s in skins do table.insert(scene.textures, { name = s, uri = s }) end local mat = Scene.material(skins[1]) mat.metallic = 0 mat.baseColorTexture = { texture = 1 } table.insert(scene.materials, mat) prim.material = 1 end scene.meshes[1] = { primitives = { prim }, extras = { skins = skins } } scene.nodes[1] = { children = {}, mesh = 1 } scene.roots[1] = 1 if morph then local fr: { buffer } = {} for f = 1, nFrames do fr[f] = frames[f] end Rig.morph(prim, fr, normals) local w = table.create(nFrames, 0) scene.meshes[1].weights = w Rig.morphAnimation(scene, 1, nFrames, groups(names, opts and opts.fps or 10), "LINEAR") end scene.meta.source = { format = "md2", frames = nFrames, frameNames = names, skinWidth = sw, skinHeight = sh, normalIndexes = false } return scene, nil end return Md2