Meshes (smoothing-group normals, uvs, mesh matrix, per-face materials), Phong materials with texture maps (uri), cameras, lights, the keyframer (hierarchy, pivots, instances, dummies, position / rotation / scale / camera tracks -> one animation). Writer: objects, materials, cameras, lights, keyframer. Tested against assimp (test models, its 3DS exporter, and reading our files).
603 lines
22 KiB
Luau
603 lines
22 KiB
Luau
-- model/3ds tests: assimp's 3DS test models (meshes, materials, textures, keyframer hierarchies, pivots, camera /
|
|
-- target / rotation animation, cameras, lights) and assimp's 3DS exporter output, against assimp (meshes, faces,
|
|
-- corners, materials, animations, cameras, lights, world bounds); the keyframer placing every mesh where its mesh
|
|
-- matrix says; round trips through our writer (world triangles, materials, textures, cameras, lights, sampled
|
|
-- animation) for the reference files and for glTF sample models; assimp reading our files like the originals
|
|
-- (3ds_xcheck); errors; the fuzz no-throw pass. Fixtures: tools/fixtures/3ds.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@formats")
|
|
local ThreeDs = require("@formats/model/3ds")
|
|
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/3ds_cases")
|
|
local Picks = require("@tests/gen/3ds_gltf")
|
|
local Xcheck = require("@tests/gen/3ds_xcheck")
|
|
|
|
local t = T.suite("3ds")
|
|
local S = Types.Scene
|
|
|
|
local function decode(data: any, opts: any?): (any, any)
|
|
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
|
return ThreeDs.decode(Bytes.reader(b), opts)
|
|
end
|
|
|
|
t:eq(ThreeDs.name, "3ds", "name")
|
|
t:eq(ThreeDs.category, "model", "category")
|
|
t:check(ThreeDs.caps.decode and ThreeDs.caps.encode, "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
|
|
|
|
-- placed primitives, triangles, corners
|
|
local function counts(scene: any): (number, number, number)
|
|
local prims, faces, corners = 0, 0, 0
|
|
local seen = {}
|
|
for _, pl in S.flatten(scene) do
|
|
if not seen[pl.primitive] then
|
|
seen[pl.primitive] = true
|
|
prims += 1
|
|
faces += pl.primitive.indices.count // 3
|
|
corners += pl.primitive.indices.count
|
|
end
|
|
end
|
|
return prims, faces, corners
|
|
end
|
|
|
|
local function noMaterial(scene: any): boolean
|
|
for _, m in scene.meshes do
|
|
for _, p in m.primitives do
|
|
if p.material == nil then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- assimp's default material: an existing grey untextured material named "*default*" is reused
|
|
local function defaultReused(scene: any): boolean
|
|
for _, m in scene.materials do
|
|
local c = m.baseColor
|
|
if string.find(string.lower(m.name or ""), "default", 1, true) and c[1] == c[2] and c[1] == c[3] and not m.baseColorTexture then
|
|
local x = m.extras["3ds"].maps
|
|
if next(x) == nil then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- world bounds: as they are, or under assimp's Z-up -> Y-up root rotation (x, z, -y)
|
|
local function boundsMatch(lo: { number }, hi: { number }, want: any, eps: number): boolean
|
|
local function eq(a: { number }, b: { number }): boolean
|
|
for k = 1, 3 do
|
|
if not near(a[k], want.min[k], eps) or not near(b[k], want.max[k], eps) then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
return eq(lo, hi) or eq({ lo[1], lo[3], -hi[2] }, { hi[1], hi[3], -lo[2] })
|
|
end
|
|
|
|
local function bstr(lo: any, hi: any): string
|
|
if not lo then
|
|
return "-"
|
|
end
|
|
return string.format("(%.4g %.4g %.4g) (%.4g %.4g %.4g)", lo[1], lo[2], lo[3], hi[1], hi[2], hi[3])
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ reference files
|
|
-- assimp ignores the camera / light frame of their children (CameraRollAnimWithChildObject: a box linked to the
|
|
-- camera); our bounds follow the mesh matrices there (checked below)
|
|
local ASSIMP_BOUNDS_DIFFER = { ["assimp/models/CameraRollAnimWithChildObject.3ds"] = true }
|
|
local function boundsDiffer(i: number): boolean
|
|
return ASSIMP_BOUNDS_DIFFER[Cases[i].name] == true
|
|
end
|
|
local scenes = {}
|
|
local checked = 0
|
|
for i, c in Cases do
|
|
local scene, err = decode(c.data)
|
|
local a = c.assimp
|
|
if a.failed then
|
|
t:note("%s: assimp rejects it; we read it: %s", c.name, tostring(scene ~= nil))
|
|
else
|
|
t:check(scene, c.name .. ": decode: " .. tostring(err))
|
|
scenes[i] = scene
|
|
if scene then
|
|
local prims, faces, corners = counts(scene)
|
|
t:eq(prims, a.meshes, c.name .. ": meshes (assimp)")
|
|
t:eq(faces, a.faces, c.name .. ": faces (assimp)")
|
|
t:eq(corners, a.vertices, c.name .. ": corners (assimp)")
|
|
t:eq(#scene.cameras, a.cameras, c.name .. ": cameras (assimp)")
|
|
t:eq(#scene.lights, a.lights, c.name .. ": lights (assimp)")
|
|
t:eq(#scene.animations, a.animations, c.name .. ": animations (assimp)")
|
|
local extra = if noMaterial(scene) and not defaultReused(scene) then 1 else 0
|
|
t:eq(#scene.materials + extra, a.materials, c.name .. ": materials (assimp, + its default)")
|
|
if a.min and not ASSIMP_BOUNDS_DIFFER[c.name] then
|
|
local lo, hi = MeshOps.bounds(MeshOps.merge(scene))
|
|
t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": world bounds (assimp) " .. bstr(lo, hi) .. " want " .. bstr(a.min, a.max))
|
|
end
|
|
checked += 1
|
|
end
|
|
end
|
|
end
|
|
t:note("%d files against assimp", checked)
|
|
|
|
-- the keyframer places every mesh where its mesh matrix put the stored (world space) vertices
|
|
do
|
|
local n = 0
|
|
for i, c in Cases do
|
|
local scene = scenes[i]
|
|
if scene and scene.meta.source.keyframer and #scene.animations == 0 and string.sub(c.name, 1, 7) == "assimp/" then
|
|
local raw = decode(c.data, { normals = false })
|
|
local flat = decode(c.data, { normals = false })
|
|
-- the file's vertices: the meshes placed at identity (Read only; via the writer-free path: raw bounds)
|
|
local lo, hi = MeshOps.bounds(MeshOps.merge(raw))
|
|
local _ = flat
|
|
local Read = require("@formats/model/3ds/Read")
|
|
local parsed = Read.parse(buffer.fromstring(c.data), 0, #c.data)
|
|
local plo, phi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
|
|
local placed: { [string]: boolean } = {}
|
|
for _, kn in parsed.keyframer.nodes do
|
|
if kn.kind == "object" then
|
|
placed[kn.name] = true
|
|
end
|
|
end
|
|
for _, o in parsed.objects do
|
|
if o.mesh and o.mesh.nf > 0 and placed[o.name] then
|
|
for v = 0, o.mesh.nv - 1 do
|
|
for k = 1, 3 do
|
|
local x = buffer.readf32(o.mesh.positions, (v * 3 + k - 1) * 4)
|
|
plo[k], phi[k] = math.min(plo[k], x), math.max(phi[k], x)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if lo and plo[1] <= phi[1] then
|
|
local ok = true
|
|
for k = 1, 3 do
|
|
ok = ok and near(lo[k], plo[k], 2e-3) and near(hi[k], phi[k], 2e-3)
|
|
end
|
|
t:check(ok, c.name .. ": keyframer world bounds = the stored vertices " .. bstr(lo, hi) .. " / " .. bstr(plo, phi))
|
|
n += 1
|
|
end
|
|
end
|
|
end
|
|
t:note("%d keyframer scenes match their stored vertices", n)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ round trips
|
|
local function triArea(prim: any): number
|
|
local P = S.reader(prim.attributes.POSITION) :: any
|
|
local I = S.reader(prim.indices) :: any
|
|
local area = 0
|
|
for tri = 0, prim.indices.count // 3 - 1 do
|
|
local a, b, c = I(tri * 3, 0), I(tri * 3 + 1, 0), I(tri * 3 + 2, 0)
|
|
local e1 = { P(b, 0) - P(a, 0), P(b, 1) - P(a, 1), P(b, 2) - P(a, 2) }
|
|
local e2 = { P(c, 0) - P(a, 0), P(c, 1) - P(a, 1), P(c, 2) - P(a, 2) }
|
|
local x, y, z = e1[2] * e2[3] - e1[3] * e2[2], e1[3] * e2[1] - e1[1] * e2[3], e1[1] * e2[2] - e1[2] * e2[1]
|
|
area += math.sqrt(x * x + y * y + z * z) / 2
|
|
end
|
|
return area
|
|
end
|
|
|
|
-- the world matrices of the nodes with a camera / light, in tree order
|
|
local function worldOf(s: any, field: string): { { number } }
|
|
local out = {}
|
|
local function visit(i: number, parent: { number })
|
|
local n = s.nodes[i]
|
|
local w = S.mul(parent, S.nodeMatrix(n))
|
|
if n[field] then
|
|
table.insert(out, { w = w, item = n[field] })
|
|
end
|
|
for _, c in n.children do
|
|
visit(c, w)
|
|
end
|
|
end
|
|
for _, r in s.roots do
|
|
visit(r, S.IDENTITY)
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- channel value at a time (LINEAR / STEP; quaternions lerped the short way)
|
|
local function evalCh(ch: any, time: number, comps: number): { number }
|
|
local IN, OUT = S.reader(ch.input) :: any, S.reader(ch.output) :: any
|
|
local n = ch.input.count
|
|
local cubic = ch.interpolation == "CUBICSPLINE"
|
|
local function v(k: number): { number }
|
|
local e = if cubic then k * 3 + 1 else k
|
|
local r = {}
|
|
for q = 0, comps - 1 do
|
|
r[q + 1] = OUT(e, q)
|
|
end
|
|
return r
|
|
end
|
|
if time <= IN(0, 0) then
|
|
return v(0)
|
|
end
|
|
for k = 0, n - 2 do
|
|
local t0, t1 = IN(k, 0), IN(k + 1, 0)
|
|
if time <= t1 then
|
|
if ch.interpolation == "STEP" or t1 <= t0 then
|
|
return if time >= t1 then v(k + 1) else v(k)
|
|
end
|
|
local u = (time - t0) / (t1 - t0)
|
|
local a0, a1 = v(k), v(k + 1)
|
|
if comps == 4 and a0[1] * a1[1] + a0[2] * a1[2] + a0[3] * a1[3] + a0[4] * a1[4] < 0 then
|
|
a1 = { -a1[1], -a1[2], -a1[3], -a1[4] }
|
|
end
|
|
local r = {}
|
|
for q = 1, comps do
|
|
r[q] = a0[q] + (a1[q] - a0[q]) * u
|
|
end
|
|
return r
|
|
end
|
|
end
|
|
return v(n - 1)
|
|
end
|
|
|
|
-- the world matrix of every named node at a time (animations[1] applied)
|
|
local function posed(s: any, time: number): { [string]: { number } }
|
|
local byNode: { [number]: { [string]: any } } = {}
|
|
local anim = s.animations[1]
|
|
if anim then
|
|
for _, ch in anim.channels do
|
|
byNode[ch.node] = byNode[ch.node] or {}
|
|
byNode[ch.node][ch.path] = ch
|
|
end
|
|
end
|
|
local out = {}
|
|
local count: { [string]: number } = {}
|
|
local function visit(i: number, parent: { number })
|
|
local n = s.nodes[i]
|
|
local m
|
|
local chs = byNode[i]
|
|
if chs then
|
|
local tr = if chs.translation then evalCh(chs.translation, time, 3) else (n.translation or { 0, 0, 0 })
|
|
local r = if chs.rotation then evalCh(chs.rotation, time, 4) else (n.rotation or { 0, 0, 0, 1 })
|
|
local sc = if chs.scale then evalCh(chs.scale, time, 3) else (n.scale or { 1, 1, 1 })
|
|
local l = math.sqrt(r[1] * r[1] + r[2] * r[2] + r[3] * r[3] + r[4] * r[4])
|
|
r = { r[1] / l, r[2] / l, r[3] / l, r[4] / l }
|
|
local Mat4 = require("@formats/codecs/model/Mat4")
|
|
m = Mat4.compose(tr, r, sc)
|
|
else
|
|
m = S.nodeMatrix(n)
|
|
end
|
|
local w = S.mul(parent, m)
|
|
local key = if n.mesh then "mesh" .. n.mesh elseif n.camera then "camera" else if n.light then "light" else nil
|
|
if key then
|
|
count[key] = (count[key] or 0) + 1
|
|
out[key .. "#" .. count[key]] = w
|
|
end
|
|
for _, c in n.children do
|
|
visit(c, w)
|
|
end
|
|
end
|
|
for _, r in s.roots do
|
|
visit(r, S.IDENTITY)
|
|
end
|
|
return out
|
|
end
|
|
|
|
local function compare(a: any, b: any, label: string, eps: number, animTimes: { number }?)
|
|
local ma, mb = MeshOps.merge(a), MeshOps.merge(b)
|
|
local na = if ma and ma.indices then ma.indices.count // 3 else 0
|
|
local nb = if mb and mb.indices then mb.indices.count // 3 else 0
|
|
t:eq(nb, na, label .. ": triangles")
|
|
if na > 0 and nb > 0 then
|
|
t:check(near(triArea(ma), triArea(mb), eps), string.format("%s: area %.7g / %.7g", label, triArea(ma), triArea(mb)))
|
|
local la, ha = MeshOps.bounds(ma)
|
|
local lb, hb = MeshOps.bounds(mb)
|
|
local ok = true
|
|
for k = 1, 3 do
|
|
ok = ok and near(la[k], lb[k], eps) and near(ha[k], hb[k], eps)
|
|
end
|
|
t:check(ok, label .. ": bounds " .. bstr(la, ha) .. " / " .. bstr(lb, hb))
|
|
end
|
|
t:eq(#b.materials, #a.materials, label .. ": materials")
|
|
for k, m in a.materials do
|
|
local m2 = b.materials[k]
|
|
if m2 then
|
|
local ok = true
|
|
for q = 1, 4 do
|
|
ok = ok and near(m.baseColor[q], m2.baseColor[q], 1e-6)
|
|
end
|
|
t:check(ok, label .. ": material " .. k .. " colour")
|
|
t:check((m.baseColorTexture == nil) == (m2.baseColorTexture == nil), label .. ": material " .. k .. " texture")
|
|
t:check(m.doubleSided == m2.doubleSided, label .. ": material " .. k .. " two-sided")
|
|
end
|
|
end
|
|
for _, field in { "camera", "light" } do
|
|
local pa, pb = worldOf(a, field), worldOf(b, field)
|
|
t:eq(#pb, #pa, label .. ": " .. field .. " nodes")
|
|
for k, e in pa do
|
|
local w = e.w
|
|
local w2 = pb[k] and pb[k].w
|
|
local ok = w2 ~= nil
|
|
if w2 then
|
|
-- position and view axis (3DS cameras and lights carry no scale)
|
|
for _, q in { 13, 14, 15 } do
|
|
ok = ok and near(w[q], w2[q], 1e-4)
|
|
end
|
|
if field == "camera" or a.lights[e.item].type ~= "point" then
|
|
local function z(m: { number }): (number, number, number)
|
|
local l = math.sqrt(m[9] * m[9] + m[10] * m[10] + m[11] * m[11])
|
|
return m[9] / l, m[10] / l, m[11] / l
|
|
end
|
|
local x1, y1, z1 = z(w)
|
|
local x2, y2, z2 = z(w2)
|
|
ok = ok and x1 * x2 + y1 * y2 + z1 * z2 > 0.9999
|
|
end
|
|
end
|
|
t:check(ok, label .. ": " .. field .. " " .. k .. " placement")
|
|
end
|
|
end
|
|
-- the horizontal field of view (3DS stores a lens, not the aspect)
|
|
for k, c in a.cameras do
|
|
local c2 = b.cameras[k]
|
|
if c2 and c.type == "perspective" then
|
|
local function hfov(cam: any): number
|
|
return 2 * math.atan(math.tan(cam.yfov / 2) * (cam.aspect or 4 / 3))
|
|
end
|
|
t:check(near(hfov(c), hfov(c2), 1e-5), label .. ": camera " .. k .. " fov")
|
|
end
|
|
end
|
|
-- one timeline: the first animation's node transforms
|
|
local trs = false
|
|
for _, ch in (if a.animations[1] then a.animations[1].channels else {}) :: { any } do
|
|
trs = trs or ch.path ~= "weights"
|
|
end
|
|
t:eq(#b.animations, if trs then 1 else 0, label .. ": animations")
|
|
if animTimes and #a.animations > 0 then
|
|
local bad, n = 0, 0
|
|
for _, time in animTimes do
|
|
local pa, pb = posed(a, time), posed(b, time)
|
|
for nm, w in pa do
|
|
local w2 = pb[nm]
|
|
if w2 then
|
|
for q = 1, 16 do
|
|
if not near(w[q], w2[q], 2e-3) then
|
|
bad += 1
|
|
end
|
|
end
|
|
n += 1
|
|
end
|
|
end
|
|
end
|
|
t:check(bad == 0 and n > 0, string.format("%s: posed world matrices: %d values of %d nodes x times differ", label, bad, n))
|
|
end
|
|
end
|
|
|
|
local TIMES = { 0, 0.1, 0.5, 1, 1.5, 2, 3.3, 5, 8 }
|
|
local nrt = 0
|
|
for i, c in Cases do
|
|
local scene = scenes[i]
|
|
if scene then
|
|
local out, err = ThreeDs.encode(scene)
|
|
t:check(out, c.name .. ": encode: " .. tostring(err))
|
|
local back, berr = decode(out :: buffer)
|
|
t:check(back, c.name .. ": decode ours: " .. tostring(berr))
|
|
if back then
|
|
compare(scene, back, c.name, 1e-4, TIMES)
|
|
local p1, f1 = counts(scene)
|
|
local p2, f2 = counts(back)
|
|
t:check(p1 == p2 and f1 == f2, c.name .. ": primitives and faces kept")
|
|
-- the smoothing groups come back, so the normals do
|
|
local same = true
|
|
for mi, m in scene.meshes do
|
|
local m2 = back.meshes[mi]
|
|
for pi, p in m.primitives do
|
|
local p2 = m2 and m2.primitives[pi]
|
|
if not p2 or p2.attributes.POSITION.count ~= p.attributes.POSITION.count then
|
|
same = false
|
|
end
|
|
end
|
|
end
|
|
t:check(same, c.name .. ": vertex splits (smoothing groups) kept")
|
|
end
|
|
nrt += 1
|
|
end
|
|
end
|
|
|
|
-- glTF sample models through 3DS
|
|
local gltfScenes = {}
|
|
local ng = 0
|
|
for gi, g in Picks do
|
|
local scene, gerr = Gltf.decode(Bytes.reader(buffer.fromstring(g.data)), { files = g.files })
|
|
t:check(scene, g.name .. ": glTF decode: " .. tostring(gerr))
|
|
if scene then
|
|
gltfScenes[gi] = scene
|
|
local out, err = ThreeDs.encode(scene, { fps = 1000 })
|
|
t:check(out, g.name .. ": encode: " .. tostring(err))
|
|
local back, berr = decode(out :: buffer, { fps = 1000 })
|
|
t:check(back, g.name .. ": decode ours: " .. tostring(berr))
|
|
if back then
|
|
-- skins and morph targets are not 3DS; STEP / CUBICSPLINE keys become linear
|
|
local linear = true
|
|
for _, an in scene.animations do
|
|
for _, ch in an.channels do
|
|
if ch.interpolation ~= "LINEAR" or ch.path == "weights" then
|
|
linear = false
|
|
end
|
|
end
|
|
end
|
|
compare(scene, back, "glTF " .. g.name, 2e-4, if linear and #scene.skins == 0 then TIMES else nil)
|
|
ng += 1
|
|
end
|
|
end
|
|
end
|
|
t:note("%d reference files and %d glTF models through our writer and back", nrt, ng)
|
|
|
|
-- ------------------------------------------------------------------------------------------------ cross-check
|
|
-- assimp reads our files like the originals
|
|
local nx = 0
|
|
for _, x in Xcheck do
|
|
local a = x.assimp
|
|
t:check(not a.failed, x.name .. ": assimp reads our file")
|
|
if not a.failed then
|
|
local scene, want
|
|
if x.case then
|
|
scene, want = scenes[x.case], Cases[x.case].assimp
|
|
else
|
|
scene = gltfScenes[x.gltf]
|
|
end
|
|
if scene then
|
|
local _, faces, corners = counts(scene)
|
|
t:eq(a.faces, faces, x.name .. ": assimp faces of our file")
|
|
t:eq(a.vertices, corners, x.name .. ": assimp corners of our file")
|
|
t:eq(a.cameras, #worldOf(scene, "camera"), x.name .. ": assimp cameras of our file")
|
|
t:eq(a.lights, #worldOf(scene, "light"), x.name .. ": assimp lights of our file")
|
|
if want and not want.failed then
|
|
t:eq(a.meshes, want.meshes, x.name .. ": assimp meshes of our file")
|
|
t:eq(a.animations, want.animations, x.name .. ": assimp animations of our file")
|
|
end
|
|
if a.min and not (x.case and boundsDiffer(x.case)) then
|
|
local lo, hi = MeshOps.bounds(MeshOps.merge(scene))
|
|
t:check(lo and boundsMatch(lo, hi, a, 1e-3), x.name .. ": assimp world bounds of our file " .. bstr(lo, hi) .. " / " .. bstr(a.min, a.max))
|
|
end
|
|
nx += 1
|
|
end
|
|
end
|
|
end
|
|
t:note("%d of our files read back by assimp", nx)
|
|
|
|
-- ------------------------------------------------------------------------------------------------ details
|
|
do
|
|
local byName = {}
|
|
for i, c in Cases do
|
|
byName[c.name] = scenes[i]
|
|
end
|
|
local tex = byName["assimp/models/cube_with_diffuse_texture.3DS"]
|
|
if tex then
|
|
local m = tex.materials[1]
|
|
t:check(m.baseColorTexture and tex.textures[m.baseColorTexture.texture].uri ~= nil, "diffuse map -> baseColorTexture with a uri")
|
|
t:check(tex.textures[1].data == nil, "texture files are not read")
|
|
t:check(tex.meshes[1].primitives[1].attributes.TEXCOORD_0 ~= nil and tex.meshes[1].primitives[1].attributes.NORMAL ~= nil, "uvs and normals")
|
|
t:eq(tex.meta.up, "z", "3DS is Z up")
|
|
end
|
|
local spec = byName["assimp/models/cube_with_specular_texture.3DS"]
|
|
if spec then
|
|
t:check(spec.materials[1].extras["3ds"].maps.specular ~= nil, "specular map kept in extras")
|
|
end
|
|
local alpha = byName["assimp/models/cubes_with_alpha.3DS"]
|
|
if alpha then
|
|
local blended = 0
|
|
for _, m in alpha.materials do
|
|
if m.alphaMode == "blend" and m.baseColor[4] < 1 then
|
|
blended += 1
|
|
end
|
|
end
|
|
t:check(blended > 0, "transparency -> alpha " .. blended)
|
|
end
|
|
local cam = byName["assimp/models/CameraRollAnim.3ds"]
|
|
if cam then
|
|
local c = cam.cameras[1]
|
|
-- 2400 / 53.33 mm = 45 degrees horizontal at 4:3
|
|
t:check(near(2 * math.atan(math.tan(c.yfov / 2) * c.aspect), math.rad(45), 1e-4), "camera lens -> fov " .. math.deg(c.yfov))
|
|
local paths = {}
|
|
for _, ch in cam.animations[1].channels do
|
|
paths[ch.path] = true
|
|
end
|
|
t:check(paths.rotation, "camera roll -> a rotation channel")
|
|
end
|
|
local child = byName["assimp/models/CameraRollAnimWithChildObject.3ds"]
|
|
if child then
|
|
local camNode
|
|
for _, n in child.nodes do
|
|
if n.camera then
|
|
camNode = n
|
|
end
|
|
end
|
|
t:check(camNode and #camNode.children == 1 and child.nodes[camNode.children[1]].name == "Box02", "a box linked to the camera")
|
|
end
|
|
local spin = byName["assimp/models/RotatingCube.3DS"]
|
|
if spin then
|
|
local ch = spin.animations[1].channels[1]
|
|
t:check(ch.path == "rotation" and ch.input.count > 100, "rotation keys accumulated: " .. ch.input.count)
|
|
end
|
|
local pyr = byName["assimp/models-nonbsd/pyramob.3DS"]
|
|
if pyr then
|
|
local types = {}
|
|
for _, l in pyr.lights do
|
|
types[l.type] = true
|
|
end
|
|
t:check(types.point and types.spot, "omni and spot lights")
|
|
end
|
|
-- the api
|
|
local s = F.load(Cases[1].data)
|
|
t:check(s and s.kind == "scene" and s.meta.source.format == "3ds", "F.load detects 3DS")
|
|
local viaApi = F.save(s, nil, { format = "3ds" })
|
|
t:check(viaApi and buffer.readu16(viaApi, 0) == 0x4D4D, "F.save format 3ds")
|
|
-- normals = false keeps the file's vertices
|
|
local raw = decode(Cases[1].data, { normals = false })
|
|
t:check(raw and raw.meshes[1].primitives[1].attributes.NORMAL == nil, "normals = false")
|
|
-- a hand-made scene: two instances of one mesh, a texture without uri goes to opts.files
|
|
local p = S.accessor(nil, "f32", 3, 3)
|
|
for v, xyz in { { 0, 0, 0 }, { 1, 0, 0 }, { 0, 1, 0 } } do
|
|
for k = 1, 3 do
|
|
buffer.writef32(p.data, ((v - 1) * 3 + k - 1) * 4, xyz[k])
|
|
end
|
|
end
|
|
local uv = S.accessor(nil, "f32", 2, 3)
|
|
local tri = S.fromPrimitive({ attributes = { POSITION = p, TEXCOORD_0 = uv }, mode = "triangles", material = 1 }, "tri")
|
|
tri.materials[1] = S.material("red")
|
|
tri.materials[1].baseColor = { 1, 0, 0, 1 }
|
|
tri.materials[1].baseColorTexture = { texture = 1 }
|
|
tri.textures[1] = { data = buffer.fromstring("\137PNG"), mime = "image/png" }
|
|
tri.nodes[2] = { name = "copy", children = {}, mesh = 1, translation = { 5, 0, 0 } }
|
|
table.insert(tri.roots, 2)
|
|
local files = {}
|
|
local out = ThreeDs.encode(tri, { name = "tri.3ds", files = files }) :: buffer
|
|
t:check(files["tri_1.png"] ~= nil, "embedded texture bytes -> opts.files")
|
|
local back = decode(out)
|
|
t:check(back and #back.meshes == 1 and #back.nodes == 2 and back.nodes[2].mesh == 1, "instances share one mesh")
|
|
t:eq(back.textures[1].uri, "tri_1.png", "the texture's file name")
|
|
local lo, hi = MeshOps.bounds(MeshOps.merge(back))
|
|
t:check(lo[1] == 0 and hi[1] == 6, "the instance stands at x = 5")
|
|
t:eq(back.nodes[2].name, "copy", "instance name")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ errors
|
|
do
|
|
local empty = decode("MM\6\0\0\0")
|
|
t:check(empty and #empty.meshes == 0 and #empty.nodes == 0, "an empty main chunk: an empty scene")
|
|
local data = Cases[1].data
|
|
local _, e2 = decode(string.sub(data, 1, #data // 2))
|
|
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
|
local _, e3 = decode("not a 3ds file at all")
|
|
t:check(e3 and e3.code == Err.CORRUPT, "garbage: " .. tostring(e3))
|
|
local _, e4 = decode(data, { maxVertices = 3 })
|
|
t:check(e4 and e4.code == Err.LIMIT, "maxVertices: " .. tostring(e4))
|
|
local _, e5 = ThreeDs.encode({} :: any)
|
|
t:check(e5 and e5.code == Err.ARG, "encode junk")
|
|
t:check(ThreeDs.sniff(Bytes.reader(buffer.fromstring(data))) > 0.5, "sniff")
|
|
t:eq(ThreeDs.sniff(Bytes.reader(buffer.fromstring("MM junk junk junk"))), 0, "sniff junk")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ fuzz
|
|
do
|
|
local samples = {}
|
|
for i, c in Cases do
|
|
if #c.data < 40000 then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
local s = ThreeDs.decode(Bytes.reader(b))
|
|
if s then
|
|
ThreeDs.encode(s)
|
|
MeshOps.merge(s)
|
|
end
|
|
return s
|
|
end, samples, { mutations = 12, keepHeader = 6 })
|
|
end
|
|
|
|
t:done()
|