Files
rosetta/src/Formats/model/robloxmesh/Parse.luau
T

441 lines
13 KiB
Luau

--!strict
--!native
--!optimize 2
-- model/robloxmesh/Parse: Roblox FileMesh bytes to a flat record (the file's arrays as packed buffers), for every version:
-- 1.00 / 1.01 (ASCII), 2.00, 3.00 / 3.01 (LOD offsets), 4.00 / 4.01 (bones, skinning, subsets), 5.00 (+ FACS) and the
-- chunked 6.00 / 7.00 (COREMESH v1, LODS, SKINNING, FACS, HSRAVIS; a Draco COREMESH v2 is "unsupported"). Written from
-- MaximumADHD's "Roblox Mesh Format" notes (devforum 326114). Little endian. Returns nil, message on bad data.
local Parse = {}
local readu8, readu16, readu32, readf32, readstring = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readf32, buffer.readstring
local writef32, writeu32 = buffer.writef32, buffer.writeu32
export type Bone = { name: string, parent: number?, lodParent: number, culling: number, cf: { number } } -- cf: r00..r22, x, y, z
export type Subset = { facesBegin: number, facesLength: number, vertsBegin: number, vertsLength: number, bones: { number } }
export type Record = {
version: string,
numVerts: number,
numFaces: number,
pos: buffer, -- f32 x3
nrm: buffer, -- f32 x3
uv: buffer, -- f32 x2
tan: buffer?, -- i8 x4 (nil in version 1)
col: buffer?, -- u8 x4 (nil when the vertices have none)
faces: buffer, -- u32 x3
lods: { number },
lodType: number,
highQualityLods: number,
bones: { Bone },
subsets: { Subset },
env: buffer?, -- per vertex: 4 subset-local bone indices, 4 weights (u8)
facsFormat: number,
facs: buffer?,
chunks: { { name: string, version: number, data: buffer } }, -- 6.00+ chunks kept raw (HSRAVIS, unknown)
}
export type Limits = { maxVertices: number, maxFaces: number }
local function record(version: string): Record
return {
version = version,
numVerts = 0,
numFaces = 0,
pos = buffer.create(0),
nrm = buffer.create(0),
uv = buffer.create(0),
tan = nil,
col = nil,
faces = buffer.create(0),
lods = {},
lodType = 0,
highQualityLods = 0,
bones = {},
subsets = {},
env = nil,
facsFormat = 0,
facs = nil,
chunks = {},
}
end
-- ------------------------------------------------------------------------------------------------ version 1
local function ascii(text: string, version: string, lim: Limits): (Record?, string?)
local nf = tonumber(string.match(text, "^[^\n]*\n%s*(%d+)"))
if not nf then
return nil, "corrupt: no face count"
end
if nf * 3 > lim.maxVertices or nf > lim.maxFaces then
return nil, "limit: " .. nf .. " faces"
end
local r = record(version)
local nv = nf * 3
r.numVerts, r.numFaces = nv, nf
r.pos, r.nrm, r.uv = buffer.create(nv * 12), buffer.create(nv * 12), buffer.create(nv * 8)
r.faces = buffer.create(nf * 12)
local scale = if version == "1.00" then 0.5 else 1
local i = 0
local vals = table.create(9, 0)
local k = 0
local start = string.find(text, "[", 1, true) or (#text + 1)
for a, b, c in string.gmatch(string.sub(text, start), "%[%s*([^,%]]+)%s*,%s*([^,%]]+)%s*,%s*([^,%]]+)%s*%]") do
local x, y, z = tonumber(a), tonumber(b), tonumber(c)
if not x or not y or not z then
return nil, "corrupt: bad number in a vector"
end
vals[k * 3 + 1], vals[k * 3 + 2], vals[k * 3 + 3] = x, y, z
k += 1
if k == 3 then
if i >= nv then
break
end
writef32(r.pos, i * 12, vals[1] * scale)
writef32(r.pos, i * 12 + 4, vals[2] * scale)
writef32(r.pos, i * 12 + 8, vals[3] * scale)
writef32(r.nrm, i * 12, vals[4])
writef32(r.nrm, i * 12 + 4, vals[5])
writef32(r.nrm, i * 12 + 8, vals[6])
writef32(r.uv, i * 8, vals[7])
writef32(r.uv, i * 8 + 4, 1 - vals[8]) -- version 1 stores V upside down
i += 1
k = 0
end
end
if i < nv then
return nil, "truncated: " .. i .. " of " .. nv .. " vertices"
end
for f = 0, nv - 1 do
writeu32(r.faces, f * 4, f)
end
r.lods = { 0, nf }
return r, nil
end
-- ------------------------------------------------------------------------------------------------ binary parts
-- vertices of size vs (36 / 40) at p
local function vertices(b: buffer, p: number, n: number, vs: number, r: Record)
local pos, nrm, uv, tan = buffer.create(n * 12), buffer.create(n * 12), buffer.create(n * 8), buffer.create(n * 4)
local col = if vs >= 40 then buffer.create(n * 4) else nil
for i = 0, n - 1 do
local q = p + i * vs
buffer.copy(pos, i * 12, b, q, 12)
buffer.copy(nrm, i * 12, b, q + 12, 12)
buffer.copy(uv, i * 8, b, q + 24, 8)
buffer.copy(tan, i * 4, b, q + 32, 4)
if col then
buffer.copy(col, i * 4, b, q + 36, 4)
end
end
r.pos, r.nrm, r.uv, r.tan, r.col = pos, nrm, uv, tan, col
r.numVerts = n
end
local function faces(b: buffer, p: number, n: number, fs: number, r: Record): string?
local out = buffer.create(n * 12)
local nv = r.numVerts
for i = 0, n - 1 do
for k = 0, 2 do
local v = readu32(b, p + i * fs + k * 4)
if v >= nv then
return "corrupt: face " .. i .. " uses vertex " .. v .. " of " .. nv
end
writeu32(out, i * 12 + k * 4, v)
end
end
r.faces, r.numFaces = out, n
return nil
end
local function bones(b: buffer, p: number, n: number, names: string, r: Record)
for i = 0, n - 1 do
local q = p + i * 60
local ni = readu32(b, q)
local name = string.match(string.sub(names, ni + 1), "^[^%z]*") or ""
local parent = readu16(b, q + 4)
local cf = table.create(12, 0)
for k = 0, 11 do
cf[k + 1] = readf32(b, q + 12 + 4 * k)
end
r.bones[i + 1] = { name = name, parent = if parent == 0xFFFF then nil else parent, lodParent = readu16(b, q + 6), culling = readf32(b, q + 8), cf = cf }
end
end
local function subsets(b: buffer, p: number, n: number, r: Record)
for i = 0, n - 1 do
local q = p + i * 72
local count = math.min(readu32(b, q + 16), 26)
local list = table.create(count, 0)
for k = 0, count - 1 do
list[k + 1] = readu16(b, q + 20 + 2 * k)
end
r.subsets[i + 1] = { facesBegin = readu32(b, q), facesLength = readu32(b, q + 4), vertsBegin = readu32(b, q + 8), vertsLength = readu32(b, q + 12), bones = list }
end
end
local function lods(b: buffer, p: number, n: number, r: Record)
local out = table.create(n, 0)
for i = 0, n - 1 do
out[i + 1] = readu32(b, p + 4 * i)
end
r.lods = out
end
-- ------------------------------------------------------------------------------------------------ versions 2 .. 5
local function classic(b: buffer, p: number, e: number, version: string, lim: Limits): (Record?, string?)
local r = record(version)
local major = tonumber(string.sub(version, 1, 1)) or 0
if p + 12 > e then
return nil, "truncated header"
end
local hs = readu16(b, p)
local nv: number, nf: number, vs: number, fs: number = 0, 0, 40, 12
local nlod: number, nbones: number, namesSize: number, nsub: number = 0, 0, 0, 0
if major == 2 or major == 3 then
vs = readu8(b, p + 2)
fs = readu8(b, p + 3)
if major == 2 then
nv = readu32(b, p + 4)
nf = readu32(b, p + 8)
else
if p + 16 > e then
return nil, "truncated header"
end
nlod = readu16(b, p + 6)
nv = readu32(b, p + 8)
nf = readu32(b, p + 12)
end
else
if p + 24 > e then
return nil, "truncated header"
end
r.lodType = readu16(b, p + 2)
nv = readu32(b, p + 4)
nf = readu32(b, p + 8)
nlod = readu16(b, p + 12)
nbones = readu16(b, p + 14)
namesSize = readu32(b, p + 16)
nsub = readu16(b, p + 20)
r.highQualityLods = readu8(b, p + 22)
if major >= 5 then
if p + 32 > e then
return nil, "truncated header"
end
r.facsFormat = readu32(b, p + 24)
end
end
if vs < 36 or fs < 12 then
return nil, "corrupt: vertex size " .. vs .. ", face size " .. fs
end
if nv > lim.maxVertices or nf > lim.maxFaces then
return nil, "limit: " .. nv .. " vertices, " .. nf .. " faces"
end
local q = p + hs
if q + nv * vs > e then
return nil, "truncated vertices"
end
vertices(b, q, nv, vs, r)
q += nv * vs
if nbones > 0 then
if q + nv * 8 > e then
return nil, "truncated skinning"
end
local env = buffer.create(nv * 8)
buffer.copy(env, 0, b, q, nv * 8)
r.env = env
q += nv * 8
end
if q + nf * fs > e then
return nil, "truncated faces"
end
local ferr = faces(b, q, nf, fs, r)
if ferr then
return nil, ferr
end
q += nf * fs
if q + nlod * 4 > e then
return nil, "truncated LOD offsets"
end
lods(b, q, nlod, r)
q += nlod * 4
if q + nbones * 60 + namesSize + nsub * 72 > e then
return nil, "truncated bones / subsets"
end
local names = readstring(b, q + nbones * 60, namesSize)
bones(b, q, nbones, names, r)
q += nbones * 60 + namesSize
subsets(b, q, nsub, r)
q += nsub * 72
if major >= 5 then
local size = readu32(b, p + 28)
if size > 0 then
if q + size > e then
return nil, "truncated FACS data"
end
local facs = buffer.create(size)
buffer.copy(facs, 0, b, q, size)
r.facs = facs
end
end
if #r.lods == 0 then
r.lods = { 0, nf }
end
return r, nil
end
-- ------------------------------------------------------------------------------------------------ versions 6 / 7
local function chunked(b: buffer, p: number, e: number, version: string, lim: Limits): (Record?, string?)
local r = record(version)
local core = false
while p < e do
if p + 16 > e then
return nil, "truncated chunk header"
end
local name = string.gsub(readstring(b, p, 8), "%z+$", "")
local cv, size = readu32(b, p + 8), readu32(b, p + 12)
local q = p + 16
local ce = q + size
if ce > e then
return nil, "truncated chunk " .. name
end
if name == "COREMESH" then
if cv == 2 then
return nil, "unsupported: Draco-compressed COREMESH (needs codecs/model/Draco)"
elseif cv ~= 1 then
return nil, "unsupported: COREMESH version " .. cv
end
if q + 4 > ce then
return nil, "truncated COREMESH"
end
local nv = readu32(b, q)
if nv > lim.maxVertices then
return nil, "limit: " .. nv .. " vertices"
end
if q + 4 + nv * 40 + 4 > ce then
return nil, "truncated COREMESH vertices"
end
vertices(b, q + 4, nv, 40, r)
local fq = q + 4 + nv * 40
local nf = readu32(b, fq)
if nf > lim.maxFaces then
return nil, "limit: " .. nf .. " faces"
end
if fq + 4 + nf * 12 > ce then
return nil, "truncated COREMESH faces"
end
local ferr = faces(b, fq + 4, nf, 12, r)
if ferr then
return nil, ferr
end
core = true
elseif name == "LODS" and cv == 1 then
if q + 7 > ce then
return nil, "truncated LODS"
end
r.lodType = readu16(b, q)
r.highQualityLods = readu8(b, q + 2)
local n = readu32(b, q + 3)
if q + 7 + n * 4 > ce then
return nil, "truncated LODS"
end
lods(b, q + 7, n, r)
elseif name == "SKINNING" and cv == 1 then
if not core then
return nil, "corrupt: SKINNING before COREMESH"
end
if q + 4 > ce then
return nil, "truncated SKINNING"
end
local ns = readu32(b, q)
local s = q + 4
if ns ~= r.numVerts or s + ns * 8 + 4 > ce then
return nil, "corrupt: SKINNING count"
end
local env = buffer.create(ns * 8)
buffer.copy(env, 0, b, s, ns * 8)
r.env = env
s += ns * 8
local nb = readu32(b, s)
s += 4
if s + nb * 60 + 4 > ce then
return nil, "truncated SKINNING bones"
end
local bq = s
s += nb * 60
local namesSize = readu32(b, s)
s += 4
if s + namesSize + 4 > ce then
return nil, "truncated SKINNING names"
end
bones(b, bq, nb, readstring(b, s, namesSize), r)
s += namesSize
local nsub = readu32(b, s)
s += 4
if s + nsub * 72 > ce then
return nil, "truncated SKINNING subsets"
end
subsets(b, s, nsub, r)
if nb == 0 then
r.env = nil
end
elseif name == "FACS" and cv == 1 then
if q + 4 > ce then
return nil, "truncated FACS"
end
local size = readu32(b, q)
if q + 4 + size > ce then
return nil, "truncated FACS"
end
local facs = buffer.create(size)
buffer.copy(facs, 0, b, q + 4, size)
r.facs, r.facsFormat = facs, 1
else
local data = buffer.create(size)
buffer.copy(data, 0, b, q, size)
table.insert(r.chunks, { name = name, version = cv, data = data })
end
p = ce
end
if not core then
return nil, "corrupt: no COREMESH chunk"
end
if #r.lods == 0 then
r.lods = { 0, r.numFaces }
end
return r, nil
end
-- parse(b, p, e, lim) -> Record | nil, message
function Parse.parse(b: buffer, p: number, e: number, lim: Limits): (Record?, string?)
if e - p < 13 then
return nil, "truncated: no version line"
end
local line = readstring(b, p, 12)
local version = string.match(line, "^version (%d%.%d%d)$")
if not version or readu8(b, p + 12) ~= 10 and readu8(b, p + 12) ~= 13 then
return nil, "corrupt: bad version line"
end
local q = p + 13
if readu8(b, p + 12) == 13 and q < e and readu8(b, q) == 10 then
q += 1
end
local major = string.sub(version, 1, 1)
if major == "1" then
return ascii(readstring(b, p, e - p), version, lim)
elseif major == "2" or major == "3" or major == "4" or major == "5" then
return classic(b, q, e, version, lim)
elseif major == "6" or major == "7" then
return chunked(b, q, e, version, lim)
end
return nil, "unsupported: version " .. version
end
-- tangent(tan, i) -> x, y, z, w of vertex i's packed tangent (signed bytes / 127)
local function snorm(v: number): number
local x = if v >= 128 then v - 256 else v
return math.clamp(x / 127, -1, 1)
end
function Parse.tangent(tan: buffer, i: number): (number, number, number, number)
return snorm(readu8(tan, i * 4)), snorm(readu8(tan, i * 4 + 1)), snorm(readu8(tan, i * 4 + 2)), snorm(readu8(tan, i * 4 + 3))
end
return Parse