Back to Drift City
SOURCE / PINNED RELEASE

Made of little things.

Drift City

Release
098731aa0f0f…
Author-recorded commit
2822ea0a4f5b…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-a4c0039c855

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

src/game/wall-surfaces.ts
import * as THREE from 'three';
import { WALL_MATERIALS, type WallMaterial } from './geo.js';

/**
 * The walls' surfaces on the higher graphics settings: one tileable 256 px texture for
 * each wall material, made here (no files), at its real size on the wall. Each texel
 * holds the surface's slope across and up (red, green: for its relief to catch the light),
 * how much it's a joint (blue: mortar, the seams between stones or panels) and how light or
 * dark it is (alpha: 0.6 to 1.4 times the wall's colour).
 */

const N = 256;

/** How many metres across (and up) each material's texture covers, in WALL_MATERIALS order. */
export const SURFACE_SIZE: Record<WallMaterial, number> = {
  brick: 0.9,
  stone: 1.8,
  plaster: 2.5,
  concrete: 3,
  glass: 1,
  siding: 1.2,
  metal: 1.2,
  wood: 2.4,
};

/** The colour of each material's joints (sRGB): mortar, the stones' and panels' seams. */
export const JOINT_COLOR: Record<WallMaterial, number> = {
  brick: 0xc9c2b4,
  stone: 0x8c877d,
  plaster: 0x9a968e,
  concrete: 0x6e6e6a,
  glass: 0x5c6068,
  siding: 0x6a6660,
  metal: 0x5c6068,
  wood: 0x3e3026,
};

function hash(x: number, y: number, seed: number): number {
  let h = Math.imul(x, 374761393) ^ Math.imul(y, 668265263) ^ Math.imul(seed, 2147483647);
  h = Math.imul(h ^ (h >>> 13), 1274126177);
  h ^= h >>> 16;
  return (h >>> 0) / 4294967296;
}

/** Value noise in 0..1 that wraps every p lattice cells each way (so the texture tiles). */
function tnoise(x: number, y: number, p: number, seed: number): number {
  const ix = Math.floor(x);
  const iy = Math.floor(y);
  const fx = x - ix;
  const fy = y - iy;
  const ux = fx * fx * (3 - 2 * fx);
  const uy = fy * fy * (3 - 2 * fy);
  const w = (v: number) => ((v % p) + p) % p;
  const a = hash(w(ix), w(iy), seed);
  const b = hash(w(ix + 1), w(iy), seed);
  const c = hash(w(ix), w(iy + 1), seed);
  const d = hash(w(ix + 1), w(iy + 1), seed);
  return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
}

/** A few octaves of wrapping noise over the texture (u, v from 0 to 1), starting p cells across. */
function tfbm(u: number, v: number, p: number, octaves: number, seed: number): number {
  let sum = 0;
  let amp = 1;
  let total = 0;
  for (let o = 0; o < octaves; o++) {
    const q = p << o;
    sum += tnoise(u * q, v * q, q, seed + o * 31) * amp;
    total += amp;
    amp *= 0.5;
  }
  return sum / total;
}

const smooth = (e0: number, e1: number, x: number) => {
  const t = Math.max(0, Math.min(1, (x - e0) / (e1 - e0)));
  return t * t * (3 - 2 * t);
};

/** A texel of a surface: its height (0 to 1), joint (0 to 1) and lightness (about 1). */
type Texel = { h: number; joint: number; light: number };

/**
 * Bricks and joints of a running bond: courses rows tall, bricks per course across, each
 * course shifted half a brick; joints `gap` texels wide (soft).
 */
function bond(x: number, y: number, rows: number, per: number, gap: number, seed: number): { joint: number; id: number; ex: number; ey: number } {
  const ch = N / rows;
  const row = Math.floor(y / ch);
  const bw = N / per;
  const shift = row % 2 ? bw / 2 : 0;
  const xs = (((x + shift) % N) + N) % N;
  const col = Math.floor(xs / bw);
  // Distance to the nearest joint, in texels.
  const ex = Math.min(xs - col * bw, (col + 1) * bw - xs);
  const ey = Math.min(y - row * ch, (row + 1) * ch - y);
  const joint = 1 - smooth(gap * 0.5, gap * 0.5 + 1.2, Math.min(ex, ey));
  return { joint, id: hash(col, row, seed), ex, ey };
}

