26 lines
1.1 KiB
TypeScript
26 lines
1.1 KiB
TypeScript
/** Floats per vertex in `Mesh.verts`: position x, y, z then texture u, v. */
|
|
export const STRIDE = 5
|
|
|
|
/**
|
|
* 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[] }
|
|
|
|
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
|
|
}
|
|
}
|