model/d3map: Doom 3 / Quake 4 editor .map (Version 2 / 3): brushDef3 through codecs/model/Brush, patchDef2 / 3 through codecs/model/Patch, entities as nodes; side areas checked against qhull
This commit is contained in:
1 parent
4bd59a30c8
commit
0f0da6e8ea
13 files changed
+2945
-1101
No files matched your search
@@ -746,6 +746,18 @@ local list: { Entry } = {
|
||||
return require("@rosetta/model/bsp")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "d3map",
|
||||
category = "model",
|
||||
path = "model/d3map",
|
||||
extensions = { "map" },
|
||||
mime = { "text/x-doom3-map" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/model/Brush", "codecs/model/Patch", "codecs/model/Mat4", "codecs/text/Lexer" },
|
||||
caps = { decode = true, encode = false, patches = true, variants = { "doom3", "quake4" } },
|
||||
load = function()
|
||||
return require("@rosetta/model/d3map")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "dae",
|
||||
category = "model",
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
--!strict
|
||||
-- model/d3map/Parse: Doom 3 / Quake 4 editor .map text ("Version 2" / "Version 3"; idMapFile / idMapEntity /
|
||||
-- idMapBrush / idMapPatch layouts of the Doom 3 GPL source docs, no code taken) to entities with brushDef3 sides
|
||||
-- (plane a b c d with a x + b y + c z + d = 0, normal out of the brush; 2 x 3 texture matrix) and patchDef2 / 3.
|
||||
local Lexer = require("@rosetta/codecs/text/Lexer")
|
||||
|
||||
local Parse = {}
|
||||
|
||||
export type Side = { plane: { number }, matrix: { { number } }, material: string, flags: { number } }
|
||||
export type Prim = {
|
||||
type: string, -- "brushDef3" | "patchDef2" | "patchDef3"
|
||||
sides: { Side }?,
|
||||
material: string?,
|
||||
info: { number }?, -- patchDef2: width height 0 0 0; patchDef3: width height subdivX subdivY 0 0 0
|
||||
width: number?,
|
||||
height: number?,
|
||||
points: { number }?, -- x y z s t per control point, in file order (column by column)
|
||||
}
|
||||
export type Entity = { keys: { [string]: string }, prims: { Prim }, skipped: number }
|
||||
export type Map = { version: number, entities: { Entity } }
|
||||
|
||||
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 want(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 number(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 tuple(p: P, n: number): { number }
|
||||
want(p, "(")
|
||||
local out = table.create(n, 0)
|
||||
for i = 1, n do
|
||||
out[i] = number(p)
|
||||
end
|
||||
want(p, ")")
|
||||
return out
|
||||
end
|
||||
|
||||
local function name(p: P): string
|
||||
local k, v = p.L:next()
|
||||
if k == "string" or k == "word" then
|
||||
return v
|
||||
end
|
||||
fail(p, "expected a material name")
|
||||
return ""
|
||||
end
|
||||
|
||||
local function brushDef3(p: P): Prim
|
||||
want(p, "{")
|
||||
local sides: { Side } = {}
|
||||
while not p.err do
|
||||
local k, v = p.L:peek()
|
||||
if k == "punct" and v == "}" then
|
||||
p.L:next()
|
||||
break
|
||||
end
|
||||
local plane = tuple(p, 4)
|
||||
want(p, "(")
|
||||
local m1 = tuple(p, 3)
|
||||
local m2 = tuple(p, 3)
|
||||
want(p, ")")
|
||||
local material = name(p)
|
||||
local flags: { number } = {}
|
||||
while not p.err do
|
||||
local fk = p.L:peek()
|
||||
if fk ~= "number" then
|
||||
break
|
||||
end
|
||||
table.insert(flags, number(p))
|
||||
end
|
||||
table.insert(sides, { plane = plane, matrix = { m1, m2 }, material = material, flags = flags })
|
||||
end
|
||||
return { type = "brushDef3", sides = sides }
|
||||
end
|
||||
|
||||
local function patchDef(p: P, kind: string, maxPoints: number): Prim
|
||||
want(p, "{")
|
||||
local material = name(p)
|
||||
local info = tuple(p, if kind == "patchDef3" then 7 else 5)
|
||||
local w, h = info[1], info[2]
|
||||
if p.err then
|
||||
return { type = kind }
|
||||
end
|
||||
if w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 or w * h > maxPoints then
|
||||
fail(p, "limit: patch size " .. w .. " x " .. h)
|
||||
return { type = kind }
|
||||
end
|
||||
local pts: { number } = table.create(w * h * 5, 0)
|
||||
want(p, "(")
|
||||
for i = 0, w - 1 do
|
||||
want(p, "(")
|
||||
for j = 0, h - 1 do
|
||||
local v = tuple(p, 5)
|
||||
if p.err then
|
||||
return { type = kind }
|
||||
end
|
||||
table.move(v, 1, 5, (i * h + j) * 5 + 1, pts)
|
||||
end
|
||||
want(p, ")")
|
||||
end
|
||||
want(p, ")")
|
||||
want(p, "}")
|
||||
return { type = kind, material = material, info = info, width = w, height = h, points = pts }
|
||||
end
|
||||
|
||||
function Parse.parse(text: string, maxPrims: number?): (Map?, string?)
|
||||
local p: P = { L = Lexer.new(text), err = nil }
|
||||
local k, v = p.L:next()
|
||||
if k ~= "word" or v ~= "Version" then
|
||||
return nil, "missing the Version line"
|
||||
end
|
||||
local version = number(p)
|
||||
local m: Map = { version = version, entities = {} }
|
||||
local limit = maxPrims or 2 ^ 20
|
||||
local count = 0
|
||||
while not p.err do
|
||||
local ek, ev = p.L:next()
|
||||
if not ek then
|
||||
if p.L.err then
|
||||
fail(p, p.L.err)
|
||||
end
|
||||
break
|
||||
end
|
||||
if ek ~= "punct" or ev ~= "{" then
|
||||
fail(p, "expected an entity")
|
||||
break
|
||||
end
|
||||
local ent: Entity = { keys = {}, prims = {}, skipped = 0 }
|
||||
while not p.err do
|
||||
local tk, tv = p.L:next()
|
||||
if tk == "string" then
|
||||
local vk, vv = p.L:next()
|
||||
if vk ~= "string" then
|
||||
fail(p, "expected a key value")
|
||||
break
|
||||
end
|
||||
ent.keys[tv] = vv
|
||||
elseif tk == "punct" and tv == "{" then
|
||||
count += 1
|
||||
if count > limit then
|
||||
p.err = "limit: more than " .. limit .. " primitives"
|
||||
break
|
||||
end
|
||||
local pk, pv = p.L:peek()
|
||||
if pk == "word" and (pv == "brushDef3" or pv == "patchDef2" or pv == "patchDef3") then
|
||||
p.L:next()
|
||||
local prim = if pv == "brushDef3" then brushDef3(p) else patchDef(p, pv, 2 ^ 16)
|
||||
want(p, "}")
|
||||
table.insert(ent.prims, prim)
|
||||
else
|
||||
-- brushDef (Quake III brush primitives) and Version 1 brushes are not read
|
||||
ent.skipped += 1
|
||||
local depth = 1
|
||||
while not p.err and depth > 0 do
|
||||
local sk, sv = p.L:next()
|
||||
if not sk then
|
||||
fail(p, "unterminated primitive")
|
||||
elseif sk == "punct" and sv == "{" then
|
||||
depth += 1
|
||||
elseif sk == "punct" and sv == "}" then
|
||||
depth -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif tk == "punct" and tv == "}" then
|
||||
break
|
||||
else
|
||||
fail(p, if tk then "unexpected " .. tostring(tv) else "unterminated entity")
|
||||
end
|
||||
end
|
||||
table.insert(m.entities, ent)
|
||||
end
|
||||
if p.err then
|
||||
return nil, p.err
|
||||
end
|
||||
return m, nil
|
||||
end
|
||||
|
||||
return Parse
|
||||
@@ -0,0 +1,280 @@
|
||||
--!strict
|
||||
-- model/d3map: Doom 3 / Quake 4 / Prey editor .map ("Version 2" / "Version 3"), read only. brushDef3 sides (plane
|
||||
-- equations, Doom 3 texture matrices on the ComputeAxisBase axes) become polygons through codecs/model/Brush,
|
||||
-- patchDef2 / patchDef3 surfaces grids through codecs/model/Patch (patchDef3: its explicit subdivisions); a mesh per
|
||||
-- entity with geometry, entities as nodes (keys in extras; origin / rotation / angle on point entities), materials as
|
||||
-- URIs. From the Doom 3 GPL source docs (MapFile.cpp layout; no code taken). Not supported: writing, brushDef (Quake
|
||||
-- III brush primitives) and Version 1 brushes (counted in meta.skipped), compiling (see model/proc for dmap output).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ 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 Brush = require("@rosetta/codecs/model/Brush")
|
||||
local Patch = require("@rosetta/codecs/model/Patch")
|
||||
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
||||
local Parse = require("@rosetta/model/d3map/Parse")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local D3map = {
|
||||
name = "d3map",
|
||||
category = "model",
|
||||
extensions = { "map" },
|
||||
mime = { "text/x-doom3-map" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/model/Brush", "codecs/model/Patch", "codecs/model/Mat4", "codecs/text/Lexer" },
|
||||
caps = { decode = true, encode = false, patches = true, variants = { "doom3", "quake4" } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
hull: boolean?, -- drop faces inside / touching other brushes of the same entity, default false
|
||||
merge: boolean?, -- merge coplanar faces with the same material and projection, default false
|
||||
epsilon: number?, -- default 1e-3
|
||||
patchLevel: number?, -- patchDef2 segments per sub-patch, default 4
|
||||
skip: { string }?, -- material names left out of the meshes ("textures/common/caulk", ...)
|
||||
maxPrimitives: number?, -- brushes + patches, default 2^20
|
||||
maxPolygons: number?, -- default 2^20
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local Scene = Types.Scene
|
||||
local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function D3map.sniff(r: Bytes.Reader): number
|
||||
local head = Sniff.text(r, 512, true)
|
||||
for _ = 1, 16 do
|
||||
local rest = string.match(head, "^//[^\n]*\n%s*(.*)$")
|
||||
if not rest then
|
||||
break
|
||||
end
|
||||
head = rest
|
||||
end
|
||||
return if string.match(head, "^Version%s+[123]%s") then 1 else 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
type Grid = { pos: { number }, nrm: { number }, uv: { number }, idx: { number } }
|
||||
|
||||
local function side(s: Parse.Side): Brush.Face?
|
||||
local a, b, c, d = s.plane[1], s.plane[2], s.plane[3], s.plane[4]
|
||||
local l = math.sqrt(a * a + b * b + c * c)
|
||||
if l < 1e-9 then
|
||||
return nil
|
||||
end
|
||||
-- a x + b y + c z + d = 0 with the normal outward: inside is n . p <= -d
|
||||
return { plane = { a / l, b / l, c / l, -d / l }, texture = s.material, bp = s.matrix, flags = s.flags }
|
||||
end
|
||||
|
||||
local function addPatch(prim: Parse.Prim, level: number, g: Grid): boolean
|
||||
local w, h = prim.width :: number, prim.height :: number
|
||||
local src = prim.points :: { number }
|
||||
if not Patch.valid(w, h, #src // 5) then
|
||||
return false
|
||||
end
|
||||
-- the file lists the grid column by column; Patch wants rows (point (i, j) at j * w + i)
|
||||
local rows = table.create(w * h * 5, 0) :: { number }
|
||||
for col = 0, w - 1 do
|
||||
for row = 0, h - 1 do
|
||||
table.move(src, (col * h + row) * 5 + 1, (col * h + row) * 5 + 5, (row * w + col) * 5 + 1, rows)
|
||||
end
|
||||
end
|
||||
local info = prim.info :: { number }
|
||||
local explicit = prim.type == "patchDef3"
|
||||
local su: number = if explicit then math.max(1, info[3] or 1) else level
|
||||
local sv: number = if explicit then math.max(1, info[4] or 1) else level
|
||||
local pts, gw, gh = Patch.grid(rows, w, h, 5, su, sv)
|
||||
if not pts then
|
||||
return false
|
||||
end
|
||||
local normals = Patch.normals(pts, 5, gw, gh)
|
||||
local first = #g.pos // 3
|
||||
local P, N, U = g.pos, g.nrm, g.uv
|
||||
for v = 0, gw * gh - 1 do
|
||||
P[#P + 1], P[#P + 2], P[#P + 3] = pts[v * 5 + 1], pts[v * 5 + 2], pts[v * 5 + 3]
|
||||
U[#U + 1], U[#U + 2] = pts[v * 5 + 4], pts[v * 5 + 5]
|
||||
N[#N + 1], N[#N + 2], N[#N + 3] = normals[v * 3 + 1], normals[v * 3 + 2], normals[v * 3 + 3]
|
||||
end
|
||||
local I = g.idx
|
||||
for _, t in Patch.triangles(gw, gh) do
|
||||
I[#I + 1] = first + t
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function gridPrimitive(g: Grid): Types.Primitive
|
||||
local n, ni = #g.pos // 3, #g.idx
|
||||
local bp, bn, bu, bi = buffer.create(n * 12), buffer.create(n * 12), buffer.create(n * 8), buffer.create(ni * 4)
|
||||
for i = 0, n - 1 do
|
||||
for c = 0, 2 do
|
||||
writef32(bp, i * 12 + c * 4, g.pos[i * 3 + c + 1])
|
||||
writef32(bn, i * 12 + c * 4, g.nrm[i * 3 + c + 1])
|
||||
end
|
||||
writef32(bu, i * 8, g.uv[i * 2 + 1])
|
||||
writef32(bu, i * 8 + 4, g.uv[i * 2 + 2])
|
||||
end
|
||||
for i = 0, ni - 1 do
|
||||
writeu32(bi, i * 4, g.idx[i + 1])
|
||||
end
|
||||
local attributes = { POSITION = Scene.accessor(bp, "f32", 3, n), NORMAL = Scene.accessor(bn, "f32", 3, n), TEXCOORD_0 = Scene.accessor(bu, "f32", 2, n) }
|
||||
return { attributes = attributes, indices = Scene.accessor(bi, "u32", 1, ni), mode = "triangles" :: any }
|
||||
end
|
||||
|
||||
local function numbers(s: string?, n: number): { number }?
|
||||
if type(s) ~= "string" then
|
||||
return nil
|
||||
end
|
||||
local out = {}
|
||||
for w in string.gmatch(s, "%S+") do
|
||||
local v = tonumber(w)
|
||||
if not v then
|
||||
return nil
|
||||
end
|
||||
table.insert(out, v)
|
||||
end
|
||||
return if #out == n then out else nil
|
||||
end
|
||||
|
||||
-- "rotation": the 3 x 3 axis matrix by rows (row k = local axis k in the world); "angle": yaw in degrees
|
||||
local function orientation(keys: { [string]: string }): { number }?
|
||||
local m = numbers(keys.rotation, 9)
|
||||
if m then
|
||||
return Mat4.quat({ m[1], m[2], m[3], 0, m[4], m[5], m[6], 0, m[7], m[8], m[9], 0, 0, 0, 0, 1 })
|
||||
end
|
||||
local a = tonumber(keys.angle)
|
||||
if a then
|
||||
local h = math.rad(a) / 2
|
||||
return { 0, 0, math.sin(h), math.cos(h) }
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function D3map.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 m, msg = Parse.parse(text, opts and opts.maxPrimitives)
|
||||
if not m then
|
||||
return nil, Err.from(msg)
|
||||
end
|
||||
if #m.entities == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "no entities")
|
||||
end
|
||||
local skip: { [string]: boolean } = {}
|
||||
for _, s in opts and opts.skip or {} do
|
||||
skip[string.lower(s)] = true
|
||||
end
|
||||
local scene = Scene.new()
|
||||
scene.meta.up = "z"
|
||||
local root: Types.Node = { name = "map", children = {} }
|
||||
scene.nodes[1] = root
|
||||
scene.roots[1] = 1
|
||||
local matIndex: { [string]: number } = {}
|
||||
local function material(nm: string): number
|
||||
local i = matIndex[nm]
|
||||
if not i then
|
||||
table.insert(scene.textures, { name = nm, uri = nm })
|
||||
local mat = Scene.material(nm)
|
||||
mat.metallic = 0
|
||||
mat.baseColorTexture = { texture = #scene.textures }
|
||||
table.insert(scene.materials, mat)
|
||||
i = #scene.materials
|
||||
matIndex[nm] = i
|
||||
end
|
||||
return i
|
||||
end
|
||||
local stats = { brushes = 0, patches = 0, skippedPrimitives = 0, skippedSides = 0, badPatches = 0 }
|
||||
local entities: { { [string]: string } } = {}
|
||||
local total = 0
|
||||
local maxPolys = opts and opts.maxPolygons or 2 ^ 20
|
||||
for ei, ent in m.entities do
|
||||
local keys = ent.keys
|
||||
table.insert(entities, keys)
|
||||
stats.skippedPrimitives += ent.skipped
|
||||
local brushes: { { Brush.Face } } = {}
|
||||
local grids: { [string]: Grid } = {}
|
||||
local gridOrder: { string } = {}
|
||||
for _, prim in ent.prims do
|
||||
if prim.sides then
|
||||
stats.brushes += 1
|
||||
local faces: { Brush.Face } = {}
|
||||
for _, s in prim.sides do
|
||||
local f = side(s)
|
||||
if f then
|
||||
table.insert(faces, f)
|
||||
else
|
||||
stats.skippedSides += 1
|
||||
end
|
||||
end
|
||||
table.insert(brushes, faces)
|
||||
elseif prim.points then
|
||||
stats.patches += 1
|
||||
local mname = prim.material or ""
|
||||
if not skip[string.lower(mname)] then
|
||||
local g = grids[mname]
|
||||
if not g then
|
||||
g = { pos = {}, nrm = {}, uv = {}, idx = {} }
|
||||
grids[mname] = g
|
||||
table.insert(gridOrder, mname)
|
||||
end
|
||||
if not addPatch(prim, opts and opts.patchLevel or 4, g :: Grid) then
|
||||
stats.badPatches += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local prims: { Types.Primitive } = {}
|
||||
if #brushes > 0 then
|
||||
local polys, perr = Brush.polygons(brushes, { hull = opts and opts.hull, merge = opts and opts.merge, epsilon = opts and opts.epsilon, maxPolygons = maxPolys })
|
||||
if not polys then
|
||||
return nil, Err.from(perr)
|
||||
end
|
||||
total += #polys
|
||||
if total > maxPolys then
|
||||
return nil, Err.new(Err.LIMIT, "too many polygons")
|
||||
end
|
||||
local keep: { Brush.Polygon } = {}
|
||||
for _, poly in polys do
|
||||
if not skip[string.lower(brushes[poly.brush][poly.face].texture or "")] then
|
||||
table.insert(keep, poly)
|
||||
end
|
||||
end
|
||||
-- Doom 3 texture matrices are in texture widths already: the size divides nothing
|
||||
local function unit(_: string): (number, number)
|
||||
return 1, 1
|
||||
end
|
||||
for _, built in Brush.primitives(keep, brushes, unit) do
|
||||
built.primitive.material = material(built.texture)
|
||||
built.primitive.extras = { d3map = { material = built.texture } }
|
||||
table.insert(prims, built.primitive)
|
||||
end
|
||||
end
|
||||
for _, mname in gridOrder do
|
||||
local g = grids[mname]
|
||||
if #g.idx > 0 then
|
||||
local prim = gridPrimitive(g)
|
||||
prim.material = material(mname)
|
||||
prim.extras = { d3map = { material = mname, patch = true } }
|
||||
table.insert(prims, prim)
|
||||
end
|
||||
end
|
||||
local node: Types.Node = { name = keys.name or keys.classname or ("entity " .. (ei - 1)), children = {}, extras = { classname = keys.classname, keys = keys, entity = ei - 1 } }
|
||||
if #prims > 0 then
|
||||
table.insert(scene.meshes, { name = node.name, primitives = prims })
|
||||
node.mesh = #scene.meshes
|
||||
elseif #ent.prims == 0 then
|
||||
node.translation = numbers(keys.origin, 3)
|
||||
node.rotation = orientation(keys)
|
||||
end
|
||||
table.insert(scene.nodes, node)
|
||||
table.insert(root.children, #scene.nodes)
|
||||
end
|
||||
scene.meta.entities = entities
|
||||
scene.meta.counts = stats
|
||||
scene.meta.source = { format = "d3map", version = m.version, variant = if m.version >= 3 then "quake4" else "doom3", entities = #m.entities, brushes = stats.brushes, patches = stats.patches }
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return D3map
|
||||
Reference in new issue
Block a user