/** Each material's texel at (x, y) texels (y up the wall). */
const PATTERNS: Record<WallMaterial, (x: number, y: number) => Texel> = {
  // 4 bricks across and 12 courses up 0.9 m: 22 cm by 7.5 cm with 1 cm of mortar.
  brick(x, y) {
    const b = bond(x, y, 12, 4, 2.6, 11);
    const grain = tfbm(x / N, y / N, 32, 2, 3);
    return { h: (1 - b.joint) * (0.85 + 0.15 * grain), joint: b.joint, light: 0.82 + 0.3 * b.id + 0.12 * (grain - 0.5) };
  },
  // Ashlar: 6 courses up 1.8 m, blocks of 2 or 3 units, edges chamfered.
  stone(x, y) {
    const rows = 6;
    const ch = N / rows;
    const row = Math.floor(y / ch);
    // Each course its own lengths of block (wrapping round), from its own seed.
    let at = Math.floor(hash(row, 0, 5) * 8) * 8;
    const cuts: number[] = [];
    while (cuts.length < 16) {
      cuts.push(at);
      at += (hash(row, cuts.length, 7) < 0.5 ? 2 : 3) * 21;
      if (at - cuts[0]! >= N - 16) break;
    }
    const xs = (((x - cuts[0]!) % N) + N) % N;
    let k = 0;
    while (k + 1 < cuts.length && cuts[k + 1]! - cuts[0]! <= xs) k++;
    const x0 = cuts[k]! - cuts[0]!;
    const x1 = k + 1 < cuts.length ? cuts[k + 1]! - cuts[0]! : N;
    const ex = Math.min(xs - x0, x1 - xs);
    const ey = Math.min(y - row * ch, (row + 1) * ch - y);
    const e = Math.min(ex, ey);
    const joint = 1 - smooth(1.2, 2.6, e);
    const chamfer = smooth(1.5, 6, e);
    const rough = tfbm(x / N, y / N, 16, 3, 9);
    const id = hash(k, row, 13);
    return { h: chamfer * (0.8 + 0.2 * rough), joint, light: 0.86 + 0.2 * id + 0.14 * (rough - 0.5) };
  },
  // Stucco: a fine rough render, faint patches of weathering.
  plaster(x, y) {
    const fine = tfbm(x / N, y / N, 64, 2, 21);
    const patch = tfbm(x / N, y / N, 4, 3, 23);
    return { h: fine * 0.6, joint: 0, light: 0.93 + 0.14 * (patch - 0.5) + 0.05 * (fine - 0.5) };
  },
  // Precast panels: 1.5 m square, with tie holes, blotchy.
  concrete(x, y) {
    const p = N / 2;
    const ex = Math.min(x % p, p - (x % p));
    const ey = Math.min(y % p, p - (y % p));
    const joint = 1 - smooth(0.8, 2.2, Math.min(ex, ey));
    const hx = (x % (p / 3)) - p / 6;
    const hy = (y % (p / 3)) - p / 6;
    const hole = 1 - smooth(1.2, 2.2, Math.hypot(hx, hy));
    const blot = tfbm(x / N, y / N, 8, 4, 31);
    return { h: (1 - joint) * (1 - hole * 0.7) * (0.9 + 0.1 * blot), joint: Math.max(joint, hole * 0.8), light: 0.9 + 0.22 * (blot - 0.5) };
  },
  // A glass tower's metal: brushed, faint vertical streaks.
  glass(x, y) {
    const streak = tfbm(x / N, y / (N * 4), 64, 2, 41);
    return { h: 0.5, joint: 0, light: 0.96 + 0.08 * (streak - 0.5) };
  },
  // Clapboards: 6 boards up 1.2 m, each thicker at its foot, in the shadow of the one above at its top.
  siding(x, y) {
    const bh = N / 6;
    const t = (y % bh) / bh;
    const grain = tfbm(x / N, y / N, 8, 2, 51);
    const shadow = smooth(0.84, 1, t);
    return { h: 1 - t * 0.8, joint: shadow * 0.7, light: 0.95 + 0.06 * (grain - 0.5) - shadow * 0.15 };
  },
  // Corrugated sheet: 8 ribs across 1.2 m, a little rust low down.
  metal(x, y) {
    const rib = 0.5 + 0.5 * Math.cos((2 * Math.PI * x) / (N / 8));
    const rust = tfbm(x / N, y / N, 8, 3, 61);
    return { h: rib, joint: 0, light: 0.92 + 0.1 * rib + 0.1 * (rust - 0.5) };
  },
  // Planks: 8 up 2.4 m, gaps between, grain running along them.
  wood(x, y) {
    const ph = N / 8;
    const t = (y % ph) / ph;
    const row = Math.floor(y / ph);
    const gap = 1 - smooth(0.03, 0.08, Math.min(t, 1 - t));
    const grain = tfbm((x / N) * 0.25, y / N, 8, 3, 71 + row);
    const round = Math.sqrt(Math.max(0, 1 - (2 * t - 1) * (2 * t - 1)));
    return { h: round * 0.7 + grain * 0.3, joint: gap, light: 0.8 + 0.35 * grain + 0.12 * (hash(row, 1, 73) - 0.5) };
  },
};

