rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,149 @@
|
||||
--!strict
|
||||
-- host/Love: LÖVE 11+. Files through love.filesystem (the game folder and the save directory), falling back to io for
|
||||
-- paths outside them; converts the standard types to LÖVE objects:
|
||||
-- host.toImageData(img) -> love ImageData (rgba8)
|
||||
-- host.fromImageData(imageData) -> Image (rgba8)
|
||||
-- host.toSoundData(audio) -> love SoundData (an Audio, or a Decoder decoded to the end; 16-bit)
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
local Host = {}
|
||||
Host.name = "love"
|
||||
Host.hasFiles = true
|
||||
|
||||
local function love(): any
|
||||
return (_G :: any).love
|
||||
end
|
||||
local function io(): any
|
||||
return (_G :: any).io
|
||||
end
|
||||
-- the FFI, when the build runs on LuaJIT (always, under LÖVE); called through _G so the build leaves it alone
|
||||
local function ffi(): any
|
||||
local req = (_G :: any).require
|
||||
local ok, m = pcall(req, "ffi")
|
||||
return if ok then m else nil
|
||||
end
|
||||
|
||||
function Host.readFile(path: string): (buffer?, Err.Err?)
|
||||
local fs = love().filesystem
|
||||
if fs.getInfo(path, "file") then
|
||||
local s, msg = fs.read(path)
|
||||
if type(s) ~= "string" then
|
||||
return nil, Err.new(Err.IO, "cannot read " .. path .. ": " .. tostring(msg))
|
||||
end
|
||||
return buffer.fromstring(s), nil
|
||||
end
|
||||
local f, msg = io().open(path, "rb")
|
||||
if not f then
|
||||
return nil, Err.new(Err.IO, "cannot open " .. path .. ": " .. tostring(msg))
|
||||
end
|
||||
local s = f:read("*a")
|
||||
f:close()
|
||||
return buffer.fromstring(s), nil
|
||||
end
|
||||
|
||||
-- writeFile: relative paths go to the save directory (love.filesystem.write), absolute ones through io
|
||||
function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?)
|
||||
local s = buffer.tostring(data)
|
||||
if string.sub(path, 1, 1) ~= "/" and not string.find(path, "^%a:[/\\]") then
|
||||
local ok, msg = love().filesystem.write(path, s)
|
||||
if not ok then
|
||||
return false, Err.new(Err.IO, "cannot write " .. path .. ": " .. tostring(msg))
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
local f, msg = io().open(path, "wb")
|
||||
if not f then
|
||||
return false, Err.new(Err.IO, "cannot create " .. path .. ": " .. tostring(msg))
|
||||
end
|
||||
f:write(s)
|
||||
f:close()
|
||||
return true, nil
|
||||
end
|
||||
|
||||
function Host.exists(path: string): boolean
|
||||
if love().filesystem.getInfo(path, "file") then
|
||||
return true
|
||||
end
|
||||
local f = io().open(path, "rb")
|
||||
if f then
|
||||
f:close()
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- toImageData(img) -> ImageData | nil, Err
|
||||
function Host.toImageData(img: any): (any, Err.Err?)
|
||||
if img.format ~= "rgba8" then
|
||||
return nil, Err.new(Err.ARG, "toImageData: needs rgba8 pixels, got " .. tostring(img.format))
|
||||
end
|
||||
local n = img.width * img.height * 4
|
||||
local F: any = ffi()
|
||||
local B: { toffi: ((buffer) -> any)? } = buffer :: any
|
||||
if F and B.toffi then
|
||||
local id = love().image.newImageData(img.width, img.height, "rgba8")
|
||||
F.copy(id:getFFIPointer(), B.toffi(img.pixels), n)
|
||||
return id
|
||||
end
|
||||
local bytes = love().data.newByteData(buffer.readstring(img.pixels, 0, n))
|
||||
return love().image.newImageData(img.width, img.height, "rgba8", bytes)
|
||||
end
|
||||
|
||||
-- fromImageData(imageData) -> Image (rgba8 copy)
|
||||
function Host.fromImageData(id: any): Types.Image
|
||||
local w, h = id:getDimensions()
|
||||
local img = Types.Image.new(w, h, "rgba8")
|
||||
local F: any = ffi()
|
||||
local B: { toffi: ((buffer) -> any)? } = buffer :: any
|
||||
if F and B.toffi and id:getFormat() == "rgba8" then
|
||||
F.copy(B.toffi(img.pixels), id:getFFIPointer(), w * h * 4)
|
||||
return img
|
||||
end
|
||||
local s = id:getString()
|
||||
buffer.writestring(img.pixels, 0, s, math.min(#s, w * h * 4))
|
||||
return img
|
||||
end
|
||||
|
||||
-- toSoundData(audio) -> SoundData (16-bit). audio: an Audio, or a Decoder (decoded to the end).
|
||||
function Host.toSoundData(a: any): (any, Err.Err?)
|
||||
local samples, frames, channels, rate = a.samples, a.frames, a.channels, a.sampleRate
|
||||
if samples == nil then
|
||||
-- a Decoder: read everything
|
||||
local parts, total = {}, 0
|
||||
while true do
|
||||
local out, n = a:read(65536)
|
||||
if n == 0 then
|
||||
break
|
||||
end
|
||||
local copy = buffer.create(n * channels * 4)
|
||||
buffer.copy(copy, 0, out, 0, n * channels * 4)
|
||||
table.insert(parts, copy)
|
||||
total += n
|
||||
end
|
||||
samples = buffer.create(total * channels * 4)
|
||||
local o = 0
|
||||
for _, p in parts do
|
||||
buffer.copy(samples, o, p)
|
||||
o += buffer.len(p)
|
||||
end
|
||||
frames = total
|
||||
end
|
||||
local sd = love().sound.newSoundData(math.max(frames, 1), rate, 16, channels)
|
||||
local count = frames * channels
|
||||
local F: any = ffi()
|
||||
if F then
|
||||
local p = F.cast("int16_t*", sd:getFFIPointer())
|
||||
for i = 0, count - 1 do
|
||||
local v = math.clamp(buffer.readf32(samples, i * 4), -1, 1)
|
||||
p[i] = math.floor(v * 32767 + 0.5)
|
||||
end
|
||||
else
|
||||
for i = 0, count - 1 do
|
||||
sd:setSample(i, math.clamp(buffer.readf32(samples, i * 4), -1, 1))
|
||||
end
|
||||
end
|
||||
return sd
|
||||
end
|
||||
|
||||
return Host
|
||||
@@ -0,0 +1,48 @@
|
||||
--!strict
|
||||
-- host/LuaJIT: plain LuaJIT (or any Lua with io): files through io.open.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
|
||||
local Host = {}
|
||||
Host.name = "luajit"
|
||||
Host.hasFiles = true
|
||||
|
||||
local function io(): any
|
||||
return (_G :: any).io
|
||||
end
|
||||
|
||||
function Host.readFile(path: string): (buffer?, Err.Err?)
|
||||
local f, msg = io().open(path, "rb")
|
||||
if not f then
|
||||
return nil, Err.new(Err.IO, "cannot open " .. path .. ": " .. tostring(msg))
|
||||
end
|
||||
local s = f:read("*a")
|
||||
f:close()
|
||||
if type(s) ~= "string" then
|
||||
return nil, Err.new(Err.IO, "cannot read " .. path)
|
||||
end
|
||||
return buffer.fromstring(s), nil
|
||||
end
|
||||
|
||||
function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?)
|
||||
local f, msg = io().open(path, "wb")
|
||||
if not f then
|
||||
return false, Err.new(Err.IO, "cannot create " .. path .. ": " .. tostring(msg))
|
||||
end
|
||||
local ok, werr = f:write(buffer.tostring(data))
|
||||
f:close()
|
||||
if not ok then
|
||||
return false, Err.new(Err.IO, "cannot write " .. path .. ": " .. tostring(werr))
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
function Host.exists(path: string): boolean
|
||||
local f = io().open(path, "rb")
|
||||
if f then
|
||||
f:close()
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
return Host
|
||||
@@ -0,0 +1,21 @@
|
||||
--!strict
|
||||
-- host/Luau: the Luau CLI (and any embedding without io): no file system. Sources are buffers or strings.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
|
||||
local Host = {}
|
||||
Host.name = "luau"
|
||||
Host.hasFiles = false
|
||||
|
||||
function Host.readFile(path: string): (buffer?, Err.Err?)
|
||||
return nil, Err.new(Err.IO, "no file system on this host (luau): pass a buffer or a string, not the path " .. path)
|
||||
end
|
||||
|
||||
function Host.writeFile(path: string, _data: buffer): (boolean, Err.Err?)
|
||||
return false, Err.new(Err.IO, "no file system on this host (luau): cannot write " .. path)
|
||||
end
|
||||
|
||||
function Host.exists(_path: string): boolean
|
||||
return false
|
||||
end
|
||||
|
||||
return Host
|
||||
@@ -0,0 +1,389 @@
|
||||
--!strict
|
||||
-- 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("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
local Host = {}
|
||||
Host.name = "roblox"
|
||||
Host.hasFiles = false
|
||||
|
||||
function Host.readFile(path: string): (buffer?, Err.Err?)
|
||||
return nil, Err.new(Err.IO, "Roblox has no file system: pass a buffer or a string, not the path " .. path)
|
||||
end
|
||||
|
||||
function Host.writeFile(path: string, _data: buffer): (boolean, Err.Err?)
|
||||
return false, Err.new(Err.IO, "Roblox has no file system: cannot write " .. path .. " (use F.save(value) for a buffer)")
|
||||
end
|
||||
|
||||
function Host.exists(_path: string): boolean
|
||||
return false
|
||||
end
|
||||
|
||||
-- toEditableImage(img) -> EditableImage | nil, Err. img.format must be "rgba8".
|
||||
function Host.toEditableImage(img: any): (any, Err.Err?)
|
||||
if img.format ~= "rgba8" then
|
||||
return nil, Err.new(Err.ARG, "toEditableImage: needs rgba8 pixels, got " .. tostring(img.format))
|
||||
end
|
||||
local AssetService = (game :: any):GetService("AssetService")
|
||||
local size = Vector2.new(img.width, img.height)
|
||||
local ok, ei = pcall(function()
|
||||
return AssetService:CreateEditableImage({ Size = size })
|
||||
end)
|
||||
if not ok or not ei then
|
||||
return nil, Err.new(Err.IO, "CreateEditableImage failed: " .. tostring(ei))
|
||||
end
|
||||
ei:WritePixelsBuffer(Vector2.zero, size, img.pixels)
|
||||
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
|
||||
@@ -0,0 +1,50 @@
|
||||
--!strict
|
||||
-- host: the runtime adapter, detected once at load time. Roblox (no file system; toEditableImage), LÖVE
|
||||
-- (love.filesystem + io; toImageData / toSoundData), plain LuaJIT (io), the Luau CLI (no file system).
|
||||
--
|
||||
-- local host = require("@rosetta/host")
|
||||
-- print(host.name) -- "roblox" | "love" | "luajit" | "luau"
|
||||
-- local buf, err = host.readFile("a.png") -- every host has readFile / writeFile / exists (failing with Err.IO
|
||||
-- -- where there is no file system)
|
||||
local Err = require("@rosetta/core/Err")
|
||||
|
||||
export type Host = {
|
||||
name: string,
|
||||
hasFiles: boolean,
|
||||
readFile: (path: string) -> (buffer?, Err.Err?),
|
||||
writeFile: (path: string, data: buffer) -> (boolean, Err.Err?),
|
||||
exists: (path: string) -> boolean,
|
||||
[string]: any,
|
||||
}
|
||||
|
||||
local G = _G :: any
|
||||
local name
|
||||
if typeof(game) == "Instance" then
|
||||
name = "roblox"
|
||||
elseif G.love ~= nil and type(G.love) == "table" and G.love.filesystem ~= nil then
|
||||
name = "love"
|
||||
elseif G.jit ~= nil or G.io ~= nil then
|
||||
name = "luajit"
|
||||
else
|
||||
name = "luau"
|
||||
end
|
||||
|
||||
local host: Host
|
||||
if name == "roblox" then
|
||||
host = require("@rosetta/host/Roblox") :: any
|
||||
elseif name == "love" then
|
||||
host = require("@rosetta/host/Love") :: any
|
||||
elseif name == "luajit" then
|
||||
host = require("@rosetta/host/LuaJIT") :: any
|
||||
else
|
||||
host = require("@rosetta/host/Luau") :: any
|
||||
end
|
||||
-- LuaJIT (plain or LÖVE): the big decoders (Opus CELT, H.264, ...) outgrow the default 1000 traces / 8 MB of machine
|
||||
-- code and fall back to the interpreter (CELT runs about 2.5x faster with these limits). Raising the limits only
|
||||
-- allows more traces; set _G.ROSETTA_NO_JIT_TUNE = true before requiring the library to leave them alone.
|
||||
if G.jit ~= nil and type(G.jit.opt) == "table" and not G.ROSETTA_NO_JIT_TUNE then
|
||||
pcall(G.jit.opt.start, "maxtrace=8000", "maxmcode=65536")
|
||||
end
|
||||
|
||||
local _ = Err
|
||||
return host
|
||||
Reference in new issue
Block a user