ws-game: model/md2 (Quake II MD2 -> Scene: frames as morph targets + grouped animations, skins as URIs); codecs/model/Rig.normals

This commit is contained in:
4DBug committed 2026-10-31 14:25:36 -05:00
1 parent ec32f359bc
commit de1d2c2bba
8 files changed
+499

No files matched your search

+33
View File
@@ -101,6 +101,39 @@ function Rig.morphAnimation(scene: Types.Scene, node: number, targets: number, r
end
end
-- normals(pos, count, indices) -> f32 x3 buffer: smooth vertex normals (area-weighted face normals, normalised) of a
-- triangle list (indices: 0-based, three per triangle; out-of-range triangles are skipped). For formats whose stored
-- normals cannot be used (Quake's precomputed normal table is not redistributable here).
function Rig.normals(pos: buffer, count: number, indices: { number }): buffer
local out = buffer.create(count * 12)
local acc = table.create(count * 3, 0)
for k = 1, #indices - 2, 3 do
local a, b, c = indices[k], indices[k + 1], indices[k + 2]
if a >= 0 and b >= 0 and c >= 0 and a < count and b < count and c < count then
local ax, ay, az = readf32(pos, a * 12), readf32(pos, a * 12 + 4), readf32(pos, a * 12 + 8)
local ux, uy, uz = readf32(pos, b * 12) - ax, readf32(pos, b * 12 + 4) - ay, readf32(pos, b * 12 + 8) - az
local vx, vy, vz = readf32(pos, c * 12) - ax, readf32(pos, c * 12 + 4) - ay, readf32(pos, c * 12 + 8) - az
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
for _, v in { a, b, c } do
acc[v * 3 + 1] += nx
acc[v * 3 + 2] += ny
acc[v * 3 + 3] += nz
end
end
end
for v = 0, count - 1 do
local x, y, z = acc[v * 3 + 1], acc[v * 3 + 2], acc[v * 3 + 3]
local l = math.sqrt(x * x + y * y + z * z)
if l > 0 then
x, y, z = x / l, y / l, z / l
end
writef32(out, v * 12, x)
writef32(out, v * 12 + 4, y)
writef32(out, v * 12 + 8, z)
end
return out
end
-- ------------------------------------------------------------------------------------------------ skeletons
local function trs(j: Joint): { number }
return Mat4.compose(j.translation, j.rotation, j.scale)