582 lines
24 KiB
Luau
582 lines
24 KiB
Luau
-- model/dae tests: assimp's Collada test models (triangles / polylist / polygons / tristrips, UTF-16 and BOM files,
|
|
-- decimal commas, 2 UV sets, vertex colours, instance_node, cameras, lights, nested / clipped / full-rotation
|
|
-- animations, skins, .zae) and glTF samples exported by assimp's Collada exporter, against `assimp info -r` (meshes,
|
|
-- faces, bounds in assimp's space, materials, animations, cameras, lights), and against the glTF originals read by
|
|
-- model/gltf (skinned rest-pose bounds, joints, every LINEAR animation key); our writer's output read back by assimp
|
|
-- (dae_xcheck) and by us (round trips, .dae and .zae); polygons kept with triangulate = false; textures decoded lazily
|
|
-- at the api layer; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/dae.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local Dae = require("@formats/model/dae")
|
|
local Gltf = require("@formats/model/gltf")
|
|
local MeshOps = require("@formats/ops/MeshOps")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Types = require("@formats/Types")
|
|
local Cases = require("@tests/gen/dae_cases")
|
|
local Xcheck = require("@tests/gen/dae_xcheck")
|
|
|
|
local Scene = Types.Scene
|
|
local t = T.suite("dae")
|
|
|
|
local function decode(data: any, opts: any?): (any, any)
|
|
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
|
return Dae.decode(Bytes.reader(b), opts)
|
|
end
|
|
|
|
t:eq(Dae.name, "dae", "name")
|
|
t:eq(Dae.category, "model", "category")
|
|
t:check(Dae.caps.decode and Dae.caps.encode and table.find(Dae.extensions, "zae"), "contract")
|
|
|
|
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 nearV(a: { number }, b: { number }, eps: number): boolean
|
|
for i = 1, #b do
|
|
if not near(a[i], b[i], eps) then
|
|
return false
|
|
end
|
|
end
|
|
return #a == #b
|
|
end
|
|
local function fmt(v: { number }?): string
|
|
if not v then
|
|
return "-"
|
|
end
|
|
return string.format("(%.4g %.4g %.4g)", v[1], v[2], v[3])
|
|
end
|
|
|
|
-- assimp's counts: one mesh per primitive reached (instances of one primitive once), faces as stored
|
|
local function counts(scene: any): (number, number)
|
|
local prims, faces = 0, 0
|
|
local seen = {}
|
|
for _, pl in Scene.flatten(scene) do
|
|
if not seen[pl.primitive] then
|
|
seen[pl.primitive] = true
|
|
local p = pl.primitive
|
|
prims += 1
|
|
if p.mode == "polygons" then
|
|
faces += p.faces.count
|
|
elseif p.mode == "triangles" then
|
|
faces += p.indices.count // 3
|
|
elseif p.mode == "lines" then
|
|
faces += p.indices.count // 2
|
|
end
|
|
end
|
|
end
|
|
return prims, faces
|
|
end
|
|
|
|
local function inverse(m: { number }): { number }
|
|
-- general 4x4 inverse (column-major; cofactors)
|
|
local inv = table.create(16, 0)
|
|
local a = m
|
|
inv[1] = a[6] * a[11] * a[16] - a[6] * a[12] * a[15] - a[10] * a[7] * a[16] + a[10] * a[8] * a[15] + a[14] * a[7] * a[12] - a[14] * a[8] * a[11]
|
|
inv[5] = -a[5] * a[11] * a[16] + a[5] * a[12] * a[15] + a[9] * a[7] * a[16] - a[9] * a[8] * a[15] - a[13] * a[7] * a[12] + a[13] * a[8] * a[11]
|
|
inv[9] = a[5] * a[10] * a[16] - a[5] * a[12] * a[14] - a[9] * a[6] * a[16] + a[9] * a[8] * a[14] + a[13] * a[6] * a[12] - a[13] * a[8] * a[10]
|
|
inv[13] = -a[5] * a[10] * a[15] + a[5] * a[11] * a[14] + a[9] * a[6] * a[15] - a[9] * a[7] * a[14] - a[13] * a[6] * a[11] + a[13] * a[7] * a[10]
|
|
inv[2] = -a[2] * a[11] * a[16] + a[2] * a[12] * a[15] + a[10] * a[3] * a[16] - a[10] * a[4] * a[15] - a[14] * a[3] * a[12] + a[14] * a[4] * a[11]
|
|
inv[6] = a[1] * a[11] * a[16] - a[1] * a[12] * a[15] - a[9] * a[3] * a[16] + a[9] * a[4] * a[15] + a[13] * a[3] * a[12] - a[13] * a[4] * a[11]
|
|
inv[10] = -a[1] * a[10] * a[16] + a[1] * a[12] * a[14] + a[9] * a[2] * a[16] - a[9] * a[4] * a[14] - a[13] * a[2] * a[12] + a[13] * a[4] * a[10]
|
|
inv[14] = a[1] * a[10] * a[15] - a[1] * a[11] * a[14] - a[9] * a[2] * a[15] + a[9] * a[3] * a[14] + a[13] * a[2] * a[11] - a[13] * a[3] * a[10]
|
|
inv[3] = a[2] * a[7] * a[16] - a[2] * a[8] * a[15] - a[6] * a[3] * a[16] + a[6] * a[4] * a[15] + a[14] * a[3] * a[8] - a[14] * a[4] * a[7]
|
|
inv[7] = -a[1] * a[7] * a[16] + a[1] * a[8] * a[15] + a[5] * a[3] * a[16] - a[5] * a[4] * a[15] - a[13] * a[3] * a[8] + a[13] * a[4] * a[7]
|
|
inv[11] = a[1] * a[6] * a[16] - a[1] * a[8] * a[14] - a[5] * a[2] * a[16] + a[5] * a[4] * a[14] + a[13] * a[2] * a[8] - a[13] * a[4] * a[6]
|
|
inv[15] = -a[1] * a[6] * a[15] + a[1] * a[7] * a[14] + a[5] * a[2] * a[15] - a[5] * a[3] * a[14] - a[13] * a[2] * a[7] + a[13] * a[3] * a[6]
|
|
inv[4] = -a[2] * a[7] * a[12] + a[2] * a[8] * a[11] + a[6] * a[3] * a[12] - a[6] * a[4] * a[11] - a[10] * a[3] * a[8] + a[10] * a[4] * a[7]
|
|
inv[8] = a[1] * a[7] * a[12] - a[1] * a[8] * a[11] - a[5] * a[3] * a[12] + a[5] * a[4] * a[11] + a[9] * a[3] * a[8] - a[9] * a[4] * a[7]
|
|
inv[12] = -a[1] * a[6] * a[12] + a[1] * a[8] * a[10] + a[5] * a[2] * a[12] - a[5] * a[4] * a[10] - a[9] * a[2] * a[8] + a[9] * a[4] * a[6]
|
|
inv[16] = a[1] * a[6] * a[11] - a[1] * a[7] * a[10] - a[5] * a[2] * a[11] + a[5] * a[3] * a[10] + a[9] * a[2] * a[7] - a[9] * a[3] * a[6]
|
|
local det = a[1] * inv[1] + a[2] * inv[5] + a[3] * inv[9] + a[4] * inv[13]
|
|
for i = 1, 16 do
|
|
inv[i] /= det
|
|
end
|
|
return inv
|
|
end
|
|
|
|
local function grow(lo: { number }, hi: { number }, x: number, y: number, z: number)
|
|
lo[1], lo[2], lo[3] = math.min(lo[1], x), math.min(lo[2], y), math.min(lo[3], z)
|
|
hi[1], hi[2], hi[3] = math.max(hi[1], x), math.max(hi[2], y), math.max(hi[3], z)
|
|
end
|
|
local function apply(m: { number }, x: number, y: number, z: number): (number, number, number)
|
|
return m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15]
|
|
end
|
|
|
|
-- the bounds `assimp info` prints: its Collada importer puts the up-axis rotation and unit scale on the root node,
|
|
-- and Info.cpp accumulates node transforms as local * parent (sic); skinned meshes without their bind shape matrix
|
|
local function assimpBounds(scene: any, written: boolean?): ({ number }?, { number }?)
|
|
local u = scene.meta.unit or 1
|
|
local root = if scene.meta.up == "z" then { u, 0, 0, 0, 0, 0, -u, 0, 0, u, 0, 0, 0, 0, 0, 1 }
|
|
elseif scene.meta.up == "x" then { 0, u, 0, 0, -u, 0, 0, 0, 0, 0, u, 0, 0, 0, 0, 1 }
|
|
else { u, 0, 0, 0, 0, u, 0, 0, 0, 0, u, 0, 0, 0, 0, 1 }
|
|
local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
|
|
local seen = {}
|
|
local function visit(i: number, parent: { number })
|
|
local n = scene.nodes[i]
|
|
if seen[i] then
|
|
return
|
|
end
|
|
seen[i] = true
|
|
local m = Scene.mul(Scene.nodeMatrix(n), parent)
|
|
local mesh = n.mesh and scene.meshes[n.mesh]
|
|
if mesh then
|
|
local mm = m
|
|
local skin = n.skin and scene.skins[n.skin]
|
|
if skin and not written and skin.extras and skin.extras.dae and skin.extras.dae.bindShapeMatrix then
|
|
mm = Scene.mul(m, inverse(skin.extras.dae.bindShapeMatrix))
|
|
end
|
|
for _, p in mesh.primitives do
|
|
local pos = p.attributes.POSITION
|
|
for v = 0, pos.count - 1 do
|
|
grow(lo, hi, apply(mm, buffer.readf32(pos.data, v * 12), buffer.readf32(pos.data, v * 12 + 4), buffer.readf32(pos.data, v * 12 + 8)))
|
|
end
|
|
end
|
|
end
|
|
for _, c in n.children do
|
|
visit(c, m)
|
|
end
|
|
end
|
|
for _, r in scene.roots do
|
|
visit(r, root)
|
|
end
|
|
if lo[1] > hi[1] then
|
|
return nil, nil
|
|
end
|
|
return lo, hi
|
|
end
|
|
|
|
-- world matrices (correct order) of every node
|
|
local function worlds(scene: any): { [number]: { number } }
|
|
local w = {}
|
|
local function visit(i: number, parent: { number })
|
|
if w[i] then
|
|
return
|
|
end
|
|
w[i] = Scene.mul(parent, Scene.nodeMatrix(scene.nodes[i]))
|
|
for _, c in scene.nodes[i].children do
|
|
visit(c, w[i])
|
|
end
|
|
end
|
|
for _, r in scene.roots do
|
|
visit(r, Scene.IDENTITY)
|
|
end
|
|
return w
|
|
end
|
|
|
|
-- bounds of the rest pose with skins applied (glTF semantics: skinned vertices = sum w * world(joint) * ibm * v)
|
|
local function posedBounds(scene: any): ({ number }?, { number }?)
|
|
local w = worlds(scene)
|
|
local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
|
|
for ni, m in w do
|
|
local n = scene.nodes[ni]
|
|
local mesh = n.mesh and scene.meshes[n.mesh]
|
|
if mesh then
|
|
local skin = n.skin and scene.skins[n.skin]
|
|
local jm = {}
|
|
if skin then
|
|
local ibm = skin.inverseBindMatrices and Scene.reader(skin.inverseBindMatrices)
|
|
for j, jn in skin.joints do
|
|
local inv = table.create(16, 0)
|
|
for k = 1, 16 do
|
|
inv[k] = if ibm then ibm(j - 1, k - 1) else (if k % 5 == 1 then 1 else 0)
|
|
end
|
|
jm[j - 1] = Scene.mul(w[jn] or Scene.IDENTITY, inv)
|
|
end
|
|
end
|
|
for _, p in mesh.primitives do
|
|
local pos = p.attributes.POSITION
|
|
local J = skin and p.attributes.JOINTS_0 and Scene.reader(p.attributes.JOINTS_0)
|
|
local W = skin and p.attributes.WEIGHTS_0 and Scene.reader(p.attributes.WEIGHTS_0)
|
|
for v = 0, pos.count - 1 do
|
|
local x, y, z = buffer.readf32(pos.data, v * 12), buffer.readf32(pos.data, v * 12 + 4), buffer.readf32(pos.data, v * 12 + 8)
|
|
if J and W then
|
|
local sx, sy, sz = 0, 0, 0
|
|
for k = 0, 3 do
|
|
local wt = W(v, k)
|
|
if wt > 0 then
|
|
local px, py, pz = apply(jm[J(v, k)] or Scene.IDENTITY, x, y, z)
|
|
sx, sy, sz = sx + wt * px, sy + wt * py, sz + wt * pz
|
|
end
|
|
end
|
|
grow(lo, hi, sx, sy, sz)
|
|
else
|
|
grow(lo, hi, apply(m, x, y, z))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if lo[1] > hi[1] then
|
|
return nil, nil
|
|
end
|
|
return lo, hi
|
|
end
|
|
|
|
local function lightCount(scene: any): number
|
|
local n = #scene.lights
|
|
for _, node in scene.nodes do
|
|
if node.extras and node.extras.dae and node.extras.dae.ambientLight then
|
|
n += 1
|
|
end
|
|
end
|
|
return n
|
|
end
|
|
|
|
-- a channel's value at time tm (LINEAR; rotations nlerped)
|
|
local function channelAt(ch: any, tm: number): { number }
|
|
local inp, out = Scene.reader(ch.input) :: any, Scene.reader(ch.output) :: any
|
|
local n, c = ch.input.count, ch.output.components
|
|
local function at(i: number): { number }
|
|
local r = {}
|
|
for k = 0, c - 1 do
|
|
r[k + 1] = out(i, k)
|
|
end
|
|
return r
|
|
end
|
|
if tm <= inp(0, 0) then
|
|
return at(0)
|
|
end
|
|
for i = 1, n - 1 do
|
|
local t1 = inp(i, 0)
|
|
if tm <= t1 + 1e-6 then
|
|
local t0 = inp(i - 1, 0)
|
|
local f = if t1 > t0 then math.clamp((tm - t0) / (t1 - t0), 0, 1) else 1
|
|
local a, b = at(i - 1), at(i)
|
|
for k = 1, c do
|
|
a[k] += (b[k] - a[k]) * f
|
|
end
|
|
return a
|
|
end
|
|
end
|
|
return at(n - 1)
|
|
end
|
|
|
|
local checked = { assimp = 0, bounds = 0, gltf = 0, keys = 0, roundTrips = 0, xcheck = 0 }
|
|
local decoded: { [number]: any } = {}
|
|
local samples: { string } = {}
|
|
for i, c in Cases do
|
|
local scene, err = decode(c.data, { files = c.files, triangulate = false })
|
|
t:check(scene, c.name .. ": decode: " .. tostring(err))
|
|
if not scene then
|
|
continue
|
|
end
|
|
t:eq(scene.meta.source.format, "dae", c.name .. ": source")
|
|
local ref = c.assimp
|
|
local prims, faces = counts(scene)
|
|
if ref and not ref.failed then
|
|
-- assimp builds a placeholder skeleton mesh for scenes without meshes
|
|
if prims > 0 then
|
|
t:eq(prims, ref.meshes, c.name .. ": meshes (assimp)")
|
|
t:eq(faces, ref.faces, c.name .. ": faces (assimp)")
|
|
local lo, hi = assimpBounds(scene)
|
|
local ok = lo ~= nil and hi ~= nil and nearV(lo :: any, ref.min, 2e-4) and nearV(hi :: any, ref.max, 2e-4)
|
|
t:check(ok, string.format("%s: bounds %s %s, assimp %s %s", c.name, fmt(lo), fmt(hi), fmt(ref.min), fmt(ref.max)))
|
|
checked.bounds += 1
|
|
-- assimp adds a default material when a mesh has none
|
|
t:check(ref.materials - #scene.materials == 0 or ref.materials - #scene.materials == 1, c.name .. ": materials (assimp)")
|
|
end
|
|
t:eq(#scene.cameras, ref.cameras, c.name .. ": cameras (assimp)")
|
|
t:eq(lightCount(scene), ref.lights, c.name .. ": lights (assimp)")
|
|
t:eq(#scene.animations, ref.animations, c.name .. ": animations (assimp)")
|
|
checked.assimp += 1
|
|
end
|
|
-- against the glTF original of an assimp export
|
|
if c.gltf then
|
|
local g, gerr = Gltf.decode(Bytes.reader(buffer.fromstring(c.gltf)), { files = c.gltfFiles })
|
|
t:check(g, c.name .. ": gltf original: " .. tostring(gerr))
|
|
if g then
|
|
local glo, ghi = posedBounds(g)
|
|
local lo, hi = posedBounds(scene)
|
|
-- assimp's exporter folds a root Z-up rotation node of the source into <up_axis>Z_UP</up_axis>
|
|
if lo and hi and scene.meta.up == "z" then
|
|
lo, hi = { lo[1], lo[3], -hi[2] }, { hi[1], hi[3], -lo[2] }
|
|
end
|
|
t:check(lo and glo and nearV(lo, glo, 1e-4) and nearV(hi :: any, ghi :: any, 1e-4), string.format("%s: posed bounds %s %s, glTF %s %s", c.name, fmt(lo), fmt(hi), fmt(glo), fmt(ghi)))
|
|
t:eq(#scene.skins, #g.skins, c.name .. ": skins")
|
|
if #g.skins > 0 and #scene.skins > 0 then
|
|
t:eq(#scene.skins[1].joints, #g.skins[1].joints, c.name .. ": joints")
|
|
end
|
|
-- every LINEAR key of the original, evaluated on our resampled channels (nodes matched by name)
|
|
local byName = {}
|
|
for ni, n in scene.nodes do
|
|
if n.name then
|
|
byName[n.name] = byName[n.name] or ni
|
|
end
|
|
end
|
|
-- assimp's Collada exporter writes key times in milliseconds (its glTF importer's ticks per second)
|
|
local function maxTime(sc: any): number
|
|
local m = 0
|
|
for _, a in sc.animations do
|
|
for _, ch in a.channels do
|
|
m = math.max(m, buffer.readf32(ch.input.data, (ch.input.count - 1) * 4))
|
|
end
|
|
end
|
|
return m
|
|
end
|
|
local gmax, omax = maxTime(g), maxTime(scene)
|
|
local tscale = if gmax > 0 and near(omax / gmax, 1000, 1e-3) then 1000 else 1
|
|
-- (files assimp cannot read back: its exporter left out animation sources; their channels are dropped)
|
|
for _, ga in if ref and not ref.failed then g.animations else {} do
|
|
for _, gc in ga.channels do
|
|
local gname = g.nodes[gc.node].name
|
|
local ni = gname and byName[gname]
|
|
if gc.interpolation == "LINEAR" and gc.path ~= "weights" and ni then
|
|
local ours = nil
|
|
for _, a in scene.animations do
|
|
for _, ch in a.channels do
|
|
if ch.node == ni and ch.path == gc.path then
|
|
ours = ch
|
|
end
|
|
end
|
|
end
|
|
t:check(ours, c.name .. ": animated " .. gname .. " " .. gc.path)
|
|
if ours then
|
|
local gin, gout = Scene.reader(gc.input) :: any, Scene.reader(gc.output) :: any
|
|
for k = 0, gc.input.count - 1 do
|
|
local want = {}
|
|
for q = 0, gc.output.components - 1 do
|
|
want[q + 1] = gout(k, q)
|
|
end
|
|
local got = channelAt(ours, gin(k, 0) * tscale)
|
|
local ok
|
|
if gc.path == "rotation" then
|
|
local d = math.abs(got[1] * want[1] + got[2] * want[2] + got[3] * want[3] + got[4] * want[4])
|
|
ok = d > 1 - 1e-4
|
|
else
|
|
ok = nearV(got, want, 1e-4)
|
|
end
|
|
t:check(ok, string.format("%s: %s %s at %g", c.name, gname, gc.path, gin(k, 0)))
|
|
checked.keys += 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
checked.gltf += 1
|
|
end
|
|
end
|
|
-- the full rotations file: no resampled step over 90 degrees
|
|
if string.find(c.name, "full_rotations", 1, true) then
|
|
local worst = 1
|
|
for _, a in scene.animations do
|
|
for _, ch in a.channels do
|
|
if ch.path == "rotation" then
|
|
local r = Scene.reader(ch.output) :: any
|
|
for k = 1, ch.output.count - 1 do
|
|
local d = 0
|
|
for q = 0, 3 do
|
|
d += r(k, q) * r(k - 1, q)
|
|
end
|
|
worst = math.min(worst, d)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
t:check(worst >= math.cos(math.rad(45)) - 1e-6, "full rotations: subdivided (worst dot " .. worst .. ")")
|
|
end
|
|
-- round trip through our writer (.dae, and .zae for a few)
|
|
local tri = decode(c.data, { files = c.files })
|
|
decoded[i] = tri
|
|
local files = {}
|
|
local out, eerr = Dae.encode(tri, { files = files, name = "rt.dae" })
|
|
t:check(out, c.name .. ": encode: " .. tostring(eerr))
|
|
if out then
|
|
local back, berr = decode(out, { files = files })
|
|
t:check(back, c.name .. ": re-decode: " .. tostring(berr))
|
|
if back then
|
|
local p1, f1 = counts(tri)
|
|
local p2, f2 = counts(back)
|
|
t:eq(p2, p1, c.name .. ": round trip meshes")
|
|
t:eq(f2, f1, c.name .. ": round trip faces")
|
|
local lo1, hi1 = MeshOps.bounds(tri)
|
|
local lo2, hi2 = MeshOps.bounds(back)
|
|
t:check((lo1 == nil and lo2 == nil) or (lo1 and lo2 and nearV(lo2, lo1, 1e-5) and nearV(hi2 :: any, hi1 :: any, 1e-5)), c.name .. ": round trip bounds")
|
|
t:eq(#back.materials, #tri.materials, c.name .. ": round trip materials")
|
|
t:eq(#back.cameras, #tri.cameras, c.name .. ": round trip cameras")
|
|
t:eq(#back.lights, #tri.lights, c.name .. ": round trip lights")
|
|
t:eq(#back.textures, #tri.textures, c.name .. ": round trip textures")
|
|
for ti, tex in tri.textures do
|
|
local bt = back.textures[ti]
|
|
if tex.data and bt then
|
|
t:check(bt.data and buffer.tostring(bt.data) == buffer.tostring(tex.data), c.name .. ": round trip texture " .. ti)
|
|
end
|
|
end
|
|
for mi, m in tri.materials do
|
|
local bm = back.materials[mi]
|
|
t:check(bm and nearV(bm.baseColor, m.baseColor, 1e-6), c.name .. ": round trip baseColor " .. mi)
|
|
end
|
|
checked.roundTrips += 1
|
|
end
|
|
end
|
|
if #c.data < 20000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
|
|
-- assimp reads what we wrote like the source
|
|
for _, x in Xcheck do
|
|
local src = decoded[x.case]
|
|
local ref = x.assimp
|
|
t:check(ref and not ref.failed, x.name .. ": assimp reads our file")
|
|
if src and ref and not ref.failed then
|
|
local prims, faces = counts(src)
|
|
if prims > 0 then
|
|
t:eq(ref.meshes, prims, x.name .. ": meshes (assimp, ours)")
|
|
t:eq(ref.faces, faces, x.name .. ": faces (assimp, ours)")
|
|
local lo, hi = assimpBounds(src, true) -- our writer bakes the bind shape (it writes no skins)
|
|
t:check(lo and nearV(lo, ref.min, 2e-4) and nearV(hi :: any, ref.max, 2e-4), string.format("%s: bounds %s %s, assimp %s %s", x.name, fmt(lo), fmt(hi), fmt(ref.min), fmt(ref.max)))
|
|
end
|
|
t:eq(ref.cameras, #src.cameras, x.name .. ": cameras (assimp, ours)")
|
|
t:eq(ref.lights, #src.lights, x.name .. ": lights (assimp, ours)")
|
|
checked.xcheck += 1
|
|
end
|
|
end
|
|
|
|
-- polygons: kept or fan-triangulated
|
|
local function case(name: string): any
|
|
for i, c in Cases do
|
|
if string.find(c.name, name, 1, true) then
|
|
return c, i
|
|
end
|
|
end
|
|
return nil, nil
|
|
end
|
|
do
|
|
local c = case("ConcavePolygon")
|
|
if c then
|
|
local s = decode(c.data, { triangulate = false })
|
|
local p = s.meshes[1].primitives[1]
|
|
t:eq(p.mode, "polygons", "polygons kept")
|
|
local corners = 0
|
|
local get = Scene.reader(p.faces) :: any
|
|
for k = 0, p.faces.count - 1 do
|
|
corners += get(k, 0) - 2
|
|
end
|
|
local s2 = decode(c.data)
|
|
t:eq(s2.meshes[1].primitives[1].mode, "triangles", "polygons triangulated")
|
|
t:eq(s2.meshes[1].primitives[1].indices.count // 3, corners, "fan triangle count")
|
|
local tri = MeshOps.triangulate(p)
|
|
t:check(tri and tri.indices.count // 3 == corners, "MeshOps ear-clips the kept polygon")
|
|
end
|
|
end
|
|
|
|
-- materials and textures (duck: blinn, a TGA diffuse texture; decoded at the api layer)
|
|
do
|
|
local c = case("assimp/duck.dae")
|
|
if c then
|
|
local s = decode(c.data, { files = c.files })
|
|
local m = s.materials[1]
|
|
local ti = m.baseColorTexture and m.baseColorTexture.texture
|
|
local tex = ti and s.textures[ti]
|
|
t:check(tex and tex.data, "duck: diffuse texture bytes")
|
|
t:eq(tex and tex.mime, "image/tga", "duck: texture mime")
|
|
t:check(m.extras.phong.model == "blinn" and type(m.extras.phong.shininess) == "number", "duck: phong extras")
|
|
local _, errs = F.model.loadTextures(s)
|
|
t:check(tex and tex.image and tex.image.width > 0 and errs[ti :: number] == nil, "duck: texture decoded lazily")
|
|
-- without the image files: the uri stays, no data
|
|
local s2 = decode(c.data)
|
|
t:check(ti and s2.textures[ti].data == nil and s2.textures[ti].uri == "./duckCM.tga", "duck: uri without files")
|
|
local cam = s.cameras[1]
|
|
t:check(cam.type == "perspective" and cam.yfov > 0 and cam.yfov < math.pi, "duck: camera")
|
|
t:eq(s.meta.up, "y", "duck: up axis")
|
|
end
|
|
local z = case("assimp/duck.zae")
|
|
if z then
|
|
local s = decode(z.data)
|
|
t:check(s.textures[1] and s.textures[1].data, "zae: texture read from the zip")
|
|
t:check(Dae.sniff(Bytes.reader(buffer.fromstring(z.data))) > 0.9, "zae sniff")
|
|
local zb = Dae.encode(s, { format = "zae" })
|
|
local back = zb and decode(zb)
|
|
t:check(back and back.textures[1] and back.textures[1].data and buffer.len(back.textures[1].data) == buffer.len(s.textures[1].data), "zae round trip with the texture")
|
|
end
|
|
local l = case("assimp/lights.dae")
|
|
if l then
|
|
local s = decode(l.data)
|
|
local kinds = {}
|
|
for _, li in s.lights do
|
|
kinds[li.type] = (kinds[li.type] or 0) + 1
|
|
end
|
|
t:check(kinds.point == 2 and kinds.directional == 1 and kinds.spot == 1, "lights: kinds")
|
|
t:eq(lightCount(s) - #s.lights, 1, "lights: one ambient")
|
|
end
|
|
local u = case("cube_UTF16LE")
|
|
if u then
|
|
t:eq(Dae.sniff(Bytes.reader(buffer.fromstring(u.data))), 1, "UTF-16 sniff")
|
|
end
|
|
local k = case("kwxport")
|
|
if k then
|
|
local s = decode(k.data)
|
|
t:check(s.meta.up == "z" and near(s.meta.unit, 0.0254, 1e-9), "kwxport: Z_UP, inches")
|
|
t:check(s.meshes[1].primitives[1].attributes.COLOR_0 ~= nil, "kwxport: vertex colours")
|
|
end
|
|
local uv2 = case("cube_with_2UVs")
|
|
if uv2 then
|
|
local s = decode(uv2.data)
|
|
local a = s.meshes[1].primitives[1].attributes
|
|
t:check(a.TEXCOORD_0 and a.TEXCOORD_1, "2 UV sets")
|
|
end
|
|
local clips = case("library_animation_clips")
|
|
if clips then
|
|
local s = decode(clips.data)
|
|
t:check(#s.animations == 2 and s.animations[1].name ~= nil, "animation clips named")
|
|
t:check(#s.skins >= 1 and s.skins[1].inverseBindMatrices ~= nil, "clips: skin with inverse bind matrices")
|
|
local p = s.meshes[s.nodes[(function()
|
|
for i, n in s.nodes do
|
|
if n.skin then
|
|
return i
|
|
end
|
|
end
|
|
return 1
|
|
end)()].mesh].primitives[1]
|
|
local w = Scene.reader(p.attributes.WEIGHTS_0) :: any
|
|
local okSum = true
|
|
for v = 0, p.attributes.WEIGHTS_0.count - 1 do
|
|
local sum = w(v, 0) + w(v, 1) + w(v, 2) + w(v, 3)
|
|
if sum > 0 and math.abs(sum - 1) > 1e-5 then
|
|
okSum = false
|
|
end
|
|
end
|
|
t:check(okSum, "clips: weights normalized")
|
|
end
|
|
end
|
|
|
|
-- api: F.load by content / extension
|
|
do
|
|
local c = case("cube_tristrips")
|
|
if c then
|
|
local s, err = F.load(c.data)
|
|
t:check(s and s.meta.source.format == "dae", "F.load: " .. tostring(err))
|
|
local out = F.save(s, nil, { format = "dae" })
|
|
t:check(out and string.find(buffer.tostring(out), "<COLLADA", 1, true), "F.save dae")
|
|
end
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
local _, e1 = decode('<?xml version="1.0"?><root/>')
|
|
t:check(e1 and e1.code == Err.CORRUPT, "not COLLADA: CORRUPT")
|
|
local _, e2 = decode('<COLLADA version="1.4.1"><library_geometries>')
|
|
t:check(e2 and (e2.code == Err.TRUNCATED or e2.code == Err.CORRUPT), "unclosed XML")
|
|
local bad = [[<COLLADA version="1.4.1"><library_geometries><geometry id="g"><mesh>
|
|
<source id="p"><float_array id="pa" count="3">0 0 0</float_array><technique_common><accessor source="#pa" count="1" stride="3"><param name="X" type="float"/><param name="Y" type="float"/><param name="Z" type="float"/></accessor></technique_common></source>
|
|
<vertices id="v"><input semantic="POSITION" source="#p"/></vertices><triangles count="1"><input semantic="VERTEX" source="#v" offset="0"/><p>0 0 7</p></triangles></mesh></geometry></library_geometries>
|
|
<library_visual_scenes><visual_scene id="s"><node id="n"><instance_geometry url="#g"/></node></visual_scene></library_visual_scenes><scene><instance_visual_scene url="#s"/></scene></COLLADA>]]
|
|
local _, e3 = decode(bad)
|
|
t:check(e3 and e3.code == Err.CORRUPT, "index out of range: CORRUPT")
|
|
local _, e4 = decode(string.gsub(bad, "0 0 7", "0 0 0"), { maxVertices = 0 })
|
|
t:check(e4 and e4.code == Err.LIMIT, "maxVertices: LIMIT")
|
|
local ok = decode((string.gsub(bad, "0 0 7", "0 0 0")))
|
|
t:check(ok and #ok.meshes == 1 and ok.meshes[1].primitives[1].attributes.POSITION.count == 1, "minimal file")
|
|
local _, e5 = Dae.encode({} :: any)
|
|
t:check(e5 and e5.code == Err.ARG, "encode: ARG")
|
|
t:eq(Dae.sniff(Bytes.reader(buffer.fromstring("<svg/>"))), 0, "sniff: other XML")
|
|
table.insert(samples, (string.gsub(bad, "0 0 7", "0 0 0")))
|
|
end
|
|
|
|
t:note("%d cases: %d against assimp (%d bounds), %d against their glTF originals (%d animation keys), %d round trips", #Cases, checked.assimp, checked.bounds, checked.gltf, checked.keys, checked.roundTrips)
|
|
t:note("%d of our files read back by assimp like the sources", checked.xcheck)
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Dae.decode(Bytes.reader(b))
|
|
end, samples)
|
|
t:done()
|