Files

256 lines
10 KiB
Luau

-- model/pmx tests: assimp's Alicia_blade.pmx and generated PMX 2.0 / 2.1 / PMD models against assimp and the generator.
-- Fixtures: tools/fixtures/pmx.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Pmx = require("@rosetta/model/pmx")
local MeshOps = require("@rosetta/ops/MeshOps")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Cases = require("@tests/gen/pmx_cases")
local Pmds = require("@tests/gen/pmx_pmd")
local t = T.suite("pmx")
local function decode(data: any, opts: any?): (any, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Pmx.decode(Bytes.reader(b), opts)
end
local function near(a: number, b: number, eps: number): boolean
return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b))
end
t:eq(Pmx.name, "pmx", "name")
t:eq(Pmx.category, "model", "category")
t:check(Pmx.caps.decode and not Pmx.caps.encode, "contract")
local function faces(scene: any): (number, { number })
local n, per = 0, {}
for i, p in scene.meshes[1].primitives do
per[i] = p.indices.count // 3
n += per[i]
end
return n, per
end
local function bounds(scene: any): ({ number }, { number })
return MeshOps.bounds(MeshOps.merge(scene))
end
-- a file vertex in the decoded primitives: position (z negated) and uv
local function findVertex(scene: any, pos: { number }, uv: { number }): (any, number?)
for _, p in scene.meshes[1].primitives do
local P, U = p.attributes.POSITION, p.attributes.TEXCOORD_0
for v = 0, P.count - 1 do
local x, y, z = buffer.readf32(P.data, v * 12), buffer.readf32(P.data, v * 12 + 4), buffer.readf32(P.data, v * 12 + 8)
if near(x, pos[1], 1e-6) and near(y, pos[2], 1e-6) and near(z, -pos[3], 1e-6) and near(buffer.readf32(U.data, v * 8), uv[1], 1e-6) and near(buffer.readf32(U.data, v * 8 + 4), uv[2], 1e-6) then
return p, v
end
end
end
return nil, nil
end
local scenes = {}
local checked = { assimp = 0, expect = 0 }
for i, c in Cases do
local scene, err = decode(c.data, { files = c.files })
t:check(scene, c.name .. ": decode: " .. tostring(err))
if not scene then
continue
end
scenes[i] = scene
local a = c.assimp
local nf, per = faces(scene)
if not a.skip then
t:eq(#scene.meshes[1].primitives, a.meshes, c.name .. ": meshes (assimp)")
t:eq(nf, a.faces, c.name .. ": faces (assimp)")
t:eq(#scene.materials, a.materials, c.name .. ": materials (assimp)")
local ok = true
for k, pm in a.perMesh do
ok = ok and per[k] == pm.faces
end
t:check(ok, c.name .. ": faces per material (assimp)")
t:eq(#scene.skins[1].joints * #scene.meshes[1].primitives, a.bones, c.name .. ": bones x meshes (assimp)")
if a.min then
local lo, hi = bounds(scene)
local okb = true
for k = 1, 3 do
okb = okb and near(lo[k], a.min[k], 1e-5) and near(hi[k], a.max[k], 1e-5)
end
t:check(okb, string.format("%s: world bounds (assimp) %s / %s", c.name, table.concat(lo, " "), table.concat(hi, " ")))
end
checked.assimp += 1
end
local x = c.expect
if x then
local src = scene.meta.source
t:eq(src.version, x.version, c.name .. ": version")
t:eq(src.name, x.name, c.name .. ": model name")
t:eq(#scene.skins[1].joints, x.bones, c.name .. ": bones")
t:eq(#scene.nodes, 2 + x.bones, c.name .. ": nodes")
t:eq(#scene.textures, x.textures, c.name .. ": textures")
t:eq(#src.rigidBodies, x.rigid, c.name .. ": rigid bodies")
t:eq(#src.joints, x.joints, c.name .. ": joints")
t:eq(#src.displayFrames, x.frames, c.name .. ": display frames")
local mesh = scene.meshes[1]
local vertexMorphs = x.morphTypes["1"] or 0
t:eq(#(mesh.extras.targetNames or {}), vertexMorphs, c.name .. ": vertex morphs -> targets")
t:eq(#mesh.primitives[1].targets, vertexMorphs, c.name .. ": targets per primitive")
t:eq(#((mesh.extras.pmx or {}).morphs or {}), x.morphs - vertexMorphs, c.name .. ": other morphs in extras")
t:check(x.uva == 0 or mesh.primitives[1].attributes["_UVA" .. x.uva] ~= nil, c.name .. ": additional UVs")
-- unique vertices: a material boundary can split a ring, so at least the file's count
local nv = 0
for _, p in mesh.primitives do
nv += p.attributes.POSITION.count
end
t:check(nv >= x.vertices and nv <= x.vertices * 2, c.name .. ": vertices " .. nv .. " / " .. x.vertices)
for _, s in x.samples do
local p, v = findVertex(scene, s.pos, s.uv)
t:check(p ~= nil, c.name .. ": vertex " .. s.index .. " found")
if p and v then
local J, W = p.attributes.JOINTS_0, p.attributes.WEIGHTS_0
local want: { [number]: number } = {}
local sum = 0
local ws = if s.deform == 1 or s.deform == 3 then { s.weights[1], 1 - s.weights[1] } elseif s.deform == 0 then { 1 } else s.weights
for k, b in s.bones do
if b >= 0 and ws[k] and ws[k] > 0 then
want[b] = (want[b] or 0) + ws[k]
sum += ws[k]
end
end
local got: { [number]: number } = {}
for k = 0, 3 do
local w = buffer.readf32(W.data, (v * 4 + k) * 4)
if w > 0 then
local j = buffer.readu16(J.data, (v * 4 + k) * 2)
got[j] = (got[j] or 0) + w
end
end
local ok = true
for b, w in want do
ok = ok and got[b] ~= nil and near(got[b], w / sum, 1e-5)
end
t:check(ok, c.name .. ": vertex " .. s.index .. " weights (deform " .. s.deform .. ")")
if s.deform == 3 then
t:check(p.attributes._SDEF_C ~= nil and buffer.readu8(p.attributes._DEFORM.data, v) == 3, c.name .. ": SDEF attributes")
end
end
end
-- the texture named tex\body.png came from files["tex/body.png"]
t:check(scene.textures[1].uri == "tex/body.png" and scene.textures[1].data ~= nil and scene.textures[1].mime == "image/png", c.name .. ": texture data")
local m1 = scene.materials[1]
t:check(m1.baseColorTexture and m1.baseColorTexture.texture == 1 and m1.doubleSided, c.name .. ": material 1 texture, no-cull")
t:check(m1.extras.pmx.sphereTexture == 2 and m1.extras.pmx.sphereMode == "multiply" and m1.extras.pmx.toonShared == 3, c.name .. ": sphere / toon")
t:check(scene.materials[2].alphaMode == "blend", c.name .. ": translucent material")
local ik = nil
for _, n in scene.nodes do
if n.extras and n.extras.pmx and n.extras.pmx.ik then
ik = n.extras.pmx.ik
end
end
t:check(ik and ik.loops == 40 and #ik.links == 2, c.name .. ": IK bone")
checked.expect += 1
end
end
t:note("%d files: %d against assimp, %d against the generator", #Cases, checked.assimp, checked.expect)
-- skins: every joint's inverse bind matrix undoes its world translation
do
local s = scenes[2]
if s then
local skin = s.skins[1]
local ok = true
local world = {}
local function walk(n: number, parent: { number })
local w = Types.Scene.mul(parent, Types.Scene.nodeMatrix(s.nodes[n]))
world[n] = w
for _, c in s.nodes[n].children do
walk(c, w)
end
end
walk(1, Types.Scene.IDENTITY)
for k, j in skin.joints do
local w = world[j]
for c = 0, 2 do
ok = ok and near(buffer.readf32(skin.inverseBindMatrices.data, ((k - 1) * 16 + 12 + c) * 4), -w[13 + c], 1e-5)
end
end
t:check(ok, "inverse bind matrices = -world bone positions")
end
end
for _, c in Pmds do
local s, err = decode(c.data)
t:check(s, c.name .. ": decode: " .. tostring(err))
local ref = scenes[c.pmx]
if s and ref then
t:check(s.meta.source.pmd == true and s.meta.source.format == "pmx", c.name .. ": meta.source")
local a, pa = faces(s)
local b, pb = faces(ref)
t:eq(a, b, c.name .. ": faces (= PMX)")
t:eq(#pa, #pb, c.name .. ": primitives (= PMX)")
t:eq(#s.skins[1].joints, #ref.skins[1].joints, c.name .. ": bones (= PMX)")
t:eq(#s.meshes[1].extras.targetNames, #ref.meshes[1].extras.targetNames, c.name .. ": vertex morphs (= PMX)")
local lo, hi = bounds(s)
local lo2, hi2 = bounds(ref)
local ok = true
for k = 1, 3 do
ok = ok and near(lo[k], lo2[k], 1e-6) and near(hi[k], hi2[k], 1e-6)
end
t:check(ok, c.name .. ": bounds (= PMX)")
-- the same morph deltas
local d1 = s.meshes[1].primitives[1].targets[1].POSITION
local d2 = ref.meshes[1].primitives[1].targets[1].POSITION
t:check(d1.count == d2.count and buffer.tostring(d1.data) == buffer.tostring(d2.data), c.name .. ": morph deltas (= PMX)")
t:eq(s.meshes[1].extras.targetNames[1], "up", c.name .. ": English morph names")
t:eq(s.materials[1].extras.pmx.sphereMode, "multiply", c.name .. ": texture*sphere name")
end
end
do
local c = Cases[2]
t:eq(F.detect(c.data), "pmx", "F.detect pmx")
t:eq(F.detect(Pmds[1].data), "pmx", "F.detect pmd")
local s = F.load(c.data, { files = c.files })
t:check(s and s.kind == "scene" and s.meta.up == "y", "F.load")
local _, e1 = decode(string.sub(c.data, 1, 300))
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
local _, e2 = decode("PMX " .. "\0\0\64\64" .. string.sub(c.data, 9))
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 2.0 -> 3.0: " .. tostring(e2))
local _, e3 = decode(string.sub(c.data, 1, 11) .. "\3" .. string.sub(c.data, 13))
t:check(e3 and e3.code == Err.CORRUPT, "index size 3: " .. tostring(e3))
local _, e4 = decode(c.data, { maxVertices = 10 })
t:check(e4 and e4.code == Err.LIMIT, "maxVertices: " .. tostring(e4))
local _, e5 = decode(c.data, { maxBytes = 100 })
t:check(e5 and e5.code == Err.LIMIT, "maxBytes (morph targets): " .. tostring(e5))
local s2 = decode(c.data, { maxBytes = 100, morphs = false })
t:check(s2 and s2.meshes[1].primitives[1].targets == nil, "morphs = false")
local _, e6 = decode("Pmd\0\0\0\64" .. string.rep("\0", 300))
t:check(e6 and e6.code == Err.UNSUPPORTED, "PMD version 2: " .. tostring(e6))
local _, e7 = decode(string.sub(Pmds[1].data, 1, 400))
t:check(e7 and (e7.code == Err.TRUNCATED or e7.code == Err.CORRUPT), "PMD truncated: " .. tostring(e7))
end
do
local samples = {}
for _, c in Cases do
if #c.data < 120000 then
table.insert(samples, c.data)
end
end
for _, c in Pmds do
table.insert(samples, c.data)
end
t:fuzz("decode", function(b: buffer)
local s = Pmx.decode(Bytes.reader(b))
if s then
MeshOps.merge(s)
end
return s
end, samples, { mutations = 16, keepHeader = 16 })
end
t:done()