ws-game: model/md5 (Doom 3 md5mesh + md5anim: skeleton, weighted bind pose, skin, animations via opts.anims / resolve / files; standalone md5anim)

This commit is contained in:
4DBug committed 2026-10-31 14:52:15 -05:00
1 parent 08045ba813
commit 9b2b50fe75
6 files changed
+847

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
--!strict
-- model/md5: Doom 3 / Quake 4 MD5 models (MD5Version 10 text): .md5mesh (skeleton, skin, meshes) and .md5anim
-- (skeletal animation), read into a Scene through model/md5/Read. A .md5mesh loads its animations from
-- opts.anims = { "idle.md5anim", ... } (each found through opts.files[name] or opts.resolve(name); joints matched by
-- name; the animation is named after the file without its extension); a missing one fails with UNSUPPORTED unless
-- opts.skipMissing. A .md5anim alone loads as its skeleton (baseframe pose) with the animation. Units are Doom
-- units, z up. Not supported: writing, .md5camera.
-- ------------------------------------------------------------------------------------------------ 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 Read = require("@formats/model/md5/Read")
-- ------------------------------------------------------------------------------------------------ contract
local Md5 = {
name = "md5",
category = "model",
extensions = { "md5mesh", "md5anim" },
mime = { "model/x-md5mesh", "model/x-md5anim" },
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/model/Rig" },
caps = { decode = true, encode = false, skins = true, animation = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
anims: { string }?, -- .md5anim files to load with a .md5mesh
resolve: ((uri: string) -> (buffer | string)?)?,
files: { [string]: buffer | string }?,
skipMissing: boolean?, -- ignore animations that cannot be found (default false: UNSUPPORTED)
name: string?, -- the file name (names a standalone animation)
maxVertices: number?, -- default 2^24
}
-- ------------------------------------------------------------------------------------------------ sniff
function Md5.sniff(r: Bytes.Reader): number
local head = Sniff.text(r, 512, true)
head = string.gsub(head, "^//[^\n]*\n%s*", "")
return if string.match(head, "^MD5Version%s+%d+") then 1 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
local function fetch(opts: DecodeOptions, uri: string): string?
local v: any = nil
if type(opts.files) == "table" then
v = opts.files[uri]
end
if v == nil and type(opts.resolve) == "function" then
local ok, got = pcall(opts.resolve, uri)
v = if ok then got else nil
end
if type(v) == "buffer" then
return buffer.tostring(v)
end
return if type(v) == "string" then v else nil
end
local function stem(name: string): string
local base = string.match(name, "([^/\\]+)$") or name
return (string.gsub(base, "%.[^.]*$", ""))
end
function Md5.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
local text = Sniff.dropBom(r:string(r:remaining()))
if string.find(text, "numAnimatedComponents", 1, true) then
local a, msg = Read.anim(text)
if not a then
return nil, Err.from(msg)
end
return Read.animScene(a, if opts and type(opts.name) == "string" then stem(opts.name) else "animation"), nil
end
local scene, byName = Read.mesh(text, opts :: any)
if not scene then
return nil, Err.from(byName)
end
if opts and type(opts.anims) == "table" then
for _, name in opts.anims do
local src = if type(name) == "string" then fetch(opts, name) else nil
if not src then
if not opts.skipMissing then
return nil, Err.new(Err.UNSUPPORTED, "animation " .. tostring(name) .. " not available (opts.resolve / opts.files)")
end
else
local a, msg = Read.anim(Sniff.dropBom(src))
if not a then
return nil, Err.from("animation " .. name .. ": " .. tostring(msg))
end
Read.addAnim(scene, byName, a, stem(name))
end
end
end
return scene, nil
end
return Md5