perf: MeshOps normals / tangents accumulate without a table per triangle

This commit is contained in:
4DBug committed 2026-11-01 19:29:39 -06:00
1 parent f6c7857525
commit b1d9fad454
1 file changed
+29 -13
+29 -13
View File
@@ -438,11 +438,16 @@ function MeshOps.computeNormals(prim: Primitive, opts: { flat: boolean? }?): (Pr
local e2x, e2y, e2z = readf32(pd, c * 12) - ax, readf32(pd, c * 12 + 4) - ay, readf32(pd, c * 12 + 8) - az
-- the cross product is twice the area times the unit normal: area weighting for free
local nx, ny, nz = e1y * e2z - e1z * e2y, e1z * e2x - e1x * e2z, e1x * e2y - e1y * e2x
for _, v in { a, b, c } do
acc[v * 3 + 1] += nx
acc[v * 3 + 2] += ny
acc[v * 3 + 3] += nz
end
a, b, c = a * 3, b * 3, c * 3
acc[a + 1] += nx
acc[a + 2] += ny
acc[a + 3] += nz
acc[b + 1] += nx
acc[b + 2] += ny
acc[b + 3] += nz
acc[c + 1] += nx
acc[c + 2] += ny
acc[c + 3] += nz
end
local out = buffer.create(nv * 12)
for v = 0, nv - 1 do
@@ -500,14 +505,25 @@ function MeshOps.computeTangents(prim: Primitive, opts: { texCoord: number? }?):
local r = 1 / den
local tx, ty, tz = (t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r
local bx, by, bz = (s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r
for _, v in { a, b, c } do
tan[v * 3 + 1] += tx
tan[v * 3 + 2] += ty
tan[v * 3 + 3] += tz
bit[v * 3 + 1] += bx
bit[v * 3 + 2] += by
bit[v * 3 + 3] += bz
end
local a3, b3, c3 = a * 3, b * 3, c * 3
tan[a3 + 1] += tx
tan[a3 + 2] += ty
tan[a3 + 3] += tz
bit[a3 + 1] += bx
bit[a3 + 2] += by
bit[a3 + 3] += bz
tan[b3 + 1] += tx
tan[b3 + 2] += ty
tan[b3 + 3] += tz
bit[b3 + 1] += bx
bit[b3 + 2] += by
bit[b3 + 3] += bz
tan[c3 + 1] += tx
tan[c3 + 2] += ty
tan[c3 + 3] += tz
bit[c3 + 1] += bx
bit[c3 + 2] += by
bit[c3 + 3] += bz
end
end
local out = buffer.create(nv * 16)