Back to Faultline
SOURCE / PINNED RELEASE

Made of little things.

Faultline

Release
da1ffaa5624c…
Author-recorded commit
60f5f03189db…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-6932b12c78c

Archive hash verified: 5a2e0492417ec4e7…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/terrain.ts
// The woodland surface is triangulated once. Movement, visibility and rendering use the same planes.
import type { Vec } from './maps';
export type Terrain = { extent: number; spacing: number; side: number; heights: Float32Array };
export function woodlandTerrain(extent: number): Terrain {
  const spacing = 4,
    side = (2 * extent) / spacing + 1,
    heights = new Float32Array(side * side);
  for (let iz = 0; iz < side; iz++)
    for (let ix = 0; ix < side; ix++) {
      const x = ix * spacing - extent,
        z = iz * spacing - extent;
      const hill = (cx: number, cz: number, rx: number, rz: number, h: number) =>
        h * Math.exp(-(((x - cx) / rx) ** 2 + ((z - cz) / rz) ** 2));
      heights[iz * side + ix] =
        0.3 +
        hill(-22, 9, 15, 23, 7) +
        hill(24, -18, 22, 14, 8) +
        hill(-7, -38, 24, 13, 4) +
        hill(28, 33, 20, 18, 5);
    }
  return { extent, spacing, side, heights };
}
export function groundHeight(t: Terrain | undefined, x: number, z: number) {
  if (!t) return 0;
  const gx = Math.min(t.side - 1, Math.max(0, (x + t.extent) / t.spacing)),
    gz = Math.min(t.side - 1, Math.max(0, (z + t.extent) / t.spacing));
  const ix = Math.min(t.side - 2, Math.floor(gx)),
    iz = Math.min(t.side - 2, Math.floor(gz)),
    i = iz * t.side + ix,
    u = gx - ix,
    v = gz - iz;
  const a = t.heights[i],
    b = t.heights[i + 1],
    c = t.heights[i + t.side],
    d = t.heights[i + t.side + 1];
  return u + v <= 1 ? a + (b - a) * u + (c - a) * v : d + (c - d) * (1 - u) + (b - d) * (1 - v);
}
export function groundCrossing(t: Terrain | undefined, o: Vec, v: Vec, limit: number) {
  if (!t) return Infinity;
  // Triangle boundaries lie on three families of equally spaced parallel lines.
  const axes = [
    [o[0] + t.extent, v[0]],
    [o[2] + t.extent, v[2]],
    [o[0] + o[2] + 2 * t.extent, v[0] + v[2]],
  ];
  const next = axes.map(([s, d]) =>
    Math.abs(d) < 1e-8
      ? Infinity
      : ((d > 0 ? Math.floor(s / t.spacing + 1e-8) + 1 : Math.ceil(s / t.spacing - 1e-8) - 1) *
          t.spacing -
          s) /
        d,
  );
  const stride = axes.map(([, d]) => t.spacing / Math.abs(d));
  let start = 0,
    previous = o[1] - groundHeight(t, o[0], o[2]);
  if (previous <= 0) return 0;
  while (start < limit) {
    const end = Math.min(limit, ...next),
      gap = o[1] + v[1] * end - groundHeight(t, o[0] + v[0] * end, o[2] + v[2] * end);
    if (gap <= 0) return start + ((end - start) * previous) / (previous - gap);
    for (let k = 0; k < 3; k++) if (next[k] <= end + 1e-7) next[k] += stride[k];
    start = end;
    previous = gap;
  }
  return Infinity;
}