84 lines
3.5 KiB
Luau
84 lines
3.5 KiB
Luau
--!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 Input = require("@formats/core/Input")
|
|
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/Input", "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 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 got = if type(name) == "string" then Input.sibling(opts, name) else nil
|
|
local src = if got then buffer.tostring(got) 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
|