model/proc: Doom 3 / Quake 4 .proc render geometry (models / surfaces to meshes, clockwise-front turned CCW; portals, BSP nodes, shadow models as data)
This commit is contained in:
1 parent
7d4e0381fe
commit
719abb97c0
12 files changed
+2498
-1081
No files matched your search
@@ -44,7 +44,7 @@ print(F.detect(webp), #F.formats()) -- "webp", the format list
|
||||
## Formats
|
||||
|
||||
<!-- formats:begin -->
|
||||
175 formats. Generated from the format contracts by `tools/gen_registry.py`; [docs/FORMATS.md](docs/FORMATS.md)
|
||||
176 formats. Generated from the format contracts by `tools/gen_registry.py`; [docs/FORMATS.md](docs/FORMATS.md)
|
||||
adds each format's capabilities.
|
||||
|
||||
| Category | Format | Extensions | Read | Write |
|
||||
@@ -126,6 +126,7 @@ adds each format's capabilities.
|
||||
| 3D models | off | .off | yes | yes |
|
||||
| 3D models | ply | .ply | yes | yes |
|
||||
| 3D models | pmx | .pmx .pmd | yes | - |
|
||||
| 3D models | proc | .proc | yes | - |
|
||||
| 3D models | qb | .qb | yes | yes |
|
||||
| 3D models | rbxm | .rbxm .rbxl | yes | yes |
|
||||
| 3D models | rbxmx | .rbxmx .rbxlx | yes | yes |
|
||||
|
||||
@@ -1808,6 +1808,18 @@ local list: { Entry } = {
|
||||
return __include("model/pmx")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "proc",
|
||||
category = "model",
|
||||
path = "model/proc",
|
||||
extensions = { "proc" },
|
||||
mime = { "model/x-doom3-proc" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/Lexer" },
|
||||
caps = { decode = true, encode = false, portals = true },
|
||||
load = function()
|
||||
return __include("model/proc")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "qb",
|
||||
category = "model",
|
||||
@@ -163142,6 +163154,388 @@ end
|
||||
|
||||
return Read
|
||||
end
|
||||
__FILES["model/proc"] = function()
|
||||
|
||||
-- model/proc: Doom 3 / Quake 4 / Prey `.proc` render geometry (dmap output: "mapProcFile003", Quake 4 "PROC" "4"; the
|
||||
-- Doom 3 GPL source's RenderWorld_load.cpp read for the layout, no code taken), read only. Every model (areas
|
||||
-- "_areaN", inline entity models) is a mesh with a primitive per surface and a node; materials are URIs. Portals,
|
||||
-- the BSP nodes and shadow models load as data in meta.proc. Triangles are clockwise-front in id Tech 4
|
||||
-- (R_DeriveFacePlanes: normal = d2 x d1) and are turned counter-clockwise. Not supported: writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = __include("core/Err")
|
||||
local Bytes = __include("core/Bytes")
|
||||
local Sniff = __include("core/Sniff")
|
||||
local Types = __include("Types")
|
||||
local Lexer = __include("codecs/text/Lexer")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Proc = {
|
||||
name = "proc",
|
||||
category = "model",
|
||||
extensions = { "proc" },
|
||||
mime = { "model/x-doom3-proc" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/Lexer" },
|
||||
caps = { decode = true, encode = false, portals = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
type DecodeOptions = {
|
||||
maxVertices: number?, -- default 2^24 over the file
|
||||
shadows: boolean?, -- default true: keep shadow models in meta.proc.shadowModels
|
||||
}
|
||||
type Portal = { positive: number, negative: number, points: { number } } -- areas; points x, y, z, ...
|
||||
type BspNode = { plane: { number }, positive: number, negative: number } -- children: > 0 node (1-based), 0 solid, < 0 area -1 - n
|
||||
type ShadowModel = { name: string, vertices: { number }, indices: { number }, noCaps: number, noFrontCaps: number, planeBits: number }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local Scene = Types.Scene
|
||||
local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Proc.sniff(r: Bytes.Reader): number
|
||||
local head = Sniff.text(r, 64, true)
|
||||
if string.match(head, "^mapProcFile%d%d%d") then
|
||||
return 1
|
||||
end
|
||||
if string.match(head, '^"?PROC"?%s+"?%d') then
|
||||
return 0.9
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
type P = { L: Lexer.Lexer, err: string? }
|
||||
|
||||
local function fail(p: P, msg: string)
|
||||
if not p.err then
|
||||
p.err = (p.L.err or msg) .. " at line " .. p.L:line()
|
||||
end
|
||||
end
|
||||
|
||||
local function num(p: P): number
|
||||
local k, v = p.L:next()
|
||||
if k ~= "number" then
|
||||
fail(p, "expected a number")
|
||||
return 0
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
local function punct(p: P, c: string)
|
||||
local k, v = p.L:next()
|
||||
if k ~= "punct" or v ~= c then
|
||||
fail(p, "expected " .. c)
|
||||
end
|
||||
end
|
||||
|
||||
local function str(p: P): string
|
||||
local k, v = p.L:next()
|
||||
if k ~= "string" and k ~= "word" then
|
||||
fail(p, "expected a name")
|
||||
return ""
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- numbers up to the next punctuation (the counts after a model / surface name)
|
||||
local function counts(p: P): { number }
|
||||
local out = {}
|
||||
while not p.err do
|
||||
local k, v = p.L:peek()
|
||||
if k ~= "number" then
|
||||
break
|
||||
end
|
||||
p.L:next()
|
||||
table.insert(out, v)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function skipBlock(p: P)
|
||||
local depth = 1
|
||||
while not p.err do
|
||||
local k, v = p.L:next()
|
||||
if not k then
|
||||
fail(p, "unterminated block")
|
||||
return
|
||||
end
|
||||
if k == "punct" then
|
||||
if v == "{" then
|
||||
depth += 1
|
||||
elseif v == "}" then
|
||||
depth -= 1
|
||||
if depth == 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
type Surface = { material: string, verts: { number }, arity: number, indices: { number } }
|
||||
|
||||
local function surface(p: P, budget: { number }): Surface?
|
||||
local material = str(p)
|
||||
local c = counts(p)
|
||||
if p.err then
|
||||
return nil
|
||||
end
|
||||
if #c < 2 then
|
||||
fail(p, "surface without vertex / index counts")
|
||||
return nil
|
||||
end
|
||||
local nv, ni = c[#c - 1], c[#c]
|
||||
if nv < 0 or ni < 0 or nv % 1 ~= 0 or ni % 1 ~= 0 or ni % 3 ~= 0 then
|
||||
fail(p, "bad surface counts")
|
||||
return nil
|
||||
end
|
||||
budget[1] -= nv
|
||||
if budget[1] < 0 then
|
||||
p.err = "limit: too many vertices"
|
||||
return nil
|
||||
end
|
||||
local verts: { number } = {}
|
||||
local arity = 0
|
||||
for i = 1, nv do
|
||||
punct(p, "(")
|
||||
local n = 0
|
||||
while not p.err do
|
||||
local k, v = p.L:next()
|
||||
if k == "number" then
|
||||
n += 1
|
||||
table.insert(verts, v)
|
||||
elseif k == "punct" and v == ")" then
|
||||
break
|
||||
else
|
||||
fail(p, "bad vertex")
|
||||
end
|
||||
end
|
||||
if p.err then
|
||||
return nil
|
||||
end
|
||||
if i == 1 then
|
||||
arity = n
|
||||
if n < 8 then
|
||||
fail(p, "a vertex needs x y z s t nx ny nz")
|
||||
return nil
|
||||
end
|
||||
elseif n ~= arity then
|
||||
fail(p, "vertices of different sizes")
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local idx: { number } = table.create(ni, 0)
|
||||
for i = 1, ni do
|
||||
local v = num(p)
|
||||
if v < 0 or v >= nv or v % 1 ~= 0 then
|
||||
fail(p, "index out of range")
|
||||
return nil
|
||||
end
|
||||
idx[i] = v
|
||||
end
|
||||
punct(p, "}")
|
||||
return { material = material, verts = verts, arity = arity, indices = idx }
|
||||
end
|
||||
|
||||
local function primitive(scene: Types.Scene, s: Surface, matIndex: { [string]: number }): Types.Primitive
|
||||
local a = s.arity
|
||||
local n = #s.verts // a
|
||||
local pb, nb, ub = buffer.create(n * 12), buffer.create(n * 12), buffer.create(n * 8)
|
||||
local cb: buffer? = if a >= 12 then buffer.create(n * 16) else nil
|
||||
local V = s.verts
|
||||
for i = 0, n - 1 do
|
||||
local o = i * a
|
||||
writef32(pb, i * 12, V[o + 1])
|
||||
writef32(pb, i * 12 + 4, V[o + 2])
|
||||
writef32(pb, i * 12 + 8, V[o + 3])
|
||||
writef32(ub, i * 8, V[o + 4])
|
||||
writef32(ub, i * 8 + 4, V[o + 5])
|
||||
writef32(nb, i * 12, V[o + 6])
|
||||
writef32(nb, i * 12 + 4, V[o + 7])
|
||||
writef32(nb, i * 12 + 8, V[o + 8])
|
||||
if cb then
|
||||
for k = 0, 3 do
|
||||
local c = V[o + 9 + k]
|
||||
writef32(cb, i * 16 + k * 4, if c > 1 then c / 255 else c)
|
||||
end
|
||||
end
|
||||
end
|
||||
local ni = #s.indices
|
||||
local ib = buffer.create(ni * 4)
|
||||
for t = 0, ni - 1, 3 do
|
||||
writeu32(ib, t * 4, s.indices[t + 1])
|
||||
writeu32(ib, t * 4 + 4, s.indices[t + 3])
|
||||
writeu32(ib, t * 4 + 8, s.indices[t + 2])
|
||||
end
|
||||
local mi = matIndex[s.material]
|
||||
if not mi then
|
||||
table.insert(scene.textures, { name = s.material, uri = s.material })
|
||||
local mat = Scene.material(s.material)
|
||||
mat.metallic = 0
|
||||
mat.baseColorTexture = { texture = #scene.textures }
|
||||
table.insert(scene.materials, mat)
|
||||
mi = #scene.materials
|
||||
matIndex[s.material] = mi
|
||||
end
|
||||
local attrs: { [string]: Types.Accessor } = {
|
||||
POSITION = Scene.accessor(pb, "f32", 3, n),
|
||||
NORMAL = Scene.accessor(nb, "f32", 3, n),
|
||||
TEXCOORD_0 = Scene.accessor(ub, "f32", 2, n),
|
||||
}
|
||||
if cb then
|
||||
attrs.COLOR_0 = Scene.accessor(cb, "f32", 4, n)
|
||||
end
|
||||
return { attributes = attrs, indices = Scene.accessor(ib, "u32", 1, ni), mode = "triangles" :: any, material = mi, extras = { proc = { material = s.material } } }
|
||||
end
|
||||
|
||||
function Proc.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local text = Sniff.dropBom(r:string(r:remaining()))
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
local p: P = { L = Lexer.new(text), err = nil }
|
||||
local k, head = p.L:next()
|
||||
local version: string
|
||||
if (k == "word" or k == "string") and string.match(head, "^mapProcFile%d+$") then
|
||||
version = head
|
||||
elseif (k == "word" or k == "string") and head == "PROC" then
|
||||
local vk, vv = p.L:next()
|
||||
if vk ~= "number" and vk ~= "string" then
|
||||
return nil, Err.new(Err.CORRUPT, "PROC without a version")
|
||||
end
|
||||
version = "PROC " .. tostring(vv)
|
||||
else
|
||||
return nil, Err.new(Err.CORRUPT, "not a .proc file")
|
||||
end
|
||||
local scene = Scene.new()
|
||||
scene.meta.up = "z"
|
||||
local world: Types.Node = { name = "proc", children = {} }
|
||||
table.insert(scene.nodes, world)
|
||||
scene.roots[1] = 1
|
||||
local info = { version = version, areas = 0, models = {}, portals = {} :: { Portal }, nodes = {} :: { BspNode }, shadowModels = {} :: { ShadowModel } }
|
||||
local matIndex: { [string]: number } = {}
|
||||
local budget = { opts and opts.maxVertices or 2 ^ 24 }
|
||||
local keepShadows = not (opts and opts.shadows == false)
|
||||
while not p.err do
|
||||
local tk, tv = p.L:next()
|
||||
if not tk then
|
||||
if p.L.err then
|
||||
fail(p, p.L.err)
|
||||
end
|
||||
break
|
||||
end
|
||||
if tk ~= "word" then
|
||||
fail(p, "expected a section name")
|
||||
break
|
||||
end
|
||||
punct(p, "{")
|
||||
local section = string.lower(tv)
|
||||
if section == "model" then
|
||||
local name = str(p)
|
||||
local c = counts(p)
|
||||
local ns = c[1] or 0
|
||||
local mesh: Types.Mesh = { name = name, primitives = {} }
|
||||
for _ = 1, ns do
|
||||
punct(p, "{")
|
||||
local s = surface(p, budget)
|
||||
if not s then
|
||||
break
|
||||
end
|
||||
table.insert(mesh.primitives, primitive(scene, s, matIndex))
|
||||
end
|
||||
punct(p, "}")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
table.insert(scene.meshes, mesh)
|
||||
table.insert(scene.nodes, { name = name, children = {}, mesh = #scene.meshes })
|
||||
table.insert(world.children, #scene.nodes)
|
||||
table.insert(info.models, { name = name, surfaces = #mesh.primitives, area = tonumber(string.match(name, "^_area(%d+)$")) })
|
||||
elseif section == "interareaportals" then
|
||||
info.areas = num(p)
|
||||
local n = num(p)
|
||||
for _ = 1, n do
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
local np = num(p)
|
||||
local pos, neg = num(p), num(p)
|
||||
local pts = {}
|
||||
for _ = 1, np do
|
||||
punct(p, "(")
|
||||
table.insert(pts, num(p))
|
||||
table.insert(pts, num(p))
|
||||
table.insert(pts, num(p))
|
||||
punct(p, ")")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
table.insert(info.portals, { positive = pos, negative = neg, points = pts })
|
||||
end
|
||||
punct(p, "}")
|
||||
elseif section == "nodes" then
|
||||
local n = num(p)
|
||||
for _ = 1, n do
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
punct(p, "(")
|
||||
local pl = { num(p), num(p), num(p), num(p) }
|
||||
punct(p, ")")
|
||||
local pos, neg = num(p), num(p)
|
||||
table.insert(info.nodes, { plane = pl, positive = if pos > 0 then pos + 1 else pos, negative = if neg > 0 then neg + 1 else neg })
|
||||
end
|
||||
punct(p, "}")
|
||||
elseif section == "shadowmodel" then
|
||||
local name = str(p)
|
||||
local c = counts(p)
|
||||
if #c < 5 then
|
||||
fail(p, "shadow model without counts")
|
||||
break
|
||||
end
|
||||
local nv, ni = c[1], c[4]
|
||||
budget[1] -= nv
|
||||
if nv < 0 or ni < 0 or budget[1] < 0 then
|
||||
p.err = if budget[1] < 0 then "limit: too many vertices" else "bad shadow model counts"
|
||||
break
|
||||
end
|
||||
local verts, idx = {}, {}
|
||||
for _ = 1, nv do
|
||||
punct(p, "(")
|
||||
table.insert(verts, num(p))
|
||||
table.insert(verts, num(p))
|
||||
table.insert(verts, num(p))
|
||||
punct(p, ")")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
for _ = 1, ni do
|
||||
table.insert(idx, num(p))
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
punct(p, "}")
|
||||
if keepShadows then
|
||||
table.insert(info.shadowModels, { name = name, vertices = verts, indices = idx, noCaps = c[2], noFrontCaps = c[3], planeBits = c[5] })
|
||||
end
|
||||
else
|
||||
skipBlock(p)
|
||||
end
|
||||
end
|
||||
if p.err then
|
||||
return nil, Err.from(p.err)
|
||||
end
|
||||
scene.meta.proc = info
|
||||
scene.meta.source = { format = "proc", version = version, models = #info.models, portals = #info.portals, nodes = #info.nodes, areas = info.areas }
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Proc
|
||||
end
|
||||
__FILES["model/qb"] = function()
|
||||
|
||||
-- model/qb: Qubicle Binary (.qb 1.1.0.0, minddesk.com "Qubicle Binary"), read + write. Supports: RGBA / BGRA, left- or
|
||||
|
||||
+539
-538
File diff suppressed because it is too large.
Load diff
@@ -2441,6 +2441,18 @@ local list= {
|
||||
return __include("model/pmx")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "proc",
|
||||
category = "model",
|
||||
path = "model/proc",
|
||||
extensions = { "proc" },
|
||||
mime = { "model/x-doom3-proc" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/Lexer" },
|
||||
caps = { decode = true, encode = false, portals = true },
|
||||
load = function()
|
||||
return __include("model/proc")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "qb",
|
||||
category = "model",
|
||||
@@ -163775,6 +163787,388 @@ end
|
||||
|
||||
return Read
|
||||
end
|
||||
__FILES["model/proc"] = function()
|
||||
local __compat = __compat local buffer = __compat.buffer --!strict
|
||||
-- model/proc: Doom 3 / Quake 4 / Prey `.proc` render geometry (dmap output: "mapProcFile003", Quake 4 "PROC" "4"; the
|
||||
-- Doom 3 GPL source's RenderWorld_load.cpp read for the layout, no code taken), read only. Every model (areas
|
||||
-- "_areaN", inline entity models) is a mesh with a primitive per surface and a node; materials are URIs. Portals,
|
||||
-- the BSP nodes and shadow models load as data in meta.proc. Triangles are clockwise-front in id Tech 4
|
||||
-- (R_DeriveFacePlanes: normal = d2 x d1) and are turned counter-clockwise. Not supported: writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = __include("core/Err")
|
||||
local Bytes = __include("core/Bytes")
|
||||
local Sniff = __include("core/Sniff")
|
||||
local Types = __include("Types")
|
||||
local Lexer = __include("codecs/text/Lexer")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Proc = {
|
||||
name = "proc",
|
||||
category = "model",
|
||||
extensions = { "proc" },
|
||||
mime = { "model/x-doom3-proc" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/Lexer" },
|
||||
caps = { decode = true, encode = false, portals = true },
|
||||
}
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local Scene = Types.Scene
|
||||
local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Proc.sniff(r)
|
||||
local head = Sniff.text(r, 64, true)
|
||||
if string.match(head, "^mapProcFile%d%d%d") then
|
||||
return 1
|
||||
end
|
||||
if string.match(head, '^"?PROC"?%s+"?%d') then
|
||||
return 0.9
|
||||
end
|
||||
return 0
|
||||
end
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
|
||||
|
||||
|
||||
local function fail(p, msg)
|
||||
if not p.err then
|
||||
p.err = (p.L.err or msg) .. " at line " .. p.L:line()
|
||||
end
|
||||
end
|
||||
|
||||
local function num(p)
|
||||
local k, v = p.L:next()
|
||||
if k ~= "number" then
|
||||
fail(p, "expected a number")
|
||||
return 0
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
local function punct(p, c)
|
||||
local k, v = p.L:next()
|
||||
if k ~= "punct" or v ~= c then
|
||||
fail(p, "expected " .. c)
|
||||
end
|
||||
end
|
||||
|
||||
local function str(p)
|
||||
local k, v = p.L:next()
|
||||
if k ~= "string" and k ~= "word" then
|
||||
fail(p, "expected a name")
|
||||
return ""
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- numbers up to the next punctuation (the counts after a model / surface name)
|
||||
local function counts(p)
|
||||
local out = {}
|
||||
while not p.err do
|
||||
local k, v = p.L:peek()
|
||||
if k ~= "number" then
|
||||
break
|
||||
end
|
||||
p.L:next()
|
||||
table.insert(out, v)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function skipBlock(p)
|
||||
local depth = 1
|
||||
while not p.err do
|
||||
local k, v = p.L:next()
|
||||
if not k then
|
||||
fail(p, "unterminated block")
|
||||
return
|
||||
end
|
||||
if k == "punct" then
|
||||
if v == "{" then
|
||||
depth =depth+ 1
|
||||
elseif v == "}" then
|
||||
depth =depth- 1
|
||||
if depth == 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function surface(p, budget)
|
||||
local material = str(p)
|
||||
local c = counts(p)
|
||||
if p.err then
|
||||
return nil
|
||||
end
|
||||
if #c < 2 then
|
||||
fail(p, "surface without vertex / index counts")
|
||||
return nil
|
||||
end
|
||||
local nv, ni = c[#c - 1], c[#c]
|
||||
if nv < 0 or ni < 0 or nv % 1 ~= 0 or ni % 1 ~= 0 or ni % 3 ~= 0 then
|
||||
fail(p, "bad surface counts")
|
||||
return nil
|
||||
end
|
||||
budget[1]=budget[1]- nv
|
||||
if budget[1] < 0 then
|
||||
p.err = "limit: too many vertices"
|
||||
return nil
|
||||
end
|
||||
local verts= {}
|
||||
local arity = 0
|
||||
for i = 1, nv do
|
||||
punct(p, "(")
|
||||
local n = 0
|
||||
while not p.err do
|
||||
local k, v = p.L:next()
|
||||
if k == "number" then
|
||||
n =n+ 1
|
||||
table.insert(verts, v)
|
||||
elseif k == "punct" and v == ")" then
|
||||
break
|
||||
else
|
||||
fail(p, "bad vertex")
|
||||
end
|
||||
end
|
||||
if p.err then
|
||||
return nil
|
||||
end
|
||||
if i == 1 then
|
||||
arity = n
|
||||
if n < 8 then
|
||||
fail(p, "a vertex needs x y z s t nx ny nz")
|
||||
return nil
|
||||
end
|
||||
elseif n ~= arity then
|
||||
fail(p, "vertices of different sizes")
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local idx= table.create(ni, 0)
|
||||
for i = 1, ni do
|
||||
local v = num(p)
|
||||
if v < 0 or v >= nv or v % 1 ~= 0 then
|
||||
fail(p, "index out of range")
|
||||
return nil
|
||||
end
|
||||
idx[i] = v
|
||||
end
|
||||
punct(p, "}")
|
||||
return { material = material, verts = verts, arity = arity, indices = idx }
|
||||
end
|
||||
|
||||
local function primitive(scene, s, matIndex)
|
||||
local a = s.arity
|
||||
local n = math.floor(#s.verts / a
|
||||
) local pb, nb, ub = buffer.create(n * 12), buffer.create(n * 12), buffer.create(n * 8)
|
||||
local cb= (a >= 12 and{(buffer.create(n * 16) )}or{nil
|
||||
})[1] local V = s.verts
|
||||
for i = 0, n - 1 do
|
||||
local o = i * a
|
||||
writef32(pb, i * 12, V[o + 1])
|
||||
writef32(pb, i * 12 + 4, V[o + 2])
|
||||
writef32(pb, i * 12 + 8, V[o + 3])
|
||||
writef32(ub, i * 8, V[o + 4])
|
||||
writef32(ub, i * 8 + 4, V[o + 5])
|
||||
writef32(nb, i * 12, V[o + 6])
|
||||
writef32(nb, i * 12 + 4, V[o + 7])
|
||||
writef32(nb, i * 12 + 8, V[o + 8])
|
||||
if cb then
|
||||
for k = 0, 3 do
|
||||
local c = V[o + 9 + k]
|
||||
writef32(cb, i * 16 + k * 4, (c > 1 and((c / 255 ))or(c)))
|
||||
end
|
||||
end
|
||||
end
|
||||
local ni = #s.indices
|
||||
local ib = buffer.create(ni * 4)
|
||||
for t = 0, ni - 1, 3 do
|
||||
writeu32(ib, t * 4, s.indices[t + 1])
|
||||
writeu32(ib, t * 4 + 4, s.indices[t + 3])
|
||||
writeu32(ib, t * 4 + 8, s.indices[t + 2])
|
||||
end
|
||||
local mi = matIndex[s.material]
|
||||
if not mi then
|
||||
table.insert(scene.textures, { name = s.material, uri = s.material })
|
||||
local mat = Scene.material(s.material)
|
||||
mat.metallic = 0
|
||||
mat.baseColorTexture = { texture = #scene.textures }
|
||||
table.insert(scene.materials, mat)
|
||||
mi = #scene.materials
|
||||
matIndex[s.material] = mi
|
||||
end
|
||||
local attrs= {
|
||||
POSITION = Scene.accessor(pb, "f32", 3, n),
|
||||
NORMAL = Scene.accessor(nb, "f32", 3, n),
|
||||
TEXCOORD_0 = Scene.accessor(ub, "f32", 2, n),
|
||||
}
|
||||
if cb then
|
||||
attrs.COLOR_0 = Scene.accessor(cb, "f32", 4, n)
|
||||
end
|
||||
return { attributes = attrs, indices = Scene.accessor(ib, "u32", 1, ni), mode = "triangles" , material = mi, extras = { proc = { material = s.material } } }
|
||||
end
|
||||
|
||||
function Proc.decode(r, opts)
|
||||
local text = Sniff.dropBom(r:string(r:remaining()))
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
local p= { L = Lexer.new(text), err = nil }
|
||||
local k, head = p.L:next()
|
||||
local version
|
||||
if (k == "word" or k == "string") and string.match(head, "^mapProcFile%d+$") then
|
||||
version = head
|
||||
elseif (k == "word" or k == "string") and head == "PROC" then
|
||||
local vk, vv = p.L:next()
|
||||
if vk ~= "number" and vk ~= "string" then
|
||||
return nil, Err.new(Err.CORRUPT, "PROC without a version")
|
||||
end
|
||||
version = "PROC " .. tostring(vv)
|
||||
else
|
||||
return nil, Err.new(Err.CORRUPT, "not a .proc file")
|
||||
end
|
||||
local scene = Scene.new()
|
||||
scene.meta.up = "z"
|
||||
local world= { name = "proc", children = {} }
|
||||
table.insert(scene.nodes, world)
|
||||
scene.roots[1] = 1
|
||||
local info = { version = version, areas = 0, models = {}, portals = {} , nodes = {} , shadowModels = {} }
|
||||
local matIndex= {}
|
||||
local budget = { opts and opts.maxVertices or 2 ^ 24 }
|
||||
local keepShadows = not (opts and opts.shadows == false)
|
||||
while not p.err do
|
||||
local tk, tv = p.L:next()
|
||||
if not tk then
|
||||
if p.L.err then
|
||||
fail(p, p.L.err)
|
||||
end
|
||||
break
|
||||
end
|
||||
if tk ~= "word" then
|
||||
fail(p, "expected a section name")
|
||||
break
|
||||
end
|
||||
punct(p, "{")
|
||||
local section = string.lower(tv)
|
||||
if section == "model" then
|
||||
local name = str(p)
|
||||
local c = counts(p)
|
||||
local ns = c[1] or 0
|
||||
local mesh= { name = name, primitives = {} }
|
||||
for _ = 1, ns do
|
||||
punct(p, "{")
|
||||
local s = surface(p, budget)
|
||||
if not s then
|
||||
break
|
||||
end
|
||||
table.insert(mesh.primitives, primitive(scene, s, matIndex))
|
||||
end
|
||||
punct(p, "}")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
table.insert(scene.meshes, mesh)
|
||||
table.insert(scene.nodes, { name = name, children = {}, mesh = #scene.meshes })
|
||||
table.insert(world.children, #scene.nodes)
|
||||
table.insert(info.models, { name = name, surfaces = #mesh.primitives, area = tonumber(string.match(name, "^_area(%d+)$")) })
|
||||
elseif section == "interareaportals" then
|
||||
info.areas = num(p)
|
||||
local n = num(p)
|
||||
for _ = 1, n do
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
local np = num(p)
|
||||
local pos, neg = num(p), num(p)
|
||||
local pts = {}
|
||||
for _ = 1, np do
|
||||
punct(p, "(")
|
||||
table.insert(pts, num(p))
|
||||
table.insert(pts, num(p))
|
||||
table.insert(pts, num(p))
|
||||
punct(p, ")")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
table.insert(info.portals, { positive = pos, negative = neg, points = pts })
|
||||
end
|
||||
punct(p, "}")
|
||||
elseif section == "nodes" then
|
||||
local n = num(p)
|
||||
for _ = 1, n do
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
punct(p, "(")
|
||||
local pl = { num(p), num(p), num(p), num(p) }
|
||||
punct(p, ")")
|
||||
local pos, neg = num(p), num(p)
|
||||
table.insert(info.nodes, { plane = pl, positive = (pos > 0 and((pos + 1 ))or(pos)), negative = (neg > 0 and((neg + 1 ))or(neg ))})
|
||||
end
|
||||
punct(p, "}")
|
||||
elseif section == "shadowmodel" then
|
||||
local name = str(p)
|
||||
local c = counts(p)
|
||||
if #c < 5 then
|
||||
fail(p, "shadow model without counts")
|
||||
break
|
||||
end
|
||||
local nv, ni = c[1], c[4]
|
||||
budget[1]=budget[1]- nv
|
||||
if nv < 0 or ni < 0 or budget[1] < 0 then
|
||||
p.err = budget[1] < 0 and"limit: too many vertices" or"bad shadow model counts"
|
||||
break
|
||||
end
|
||||
local verts, idx = {}, {}
|
||||
for _ = 1, nv do
|
||||
punct(p, "(")
|
||||
table.insert(verts, num(p))
|
||||
table.insert(verts, num(p))
|
||||
table.insert(verts, num(p))
|
||||
punct(p, ")")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
for _ = 1, ni do
|
||||
table.insert(idx, num(p))
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
punct(p, "}")
|
||||
if keepShadows then
|
||||
table.insert(info.shadowModels, { name = name, vertices = verts, indices = idx, noCaps = c[2], noFrontCaps = c[3], planeBits = c[5] })
|
||||
end
|
||||
else
|
||||
skipBlock(p)
|
||||
end
|
||||
end
|
||||
if p.err then
|
||||
return nil, Err.from(p.err)
|
||||
end
|
||||
scene.meta.proc = info
|
||||
scene.meta.source = { format = "proc", version = version, models = #info.models, portals = #info.portals, nodes = #info.nodes, areas = info.areas }
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Proc
|
||||
end
|
||||
__FILES["model/qb"] = function()
|
||||
local __compat = __compat local buffer, type, __iter = __compat.buffer, __compat.type, __compat.__iter --!strict
|
||||
-- model/qb: Qubicle Binary (.qb 1.1.0.0, minddesk.com "Qubicle Binary"), read + write. Supports: RGBA / BGRA, left- or
|
||||
|
||||
+539
-538
File diff suppressed because it is too large.
Load diff
@@ -82,6 +82,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| model | off | .off | yes | yes | polygons: true, points: true, colors: true, binary: false | `tests/model/off.luau` |
|
||||
| model | ply | .ply | yes | yes | ascii: true, binary: true, polygons: true, points: true, colors: true | `tests/model/ply.luau` |
|
||||
| model | pmx | .pmx .pmd | yes | - | materials: true, skins: true, morphTargets: true, pmd: true | `tests/model/pmx.luau` |
|
||||
| model | proc | .proc | yes | - | portals: true | `tests/model/proc.luau` |
|
||||
| model | qb | .qb | yes | yes | voxels: true, truecolor: true | `tests/model/qb.luau` |
|
||||
| model | rbxm | .rbxm .rbxl | yes | yes | tree: true, compression: "none", "lz4", "zstd" | `tests/model/rbxm.luau` |
|
||||
| model | rbxmx | .rbxmx .rbxlx | yes | yes | tree: true | `tests/model/rbxmx.luau` |
|
||||
|
||||
@@ -527,6 +527,23 @@ assert(data.sectors[1].fields.user_gravity == 0.5)
|
||||
assert(data.linedefs[4].tag == 7 and data.blocks[1].type == "vavoombox")
|
||||
```
|
||||
|
||||
## Doom 3 / Quake 4 levels
|
||||
|
||||
A compiled `.proc` file (dmap's render geometry: `mapProcFile003`, Quake 4's `PROC "4"`) loads as a Scene: each model
|
||||
(the areas `_area0`, `_area1`, ... and the inline models of entities) is a mesh with a primitive per surface
|
||||
(positions, normals, texture coordinates, Quake 4 vertex colours as `COLOR_0`) and a child node of the root; the
|
||||
materials are URIs. id Tech 4 triangles are clockwise-front, so they are turned counter-clockwise. The portals between
|
||||
areas, the BSP nodes and the shadow models load as data in `meta.proc`.
|
||||
|
||||
```luau
|
||||
local level = assert(F.model.load(procText))
|
||||
assert(level.meshes[1].name == "_area0" and level.meta.up == "z")
|
||||
local proc = level.meta.proc
|
||||
assert(proc.areas == 2 and proc.portals[1].positive == 1 and #proc.portals[1].points == 12)
|
||||
assert(proc.nodes[1].negative == -1) -- < 0: area -1 - n; > 0: a node (1-based); 0: solid
|
||||
assert(level.materials[1].name == "textures/base_wall/lfwall1")
|
||||
```
|
||||
|
||||
## Blitz3D (B3D)
|
||||
|
||||
`.b3d` files (Blitz3D, also written by many old game tools) load read-only. The node tree keeps its names and TRS;
|
||||
@@ -766,6 +783,7 @@ assert(back.meshes[1].primitives[1].indices.count == 3)
|
||||
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
|
||||
| MD2, MD3, MDL, MD5, BSP | yes | - | game models and maps: see "Game models" above |
|
||||
| SMD | yes | yes | Valve studiomdl data |
|
||||
| Doom 3 / Quake 4 `.proc` | yes | | render geometry per area, portals and BSP nodes as data |
|
||||
| UDMF (`.udmf`, `TEXTMAP`) | yes | | Doom maps as text: see "Doom maps (UDMF)" above |
|
||||
| Roblox `.mesh` (robloxmesh) | yes | yes | see [roblox.md](roblox.md) |
|
||||
| Roblox `.rbxm` / `.rbxl`, `.rbxmx` / `.rbxlx` | as an `RbxTree` | yes | instance trees with typed properties: see [roblox.md](roblox.md) |
|
||||
|
||||
@@ -938,6 +938,18 @@ local list: { Entry } = {
|
||||
return require("@rosetta/model/pmx")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "proc",
|
||||
category = "model",
|
||||
path = "model/proc",
|
||||
extensions = { "proc" },
|
||||
mime = { "model/x-doom3-proc" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/Lexer" },
|
||||
caps = { decode = true, encode = false, portals = true },
|
||||
load = function()
|
||||
return require("@rosetta/model/proc")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "qb",
|
||||
category = "model",
|
||||
|
||||
@@ -0,0 +1,380 @@
|
||||
--!strict
|
||||
-- model/proc: Doom 3 / Quake 4 / Prey `.proc` render geometry (dmap output: "mapProcFile003", Quake 4 "PROC" "4"; the
|
||||
-- Doom 3 GPL source's RenderWorld_load.cpp read for the layout, no code taken), read only. Every model (areas
|
||||
-- "_areaN", inline entity models) is a mesh with a primitive per surface and a node; materials are URIs. Portals,
|
||||
-- the BSP nodes and shadow models load as data in meta.proc. Triangles are clockwise-front in id Tech 4
|
||||
-- (R_DeriveFacePlanes: normal = d2 x d1) and are turned counter-clockwise. Not supported: writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Sniff = require("@rosetta/core/Sniff")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Lexer = require("@rosetta/codecs/text/Lexer")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Proc = {
|
||||
name = "proc",
|
||||
category = "model",
|
||||
extensions = { "proc" },
|
||||
mime = { "model/x-doom3-proc" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/Lexer" },
|
||||
caps = { decode = true, encode = false, portals = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
maxVertices: number?, -- default 2^24 over the file
|
||||
shadows: boolean?, -- default true: keep shadow models in meta.proc.shadowModels
|
||||
}
|
||||
export type Portal = { positive: number, negative: number, points: { number } } -- areas; points x, y, z, ...
|
||||
export type BspNode = { plane: { number }, positive: number, negative: number } -- children: > 0 node (1-based), 0 solid, < 0 area -1 - n
|
||||
export type ShadowModel = { name: string, vertices: { number }, indices: { number }, noCaps: number, noFrontCaps: number, planeBits: number }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local Scene = Types.Scene
|
||||
local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Proc.sniff(r: Bytes.Reader): number
|
||||
local head = Sniff.text(r, 64, true)
|
||||
if string.match(head, "^mapProcFile%d%d%d") then
|
||||
return 1
|
||||
end
|
||||
if string.match(head, '^"?PROC"?%s+"?%d') then
|
||||
return 0.9
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
type P = { L: Lexer.Lexer, err: string? }
|
||||
|
||||
local function fail(p: P, msg: string)
|
||||
if not p.err then
|
||||
p.err = (p.L.err or msg) .. " at line " .. p.L:line()
|
||||
end
|
||||
end
|
||||
|
||||
local function num(p: P): number
|
||||
local k, v = p.L:next()
|
||||
if k ~= "number" then
|
||||
fail(p, "expected a number")
|
||||
return 0
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
local function punct(p: P, c: string)
|
||||
local k, v = p.L:next()
|
||||
if k ~= "punct" or v ~= c then
|
||||
fail(p, "expected " .. c)
|
||||
end
|
||||
end
|
||||
|
||||
local function str(p: P): string
|
||||
local k, v = p.L:next()
|
||||
if k ~= "string" and k ~= "word" then
|
||||
fail(p, "expected a name")
|
||||
return ""
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- numbers up to the next punctuation (the counts after a model / surface name)
|
||||
local function counts(p: P): { number }
|
||||
local out = {}
|
||||
while not p.err do
|
||||
local k, v = p.L:peek()
|
||||
if k ~= "number" then
|
||||
break
|
||||
end
|
||||
p.L:next()
|
||||
table.insert(out, v)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function skipBlock(p: P)
|
||||
local depth = 1
|
||||
while not p.err do
|
||||
local k, v = p.L:next()
|
||||
if not k then
|
||||
fail(p, "unterminated block")
|
||||
return
|
||||
end
|
||||
if k == "punct" then
|
||||
if v == "{" then
|
||||
depth += 1
|
||||
elseif v == "}" then
|
||||
depth -= 1
|
||||
if depth == 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
type Surface = { material: string, verts: { number }, arity: number, indices: { number } }
|
||||
|
||||
local function surface(p: P, budget: { number }): Surface?
|
||||
local material = str(p)
|
||||
local c = counts(p)
|
||||
if p.err then
|
||||
return nil
|
||||
end
|
||||
if #c < 2 then
|
||||
fail(p, "surface without vertex / index counts")
|
||||
return nil
|
||||
end
|
||||
local nv, ni = c[#c - 1], c[#c]
|
||||
if nv < 0 or ni < 0 or nv % 1 ~= 0 or ni % 1 ~= 0 or ni % 3 ~= 0 then
|
||||
fail(p, "bad surface counts")
|
||||
return nil
|
||||
end
|
||||
budget[1] -= nv
|
||||
if budget[1] < 0 then
|
||||
p.err = "limit: too many vertices"
|
||||
return nil
|
||||
end
|
||||
local verts: { number } = {}
|
||||
local arity = 0
|
||||
for i = 1, nv do
|
||||
punct(p, "(")
|
||||
local n = 0
|
||||
while not p.err do
|
||||
local k, v = p.L:next()
|
||||
if k == "number" then
|
||||
n += 1
|
||||
table.insert(verts, v)
|
||||
elseif k == "punct" and v == ")" then
|
||||
break
|
||||
else
|
||||
fail(p, "bad vertex")
|
||||
end
|
||||
end
|
||||
if p.err then
|
||||
return nil
|
||||
end
|
||||
if i == 1 then
|
||||
arity = n
|
||||
if n < 8 then
|
||||
fail(p, "a vertex needs x y z s t nx ny nz")
|
||||
return nil
|
||||
end
|
||||
elseif n ~= arity then
|
||||
fail(p, "vertices of different sizes")
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local idx: { number } = table.create(ni, 0)
|
||||
for i = 1, ni do
|
||||
local v = num(p)
|
||||
if v < 0 or v >= nv or v % 1 ~= 0 then
|
||||
fail(p, "index out of range")
|
||||
return nil
|
||||
end
|
||||
idx[i] = v
|
||||
end
|
||||
punct(p, "}")
|
||||
return { material = material, verts = verts, arity = arity, indices = idx }
|
||||
end
|
||||
|
||||
local function primitive(scene: Types.Scene, s: Surface, matIndex: { [string]: number }): Types.Primitive
|
||||
local a = s.arity
|
||||
local n = #s.verts // a
|
||||
local pb, nb, ub = buffer.create(n * 12), buffer.create(n * 12), buffer.create(n * 8)
|
||||
local cb: buffer? = if a >= 12 then buffer.create(n * 16) else nil
|
||||
local V = s.verts
|
||||
for i = 0, n - 1 do
|
||||
local o = i * a
|
||||
writef32(pb, i * 12, V[o + 1])
|
||||
writef32(pb, i * 12 + 4, V[o + 2])
|
||||
writef32(pb, i * 12 + 8, V[o + 3])
|
||||
writef32(ub, i * 8, V[o + 4])
|
||||
writef32(ub, i * 8 + 4, V[o + 5])
|
||||
writef32(nb, i * 12, V[o + 6])
|
||||
writef32(nb, i * 12 + 4, V[o + 7])
|
||||
writef32(nb, i * 12 + 8, V[o + 8])
|
||||
if cb then
|
||||
for k = 0, 3 do
|
||||
local c = V[o + 9 + k]
|
||||
writef32(cb, i * 16 + k * 4, if c > 1 then c / 255 else c)
|
||||
end
|
||||
end
|
||||
end
|
||||
local ni = #s.indices
|
||||
local ib = buffer.create(ni * 4)
|
||||
for t = 0, ni - 1, 3 do
|
||||
writeu32(ib, t * 4, s.indices[t + 1])
|
||||
writeu32(ib, t * 4 + 4, s.indices[t + 3])
|
||||
writeu32(ib, t * 4 + 8, s.indices[t + 2])
|
||||
end
|
||||
local mi = matIndex[s.material]
|
||||
if not mi then
|
||||
table.insert(scene.textures, { name = s.material, uri = s.material })
|
||||
local mat = Scene.material(s.material)
|
||||
mat.metallic = 0
|
||||
mat.baseColorTexture = { texture = #scene.textures }
|
||||
table.insert(scene.materials, mat)
|
||||
mi = #scene.materials
|
||||
matIndex[s.material] = mi
|
||||
end
|
||||
local attrs: { [string]: Types.Accessor } = {
|
||||
POSITION = Scene.accessor(pb, "f32", 3, n),
|
||||
NORMAL = Scene.accessor(nb, "f32", 3, n),
|
||||
TEXCOORD_0 = Scene.accessor(ub, "f32", 2, n),
|
||||
}
|
||||
if cb then
|
||||
attrs.COLOR_0 = Scene.accessor(cb, "f32", 4, n)
|
||||
end
|
||||
return { attributes = attrs, indices = Scene.accessor(ib, "u32", 1, ni), mode = "triangles" :: any, material = mi, extras = { proc = { material = s.material } } }
|
||||
end
|
||||
|
||||
function Proc.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local text = Sniff.dropBom(r:string(r:remaining()))
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
local p: P = { L = Lexer.new(text), err = nil }
|
||||
local k, head = p.L:next()
|
||||
local version: string
|
||||
if (k == "word" or k == "string") and string.match(head, "^mapProcFile%d+$") then
|
||||
version = head
|
||||
elseif (k == "word" or k == "string") and head == "PROC" then
|
||||
local vk, vv = p.L:next()
|
||||
if vk ~= "number" and vk ~= "string" then
|
||||
return nil, Err.new(Err.CORRUPT, "PROC without a version")
|
||||
end
|
||||
version = "PROC " .. tostring(vv)
|
||||
else
|
||||
return nil, Err.new(Err.CORRUPT, "not a .proc file")
|
||||
end
|
||||
local scene = Scene.new()
|
||||
scene.meta.up = "z"
|
||||
local world: Types.Node = { name = "proc", children = {} }
|
||||
table.insert(scene.nodes, world)
|
||||
scene.roots[1] = 1
|
||||
local info = { version = version, areas = 0, models = {}, portals = {} :: { Portal }, nodes = {} :: { BspNode }, shadowModels = {} :: { ShadowModel } }
|
||||
local matIndex: { [string]: number } = {}
|
||||
local budget = { opts and opts.maxVertices or 2 ^ 24 }
|
||||
local keepShadows = not (opts and opts.shadows == false)
|
||||
while not p.err do
|
||||
local tk, tv = p.L:next()
|
||||
if not tk then
|
||||
if p.L.err then
|
||||
fail(p, p.L.err)
|
||||
end
|
||||
break
|
||||
end
|
||||
if tk ~= "word" then
|
||||
fail(p, "expected a section name")
|
||||
break
|
||||
end
|
||||
punct(p, "{")
|
||||
local section = string.lower(tv)
|
||||
if section == "model" then
|
||||
local name = str(p)
|
||||
local c = counts(p)
|
||||
local ns = c[1] or 0
|
||||
local mesh: Types.Mesh = { name = name, primitives = {} }
|
||||
for _ = 1, ns do
|
||||
punct(p, "{")
|
||||
local s = surface(p, budget)
|
||||
if not s then
|
||||
break
|
||||
end
|
||||
table.insert(mesh.primitives, primitive(scene, s, matIndex))
|
||||
end
|
||||
punct(p, "}")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
table.insert(scene.meshes, mesh)
|
||||
table.insert(scene.nodes, { name = name, children = {}, mesh = #scene.meshes })
|
||||
table.insert(world.children, #scene.nodes)
|
||||
table.insert(info.models, { name = name, surfaces = #mesh.primitives, area = tonumber(string.match(name, "^_area(%d+)$")) })
|
||||
elseif section == "interareaportals" then
|
||||
info.areas = num(p)
|
||||
local n = num(p)
|
||||
for _ = 1, n do
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
local np = num(p)
|
||||
local pos, neg = num(p), num(p)
|
||||
local pts = {}
|
||||
for _ = 1, np do
|
||||
punct(p, "(")
|
||||
table.insert(pts, num(p))
|
||||
table.insert(pts, num(p))
|
||||
table.insert(pts, num(p))
|
||||
punct(p, ")")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
table.insert(info.portals, { positive = pos, negative = neg, points = pts })
|
||||
end
|
||||
punct(p, "}")
|
||||
elseif section == "nodes" then
|
||||
local n = num(p)
|
||||
for _ = 1, n do
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
punct(p, "(")
|
||||
local pl = { num(p), num(p), num(p), num(p) }
|
||||
punct(p, ")")
|
||||
local pos, neg = num(p), num(p)
|
||||
table.insert(info.nodes, { plane = pl, positive = if pos > 0 then pos + 1 else pos, negative = if neg > 0 then neg + 1 else neg })
|
||||
end
|
||||
punct(p, "}")
|
||||
elseif section == "shadowmodel" then
|
||||
local name = str(p)
|
||||
local c = counts(p)
|
||||
if #c < 5 then
|
||||
fail(p, "shadow model without counts")
|
||||
break
|
||||
end
|
||||
local nv, ni = c[1], c[4]
|
||||
budget[1] -= nv
|
||||
if nv < 0 or ni < 0 or budget[1] < 0 then
|
||||
p.err = if budget[1] < 0 then "limit: too many vertices" else "bad shadow model counts"
|
||||
break
|
||||
end
|
||||
local verts, idx = {}, {}
|
||||
for _ = 1, nv do
|
||||
punct(p, "(")
|
||||
table.insert(verts, num(p))
|
||||
table.insert(verts, num(p))
|
||||
table.insert(verts, num(p))
|
||||
punct(p, ")")
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
for _ = 1, ni do
|
||||
table.insert(idx, num(p))
|
||||
if p.err then
|
||||
break
|
||||
end
|
||||
end
|
||||
punct(p, "}")
|
||||
if keepShadows then
|
||||
table.insert(info.shadowModels, { name = name, vertices = verts, indices = idx, noCaps = c[2], noFrontCaps = c[3], planeBits = c[5] })
|
||||
end
|
||||
else
|
||||
skipBlock(p)
|
||||
end
|
||||
end
|
||||
if p.err then
|
||||
return nil, Err.from(p.err)
|
||||
end
|
||||
scene.meta.proc = info
|
||||
scene.meta.source = { format = "proc", version = version, models = #info.models, portals = #info.portals, nodes = #info.nodes, areas = info.areas }
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Proc
|
||||
@@ -0,0 +1,87 @@
|
||||
-- model/proc tests: dmap-layout Doom 3 and Quake 4 files written by the fixture (models, surfaces, portals, nodes,
|
||||
-- shadow models) against the writer's geometry. Fixtures: tools/fixtures/proc.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@rosetta")
|
||||
local Proc = require("@rosetta/model/proc")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Crc32 = require("@rosetta/core/Crc32")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Fx = require("@tests/gen/proc")
|
||||
|
||||
local t = T.suite("proc")
|
||||
t:eq(Proc.name, "proc", "name")
|
||||
|
||||
for key, c in Fx do
|
||||
t:eq(F.detect(c.text), "proc", key .. ": F.detect")
|
||||
local s, e = Proc.decode(Bytes.reader(buffer.fromstring(c.text)))
|
||||
t:check(s, key .. ": " .. tostring(e))
|
||||
local sc = s :: any
|
||||
t:eq(#sc.meshes, #c.models, key .. ": models")
|
||||
for mi, m in c.models do
|
||||
local mesh = sc.meshes[mi]
|
||||
t:eq(mesh.name, m.name, key .. ": model name")
|
||||
t:eq(#mesh.primitives, #m.surfaces, key .. ": surfaces of " .. m.name)
|
||||
for si, w in m.surfaces do
|
||||
local p = mesh.primitives[si]
|
||||
local label = key .. " " .. m.name .. " surface " .. si
|
||||
t:eq(p.attributes.POSITION.count, w.verts, label .. ": vertices")
|
||||
t:eq(Crc32.of(p.attributes.POSITION.data), w.pos, label .. ": positions")
|
||||
t:eq(Crc32.of(p.attributes.TEXCOORD_0.data), w.uv, label .. ": texcoords")
|
||||
t:eq(Crc32.of(p.indices.data), w.idx, label .. ": indices, counter-clockwise")
|
||||
t:eq(sc.materials[p.material].name, w.material, label .. ": material")
|
||||
-- every triangle faces its vertex normal after the turn
|
||||
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
|
||||
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
|
||||
local idx = Types.Scene.reader(p.indices) :: any
|
||||
local ok = true
|
||||
for k = 0, p.indices.count - 1, 3 do
|
||||
local a, b, cc = idx(k, 0), idx(k + 1, 0), idx(k + 2, 0)
|
||||
local ux, uy, uz = pos(b, 0) - pos(a, 0), pos(b, 1) - pos(a, 1), pos(b, 2) - pos(a, 2)
|
||||
local vx, vy, vz = pos(cc, 0) - pos(a, 0), pos(cc, 1) - pos(a, 1), pos(cc, 2) - pos(a, 2)
|
||||
local d = (uy * vz - uz * vy) * nrm(a, 0) + (uz * vx - ux * vz) * nrm(a, 1) + (ux * vy - uy * vx) * nrm(a, 2)
|
||||
ok = ok and d > 0
|
||||
end
|
||||
t:check(ok, label .. ": winding faces the normals")
|
||||
if key == "quake4" then
|
||||
t:check(p.attributes.COLOR_0 ~= nil, label .. ": vertex colours")
|
||||
end
|
||||
end
|
||||
end
|
||||
local info = sc.meta.proc
|
||||
t:eq(info.areas, c.areas, key .. ": areas")
|
||||
t:eq(#info.portals, #c.portals, key .. ": portals")
|
||||
t:eq(info.portals[1].positive, c.portals[1].positive, key .. ": portal areas")
|
||||
t:eq(#info.portals[1].points, #c.portals[1].points, key .. ": portal points")
|
||||
t:eq(info.portals[1].points[12], c.portals[1].points[12], key .. ": portal point")
|
||||
t:eq(#info.nodes, #c.nodes, key .. ": nodes")
|
||||
t:eq(info.nodes[1].positive, 2, key .. ": node children 1-based")
|
||||
t:eq(info.nodes[1].negative, -1, key .. ": area children kept")
|
||||
t:eq(info.nodes[1].plane[4], c.nodes[1].plane[4], key .. ": node plane")
|
||||
t:eq(info.shadowModels[1].name, c.shadow.name, key .. ": shadow model")
|
||||
t:eq(#info.shadowModels[1].vertices, c.shadow.verts * 3, key .. ": shadow vertices")
|
||||
t:eq(#info.shadowModels[1].indices, c.shadow.indices, key .. ": shadow indices")
|
||||
t:eq(info.models[1].area, 0, key .. ": area number")
|
||||
t:eq(#sc.nodes, 1 + #c.models, key .. ": a node per model")
|
||||
t:eq(sc.meta.up, "z", key .. ": z up")
|
||||
end
|
||||
t:eq((F.load(Fx.quake4.text) :: any).meta.source.version, "PROC 4", "Quake 4 version")
|
||||
|
||||
do
|
||||
local function code(s: string, opts: any?): any
|
||||
local _, e = Proc.decode(Bytes.reader(buffer.fromstring(s)), opts)
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("hello"), Err.CORRUPT, "not a proc")
|
||||
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 0 0 0 0 1 ) 0 0 5 } }'), Err.CORRUPT, "index out of range")
|
||||
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 ) 0 0 0 } }'), Err.CORRUPT, "short vertex")
|
||||
t:eq(code('mapProcFile003 model { "m" 1 { "x" 1 3 ( 0 0 0 0 0 0 0 1 ) 0 0'), Err.CORRUPT, "truncated")
|
||||
t:eq(code(Fx.doom3.text, { maxVertices = 10 }), Err.LIMIT, "maxVertices")
|
||||
local s = Proc.decode(Bytes.reader(buffer.fromstring("mapProcFile003 unknownSection { a { b } c } model { \"m\" 0 }"))) :: any
|
||||
t:check(s and #s.meshes == 1, "unknown sections skipped")
|
||||
end
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Proc.decode(Bytes.reader(b))
|
||||
end, { buffer.fromstring(Fx.doom3.text), buffer.fromstring(Fx.quake4.text) }, { mutations = 300 })
|
||||
t:done()
|
||||
@@ -1,9 +1,10 @@
|
||||
# Docs inputs for the id Tech sections of the scene / tilemap / archive pages (tools/fixtures.py; tools/docs_check.py
|
||||
# explains the inputs), made by the format fixtures' writers: the hand-written UDMF room (udmf.py).
|
||||
# explains the inputs), made by the format fixtures' writers: the hand-written UDMF room (udmf.py), a dmap-layout
|
||||
# .proc (proc.py).
|
||||
# tests/gen/docs_idtech.luau { files = { [name] = bytes }, vars = { udmfText = "room.udmf", ... } }
|
||||
import importlib.util, os
|
||||
|
||||
INPUTS = ["tools/docs_check.py"]
|
||||
INPUTS = ["tools/docs_check.py", "tools/fixtures/udmf.py", "tools/fixtures/proc.py"]
|
||||
|
||||
|
||||
def mod(ctx, name):
|
||||
@@ -16,6 +17,9 @@ def mod(ctx, name):
|
||||
|
||||
def generate(ctx):
|
||||
check = mod(ctx, "docs_check")
|
||||
files = {"room.udmf": mod(ctx, "udmf").ROOM.encode()}
|
||||
vars = {"udmfText": "room.udmf"}
|
||||
P = mod(ctx, "proc")
|
||||
models, portals, nodes, areas, shadow = P.level(ctx.rng(2), False)
|
||||
files = {"room.udmf": mod(ctx, "udmf").ROOM.encode(),
|
||||
"level.proc": P.write(models, portals, nodes, areas, shadow, "mapProcFile003", False).encode()}
|
||||
vars = {"udmfText": "room.udmf", "procText": "level.proc"}
|
||||
check.emit_inputs(ctx, files, vars)
|
||||
@@ -0,0 +1,124 @@
|
||||
# Doom 3 .proc fixtures (tools/fixtures.py). Doom 3's data is not redistributable, so the files are written here in
|
||||
# dmap's layout (the Doom 3 GPL source's WriteOutputFile, read for the text layout; no code taken): two area models
|
||||
# made of boxes, an inline entity model, portals, BSP nodes and a shadow model; and a Quake 4 "PROC" "4" file with
|
||||
# vertex colours. The reference is this script's own geometry: per model and surface the vertex count, CRC-32 of the
|
||||
# positions / texcoords as f32 LE, and of the indices as u32 LE with every triangle turned counter-clockwise.
|
||||
# tests/gen/proc.luau { doom3 = { text, models = { { name, surfaces = { { material, verts, pos, uv, idx } } } },
|
||||
# portals, nodes, areas, shadow = { name, verts, indices } }, quake4 = { ... } }
|
||||
import struct, zlib
|
||||
|
||||
|
||||
def box(rng, x0, y0, z0, x1, y1, z1):
|
||||
# 6 quads; triangles clockwise seen from outside (id Tech 4's front faces)
|
||||
c = [(x0, y0, z0), (x1, y0, z0), (x1, y1, z0), (x0, y1, z0), (x0, y0, z1), (x1, y0, z1), (x1, y1, z1), (x0, y1, z1)]
|
||||
faces = [((0, 1, 2, 3), (0, 0, -1)), ((4, 7, 6, 5), (0, 0, 1)), ((0, 4, 5, 1), (0, -1, 0)), ((1, 5, 6, 2), (1, 0, 0)),
|
||||
((2, 6, 7, 3), (0, 1, 0)), ((3, 7, 4, 0), (-1, 0, 0))]
|
||||
verts, idx = [], []
|
||||
for quad, n in faces:
|
||||
b = len(verts)
|
||||
for k, vi in enumerate(quad):
|
||||
x, y, z = c[vi]
|
||||
verts.append((x, y, z, (k & 1) * 2.5 + rng.randint(0, 3) * 0.25, (k >> 1) * -1.5 + 0.0, n[0], n[1], n[2]))
|
||||
# clockwise triangles about n, whichever way the corners run
|
||||
(ax, ay, az), (bx, by, bz), (cx, cy, cz) = c[quad[0]], c[quad[1]], c[quad[2]]
|
||||
ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
|
||||
ccw = (uy * vz - uz * vy) * n[0] + (uz * vx - ux * vz) * n[1] + (ux * vy - uy * vx) * n[2] > 0
|
||||
idx += [b, b + 2, b + 1, b, b + 3, b + 2] if ccw else [b, b + 1, b + 2, b, b + 2, b + 3]
|
||||
return verts, idx
|
||||
|
||||
|
||||
def fmt(v):
|
||||
return ("%d" % v) if float(v).is_integer() else ("%g" % v)
|
||||
|
||||
|
||||
def write(models, portals, nodes, areas, shadow, header, colors):
|
||||
out = [header, ""]
|
||||
for m in models:
|
||||
out.append('model { /* name = */ "%s" /* numSurfaces = */ %d' % (m["name"], len(m["surfaces"])))
|
||||
out.append("")
|
||||
for si, s in enumerate(m["surfaces"]):
|
||||
out.append('/* surface %d */ { "%s" /* numVerts = */ %d /* numIndexes = */ %d' % (si, s["material"], len(s["verts"]), len(s["idx"])))
|
||||
line = []
|
||||
for v in s["verts"]:
|
||||
vals = list(v) + (s["color"] if colors else [])
|
||||
line.append("( " + " ".join(fmt(x) for x in vals) + " )")
|
||||
if len(line) == 3:
|
||||
out.append(" ".join(line))
|
||||
line = []
|
||||
if line:
|
||||
out.append(" ".join(line))
|
||||
for k in range(0, len(s["idx"]), 15):
|
||||
out.append(" ".join(str(i) for i in s["idx"][k:k + 15]) + " ")
|
||||
out.append("}")
|
||||
out.append("")
|
||||
out.append("}")
|
||||
out.append("")
|
||||
out.append("shadowModel { /* name = */ \"%s\"" % shadow["name"])
|
||||
out.append("/* numVerts = */ %d /* noCaps = */ 0 /* noFrontCaps = */ 0 /* numIndexes = */ %d /* planeBits = */ 59"
|
||||
% (len(shadow["verts"]) // 3, len(shadow["indices"])))
|
||||
out.append(" ".join("( %s %s %s )" % tuple(fmt(x) for x in shadow["verts"][k:k + 3]) for k in range(0, len(shadow["verts"]), 3)))
|
||||
out.append(" ".join(str(i) for i in shadow["indices"]))
|
||||
out.append("}")
|
||||
out.append("")
|
||||
out.append("interAreaPortals { /* numAreas = */ %d /* numIAP = */ %d" % (areas, len(portals)))
|
||||
out.append("")
|
||||
out.append("/* interAreaPortal format is: numPoints positiveSideArea negativeSideArea ( point) ... */")
|
||||
for i, p in enumerate(portals):
|
||||
pts = " ".join("( %s %s %s )" % tuple(fmt(x) for x in p["points"][k:k + 3]) for k in range(0, len(p["points"]), 3))
|
||||
out.append("/* iap %d */ %d %d %d %s " % (i, len(p["points"]) // 3, p["positive"], p["negative"], pts))
|
||||
out.append("}")
|
||||
out.append("")
|
||||
out.append("nodes { /* numNodes = */ %d" % len(nodes))
|
||||
out.append("")
|
||||
out.append("/* node format is: ( planeVector ) positiveChild negativeChild */")
|
||||
out.append("/* a child number of 0 is an opaque, solid area */")
|
||||
out.append("/* negative child numbers are areas: (-1-child) */")
|
||||
for i, n in enumerate(nodes):
|
||||
out.append("/* node %d */ ( %s ) %d %d " % (i, " ".join(fmt(x) for x in n["plane"]), n["positive"], n["negative"]))
|
||||
out.append("}")
|
||||
return "\n".join(out) + "\n"
|
||||
|
||||
|
||||
def ref(models):
|
||||
res = []
|
||||
for m in models:
|
||||
ss = []
|
||||
for s in m["surfaces"]:
|
||||
pos = b"".join(struct.pack("<3f", *v[0:3]) for v in s["verts"])
|
||||
uv = b"".join(struct.pack("<2f", *v[3:5]) for v in s["verts"])
|
||||
ccw = []
|
||||
for k in range(0, len(s["idx"]), 3):
|
||||
a, b, c = s["idx"][k:k + 3]
|
||||
ccw += [a, c, b]
|
||||
ss.append({"material": s["material"], "verts": len(s["verts"]), "pos": zlib.crc32(pos), "uv": zlib.crc32(uv),
|
||||
"idx": zlib.crc32(struct.pack("<%dI" % len(ccw), *ccw)), "tris": len(ccw) // 3})
|
||||
res.append({"name": m["name"], "surfaces": ss})
|
||||
return res
|
||||
|
||||
|
||||
def level(rng, colors):
|
||||
models = []
|
||||
for a in range(2):
|
||||
surfs = []
|
||||
for k in range(2 + a):
|
||||
v, i = box(rng, a * 512 + k * 64, -128, 0, a * 512 + k * 64 + 48, 128 + rng.randint(0, 4) * 8, 96.5)
|
||||
surfs.append({"material": "textures/base_wall/lfwall%d" % (k + 1), "verts": v, "idx": i,
|
||||
"color": [255, 128, 0, 255]})
|
||||
models.append({"name": "_area%d" % a, "surfaces": surfs})
|
||||
v, i = box(rng, 100, 0, 8, 116, 16, 24)
|
||||
models.append({"name": "func_static_1", "surfaces": [{"material": "models/mapobjects/crate", "verts": v, "idx": i, "color": [10, 20, 30, 40]}]})
|
||||
portals = [{"positive": 1, "negative": 0, "points": [448, -128, 0, 448, 128, 0, 448, 128, 96, 448, -128, 96]}]
|
||||
nodes = [{"plane": [1, 0, 0, -448], "positive": 1, "negative": -1}, {"plane": [0, 0, 1, 0], "positive": 0, "negative": -2}]
|
||||
shadow = {"name": "_prelight_light_1", "verts": [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], "indices": [0, 1, 2, 0, 2, 3]}
|
||||
return models, portals, nodes, 2, shadow
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(31)
|
||||
out = {}
|
||||
for key, header, colors in [("doom3", "mapProcFile003", False), ("quake4", 'PROC "4"', True)]:
|
||||
models, portals, nodes, areas, shadow = level(rng, colors)
|
||||
out[key] = {"text": write(models, portals, nodes, areas, shadow, header, colors), "models": ref(models),
|
||||
"portals": portals, "nodes": nodes, "areas": areas,
|
||||
"shadow": {"name": shadow["name"], "verts": len(shadow["verts"]) // 3, "indices": len(shadow["indices"])}}
|
||||
ctx.emit("proc", out)
|
||||
Reference in new issue
Block a user