Adding Components
Writing geometry
A geometry builder is a pure TypeScript function that returns a THREE.Group. It receives the component's position data and returns a fully-formed 3D mesh — no React, no hooks, no side effects.
The three-step process
Adding a new renderable component type requires three changes:
- Add the variant to
src/labs/types.ts— extends theComponentInstancediscriminated union. - Create a component folder in
src/components/— write the geometry builder and export it. - Add one registry entry to
LabScene.tsx— maps the type string to the builder function.
The renderer itself never changes. That's the point of the registry.
Step 1 — Add the type variant
Open src/labs/types.ts and add a new branch to ComponentInstance:
// src/labs/types.ts
export type ComponentInstance =
| { id: string; type: 'breadboard' }
// ... existing types ...
| { id: string; type: 'relay'; mountedAt: MountPoint; coilVoltage?: number }
// ↑ newStep 2 — Create the component folder
Create a new folder under src/components/ following the established pattern. Each component folder has an index.ts that exports both the board-mounted and standalone builder functions:
src/components/
relay/
index.ts ← exports buildRelay() and buildRelayStandalone()Here's an example builder:
// src/components/relay/index.ts
import * as THREE from 'three';
import { PITCH, TOP_Y, BOARD_H } from '@/labs/coords';
import { M, solidBox, solidCyl, centreAtOrigin } from '@/components/shared';
// ── RELAY ──────────────────────────────────────────────────────────────────
// board(mountPos) — placed at exact hole position
// standalone() — centred at origin for showcase cards
export function buildRelay(
mountPos: THREE.Vector3,
): THREE.Group {
const root = new THREE.Group();
// Body — dark housing
const body = solidBox(PITCH * 4, PITCH * 3, PITCH * 2.5, M.dark());
body.position.set(mountPos.x + PITCH, TOP_Y + PITCH * 1.5, mountPos.z);
root.add(body);
// Coil indicator
const coil = solidCyl(PITCH * 0.6, PITCH * 2.2, M.hex(0x8b4513));
coil.position.set(mountPos.x + PITCH, TOP_Y + PITCH * 1.5, mountPos.z);
root.add(coil);
// Leads (4 pins)
const leadH = PITCH * 1.8 + BOARD_H * 0.6;
for (let i = 0; i < 4; i++) {
const lx = mountPos.x + (i - 1) * PITCH;
const lead = new THREE.Mesh(
new THREE.CylinderGeometry(PITCH * 0.07, PITCH * 0.07, leadH, 6),
M.gold(),
);
lead.position.set(lx, TOP_Y - BOARD_H * 0.3 + leadH / 2, mountPos.z);
root.add(lead);
}
return root;
}
export function buildRelayStandalone(): THREE.Group {
const g = buildRelay(new THREE.Vector3(0, 0, 0));
centreAtOrigin(g);
return g;
}Geometry style rules
- Use only Three.js primitives:
BoxGeometry,CylinderGeometry,SphereGeometry,TorusGeometry. - No
.glbfiles, no textures, no external assets. - White/cream fill + black wireframe edges — use
M.white(),M.cream(),M.dark(). - Leads always use
M.gold(). - Body height expressed in
PITCHmultiples (PITCH = 0.18units ≈ 2.54mm). - Position relative to
TOP_Y(the breadboard surface). Leads hang below into the board. - Both variants: board-mounted (takes
THREE.Vector3hole positions) andStandalone(centred at origin for display cards).
Available material helpers
M.white() // #ffffff fill M.gray() // #c8c8c8 fill M.dark() // #141414 fill M.cream() // #f5e6c8 fill (resistor body) M.capblue() // #1a4a7a fill (capacitor body) M.gold() // #d4a017 fill (leads) M.silver() // #c0c0c0 fill M.edge() // black EdgesGeometry material (wireframe) M.hex(0xrrggbb) // arbitrary fill colour
Available primitive helpers
solidBox(w, h, d, mat) // box mesh + edge lines solidCyl(r, h, mat, seg=14) // cylinder mesh + edge lines textLabel(text, w, h, opts) // canvas-texture plane (SSR-safe, returns null on server) centreAtOrigin(group) // recentres a group's bounding box at (0,0,0) instrumentWire(from, to) // probe wire between two Vector3 points
Step 3 — Export the builder
Add the export to src/components/index.ts:
// src/components/index.ts
export { buildRelay, buildRelayStandalone } from './relay';Step 4 — Add the registry entry
Open src/labs/LabScene.tsx and add one entry to COMPONENT_REGISTRY:
// src/labs/LabScene.tsx
import { buildRelay, /* ... */ } from '@/components';
const COMPONENT_REGISTRY: Record<string, BuildFn> = {
// ... existing entries ...
'relay': (inst) => {
const d = inst as Extract<ComponentInstance, { type: 'relay' }>;
return buildRelay(hole(d.mountedAt.col, d.mountedAt.row));
},
};One import and one object entry. The renderer's buildInstancefunction is generic — it calls COMPONENT_REGISTRY[inst.type]and returns whatever the builder gives back. No switch statement, no fallthrough, no case to forget.