perf: faster still

This commit is contained in:
Dan Finch 2026-08-04 19:12:09 +02:00
parent ef5029da1d
commit 67cd54fe33
8 changed files with 280 additions and 203 deletions

View file

@ -1,4 +1,4 @@
import type { Mesh } from "./Mesh"
import { STRIDE, type Mesh } from "./Mesh"
/** A procedural heightfield surrounding the room. It is the single source of
* ground height: the outdoor mesh is built from it and the player stands on the
@ -59,15 +59,15 @@ export namespace Terrain {
rows: number,
uvScale: number,
): void {
const base = mesh.vertices.length
const base = mesh.verts.length / STRIDE
const dx = (x1 - x0) / cols
const dz = (z1 - z0) / rows
const stride = cols + 1
const rowLen = cols + 1
for (let i = 0; i <= rows; i++) {
const z = z0 + i * dz
for (let j = 0; j <= cols; j++) {
const x = x0 + j * dx
mesh.vertices.push({ pos: { x, y: height(t, x, z), z }, uv: { x: x * uvScale, y: z * uvScale } })
mesh.verts.push(x, height(t, x, z), z, x * uvScale, z * uvScale)
}
}
for (let i = 0; i < rows; i++) {
@ -77,9 +77,9 @@ export namespace Terrain {
if (Math.max(Math.abs(cx), Math.abs(cz)) < t.inner) {
continue
}
const p = base + i * stride + j
const p = base + i * rowLen + j
// Wound so the surface faces up/out, matching the backface-cull sign.
mesh.indices.push(p, p + stride + 1, p + 1, p, p + stride, p + stride + 1)
mesh.indices.push(p, p + rowLen + 1, p + 1, p, p + rowLen, p + rowLen + 1)
}
}
}