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.
import * as THREE from 'three';
/**
* Road signs: speed limits, curve warnings, stop signs, city limits and direction signs,
* each a painted board on a steel post. Faces are drawn on canvases once per design and
* shared. A sign's origin is the foot of its post, and its board faces +z (turn the sign
* to face the traffic it's for). Signs are knocked flat like lamp posts.
*/
export type SignKind = 'limit' | 'curve-left' | 'curve-right' | 'stop' | 'city' | 'arrow-left' | 'arrow-right';
const faces = new Map<string, THREE.Texture>();
const postMat = new THREE.MeshLambertMaterial({ color: 0x8a9096, flatShading: true });
const backMat = new THREE.MeshLambertMaterial({ color: 0x9aa0a6, flatShading: true });
const postGeo = new THREE.CylinderGeometry(0.07, 0.08, 1, 6).translate(0, 0.5, 0);
function canvas(w: number, h: number): [HTMLCanvasElement, CanvasRenderingContext2D] {
const c = document.createElement('canvas');
c.width = w;
c.height = h;
return [c, c.getContext('2d')!];
}
/** The painted face of a sign, drawn once per design. */
function face(kind: SignKind, text: string): THREE.Texture {
const key = `${kind}:${text}`;
let tex = faces.get(key);
if (tex) return tex;
const font = '"Arial Narrow", "Helvetica Neue", Arial, sans-serif';
let c: HTMLCanvasElement;
let g: CanvasRenderingContext2D;
if (kind === 'limit') {
// White plate, black border: SPEED LIMIT over the number.
[c, g] = canvas(128, 160);
g.fillStyle = '#f4f4ef';
g.fillRect(0, 0, 128, 160);
g.strokeStyle = '#111';
g.lineWidth = 7;
g.strokeRect(8, 8, 112, 144);
g.fillStyle = '#111';
g.textAlign = 'center';
g.font = `bold 22px ${font}`;
g.fillText('SPEED', 64, 42);
g.fillText('LIMIT', 64, 66);
g.font = `bold 64px ${font}`;
g.fillText(text, 64, 134);
} else if (kind === 'curve-left' || kind === 'curve-right') {
// Yellow diamond with a bent arrow.
[c, g] = canvas(128, 128);
g.translate(64, 64);
g.rotate(Math.PI / 4);
g.fillStyle = '#f2c53d';
g.fillRect(-44, -44, 88, 88);
g.strokeStyle = '#111';
g.lineWidth = 5;
g.strokeRect(-40, -40, 80, 80);
g.setTransform(1, 0, 0, 1, 0, 0);
const s = kind === 'curve-left' ? -1 : 1;
g.strokeStyle = '#111';
g.lineWidth = 10;
g.lineCap = 'round';
g.beginPath();
g.moveTo(64 - s * 4, 98);
g.lineTo(64 - s * 4, 66);
g.quadraticCurveTo(64 - s * 4, 46, 64 + s * 18, 42);
g.stroke();
g.fillStyle = '#111';
g.beginPath();
g.moveTo(64 + s * 34, 40);
g.lineTo(64 + s * 14, 26);
g.lineTo(64 + s * 16, 56);
g.closePath();
g.fill();
} else if (kind === 'stop') {
// Red octagon.
[c, g] = canvas(128, 128);
const oct = (r: number) => {
g.beginPath();
for (let i = 0; i < 8; i++) {
const a = Math.PI / 8 + (i * Math.PI) / 4;
g.lineTo(64 + Math.cos(a) * r, 64 + Math.sin(a) * r);
}
g.closePath();
};
oct(62);
g.fillStyle = '#f4f4ef';
g.fill();
oct(56);
g.fillStyle = '#c8202a';
g.fill();
g.fillStyle = '#f4f4ef';
g.textAlign = 'center';
g.font = `bold 40px ${font}`;
g.fillText('STOP', 64, 79);
} else {
// Green guide sign: a place name, or a direction with an arrow.
[c, g] = canvas(256, 128);
g.fillStyle = '#1f6b3a';
g.fillRect(0, 0, 256, 128);
g.strokeStyle = '#f4f4ef';
g.lineWidth = 5;
g.strokeRect(7, 7, 242, 114);
g.fillStyle = '#f4f4ef';
g.textAlign = 'center';
if (kind === 'city') {
g.font = `bold 20px ${font}`;
g.fillText('CITY LIMIT', 128, 42);
g.font = `bold 42px ${font}`;
g.fillText(text, 128, 96);
} else {
const s = kind === 'arrow-left' ? -1 : 1;
g.font = `bold 36px ${font}`;
g.fillText(text, 128 - s * 22, 78);
g.beginPath();
const ax = 128 + s * 92;
g.moveTo(ax + s * 18, 66);
g.lineTo(ax - s * 6, 44);
g.lineTo(ax - s * 6, 56);
g.lineTo(ax - s * 22, 56);
g.lineTo(ax - s * 22, 76);
g.lineTo(ax - s * 6, 76);
g.lineTo(ax - s * 6, 88);
g.closePath();
g.fill();
}
}
tex = new THREE.CanvasTexture(c);
tex.colorSpace = THREE.SRGBColorSpace;
tex.anisotropy = 4;
faces.set(key, tex);
return tex;
}
/** Board sizes in metres (width, height), and how high the board's centre stands: larger
* than life, so they read from the chase camera. */
const SIZE: Record<SignKind, [number, number, number]> = {
limit: [1.05, 1.3, 2.6],
'curve-left': [1.35, 1.35, 2.6],
'curve-right': [1.35, 1.35, 2.6],
stop: [1.25, 1.25, 2.5],
city: [3.2, 1.6, 2.9],
'arrow-left': [2.8, 1.4, 2.8],
'arrow-right': [2.8, 1.4, 2.8],
};
/** A sign on its post (two posts for the wide green ones). */
export function makeSign(kind: SignKind, text = ''): THREE.Group {
const g = new THREE.Group();
const [w, h, y] = SIZE[kind];
const wide = w > 1;
for (const x of wide ? [-w * 0.32, w * 0.32] : [0]) {
const post = new THREE.Mesh(postGeo, postMat);
post.scale.y = y + h / 2 - 0.05;
post.position.x = x;
post.castShadow = true;
g.add(post);
}
const mat = new THREE.MeshLambertMaterial({ map: face(kind, text), transparent: kind !== 'limit' && !wide, alphaTest: 0.5 });
// The board leans back a touch, towards the camera above.
const tilt = new THREE.Group();
tilt.position.set(0, y, 0.1);
tilt.rotation.x = -0.25;
g.add(tilt);
const board = new THREE.Mesh(new THREE.PlaneGeometry(w, h), mat);
board.position.z = 0.01;
board.castShadow = true;
tilt.add(board);
// A plain grey back, so it isn't see-through from behind.
const back = new THREE.Mesh(new THREE.PlaneGeometry(w * (kind === 'stop' || kind.startsWith('curve') ? 0.75 : 1), h * (kind === 'stop' || kind.startsWith('curve') ? 0.75 : 1)), backMat);
back.rotation.y = Math.PI;
tilt.add(back);
g.userData.sign = kind;
return g;
}
