codecs/model/Displacement: Source displacement surfaces (CCoreDispInfo grid, normals, triangles); model/vbsp uses it

STYLE.md: Displacement listed in the layout block and the section 2a table.
This commit is contained in:
4DBug committed 2026-11-01 18:59:58 -06:00
1 parent 9efeea304e
commit c30e8618fc
6 files changed
+175 -94

No files matched your search

+1 -1
View File
@@ -1052,7 +1052,7 @@ local list: { Entry } = {
path = "model/vbsp",
extensions = { "bsp" },
mime = { "model/x-vbsp" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Lzma", "codecs/text/KeyValues", "codecs/model/Mat4", "containers/Zip", "containers/Archive" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Lzma", "codecs/text/KeyValues", "codecs/model/Mat4", "codecs/model/Displacement", "containers/Zip", "containers/Archive" },
caps = { decode = true, encode = false, versions = { 19, 20, 21 }, displacements = true, staticProps = true, pak = true },
load = function()
return require("@rosetta/model/vbsp")
+105
View File
@@ -0,0 +1,105 @@
--!strict
-- codecs/model/Displacement: Source engine displacement surfaces (VBSP ddispinfo_t / Hammer dispinfo), as
-- CCoreDispInfo::GenerateDispSurf of the Source SDK 2013 (no code taken): corner 0 is the base face corner nearest the
-- start position, rows run along corner 0 -> 1, columns along 0 -> 3 (the face's own, clockwise, winding), each vertex
-- moved by its offset; the 2^power quads split along alternating diagonals. Normals come from the grid.
local Displacement = {}
local sqrt = math.sqrt
export type Surface = {
n: number, -- vertices per row: 2^power + 1
flat: { number }, -- x, y, z on the base face (texture coordinates are projected from these)
pos: { number }, -- x, y, z displaced
nrm: { number }, -- unit normals on the face normal's side
idx: { number }, -- 0-based triangles, counter-clockwise seen from the face normal's side
}
local function cross(ax: number, ay: number, az: number, bx: number, by: number, bz: number): (number, number, number)
return ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx
end
-- corners: the 4 face corners (12 numbers) in the face's clockwise winding; offsets: n * n * 3 numbers (row major,
-- row = i along corner 0 -> 1) added to the flat grid; normal: the face's outward normal
function Displacement.surface(corners: { number }, start: { number }, power: number, offsets: { number }, normal: { number }): (Surface?, string?)
if #corners ~= 12 then
return nil, "a displacement needs a 4-sided face"
end
if power < 1 or power > 4 or power % 1 ~= 0 then
return nil, "displacement power " .. tostring(power)
end
local n = 2 ^ power + 1
if #offsets < n * n * 3 then
return nil, "displacement offsets truncated"
end
local best, bestD = 0, math.huge
for k = 0, 3 do
local dx, dy, dz = corners[k * 3 + 1] - start[1], corners[k * 3 + 2] - start[2], corners[k * 3 + 3] - start[3]
local d = dx * dx + dy * dy + dz * dz
if d < bestD then
best, bestD = k, d
end
end
local c: { number } = table.create(12) :: any
for k = 0, 3 do
local s = ((best + k) % 4) * 3
c[k * 3 + 1], c[k * 3 + 2], c[k * 3 + 3] = corners[s + 1], corners[s + 2], corners[s + 3]
end
local flat: { number } = table.create(n * n * 3) :: any
local pos: { number } = table.create(n * n * 3) :: any
for i = 0, n - 1 do
local t = i / (n - 1)
local ax, ay, az = c[1] + (c[4] - c[1]) * t, c[2] + (c[5] - c[2]) * t, c[3] + (c[6] - c[3]) * t
local bx, by, bz = c[10] + (c[7] - c[10]) * t, c[11] + (c[8] - c[11]) * t, c[12] + (c[9] - c[12]) * t
for j = 0, n - 1 do
local s = j / (n - 1)
local k = (i * n + j) * 3
local x, y, z = ax + (bx - ax) * s, ay + (by - ay) * s, az + (bz - az) * s
flat[k + 1], flat[k + 2], flat[k + 3] = x, y, z
pos[k + 1], pos[k + 2], pos[k + 3] = x + offsets[k + 1], y + offsets[k + 2], z + offsets[k + 3]
end
end
local nx, ny, nz = normal[1], normal[2], normal[3]
local ux, uy, uz = cross(c[4] - c[1], c[5] - c[2], c[6] - c[3], c[10] - c[1], c[11] - c[2], c[12] - c[3])
local sign = if ux * nx + uy * ny + uz * nz >= 0 then 1 else -1
local nrm: { number } = table.create(n * n * 3) :: any
for i = 0, n - 1 do
local i0, i1 = math.max(i - 1, 0), math.min(i + 1, n - 1)
for j = 0, n - 1 do
local j0, j1 = math.max(j - 1, 0), math.min(j + 1, n - 1)
local a, b = (i1 * n + j) * 3, (i0 * n + j) * 3
local e, f = (i * n + j1) * 3, (i * n + j0) * 3
local x, y, z = cross(pos[a + 1] - pos[b + 1], pos[a + 2] - pos[b + 2], pos[a + 3] - pos[b + 3], pos[e + 1] - pos[f + 1], pos[e + 2] - pos[f + 2], pos[e + 3] - pos[f + 3])
local l = sqrt(x * x + y * y + z * z)
local k = (i * n + j) * 3
if l < 1e-12 then
nrm[k + 1], nrm[k + 2], nrm[k + 3] = nx, ny, nz
else
nrm[k + 1], nrm[k + 2], nrm[k + 3] = x * sign / l, y * sign / l, z * sign / l
end
end
end
-- triangle (i, j), (i + 1, j), (i, j + 1) turns about rows x columns: flipped when that faces away
local idx: { number } = table.create((n - 1) * (n - 1) * 6) :: any
local flip = sign < 0
for i = 0, n - 2 do
for j = 0, n - 2 do
local a = i * n + j
local b, cc, d = a + n, a + n + 1, a + 1
local t1, t2, t3, t4, t5, t6
if (i + j) % 2 == 0 then
t1, t2, t3, t4, t5, t6 = a, b, d, b, cc, d
else
t1, t2, t3, t4, t5, t6 = a, b, cc, a, cc, d
end
if flip then
t2, t3, t5, t6 = t3, t2, t6, t5
end
local q = #idx
idx[q + 1], idx[q + 2], idx[q + 3], idx[q + 4], idx[q + 5], idx[q + 6] = t1, t2, t3, t4, t5, t6
end
end
return { n = n, flat = flat, pos = pos, nrm = nrm, idx = idx }, nil
end
return Displacement
+25 -90
View File
@@ -1,16 +1,14 @@
--!strict
-- model/vbsp/Faces: VBSP faces -> primitives, one per (model, texdata), from public/bspfile.h of the Source SDK 2013
-- (dface_t, texinfo_t, dtexdata_t, ddispinfo_t, CDispVert). Displacements tessellate as CCoreDispInfo::
-- GenerateDispSurf (rows along corner 0 -> 1, columns 0 -> 3, corner 0 nearest startPosition) with the alternating
-- triangle pattern of the SDK's disp tesselation; their normals come from the grid.
-- (dface_t, texinfo_t, dtexdata_t, ddispinfo_t, CDispVert); displacements through codecs/model/Displacement.
local Types = require("@rosetta/Types")
local Displacement = require("@rosetta/codecs/model/Displacement")
local Faces = {}
local Scene = Types.Scene
local readi32, readu16, readi16, readf32, readu8 = buffer.readi32, buffer.readu16, buffer.readi16, buffer.readf32, buffer.readu8
local writef32, writeu32 = buffer.writef32, buffer.writeu32
local sqrt = math.sqrt
export type Lump = { buf: buffer, at: number, size: number }
export type Input = {
@@ -32,10 +30,6 @@ export type Group = { model: number, texdata: number, pos: { number }, nrm: { nu
-- SURF_SKY2D | SURF_SKY | SURF_NODRAW | SURF_HINT | SURF_SKIP
local TOOL_FLAGS = 0x2 + 0x4 + 0x80 + 0x100 + 0x200
local function cross(ax: number, ay: number, az: number, bx: number, by: number, bz: number): (number, number, number)
return ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx
end
local function emit(g: Group, x: number, y: number, z: number, nx: number, ny: number, nz: number, u: number, v: number): number
local pos, nrm, uv = g.pos, g.nrm, g.uv
local k = #pos
@@ -46,29 +40,6 @@ local function emit(g: Group, x: number, y: number, z: number, nx: number, ny: n
return k // 3
end
-- the 2^power x 2^power quads in the SDK's alternating diagonal pattern
local function dispTriangles(n: number, flip: boolean, base: number, idx: { number })
for i = 0, n - 2 do
for j = 0, n - 2 do
local a = base + i * n + j
local b, c, d = a + n, a + n + 1, a + 1
local tris: { number }
if (i + j) % 2 == 0 then
tris = { a, b, d, b, c, d }
else
tris = { a, b, c, a, c, d }
end
for t = 0, 1 do
local p, q, r = tris[t * 3 + 1], tris[t * 3 + 2], tris[t * 3 + 3]
if flip then
q, r = r, q
end
idx[#idx + 1], idx[#idx + 2], idx[#idx + 3] = p, q, r
end
end
end
end
local function displacement(I: Input, g: Group, d: number, poly: { number }, nx: number, ny: number, nz: number, tex: { number }): string?
local di = I.dispinfo
if (d + 1) * 176 > di.size then
@@ -76,76 +47,45 @@ local function displacement(I: Input, g: Group, d: number, poly: { number }, nx:
end
local o = di.at + d * 176
local b = di.buf
local sx, sy, sz = readf32(b, o), readf32(b, o + 4), readf32(b, o + 8)
local vstart, power = readi32(b, o + 12), readi32(b, o + 20)
if power < 1 or power > 4 then
return "displacement power " .. power
end
if #poly ~= 12 then
return nil -- not a quad: the base face is kept
end
if power < 1 or power > 4 then
return "displacement power " .. power
end
local n = 2 ^ power + 1
local dv = I.dispverts
if vstart < 0 or (vstart + n * n) * 20 > dv.size then
return "displacement vertices out of range"
end
local best, bestD = 0, math.huge
for k = 0, 3 do
local dx, dy, dz = poly[k * 3 + 1] - sx, poly[k * 3 + 2] - sy, poly[k * 3 + 3] - sz
local dd = dx * dx + dy * dy + dz * dz
if dd < bestD then
best, bestD = k, dd
end
-- CDispVert: vector * dist, then alpha (0 .. 255)
local offsets: { number } = table.create(n * n * 3) :: any
for k = 0, n * n - 1 do
local vo = dv.at + (vstart + k) * 20
local dist = readf32(dv.buf, vo + 12)
offsets[k * 3 + 1] = readf32(dv.buf, vo) * dist
offsets[k * 3 + 2] = readf32(dv.buf, vo + 4) * dist
offsets[k * 3 + 3] = readf32(dv.buf, vo + 8) * dist
end
local c: { number } = table.create(12) :: any
for k = 0, 3 do
local s = ((best + k) % 4) * 3
c[k * 3 + 1], c[k * 3 + 2], c[k * 3 + 3] = poly[s + 1], poly[s + 2], poly[s + 3]
local surf, err = Displacement.surface(poly, { readf32(b, o), readf32(b, o + 4), readf32(b, o + 8) }, power, offsets, { nx, ny, nz })
if not surf then
return err
end
local P: { number } = table.create(n * n * 3) :: any
local base = #g.pos // 3
local blend = g.blend or {}
g.blend = blend
for i = 0, n - 1 do
local t = i / (n - 1)
local ax, ay, az = c[1] + (c[4] - c[1]) * t, c[2] + (c[5] - c[2]) * t, c[3] + (c[6] - c[3]) * t
local bx, by, bz = c[10] + (c[7] - c[10]) * t, c[11] + (c[8] - c[11]) * t, c[12] + (c[9] - c[12]) * t
for j = 0, n - 1 do
local s = j / (n - 1)
local x, y, z = ax + (bx - ax) * s, ay + (by - ay) * s, az + (bz - az) * s
local vo = dv.at + (vstart + i * n + j) * 20
local dist = readf32(dv.buf, vo + 12)
local k = (i * n + j) * 3
P[k + 1] = x + readf32(dv.buf, vo) * dist
P[k + 2] = y + readf32(dv.buf, vo + 4) * dist
P[k + 3] = z + readf32(dv.buf, vo + 8) * dist
local u = (x * tex[1] + y * tex[2] + z * tex[3] + tex[4]) / tex[9]
local v = (x * tex[5] + y * tex[6] + z * tex[7] + tex[8]) / tex[10]
emit(g, P[k + 1], P[k + 2], P[k + 3], 0, 0, 0, u, v)
blend[base + i * n + j + 1] = math.clamp(readf32(dv.buf, vo + 16) / 255, 0, 1)
end
local flat, pos, nrm = surf.flat, surf.pos, surf.nrm
for k = 0, n * n - 1 do
local x, y, z = flat[k * 3 + 1], flat[k * 3 + 2], flat[k * 3 + 3]
emit(g, pos[k * 3 + 1], pos[k * 3 + 2], pos[k * 3 + 3], nrm[k * 3 + 1], nrm[k * 3 + 2], nrm[k * 3 + 3],
(x * tex[1] + y * tex[2] + z * tex[3] + tex[4]) / tex[9], (x * tex[5] + y * tex[6] + z * tex[7] + tex[8]) / tex[10])
blend[base + k + 1] = math.clamp(readf32(dv.buf, dv.at + (vstart + k) * 20 + 16) / 255, 0, 1)
end
-- grid normals by central differences, facing the base face's side
local ux, uy, uz = cross(c[4] - c[1], c[5] - c[2], c[6] - c[3], c[10] - c[1], c[11] - c[2], c[12] - c[3])
local sign = if ux * nx + uy * ny + uz * nz >= 0 then 1 else -1
local nrm = g.nrm
for i = 0, n - 1 do
local i0, i1 = math.max(i - 1, 0), math.min(i + 1, n - 1)
for j = 0, n - 1 do
local j0, j1 = math.max(j - 1, 0), math.min(j + 1, n - 1)
local a, b2 = (i1 * n + j) * 3, (i0 * n + j) * 3
local e, f = (i * n + j1) * 3, (i * n + j0) * 3
local x, y, z = cross(P[a + 1] - P[b2 + 1], P[a + 2] - P[b2 + 2], P[a + 3] - P[b2 + 3], P[e + 1] - P[f + 1], P[e + 2] - P[f + 2], P[e + 3] - P[f + 3])
local l = sqrt(x * x + y * y + z * z)
if l < 1e-12 then
x, y, z, l = nx, ny, nz, sign
end
local k = (base + i * n + j) * 3
nrm[k + 1], nrm[k + 2], nrm[k + 3] = x * sign / l, y * sign / l, z * sign / l
end
local idx = g.idx
for _, v in surf.idx do
idx[#idx + 1] = base + v
end
-- triangle (0, n, 1) winds along rows x columns: flip it when that does not face the face normal
dispTriangles(n, sign < 0, base, g.idx)
return nil
end
@@ -266,11 +206,6 @@ function Faces.build(I: Input): ({ Group }?, string?, number?)
end
idx[#idx + 1], idx[#idx + 2], idx[#idx + 3] = first, b, c
end
if grp.blend then
for k = before // 3 + 1, #grp.pos // 3 do
grp.blend[k] = 0
end
end
end
verts += (#grp.pos - before) // 3
if verts > I.maxVertices then
+1 -1
View File
@@ -24,7 +24,7 @@ local Vbsp = {
category = "model",
extensions = { "bsp" },
mime = { "model/x-vbsp" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Lzma", "codecs/text/KeyValues", "codecs/model/Mat4", "containers/Zip", "containers/Archive" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Lzma", "codecs/text/KeyValues", "codecs/model/Mat4", "codecs/model/Displacement", "containers/Zip", "containers/Archive" },
caps = { decode = true, encode = false, versions = { 19, 20, 21 }, displacements = true, staticProps = true, pak = true },
}