100 lines
3.3 KiB
Luau
100 lines
3.3 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;
|
|
-- "neg": v -> -v, assimp's Half-Life loader)
|
|
function Soup.uvs(prim: any, set: number?, flip: (boolean | string)?): { 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 == "neg" then -tc(v, 1) elseif 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
|
|
|
|
-- triangles(a, b, tol, stride?) -> true when the triangle lists a and b (flat numbers, stride per corner: 3 for
|
|
-- positions, 2 for texture coordinates) hold the same triangles in the same order, each up to a cyclic rotation of
|
|
-- its corners (assimp rotates them); false, the first differing triangle otherwise
|
|
function Soup.triangles(a: { number }, b: { number }, tol: number, stride: number?): (boolean, number?)
|
|
local s = stride or 3
|
|
if #a ~= #b then
|
|
return false, -1
|
|
end
|
|
local per = 3 * s
|
|
for t = 0, #a // per - 1 do
|
|
local found = false
|
|
for rot = 0, 2 do
|
|
local same = true
|
|
for c = 0, 2 do
|
|
local ca, cb = t * per + c * s, t * per + ((c + rot) % 3) * s
|
|
for k = 1, s do
|
|
if math.abs(a[ca + k] - b[cb + k]) > tol * math.max(1, math.abs(b[cb + k])) then
|
|
same = false
|
|
end
|
|
end
|
|
end
|
|
if same then
|
|
found = true
|
|
break
|
|
end
|
|
end
|
|
if not found then
|
|
return false, t
|
|
end
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
-- list(buf) -> the f32 values of a buffer as numbers
|
|
function Soup.list(b: buffer): { number }
|
|
local out = table.create(buffer.len(b) // 4)
|
|
for i = 0, buffer.len(b) // 4 - 1 do
|
|
out[i + 1] = buffer.readf32(b, i * 4)
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- slice(list, from, n) -> n values from index from (1-based)
|
|
function Soup.slice(list: { number }, from: number, n: number): { number }
|
|
return table.move(list, from, from + n - 1, 1, table.create(n))
|
|
end
|
|
|
|
return Soup
|