rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,261 @@
|
||||
--!strict
|
||||
-- model/bsp/Quake3: Quake III Arena maps (IBSP version 46; Return to Castle Wolfenstein 47 has the same layout) to a
|
||||
-- Scene: polygon and mesh faces (meshvert triangles, reversed to counter-clockwise), biquadratic patches
|
||||
-- tessellated (opts.patchLevel subdivisions per 3x3 control patch, default 4), one primitive per shader and lightmap
|
||||
-- per model (model 0 = the world, the others "*1", ...), POSITION / NORMAL / TEXCOORD_0 (surface) / TEXCOORD_1
|
||||
-- (lightmap) / COLOR_0 (vertex colours, unorm8); shaders as texture URIs; the 128 x 128 RGB lightmaps as
|
||||
-- texture.image (material.extras.bsp.lightmap = its texture index, texCoord 1). Billboards (flares) are skipped.
|
||||
-- Written from the Unofficial Quake 3 Map Specs (Kekoa Proudfoot); no code taken.
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
local Quake3 = {}
|
||||
|
||||
local Scene = Types.Scene
|
||||
local readi32, readf32, readu8 = buffer.readi32, buffer.readf32, buffer.readu8
|
||||
local writef32, writeu32, writeu8 = buffer.writef32, buffer.writeu32, buffer.writeu8
|
||||
|
||||
export type Lump = { at: number, size: number }
|
||||
export type Options = { patchLevel: number?, maxVertices: number? }
|
||||
|
||||
type Batch = { shader: number, lightmap: number, verts: { number } } -- vertex records (indices into the vertex lump, or -1 - k for patch vertices)
|
||||
|
||||
local function cstr(b: buffer, at: number, n: number): string
|
||||
return (string.match(buffer.readstring(b, at, n), "^[^%z]*")) :: string
|
||||
end
|
||||
|
||||
-- decode(b, base, lumps, opts) -> Scene | nil, message
|
||||
function Quake3.decode(b: buffer, base: number, lumps: { [string]: Lump }, opts: Options?): (Types.Scene?, string?)
|
||||
local T, M, VX, MV, F, LM = lumps.textures, lumps.models, lumps.vertexes, lumps.meshverts, lumps.faces, lumps.lightmaps
|
||||
local nT, nM, nV, nMV, nF, nLM = T.size // 72, M.size // 40, VX.size // 44, MV.size // 4, F.size // 104, LM.size // 49152
|
||||
local level = math.clamp(math.floor(opts and opts.patchLevel or 4), 1, 16)
|
||||
local maxV = opts and opts.maxVertices or 2 ^ 24
|
||||
-- patch vertices: 12 floats + 4 colour bytes each, appended as negative ids
|
||||
local extra: { number } = {}
|
||||
local function vertex(i: number): { number }
|
||||
local o = base + VX.at + i * 44
|
||||
local v = table.create(14, 0)
|
||||
for k = 0, 9 do
|
||||
v[k + 1] = readf32(b, o + k * 4)
|
||||
end
|
||||
for k = 0, 3 do
|
||||
v[11 + k] = readu8(b, o + 40 + k)
|
||||
end
|
||||
return v
|
||||
end
|
||||
local function addExtra(v: { number }): number
|
||||
for k = 1, 14 do
|
||||
table.insert(extra, v[k])
|
||||
end
|
||||
return -(#extra // 14)
|
||||
end
|
||||
local faceModel: { number } = {}
|
||||
for m = 0, nM - 1 do
|
||||
local o = base + M.at + m * 40
|
||||
local first, count = readi32(b, o + 24), readi32(b, o + 28)
|
||||
if first >= 0 and count >= 0 and first + count <= nF then
|
||||
for f = first, first + count - 1 do
|
||||
faceModel[f + 1] = faceModel[f + 1] or (m + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local batches: { [number]: { [string]: Batch } } = {}
|
||||
local orders: { [number]: { string } } = {}
|
||||
local counts = { polygons = 0, meshes = 0, patches = 0, billboards = 0, skipped = 0 }
|
||||
local total = 0
|
||||
for f = 0, nF - 1 do
|
||||
local o = base + F.at + f * 104
|
||||
local shader, kind, first, n, mv, nmv, lm = readi32(b, o), readi32(b, o + 8), readi32(b, o + 12), readi32(b, o + 16), readi32(b, o + 20), readi32(b, o + 24), readi32(b, o + 28)
|
||||
local model = faceModel[f + 1] or 1
|
||||
local tris: { number } = {}
|
||||
if (kind == 1 or kind == 3) and first >= 0 and n >= 0 and first + n <= nV and mv >= 0 and nmv >= 0 and mv + nmv <= nMV then
|
||||
for k = 0, nmv - 3, 3 do
|
||||
local ok = true
|
||||
local tri = {}
|
||||
for c = 0, 2 do
|
||||
local idx = readi32(b, base + MV.at + (mv + k + c) * 4)
|
||||
if idx < 0 or idx >= n then
|
||||
ok = false
|
||||
end
|
||||
tri[c + 1] = first + idx
|
||||
end
|
||||
if ok then
|
||||
table.insert(tris, tri[3])
|
||||
table.insert(tris, tri[2])
|
||||
table.insert(tris, tri[1])
|
||||
end
|
||||
end
|
||||
counts[if kind == 1 then "polygons" else "meshes"] += 1
|
||||
elseif kind == 2 and first >= 0 and n >= 0 and first + n <= nV then
|
||||
local w, h = readi32(b, o + 96), readi32(b, o + 100)
|
||||
if w >= 3 and h >= 3 and w % 2 == 1 and h % 2 == 1 and w * h == n then
|
||||
counts.patches += 1
|
||||
local L = level
|
||||
for py = 0, (h - 1) // 2 - 1 do
|
||||
for px = 0, (w - 1) // 2 - 1 do
|
||||
-- the 3x3 control points of this sub-patch
|
||||
local ctrl: { { number } } = {}
|
||||
for j = 0, 2 do
|
||||
for i = 0, 2 do
|
||||
ctrl[j * 3 + i + 1] = vertex(first + (py * 2 + j) * w + px * 2 + i)
|
||||
end
|
||||
end
|
||||
local grid: { number } = {}
|
||||
for gy = 0, L do
|
||||
local tv = gy / L
|
||||
for gx = 0, L do
|
||||
local tu = gx / L
|
||||
local bu = { (1 - tu) ^ 2, 2 * tu * (1 - tu), tu * tu }
|
||||
local bv = { (1 - tv) ^ 2, 2 * tv * (1 - tv), tv * tv }
|
||||
local v = table.create(14, 0)
|
||||
for j = 0, 2 do
|
||||
for i = 0, 2 do
|
||||
local wgt = bu[i + 1] * bv[j + 1]
|
||||
local cp = ctrl[j * 3 + i + 1]
|
||||
for k = 1, 14 do
|
||||
v[k] += cp[k] * wgt
|
||||
end
|
||||
end
|
||||
end
|
||||
local l = math.sqrt(v[8] ^ 2 + v[9] ^ 2 + v[10] ^ 2)
|
||||
if l > 0 then
|
||||
v[8], v[9], v[10] = v[8] / l, v[9] / l, v[10] / l
|
||||
end
|
||||
grid[gy * (L + 1) + gx + 1] = addExtra(v)
|
||||
end
|
||||
end
|
||||
for gy = 0, L - 1 do
|
||||
for gx = 0, L - 1 do
|
||||
local a, c2 = grid[gy * (L + 1) + gx + 1], grid[gy * (L + 1) + gx + 2]
|
||||
local d, e = grid[(gy + 1) * (L + 1) + gx + 1], grid[(gy + 1) * (L + 1) + gx + 2]
|
||||
-- counter-clockwise for Quake's clockwise-front control grids
|
||||
for _, v in { a, d, c2, c2, d, e } do
|
||||
table.insert(tris, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
counts.skipped += 1
|
||||
end
|
||||
elseif kind == 4 then
|
||||
counts.billboards += 1
|
||||
else
|
||||
counts.skipped += 1
|
||||
end
|
||||
total += #tris
|
||||
if total > maxV then
|
||||
return nil, "limit: too many vertices"
|
||||
end
|
||||
if #tris > 0 then
|
||||
local key = shader .. ":" .. lm
|
||||
local bm = batches[model] or {}
|
||||
batches[model] = bm
|
||||
local bt = bm[key]
|
||||
if not bt then
|
||||
bt = { shader = shader, lightmap = lm, verts = {} }
|
||||
bm[key] = bt
|
||||
local ol = orders[model] or {}
|
||||
orders[model] = ol
|
||||
table.insert(ol, key)
|
||||
end
|
||||
for _, v in tris do
|
||||
table.insert(bt.verts, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
local scene = Scene.new()
|
||||
scene.meta.up = "z"
|
||||
-- shaders, lightmaps
|
||||
for i = 0, nT - 1 do
|
||||
local name = cstr(b, base + T.at + i * 72, 64)
|
||||
table.insert(scene.textures, { name = name, uri = name })
|
||||
end
|
||||
local lmTex: { [number]: number } = {}
|
||||
for i = 0, nLM - 1 do
|
||||
local img = Types.Image.new(128, 128)
|
||||
local o = base + LM.at + i * 49152
|
||||
for k = 0, 128 * 128 - 1 do
|
||||
writeu8(img.pixels, k * 4, readu8(b, o + k * 3))
|
||||
writeu8(img.pixels, k * 4 + 1, readu8(b, o + k * 3 + 1))
|
||||
writeu8(img.pixels, k * 4 + 2, readu8(b, o + k * 3 + 2))
|
||||
writeu8(img.pixels, k * 4 + 3, 255)
|
||||
end
|
||||
img.meta.source = { format = "bsp", lightmap = i }
|
||||
table.insert(scene.textures, { name = "lightmap " .. i, image = img })
|
||||
lmTex[i] = #scene.textures
|
||||
end
|
||||
local matOf: { [string]: number } = {}
|
||||
local root: Types.Node = { name = "bsp", children = {} }
|
||||
table.insert(scene.nodes, root)
|
||||
scene.roots[1] = 1
|
||||
local modelCount = math.max(nM, 1)
|
||||
for m = 1, modelCount do
|
||||
local list = orders[m]
|
||||
if list then
|
||||
local mesh: Types.Mesh = { name = if m == 1 then "world" else "*" .. (m - 1), primitives = {} }
|
||||
for _, key in list do
|
||||
local bt = batches[m][key]
|
||||
local nv = #bt.verts
|
||||
local pb, nb, ub, lb, cb, ib = buffer.create(nv * 12), buffer.create(nv * 12), buffer.create(nv * 8), buffer.create(nv * 8), buffer.create(nv * 4), buffer.create(nv * 4)
|
||||
for k, id in bt.verts do
|
||||
local v: { number }
|
||||
if id >= 0 then
|
||||
v = vertex(id)
|
||||
else
|
||||
local e = (-id - 1) * 14
|
||||
v = table.move(extra, e + 1, e + 14, 1, table.create(14, 0)) :: { number }
|
||||
end
|
||||
local o = (k - 1)
|
||||
writef32(pb, o * 12, v[1])
|
||||
writef32(pb, o * 12 + 4, v[2])
|
||||
writef32(pb, o * 12 + 8, v[3])
|
||||
writef32(ub, o * 8, v[4])
|
||||
writef32(ub, o * 8 + 4, v[5])
|
||||
writef32(lb, o * 8, v[6])
|
||||
writef32(lb, o * 8 + 4, v[7])
|
||||
writef32(nb, o * 12, v[8])
|
||||
writef32(nb, o * 12 + 4, v[9])
|
||||
writef32(nb, o * 12 + 8, v[10])
|
||||
for c = 0, 3 do
|
||||
writeu8(cb, o * 4 + c, math.clamp(math.floor(v[11 + c] + 0.5), 0, 255))
|
||||
end
|
||||
writeu32(ib, o * 4, o)
|
||||
end
|
||||
local mi = matOf[key]
|
||||
if not mi then
|
||||
local name = if bt.shader >= 0 and bt.shader < nT then scene.textures[bt.shader + 1].name else "noshader"
|
||||
local mat = Scene.material(name)
|
||||
mat.metallic = 0
|
||||
if bt.shader >= 0 and bt.shader < nT then
|
||||
mat.baseColorTexture = { texture = bt.shader + 1 }
|
||||
end
|
||||
local lt = lmTex[bt.lightmap]
|
||||
mat.extras = { bsp = { lightmap = lt, lightmapTexCoord = if lt then 1 else nil } }
|
||||
table.insert(scene.materials, mat)
|
||||
mi = #scene.materials
|
||||
matOf[key] = mi
|
||||
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),
|
||||
TEXCOORD_1 = Scene.accessor(lb, "f32", 2, nv),
|
||||
COLOR_0 = Scene.accessor(cb, "unorm8", 4, nv),
|
||||
},
|
||||
indices = Scene.accessor(ib, "u32", 1, nv),
|
||||
mode = "triangles" :: any,
|
||||
material = mi,
|
||||
})
|
||||
end
|
||||
table.insert(scene.meshes, mesh)
|
||||
table.insert(scene.nodes, { name = mesh.name, children = {}, mesh = #scene.meshes, extras = { bsp = { model = m - 1 } } })
|
||||
table.insert(root.children, #scene.nodes)
|
||||
end
|
||||
end
|
||||
scene.meta.faces = counts
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Quake3
|
||||
Reference in new issue
Block a user