-- ops/MeshOps tests against analytic references (closed forms). local T = require("@tests/T") local MeshOps = require("@rosetta/ops/MeshOps") local Types = require("@rosetta/Types") local Err = require("@rosetta/core/Err") local F = require("@rosetta") local t = T.suite("meshops") local S = Types.Scene local function f32(list: { number }, comps: number): Types.Accessor local a = S.accessor(nil, "f32", comps, #list // comps) for i, v in list do buffer.writef32(a.data, (i - 1) * 4, v) end return a end local function u32(list: { number }): Types.Accessor local a = S.accessor(nil, "u32", 1, #list) for i, v in list do buffer.writeu32(a.data, (i - 1) * 4, v) end return a end local function idx(acc: Types.Accessor): { number } local out = {} for i = 0, acc.count - 1 do out[i + 1] = buffer.readu32(acc.data, i * 4) end return out end local function get(acc: Types.Accessor, i: number, c: number): number return (S.reader(acc) :: any)(i, c) end local function near(a: number, b: number, eps: number?): boolean return math.abs(a - b) <= (eps or 1e-5) end local function area(prim: any): number local pos, ind = prim.attributes.POSITION, prim.indices local total = 0 for tri = 0, ind.count // 3 - 1 do local a, b, c = buffer.readu32(ind.data, tri * 12), buffer.readu32(ind.data, tri * 12 + 4), buffer.readu32(ind.data, tri * 12 + 8) local e1 = { get(pos, b, 0) - get(pos, a, 0), get(pos, b, 1) - get(pos, a, 1), get(pos, b, 2) - get(pos, a, 2) } local e2 = { get(pos, c, 0) - get(pos, a, 0), get(pos, c, 1) - get(pos, a, 1), get(pos, c, 2) - get(pos, 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] total += math.sqrt(x * x + y * y + z * z) / 2 end return total end local function cube(): Types.Primitive local P = {} local faces = { { { 0, 0, 0 }, { 0, 1, 0 }, { 1, 1, 0 }, { 1, 0, 0 } }, { { 0, 0, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 0, 1, 1 } }, { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 0, 1 }, { 0, 0, 1 } }, { { 0, 1, 0 }, { 0, 1, 1 }, { 1, 1, 1 }, { 1, 1, 0 } }, { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 1, 0 } }, { { 1, 0, 0 }, { 1, 1, 0 }, { 1, 1, 1 }, { 1, 0, 1 } }, } local ind, sizes = {}, {} for _, f in faces do for _, v in f do table.insert(P, v[1]) table.insert(P, v[2]) table.insert(P, v[3]) table.insert(ind, #ind) end table.insert(sizes, 4) end return { attributes = { POSITION = f32(P, 3) }, indices = u32(ind), faces = u32(sizes), mode = "polygons" } end do local c = cube() local tri = MeshOps.triangulate(c) :: any t:check(tri.mode == "triangles" and tri.indices.count == 36, "cube quads -> 12 triangles") t:check(near(area(tri), 6), "cube area 6") -- every triangle faces outward (the cube is centred at 0.5) local nm = MeshOps.computeNormals(MeshOps.unweld(tri)) :: any local outward = true for v = 0, nm.attributes.POSITION.count - 1 do local d = 0 for k = 0, 2 do d += (get(nm.attributes.POSITION, v, k) - 0.5) * get(nm.attributes.NORMAL, v, k) end if d <= 0 then outward = false end end t:check(outward, "triangulated winding keeps the faces outward") local P = f32({ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 2, 0, 1, 2, 0 }, 3) local strip = MeshOps.triangulate({ attributes = { POSITION = P }, mode = "triangle_strip" }) :: any t:check(table.concat(idx(strip.indices), ",") == "0,1,2,2,1,3,2,3,4,4,3,5", "strip unrolled with alternating winding") t:check(near(area(strip), 2), "strip area") local fan = MeshOps.triangulate({ attributes = { POSITION = P }, indices = u32({ 0, 1, 3, 2 }), mode = "triangle_fan" }) :: any t:check(table.concat(idx(fan.indices), ",") == "0,1,3,0,3,2", "fan unrolled") -- a concave n-gon (a star) by ear clipping vs the shoelace area local star, sidx = {}, {} local shoelace = 0 for k = 0, 9 do local r = if k % 2 == 0 then 2 else 0.8 local a = math.pi * 2 * k / 10 table.insert(star, r * math.cos(a)) table.insert(star, 0) table.insert(star, -r * math.sin(a)) table.insert(sidx, k) end for k = 0, 9 do local x1, z1 = star[k * 3 + 1], star[k * 3 + 3] local x2, z2 = star[((k + 1) % 10) * 3 + 1], star[((k + 1) % 10) * 3 + 3] shoelace += x1 * z2 - x2 * z1 end local sp = { attributes = { POSITION = f32(star, 3) }, indices = u32(sidx), faces = u32({ 10 }), mode = "polygons" } local ear = MeshOps.triangulate(sp) :: any t:check(ear.indices.count == 24 and near(area(ear), math.abs(shoelace) / 2, 1e-4), "star: ear clipping area " .. area(ear) .. " = " .. math.abs(shoelace) / 2) t:check(area(MeshOps.triangulate(sp, { method = "fan" }) :: any) ~= area(ear), "the fan gets a concave polygon wrong") -- degenerate polygons: collinear corners, faces of 1 or 2 corners are dropped local deg = { attributes = { POSITION = f32({ 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0 }, 3) }, indices = u32({ 0, 1, 2, 3, 0, 1 }), faces = u32({ 4, 2 }), mode = "polygons" } local d = MeshOps.triangulate(deg) :: any t:check(d and d.indices.count == 6 and near(area(d), 0), "collinear quad: two zero-area triangles, the 2-gon dropped") local lines = { attributes = { POSITION = P }, mode = "lines" } t:check(MeshOps.triangulate(lines) == lines, "lines come back unchanged") end do local tri = MeshOps.triangulate(cube()) :: any MeshOps.computeNormals(MeshOps.unweld(tri)) local soup = MeshOps.computeNormals(MeshOps.unweld(tri)) :: any -- non-uniform scale: normals by the inverse transpose stay perpendicular to the faces local m = { 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.5, 0, 10, 20, 30, 1 } MeshOps.transform(soup, m) local lo, hi = MeshOps.bounds(soup) t:check(lo and near(lo[1], 10) and near(hi[1], 12) and near(lo[2], 20) and near(hi[2], 21) and near(lo[3], 30) and near(hi[3], 30.5), "transformed bounds") local ok = true for tri0 = 0, soup.attributes.POSITION.count // 3 - 1 do local a = tri0 * 3 for e = 1, 2 do local d = 0 for k = 0, 2 do d += (get(soup.attributes.POSITION, a + e, k) - get(soup.attributes.POSITION, a, k)) * get(soup.attributes.NORMAL, a, k) end if math.abs(d) > 1e-5 then ok = false end end end t:check(ok, "normals stay perpendicular under non-uniform scale") -- a mirror flips the winding: recomputed normals still agree with the transformed ones local mir = MeshOps.computeNormals(MeshOps.unweld(MeshOps.triangulate(cube()) :: any)) :: any local before = MeshOps.computeNormals(MeshOps.unweld(MeshOps.triangulate(cube()) :: any)) :: any MeshOps.transform(mir, { -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }) local saved = buffer.tostring(mir.attributes.NORMAL.data) local re = MeshOps.computeNormals(MeshOps.unweld(mir)) :: any local agree = true for v = 0, re.attributes.NORMAL.count - 1 do for k = 0, 2 do if not near(get(re.attributes.NORMAL, v, k), buffer.readf32(buffer.fromstring(saved), (v * 3 + k) * 4), 1e-5) then agree = false end end end t:check(agree and before ~= nil, "mirror: winding reversed (recomputed normals = transformed normals)") local _, e = MeshOps.transform(mir, { 1, 2, 3 }) t:check(e and e.code == Err.ARG, "transform: a bad matrix") end do local rings, segs = 16, 32 local P, UV, ind = {}, {}, {} for i = 0, rings do local th = math.pi * i / rings for j = 0, segs do local ph = 2 * math.pi * j / segs table.insert(P, math.sin(th) * math.cos(ph)) table.insert(P, math.cos(th)) table.insert(P, math.sin(th) * math.sin(ph)) table.insert(UV, j / segs) table.insert(UV, i / rings) end end for i = 0, rings - 1 do for j = 0, segs - 1 do local a, b = i * (segs + 1) + j, i * (segs + 1) + j + 1 local c, d = a + segs + 1, b + segs + 1 for _, v in { a, b, c, b, d, c } do table.insert(ind, v) end end end local sphere: any = { attributes = { POSITION = f32(P, 3), TEXCOORD_0 = f32(UV, 2) }, indices = u32(ind), mode = "triangles" } MeshOps.computeNormals(sphere) -- smooth normals of interior vertices (not on the seam / poles) point along the radius local worst = 0 for i = 1, rings - 1 do for j = 1, segs - 1 do local v = i * (segs + 1) + j local dot = 0 for k = 0, 2 do dot += get(sphere.attributes.NORMAL, v, k) * get(sphere.attributes.POSITION, v, k) end worst = math.max(worst, 1 - dot) end end t:check(worst < 1e-3, "sphere normals = radial directions (worst 1 - cos " .. worst .. ")") t:check(get(sphere.attributes.NORMAL, (rings // 2) * (segs + 1) + 3, 1) < 1e-3, "outward, not inward") MeshOps.computeTangents(sphere) local tan = sphere.attributes.TANGENT t:check(tan and tan.components == 4, "TANGENT vec4") local ortho, unit, hand = 0, 0, true for i = 1, rings - 1 do for j = 1, segs - 1 do local v = i * (segs + 1) + j local d, l = 0, 0 for k = 0, 2 do d += get(tan, v, k) * get(sphere.attributes.NORMAL, v, k) l += get(tan, v, k) ^ 2 end ortho = math.max(ortho, math.abs(d)) unit = math.max(unit, math.abs(l - 1)) if math.abs(get(tan, v, 3)) ~= 1 then hand = false end -- u grows with phi: the tangent follows d/dphi = (-sin phi, 0, cos phi) local ph = 2 * math.pi * j / segs if get(tan, v, 0) * -math.sin(ph) + get(tan, v, 2) * math.cos(ph) < 0.99 then hand = false end end end t:check(ortho < 1e-5 and unit < 1e-5 and hand, string.format("tangents orthogonal (%g), unit (%g), along +u", ortho, unit)) local _, e = MeshOps.computeTangents({ attributes = { POSITION = f32(P, 3) }, indices = u32(ind), mode = "triangles" }) t:check(e and e.code == Err.ARG, "computeTangents without uv") -- flat normals on the cube: 36 corners, axis-aligned local flat = MeshOps.computeNormals(MeshOps.triangulate(cube()) :: any, { flat = true }) :: any t:check(flat.attributes.NORMAL.count == 36 and flat.indices == nil, "flat: unwelded corners") local axis = true for v = 0, 35 do local s = math.abs(get(flat.attributes.NORMAL, v, 0)) + math.abs(get(flat.attributes.NORMAL, v, 1)) + math.abs(get(flat.attributes.NORMAL, v, 2)) if not near(s, 1) then axis = false end end t:check(axis, "flat cube normals are axis-aligned") end do local soup = MeshOps.unweld(MeshOps.triangulate(cube()) :: any) :: any t:eq(soup.attributes.POSITION.count, 36, "unwelded cube: 36 vertices") local w = MeshOps.weld(soup) :: any t:check(w.attributes.POSITION.count == 8 and w.indices.count == 36, "welded by position: 8") t:check(near(area(w), 6), "weld keeps the surface") local withN = MeshOps.weld(MeshOps.computeNormals(soup, { flat = true }) :: any) :: any t:eq(withN.attributes.POSITION.count, 24, "welded with flat normals: 24") -- an epsilon merges near-identical positions local P = f32({ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1e-7, 0, 0, 1, 1e-7, 0, 0, 1, 1e-7 }, 3) local two = { attributes = { POSITION = P }, mode = "triangles" } t:eq((MeshOps.weld(two) :: any).attributes.POSITION.count, 6, "exact: 6") t:eq((MeshOps.weld(two, { epsilon = 1e-4 }) :: any).attributes.POSITION.count, 3, "epsilon: 3") local positionsOnly = MeshOps.weld(withN, { positionsOnly = true }) :: any t:eq(positionsOnly.attributes.POSITION.count, 8, "positionsOnly: 8") end do local s = S.new() local tri = MeshOps.triangulate(cube()) :: any tri.attributes.COLOR_0 = S.accessor(nil, "unorm8", 4, tri.attributes.POSITION.count) buffer.fill(tri.attributes.COLOR_0.data, 0, 255) s.meshes[1] = { primitives = { tri } } local other = { attributes = { POSITION = f32({ 0, 0, 0, 1, 0, 0, 0, 1, 0 }, 3), _CUSTOM = S.accessor(nil, "u16", 1, 3) }, mode = "triangles", material = 2 } s.meshes[2] = { primitives = { other } } s.nodes = { { children = { 2, 3 } }, { children = {}, mesh = 1, translation = { 10, 0, 0 } }, { children = {}, mesh = 1, translation = { -10, 0, 0 }, scale = { 2, 2, 2 } }, { children = {}, mesh = 2, rotation = { 0, 0, math.sqrt(0.5), math.sqrt(0.5) } }, } s.roots = { 1, 4 } local m = MeshOps.merge(s) :: any t:check(m.indices.count == 36 * 2 + 3, "two cube instances and a triangle") t:check(near(area(m), 6 + 24 + 0.5), "instances baked (scale 2: area x4)") t:check(m.attributes.COLOR_0 and m.attributes._CUSTOM and m.attributes.COLOR_0.type == "f32", "attributes unioned (f32)") local white = get(m.attributes.COLOR_0, m.attributes.POSITION.count - 1, 0) t:eq(white, 1, "missing colours are white") local lo, hi = MeshOps.bounds(s) t:check(lo and near(lo[1], -10) and near(hi[1], 11) and near(lo[2], 0) and near(hi[2], 2), "scene bounds in world space") local byMat = MeshOps.merge(s, { byMaterial = true }) :: any t:check(#byMat == 2 and byMat[1].material == nil and byMat[2].material == 2, "byMaterial") local _, e = MeshOps.merge(5 :: any) t:check(e and e.code == Err.ARG, "merge junk") end -- merge with morph targets: by name across meshes, zero where missing, deltas through the node's linear part only do local s = S.new() local tri = { 0, 0, 0, 1, 0, 0, 0, 1, 0 } local a = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles", targets = { { POSITION = f32({ 0, 0, 1, 0, 0, 1, 0, 0, 1 }, 3) }, { POSITION = f32({ 1, 0, 0, 1, 0, 0, 1, 0, 0 }, 3) }, } } local b = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles", targets = { { POSITION = f32({ 0, 2, 0, 0, 2, 0, 0, 2, 0 }, 3) }, { POSITION = f32({ 0, 0, 3, 0, 0, 3, 0, 0, 3 }, 3) }, } } local c = { attributes = { POSITION = f32(tri, 3) }, mode = "triangles" } s.meshes = { { primitives = { a }, weights = { 0.5, 0.25 }, extras = { targetNames = { "smile", "blink" } } }, { primitives = { b }, weights = { 0.75, 1 }, extras = { targetNames = { "blink", "puff" } } }, { primitives = { c } }, } s.nodes = { { children = {}, mesh = 1, scale = { 2, 2, 2 } }, { children = {}, mesh = 2, translation = { 5, 0, 0 } }, { children = {}, mesh = 3 }, } s.roots = { 1, 2, 3 } t:check(MeshOps.merge(s).targets == nil, "merge drops targets by default") local m = MeshOps.merge(s, { targets = true }) :: any local names = m.extras and m.extras.targetNames or {} t:check(#m.targets == 3 and names[1] == "smile" and names[2] == "blink" and names[3] == "puff", "targets merged by name") t:check(m.extras.weights[1] == 0.5 and m.extras.weights[2] == 0.25 and m.extras.weights[3] == 1, "default weights (first mesh wins)") local function d(ti: number, v: number, k: number): number return get(m.targets[ti].POSITION, v, k) end t:check(near(d(1, 0, 2), 2) and near(d(1, 3, 2), 0) and near(d(1, 6, 2), 0), "smile: scaled by the node, zero elsewhere") t:check(near(d(2, 0, 0), 2) and near(d(2, 3, 1), 2) and near(d(2, 6, 0), 0), "blink from both meshes, translation ignored") t:check(near(d(3, 3, 2), 3) and near(d(3, 0, 2), 0), "puff from the second mesh only") t:check(get(a.targets[1].POSITION, 0, 2) == 1, "the source targets are untouched") local list = MeshOps.merge({ a, c }, { targets = true }) :: any t:check(#list.targets == 2 and list.extras.targetNames[1] == "1", "a list of primitives: targets by index") end do local s = S.fromPrimitive(MeshOps.triangulate(cube()) :: any) s.nodes[1].translation = { 1, 2, 3 } s.meta.unit = 0.01 MeshOps.setUnit(s, 1) local lo, hi = MeshOps.bounds(s) t:check(lo and near(lo[1], 0.01) and near(hi[1], 0.02) and near(lo[3], 0.03), "cm -> m") t:eq(s.meta.unit, 1, "meta.unit updated") s.meta.up = "z" MeshOps.setUp(s, "y") local lo2, hi2 = MeshOps.bounds(s) -- z-up (x, y, z) -> y-up (x, z, -y) t:check(lo2 and near(lo2[2], 0.03) and near(hi2[2], 0.04) and near(lo2[3], -0.03) and near(hi2[3], -0.02), "z-up -> y-up") t:check(MeshOps.setUp(s, "x" :: any) == nil, "setUp: bad axis") end do local v = Types.Voxels.new(4, 4, 4) for x = 0, 3 do for y = 0, 3 do for z = 0, 3 do Types.Voxels.set(v, x, y, z, 1) end end end local p = MeshOps.voxelsToPrimitive(v, { pivot = "corner" }) :: any t:check(p.indices.count == 36 and near(area(p), 96), "a full 4x4x4 block: 6 quads, area 96") Types.Voxels.set(v, 1, 1, 3, 2) local p2 = MeshOps.voxelsToPrimitive(v, { pivot = "corner" }) :: any t:check(near(area(p2), 96) and p2.indices.count > 36, "a different colour splits the top face, same area") local scaled = MeshOps.voxelsToPrimitive(v, { scale = 0.5 }) :: any local lo, hi = MeshOps.bounds(scaled) t:check(lo and near(lo[1], -1) and near(hi[1], 1), "scale and centre pivot") end -- the api encodes image-only textures to PNG before a model writer sees them do local s = S.fromPrimitive(MeshOps.triangulate(cube()) :: any) s.textures[1] = { image = Types.Image.new(2, 2) } s.materials[1] = S.material("m") s.materials[1].baseColorTexture = { texture = 1 } s.meshes[1].primitives[1].material = 1 local glb = F.save(s, nil, { format = "glb" }) t:check(glb and s.textures[1].mime == "image/png" and s.textures[1].data ~= nil, "F.save: texture image -> PNG bytes") local back = F.load(glb) local _, errs = F.model.loadTextures(back) t:check(back.textures[1].image and back.textures[1].image.width == 2 and next(errs) == nil, "PNG texture through GLB and back") end t:done()