160 lines
6.4 KiB
Luau
160 lines
6.4 KiB
Luau
-- model/gltf KHR_draco_mesh_compression tests: Draco primitives against libdraco, transcoded files against their sources.
|
|
-- Fixtures: tools/fixtures/gltf_draco.py.
|
|
local T = require("@tests/T")
|
|
local Gltf = require("@rosetta/model/gltf")
|
|
local MeshOps = require("@rosetta/ops/MeshOps")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Cases = require("@tests/gen/gltf_draco")
|
|
|
|
local t = T.suite("gltf_draco")
|
|
|
|
local DT = { [1] = "i8", [2] = "u8", [3] = "i16", [4] = "u16", [5] = "i32", [6] = "u32", [9] = "f32" }
|
|
local SAME = { i8 = "i8", snorm8 = "i8", u8 = "u8", unorm8 = "u8", i16 = "i16", snorm16 = "i16", u16 = "u16", unorm16 = "u16", i32 = "i32", u32 = "u32", f32 = "f32" }
|
|
|
|
local function decode(data: string, files: any): (any, any)
|
|
return Gltf.decode(Bytes.reader(buffer.fromstring(data)), { files = files })
|
|
end
|
|
|
|
local function triangles(scene: any): number
|
|
local n = 0
|
|
for _, m in scene.meshes do
|
|
for _, p in m.primitives do
|
|
if p.mode == "triangles" then
|
|
n += if p.indices then p.indices.count // 3 else p.attributes.POSITION.count // 3
|
|
end
|
|
end
|
|
end
|
|
return n
|
|
end
|
|
|
|
local checked = { prims = 0, attrs = 0, converted = 0, originals = 0, roundTrips = 0 }
|
|
local samples = {}
|
|
for _, c in Cases do
|
|
local scene, err = decode(c.data, c.files)
|
|
t:check(scene, c.name .. ": decode: " .. tostring(err))
|
|
if scene then
|
|
for _, ref in c.prims do
|
|
local label = string.format("%s: mesh %d primitive %d", c.name, ref.mesh, ref.prim)
|
|
local prim = scene.meshes[ref.mesh] and scene.meshes[ref.mesh].primitives[ref.prim]
|
|
t:check(prim, label .. ": present")
|
|
if prim then
|
|
t:eq(prim.attributes.POSITION.count, ref.points, label .. ": points")
|
|
if ref.faces then
|
|
t:eq(prim.indices.count, ref.faces * 3, label .. ": indices")
|
|
t:eq(Crc32.of(prim.indices.data), ref.indices, label .. ": index CRC")
|
|
end
|
|
for name, a in ref.attrs do
|
|
local acc = prim.attributes[name]
|
|
t:check(acc, label .. ": " .. name)
|
|
if acc then
|
|
if SAME[acc.type] == DT[a.dataType] then
|
|
t:eq(Crc32.of(acc.data), a.crc, label .. ": " .. name .. " values")
|
|
checked.attrs += 1
|
|
else
|
|
checked.converted += 1
|
|
end
|
|
end
|
|
end
|
|
checked.prims += 1
|
|
end
|
|
end
|
|
local o = c.original
|
|
if o then
|
|
local orig, oerr = decode(o.data, o.files)
|
|
t:check(orig, c.name .. ": original: " .. tostring(oerr))
|
|
if orig then
|
|
t:eq(#scene.meshes, #orig.meshes, c.name .. ": meshes")
|
|
for i, m in orig.meshes do
|
|
t:eq(#scene.meshes[i].primitives, #m.primitives, c.name .. ": mesh " .. i .. " primitives")
|
|
end
|
|
t:eq(#scene.nodes, #orig.nodes, c.name .. ": nodes")
|
|
-- draco_transcoder adds a default material to files without one
|
|
t:check(#scene.materials == #orig.materials or (#orig.materials == 0 and #scene.materials == 1), c.name .. ": materials")
|
|
t:eq(#scene.skins, #orig.skins, c.name .. ": skins")
|
|
t:eq(#scene.animations, #orig.animations, c.name .. ": animations")
|
|
t:eq(triangles(scene), triangles(orig), c.name .. ": triangles")
|
|
local lo, hi = MeshOps.bounds(MeshOps.merge(scene))
|
|
local olo, ohi = MeshOps.bounds(MeshOps.merge(orig))
|
|
if lo and olo then
|
|
-- the quantization step relative to the scene: 11-bit positions by default, 16-bit for "fine"
|
|
local rel = if string.find(c.name, "fine", 1, true) then 1e-4 else 5e-3
|
|
local ok = true
|
|
for k = 1, 3 do
|
|
local eps = math.max(ohi[k] - olo[k], 1e-6) * rel
|
|
if math.abs(lo[k] - olo[k]) > eps or math.abs(hi[k] - ohi[k]) > eps then
|
|
ok = false
|
|
end
|
|
end
|
|
t:check(ok, c.name .. ": world bounds within the quantization step")
|
|
end
|
|
checked.originals += 1
|
|
end
|
|
end
|
|
local glb = Gltf.encode(scene, { binary = true })
|
|
t:check(glb, c.name .. ": encode")
|
|
if glb then
|
|
t:check(not string.find(buffer.tostring(glb), "KHR_draco_mesh_compression", 1, true), c.name .. ": no Draco extension written")
|
|
local again = Gltf.decode(Bytes.reader(glb))
|
|
t:check(again and triangles(again) == triangles(scene), c.name .. ": round trip")
|
|
checked.roundTrips += 1
|
|
end
|
|
if #c.data < 6000 and next(c.files) == nil then
|
|
table.insert(samples, c.data)
|
|
end
|
|
end
|
|
end
|
|
t:note("%d Draco primitives like libdraco (%d attributes byte for byte, %d converted to their accessor type), %d against the uncompressed sources, %d round trips", checked.prims, checked.attrs, checked.converted, checked.originals, checked.roundTrips)
|
|
|
|
-- an accessor type other than the Draco attribute's: u8 normalized colours declared as f32
|
|
do
|
|
local Draco = require("@rosetta/codecs/model/Draco")
|
|
local Base64 = require("@rosetta/core/Base64")
|
|
local DracoCases = require("@tests/gen/draco_cases")
|
|
local drc
|
|
for _, dc in DracoCases do
|
|
if dc.name == "enc/test_generic.ply.cl7" then
|
|
drc = dc.data
|
|
end
|
|
end
|
|
local g = drc and Draco.decode(drc)
|
|
t:check(g, "colour .drc fixture")
|
|
if g then
|
|
local pos, col
|
|
for _, a in g.attributes do
|
|
if a.name == "POSITION" then
|
|
pos = a
|
|
elseif a.name == "COLOR" then
|
|
col = a
|
|
end
|
|
end
|
|
local n = g.numPoints
|
|
local doc = string.format(
|
|
'{"asset":{"version":"2.0"},"extensionsUsed":["KHR_draco_mesh_compression"],"extensionsRequired":["KHR_draco_mesh_compression"],'
|
|
.. '"buffers":[{"byteLength":%d,"uri":"data:application/octet-stream;base64,%s"}],"bufferViews":[{"buffer":0,"byteLength":%d}],'
|
|
.. '"accessors":[{"componentType":5126,"count":%d,"type":"VEC3"},{"componentType":5126,"count":%d,"type":"VEC%d"},{"componentType":5125,"count":%d,"type":"SCALAR"}],'
|
|
.. '"meshes":[{"primitives":[{"attributes":{"POSITION":0,"COLOR_0":1},"indices":2,"extensions":{"KHR_draco_mesh_compression":{"bufferView":0,"attributes":{"POSITION":%d,"COLOR_0":%d}}}}]}],'
|
|
.. '"nodes":[{"mesh":0}],"scenes":[{"nodes":[0]}]}',
|
|
#drc, Base64.encode(buffer.fromstring(drc)), #drc, n, n, col.components, g.numFaces * 3, pos.uniqueId, col.uniqueId)
|
|
local scene, err = decode(doc, {})
|
|
t:check(scene, "converted accessor: " .. tostring(err))
|
|
if scene then
|
|
local acc = scene.meshes[1].primitives[1].attributes.COLOR_0
|
|
t:eq(acc.type, "f32", "colour accessor type")
|
|
local ok = true
|
|
for i = 0, n * col.components - 1 do
|
|
local want = buffer.readu8(col.data, i) / 255
|
|
if math.abs(buffer.readf32(acc.data, i * 4) - want) > 1e-6 then
|
|
ok = false
|
|
end
|
|
end
|
|
t:check(ok, "u8 normalized colours -> f32 / 255")
|
|
end
|
|
end
|
|
end
|
|
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Gltf.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 6, garbage = 50 })
|
|
t:done()
|