/** How steep each material's relief is (its height's slope, to the normal's tilt). */
const RELIEF: Record<WallMaterial, number> = { brick: 2.2, stone: 2.6, plaster: 0.45, concrete: 1.4, glass: 0.2, siding: 3, metal: 2.2, wood: 2.4 };

let made: THREE.DataArrayTexture | null = null;

/** The surfaces' textures, one layer for each wall material (in WALL_MATERIALS order). */
export function wallSurfaces(): THREE.DataArrayTexture {
  if (made) return made;
  const layers = WALL_MATERIALS.length;
  const data = new Uint8Array(N * N * 4 * layers);
  WALL_MATERIALS.forEach((m, l) => {
    const h = new Float32Array(N * N);
    const tex: Texel[] = [];
    for (let y = 0; y < N; y++)
      for (let x = 0; x < N; x++) {
        const t = PATTERNS[m](x + 0.5, y + 0.5);
        h[y * N + x] = t.h;
        tex.push(t);
      }
    const at = (x: number, y: number) => h[((y + N) % N) * N + ((x + N) % N)]!;
    for (let y = 0; y < N; y++)
      for (let x = 0; x < N; x++) {
        const i = (l * N * N + y * N + x) * 4;
        const t = tex[y * N + x]!;
        // The slope across and up (wrapping), tilting the normal away from the higher side.
        const sx = (at(x - 1, y) - at(x + 1, y)) * RELIEF[m];
        const sy = (at(x, y - 1) - at(x, y + 1)) * RELIEF[m];
        data[i] = Math.round(Math.max(0, Math.min(1, 0.5 + sx * 0.5)) * 255);
        data[i + 1] = Math.round(Math.max(0, Math.min(1, 0.5 + sy * 0.5)) * 255);
        data[i + 2] = Math.round(Math.max(0, Math.min(1, t.joint)) * 255);
        data[i + 3] = Math.round(Math.max(0, Math.min(1, (t.light - 0.6) / 0.8)) * 255);
      }
  });
  const t = new THREE.DataArrayTexture(data, N, N, layers);
  t.format = THREE.RGBAFormat;
  t.type = THREE.UnsignedByteType;
  t.wrapS = t.wrapT = THREE.RepeatWrapping;
  t.minFilter = THREE.LinearMipmapLinearFilter;
  t.magFilter = THREE.LinearFilter;
  t.generateMipmaps = true;
  t.anisotropy = 8;
  t.needsUpdate = true;
  made = t;
  return t;
}