231 lines
9.3 KiB
Luau
231 lines
9.3 KiB
Luau
-- model/b3d tests: assimp's Blitz3D models (WusonBlitz, dwarf2, turtle1) and generated files (textures, brushes,
|
|
-- vertex colours, two UV sets, nested rotated / scaled / mirrored nodes, pivots, master brushes, bones, keys + ANIM)
|
|
-- against assimp (meshes, faces, materials, animations, channels, bones; world bounds, triangle area and signed
|
|
-- volume from its PreTransformVertices export: the left- to right-handed conversion and the winding) and the counts
|
|
-- the generator wrote; the skin's bind pose; error codes; the fuzz no-throw pass. Fixtures: tools/fixtures/b3d.py.
|
|
local T = require("@tests/T")
|
|
local B3d = require("@rosetta/model/b3d")
|
|
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/b3d_cases")
|
|
|
|
local t = T.suite("b3d")
|
|
|
|
local function decode(data: any, opts: any?): (any, any)
|
|
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
|
return B3d.decode(Bytes.reader(b), opts)
|
|
end
|
|
|
|
t:eq(B3d.name, "b3d", "name")
|
|
t:eq(B3d.category, "model", "category")
|
|
t:check(B3d.caps.decode and not B3d.caps.encode, "caps")
|
|
t:eq(B3d.sniff(Bytes.reader(buffer.fromstring("BB3D\0\0\0\0\1\0\0\0"))), 1, "sniff")
|
|
t:eq(B3d.sniff(Bytes.reader(buffer.fromstring("BX3D\0\0\0\0\1\0\0\0"))), 0, "sniff other")
|
|
|
|
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
|
|
|
|
-- world triangles of the merged scene: area and signed volume
|
|
local function measure(prim: any): (number, number)
|
|
local get = assert(Types.Scene.reader(prim.attributes.POSITION))
|
|
local idx = prim.indices and Types.Scene.reader(prim.indices)
|
|
local n = if idx then prim.indices.count else prim.attributes.POSITION.count
|
|
local area, vol = 0, 0
|
|
for k = 0, n // 3 - 1 do
|
|
local i0, i1, i2 = 3 * k, 3 * k + 1, 3 * k + 2
|
|
if idx then
|
|
i0, i1, i2 = idx(i0, 0), idx(i1, 0), idx(i2, 0)
|
|
end
|
|
local ax, ay, az = get(i0, 0), get(i0, 1), get(i0, 2)
|
|
local bx, by, bz = get(i1, 0), get(i1, 1), get(i1, 2)
|
|
local cx, cy, cz = get(i2, 0), get(i2, 1), get(i2, 2)
|
|
local ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
|
|
local x, y, z = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
|
area += math.sqrt(x * x + y * y + z * z) / 2
|
|
vol += (ax * (by * cz - bz * cy) - ay * (bx * cz - bz * cx) + az * (bx * cy - by * cx)) / 6
|
|
end
|
|
return area, vol
|
|
end
|
|
|
|
-- assimp's bone count: per primitive, the joints that weight one of its vertices (not the mesh node's own joint)
|
|
local function boneCount(scene: any): number
|
|
local n = 0
|
|
for ni, node in scene.nodes do
|
|
if node.mesh and node.skin then
|
|
local skin = scene.skins[node.skin]
|
|
for _, p in scene.meshes[node.mesh].primitives do
|
|
local J, W = Types.Scene.reader(p.attributes.JOINTS_0), Types.Scene.reader(p.attributes.WEIGHTS_0)
|
|
local idx = assert(Types.Scene.reader(p.indices))
|
|
local seen = {}
|
|
for i = 0, p.indices.count - 1 do
|
|
local v = idx(i, 0)
|
|
for k = 0, 3 do
|
|
local j = (J :: any)(v, k)
|
|
if (W :: any)(v, k) > 0 and skin.joints[j + 1] ~= ni and not seen[j] then
|
|
seen[j] = true
|
|
n += 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return n
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ cases
|
|
local samples = {}
|
|
for _, c in Cases do
|
|
local scene, err = decode(c.data, { files = c.files })
|
|
local a = c.assimp
|
|
t:check(scene, c.name .. ": decode: " .. tostring(err))
|
|
if scene and not a.failed then
|
|
local prims, faces = 0, 0
|
|
for _, pl in Types.Scene.flatten(scene) do
|
|
prims += 1
|
|
faces += pl.primitive.indices.count // 3
|
|
end
|
|
t:eq(prims, a.meshes, c.name .. ": meshes (assimp)")
|
|
t:eq(faces, a.faces, c.name .. ": faces (assimp)")
|
|
t:eq(math.max(1, scene.meta.source.brushes), a.materials, c.name .. ": materials (assimp)")
|
|
t:eq(#scene.animations, a.animations, c.name .. ": animations (assimp)")
|
|
local animated = {}
|
|
local nAnimated = 0
|
|
for _, anim in scene.animations do
|
|
for _, ch in anim.channels do
|
|
if not animated[ch.node] then
|
|
animated[ch.node] = true
|
|
nAnimated += 1
|
|
end
|
|
end
|
|
end
|
|
if a.animations > 0 then
|
|
t:eq(nAnimated, a.animation_channels, c.name .. ": animated nodes (assimp)")
|
|
end
|
|
t:eq(boneCount(scene), a.bones, c.name .. ": bones (assimp)")
|
|
local merged = assert(MeshOps.merge(scene))
|
|
local lo, hi = MeshOps.bounds(merged)
|
|
local ok = lo ~= nil
|
|
for k = 1, 3 do
|
|
ok = ok and near((lo :: any)[k], a.min[k], 1e-4) and near((hi :: any)[k], a.max[k], 1e-4)
|
|
end
|
|
t:check(ok, string.format("%s: world bounds (assimp) %s / %s", c.name, table.concat(lo or {}, " "), table.concat(hi or {}, " ")))
|
|
local area, vol = measure(merged)
|
|
t:check(near(area, a.area, 1e-4), string.format("%s: area %.6g (assimp %.6g)", c.name, area, a.area))
|
|
-- (a mirrored node: glTF turns its faces back, assimp's PreTransformVertices does not; skip the volume there)
|
|
if not (c.want and c.want.mirrored) then
|
|
t:check(near(vol, a.volume, 1e-3), string.format("%s: signed volume %.6g (assimp %.6g)", c.name, vol, a.volume))
|
|
end
|
|
-- textures resolved through files
|
|
for _, tex in scene.textures do
|
|
local base = string.match(tex.uri, "([^/\\]+)$")
|
|
t:check((tex.data ~= nil) == (c.files[base] ~= nil), c.name .. ": texture data " .. tostring(tex.uri))
|
|
end
|
|
local w = c.want
|
|
if w then
|
|
local nv, nt = 0, 0
|
|
for _, m in scene.meshes do
|
|
nv += m.primitives[1].attributes.POSITION.count
|
|
for _, p in m.primitives do
|
|
nt += p.indices.count // 3
|
|
end
|
|
end
|
|
t:eq(nv, w.vertices, c.name .. ": vertices")
|
|
t:eq(nt, w.triangles, c.name .. ": triangles")
|
|
t:eq(#scene.textures, w.textures, c.name .. ": textures")
|
|
t:eq(#scene.materials, w.materials, c.name .. ": materials")
|
|
t:eq(#scene.nodes, w.nodes, c.name .. ": nodes")
|
|
local attrs = scene.meshes[1].primitives[1].attributes
|
|
t:eq(attrs.COLOR_0 ~= nil, w.colors, c.name .. ": COLOR_0")
|
|
local sets = 0
|
|
while attrs["TEXCOORD_" .. sets] do
|
|
sets += 1
|
|
end
|
|
t:eq(sets, w.uvSets, c.name .. ": uv sets")
|
|
if w.joints then
|
|
t:eq(#scene.skins, 1, c.name .. ": one skin")
|
|
t:eq(#scene.skins[1].joints, w.joints, c.name .. ": joints")
|
|
t:eq(#scene.animations[1].channels, w.channels, c.name .. ": channels")
|
|
t:eq(scene.meta.source.fps, w.fps, c.name .. ": fps")
|
|
t:eq(scene.meta.source.frames, w.frames, c.name .. ": frames")
|
|
end
|
|
end
|
|
if c.name == "gen/textured" then
|
|
local m1, m2 = scene.materials[1], scene.materials[2]
|
|
t:check(m1.name == "red" and m1.baseColorTexture and m1.baseColorTexture.texture == 1, "textured: brush texture")
|
|
local tr = m1.baseColorTexture and m1.baseColorTexture.transform
|
|
t:check(tr and tr.offset[1] == 0.25 and tr.scale[2] == 3 and tr.rotation == 0.5, "textured: texture transform")
|
|
t:check(m1.extras.b3d.textures[2] == 2, "textured: second brush texture in extras")
|
|
t:check(m2.name == "glass" and m2.alphaMode == "blend" and m2.doubleSided and m2.extensions.KHR_materials_unlit, "textured: alpha, FX")
|
|
t:check(m2.baseColorTexture and m2.baseColorTexture.texCoord == 1, "textured: second UV set flag")
|
|
t:check(scene.textures[2].sampler and scene.textures[2].sampler.wrapS == "clamp", "textured: clamp")
|
|
end
|
|
-- the bind pose: skinning the vertices with the inverse bind matrices gives the static placement
|
|
for ni, node in scene.nodes do
|
|
if node.skin then
|
|
local skin = scene.skins[node.skin]
|
|
local worlds = {}
|
|
for _, pl in Types.Scene.flatten(scene) do
|
|
worlds[pl.node] = pl.matrix
|
|
end
|
|
local ibm = assert(Types.Scene.reader(skin.inverseBindMatrices))
|
|
local function world(i: number): { number }
|
|
local parent
|
|
for pi, n in scene.nodes do
|
|
if table.find(n.children, i) then
|
|
parent = pi
|
|
end
|
|
end
|
|
local l = Types.Scene.nodeMatrix(scene.nodes[i])
|
|
return if parent then Types.Scene.mul(world(parent), l) else l
|
|
end
|
|
local maxErr = 0
|
|
local meshWorld = world(ni)
|
|
for j, jn in skin.joints do
|
|
local m = {}
|
|
for k = 1, 16 do
|
|
m[k] = ibm(j - 1, k - 1)
|
|
end
|
|
local jm = Types.Scene.mul(world(jn), m)
|
|
for k = 1, 16 do
|
|
maxErr = math.max(maxErr, math.abs(jm[k] - meshWorld[k]))
|
|
end
|
|
end
|
|
t:check(maxErr < 1e-3, c.name .. ": joint * inverse bind = the mesh's world matrix (" .. maxErr .. ")")
|
|
end
|
|
end
|
|
end
|
|
if #c.data < 100000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ errors
|
|
do
|
|
local _, e = decode("BB3D\4\0\0\0\1\0\0\0")
|
|
t:check(e and e.code == Err.CORRUPT, "no NODE: CORRUPT")
|
|
_, e = decode("BB3D\100\0\0\0\1\0\0\0")
|
|
t:check(e and e.code == Err.TRUNCATED, "short BB3D: TRUNCATED")
|
|
_, e = decode("RIFF\0\0\0\0")
|
|
t:check(e and e.code == Err.CORRUPT, "not B3D: CORRUPT")
|
|
local s = Cases[#Cases].data
|
|
local _, e2 = decode(string.sub(s, 1, #s - 7))
|
|
t:check(e2 and e2.code == Err.TRUNCATED, "truncated file: TRUNCATED (" .. tostring(e2) .. ")")
|
|
local _, e3 = decode(s, { maxVertices = 4 })
|
|
t:check(e3 and e3.code == Err.LIMIT, "maxVertices: LIMIT")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ fuzz
|
|
t:fuzz("decode", function(b: buffer)
|
|
local s = B3d.decode(Bytes.reader(b))
|
|
if s then
|
|
MeshOps.merge(s)
|
|
end
|
|
return s
|
|
end, samples, { mutations = 16, keepHeader = 4 })
|
|
|
|
t:done()
|