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:
4DBug committed 2026-11-01 18:40:10 -06:00
1 parent 7d4e0381fe
commit 719abb97c0
12 files changed
+2498 -1081

No files matched your search

+394
View File
@@ -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