merge ws/3d3-mesh
This commit is contained in:
commit
d2727aeab0
7 files changed
+291
-36
No files matched your search
@@ -980,7 +980,7 @@ local list: { Entry } = {
|
||||
path = "model/robloxmesh",
|
||||
extensions = { "mesh" },
|
||||
mime = { "application/x-roblox-mesh" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Mat4", "codecs/model/MeshFlatten" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Mat4", "codecs/model/MeshFlatten", "codecs/model/Draco" },
|
||||
caps = { decode = true, encode = true, skinning = true, lods = true, versions = { "1.00", "1.01", "2.00", "3.00", "3.01", "4.00", "4.01", "5.00", "6.00", "7.00" } },
|
||||
load = function()
|
||||
return require("@formats/model/robloxmesh")
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
--!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.
|
||||
-- chunked 6.00 / 7.00 (COREMESH v1, LODS, SKINNING, FACS, HSRAVIS; a Draco COREMESH v2 through the lim.core
|
||||
-- callback). 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
|
||||
@@ -31,9 +31,13 @@ export type Record = {
|
||||
facsFormat: number,
|
||||
facs: buffer?,
|
||||
chunks: { { name: string, version: number, data: buffer } }, -- 6.00+ chunks kept raw (HSRAVIS, unknown)
|
||||
coreVersion: number?, -- 6.00+: 2 when the COREMESH was Draco-compressed
|
||||
}
|
||||
|
||||
export type Limits = { maxVertices: number, maxFaces: number }
|
||||
-- core: decodes a Draco-compressed COREMESH (v2) body at (b, off, len) into the vertex / face fields of a Record
|
||||
-- (numVerts, pos, nrm, uv, tan?, col?, numFaces, faces); the format's init passes it (codecs/model/Draco)
|
||||
export type Core = { numVerts: number, pos: buffer, nrm: buffer, uv: buffer, tan: buffer?, col: buffer?, numFaces: number, faces: buffer }
|
||||
export type Limits = { maxVertices: number, maxFaces: number, core: ((b: buffer, off: number, len: number) -> (Core?, string?))? }
|
||||
|
||||
local function record(version: string): Record
|
||||
return {
|
||||
@@ -55,6 +59,7 @@ local function record(version: string): Record
|
||||
facsFormat = 0,
|
||||
facs = nil,
|
||||
chunks = {},
|
||||
coreVersion = nil,
|
||||
}
|
||||
end
|
||||
|
||||
@@ -298,34 +303,49 @@ local function chunked(b: buffer, p: number, e: number, version: string, lim: Li
|
||||
end
|
||||
if name == "COREMESH" then
|
||||
if cv == 2 then
|
||||
return nil, "unsupported: Draco-compressed COREMESH (needs codecs/model/Draco)"
|
||||
local decodeCore = lim.core
|
||||
if not decodeCore then
|
||||
return nil, "unsupported: Draco-compressed COREMESH"
|
||||
end
|
||||
local c, cerr = decodeCore(b, q, size)
|
||||
if not c then
|
||||
return nil, cerr or "corrupt: bad Draco COREMESH"
|
||||
end
|
||||
if c.numVerts > lim.maxVertices or c.numFaces > lim.maxFaces then
|
||||
return nil, "limit: " .. c.numVerts .. " vertices, " .. c.numFaces .. " faces"
|
||||
end
|
||||
r.numVerts, r.pos, r.nrm, r.uv, r.tan, r.col = c.numVerts, c.pos, c.nrm, c.uv, c.tan, c.col
|
||||
r.numFaces, r.faces = c.numFaces, c.faces
|
||||
core = true
|
||||
r.coreVersion = 2
|
||||
elseif cv ~= 1 then
|
||||
return nil, "unsupported: COREMESH version " .. cv
|
||||
else
|
||||
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
|
||||
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"
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
-- Format" notes (devforum.roblox.com/t/roblox-mesh-format/326114); no port.
|
||||
-- Decode: 1.00 / 1.01 (ASCII; 1.00 positions halved, V flipped), 2.00, 3.00 / 3.01 (LOD offsets), 4.00 / 4.01 (bones,
|
||||
-- skinning, subsets), 5.00 (+ FACS), 6.00 / 7.00 chunked (COREMESH v1, LODS, SKINNING, FACS; HSRAVIS and unknown
|
||||
-- chunks kept raw in meta.source.chunks). A Draco-compressed COREMESH (v2) is Err.UNSUPPORTED until codecs/model/Draco
|
||||
-- exists. One mesh, one triangle primitive (LOD 0): POSITION, NORMAL, TEXCOORD_0 (f32), TANGENT (f32 x4 from the signed
|
||||
-- chunks kept raw in meta.source.chunks). A Draco-compressed COREMESH (v2: a u32 size, then the Draco stream) decodes
|
||||
-- through codecs/model/Draco: its POSITION / NORMAL / TEX_COORD / COLOR attributes and a 4-component TANGENT or GENERIC
|
||||
-- one as the tangents (meta.source.draco = true). One mesh, one triangle primitive (LOD 0): POSITION, NORMAL, TEXCOORD_0 (f32), TANGENT (f32 x4 from the signed
|
||||
-- bytes), COLOR_0 (unorm8 x4, when the vertices carry colours), u32 indices; the other LODs as primitives in
|
||||
-- mesh.extras.lods (same attributes, their own indices). Bones become joint nodes (local matrices from the files'
|
||||
-- world-space CFrames; extras.culling / lodParent) under one skin with inverse bind matrices; skinning becomes
|
||||
@@ -23,6 +24,7 @@ local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
local Flatten = require("@formats/codecs/model/MeshFlatten")
|
||||
local Draco = require("@formats/codecs/model/Draco")
|
||||
local Parse = require("@formats/model/robloxmesh/Parse")
|
||||
local Write = require("@formats/model/robloxmesh/Write")
|
||||
|
||||
@@ -32,7 +34,7 @@ local RobloxMesh = {
|
||||
category = "model",
|
||||
extensions = { "mesh" },
|
||||
mime = { "application/x-roblox-mesh" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Mat4", "codecs/model/MeshFlatten" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Mat4", "codecs/model/MeshFlatten", "codecs/model/Draco" },
|
||||
caps = { decode = true, encode = true, skinning = true, lods = true, versions = { "1.00", "1.01", "2.00", "3.00", "3.01", "4.00", "4.01", "5.00", "6.00", "7.00" } },
|
||||
}
|
||||
|
||||
@@ -67,8 +69,111 @@ local function indices(r: Parse.Record, from: number, to: number): Types.Accesso
|
||||
return accessor(d, "u32", 1, n * 3)
|
||||
end
|
||||
|
||||
-- a Draco-compressed COREMESH body -> the record's vertex / face fields
|
||||
local function dracoCore(b: buffer, off: number, len: number): (Parse.Core?, string?)
|
||||
-- the stream follows a u32 byte count (accepted without it too)
|
||||
if len >= 4 and buffer.readu32(b, off) <= len - 4 and buffer.readu32(b, off) >= 5 and buffer.readstring(b, off + 4, 5) == "DRACO" then
|
||||
len = buffer.readu32(b, off)
|
||||
off += 4
|
||||
end
|
||||
local g, err = Draco.decode(b, off, len)
|
||||
if not g then
|
||||
return nil, (err :: Err.Err).message
|
||||
end
|
||||
if g.kind ~= "mesh" then
|
||||
return nil, "corrupt: Draco COREMESH holds a point cloud"
|
||||
end
|
||||
local n = g.numPoints
|
||||
local by: { [string]: Draco.Attribute } = {}
|
||||
for _, a in g.attributes do
|
||||
local key = if a.name == "GENERIC" and a.components == 4 then "TANGENT" else a.name
|
||||
if not by[key] then
|
||||
by[key] = a
|
||||
end
|
||||
end
|
||||
local pos = by.POSITION
|
||||
if not pos or pos.components < 3 then
|
||||
return nil, "corrupt: Draco COREMESH without positions"
|
||||
end
|
||||
-- an attribute's values as numbers (normalized integers as -1..1 / 0..1)
|
||||
local function getter(a: Draco.Attribute): (number, number) -> number
|
||||
local t = if a.normalized then (({ u8 = "unorm8", i8 = "snorm8", u16 = "unorm16", i16 = "snorm16" }) :: any)[a.type] or a.type else a.type
|
||||
local rd = Types.Scene.reader({ data = a.data, type = t, components = a.components, count = n })
|
||||
if not rd then
|
||||
return function(_i: number, _c: number): number
|
||||
return 0
|
||||
end
|
||||
end
|
||||
return rd
|
||||
end
|
||||
local function floats(a: Draco.Attribute?, comps: number): buffer
|
||||
local out = buffer.create(n * comps * 4)
|
||||
if a then
|
||||
local get = getter(a)
|
||||
local m = math.min(comps, a.components)
|
||||
for i = 0, n - 1 do
|
||||
for c = 0, m - 1 do
|
||||
writef32(out, (i * comps + c) * 4, get(i, c))
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
local tan: buffer? = nil
|
||||
if by.TANGENT then
|
||||
local get = getter(by.TANGENT)
|
||||
local t = buffer.create(n * 4)
|
||||
for i = 0, n - 1 do
|
||||
for c = 0, 3 do
|
||||
local v = if c < by.TANGENT.components then get(i, c) else 1
|
||||
buffer.writei8(t, i * 4 + c, math.clamp(math.floor(v * 127 + 0.5), -127, 127))
|
||||
end
|
||||
end
|
||||
tan = t
|
||||
end
|
||||
local col: buffer? = nil
|
||||
if by.COLOR then
|
||||
local a = by.COLOR
|
||||
local c = buffer.create(n * 4)
|
||||
if a.type == "u8" and a.components == 4 then
|
||||
buffer.copy(c, 0, a.data, 0, n * 4)
|
||||
else
|
||||
local get = getter(a)
|
||||
for i = 0, n - 1 do
|
||||
for k = 0, 3 do
|
||||
local v = if k < a.components then get(i, k) else 1
|
||||
if a.type ~= "f32" and not a.normalized then
|
||||
v /= 255
|
||||
end
|
||||
writeu8(c, i * 4 + k, math.clamp(math.floor(v * 255 + 0.5), 0, 255))
|
||||
end
|
||||
end
|
||||
end
|
||||
col = c
|
||||
end
|
||||
return {
|
||||
numVerts = n,
|
||||
pos = floats(pos, 3),
|
||||
nrm = floats(by.NORMAL, 3),
|
||||
uv = floats(by.TEX_COORD, 2),
|
||||
tan = tan,
|
||||
col = col,
|
||||
numFaces = g.numFaces,
|
||||
faces = g.indices :: buffer,
|
||||
}, nil
|
||||
end
|
||||
|
||||
-- Parse is native: nothing may throw through it, so the callback never does
|
||||
local function safeCore(b: buffer, off: number, len: number): (Parse.Core?, string?)
|
||||
local ok, c, err = pcall(dracoCore, b, off, len)
|
||||
if not ok then
|
||||
return nil, "corrupt: Draco COREMESH: " .. tostring(c)
|
||||
end
|
||||
return c, err
|
||||
end
|
||||
|
||||
function RobloxMesh.decode(rd: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local lim = { maxVertices = opts and opts.maxVertices or 2 ^ 24, maxFaces = opts and opts.maxFaces or 2 ^ 25 }
|
||||
local lim: Parse.Limits = { maxVertices = opts and opts.maxVertices or 2 ^ 24, maxFaces = opts and opts.maxFaces or 2 ^ 25, core = safeCore }
|
||||
local r, msg = Parse.parse(rd.buf, rd.base, rd.limit, lim)
|
||||
if not r then
|
||||
return nil, Err.from(msg, rd.base)
|
||||
@@ -188,6 +293,7 @@ function RobloxMesh.decode(rd: Bytes.Reader, opts: DecodeOptions?): (Types.Scene
|
||||
facsFormat = r.facsFormat,
|
||||
facs = r.facs,
|
||||
chunks = r.chunks,
|
||||
draco = if r.coreVersion == 2 then true else nil,
|
||||
}
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user