feat: culling

This commit is contained in:
Dan Finch 2026-08-04 15:05:02 +02:00
parent 680e08aadc
commit ef5029da1d
7 changed files with 309 additions and 103 deletions

View file

@ -42,34 +42,46 @@ export namespace Terrain {
return rise * (hills + peaks)
}
/** Build the outdoor ground as a `divisions`x`divisions` grid over the whole
* world, each vertex lifted onto the heightfield. Cells inside the clearing
* are skipped so the mesh has a hole where the flat room floor goes (no
* z-fighting). `uvScale` sets texture tiles per world unit. */
export function ground(t: Terrain, divisions: number, uvScale: number): Mesh {
const vertices: Mesh["vertices"] = []
const indices: number[] = []
const step = (t.outer * 2) / divisions
const row = divisions + 1
for (let i = 0; i <= divisions; i++) {
const z = -t.outer + i * step
for (let j = 0; j <= divisions; j++) {
const x = -t.outer + j * step
vertices.push({ pos: { x, y: height(t, x, z), z }, uv: { x: x * uvScale, y: z * uvScale } })
/** Append one ground patch: a `cols`x`rows` heightfield grid over the rectangle
* [x0,x1] x [z0,z1], each vertex lifted onto the heightfield. Quads whose
* center is inside the clearing are skipped (the room floor's hole). UVs use
* world position * `uvScale`, so neighboring patches tile seamlessly. Callers
* keep the spacing uniform and cell edges aligned, so shared edges weld with
* no cracks. Used to build the terrain per spatial chunk. */
export function patch(
t: Terrain,
mesh: Mesh,
x0: number,
z0: number,
x1: number,
z1: number,
cols: number,
rows: number,
uvScale: number,
): void {
const base = mesh.vertices.length
const dx = (x1 - x0) / cols
const dz = (z1 - z0) / rows
const stride = 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 } })
}
}
for (let i = 0; i < divisions; i++) {
for (let j = 0; j < divisions; j++) {
const cx = -t.outer + (j + 0.5) * step
const cz = -t.outer + (i + 0.5) * step
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const cx = x0 + (j + 0.5) * dx
const cz = z0 + (i + 0.5) * dz
if (Math.max(Math.abs(cx), Math.abs(cz)) < t.inner) {
continue
}
const p = i * row + j
indices.push(p, p + 1, p + row + 1, p, p + row + 1, p + row)
const p = base + i * stride + 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)
}
}
return { vertices, indices }
}
/** Rolling hills in 0..1, always non-negative so the ground never dips below