Skip to content

the ultimate vocal synthesizer - sing, speak, beatbox, and more

Rust

Face screen in 3D: the head you can turn, animated from the engine

Jameson Hodgedd2dc7e6139a
diff --git a/bun.lock b/bun.lock
index 7bdccac..94eaee6 100644
--- a/bun.lock
+++ b/bun.lock
@@ -7,12 +7,14 @@
"dependencies": {
"react": "19.3.1",
"react-dom": "19.3.1",
+ "three": "0.186.1",
"zustand": "5.0.16",
},
"devDependencies": {
"@types/bun": "1.4.4",
"@types/react": "19.3.0",
"@types/react-dom": "19.3.0",
+ "@types/three": "0.186.0",
"@vitejs/plugin-react": "6.2.1",
"playwright-core": "1.64.1",
"typescript": "7.0.2",
@@ -21,6 +23,8 @@
},
},
"packages": {
+ "@dimforge/rapier3d-compat": ["@dimforge/[email protected]", "", {}, "sha512-uekIGetywIgopfD97oDL5PfeezkFpNhwlzlaEYNOA0N6ghdsOvh/HYjSMek5Q2O1PYvRSDFcqFVJl4r4ZBwOow=="],
+
"@oxc-project/types": ["@oxc-project/[email protected]", "", {}, "sha512-1Duyg48TdJNdyhEB8Xn1L36bT9Pv6GUtlKjHxOys8xkaF/rQq3XJE+bAurSK/AgIz5mGFsb77cn9Pb20+WCScw=="],
"@rolldown/binding-android-arm-eabi": ["@rolldown/[email protected]", "", { "os": "android", "cpu": "arm" }, "sha512-ia3DwAs0OsMGS7+IConptSiyk9pEXkOx/AbJ2n/lAGxH3QkWQus1t8d7gnAT38FCU2sQu8lUbeR6AiZFv4JxaA=="],
@@ -55,6 +59,8 @@
"@rolldown/pluginutils": ["@rolldown/[email protected]", "", {}, "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw=="],
+ "@tweenjs/tween.js": ["@tweenjs/[email protected]", "", {}, "sha512-vJmvvwFxYuGnF2axRtPYocag6Clbb5YS7kLL+SO/TeVFzHqDIWrNKYtcsPMibjDx9O+bu+psAy9NKfWklassUA=="],
+
"@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.4.4" } }, "sha512-9wkRDXUXK124pEnAutE6n/l18j+i9GC5IAHK8TmX/2nlySETmky0OU6yL5U8pW1AoKh++AN9auDCr647BfurjQ=="],
"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~8.9.0" } }, "sha512-MzQj+8Kx3XI40OmUAE6Qoh58ZMNstNNMwn2nfd3qWsfe7nDNSqJNzes4njhz6AHwrSHOffSLwXU6yb474Q4aog=="],
@@ -63,6 +69,12 @@
"@types/react-dom": ["@types/[email protected]", "", { "peerDependencies": { "@types/react": "^19.3.0" } }, "sha512-ZI7bU42mZXXKHn/qNLEw2IrbiINU7X5+vfgdixBHkCNpYWXjKgfQ/P+uyGb5CjOLB9UcnTeg3rylQtV2hym44Q=="],
+ "@types/stats.js": ["@types/[email protected]", "", {}, "sha512-jIBvWWShCvlBqBNIZt0KAshWpvSjhkwkEu4ZUcASoAvhmrgAUI2t1dXrjSL4xXVLB4FznPrIsX3nKXFl/Dt4vA=="],
+
+ "@types/three": ["@types/[email protected]", "", { "dependencies": { "@dimforge/rapier3d-compat": "~0.12.0", "@tweenjs/tween.js": "~23.1.3", "@types/stats.js": "*", "@types/webxr": ">=0.5.17", "fflate": "~0.8.3", "meshoptimizer": "~1.1.1" } }, "sha512-mxYSBpDC+D0pLfSP6sW4WZTcT+nrtmZcimMqnVmy36Hte3XpeYSrvgg4TRdaM1GemGog1AWzI5qL2VoIfMXbJQ=="],
+
+ "@types/webxr": ["@types/[email protected]", "", {}, "sha512-h8fgEd/DpoS9CBrjEQXR+dIDraopAEfu4wYVNY2tEPwk60stPWhvZMf4Foo5FakuQ7HFZoa8WceaWFervK2Ovg=="],
+
"@typescript/typescript-aix-ppc64": ["@typescript/[email protected]", "", { "os": "aix", "cpu": "ppc64" }, "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ=="],
"@typescript/typescript-darwin-arm64": ["@typescript/[email protected]", "", { "os": "darwin", "cpu": "arm64" }, "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA=="],
@@ -113,6 +125,8 @@
"fdir": ["[email protected]", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="],
+ "fflate": ["[email protected]", "", {}, "sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA=="],
+
"fsevents": ["[email protected]", "", { "os": "darwin", "hasInstallScript": true }, "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw=="],
"lightningcss": ["[email protected]", "", { "dependencies": { "detect-libc": "^2.0.3" }, "optionalDependencies": { "lightningcss-android-arm64": "1.33.0", "lightningcss-darwin-arm64": "1.33.0", "lightningcss-darwin-x64": "1.33.0", "lightningcss-freebsd-x64": "1.33.0", "lightningcss-linux-arm-gnueabihf": "1.33.0", "lightningcss-linux-arm64-gnu": "1.33.0", "lightningcss-linux-arm64-musl": "1.33.0", "lightningcss-linux-x64-gnu": "1.33.0", "lightningcss-linux-x64-musl": "1.33.0", "lightningcss-win32-arm64-msvc": "1.33.0", "lightningcss-win32-x64-msvc": "1.33.0" } }, "sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA=="],
@@ -139,6 +153,8 @@
"lightningcss-win32-x64-msvc": ["[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-OlEICDx/Xl0FqSp4bry8zFnCvGpig3Gl4gCquvYwHuqJKEC1+n9NgDniFvqHGmMv1ZkqDJrDqKKSykTDX+ehuA=="],
+ "meshoptimizer": ["[email protected]", "", {}, "sha512-oRFNWJRDA/WTrVj7NWvqa5HqE1t9MYDj2VaWirQCzCCrAd2GHrqR/sQezCxiWATPNlKTcRaPRHPJwIRoPBAp5g=="],
+
"nanoid": ["[email protected]", "", { "bin": { "nanoid": "bin/nanoid.cjs" } }, "sha512-uKdg2G3GNCKQn9byYOpxbGqrT2fGO5KRt5J/8b3pok8rT6qxGWF6hxMyJiEYtAf+FVyYuD9hRaDqX5uPFYJ4ZQ=="],
"picocolors": ["[email protected]", "", {}, "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA=="],
@@ -159,6 +175,8 @@
"source-map-js": ["[email protected]", "", {}, "sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw=="],
+ "three": ["[email protected]", "", {}, "sha512-blFeqb49wRCSGUGj7gtpfnSGHy2lwDk94RhUmS1c/hTby70kvChbWpkJ4Pm1390LqzzvTmzgXKHPEafJwCb8jA=="],
+
"tinyglobby": ["[email protected]", "", { "dependencies": { "fdir": "^6.5.0", "picomatch": "^4.0.4" } }, "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g=="],
"typescript": ["[email protected]", "", { "optionalDependencies": { "@typescript/typescript-aix-ppc64": "7.0.2", "@typescript/typescript-darwin-arm64": "7.0.2", "@typescript/typescript-darwin-x64": "7.0.2", "@typescript/typescript-freebsd-arm64": "7.0.2", "@typescript/typescript-freebsd-x64": "7.0.2", "@typescript/typescript-linux-arm": "7.0.2", "@typescript/typescript-linux-arm64": "7.0.2", "@typescript/typescript-linux-loong64": "7.0.2", "@typescript/typescript-linux-mips64el": "7.0.2", "@typescript/typescript-linux-ppc64": "7.0.2", "@typescript/typescript-linux-riscv64": "7.0.2", "@typescript/typescript-linux-s390x": "7.0.2", "@typescript/typescript-linux-x64": "7.0.2", "@typescript/typescript-netbsd-arm64": "7.0.2", "@typescript/typescript-netbsd-x64": "7.0.2", "@typescript/typescript-openbsd-arm64": "7.0.2", "@typescript/typescript-openbsd-x64": "7.0.2", "@typescript/typescript-sunos-x64": "7.0.2", "@typescript/typescript-win32-arm64": "7.0.2", "@typescript/typescript-win32-x64": "7.0.2" }, "bin": { "tsc": "bin/tsc" } }, "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA=="],
diff --git a/package.json b/package.json
index cc4212c..528d0c0 100644
--- a/package.json
+++ b/package.json
@@ -14,12 +14,14 @@
"dependencies": {
"react": "19.3.1",
"react-dom": "19.3.1",
+ "three": "0.186.1",
"zustand": "5.0.16"
},
"devDependencies": {
"@types/bun": "1.4.4",
"@types/react": "19.3.0",
"@types/react-dom": "19.3.0",
+ "@types/three": "0.186.0",
"@vitejs/plugin-react": "6.2.1",
"playwright-core": "1.64.1",
"typescript": "7.0.2",
diff --git a/src/ui/App.tsx b/src/ui/App.tsx
index 42549da..3021be7 100644
--- a/src/ui/App.tsx
+++ b/src/ui/App.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useState } from "react";
+import { lazy, Suspense, useEffect, useState } from "react";
import { useStore } from "./store.ts";
import { engine } from "../audio/AudioEngine.ts";
import { AnatomyView } from "./components/AnatomyView.tsx";
@@ -22,6 +22,9 @@ import type { BodyName } from "../worklet/protocol.ts";
import { BodyPad } from "./components/BodyPad.tsx";
import { bodyLabel, presetOf, PRESETS } from "./body.ts";
+// three.js only loads when the 3D view is first shown
+const AnatomyView3D = lazy(() => import("./components/AnatomyView3D.tsx").then((m) => ({ default: m.AnatomyView3D })));
+
function TopBar() {
const started = useStore((s) => s.started);
const starting = useStore((s) => s.starting);
@@ -95,6 +98,7 @@ function ViewToggles() {
const bestiary = useStore((s) => s.mode === "bestiary");
return (
<div className="view-toggles">
+ {!bestiary && <Toggle label="3D" checked={view.threeD} onChange={(v) => setView({ threeD: v })} />}
<Toggle label="Sound pressure" checked={view.pressure} onChange={(v) => setView({ pressure: v })} />
{!bestiary && <Toggle label="Airflow" checked={view.particles} onChange={(v) => setView({ particles: v })} />}
<Toggle label={bestiary ? "Labels" : "Measurements"} checked={view.calipers} onChange={(v) => setView({ calipers: v })} />
@@ -147,17 +151,21 @@ function StartPrompt() {
function LeftScreen() {
const mode = useStore((s) => s.mode);
const body = useStore((s) => s.body);
+ const threeD = useStore((s) => s.view.threeD);
const bestiary = mode === "bestiary";
+ const Face = threeD ? AnatomyView3D : AnatomyView;
return (
<Screen
className="left"
focus
title={bestiary ? "Creature" : "Vocal tract"}
- sub={bestiary ? "Airway / live" : `${bodyLabel(body)} / midsagittal`}
+ sub={bestiary ? "Airway / live" : `${bodyLabel(body)} / ${threeD ? "3D" : "midsagittal"}`}
label={bestiary ? "Live creature airway" : "Live vocal tract"}
tools={<ViewToggles />}
>
- {bestiary ? <CreatureView /> : mode === "speak" ? <AnatomyView slots={speechSlots} info={speechInfo} overlay={speechWords} /> : mode === "sing" ? <AnatomyView info={singInfo} overlay={singWords} /> : mode === "beatbox" ? <AnatomyView slots={beatSlots} info={beatInfo} showFormants={false} /> : <AnatomyView />}
+ <Suspense fallback={null}>
+ {bestiary ? <CreatureView /> : mode === "speak" ? <Face slots={speechSlots} info={speechInfo} overlay={speechWords} /> : mode === "sing" ? <Face info={singInfo} overlay={singWords} /> : mode === "beatbox" ? <Face slots={beatSlots} info={beatInfo} showFormants={false} /> : <Face />}
+ </Suspense>
{mode === "beatbox" && <BeatOverlay />}
<StartPrompt />
</Screen>
diff --git a/src/ui/components/AnatomyView.tsx b/src/ui/components/AnatomyView.tsx
index aee4307..5ad503c 100644
--- a/src/ui/components/AnatomyView.tsx
+++ b/src/ui/components/AnatomyView.tsx
@@ -1,5 +1,5 @@
import { useEffect, useRef, useState } from "react";
-import { AnatomyRenderer, type HandleId, type Slot } from "../render/anatomy.ts";
+import { AnatomyRenderer, type FaceView, type HandleId, type Slot } from "../render/anatomy.ts";
import { Glow } from "../render/vector.ts";
import { engine } from "../../audio/AudioEngine.ts";
import { sameBody } from "../body.ts";
@@ -16,7 +16,7 @@ export type SlotsFn = (m: VoiceModel, a: HumanArtic, f: HumanFrame, formants: nu
export type InfoFn = (m: VoiceModel, f: HumanFrame, tele: TelemetryMessage | null) => string[];
/** drawn over the face each frame, before the glow (Sing and Speak write the words at the mouth) */
-export type OverlayFn = (ctx: CanvasRenderingContext2D, r: AnatomyRenderer, m: VoiceModel, f: HumanFrame, a: HumanArtic, size: { w: number; h: number; dt: number }) => void;
+export type OverlayFn = (ctx: CanvasRenderingContext2D, r: FaceView, m: VoiceModel, f: HumanFrame, a: HumanArtic, size: { w: number; h: number; dt: number }) => void;
/** the tract's length and mouth opening, then the live pitch and lungs */
export const tractInfo: InfoFn = (_m, f, tele) => {
diff --git a/src/ui/components/AnatomyView3D.tsx b/src/ui/components/AnatomyView3D.tsx
new file mode 100644
index 0000000..34d6df8
--- /dev/null
+++ b/src/ui/components/AnatomyView3D.tsx
@@ -0,0 +1,192 @@
+/**
+ * The face screen in 3D: the head, turned with the pointer (drag to turn, wheel or pinch to zoom,
+ * right-drag to pan, double-click to go back). The articulators can't be dragged here; the 2D
+ * view keeps the handles. three.js draws into its own canvas, which is copied into the screen's
+ * 2D canvas each frame so the readouts, the words at the mouth and the glow pass are the 2D
+ * view's own.
+ */
+import { useEffect, useRef, useState } from "react";
+import * as THREE from "three";
+import { OrbitControls } from "three/addons/controls/OrbitControls.js";
+import { Anatomy3D } from "../render/anatomy3d.ts";
+import { drawReadouts } from "../render/readouts.ts";
+import { Glow, label } from "../render/vector.ts";
+import { engine } from "../../audio/AudioEngine.ts";
+import { sameBody } from "../body.ts";
+import { useStore } from "../store.ts";
+import { VoiceModel } from "../analysis.ts";
+import { useModel } from "../useModel.ts";
+import { V } from "../colors.ts";
+import { articulatorSlots, tractInfo, type InfoFn, type OverlayFn, type SlotsFn } from "./AnatomyView.tsx";
+
+const ease = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2);
+
+export function AnatomyView3D(props: { slots?: SlotsFn; info?: InfoFn; showFormants?: boolean; overlay?: OverlayFn }) {
+ const overlayRef = useRef<OverlayFn | undefined>(props.overlay);
+ overlayRef.current = props.overlay;
+ const formantsRef = useRef(props.showFormants ?? true);
+ formantsRef.current = props.showFormants ?? true;
+ const slotsRef = useRef<SlotsFn>(props.slots ?? articulatorSlots);
+ slotsRef.current = props.slots ?? articulatorSlots;
+ const infoRef = useRef<InfoFn>(props.info ?? tractInfo);
+ infoRef.current = props.info ?? tractInfo;
+ const canvasRef = useRef<HTMLCanvasElement>(null);
+ const wrapRef = useRef<HTMLDivElement>(null);
+ const model = useModel();
+ const [failed, setFailed] = useState(false);
+
+ useEffect(() => {
+ const canvas = canvasRef.current!;
+ const ctx = canvas.getContext("2d")!;
+ const wrap = wrapRef.current!;
+ const glCanvas = document.createElement("canvas");
+ let gl: THREE.WebGLRenderer;
+ try {
+ gl = new THREE.WebGLRenderer({ canvas: glCanvas, antialias: true, alpha: false, powerPreference: "high-performance" });
+ } catch {
+ setFailed(true);
+ return;
+ }
+ // the palette's sRGB values go straight through, as on the 2D screens
+ gl.outputColorSpace = THREE.LinearSRGBColorSpace;
+ gl.setClearColor(0x000000, 1);
+ const r = new Anatomy3D();
+ const glow = new Glow();
+ const camera = r.camera;
+ const controls = new OrbitControls(camera, canvas);
+ controls.enableDamping = true;
+ controls.dampingFactor = 0.08;
+ controls.rotateSpeed = 0.7;
+ controls.zoomSpeed = 0.8;
+ controls.screenSpacePanning = true;
+ controls.minDistance = 20;
+ controls.maxDistance = 220;
+
+ // camera moves (the opening turn from the side view, double-click home): orbit round the target
+ const tween = { on: false, t: 0, dur: 1, p0: new THREE.Vector3(), q0: new THREE.Vector3(), p1: new THREE.Vector3(), q1: new THREE.Vector3() };
+ const s0 = new THREE.Spherical(), s1 = new THREE.Spherical(), off = new THREE.Vector3();
+ const moveTo = (pos: THREE.Vector3, target: THREE.Vector3, dur: number) => {
+ tween.p0.copy(camera.position);
+ tween.q0.copy(controls.target);
+ tween.p1.copy(pos);
+ tween.q1.copy(target);
+ tween.t = 0;
+ tween.dur = dur;
+ tween.on = true;
+ };
+ const stepTween = (dt: number) => {
+ if (!tween.on) return;
+ tween.t = Math.min(1, tween.t + dt / tween.dur);
+ const e = ease(tween.t);
+ s0.setFromVector3(off.copy(tween.p0).sub(tween.q0));
+ s1.setFromVector3(off.copy(tween.p1).sub(tween.q1));
+ let dth = s1.theta - s0.theta;
+ dth -= Math.round(dth / (2 * Math.PI)) * 2 * Math.PI;
+ s0.set(s0.radius + (s1.radius - s0.radius) * e, s0.phi + (s1.phi - s0.phi) * e, s0.theta + dth * e);
+ controls.target.lerpVectors(tween.q0, tween.q1, e);
+ camera.position.copy(controls.target).add(off.setFromSpherical(s0));
+ if (tween.t >= 1) tween.on = false;
+ };
+ controls.addEventListener("start", () => (tween.on = false));
+ let posed: VoiceModel | null = null;
+ const home = () => {
+ const m = model.current;
+ if (m) {
+ const p = r.poses(m);
+ moveTo(p.home, p.target, 0.9);
+ }
+ };
+ canvas.addEventListener("dblclick", home);
+
+ let w = 0, h = 0, dpr = 1;
+ const fit = () => {
+ const b = wrap.getBoundingClientRect();
+ dpr = Math.min(2, window.devicePixelRatio || 1);
+ w = b.width;
+ h = b.height;
+ canvas.width = Math.max(1, Math.round(w * dpr));
+ canvas.height = Math.max(1, Math.round(h * dpr));
+ gl.setPixelRatio(dpr);
+ gl.setSize(Math.max(1, w), Math.max(1, h), false);
+ r.resize(Math.max(1, w), Math.max(1, h));
+ };
+ const ro = new ResizeObserver(fit);
+ ro.observe(wrap);
+ let raf = 0;
+ let last = performance.now();
+ let fTick = 0;
+ let formants: number[] = [];
+ const loop = (now: number) => {
+ const dt = Math.min(0.05, (now - last) / 1000);
+ last = now;
+ if (Math.min(2, window.devicePixelRatio || 1) !== dpr) fit();
+ const st = useStore.getState();
+ const tele = engine.latest && sameBody(engine.latest.body, st.body) ? engine.latest : null;
+ const a = tele ? VoiceModel.articFromArray(tele.t.artic) : st.artic;
+ const m = model.current;
+ if (m && w > 0 && h > 0) {
+ if (posed !== m) {
+ const p = r.poses(m);
+ if (!posed) {
+ // open on the 2D view's side view, then turn to show the face
+ camera.position.copy(p.side);
+ controls.target.copy(p.target);
+ moveTo(p.home, p.target, 1.6);
+ } else {
+ // another body: keep the view, centred on the new head
+ const q = r.poses(posed);
+ off.copy(p.target).sub(q.target);
+ camera.position.add(off);
+ controls.target.add(off);
+ }
+ posed = m;
+ }
+ stepTween(dt);
+ controls.update(dt);
+ const f = m.frame(a);
+ if (++fTick % 4 === 0 || formants.length === 0) formants = m.transfer(a).formants;
+ r.update(m, f, a, tele, st.view, dt);
+ gl.render(r.scene, camera);
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
+ ctx.globalCompositeOperation = "source-over";
+ ctx.fillStyle = "#000";
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
+ ctx.drawImage(glCanvas, 0, 0, canvas.width, canvas.height);
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
+ if (r.caliper) {
+ const [cx, cy] = r.project(r.caliper.x, r.caliper.y, r.caliper.z);
+ label(ctx, r.caliper.area <= 0.0005 ? "CLOSED" : `${r.caliper.area.toFixed(2)} CM²`, cx + 10, cy - 6, { size: 11, color: V.white, ls: 0.12 });
+ }
+ const info = infoRef.current(m, f, tele).map((s) => s.toUpperCase());
+ const slots = slotsRef.current(m, a, f, formants, tele);
+ drawReadouts(ctx, { w, h, compact: r.compact, slotX: w - Math.min(340, w * 0.42), slotY: h - Math.min(236, h * 0.3) }, formantsRef.current ? formants : [], info, slots);
+ overlayRef.current?.(ctx, r.faceView(m, f, a), m, f, a, { w, h, dt });
+ glow.apply(canvas, ctx, dpr);
+ }
+ raf = requestAnimationFrame(loop);
+ };
+ raf = requestAnimationFrame(loop);
+ return () => {
+ cancelAnimationFrame(raf);
+ ro.disconnect();
+ canvas.removeEventListener("dblclick", home);
+ controls.dispose();
+ r.dispose();
+ gl.dispose();
+ // browsers allow few live WebGL contexts: give this one back now, not at garbage collection
+ gl.forceContextLoss();
+ };
+ }, [model]);
+
+ if (failed)
+ return (
+ <div className="anatomy three">
+ <p className="no-webgl">3D needs WebGL, which this browser has turned off. Untick 3D for the flat view.</p>
+ </div>
+ );
+ return (
+ <div ref={wrapRef} className="anatomy three">
+ <canvas ref={canvasRef} aria-label="Vocal tract in 3D. Drag to turn the head, scroll or pinch to zoom, double-click to go back to the starting view." role="img" />
+ </div>
+ );
+}
diff --git a/src/ui/render/anatomy.ts b/src/ui/render/anatomy.ts
index b3c911c..c8304fb 100644
--- a/src/ui/render/anatomy.ts
+++ b/src/ui/render/anatomy.ts
@@ -11,11 +11,14 @@
*/
import type { HumanArtic, HumanFrame } from "../../wasm/types.ts";
import type { Vec } from "./geom.ts";
-import { rotateAbout, dist, catmullRom } from "./geom.ts";
import type { TelemetryMessage } from "../../worklet/protocol.ts";
import type { VoiceModel } from "../analysis.ts";
import { V, heat, pressureLevel } from "../colors.ts";
-import { beam, beamPts, chaikin, dot, handle, ipaGlyph, label, ring, seg, type Pt } from "./vector.ts";
+import { beam, beamPts, chaikin, dot, handle, label, ring, seg, type Pt } from "./vector.ts";
+import { buildProfile, Gaze, jawFn, VIEW, type Profile } from "./profile.ts";
+import { drawReadouts, type Slot } from "./readouts.ts";
+
+export type { Slot } from "./readouts.ts";
export type HandleId = "tongue" | "tip" | "jaw" | "lips" | "velum" | "larynx";
@@ -25,6 +28,29 @@ export interface Handle {
label: string;
}
+/** where the articulators' handles sit (body cm) */
+export function faceHandles(model: VoiceModel, f: HumanFrame, a: HumanArtic): Handle[] {
+ const t = model.tract;
+ const J = jawFn(model, a);
+ const lips = { x: (f.upperLip[6].x + f.lowerLip[6].x) / 2 + 0.15, y: (f.upperLip[6].y + f.lowerLip[6].y) / 2 };
+ return [
+ { id: "tongue", pos: f.tongueCenter, label: "Tongue body" },
+ { id: "tip", pos: f.tip, label: "Tongue tip" },
+ { id: "jaw", pos: J(7.5, 4.9), label: "Jaw" },
+ { id: "lips", pos: lips, label: "Lips" },
+ { id: "velum", pos: f.velum[Math.floor(f.velum.length / 2)], label: "Soft palate" },
+ { id: "larynx", pos: { x: f.glottis.x + 1.2 * t.scale, y: f.glottis.y }, label: "Larynx" },
+ ];
+}
+
+/** What an overlay needs of a face view: where a body point lands on screen, and the scale there. */
+export interface FaceView {
+ handles(model: VoiceModel, f: HumanFrame, a: HumanArtic): Handle[];
+ X(p: Vec): number;
+ Y(p: Vec): number;
+ readonly pxPerCm: number;
+}
+
export interface ViewFlags {
pressure: boolean;
particles: boolean;
@@ -32,16 +58,6 @@ export interface ViewFlags {
calipers: boolean;
}
-/** an OP-1 style readout in the space under the chin */
-export interface Slot {
- label: string;
- value: string;
- unit?: string;
- color: string;
- /** value is an IPA symbol, drawn as strokes */
- ipa?: boolean;
-}
-
interface Particle {
s: number;
u: number; // lateral position −1..1
@@ -59,39 +75,6 @@ interface Echo {
lowerLip: Pt[];
}
-/** Body-space outlines for one frame (rebuilt when the frame changes). */
-interface Geometry {
- f: HumanFrame;
- model: VoiceModel;
- skinLower: Vec[];
- skinUpper: Vec[];
- inset: Vec[];
- skull: Vec[];
- brain: Vec[];
- sulcus: Vec[];
- eye: Vec[];
- eyeC: Vec;
- vertebrae: Vec[][];
- cords: Vec[][];
- nasal: Vec[];
- turbinates: Vec[][];
- palOral: Vec[];
- palNasal: Vec[];
- jawLower: Vec[];
- jawUpper: Vec[];
- hyoid: Vec[];
- thyroid: Vec[];
- trachea: Vec[][];
- rings: Vec[][];
- outerWall: Vec[];
- glottisWall: Vec[];
- fibres: Vec[][];
- folds: Vec[][];
- lowerTeeth: Vec[];
-}
-
-/** the visible part of the head, in body cm */
-const VIEW = { x0: -7.5, x1: 10.25, y0: -4.55, y1: 19.9 };
const ECHO_AGES = [0.09, 0.06, 0.03];
export class AnatomyRenderer {
@@ -104,12 +87,12 @@ export class AnatomyRenderer {
/** a narrow screen (a phone): readouts in a row above the head, slots in a row below it */
private compact = false;
private sparkSeed = 1;
- private geo: Geometry | null = null;
+ private geo: Profile | null = null;
private echoes: Echo[] = [];
private clock = 0;
private lastEcho = -1;
- /** where the eye looks (pitch up +, yaw out of the plane), easing toward a target picked now and then */
- private gaze = { pitch: 0, yaw: 0, toPitch: 0, toYaw: 0, next: 2 };
+ /** where the eye looks */
+ private gaze = new Gaze();
constructor() {
for (let i = 0; i < 170; i++) this.particles.push({ s: Math.random() * 17, u: Math.random() * 2 - 1, life: Math.random(), px: NaN, py: NaN });
@@ -149,141 +132,13 @@ export class AnatomyRenderer {
}
handles(model: VoiceModel, f: HumanFrame, a: HumanArtic): Handle[] {
- const t = model.tract;
- const J = this.jawFn(model, a);
- const lips = { x: (f.upperLip[6].x + f.lowerLip[6].x) / 2 + 0.15, y: (f.upperLip[6].y + f.lowerLip[6].y) / 2 };
- return [
- { id: "tongue", pos: f.tongueCenter, label: "Tongue body" },
- { id: "tip", pos: f.tip, label: "Tongue tip" },
- { id: "jaw", pos: J(7.5, 4.9), label: "Jaw" },
- { id: "lips", pos: lips, label: "Lips" },
- { id: "velum", pos: f.velum[Math.floor(f.velum.length / 2)], label: "Soft palate" },
- { id: "larynx", pos: { x: f.glottis.x + 1.2 * t.scale, y: f.glottis.y }, label: "Larynx" },
- ];
- }
-
- private jawFn(model: VoiceModel, a: HumanArtic): (x: number, y: number) => Vec {
- const t = model.tract;
- const k = t.scale;
- const condyle = t.P(-2.5, 10.5);
- const jawRef = t.P(6.95, 7.75);
- const phi = (-Math.max(-0.2, Math.min(3, a.jaw)) * k) / dist(condyle, jawRef);
- return (x: number, y: number) => rotateAbout(t.P(x, y), condyle, phi);
+ return faceHandles(model, f, a);
}
// ------------------------------------------------------------------ geometry
- private geometry(model: VoiceModel, f: HumanFrame, a: HumanArtic): Geometry {
+ private geometry(model: VoiceModel, f: HumanFrame, a: HumanArtic): Profile {
if (this.geo && this.geo.f === f && this.geo.model === model) return this.geo;
- const t = model.tract;
- const P = (x: number, y: number) => t.P(x, y);
- const J = this.jawFn(model, a);
- const k = t.scale;
- const lar = a.larynx * k;
- const ph = model.bodyParams.pharynx;
- // facial profile in true body coordinates (P()'s oral-region x compression would flatten the nose)
- const B = (x: number, y: number): Vec => ({ x: x * k, y: (y < 7 ? 7 - (7 - y) * ph : y) * k });
- const ulOut = f.upperLip.slice(7); // edge → nose
- const llOut = f.lowerLip.slice(7); // edge → chin
- const neckFront = catmullRom([B(2.3, -4.6), B(2.38, -1.5), B(2.52, 0.4), B(3.0, 1.7), J(5.3, 3.0), J(7.25, 3.35), J(7.95, 4.6), J(7.55, 5.7)], 4);
- const face = catmullRom([ulOut[ulOut.length - 1], B(7.05, 10.6), B(7.75, 10.78), B(8.75, 11.05), B(9.25, 11.65), B(8.95, 12.6), B(8.35, 13.9), B(7.7, 15.0), B(7.95, 16.1), B(7.9, 18.1), B(6.9, 20.5)], 4);
- const cranium = catmullRom([B(6.9, 20.5), B(3.0, 22.9), B(-2.2, 22.7), B(-6.9, 19.6), B(-8.7, 15.4), B(-8.3, 11.9), B(-7.4, 8.9), B(-6.8, 4.8), B(-6.6, 0), B(-6.6, -4.6)], 4);
- const ellipse = (c: Vec, rx: number, ry: number, rot = 0, n = 40): Vec[] => {
- // rotation is in screen space (y down) in the MRI renderer's convention: negate for body space
- const cr = Math.cos(-rot), sr = Math.sin(-rot);
- const o: Vec[] = [];
- for (let i = 0; i <= n; i++) {
- const an = (i / n) * Math.PI * 2;
- const x = Math.cos(an) * rx, y = Math.sin(an) * ry;
- o.push({ x: c.x + x * cr - y * sr, y: c.y + x * sr + y * cr });
- }
- return o;
- };
- const roundRect = (p0: Vec, p1: Vec, r: number): Vec[] => {
- const x0 = Math.min(p0.x, p1.x), x1 = Math.max(p0.x, p1.x), y0 = Math.min(p0.y, p1.y), y1 = Math.max(p0.y, p1.y);
- const rr = Math.min(r, (x1 - x0) / 2, (y1 - y0) / 2);
- const o: Vec[] = [];
- const corner = (cx: number, cy: number, a0: number) => {
- for (let i = 0; i <= 6; i++) {
- const an = a0 + (i / 6) * (Math.PI / 2);
- o.push({ x: cx + Math.cos(an) * rr, y: cy + Math.sin(an) * rr });
- }
- };
- corner(x1 - rr, y1 - rr, 0);
- corner(x0 + rr, y1 - rr, Math.PI / 2);
- corner(x0 + rr, y0 + rr, Math.PI);
- corner(x1 - rr, y0 + rr, (3 * Math.PI) / 2);
- o.push(o[0]);
- return o;
- };
- const wallX = (y: number) => -1.2 - 0.025 * y;
- const vertebrae: Vec[][] = [];
- for (let i = 0; i < 9; i++) {
- const y0 = -4.4 + i * 1.75;
- if (y0 > 10.5) break;
- const x0 = wallX(y0) - 2.25;
- vertebrae.push(roundRect(P(x0, y0 + 1.4), P(x0 + 1.6, y0), 0.32 * k));
- }
- const palRaw: [number, number][] = [[1.0, 9.6], [1.8, 9.92], [2.7, 10.18], [3.6, 10.3], [4.4, 10.25], [5.1, 10.05], [5.7, 9.72], [6.15, 9.36], [6.55, 9.15], [7.0, 9.6]];
- const palOral = palRaw.map(([x, y]) => t.P(x, y));
- // the mandible's body only: its ramus is lateral, not in the midsagittal plane
- const jb = f.jawBone.filter((p) => p.x > 0.8 * k);
- const g = f.glottis;
- const hy = { x: P(1.6, 0).x, y: P(0, 2.9).y + 0.6 * lar };
- const rings: Vec[][] = [];
- for (let i = 0; i < 4; i++) {
- const y = g.y - (0.9 + i * 1.0) * k;
- if (y < -4.4) break;
- rings.push([{ x: g.x + 0.8 * k, y }, { x: g.x + 1.05 * k, y: y - 0.05 }, { x: g.x + 1.05 * k, y: y - 0.5 * k }, { x: g.x + 0.82 * k, y: y - 0.45 * k }, { x: g.x + 0.8 * k, y }]);
- }
- const origin = J(6.0, 5.45);
- const tc = f.tongueCenter;
- const fibres: Vec[][] = [];
- for (let i = 0; i <= 16; i++) {
- const ang = Math.PI * (0.18 + (0.9 * i) / 16);
- const end = { x: tc.x + Math.cos(ang) * f.tongueRadius * 1.25, y: tc.y + Math.sin(ang) * f.tongueRadius * 1.25 };
- const ctrl = { x: tc.x, y: (tc.y + origin.y) / 2 };
- const c: Vec[] = [];
- for (let j = 0; j <= 12; j++) {
- const u = j / 12;
- c.push({ x: (1 - u) * (1 - u) * origin.x + 2 * (1 - u) * u * ctrl.x + u * u * end.x, y: (1 - u) * (1 - u) * origin.y + 2 * (1 - u) * u * ctrl.y + u * u * end.y });
- }
- fibres.push(c);
- }
- const wallAtGlottis = { x: wallX(g.y / k) * k, y: g.y };
- const geo: Geometry = {
- f,
- model,
- skinLower: [...neckFront, ...llOut.slice().reverse()],
- skinUpper: [...ulOut, ...face.slice(1), ...cranium.slice(1)],
- inset: catmullRom([B(1.85, -4.6), B(1.9, -0.8), B(2.3, 1.6), J(5.2, 3.6), J(6.9, 3.9), J(7.4, 4.9), J(7.2, 5.9), B(6.6, 9.9), B(7.6, 10.9), B(8.55, 11.5), B(8.1, 13.4), B(7.25, 15.2), B(7.45, 18.0), B(3.0, 22.2), B(-2.0, 22.0), B(-6.3, 19.2), B(-7.9, 15.3), B(-7.6, 12.0), B(-6.7, 8.7), B(-6.1, 4.6), B(-6.0, -4.6)], 3),
- skull: catmullRom([B(7.0, 16.2), B(7.3, 18.5), B(6.0, 20.7), B(3.0, 22.1), B(-2.0, 22.0), B(-6.3, 19.2), B(-7.9, 15.3), B(-7.4, 12.6), B(-4.6, 12.2), B(-2.2, 13.3), B(1.0, 13.7), B(4.1, 14.1), B(6.2, 14.9)], 4),
- brain: catmullRom([B(6.55, 16.3), B(6.75, 18.4), B(5.5, 20.1), B(2.8, 21.4), B(-1.9, 21.3), B(-5.8, 18.8), B(-7.1, 15.4), B(-6.7, 13.1), B(-4.4, 12.8), B(-2.1, 13.9), B(1.0, 14.3), B(4.0, 14.7), B(5.9, 15.4)], 4),
- sulcus: catmullRom([B(5.4, 19.4), B(2.4, 20.5), B(-1.4, 20.4), B(-4.8, 18.4)], 4),
- eye: ellipse(B(6.5, 15.4), 1.1 * k, 0.95 * k),
- eyeC: B(6.5, 15.4),
- vertebrae,
- cords: [[P(-5.3, -4.6), P(-4.5, -4.6), P(-4.5, 11.8), P(-5.1, 12.4), P(-5.6, 11.6)], [P(-5.1, -4.6), P(-4.7, -4.6), P(-4.72, 11.4), P(-5.15, 11.6)]],
- nasal: catmullRom([B(0.3, 11.2), B(1.1, 10.25), B(2.7, 10.75), B(4.6, 10.85), B(6.4, 10.65), B(7.6, 10.85), B(8.45, 11.3), B(8.0, 12.2), B(6.7, 13.4), B(4.4, 13.9), B(1.8, 13.6), B(0.2, 12.6)], 4),
- turbinates: ([[3.0, 11.45, 2.1, 0.32], [3.5, 12.45, 1.9, 0.3], [4.0, 13.3, 1.45, 0.22]] as const).map(([cx, cy, rx, ry]) => ellipse(B(cx, cy), rx * k, ry * k, -0.04)),
- palOral,
- palNasal: palOral.map((p) => ({ x: p.x, y: p.y + 0.55 * k })),
- jawLower: jb,
- jawUpper: jb.map((p) => ({ x: p.x, y: p.y + 0.75 * k })),
- hyoid: ellipse(hy, 0.5 * k, 0.28 * k, 0.3, 28),
- thyroid: [{ x: g.x + 0.75 * k, y: g.y - 1.6 * k }, { x: g.x + 1.75 * k, y: g.y - 0.6 * k }, { x: g.x + 1.95 * k, y: g.y + 1.2 * k }, { x: g.x + 1.45 * k, y: g.y + 1.9 * k }, { x: g.x + 1.15 * k, y: g.y + 1.1 * k }, { x: g.x + 1.3 * k, y: g.y - 0.4 * k }, { x: g.x + 0.75 * k, y: g.y - 1.6 * k }],
- trachea: [[{ x: g.x - 0.65 * k, y: g.y }, { x: g.x - 0.75 * k, y: -4.8 }], [{ x: g.x + 0.75 * k, y: g.y }, { x: g.x + 0.95 * k, y: -4.8 }]],
- rings,
- outerWall: f.outer.filter((p) => p.y > g.y || p.x > 0),
- glottisWall: [wallAtGlottis, f.outer.find((p) => p.y > g.y) ?? wallAtGlottis],
- fibres,
- folds: [
- [{ x: g.x - 0.62 * k, y: g.y + 0.05 }, { x: g.x - 0.1, y: g.y + 0.12 * k }, { x: g.x - 0.62 * k, y: g.y + 0.32 * k }, { x: g.x - 0.62 * k, y: g.y + 0.05 }],
- [{ x: g.x + 0.72 * k, y: g.y + 0.05 }, { x: g.x + 0.12, y: g.y + 0.12 * k }, { x: g.x + 0.72 * k, y: g.y + 0.32 * k }, { x: g.x + 0.72 * k, y: g.y + 0.05 }],
- ],
- lowerTeeth: [...f.lowerTeeth, J(6.8, 6.0), J(6.4, 6.2), f.lowerTeeth[0]],
- };
- this.geo = geo;
- return geo;
+ return (this.geo = buildProfile(model, f, a));
}
/**
@@ -294,15 +149,7 @@ export class AnatomyRenderer {
*/
private lens(c: Vec, k: number, dt: number): Vec[] {
const g = this.gaze;
- if (this.clock >= g.next) {
- const centre = Math.random() < 0.35;
- g.toPitch = centre ? 0 : (Math.random() * 2 - 1) * 0.55;
- g.toYaw = centre ? 0 : (Math.random() * 2 - 1) * 0.9;
- g.next = this.clock + 1.2 + Math.random() * 3.5;
- }
- const ease = 1 - Math.exp(-dt * 28);
- g.pitch += (g.toPitch - g.pitch) * ease;
- g.yaw += (g.toYaw - g.yaw) * ease;
+ g.update(this.clock, dt);
const off = 0.85 * k * Math.cos(g.yaw);
const cp = Math.cos(g.pitch), sp = Math.sin(g.pitch);
const lc = { x: c.x + off * cp, y: c.y + off * sp };
@@ -552,62 +399,7 @@ export class AnatomyRenderer {
}
private drawReadouts(ctx: CanvasRenderingContext2D, formants: number[], info: string[], slots: Slot[]): void {
- if (this.compact) return this.drawReadoutsCompact(ctx, formants, info, slots);
- const fx = this.w - 18;
- let y = 26;
- if (formants.length >= 3) {
- ["F1", "F2", "F3"].forEach((lab, i) => {
- label(ctx, lab, fx - 78, y + i * 22, { size: 12, color: V.amber, ls: 0.12, alpha: 0.75 });
- label(ctx, String(Math.round(formants[i])), fx, y + i * 22, { size: 15, color: V.amber, align: "right", ls: 0.08 });
- });
- label(ctx, "HZ", fx, y + 64, { size: 10, color: V.amber, align: "right", alpha: 0.6 });
- y += 110;
- }
- for (const s of info) {
- label(ctx, s, fx, y, { size: 10, color: V.blueMid, align: "right", ls: 0.14 });
- y += 16;
- }
- if (!slots.length) return;
- // the empty space under the chin and in front of the neck
- const gx0 = this.X({ x: 3.35, y: 0 }), gy0 = this.Y({ x: 0, y: 2.05 });
- const cellW = Math.max(60, (this.w - 14 - gx0) / 2);
- const cellH = Math.max(56, Math.min(112, (this.h - 8 - gy0) / 2));
- const big = Math.max(22, Math.min(44, cellH * 0.4, cellW * 0.32));
- slots.slice(0, 4).forEach((s, i) => {
- const cx = gx0 + (i % 2) * cellW, cy = gy0 + Math.floor(i / 2) * cellH;
- dot(ctx, cx + 6, cy + 4, 4, s.color);
- label(ctx, s.label.toUpperCase(), cx + 16, cy + 8, { size: 11, color: s.color, ls: 0.2, alpha: 0.85 });
- const by = cy + 18 + big * 1.1;
- if (s.ipa) ipaGlyph(ctx, cx + big * 0.6, by, s.value, big * 1.45, s.color);
- else label(ctx, s.value.toUpperCase(), cx, by, { size: s.value.length > 3 ? big * 0.82 : big, color: s.color, weight: "m", ls: 0.04 });
- if (s.unit) label(ctx, s.unit.toUpperCase(), s.ipa ? cx + big * 1.5 : cx + (s.value.length > 3 ? 0 : big * 1.64), s.ipa || s.value.length <= 3 ? by : by + 18, { size: 11, color: s.color, ls: 0.16, alpha: 0.7 });
- });
- }
-
- /** narrow screens: formants and info in two rows on top, the four slots in a row at the bottom */
- private drawReadoutsCompact(ctx: CanvasRenderingContext2D, formants: number[], info: string[], slots: Slot[]): void {
- const w = this.w, h = this.h;
- if (formants.length >= 3) {
- const cw = (w - 24) / 3;
- ["F1", "F2", "F3"].forEach((lab, i) => {
- label(ctx, lab, 12 + i * cw, 18, { size: 10, color: V.amber, ls: 0.12, alpha: 0.75 });
- label(ctx, String(Math.round(formants[i])), 12 + i * cw + 26, 18, { size: 13, color: V.amber, ls: 0.06 });
- });
- }
- label(ctx, info.slice(0, 2).join(" "), 12, 36, { size: 9, color: V.blueMid, ls: 0.12 });
- if (info.length > 2) label(ctx, info.slice(2).join(" "), 12, 50, { size: 9, color: V.blueMid, ls: 0.12 });
- const four = slots.slice(0, 4);
- if (!four.length) return;
- const cw = (w - 24) / four.length, y0 = h - 82;
- const big = Math.max(16, Math.min(30, cw * 0.28));
- four.forEach((s, i) => {
- const cx = 12 + i * cw;
- dot(ctx, cx + 4, y0, 3, s.color);
- label(ctx, s.label.toUpperCase(), cx + 12, y0 + 3, { size: 8, color: s.color, ls: 0.14, alpha: 0.85 });
- const by = y0 + 18 + big;
- if (s.ipa) ipaGlyph(ctx, cx + big * 0.5, by, s.value, big * 1.4, s.color);
- else label(ctx, s.value.toUpperCase(), cx, by, { size: s.value.length > 3 ? big * 0.62 : big, color: s.color, weight: "m", ls: 0.02 });
- if (s.unit) label(ctx, s.unit.toUpperCase(), cx, by + 16, { size: 8, color: s.color, ls: 0.14, alpha: 0.7 });
- });
+ // the slots sit in the empty space under the chin and in front of the neck
+ drawReadouts(ctx, { w: this.w, h: this.h, compact: this.compact, slotX: this.X({ x: 3.35, y: 0 }), slotY: this.Y({ x: 0, y: 2.05 }) }, formants, info, slots);
}
}
diff --git a/src/ui/render/anatomy3d.ts b/src/ui/render/anatomy3d.ts
new file mode 100644
index 0000000..f0f6565
--- /dev/null
+++ b/src/ui/render/anatomy3d.ts
@@ -0,0 +1,1293 @@
+/**
+ * The face screen in 3D: the midsagittal anatomy of the 2D view grown into a head you can turn,
+ * drawn as glowing lines like everything else.
+ *
+ * The engine's anatomy is a midsagittal section, so width comes from three places:
+ * - the area function: each line of the airway's measuring grid has a gap and an area, which make
+ * an elliptical cross-section, so the 3D airway is exactly the tube the acoustics see;
+ * - the articulators' midline outlines, swept sideways with anatomical widths: the tongue and soft
+ * palate as ribbed surfaces, the lips as a patch whose opening is the engine's aperture and
+ * spreading;
+ * - fixed proportions for everything else (head, mandible, teeth, larynx, vertebrae, eyes, ears).
+ * The jaw turns the mandible, the lower teeth and the lower face (skinned, so the cheeks stretch)
+ * about the condyles; the larynx carries its cartilages and the hyoid.
+ *
+ * Body cm throughout: x forward, y up, z out of the right side of the face (towards the default
+ * camera, which looks at the side view the 2D screen shows), glottis at the origin.
+ */
+import * as THREE from "three";
+import type { HumanArtic, HumanFrame } from "../../wasm/types.ts";
+import { NEUTRAL } from "../../wasm/engine.ts";
+import type { TelemetryMessage } from "../../worklet/protocol.ts";
+import type { VoiceModel } from "../analysis.ts";
+import { V, heat, pressureLevel } from "../colors.ts";
+import { catmullRom, rotateAbout, type Vec } from "./geom.ts";
+import { chaikin, type Pt } from "./vector.ts";
+import { Beams, Poly } from "./beams3d.ts";
+import { bodyPoint, buildProfile, Gaze, jawAngle, wallX } from "./profile.ts";
+import { faceHandles, type FaceView, type ViewFlags } from "./anatomy.ts";
+
+type Table = readonly (readonly [number, number])[];
+type V3 = [number, number, number];
+
+const smooth = (a: number, b: number, x: number): number => {
+ const t = Math.max(0, Math.min(1, (x - a) / (b - a)));
+ return t * t * (3 - 2 * t);
+};
+const clamp01 = (x: number) => Math.max(0, Math.min(1, x));
+
+function interp(tab: Table, x: number): number {
+ if (x <= tab[0][0]) return tab[0][1];
+ for (let i = 1; i < tab.length; i++) {
+ if (x <= tab[i][0]) {
+ const [x0, y0] = tab[i - 1], [x1, y1] = tab[i];
+ return y0 + ((y1 - y0) * (x - x0)) / (x1 - x0);
+ }
+ }
+ return tab[tab.length - 1][1];
+}
+
+// ------------------------------------------------------------------ proportions
+// template cm (the adult male, before the body's scale), by template height unless noted
+
+/** half-width of the head and neck */
+const WIDTH: Table = [[-4.6, 5.3], [0, 5.4], [2.5, 5.5], [4.5, 5.25], [6.5, 5.5], [8.5, 6.1], [10.5, 6.6], [12.5, 7.0], [15, 7.3], [17.5, 7.65], [19.5, 7.4], [21, 6.5], [22.2, 4.6], [23.0, 1.0]];
+/** the face's surface beside the midline features (chin, lips, nose), as body x */
+const PLANE: Table = [[3.4, 5.9], [4.6, 6.2], [5.7, 6.15], [6.5, 6.2], [8.0, 6.35], [9.5, 6.35], [10.6, 6.55], [11.6, 6.75], [12.6, 6.9], [13.9, 7.15], [15.0, 7.45], [16.1, 7.95]];
+/** half-width of the midline feature: the chin, then the nose */
+const FEATURE: Table = [[3.4, 2.0], [4.6, 2.2], [5.7, 2.4], [6.5, 2.6], [10.6, 1.7], [11.0, 1.75], [11.65, 1.2], [12.6, 0.95], [13.9, 0.75], [15.0, 0.9], [16.1, 1.6]];
+/** where the head is widest, as a fraction of the way from the back to the face */
+const WIDEST: Table = [[-4.6, 0.5], [3.0, 0.5], [6.0, 0.42], [12, 0.42], [16, 0.45], [23, 0.45]];
+/** the jaw's lower border in the side view (x → y): the skin below it is the neck's */
+const JAW_BORDER: Table = [[-3, 8.5], [-1.6, 8.0], [-0.6, 6.4], [1.2, 4.6], [3.5, 3.6], [5.3, 3.0], [7.3, 3.3], [8.6, 4.4]];
+/** the back edge of the ramus (y → x): the skin behind it stays with the skull */
+const RAMUS_BACK: Table = [[6.0, -0.6], [7.4, -0.8], [9.6, -1.9], [10.6, -2.5], [12, -2.6]];
+const BRAIN_WIDTH: Table = [[12.3, 4.0], [13.0, 5.6], [14.5, 6.4], [16.5, 6.9], [18.5, 6.6], [20.0, 5.4], [21.0, 3.6], [21.45, 0.5]];
+/** the tongue's half-width and how far its sides fall below the midline, root (0) to tip (1) */
+const TONGUE_W: Table = [[0, 1.5], [0.15, 1.9], [0.35, 2.35], [0.6, 2.45], [0.8, 2.2], [0.92, 1.5], [1, 0.55]];
+const TONGUE_H: Table = [[0, 1.3], [0.35, 1.1], [0.7, 0.9], [0.9, 0.55], [1, 0.25]];
+/** teeth from the midline back, upper and lower: crown width along the arch, height, thickness */
+const UPPER_TEETH = [[0.85, 1.05, 0.7], [0.68, 0.95, 0.65], [0.78, 1.1, 0.8], [0.7, 0.85, 0.9], [0.68, 0.8, 0.9], [1.0, 0.75, 1.1], [0.92, 0.7, 1.05]] as const;
+const LOWER_TEETH = [[0.54, 0.9, 0.6], [0.6, 0.92, 0.62], [0.7, 1.0, 0.75], [0.7, 0.85, 0.8], [0.72, 0.8, 0.85], [1.1, 0.72, 1.05], [1.05, 0.7, 1.0]] as const;
+/** the lips' patch reaches this far to each side (template cm), past the widest mouth */
+const LIP_HALF = 3.0;
+/** superellipse exponent of the back half of a cross-section */
+const PB = 2.2;
+
+/** a head (or brain) cross-section at one height: front and back on the midline, the bare front
+ * beside the midline feature, half-width, where it is widest, the feature's size and width, and
+ * the superellipse exponent of the front half (the back half uses PB) */
+interface Section {
+ F: number;
+ Bk: number;
+ Fs: number;
+ W: number;
+ xw: number;
+ E: number;
+ wf: number;
+ pf: number;
+}
+
+const bump = (u: number) => (Math.abs(u) < 1 ? (1 - u * u) ** 2 : 0);
+
+/** x of the front surface at lateral z (without the midline feature when bare) */
+function frontX(s: Section, z: number, bare = false): number {
+ const u = Math.min(1, Math.abs(z) / s.W);
+ const c = Math.pow(1 - Math.pow(u, s.pf), 1 / s.pf);
+ return s.xw + (s.Fs - s.xw) * c + (bare ? 0 : s.E * bump(z / s.wf));
+}
+
+/** half-width of the surface at x (for things on the side of the head) */
+function sideZ(s: Section, x: number): number {
+ const front = x > s.xw;
+ const c = front ? (x - s.xw) / Math.max(1e-6, s.Fs - s.xw) : (s.xw - x) / Math.max(1e-6, s.xw - s.Bk);
+ const p = front ? s.pf : PB;
+ return s.W * Math.pow(Math.max(0, 1 - Math.pow(Math.min(1, c), p)), 1 / p);
+}
+
+/** one side of a cross-section, from the back (z = 0) round to the front (z = 0), as x, z pairs */
+function halfRing(s: Section, zf: number, out: number[]): void {
+ out.length = 0;
+ const NB = 12, NF = 10, NP = 16;
+ for (let i = 0; i < NB; i++) {
+ const th = Math.PI - (i / NB) * (Math.PI / 2);
+ out.push(s.xw - (s.xw - s.Bk) * Math.pow(Math.abs(Math.cos(th)), 2 / PB), s.W * Math.pow(Math.sin(th), 2 / PB));
+ }
+ const thf = Math.asin(Math.min(1, Math.pow(zf / s.W, s.pf / 2)));
+ for (let i = 0; i < NF; i++) {
+ const th = Math.PI / 2 - (i / NF) * (Math.PI / 2 - thf);
+ const z = s.W * Math.pow(Math.sin(th), 2 / s.pf);
+ out.push(s.xw + (s.Fs - s.xw) * Math.pow(Math.cos(th), 2 / s.pf) + s.E * bump(z / s.wf), z);
+ }
+ for (let i = 0; i <= NP; i++) {
+ const z = zf * (1 - i / NP);
+ out.push(frontX(s, z), z);
+ }
+}
+
+/** A closed outline's cross-sections by height, tabulated (rest shapes are built once per body). */
+class Shape {
+ private readonly rows: (Section | null)[] = [];
+ readonly y0: number;
+ readonly y1: number;
+
+ constructor(outline: Vec[], private readonly dy: number, make: (y: number, F: number, Bk: number) => Section) {
+ let lo = Infinity, hi = -Infinity;
+ for (const p of outline) {
+ lo = Math.min(lo, p.y);
+ hi = Math.max(hi, p.y);
+ }
+ this.y0 = lo;
+ this.y1 = hi;
+ for (let y = lo; y <= hi; y += dy) {
+ let F = -Infinity, Bk = Infinity;
+ for (let i = 0; i < outline.length - 1; i++) {
+ const a = outline[i], b = outline[i + 1];
+ if (a.y > y === b.y > y) continue;
+ const x = a.x + ((b.x - a.x) * (y - a.y)) / (b.y - a.y);
+ F = Math.max(F, x);
+ Bk = Math.min(Bk, x);
+ }
+ this.rows.push(F > Bk ? make(y, F, Bk) : null);
+ }
+ }
+
+ at(y: number): Section | null {
+ const u = (y - this.y0) / this.dy;
+ const i = Math.floor(u);
+ if (i < 0 || i >= this.rows.length - 1) return null;
+ const a = this.rows[i], b = this.rows[i + 1];
+ if (!a || !b) return a ?? b;
+ const t = u - i, l = (p: number, q: number) => p + (q - p) * t;
+ return { F: l(a.F, b.F), Bk: l(a.Bk, b.Bk), Fs: l(a.Fs, b.Fs), W: l(a.W, b.W), xw: l(a.xw, b.xw), E: l(a.E, b.E), wf: l(a.wf, b.wf), pf: l(a.pf, b.pf) };
+ }
+}
+
+/** polylines in flat xyz arrays */
+class Lines {
+ readonly lines: Float32Array[] = [];
+ private cur: number[] = [];
+ p(x: number, y: number, z: number): this {
+ this.cur.push(x, y, z);
+ return this;
+ }
+ end(close = false): void {
+ if (close && this.cur.length >= 3) this.cur.push(this.cur[0], this.cur[1], this.cur[2]);
+ if (this.cur.length >= 6) this.lines.push(new Float32Array(this.cur));
+ this.cur = [];
+ }
+ /** an ellipse c + u·cos + v·sin from angle a0 to a1 */
+ ellipse(c: V3, u: V3, v: V3, n = 24, a0 = 0, a1 = Math.PI * 2): void {
+ for (let i = 0; i <= n; i++) {
+ const an = a0 + ((a1 - a0) * i) / n, cs = Math.cos(an), sn = Math.sin(an);
+ this.p(c[0] + u[0] * cs + v[0] * sn, c[1] + u[1] * cs + v[1] * sn, c[2] + u[2] * cs + v[2] * sn);
+ }
+ this.end();
+ }
+}
+
+/** static lines in one style */
+interface Group {
+ lines: Float32Array[];
+ color: string;
+ width: number;
+ alpha: number;
+ hot?: boolean;
+}
+
+/** a polyline whose points the jaw carries by weight w (the lower face's skin) */
+interface Skinned {
+ a: Float32Array;
+ w: Float32Array;
+}
+
+/** everything that only depends on the body, in the rest pose (jaw closed) */
+interface Rest {
+ model: VoiceModel;
+ head: Shape;
+ condyle: Vec;
+ /** the lips' patch reaches this far to each side (the shell leaves a hole for it) */
+ lipHalf: number;
+ center: THREE.Vector3;
+ shell: Skinned[];
+ fixed: Group[];
+ /** carried by the jaw */
+ jaw: Group[];
+ /** the chin's midline outline (rest pose): part of the midsagittal section */
+ chin: Float32Array;
+ eyes: V3[];
+ eyeR: number;
+}
+
+/** how much of the jaw's rotation a point of the face's skin follows (rest pose, body cm) */
+function jawWeight(k: number, ph: number, x: number, y: number): number {
+ const X = x / k, Yb = y / k;
+ const Y = Yb >= 7 ? Yb : 7 - (7 - Yb) / ph;
+ const wv = 1 - smooth(7.6, 9.1, Y); // the cheek above the mouth's corners stays with the skull
+ const yb = interp(JAW_BORDER, X);
+ const wl = smooth(yb - 1.5, yb + 0.15, Y); // the neck below the jaw stays
+ const xr = interp(RAMUS_BACK, Y);
+ const wb = smooth(xr - 0.7, xr + 0.9, X); // and behind the ramus, the ear and the back of the neck
+ return wv * wl * wb;
+}
+
+/** split a polyline (flat xyz) into the runs whose points are kept */
+function keptRuns(pts: number[], keep: boolean[], closed: boolean): number[][] {
+ const n = keep.length;
+ if (keep.every(Boolean)) return [closed ? [...pts, pts[0], pts[1], pts[2]] : pts];
+ const out: number[][] = [];
+ // a closed loop starts just after a dropped point, so no run wraps round the seam
+ const start = closed ? keep.indexOf(false) + 1 : 0;
+ let run: number[] = [];
+ for (let j = 0; j < n; j++) {
+ const i = (start + j) % n;
+ if (keep[i]) run.push(pts[3 * i], pts[3 * i + 1], pts[3 * i + 2]);
+ else {
+ if (run.length >= 6) out.push(run);
+ run = [];
+ }
+ }
+ if (run.length >= 6) out.push(run);
+ return out;
+}
+
+/** a polyline resampled to n points evenly along its length */
+function resample(p: Vec[], n: number): Vec[] {
+ const cum = [0];
+ for (let i = 1; i < p.length; i++) cum.push(cum[i - 1] + Math.hypot(p[i].x - p[i - 1].x, p[i].y - p[i - 1].y));
+ const L = cum[cum.length - 1];
+ const out: Vec[] = [];
+ let j = 0;
+ for (let i = 0; i < n; i++) {
+ const s = (L * i) / (n - 1);
+ while (j < p.length - 2 && cum[j + 1] < s) j++;
+ const t = (s - cum[j]) / Math.max(1e-9, cum[j + 1] - cum[j]);
+ out.push({ x: p[j].x + (p[j + 1].x - p[j].x) * t, y: p[j].y + (p[j + 1].y - p[j].y) * t });
+ }
+ return out;
+}
+
+/** the point at fraction t of a polyline's length */
+function along(p: Vec[], cum: number[], t: number): Vec {
+ const s = t * cum[cum.length - 1];
+ let j = 0;
+ while (j < p.length - 2 && cum[j + 1] < s) j++;
+ const u = (s - cum[j]) / Math.max(1e-9, cum[j + 1] - cum[j]);
+ return { x: p[j].x + (p[j + 1].x - p[j].x) * u, y: p[j].y + (p[j + 1].y - p[j].y) * u };
+}
+const cumulative = (p: Vec[]): number[] => {
+ const c = [0];
+ for (let i = 1; i < p.length; i++) c.push(c[i - 1] + Math.hypot(p[i].x - p[i - 1].x, p[i].y - p[i - 1].y));
+ return c;
+};
+
+// ------------------------------------------------------------------ the rest pose
+
+function buildRest(model: VoiceModel): Rest {
+ const t = model.tract;
+ const k = t.scale;
+ const ph = model.bodyParams.pharynx;
+ const B = bodyPoint(model);
+ const By = (Y: number) => B(0, Y).y;
+ const unBy = (y: number) => (y / k >= 7 ? y / k : 7 - (7 - y / k) / ph);
+ const P = (x: number, y: number) => t.P(x, y);
+ // jaw closed, lips together
+ const artic: HumanArtic = { ...NEUTRAL, jaw: 0, lipAp: 0, labiodental: 0 };
+ const frame = model.frame(artic);
+ const prof = buildProfile(model, frame, artic);
+ const skin = [...prof.skinLower, ...prof.skinUpper];
+ const head = new Shape(skin, 0.05 * k, (y, F, Bk) => {
+ const Y = unBy(y);
+ const plane = Y >= 3.4 && Y <= 16.1 ? interp(PLANE, Y) * k : Infinity;
+ const Fs = Math.min(F, plane);
+ const pf = 2.2 + 0.4 * smooth(2.5, 3.5, Y) * (1 - smooth(17, 18.5, Y));
+ return { F, Bk, Fs, W: interp(WIDTH, Y) * k, xw: Bk + (Fs - Bk) * interp(WIDEST, Y), E: F - Fs, wf: interp(FEATURE, Y) * k, pf };
+ });
+ const condyle = P(-2.5, 10.5);
+ const lipTop = frame.upperLip[frame.upperLip.length - 1].y;
+ const lipBottom = frame.lowerLip[frame.lowerLip.length - 1].y;
+ const lipHalf = LIP_HALF * k;
+ const inLips = (x: number, y: number, z: number, s: Section) => y > lipBottom && y < lipTop && Math.abs(z) < lipHalf && x > s.xw;
+
+ // ---- the head's shell: rings by height, verticals over the face, meridians round the back
+ const shell: Skinned[] = [];
+ const addShell = (pts: number[], keep: boolean[], closed: boolean) => {
+ for (const run of keptRuns(pts, keep, closed)) {
+ const n = run.length / 3;
+ const w = new Float32Array(n);
+ for (let i = 0; i < n; i++) w[i] = jawWeight(k, ph, run[3 * i], run[3 * i + 1]);
+ shell.push({ a: new Float32Array(run), w });
+ }
+ };
+ const half: number[] = [];
+ const ringStep = 1.4 * k;
+ for (let y = head.y0 + 0.25 * k; y < head.y1 - 0.2 * k; y += ringStep) {
+ const s = head.at(y);
+ if (!s || s.W < 0.2 * k) continue;
+ halfRing(s, Math.min(0.6 * s.W, 3.6 * k), half);
+ const pts: number[] = [], keep: boolean[] = [];
+ const n = half.length / 2;
+ // right side from the back to the front, then the left side back again (no doubled ends)
+ for (let i = 0; i < n; i++) pts.push(half[2 * i], y, half[2 * i + 1]);
+ for (let i = n - 2; i > 0; i--) pts.push(half[2 * i], y, -half[2 * i + 1]);
+ for (let i = 0; i < pts.length / 3; i++) keep.push(!inLips(pts[3 * i], y, pts[3 * i + 2], s));
+ addShell(pts, keep, true);
+ }
+ const vstep = 0.3 * k;
+ for (const z of [-3.6, -2.4, -1.2, 1.2, 2.4, 3.6].map((v) => v * k)) {
+ let pts: number[] = [], keep: boolean[] = [];
+ for (let y = head.y0 + 0.1 * k; y <= head.y1; y += vstep) {
+ const s = head.at(y);
+ if (!s || Math.abs(z) > 0.97 * s.W) {
+ addShell(pts, keep, false);
+ pts = [];
+ keep = [];
+ continue;
+ }
+ const x = frontX(s, z);
+ pts.push(x, y, z);
+ keep.push(!inLips(x, y, z, s));
+ }
+ addShell(pts, keep, false);
+ }
+ for (const th of [0.5, 0.8].map((v) => v * Math.PI)) {
+ for (const side of [1, -1]) {
+ const pts: number[] = [], keep: boolean[] = [];
+ for (let y = head.y0 + 0.1 * k; y <= head.y1; y += vstep) {
+ const s = head.at(y);
+ if (!s || s.W < 0.2 * k) continue;
+ pts.push(s.xw - (s.xw - s.Bk) * Math.pow(Math.abs(Math.cos(th)), 2 / PB), y, side * s.W * Math.pow(Math.sin(th), 2 / PB));
+ keep.push(true);
+ }
+ addShell(pts, keep, false);
+ }
+ }
+
+ const fixed: Group[] = [];
+ const jaw: Group[] = [];
+ const group = (into: Group[], color: string, width: number, alpha: number, fill: (L: Lines) => void, hot = false) => {
+ const L = new Lines();
+ fill(L);
+ into.push({ lines: L.lines, color, width, alpha, hot });
+ };
+ /** a template outline on the side of the head, standing out from it by `off` (body cm) */
+ const onSide = (L: Lines, pts: [number, number][], off: (i: number, n: number) => number, side: number) => {
+ const curve = catmullRom(pts.map(([x, y]) => B(x, y)), 4);
+ curve.forEach((p, i) => {
+ const s = head.at(p.y);
+ L.p(p.x, p.y, side * ((s ? sideZ(s, p.x) : 0) + off(i, curve.length)));
+ });
+ L.end();
+ };
+
+ // ---- ears
+ group(fixed, V.blueDim, 1.05, 0.9, (L) => {
+ for (const side of [1, -1]) {
+ onSide(L, [[-2.55, 13.4], [-2.85, 14.75], [-3.65, 15.45], [-4.6, 15.25], [-5.2, 14.25], [-5.4, 12.8], [-5.05, 11.3], [-4.55, 10.2], [-3.95, 9.55], [-3.35, 9.65], [-3.0, 10.35]], (i, n) => (0.25 + 1.2 * Math.sin((Math.PI * i) / (n - 1)) ** 1.5) * k, side);
+ onSide(L, [[-3.15, 14.3], [-3.95, 14.45], [-4.55, 13.5], [-4.6, 12.1], [-4.15, 11.05]], () => 0.55 * k, side);
+ onSide(L, [[-2.7, 11.2], [-2.55, 11.85], [-2.75, 12.35]], () => 0.35 * k, side);
+ const concha: [number, number][] = [];
+ for (let i = 0; i <= 16; i++) concha.push([-3.5 + 0.6 * Math.cos((i / 16) * Math.PI * 2), 12.0 + 0.85 * Math.sin((i / 16) * Math.PI * 2)]);
+ onSide(L, concha, () => 0.12 * k, side);
+ }
+ });
+ // ---- the nose's sides and wings, so it reads from the front
+ group(fixed, V.blueMid, 1.0, 0.8, (L) => {
+ for (const side of [1, -1]) {
+ for (let Y = 14.6; Y >= 11.3; Y -= 0.15) {
+ const y = By(Y), s = head.at(y);
+ if (!s) continue;
+ const z = side * (0.3 + (1.05 * (14.6 - Y)) / 3.3) * k;
+ L.p(frontX(s, z), y, z);
+ }
+ L.end();
+ const wing: [number, number, number][] = [[7.95, 11.05, 0.95], [7.4, 11.15, 1.45], [7.25, 11.6, 1.55], [7.55, 12.1, 1.35], [8.2, 12.15, 1.0]];
+ for (const [x, Y, z] of wing) L.p(x * k, By(Y), side * z * k);
+ L.end();
+ }
+ });
+ // ---- nostrils, under the tip of the nose
+ group(fixed, V.blueMid, 1.0, 0.9, (L) => {
+ for (const side of [1, -1]) L.ellipse([8.05 * k, By(10.98), side * 0.62 * k], [0.42 * k, 0.04 * k, 0], [0, 0, 0.26 * k], 16);
+ });
+
+ // ---- brain: rings and meridians over the 2D outline, the skull and its base stay on the midline
+ const brain = new Shape(prof.brain, 0.05 * k, (y, F, Bk) => ({ F, Bk, Fs: F, W: interp(BRAIN_WIDTH, unBy(y)) * k, xw: Bk + (F - Bk) * 0.48, E: 0, wf: 1, pf: 2.3 }));
+ group(fixed, V.blueMid, 0.85, 0.32, (L) => {
+ for (let y = brain.y0 + 0.6 * k; y < brain.y1 - 0.15 * k; y += 1.5 * k) {
+ const s = brain.at(y);
+ if (!s) continue;
+ for (let i = 0; i <= 48; i++) {
+ const th = (i / 48) * Math.PI * 2, c = Math.cos(th), sn = Math.sin(th);
+ const p = c >= 0 ? s.pf : PB;
+ const x = c >= 0 ? s.xw + (s.Fs - s.xw) * Math.pow(c, 2 / p) : s.xw - (s.xw - s.Bk) * Math.pow(-c, 2 / p);
+ L.p(x, y, Math.sign(sn) * s.W * Math.pow(Math.abs(sn), 2 / p));
+ }
+ L.end();
+ }
+ for (const th of [0.3, 0.7].map((v) => v * Math.PI)) {
+ for (const side of [1, -1]) {
+ for (let y = brain.y0; y <= brain.y1; y += 0.3 * k) {
+ const s = brain.at(y);
+ if (!s) continue;
+ const c = Math.cos(th), sn = Math.sin(th);
+ const p = c >= 0 ? s.pf : PB;
+ const x = c >= 0 ? s.xw + (s.Fs - s.xw) * Math.pow(c, 2 / p) : s.xw - (s.xw - s.Bk) * Math.pow(-c, 2 / p);
+ L.p(x, y, side * s.W * Math.pow(sn, 2 / p));
+ }
+ L.end();
+ }
+ }
+ });
+
+ // ---- cervical vertebrae: bodies, arches, processes; the spinal cord through them
+ group(fixed, V.blueDim, 0.9, 0.6, (L) => {
+ for (let i = 0; i < 9; i++) {
+ const y0 = -4.4 + i * 1.75;
+ if (y0 > 10.5) break;
+ const x0 = wallX(y0) - 2.25;
+ const lo = P(x0, y0 + 0.08), hi = P(x0, y0 + 1.32);
+ const cx = P(x0 + 0.8, 0).x, rx = 0.8 * k, rz = 1.05 * k;
+ for (const y of [lo.y, hi.y]) L.ellipse([cx, y, 0], [rx, 0, 0], [0, 0, rz], 24);
+ for (const a of [0.25, 0.75, 1.25, 1.75].map((v) => v * Math.PI)) L.p(cx + rx * Math.cos(a), lo.y, rz * Math.sin(a)).p(cx + rx * Math.cos(a), hi.y, rz * Math.sin(a)).end();
+ const ym = (lo.y + hi.y) / 2, ax = cx - 2.15 * k;
+ L.ellipse([ax, ym, 0], [0.72 * k, 0, 0], [0, 0, 0.88 * k], 20);
+ for (const s of [1, -1]) {
+ L.p(cx - 0.7 * k, ym, s * 0.55 * k).p(ax + 0.45 * k, ym, s * 0.72 * k).end();
+ L.p(cx - 0.5 * k, ym, s * 0.95 * k).p(cx - 0.9 * k, ym - 0.1 * k, s * 2.3 * k).end();
+ }
+ L.p(ax - 0.72 * k, ym, 0).p(ax - 1.8 * k, ym - 0.55 * k, 0).end();
+ }
+ });
+ group(fixed, V.blueDim, 0.85, 0.5, (L) => {
+ const cx = P(-4.9, 0).x;
+ for (let y = -4.4; y <= 11.0; y += 1.75) L.ellipse([cx, P(0, y).y, 0], [0.42 * k, 0, 0], [0, 0, 0.6 * k], 16);
+ for (const s of [1, -1]) L.p(cx, P(0, -4.6).y, s * 0.6 * k).p(cx, P(0, 11.4).y, s * 0.6 * k).end();
+ });
+
+ // ---- nasal cavity: the septum is the 2D outline; its side walls and the turbinates beside it
+ group(fixed, V.blueDim, 0.9, 0.7, (L) => {
+ for (const s of [1, -1]) {
+ for (const p of prof.nasal) L.p(p.x, p.y, s * 1.2 * k);
+ L.end(true);
+ for (const tb of prof.turbinates) {
+ for (const p of tb) L.p(p.x, p.y, s * 0.95 * k);
+ L.end();
+ }
+ }
+ });
+
+ // ---- upper teeth on their arch, the palate's vault between them
+ const arch = (front: number, depth: number, halfW: number) => {
+ // x(z) = front − depth·(|z|/halfW)^1.85, sampled along its length
+ const pts: Vec[] = [];
+ for (let i = 0; i <= 60; i++) {
+ const z = (halfW * i) / 60;
+ pts.push({ x: front - depth * Math.pow(z / halfW, 1.85), y: z });
+ }
+ return { pts, cum: cumulative(pts) };
+ };
+ const teeth = (L: Lines, front: number, depth: number, halfW: number, spec: readonly (readonly [number, number, number])[], occ: (u: number) => number, dir: 1 | -1) => {
+ const a = arch(front, depth, halfW);
+ const len = a.cum[a.cum.length - 1];
+ const total = spec.reduce((s, q) => s + q[0], 0);
+ const scale = (0.97 * len) / (total * k);
+ for (const side of [1, -1]) {
+ let s0 = 0;
+ for (const [w0, h0, th0] of spec) {
+ const w = w0 * k * scale, h = h0 * k, th = th0 * k;
+ const tc = (s0 + w / 2) / len;
+ s0 += w;
+ const c = along(a.pts, a.cum, tc);
+ const c1 = along(a.pts, a.cum, Math.min(1, tc + 0.01)), c0 = along(a.pts, a.cum, Math.max(0, tc - 0.01));
+ // tangent along the arch (x, z) and the outward normal
+ let tx = c1.x - c0.x, tz = c1.y - c0.y;
+ const tl = Math.hypot(tx, tz) || 1;
+ tx /= tl;
+ tz /= tl;
+ const nx = tz, nz = -tx; // outward (front at the midline)
+ const y0 = occ(c.y / halfW);
+ const P3 = (u: number, v: number, inward = 0): V3 => [c.x + tx * u - nx * inward, y0 + dir * v, side * (c.y + tz * u - nz * inward)];
+ // the face towards the lips and cheeks: flat biting edge, narrowing to the gum
+ for (const [u, v] of [[-0.5, 0], [-0.5, 0.55], [-0.38, 1], [0, 1.08], [0.38, 1], [0.5, 0.55], [0.5, 0]] as const) L.p(...P3(u * w, v * h));
+ L.end(true);
+ // the biting surface
+ for (const [u, v] of [[-0.45, 0.1], [0.45, 0.1], [0.45, 0.9], [-0.45, 0.9]] as const) L.p(...P3(u * w, 0, v * th));
+ L.end(true);
+ }
+ }
+ };
+ const upFront = P(7.3, 0).x, upDepth = 4.2 * k, upHalf = 2.65 * k;
+ group(fixed, V.blue, 1.0, 0.8, (L) => teeth(L, upFront, upDepth, upHalf, UPPER_TEETH, (u) => (7.95 + 0.3 * u * u) * k, 1));
+ group(fixed, V.blueMid, 1.0, 0.6, (L) => {
+ for (const i of [0, 2, 4, 5, 6, 7]) {
+ const m = prof.palOral[i];
+ // the arch's half-width at this depth, inside the teeth
+ const u = Math.max(0, (upFront - m.x) / upDepth);
+ const za = (u >= 1 ? upHalf : upHalf * Math.pow(u, 1 / 1.85)) - 0.45 * k;
+ const ya = (8.95 + 0.3 * Math.min(1, za / upHalf) ** 2) * k;
+ if (za < 0.6 * k || m.y < ya + 0.15 * k) continue;
+ for (let j = 0; j <= 20; j++) {
+ const z = za * (j / 10 - 1);
+ L.p(m.x, ya + (m.y - ya) * Math.pow(1 - Math.pow(Math.abs(z) / za, 2.4), 0.7), z);
+ }
+ L.end();
+ }
+ });
+
+ // ---- the mandible (rest pose; the jaw turns it about the condyles)
+ const M = (x: number, y: number, z: number): V3 => {
+ const p = P(x, y);
+ return [p.x, p.y, z * k];
+ };
+ /** a smooth curve through mandible landmarks (template x, y and z) */
+ const mline = (L: Lines, pts: [number, number, number][], side: number) => {
+ const q = pts.map(([x, y, z]) => M(x, y, side * z));
+ for (let i = 0; i < q.length - 1; i++) {
+ const p0 = q[Math.max(0, i - 1)], p1 = q[i], p2 = q[i + 1], p3 = q[Math.min(q.length - 1, i + 2)];
+ for (let j = 0; j < 4; j++) {
+ const t = j / 4, t2 = t * t, t3 = t2 * t;
+ const c = (a: number, b: number, cc: number, d: number) => 0.5 * (2 * b + (-a + cc) * t + (2 * a - 5 * b + 4 * cc - d) * t2 + (-a + 3 * b - 3 * cc + d) * t3);
+ L.p(c(p0[0], p1[0], p2[0], p3[0]), c(p0[1], p1[1], p2[1], p3[1]), c(p0[2], p1[2], p2[2], p3[2]));
+ }
+ }
+ L.p(...q[q.length - 1]);
+ L.end();
+ };
+ group(jaw, V.red, 1.25, 0.9, (L) => {
+ for (const side of [1, -1]) {
+ // lower border, then the ramus up to the condyle
+ mline(L, [[6.4, 4.6, 0], [5.6, 4.45, 1.5], [4.5, 4.4, 2.6], [3.2, 4.5, 3.5], [1.8, 4.85, 4.15], [0.4, 5.6, 4.6], [-0.55, 6.6, 4.85], [-0.9, 7.6, 4.9], [-1.45, 8.9, 4.95], [-1.9, 9.7, 5.0], [-2.15, 10.2, 5.0]], side);
+ // condyle neck forward, the notch, the coronoid and the ramus' front edge down to the teeth
+ mline(L, [[-1.8, 10.25, 4.95], [-0.9, 9.4, 4.85], [0.15, 10.1, 4.6], [0.6, 8.9, 4.35], [1.1, 7.7, 4.0], [1.9, 7.2, 3.55]], side);
+ const c = M(-2.5, 10.5, side * 4.95);
+ L.ellipse(c, [0.45 * k, 0, 0], [0, 0.12 * k, 0.85 * k], 16);
+ }
+ });
+ group(jaw, V.red, 1.0, 0.55, (L) => {
+ for (const side of [1, -1]) {
+ // the alveolar border along the lower teeth, and the body's height at a few places
+ mline(L, [[1.9, 7.2, 3.55], [3.0, 7.15, 3.15], [4.3, 7.1, 2.6], [5.4, 7.05, 1.85], [6.2, 7.05, 1.05], [6.72, 7.15, 0]], side);
+ for (const [xa, za, xb, yb, zb] of [[3.0, 3.15, 3.2, 4.5, 3.5], [5.4, 1.85, 5.6, 4.45, 1.5], [1.9, 3.55, 1.8, 4.85, 4.15]]) mline(L, [[xa, 7.15, za], [(xa + xb) / 2, 5.9, (za + zb) / 2 + 0.15], [xb, yb, zb]], side);
+ }
+ });
+ // the chin on the midline: the 2D view's jaw outline
+ const chinL = new Lines();
+ mline(chinL, [[7.25, 6.75, 0], [7.4, 5.4, 0], [6.4, 4.6, 0], [5.6, 5.0, 0], [5.9, 6.2, 0], [6.45, 6.55, 0]], 1);
+ const chin = chinL.lines[0];
+ const loFront = P(6.98, 0).x, loDepth = 4.0 * k, loHalf = 2.5 * k;
+ group(jaw, V.blue, 1.0, 0.8, (L) => teeth(L, loFront, loDepth, loHalf, LOWER_TEETH, (u) => (7.75 + 0.45 * Math.pow(u, 1.5)) * k, -1));
+
+ // eyes: the 2D view draws one on the midline; here they sit either side of the nose
+ const eyeC = B(6.15, 15.35);
+ const center = new THREE.Vector3(0.3 * k, (head.y0 + head.y1) / 2, 0);
+ return { model, head, condyle, lipHalf, center, shell, fixed, jaw, chin, eyes: [[eyeC.x, eyeC.y, 3.15 * k], [eyeC.x, eyeC.y, -3.15 * k]], eyeR: 1.05 * k };
+}
+
+// ------------------------------------------------------------------ the renderer
+
+interface Particle {
+ s: number;
+ u: number;
+ v: number;
+ life: number;
+ px: number;
+ py: number;
+ pz: number;
+}
+
+/** what the tongue, jaw and lips looked like a moment ago */
+interface Echo {
+ t: number;
+ lines: { a: Float32Array; color: string }[];
+}
+const ECHO_AGES = [0.09, 0.06, 0.03];
+
+export class Anatomy3D {
+ readonly scene = new THREE.Scene();
+ readonly camera = new THREE.PerspectiveCamera(24, 1, 5, 2000);
+ private readonly beams = new Beams();
+ private rest: Rest | null = null;
+ private w = 1;
+ private h = 1;
+ /** a narrow screen: readouts above and below the head (as the 2D view) */
+ compact = false;
+ private clock = 0;
+ private readonly gaze = new Gaze();
+ private strobe = 0;
+ private readonly tmp = new Poly();
+ private readonly particles: Particle[] = [];
+ private echoes: Echo[] = [];
+ private lastEcho = -1;
+ private sparkSeed = 7;
+ /** the body's scale this frame */
+ private k = 1;
+ /** the airway's cross-sections this frame: centre, the grid line's direction, half-gap, half-width */
+ private ring = { n: 0, s: new Float64Array(80), cx: new Float64Array(80), cy: new Float64Array(80), ux: new Float64Array(80), uy: new Float64Array(80), a: new Float64Array(80), b: new Float64Array(80) };
+ private readonly v3 = new THREE.Vector3();
+ /** the narrowest constriction this frame (for the measurement label), body cm */
+ caliper: { x: number; y: number; z: number; area: number } | null = null;
+
+ constructor() {
+ this.scene.add(this.beams.group);
+ this.scene.fog = new THREE.Fog(0x000000, 50, 100);
+ for (let i = 0; i < 200; i++) this.particles.push({ s: Math.random() * 17, u: Math.random() * 2 - 1, v: Math.random() * 2 - 1, life: Math.random(), px: NaN, py: NaN, pz: NaN });
+ }
+
+ dispose(): void {
+ this.beams.dispose();
+ }
+
+ resize(w: number, h: number): void {
+ this.w = w;
+ this.h = h;
+ this.compact = w < 560 || w / h < 0.95;
+ this.camera.aspect = w / h;
+ // on a wide screen the head sits left of centre, leaving the right for the readouts
+ if (this.compact) this.camera.clearViewOffset();
+ else this.camera.setViewOffset(w, h, 0.1 * w, 0, w, h);
+ this.camera.updateProjectionMatrix();
+ this.beams.setResolution(w, h);
+ for (const p of this.particles) p.px = NaN;
+ }
+
+ /** the head's centre and camera positions for this body: the turned "home" view and the 2D view's side */
+ poses(model: VoiceModel): { target: THREE.Vector3; home: THREE.Vector3; side: THREE.Vector3 } {
+ const r = this.restFor(model);
+ const half = Math.tan((this.camera.fov * Math.PI) / 360);
+ // fit the head's height (and its width in a narrow screen) at a male's size, so bodies compare
+ const d = 1.1 * Math.max(15.2 / half, 11 / (half * Math.max(0.3, this.camera.aspect)));
+ const target = r.center.clone();
+ const az = 0.62, el = 0.2;
+ return {
+ target,
+ home: target.clone().add(new THREE.Vector3(Math.sin(az) * Math.cos(el), Math.sin(el), Math.cos(az) * Math.cos(el)).multiplyScalar(d)),
+ side: target.clone().add(new THREE.Vector3(0, 0, d)),
+ };
+ }
+
+ private restFor(model: VoiceModel): Rest {
+ if (!this.rest || this.rest.model !== model) this.rest = buildRest(model);
+ return this.rest;
+ }
+
+ /** body point → CSS px on the canvas */
+ project(x: number, y: number, z: number): [number, number] {
+ const v = this.v3.set(x, y, z).project(this.camera);
+ return [((v.x + 1) / 2) * this.w, ((1 - v.y) / 2) * this.h];
+ }
+
+ /** CSS px per cm at a body point, across the screen */
+ pxPerCmAt(x: number, y: number, z: number): number {
+ const e = this.camera.matrixWorld.elements;
+ const [x0, y0] = this.project(x, y, z);
+ const [x1, y1] = this.project(x + e[0], y + e[1], z + e[2]);
+ return Math.hypot(x1 - x0, y1 - y0);
+ }
+
+ /** what the words at the mouth (and other overlays) need of this view */
+ faceView(model: VoiceModel, f: HumanFrame, a: HumanArtic): FaceView {
+ const lips = faceHandles(model, f, a).find((h) => h.id === "lips")!.pos;
+ return {
+ handles: faceHandles,
+ X: (p) => this.project(p.x, p.y, 0)[0],
+ Y: (p) => this.project(p.x, p.y, 0)[1],
+ pxPerCm: this.pxPerCmAt(lips.x, lips.y, 0),
+ };
+ }
+
+ // ------------------------------------------------------------------ one frame
+ update(model: VoiceModel, f: HumanFrame, a: HumanArtic, tele: TelemetryMessage | null, view: ViewFlags, dt: number): void {
+ this.clock += dt;
+ const rest = this.restFor(model);
+ const k = model.tract.scale;
+ const phi = jawAngle(model, a.jaw);
+ const C = rest.condyle;
+ const B = this.beams;
+ // dim with depth: full brightness up to just in front of the head's middle
+ const dist = this.camera.position.distanceTo(rest.center);
+ const fog = this.scene.fog as THREE.Fog;
+ fog.near = dist - 3 * k;
+ fog.far = dist + 30 * k;
+
+ B.begin();
+ const flat = (pts: Vec[], z = 0): Float32Array => {
+ const o = new Float32Array(pts.length * 3);
+ pts.forEach((p, i) => {
+ o[3 * i] = p.x;
+ o[3 * i + 1] = p.y;
+ o[3 * i + 2] = z;
+ });
+ return o;
+ };
+ // The midsagittal section is the 2D view's drawing: full on from the side, but seen edge-on (from
+ // the front, back or above) its lines pile into one bright bar down the middle, so it fades as
+ // the head turns and the 3D shapes take over; the articulators' outlines fade less.
+ const toCam = this.v3.copy(this.camera.position).sub(rest.center).normalize();
+ const side = smooth(0.25, 0.8, Math.abs(toCam.z));
+ const cut = 0.22 + 0.78 * side, artCut = 0.45 + 0.55 * side;
+ const line = (pts: Vec[], color: string, w: number, o: { alpha?: number; hot?: boolean; close?: boolean } = {}, fade = cut) => B.line(flat(pts), pts.length, color, w, { ...o, alpha: (o.alpha ?? 1) * fade });
+ const rot = (x: number, y: number, ang: number): [number, number] => (ang === 0 ? [x, y] : rotXY(x, y, C, ang));
+ this.k = k;
+
+ // ---- the fixed and jaw-carried structures
+ for (const g of rest.fixed) for (const l of g.lines) B.line(l, l.length / 3, g.color, g.width, { alpha: g.alpha, hot: g.hot });
+ for (const g of rest.jaw) {
+ for (const l of g.lines) {
+ const P = this.tmp.reset();
+ for (let i = 0; i < l.length; i += 3) {
+ const [x, y] = rot(l[i], l[i + 1], phi);
+ P.p(x, y, l[i + 2]);
+ }
+ B.poly(P, g.color, g.width, { alpha: g.alpha, hot: g.hot });
+ }
+ }
+ for (const sk of rest.shell) {
+ const P = this.tmp.reset();
+ for (let i = 0; i < sk.w.length; i++) {
+ const [x, y] = rot(sk.a[3 * i], sk.a[3 * i + 1], phi * sk.w[i]);
+ P.p(x, y, sk.a[3 * i + 2]);
+ }
+ B.poly(P, V.blueDim, 0.85, { alpha: 0.42 });
+ }
+
+ // ---- the midsagittal section, as the 2D view draws it
+ const G = buildProfile(model, f, a);
+ line(G.inset, V.blueDim, 0.9, { alpha: 0.5 });
+ line(G.skull, V.blueDim, 0.9, { close: true, alpha: 0.6 });
+ line(G.brain, V.blueMid, 1.0, { close: true, alpha: 0.75 });
+ line(G.sulcus, V.blueDim, 0.9);
+ line(G.nasal, V.blueMid, 1, { close: true });
+ line(G.palOral, V.blueMid, 1.1);
+ line(G.palNasal, V.blueMid, 1.1);
+ line(G.skinLower, V.blue, 1.5);
+ line(G.skinUpper, V.blue, 1.5);
+ line(G.outerWall, V.blue, 1.4, { hot: true });
+ line(f.inner, V.blue, 1.3, { alpha: 0.9 });
+ line(G.glottisWall, V.blue, 1.2, { alpha: 0.8 });
+
+ this.eyes(rest, dt);
+ this.airway(model, f, tele, view, k);
+ this.velum(f, k);
+ this.epiglottis(f, k, artCut);
+ this.larynx(model, f, a, tele, k, dt, rest.head.y0, cut);
+
+ const tongueOutline = flat(f.tongue);
+ const soft = (v: Vec[], it: number) => flat(chaikin(v.map((p) => [p.x, p.y] as Pt), it).map(([x, y]) => ({ x, y })));
+ const upperLip = soft(f.upperLip, 2), lowerLip = soft(f.lowerLip, 2);
+ const mouth = this.lips(rest, model, f, a, phi);
+ const chin = rest.chin;
+ const chinNow = new Float32Array(chin.length);
+ for (let i = 0; i < chin.length; i += 3) {
+ const [x, y] = rot(chin[i], chin[i + 1], phi);
+ chinNow[i] = x;
+ chinNow[i + 1] = y;
+ }
+
+ // afterglow: where the tongue, jaw and lips were a moment ago
+ if (tele && this.clock - this.lastEcho >= 0.028) {
+ this.lastEcho = this.clock;
+ const lines = [{ a: tongueOutline, color: V.green }, { a: chinNow, color: V.red }, { a: upperLip, color: V.pink }, { a: lowerLip, color: V.pink }];
+ if (mouth) lines.push({ a: mouth, color: V.pink });
+ this.echoes.push({ t: this.clock, lines });
+ while (this.echoes.length && this.clock - this.echoes[0].t > 0.12) this.echoes.shift();
+ }
+ if (tele) {
+ ECHO_AGES.forEach((age, i) => {
+ const e = this.nearestEcho(this.clock - age);
+ if (!e) return;
+ for (const l of e.lines) B.line(l.a, l.a.length / 3, l.color, 1, { alpha: (0.1 + 0.07 * i) * artCut });
+ });
+ }
+
+ this.tongue(f, a, k);
+ B.line(chinNow, chinNow.length / 3, V.red, 1.5, { hot: true, alpha: artCut });
+ B.line(tongueOutline, f.tongue.length, V.green, 1.6, { hot: true, alpha: artCut });
+ B.line(upperLip, upperLip.length / 3, V.pink, 1.5, { hot: true, alpha: artCut });
+ B.line(lowerLip, lowerLip.length / 3, V.pink, 1.5, { hot: true, alpha: artCut });
+ line(chaikin(f.velum.map((p) => [p.x, p.y] as Pt), 3).map(([x, y]) => ({ x, y })), V.white, 1.25, { alpha: 0.92 }, artCut);
+
+ if (view.particles && tele) this.drawParticles(model, f, tele, dt);
+ if (tele) this.drawTurbulence(model, f, tele);
+ this.caliper = null;
+ if (view.calipers) this.drawCaliper(f);
+ B.end();
+ }
+
+ private nearestEcho(t: number): Echo | null {
+ let best: Echo | null = null, bd = 0.016;
+ for (const e of this.echoes) {
+ const d = Math.abs(e.t - t);
+ if (d < bd) {
+ bd = d;
+ best = e;
+ }
+ }
+ return best;
+ }
+
+ // ------------------------------------------------------------------ eyes
+ private eyes(rest: Rest, dt: number): void {
+ const g = this.gaze;
+ g.update(this.clock, dt);
+ const r = rest.eyeR;
+ // where they look: forward, turned by the gaze (yaw towards +z, pitch up)
+ const d: V3 = [Math.cos(g.pitch) * Math.cos(g.yaw), Math.sin(g.pitch), Math.cos(g.pitch) * Math.sin(g.yaw)];
+ const basis = (n: V3): [V3, V3] => {
+ const up: V3 = Math.abs(n[1]) > 0.9 ? [1, 0, 0] : [0, 1, 0];
+ const u = norm3(cross(up, n));
+ return [u, cross(n, u)];
+ };
+ const cam = this.camera.position;
+ for (const c of rest.eyes) {
+ // a sphere's outline is the circle facing the camera
+ const toCam = norm3([cam.x - c[0], cam.y - c[1], cam.z - c[2]]);
+ const [u, v] = basis(toCam);
+ const L = new Lines();
+ L.ellipse(c, scale3(u, r), scale3(v, r), 36);
+ const [iu, iv] = basis(d);
+ const ic: V3 = [c[0] + d[0] * r * 0.86, c[1] + d[1] * r * 0.86, c[2] + d[2] * r * 0.86];
+ L.ellipse(ic, scale3(iu, r * 0.5), scale3(iv, r * 0.5), 24);
+ const pc: V3 = [c[0] + d[0] * r * 0.97, c[1] + d[1] * r * 0.97, c[2] + d[2] * r * 0.97];
+ L.ellipse(pc, scale3(iu, r * 0.2), scale3(iv, r * 0.2), 14);
+ // the lids: an almond over the front of the eyeball, inner corner towards the nose
+ const out = Math.sign(c[2]);
+ const lid = (open: number) => {
+ for (let i = 0; i <= 16; i++) {
+ const sv = -1 + i / 8, al = out * sv * 0.95, be = open * (1 - sv * sv) + 0.06 * sv * out * Math.sign(open);
+ L.p(c[0] + 1.04 * r * Math.cos(be) * Math.cos(al), c[1] + 1.04 * r * Math.sin(be), c[2] + 1.04 * r * Math.cos(be) * Math.sin(al));
+ }
+ L.end();
+ };
+ lid(0.5);
+ lid(-0.36);
+ // the brow above it
+ for (let i = 0; i <= 12; i++) {
+ const sv = -1 + i / 6, al = out * (0.15 + sv * 0.62), be = 0.82 + 0.12 * (1 - sv * sv) - 0.06 * sv;
+ L.p(c[0] + 1.32 * r * Math.cos(be) * Math.cos(al), c[1] + 1.32 * r * Math.sin(be), c[2] + 1.32 * r * Math.cos(be) * Math.sin(al));
+ }
+ L.end();
+ const style = [[V.blueMid, 1.0, 0.35], [V.blue, 1.0, 0.9], [V.blue, 1.0, 0.7], [V.blue, 1.25, 0.95], [V.blue, 1.05, 0.75], [V.blueMid, 1.3, 0.8]] as const;
+ L.lines.forEach((l, i) => this.beams.line(l, l.length / 3, style[i][0], style[i][1], { alpha: style[i][2] }));
+ }
+ }
+
+ // ------------------------------------------------------------------ the lips
+ /**
+ * The lips as a patch over the mouth: the engine's midline outlines swept sideways. The opening's
+ * width is the engine's lip width (rounded to spread) and its height the midline gap, closing to
+ * the corners. The upper lip stays with the skull and the lower with the jaw; the corners go half
+ * way, and towards the patch's edges each point follows the face's skin. Returns the opening's
+ * outline (for the afterglow) when the lips are apart.
+ */
+ private lips(rest: Rest, model: VoiceModel, f: HumanFrame, a: HumanArtic, phi: number): Float32Array | null {
+ const k = model.tract.scale, C = rest.condyle, head = rest.head, B = this.beams;
+ const ph = model.bodyParams.pharynx;
+ const Wl = rest.lipHalf;
+ const Wm = Math.max(0.3 * k, ((1.1 + (model.bodyParams.lipWidth - 1.1) * clamp01(a.lipWidth)) * k) / 2);
+ const up = f.upperLip.slice(6); // edge → nose
+ const lo = f.lowerLip.slice(6).map((p) => rotateAbout(p, C, -phi)); // edge → chin, as if the jaw were closed
+ const cu = cumulative(up), cl = cumulative(lo);
+ const ul = up[0], ll = lo[0];
+ const yc = (ul.y + ll.y) / 2;
+ const base = (y: number, z: number): number => {
+ const s = head.at(y);
+ return s ? frontX(s, z, true) : 0;
+ };
+ const bulge0u = ul.x - base(ul.y, 0), bulge0l = ll.x - base(ll.y, 0), avg0 = (bulge0u + bulge0l) / 2;
+ const yTop = up[up.length - 1].y, yBot = lo[lo.length - 1].y;
+ const P = this.tmp;
+ /** a point of the patch: upper or lower lip, t from the edge (0) to the patch's far edge (1), at z */
+ const pt = (upper: boolean, t: number, z: number): V3 => {
+ const sz = Math.sqrt(Math.max(0, 1 - (z / Wm) ** 2));
+ const M = along(upper ? up : lo, upper ? cu : cl, t);
+ const edge = upper ? ul : ll, far = upper ? yTop : yBot;
+ const ye = yc + (edge.y - yc) * sz;
+ const span = far - edge.y;
+ const y = ye + ((M.y - edge.y) * (far - ye)) / (Math.abs(span) < 1e-4 ? 1e-4 : span);
+ const g = Math.pow(Math.max(0, 1 - (z / Wl) ** 2), 1.5);
+ const bulge = M.x - base(M.y, 0) + (avg0 - (upper ? bulge0u : bulge0l)) * (1 - sz) * (1 - t);
+ const x = base(y, z) + bulge * g;
+ const natural = upper ? 0.5 * (1 - sz) * (1 - t) : 0.5 * (1 + sz) + 0.5 * (1 - sz) * t;
+ const w = natural + (jawWeight(k, ph, x, y) - natural) * smooth(Wm, Wl, Math.abs(z));
+ const [rx, ry] = w ? rotXY(x, y, C, phi * w) : [x, y];
+ return [rx, ry, z];
+ };
+ const row = (upper: boolean, t: (z: number) => number, z0: number, z1: number, n: number): Poly => {
+ P.reset();
+ for (let i = 0; i <= n; i++) {
+ const z = z0 + ((z1 - z0) * i) / n;
+ P.p(...pt(upper, t(z), z));
+ }
+ return P;
+ };
+ const skinRows = [0.66, 0.88];
+ for (const upper of [true, false]) {
+ for (const t of skinRows) B.poly(row(upper, () => t, -Wl, Wl, 24), V.blueDim, 0.85, { alpha: 0.42 });
+ for (const z of [-0.8, -0.5, 0.5, 0.8].map((v) => v * Wl)) {
+ P.reset();
+ for (let i = 0; i <= 8; i++) P.p(...pt(upper, 0.45 + (0.55 * i) / 8, z));
+ B.poly(P, V.blueDim, 0.85, { alpha: 0.42 });
+ }
+ // the vermilion border, closing to the corners; the upper one with a Cupid's bow
+ const Wv = Math.min(Wl, Wm + 0.3 * k);
+ const border = (z: number) => {
+ const s = Math.sqrt(Math.max(0, 1 - (z / Wv) ** 2));
+ const bow = upper ? -0.07 * Math.exp(-((z / (0.32 * k)) ** 2)) + 0.05 * Math.exp(-(((Math.abs(z) - 0.55 * k) / (0.3 * k)) ** 2)) : 0;
+ return Math.max(0, (upper ? 0.42 : 0.45) * s + bow * s);
+ };
+ B.poly(row(upper, border, -Wv, Wv, 28), V.pink, 1.3, { alpha: 0.95 });
+ B.poly(row(upper, (z) => 0.45 * border(z), -Wv, Wv, 28), V.pink, 1.0, { alpha: 0.45 });
+ }
+ // where the lips meet beyond the corners
+ for (const s of [1, -1]) B.poly(row(true, () => 0, s * Wm, s * Wl, 6), V.pink, 1.0, { alpha: 0.6 });
+ // the opening
+ const open = ul.y - ll.y > 0.02 * k;
+ const n = 24;
+ P.reset();
+ for (let i = 0; i <= n; i++) P.p(...pt(true, 0, -Wm + (2 * Wm * i) / n));
+ if (open) for (let i = n - 1; i > 0; i--) P.p(...pt(false, 0, -Wm + (2 * Wm * i) / n));
+ B.poly(P, V.pink, 2.1, { hot: true, close: open });
+ return open ? P.a.slice(0, P.n * 3) : null;
+ }
+
+ // ------------------------------------------------------------------ the tongue
+ private tongue(f: HumanFrame, a: HumanArtic, k: number): void {
+ const T = f.tongue;
+ let iTip = 0, bd = Infinity;
+ T.forEach((p, i) => {
+ const d = (p.x - f.tip.x) ** 2 + (p.y - f.tip.y) ** 2;
+ if (d < bd) {
+ bd = d;
+ iTip = i;
+ }
+ });
+ // the upper surface from the root over the dorsum to the tip, in even stations
+ const N = 22;
+ const st = resample(T.slice(0, iTip + 1), N);
+ const lateral = clamp01(a.lateral);
+ const frames = st.map((p, i) => {
+ const q0 = st[Math.max(0, i - 1)], q1 = st[Math.min(N - 1, i + 1)];
+ let tx = q1.x - q0.x, ty = q1.y - q0.y;
+ const tl = Math.hypot(tx, ty) || 1;
+ tx /= tl;
+ ty /= tl;
+ const sg = i / (N - 1);
+ // outward: the surface runs root → tip with the tongue below it, so its left normal
+ return { p, nx: -ty, ny: tx, W: interp(TONGUE_W, sg) * k, h: (interp(TONGUE_H, sg) + 0.8 * lateral * smooth(0.45, 0.8, sg)) * k };
+ });
+ const at = (fr: (typeof frames)[number], ph: number): V3 => {
+ const drop = fr.h * (1 - Math.cos(ph));
+ return [fr.p.x - fr.nx * drop, fr.p.y - fr.ny * drop, fr.W * Math.sin(ph)];
+ };
+ const P = this.tmp;
+ for (let i = 1; i < N - 1; i += 2) {
+ P.reset();
+ for (let j = 0; j <= 12; j++) P.p(...at(frames[i], -Math.PI / 2 + (Math.PI * j) / 12));
+ this.beams.poly(P, V.green, 1.0, { alpha: 0.5 });
+ }
+ for (const ph of [-Math.PI / 2, -Math.PI / 4, Math.PI / 4, Math.PI / 2]) {
+ P.reset();
+ for (const fr of frames) P.p(...at(fr, ph));
+ this.beams.poly(P, V.green, Math.abs(ph) > 1 ? 1.25 : 1.0, { alpha: Math.abs(ph) > 1 ? 0.8 : 0.45 });
+ }
+ }
+
+ // ------------------------------------------------------------------ the airway
+ private airway(model: VoiceModel, f: HumanFrame, tele: TelemetryMessage | null, view: ViewFlags, k: number): void {
+ const g = f.grid;
+ const R = this.ring;
+ R.n = 0;
+ if (g.length < 2) return;
+ const nodeS = tele ? model.nodeS(tele.t.phLen, tele.t.orLen) : null;
+ const pressure = view.pressure && tele && nodeS && nodeS.length === tele.t.pressureRms.length;
+ let ux = -1, uy = 0;
+ for (let i = 0; i < g.length && R.n < R.s.length; i++) {
+ const s = g[i];
+ const dx = s.outer.x - s.inner.x, dy = s.outer.y - s.inner.y, d = Math.hypot(dx, dy);
+ if (d > 1e-4) {
+ ux = dx / d;
+ uy = dy / d;
+ }
+ // the acoustic gap hangs from the outer wall (contact models can shorten the ray's gap)
+ const half = s.d / 2;
+ const j = R.n++;
+ R.s[j] = s.s;
+ R.cx[j] = s.outer.x - ux * half;
+ R.cy[j] = s.outer.y - uy * half;
+ R.ux[j] = ux;
+ R.uy[j] = uy;
+ R.a[j] = half;
+ // an ellipse with this midsagittal gap and the section's area
+ R.b[j] = s.d > 0.01 ? Math.min(3.5 * k, (2 * s.area) / (Math.PI * s.d)) : 0;
+ }
+ const P = this.tmp;
+ const cam = this.camera.position;
+ for (let j = 0; j < R.n; j += 2) {
+ P.reset();
+ for (let i = 0; i <= 24; i++) {
+ const an = (i / 24) * Math.PI * 2, c = Math.cos(an) * R.a[j], sn = Math.sin(an) * R.b[j];
+ P.p(R.cx[j] + R.ux[j] * c, R.cy[j] + R.uy[j] * c, sn);
+ }
+ // the ring's plane faces along the tract (its normal is across the grid line)
+ const vx = cam.x - R.cx[j], vy = cam.y - R.cy[j], vz = cam.z;
+ const facing = Math.abs(R.uy[j] * vx - R.ux[j] * vy) / (Math.hypot(vx, vy, vz) || 1);
+ const fade = 1 - 0.65 * facing * facing;
+ if (pressure) {
+ const lvl = Math.pow(pressureLevel(nodeValueAtS(tele!.t.pressureRms, nodeS!, R.s[j])), 0.85);
+ this.beams.poly(P, heat(lvl), 0.85, { alpha: (0.25 + 0.65 * lvl) * fade });
+ } else this.beams.poly(P, V.blueDim, 0.85, { alpha: (view.grid ? 0.8 : 0.45) * fade });
+ }
+ // the tube's side walls
+ for (const side of [1, -1]) {
+ P.reset();
+ for (let j = 0; j < R.n; j++) P.p(R.cx[j], R.cy[j], side * R.b[j]);
+ this.beams.poly(P, V.blue, 1.0, { alpha: 0.55 });
+ }
+ // the epilarynx: a short round tube up from the folds
+ const gl = f.glottis;
+ for (const u of [0.3, 0.65]) {
+ const cx = gl.x + (R.cx[0] - gl.x) * u, cy = gl.y + (R.cy[0] - gl.y) * u, r = (0.55 + 0.25 * u) * k;
+ P.reset();
+ for (let i = 0; i <= 20; i++) P.p(cx + r * Math.cos((i / 20) * Math.PI * 2), cy, r * Math.sin((i / 20) * Math.PI * 2));
+ this.beams.poly(P, V.blueDim, 0.9, { alpha: 0.6 });
+ }
+ }
+
+ /** a point in the airway at s (cm from the glottis), across (u) and sideways (v), each −1..1 */
+ private airPoint(f: HumanFrame, s: number, u: number, v: number): V3 | null {
+ const R = this.ring;
+ if (R.n < 2) return null;
+ if (s <= R.s[0]) {
+ const t = Math.max(0, s / Math.max(0.01, R.s[0]));
+ const g = f.glottis;
+ return [g.x + (R.cx[0] - g.x) * t + u * 0.3 * this.k, g.y + (R.cy[0] - g.y) * t, v * 0.45 * this.k];
+ }
+ let lo = 0, hi = R.n - 1;
+ if (s >= R.s[hi]) return null;
+ while (hi - lo > 1) {
+ const m = (lo + hi) >> 1;
+ if (R.s[m] <= s) lo = m;
+ else hi = m;
+ }
+ const t = (s - R.s[lo]) / Math.max(1e-6, R.s[hi] - R.s[lo]);
+ const l = (q: Float64Array) => q[lo] + (q[hi] - q[lo]) * t;
+ const a = l(R.a) * u * 0.82, b = l(R.b) * v * 0.82;
+ return [l(R.cx) + l(R.ux) * a, l(R.cy) + l(R.uy) * a, b];
+ }
+
+ private drawParticles(model: VoiceModel, f: HumanFrame, tele: TelemetryMessage, dt: number): void {
+ const t = tele.t;
+ const nodeS = model.nodeS(t.phLen, t.orLen);
+ const areas = new Float32Array(t.phAreas.length + t.orAreas.length);
+ areas.set(t.phAreas, 0);
+ areas.set(t.orAreas, t.phAreas.length);
+ const L = f.lipS;
+ for (const p of this.particles) {
+ const U = nodeValueAtS(t.flow, nodeS, p.s);
+ const A = Math.max(0.05, nodeValueAtS(areas, nodeS, p.s));
+ const u = U / A; // particle velocity cm/s
+ const vis = Math.sign(u) * 2.2 * Math.log1p(Math.abs(u) / 30); // perceptual speed (cm/s on screen)
+ p.s += vis * dt;
+ p.life -= dt * 0.25;
+ if (p.s > L || p.s < 0 || p.life <= 0 || Math.abs(u) < 3) {
+ if (!(Math.abs(u) < 3 && p.life > 0)) {
+ p.s = Math.random() * 0.8;
+ p.u = Math.random() * 2 - 1;
+ p.v = Math.random() * 2 - 1;
+ p.life = 0.6 + Math.random() * 0.8;
+ p.px = NaN;
+ continue;
+ }
+ }
+ const pos = this.airPoint(f, p.s, p.u, p.v);
+ if (!pos) continue;
+ if (Number.isFinite(p.px)) {
+ const band = Math.min(2, Math.floor(Math.min(1, Math.abs(vis) / 6) * Math.min(1, p.life) * 3));
+ this.beams.seg(p.px, p.py, p.pz, pos[0], pos[1], pos[2], "#ffe2a8", 1.05, 0.16 + 0.2 * band);
+ }
+ [p.px, p.py, p.pz] = pos;
+ }
+ }
+
+ private drawTurbulence(model: VoiceModel, f: HumanFrame, tele: TelemetryMessage): void {
+ const t = tele.t;
+ if (!(t.noiseLevel > 1) || t.noiseNode < 0) return;
+ const s = model.branchS(t.noiseNode, t.phLen, t.orLen);
+ if (s < 0) return;
+ const intensity = Math.min(1, Math.log10(t.noiseLevel) / 3.2);
+ if (intensity <= 0.05) return;
+ let seed = (this.sparkSeed = (this.sparkSeed * 16807) % 2147483647);
+ const rnd = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
+ for (let i = 0; i < 40; i++) {
+ const pos = this.airPoint(f, s + 0.2 + rnd() * 1.2, rnd() * 2 - 1, rnd() * 2 - 1);
+ if (!pos) continue;
+ const r = 0.02 + rnd() * 0.05;
+ this.beams.seg(pos[0] - r, pos[1], pos[2], pos[0] + r, pos[1], pos[2], "#f4fbff", 1.6, 0.15 + 0.7 * intensity * rnd());
+ }
+ }
+
+ private drawCaliper(f: HumanFrame): void {
+ const R = this.ring;
+ let mi = -1, ma = Infinity;
+ for (let i = 2; i < f.grid.length - 1; i++) if (f.grid[i].area < ma) {
+ ma = f.grid[i].area;
+ mi = i;
+ }
+ if (mi < 0 || ma >= 1.2 || mi >= R.n) return;
+ const P = this.tmp.reset();
+ for (let i = 0; i <= 24; i++) {
+ const an = (i / 24) * Math.PI * 2, c = Math.cos(an) * R.a[mi], sn = Math.sin(an) * R.b[mi];
+ P.p(R.cx[mi] + R.ux[mi] * c, R.cy[mi] + R.uy[mi] * c, sn);
+ }
+ this.beams.poly(P, V.white, 1.3, { hot: true });
+ const g = f.grid[mi];
+ this.caliper = { x: g.outer.x, y: g.outer.y, z: 0, area: ma };
+ }
+
+ // ------------------------------------------------------------------ soft palate, epiglottis, larynx
+ private velum(f: HumanFrame, k: number): void {
+ const v = f.velum;
+ const n = Math.floor(v.length / 2) - 1; // 0..n along the nasal side
+ const fr: { x: number; y: number; nx: number; ny: number; W: number; droop: number }[] = [];
+ for (let i = 0; i <= n; i++) {
+ const a = v[i], b = v[v.length - 1 - i];
+ let nx = a.x - b.x, ny = a.y - b.y;
+ const l = Math.hypot(nx, ny) || 1;
+ nx /= l;
+ ny /= l;
+ const u = i / n;
+ fr.push({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, nx, ny, W: (1.95 - 1.5 * Math.pow(u, 1.5)) * k, droop: 0.45 * k * u });
+ }
+ const at = (q: (typeof fr)[number], u: number): V3 => {
+ const off = 0.12 * k * (1 - u * u) - q.droop * u * u;
+ return [q.x + q.nx * off, q.y + q.ny * off, q.W * u];
+ };
+ const P = this.tmp;
+ for (const i of [1, 3, 5, 7, 9].filter((i) => i <= n)) {
+ P.reset();
+ for (let j = 0; j <= 12; j++) P.p(...at(fr[i], -1 + j / 6));
+ this.beams.poly(P, V.white, 1.0, { alpha: 0.5 });
+ }
+ for (const u of [-1, 1]) {
+ P.reset();
+ for (const q of fr) P.p(...at(q, u));
+ this.beams.poly(P, V.white, 1.0, { alpha: 0.65 });
+ }
+ }
+
+ private epiglottis(f: HumanFrame, k: number, cut: number): void {
+ const [base, tip] = f.epiglottis;
+ let dx = tip.x - base.x, dy = tip.y - base.y;
+ const l = Math.hypot(dx, dy) || 1;
+ dx /= l;
+ dy /= l;
+ // it is a leaf, curling back towards the larynx at its edges
+ const back = { x: -dy, y: dx }; // the side away from the tongue
+ const halfW = (u: number) => (0.22 + 0.9 * Math.pow(Math.sin((u * Math.PI) / 2), 1.3)) * k;
+ const P = this.tmp.reset();
+ const N = 10;
+ for (let i = 0; i <= N; i++) {
+ const u = i / N, w = halfW(u), curl = 0.28 * k * (w / (1.12 * k));
+ P.p(base.x + dx * l * u + back.x * curl, base.y + dy * l * u + back.y * curl, w);
+ }
+ for (let i = 1; i < 12; i++) {
+ const z = halfW(1) * (1 - (2 * i) / 12);
+ const curl = 0.28 * k * (Math.abs(z) / (1.12 * k)) ** 2;
+ P.p(tip.x + back.x * curl + dx * 0.08 * k * (1 - (z / halfW(1)) ** 2), tip.y + back.y * curl + dy * 0.08 * k * (1 - (z / halfW(1)) ** 2), z);
+ }
+ for (let i = N; i >= 0; i--) {
+ const u = i / N, w = halfW(u), curl = 0.28 * k * (w / (1.12 * k));
+ P.p(base.x + dx * l * u + back.x * curl, base.y + dy * l * u + back.y * curl, -w);
+ }
+ this.beams.poly(P, V.blue, 1.1, { alpha: 0.75 });
+ this.beams.line(new Float32Array([base.x, base.y, 0, tip.x, tip.y, 0]), 2, V.blue, 2.1, { alpha: 0.85 * cut });
+ }
+
+ /** the larynx's cartilages and folds round the glottis, the trachea down to the neck's end (bottom) */
+ private larynx(model: VoiceModel, f: HumanFrame, a: HumanArtic, tele: TelemetryMessage | null, k: number, dt: number, bottom: number, cut: number): void {
+ const g = f.glottis, B = this.beams;
+ const L = new Lines();
+ const at = (x: number, y: number, z: number): V3 => [g.x + x * k, g.y + y * k, z * k];
+ const pl = (pts: [number, number, number][], close = false) => {
+ for (const [x, y, z] of pts) L.p(...at(x, y, z));
+ L.end(close);
+ };
+ // thyroid cartilage: two plates meeting at the front (the Adam's apple)
+ for (const s of [1, -1]) {
+ pl([[1.45, 1.75, 0], [0.75, 2.0, s * 1.2], [-0.5, 1.95, s * 1.9], [-0.6, -0.6, s * 1.85], [0.25, -0.85, s * 1.5], [1.05, -0.8, s * 0.55], [1.2, -0.75, 0], [1.6, 0.05, 0], [1.9, 1.15, 0], [1.45, 1.75, 0]]);
+ pl([[-0.5, 1.95, s * 1.9], [-0.7, 2.85, s * 1.85]]);
+ pl([[-0.6, -0.6, s * 1.85], [-0.45, -1.25, s * 1.7]]);
+ }
+ // cricoid: a signet ring, low in front and tall behind
+ for (const [y0, rise] of [[-1.75, 0.15], [-1.4, 1.1]] as const) {
+ for (let i = 0; i <= 28; i++) {
+ const an = (i / 28) * Math.PI * 2;
+ L.p(...at(0.1 + 1.0 * Math.cos(an), y0 + (rise * (1 - Math.cos(an))) / 2, 1.1 * Math.sin(an)));
+ }
+ L.end();
+ }
+ for (const l of L.lines) B.line(l, l.length / 3, V.blueMid, 1.0, { alpha: 0.85 });
+ // trachea: C-shaped rings, open at the back
+ const T = new Lines();
+ const top = g.y - 2.1 * k, cx = g.x + 0.08 * k;
+ for (let y = top; y > bottom; y -= 0.5 * k) T.ellipse([cx, y, 0], [0.82 * k, 0, 0], [0, 0, 0.95 * k], 20, -0.8 * Math.PI, 0.8 * Math.PI);
+ for (const s of [1, -1]) T.p(cx, top, s * 0.95 * k).p(cx, bottom, s * 0.95 * k).end();
+ for (const l of T.lines) B.line(l, l.length / 3, V.blueDim, 0.9, { alpha: 0.85 });
+ // its front and back walls on the midline, as the 2D view draws them
+ for (const s of [1, -1]) B.line(new Float32Array([cx + s * 0.82 * k, g.y, 0, cx + s * 0.82 * k, bottom, 0]), 2, V.blueMid, 1, { alpha: cut });
+ // hyoid: a U open at the back, riding above the larynx
+ const H = new Lines();
+ const hy = { x: model.tract.P(1.6, 0).x, y: model.tract.P(0, 2.9).y + 0.6 * a.larynx * k };
+ for (const dy of [-0.16, 0.16]) {
+ for (let i = 0; i <= 20; i++) {
+ const u = -1 + i / 10;
+ H.p(hy.x + (0.35 - 0.65 * u * u) * k, hy.y + dy * k, 1.15 * u * k);
+ }
+ H.end();
+ for (const s of [1, -1]) H.p(hy.x - 0.3 * k, hy.y + dy * k, s * 1.15 * k).p(hy.x - 2.8 * k, hy.y + (0.7 + dy) * k, s * 2.05 * k).end();
+ }
+ for (const l of H.lines) B.line(l, l.length / 3, V.blueMid, 1.0, { alpha: 0.85 });
+
+ // the vocal folds, opening as the glottal area does (replayed slowly, as the stroboscope shows it)
+ const area = tele?.t.glottalArea;
+ let open = 0;
+ if (area && area.length) {
+ this.strobe = (this.strobe + dt / 1.8) % 1;
+ let maxA = 0.02;
+ for (let i = 0; i < area.length; i++) maxA = Math.max(maxA, area[i]);
+ open = Math.min(0.45 * k, (area[Math.floor(this.strobe * area.length) % area.length] / (1.3 * k)) * 2.2);
+ }
+ const F = new Lines();
+ for (const s of [1, -1]) {
+ F.p(...at(0.72, 0.1, 0)).p(...at(-0.55, 0.1, s * (0.06 + open / k))).end();
+ F.p(...at(0.72, 0.0, 0)).p(...at(-0.6, 0.0, s * 0.62)).end();
+ F.p(...at(0.65, 0.45, 0)).p(...at(-0.5, 0.45, s * 0.45)).end();
+ F.ellipse(at(-0.72, 0.15, s * (0.3 + open / k)), [0.18 * k, 0, 0], [0, 0.12 * k, 0.12 * k], 10);
+ }
+ F.lines.forEach((l, i) => B.line(l, l.length / 3, i % 4 === 0 ? V.white : V.blueMid, i % 4 === 0 ? 1.3 : 1.0, { alpha: i % 4 === 2 ? 0.6 : 0.9, hot: i % 4 === 0 }));
+ }
+}
+
+function rotXY(x: number, y: number, c: Vec, ang: number): [number, number] {
+ const dx = x - c.x, dy = y - c.y, cs = Math.cos(ang), sn = Math.sin(ang);
+ return [c.x + dx * cs - dy * sn, c.y + dx * sn + dy * cs];
+}
+const cross = (a: V3, b: V3): V3 => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
+const norm3 = (a: V3): V3 => {
+ const l = Math.hypot(a[0], a[1], a[2]) || 1;
+ return [a[0] / l, a[1] / l, a[2] / l];
+};
+const scale3 = (a: V3, s: number): V3 => [a[0] * s, a[1] * s, a[2] * s];
+
+function nodeValueAtS(values: ArrayLike<number>, nodeS: Float64Array, s: number): number {
+ let best = 0, bd = Infinity;
+ for (let i = 0; i < nodeS.length; i++) {
+ const d = Math.abs(nodeS[i] - s);
+ if (d < bd) {
+ bd = d;
+ best = i;
+ }
+ }
+ return values[best] ?? 0;
+}
diff --git a/src/ui/render/beams3d.ts b/src/ui/render/beams3d.ts
new file mode 100644
index 0000000..d7f5189
--- /dev/null
+++ b/src/ui/render/beams3d.ts
@@ -0,0 +1,196 @@
+/**
+ * Glowing beams in 3D: an immediate-mode line kit over three.js fat lines, the 3D twin of the
+ * canvas kit in vector.ts. Each frame the renderer begins, adds polylines (body cm) in a colour,
+ * width and alpha, and ends; the segments go into one instanced buffer per line width.
+ *
+ * Lines blend by maximum, not by adding: a polyline's segments overlap at every joint, and adding
+ * would bead each joint. Max blending also keeps a dense wireframe from bleaching to white where
+ * many lines converge (the silhouette of a shell). Scene fog dims lines with depth.
+ * Colours are the palette's sRGB values passed straight through (the renderer outputs them
+ * untransformed), so a beam here matches the same beam on a 2D screen.
+ */
+import * as THREE from "three";
+import { LineSegments2 } from "three/addons/lines/LineSegments2.js";
+import { LineSegmentsGeometry } from "three/addons/lines/LineSegmentsGeometry.js";
+import { LineMaterial } from "three/addons/lines/LineMaterial.js";
+import { core } from "../colors.ts";
+
+/** line widths (CSS px) the batches draw at; a beam takes the nearest */
+const WIDTHS = [0.6, 0.85, 1.05, 1.3, 1.6, 2.1];
+
+/** A growable polyline of xyz points, reused between frames. */
+export class Poly {
+ a = new Float32Array(3 * 256);
+ n = 0;
+ reset(): this {
+ this.n = 0;
+ return this;
+ }
+ p(x: number, y: number, z: number): this {
+ if (3 * this.n + 3 > this.a.length) {
+ const b = new Float32Array(this.a.length * 2);
+ b.set(this.a);
+ this.a = b;
+ }
+ const i = 3 * this.n++;
+ this.a[i] = x;
+ this.a[i + 1] = y;
+ this.a[i + 2] = z;
+ return this;
+ }
+}
+
+class Batch {
+ readonly mesh: LineSegments2;
+ private geo = new LineSegmentsGeometry();
+ private pos = new Float32Array(0);
+ private col = new Float32Array(0);
+ private pb: THREE.InstancedInterleavedBuffer | null = null;
+ private cb: THREE.InstancedInterleavedBuffer | null = null;
+ private cap = 0;
+ n = 0;
+
+ constructor(readonly material: LineMaterial) {
+ this.mesh = new LineSegments2(this.geo, material);
+ this.mesh.frustumCulled = false;
+ this.grow(2048);
+ }
+
+ private grow(cap: number): void {
+ const pos = new Float32Array(cap * 6), col = new Float32Array(cap * 6);
+ pos.set(this.pos.subarray(0, this.n * 6));
+ col.set(this.col.subarray(0, this.n * 6));
+ this.pos = pos;
+ this.col = col;
+ this.cap = cap;
+ // a fresh geometry: three keeps a GPU buffer per attribute, and the old ones go with it
+ this.geo.dispose();
+ this.geo = new LineSegmentsGeometry();
+ const pb = (this.pb = new THREE.InstancedInterleavedBuffer(pos, 6, 1).setUsage(THREE.DynamicDrawUsage));
+ const cb = (this.cb = new THREE.InstancedInterleavedBuffer(col, 6, 1).setUsage(THREE.DynamicDrawUsage));
+ this.geo.setAttribute("instanceStart", new THREE.InterleavedBufferAttribute(pb, 3, 0));
+ this.geo.setAttribute("instanceEnd", new THREE.InterleavedBufferAttribute(pb, 3, 3));
+ this.geo.setAttribute("instanceColorStart", new THREE.InterleavedBufferAttribute(cb, 3, 0));
+ this.geo.setAttribute("instanceColorEnd", new THREE.InterleavedBufferAttribute(cb, 3, 3));
+ this.mesh.geometry = this.geo;
+ }
+
+ seg(x0: number, y0: number, z0: number, x1: number, y1: number, z1: number, r: number, g: number, b: number): void {
+ if (this.n >= this.cap) this.grow(this.cap * 2);
+ const i = this.n++ * 6, p = this.pos, c = this.col;
+ p[i] = x0;
+ p[i + 1] = y0;
+ p[i + 2] = z0;
+ p[i + 3] = x1;
+ p[i + 4] = y1;
+ p[i + 5] = z1;
+ c[i] = c[i + 3] = r;
+ c[i + 1] = c[i + 4] = g;
+ c[i + 2] = c[i + 5] = b;
+ }
+
+ commit(): void {
+ this.geo.instanceCount = this.n;
+ this.mesh.visible = this.n > 0;
+ for (const buf of [this.pb!, this.cb!]) {
+ buf.clearUpdateRanges();
+ buf.addUpdateRange(0, this.n * 6);
+ buf.needsUpdate = true;
+ }
+ }
+
+ dispose(): void {
+ this.geo.dispose();
+ this.material.dispose();
+ }
+}
+
+const rgbCache = new Map<string, [number, number, number]>();
+function rgb(hex: string): [number, number, number] {
+ let c = rgbCache.get(hex);
+ if (!c) {
+ const v = parseInt(hex.slice(1), 16);
+ c = [((v >> 16) & 255) / 255, ((v >> 8) & 255) / 255, (v & 255) / 255];
+ rgbCache.set(hex, c);
+ }
+ return c;
+}
+
+export interface Beam3Opts {
+ alpha?: number;
+ /** whitish core, for the brightest lines */
+ hot?: boolean;
+ close?: boolean;
+}
+
+export class Beams {
+ readonly group = new THREE.Group();
+ private readonly batches: Batch[];
+
+ constructor() {
+ this.batches = WIDTHS.map((w) => {
+ const m = new LineMaterial({ linewidth: w, vertexColors: true, transparent: true, depthTest: false, depthWrite: false, toneMapped: false });
+ m.blending = THREE.CustomBlending;
+ m.blendEquation = THREE.MaxEquation;
+ m.fog = true;
+ const b = new Batch(m);
+ this.group.add(b.mesh);
+ return b;
+ });
+ }
+
+ /** the canvas size in CSS px (the fat lines' widths are in CSS px) */
+ setResolution(w: number, h: number): void {
+ for (const b of this.batches) b.material.resolution.set(w, h);
+ }
+
+ begin(): void {
+ for (const b of this.batches) b.n = 0;
+ }
+
+ end(): void {
+ for (const b of this.batches) b.commit();
+ }
+
+ private batch(width: number): Batch {
+ let best = 0;
+ for (let i = 1; i < WIDTHS.length; i++) if (Math.abs(WIDTHS[i] - width) < Math.abs(WIDTHS[best] - width)) best = i;
+ return this.batches[best];
+ }
+
+ /** a polyline through the first n points of a flat xyz array */
+ line(a: ArrayLike<number>, n: number, color: string, width: number, o: Beam3Opts = {}): void {
+ if (n < 2) return;
+ const al = o.alpha ?? 1;
+ this.strokes(a, n, rgb(color), al, this.batch(width), o.close ?? false);
+ if (o.hot) this.strokes(a, n, rgb(core(color)), al, this.batch(width * 0.42), o.close ?? false);
+ }
+
+ poly(p: Poly, color: string, width: number, o: Beam3Opts = {}): void {
+ this.line(p.a, p.n, color, width, o);
+ }
+
+ /** one segment */
+ seg(x0: number, y0: number, z0: number, x1: number, y1: number, z1: number, color: string, width: number, alpha = 1): void {
+ const [r, g, b] = rgb(color);
+ this.batch(width).seg(x0, y0, z0, x1, y1, z1, r * alpha, g * alpha, b * alpha);
+ }
+
+ private strokes(a: ArrayLike<number>, n: number, [r, g, b]: [number, number, number], al: number, bt: Batch, close: boolean): void {
+ r *= al;
+ g *= al;
+ b *= al;
+ for (let i = 0; i < n - 1; i++) {
+ const j = 3 * i;
+ bt.seg(a[j], a[j + 1], a[j + 2], a[j + 3], a[j + 4], a[j + 5], r, g, b);
+ }
+ if (close) {
+ const j = 3 * (n - 1);
+ bt.seg(a[j], a[j + 1], a[j + 2], a[0], a[1], a[2], r, g, b);
+ }
+ }
+
+ dispose(): void {
+ for (const b of this.batches) b.dispose();
+ }
+}
diff --git a/src/ui/render/profile.ts b/src/ui/render/profile.ts
new file mode 100644
index 0000000..2f7d25d
--- /dev/null
+++ b/src/ui/render/profile.ts
@@ -0,0 +1,203 @@
+/**
+ * The head around the vocal tract, in body cm (x forward, y up, glottis at the origin): the
+ * midsagittal outlines the anatomy views draw (skin, skull, brain, vertebrae, palate, jaw, larynx)
+ * built from the engine's frame, and the eye's wandering gaze. The 2D view draws these outlines;
+ * the 3D view uses them as the midline of its shapes.
+ */
+import type { HumanArtic, HumanFrame } from "../../wasm/types.ts";
+import type { Vec } from "./geom.ts";
+import { rotateAbout, dist, catmullRom } from "./geom.ts";
+import type { VoiceModel } from "../analysis.ts";
+
+/** the visible part of the head, in body cm */
+export const VIEW = { x0: -7.5, x1: 10.25, y0: -4.55, y1: 19.9 };
+
+/** Body-space outlines for one frame. */
+export interface Profile {
+ f: HumanFrame;
+ model: VoiceModel;
+ skinLower: Vec[];
+ skinUpper: Vec[];
+ inset: Vec[];
+ skull: Vec[];
+ brain: Vec[];
+ sulcus: Vec[];
+ eye: Vec[];
+ eyeC: Vec;
+ vertebrae: Vec[][];
+ cords: Vec[][];
+ nasal: Vec[];
+ turbinates: Vec[][];
+ palOral: Vec[];
+ palNasal: Vec[];
+ jawLower: Vec[];
+ jawUpper: Vec[];
+ hyoid: Vec[];
+ thyroid: Vec[];
+ trachea: Vec[][];
+ rings: Vec[][];
+ outerWall: Vec[];
+ glottisWall: Vec[];
+ fibres: Vec[][];
+ folds: Vec[][];
+ lowerTeeth: Vec[];
+}
+
+/** the jaw's rotation (radians, about the condyle) for an opening at the incisors */
+export function jawAngle(model: VoiceModel, jaw: number): number {
+ const t = model.tract;
+ return (-Math.max(-0.2, Math.min(3, jaw)) * t.scale) / dist(t.P(-2.5, 10.5), t.P(6.95, 7.75));
+}
+
+/** template point → body point carried by the jaw */
+export function jawFn(model: VoiceModel, a: HumanArtic): (x: number, y: number) => Vec {
+ const t = model.tract;
+ const condyle = t.P(-2.5, 10.5);
+ const phi = jawAngle(model, a.jaw);
+ return (x: number, y: number) => rotateAbout(t.P(x, y), condyle, phi);
+}
+
+/** an ellipse; rotation is in screen space (y down), as the MRI renderer had it: negated for body space */
+export function ellipse(c: Vec, rx: number, ry: number, rot = 0, n = 40): Vec[] {
+ const cr = Math.cos(-rot), sr = Math.sin(-rot);
+ const o: Vec[] = [];
+ for (let i = 0; i <= n; i++) {
+ const an = (i / n) * Math.PI * 2;
+ const x = Math.cos(an) * rx, y = Math.sin(an) * ry;
+ o.push({ x: c.x + x * cr - y * sr, y: c.y + x * sr + y * cr });
+ }
+ return o;
+}
+
+function roundRect(p0: Vec, p1: Vec, r: number): Vec[] {
+ const x0 = Math.min(p0.x, p1.x), x1 = Math.max(p0.x, p1.x), y0 = Math.min(p0.y, p1.y), y1 = Math.max(p0.y, p1.y);
+ const rr = Math.min(r, (x1 - x0) / 2, (y1 - y0) / 2);
+ const o: Vec[] = [];
+ const corner = (cx: number, cy: number, a0: number) => {
+ for (let i = 0; i <= 6; i++) {
+ const an = a0 + (i / 6) * (Math.PI / 2);
+ o.push({ x: cx + Math.cos(an) * rr, y: cy + Math.sin(an) * rr });
+ }
+ };
+ corner(x1 - rr, y1 - rr, 0);
+ corner(x0 + rr, y1 - rr, Math.PI / 2);
+ corner(x0 + rr, y0 + rr, Math.PI);
+ corner(x1 - rr, y0 + rr, (3 * Math.PI) / 2);
+ o.push(o[0]);
+ return o;
+}
+
+/** the posterior pharyngeal wall's x at height y (template cm) */
+export const wallX = (y: number) => -1.2 - 0.025 * y;
+
+/** facial profile in true body coordinates (the tract's P() compresses the oral region's x, which would flatten the nose) */
+export function bodyPoint(model: VoiceModel): (x: number, y: number) => Vec {
+ const k = model.tract.scale;
+ const ph = model.bodyParams.pharynx;
+ return (x: number, y: number): Vec => ({ x: x * k, y: (y < 7 ? 7 - (7 - y) * ph : y) * k });
+}
+
+export function buildProfile(model: VoiceModel, f: HumanFrame, a: HumanArtic): Profile {
+ const t = model.tract;
+ const P = (x: number, y: number) => t.P(x, y);
+ const J = jawFn(model, a);
+ const k = t.scale;
+ const lar = a.larynx * k;
+ const B = bodyPoint(model);
+ const ulOut = f.upperLip.slice(7); // edge → nose
+ const llOut = f.lowerLip.slice(7); // edge → chin
+ const neckFront = catmullRom([B(2.3, -4.6), B(2.38, -1.5), B(2.52, 0.4), B(3.0, 1.7), J(5.3, 3.0), J(7.25, 3.35), J(7.95, 4.6), J(7.55, 5.7)], 4);
+ const face = catmullRom([ulOut[ulOut.length - 1], B(7.05, 10.6), B(7.75, 10.78), B(8.75, 11.05), B(9.25, 11.65), B(8.95, 12.6), B(8.35, 13.9), B(7.7, 15.0), B(7.95, 16.1), B(7.9, 18.1), B(6.9, 20.5)], 4);
+ const cranium = catmullRom([B(6.9, 20.5), B(3.0, 22.9), B(-2.2, 22.7), B(-6.9, 19.6), B(-8.7, 15.4), B(-8.3, 11.9), B(-7.4, 8.9), B(-6.8, 4.8), B(-6.6, 0), B(-6.6, -4.6)], 4);
+ const vertebrae: Vec[][] = [];
+ for (let i = 0; i < 9; i++) {
+ const y0 = -4.4 + i * 1.75;
+ if (y0 > 10.5) break;
+ const x0 = wallX(y0) - 2.25;
+ vertebrae.push(roundRect(P(x0, y0 + 1.4), P(x0 + 1.6, y0), 0.32 * k));
+ }
+ const palRaw: [number, number][] = [[1.0, 9.6], [1.8, 9.92], [2.7, 10.18], [3.6, 10.3], [4.4, 10.25], [5.1, 10.05], [5.7, 9.72], [6.15, 9.36], [6.55, 9.15], [7.0, 9.6]];
+ const palOral = palRaw.map(([x, y]) => t.P(x, y));
+ // the mandible's body only: its ramus is lateral, not in the midsagittal plane
+ const jb = f.jawBone.filter((p) => p.x > 0.8 * k);
+ const g = f.glottis;
+ const hy = { x: P(1.6, 0).x, y: P(0, 2.9).y + 0.6 * lar };
+ const rings: Vec[][] = [];
+ for (let i = 0; i < 4; i++) {
+ const y = g.y - (0.9 + i * 1.0) * k;
+ if (y < -4.4) break;
+ rings.push([{ x: g.x + 0.8 * k, y }, { x: g.x + 1.05 * k, y: y - 0.05 }, { x: g.x + 1.05 * k, y: y - 0.5 * k }, { x: g.x + 0.82 * k, y: y - 0.45 * k }, { x: g.x + 0.8 * k, y }]);
+ }
+ const origin = J(6.0, 5.45);
+ const tc = f.tongueCenter;
+ const fibres: Vec[][] = [];
+ for (let i = 0; i <= 16; i++) {
+ const ang = Math.PI * (0.18 + (0.9 * i) / 16);
+ const end = { x: tc.x + Math.cos(ang) * f.tongueRadius * 1.25, y: tc.y + Math.sin(ang) * f.tongueRadius * 1.25 };
+ const ctrl = { x: tc.x, y: (tc.y + origin.y) / 2 };
+ const c: Vec[] = [];
+ for (let j = 0; j <= 12; j++) {
+ const u = j / 12;
+ c.push({ x: (1 - u) * (1 - u) * origin.x + 2 * (1 - u) * u * ctrl.x + u * u * end.x, y: (1 - u) * (1 - u) * origin.y + 2 * (1 - u) * u * ctrl.y + u * u * end.y });
+ }
+ fibres.push(c);
+ }
+ const wallAtGlottis = { x: wallX(g.y / k) * k, y: g.y };
+ return {
+ f,
+ model,
+ skinLower: [...neckFront, ...llOut.slice().reverse()],
+ skinUpper: [...ulOut, ...face.slice(1), ...cranium.slice(1)],
+ inset: catmullRom([B(1.85, -4.6), B(1.9, -0.8), B(2.3, 1.6), J(5.2, 3.6), J(6.9, 3.9), J(7.4, 4.9), J(7.2, 5.9), B(6.6, 9.9), B(7.6, 10.9), B(8.55, 11.5), B(8.1, 13.4), B(7.25, 15.2), B(7.45, 18.0), B(3.0, 22.2), B(-2.0, 22.0), B(-6.3, 19.2), B(-7.9, 15.3), B(-7.6, 12.0), B(-6.7, 8.7), B(-6.1, 4.6), B(-6.0, -4.6)], 3),
+ skull: catmullRom([B(7.0, 16.2), B(7.3, 18.5), B(6.0, 20.7), B(3.0, 22.1), B(-2.0, 22.0), B(-6.3, 19.2), B(-7.9, 15.3), B(-7.4, 12.6), B(-4.6, 12.2), B(-2.2, 13.3), B(1.0, 13.7), B(4.1, 14.1), B(6.2, 14.9)], 4),
+ brain: catmullRom([B(6.55, 16.3), B(6.75, 18.4), B(5.5, 20.1), B(2.8, 21.4), B(-1.9, 21.3), B(-5.8, 18.8), B(-7.1, 15.4), B(-6.7, 13.1), B(-4.4, 12.8), B(-2.1, 13.9), B(1.0, 14.3), B(4.0, 14.7), B(5.9, 15.4)], 4),
+ sulcus: catmullRom([B(5.4, 19.4), B(2.4, 20.5), B(-1.4, 20.4), B(-4.8, 18.4)], 4),
+ eye: ellipse(B(6.5, 15.4), 1.1 * k, 0.95 * k),
+ eyeC: B(6.5, 15.4),
+ vertebrae,
+ cords: [[P(-5.3, -4.6), P(-4.5, -4.6), P(-4.5, 11.8), P(-5.1, 12.4), P(-5.6, 11.6)], [P(-5.1, -4.6), P(-4.7, -4.6), P(-4.72, 11.4), P(-5.15, 11.6)]],
+ nasal: catmullRom([B(0.3, 11.2), B(1.1, 10.25), B(2.7, 10.75), B(4.6, 10.85), B(6.4, 10.65), B(7.6, 10.85), B(8.45, 11.3), B(8.0, 12.2), B(6.7, 13.4), B(4.4, 13.9), B(1.8, 13.6), B(0.2, 12.6)], 4),
+ turbinates: ([[3.0, 11.45, 2.1, 0.32], [3.5, 12.45, 1.9, 0.3], [4.0, 13.3, 1.45, 0.22]] as const).map(([cx, cy, rx, ry]) => ellipse(B(cx, cy), rx * k, ry * k, -0.04)),
+ palOral,
+ palNasal: palOral.map((p) => ({ x: p.x, y: p.y + 0.55 * k })),
+ jawLower: jb,
+ jawUpper: jb.map((p) => ({ x: p.x, y: p.y + 0.75 * k })),
+ hyoid: ellipse(hy, 0.5 * k, 0.28 * k, 0.3, 28),
+ thyroid: [{ x: g.x + 0.75 * k, y: g.y - 1.6 * k }, { x: g.x + 1.75 * k, y: g.y - 0.6 * k }, { x: g.x + 1.95 * k, y: g.y + 1.2 * k }, { x: g.x + 1.45 * k, y: g.y + 1.9 * k }, { x: g.x + 1.15 * k, y: g.y + 1.1 * k }, { x: g.x + 1.3 * k, y: g.y - 0.4 * k }, { x: g.x + 0.75 * k, y: g.y - 1.6 * k }],
+ trachea: [[{ x: g.x - 0.65 * k, y: g.y }, { x: g.x - 0.75 * k, y: -4.8 }], [{ x: g.x + 0.75 * k, y: g.y }, { x: g.x + 0.95 * k, y: -4.8 }]],
+ rings,
+ outerWall: f.outer.filter((p) => p.y > g.y || p.x > 0),
+ glottisWall: [wallAtGlottis, f.outer.find((p) => p.y > g.y) ?? wallAtGlottis],
+ fibres,
+ folds: [
+ [{ x: g.x - 0.62 * k, y: g.y + 0.05 }, { x: g.x - 0.1, y: g.y + 0.12 * k }, { x: g.x - 0.62 * k, y: g.y + 0.32 * k }, { x: g.x - 0.62 * k, y: g.y + 0.05 }],
+ [{ x: g.x + 0.72 * k, y: g.y + 0.05 }, { x: g.x + 0.12, y: g.y + 0.12 * k }, { x: g.x + 0.72 * k, y: g.y + 0.32 * k }, { x: g.x + 0.72 * k, y: g.y + 0.05 }],
+ ],
+ lowerTeeth: [...f.lowerTeeth, J(6.8, 6.0), J(6.4, 6.2), f.lowerTeeth[0]],
+ };
+}
+
+/**
+ * Where the eye looks: it holds still for a few seconds, then darts somewhere new (a saccade),
+ * and every so often back to straight ahead. Pitch is up (+) and down; yaw is out of the
+ * midsagittal plane (towards the viewer of the side view).
+ */
+export class Gaze {
+ pitch = 0;
+ yaw = 0;
+ private toPitch = 0;
+ private toYaw = 0;
+ private next = 2;
+
+ update(clock: number, dt: number): void {
+ if (clock >= this.next) {
+ const centre = Math.random() < 0.35;
+ this.toPitch = centre ? 0 : (Math.random() * 2 - 1) * 0.55;
+ this.toYaw = centre ? 0 : (Math.random() * 2 - 1) * 0.9;
+ this.next = clock + 1.2 + Math.random() * 3.5;
+ }
+ const ease = 1 - Math.exp(-dt * 28);
+ this.pitch += (this.toPitch - this.pitch) * ease;
+ this.yaw += (this.toYaw - this.yaw) * ease;
+ }
+}
diff --git a/src/ui/render/readouts.ts b/src/ui/render/readouts.ts
new file mode 100644
index 0000000..84cc9cc
--- /dev/null
+++ b/src/ui/render/readouts.ts
@@ -0,0 +1,87 @@
+/**
+ * The face screen's readouts: formants and info lines in one corner, and four OP-1 style slots
+ * (big numerals in each control's colour). Shared by the 2D and 3D anatomy views.
+ */
+import { V } from "../colors.ts";
+import { dot, ipaGlyph, label } from "./vector.ts";
+
+/** an OP-1 style readout in the space under the chin */
+export interface Slot {
+ label: string;
+ value: string;
+ unit?: string;
+ color: string;
+ /** value is an IPA symbol, drawn as strokes */
+ ipa?: boolean;
+}
+
+export interface ReadoutLayout {
+ /** canvas size in CSS px */
+ w: number;
+ h: number;
+ /** a narrow screen (a phone): readouts in a row above the head, slots in a row below it */
+ compact: boolean;
+ /** top-left of the 2×2 slot grid (wide layout); the grid fills from there to the bottom right */
+ slotX: number;
+ slotY: number;
+}
+
+export function drawReadouts(ctx: CanvasRenderingContext2D, layout: ReadoutLayout, formants: number[], info: string[], slots: Slot[]): void {
+ if (layout.compact) return drawReadoutsCompact(ctx, layout, formants, info, slots);
+ const { w, h } = layout;
+ const fx = w - 18;
+ let y = 26;
+ if (formants.length >= 3) {
+ ["F1", "F2", "F3"].forEach((lab, i) => {
+ label(ctx, lab, fx - 78, y + i * 22, { size: 12, color: V.amber, ls: 0.12, alpha: 0.75 });
+ label(ctx, String(Math.round(formants[i])), fx, y + i * 22, { size: 15, color: V.amber, align: "right", ls: 0.08 });
+ });
+ label(ctx, "HZ", fx, y + 64, { size: 10, color: V.amber, align: "right", alpha: 0.6 });
+ y += 110;
+ }
+ for (const s of info) {
+ label(ctx, s, fx, y, { size: 10, color: V.blueMid, align: "right", ls: 0.14 });
+ y += 16;
+ }
+ if (!slots.length) return;
+ const gx0 = layout.slotX, gy0 = layout.slotY;
+ const cellW = Math.max(60, (w - 14 - gx0) / 2);
+ const cellH = Math.max(56, Math.min(112, (h - 8 - gy0) / 2));
+ const big = Math.max(22, Math.min(44, cellH * 0.4, cellW * 0.32));
+ slots.slice(0, 4).forEach((s, i) => {
+ const cx = gx0 + (i % 2) * cellW, cy = gy0 + Math.floor(i / 2) * cellH;
+ dot(ctx, cx + 6, cy + 4, 4, s.color);
+ label(ctx, s.label.toUpperCase(), cx + 16, cy + 8, { size: 11, color: s.color, ls: 0.2, alpha: 0.85 });
+ const by = cy + 18 + big * 1.1;
+ if (s.ipa) ipaGlyph(ctx, cx + big * 0.6, by, s.value, big * 1.45, s.color);
+ else label(ctx, s.value.toUpperCase(), cx, by, { size: s.value.length > 3 ? big * 0.82 : big, color: s.color, weight: "m", ls: 0.04 });
+ if (s.unit) label(ctx, s.unit.toUpperCase(), s.ipa ? cx + big * 1.5 : cx + (s.value.length > 3 ? 0 : big * 1.64), s.ipa || s.value.length <= 3 ? by : by + 18, { size: 11, color: s.color, ls: 0.16, alpha: 0.7 });
+ });
+}
+
+/** narrow screens: formants and info in two rows on top, the four slots in a row at the bottom */
+function drawReadoutsCompact(ctx: CanvasRenderingContext2D, layout: ReadoutLayout, formants: number[], info: string[], slots: Slot[]): void {
+ const { w, h } = layout;
+ if (formants.length >= 3) {
+ const cw = (w - 24) / 3;
+ ["F1", "F2", "F3"].forEach((lab, i) => {
+ label(ctx, lab, 12 + i * cw, 18, { size: 10, color: V.amber, ls: 0.12, alpha: 0.75 });
+ label(ctx, String(Math.round(formants[i])), 12 + i * cw + 26, 18, { size: 13, color: V.amber, ls: 0.06 });
+ });
+ }
+ label(ctx, info.slice(0, 2).join(" "), 12, 36, { size: 9, color: V.blueMid, ls: 0.12 });
+ if (info.length > 2) label(ctx, info.slice(2).join(" "), 12, 50, { size: 9, color: V.blueMid, ls: 0.12 });
+ const four = slots.slice(0, 4);
+ if (!four.length) return;
+ const cw = (w - 24) / four.length, y0 = h - 82;
+ const big = Math.max(16, Math.min(30, cw * 0.28));
+ four.forEach((s, i) => {
+ const cx = 12 + i * cw;
+ dot(ctx, cx + 4, y0, 3, s.color);
+ label(ctx, s.label.toUpperCase(), cx + 12, y0 + 3, { size: 8, color: s.color, ls: 0.14, alpha: 0.85 });
+ const by = y0 + 18 + big;
+ if (s.ipa) ipaGlyph(ctx, cx + big * 0.5, by, s.value, big * 1.4, s.color);
+ else label(ctx, s.value.toUpperCase(), cx, by, { size: s.value.length > 3 ? big * 0.62 : big, color: s.color, weight: "m", ls: 0.02 });
+ if (s.unit) label(ctx, s.unit.toUpperCase(), cx, by + 16, { size: 8, color: s.color, ls: 0.14, alpha: 0.7 });
+ });
+}
diff --git a/src/ui/store.ts b/src/ui/store.ts
index 6e14cfe..a982a0a 100644
--- a/src/ui/store.ts
+++ b/src/ui/store.ts
@@ -26,6 +26,8 @@ export interface ViewOptions {
particles: boolean;
grid: boolean;
calipers: boolean;
+ /** the face screen shows the head in 3D (turn it with the pointer; no handles) */
+ threeD: boolean;
}
interface State {
@@ -129,7 +131,7 @@ export const useStore = create<State>((set, get) => ({
volume: 0.8,
hold: false,
heldNotes: [],
- view: { pressure: true, particles: true, grid: false, calipers: true },
+ view: { pressure: true, particles: true, grid: false, calipers: true, threeD: false },
mode: "play",
setMode: (m) => {
const prev = get().mode;
diff --git a/src/ui/styles.css b/src/ui/styles.css
index 7212af5..0e5ddc1 100644
--- a/src/ui/styles.css
+++ b/src/ui/styles.css
@@ -216,10 +216,23 @@ button.primary.large {
touch-action: none;
}
-.anatomy[data-hover] canvas {
+.anatomy[data-hover] canvas,
+.anatomy.three canvas {
cursor: grab;
}
+.anatomy.three canvas:active {
+ cursor: grabbing;
+}
+
+.anatomy .no-webgl {
+ margin: 24px;
+ color: var(--blue-dim);
+ font-size: 12px;
+ letter-spacing: 0.12em;
+ text-transform: uppercase;
+}
+
.anatomy[data-hover] canvas:active {
cursor: grabbing;
}