653 lines
24 KiB
Luau
653 lines
24 KiB
Luau
-- model/blend tests: scenes saved by Blender 2.79, 2.93, 3.5, 4.1 and 5.x (plain, gzip, zstd; the 2.79 legacy mesh
|
|
-- format) against the current Blender's own values for the same files (world matrices, mesh counts, bounds, UV / colour
|
|
-- sums, corner normals, weights, shape keys, f-curve values, posed matrices per frame, skinned positions) and its
|
|
-- glTF exporter (materials, cameras, lights, skins, triangle counts). Fixtures: tools/fixtures/blend.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local B = require("@rosetta/model/blend")
|
|
local Blend = require("@rosetta/containers/Blend")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
|
local Sampler = require("@rosetta/codecs/model/Sampler")
|
|
local Versions = require("@tests/gen/blend_versions")
|
|
local Files = require("@tests/gen/blend_files")
|
|
local Sets = {
|
|
["279"] = require("@tests/gen/blend_279"),
|
|
["293"] = require("@tests/gen/blend_293"),
|
|
["35"] = require("@tests/gen/blend_35"),
|
|
["41"] = require("@tests/gen/blend_41"),
|
|
cur = require("@tests/gen/blend_cur"),
|
|
old = require("@tests/gen/blend_old"), -- Blender 1.66 - 2.04 demo files (big-endian among them)
|
|
}
|
|
|
|
local t = T.suite("blend")
|
|
local Scene = Types.Scene
|
|
|
|
t:eq(B.name, "blend", "name")
|
|
t:eq(B.category, "model", "category")
|
|
t:check(B.caps.decode and not B.caps.encode, "read only")
|
|
t:eq(#Versions, 5, "every Blender version made its files (" .. table.concat(Versions, " ") .. ")")
|
|
|
|
local function decode(data: string, opts: any?): (any, any)
|
|
return B.decode(Bytes.reader(buffer.fromstring(data)), 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
|
|
|
|
local function nearList(a: { number }?, b: { number }?, eps: number): boolean
|
|
if not a or not b or #a ~= #b then
|
|
return false
|
|
end
|
|
for i = 1, #a do
|
|
if not near(a[i], b[i], eps) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
local function fmt(m: { number }?): string
|
|
if not m then
|
|
return "nil"
|
|
end
|
|
local s = {}
|
|
for i, v in m do
|
|
s[i] = string.format("%.4f", v)
|
|
end
|
|
return table.concat(s, " ")
|
|
end
|
|
|
|
-- world matrices of every node, with an optional pose ({ [node] = { translation, rotation, scale, weights } })
|
|
local function worlds(scene: any, pose: any?): { [number]: { number } }
|
|
local out = {}
|
|
local function walk(i: number, parent: { number })
|
|
local n = scene.nodes[i]
|
|
local p = pose and pose[i]
|
|
local m
|
|
if p then
|
|
local base = if n.matrix then { Mat4.decompose(n.matrix) } else { n.translation, n.rotation, n.scale }
|
|
m = Mat4.compose(p.translation or base[1], p.rotation or base[2], p.scale or base[3])
|
|
else
|
|
m = Scene.nodeMatrix(n)
|
|
end
|
|
local w = Scene.mul(parent, m)
|
|
out[i] = w
|
|
for _, c in n.children do
|
|
walk(c, w)
|
|
end
|
|
end
|
|
for _, r in scene.roots do
|
|
walk(r, Mat4.identity())
|
|
end
|
|
return out
|
|
end
|
|
|
|
local function byName(scene: any): ({ [string]: number }, { [string]: number })
|
|
local objects, bones = {}, {}
|
|
for i, n in scene.nodes do
|
|
local e = n.extras and n.extras.blend
|
|
if e and e.bone then
|
|
bones[n.name] = i
|
|
elseif e and e.type then
|
|
objects[n.name] = i
|
|
end
|
|
end
|
|
return objects, bones
|
|
end
|
|
|
|
local function read(acc: any): (number, number) -> number
|
|
return assert(Scene.reader(acc))
|
|
end
|
|
|
|
-- the corners of the polygons decode in Blender's loop order: { prim, vertex } per corner
|
|
local function corners(mesh: any, facemats: { number }, faceSizes: { number }?): { { any } }?
|
|
local slots = {}
|
|
local cursor = {}
|
|
for _, p in mesh.primitives do
|
|
table.insert(slots, p)
|
|
end
|
|
local used = {}
|
|
for _, m in facemats do
|
|
used[m] = true
|
|
end
|
|
local order = {}
|
|
for m in used do
|
|
table.insert(order, m)
|
|
end
|
|
table.sort(order)
|
|
local primOf = {}
|
|
for k, m in order do
|
|
primOf[m] = slots[k]
|
|
end
|
|
local out = {}
|
|
for fi, m in facemats do
|
|
local p = primOf[m]
|
|
if not p or not p.faces then
|
|
return nil
|
|
end
|
|
local c = cursor[p] or { face = 0, at = 0 }
|
|
cursor[p] = c
|
|
local size = buffer.readu32(p.faces.data, c.face * 4)
|
|
if faceSizes and faceSizes[fi] ~= size then
|
|
return nil
|
|
end
|
|
for k = 0, size - 1 do
|
|
table.insert(out, { p, buffer.readu32(p.indices.data, (c.at + k) * 4) })
|
|
end
|
|
c.face += 1
|
|
c.at += size
|
|
end
|
|
return out
|
|
end
|
|
|
|
local checked = { worlds = 0, meshes = 0, normals = 0, weights = 0, shapes = 0, frames = 0, skinned = 0, curves = 0, materials = 0, cameras = 0, lights = 0 }
|
|
local samples = {}
|
|
|
|
t:check(#Sets.old >= 4, "the old demo files were fetched (" .. #Sets.old .. ")")
|
|
for _, tag in { "old", "279", "293", "35", "41", "cur" } do
|
|
for _, c in Sets[tag] do
|
|
local ref = c.ref
|
|
local label = c.name
|
|
local scene, err = decode(c.data, { files = Files })
|
|
t:check(scene, label .. ": decode: " .. tostring(err))
|
|
if not scene then
|
|
continue
|
|
end
|
|
if #c.data < 500000 and #samples < 2 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
local src = scene.meta.source
|
|
t:eq(src.format, "blend", label .. ": meta.source.format")
|
|
t:eq(scene.meta.up, "z", label .. ": z up")
|
|
if string.find(label, "_z%.blend$") then
|
|
t:eq(src.compression, if src.version < 300 then "gzip" else "zstd", label .. ": compression")
|
|
end
|
|
local objects, bones = byName(scene)
|
|
local W = worlds(scene)
|
|
-- objects: world matrices at the saved frame
|
|
for _, o in ref.objects do
|
|
local i = objects[o.name]
|
|
t:check(i, label .. ": object " .. o.name)
|
|
if i then
|
|
t:check(nearList(W[i], o.world, 1e-4), label .. ": " .. o.name .. " world\n got " .. fmt(W[i]) .. "\n want " .. fmt(o.world))
|
|
checked.worlds += 1
|
|
end
|
|
end
|
|
-- meshes
|
|
local polyScene = decode(c.data, { triangulate = false })
|
|
local polyObjects = polyScene and byName(polyScene)
|
|
for _, o in ref.objects do
|
|
local mi = o.mesh
|
|
local i = objects[o.name]
|
|
if not mi or not i then
|
|
continue
|
|
end
|
|
local node = scene.nodes[i]
|
|
local mesh = scene.meshes[node.mesh]
|
|
t:check(mesh, label .. ": " .. o.name .. " has a mesh")
|
|
if not mesh then
|
|
continue
|
|
end
|
|
local e = mesh.extras.blend
|
|
t:eq(e.vertices, mi.vertices, label .. ": " .. o.name .. " vertices")
|
|
local tris = 0
|
|
local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
|
|
for _, p in mesh.primitives do
|
|
tris += p.indices.count // 3
|
|
local P = read(p.attributes.POSITION)
|
|
for v = 0, p.attributes.POSITION.count - 1 do
|
|
for k = 1, 3 do
|
|
lo[k] = math.min(lo[k], P(v, k - 1))
|
|
hi[k] = math.max(hi[k], P(v, k - 1))
|
|
end
|
|
end
|
|
end
|
|
t:eq(tris, mi.triangles, label .. ": " .. o.name .. " triangles")
|
|
if e.generation == "mface" then
|
|
-- the 2.7x legacy save drops n-gons, leaving their vertices loose: inside the bounds
|
|
local inside = true
|
|
for k = 1, 3 do
|
|
inside = inside and lo[k] >= mi.min[k] - 1e-5 and hi[k] <= mi.max[k] + 1e-5
|
|
end
|
|
t:check(inside, label .. ": " .. o.name .. " bounds")
|
|
else
|
|
t:check(nearList(lo, mi.min, 1e-5) and nearList(hi, mi.max, 1e-5), label .. ": " .. o.name .. " bounds")
|
|
end
|
|
t:eq(e.faces, mi.faces, label .. ": " .. o.name .. " faces")
|
|
t:eq(e.corners, mi.corners, label .. ": " .. o.name .. " corners")
|
|
t:eq(#mesh.primitives, (function()
|
|
local u, n = {}, 0
|
|
for _, m in mi.facemats do
|
|
if not u[m] then
|
|
u[m] = true
|
|
n += 1
|
|
end
|
|
end
|
|
return n
|
|
end)(), label .. ": " .. o.name .. " one primitive per used slot")
|
|
checked.meshes += 1
|
|
-- corner level (the polygons decode, in Blender's loop order)
|
|
local pm = polyScene and polyScene.meshes[polyScene.nodes[polyObjects[o.name]].mesh]
|
|
local cs = pm and corners(pm, mi.facemats)
|
|
t:check(cs and #cs == mi.corners, label .. ": " .. o.name .. " corners in loop order")
|
|
if not cs then
|
|
continue
|
|
end
|
|
for k, uv in mi.uvs do
|
|
local su, sv = 0, 0
|
|
for _, cv in cs do
|
|
local acc = cv[1].attributes["TEXCOORD_" .. (k - 1)]
|
|
if acc then
|
|
local get = read(acc)
|
|
su += get(cv[2], 0)
|
|
sv += 1 - get(cv[2], 1)
|
|
end
|
|
end
|
|
t:check(near(su, uv.sum[1], 1e-4) and near(sv, uv.sum[2], 1e-4), label .. ": " .. o.name .. " uv " .. uv.name .. " sums " .. su .. " " .. sv)
|
|
end
|
|
for _, col in mi.colors do
|
|
local s = { 0, 0, 0, 0 }
|
|
local found = false
|
|
for _, cv in cs do
|
|
for name, acc in cv[1].attributes do
|
|
if string.sub(name, 1, 6) == "COLOR_" and acc.name == col.name then
|
|
found = true
|
|
local get = read(acc)
|
|
for k = 1, 4 do
|
|
s[k] += get(cv[2], k - 1)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
t:check(found and nearList(s, col.cornerSum, 1e-3), label .. ": " .. o.name .. " colour " .. col.name .. " " .. fmt(s) .. " / " .. fmt(col.cornerSum))
|
|
end
|
|
if mi.normals then
|
|
local worst = 0
|
|
for ci, cv in cs do
|
|
local get = read(cv[1].attributes.NORMAL)
|
|
local want = mi.normals[ci]
|
|
for k = 1, 3 do
|
|
worst = math.max(worst, math.abs(get(cv[2], k - 1) - want[k]))
|
|
end
|
|
end
|
|
t:check(worst < (if mi.custom then 2e-3 else 1e-4), label .. ": " .. o.name .. " corner normals (worst " .. string.format("%.2g", worst) .. (if mi.custom then ", custom" else "") .. ")")
|
|
checked.normals += 1
|
|
end
|
|
-- deform weights: the deform bones' groups, top 4 normalized
|
|
local arm = nil
|
|
for _, a in ref.armatures do
|
|
arm = a
|
|
end
|
|
if mi.weights and arm and node.skin then
|
|
local deform = {}
|
|
for _, b in arm do
|
|
deform[b.name] = b.deform
|
|
end
|
|
local skin = scene.skins[node.skin]
|
|
local jointOf = {}
|
|
for j, n in skin.joints do
|
|
jointOf[scene.nodes[n].name] = j - 1
|
|
end
|
|
local bad = 0
|
|
for ci, cv in cs do
|
|
local list = {}
|
|
for _, g in mi.weights[mi.cornerVerts[ci] + 1] do
|
|
if deform[g[1]] and g[2] > 0 then
|
|
table.insert(list, { jointOf[g[1]], g[2] })
|
|
end
|
|
end
|
|
table.sort(list, function(a, b)
|
|
return a[2] > b[2]
|
|
end)
|
|
local sum = 0
|
|
for k = 1, math.min(4, #list) do
|
|
sum += list[k][2]
|
|
end
|
|
local J, Wt = read(cv[1].attributes.JOINTS_0), read(cv[1].attributes.WEIGHTS_0)
|
|
for k = 1, math.min(4, #list) do
|
|
local ok = false
|
|
for q = 0, 3 do
|
|
if J(cv[2], q) == list[k][1] and near(Wt(cv[2], q), list[k][2] / sum, 1e-5) then
|
|
ok = true
|
|
end
|
|
end
|
|
if not ok then
|
|
bad += 1
|
|
end
|
|
end
|
|
end
|
|
t:eq(bad, 0, label .. ": " .. o.name .. " joints / weights per corner")
|
|
checked.weights += 1
|
|
end
|
|
if mi.shapes then
|
|
local refIndex = 1
|
|
for k, s in mi.shapes do
|
|
if s.name == mi.reference then
|
|
refIndex = k
|
|
end
|
|
end
|
|
t:eq(#(mesh.weights or {}), #mi.shapes - 1, label .. ": " .. o.name .. " morph weights")
|
|
local ti = 0
|
|
local worst = 0
|
|
for k, s in mi.shapes do
|
|
if k ~= refIndex then
|
|
ti += 1
|
|
if not o.shapeAction then -- animated values are checked per frame below
|
|
t:check(near(mesh.weights[ti], s.value, 1e-6), label .. ": " .. o.name .. " " .. s.name .. " weight")
|
|
end
|
|
for ci, cv in if mi.cornerVerts then cs else {} do
|
|
local tgt = cv[1].targets and cv[1].targets[ti]
|
|
local get = tgt and read(tgt.POSITION)
|
|
local v = mi.cornerVerts[ci]
|
|
for q = 1, 3 do
|
|
local want = s.co[v * 3 + q] - mi.shapes[refIndex].co[v * 3 + q]
|
|
worst = math.max(worst, if get then math.abs(get(cv[2], q - 1) - want) else math.huge)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
t:check(worst < 1e-5, label .. ": " .. o.name .. " shape key deltas (worst " .. worst .. ")")
|
|
checked.shapes += 1
|
|
end
|
|
end
|
|
-- materials against the glTF exporter (Principled) or the legacy colour
|
|
local gl = ref.gltf or {}
|
|
local glMats = {}
|
|
for _, m in gl.materials or {} do
|
|
glMats[m.name] = m
|
|
end
|
|
for _, m in scene.materials do
|
|
local g = glMats[m.name]
|
|
if m.extras.blend.principled and g then
|
|
local pbr = g.pbrMetallicRoughness or {}
|
|
t:check(nearList(m.baseColor, pbr.baseColorFactor or { 1, 1, 1, 1 }, 1e-5), label .. ": " .. m.name .. " base colour " .. fmt(m.baseColor))
|
|
t:check(near(m.metallic, pbr.metallicFactor or 1, 1e-5) and near(m.roughness, pbr.roughnessFactor or 1, 1e-5), label .. ": " .. m.name .. " metallic / roughness")
|
|
t:check(nearList(m.emissive, g.emissiveFactor or { 0, 0, 0 }, 1e-5), label .. ": " .. m.name .. " emissive " .. fmt(m.emissive))
|
|
local ges = g.extensions and g.extensions.KHR_materials_emissive_strength
|
|
local es = m.extensions and m.extensions.KHR_materials_emissive_strength
|
|
t:check(near(es and es.emissiveStrength or 1, ges and ges.emissiveStrength or 1, 1e-5), label .. ": " .. m.name .. " emissive strength")
|
|
t:eq(string.upper(m.alphaMode), g.alphaMode or "OPAQUE", label .. ": " .. m.name .. " alpha mode")
|
|
t:eq(m.baseColorTexture ~= nil, pbr.baseColorTexture ~= nil, label .. ": " .. m.name .. " base colour texture")
|
|
t:eq(m.emissiveTexture ~= nil, g.emissiveTexture ~= nil, label .. ": " .. m.name .. " emissive texture")
|
|
t:eq(m.normalTexture ~= nil, g.normalTexture ~= nil, label .. ": " .. m.name .. " normal texture")
|
|
if g.normalTexture and m.normalTexture then
|
|
t:check(near(m.normalTexture.scale, g.normalTexture.scale or 1, 1e-5), label .. ": " .. m.name .. " normal scale")
|
|
end
|
|
checked.materials += 1
|
|
elseif not m.extras.blend.principled then
|
|
for _, rm in ref.materials do
|
|
if rm.name == m.name then
|
|
t:check(nearList({ m.baseColor[1], m.baseColor[2], m.baseColor[3] }, { rm.diffuse[1], rm.diffuse[2], rm.diffuse[3] }, 1e-5), label .. ": " .. m.name .. " legacy colour")
|
|
checked.materials += 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- images: packed bytes, external uris (with the bytes from opts.files)
|
|
for _, im in ref.images do
|
|
local tex = nil
|
|
for _, x in scene.textures do
|
|
if x.name == im.name then
|
|
tex = x
|
|
end
|
|
end
|
|
if not tex then
|
|
continue -- not used by a node material (legacy texture slots, face images)
|
|
elseif im.packed > 0 then
|
|
t:check(tex.data and buffer.len(tex.data) == im.packed and (tag == "old" or tex.mime == "image/png"), label .. ": packed image " .. im.name)
|
|
else
|
|
t:eq(tex.uri, "outside.png", label .. ": external image uri")
|
|
t:check(tex.data and buffer.len(tex.data) == #Files["outside.png"], label .. ": external image bytes through opts.files")
|
|
end
|
|
end
|
|
-- cameras and lights against the glTF exporter
|
|
for _, gc in gl.cameras or {} do
|
|
local cam = nil
|
|
for _, x in scene.cameras do
|
|
if x.name == gc.name then
|
|
cam = x
|
|
end
|
|
end
|
|
t:check(cam, label .. ": camera " .. gc.name)
|
|
if cam then
|
|
if gc.type == "perspective" then
|
|
t:check(cam.type == "perspective" and near(cam.yfov, gc.perspective.yfov, 1e-5) and near(cam.znear, gc.perspective.znear, 1e-5) and near(cam.zfar, gc.perspective.zfar, 1e-5), label .. ": perspective " .. gc.name)
|
|
else
|
|
t:check(cam.type == "orthographic" and near(cam.xmag, gc.orthographic.xmag, 1e-5) and near(cam.ymag, gc.orthographic.ymag, 1e-5), label .. ": orthographic " .. gc.name)
|
|
end
|
|
checked.cameras += 1
|
|
end
|
|
end
|
|
local glLights = gl.extensions and gl.extensions.KHR_lights_punctual and gl.extensions.KHR_lights_punctual.lights or {}
|
|
t:eq(#scene.lights, #glLights, label .. ": lights (the area light is skipped)")
|
|
for _, gli in glLights do
|
|
local l = nil
|
|
for _, x in scene.lights do
|
|
if x.name == gli.name then
|
|
l = x
|
|
end
|
|
end
|
|
t:check(l and l.type == gli.type and near(l.intensity, gli.intensity, 1e-5) and nearList(l.color, gli.color, 1e-5), label .. ": light " .. gli.name .. " " .. (l and l.intensity or -1) .. " " .. gli.intensity)
|
|
if l and gli.spot then
|
|
t:check(near(l.outerCone, gli.spot.outerConeAngle, 1e-5) and near(l.innerCone, gli.spot.innerConeAngle, 1e-5), label .. ": spot cones")
|
|
end
|
|
checked.lights += 1
|
|
end
|
|
-- triangles: the glTF exporter's meshes
|
|
local curves = false -- the exporter turns curves / text / surfaces / metaballs into meshes; we do not
|
|
for _, o in ref.objects do
|
|
curves = curves or o.type == "CURVE" or o.type == "FONT" or o.type == "SURFACE" or o.type == "META"
|
|
end
|
|
if gl.meshes and not curves then
|
|
-- per instance (both sides share meshes differently)
|
|
local want = 0
|
|
for _, n in gl.nodes or {} do
|
|
if n.mesh then
|
|
for _, p in gl.meshes[n.mesh + 1].primitives do
|
|
want += gl.accessors[p.indices + 1].count // 3
|
|
end
|
|
end
|
|
end
|
|
local got = 0
|
|
for _, n in scene.nodes do
|
|
if n.mesh then
|
|
for _, p in scene.meshes[n.mesh].primitives do
|
|
got += p.indices.count // 3
|
|
end
|
|
end
|
|
end
|
|
t:eq(got, want, label .. ": triangles (glTF exporter)")
|
|
end
|
|
t:eq(#scene.skins, #(gl.skins or {}), label .. ": skins (glTF exporter)")
|
|
for k, s in scene.skins do
|
|
t:eq(#s.joints, #gl.skins[k].joints, label .. ": skin joints")
|
|
end
|
|
-- bones at the saved frame (the rest pose here)
|
|
for armName, list in ref.armatures do
|
|
local aw = W[objects[armName]]
|
|
for _, b in list do
|
|
local j = bones[b.name]
|
|
t:check(j and nearList(W[j], Scene.mul(aw, b["local"]), 1e-4), label .. ": bone " .. b.name)
|
|
end
|
|
end
|
|
-- f-curves through the evaluator, at every half frame (bezier, linear, constant, extrapolation)
|
|
local file = assert(Blend.open(buffer.fromstring(c.data)))
|
|
for _, o in ref.objects do
|
|
local refCurves = o.action and ref.actions[o.action]
|
|
if refCurves then
|
|
local ob = nil
|
|
for _, blk in Blend.blocks(file, "OB") do
|
|
local v = Blend.struct(file, blk)
|
|
if v and v:idName() == o.name then
|
|
ob = v
|
|
end
|
|
end
|
|
local curves = B.curves(file, ob and ob:deref("adt", 0, "AnimData"))
|
|
if not curves and ob and not ob:has("adt") then
|
|
curves = B.ipo(file, ob:deref("ipo", 0, "Ipo")) -- before 2.5
|
|
end
|
|
curves = curves or {}
|
|
for _, rc in refCurves do
|
|
local p = rc.path
|
|
if not (p == "location" or p == "scale" or string.find(p, "rotation") or string.find(p, "^delta_")) then
|
|
continue -- only the transform curves are read
|
|
end
|
|
local cur = nil
|
|
for _, x in curves do
|
|
if x.path == rc.path and x.index == rc.index then
|
|
cur = x
|
|
end
|
|
end
|
|
local worst = 0
|
|
for k, want in rc.values do
|
|
local got = if cur then B.evaluate(cur, 1 + 0.5 * (k - 1)) else math.huge
|
|
worst = math.max(worst, math.abs(got - want))
|
|
end
|
|
t:check(worst < 1e-4, label .. ": " .. o.name .. " " .. rc.path .. "[" .. rc.index .. "] (worst " .. worst .. ")")
|
|
checked.curves += 1
|
|
end
|
|
end
|
|
end
|
|
-- the animations sampled at whole frames: posed world matrices, bones, shape key values, skinned positions
|
|
if #ref.poses > 0 then
|
|
local animated = false -- transform or shape key curves (others, such as an object colour, are not read)
|
|
for _, list in ref.actions do
|
|
for _, rc in list do
|
|
local p = rc.path
|
|
animated = animated or p == "location" or p == "scale" or string.find(p, "rotation") ~= nil or string.find(p, "key_blocks") ~= nil
|
|
end
|
|
end
|
|
t:check(#scene.animations > 0 or not animated, label .. ": animations")
|
|
local caches = {}
|
|
for _, p in ref.poses do
|
|
local frame = p.frame
|
|
if frame ~= math.floor(frame) or frame > 20 then
|
|
continue
|
|
end
|
|
local time = (frame - (src.frameZero or src.frameStart)) / src.fps
|
|
local pose = {}
|
|
for ai, a in scene.animations do
|
|
caches[ai] = caches[ai] or {}
|
|
for node, v in Sampler.pose(scene, a, time, caches[ai]) do
|
|
pose[node] = pose[node] or {}
|
|
for k, x in v do
|
|
pose[node][k] = x
|
|
end
|
|
end
|
|
end
|
|
local PW = worlds(scene, pose)
|
|
for name, want in p.objects do
|
|
local i = objects[name]
|
|
t:check(i and nearList(PW[i], want, 1e-3), label .. ": frame " .. frame .. " " .. name .. "\n got " .. fmt(i and PW[i]) .. "\n want " .. fmt(want))
|
|
end
|
|
for key, want in p.bones do
|
|
local j = bones[string.match(key, "/(.+)$")]
|
|
t:check(j and nearList(PW[j], want, 1e-3), label .. ": frame " .. frame .. " bone " .. key)
|
|
end
|
|
for name, values in p.shapes do
|
|
local i = objects[name]
|
|
local w = pose[i] and pose[i].weights or scene.nodes[i].weights
|
|
local want = table.move(values, 2, #values, 1, {})
|
|
-- pre-2.5 IPO bezier keys: Blender recomputes auto handles when converting them; we keep the stored ones
|
|
local eps = if tag == "old" then 1e-2 else 1e-3
|
|
t:check(nearList(w, want, eps), label .. ": frame " .. frame .. " " .. name .. " shape values " .. fmt(w) .. " / " .. fmt(want))
|
|
end
|
|
for name, want in p.skinned or {} do
|
|
-- morph, then skin: sum of w * jointWorld * inverseBind * position
|
|
local i = objects[name]
|
|
local node = polyScene.nodes[polyObjects[name]]
|
|
local mi = nil
|
|
for _, o in ref.objects do
|
|
if o.name == name then
|
|
mi = o.mesh
|
|
end
|
|
end
|
|
local pm = polyScene.meshes[node.mesh]
|
|
local cs = corners(pm, mi.facemats)
|
|
local skin = scene.skins[scene.nodes[i].skin]
|
|
local IB = read(skin.inverseBindMatrices)
|
|
local weights = pose[i] and pose[i].weights or scene.nodes[i].weights or {}
|
|
local worst = 0
|
|
for ci, cv in cs do
|
|
local P = read(cv[1].attributes.POSITION)
|
|
local x, y, z = P(cv[2], 0), P(cv[2], 1), P(cv[2], 2)
|
|
for ti, tg in cv[1].targets or {} do
|
|
local D = read(tg.POSITION)
|
|
x += D(cv[2], 0) * (weights[ti] or 0)
|
|
y += D(cv[2], 1) * (weights[ti] or 0)
|
|
z += D(cv[2], 2) * (weights[ti] or 0)
|
|
end
|
|
local J, Wt = read(cv[1].attributes.JOINTS_0), read(cv[1].attributes.WEIGHTS_0)
|
|
local ox, oy, oz = 0, 0, 0
|
|
for q = 0, 3 do
|
|
local w = Wt(cv[2], q)
|
|
if w > 0 then
|
|
local j = J(cv[2], q)
|
|
local ib = {}
|
|
for k = 0, 15 do
|
|
ib[k + 1] = IB(j, k)
|
|
end
|
|
local m = Scene.mul(PW[skin.joints[j + 1]], ib)
|
|
local px, py, pz = Mat4.point(m, x, y, z)
|
|
ox += w * px
|
|
oy += w * py
|
|
oz += w * pz
|
|
end
|
|
end
|
|
local v = mi.cornerVerts[ci]
|
|
worst = math.max(worst, math.abs(ox - want[v * 3 + 1]), math.abs(oy - want[v * 3 + 2]), math.abs(oz - want[v * 3 + 3]))
|
|
end
|
|
t:check(worst < 1e-3, label .. ": frame " .. frame .. " " .. name .. " skinned positions (worst " .. worst .. ")")
|
|
checked.skinned += 1
|
|
end
|
|
checked.frames += 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
t:note("checked: %d world matrices, %d meshes, %d corner normal sets, %d weight sets, %d shape key sets, %d f-curves, %d posed frames, %d skinned frames, %d materials, %d cameras, %d lights",
|
|
checked.worlds, checked.meshes, checked.normals, checked.weights, checked.shapes, checked.curves, checked.frames, checked.skinned, checked.materials, checked.cameras, checked.lights)
|
|
|
|
-- the api: by extension and magic, compressed files included
|
|
local cur = Sets.cur[1]
|
|
if cur then
|
|
t:eq(F.detect(buffer.fromstring(cur.data)), "blend", "F.detect")
|
|
local s = F.load(buffer.fromstring(cur.data))
|
|
t:check(s and s.kind == "scene", "F.load")
|
|
end
|
|
|
|
-- compressed files: by content for model loads, by extension otherwise
|
|
for _, c in Sets.cur do
|
|
if string.find(c.name, "_z%.blend$") then
|
|
local b = buffer.fromstring(c.data)
|
|
local s = F.model.load(b)
|
|
t:check(s and s.meta.source.compression == "zstd", "F.model.load of a zstd .blend")
|
|
t:eq(F.detect(b, { name = "x.blend" }), "blend", "F.detect of a zstd .blend by name")
|
|
end
|
|
end
|
|
for _, c in Sets["293"] do
|
|
if string.find(c.name, "_z%.blend$") then
|
|
local s = F.model.load(buffer.fromstring(c.data))
|
|
t:check(s and s.meta.source.compression == "gzip", "F.model.load of a gzip .blend")
|
|
end
|
|
end
|
|
|
|
-- errors
|
|
local function code(data: string): string?
|
|
local s, e = decode(data)
|
|
return if s then nil else e.code
|
|
end
|
|
t:eq(code("BLENDER"), Err.TRUNCATED, "a 7-byte file is truncated")
|
|
t:eq(code("BLENDER-v279REND"), Err.TRUNCATED, "a truncated block header")
|
|
t:eq(code("BLENDERxv279" .. string.rep("\0", 24)), Err.CORRUPT, "bad pointer size")
|
|
t:eq(code("BLENDER-v279ENDB" .. string.rep("\0", 20)), Err.CORRUPT, "no DNA1 block")
|
|
|
|
t:fuzz("decode", function(b: buffer)
|
|
return B.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 24 })
|
|
t:done()
|