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,11 +1,26 @@
import type { Vec2 } from "../math/Vec2"
import type { Vec3 } from "../math/Vec3"
/** Floats per vertex in `Mesh.verts`: position x, y, z then texture u, v. */
export const STRIDE = 5
/** One mesh vertex: a world-space position and its texture coordinate. uv is in
* tile units, not 0..1, so values >1 repeat the texture (see Texture.sample). */
export type Vertex = { pos: Vec3; uv: Vec2 }
/**
* Indexed triangle mesh, stored flat for speed. `verts` is a packed run of
* `STRIDE` floats per vertex (x, y, z, u, v) instead of an array of nested
* `{pos, uv}` objects, so the transform loop reads contiguous numbers with no
* pointer chasing or per-vertex allocation. `indices` holds three vertex indices
* per triangle (an index `i` addresses `verts[i * STRIDE ..]`); sharing vertices
* keeps seams welded and shrinks the data. uv is in tile units, not 0..1, so
* values >1 repeat the texture (see Texture.sample). Build with `Mesh.push`.
*/
export type Mesh = { verts: number[]; indices: number[] }
/** Indexed triangle mesh in world space. `indices` holds three entries per
* triangle, each indexing into `vertices`; sharing vertices between triangles
* keeps seams welded and shrinks the data. */
export type Mesh = { vertices: Vertex[]; indices: number[] }
export namespace Mesh {
export function create(): Mesh {
return { verts: [], indices: [] }
}
/** Append a vertex, returning its index (for wiring up `indices`). */
export function push(mesh: Mesh, x: number, y: number, z: number, u: number, v: number): number {
const index = mesh.verts.length / STRIDE
mesh.verts.push(x, y, z, u, v)
return index
}
}