merge ws/video2

This commit is contained in:
4DBug committed 2026-10-31 15:31:56 -05:00
commit c65a66695f
63 files changed
+12207 -1509

No files matched your search

+349
View File
@@ -2,7 +2,12 @@
-- host/Roblox: no file system (sources are buffers or strings, e.g. from HttpService or an asset); converts the
-- standard types to engine objects:
-- host.toEditableImage(img) -> EditableImage (rgba8; other formats are converted by the caller first)
-- host.meshPages(prim, opts?) -> { MeshPage } a triangle Primitive split into pages that fit one EditableMesh
-- host.pageData(prim, page, opts?) -> PageData a page's vertices as flat arrays (scaled, centred, sRGB colours)
-- host.toEditableMesh(prim, page?, opts?) -> EditableMesh, centre one page as an EditableMesh
-- meshPages / pageData are pure (no engine calls: tests/host/roblox.luau runs them on every runtime).
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Host = {}
Host.name = "roblox"
@@ -37,4 +42,348 @@ function Host.toEditableImage(img: any): (any, Err.Err?)
return ei
end
-- ---------------------------------------------------------------------------------------------------------- meshes
-- A triangle Primitive (F.ops.mesh.merge gives one in world space) becomes EditableMeshes page by page. One
-- EditableMesh holds at most ~20k triangles and 60k vertices (measured in Studio: surfacenet-luau MeshOut), and a client
-- has only 8 growable ones at a time: build a page, turn it into what you keep (a MeshPart, a FixedSize copy through
-- AssetService:CreateEditableMeshAsync), destroy it, then build the next.
--
-- local prims = F.ops.mesh.merge(scene, { byMaterial = true })
-- for _, prim in prims do
-- for _, page in assert(F.host.meshPages(prim)) do
-- local em, centre = assert(F.host.toEditableMesh(prim, page, { scale = 1 / 0.28 })) -- metres -> studs
-- local part = AssetService:CreateMeshPartAsync(Content.fromObject(em))
-- part.Position = Vector3.new(centre[1], centre[2], centre[3])
-- end
-- end
--
-- Axes: Roblox is right-handed, y up, like glTF: z-up sources (STL, VOX, CAD) go through F.ops.mesh.setUp(scene, "y")
-- before merging. Winding: counter-clockwise front faces, as glTF. UVs keep the glTF top-left origin (opts.flipV
-- turns them over). Colours: COLOR_0 is linear (glTF); EditableMesh colours are sRGB, so they are converted.
export type MeshPage = {
first: number, -- the first triangle (0-based) of the primitive in this page
count: number, -- triangles
vertices: { number }, -- local vertex i (1-based) = source vertex vertices[i] (0-based)
corners: buffer, -- u32 local vertex (0-based) per corner, 3 per triangle
min: { number }, -- bounds of the page's positions (source units)
max: { number },
}
export type PageData = {
vertexCount: number,
triangleCount: number,
positions: { number }, -- x, y, z per vertex: (source - origin) * scale
normals: { number }?, -- x, y, z per vertex (unit; NORMAL scaled by the inverse scale)
uvs: { number }?, -- u, v per vertex (TEXCOORD_<opts.texCoord or 0>)
colors: { number }?, -- r, g, b, a per vertex, sRGB 0..1 (from linear COLOR_0)
corners: buffer, -- as MeshPage.corners
centre: { number }, -- origin * scale: where the page's local (0, 0, 0) sits
}
export type MeshOptions = {
maxTriangles: number?, -- per page, default 20000
maxVertices: number?, -- per page, default 60000
doubleSided: boolean?, -- toEditableMesh adds a back face per triangle (meshPages counts them), default false
scale: (number | { number })?, -- uniform or per axis, default 1
origin: { number }?, -- subtracted before scaling, default the page's bounds centre
texCoord: number?, -- the TEXCOORD_n set for uvs, default 0
flipV: boolean?, -- v = 1 - v, default false
normals: boolean?, -- NORMAL when present, default true
uvs: boolean?, -- default true
colors: boolean?, -- COLOR_0 when present, default true
editableMesh: any?, -- toEditableMesh appends to this mesh instead of creating one
yield: (() -> ())?, -- toEditableMesh calls it every yieldEvery triangles (e.g. a frame budget check)
yieldEvery: number?, -- default 2000
}
-- the index of each corner (0-based vertex numbers) or nil, Err
local function cornerReader(prim: any): ((number) -> number)?
local idx = prim.indices
if not idx then
return function(i: number): number
return i
end
end
local get = Types.Scene.reader(idx)
if not get then
return nil
end
return function(i: number): number
return (get :: any)(i, 0)
end
end
local function checkPrim(prim: any): (number, Err.Err?)
if type(prim) ~= "table" or type(prim.attributes) ~= "table" or type(prim.attributes.POSITION) ~= "table" then
return 0, Err.new(Err.ARG, "expected a Primitive with POSITION")
end
if (prim.mode or "triangles") ~= "triangles" then
return 0, Err.new(Err.ARG, "needs a triangle primitive (F.ops.mesh.merge or triangulate first), got " .. tostring(prim.mode))
end
local n = if prim.indices then prim.indices.count else prim.attributes.POSITION.count
return n // 3, nil
end
-- meshPages(prim, opts?) -> { MeshPage } | nil, Err: consecutive triangle runs, each within maxTriangles (back faces
-- counted when doubleSided) and maxVertices.
function Host.meshPages(prim: any, opts: MeshOptions?): ({ MeshPage }?, Err.Err?)
local tris, err = checkPrim(prim)
if err then
return nil, err
end
local o: MeshOptions = opts or {}
local per = if o.doubleSided then 2 else 1
local maxT = math.max(1, (o.maxTriangles or 20000) // per)
local maxV = math.max(3, o.maxVertices or 60000)
local pos = prim.attributes.POSITION
local getP = Types.Scene.reader(pos)
local corner = cornerReader(prim)
if not getP or not corner then
return nil, Err.new(Err.ARG, "unsupported accessor type")
end
local nv = pos.count
local pages: { MeshPage } = {}
local t = 0
while t < tris do
local map: { [number]: number } = {}
local verts: { number } = {}
local first = t
local lo = { math.huge, math.huge, math.huge }
local hi = { -math.huge, -math.huge, -math.huge }
local cs: { number } = {}
while t < tris and t - first < maxT do
local a, b, c = corner(t * 3), corner(t * 3 + 1), corner(t * 3 + 2)
if a >= nv or b >= nv or c >= nv or a < 0 or b < 0 or c < 0 then
return nil, Err.new(Err.ARG, string.format("triangle %d: vertex index out of range", t))
end
local fresh = (if map[a] then 0 else 1) + (if map[b] or b == a then 0 else 1) + (if map[c] or c == a or c == b then 0 else 1)
if #verts + fresh > maxV and t > first then
break
end
for _, v in { a, b, c } do
local l = map[v]
if not l then
l = #verts
verts[l + 1] = v
map[v] = l
for k = 1, 3 do
local x = (getP :: any)(v, k - 1)
if x < lo[k] then
lo[k] = x
end
if x > hi[k] then
hi[k] = x
end
end
end
cs[#cs + 1] = l
end
t += 1
end
local corners = buffer.create(#cs * 4)
for i, l in cs do
buffer.writeu32(corners, (i - 1) * 4, l)
end
pages[#pages + 1] = { first = first, count = t - first, vertices = verts, corners = corners, min = lo, max = hi }
end
return pages, nil
end
local function toSrgb(c: number): number
c = math.clamp(c, 0, 1)
return if c <= 0.0031308 then c * 12.92 else 1.055 * c ^ (1 / 2.4) - 0.055
end
-- pageData(prim, page, opts?) -> PageData | nil, Err: the page's vertex attributes as flat number arrays, ready for
-- engine calls (positions relative to the origin and scaled, unit normals, uvs, sRGB colours).
function Host.pageData(prim: any, page: MeshPage, opts: MeshOptions?): (PageData?, Err.Err?)
local _, err = checkPrim(prim)
if err then
return nil, err
end
if type(page) ~= "table" or type(page.vertices) ~= "table" or type(page.corners) ~= "buffer" then
return nil, Err.new(Err.ARG, "expected a MeshPage from meshPages")
end
local o: MeshOptions = opts or {}
local sc = o.scale or 1
local sx, sy, sz
if type(sc) == "table" then
sx, sy, sz = sc[1] or 1, sc[2] or 1, sc[3] or 1
else
sx, sy, sz = sc, sc, sc
end
local origin = o.origin
or { (page.min[1] + page.max[1]) / 2, (page.min[2] + page.max[2]) / 2, (page.min[3] + page.max[3]) / 2 }
local attrs = prim.attributes
local getP = Types.Scene.reader(attrs.POSITION)
local getN = if o.normals ~= false and attrs.NORMAL then Types.Scene.reader(attrs.NORMAL) else nil
local uvAcc = if o.uvs ~= false then attrs["TEXCOORD_" .. tostring(o.texCoord or 0)] else nil
local getT = if uvAcc then Types.Scene.reader(uvAcc) else nil
local colAcc = if o.colors ~= false then attrs.COLOR_0 else nil
local getC = if colAcc then Types.Scene.reader(colAcc) else nil
if not getP then
return nil, Err.new(Err.ARG, "unsupported POSITION accessor type")
end
local n = #page.vertices
local positions = table.create(n * 3, 0)
local normals = if getN then table.create(n * 3, 0) else nil
local uvs = if getT then table.create(n * 2, 0) else nil
local colors = if getC then table.create(n * 4, 1) else nil
local ox, oy, oz = origin[1], origin[2], origin[3]
local flip = o.flipV == true
local alpha = colAcc and colAcc.components >= 4
for i, v in page.vertices do
local j = (i - 1) * 3
positions[j + 1] = ((getP :: any)(v, 0) - ox) * sx
positions[j + 2] = ((getP :: any)(v, 1) - oy) * sy
positions[j + 3] = ((getP :: any)(v, 2) - oz) * sz
if normals then
-- normals of a scaled mesh scale by the inverse
local x, y, z = (getN :: any)(v, 0) / sx, (getN :: any)(v, 1) / sy, (getN :: any)(v, 2) / sz
local len = math.sqrt(x * x + y * y + z * z)
if len > 0 then
x, y, z = x / len, y / len, z / len
else
x, y, z = 0, 1, 0
end
normals[j + 1], normals[j + 2], normals[j + 3] = x, y, z
end
if uvs then
local u, w = (getT :: any)(v, 0), (getT :: any)(v, 1)
uvs[(i - 1) * 2 + 1] = u
uvs[(i - 1) * 2 + 2] = if flip then 1 - w else w
end
if colors then
local k = (i - 1) * 4
colors[k + 1] = toSrgb((getC :: any)(v, 0))
colors[k + 2] = toSrgb((getC :: any)(v, 1))
colors[k + 3] = toSrgb((getC :: any)(v, 2))
colors[k + 4] = if alpha then math.clamp((getC :: any)(v, 3), 0, 1) else 1
end
end
return {
vertexCount = n,
triangleCount = page.count,
positions = positions,
normals = normals,
uvs = uvs,
colors = colors,
corners = page.corners,
centre = { ox * sx, oy * sy, oz * sz },
}, nil
end
-- toEditableMesh(prim, page?, opts?) -> EditableMesh, centre { x, y, z } | nil, Err. Without a page the whole
-- primitive must fit one (else Err.LIMIT: use meshPages). Positions are relative to the centre (place the MeshPart
-- there). Engine errors (no free EditableMesh, limits) come back as Err.IO; a mesh this call created is destroyed then.
function Host.toEditableMesh(prim: any, page: MeshPage?, opts: MeshOptions?): (any, any)
local o: MeshOptions = opts or {}
local pg = page
if not pg then
local pages, err = Host.meshPages(prim, o)
if not pages then
return nil, err
end
if #pages ~= 1 then
return nil, Err.new(Err.LIMIT, string.format("toEditableMesh: %d pages: split with meshPages", #pages))
end
pg = pages[1]
end
local data, err = Host.pageData(prim, pg :: MeshPage, o)
if not data then
return nil, err
end
local d = data :: PageData
local em = o.editableMesh
local own = em == nil
if own then
local AssetService = (game :: any):GetService("AssetService")
local ok, made = pcall(function()
return AssetService:CreateEditableMesh()
end)
if not ok or not made then
return nil, Err.new(Err.IO, "CreateEditableMesh failed (a client has 8 growable EditableMeshes): " .. tostring(made))
end
em = made
end
local ok, perr = pcall(function()
local P, N, U, C = d.positions, d.normals, d.uvs, d.colors
local vids = table.create(d.vertexCount, 0)
local nids = if N then table.create(d.vertexCount, 0) else nil
local bids = if N and o.doubleSided then table.create(d.vertexCount, 0) else nil
local uids = if U then table.create(d.vertexCount, 0) else nil
local cids = if C then table.create(d.vertexCount, 0) else nil
local palette: { [string]: number } = {}
for i = 1, d.vertexCount do
local j = (i - 1) * 3
vids[i] = em:AddVertex(Vector3.new(P[j + 1], P[j + 2], P[j + 3]))
if N and nids then
local n = Vector3.new(N[j + 1], N[j + 2], N[j + 3])
nids[i] = em:AddNormal(n)
if bids then
bids[i] = em:AddNormal(-n)
end
end
if U and uids then
uids[i] = em:AddUV(Vector2.new(U[(i - 1) * 2 + 1], U[(i - 1) * 2 + 2]))
end
if C and cids then
local k = (i - 1) * 4
local r, g, b, a = C[k + 1], C[k + 2], C[k + 3], C[k + 4]
-- equal colours share one id (voxel palettes, flat-coloured models)
local key = string.format("%d,%d,%d,%d", math.floor(r * 255 + 0.5), math.floor(g * 255 + 0.5), math.floor(b * 255 + 0.5), math.floor(a * 255 + 0.5))
local id = palette[key]
if not id then
id = em:AddColor(Color3.new(r, g, b), a)
palette[key] = id
end
cids[i] = id
end
end
local corners = d.corners
local every = o.yieldEvery or 2000
local yield = o.yield
for t = 0, d.triangleCount - 1 do
local a = buffer.readu32(corners, t * 12) + 1
local b = buffer.readu32(corners, t * 12 + 4) + 1
local c = buffer.readu32(corners, t * 12 + 8) + 1
local f = em:AddTriangle(vids[a], vids[b], vids[c])
if nids then
em:SetFaceNormals(f, { nids[a], nids[b], nids[c] })
end
if uids then
em:SetFaceUVs(f, { uids[a], uids[b], uids[c] })
end
if cids then
em:SetFaceColors(f, { cids[a], cids[b], cids[c] })
end
if o.doubleSided then
local g = em:AddTriangle(vids[a], vids[c], vids[b])
if bids then
em:SetFaceNormals(g, { bids[a], bids[c], bids[b] })
end
if uids then
em:SetFaceUVs(g, { uids[a], uids[c], uids[b] })
end
if cids then
em:SetFaceColors(g, { cids[a], cids[c], cids[b] })
end
end
if yield and (t + 1) % every == 0 then
yield()
end
end
end)
if not ok then
if own then
pcall(function()
em:Destroy()
end)
end
return nil, Err.new(Err.IO, "EditableMesh: " .. tostring(perr))
end
return em, d.centre
end
return Host