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/contact.ts
import * as THREE from 'three';

/**
 * Contact shadows: the soft darkening right under a vehicle, where the sky's light can't
 * reach (ambient occlusion). The sun's shadow map gives the cast shadow; this is what
 * sits a car on the road. All of them are one instanced mesh, so they cost one draw.
 */

/** A soft dark oval: darkest in the middle, gone at the edge. Black, with the shade in alpha. */
function blobTexture(): THREE.Texture {
  const S = 64;
  const data = new Uint8Array(S * S * 4);
  for (let y = 0; y < S; y++)
    for (let x = 0; x < S; x++) {
      const d = Math.hypot(x + 0.5 - S / 2, y + 0.5 - S / 2) / (S / 2);
      // A broad dark core falling away smoothly to nothing at the rim.
      const t = Math.min(1, Math.max(0, (d - 0.5) / 0.5));
      data[(y * S + x) * 4 + 3] = Math.round(0.8 * (1 - t) ** 1.6 * 255);
    }
  const t = new THREE.DataTexture(data, S, S, THREE.RGBAFormat);
  t.magFilter = THREE.LinearFilter;
  // No mipmaps: some drivers lose a small alpha-only texture's mips (the shadow vanishes).
  t.minFilter = THREE.LinearFilter;
  t.generateMipmaps = false;
  t.needsUpdate = true;
  return t;
}

const geometry = new THREE.PlaneGeometry(1, 1).rotateX(-Math.PI / 2);
let texture: THREE.Texture | null = null;

/** A contact-shadow material: blended over the ground, never hiding what's behind it. */
export function contactMaterial(): THREE.MeshBasicMaterial {
  texture ??= blobTexture();
  return new THREE.MeshBasicMaterial({ map: texture, transparent: true, depthWrite: false, toneMapped: false });
}

const _m = new THREE.Matrix4();
const _q = new THREE.Quaternion();
const _p = new THREE.Vector3();
const _s = new THREE.Vector3();
const _e = new THREE.Euler();

export class ContactShadows {
  readonly mesh: THREE.InstancedMesh;
  private n = 0;

  constructor(max: number) {
    this.mesh = new THREE.InstancedMesh(geometry, contactMaterial(), max);
    this.mesh.count = 0;
    this.mesh.frustumCulled = false;
    this.mesh.renderOrder = 1;
  }

  /** Start a frame's worth. */
  begin(): void {
    this.n = 0;
  }

  /** One under a footprint len long and wid wide, centred at (x, y, z), turned to yaw. */
  add(x: number, y: number, z: number, yaw: number, len: number, wid: number): void {
    if (this.n >= this.mesh.instanceMatrix.count) return;
    _q.setFromEuler(_e.set(0, yaw, 0));
    _m.compose(_p.set(x, y, z), _q, _s.set(wid * 1.9, 1, len * 1.4));
    this.mesh.setMatrixAt(this.n++, _m);
  }

  end(): void {
    this.mesh.count = this.n;
    this.mesh.instanceMatrix.needsUpdate = true;
  }
}

/** A single contact shadow (the player's car), fading as it leaves the ground. */
export function contactShadow(len: number, wid: number): THREE.Mesh {
  const m = new THREE.Mesh(geometry, contactMaterial());
  m.scale.set(wid * 1.9, 1, len * 1.4);
  m.renderOrder = 1;
  return m;
}