ws-game: model/md2 (Quake II MD2 -> Scene: frames as morph targets + grouped animations, skins as URIs); codecs/model/Rig.normals
This commit is contained in:
1 parent
ec32f359bc
commit
de1d2c2bba
8 files changed
+499
No files matched your search
@@ -0,0 +1,51 @@
|
||||
-- tests/model/_soup: helpers for the game model tests: a primitive's triangle soup (positions, morph target frames,
|
||||
-- texture coordinates in index order) and a tolerant comparison against assimp's soup.
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Soup = {}
|
||||
|
||||
-- positions(prim, target?) -> f32 x3 buffer of the corners in index order (target k adds morph target k's deltas)
|
||||
function Soup.positions(prim: any, target: number?): buffer
|
||||
local pos = Types.Scene.reader(prim.attributes.POSITION) :: any
|
||||
local d = if target then Types.Scene.reader(prim.targets[target].POSITION) :: any else nil
|
||||
local idx = prim.indices
|
||||
local n = idx.count
|
||||
local out = buffer.create(n * 12)
|
||||
for k = 0, n - 1 do
|
||||
local v = buffer.readu32(idx.data, k * 4)
|
||||
for c = 0, 2 do
|
||||
buffer.writef32(out, k * 12 + c * 4, pos(v, c) + (if d then d(v, c) else 0))
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- uvs(prim, set?, flip?) -> { u, v, ... } of the corners in index order (flip: v -> 1 - v, assimp's convention)
|
||||
function Soup.uvs(prim: any, set: number?, flip: boolean?): { number }
|
||||
local tc = Types.Scene.reader(prim.attributes["TEXCOORD_" .. (set or 0)]) :: any
|
||||
local idx = prim.indices
|
||||
local out = {}
|
||||
for k = 0, idx.count - 1 do
|
||||
local v = buffer.readu32(idx.data, k * 4)
|
||||
table.insert(out, tc(v, 0))
|
||||
table.insert(out, if flip then 1 - tc(v, 1) else tc(v, 1))
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- close(a: buffer f32 | {number}, b: {number}, tol) -> true | false, first index that differs
|
||||
function Soup.close(a: any, b: { number }, tol: number): (boolean, number?)
|
||||
local n = if type(a) == "buffer" then buffer.len(a) // 4 else #a
|
||||
if n ~= #b then
|
||||
return false, -1
|
||||
end
|
||||
for i = 1, n do
|
||||
local x = if type(a) == "buffer" then buffer.readf32(a, (i - 1) * 4) else a[i]
|
||||
if math.abs(x - b[i]) > tol * math.max(1, math.abs(b[i])) then
|
||||
return false, i
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
return Soup
|
||||
@@ -0,0 +1,83 @@
|
||||
-- model/md2 tests: MD2 files written by tools/fixtures/md2.py decode to assimp's frame-0 triangle soup (positions,
|
||||
-- texture coordinates) and to the writer's exact positions for every frame (base + morph target deltas), frame
|
||||
-- groups as animations, skins as texture URIs, opts.frame / morph = false, errors, the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Md2 = require("@formats/model/md2")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Soup = require("@tests/model/_soup")
|
||||
local Fx = require("@tests/gen/md2")
|
||||
|
||||
local t = T.suite("md2")
|
||||
t:eq(Md2.name, "md2", "name")
|
||||
|
||||
for _, c in Fx do
|
||||
local s, err = Md2.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(s, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "md2", c.name .. ": F.detect")
|
||||
local prim = s.meshes[1].primitives[1]
|
||||
t:eq(prim.indices.count, c.tris * 3, c.name .. ": triangles")
|
||||
-- assimp, frame 0
|
||||
local ok, at = Soup.close(Soup.positions(prim), c.assimp.pos, 1e-5)
|
||||
t:check(ok, c.name .. ": positions against assimp (" .. tostring(at) .. ")")
|
||||
local uok, uat = Soup.close(Soup.uvs(prim, 0, true), c.assimp.uv, 1e-6)
|
||||
t:check(uok, c.name .. ": texture coordinates against assimp (" .. tostring(uat) .. ")")
|
||||
-- every frame, exact
|
||||
t:eq(Crc32.of(Soup.positions(prim)), c.crcs[1], c.name .. ": frame 1 exact")
|
||||
for k = 1, c.frames do
|
||||
local f = Soup.positions(prim, k)
|
||||
-- base + delta in f32 can differ from the frame by an ulp; compare to the decoded frame directly
|
||||
local frame = Md2.decode(Bytes.reader(buffer.fromstring(c.data)), { frame = k, morph = false }) :: any
|
||||
t:eq(Crc32.of(Soup.positions(frame.meshes[1].primitives[1])), c.crcs[k], c.name .. ": frame " .. k .. " exact (opts.frame)")
|
||||
local fok = Soup.close(f, (function()
|
||||
local list = {}
|
||||
local fp = Soup.positions(frame.meshes[1].primitives[1])
|
||||
for i = 0, buffer.len(fp) // 4 - 1 do
|
||||
list[i + 1] = buffer.readf32(fp, i * 4)
|
||||
end
|
||||
return list
|
||||
end)(), 1e-5)
|
||||
t:check(fok, c.name .. ": morph target " .. k .. " = frame " .. k)
|
||||
end
|
||||
-- animations from the frame names
|
||||
local groups = {}
|
||||
for _, a in s.animations do
|
||||
table.insert(groups, a.name)
|
||||
end
|
||||
t:eq(#s.meshes[1].weights, c.frames, c.name .. ": one weight per frame")
|
||||
t:eq(s.meta.source.frameNames[1], c.names[1], c.name .. ": frame names")
|
||||
if c.name == "monster" then
|
||||
t:eq(table.concat(groups, ","), "stand,run,attack,pain", c.name .. ": animation groups")
|
||||
t:eq(s.animations[2].channels[1].input.count, 4, c.name .. ": run has 4 keys")
|
||||
end
|
||||
-- skins
|
||||
t:eq(#s.textures, #c.skins, c.name .. ": skins")
|
||||
if #c.skins > 0 then
|
||||
t:eq(s.textures[1].uri, c.skins[1], c.name .. ": skin uri")
|
||||
t:eq(s.materials[prim.material].baseColorTexture.texture, 1, c.name .. ": material")
|
||||
end
|
||||
t:eq(s.meta.up, "z", c.name .. ": z up")
|
||||
end
|
||||
|
||||
do
|
||||
local c = Fx[1]
|
||||
local lean = Md2.decode(Bytes.reader(buffer.fromstring(c.data)), { morph = false }) :: any
|
||||
t:check(lean.meshes[1].primitives[1].targets == nil and #lean.animations == 0, "morph = false")
|
||||
local _, e1 = Md2.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 300))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local bad = buffer.fromstring(c.data)
|
||||
buffer.writei32(bad, 4, 7)
|
||||
local _, e2 = Md2.decode(Bytes.reader(bad))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 7")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Md2.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 80 })
|
||||
t:done()
|
||||
Reference in new issue
Block a user