Files
rosetta/tests/model/_soup.luau
T

52 lines
1.8 KiB
Luau

-- tests/model/_soup: helpers for the game model tests: a primitive's triangle soup (positions, morph target frames,
-- texture coordinates in index order) and a tolerant comparison against assimp's soup.
local Types = require("@formats/Types")
local Soup = {}
-- positions(prim, target?) -> f32 x3 buffer of the corners in index order (target k adds morph target k's deltas)
function Soup.positions(prim: any, target: number?): buffer
local pos = Types.Scene.reader(prim.attributes.POSITION) :: any
local d = if target then Types.Scene.reader(prim.targets[target].POSITION) :: any else nil
local idx = prim.indices
local n = idx.count
local out = buffer.create(n * 12)
for k = 0, n - 1 do
local v = buffer.readu32(idx.data, k * 4)
for c = 0, 2 do
buffer.writef32(out, k * 12 + c * 4, pos(v, c) + (if d then d(v, c) else 0))
end
end
return out
end
-- uvs(prim, set?, flip?) -> { u, v, ... } of the corners in index order (flip: v -> 1 - v, assimp's convention)
function Soup.uvs(prim: any, set: number?, flip: boolean?): { number }
local tc = Types.Scene.reader(prim.attributes["TEXCOORD_" .. (set or 0)]) :: any
local idx = prim.indices
local out = {}
for k = 0, idx.count - 1 do
local v = buffer.readu32(idx.data, k * 4)
table.insert(out, tc(v, 0))
table.insert(out, if flip then 1 - tc(v, 1) else tc(v, 1))
end
return out
end
-- close(a: buffer f32 | {number}, b: {number}, tol) -> true | false, first index that differs
function Soup.close(a: any, b: { number }, tol: number): (boolean, number?)
local n = if type(a) == "buffer" then buffer.len(a) // 4 else #a
if n ~= #b then
return false, -1
end
for i = 1, n do
local x = if type(a) == "buffer" then buffer.readf32(a, (i - 1) * 4) else a[i]
if math.abs(x - b[i]) > tol * math.max(1, math.abs(b[i])) then
return false, i
end
end
return true, nil
end
return Soup