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/race-trail.ts
import * as THREE from 'three';
import { START_S, type RaceRoute, type RouteSample } from './race.js';
import type { World } from './world.js';

/**
 * A trail of see-through blue chevrons on the road, along the race route just ahead of the
 * player, showing the way. They drift forward along it, and grow in near the car and fade
 * away into the distance.
 */

const COUNT = 16;
const SPACING = 7;
/** How far ahead of the car the trail starts (m). */
const NEAR = 6;

/** A chevron lying flat, its point towards +z. */
function chevronGeometry(): THREE.BufferGeometry {
  const s = new THREE.Shape();
  s.moveTo(-1, -0.5);
  s.lineTo(0, 0.5);
  s.lineTo(1, -0.5);
  s.lineTo(1, -1.05);
  s.lineTo(0, -0.05);
  s.lineTo(-1, -1.05);
  s.closePath();
  return new THREE.ShapeGeometry(s).rotateX(Math.PI / 2);
}

const _p: RouteSample = { x: 0, z: 0, tx: 0, tz: 1, hw: 4 };
const _m = new THREE.Matrix4();
const _q = new THREE.Quaternion();
const _pos = new THREE.Vector3();
const _scale = new THREE.Vector3();
const _up = new THREE.Vector3(0, 1, 0);
const _c = new THREE.Color();

export class RaceTrail {
  readonly mesh: THREE.InstancedMesh;
  private readonly geometry = chevronGeometry();
  private readonly material = new THREE.MeshBasicMaterial({
    color: 0xffffff,
    transparent: true,
    opacity: 0.85,
    // Added over the road: a glow of blue that lets the road show through, fading to nothing as its colour goes dark.
    blending: THREE.AdditiveBlending,
    depthWrite: false,
    side: THREE.DoubleSide,
    toneMapped: false,
    polygonOffset: true,
    polygonOffsetFactor: -3,
    polygonOffsetUnits: -3,
  });
  private phase = 0;

  constructor(
    private readonly route: RaceRoute,
    private readonly world: World,
  ) {
    this.mesh = new THREE.InstancedMesh(this.geometry, this.material, COUNT);
    this.mesh.frustumCulled = false;
    this.mesh.renderOrder = 2;
    // Per-chevron colour carries the fade (added over the road, darker is fainter).
    for (let i = 0; i < COUNT; i++) this.mesh.setColorAt(i, _c.set(0x3fa9ff));
  }

  /** Lay the trail ahead of progress (the player's distance from the start line); hidden when off. */
  update(dt: number, progress: number, visible: boolean, end: number | null): void {
    this.mesh.visible = visible;
    if (!visible) return;
    this.phase = (this.phase + dt * 9) % SPACING;
    const base = START_S + Math.max(progress, -20) + NEAR + this.phase;
    const span = COUNT * SPACING;
    let n = 0;
    for (let i = 0; i < COUNT; i++) {
      const d = base + i * SPACING;
      // Not past the finish of a point-to-point route.
      if (end !== null && d > end) break;
      const p = this.route.at(d, _p);
      // In the middle of the right-hand half of the road, where you'd be driving.
      const off = p.hw * 0.45;
      const x = p.x - p.tz * off;
      const z = p.z + p.tx * off;
      // Grows in near the car, fades out into the distance.
      const along = (d - base + this.phase) / span;
      const near = Math.min(1, (d - (base - this.phase)) / (SPACING * 1.2));
      const fade = Math.max(0, Math.min(1, near, (1 - along) * 2.2));
      const size = (p.hw > 6 ? 1.7 : 1.35) * (0.4 + 0.6 * fade);
      _q.setFromAxisAngle(_up, Math.atan2(p.tx, p.tz));
      _m.compose(_pos.set(x, this.world.height(x, z, (p.y ?? 0) + 0.5) + 0.14, z), _q, _scale.set(size, 1, size * 1.1));
      this.mesh.setMatrixAt(n, _m);
      this.mesh.setColorAt(n, _c.setRGB(0.25 * fade, 0.66 * fade, 1 * fade));
      n++;
    }
    this.mesh.count = n;
    this.mesh.instanceMatrix.needsUpdate = true;
    if (this.mesh.instanceColor) this.mesh.instanceColor.needsUpdate = true;
  }

  dispose(): void {
    this.mesh.removeFromParent();
    this.geometry.dispose();
    this.material.dispose();
    this.mesh.dispose();
  }
}