Files

307 lines
14 KiB
Luau

-- model/lwo tests: assimp's LightWave models (LWO2, LWOB, LXOB) against assimp, and a hand-built LWO2.
-- Fixtures: tools/fixtures/lwo.py.
local T = require("@tests/T")
local Lwo = require("@rosetta/model/lwo")
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/lwo_cases")
local t = T.suite("lwo")
local function decode(data: any, opts: any?): (any, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Lwo.decode(Bytes.reader(b), opts)
end
t:eq(Lwo.name, "lwo", "name")
t:eq(Lwo.category, "model", "category")
t:check(Lwo.caps.decode and not Lwo.caps.encode, "caps")
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
local function counts(scene: any): (number, number, number)
local meshes, faces, corners = 0, 0, 0
for _, m in scene.meshes do
local mats = {}
for _, p in m.primitives do
if not mats[p.material] then
mats[p.material] = true
meshes += 1
end
local n = if p.indices then p.indices.count else p.attributes.POSITION.count
corners += n
faces += if p.mode == "polygons" then p.faces.count elseif p.mode == "lines" then n // 2 elseif p.mode == "points" then n else n // 3
end
end
return meshes, faces, corners
end
local function find(name: string): any
for _, c in Cases do
if c.name == name then
return c
end
end
return nil
end
local checked, scenes = 0, {}
for i, c in Cases do
local a = c.assimp
local scene, err = decode(c.data, { triangulate = false })
if a.failed then
t:note("%s: assimp rejects it; we: %s", c.name, if scene then "decoded" else tostring(err))
else
t:check(scene, c.name .. ": decode: " .. tostring(err))
if scene then
scenes[i] = scene
local meshes, faces, corners = counts(scene)
-- LXOB / LWOB files: assimp's material and surface handling differs (MATR tags, flattened detail polygons)
t:eq(meshes, a.meshes, c.name .. ": meshes (assimp)")
t:eq(faces, a.faces, c.name .. ": polygons (assimp)")
t:eq(corners, a.vertices, c.name .. ": corners (assimp)")
t:eq(#scene.materials, a.materials, c.name .. ": materials (assimp)")
if a.min then
local lo, hi = MeshOps.bounds(MeshOps.merge(scene))
local ok = lo ~= nil
for k = 1, 3 do
ok = ok and near(lo[k], a.min[k], 1e-4) and near(hi[k], a.max[k], 1e-4)
end
t:check(ok, string.format("%s: world bounds (assimp) %s / %s", c.name, if lo then table.concat(lo, " ") else "-", if hi then table.concat(hi, " ") else "-"))
end
local tri = decode(c.data)
t:check(tri and tri.meshes[1] and tri.meshes[1].primitives[1].mode ~= "polygons", c.name .. ": triangulated by default")
checked += 1
end
end
end
t:note("%d files checked against assimp", checked)
do
local c = find("models/LWO2/hierarchy.lwo")
local s = decode(c.data)
local byName = {}
for i, n in s.nodes do
byName[n.name] = i
end
local root, child0, grand = byName.RootOfHierarchy, byName.ChildOfRoot0, byName.GrandChildOfRoot0
t:check(root and child0 and grand and byName.ChildOfRoot1, "hierarchy: layer names")
t:check(table.find(s.roots, root) ~= nil and #s.roots == 1, "hierarchy: one root layer")
t:check(table.find(s.nodes[root].children, child0) ~= nil, "hierarchy: ChildOfRoot0 under RootOfHierarchy")
t:check(table.find(s.nodes[child0].children, grand) ~= nil, "hierarchy: GrandChildOfRoot0 under ChildOfRoot0")
local p = s.nodes[child0].extras.lwo.pivot
t:check(p and #p == 3, "hierarchy: pivot kept in extras")
-- weight maps -> _WEIGHT_n (no skeleton in LWO)
local prim = s.meshes[s.nodes[root].mesh].primitives[1]
t:check(prim.attributes._WEIGHT_0 and prim.attributes._WEIGHT_1, "hierarchy: weight maps as _WEIGHT_n")
t:check(prim.extras.lwo.maps._WEIGHT_0 ~= nil, "hierarchy: weight map names")
end
do
local c = find("models/LWO2/boxuv.lwo")
local s = decode(c.data)
local mat = s.materials[1]
t:check(mat.baseColorTexture, "boxuv: COLR image block -> baseColorTexture")
local tex = s.textures[mat.baseColorTexture.texture]
t:eq(tex.uri, "boxuv.png", "boxuv: texture uri")
t:eq(tex.mime, "image/png", "boxuv: texture mime")
local prim = s.meshes[1].primitives[1]
local uv = prim.attributes.TEXCOORD_0
t:check(uv and uv.count == prim.attributes.POSITION.count, "boxuv: TEXCOORD_0")
local inRange = true
local get = assert(Types.Scene.reader(uv))
for i = 0, uv.count - 1 do
local u, v = get(i, 0), get(i, 1)
inRange = inRange and u >= -1e-6 and u <= 1 + 1e-6 and v >= -1e-6 and v <= 1 + 1e-6
end
t:check(inRange, "boxuv: uvs in 0..1")
end
do
local c = find("models/LWO2/box_2vc_1unused.lwo")
local s = decode(c.data)
local any = false
for _, p in s.meshes[1].primitives do
any = any or (p.attributes.COLOR_0 ~= nil or p.attributes._COLOR_0 ~= nil)
end
t:check(any, "box_2vc: colour maps")
local mat = s.materials[1]
t:check(mat.extras.lwo.vcol ~= nil, "box_2vc: surface VCOL map name")
end
do
local c = find("models/LWO2/ModoExport_vertNormals.lwo")
local s = decode(c.data)
local prim = s.meshes[1].primitives[1]
t:check(prim.attributes.NORMAL, "Modo NORM map -> NORMAL")
local get = assert(Types.Scene.reader(prim.attributes.NORMAL))
local len = math.sqrt(get(0, 0) ^ 2 + get(0, 1) ^ 2 + get(0, 2) ^ 2)
t:check(near(len, 1, 1e-3), "Modo normals are unit length")
end
do
local c = find("models/LWO2/Subdivision.lwo")
local s = decode(c.data)
t:check(s.meshes[1].primitives[1].extras.lwo.subdivision == true, "PTCH cage flagged")
local lx = find("models/LXOB_Modo/sphereWithVertMap.lxo")
local sx = decode(lx.data)
t:eq(sx.meta.source.version, "LXOB", "LXOB version")
t:check(sx.meshes[1].primitives[1].attributes.NORMAL, "LXOB normals")
local lb = find("models/LWOB/sphere_with_mat_gloss_10pc.lwo")
local sb = decode(lb.data)
t:eq(sb.meta.source.version, "LWOB", "LWOB version")
t:check(sb.materials[1].extras.lwo.glos ~= nil, "LWOB glossiness")
end
local function be16(n: number): string
return string.char(bit32.band(bit32.rshift(n, 8), 255), bit32.band(n, 255))
end
local function be32(n: number): string
return be16(bit32.rshift(n, 16)) .. be16(bit32.band(n, 0xFFFF))
end
local function f4(x: number): string
local b = buffer.create(4)
buffer.writef32(b, 0, x)
return be32(buffer.readu32(b, 0))
end
local function s0(s: string): string
s ..= "\0"
return if #s % 2 == 1 then s .. "\0" else s
end
local function chunk(id: string, body: string): string
return id .. be32(#body) .. body .. (if #body % 2 == 1 then "\0" else "")
end
local function sub(id: string, body: string): string
return id .. be16(#body) .. body .. (if #body % 2 == 1 then "\0" else "")
end
local function form(body: string): string
return "FORM" .. be32(#body + 4) .. "LWO2" .. body
end
local function quadFile(): string
local pts = ""
for _, p in { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 }, { 2, 0, 0 }, { 3, 0, 0 } } do
pts ..= f4(p[1]) .. f4(p[2]) .. f4(p[3])
end
local pols = "FACE" .. be16(4) .. be16(0) .. be16(1) .. be16(2) .. be16(3) -- 0: quad
.. be16(2) .. be16(4) .. be16(5) -- 1: a line
.. be16(1) .. be16(5) -- 2: a point
local curv = "CURV" .. be16(2) .. be16(0) .. be16(4) -- 3: skipped
local ptch = "PTCH" .. be16(3) .. be16(0) .. be16(1) .. be16(2) -- 4: a patch
local uv = "TXUV" .. be16(2) .. s0("UV")
for i, v in { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } } do
uv ..= be16(i - 1) .. f4(v[1]) .. f4(v[2])
end
local vmad = "TXUV" .. be16(2) .. s0("UV") .. be16(0) .. be16(0) .. f4(0.5) .. f4(0.25) -- point 0 in polygon 0
local morf = "MORF" .. be16(3) .. s0("up") .. be16(2) .. f4(0) .. f4(0) .. f4(1)
local spot = "SPOT" .. be16(3) .. s0("moved") .. be16(1) .. f4(1) .. f4(2) .. f4(0)
local wght = "WGHT" .. be16(1) .. s0("bone") .. be16(3) .. f4(0.75)
local tags = s0("Base") .. s0("Child")
local base = s0("Base") .. s0("") .. sub("COLR", f4(1) .. f4(0) .. f4(0) .. be16(0)) .. sub("TRAN", f4(0.25) .. be16(0)) .. sub("SIDE", be16(3))
local blok = sub("IMAP", s0("\128") .. sub("CHAN", "COLR") .. sub("ENAB", be16(1))) .. sub("PROJ", be16(5)) .. sub("IMAG", be16(2)) .. sub("VMAP", s0("UV"))
local child = s0("Child") .. s0("Base") .. sub("LUMI", f4(0.5) .. be16(0)) .. sub("BLOK", blok)
local ptag = "SURF" .. be16(0) .. be16(0) .. be16(1) .. be16(1) .. be16(2) .. be16(1)
local ptag2 = "SURF" .. be16(0) .. be16(1)
return form(chunk("TAGS", tags) .. chunk("LAYR", be16(0) .. be16(0) .. f4(0.5) .. f4(0) .. f4(0) .. s0("Main")) .. chunk("PNTS", pts)
.. chunk("VMAP", uv) .. chunk("VMAP", morf) .. chunk("VMAP", spot) .. chunk("VMAP", wght)
.. chunk("POLS", pols) .. chunk("PTAG", ptag) .. chunk("VMAD", vmad) .. chunk("POLS", curv) .. chunk("POLS", ptch) .. chunk("PTAG", ptag2)
.. chunk("CLIP", be32(1) .. sub("STIL", s0("C:\\images\\Wood.PNG"))) .. chunk("CLIP", be32(2) .. sub("XREF", be32(1) .. s0("ref")))
.. chunk("SURF", base) .. chunk("SURF", child)
.. chunk("LAYR", be16(1) .. be16(0) .. f4(0.5) .. f4(1) .. f4(2) .. s0("Sub") .. be16(0)))
end
do
local data = quadFile()
local s, err = decode(data, { triangulate = false })
t:check(s, "hand-built: decode " .. tostring(err))
t:eq(#s.nodes, 2, "hand-built: two layers")
local main, subl = s.nodes[1], s.nodes[2]
t:eq(main.name, "Main", "hand-built: layer name")
t:check(table.find(main.children, 2) ~= nil and #s.roots == 1, "hand-built: Sub is a child of Main")
t:check(main.translation and main.translation[1] == 0.5, "hand-built: Main pivot translation")
t:check(subl.translation and subl.translation[2] == 1 and subl.translation[3] == -2, "hand-built: Sub pivot relative to Main, z negated")
t:eq(s.meta.source.skipped.CURV, 1, "hand-built: curve skipped and counted")
local mesh = s.meshes[main.mesh]
local modes = {}
for _, p in mesh.primitives do
modes[#modes + 1] = p.mode .. "/" .. p.material .. (if p.extras.lwo.subdivision then "/patch" else "")
end
t:eq(table.concat(modes, " "), "polygons/1 polygons/2/patch lines/2 points/2", "hand-built: primitives by surface and kind")
local quad = mesh.primitives[1]
t:eq(quad.attributes.POSITION.count, 4, "hand-built: quad vertices")
-- winding reversed, positions relative to the pivot, z negated
local idx = {}
for k = 0, 3 do
idx[k + 1] = buffer.readu32(quad.indices.data, k * 4)
end
local P = assert(Types.Scene.reader(quad.attributes.POSITION))
local UV = assert(Types.Scene.reader(quad.attributes.TEXCOORD_0))
t:eq(P(idx[1], 0), -0.5, "hand-built: x relative to the pivot")
t:eq(P(idx[2], 0), -0.5, "hand-built: winding reversed (second corner is point 3)")
t:eq(P(idx[2], 1), 1, "hand-built: second corner y")
t:eq(UV(idx[1], 0), 0.5, "hand-built: VMAD u")
t:eq(UV(idx[1], 1), 0.75, "hand-built: VMAD v flipped")
t:eq(UV(idx[3], 1), 0, "hand-built: VMAP v flipped (point 2: 1 -> 0)")
t:check(quad.targets and #quad.targets == 2, "hand-built: MORF + SPOT targets")
local T1 = assert(Types.Scene.reader(quad.targets[1].POSITION))
local T2 = assert(Types.Scene.reader(quad.targets[2].POSITION))
t:eq(T1(idx[3], 2), -1, "hand-built: MORF delta (z negated)")
t:eq(T2(idx[4], 1), 2, "hand-built: SPOT -> delta (point 1: y 0 -> 2)")
t:eq(mesh.weights and #mesh.weights, 2, "hand-built: mesh weights")
t:eq(mesh.extras.targetNames[2], "moved", "hand-built: target names")
local W = assert(Types.Scene.reader(quad.attributes._WEIGHT_0))
t:eq(W(idx[2], 0), 0.75, "hand-built: weight map value (point 3)")
local base, child = s.materials[1], s.materials[2]
t:eq(base.baseColor[1], 1, "hand-built: COLR")
t:check(near(base.baseColor[4], 0.75, 1e-6) and base.alphaMode == "blend", "hand-built: TRAN -> alpha")
t:check(base.doubleSided, "hand-built: SIDE 3")
t:check(child.doubleSided and child.baseColor[1] == 1, "hand-built: derived surface copies its source")
t:check(near(child.emissive[1], 0.5, 1e-6), "hand-built: LUMI -> emissive")
t:check(child.baseColorTexture and child.baseColorTexture.texCoord == 0, "hand-built: image block via XREF clip")
t:eq(s.textures[1].uri, "Wood.PNG", "hand-built: texture uri is the file name")
t:eq(s.textures[1].extras.lwo.path, "C:\\images\\Wood.PNG", "hand-built: full path in extras")
local tri = decode(data)
t:eq(tri.meshes[1].primitives[1].indices.count, 6, "hand-built: quad fan-triangulated")
-- triangulated quads keep the right facing: +z after the conversion (LightWave clockwise from -z)
local tp = tri.meshes[1].primitives[1]
local TP = assert(Types.Scene.reader(tp.attributes.POSITION))
local a, b, c = buffer.readu32(tp.indices.data, 0), buffer.readu32(tp.indices.data, 4), buffer.readu32(tp.indices.data, 8)
local ux, uy = TP(b, 0) - TP(a, 0), TP(b, 1) - TP(a, 1)
local vx, vy = TP(c, 0) - TP(a, 0), TP(c, 1) - TP(a, 1)
t:check(ux * vy - uy * vx < 0, "hand-built: facing kept (LightWave +z normal -> glTF -z)")
end
do
local _, e1 = decode("FORM\0\0\0\4LWO3")
t:check(e1 and e1.code == Err.UNSUPPORTED, "LWO3: " .. tostring(e1))
local _, e2 = decode("FORM\0\0\0\4ILBM")
t:check(e2 and e2.code == Err.CORRUPT, "not LWO: " .. tostring(e2))
local _, e3 = decode("FORM")
t:check(e3 and e3.code == Err.TRUNCATED, "short: " .. tostring(e3))
local _, e4 = decode(quadFile(), { maxVertices = 3 })
t:check(e4 and e4.code == Err.LIMIT, "maxVertices: " .. tostring(e4))
local bad = form(chunk("PNTS", f4(0) .. f4(0) .. f4(0)) .. chunk("POLS", "FACE" .. be16(3) .. be16(0) .. be16(1) .. be16(2)))
local _, e5 = decode(bad)
t:check(e5 and e5.code == Err.CORRUPT, "point index out of range: " .. tostring(e5))
t:eq(Lwo.sniff(Bytes.reader(buffer.fromstring(quadFile()))), 1, "sniff")
t:eq(Lwo.sniff(Bytes.reader(buffer.fromstring("FORM\0\0\0\4AIFF"))), 0, "sniff AIFF")
end
do
local samples = { quadFile() }
for _, c in Cases do
if #c.data < 30000 and #samples < 16 then
table.insert(samples, c.data)
end
end
t:fuzz("decode", function(b: buffer)
local s = Lwo.decode(Bytes.reader(b), { triangulate = false })
if s then
MeshOps.merge(s)
end
return s
end, samples, { mutations = 16, keepHeader = 12 })
end
t:done()