-- host/Roblox tests: the pure half of the EditableMesh adapter (meshPages, pageData); toEditableMesh needs Roblox. local T = require("@tests/T") local Host = require("@rosetta/host/Roblox") local Types = require("@rosetta/Types") local Err = require("@rosetta/core/Err") local t = T.suite("host_roblox") 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 near(a: number, b: number, eps: number?): boolean return math.abs(a - b) <= (eps or 1e-5) end local function grid(n: number): Types.Primitive local pos, nrm, uv, col = {}, {}, {}, {} for z = 0, n do for x = 0, n do table.insert(pos, x) table.insert(pos, 0) table.insert(pos, z) for _, v in { 0, 1, 0 } do table.insert(nrm, v) end table.insert(uv, x / n) table.insert(uv, z / n) for _, v in { 0.5, 0, 1, 0.25 } do table.insert(col, v) end end end local idx = {} for z = 0, n - 1 do for x = 0, n - 1 do local a = z * (n + 1) + x local b, c, d = a + 1, a + n + 1, a + n + 2 for _, v in { a, c, b, b, c, d } do table.insert(idx, v) end end end local ia = S.accessor(nil, "u16", 1, #idx) for i, v in idx do buffer.writeu16(ia.data, (i - 1) * 2, v) end return { attributes = { POSITION = f32(pos, 3), NORMAL = f32(nrm, 3), TEXCOORD_0 = f32(uv, 2), COLOR_0 = f32(col, 4) }, indices = ia, mode = "triangles", } end local prim = grid(10) -- 121 vertices, 200 triangles local pages = assert(Host.meshPages(prim, { maxTriangles = 64 })) t:eq(#pages, 4, "200 triangles / 64 per page") local covered = 0 for pi, pg in pages do t:eq(pg.first, covered, "page " .. pi .. " starts after the previous one") t:check(pg.count <= 64, "page " .. pi .. " within maxTriangles") t:eq(buffer.len(pg.corners), pg.count * 12, "page " .. pi .. " corners") for k = 0, pg.count * 3 - 1 do local src = buffer.readu16((prim.indices :: any).data, (pg.first * 3 + k) * 2) local l = buffer.readu32(pg.corners, k * 4) if pg.vertices[l + 1] ~= src then t:check(false, string.format("page %d corner %d maps back to %d", pi, k, src)) end end covered += pg.count end t:eq(covered, 200, "every triangle in a page") t:eq(pages[1].min[1], 0, "page bounds min") t:check(pages[1].max[3] > 0, "page bounds max") local one = assert(Host.meshPages(prim)) t:eq(#one, 1, "default limits: one page") t:eq(#one[1].vertices, 121, "every vertex once") local two = assert(Host.meshPages(prim, { maxTriangles = 64, doubleSided = true })) t:eq(#two, 7, "double-sided: 32 front triangles per page") local tight = assert(Host.meshPages(prim, { maxVertices = 30 })) for _, pg in tight do t:check(#pg.vertices <= 30, "maxVertices") end t:check(#tight > 1, "maxVertices splits") -- no indices: vertices in order local soup = { attributes = { POSITION = f32({ 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0, 0, 3, 0, 0, 2, 1, 0 }, 3) }, mode = "triangles" } local sp = assert(Host.meshPages(soup :: any)) t:eq(sp[1].count, 2, "unindexed triangles") t:eq(#sp[1].vertices, 6, "unindexed vertices") local _, e1 = Host.meshPages({ attributes = { POSITION = f32({ 0, 0, 0 }, 3) }, mode = "lines" } :: any) t:check(e1 and e1.code == Err.ARG, "lines are not triangles") local _, e2 = Host.meshPages({} :: any) t:check(e2 and e2.code == Err.ARG, "no POSITION") local bad = grid(1) buffer.writeu16((bad.indices :: any).data, 2, 999) local _, e3 = Host.meshPages(bad) t:check(e3 and e3.code == Err.ARG, "index out of range") local _, e4 = Host.pageData(prim, {} :: any) t:check(e4 and e4.code == Err.ARG, "pageData needs a page") local pg = one[1] local d = assert(Host.pageData(prim, pg, { scale = { 2, 1, 4 } })) t:eq(d.vertexCount, 121, "vertex count") t:eq(d.triangleCount, 200, "triangle count") t:eq(d.centre[1], 10, "centre x (5 * 2)") t:eq(d.centre[3], 20, "centre z (5 * 4)") local v0 = pg.vertices[1] -- source vertex of local 1 local sx0 = v0 % 11 t:eq(d.positions[1], (sx0 - 5) * 2, "position x relative to the centre, scaled") t:check(d.normals and near(d.normals[2], 1), "unit normal") t:check(d.uvs and near(d.uvs[1], sx0 / 10), "uv") t:check(d.colors and near(d.colors[1], 0.7353569, 1e-4), "linear 0.5 -> sRGB") t:check(d.colors and near(d.colors[3], 1) and near(d.colors[4], 0.25), "sRGB 1 and alpha kept") local slanted = { attributes = { POSITION = f32({ 0, 0, 0, 1, 0, 0, 0, 1, 0 }, 3), NORMAL = f32({ 1, 1, 0, 1, 1, 0, 1, 1, 0 }, 3) }, mode = "triangles" } local sd = assert(Host.pageData(slanted :: any, assert(Host.meshPages(slanted :: any))[1], { scale = { 2, 1, 1 }, origin = { 0, 0, 0 } })) t:check(near(sd.normals[1], 0.5 / math.sqrt(1.25)) and near(sd.normals[2], 1 / math.sqrt(1.25)), "normals scale by the inverse") t:eq(sd.positions[4], 2, "origin option") local fl = assert(Host.pageData(prim, pg, { flipV = true, normals = false, colors = false })) t:check(fl.normals == nil and fl.colors == nil, "attributes switched off") t:check(fl.uvs and near(fl.uvs[2], 1 - (v0 // 11) / 10), "flipV") t:done()