model/bsp: BSP2 / 2PSB, Quake 1 / 2 / Half-Life lightmap atlas (TEXCOORD_1, .lit), tree / hulls / visibility in meta.bsp
Bsp.leafAt / contents / pvs point queries; tested on ericw-tools qbsp + vis output in all three Quake 1 layouts.
This commit is contained in:
1 parent
2704653a3e
commit
67e116a6c6
13 files changed
+2556
-375
No files matched your search
+654
-30
@@ -1611,7 +1611,7 @@ local list: { Entry } = {
|
||||
extensions = { "bsp" },
|
||||
mime = { "model/x-bsp" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/image/Miptex", "codecs/model/Patch" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "halflife", "quake2", "quake3" }, lightmaps = "quake3" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "bsp2", "2psb", "halflife", "quake2", "quake3" }, lightmaps = true, collision = true, visibility = true },
|
||||
load = function()
|
||||
return __include("model/bsp")
|
||||
end,
|
||||
@@ -145494,17 +145494,20 @@ return Read
|
||||
end
|
||||
__FILES["model/bsp"] = function()
|
||||
|
||||
-- model/bsp: compiled game maps, read: Quake 1 (BSP 29), Half-Life (BSP 30), Quake II (IBSP 38), Quake III / RTCW
|
||||
-- (IBSP 46 / 47). Quake textures need the game's palette (gfx/palette.lmp, not redistributable). Map units, z up.
|
||||
-- Not supported: Source engine VBSP, Quake 1/2 lightmaps, visibility and collision data, writing.
|
||||
-- model/bsp: compiled game maps, read: Quake 1 (BSP 29, BSP2, 2PSB), Half-Life (BSP 30), Quake II (IBSP 38), Quake III
|
||||
-- / RTCW (IBSP 46 / 47). Lightmaps (Quake 1 / 2 / Half-Life: one atlas, Quake III: its pages) as TEXCOORD_1; the tree,
|
||||
-- collision hulls / brushes and visibility in meta.bsp with point queries (Bsp.leafAt / contents / pvs). Quake textures
|
||||
-- need the game's palette (gfx/palette.lmp, not redistributable). Map units, z up. Not supported: Source engine VBSP,
|
||||
-- light styles other than the first, BSPX lumps, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = __include("core/Err")
|
||||
local Bytes = __include("core/Bytes")
|
||||
local Input = __include("core/Input")
|
||||
local Types = __include("Types")
|
||||
local Brush = __include("model/bsp/Brush")
|
||||
local Quake = __include("model/bsp/Quake")
|
||||
local Quake3 = __include("model/bsp/Quake3")
|
||||
local Tree = __include("model/bsp/Tree")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Bsp = {
|
||||
@@ -145513,7 +145516,7 @@ local Bsp = {
|
||||
extensions = { "bsp" },
|
||||
mime = { "model/x-bsp" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/image/Miptex", "codecs/model/Patch" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "halflife", "quake2", "quake3" }, lightmaps = "quake3" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "bsp2", "2psb", "halflife", "quake2", "quake3" }, lightmaps = true, collision = true, visibility = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
@@ -145522,6 +145525,10 @@ type DecodeOptions = {
|
||||
resolve: ((uri: string) -> (buffer | string)?)?,
|
||||
files: { [string]: buffer | string }?,
|
||||
patchLevel: number?, -- default 4
|
||||
lit: (buffer | string)?, -- Quake 1: the map's .lit file (coloured light); also found as "<map>.lit" through files / resolve
|
||||
lightmaps: boolean?, -- default true
|
||||
tree: boolean?, -- meta.bsp (tree, hulls, visibility), default true
|
||||
name: string?, -- the file name (finds the .lit)
|
||||
maxPixels: number?, -- default 2^24
|
||||
maxVertices: number?, -- default 2^24
|
||||
}
|
||||
@@ -145541,6 +145548,9 @@ function Bsp.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
local m = r:peek(4)
|
||||
local v = buffer.readi32(r.buf, r.pos + (if m == "IBSP" then 4 else 0))
|
||||
if m == "BSP2" or m == "2PSB" then
|
||||
v = 29
|
||||
end
|
||||
if m == "IBSP" then
|
||||
return if v == 38 or v == 46 or v == 47 then 1 else 0.3
|
||||
end
|
||||
@@ -145583,6 +145593,8 @@ function Bsp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
names, dir, variant = Q2_LUMPS, 8, "q2"
|
||||
elseif m == "IBSP" and (version == 46 or version == 47) then
|
||||
names, dir, variant = Q3_LUMPS, 8, "q3"
|
||||
elseif m == "BSP2" or m == "2PSB" then
|
||||
names, dir, variant = Q1_LUMPS, 4, string.lower(m)
|
||||
elseif m ~= "IBSP" and (version == 29 or version == 30) then
|
||||
names, dir, variant = Q1_LUMPS, 4, if version == 30 then "hl" else "q1"
|
||||
elseif m == "VBSP" then
|
||||
@@ -145609,13 +145621,15 @@ function Bsp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
if pal and buffer.len(pal) < 768 then
|
||||
pal = nil
|
||||
end
|
||||
scene, msg = Brush.decode(b, base, lumps, variant, {
|
||||
scene, msg = Quake.decode(b, base, lumps, variant, {
|
||||
palette = pal,
|
||||
wal = function(name: string): buffer?
|
||||
return Input.sibling(opts, "textures/" .. name .. ".wal")
|
||||
end,
|
||||
maxPixels = opts and opts.maxPixels,
|
||||
maxVertices = opts and opts.maxVertices,
|
||||
lit = if opts and opts.lit then Bytes.toBuffer(opts.lit) else (if opts and opts.name then Input.sibling(opts, (string.gsub(opts.name :: string, "%.[^./\\]*$", "")) .. ".lit") else nil),
|
||||
lightmaps = opts and opts.lightmaps,
|
||||
})
|
||||
end
|
||||
if not scene then
|
||||
@@ -145627,29 +145641,129 @@ function Bsp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
scene.meta.entities = entities(text)
|
||||
scene.meta.source = {
|
||||
format = "bsp",
|
||||
variant = ({ q1 = "quake", hl = "halflife", q2 = "quake2", q3 = "quake3" } :: { [string]: string })[variant],
|
||||
variant = ({ q1 = "quake", bsp2 = "quake", ["2psb"] = "quake", hl = "halflife", q2 = "quake2", q3 = "quake3" } :: { [string]: string })[variant],
|
||||
layout = if variant == "bsp2" then "BSP2" elseif variant == "2psb" then "2PSB" else nil,
|
||||
version = version,
|
||||
entities = text,
|
||||
palette = if variant == "q1" then (opts ~= nil and (opts.palette ~= nil or Input.sibling(opts, "gfx/palette.lmp") ~= nil)) else nil,
|
||||
palette = if variant == "q1" or variant == "bsp2" or variant == "2psb" then (opts ~= nil and (opts.palette ~= nil or Input.sibling(opts, "gfx/palette.lmp") ~= nil)) else nil,
|
||||
}
|
||||
if not (opts and opts.tree == false) then
|
||||
local tree, terr = Tree.read(b, base, lumps, variant)
|
||||
if not tree then
|
||||
return nil, Err.from(terr)
|
||||
end
|
||||
scene.meta.bsp = tree
|
||||
end
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
local function tree(scene: any): Tree.Data?
|
||||
return type(scene) == "table" and type(scene.meta) == "table" and scene.meta.bsp or nil
|
||||
end
|
||||
|
||||
-- the leaf (0-based) at a point, in the tree of a model (0 = the world)
|
||||
function Bsp.leafAt(scene: Types.Scene, x: number, y: number, z: number, model: number?): number?
|
||||
local d = tree(scene)
|
||||
return if d then Tree.leafAt(d, x, y, z, model) else nil
|
||||
end
|
||||
|
||||
-- the contents at a point: Quake 1 CONTENTS_* (-1 empty, -2 solid, ...; hull 0 point, 1 player, 2 shambler, 3 crouch
|
||||
-- in Half-Life), Quake II / III contents bits
|
||||
function Bsp.contents(scene: Types.Scene, x: number, y: number, z: number, hull: number?, model: number?): number?
|
||||
local d = tree(scene)
|
||||
return if d then Tree.contents(d, x, y, z, hull, model) else nil
|
||||
end
|
||||
|
||||
-- the leaves (0-based) potentially visible from a leaf; nil when the map has no visibility data
|
||||
function Bsp.pvs(scene: Types.Scene, leaf: number): { number }?
|
||||
local d = tree(scene)
|
||||
return if d then Tree.pvs(d, leaf) else nil
|
||||
end
|
||||
|
||||
return Bsp
|
||||
end
|
||||
__FILES["model/bsp/Brush"] = function()
|
||||
__FILES["model/bsp/Lightmap"] = function()
|
||||
|
||||
-- model/bsp/Brush: Quake 1 (BSP 29), Half-Life (BSP 30) and Quake II (IBSP 38) maps -> Scene, from bspfile.h of the
|
||||
-- Quake / Quake II source releases (no code taken). Texture coordinates: s = v . s.xyz + s.w, divided by the texture
|
||||
-- size.
|
||||
-- model/bsp/Lightmap: packs face lightmaps (w x h luxels each) into one atlas, shelf by shelf, tallest first, each
|
||||
-- with a one-luxel border that repeats its edge (bilinear filtering stays inside the face).
|
||||
local Lightmap = {}
|
||||
|
||||
type Rect = { x: number, y: number, w: number, h: number } -- x, y: the first luxel inside the border
|
||||
|
||||
-- nil, message past maxPixels
|
||||
function Lightmap.pack(sizes: { { number } }, maxPixels: number): (number, number, { Rect }?, string?)
|
||||
local area, widest = 0, 1
|
||||
for _, s in sizes do
|
||||
area += (s[1] + 2) * (s[2] + 2)
|
||||
widest = math.max(widest, s[1] + 2)
|
||||
end
|
||||
local W = 64
|
||||
while W * W < area * 1.2 and W < 4096 do
|
||||
W *= 2
|
||||
end
|
||||
W = math.max(W, widest)
|
||||
local order = table.create(#sizes) :: { number }
|
||||
for i = 1, #sizes do
|
||||
order[i] = i
|
||||
end
|
||||
table.sort(order, function(a: number, b: number): boolean
|
||||
local ha, hb = sizes[a][2], sizes[b][2]
|
||||
if ha ~= hb then
|
||||
return ha > hb
|
||||
end
|
||||
return a < b
|
||||
end)
|
||||
local out = table.create(#sizes) :: { Rect }
|
||||
local x, y, rowH = 0, 0, 0
|
||||
for _, i in order do
|
||||
local w, h = sizes[i][1] + 2, sizes[i][2] + 2
|
||||
if x + w > W then
|
||||
x, y, rowH = 0, y + rowH, 0
|
||||
end
|
||||
out[i] = { x = x + 1, y = y + 1, w = sizes[i][1], h = sizes[i][2] }
|
||||
x += w
|
||||
rowH = math.max(rowH, h)
|
||||
end
|
||||
local H = math.max(1, y + rowH)
|
||||
if W * H > maxPixels then
|
||||
return 0, 0, nil, "limit: lightmap atlas " .. W .. " x " .. H
|
||||
end
|
||||
return W, H, out, nil
|
||||
end
|
||||
|
||||
-- copies the outermost luxels of a rect into its border (rgba8 pixels, atlas width W)
|
||||
function Lightmap.border(px: buffer, W: number, r: Rect)
|
||||
local function copy(dx: number, dy: number, sx: number, sy: number)
|
||||
buffer.writeu32(px, (dy * W + dx) * 4, buffer.readu32(px, (sy * W + sx) * 4))
|
||||
end
|
||||
for yy = r.y, r.y + r.h - 1 do
|
||||
copy(r.x - 1, yy, r.x, yy)
|
||||
copy(r.x + r.w, yy, r.x + r.w - 1, yy)
|
||||
end
|
||||
for xx = r.x - 1, r.x + r.w do
|
||||
copy(xx, r.y - 1, xx, r.y)
|
||||
copy(xx, r.y + r.h, xx, r.y + r.h - 1)
|
||||
end
|
||||
end
|
||||
|
||||
return Lightmap
|
||||
end
|
||||
__FILES["model/bsp/Quake"] = function()
|
||||
|
||||
-- model/bsp/Quake: Quake 1 (BSP 29, BSP2, 2PSB), Half-Life (BSP 30) and Quake II (IBSP 38) maps -> Scene, from
|
||||
-- bspfile.h of the Quake / Quake II source releases and ericw-tools (BSP2 / 2PSB; no code taken). Texture coordinates:
|
||||
-- s = v . s.xyz + s.w over the texture size; lightmaps (style 0) packed into one atlas as TEXCOORD_1, extents as
|
||||
-- CalcSurfaceExtents (16 texels per luxel).
|
||||
local Types = __include("Types")
|
||||
local Pixels = __include("codecs/image/Pixels")
|
||||
local Lightmap = __include("model/bsp/Lightmap")
|
||||
local Miptex = __include("codecs/image/Miptex")
|
||||
|
||||
local Brush = {}
|
||||
local Quake = {}
|
||||
|
||||
local Scene = Types.Scene
|
||||
local readi32, readu16, readf32, readu32 = buffer.readi32, buffer.readu16, buffer.readf32, buffer.readu32
|
||||
local readi32, readu16, readf32, readu32, readu8 = buffer.readi32, buffer.readu16, buffer.readf32, buffer.readu32, buffer.readu8
|
||||
local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
type Lump = { at: number, size: number }
|
||||
@@ -145658,10 +145772,12 @@ type Options = {
|
||||
wal: ((name: string) -> buffer?)?, -- Quake II: a texture's .wal file (for its size)
|
||||
maxPixels: number?,
|
||||
maxVertices: number?,
|
||||
lit: buffer?, -- Quake 1 coloured light (.lit: "QLIT", version 1, RGB per luxel)
|
||||
lightmaps: boolean?, -- default true
|
||||
}
|
||||
|
||||
type Tex = { name: string, width: number, height: number, image: Types.Image?, texels: boolean }
|
||||
type Batch = { tex: number, pos: { number }, nrm: { number }, uv: { number } }
|
||||
type Batch = { tex: number, pos: { number }, nrm: { number }, uv: { number }, lm: { number } } -- lm: face, s, t per vertex
|
||||
|
||||
local function cstr(b: buffer, at: number, n: number): string
|
||||
return (string.match(buffer.readstring(b, at, n), "^[^%z]*")) :: string
|
||||
@@ -145698,11 +145814,14 @@ local function miptex(b: buffer, base: number, l: Lump, hl: boolean, pal: buffer
|
||||
return out, nil
|
||||
end
|
||||
|
||||
function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, variant: string, opts: Options?): (Types.Scene?, string?)
|
||||
function Quake.decode(b: buffer, base: number, lumps: { [string]: Lump }, variant: string, opts: Options?): (Types.Scene?, string?)
|
||||
local q2 = variant == "q2"
|
||||
local big = variant == "bsp2" or variant == "2psb"
|
||||
local maxPixels = opts and opts.maxPixels or 2 ^ 24
|
||||
local V, E, SE, Fc, TI, P, M = lumps.vertices, lumps.edges, lumps.surfedges, lumps.faces, lumps.texinfo, lumps.planes, lumps.models
|
||||
local nV, nE, nSE, nF = V.size // 12, E.size // 4, SE.size // 4, Fc.size // 20
|
||||
local fSize: number = if big then 28 else 20
|
||||
local eSize: number = if big then 8 else 4
|
||||
local nV, nE, nSE, nF = V.size // 12, E.size // eSize, SE.size // 4, Fc.size // fSize
|
||||
local tiSize = if q2 then 76 else 40
|
||||
local nTI, nP = TI.size // tiSize, P.size // 20
|
||||
local mSize = if q2 then 48 else 64
|
||||
@@ -145765,11 +145884,28 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
local batches: { [number]: { [number]: Batch } } = {}
|
||||
local orders: { [number]: { number } } = {}
|
||||
local skipped = 0
|
||||
-- lightmaps: record 1 = a white luxel (sky / liquids), 2 = black (unlit faces), then one per lit face
|
||||
local LT = lumps.lighting
|
||||
local useLight = LT.size > 0 and not (opts and opts.lightmaps == false)
|
||||
local rgb = variant == "hl" or q2
|
||||
local lit = opts and opts.lit
|
||||
if lit and (buffer.len(lit) < 8 or buffer.readstring(lit, 0, 4) ~= "QLIT" or buffer.readi32(lit, 4) ~= 1) then
|
||||
lit = nil
|
||||
end
|
||||
local lmSizes: { { number } } = { { 1, 1 }, { 1, 1 } }
|
||||
local lmFaces: { { face: number, ofs: number, smin: number, tmin: number, styles: number } } = { { face = -1, ofs = -1, smin = 0, tmin = 0, styles = 0 }, { face = -1, ofs = -1, smin = 0, tmin = 0, styles = 0 } }
|
||||
local lumaTotal = 0
|
||||
for f = 0, nF - 1 do
|
||||
local fo = base + Fc.at + f * 20
|
||||
local plane, side, firstEdge, numEdges, texinfo = readu16(b, fo), readu16(b, fo + 2), readi32(b, fo + 4), readu16(b, fo + 8), readu16(b, fo + 10)
|
||||
local fo = base + Fc.at + f * fSize
|
||||
local plane = if big then readi32(b, fo) else readu16(b, fo)
|
||||
local side = if big then readi32(b, fo + 4) else readu16(b, fo + 2)
|
||||
local firstEdge = if big then readi32(b, fo + 8) else readi32(b, fo + 4)
|
||||
local numEdges = if big then readi32(b, fo + 12) else readu16(b, fo + 8)
|
||||
local texinfo = if big then readi32(b, fo + 16) else readu16(b, fo + 10)
|
||||
local styles = if big then fo + 20 else fo + 12
|
||||
local lightofs = if big then readi32(b, fo + 24) else readi32(b, fo + 16)
|
||||
local model = faceModel[f + 1]
|
||||
if not model or numEdges < 3 or firstEdge < 0 or firstEdge + numEdges > nSE or plane >= nP or texinfo >= nTI then
|
||||
if not model or numEdges < 3 or plane < 0 or texinfo < 0 or firstEdge < 0 or firstEdge + numEdges > nSE or plane >= nP or texinfo >= nTI then
|
||||
skipped += 1
|
||||
continue
|
||||
end
|
||||
@@ -145782,7 +145918,13 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
ok = false
|
||||
break
|
||||
end
|
||||
local v = if se >= 0 then readu16(b, base + E.at + e * 4) else readu16(b, base + E.at + e * 4 + 2)
|
||||
local eo = base + E.at + e * eSize
|
||||
local v
|
||||
if big then
|
||||
v = if se >= 0 then readu32(b, eo) else readu32(b, eo + 4)
|
||||
else
|
||||
v = if se >= 0 then readu16(b, eo) else readu16(b, eo + 2)
|
||||
end
|
||||
if v >= nV then
|
||||
ok = false
|
||||
break
|
||||
@@ -145826,6 +145968,37 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
local tio = base + TI.at + texinfo * tiSize
|
||||
local sx, sy, sz, so = readf32(b, tio), readf32(b, tio + 4), readf32(b, tio + 8), readf32(b, tio + 12)
|
||||
local tx, ty, tz, to = readf32(b, tio + 16), readf32(b, tio + 20), readf32(b, tio + 24), readf32(b, tio + 28)
|
||||
local rec = 2
|
||||
if useLight then
|
||||
local tflags = readi32(b, tio + (if q2 then 32 else 36))
|
||||
local special = if q2 then bit32.band(tflags, 0x8C) ~= 0 else bit32.band(tflags, 1) ~= 0
|
||||
if special then
|
||||
rec = 1
|
||||
elseif lightofs >= 0 then
|
||||
local smin: number, smax: number, tmin: number, tmax: number = math.huge, -math.huge, math.huge, -math.huge
|
||||
for k = 0, n - 1 do
|
||||
local x, y, z = poly[k * 3 + 1], poly[k * 3 + 2], poly[k * 3 + 3]
|
||||
local sv, tv = x * sx + y * sy + z * sz + so, x * tx + y * ty + z * tz + to
|
||||
smin, smax = math.min(smin, sv), math.max(smax, sv)
|
||||
tmin, tmax = math.min(tmin, tv), math.max(tmax, tv)
|
||||
end
|
||||
local bs, bt0 = math.floor(smin / 16), math.floor(tmin / 16)
|
||||
local lw, lh = math.ceil((smax :: number) / 16) - bs + 1, math.ceil((tmax :: number) / 16) - bt0 + 1
|
||||
local ns = 0
|
||||
for k = 0, 3 do
|
||||
if readu8(b, styles + k) ~= 255 then
|
||||
ns += 1
|
||||
end
|
||||
end
|
||||
local bytes = lw * lh * (if rgb then 3 else 1)
|
||||
lumaTotal += lw * lh
|
||||
if ns > 0 and lw > 0 and lh > 0 and lw <= 256 and lh <= 256 and lightofs + bytes <= LT.size and lumaTotal <= maxPixels then
|
||||
table.insert(lmSizes, { lw, lh })
|
||||
table.insert(lmFaces, { face = f, ofs = lightofs, smin = bs * 16, tmin = bt0 * 16, styles = ns })
|
||||
rec = #lmFaces
|
||||
end
|
||||
end
|
||||
end
|
||||
local ti = texOf[texinfo + 1] or 0
|
||||
local tex = texs[ti]
|
||||
local w, h = if tex then tex.width else 1, if tex then tex.height else 1
|
||||
@@ -145833,7 +146006,7 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
batches[model] = bm
|
||||
local bt = bm[ti]
|
||||
if not bt then
|
||||
bt = { tex = ti, pos = {}, nrm = {}, uv = {} }
|
||||
bt = { tex = ti, pos = {}, nrm = {}, uv = {}, lm = {} }
|
||||
bm[ti] = bt
|
||||
local ol = orders[model] or {}
|
||||
orders[model] = ol
|
||||
@@ -145846,21 +146019,69 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
local pp, nn, uu = batch.pos, batch.nrm, batch.uv
|
||||
pp[#pp + 1], pp[#pp + 2], pp[#pp + 3] = x, y, z
|
||||
nn[#nn + 1], nn[#nn + 2], nn[#nn + 3] = nx, ny, nz
|
||||
uu[#uu + 1] = (x * sx + y * sy + z * sz + so) / w
|
||||
uu[#uu + 1] = (x * tx + y * ty + z * tz + to) / h
|
||||
local sv, tv = x * sx + y * sy + z * sz + so, x * tx + y * ty + z * tz + to
|
||||
uu[#uu + 1] = sv / w
|
||||
uu[#uu + 1] = tv / h
|
||||
local ll = batch.lm
|
||||
ll[#ll + 1], ll[#ll + 2], ll[#ll + 3] = rec, sv, tv
|
||||
end
|
||||
end
|
||||
end
|
||||
local atlasW, atlasH, rects = 0, 0, nil :: { Lightmap.Rect }?
|
||||
local lmTex: number? = nil
|
||||
local atlas: Types.Image? = nil
|
||||
if useLight and #lmFaces > 2 then
|
||||
local W, H, rs, err = Lightmap.pack(lmSizes, maxPixels)
|
||||
if not rs then
|
||||
return nil, err
|
||||
end
|
||||
atlasW, atlasH, rects = W, H, rs
|
||||
local img = Types.Image.new(W, H)
|
||||
local px = img.pixels
|
||||
local la = base + LT.at
|
||||
buffer.writeu32(px, (rs[1].y * W + rs[1].x) * 4, 0xFFFFFFFF)
|
||||
buffer.writeu32(px, (rs[2].y * W + rs[2].x) * 4, 0xFF000000)
|
||||
local placed = {}
|
||||
for i = 3, #lmFaces do
|
||||
local lf, r = lmFaces[i], rs[i]
|
||||
for yy = 0, r.h - 1 do
|
||||
for xx = 0, r.w - 1 do
|
||||
local k = yy * r.w + xx
|
||||
local cr: number, cg: number, cb: number = 0, 0, 0
|
||||
if rgb then
|
||||
local o = la + lf.ofs + k * 3
|
||||
cr, cg, cb = readu8(b, o), readu8(b, o + 1), readu8(b, o + 2)
|
||||
elseif lit and 8 + (lf.ofs + k) * 3 + 3 <= buffer.len(lit) then
|
||||
local o = 8 + (lf.ofs + k) * 3
|
||||
cr, cg, cb = readu8(lit, o), readu8(lit, o + 1), readu8(lit, o + 2)
|
||||
else
|
||||
local v = readu8(b, la + lf.ofs + k)
|
||||
cr, cg, cb = v, v, v
|
||||
end
|
||||
writeu32(px, ((r.y + yy) * W + r.x + xx) * 4, 0xFF000000 + (cb :: number) * 65536 + (cg :: number) * 256 + (cr :: number))
|
||||
end
|
||||
end
|
||||
table.insert(placed, { face = lf.face, x = r.x, y = r.y, w = r.w, h = r.h, styles = lf.styles })
|
||||
end
|
||||
for i = 1, #lmFaces do
|
||||
Lightmap.border(px, W, rs[i])
|
||||
end
|
||||
img.meta.source = { format = "bsp", lightmap = "atlas", coloured = rgb or lit ~= nil }
|
||||
atlas = img
|
||||
scene.meta.lightmaps = placed
|
||||
lmTex = #texs + 1
|
||||
end
|
||||
for i, t in texs do
|
||||
local tex: Types.Texture = { name = t.name, image = t.image, uri = if t.image then nil else (if q2 then "textures/" .. t.name .. ".wal" else t.name) }
|
||||
table.insert(scene.textures, tex)
|
||||
local mat = Scene.material(t.name)
|
||||
mat.metallic = 0
|
||||
local extras: any = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height } }
|
||||
mat.extras = extras
|
||||
mat.extras = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height } } :: any
|
||||
mat.extras = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height, lightmap = lmTex, lightmapTexCoord = if lmTex then 1 else nil } } :: any
|
||||
table.insert(scene.materials, mat)
|
||||
end
|
||||
if atlas then
|
||||
table.insert(scene.textures, { name = "lightmap", image = atlas })
|
||||
end
|
||||
local root: Types.Node = { name = "bsp", children = {} }
|
||||
table.insert(scene.nodes, root)
|
||||
scene.roots[1] = 1
|
||||
@@ -145882,8 +146103,23 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
for k = 0, nv - 1 do
|
||||
writeu32(ib, k * 4, k)
|
||||
end
|
||||
local attrs: { [string]: Types.Accessor } = { POSITION = Scene.accessor(pb, "f32", 3, nv), NORMAL = Scene.accessor(nb, "f32", 3, nv), TEXCOORD_0 = Scene.accessor(ub, "f32", 2, nv) }
|
||||
if rects then
|
||||
local lb = buffer.create(nv * 8)
|
||||
for k = 0, nv - 1 do
|
||||
local rec, sv, tv = bt.lm[k * 3 + 1], bt.lm[k * 3 + 2], bt.lm[k * 3 + 3]
|
||||
local r, lf = rects[rec], lmFaces[rec]
|
||||
local lu, lv = r.x + 0.5, r.y + 0.5
|
||||
if rec > 2 then
|
||||
lu, lv = r.x + (sv - lf.smin) / 16 + 0.5, r.y + (tv - lf.tmin) / 16 + 0.5
|
||||
end
|
||||
writef32(lb, k * 8, lu / atlasW)
|
||||
writef32(lb, k * 8 + 4, lv / atlasH)
|
||||
end
|
||||
attrs.TEXCOORD_1 = Scene.accessor(lb, "f32", 2, nv)
|
||||
end
|
||||
table.insert(mesh.primitives, {
|
||||
attributes = { POSITION = Scene.accessor(pb, "f32", 3, nv), NORMAL = Scene.accessor(nb, "f32", 3, nv), TEXCOORD_0 = Scene.accessor(ub, "f32", 2, nv) },
|
||||
attributes = attrs,
|
||||
indices = Scene.accessor(ib, "u32", 1, nv),
|
||||
mode = "triangles" :: any,
|
||||
material = if ti > 0 then ti else nil,
|
||||
@@ -145898,7 +146134,7 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Brush
|
||||
return Quake
|
||||
end
|
||||
__FILES["model/bsp/Quake3"] = function()
|
||||
|
||||
@@ -146128,6 +146364,394 @@ end
|
||||
|
||||
return Quake3
|
||||
end
|
||||
__FILES["model/bsp/Tree"] = function()
|
||||
|
||||
-- model/bsp/Tree: the BSP tree, collision hulls and visibility of Quake 1 / BSP2 / 2PSB / Half-Life / Quake II / Quake
|
||||
-- III maps as tables (bspfile.h layouts; Unofficial Quake 3 Map Specs), plus point queries: the leaf and contents at a
|
||||
-- point (Quake 1 hulls 0..3 through nodes / clipnodes as SV_HullPointContents; Quake II leaf contents; Quake III the
|
||||
-- contents of the leaf's brushes) and the potentially visible set of a leaf (run-length zero bytes, as Mod_DecompressVis).
|
||||
local Tree = {}
|
||||
|
||||
local readi32, readu16, readi16, readf32, readu8, readu32 = buffer.readi32, buffer.readu16, buffer.readi16, buffer.readf32, buffer.readu8, buffer.readu32
|
||||
|
||||
type Lump = { at: number, size: number }
|
||||
type Plane = { normal: { number }, dist: number, type: number? }
|
||||
type Node = { plane: number, children: { number }, mins: { number }, maxs: { number }, firstFace: number?, numFaces: number? } -- children: >= 0 node, < 0 leaf -(child + 1)
|
||||
type Leaf = {
|
||||
contents: number?,
|
||||
cluster: number?,
|
||||
area: number?,
|
||||
visofs: number?,
|
||||
mins: { number },
|
||||
maxs: { number },
|
||||
firstFace: number?,
|
||||
numFaces: number?,
|
||||
firstBrush: number?,
|
||||
numBrushes: number?,
|
||||
ambient: { number }?,
|
||||
}
|
||||
type ClipNode = { plane: number, children: { number } } -- children: >= 0 clipnode, < 0 contents
|
||||
type Model = { mins: { number }, maxs: { number }, origin: { number }?, headnodes: { number }, visleafs: number?, firstFace: number, numFaces: number }
|
||||
type Brush = { firstSide: number, numSides: number, contents: number }
|
||||
type Side = { plane: number, texinfo: number }
|
||||
type Data = {
|
||||
variant: string,
|
||||
planes: { Plane },
|
||||
nodes: { Node },
|
||||
leaves: { Leaf },
|
||||
clipnodes: { ClipNode }?,
|
||||
models: { Model },
|
||||
brushes: { Brush }?,
|
||||
brushSides: { Side }?,
|
||||
leafBrushes: { number }?,
|
||||
leafFaces: { number }?,
|
||||
visibility: buffer?, -- the raw lump
|
||||
numClusters: number?,
|
||||
clusterBytes: number?, -- Quake III bytes per cluster vector
|
||||
}
|
||||
|
||||
local function v3(b: buffer, o: number, kind: string): { number }
|
||||
if kind == "f" then
|
||||
return { readf32(b, o), readf32(b, o + 4), readf32(b, o + 8) }
|
||||
elseif kind == "i" then
|
||||
return { readi32(b, o), readi32(b, o + 4), readi32(b, o + 8) }
|
||||
end
|
||||
return { readi16(b, o), readi16(b, o + 2), readi16(b, o + 4) }
|
||||
end
|
||||
|
||||
local function slice(b: buffer, base: number, l: Lump): buffer?
|
||||
if l.size <= 0 then
|
||||
return nil
|
||||
end
|
||||
local out = buffer.create(l.size)
|
||||
buffer.copy(out, 0, b, base + l.at, l.size)
|
||||
return out
|
||||
end
|
||||
|
||||
-- nil, message past maxItems
|
||||
function Tree.read(b: buffer, base: number, lumps: { [string]: Lump }, variant: string, maxItems: number?): (Data?, string?)
|
||||
local limit = maxItems or 2 ^ 22
|
||||
local q2, q3 = variant == "q2", variant == "q3"
|
||||
local bsp2, rmq = variant == "bsp2", variant == "2psb"
|
||||
local d: Data = { variant = variant, planes = {}, nodes = {}, leaves = {}, models = {} }
|
||||
local function count(l: Lump?, size: number): number
|
||||
return if l then l.size // size else 0
|
||||
end
|
||||
local P = lumps.planes
|
||||
local pSize = if q3 then 16 else 20
|
||||
local np = count(P, pSize)
|
||||
if np > limit then
|
||||
return nil, "limit: too many planes"
|
||||
end
|
||||
for i = 0, np - 1 do
|
||||
local o = base + P.at + i * pSize
|
||||
d.planes[i + 1] = { normal = v3(b, o, "f"), dist = readf32(b, o + 12), type = if q3 then nil else readi32(b, o + 16) }
|
||||
end
|
||||
local N = lumps.nodes
|
||||
local nSize = if q3 then 36 elseif q2 then 28 elseif bsp2 then 44 elseif rmq then 32 else 24
|
||||
local nn = count(N, nSize)
|
||||
if nn > limit then
|
||||
return nil, "limit: too many nodes"
|
||||
end
|
||||
for i = 0, nn - 1 do
|
||||
local o = base + N.at + i * nSize
|
||||
local node: any
|
||||
if q3 or q2 or bsp2 or rmq then
|
||||
local bk = if q3 then "i" elseif bsp2 then "f" else "s"
|
||||
local bs = if bk == "s" then 6 else 12
|
||||
node = { plane = readi32(b, o), children = { readi32(b, o + 4), readi32(b, o + 8) }, mins = v3(b, o + 12, bk), maxs = v3(b, o + 12 + bs, bk) }
|
||||
if not q3 then
|
||||
local fo = o + 12 + bs * 2
|
||||
if q2 then
|
||||
node.firstFace, node.numFaces = readu16(b, fo), readu16(b, fo + 2)
|
||||
else
|
||||
node.firstFace, node.numFaces = readu32(b, fo), readu32(b, fo + 4)
|
||||
end
|
||||
end
|
||||
else
|
||||
local c = {}
|
||||
for k = 0, 1 do
|
||||
local p = readu16(b, o + 4 + k * 2)
|
||||
c[k + 1] = if p < nn then p else -(65535 - p) - 1 -- Quakespasm Mod_LoadNodes_S: leaf 65535 - p
|
||||
end
|
||||
node = { plane = readi32(b, o), children = c, mins = v3(b, o + 8, "s"), maxs = v3(b, o + 14, "s"), firstFace = readu16(b, o + 20), numFaces = readu16(b, o + 22) }
|
||||
end
|
||||
d.nodes[i + 1] = node
|
||||
end
|
||||
local L = lumps.leaves or lumps.leafs
|
||||
local lSize = if q3 then 48 elseif q2 then 28 elseif bsp2 then 44 elseif rmq then 32 else 28
|
||||
local nl = count(L, lSize)
|
||||
if nl > limit then
|
||||
return nil, "limit: too many leaves"
|
||||
end
|
||||
for i = 0, nl - 1 do
|
||||
local o = base + L.at + i * lSize
|
||||
local leaf: any
|
||||
if q3 then
|
||||
leaf = { cluster = readi32(b, o), area = readi32(b, o + 4), mins = v3(b, o + 8, "i"), maxs = v3(b, o + 20, "i"),
|
||||
firstFace = readi32(b, o + 32), numFaces = readi32(b, o + 36), firstBrush = readi32(b, o + 40), numBrushes = readi32(b, o + 44) }
|
||||
elseif q2 then
|
||||
leaf = { contents = readi32(b, o), cluster = readi16(b, o + 4), area = readi16(b, o + 6), mins = v3(b, o + 8, "s"), maxs = v3(b, o + 14, "s"),
|
||||
firstFace = readu16(b, o + 20), numFaces = readu16(b, o + 22), firstBrush = readu16(b, o + 24), numBrushes = readu16(b, o + 26) }
|
||||
else
|
||||
local bk = if bsp2 then "f" else "s"
|
||||
local bs = if bsp2 then 12 else 6
|
||||
local fo = o + 8 + bs * 2
|
||||
local big = bsp2 or rmq
|
||||
leaf = { contents = readi32(b, o), visofs = readi32(b, o + 4), mins = v3(b, o + 8, bk), maxs = v3(b, o + 8 + bs, bk),
|
||||
firstFace = if big then readu32(b, fo) else readu16(b, fo), numFaces = if big then readu32(b, fo + 4) else readu16(b, fo + 2) }
|
||||
local ao = fo + (if big then 8 else 4)
|
||||
leaf.ambient = { readu8(b, ao), readu8(b, ao + 1), readu8(b, ao + 2), readu8(b, ao + 3) }
|
||||
end
|
||||
d.leaves[i + 1] = leaf
|
||||
end
|
||||
if not q2 and not q3 then
|
||||
local C = lumps.clipnodes
|
||||
local cSize = if bsp2 or rmq then 12 else 8
|
||||
local nc = count(C, cSize)
|
||||
if nc > limit then
|
||||
return nil, "limit: too many clipnodes"
|
||||
end
|
||||
local cl: { ClipNode } = {}
|
||||
for i = 0, nc - 1 do
|
||||
local o = base + C.at + i * cSize
|
||||
local c
|
||||
if cSize == 12 then
|
||||
c = { readi32(b, o + 4), readi32(b, o + 8) }
|
||||
else
|
||||
c = {}
|
||||
for k = 0, 1 do
|
||||
local p = readu16(b, o + 4 + k * 2)
|
||||
c[k + 1] = if p > nc then p - 65536 else p -- Quakespasm Mod_LoadClipnodes
|
||||
end
|
||||
end
|
||||
cl[i + 1] = { plane = readi32(b, o), children = c }
|
||||
end
|
||||
d.clipnodes = cl
|
||||
end
|
||||
local M = lumps.models
|
||||
local mSize = if q3 then 40 elseif q2 then 48 else 64
|
||||
for i = 0, count(M, mSize) - 1 do
|
||||
local o = base + M.at + i * mSize
|
||||
local m: any
|
||||
if q3 then
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), headnodes = { 0 }, firstFace = readi32(b, o + 24), numFaces = readi32(b, o + 28) }
|
||||
elseif q2 then
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), origin = v3(b, o + 24, "f"), headnodes = { readi32(b, o + 36) }, firstFace = readi32(b, o + 40), numFaces = readi32(b, o + 44) }
|
||||
else
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), origin = v3(b, o + 24, "f"),
|
||||
headnodes = { readi32(b, o + 36), readi32(b, o + 40), readi32(b, o + 44), readi32(b, o + 48) },
|
||||
visleafs = readi32(b, o + 52), firstFace = readi32(b, o + 56), numFaces = readi32(b, o + 60) }
|
||||
end
|
||||
d.models[i + 1] = m
|
||||
end
|
||||
if q2 or q3 then
|
||||
local B, S = lumps.brushes, lumps.brushsides
|
||||
local nb, ns = count(B, 12), count(S, if q3 then 8 else 4)
|
||||
if nb > limit or ns > limit then
|
||||
return nil, "limit: too many brushes"
|
||||
end
|
||||
local brushes: { Brush }, sides: { Side } = {}, {}
|
||||
for i = 0, nb - 1 do
|
||||
local o = base + B.at + i * 12
|
||||
brushes[i + 1] = { firstSide = readi32(b, o), numSides = readi32(b, o + 4), contents = readi32(b, o + 8) } -- Quake III: a texture index
|
||||
end
|
||||
for i = 0, ns - 1 do
|
||||
if q3 then
|
||||
local o = base + S.at + i * 8
|
||||
sides[i + 1] = { plane = readi32(b, o), texinfo = readi32(b, o + 4) }
|
||||
else
|
||||
local o = base + S.at + i * 4
|
||||
sides[i + 1] = { plane = readu16(b, o), texinfo = readi16(b, o + 2) }
|
||||
end
|
||||
end
|
||||
d.brushes, d.brushSides = brushes, sides
|
||||
local LB = lumps.leafbrushes
|
||||
local lbs: { number } = {}
|
||||
for i = 0, count(LB, if q3 then 4 else 2) - 1 do
|
||||
lbs[i + 1] = if q3 then readi32(b, base + LB.at + i * 4) else readu16(b, base + LB.at + i * 2)
|
||||
end
|
||||
d.leafBrushes = lbs
|
||||
if q3 then
|
||||
-- brush contents come from the texture's contents flags
|
||||
local T = lumps.textures
|
||||
local nt = count(T, 72)
|
||||
for _, br in brushes do
|
||||
local t = br.contents
|
||||
br.contents = if t >= 0 and t < nt then readi32(b, base + T.at + t * 72 + 68) else 0
|
||||
end
|
||||
end
|
||||
end
|
||||
local VL = lumps.visibility or lumps.visdata
|
||||
d.visibility = if VL then slice(b, base, VL) else nil
|
||||
local vis = d.visibility
|
||||
if vis and q2 and buffer.len(vis) >= 4 then
|
||||
d.numClusters = readi32(vis, 0)
|
||||
elseif vis and q3 and buffer.len(vis) >= 8 then
|
||||
d.numClusters, d.clusterBytes = readi32(vis, 0), readi32(vis, 4)
|
||||
end
|
||||
return d, nil
|
||||
end
|
||||
|
||||
local function side(d: Data, plane: number, x: number, y: number, z: number): number
|
||||
local p = d.planes[plane + 1]
|
||||
if not p then
|
||||
return 0
|
||||
end
|
||||
local n = p.normal
|
||||
return x * n[1] + y * n[2] + z * n[3] - p.dist
|
||||
end
|
||||
|
||||
-- the leaf (0-based) at a point, walking the tree of model (0-based, default 0)
|
||||
function Tree.leafAt(d: Data, x: number, y: number, z: number, model: number?): number?
|
||||
local m = d.models[(model or 0) + 1]
|
||||
local node = if m then m.headnodes[1] else 0
|
||||
for _ = 1, #d.nodes + 1 do
|
||||
if node < 0 then
|
||||
return -node - 1
|
||||
end
|
||||
local nd = d.nodes[node + 1]
|
||||
if not nd then
|
||||
return nil
|
||||
end
|
||||
node = if side(d, nd.plane, x, y, z) >= 0 then nd.children[1] else nd.children[2]
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Quake 1: CONTENTS_* (-1 empty, -2 solid, -3 water, -4 slime, -5 lava, -6 sky) in hull 0..3; Quake II: the leaf's
|
||||
-- CONTENTS_* bits; Quake III: the contents bits of the brushes holding the point
|
||||
function Tree.contents(d: Data, x: number, y: number, z: number, hull: number?, model: number?): number?
|
||||
local h = hull or 0
|
||||
if (d.variant == "q1" or d.variant == "hl" or d.variant == "bsp2" or d.variant == "2psb") and h > 0 then
|
||||
local m = d.models[(model or 0) + 1]
|
||||
local cl = d.clipnodes or {}
|
||||
local node = if m then m.headnodes[h + 1] or -1 else -1
|
||||
for _ = 1, #cl + 1 do
|
||||
if node < 0 then
|
||||
return node
|
||||
end
|
||||
local c = cl[node + 1]
|
||||
if not c then
|
||||
return nil
|
||||
end
|
||||
node = if side(d, c.plane, x, y, z) >= 0 then c.children[1] else c.children[2]
|
||||
end
|
||||
return nil
|
||||
end
|
||||
local leaf = Tree.leafAt(d, x, y, z, model)
|
||||
local lf = leaf and d.leaves[leaf + 1]
|
||||
if not lf then
|
||||
return nil
|
||||
end
|
||||
if d.variant ~= "q3" then
|
||||
return lf.contents
|
||||
end
|
||||
local out = 0
|
||||
local lbs: { number }, brushes: { Brush }, sides: { Side } = d.leafBrushes or {}, d.brushes or {}, d.brushSides or {}
|
||||
local fb: number, nb: number = lf.firstBrush or 0, lf.numBrushes or 0
|
||||
for k = fb, fb + nb - 1 do
|
||||
local br = brushes[(lbs[k + 1] or -1) + 1]
|
||||
if br then
|
||||
local inside = true
|
||||
for s = br.firstSide, br.firstSide + br.numSides - 1 do
|
||||
local sd = sides[s + 1]
|
||||
if not sd or side(d, sd.plane, x, y, z) > 0 then
|
||||
inside = false
|
||||
break
|
||||
end
|
||||
end
|
||||
if inside then
|
||||
out = bit32.bor(out, br.contents)
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function decompress(vis: buffer, at: number, bytes: number, out: { boolean })
|
||||
local n = buffer.len(vis)
|
||||
local i = 0
|
||||
while i < bytes and at < n do
|
||||
local c = readu8(vis, at)
|
||||
if c ~= 0 then
|
||||
for bit = 0, 7 do
|
||||
if bit32.band(c, bit32.lshift(1, bit)) ~= 0 then
|
||||
out[i * 8 + bit + 1] = true
|
||||
end
|
||||
end
|
||||
i += 1
|
||||
at += 1
|
||||
else
|
||||
if at + 1 >= n then
|
||||
break
|
||||
end
|
||||
i += readu8(vis, at + 1)
|
||||
at += 2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the leaves (0-based, sorted) potentially visible from a leaf; nil without visibility data (everything visible)
|
||||
function Tree.pvs(d: Data, leaf: number): { number }?
|
||||
local lf = d.leaves[leaf + 1]
|
||||
local vis = d.visibility
|
||||
if not lf or not vis then
|
||||
return nil
|
||||
end
|
||||
local out: { number } = {}
|
||||
if d.variant == "q2" or d.variant == "q3" then
|
||||
local nc = d.numClusters or 0
|
||||
local c = lf.cluster or -1
|
||||
if c < 0 or c >= nc then
|
||||
return out
|
||||
end
|
||||
local bits: { boolean } = {}
|
||||
if d.variant == "q2" then
|
||||
if 4 + nc * 8 > buffer.len(vis) then
|
||||
return nil
|
||||
end
|
||||
decompress(vis, readi32(vis, 4 + c * 8), (nc + 7) // 8, bits)
|
||||
else
|
||||
local sz = d.clusterBytes or 0
|
||||
local o = 8 + c * sz
|
||||
for k = 0, sz - 1 do
|
||||
if o + k < buffer.len(vis) then
|
||||
local v = readu8(vis, o + k)
|
||||
for bit = 0, 7 do
|
||||
if bit32.band(v, bit32.lshift(1, bit)) ~= 0 then
|
||||
bits[k * 8 + bit + 1] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for i, l in d.leaves do
|
||||
local cl = l.cluster or -1
|
||||
if cl >= 0 and bits[cl + 1] then
|
||||
table.insert(out, i - 1)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
-- Quake 1: bit k = leaf k + 1 (leaf 0 is the shared solid leaf), visleafs bits
|
||||
local visofs = lf.visofs or -1
|
||||
if leaf == 0 or visofs < 0 then
|
||||
return out
|
||||
end
|
||||
local n = d.models[1] and d.models[1].visleafs or (#d.leaves - 1)
|
||||
local bits: { boolean } = {}
|
||||
decompress(vis, visofs, (n + 7) // 8, bits)
|
||||
for k = 0, n - 1 do
|
||||
if bits[k + 1] then
|
||||
table.insert(out, k + 1)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return Tree
|
||||
end
|
||||
__FILES["model/dae"] = function()
|
||||
|
||||
-- model/dae: COLLADA 1.4.1 / 1.5.0 (Khronos specifications; .dae XML, .zae zip with manifest.xml). Supports: geometry,
|
||||
|
||||
+141
-139
@@ -404,143 +404,145 @@
|
||||
[144870, "src/Rosetta/model/b3d/init.luau"],
|
||||
[144925, "src/Rosetta/model/b3d/Read.luau"],
|
||||
[145496, "src/Rosetta/model/bsp/init.luau"],
|
||||
[145641, "src/Rosetta/model/bsp/Brush.luau"],
|
||||
[145904, "src/Rosetta/model/bsp/Quake3.luau"],
|
||||
[146132, "src/Rosetta/model/dae/init.luau"],
|
||||
[146321, "src/Rosetta/model/dae/Math.luau"],
|
||||
[146368, "src/Rosetta/model/dae/Read.luau"],
|
||||
[148218, "src/Rosetta/model/dae/Write.luau"],
|
||||
[148587, "src/Rosetta/model/fbx/init.luau"],
|
||||
[148735, "src/Rosetta/model/fbx/Ascii.luau"],
|
||||
[149005, "src/Rosetta/model/fbx/Binary.luau"],
|
||||
[149460, "src/Rosetta/model/fbx/Read.luau"],
|
||||
[150822, "src/Rosetta/model/fbx/Write.luau"],
|
||||
[151807, "src/Rosetta/model/fbx/Xform.luau"],
|
||||
[151848, "src/Rosetta/model/gltf/init.luau"],
|
||||
[152085, "src/Rosetta/model/gltf/Pack.luau"],
|
||||
[152154, "src/Rosetta/model/gltf/Read.luau"],
|
||||
[152885, "src/Rosetta/model/gltf/Write.luau"],
|
||||
[153565, "src/Rosetta/model/iqm/init.luau"],
|
||||
[153641, "src/Rosetta/model/iqm/Read.luau"],
|
||||
[153996, "src/Rosetta/model/iqm/Write.luau"],
|
||||
[154683, "src/Rosetta/model/ldraw/init.luau"],
|
||||
[154761, "src/Rosetta/model/ldraw/Colors.luau"],
|
||||
[155079, "src/Rosetta/model/ldraw/Read.luau"],
|
||||
[155656, "src/Rosetta/model/lwo/init.luau"],
|
||||
[155732, "src/Rosetta/model/lwo/Build.luau"],
|
||||
[156194, "src/Rosetta/model/lwo/Parse.luau"],
|
||||
[156737, "src/Rosetta/model/map/init.luau"],
|
||||
[157054, "src/Rosetta/model/map/Build.luau"],
|
||||
[157297, "src/Rosetta/model/map/Parse.luau"],
|
||||
[157682, "src/Rosetta/model/map/Write.luau"],
|
||||
[157828, "src/Rosetta/model/mca/init.luau"],
|
||||
[158170, "src/Rosetta/model/mca/Chunk.luau"],
|
||||
[158303, "src/Rosetta/model/mca/Lz4Block.luau"],
|
||||
[158373, "src/Rosetta/model/md2/init.luau"],
|
||||
[158572, "src/Rosetta/model/md3/init.luau"],
|
||||
[158800, "src/Rosetta/model/md5/init.luau"],
|
||||
[158881, "src/Rosetta/model/md5/Read.luau"],
|
||||
[159314, "src/Rosetta/model/mdl/init.luau"],
|
||||
[159396, "src/Rosetta/model/mdl/HalfLife.luau"],
|
||||
[159724, "src/Rosetta/model/mdl/Quake.luau"],
|
||||
[159942, "src/Rosetta/model/ms3d/init.luau"],
|
||||
[160014, "src/Rosetta/model/ms3d/Read.luau"],
|
||||
[160460, "src/Rosetta/model/ms3d/Write.luau"],
|
||||
[160929, "src/Rosetta/model/obj/init.luau"],
|
||||
[161671, "src/Rosetta/model/obj/Mtl.luau"],
|
||||
[161958, "src/Rosetta/model/off/init.luau"],
|
||||
[162420, "src/Rosetta/model/ply/init.luau"],
|
||||
[163525, "src/Rosetta/model/ply/Binary.luau"],
|
||||
[163726, "src/Rosetta/model/pmx/init.luau"],
|
||||
[163795, "src/Rosetta/model/pmx/Build.luau"],
|
||||
[164091, "src/Rosetta/model/pmx/Pmd.luau"],
|
||||
[164426, "src/Rosetta/model/pmx/Read.luau"],
|
||||
[164830, "src/Rosetta/model/qb/init.luau"],
|
||||
[165223, "src/Rosetta/model/rbxm/init.luau"],
|
||||
[165815, "src/Rosetta/model/rbxmx/init.luau"],
|
||||
[166468, "src/Rosetta/model/robloxmesh/init.luau"],
|
||||
[167127, "src/Rosetta/model/robloxmesh/Parse.luau"],
|
||||
[167584, "src/Rosetta/model/robloxmesh/Write.luau"],
|
||||
[167693, "src/Rosetta/model/schematic/init.luau"],
|
||||
[168014, "src/Rosetta/model/schematic/Common.luau"],
|
||||
[168134, "src/Rosetta/model/schematic/Litematic.luau"],
|
||||
[168345, "src/Rosetta/model/schematic/Mcedit.luau"],
|
||||
[168589, "src/Rosetta/model/schematic/Sponge.luau"],
|
||||
[168718, "src/Rosetta/model/smd/init.luau"],
|
||||
[168785, "src/Rosetta/model/smd/Read.luau"],
|
||||
[169054, "src/Rosetta/model/smd/Write.luau"],
|
||||
[169297, "src/Rosetta/model/stl/init.luau"],
|
||||
[169747, "src/Rosetta/model/stl/Decode.luau"],
|
||||
[169821, "src/Rosetta/model/usda/init.luau"],
|
||||
[170030, "src/Rosetta/model/usda/Build.luau"],
|
||||
[171308, "src/Rosetta/model/usda/Parse.luau"],
|
||||
[172106, "src/Rosetta/model/usda/Write.luau"],
|
||||
[172923, "src/Rosetta/model/vox/init.luau"],
|
||||
[173809, "src/Rosetta/model/vox/Grid.luau"],
|
||||
[173866, "src/Rosetta/model/vrml/init.luau"],
|
||||
[173932, "src/Rosetta/model/x/init.luau"],
|
||||
[174062, "src/Rosetta/model/x/Build.luau"],
|
||||
[174625, "src/Rosetta/model/x/Parse.luau"],
|
||||
[174922, "src/Rosetta/model/x3d/init.luau"],
|
||||
[175000, "src/Rosetta/music/669/init.luau"],
|
||||
[175206, "src/Rosetta/music/ahx/init.luau"],
|
||||
[175257, "src/Rosetta/music/far/init.luau"],
|
||||
[175486, "src/Rosetta/music/hmp/init.luau"],
|
||||
[175750, "src/Rosetta/music/hvl/init.luau"],
|
||||
[175801, "src/Rosetta/music/it/init.luau"],
|
||||
[176219, "src/Rosetta/music/it/Itsex.luau"],
|
||||
[176335, "src/Rosetta/music/med/init.luau"],
|
||||
[176982, "src/Rosetta/music/midi/init.luau"],
|
||||
[177125, "src/Rosetta/music/midi/Smf.luau"],
|
||||
[177536, "src/Rosetta/music/mod/init.luau"],
|
||||
[177889, "src/Rosetta/music/mtm/init.luau"],
|
||||
[178111, "src/Rosetta/music/mus/init.luau"],
|
||||
[178268, "src/Rosetta/music/okt/init.luau"],
|
||||
[178549, "src/Rosetta/music/ptm/init.luau"],
|
||||
[178771, "src/Rosetta/music/s3m/init.luau"],
|
||||
[179007, "src/Rosetta/music/sf2/init.luau"],
|
||||
[179050, "src/Rosetta/music/stm/init.luau"],
|
||||
[179281, "src/Rosetta/music/ult/init.luau"],
|
||||
[179585, "src/Rosetta/music/umx/init.luau"],
|
||||
[179801, "src/Rosetta/music/xm/init.luau"],
|
||||
[180362, "src/Rosetta/music/xmi/init.luau"],
|
||||
[180570, "src/Rosetta/ops/AudioOps.luau"],
|
||||
[181019, "src/Rosetta/ops/ImageOps/init.luau"],
|
||||
[181663, "src/Rosetta/ops/ImageOps/Kernels.luau"],
|
||||
[182173, "src/Rosetta/ops/LayerOps.luau"],
|
||||
[182266, "src/Rosetta/ops/MeshOps/init.luau"],
|
||||
[183302, "src/Rosetta/ops/MeshOps/Greedy.luau"],
|
||||
[183469, "src/Rosetta/ops/MusicOps.luau"],
|
||||
[183817, "src/Rosetta/playlist/m3u/init.luau"],
|
||||
[184170, "src/Rosetta/playlist/pls/init.luau"],
|
||||
[184348, "src/Rosetta/playlist/xspf/init.luau"],
|
||||
[184551, "src/Rosetta/subtitle/ass/init.luau"],
|
||||
[184995, "src/Rosetta/subtitle/srt/init.luau"],
|
||||
[185168, "src/Rosetta/subtitle/vtt/init.luau"],
|
||||
[185371, "src/Rosetta/vector/svg/init.luau"],
|
||||
[185457, "src/Rosetta/vector/svg/Parse.luau"],
|
||||
[186203, "src/Rosetta/vector/svg/PathData.luau"],
|
||||
[186571, "src/Rosetta/vector/svg/Render.luau"],
|
||||
[186988, "src/Rosetta/vector/svg/Style.luau"],
|
||||
[187360, "src/Rosetta/vector/svg/Write.luau"],
|
||||
[187560, "src/Rosetta/vector/tiled/init.luau"],
|
||||
[187740, "src/Rosetta/vector/tiled/Data.luau"],
|
||||
[187891, "src/Rosetta/vector/tiled/Tmj.luau"],
|
||||
[188522, "src/Rosetta/vector/tiled/Tmx.luau"],
|
||||
[189134, "src/Rosetta/video/avi/init.luau"],
|
||||
[189428, "src/Rosetta/video/fli/init.luau"],
|
||||
[189579, "src/Rosetta/video/flv/init.luau"],
|
||||
[189963, "src/Rosetta/video/flv/Amf.luau"],
|
||||
[190121, "src/Rosetta/video/flv/Mux.luau"],
|
||||
[190402, "src/Rosetta/video/h264/init.luau"],
|
||||
[190541, "src/Rosetta/video/mkv/init.luau"],
|
||||
[190697, "src/Rosetta/video/mkv/Mux.luau"],
|
||||
[190976, "src/Rosetta/video/mkv/Parse.luau"],
|
||||
[191697, "src/Rosetta/video/mp4/init.luau"],
|
||||
[192025, "src/Rosetta/video/mpegps/init.luau"],
|
||||
[192212, "src/Rosetta/video/mpegts/init.luau"],
|
||||
[192450, "src/Rosetta/video/ogv/init.luau"],
|
||||
[192845, "src/Rosetta/video/smk/init.luau"],
|
||||
[193154, "src/Rosetta/video/smk/Audio.luau"],
|
||||
[193274, "src/Rosetta/video/y4m/init.luau"]
|
||||
[145687, "src/Rosetta/model/bsp/Lightmap.luau"],
|
||||
[145753, "src/Rosetta/model/bsp/Quake.luau"],
|
||||
[146140, "src/Rosetta/model/bsp/Quake3.luau"],
|
||||
[146368, "src/Rosetta/model/bsp/Tree.luau"],
|
||||
[146756, "src/Rosetta/model/dae/init.luau"],
|
||||
[146945, "src/Rosetta/model/dae/Math.luau"],
|
||||
[146992, "src/Rosetta/model/dae/Read.luau"],
|
||||
[148842, "src/Rosetta/model/dae/Write.luau"],
|
||||
[149211, "src/Rosetta/model/fbx/init.luau"],
|
||||
[149359, "src/Rosetta/model/fbx/Ascii.luau"],
|
||||
[149629, "src/Rosetta/model/fbx/Binary.luau"],
|
||||
[150084, "src/Rosetta/model/fbx/Read.luau"],
|
||||
[151446, "src/Rosetta/model/fbx/Write.luau"],
|
||||
[152431, "src/Rosetta/model/fbx/Xform.luau"],
|
||||
[152472, "src/Rosetta/model/gltf/init.luau"],
|
||||
[152709, "src/Rosetta/model/gltf/Pack.luau"],
|
||||
[152778, "src/Rosetta/model/gltf/Read.luau"],
|
||||
[153509, "src/Rosetta/model/gltf/Write.luau"],
|
||||
[154189, "src/Rosetta/model/iqm/init.luau"],
|
||||
[154265, "src/Rosetta/model/iqm/Read.luau"],
|
||||
[154620, "src/Rosetta/model/iqm/Write.luau"],
|
||||
[155307, "src/Rosetta/model/ldraw/init.luau"],
|
||||
[155385, "src/Rosetta/model/ldraw/Colors.luau"],
|
||||
[155703, "src/Rosetta/model/ldraw/Read.luau"],
|
||||
[156280, "src/Rosetta/model/lwo/init.luau"],
|
||||
[156356, "src/Rosetta/model/lwo/Build.luau"],
|
||||
[156818, "src/Rosetta/model/lwo/Parse.luau"],
|
||||
[157361, "src/Rosetta/model/map/init.luau"],
|
||||
[157678, "src/Rosetta/model/map/Build.luau"],
|
||||
[157921, "src/Rosetta/model/map/Parse.luau"],
|
||||
[158306, "src/Rosetta/model/map/Write.luau"],
|
||||
[158452, "src/Rosetta/model/mca/init.luau"],
|
||||
[158794, "src/Rosetta/model/mca/Chunk.luau"],
|
||||
[158927, "src/Rosetta/model/mca/Lz4Block.luau"],
|
||||
[158997, "src/Rosetta/model/md2/init.luau"],
|
||||
[159196, "src/Rosetta/model/md3/init.luau"],
|
||||
[159424, "src/Rosetta/model/md5/init.luau"],
|
||||
[159505, "src/Rosetta/model/md5/Read.luau"],
|
||||
[159938, "src/Rosetta/model/mdl/init.luau"],
|
||||
[160020, "src/Rosetta/model/mdl/HalfLife.luau"],
|
||||
[160348, "src/Rosetta/model/mdl/Quake.luau"],
|
||||
[160566, "src/Rosetta/model/ms3d/init.luau"],
|
||||
[160638, "src/Rosetta/model/ms3d/Read.luau"],
|
||||
[161084, "src/Rosetta/model/ms3d/Write.luau"],
|
||||
[161553, "src/Rosetta/model/obj/init.luau"],
|
||||
[162295, "src/Rosetta/model/obj/Mtl.luau"],
|
||||
[162582, "src/Rosetta/model/off/init.luau"],
|
||||
[163044, "src/Rosetta/model/ply/init.luau"],
|
||||
[164149, "src/Rosetta/model/ply/Binary.luau"],
|
||||
[164350, "src/Rosetta/model/pmx/init.luau"],
|
||||
[164419, "src/Rosetta/model/pmx/Build.luau"],
|
||||
[164715, "src/Rosetta/model/pmx/Pmd.luau"],
|
||||
[165050, "src/Rosetta/model/pmx/Read.luau"],
|
||||
[165454, "src/Rosetta/model/qb/init.luau"],
|
||||
[165847, "src/Rosetta/model/rbxm/init.luau"],
|
||||
[166439, "src/Rosetta/model/rbxmx/init.luau"],
|
||||
[167092, "src/Rosetta/model/robloxmesh/init.luau"],
|
||||
[167751, "src/Rosetta/model/robloxmesh/Parse.luau"],
|
||||
[168208, "src/Rosetta/model/robloxmesh/Write.luau"],
|
||||
[168317, "src/Rosetta/model/schematic/init.luau"],
|
||||
[168638, "src/Rosetta/model/schematic/Common.luau"],
|
||||
[168758, "src/Rosetta/model/schematic/Litematic.luau"],
|
||||
[168969, "src/Rosetta/model/schematic/Mcedit.luau"],
|
||||
[169213, "src/Rosetta/model/schematic/Sponge.luau"],
|
||||
[169342, "src/Rosetta/model/smd/init.luau"],
|
||||
[169409, "src/Rosetta/model/smd/Read.luau"],
|
||||
[169678, "src/Rosetta/model/smd/Write.luau"],
|
||||
[169921, "src/Rosetta/model/stl/init.luau"],
|
||||
[170371, "src/Rosetta/model/stl/Decode.luau"],
|
||||
[170445, "src/Rosetta/model/usda/init.luau"],
|
||||
[170654, "src/Rosetta/model/usda/Build.luau"],
|
||||
[171932, "src/Rosetta/model/usda/Parse.luau"],
|
||||
[172730, "src/Rosetta/model/usda/Write.luau"],
|
||||
[173547, "src/Rosetta/model/vox/init.luau"],
|
||||
[174433, "src/Rosetta/model/vox/Grid.luau"],
|
||||
[174490, "src/Rosetta/model/vrml/init.luau"],
|
||||
[174556, "src/Rosetta/model/x/init.luau"],
|
||||
[174686, "src/Rosetta/model/x/Build.luau"],
|
||||
[175249, "src/Rosetta/model/x/Parse.luau"],
|
||||
[175546, "src/Rosetta/model/x3d/init.luau"],
|
||||
[175624, "src/Rosetta/music/669/init.luau"],
|
||||
[175830, "src/Rosetta/music/ahx/init.luau"],
|
||||
[175881, "src/Rosetta/music/far/init.luau"],
|
||||
[176110, "src/Rosetta/music/hmp/init.luau"],
|
||||
[176374, "src/Rosetta/music/hvl/init.luau"],
|
||||
[176425, "src/Rosetta/music/it/init.luau"],
|
||||
[176843, "src/Rosetta/music/it/Itsex.luau"],
|
||||
[176959, "src/Rosetta/music/med/init.luau"],
|
||||
[177606, "src/Rosetta/music/midi/init.luau"],
|
||||
[177749, "src/Rosetta/music/midi/Smf.luau"],
|
||||
[178160, "src/Rosetta/music/mod/init.luau"],
|
||||
[178513, "src/Rosetta/music/mtm/init.luau"],
|
||||
[178735, "src/Rosetta/music/mus/init.luau"],
|
||||
[178892, "src/Rosetta/music/okt/init.luau"],
|
||||
[179173, "src/Rosetta/music/ptm/init.luau"],
|
||||
[179395, "src/Rosetta/music/s3m/init.luau"],
|
||||
[179631, "src/Rosetta/music/sf2/init.luau"],
|
||||
[179674, "src/Rosetta/music/stm/init.luau"],
|
||||
[179905, "src/Rosetta/music/ult/init.luau"],
|
||||
[180209, "src/Rosetta/music/umx/init.luau"],
|
||||
[180425, "src/Rosetta/music/xm/init.luau"],
|
||||
[180986, "src/Rosetta/music/xmi/init.luau"],
|
||||
[181194, "src/Rosetta/ops/AudioOps.luau"],
|
||||
[181643, "src/Rosetta/ops/ImageOps/init.luau"],
|
||||
[182287, "src/Rosetta/ops/ImageOps/Kernels.luau"],
|
||||
[182797, "src/Rosetta/ops/LayerOps.luau"],
|
||||
[182890, "src/Rosetta/ops/MeshOps/init.luau"],
|
||||
[183926, "src/Rosetta/ops/MeshOps/Greedy.luau"],
|
||||
[184093, "src/Rosetta/ops/MusicOps.luau"],
|
||||
[184441, "src/Rosetta/playlist/m3u/init.luau"],
|
||||
[184794, "src/Rosetta/playlist/pls/init.luau"],
|
||||
[184972, "src/Rosetta/playlist/xspf/init.luau"],
|
||||
[185175, "src/Rosetta/subtitle/ass/init.luau"],
|
||||
[185619, "src/Rosetta/subtitle/srt/init.luau"],
|
||||
[185792, "src/Rosetta/subtitle/vtt/init.luau"],
|
||||
[185995, "src/Rosetta/vector/svg/init.luau"],
|
||||
[186081, "src/Rosetta/vector/svg/Parse.luau"],
|
||||
[186827, "src/Rosetta/vector/svg/PathData.luau"],
|
||||
[187195, "src/Rosetta/vector/svg/Render.luau"],
|
||||
[187612, "src/Rosetta/vector/svg/Style.luau"],
|
||||
[187984, "src/Rosetta/vector/svg/Write.luau"],
|
||||
[188184, "src/Rosetta/vector/tiled/init.luau"],
|
||||
[188364, "src/Rosetta/vector/tiled/Data.luau"],
|
||||
[188515, "src/Rosetta/vector/tiled/Tmj.luau"],
|
||||
[189146, "src/Rosetta/vector/tiled/Tmx.luau"],
|
||||
[189758, "src/Rosetta/video/avi/init.luau"],
|
||||
[190052, "src/Rosetta/video/fli/init.luau"],
|
||||
[190203, "src/Rosetta/video/flv/init.luau"],
|
||||
[190587, "src/Rosetta/video/flv/Amf.luau"],
|
||||
[190745, "src/Rosetta/video/flv/Mux.luau"],
|
||||
[191026, "src/Rosetta/video/h264/init.luau"],
|
||||
[191165, "src/Rosetta/video/mkv/init.luau"],
|
||||
[191321, "src/Rosetta/video/mkv/Mux.luau"],
|
||||
[191600, "src/Rosetta/video/mkv/Parse.luau"],
|
||||
[192321, "src/Rosetta/video/mp4/init.luau"],
|
||||
[192649, "src/Rosetta/video/mpegps/init.luau"],
|
||||
[192836, "src/Rosetta/video/mpegts/init.luau"],
|
||||
[193074, "src/Rosetta/video/ogv/init.luau"],
|
||||
[193469, "src/Rosetta/video/smk/init.luau"],
|
||||
[193778, "src/Rosetta/video/smk/Audio.luau"],
|
||||
[193898, "src/Rosetta/video/y4m/init.luau"]
|
||||
]
|
||||
+656
-32
@@ -2244,7 +2244,7 @@ local list= {
|
||||
extensions = { "bsp" },
|
||||
mime = { "model/x-bsp" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/image/Miptex", "codecs/model/Patch" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "halflife", "quake2", "quake3" }, lightmaps = "quake3" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "bsp2", "2psb", "halflife", "quake2", "quake3" }, lightmaps = true, collision = true, visibility = true },
|
||||
load = function()
|
||||
return __include("model/bsp")
|
||||
end,
|
||||
@@ -146126,18 +146126,21 @@ end
|
||||
return Read
|
||||
end
|
||||
__FILES["model/bsp"] = function()
|
||||
local __compat = __compat local buffer, __iter = __compat.buffer, __compat.__iter --!strict
|
||||
-- model/bsp: compiled game maps, read: Quake 1 (BSP 29), Half-Life (BSP 30), Quake II (IBSP 38), Quake III / RTCW
|
||||
-- (IBSP 46 / 47). Quake textures need the game's palette (gfx/palette.lmp, not redistributable). Map units, z up.
|
||||
-- Not supported: Source engine VBSP, Quake 1/2 lightmaps, visibility and collision data, writing.
|
||||
local __compat = __compat local buffer, type, __iter = __compat.buffer, __compat.type, __compat.__iter --!strict
|
||||
-- model/bsp: compiled game maps, read: Quake 1 (BSP 29, BSP2, 2PSB), Half-Life (BSP 30), Quake II (IBSP 38), Quake III
|
||||
-- / RTCW (IBSP 46 / 47). Lightmaps (Quake 1 / 2 / Half-Life: one atlas, Quake III: its pages) as TEXCOORD_1; the tree,
|
||||
-- collision hulls / brushes and visibility in meta.bsp with point queries (Bsp.leafAt / contents / pvs). Quake textures
|
||||
-- need the game's palette (gfx/palette.lmp, not redistributable). Map units, z up. Not supported: Source engine VBSP,
|
||||
-- light styles other than the first, BSPX lumps, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = __include("core/Err")
|
||||
local Bytes = __include("core/Bytes")
|
||||
local Input = __include("core/Input")
|
||||
local Types = __include("Types")
|
||||
local Brush = __include("model/bsp/Brush")
|
||||
local Quake = __include("model/bsp/Quake")
|
||||
local Quake3 = __include("model/bsp/Quake3")
|
||||
local Tree = __include("model/bsp/Tree")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Bsp = {
|
||||
@@ -146146,7 +146149,7 @@ local Bsp = {
|
||||
extensions = { "bsp" },
|
||||
mime = { "model/x-bsp" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/image/Miptex", "codecs/model/Patch" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "halflife", "quake2", "quake3" }, lightmaps = "quake3" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "bsp2", "2psb", "halflife", "quake2", "quake3" }, lightmaps = true, collision = true, visibility = true },
|
||||
}
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -146160,6 +146163,10 @@ local Bsp = {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local Q1_LUMPS = { "entities", "planes", "miptex", "vertices", "visibility", "nodes", "texinfo", "faces", "lighting",
|
||||
"clipnodes", "leaves", "marksurfaces", "edges", "surfedges", "models" }
|
||||
local Q2_LUMPS = { "entities", "planes", "vertices", "visibility", "nodes", "texinfo", "faces", "lighting", "leaves",
|
||||
@@ -146174,6 +146181,9 @@ if r:remaining() < 64 then
|
||||
end
|
||||
local m = r:peek(4)
|
||||
local v = buffer.readi32(r.buf, r.pos + (m == "IBSP" and 4 or 0))
|
||||
if m == "BSP2" or m == "2PSB" then
|
||||
v = 29
|
||||
end
|
||||
if m == "IBSP" then
|
||||
return (v == 38 or v == 46 or v == 47 )and 1 or 0.3
|
||||
end
|
||||
@@ -146216,6 +146226,8 @@ if m == "IBSP" and version == 38 then
|
||||
names, dir, variant = Q2_LUMPS, 8, "q2"
|
||||
elseif m == "IBSP" and (version == 46 or version == 47) then
|
||||
names, dir, variant = Q3_LUMPS, 8, "q3"
|
||||
elseif m == "BSP2" or m == "2PSB" then
|
||||
names, dir, variant = Q1_LUMPS, 4, string.lower(m)
|
||||
elseif m ~= "IBSP" and (version == 29 or version == 30) then
|
||||
names, dir, variant = Q1_LUMPS, 4, version == 30 and"hl" or"q1"
|
||||
elseif m == "VBSP" then
|
||||
@@ -146242,13 +146254,15 @@ if variant == "q3" then
|
||||
)})[1] if pal and buffer.len(pal) < 768 then
|
||||
pal = nil
|
||||
end
|
||||
scene, msg = Brush.decode(b, base, lumps, variant, {
|
||||
scene, msg = Quake.decode(b, base, lumps, variant, {
|
||||
palette = pal,
|
||||
wal = function(name)
|
||||
return Input.sibling(opts, "textures/" .. name .. ".wal")
|
||||
end,
|
||||
maxPixels = opts and opts.maxPixels,
|
||||
maxVertices = opts and opts.maxVertices,
|
||||
lit = (opts and opts.lit and{(Bytes.toBuffer(opts.lit) )}or{((opts and opts.name and{(Input.sibling(opts, (string.gsub(opts.name , "%.[^./\\]*$", "")) .. ".lit") )}or{nil})[1])})[1],
|
||||
lightmaps = opts and opts.lightmaps,
|
||||
})
|
||||
end
|
||||
if not scene then
|
||||
@@ -146260,29 +146274,129 @@ return Input.sibling(opts, "textures/" .. name .. ".wal")
|
||||
scene.meta.entities = entities(text)
|
||||
scene.meta.source = {
|
||||
format = "bsp",
|
||||
variant = ({ q1 = "quake", hl = "halflife", q2 = "quake2", q3 = "quake3" } )[variant],
|
||||
variant = ({ q1 = "quake", bsp2 = "quake", ["2psb"] = "quake", hl = "halflife", q2 = "quake2", q3 = "quake3" } )[variant],
|
||||
layout = variant == "bsp2" and"BSP2" or(variant == "2psb" and"2PSB" or nil),
|
||||
version = version,
|
||||
entities = text,
|
||||
palette = (variant == "q1" and{(opts ~= nil and (opts.palette ~= nil or Input.sibling(opts, "gfx/palette.lmp") ~= nil)) }or{nil})[1],
|
||||
palette = ((variant == "q1" or variant == "bsp2" or variant == "2psb" )and{(opts ~= nil and (opts.palette ~= nil or Input.sibling(opts, "gfx/palette.lmp") ~= nil)) }or{nil})[1],
|
||||
}
|
||||
if not (opts and opts.tree == false) then
|
||||
local tree, terr = Tree.read(b, base, lumps, variant)
|
||||
if not tree then
|
||||
return nil, Err.from(terr)
|
||||
end
|
||||
scene.meta.bsp = tree
|
||||
end
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
local function tree(scene)
|
||||
return type(scene) == "table" and type(scene.meta) == "table" and scene.meta.bsp or nil
|
||||
end
|
||||
|
||||
-- the leaf (0-based) at a point, in the tree of a model (0 = the world)
|
||||
function Bsp.leafAt(scene, x, y, z, model)
|
||||
local d = tree(scene)
|
||||
return (d and{(Tree.leafAt(d, x, y, z, model) )}or{nil
|
||||
})[1]end
|
||||
|
||||
-- the contents at a point: Quake 1 CONTENTS_* (-1 empty, -2 solid, ...; hull 0 point, 1 player, 2 shambler, 3 crouch
|
||||
-- in Half-Life), Quake II / III contents bits
|
||||
function Bsp.contents(scene, x, y, z, hull, model)
|
||||
local d = tree(scene)
|
||||
return (d and{(Tree.contents(d, x, y, z, hull, model) )}or{nil
|
||||
})[1]end
|
||||
|
||||
-- the leaves (0-based) potentially visible from a leaf; nil when the map has no visibility data
|
||||
function Bsp.pvs(scene, leaf)
|
||||
local d = tree(scene)
|
||||
return (d and{(Tree.pvs(d, leaf) )}or{nil
|
||||
})[1]end
|
||||
|
||||
return Bsp
|
||||
end
|
||||
__FILES["model/bsp/Brush"] = function()
|
||||
__FILES["model/bsp/Lightmap"] = function()
|
||||
local __compat = __compat local buffer, __iter = __compat.buffer, __compat.__iter --!strict
|
||||
-- model/bsp/Brush: Quake 1 (BSP 29), Half-Life (BSP 30) and Quake II (IBSP 38) maps -> Scene, from bspfile.h of the
|
||||
-- Quake / Quake II source releases (no code taken). Texture coordinates: s = v . s.xyz + s.w, divided by the texture
|
||||
-- size.
|
||||
-- model/bsp/Lightmap: packs face lightmaps (w x h luxels each) into one atlas, shelf by shelf, tallest first, each
|
||||
-- with a one-luxel border that repeats its edge (bilinear filtering stays inside the face).
|
||||
local Lightmap = {}
|
||||
|
||||
-- nil, message past maxPixels
|
||||
|
||||
|
||||
function Lightmap.pack(sizes, maxPixels)
|
||||
local area, widest = 0, 1
|
||||
for _, s in __iter(sizes) do
|
||||
area =area+ (s[1] + 2) * (s[2] + 2)
|
||||
widest = math.max(widest, s[1] + 2)
|
||||
end
|
||||
local W = 64
|
||||
while W * W < area * 1.2 and W < 4096 do
|
||||
W =W* 2
|
||||
end
|
||||
W = math.max(W, widest)
|
||||
local order = (table.create(#sizes) )
|
||||
for i = 1, #sizes do
|
||||
order[i] = i
|
||||
end
|
||||
table.sort(order, function(a, b)
|
||||
local ha, hb = sizes[a][2], sizes[b][2]
|
||||
if ha ~= hb then
|
||||
return ha > hb
|
||||
end
|
||||
return a < b
|
||||
end)
|
||||
local out = (table.create(#sizes) )
|
||||
local x, y, rowH = 0, 0, 0
|
||||
for _, i in __iter(order) do
|
||||
local w, h = sizes[i][1] + 2, sizes[i][2] + 2
|
||||
if x + w > W then
|
||||
x, y, rowH = 0, y + rowH, 0
|
||||
end
|
||||
out[i] = { x = x + 1, y = y + 1, w = sizes[i][1], h = sizes[i][2] }
|
||||
x =x+ w
|
||||
rowH = math.max(rowH, h)
|
||||
end
|
||||
local H = math.max(1, y + rowH)
|
||||
if W * H > maxPixels then
|
||||
return 0, 0, nil, "limit: lightmap atlas " .. W .. " x " .. H
|
||||
end
|
||||
return W, H, out, nil
|
||||
end
|
||||
|
||||
-- copies the outermost luxels of a rect into its border (rgba8 pixels, atlas width W)
|
||||
function Lightmap.border(px, W, r)
|
||||
local function copy(dx, dy, sx, sy)
|
||||
buffer.writeu32(px, (dy * W + dx) * 4, buffer.readu32(px, (sy * W + sx) * 4))
|
||||
end
|
||||
for yy = r.y, r.y + r.h - 1 do
|
||||
copy(r.x - 1, yy, r.x, yy)
|
||||
copy(r.x + r.w, yy, r.x + r.w - 1, yy)
|
||||
end
|
||||
for xx = r.x - 1, r.x + r.w do
|
||||
copy(xx, r.y - 1, xx, r.y)
|
||||
copy(xx, r.y + r.h, xx, r.y + r.h - 1)
|
||||
end
|
||||
end
|
||||
|
||||
return Lightmap
|
||||
end
|
||||
__FILES["model/bsp/Quake"] = function()
|
||||
local __compat = __compat local buffer, bit32, __iter = __compat.buffer, __compat.bit32, __compat.__iter --!strict
|
||||
-- model/bsp/Quake: Quake 1 (BSP 29, BSP2, 2PSB), Half-Life (BSP 30) and Quake II (IBSP 38) maps -> Scene, from
|
||||
-- bspfile.h of the Quake / Quake II source releases and ericw-tools (BSP2 / 2PSB; no code taken). Texture coordinates:
|
||||
-- s = v . s.xyz + s.w over the texture size; lightmaps (style 0) packed into one atlas as TEXCOORD_1, extents as
|
||||
-- CalcSurfaceExtents (16 texels per luxel).
|
||||
local Types = __include("Types")
|
||||
local Pixels = __include("codecs/image/Pixels")
|
||||
local Lightmap = __include("model/bsp/Lightmap")
|
||||
local Miptex = __include("codecs/image/Miptex")
|
||||
|
||||
local Brush = {}
|
||||
local Quake = {}
|
||||
|
||||
local Scene = Types.Scene
|
||||
local readi32, readu16, readf32, readu32 = buffer.readi32, buffer.readu16, buffer.readf32, buffer.readu32
|
||||
local readi32, readu16, readf32, readu32, readu8 = buffer.readi32, buffer.readu16, buffer.readf32, buffer.readu32, buffer.readu8
|
||||
local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
|
||||
@@ -146296,6 +146410,8 @@ local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local function cstr(b, at, n)
|
||||
return (string.match(buffer.readstring(b, at, n), "^[^%z]*"))
|
||||
end
|
||||
@@ -146331,11 +146447,14 @@ local out= {}
|
||||
return out, nil
|
||||
end
|
||||
|
||||
function Brush.decode(b, base, lumps, variant, opts)
|
||||
function Quake.decode(b, base, lumps, variant, opts)
|
||||
local q2 = variant == "q2"
|
||||
local big = variant == "bsp2" or variant == "2psb"
|
||||
local maxPixels = opts and opts.maxPixels or 2 ^ 24
|
||||
local V, E, SE, Fc, TI, P, M = lumps.vertices, lumps.edges, lumps.surfedges, lumps.faces, lumps.texinfo, lumps.planes, lumps.models
|
||||
local nV, nE, nSE, nF = math.floor(V.size / 12), math.floor(E.size / 4), math.floor(SE.size / 4), math.floor(Fc.size / 20
|
||||
local fSize= big and 28 or 20
|
||||
local eSize= big and 8 or 4
|
||||
local nV, nE, nSE, nF = math.floor(V.size / 12), math.floor(E.size / eSize), math.floor(SE.size / 4), math.floor(Fc.size / fSize
|
||||
) local tiSize = q2 and 76 or 40
|
||||
local nTI, nP = math.floor(TI.size / tiSize), math.floor(P.size / 20
|
||||
) local mSize = q2 and 48 or 64
|
||||
@@ -146398,11 +146517,28 @@ local q2 = variant == "q2"
|
||||
local batches= {}
|
||||
local orders= {}
|
||||
local skipped = 0
|
||||
-- lightmaps: record 1 = a white luxel (sky / liquids), 2 = black (unlit faces), then one per lit face
|
||||
local LT = lumps.lighting
|
||||
local useLight = LT.size > 0 and not (opts and opts.lightmaps == false)
|
||||
local rgb = variant == "hl" or q2
|
||||
local lit = opts and opts.lit
|
||||
if lit and (buffer.len(lit) < 8 or buffer.readstring(lit, 0, 4) ~= "QLIT" or buffer.readi32(lit, 4) ~= 1) then
|
||||
lit = nil
|
||||
end
|
||||
local lmSizes= { { 1, 1 }, { 1, 1 } }
|
||||
local lmFaces= { { face = -1, ofs = -1, smin = 0, tmin = 0, styles = 0 }, { face = -1, ofs = -1, smin = 0, tmin = 0, styles = 0 } }
|
||||
local lumaTotal = 0
|
||||
for f = 0, nF - 1 do
|
||||
local __DARKLUA_CONTINUE_7=false repeat local fo = base + Fc.at + f * 20
|
||||
local plane, side, firstEdge, numEdges, texinfo = readu16(b, fo), readu16(b, fo + 2), readi32(b, fo + 4), readu16(b, fo + 8), readu16(b, fo + 10)
|
||||
local model = faceModel[f + 1]
|
||||
if not model or numEdges < 3 or firstEdge < 0 or firstEdge + numEdges > nSE or plane >= nP or texinfo >= nTI then
|
||||
local __DARKLUA_CONTINUE_7=false repeat local fo = base + Fc.at + f * fSize
|
||||
local plane = (big and{(readi32(b, fo) )}or{(readu16(b, fo)
|
||||
)})[1] local side = (big and{(readi32(b, fo + 4) )}or{(readu16(b, fo + 2)
|
||||
)})[1] local firstEdge = (big and{(readi32(b, fo + 8) )}or{(readi32(b, fo + 4)
|
||||
)})[1] local numEdges = (big and{(readi32(b, fo + 12) )}or{(readu16(b, fo + 8)
|
||||
)})[1] local texinfo = (big and{(readi32(b, fo + 16) )}or{(readu16(b, fo + 10)
|
||||
)})[1] local styles = (big and((fo + 20 ))or((fo + 12
|
||||
))) local lightofs = (big and{(readi32(b, fo + 24) )}or{(readi32(b, fo + 16)
|
||||
)})[1] local model = faceModel[f + 1]
|
||||
if not model or numEdges < 3 or plane < 0 or texinfo < 0 or firstEdge < 0 or firstEdge + numEdges > nSE or plane >= nP or texinfo >= nTI then
|
||||
skipped =skipped+ 1
|
||||
__DARKLUA_CONTINUE_7=true break
|
||||
end
|
||||
@@ -146415,8 +146551,14 @@ __DARKLUA_CONTINUE_7=true break
|
||||
ok = false
|
||||
break
|
||||
end
|
||||
local v = (se >= 0 and{(readu16(b, base + E.at + e * 4) )}or{(readu16(b, base + E.at + e * 4 + 2)
|
||||
)})[1] if v >= nV then
|
||||
local eo = base + E.at + e * eSize
|
||||
local v
|
||||
if big then
|
||||
v = (se >= 0 and{(readu32(b, eo) )}or{(readu32(b, eo + 4)
|
||||
)})[1] else
|
||||
v = (se >= 0 and{(readu16(b, eo) )}or{(readu16(b, eo + 2)
|
||||
)})[1] end
|
||||
if v >= nV then
|
||||
ok = false
|
||||
break
|
||||
end
|
||||
@@ -146459,6 +146601,37 @@ __DARKLUA_CONTINUE_7=true break
|
||||
local tio = base + TI.at + texinfo * tiSize
|
||||
local sx, sy, sz, so = readf32(b, tio), readf32(b, tio + 4), readf32(b, tio + 8), readf32(b, tio + 12)
|
||||
local tx, ty, tz, to = readf32(b, tio + 16), readf32(b, tio + 20), readf32(b, tio + 24), readf32(b, tio + 28)
|
||||
local rec = 2
|
||||
if useLight then
|
||||
local tflags = readi32(b, tio + (q2 and 32 or 36))
|
||||
local special = (q2 and{(bit32.band(tflags, 0x8C) ~= 0 )}or{(bit32.band(tflags, 1) ~= 0
|
||||
)})[1] if special then
|
||||
rec = 1
|
||||
elseif lightofs >= 0 then
|
||||
local smin, smax, tmin, tmax= math.huge, -math.huge, math.huge, -math.huge
|
||||
for k = 0, n - 1 do
|
||||
local x, y, z = poly[k * 3 + 1], poly[k * 3 + 2], poly[k * 3 + 3]
|
||||
local sv, tv = x * sx + y * sy + z * sz + so, x * tx + y * ty + z * tz + to
|
||||
smin, smax = math.min(smin, sv), math.max(smax, sv)
|
||||
tmin, tmax = math.min(tmin, tv), math.max(tmax, tv)
|
||||
end
|
||||
local bs, bt0 = math.floor(smin / 16), math.floor(tmin / 16)
|
||||
local lw, lh = math.ceil((smax ) / 16) - bs + 1, math.ceil((tmax ) / 16) - bt0 + 1
|
||||
local ns = 0
|
||||
for k = 0, 3 do
|
||||
if readu8(b, styles + k) ~= 255 then
|
||||
ns =ns+ 1
|
||||
end
|
||||
end
|
||||
local bytes = lw * lh * (rgb and 3 or 1)
|
||||
lumaTotal =lumaTotal+ lw * lh
|
||||
if ns > 0 and lw > 0 and lh > 0 and lw <= 256 and lh <= 256 and lightofs + bytes <= LT.size and lumaTotal <= maxPixels then
|
||||
table.insert(lmSizes, { lw, lh })
|
||||
table.insert(lmFaces, { face = f, ofs = lightofs, smin = bs * 16, tmin = bt0 * 16, styles = ns })
|
||||
rec = #lmFaces
|
||||
end
|
||||
end
|
||||
end
|
||||
local ti = texOf[texinfo + 1] or 0
|
||||
local tex = texs[ti]
|
||||
local w, h = (tex and{tex.width }or{1})[1], (tex and{tex.height }or{1
|
||||
@@ -146466,7 +146639,7 @@ __DARKLUA_CONTINUE_7=true break
|
||||
batches[model] = bm
|
||||
local bt = bm[ti]
|
||||
if not bt then
|
||||
bt = { tex = ti, pos = {}, nrm = {}, uv = {} }
|
||||
bt = { tex = ti, pos = {}, nrm = {}, uv = {}, lm = {} }
|
||||
bm[ti] = bt
|
||||
local ol = orders[model] or {}
|
||||
orders[model] = ol
|
||||
@@ -146479,21 +146652,69 @@ __DARKLUA_CONTINUE_7=true break
|
||||
local pp, nn, uu = batch.pos, batch.nrm, batch.uv
|
||||
pp[#pp + 1], pp[#pp + 2], pp[#pp + 3] = x, y, z
|
||||
nn[#nn + 1], nn[#nn + 2], nn[#nn + 3] = nx, ny, nz
|
||||
uu[#uu + 1] = (x * sx + y * sy + z * sz + so) / w
|
||||
uu[#uu + 1] = (x * tx + y * ty + z * tz + to) / h
|
||||
local sv, tv = x * sx + y * sy + z * sz + so, x * tx + y * ty + z * tz + to
|
||||
uu[#uu + 1] = sv / w
|
||||
uu[#uu + 1] = tv / h
|
||||
local ll = batch.lm
|
||||
ll[#ll + 1], ll[#ll + 2], ll[#ll + 3] = rec, sv, tv
|
||||
end
|
||||
end
|
||||
__DARKLUA_CONTINUE_7=true until true if not __DARKLUA_CONTINUE_7 then break end end
|
||||
local atlasW, atlasH, rects = 0, 0, nil
|
||||
local lmTex= nil
|
||||
local atlas= nil
|
||||
if useLight and #lmFaces > 2 then
|
||||
local W, H, rs, err = Lightmap.pack(lmSizes, maxPixels)
|
||||
if not rs then
|
||||
return nil, err
|
||||
end
|
||||
atlasW, atlasH, rects = W, H, rs
|
||||
local img = Types.Image.new(W, H)
|
||||
local px = img.pixels
|
||||
local la = base + LT.at
|
||||
buffer.writeu32(px, (rs[1].y * W + rs[1].x) * 4, 0xFFFFFFFF)
|
||||
buffer.writeu32(px, (rs[2].y * W + rs[2].x) * 4, 0xFF000000)
|
||||
local placed = {}
|
||||
for i = 3, #lmFaces do
|
||||
local lf, r = lmFaces[i], rs[i]
|
||||
for yy = 0, r.h - 1 do
|
||||
for xx = 0, r.w - 1 do
|
||||
local k = yy * r.w + xx
|
||||
local cr, cg, cb= 0, 0, 0
|
||||
if rgb then
|
||||
local o = la + lf.ofs + k * 3
|
||||
cr, cg, cb = readu8(b, o), readu8(b, o + 1), readu8(b, o + 2)
|
||||
elseif lit and 8 + (lf.ofs + k) * 3 + 3 <= buffer.len(lit) then
|
||||
local o = 8 + (lf.ofs + k) * 3
|
||||
cr, cg, cb = readu8(lit, o), readu8(lit, o + 1), readu8(lit, o + 2)
|
||||
else
|
||||
local v = readu8(b, la + lf.ofs + k)
|
||||
cr, cg, cb = v, v, v
|
||||
end
|
||||
writeu32(px, ((r.y + yy) * W + r.x + xx) * 4, 0xFF000000 + (cb ) * 65536 + (cg ) * 256 + (cr ))
|
||||
end
|
||||
end
|
||||
table.insert(placed, { face = lf.face, x = r.x, y = r.y, w = r.w, h = r.h, styles = lf.styles })
|
||||
end
|
||||
for i = 1, #lmFaces do
|
||||
Lightmap.border(px, W, rs[i])
|
||||
end
|
||||
img.meta.source = { format = "bsp", lightmap = "atlas", coloured = rgb or lit ~= nil }
|
||||
atlas = img
|
||||
scene.meta.lightmaps = placed
|
||||
lmTex = #texs + 1
|
||||
end
|
||||
for i, t in __iter(texs) do
|
||||
local tex= { name = t.name, image = t.image, uri = (t.image and{nil }or{((q2 and(("textures/" .. t.name .. ".wal" ))or(t.name))) })[1]}
|
||||
table.insert(scene.textures, tex)
|
||||
local mat = Scene.material(t.name)
|
||||
mat.metallic = 0
|
||||
local extras= { bsp = { texels = t.texels and true or nil, width = t.width, height = t.height } }
|
||||
mat.extras = extras
|
||||
mat.extras = { bsp = { texels = t.texels and true or nil, width = t.width, height = t.height } }
|
||||
mat.extras = { bsp = { texels = t.texels and true or nil, width = t.width, height = t.height, lightmap = lmTex, lightmapTexCoord = lmTex and 1 or nil } }
|
||||
table.insert(scene.materials, mat)
|
||||
end
|
||||
if atlas then
|
||||
table.insert(scene.textures, { name = "lightmap", image = atlas })
|
||||
end
|
||||
local root= { name = "bsp", children = {} }
|
||||
table.insert(scene.nodes, root)
|
||||
scene.roots[1] = 1
|
||||
@@ -146515,8 +146736,23 @@ table.insert(scene.materials, mat)
|
||||
for k = 0, nv - 1 do
|
||||
writeu32(ib, k * 4, k)
|
||||
end
|
||||
local attrs= { POSITION = Scene.accessor(pb, "f32", 3, nv), NORMAL = Scene.accessor(nb, "f32", 3, nv), TEXCOORD_0 = Scene.accessor(ub, "f32", 2, nv) }
|
||||
if rects then
|
||||
local lb = buffer.create(nv * 8)
|
||||
for k = 0, nv - 1 do
|
||||
local rec, sv, tv = bt.lm[k * 3 + 1], bt.lm[k * 3 + 2], bt.lm[k * 3 + 3]
|
||||
local r, lf = rects[rec], lmFaces[rec]
|
||||
local lu, lv = r.x + 0.5, r.y + 0.5
|
||||
if rec > 2 then
|
||||
lu, lv = r.x + (sv - lf.smin) / 16 + 0.5, r.y + (tv - lf.tmin) / 16 + 0.5
|
||||
end
|
||||
writef32(lb, k * 8, lu / atlasW)
|
||||
writef32(lb, k * 8 + 4, lv / atlasH)
|
||||
end
|
||||
attrs.TEXCOORD_1 = Scene.accessor(lb, "f32", 2, nv)
|
||||
end
|
||||
table.insert(mesh.primitives, {
|
||||
attributes = { POSITION = Scene.accessor(pb, "f32", 3, nv), NORMAL = Scene.accessor(nb, "f32", 3, nv), TEXCOORD_0 = Scene.accessor(ub, "f32", 2, nv) },
|
||||
attributes = attrs,
|
||||
indices = Scene.accessor(ib, "u32", 1, nv),
|
||||
mode = "triangles" ,
|
||||
material = (ti > 0 and{ti }or{nil})[1],
|
||||
@@ -146531,7 +146767,7 @@ table.insert(scene.materials, mat)
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Brush
|
||||
return Quake
|
||||
end
|
||||
__FILES["model/bsp/Quake3"] = function()
|
||||
local __compat = __compat local buffer, __iter = __compat.buffer, __compat.__iter --!strict
|
||||
@@ -146761,6 +146997,394 @@ end
|
||||
|
||||
return Quake3
|
||||
end
|
||||
__FILES["model/bsp/Tree"] = function()
|
||||
local __compat = __compat local buffer, bit32, type, __iter = __compat.buffer, __compat.bit32, __compat.type, __compat.__iter --!strict
|
||||
-- model/bsp/Tree: the BSP tree, collision hulls and visibility of Quake 1 / BSP2 / 2PSB / Half-Life / Quake II / Quake
|
||||
-- III maps as tables (bspfile.h layouts; Unofficial Quake 3 Map Specs), plus point queries: the leaf and contents at a
|
||||
-- point (Quake 1 hulls 0..3 through nodes / clipnodes as SV_HullPointContents; Quake II leaf contents; Quake III the
|
||||
-- contents of the leaf's brushes) and the potentially visible set of a leaf (run-length zero bytes, as Mod_DecompressVis).
|
||||
local Tree = {}
|
||||
|
||||
local readi32, readu16, readi16, readf32, readu8, readu32 = buffer.readi32, buffer.readu16, buffer.readi16, buffer.readf32, buffer.readu8, buffer.readu32
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local function v3(b, o, kind)
|
||||
if kind == "f" then
|
||||
return { readf32(b, o), readf32(b, o + 4), readf32(b, o + 8) }
|
||||
elseif kind == "i" then
|
||||
return { readi32(b, o), readi32(b, o + 4), readi32(b, o + 8) }
|
||||
end
|
||||
return { readi16(b, o), readi16(b, o + 2), readi16(b, o + 4) }
|
||||
end
|
||||
|
||||
local function slice(b, base, l)
|
||||
if l.size <= 0 then
|
||||
return nil
|
||||
end
|
||||
local out = buffer.create(l.size)
|
||||
buffer.copy(out, 0, b, base + l.at, l.size)
|
||||
return out
|
||||
end
|
||||
|
||||
-- nil, message past maxItems
|
||||
function Tree.read(b, base, lumps, variant, maxItems)
|
||||
local limit = maxItems or 2 ^ 22
|
||||
local q2, q3 = variant == "q2", variant == "q3"
|
||||
local bsp2, rmq = variant == "bsp2", variant == "2psb"
|
||||
local d= { variant = variant, planes = {}, nodes = {}, leaves = {}, models = {} }
|
||||
local function count(l, size)
|
||||
return (l and{(math.floor(l.size / size ))}or{0
|
||||
})[1] end
|
||||
local P = lumps.planes
|
||||
local pSize = q3 and 16 or 20
|
||||
local np = count(P, pSize)
|
||||
if np > limit then
|
||||
return nil, "limit: too many planes"
|
||||
end
|
||||
for i = 0, np - 1 do
|
||||
local o = base + P.at + i * pSize
|
||||
d.planes[i + 1] = { normal = v3(b, o, "f"), dist = readf32(b, o + 12), type = (q3 and{nil }or{(readi32(b, o + 16) )})[1]}
|
||||
end
|
||||
local N = lumps.nodes
|
||||
local nSize = q3 and 36 or(q2 and 28 or(bsp2 and 44 or(rmq and 32 or 24
|
||||
))) local nn = count(N, nSize)
|
||||
if nn > limit then
|
||||
return nil, "limit: too many nodes"
|
||||
end
|
||||
for i = 0, nn - 1 do
|
||||
local o = base + N.at + i * nSize
|
||||
local node
|
||||
if q3 or q2 or bsp2 or rmq then
|
||||
local bk = q3 and"i" or(bsp2 and"f" or"s"
|
||||
) local bs = bk == "s" and 6 or 12
|
||||
node = { plane = readi32(b, o), children = { readi32(b, o + 4), readi32(b, o + 8) }, mins = v3(b, o + 12, bk), maxs = v3(b, o + 12 + bs, bk) }
|
||||
if not q3 then
|
||||
local fo = o + 12 + bs * 2
|
||||
if q2 then
|
||||
node.firstFace, node.numFaces = readu16(b, fo), readu16(b, fo + 2)
|
||||
else
|
||||
node.firstFace, node.numFaces = readu32(b, fo), readu32(b, fo + 4)
|
||||
end
|
||||
end
|
||||
else
|
||||
local c = {}
|
||||
for k = 0, 1 do
|
||||
local p = readu16(b, o + 4 + k * 2)
|
||||
c[k + 1] = (p < nn and{p }or{(-(65535 - p) - 1 -- Quakespasm Mod_LoadNodes_S: leaf 65535 - p
|
||||
)})[1] end
|
||||
node = { plane = readi32(b, o), children = c, mins = v3(b, o + 8, "s"), maxs = v3(b, o + 14, "s"), firstFace = readu16(b, o + 20), numFaces = readu16(b, o + 22) }
|
||||
end
|
||||
d.nodes[i + 1] = node
|
||||
end
|
||||
local L = lumps.leaves or lumps.leafs
|
||||
local lSize = q3 and 48 or(q2 and 28 or(bsp2 and 44 or(rmq and 32 or 28
|
||||
))) local nl = count(L, lSize)
|
||||
if nl > limit then
|
||||
return nil, "limit: too many leaves"
|
||||
end
|
||||
for i = 0, nl - 1 do
|
||||
local o = base + L.at + i * lSize
|
||||
local leaf
|
||||
if q3 then
|
||||
leaf = { cluster = readi32(b, o), area = readi32(b, o + 4), mins = v3(b, o + 8, "i"), maxs = v3(b, o + 20, "i"),
|
||||
firstFace = readi32(b, o + 32), numFaces = readi32(b, o + 36), firstBrush = readi32(b, o + 40), numBrushes = readi32(b, o + 44) }
|
||||
elseif q2 then
|
||||
leaf = { contents = readi32(b, o), cluster = readi16(b, o + 4), area = readi16(b, o + 6), mins = v3(b, o + 8, "s"), maxs = v3(b, o + 14, "s"),
|
||||
firstFace = readu16(b, o + 20), numFaces = readu16(b, o + 22), firstBrush = readu16(b, o + 24), numBrushes = readu16(b, o + 26) }
|
||||
else
|
||||
local bk = bsp2 and"f" or"s"
|
||||
local bs = bsp2 and 12 or 6
|
||||
local fo = o + 8 + bs * 2
|
||||
local big = bsp2 or rmq
|
||||
leaf = { contents = readi32(b, o), visofs = readi32(b, o + 4), mins = v3(b, o + 8, bk), maxs = v3(b, o + 8 + bs, bk),
|
||||
firstFace = (big and{(readu32(b, fo) )}or{(readu16(b, fo))})[1], numFaces = (big and{(readu32(b, fo + 4) )}or{(readu16(b, fo + 2) )})[1]}
|
||||
local ao = fo + (big and 8 or 4)
|
||||
leaf.ambient = { readu8(b, ao), readu8(b, ao + 1), readu8(b, ao + 2), readu8(b, ao + 3) }
|
||||
end
|
||||
d.leaves[i + 1] = leaf
|
||||
end
|
||||
if not q2 and not q3 then
|
||||
local C = lumps.clipnodes
|
||||
local cSize = (bsp2 or rmq )and 12 or 8
|
||||
local nc = count(C, cSize)
|
||||
if nc > limit then
|
||||
return nil, "limit: too many clipnodes"
|
||||
end
|
||||
local cl= {}
|
||||
for i = 0, nc - 1 do
|
||||
local o = base + C.at + i * cSize
|
||||
local c
|
||||
if cSize == 12 then
|
||||
c = { readi32(b, o + 4), readi32(b, o + 8) }
|
||||
else
|
||||
c = {}
|
||||
for k = 0, 1 do
|
||||
local p = readu16(b, o + 4 + k * 2)
|
||||
c[k + 1] = (p > nc and((p - 65536 ))or(p -- Quakespasm Mod_LoadClipnodes
|
||||
)) end
|
||||
end
|
||||
cl[i + 1] = { plane = readi32(b, o), children = c }
|
||||
end
|
||||
d.clipnodes = cl
|
||||
end
|
||||
local M = lumps.models
|
||||
local mSize = q3 and 40 or(q2 and 48 or 64
|
||||
) for i = 0, count(M, mSize) - 1 do
|
||||
local o = base + M.at + i * mSize
|
||||
local m
|
||||
if q3 then
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), headnodes = { 0 }, firstFace = readi32(b, o + 24), numFaces = readi32(b, o + 28) }
|
||||
elseif q2 then
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), origin = v3(b, o + 24, "f"), headnodes = { readi32(b, o + 36) }, firstFace = readi32(b, o + 40), numFaces = readi32(b, o + 44) }
|
||||
else
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), origin = v3(b, o + 24, "f"),
|
||||
headnodes = { readi32(b, o + 36), readi32(b, o + 40), readi32(b, o + 44), readi32(b, o + 48) },
|
||||
visleafs = readi32(b, o + 52), firstFace = readi32(b, o + 56), numFaces = readi32(b, o + 60) }
|
||||
end
|
||||
d.models[i + 1] = m
|
||||
end
|
||||
if q2 or q3 then
|
||||
local B, S = lumps.brushes, lumps.brushsides
|
||||
local nb, ns = count(B, 12), count(S, q3 and 8 or 4)
|
||||
if nb > limit or ns > limit then
|
||||
return nil, "limit: too many brushes"
|
||||
end
|
||||
local brushes, sides= {}, {}
|
||||
for i = 0, nb - 1 do
|
||||
local o = base + B.at + i * 12
|
||||
brushes[i + 1] = { firstSide = readi32(b, o), numSides = readi32(b, o + 4), contents = readi32(b, o + 8) } -- Quake III: a texture index
|
||||
end
|
||||
for i = 0, ns - 1 do
|
||||
if q3 then
|
||||
local o = base + S.at + i * 8
|
||||
sides[i + 1] = { plane = readi32(b, o), texinfo = readi32(b, o + 4) }
|
||||
else
|
||||
local o = base + S.at + i * 4
|
||||
sides[i + 1] = { plane = readu16(b, o), texinfo = readi16(b, o + 2) }
|
||||
end
|
||||
end
|
||||
d.brushes, d.brushSides = brushes, sides
|
||||
local LB = lumps.leafbrushes
|
||||
local lbs= {}
|
||||
for i = 0, count(LB, q3 and 4 or 2) - 1 do
|
||||
lbs[i + 1] = (q3 and{(readi32(b, base + LB.at + i * 4) )}or{(readu16(b, base + LB.at + i * 2)
|
||||
)})[1] end
|
||||
d.leafBrushes = lbs
|
||||
if q3 then
|
||||
-- brush contents come from the texture's contents flags
|
||||
local T = lumps.textures
|
||||
local nt = count(T, 72)
|
||||
for _, br in __iter(brushes) do
|
||||
local t = br.contents
|
||||
br.contents = (t >= 0 and t < nt and{(readi32(b, base + T.at + t * 72 + 68) )}or{0
|
||||
})[1] end
|
||||
end
|
||||
end
|
||||
local VL = lumps.visibility or lumps.visdata
|
||||
d.visibility = (VL and{(slice(b, base, VL) )}or{nil
|
||||
})[1] local vis = d.visibility
|
||||
if vis and q2 and buffer.len(vis) >= 4 then
|
||||
d.numClusters = readi32(vis, 0)
|
||||
elseif vis and q3 and buffer.len(vis) >= 8 then
|
||||
d.numClusters, d.clusterBytes = readi32(vis, 0), readi32(vis, 4)
|
||||
end
|
||||
return d, nil
|
||||
end
|
||||
|
||||
local function side(d, plane, x, y, z)
|
||||
local p = d.planes[plane + 1]
|
||||
if not p then
|
||||
return 0
|
||||
end
|
||||
local n = p.normal
|
||||
return x * n[1] + y * n[2] + z * n[3] - p.dist
|
||||
end
|
||||
|
||||
-- the leaf (0-based) at a point, walking the tree of model (0-based, default 0)
|
||||
function Tree.leafAt(d, x, y, z, model)
|
||||
local m = d.models[(model or 0) + 1]
|
||||
local node = (m and{m.headnodes[1] }or{0
|
||||
})[1] for _ = 1, #d.nodes + 1 do
|
||||
if node < 0 then
|
||||
return -node - 1
|
||||
end
|
||||
local nd = d.nodes[node + 1]
|
||||
if not nd then
|
||||
return nil
|
||||
end
|
||||
node = (side(d, nd.plane, x, y, z) >= 0 and{nd.children[1] }or{nd.children[2]
|
||||
})[1] end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Quake 1: CONTENTS_* (-1 empty, -2 solid, -3 water, -4 slime, -5 lava, -6 sky) in hull 0..3; Quake II: the leaf's
|
||||
-- CONTENTS_* bits; Quake III: the contents bits of the brushes holding the point
|
||||
function Tree.contents(d, x, y, z, hull, model)
|
||||
local h = hull or 0
|
||||
if (d.variant == "q1" or d.variant == "hl" or d.variant == "bsp2" or d.variant == "2psb") and h > 0 then
|
||||
local m = d.models[(model or 0) + 1]
|
||||
local cl = d.clipnodes or {}
|
||||
local node = (m and{m.headnodes[h + 1] or -1 }or{(-1
|
||||
)})[1] for _ = 1, #cl + 1 do
|
||||
if node < 0 then
|
||||
return node
|
||||
end
|
||||
local c = cl[node + 1]
|
||||
if not c then
|
||||
return nil
|
||||
end
|
||||
node = (side(d, c.plane, x, y, z) >= 0 and{c.children[1] }or{c.children[2]
|
||||
})[1] end
|
||||
return nil
|
||||
end
|
||||
local leaf = Tree.leafAt(d, x, y, z, model)
|
||||
local lf = leaf and d.leaves[leaf + 1]
|
||||
if not lf then
|
||||
return nil
|
||||
end
|
||||
if d.variant ~= "q3" then
|
||||
return lf.contents
|
||||
end
|
||||
local out = 0
|
||||
local lbs, brushes, sides= d.leafBrushes or {}, d.brushes or {}, d.brushSides or {}
|
||||
local fb, nb= lf.firstBrush or 0, lf.numBrushes or 0
|
||||
for k = fb, fb + nb - 1 do
|
||||
local br = brushes[(lbs[k + 1] or -1) + 1]
|
||||
if br then
|
||||
local inside = true
|
||||
for s = br.firstSide, br.firstSide + br.numSides - 1 do
|
||||
local sd = sides[s + 1]
|
||||
if not sd or side(d, sd.plane, x, y, z) > 0 then
|
||||
inside = false
|
||||
break
|
||||
end
|
||||
end
|
||||
if inside then
|
||||
out = bit32.bor(out, br.contents)
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function decompress(vis, at, bytes, out)
|
||||
local n = buffer.len(vis)
|
||||
local i = 0
|
||||
while i < bytes and at < n do
|
||||
local c = readu8(vis, at)
|
||||
if c ~= 0 then
|
||||
for bit = 0, 7 do
|
||||
if bit32.band(c, bit32.lshift(1, bit)) ~= 0 then
|
||||
out[i * 8 + bit + 1] = true
|
||||
end
|
||||
end
|
||||
i =i+ 1
|
||||
at =at+ 1
|
||||
else
|
||||
if at + 1 >= n then
|
||||
break
|
||||
end
|
||||
i =i+ readu8(vis, at + 1)
|
||||
at =at+ 2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the leaves (0-based, sorted) potentially visible from a leaf; nil without visibility data (everything visible)
|
||||
function Tree.pvs(d, leaf)
|
||||
local lf = d.leaves[leaf + 1]
|
||||
local vis = d.visibility
|
||||
if not lf or not vis then
|
||||
return nil
|
||||
end
|
||||
local out= {}
|
||||
if d.variant == "q2" or d.variant == "q3" then
|
||||
local nc = d.numClusters or 0
|
||||
local c = lf.cluster or -1
|
||||
if c < 0 or c >= nc then
|
||||
return out
|
||||
end
|
||||
local bits= {}
|
||||
if d.variant == "q2" then
|
||||
if 4 + nc * 8 > buffer.len(vis) then
|
||||
return nil
|
||||
end
|
||||
decompress(vis, readi32(vis, 4 + c * 8), math.floor((nc + 7) / 8), bits)
|
||||
else
|
||||
local sz = d.clusterBytes or 0
|
||||
local o = 8 + c * sz
|
||||
for k = 0, sz - 1 do
|
||||
if o + k < buffer.len(vis) then
|
||||
local v = readu8(vis, o + k)
|
||||
for bit = 0, 7 do
|
||||
if bit32.band(v, bit32.lshift(1, bit)) ~= 0 then
|
||||
bits[k * 8 + bit + 1] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for i, l in __iter(d.leaves) do
|
||||
local cl = l.cluster or -1
|
||||
if cl >= 0 and bits[cl + 1] then
|
||||
table.insert(out, i - 1)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
-- Quake 1: bit k = leaf k + 1 (leaf 0 is the shared solid leaf), visleafs bits
|
||||
local visofs = lf.visofs or -1
|
||||
if leaf == 0 or visofs < 0 then
|
||||
return out
|
||||
end
|
||||
local n = d.models[1] and d.models[1].visleafs or (#d.leaves - 1)
|
||||
local bits= {}
|
||||
decompress(vis, visofs, math.floor((n + 7) / 8), bits)
|
||||
for k = 0, n - 1 do
|
||||
if bits[k + 1] then
|
||||
table.insert(out, k + 1)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return Tree
|
||||
end
|
||||
__FILES["model/dae"] = function()
|
||||
local __compat = __compat local buffer, type, __iter = __compat.buffer, __compat.type, __compat.__iter --!strict
|
||||
-- model/dae: COLLADA 1.4.1 / 1.5.0 (Khronos specifications; .dae XML, .zae zip with manifest.xml). Supports: geometry,
|
||||
|
||||
+141
-139
@@ -404,143 +404,145 @@
|
||||
[145503, "src/Rosetta/model/b3d/init.luau"],
|
||||
[145558, "src/Rosetta/model/b3d/Read.luau"],
|
||||
[146129, "src/Rosetta/model/bsp/init.luau"],
|
||||
[146274, "src/Rosetta/model/bsp/Brush.luau"],
|
||||
[146537, "src/Rosetta/model/bsp/Quake3.luau"],
|
||||
[146765, "src/Rosetta/model/dae/init.luau"],
|
||||
[146954, "src/Rosetta/model/dae/Math.luau"],
|
||||
[147001, "src/Rosetta/model/dae/Read.luau"],
|
||||
[148851, "src/Rosetta/model/dae/Write.luau"],
|
||||
[149220, "src/Rosetta/model/fbx/init.luau"],
|
||||
[149368, "src/Rosetta/model/fbx/Ascii.luau"],
|
||||
[149638, "src/Rosetta/model/fbx/Binary.luau"],
|
||||
[150093, "src/Rosetta/model/fbx/Read.luau"],
|
||||
[151455, "src/Rosetta/model/fbx/Write.luau"],
|
||||
[152440, "src/Rosetta/model/fbx/Xform.luau"],
|
||||
[152481, "src/Rosetta/model/gltf/init.luau"],
|
||||
[152718, "src/Rosetta/model/gltf/Pack.luau"],
|
||||
[152787, "src/Rosetta/model/gltf/Read.luau"],
|
||||
[153518, "src/Rosetta/model/gltf/Write.luau"],
|
||||
[154198, "src/Rosetta/model/iqm/init.luau"],
|
||||
[154274, "src/Rosetta/model/iqm/Read.luau"],
|
||||
[154629, "src/Rosetta/model/iqm/Write.luau"],
|
||||
[155316, "src/Rosetta/model/ldraw/init.luau"],
|
||||
[155394, "src/Rosetta/model/ldraw/Colors.luau"],
|
||||
[155712, "src/Rosetta/model/ldraw/Read.luau"],
|
||||
[156289, "src/Rosetta/model/lwo/init.luau"],
|
||||
[156365, "src/Rosetta/model/lwo/Build.luau"],
|
||||
[156827, "src/Rosetta/model/lwo/Parse.luau"],
|
||||
[157370, "src/Rosetta/model/map/init.luau"],
|
||||
[157687, "src/Rosetta/model/map/Build.luau"],
|
||||
[157930, "src/Rosetta/model/map/Parse.luau"],
|
||||
[158315, "src/Rosetta/model/map/Write.luau"],
|
||||
[158461, "src/Rosetta/model/mca/init.luau"],
|
||||
[158803, "src/Rosetta/model/mca/Chunk.luau"],
|
||||
[158936, "src/Rosetta/model/mca/Lz4Block.luau"],
|
||||
[159006, "src/Rosetta/model/md2/init.luau"],
|
||||
[159205, "src/Rosetta/model/md3/init.luau"],
|
||||
[159433, "src/Rosetta/model/md5/init.luau"],
|
||||
[159514, "src/Rosetta/model/md5/Read.luau"],
|
||||
[159947, "src/Rosetta/model/mdl/init.luau"],
|
||||
[160029, "src/Rosetta/model/mdl/HalfLife.luau"],
|
||||
[160357, "src/Rosetta/model/mdl/Quake.luau"],
|
||||
[160575, "src/Rosetta/model/ms3d/init.luau"],
|
||||
[160647, "src/Rosetta/model/ms3d/Read.luau"],
|
||||
[161093, "src/Rosetta/model/ms3d/Write.luau"],
|
||||
[161562, "src/Rosetta/model/obj/init.luau"],
|
||||
[162304, "src/Rosetta/model/obj/Mtl.luau"],
|
||||
[162591, "src/Rosetta/model/off/init.luau"],
|
||||
[163053, "src/Rosetta/model/ply/init.luau"],
|
||||
[164158, "src/Rosetta/model/ply/Binary.luau"],
|
||||
[164359, "src/Rosetta/model/pmx/init.luau"],
|
||||
[164428, "src/Rosetta/model/pmx/Build.luau"],
|
||||
[164724, "src/Rosetta/model/pmx/Pmd.luau"],
|
||||
[165059, "src/Rosetta/model/pmx/Read.luau"],
|
||||
[165463, "src/Rosetta/model/qb/init.luau"],
|
||||
[165856, "src/Rosetta/model/rbxm/init.luau"],
|
||||
[166448, "src/Rosetta/model/rbxmx/init.luau"],
|
||||
[167101, "src/Rosetta/model/robloxmesh/init.luau"],
|
||||
[167760, "src/Rosetta/model/robloxmesh/Parse.luau"],
|
||||
[168217, "src/Rosetta/model/robloxmesh/Write.luau"],
|
||||
[168326, "src/Rosetta/model/schematic/init.luau"],
|
||||
[168647, "src/Rosetta/model/schematic/Common.luau"],
|
||||
[168767, "src/Rosetta/model/schematic/Litematic.luau"],
|
||||
[168978, "src/Rosetta/model/schematic/Mcedit.luau"],
|
||||
[169222, "src/Rosetta/model/schematic/Sponge.luau"],
|
||||
[169351, "src/Rosetta/model/smd/init.luau"],
|
||||
[169418, "src/Rosetta/model/smd/Read.luau"],
|
||||
[169687, "src/Rosetta/model/smd/Write.luau"],
|
||||
[169930, "src/Rosetta/model/stl/init.luau"],
|
||||
[170380, "src/Rosetta/model/stl/Decode.luau"],
|
||||
[170454, "src/Rosetta/model/usda/init.luau"],
|
||||
[170663, "src/Rosetta/model/usda/Build.luau"],
|
||||
[171941, "src/Rosetta/model/usda/Parse.luau"],
|
||||
[172739, "src/Rosetta/model/usda/Write.luau"],
|
||||
[173556, "src/Rosetta/model/vox/init.luau"],
|
||||
[174442, "src/Rosetta/model/vox/Grid.luau"],
|
||||
[174499, "src/Rosetta/model/vrml/init.luau"],
|
||||
[174565, "src/Rosetta/model/x/init.luau"],
|
||||
[174695, "src/Rosetta/model/x/Build.luau"],
|
||||
[175258, "src/Rosetta/model/x/Parse.luau"],
|
||||
[175555, "src/Rosetta/model/x3d/init.luau"],
|
||||
[175633, "src/Rosetta/music/669/init.luau"],
|
||||
[175839, "src/Rosetta/music/ahx/init.luau"],
|
||||
[175890, "src/Rosetta/music/far/init.luau"],
|
||||
[176119, "src/Rosetta/music/hmp/init.luau"],
|
||||
[176383, "src/Rosetta/music/hvl/init.luau"],
|
||||
[176434, "src/Rosetta/music/it/init.luau"],
|
||||
[176852, "src/Rosetta/music/it/Itsex.luau"],
|
||||
[176968, "src/Rosetta/music/med/init.luau"],
|
||||
[177615, "src/Rosetta/music/midi/init.luau"],
|
||||
[177758, "src/Rosetta/music/midi/Smf.luau"],
|
||||
[178169, "src/Rosetta/music/mod/init.luau"],
|
||||
[178522, "src/Rosetta/music/mtm/init.luau"],
|
||||
[178744, "src/Rosetta/music/mus/init.luau"],
|
||||
[178901, "src/Rosetta/music/okt/init.luau"],
|
||||
[179182, "src/Rosetta/music/ptm/init.luau"],
|
||||
[179404, "src/Rosetta/music/s3m/init.luau"],
|
||||
[179640, "src/Rosetta/music/sf2/init.luau"],
|
||||
[179683, "src/Rosetta/music/stm/init.luau"],
|
||||
[179914, "src/Rosetta/music/ult/init.luau"],
|
||||
[180218, "src/Rosetta/music/umx/init.luau"],
|
||||
[180434, "src/Rosetta/music/xm/init.luau"],
|
||||
[180995, "src/Rosetta/music/xmi/init.luau"],
|
||||
[181203, "src/Rosetta/ops/AudioOps.luau"],
|
||||
[181652, "src/Rosetta/ops/ImageOps/init.luau"],
|
||||
[182296, "src/Rosetta/ops/ImageOps/Kernels.luau"],
|
||||
[182806, "src/Rosetta/ops/LayerOps.luau"],
|
||||
[182899, "src/Rosetta/ops/MeshOps/init.luau"],
|
||||
[183935, "src/Rosetta/ops/MeshOps/Greedy.luau"],
|
||||
[184102, "src/Rosetta/ops/MusicOps.luau"],
|
||||
[184450, "src/Rosetta/playlist/m3u/init.luau"],
|
||||
[184803, "src/Rosetta/playlist/pls/init.luau"],
|
||||
[184981, "src/Rosetta/playlist/xspf/init.luau"],
|
||||
[185184, "src/Rosetta/subtitle/ass/init.luau"],
|
||||
[185628, "src/Rosetta/subtitle/srt/init.luau"],
|
||||
[185801, "src/Rosetta/subtitle/vtt/init.luau"],
|
||||
[186004, "src/Rosetta/vector/svg/init.luau"],
|
||||
[186090, "src/Rosetta/vector/svg/Parse.luau"],
|
||||
[186836, "src/Rosetta/vector/svg/PathData.luau"],
|
||||
[187204, "src/Rosetta/vector/svg/Render.luau"],
|
||||
[187621, "src/Rosetta/vector/svg/Style.luau"],
|
||||
[187993, "src/Rosetta/vector/svg/Write.luau"],
|
||||
[188193, "src/Rosetta/vector/tiled/init.luau"],
|
||||
[188373, "src/Rosetta/vector/tiled/Data.luau"],
|
||||
[188524, "src/Rosetta/vector/tiled/Tmj.luau"],
|
||||
[189155, "src/Rosetta/vector/tiled/Tmx.luau"],
|
||||
[189767, "src/Rosetta/video/avi/init.luau"],
|
||||
[190061, "src/Rosetta/video/fli/init.luau"],
|
||||
[190212, "src/Rosetta/video/flv/init.luau"],
|
||||
[190596, "src/Rosetta/video/flv/Amf.luau"],
|
||||
[190754, "src/Rosetta/video/flv/Mux.luau"],
|
||||
[191035, "src/Rosetta/video/h264/init.luau"],
|
||||
[191174, "src/Rosetta/video/mkv/init.luau"],
|
||||
[191330, "src/Rosetta/video/mkv/Mux.luau"],
|
||||
[191609, "src/Rosetta/video/mkv/Parse.luau"],
|
||||
[192330, "src/Rosetta/video/mp4/init.luau"],
|
||||
[192658, "src/Rosetta/video/mpegps/init.luau"],
|
||||
[192845, "src/Rosetta/video/mpegts/init.luau"],
|
||||
[193083, "src/Rosetta/video/ogv/init.luau"],
|
||||
[193478, "src/Rosetta/video/smk/init.luau"],
|
||||
[193787, "src/Rosetta/video/smk/Audio.luau"],
|
||||
[193907, "src/Rosetta/video/y4m/init.luau"]
|
||||
[146320, "src/Rosetta/model/bsp/Lightmap.luau"],
|
||||
[146386, "src/Rosetta/model/bsp/Quake.luau"],
|
||||
[146773, "src/Rosetta/model/bsp/Quake3.luau"],
|
||||
[147001, "src/Rosetta/model/bsp/Tree.luau"],
|
||||
[147389, "src/Rosetta/model/dae/init.luau"],
|
||||
[147578, "src/Rosetta/model/dae/Math.luau"],
|
||||
[147625, "src/Rosetta/model/dae/Read.luau"],
|
||||
[149475, "src/Rosetta/model/dae/Write.luau"],
|
||||
[149844, "src/Rosetta/model/fbx/init.luau"],
|
||||
[149992, "src/Rosetta/model/fbx/Ascii.luau"],
|
||||
[150262, "src/Rosetta/model/fbx/Binary.luau"],
|
||||
[150717, "src/Rosetta/model/fbx/Read.luau"],
|
||||
[152079, "src/Rosetta/model/fbx/Write.luau"],
|
||||
[153064, "src/Rosetta/model/fbx/Xform.luau"],
|
||||
[153105, "src/Rosetta/model/gltf/init.luau"],
|
||||
[153342, "src/Rosetta/model/gltf/Pack.luau"],
|
||||
[153411, "src/Rosetta/model/gltf/Read.luau"],
|
||||
[154142, "src/Rosetta/model/gltf/Write.luau"],
|
||||
[154822, "src/Rosetta/model/iqm/init.luau"],
|
||||
[154898, "src/Rosetta/model/iqm/Read.luau"],
|
||||
[155253, "src/Rosetta/model/iqm/Write.luau"],
|
||||
[155940, "src/Rosetta/model/ldraw/init.luau"],
|
||||
[156018, "src/Rosetta/model/ldraw/Colors.luau"],
|
||||
[156336, "src/Rosetta/model/ldraw/Read.luau"],
|
||||
[156913, "src/Rosetta/model/lwo/init.luau"],
|
||||
[156989, "src/Rosetta/model/lwo/Build.luau"],
|
||||
[157451, "src/Rosetta/model/lwo/Parse.luau"],
|
||||
[157994, "src/Rosetta/model/map/init.luau"],
|
||||
[158311, "src/Rosetta/model/map/Build.luau"],
|
||||
[158554, "src/Rosetta/model/map/Parse.luau"],
|
||||
[158939, "src/Rosetta/model/map/Write.luau"],
|
||||
[159085, "src/Rosetta/model/mca/init.luau"],
|
||||
[159427, "src/Rosetta/model/mca/Chunk.luau"],
|
||||
[159560, "src/Rosetta/model/mca/Lz4Block.luau"],
|
||||
[159630, "src/Rosetta/model/md2/init.luau"],
|
||||
[159829, "src/Rosetta/model/md3/init.luau"],
|
||||
[160057, "src/Rosetta/model/md5/init.luau"],
|
||||
[160138, "src/Rosetta/model/md5/Read.luau"],
|
||||
[160571, "src/Rosetta/model/mdl/init.luau"],
|
||||
[160653, "src/Rosetta/model/mdl/HalfLife.luau"],
|
||||
[160981, "src/Rosetta/model/mdl/Quake.luau"],
|
||||
[161199, "src/Rosetta/model/ms3d/init.luau"],
|
||||
[161271, "src/Rosetta/model/ms3d/Read.luau"],
|
||||
[161717, "src/Rosetta/model/ms3d/Write.luau"],
|
||||
[162186, "src/Rosetta/model/obj/init.luau"],
|
||||
[162928, "src/Rosetta/model/obj/Mtl.luau"],
|
||||
[163215, "src/Rosetta/model/off/init.luau"],
|
||||
[163677, "src/Rosetta/model/ply/init.luau"],
|
||||
[164782, "src/Rosetta/model/ply/Binary.luau"],
|
||||
[164983, "src/Rosetta/model/pmx/init.luau"],
|
||||
[165052, "src/Rosetta/model/pmx/Build.luau"],
|
||||
[165348, "src/Rosetta/model/pmx/Pmd.luau"],
|
||||
[165683, "src/Rosetta/model/pmx/Read.luau"],
|
||||
[166087, "src/Rosetta/model/qb/init.luau"],
|
||||
[166480, "src/Rosetta/model/rbxm/init.luau"],
|
||||
[167072, "src/Rosetta/model/rbxmx/init.luau"],
|
||||
[167725, "src/Rosetta/model/robloxmesh/init.luau"],
|
||||
[168384, "src/Rosetta/model/robloxmesh/Parse.luau"],
|
||||
[168841, "src/Rosetta/model/robloxmesh/Write.luau"],
|
||||
[168950, "src/Rosetta/model/schematic/init.luau"],
|
||||
[169271, "src/Rosetta/model/schematic/Common.luau"],
|
||||
[169391, "src/Rosetta/model/schematic/Litematic.luau"],
|
||||
[169602, "src/Rosetta/model/schematic/Mcedit.luau"],
|
||||
[169846, "src/Rosetta/model/schematic/Sponge.luau"],
|
||||
[169975, "src/Rosetta/model/smd/init.luau"],
|
||||
[170042, "src/Rosetta/model/smd/Read.luau"],
|
||||
[170311, "src/Rosetta/model/smd/Write.luau"],
|
||||
[170554, "src/Rosetta/model/stl/init.luau"],
|
||||
[171004, "src/Rosetta/model/stl/Decode.luau"],
|
||||
[171078, "src/Rosetta/model/usda/init.luau"],
|
||||
[171287, "src/Rosetta/model/usda/Build.luau"],
|
||||
[172565, "src/Rosetta/model/usda/Parse.luau"],
|
||||
[173363, "src/Rosetta/model/usda/Write.luau"],
|
||||
[174180, "src/Rosetta/model/vox/init.luau"],
|
||||
[175066, "src/Rosetta/model/vox/Grid.luau"],
|
||||
[175123, "src/Rosetta/model/vrml/init.luau"],
|
||||
[175189, "src/Rosetta/model/x/init.luau"],
|
||||
[175319, "src/Rosetta/model/x/Build.luau"],
|
||||
[175882, "src/Rosetta/model/x/Parse.luau"],
|
||||
[176179, "src/Rosetta/model/x3d/init.luau"],
|
||||
[176257, "src/Rosetta/music/669/init.luau"],
|
||||
[176463, "src/Rosetta/music/ahx/init.luau"],
|
||||
[176514, "src/Rosetta/music/far/init.luau"],
|
||||
[176743, "src/Rosetta/music/hmp/init.luau"],
|
||||
[177007, "src/Rosetta/music/hvl/init.luau"],
|
||||
[177058, "src/Rosetta/music/it/init.luau"],
|
||||
[177476, "src/Rosetta/music/it/Itsex.luau"],
|
||||
[177592, "src/Rosetta/music/med/init.luau"],
|
||||
[178239, "src/Rosetta/music/midi/init.luau"],
|
||||
[178382, "src/Rosetta/music/midi/Smf.luau"],
|
||||
[178793, "src/Rosetta/music/mod/init.luau"],
|
||||
[179146, "src/Rosetta/music/mtm/init.luau"],
|
||||
[179368, "src/Rosetta/music/mus/init.luau"],
|
||||
[179525, "src/Rosetta/music/okt/init.luau"],
|
||||
[179806, "src/Rosetta/music/ptm/init.luau"],
|
||||
[180028, "src/Rosetta/music/s3m/init.luau"],
|
||||
[180264, "src/Rosetta/music/sf2/init.luau"],
|
||||
[180307, "src/Rosetta/music/stm/init.luau"],
|
||||
[180538, "src/Rosetta/music/ult/init.luau"],
|
||||
[180842, "src/Rosetta/music/umx/init.luau"],
|
||||
[181058, "src/Rosetta/music/xm/init.luau"],
|
||||
[181619, "src/Rosetta/music/xmi/init.luau"],
|
||||
[181827, "src/Rosetta/ops/AudioOps.luau"],
|
||||
[182276, "src/Rosetta/ops/ImageOps/init.luau"],
|
||||
[182920, "src/Rosetta/ops/ImageOps/Kernels.luau"],
|
||||
[183430, "src/Rosetta/ops/LayerOps.luau"],
|
||||
[183523, "src/Rosetta/ops/MeshOps/init.luau"],
|
||||
[184559, "src/Rosetta/ops/MeshOps/Greedy.luau"],
|
||||
[184726, "src/Rosetta/ops/MusicOps.luau"],
|
||||
[185074, "src/Rosetta/playlist/m3u/init.luau"],
|
||||
[185427, "src/Rosetta/playlist/pls/init.luau"],
|
||||
[185605, "src/Rosetta/playlist/xspf/init.luau"],
|
||||
[185808, "src/Rosetta/subtitle/ass/init.luau"],
|
||||
[186252, "src/Rosetta/subtitle/srt/init.luau"],
|
||||
[186425, "src/Rosetta/subtitle/vtt/init.luau"],
|
||||
[186628, "src/Rosetta/vector/svg/init.luau"],
|
||||
[186714, "src/Rosetta/vector/svg/Parse.luau"],
|
||||
[187460, "src/Rosetta/vector/svg/PathData.luau"],
|
||||
[187828, "src/Rosetta/vector/svg/Render.luau"],
|
||||
[188245, "src/Rosetta/vector/svg/Style.luau"],
|
||||
[188617, "src/Rosetta/vector/svg/Write.luau"],
|
||||
[188817, "src/Rosetta/vector/tiled/init.luau"],
|
||||
[188997, "src/Rosetta/vector/tiled/Data.luau"],
|
||||
[189148, "src/Rosetta/vector/tiled/Tmj.luau"],
|
||||
[189779, "src/Rosetta/vector/tiled/Tmx.luau"],
|
||||
[190391, "src/Rosetta/video/avi/init.luau"],
|
||||
[190685, "src/Rosetta/video/fli/init.luau"],
|
||||
[190836, "src/Rosetta/video/flv/init.luau"],
|
||||
[191220, "src/Rosetta/video/flv/Amf.luau"],
|
||||
[191378, "src/Rosetta/video/flv/Mux.luau"],
|
||||
[191659, "src/Rosetta/video/h264/init.luau"],
|
||||
[191798, "src/Rosetta/video/mkv/init.luau"],
|
||||
[191954, "src/Rosetta/video/mkv/Mux.luau"],
|
||||
[192233, "src/Rosetta/video/mkv/Parse.luau"],
|
||||
[192954, "src/Rosetta/video/mp4/init.luau"],
|
||||
[193282, "src/Rosetta/video/mpegps/init.luau"],
|
||||
[193469, "src/Rosetta/video/mpegts/init.luau"],
|
||||
[193707, "src/Rosetta/video/ogv/init.luau"],
|
||||
[194102, "src/Rosetta/video/smk/init.luau"],
|
||||
[194411, "src/Rosetta/video/smk/Audio.luau"],
|
||||
[194531, "src/Rosetta/video/y4m/init.luau"]
|
||||
]
|
||||
+1
-1
@@ -65,7 +65,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| model | 3ds | .3ds | yes | yes | materials: true, textures: true, animation: true, cameras: true, lights: true, instancing: true | `tests/model/3ds.luau` |
|
||||
| model | 3mf | .3mf | yes | yes | materials: true, vertexColors: true, textures: true, units: true | `tests/model/3mf.luau` |
|
||||
| model | b3d | .b3d | yes | - | materials: true, textures: true, skins: true, animation: true, colors: true | `tests/model/b3d.luau` |
|
||||
| model | bsp | .bsp | yes | - | variants: "quake", "halflife", "quake2", "quake3", lightmaps: "quake3" | `tests/model/bsp.luau` |
|
||||
| model | bsp | .bsp | yes | - | variants: "quake", "bsp2", "2psb", "halflife", "quake2", "quake3", lightmaps: true, collision: true, visibility: true | `tests/model/bsp.luau` |
|
||||
| model | dae | .dae .zae | yes | yes | materials: true, polygons: true, skins: true, morphTargets: true, animation: true, cameras: true, lights: true, zae: true | `tests/model/dae.luau` |
|
||||
| model | fbx | .fbx | yes | yes | binary: true, ascii: true, materials: true, animation: true, skins: true, morphTargets: true, cameras: true, lights: true, polygons: true | `tests/model/fbx.luau` |
|
||||
| model | gltf | .gltf .glb | yes | yes | binary: true, materials: true, animation: true, skins: true, morphTargets: true, cameras: true, lights: true, draco: true | `tests/model/gltf.luau` |
|
||||
|
||||
@@ -741,7 +741,7 @@ local list: { Entry } = {
|
||||
extensions = { "bsp" },
|
||||
mime = { "model/x-bsp" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/image/Miptex", "codecs/model/Patch" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "halflife", "quake2", "quake3" }, lightmaps = "quake3" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "bsp2", "2psb", "halflife", "quake2", "quake3" }, lightmaps = true, collision = true, visibility = true },
|
||||
load = function()
|
||||
return require("@rosetta/model/bsp")
|
||||
end,
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
--!strict
|
||||
-- model/bsp/Lightmap: packs face lightmaps (w x h luxels each) into one atlas, shelf by shelf, tallest first, each
|
||||
-- with a one-luxel border that repeats its edge (bilinear filtering stays inside the face).
|
||||
local Lightmap = {}
|
||||
|
||||
export type Rect = { x: number, y: number, w: number, h: number } -- x, y: the first luxel inside the border
|
||||
|
||||
-- nil, message past maxPixels
|
||||
function Lightmap.pack(sizes: { { number } }, maxPixels: number): (number, number, { Rect }?, string?)
|
||||
local area, widest = 0, 1
|
||||
for _, s in sizes do
|
||||
area += (s[1] + 2) * (s[2] + 2)
|
||||
widest = math.max(widest, s[1] + 2)
|
||||
end
|
||||
local W = 64
|
||||
while W * W < area * 1.2 and W < 4096 do
|
||||
W *= 2
|
||||
end
|
||||
W = math.max(W, widest)
|
||||
local order = table.create(#sizes) :: { number }
|
||||
for i = 1, #sizes do
|
||||
order[i] = i
|
||||
end
|
||||
table.sort(order, function(a: number, b: number): boolean
|
||||
local ha, hb = sizes[a][2], sizes[b][2]
|
||||
if ha ~= hb then
|
||||
return ha > hb
|
||||
end
|
||||
return a < b
|
||||
end)
|
||||
local out = table.create(#sizes) :: { Rect }
|
||||
local x, y, rowH = 0, 0, 0
|
||||
for _, i in order do
|
||||
local w, h = sizes[i][1] + 2, sizes[i][2] + 2
|
||||
if x + w > W then
|
||||
x, y, rowH = 0, y + rowH, 0
|
||||
end
|
||||
out[i] = { x = x + 1, y = y + 1, w = sizes[i][1], h = sizes[i][2] }
|
||||
x += w
|
||||
rowH = math.max(rowH, h)
|
||||
end
|
||||
local H = math.max(1, y + rowH)
|
||||
if W * H > maxPixels then
|
||||
return 0, 0, nil, "limit: lightmap atlas " .. W .. " x " .. H
|
||||
end
|
||||
return W, H, out, nil
|
||||
end
|
||||
|
||||
-- copies the outermost luxels of a rect into its border (rgba8 pixels, atlas width W)
|
||||
function Lightmap.border(px: buffer, W: number, r: Rect)
|
||||
local function copy(dx: number, dy: number, sx: number, sy: number)
|
||||
buffer.writeu32(px, (dy * W + dx) * 4, buffer.readu32(px, (sy * W + sx) * 4))
|
||||
end
|
||||
for yy = r.y, r.y + r.h - 1 do
|
||||
copy(r.x - 1, yy, r.x, yy)
|
||||
copy(r.x + r.w, yy, r.x + r.w - 1, yy)
|
||||
end
|
||||
for xx = r.x - 1, r.x + r.w do
|
||||
copy(xx, r.y - 1, xx, r.y)
|
||||
copy(xx, r.y + r.h, xx, r.y + r.h - 1)
|
||||
end
|
||||
end
|
||||
|
||||
return Lightmap
|
||||
@@ -1,15 +1,17 @@
|
||||
--!strict
|
||||
-- model/bsp/Brush: Quake 1 (BSP 29), Half-Life (BSP 30) and Quake II (IBSP 38) maps -> Scene, from bspfile.h of the
|
||||
-- Quake / Quake II source releases (no code taken). Texture coordinates: s = v . s.xyz + s.w, divided by the texture
|
||||
-- size.
|
||||
-- model/bsp/Quake: Quake 1 (BSP 29, BSP2, 2PSB), Half-Life (BSP 30) and Quake II (IBSP 38) maps -> Scene, from
|
||||
-- bspfile.h of the Quake / Quake II source releases and ericw-tools (BSP2 / 2PSB; no code taken). Texture coordinates:
|
||||
-- s = v . s.xyz + s.w over the texture size; lightmaps (style 0) packed into one atlas as TEXCOORD_1, extents as
|
||||
-- CalcSurfaceExtents (16 texels per luxel).
|
||||
local Types = require("@rosetta/Types")
|
||||
local Pixels = require("@rosetta/codecs/image/Pixels")
|
||||
local Lightmap = require("@rosetta/model/bsp/Lightmap")
|
||||
local Miptex = require("@rosetta/codecs/image/Miptex")
|
||||
|
||||
local Brush = {}
|
||||
local Quake = {}
|
||||
|
||||
local Scene = Types.Scene
|
||||
local readi32, readu16, readf32, readu32 = buffer.readi32, buffer.readu16, buffer.readf32, buffer.readu32
|
||||
local readi32, readu16, readf32, readu32, readu8 = buffer.readi32, buffer.readu16, buffer.readf32, buffer.readu32, buffer.readu8
|
||||
local writef32, writeu32 = buffer.writef32, buffer.writeu32
|
||||
|
||||
export type Lump = { at: number, size: number }
|
||||
@@ -18,10 +20,12 @@ export type Options = {
|
||||
wal: ((name: string) -> buffer?)?, -- Quake II: a texture's .wal file (for its size)
|
||||
maxPixels: number?,
|
||||
maxVertices: number?,
|
||||
lit: buffer?, -- Quake 1 coloured light (.lit: "QLIT", version 1, RGB per luxel)
|
||||
lightmaps: boolean?, -- default true
|
||||
}
|
||||
|
||||
type Tex = { name: string, width: number, height: number, image: Types.Image?, texels: boolean }
|
||||
type Batch = { tex: number, pos: { number }, nrm: { number }, uv: { number } }
|
||||
type Batch = { tex: number, pos: { number }, nrm: { number }, uv: { number }, lm: { number } } -- lm: face, s, t per vertex
|
||||
|
||||
local function cstr(b: buffer, at: number, n: number): string
|
||||
return (string.match(buffer.readstring(b, at, n), "^[^%z]*")) :: string
|
||||
@@ -58,11 +62,14 @@ local function miptex(b: buffer, base: number, l: Lump, hl: boolean, pal: buffer
|
||||
return out, nil
|
||||
end
|
||||
|
||||
function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, variant: string, opts: Options?): (Types.Scene?, string?)
|
||||
function Quake.decode(b: buffer, base: number, lumps: { [string]: Lump }, variant: string, opts: Options?): (Types.Scene?, string?)
|
||||
local q2 = variant == "q2"
|
||||
local big = variant == "bsp2" or variant == "2psb"
|
||||
local maxPixels = opts and opts.maxPixels or 2 ^ 24
|
||||
local V, E, SE, Fc, TI, P, M = lumps.vertices, lumps.edges, lumps.surfedges, lumps.faces, lumps.texinfo, lumps.planes, lumps.models
|
||||
local nV, nE, nSE, nF = V.size // 12, E.size // 4, SE.size // 4, Fc.size // 20
|
||||
local fSize: number = if big then 28 else 20
|
||||
local eSize: number = if big then 8 else 4
|
||||
local nV, nE, nSE, nF = V.size // 12, E.size // eSize, SE.size // 4, Fc.size // fSize
|
||||
local tiSize = if q2 then 76 else 40
|
||||
local nTI, nP = TI.size // tiSize, P.size // 20
|
||||
local mSize = if q2 then 48 else 64
|
||||
@@ -125,11 +132,28 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
local batches: { [number]: { [number]: Batch } } = {}
|
||||
local orders: { [number]: { number } } = {}
|
||||
local skipped = 0
|
||||
-- lightmaps: record 1 = a white luxel (sky / liquids), 2 = black (unlit faces), then one per lit face
|
||||
local LT = lumps.lighting
|
||||
local useLight = LT.size > 0 and not (opts and opts.lightmaps == false)
|
||||
local rgb = variant == "hl" or q2
|
||||
local lit = opts and opts.lit
|
||||
if lit and (buffer.len(lit) < 8 or buffer.readstring(lit, 0, 4) ~= "QLIT" or buffer.readi32(lit, 4) ~= 1) then
|
||||
lit = nil
|
||||
end
|
||||
local lmSizes: { { number } } = { { 1, 1 }, { 1, 1 } }
|
||||
local lmFaces: { { face: number, ofs: number, smin: number, tmin: number, styles: number } } = { { face = -1, ofs = -1, smin = 0, tmin = 0, styles = 0 }, { face = -1, ofs = -1, smin = 0, tmin = 0, styles = 0 } }
|
||||
local lumaTotal = 0
|
||||
for f = 0, nF - 1 do
|
||||
local fo = base + Fc.at + f * 20
|
||||
local plane, side, firstEdge, numEdges, texinfo = readu16(b, fo), readu16(b, fo + 2), readi32(b, fo + 4), readu16(b, fo + 8), readu16(b, fo + 10)
|
||||
local fo = base + Fc.at + f * fSize
|
||||
local plane = if big then readi32(b, fo) else readu16(b, fo)
|
||||
local side = if big then readi32(b, fo + 4) else readu16(b, fo + 2)
|
||||
local firstEdge = if big then readi32(b, fo + 8) else readi32(b, fo + 4)
|
||||
local numEdges = if big then readi32(b, fo + 12) else readu16(b, fo + 8)
|
||||
local texinfo = if big then readi32(b, fo + 16) else readu16(b, fo + 10)
|
||||
local styles = if big then fo + 20 else fo + 12
|
||||
local lightofs = if big then readi32(b, fo + 24) else readi32(b, fo + 16)
|
||||
local model = faceModel[f + 1]
|
||||
if not model or numEdges < 3 or firstEdge < 0 or firstEdge + numEdges > nSE or plane >= nP or texinfo >= nTI then
|
||||
if not model or numEdges < 3 or plane < 0 or texinfo < 0 or firstEdge < 0 or firstEdge + numEdges > nSE or plane >= nP or texinfo >= nTI then
|
||||
skipped += 1
|
||||
continue
|
||||
end
|
||||
@@ -142,7 +166,13 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
ok = false
|
||||
break
|
||||
end
|
||||
local v = if se >= 0 then readu16(b, base + E.at + e * 4) else readu16(b, base + E.at + e * 4 + 2)
|
||||
local eo = base + E.at + e * eSize
|
||||
local v
|
||||
if big then
|
||||
v = if se >= 0 then readu32(b, eo) else readu32(b, eo + 4)
|
||||
else
|
||||
v = if se >= 0 then readu16(b, eo) else readu16(b, eo + 2)
|
||||
end
|
||||
if v >= nV then
|
||||
ok = false
|
||||
break
|
||||
@@ -186,6 +216,37 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
local tio = base + TI.at + texinfo * tiSize
|
||||
local sx, sy, sz, so = readf32(b, tio), readf32(b, tio + 4), readf32(b, tio + 8), readf32(b, tio + 12)
|
||||
local tx, ty, tz, to = readf32(b, tio + 16), readf32(b, tio + 20), readf32(b, tio + 24), readf32(b, tio + 28)
|
||||
local rec = 2
|
||||
if useLight then
|
||||
local tflags = readi32(b, tio + (if q2 then 32 else 36))
|
||||
local special = if q2 then bit32.band(tflags, 0x8C) ~= 0 else bit32.band(tflags, 1) ~= 0
|
||||
if special then
|
||||
rec = 1
|
||||
elseif lightofs >= 0 then
|
||||
local smin: number, smax: number, tmin: number, tmax: number = math.huge, -math.huge, math.huge, -math.huge
|
||||
for k = 0, n - 1 do
|
||||
local x, y, z = poly[k * 3 + 1], poly[k * 3 + 2], poly[k * 3 + 3]
|
||||
local sv, tv = x * sx + y * sy + z * sz + so, x * tx + y * ty + z * tz + to
|
||||
smin, smax = math.min(smin, sv), math.max(smax, sv)
|
||||
tmin, tmax = math.min(tmin, tv), math.max(tmax, tv)
|
||||
end
|
||||
local bs, bt0 = math.floor(smin / 16), math.floor(tmin / 16)
|
||||
local lw, lh = math.ceil((smax :: number) / 16) - bs + 1, math.ceil((tmax :: number) / 16) - bt0 + 1
|
||||
local ns = 0
|
||||
for k = 0, 3 do
|
||||
if readu8(b, styles + k) ~= 255 then
|
||||
ns += 1
|
||||
end
|
||||
end
|
||||
local bytes = lw * lh * (if rgb then 3 else 1)
|
||||
lumaTotal += lw * lh
|
||||
if ns > 0 and lw > 0 and lh > 0 and lw <= 256 and lh <= 256 and lightofs + bytes <= LT.size and lumaTotal <= maxPixels then
|
||||
table.insert(lmSizes, { lw, lh })
|
||||
table.insert(lmFaces, { face = f, ofs = lightofs, smin = bs * 16, tmin = bt0 * 16, styles = ns })
|
||||
rec = #lmFaces
|
||||
end
|
||||
end
|
||||
end
|
||||
local ti = texOf[texinfo + 1] or 0
|
||||
local tex = texs[ti]
|
||||
local w, h = if tex then tex.width else 1, if tex then tex.height else 1
|
||||
@@ -193,7 +254,7 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
batches[model] = bm
|
||||
local bt = bm[ti]
|
||||
if not bt then
|
||||
bt = { tex = ti, pos = {}, nrm = {}, uv = {} }
|
||||
bt = { tex = ti, pos = {}, nrm = {}, uv = {}, lm = {} }
|
||||
bm[ti] = bt
|
||||
local ol = orders[model] or {}
|
||||
orders[model] = ol
|
||||
@@ -206,21 +267,69 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
local pp, nn, uu = batch.pos, batch.nrm, batch.uv
|
||||
pp[#pp + 1], pp[#pp + 2], pp[#pp + 3] = x, y, z
|
||||
nn[#nn + 1], nn[#nn + 2], nn[#nn + 3] = nx, ny, nz
|
||||
uu[#uu + 1] = (x * sx + y * sy + z * sz + so) / w
|
||||
uu[#uu + 1] = (x * tx + y * ty + z * tz + to) / h
|
||||
local sv, tv = x * sx + y * sy + z * sz + so, x * tx + y * ty + z * tz + to
|
||||
uu[#uu + 1] = sv / w
|
||||
uu[#uu + 1] = tv / h
|
||||
local ll = batch.lm
|
||||
ll[#ll + 1], ll[#ll + 2], ll[#ll + 3] = rec, sv, tv
|
||||
end
|
||||
end
|
||||
end
|
||||
local atlasW, atlasH, rects = 0, 0, nil :: { Lightmap.Rect }?
|
||||
local lmTex: number? = nil
|
||||
local atlas: Types.Image? = nil
|
||||
if useLight and #lmFaces > 2 then
|
||||
local W, H, rs, err = Lightmap.pack(lmSizes, maxPixels)
|
||||
if not rs then
|
||||
return nil, err
|
||||
end
|
||||
atlasW, atlasH, rects = W, H, rs
|
||||
local img = Types.Image.new(W, H)
|
||||
local px = img.pixels
|
||||
local la = base + LT.at
|
||||
buffer.writeu32(px, (rs[1].y * W + rs[1].x) * 4, 0xFFFFFFFF)
|
||||
buffer.writeu32(px, (rs[2].y * W + rs[2].x) * 4, 0xFF000000)
|
||||
local placed = {}
|
||||
for i = 3, #lmFaces do
|
||||
local lf, r = lmFaces[i], rs[i]
|
||||
for yy = 0, r.h - 1 do
|
||||
for xx = 0, r.w - 1 do
|
||||
local k = yy * r.w + xx
|
||||
local cr: number, cg: number, cb: number = 0, 0, 0
|
||||
if rgb then
|
||||
local o = la + lf.ofs + k * 3
|
||||
cr, cg, cb = readu8(b, o), readu8(b, o + 1), readu8(b, o + 2)
|
||||
elseif lit and 8 + (lf.ofs + k) * 3 + 3 <= buffer.len(lit) then
|
||||
local o = 8 + (lf.ofs + k) * 3
|
||||
cr, cg, cb = readu8(lit, o), readu8(lit, o + 1), readu8(lit, o + 2)
|
||||
else
|
||||
local v = readu8(b, la + lf.ofs + k)
|
||||
cr, cg, cb = v, v, v
|
||||
end
|
||||
writeu32(px, ((r.y + yy) * W + r.x + xx) * 4, 0xFF000000 + (cb :: number) * 65536 + (cg :: number) * 256 + (cr :: number))
|
||||
end
|
||||
end
|
||||
table.insert(placed, { face = lf.face, x = r.x, y = r.y, w = r.w, h = r.h, styles = lf.styles })
|
||||
end
|
||||
for i = 1, #lmFaces do
|
||||
Lightmap.border(px, W, rs[i])
|
||||
end
|
||||
img.meta.source = { format = "bsp", lightmap = "atlas", coloured = rgb or lit ~= nil }
|
||||
atlas = img
|
||||
scene.meta.lightmaps = placed
|
||||
lmTex = #texs + 1
|
||||
end
|
||||
for i, t in texs do
|
||||
local tex: Types.Texture = { name = t.name, image = t.image, uri = if t.image then nil else (if q2 then "textures/" .. t.name .. ".wal" else t.name) }
|
||||
table.insert(scene.textures, tex)
|
||||
local mat = Scene.material(t.name)
|
||||
mat.metallic = 0
|
||||
local extras: any = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height } }
|
||||
mat.extras = extras
|
||||
mat.extras = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height } } :: any
|
||||
mat.extras = { bsp = { texels = if t.texels then true else nil, width = t.width, height = t.height, lightmap = lmTex, lightmapTexCoord = if lmTex then 1 else nil } } :: any
|
||||
table.insert(scene.materials, mat)
|
||||
end
|
||||
if atlas then
|
||||
table.insert(scene.textures, { name = "lightmap", image = atlas })
|
||||
end
|
||||
local root: Types.Node = { name = "bsp", children = {} }
|
||||
table.insert(scene.nodes, root)
|
||||
scene.roots[1] = 1
|
||||
@@ -242,8 +351,23 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
for k = 0, nv - 1 do
|
||||
writeu32(ib, k * 4, k)
|
||||
end
|
||||
local attrs: { [string]: Types.Accessor } = { POSITION = Scene.accessor(pb, "f32", 3, nv), NORMAL = Scene.accessor(nb, "f32", 3, nv), TEXCOORD_0 = Scene.accessor(ub, "f32", 2, nv) }
|
||||
if rects then
|
||||
local lb = buffer.create(nv * 8)
|
||||
for k = 0, nv - 1 do
|
||||
local rec, sv, tv = bt.lm[k * 3 + 1], bt.lm[k * 3 + 2], bt.lm[k * 3 + 3]
|
||||
local r, lf = rects[rec], lmFaces[rec]
|
||||
local lu, lv = r.x + 0.5, r.y + 0.5
|
||||
if rec > 2 then
|
||||
lu, lv = r.x + (sv - lf.smin) / 16 + 0.5, r.y + (tv - lf.tmin) / 16 + 0.5
|
||||
end
|
||||
writef32(lb, k * 8, lu / atlasW)
|
||||
writef32(lb, k * 8 + 4, lv / atlasH)
|
||||
end
|
||||
attrs.TEXCOORD_1 = Scene.accessor(lb, "f32", 2, nv)
|
||||
end
|
||||
table.insert(mesh.primitives, {
|
||||
attributes = { POSITION = Scene.accessor(pb, "f32", 3, nv), NORMAL = Scene.accessor(nb, "f32", 3, nv), TEXCOORD_0 = Scene.accessor(ub, "f32", 2, nv) },
|
||||
attributes = attrs,
|
||||
indices = Scene.accessor(ib, "u32", 1, nv),
|
||||
mode = "triangles" :: any,
|
||||
material = if ti > 0 then ti else nil,
|
||||
@@ -258,4 +382,4 @@ function Brush.decode(b: buffer, base: number, lumps: { [string]: Lump }, varian
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Brush
|
||||
return Quake
|
||||
@@ -0,0 +1,386 @@
|
||||
--!strict
|
||||
-- model/bsp/Tree: the BSP tree, collision hulls and visibility of Quake 1 / BSP2 / 2PSB / Half-Life / Quake II / Quake
|
||||
-- III maps as tables (bspfile.h layouts; Unofficial Quake 3 Map Specs), plus point queries: the leaf and contents at a
|
||||
-- point (Quake 1 hulls 0..3 through nodes / clipnodes as SV_HullPointContents; Quake II leaf contents; Quake III the
|
||||
-- contents of the leaf's brushes) and the potentially visible set of a leaf (run-length zero bytes, as Mod_DecompressVis).
|
||||
local Tree = {}
|
||||
|
||||
local readi32, readu16, readi16, readf32, readu8, readu32 = buffer.readi32, buffer.readu16, buffer.readi16, buffer.readf32, buffer.readu8, buffer.readu32
|
||||
|
||||
export type Lump = { at: number, size: number }
|
||||
export type Plane = { normal: { number }, dist: number, type: number? }
|
||||
export type Node = { plane: number, children: { number }, mins: { number }, maxs: { number }, firstFace: number?, numFaces: number? } -- children: >= 0 node, < 0 leaf -(child + 1)
|
||||
export type Leaf = {
|
||||
contents: number?,
|
||||
cluster: number?,
|
||||
area: number?,
|
||||
visofs: number?,
|
||||
mins: { number },
|
||||
maxs: { number },
|
||||
firstFace: number?,
|
||||
numFaces: number?,
|
||||
firstBrush: number?,
|
||||
numBrushes: number?,
|
||||
ambient: { number }?,
|
||||
}
|
||||
export type ClipNode = { plane: number, children: { number } } -- children: >= 0 clipnode, < 0 contents
|
||||
export type Model = { mins: { number }, maxs: { number }, origin: { number }?, headnodes: { number }, visleafs: number?, firstFace: number, numFaces: number }
|
||||
export type Brush = { firstSide: number, numSides: number, contents: number }
|
||||
export type Side = { plane: number, texinfo: number }
|
||||
export type Data = {
|
||||
variant: string,
|
||||
planes: { Plane },
|
||||
nodes: { Node },
|
||||
leaves: { Leaf },
|
||||
clipnodes: { ClipNode }?,
|
||||
models: { Model },
|
||||
brushes: { Brush }?,
|
||||
brushSides: { Side }?,
|
||||
leafBrushes: { number }?,
|
||||
leafFaces: { number }?,
|
||||
visibility: buffer?, -- the raw lump
|
||||
numClusters: number?,
|
||||
clusterBytes: number?, -- Quake III bytes per cluster vector
|
||||
}
|
||||
|
||||
local function v3(b: buffer, o: number, kind: string): { number }
|
||||
if kind == "f" then
|
||||
return { readf32(b, o), readf32(b, o + 4), readf32(b, o + 8) }
|
||||
elseif kind == "i" then
|
||||
return { readi32(b, o), readi32(b, o + 4), readi32(b, o + 8) }
|
||||
end
|
||||
return { readi16(b, o), readi16(b, o + 2), readi16(b, o + 4) }
|
||||
end
|
||||
|
||||
local function slice(b: buffer, base: number, l: Lump): buffer?
|
||||
if l.size <= 0 then
|
||||
return nil
|
||||
end
|
||||
local out = buffer.create(l.size)
|
||||
buffer.copy(out, 0, b, base + l.at, l.size)
|
||||
return out
|
||||
end
|
||||
|
||||
-- nil, message past maxItems
|
||||
function Tree.read(b: buffer, base: number, lumps: { [string]: Lump }, variant: string, maxItems: number?): (Data?, string?)
|
||||
local limit = maxItems or 2 ^ 22
|
||||
local q2, q3 = variant == "q2", variant == "q3"
|
||||
local bsp2, rmq = variant == "bsp2", variant == "2psb"
|
||||
local d: Data = { variant = variant, planes = {}, nodes = {}, leaves = {}, models = {} }
|
||||
local function count(l: Lump?, size: number): number
|
||||
return if l then l.size // size else 0
|
||||
end
|
||||
local P = lumps.planes
|
||||
local pSize = if q3 then 16 else 20
|
||||
local np = count(P, pSize)
|
||||
if np > limit then
|
||||
return nil, "limit: too many planes"
|
||||
end
|
||||
for i = 0, np - 1 do
|
||||
local o = base + P.at + i * pSize
|
||||
d.planes[i + 1] = { normal = v3(b, o, "f"), dist = readf32(b, o + 12), type = if q3 then nil else readi32(b, o + 16) }
|
||||
end
|
||||
local N = lumps.nodes
|
||||
local nSize = if q3 then 36 elseif q2 then 28 elseif bsp2 then 44 elseif rmq then 32 else 24
|
||||
local nn = count(N, nSize)
|
||||
if nn > limit then
|
||||
return nil, "limit: too many nodes"
|
||||
end
|
||||
for i = 0, nn - 1 do
|
||||
local o = base + N.at + i * nSize
|
||||
local node: any
|
||||
if q3 or q2 or bsp2 or rmq then
|
||||
local bk = if q3 then "i" elseif bsp2 then "f" else "s"
|
||||
local bs = if bk == "s" then 6 else 12
|
||||
node = { plane = readi32(b, o), children = { readi32(b, o + 4), readi32(b, o + 8) }, mins = v3(b, o + 12, bk), maxs = v3(b, o + 12 + bs, bk) }
|
||||
if not q3 then
|
||||
local fo = o + 12 + bs * 2
|
||||
if q2 then
|
||||
node.firstFace, node.numFaces = readu16(b, fo), readu16(b, fo + 2)
|
||||
else
|
||||
node.firstFace, node.numFaces = readu32(b, fo), readu32(b, fo + 4)
|
||||
end
|
||||
end
|
||||
else
|
||||
local c = {}
|
||||
for k = 0, 1 do
|
||||
local p = readu16(b, o + 4 + k * 2)
|
||||
c[k + 1] = if p < nn then p else -(65535 - p) - 1 -- Quakespasm Mod_LoadNodes_S: leaf 65535 - p
|
||||
end
|
||||
node = { plane = readi32(b, o), children = c, mins = v3(b, o + 8, "s"), maxs = v3(b, o + 14, "s"), firstFace = readu16(b, o + 20), numFaces = readu16(b, o + 22) }
|
||||
end
|
||||
d.nodes[i + 1] = node
|
||||
end
|
||||
local L = lumps.leaves or lumps.leafs
|
||||
local lSize = if q3 then 48 elseif q2 then 28 elseif bsp2 then 44 elseif rmq then 32 else 28
|
||||
local nl = count(L, lSize)
|
||||
if nl > limit then
|
||||
return nil, "limit: too many leaves"
|
||||
end
|
||||
for i = 0, nl - 1 do
|
||||
local o = base + L.at + i * lSize
|
||||
local leaf: any
|
||||
if q3 then
|
||||
leaf = { cluster = readi32(b, o), area = readi32(b, o + 4), mins = v3(b, o + 8, "i"), maxs = v3(b, o + 20, "i"),
|
||||
firstFace = readi32(b, o + 32), numFaces = readi32(b, o + 36), firstBrush = readi32(b, o + 40), numBrushes = readi32(b, o + 44) }
|
||||
elseif q2 then
|
||||
leaf = { contents = readi32(b, o), cluster = readi16(b, o + 4), area = readi16(b, o + 6), mins = v3(b, o + 8, "s"), maxs = v3(b, o + 14, "s"),
|
||||
firstFace = readu16(b, o + 20), numFaces = readu16(b, o + 22), firstBrush = readu16(b, o + 24), numBrushes = readu16(b, o + 26) }
|
||||
else
|
||||
local bk = if bsp2 then "f" else "s"
|
||||
local bs = if bsp2 then 12 else 6
|
||||
local fo = o + 8 + bs * 2
|
||||
local big = bsp2 or rmq
|
||||
leaf = { contents = readi32(b, o), visofs = readi32(b, o + 4), mins = v3(b, o + 8, bk), maxs = v3(b, o + 8 + bs, bk),
|
||||
firstFace = if big then readu32(b, fo) else readu16(b, fo), numFaces = if big then readu32(b, fo + 4) else readu16(b, fo + 2) }
|
||||
local ao = fo + (if big then 8 else 4)
|
||||
leaf.ambient = { readu8(b, ao), readu8(b, ao + 1), readu8(b, ao + 2), readu8(b, ao + 3) }
|
||||
end
|
||||
d.leaves[i + 1] = leaf
|
||||
end
|
||||
if not q2 and not q3 then
|
||||
local C = lumps.clipnodes
|
||||
local cSize = if bsp2 or rmq then 12 else 8
|
||||
local nc = count(C, cSize)
|
||||
if nc > limit then
|
||||
return nil, "limit: too many clipnodes"
|
||||
end
|
||||
local cl: { ClipNode } = {}
|
||||
for i = 0, nc - 1 do
|
||||
local o = base + C.at + i * cSize
|
||||
local c
|
||||
if cSize == 12 then
|
||||
c = { readi32(b, o + 4), readi32(b, o + 8) }
|
||||
else
|
||||
c = {}
|
||||
for k = 0, 1 do
|
||||
local p = readu16(b, o + 4 + k * 2)
|
||||
c[k + 1] = if p > nc then p - 65536 else p -- Quakespasm Mod_LoadClipnodes
|
||||
end
|
||||
end
|
||||
cl[i + 1] = { plane = readi32(b, o), children = c }
|
||||
end
|
||||
d.clipnodes = cl
|
||||
end
|
||||
local M = lumps.models
|
||||
local mSize = if q3 then 40 elseif q2 then 48 else 64
|
||||
for i = 0, count(M, mSize) - 1 do
|
||||
local o = base + M.at + i * mSize
|
||||
local m: any
|
||||
if q3 then
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), headnodes = { 0 }, firstFace = readi32(b, o + 24), numFaces = readi32(b, o + 28) }
|
||||
elseif q2 then
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), origin = v3(b, o + 24, "f"), headnodes = { readi32(b, o + 36) }, firstFace = readi32(b, o + 40), numFaces = readi32(b, o + 44) }
|
||||
else
|
||||
m = { mins = v3(b, o, "f"), maxs = v3(b, o + 12, "f"), origin = v3(b, o + 24, "f"),
|
||||
headnodes = { readi32(b, o + 36), readi32(b, o + 40), readi32(b, o + 44), readi32(b, o + 48) },
|
||||
visleafs = readi32(b, o + 52), firstFace = readi32(b, o + 56), numFaces = readi32(b, o + 60) }
|
||||
end
|
||||
d.models[i + 1] = m
|
||||
end
|
||||
if q2 or q3 then
|
||||
local B, S = lumps.brushes, lumps.brushsides
|
||||
local nb, ns = count(B, 12), count(S, if q3 then 8 else 4)
|
||||
if nb > limit or ns > limit then
|
||||
return nil, "limit: too many brushes"
|
||||
end
|
||||
local brushes: { Brush }, sides: { Side } = {}, {}
|
||||
for i = 0, nb - 1 do
|
||||
local o = base + B.at + i * 12
|
||||
brushes[i + 1] = { firstSide = readi32(b, o), numSides = readi32(b, o + 4), contents = readi32(b, o + 8) } -- Quake III: a texture index
|
||||
end
|
||||
for i = 0, ns - 1 do
|
||||
if q3 then
|
||||
local o = base + S.at + i * 8
|
||||
sides[i + 1] = { plane = readi32(b, o), texinfo = readi32(b, o + 4) }
|
||||
else
|
||||
local o = base + S.at + i * 4
|
||||
sides[i + 1] = { plane = readu16(b, o), texinfo = readi16(b, o + 2) }
|
||||
end
|
||||
end
|
||||
d.brushes, d.brushSides = brushes, sides
|
||||
local LB = lumps.leafbrushes
|
||||
local lbs: { number } = {}
|
||||
for i = 0, count(LB, if q3 then 4 else 2) - 1 do
|
||||
lbs[i + 1] = if q3 then readi32(b, base + LB.at + i * 4) else readu16(b, base + LB.at + i * 2)
|
||||
end
|
||||
d.leafBrushes = lbs
|
||||
if q3 then
|
||||
-- brush contents come from the texture's contents flags
|
||||
local T = lumps.textures
|
||||
local nt = count(T, 72)
|
||||
for _, br in brushes do
|
||||
local t = br.contents
|
||||
br.contents = if t >= 0 and t < nt then readi32(b, base + T.at + t * 72 + 68) else 0
|
||||
end
|
||||
end
|
||||
end
|
||||
local VL = lumps.visibility or lumps.visdata
|
||||
d.visibility = if VL then slice(b, base, VL) else nil
|
||||
local vis = d.visibility
|
||||
if vis and q2 and buffer.len(vis) >= 4 then
|
||||
d.numClusters = readi32(vis, 0)
|
||||
elseif vis and q3 and buffer.len(vis) >= 8 then
|
||||
d.numClusters, d.clusterBytes = readi32(vis, 0), readi32(vis, 4)
|
||||
end
|
||||
return d, nil
|
||||
end
|
||||
|
||||
local function side(d: Data, plane: number, x: number, y: number, z: number): number
|
||||
local p = d.planes[plane + 1]
|
||||
if not p then
|
||||
return 0
|
||||
end
|
||||
local n = p.normal
|
||||
return x * n[1] + y * n[2] + z * n[3] - p.dist
|
||||
end
|
||||
|
||||
-- the leaf (0-based) at a point, walking the tree of model (0-based, default 0)
|
||||
function Tree.leafAt(d: Data, x: number, y: number, z: number, model: number?): number?
|
||||
local m = d.models[(model or 0) + 1]
|
||||
local node = if m then m.headnodes[1] else 0
|
||||
for _ = 1, #d.nodes + 1 do
|
||||
if node < 0 then
|
||||
return -node - 1
|
||||
end
|
||||
local nd = d.nodes[node + 1]
|
||||
if not nd then
|
||||
return nil
|
||||
end
|
||||
node = if side(d, nd.plane, x, y, z) >= 0 then nd.children[1] else nd.children[2]
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Quake 1: CONTENTS_* (-1 empty, -2 solid, -3 water, -4 slime, -5 lava, -6 sky) in hull 0..3; Quake II: the leaf's
|
||||
-- CONTENTS_* bits; Quake III: the contents bits of the brushes holding the point
|
||||
function Tree.contents(d: Data, x: number, y: number, z: number, hull: number?, model: number?): number?
|
||||
local h = hull or 0
|
||||
if (d.variant == "q1" or d.variant == "hl" or d.variant == "bsp2" or d.variant == "2psb") and h > 0 then
|
||||
local m = d.models[(model or 0) + 1]
|
||||
local cl = d.clipnodes or {}
|
||||
local node = if m then m.headnodes[h + 1] or -1 else -1
|
||||
for _ = 1, #cl + 1 do
|
||||
if node < 0 then
|
||||
return node
|
||||
end
|
||||
local c = cl[node + 1]
|
||||
if not c then
|
||||
return nil
|
||||
end
|
||||
node = if side(d, c.plane, x, y, z) >= 0 then c.children[1] else c.children[2]
|
||||
end
|
||||
return nil
|
||||
end
|
||||
local leaf = Tree.leafAt(d, x, y, z, model)
|
||||
local lf = leaf and d.leaves[leaf + 1]
|
||||
if not lf then
|
||||
return nil
|
||||
end
|
||||
if d.variant ~= "q3" then
|
||||
return lf.contents
|
||||
end
|
||||
local out = 0
|
||||
local lbs: { number }, brushes: { Brush }, sides: { Side } = d.leafBrushes or {}, d.brushes or {}, d.brushSides or {}
|
||||
local fb: number, nb: number = lf.firstBrush or 0, lf.numBrushes or 0
|
||||
for k = fb, fb + nb - 1 do
|
||||
local br = brushes[(lbs[k + 1] or -1) + 1]
|
||||
if br then
|
||||
local inside = true
|
||||
for s = br.firstSide, br.firstSide + br.numSides - 1 do
|
||||
local sd = sides[s + 1]
|
||||
if not sd or side(d, sd.plane, x, y, z) > 0 then
|
||||
inside = false
|
||||
break
|
||||
end
|
||||
end
|
||||
if inside then
|
||||
out = bit32.bor(out, br.contents)
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function decompress(vis: buffer, at: number, bytes: number, out: { boolean })
|
||||
local n = buffer.len(vis)
|
||||
local i = 0
|
||||
while i < bytes and at < n do
|
||||
local c = readu8(vis, at)
|
||||
if c ~= 0 then
|
||||
for bit = 0, 7 do
|
||||
if bit32.band(c, bit32.lshift(1, bit)) ~= 0 then
|
||||
out[i * 8 + bit + 1] = true
|
||||
end
|
||||
end
|
||||
i += 1
|
||||
at += 1
|
||||
else
|
||||
if at + 1 >= n then
|
||||
break
|
||||
end
|
||||
i += readu8(vis, at + 1)
|
||||
at += 2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the leaves (0-based, sorted) potentially visible from a leaf; nil without visibility data (everything visible)
|
||||
function Tree.pvs(d: Data, leaf: number): { number }?
|
||||
local lf = d.leaves[leaf + 1]
|
||||
local vis = d.visibility
|
||||
if not lf or not vis then
|
||||
return nil
|
||||
end
|
||||
local out: { number } = {}
|
||||
if d.variant == "q2" or d.variant == "q3" then
|
||||
local nc = d.numClusters or 0
|
||||
local c = lf.cluster or -1
|
||||
if c < 0 or c >= nc then
|
||||
return out
|
||||
end
|
||||
local bits: { boolean } = {}
|
||||
if d.variant == "q2" then
|
||||
if 4 + nc * 8 > buffer.len(vis) then
|
||||
return nil
|
||||
end
|
||||
decompress(vis, readi32(vis, 4 + c * 8), (nc + 7) // 8, bits)
|
||||
else
|
||||
local sz = d.clusterBytes or 0
|
||||
local o = 8 + c * sz
|
||||
for k = 0, sz - 1 do
|
||||
if o + k < buffer.len(vis) then
|
||||
local v = readu8(vis, o + k)
|
||||
for bit = 0, 7 do
|
||||
if bit32.band(v, bit32.lshift(1, bit)) ~= 0 then
|
||||
bits[k * 8 + bit + 1] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for i, l in d.leaves do
|
||||
local cl = l.cluster or -1
|
||||
if cl >= 0 and bits[cl + 1] then
|
||||
table.insert(out, i - 1)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
-- Quake 1: bit k = leaf k + 1 (leaf 0 is the shared solid leaf), visleafs bits
|
||||
local visofs = lf.visofs or -1
|
||||
if leaf == 0 or visofs < 0 then
|
||||
return out
|
||||
end
|
||||
local n = d.models[1] and d.models[1].visleafs or (#d.leaves - 1)
|
||||
local bits: { boolean } = {}
|
||||
decompress(vis, visofs, (n + 7) // 8, bits)
|
||||
for k = 0, n - 1 do
|
||||
if bits[k + 1] then
|
||||
table.insert(out, k + 1)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return Tree
|
||||
@@ -1,15 +1,18 @@
|
||||
--!strict
|
||||
-- model/bsp: compiled game maps, read: Quake 1 (BSP 29), Half-Life (BSP 30), Quake II (IBSP 38), Quake III / RTCW
|
||||
-- (IBSP 46 / 47). Quake textures need the game's palette (gfx/palette.lmp, not redistributable). Map units, z up.
|
||||
-- Not supported: Source engine VBSP, Quake 1/2 lightmaps, visibility and collision data, writing.
|
||||
-- model/bsp: compiled game maps, read: Quake 1 (BSP 29, BSP2, 2PSB), Half-Life (BSP 30), Quake II (IBSP 38), Quake III
|
||||
-- / RTCW (IBSP 46 / 47). Lightmaps (Quake 1 / 2 / Half-Life: one atlas, Quake III: its pages) as TEXCOORD_1; the tree,
|
||||
-- collision hulls / brushes and visibility in meta.bsp with point queries (Bsp.leafAt / contents / pvs). Quake textures
|
||||
-- need the game's palette (gfx/palette.lmp, not redistributable). Map units, z up. Not supported: Source engine VBSP,
|
||||
-- light styles other than the first, BSPX lumps, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Input = require("@rosetta/core/Input")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Brush = require("@rosetta/model/bsp/Brush")
|
||||
local Quake = require("@rosetta/model/bsp/Quake")
|
||||
local Quake3 = require("@rosetta/model/bsp/Quake3")
|
||||
local Tree = require("@rosetta/model/bsp/Tree")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Bsp = {
|
||||
@@ -18,7 +21,7 @@ local Bsp = {
|
||||
extensions = { "bsp" },
|
||||
mime = { "model/x-bsp" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Input", "Types", "codecs/image/Pixels", "codecs/image/Miptex", "codecs/model/Patch" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "halflife", "quake2", "quake3" }, lightmaps = "quake3" },
|
||||
caps = { decode = true, encode = false, variants = { "quake", "bsp2", "2psb", "halflife", "quake2", "quake3" }, lightmaps = true, collision = true, visibility = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
@@ -27,6 +30,10 @@ export type DecodeOptions = {
|
||||
resolve: ((uri: string) -> (buffer | string)?)?,
|
||||
files: { [string]: buffer | string }?,
|
||||
patchLevel: number?, -- default 4
|
||||
lit: (buffer | string)?, -- Quake 1: the map's .lit file (coloured light); also found as "<map>.lit" through files / resolve
|
||||
lightmaps: boolean?, -- default true
|
||||
tree: boolean?, -- meta.bsp (tree, hulls, visibility), default true
|
||||
name: string?, -- the file name (finds the .lit)
|
||||
maxPixels: number?, -- default 2^24
|
||||
maxVertices: number?, -- default 2^24
|
||||
}
|
||||
@@ -46,6 +53,9 @@ function Bsp.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
local m = r:peek(4)
|
||||
local v = buffer.readi32(r.buf, r.pos + (if m == "IBSP" then 4 else 0))
|
||||
if m == "BSP2" or m == "2PSB" then
|
||||
v = 29
|
||||
end
|
||||
if m == "IBSP" then
|
||||
return if v == 38 or v == 46 or v == 47 then 1 else 0.3
|
||||
end
|
||||
@@ -88,6 +98,8 @@ function Bsp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
names, dir, variant = Q2_LUMPS, 8, "q2"
|
||||
elseif m == "IBSP" and (version == 46 or version == 47) then
|
||||
names, dir, variant = Q3_LUMPS, 8, "q3"
|
||||
elseif m == "BSP2" or m == "2PSB" then
|
||||
names, dir, variant = Q1_LUMPS, 4, string.lower(m)
|
||||
elseif m ~= "IBSP" and (version == 29 or version == 30) then
|
||||
names, dir, variant = Q1_LUMPS, 4, if version == 30 then "hl" else "q1"
|
||||
elseif m == "VBSP" then
|
||||
@@ -114,13 +126,15 @@ function Bsp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
if pal and buffer.len(pal) < 768 then
|
||||
pal = nil
|
||||
end
|
||||
scene, msg = Brush.decode(b, base, lumps, variant, {
|
||||
scene, msg = Quake.decode(b, base, lumps, variant, {
|
||||
palette = pal,
|
||||
wal = function(name: string): buffer?
|
||||
return Input.sibling(opts, "textures/" .. name .. ".wal")
|
||||
end,
|
||||
maxPixels = opts and opts.maxPixels,
|
||||
maxVertices = opts and opts.maxVertices,
|
||||
lit = if opts and opts.lit then Bytes.toBuffer(opts.lit) else (if opts and opts.name then Input.sibling(opts, (string.gsub(opts.name :: string, "%.[^./\\]*$", "")) .. ".lit") else nil),
|
||||
lightmaps = opts and opts.lightmaps,
|
||||
})
|
||||
end
|
||||
if not scene then
|
||||
@@ -132,12 +146,44 @@ function Bsp.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
scene.meta.entities = entities(text)
|
||||
scene.meta.source = {
|
||||
format = "bsp",
|
||||
variant = ({ q1 = "quake", hl = "halflife", q2 = "quake2", q3 = "quake3" } :: { [string]: string })[variant],
|
||||
variant = ({ q1 = "quake", bsp2 = "quake", ["2psb"] = "quake", hl = "halflife", q2 = "quake2", q3 = "quake3" } :: { [string]: string })[variant],
|
||||
layout = if variant == "bsp2" then "BSP2" elseif variant == "2psb" then "2PSB" else nil,
|
||||
version = version,
|
||||
entities = text,
|
||||
palette = if variant == "q1" then (opts ~= nil and (opts.palette ~= nil or Input.sibling(opts, "gfx/palette.lmp") ~= nil)) else nil,
|
||||
palette = if variant == "q1" or variant == "bsp2" or variant == "2psb" then (opts ~= nil and (opts.palette ~= nil or Input.sibling(opts, "gfx/palette.lmp") ~= nil)) else nil,
|
||||
}
|
||||
if not (opts and opts.tree == false) then
|
||||
local tree, terr = Tree.read(b, base, lumps, variant)
|
||||
if not tree then
|
||||
return nil, Err.from(terr)
|
||||
end
|
||||
scene.meta.bsp = tree
|
||||
end
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
local function tree(scene: any): Tree.Data?
|
||||
return type(scene) == "table" and type(scene.meta) == "table" and scene.meta.bsp or nil
|
||||
end
|
||||
|
||||
-- the leaf (0-based) at a point, in the tree of a model (0 = the world)
|
||||
function Bsp.leafAt(scene: Types.Scene, x: number, y: number, z: number, model: number?): number?
|
||||
local d = tree(scene)
|
||||
return if d then Tree.leafAt(d, x, y, z, model) else nil
|
||||
end
|
||||
|
||||
-- the contents at a point: Quake 1 CONTENTS_* (-1 empty, -2 solid, ...; hull 0 point, 1 player, 2 shambler, 3 crouch
|
||||
-- in Half-Life), Quake II / III contents bits
|
||||
function Bsp.contents(scene: Types.Scene, x: number, y: number, z: number, hull: number?, model: number?): number?
|
||||
local d = tree(scene)
|
||||
return if d then Tree.contents(d, x, y, z, hull, model) else nil
|
||||
end
|
||||
|
||||
-- the leaves (0-based) potentially visible from a leaf; nil when the map has no visibility data
|
||||
function Bsp.pvs(scene: Types.Scene, leaf: number): { number }?
|
||||
local d = tree(scene)
|
||||
return if d then Tree.pvs(d, leaf) else nil
|
||||
end
|
||||
|
||||
return Bsp
|
||||
@@ -90,6 +90,40 @@ do
|
||||
t:eq(grey.meta.source.palette, false, "q1 without a palette: grey")
|
||||
end
|
||||
|
||||
-- Half-Life / Quake II RGB lightmaps
|
||||
for _, name in { "hl", "q2" } do
|
||||
local c = Fx[name]
|
||||
local s = assert(Bsp.decode(Bytes.reader(buffer.fromstring(c.data)))) :: any
|
||||
local data = buffer.fromstring(c.data)
|
||||
local la = buffer.readi32(data, (if name == "q2" then 8 + 7 * 8 else 4 + 8 * 8))
|
||||
local atlas
|
||||
for _, tx in s.textures do
|
||||
if tx.name == "lightmap" then
|
||||
atlas = tx.image
|
||||
end
|
||||
end
|
||||
local placed = {}
|
||||
for _, r in s.meta.lightmaps do
|
||||
placed[r.face] = r
|
||||
end
|
||||
local bad = 0
|
||||
for _, want in c.lightmaps do
|
||||
local r = placed[want.face]
|
||||
if not r or r.w ~= want.w or r.h ~= want.h then
|
||||
bad += 1
|
||||
else
|
||||
for k = 0, want.w * want.h - 1 do
|
||||
local px = buffer.readu32(atlas.pixels, ((r.y + k // want.w) * atlas.width + r.x + k % want.w) * 4)
|
||||
local o = la + want.ofs + k * 3
|
||||
if px ~= 0xFF000000 + buffer.readu8(data, o + 2) * 65536 + buffer.readu8(data, o + 1) * 256 + buffer.readu8(data, o) then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(#c.lightmaps == 12 and bad == 0, name .. ": RGB lightmaps (" .. bad .. " wrong)")
|
||||
end
|
||||
|
||||
do
|
||||
local q2 = brush("q2", Fx.q2, { files = { ["textures/brick.wal"] = Fx.q2.wal } })
|
||||
t:eq(q2.textures[1].uri, "textures/brick.wal", "q2: .wal URI")
|
||||
@@ -214,4 +248,129 @@ do
|
||||
end
|
||||
t:eq(threw, 0, "q3: mutated lumps never throw")
|
||||
end
|
||||
-- ericw-tools maps: BSP 29 / BSP2 / 2PSB, lightmap atlas, .lit, visibility and hulls
|
||||
do
|
||||
local base: any = nil
|
||||
for _, name in { "bsp29", "bsp2", "2psb" } do
|
||||
local c = Fx.ericw[name]
|
||||
local s, err = Bsp.decode(Bytes.reader(buffer.fromstring(c.data)), { lit = c.lit })
|
||||
t:check(s, name .. ": " .. tostring(err))
|
||||
local sc = s :: any
|
||||
t:eq(F.detect(c.data), "bsp", name .. ": F.detect")
|
||||
t:eq(sc.meta.source.variant, "quake", name .. ": variant")
|
||||
t:eq(sc.meta.source.layout, ({ bsp29 = nil, bsp2 = "BSP2", ["2psb"] = "2PSB" } :: any)[name], name .. ": layout")
|
||||
t:eq(sc.meta.skippedFaces, 0, name .. ": every face read")
|
||||
-- the same compile in three layouts: the same triangles
|
||||
local all = {}
|
||||
for _, mesh in sc.meshes do
|
||||
for _, p in mesh.primitives do
|
||||
for _, x in Soup.list(Soup.positions(p)) do
|
||||
table.insert(all, x)
|
||||
end
|
||||
end
|
||||
end
|
||||
if base then
|
||||
t:check(near(all, base, 0), name .. ": geometry equals BSP 29's")
|
||||
else
|
||||
base = all
|
||||
end
|
||||
-- lightmaps: sizes as CalcSurfaceExtents, luxels copied from the lump (coloured from the .lit)
|
||||
local placed = {}
|
||||
for _, r in sc.meta.lightmaps do
|
||||
placed[r.face] = r
|
||||
end
|
||||
local atlas
|
||||
for _, tx in sc.textures do
|
||||
if tx.name == "lightmap" then
|
||||
atlas = tx.image
|
||||
end
|
||||
end
|
||||
t:check(atlas ~= nil, name .. ": lightmap atlas")
|
||||
local data = buffer.fromstring(c.data)
|
||||
local litBuf = buffer.fromstring(c.lit)
|
||||
local sizeBad, pixBad = 0, 0
|
||||
for _, want in c.lightmaps do
|
||||
local r = placed[want.face]
|
||||
if not r or r.w ~= want.w or r.h ~= want.h then
|
||||
sizeBad += 1
|
||||
else
|
||||
for k = 0, want.w * want.h - 1, 7 do
|
||||
local x, y = r.x + k % want.w, r.y + k // want.w
|
||||
local px = buffer.readu32(atlas.pixels, (y * atlas.width + x) * 4)
|
||||
local o = 8 + (want.ofs + k) * 3
|
||||
local exp = 0xFF000000 + buffer.readu8(litBuf, o + 2) * 65536 + buffer.readu8(litBuf, o + 1) * 256 + buffer.readu8(litBuf, o)
|
||||
if px ~= exp then
|
||||
pixBad += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(sizeBad, 0, name .. ": lightmap sizes (" .. #c.lightmaps .. " faces)")
|
||||
t:eq(pixBad, 0, name .. ": lightmap luxels from the .lit")
|
||||
local _ = data
|
||||
local prim = sc.meshes[1].primitives[1]
|
||||
t:check(prim.attributes.TEXCOORD_1 ~= nil, name .. ": TEXCOORD_1")
|
||||
local mat = sc.materials[prim.material]
|
||||
t:eq(sc.textures[mat.extras.bsp.lightmap].name, "lightmap", name .. ": material names the atlas")
|
||||
local lu = Types.Scene.reader(prim.attributes.TEXCOORD_1) :: any
|
||||
local inRange = true
|
||||
for k = 0, prim.attributes.TEXCOORD_1.count - 1 do
|
||||
if lu(k, 0) < 0 or lu(k, 0) > 1 or lu(k, 1) < 0 or lu(k, 1) > 1 then
|
||||
inRange = false
|
||||
end
|
||||
end
|
||||
t:check(inRange, name .. ": lightmap coordinates inside the atlas")
|
||||
local grey = assert(Bsp.decode(Bytes.reader(buffer.fromstring(c.data)))) :: any
|
||||
local gr
|
||||
for _, r in grey.meta.lightmaps do
|
||||
if r.face == c.lightmaps[1].face then
|
||||
gr = r
|
||||
end
|
||||
end
|
||||
local gimg
|
||||
for _, tx in grey.textures do
|
||||
if tx.name == "lightmap" then
|
||||
gimg = tx.image
|
||||
end
|
||||
end
|
||||
local v = buffer.readu8(data, 0)
|
||||
local _ = v
|
||||
local px = buffer.readu32(gimg.pixels, (gr.y * gimg.width + gr.x) * 4)
|
||||
t:eq(bit32.band(px, 0xFF), bit32.band(bit32.rshift(px, 8), 0xFF), name .. ": without .lit: grey luxels")
|
||||
-- visibility as vis wrote it
|
||||
t:eq(#sc.meta.bsp.leaves, c.leaves, name .. ": leaves")
|
||||
local pvsBad = 0
|
||||
for leaf, want in c.pvs do
|
||||
local got = Bsp.pvs(sc, leaf - 1) or {}
|
||||
if table.concat(got, ",") ~= table.concat(want, ",") then
|
||||
pvsBad += 1
|
||||
end
|
||||
end
|
||||
t:eq(pvsBad, 0, name .. ": PVS of every leaf")
|
||||
t:check(c.visible > 0, name .. ": vis ran")
|
||||
-- contents in the point hull and the clipping hulls
|
||||
local cBad = 0
|
||||
for _, pr in c.probes do
|
||||
for h = 0, 2 do
|
||||
local got = Bsp.contents(sc, pr.p[1], pr.p[2], pr.p[3], h)
|
||||
if got ~= pr.c[h + 1] then
|
||||
cBad += 1
|
||||
t:note("%s: contents at %s hull %d: %s, want %s", name, table.concat(pr.p, " "), h, tostring(got), tostring(pr.c[h + 1]))
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(cBad, 0, name .. ": contents at the probes")
|
||||
t:eq(Bsp.contents(sc, -128, -128, 100, 0), -1, name .. ": the room is empty")
|
||||
t:eq(Bsp.contents(sc, 0, 0, 100, 0), -2, name .. ": the pillar is solid")
|
||||
t:eq(Bsp.contents(sc, -250, 0, 100, 1), -2, name .. ": hull 1 is solid near a wall")
|
||||
t:eq(Bsp.contents(sc, 0, 0, -8, 0), -2, name .. ": the floor is solid")
|
||||
local leaf = Bsp.leafAt(sc, -128, -128, 24)
|
||||
t:check(leaf and leaf > 0, name .. ": player start leaf")
|
||||
local seen = Bsp.pvs(sc, leaf :: number) or {}
|
||||
t:check(table.find(seen, Bsp.leafAt(sc, 0, 0, 220) :: number) ~= nil, name .. ": the light's leaf is visible from the start")
|
||||
end
|
||||
t:fuzz("bsp2", function(b: buffer)
|
||||
return Bsp.decode(Bytes.reader(b), { lit = Fx.ericw.bsp2.lit })
|
||||
end, { Fx.ericw.bsp2.data, Fx.ericw["2psb"].data, Fx.ericw.bsp29.data }, { mutations = 60 })
|
||||
end
|
||||
t:done()
|
||||
+50
-1
@@ -5,7 +5,7 @@
|
||||
# miptex(name, w, h, rng, palette=None) -> bytes (four mip levels of random indices; WAD3 / BSP 30 add the palette)
|
||||
# compile(ctx, text, files, qbsp_args, light_args=None, vis=False) -> { bsp, obj?, log }
|
||||
# read_bsp(data) -> dict of the parsed lumps
|
||||
import os, shutil, struct, subprocess, tempfile
|
||||
import math, os, shutil, struct, subprocess, tempfile
|
||||
|
||||
Q1_LUMPS = ["entities", "planes", "miptex", "vertices", "visibility", "nodes", "texinfo", "faces", "lighting",
|
||||
"clipnodes", "leaves", "marksurfaces", "edges", "surfedges", "models"]
|
||||
@@ -151,3 +151,52 @@ def face_plane(b, f):
|
||||
if f["side"]:
|
||||
return (-nx, -ny, -nz, -d)
|
||||
return (nx, ny, nz, d)
|
||||
|
||||
|
||||
def write_bsp(b, lumps):
|
||||
# reassembles a Quake 1 family BSP (b from read_bsp: its variant decides the header) from raw lumps
|
||||
head = {"BSP2": b"BSP2", "2PSB": b"2PSB"}.get(b["variant"]) or struct.pack("<i", int(b["variant"][3:]))
|
||||
off = len(head) + 8 * len(Q1_LUMPS)
|
||||
body, dirs = b"", []
|
||||
for n in Q1_LUMPS:
|
||||
d = lumps[n]
|
||||
dirs.append(struct.pack("<ii", off + len(body), len(d)))
|
||||
body += d + b"\0" * ((4 - len(d) % 4) % 4)
|
||||
return head + b"".join(dirs) + body
|
||||
|
||||
|
||||
def extents(b, f):
|
||||
# CalcSurfaceExtents: lightmap size in luxels (16 texels each) and the texture mins
|
||||
ti = b["texinfo"][f["texinfo"]]
|
||||
pts = face_points(b, f)
|
||||
out = []
|
||||
for j in range(2):
|
||||
vals = [p[0] * ti[j * 4] + p[1] * ti[j * 4 + 1] + p[2] * ti[j * 4 + 2] + ti[j * 4 + 3] for p in pts]
|
||||
lo, hi = math.floor(min(vals) / 16), math.ceil(max(vals) / 16)
|
||||
out.append(hi - lo + 1)
|
||||
return out
|
||||
|
||||
|
||||
def light(b, rng, rgb=False):
|
||||
# fills the lighting lump with random luxels at the extents of every face that is not TEX_SPECIAL (the nix light
|
||||
# has no ray tracing backend); returns the new BSP bytes and { face: (w, h, ofs) }
|
||||
big = b["variant"] in ("BSP2", "2PSB")
|
||||
faces = bytearray(b["lumps"]["faces"])
|
||||
size = 28 if big else 20
|
||||
data = b""
|
||||
out = {}
|
||||
for i, f in enumerate(b["faces"]):
|
||||
ti = b["texinfo"][f["texinfo"]]
|
||||
o = i * size + (20 if big else 12)
|
||||
if ti[9] & 1:
|
||||
faces[o:o + 8] = bytes([255, 255, 255, 255]) + struct.pack("<i", -1)
|
||||
continue
|
||||
w, h = extents(b, f)
|
||||
ofs = len(data)
|
||||
data += bytes(rng.randrange(256) for _ in range(w * h * (3 if rgb else 1)))
|
||||
faces[o:o + 8] = bytes([0, 255, 255, 255]) + struct.pack("<i", ofs)
|
||||
out[i] = (w, h, ofs)
|
||||
lumps = dict(b["lumps"])
|
||||
lumps["faces"] = bytes(faces)
|
||||
lumps["lighting"] = data
|
||||
return write_bsp(b, lumps), out
|
||||
+105
-4
@@ -6,11 +6,14 @@
|
||||
# q3: polygon, mesh and patch faces, two lightmaps, vertex colours; plus a .pk3 (zip) of it for assimp.
|
||||
# References: the expected triangles per model and texture (positions, normals, texture coordinates, computed here
|
||||
# with the documented rules), texture CRCs, entities; assimp's soup of the Quake III map.
|
||||
# ericw: the room of map.py compiled by ericw-tools (qbsp with hulls + vis) as BSP 29, BSP2 and 2PSB; the lighting lump
|
||||
# filled here at the extents of CalcSurfaceExtents (the nix light has no ray tracer), plus a .lit; expected: lightmap
|
||||
# sizes per face, the PVS of every leaf (Mod_DecompressVis), contents at probe points in hulls 0..2 (SV_HullPointContents).
|
||||
# tests/gen/bsp.luau { q1 = { data, palette, models = { [model] = { [texture] = { pos, uv, nrm } } }, textures,
|
||||
# entities }, hl = ..., q2 = { ..., wal }, q3 = { ..., lightmaps, patchArea, assimp } }
|
||||
import importlib.util, io, math, os, struct, zipfile, zlib
|
||||
|
||||
INPUTS = ["tools/fixtures/_quake_ref.py"]
|
||||
INPUTS = ["tools/fixtures/_quake_ref.py", "tools/fixtures/_qbsp.py", "tools/fixtures/map.py"]
|
||||
|
||||
|
||||
def ref(ctx):
|
||||
@@ -110,7 +113,23 @@ def brush_map(rng, variant, pal):
|
||||
L["vertices"] = b"".join(struct.pack("<3f", *v) for v in verts)
|
||||
L["edges"] = b"".join(struct.pack("<2H", *e) for e in edges)
|
||||
L["surfedges"] = b"".join(struct.pack("<i", s) for s in surfedges)
|
||||
L["faces"] = b"".join(struct.pack("<HHiHH4Bi", p, sd, fe, ne, ti, 0, 255, 255, 255, -1) for p, sd, fe, ne, ti in flist)
|
||||
# Half-Life / Quake II: RGB lightmaps at the CalcSurfaceExtents sizes (Quake 1 is covered by the ericw maps)
|
||||
light, lms, fb = b"", [], b""
|
||||
for fi, (p, sd, fe, ne, ti) in enumerate(flist):
|
||||
if variant == "q1":
|
||||
fb += struct.pack("<HHiHH4Bi", p, sd, fe, ne, ti, 255, 255, 255, 255, -1)
|
||||
continue
|
||||
s4, t4 = tis[ti]
|
||||
pts = [verts[edges[abs(surfedges[fe + k])][0 if surfedges[fe + k] >= 0 else 1]] for k in range(ne)]
|
||||
size = []
|
||||
for v in (s4, t4):
|
||||
vals = [q[0] * v[0] + q[1] * v[1] + q[2] * v[2] + v[3] for q in pts]
|
||||
size.append(math.ceil(max(vals) / 16) - math.floor(min(vals) / 16) + 1)
|
||||
lms.append({"face": fi, "w": size[0], "h": size[1], "ofs": len(light)})
|
||||
fb += struct.pack("<HHiHH4Bi", p, sd, fe, ne, ti, 0, 255, 255, 255, len(light))
|
||||
light += bytes(rng.randrange(256) for _ in range(size[0] * size[1] * 3))
|
||||
L["faces"] = fb
|
||||
L["lighting"] = light
|
||||
if q2:
|
||||
L["texinfo"] = b"".join(struct.pack("<8fii32si", *s, *t, 0, 0, texnames[k].encode(), -1) for s, t, k in texinfo)
|
||||
L["models"] = b"".join(struct.pack("<9fiii", *([0.0] * 9), 0, first, 6) for first in (0, 6))
|
||||
@@ -148,7 +167,7 @@ def brush_map(rng, variant, pal):
|
||||
dirs.append((off + len(body), len(d)))
|
||||
body += d + b"\0" * ((4 - len(d) % 4) % 4)
|
||||
data = head + b"".join(struct.pack("<ii", *d) for d in dirs) + body
|
||||
out = {"data": data, "models": {str(k): v for k, v in models.items()}, "names": texnames}
|
||||
out = {"data": data, "models": {str(k): v for k, v in models.items()}, "names": texnames, "lightmaps": lms}
|
||||
if not q2:
|
||||
out["textures"] = mips
|
||||
else:
|
||||
@@ -222,6 +241,88 @@ def q3_map(rng):
|
||||
return {"data": data, "models": expect, "lightmaps": lmcrc, "patchArea": 32.0 * 32.0, "shaders": [s.decode() for s in shaders]}
|
||||
|
||||
|
||||
def load(ctx, name):
|
||||
spec = importlib.util.spec_from_file_location(name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
return m
|
||||
|
||||
|
||||
def pvs(b, leaf):
|
||||
lf = b["leaves"][leaf]
|
||||
vis, ofs = b["lumps"]["visibility"], lf[1]
|
||||
n = b["models"][0][13]
|
||||
if leaf == 0 or ofs < 0:
|
||||
return []
|
||||
bits, i = [], 0
|
||||
while len(bits) < n and ofs < len(vis):
|
||||
c = vis[ofs]
|
||||
if c:
|
||||
bits += [(c >> k) & 1 for k in range(8)]
|
||||
ofs += 1
|
||||
else:
|
||||
bits += [0] * (8 * vis[ofs + 1])
|
||||
ofs += 2
|
||||
return [k + 1 for k in range(n) if k < len(bits) and bits[k]]
|
||||
|
||||
|
||||
def contents(b, hull, p):
|
||||
def side(plane):
|
||||
nx, ny, nz, d, _ = b["planes"][plane]
|
||||
return p[0] * nx + p[1] * ny + p[2] * nz - d
|
||||
head = b["models"][0][9 + hull]
|
||||
if hull == 0:
|
||||
n = head
|
||||
nn = len(b["nodes"])
|
||||
while True:
|
||||
nd = b["nodes"][n]
|
||||
c = nd[1] if side(nd[0]) >= 0 else nd[2]
|
||||
if b["variant"] == "bsp29":
|
||||
c &= 0xFFFF
|
||||
if c >= nn:
|
||||
return b["leaves"][65535 - c][0]
|
||||
elif c < 0:
|
||||
return b["leaves"][-c - 1][0]
|
||||
n = c
|
||||
n = head
|
||||
nc = len(b["clipnodes"])
|
||||
while n >= 0:
|
||||
cn = b["clipnodes"][n]
|
||||
c = cn[1] if side(cn[0]) >= 0 else cn[2]
|
||||
if b["variant"] == "bsp29":
|
||||
c &= 0xFFFF
|
||||
if c > nc:
|
||||
c -= 65536
|
||||
n = c
|
||||
return n
|
||||
|
||||
|
||||
def ericw(ctx, rng):
|
||||
H = load(ctx, "_qbsp")
|
||||
M = load(ctx, "map")
|
||||
B = load(ctx, "brush")
|
||||
world, door = M.layout(rng)
|
||||
wad = H.wad([(n, 0x44, H.miptex(n, w, h, rng)) for n, w, h in M.TEX])
|
||||
text = M.write(B, "quake", world, door, rng, "tex.wad")
|
||||
probes = [(0, 0, 220), (-128, -128, 24), (200, 200, 100), (0, 0, 100), (-264, 0, 100), (0, 0, -8), (40, 0, 100),
|
||||
(-240, -240, 30), (100, -150, 64), (300, 300, 300)]
|
||||
out = {}
|
||||
for name, args in (("bsp29", []), ("bsp2", ["-bsp2"]), ("2psb", ["-2psb"])):
|
||||
res = H.compile(ctx, text, {"tex.wad": wad}, ["-nopercent"] + args, vis=True)
|
||||
b = H.read_bsp(res["bsp"])
|
||||
data, lm = H.light(b, rng)
|
||||
b = H.read_bsp(data)
|
||||
lit = b"QLIT" + struct.pack("<i", 1) + bytes(rng.randrange(256) for _ in range(len(b["lumps"]["lighting"]) * 3))
|
||||
out[name] = {
|
||||
"data": data, "lit": lit,
|
||||
"lightmaps": [{"face": f, "w": w, "h": h, "ofs": o} for f, (w, h, o) in sorted(lm.items())],
|
||||
"pvs": [pvs(b, i) for i in range(len(b["leaves"]))],
|
||||
"probes": [{"p": list(p), "c": [contents(b, h, p) for h in range(3)]} for p in probes],
|
||||
"faces": len(b["faces"]), "leaves": len(b["leaves"]), "visible": len(b["lumps"]["visibility"]),
|
||||
}
|
||||
return out
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
rng = ctx.rng(101)
|
||||
R = ref(ctx)
|
||||
@@ -235,4 +336,4 @@ def generate(ctx):
|
||||
with zipfile.ZipFile(zbuf, "w") as z:
|
||||
z.writestr("maps/test.bsp", q3["data"])
|
||||
q3["assimp"] = R.assimp_soup(ctx, zbuf.getvalue(), "pk3")
|
||||
ctx.emit("bsp", {"q1": q1, "hl": hl, "q2": q2, "q3": q3})
|
||||
ctx.emit("bsp", {"q1": q1, "hl": hl, "q2": q2, "q3": q3, "ericw": ericw(ctx, rng)})
|
||||
Reference in new issue
Block a user