-- model/md3 tests: generated MD3 files against assimp's frame-0 soup and the writer's exact frames and tags. local T = require("@tests/T") local F = require("@rosetta") local Md3 = require("@rosetta/model/md3") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Soup = require("@tests/model/_soup") local Fx = require("@tests/gen/md3") local t = T.suite("md3") t:eq(Md3.name, "md3", "name") local function normals(prim: any): buffer local n = Types.Scene.reader(prim.attributes.NORMAL) :: any local idx = prim.indices local out = buffer.create(idx.count * 12) for k = 0, idx.count - 1 do local v = buffer.readu32(idx.data, k * 4) for c = 0, 2 do buffer.writef32(out, k * 12 + c * 4, n(v, c)) end end return out end for _, c in Fx do local s, err = Md3.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(s, c.name .. ": " .. tostring(err)) t:eq(F.detect(c.data), "md3", c.name .. ": F.detect") local prims = s.meshes[1].primitives t:eq(#prims, #c.surfaces, c.name .. ": a primitive per surface") -- assimp orders the meshes differently and rotates triangle corners: match each mesh to the surface holding its triangles t:eq(c.assimp.meshes, #c.surfaces, c.name .. ": assimp meshes") local matched = 0 for mi, m in c.assimp.per do local nextStart = if c.assimp.per[mi + 1] then c.assimp.per[mi + 1].start else c.assimp.faces local nf = nextStart - m.start local apos = Soup.slice(c.assimp.pos, m.start * 9 + 1, nf * 9) local auv = Soup.slice(c.assimp.uv, m.start * 6 + 1, nf * 6) for _, p in prims do if p.indices.count == nf * 3 and Soup.triangles(Soup.list(Soup.positions(p)), apos, 1e-5) then t:check(Soup.triangles(Soup.uvs(p, 0, true), auv, 1e-5, 2), c.name .. ": texture coordinates against assimp") matched += 1 break end end end t:eq(matched, #c.surfaces, c.name .. ": every surface matches assimp's positions") for si, w in c.surfaces do t:eq(Crc32.of(Soup.positions(prims[si])), w.crcs[1], c.name .. ": " .. w.name .. " frame 1") t:eq(s.textures[s.materials[prims[si].material].baseColorTexture.texture].uri, w.shader, c.name .. ": shader") for k = 1, c.frames do local fs = Md3.decode(Bytes.reader(buffer.fromstring(c.data)), { frame = k, morph = false }) :: any local fp = fs.meshes[1].primitives[si] t:eq(Crc32.of(Soup.positions(fp)), w.crcs[k], c.name .. ": " .. w.name .. " frame " .. k) local nb = normals(fp) -- normals are computed in double then stored as f32: compare by CRC of the fixture's f32 values t:eq(Crc32.of(nb), w.ncrcs[k], c.name .. ": " .. w.name .. " normals frame " .. k) end if c.frames > 1 then t:eq(#prims[si].targets, c.frames, c.name .. ": morph targets") end end local ntags = #c.tags // c.frames t:eq(#s.meta.source.tags, ntags, c.name .. ": tags") for _, tg in c.tags do if tg.frame == 1 then local node for _, n in s.nodes do if n.name == tg.name then node = n end end t:check(node and math.abs(node.translation[1] - tg.origin[1]) < 1e-6, c.name .. ": tag " .. tg.name .. " origin") -- the rotation about z by angle t:check(node and math.abs(node.rotation[3] - math.sin(tg.angle / 2)) < 1e-5, c.name .. ": tag " .. tg.name .. " rotation") end end if c.frames > 1 then t:eq(#s.animations, 1, c.name .. ": one animation") t:eq(#s.animations[1].channels, 1 + 2 * ntags, c.name .. ": weights + tag channels") end end do local c = Fx[1] local _, e1 = Md3.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 500)))) t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "truncated: " .. tostring(e1)) local bad = buffer.fromstring(c.data) buffer.writei32(bad, 4, 14) local _, e2 = Md3.decode(Bytes.reader(bad)) t:check(e2 and e2.code == Err.UNSUPPORTED, "version 14") end local samples = {} for _, c in Fx do table.insert(samples, c.data) end t:fuzz("decode", function(b: buffer) return Md3.decode(Bytes.reader(b)) end, samples, { mutations = 80 }) t:done()