83 lines
3.9 KiB
Luau
83 lines
3.9 KiB
Luau
--!strict
|
|
-- model/iqm: Inter-Quake Model (IQM version 2), read and write. New code from the IQM format description (iqm.txt /
|
|
-- iqm.h of Lee Salzman's IQM SDK, public domain); checked against assimp (which reads IQM geometry).
|
|
-- Reads meshes (one node + mesh each; material strings -> materials whose baseColorTexture uri is that string, as
|
|
-- IQM tools name the texture there), vertex arrays in every IQM format (position, texcoord (top-left origin, as
|
|
-- glTF), normal, tangent, blend indexes / weights -> JOINTS_0 / WEIGHTS_0, colour, custom arrays as _<name>), joints
|
|
-- (a node each, bind pose TRS, one skin with the inverse bind matrices), poses + frames + anims (16-bit channels
|
|
-- dequantized into translation / rotation / scale channels at the anim's frame rate; loop flags and frame ranges in
|
|
-- meta.source.animations) and per-frame bounds (meta.source.bounds). IQM's clockwise triangles become glTF's
|
|
-- counter-clockwise ones; IQM is z-up (meta.up = "z", nothing is rotated).
|
|
-- Writes the same (see model/iqm/Write): any Scene's triangle primitives, skins merged into one skeleton, sampled and
|
|
-- quantized animations, bounds. Not supported: IQM version 1, adjacency and extensions (skipped on read, not written).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Types = require("@rosetta/Types")
|
|
local Read = require("@rosetta/model/iqm/Read")
|
|
local Write = require("@rosetta/model/iqm/Write")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Iqm = {
|
|
name = "iqm",
|
|
category = "model",
|
|
extensions = { "iqm" },
|
|
mime = { "model/x-iqm" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Mat4", "codecs/model/Rig", "codecs/model/Sampler" },
|
|
caps = { decode = true, encode = true, materials = true, skins = true, animation = true, vertexColors = true },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type DecodeOptions = {
|
|
maxVertices: number?, -- refuse more vertices or triangles (default 2^25)
|
|
}
|
|
export type EncodeOptions = {
|
|
framerate: number?, -- animation sampling rate (default: the key spacing when uniform, else 30 per second)
|
|
comment: string?, -- stored in the comment block
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constants
|
|
local MAGIC = "INTERQUAKEMODEL\0"
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function Iqm.sniff(r: Bytes.Reader): number
|
|
return if r:peek(16) == MAGIC then 1 else 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function Iqm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
|
if r.limit - r.base < 124 then
|
|
return nil, Err.new(Err.TRUNCATED, "shorter than the header", r.base)
|
|
end
|
|
if r:peek(16) ~= MAGIC then
|
|
return nil, Err.new(Err.CORRUPT, "no INTERQUAKEMODEL magic", r.base)
|
|
end
|
|
local ok, scene, msg = pcall(Read.scene, r.buf, r.base, r.limit, opts)
|
|
if not ok then
|
|
return nil, Err.new(Err.CORRUPT, "bad model data: " .. tostring(scene))
|
|
end
|
|
if not scene then
|
|
return nil, Err.from(msg)
|
|
end
|
|
return scene, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Iqm.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
if type(scene) ~= "table" or scene.kind ~= "scene" then
|
|
return nil, Err.new(Err.ARG, "needs a Scene")
|
|
end
|
|
local ok, out, msg = pcall(Write.iqm, scene, opts)
|
|
if not ok then
|
|
return nil, Err.new(Err.ARG, "bad scene: " .. tostring(out))
|
|
end
|
|
if not out then
|
|
local e = Err.from(msg)
|
|
return nil, Err.new(if e.code == Err.CORRUPT then Err.ARG else e.code, e.message)
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
return Iqm
|