223 lines
7.3 KiB
Luau
223 lines
7.3 KiB
Luau
--!strict
|
|
-- model/mdl/Quake: Quake 1 MDL ("IDPO" version 6) to a Scene. Written from the Quake specs (Olivier Montanuy et
|
|
-- al., "Quake Specs 3.4", section 5) and the modelgen.h layout of the Quake source release; no code taken.
|
|
-- Skins (single and grouped: the first picture of a group) become texture.image through a 768-byte palette;
|
|
-- texture coordinates on the seam of back-facing triangles move half a skin width (the vertices are welded per
|
|
-- side); simple and grouped frames become morph targets with one animation per frame-name group.
|
|
local Types = require("@formats/Types")
|
|
local Pixels = require("@formats/codecs/image/Pixels")
|
|
local Rig = require("@formats/codecs/model/Rig")
|
|
|
|
local Quake = {}
|
|
|
|
local Scene = Types.Scene
|
|
local readi32, readf32, readu8, writef32, writeu32 = buffer.readi32, buffer.readf32, buffer.readu8, buffer.writef32, buffer.writeu32
|
|
|
|
export type Options = { frame: number?, morph: boolean?, fps: number?, maxVertices: number?, maxPixels: number? }
|
|
|
|
local function name16(b: buffer, at: number): string
|
|
return (string.match(buffer.readstring(b, at, 16), "^[^%z]*")) :: string
|
|
end
|
|
|
|
-- decode(b, base, size, palette, opts) -> Scene | nil, message (offsets relative to base)
|
|
function Quake.decode(b: buffer, base: number, size: number, palette: buffer?, opts: Options?): (Types.Scene?, string?)
|
|
if size < 84 then
|
|
return nil, "truncated header"
|
|
end
|
|
local scale = { readf32(b, base + 8), readf32(b, base + 12), readf32(b, base + 16) }
|
|
local origin = { readf32(b, base + 20), readf32(b, base + 24), readf32(b, base + 28) }
|
|
local nSkins, sw, sh = readi32(b, base + 48), readi32(b, base + 52), readi32(b, base + 56)
|
|
local nVerts, nTris, nFrames = readi32(b, base + 60), readi32(b, base + 64), readi32(b, base + 68)
|
|
if nSkins < 0 or sw < 0 or sh < 0 or nVerts <= 0 or nTris < 0 or nFrames <= 0 or nSkins > 1024 or nFrames > 65536 then
|
|
return nil, "bad counts"
|
|
end
|
|
if sw * sh > (opts and opts.maxPixels or 2 ^ 24) then
|
|
return nil, "limit: skin too big"
|
|
end
|
|
if nFrames * nVerts > (opts and opts.maxVertices or 2 ^ 24) then
|
|
return nil, "limit: too many vertices"
|
|
end
|
|
local p = 84
|
|
local function need(n: number): boolean
|
|
return n >= 0 and p + n <= size
|
|
end
|
|
-- skins
|
|
local pal = if palette then Pixels.palette(palette, 0, 256) else nil
|
|
local skins: { Types.Image } = {}
|
|
local px = sw * sh
|
|
for _ = 1, nSkins do
|
|
if not need(4) then
|
|
return nil, "truncated skins"
|
|
end
|
|
local group = readi32(b, base + p)
|
|
p += 4
|
|
local count = 1
|
|
if group ~= 0 then
|
|
if not need(4) then
|
|
return nil, "truncated skin group"
|
|
end
|
|
count = readi32(b, base + p)
|
|
p += 4
|
|
if count <= 0 or count > 256 or not need(count * 4) then
|
|
return nil, "bad skin group"
|
|
end
|
|
p += count * 4
|
|
end
|
|
if not need(count * px) then
|
|
return nil, "truncated skin pixels"
|
|
end
|
|
local img = Types.Image.new(sw, sh)
|
|
if pal then
|
|
Pixels.indexed(b, base + p, sw, 8, img.pixels, sw, sh, true, pal)
|
|
else
|
|
for i = 0, px - 1 do
|
|
local v = readu8(b, base + p + i)
|
|
writeu32(img.pixels, i * 4, 0xFF000000 + v * 0x10101)
|
|
end
|
|
end
|
|
img.meta.source = { format = "mdl", palette = pal ~= nil, frames = count }
|
|
table.insert(skins, img)
|
|
p += count * px
|
|
end
|
|
-- texture coordinates and triangles
|
|
if not need(nVerts * 12 + nTris * 16) then
|
|
return nil, "truncated vertices / triangles"
|
|
end
|
|
local stAt = p
|
|
p += nVerts * 12
|
|
local weld: { [number]: number } = {}
|
|
local src: { number }, uvs: { number } = {}, {}
|
|
local indices: { number } = {}
|
|
for t = 0, nTris - 1 do
|
|
local o = base + p + t * 16
|
|
local front = readi32(b, o) ~= 0
|
|
local c: { number } = {}
|
|
for k = 1, 3 do
|
|
local v = readi32(b, o + k * 4)
|
|
if v < 0 or v >= nVerts then
|
|
return nil, "triangle vertex out of range"
|
|
end
|
|
local so = base + stAt + v * 12
|
|
local seam = readi32(b, so) ~= 0 and not front
|
|
local key = v * 2 + (if seam then 1 else 0)
|
|
local id = weld[key]
|
|
if not id then
|
|
table.insert(src, v)
|
|
local s = readi32(b, so + 4) + (if seam then sw / 2 else 0)
|
|
table.insert(uvs, (s + 0.5) / math.max(sw, 1))
|
|
table.insert(uvs, (readi32(b, so + 8) + 0.5) / math.max(sh, 1))
|
|
id = #src - 1
|
|
weld[key] = id
|
|
end
|
|
c[k] = id
|
|
end
|
|
-- Quake's front faces are clockwise
|
|
table.insert(indices, c[3])
|
|
table.insert(indices, c[2])
|
|
table.insert(indices, c[1])
|
|
end
|
|
p += nTris * 16
|
|
-- frames: simple (type 0) or groups of simple frames
|
|
local frameAt: { number } = {}
|
|
local names: { string } = {}
|
|
local fsize = 24 + nVerts * 4
|
|
for _ = 1, nFrames do
|
|
if not need(4) then
|
|
return nil, "truncated frames"
|
|
end
|
|
local kind = readi32(b, base + p)
|
|
p += 4
|
|
if kind == 0 then
|
|
if not need(fsize) then
|
|
return nil, "truncated frame"
|
|
end
|
|
table.insert(frameAt, p + 8)
|
|
table.insert(names, name16(b, base + p + 8))
|
|
p += fsize
|
|
else
|
|
if not need(12) then
|
|
return nil, "truncated frame group"
|
|
end
|
|
local count = readi32(b, base + p)
|
|
if count <= 0 or count > 4096 or not need(12 + count * 4 + count * fsize) then
|
|
return nil, "bad frame group"
|
|
end
|
|
p += 12 + count * 4
|
|
for _ = 1, count do
|
|
table.insert(frameAt, p + 8)
|
|
table.insert(names, name16(b, base + p + 8))
|
|
p += fsize
|
|
end
|
|
end
|
|
end
|
|
local nv = #src
|
|
local total = #frameAt
|
|
local morph = not (opts and opts.morph == false)
|
|
local want = math.clamp(math.floor(opts and opts.frame or 1), 1, total)
|
|
local positions: { buffer }, normals: { buffer } = {}, {}
|
|
for f, at in frameAt do
|
|
if morph or f == want then
|
|
local pos = buffer.create(nv * 12)
|
|
local vo = base + at + 16
|
|
for i, v in src do
|
|
for k = 0, 2 do
|
|
writef32(pos, (i - 1) * 12 + k * 4, readu8(b, vo + v * 4 + k) * scale[k + 1] + origin[k + 1])
|
|
end
|
|
end
|
|
positions[f] = pos
|
|
normals[f] = Rig.normals(pos, nv, indices)
|
|
end
|
|
end
|
|
local ib = buffer.create(#indices * 4)
|
|
for i, v in indices do
|
|
writeu32(ib, (i - 1) * 4, v)
|
|
end
|
|
local uvb = buffer.create(nv * 8)
|
|
for i, v in uvs do
|
|
writef32(uvb, (i - 1) * 4, v)
|
|
end
|
|
local prim: Types.Primitive = {
|
|
attributes = {
|
|
POSITION = Scene.accessor(positions[want], "f32", 3, nv),
|
|
NORMAL = Scene.accessor(normals[want], "f32", 3, nv),
|
|
TEXCOORD_0 = Scene.accessor(uvb, "f32", 2, nv),
|
|
},
|
|
indices = Scene.accessor(ib, "u32", 1, #indices),
|
|
mode = "triangles",
|
|
}
|
|
local scene = Scene.new()
|
|
scene.meta.up = "z"
|
|
for i, img in skins do
|
|
table.insert(scene.textures, { name = "skin " .. i, image = img, sampler = { filter = "nearest" :: any } })
|
|
end
|
|
if #skins > 0 then
|
|
local mat = Scene.material("skin")
|
|
mat.metallic = 0
|
|
mat.baseColorTexture = { texture = 1 }
|
|
scene.materials[1] = mat
|
|
prim.material = 1
|
|
end
|
|
scene.meshes[1] = { primitives = { prim } }
|
|
scene.nodes[1] = { children = {}, mesh = 1 }
|
|
scene.roots[1] = 1
|
|
if morph then
|
|
Rig.morph(prim, positions, normals)
|
|
scene.meshes[1].weights = table.create(total, 0)
|
|
local ranges: { Rig.Range } = {}
|
|
for i, n in names do
|
|
local stem = string.match(n, "^(.-)%d*$") or n
|
|
local last = ranges[#ranges]
|
|
if last and last.name == stem then
|
|
last.last = i
|
|
else
|
|
table.insert(ranges, { name = if stem ~= "" then stem else n, first = i, last = i, fps = opts and opts.fps or 10 })
|
|
end
|
|
end
|
|
Rig.morphAnimation(scene, 1, total, ranges, "LINEAR")
|
|
end
|
|
scene.meta.source = { format = "mdl", variant = "quake", frames = total, frameNames = names, skinWidth = sw, skinHeight = sh, palette = pal ~= nil }
|
|
return scene, nil
|
|
end
|
|
|
|
return Quake
|