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:
1 parent
9efeea304e
commit
c30e8618fc
6 files changed
+175
-94
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user