182eaafAdd a Physics 3D tab: slow-motion 3D replay of the simulationJeremy Magland 1// The 3D view of the bass: plates (coloured and displaced by their normal motion), ribs, the air
2// cavity slice (coloured by pressure), the f-hole air plugs (arrows), the bridge, the strings, the
3// plucking and stopping fingers, and the static neck / fingerboard / tailpiece.
4//
5// Body coordinates (x along the body toward the neck, s across toward the treble side, z out of
6// the top, back plate at z = 0) map to three.js as X = x, Y = z, Z = -s. Displacements are drawn
7// exaggerated: the strings by `string`, the body (plates, bridge, the bridge end of the strings)
8// by `body`.
10import * as THREE from "three";
11import { OrbitControls } from "three/addons/controls/OrbitControls.js";
12import type { Params } from "../../core/params.ts";
13import { makeArch, makeBodyGrid, makeFHoles, makeOutline, markFHoles, HOLE, IN, type Outline } from "../../fe/geometry.ts";
14import type { FEShapes } from "../../fe/shapes.ts";
16export interface StringFrame { string: number; length: number; y: Float32Array; z: Float32Array }
18export interface Frame {
19 /** per top / back plate node (m), per air column (Pa), per f-hole plug (m); null = not shown */
20 top: Float32Array | null;
21 back: Float32Array | null;
22 air: Float32Array | null;
23 plug: Float32Array | null;
24 /** notch displacement: y of strings 0..3, z of strings 0..3 (m) */
25 notch: ArrayLike<number>;
26 strings: StringFrame[];
27 /** the plucking finger: pad travel along the pull direction (m, real), visibility 0..1 */
28 finger: { travel: number; alpha: number } | null;
29}
31export interface Scales {
32 /** display exaggeration of string and body motion */
33 string: number;
34 body: number;
35 /** displacement (m) and pressure (Pa) shown at full colour */
36 plate: number;
37 air: number;
38}
40export interface StringSetup {
41 string: number;
42 length: number;
43 points: number;
44 played: boolean;
45}
47export interface PlayedSetup {
48 string: number;
49 length: number;
50 stopped: boolean;
51 /** pluck point, m from the stop; pull direction (y, z) */
52 pluckX: number;
53 dirY: number;
54 dirZ: number;
55}
57export interface Layers { top: boolean; topOpacity: number; back: boolean; air: boolean; ribs: boolean; plugs: boolean; symp: boolean }
59const WOOD = new THREE.Color(0.80, 0.62, 0.40);
60const WOOD_DARK = new THREE.Color(0.45, 0.27, 0.13);
61const POS = new THREE.Color(0.86, 0.16, 0.10);
62const NEG = new THREE.Color(0.12, 0.36, 0.86);
63const AIR0 = new THREE.Color(0.93, 0.93, 0.90);
64const EBONY = 0x1c1a18;
66/** Diverging colour: v in [-1, 1] from NEG through base to POS. */
67function diverge(out: Float32Array, o: number, v: number, base: THREE.Color): void {
68 const a = Math.min(1, Math.abs(v)), c = v >= 0 ? POS : NEG;
69 out[o] = base.r + (c.r - base.r) * a;
70 out[o + 1] = base.g + (c.g - base.g) * a;
71 out[o + 2] = base.b + (c.b - base.b) * a;
72}
74interface PlateMesh { mesh: THREE.Mesh; src: Int32Array; rest: Float32Array; n: number }
76export class BassScene {
77 readonly renderer: THREE.WebGLRenderer;
78 private scene = new THREE.Scene();
79 private camera: THREE.PerspectiveCamera;
80 private controls: OrbitControls;
81 private root = new THREE.Group();
82 private outline: Outline;
83 private topZ: (x: number, s: number) => number;
84 private xb: number;
85 private notchY: number[];
86 private zNotch: number;
87 private xNut: number;
88 private nutFactor = 0.42;
89 private top: PlateMesh | null = null;
90 private back: PlateMesh | null = null;
91 private airMesh: THREE.Mesh | null = null;
92 private airCols: Int32Array | null = null; // per air-quad vertex: column
93 private ribs: THREE.Mesh;
94 private plugArrows: THREE.ArrowHelper[] = [];
95 private plugInfo: { x: number; s: number; z: number }[] = [];
96 private bridge: THREE.Mesh;
97 private bridgeRest: Float32Array;
98 private bridgeFoot: [number, number] = [-1, -1]; // top-plate node index under each foot
99 private strings: { setup: StringSetup; mesh: THREE.Mesh; rest: THREE.Vector3; dir: THREE.Vector3; radius: number }[] = [];
100 private staticStrings = new THREE.Group();
101 private played: PlayedSetup | null = null;
102 private fingerMesh: THREE.Mesh;
103 private stopMesh: THREE.Mesh;
104 private resizeObs: ResizeObserver;
105 private layers: Layers = { top: true, topOpacity: 1, back: true, air: false, ribs: true, plugs: true, symp: true };
106 private needsRender = true;
108 private container: HTMLElement;
110 constructor(container: HTMLElement, p: Params, shapes: FEShapes | null) {
111 this.container = container;
112 const fe = p.fe;
113 this.renderer = new THREE.WebGLRenderer({ antialias: true });
114 this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio || 1));
115 container.append(this.renderer.domElement);
116 const bg = getComputedStyle(document.documentElement).getPropertyValue("--panel").trim() || "#ffffff";
117 this.scene.background = new THREE.Color(bg);
118 this.camera = new THREE.PerspectiveCamera(35, 1, 0.01, 50);
119 this.controls = new OrbitControls(this.camera, this.renderer.domElement);
120 this.controls.enableDamping = true;
121 this.controls.addEventListener("change", () => { this.needsRender = true; });
122 this.scene.add(new THREE.HemisphereLight(0xffffff, 0x444444, 1.6));
123 const sun = new THREE.DirectionalLight(0xffffff, 1.6);
124 sun.position.set(-1, 3, 2);
125 this.scene.add(sun);
126 this.scene.add(this.root);
128 this.outline = makeOutline(fe);
129 const arch = makeArch(fe, this.outline);
130 this.topZ = (x, s) => fe.ribDepth + arch(x, s);
131 this.xb = fe.bridgePos * fe.bodyLength;
132 this.notchY = [0, 1, 2, 3].map((i) => (i - 1.5) * fe.stringSpacing);
133 this.zNotch = this.topZ(this.xb, 0) + fe.bridgeHeight;
134 this.xNut = this.xb + p.strings.scaleLength;
136 // ---- plates
137 let grid: FEShapes["grid"], topNodes: Int32Array, backNodes: Int32Array;
138 if (shapes) ({ grid, topNodes, backNodes } = shapes);
139 else {
140 const g = makeBodyGrid(fe, this.outline, arch, fe.gridH);
141 markFHoles(g, makeFHoles(fe));
142 grid = { nx: g.nx, ns: g.ns, h: g.h, x0: g.x0, s0: g.s0 };
143 const t: number[] = [], b: number[] = [];
144 for (let k = 0; k < g.nx * g.ns; k++) { if (g.type[k] === IN) t.push(k); if (g.type[k] === IN || g.type[k] === HOLE) b.push(k); }
145 topNodes = Int32Array.from(t); backNodes = Int32Array.from(b);
146 }
147 const topSet = new Set(topNodes), holes = new Set([...backNodes].filter((k) => !topSet.has(k)));
148 this.top = this.plate(grid, topNodes, holes, (x, s) => this.topZ(x, s));
149 this.back = this.plate(grid, backNodes, new Set(), () => 0);
150 this.root.add(this.top.mesh, this.back.mesh);
151 // the nodes under the bridge feet
152 for (const [q, sf] of [[0, -fe.footSpan / 2], [1, fe.footSpan / 2]] as const) {
153 let best = -1, bd = Infinity;
154 topNodes.forEach((k, a) => {
155 const x = grid.x0 + Math.floor(k / grid.ns) * grid.h, s = grid.s0 + (k % grid.ns) * grid.h;
156 const d = (x - this.xb) ** 2 + (s - sf) ** 2;
157 if (d < bd) { bd = d; best = a; }
158 });
159 this.bridgeFoot[q] = best;
160 }
162 // ---- ribs
163 this.ribs = this.makeRibs(fe.ribDepth);
164 this.root.add(this.ribs);
166 // ---- air slice and plugs
167 if (shapes) {
168 this.airMesh = this.makeAir(shapes);
169 this.root.add(this.airMesh);
170 for (const pl of shapes.plugs) {
171 const z = this.topZ(pl.xc, pl.sc);
172 this.plugInfo.push({ x: pl.xc, s: pl.sc, z });
173 const a = new THREE.ArrowHelper(new THREE.Vector3(0, 1, 0), new THREE.Vector3(pl.xc, z, -pl.sc), 0.001, 0x2a9d8f, 0.03, 0.025);
174 this.plugArrows.push(a);
175 this.root.add(a);
176 }
177 }
179 // ---- bridge (tapered slab across the strings)
180 {
181 const wB = fe.footSpan + 0.05, wT = 3 * fe.stringSpacing + 0.035, th = 0.022;
182 const z0 = this.topZ(this.xb, 0), z1 = this.zNotch;
183 const v: number[] = [];
184 for (const [z, w] of [[z0, wB], [z1, wT]] as const)
185 for (const s of [-w / 2, w / 2]) for (const dx of [-th / 2, th / 2]) v.push(this.xb + dx, z, -s);
186 const geo = new THREE.BufferGeometry();
187 this.bridgeRest = Float32Array.from(v);
188 geo.setAttribute("position", new THREE.BufferAttribute(Float32Array.from(v), 3));
189 // vertices: bottom (0..3), top (4..7); within each: s-, dx- / dx+, s+, dx- / dx+
190 geo.setIndex([0, 1, 3, 0, 3, 2, 4, 6, 7, 4, 7, 5, 0, 4, 5, 0, 5, 1, 2, 3, 7, 2, 7, 6, 0, 2, 6, 0, 6, 4, 1, 5, 7, 1, 7, 3]);
191 geo.computeVertexNormals();
192 this.bridge = new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ color: 0xd9b98a, roughness: 0.8, side: THREE.DoubleSide, flatShading: true }));
193 this.root.add(this.bridge);
194 }
196 // ---- neck, fingerboard, tailpiece, end pin (static)
197 const ebony = new THREE.MeshStandardMaterial({ color: EBONY, roughness: 0.6 });
198 {
199 const xEnd = Math.max(fe.bodyLength * 0.72, this.xb + 0.3);
200 const len = this.xNut - xEnd;
201 const zTopEnd = this.zNotch - 0.022, zTopNut = this.zNotch - 0.006;
202 const board = new THREE.Mesh(new THREE.BoxGeometry(len, 0.02, 0.1), ebony);
203 board.position.set((xEnd + this.xNut) / 2, (zTopEnd + zTopNut) / 2 - 0.01, 0);
204 board.rotation.z = Math.atan2(zTopNut - zTopEnd, len);
205 board.scale.set(1, 1, 1);
206 this.root.add(board);
207 const neck = new THREE.Mesh(new THREE.BoxGeometry(this.xNut - fe.bodyLength + 0.05, 0.05, 0.06), new THREE.MeshStandardMaterial({ color: WOOD_DARK, roughness: 0.7 }));
208 neck.position.set((fe.bodyLength + this.xNut) / 2, zTopNut - 0.045, 0);
209 this.root.add(neck);
210 const scroll = new THREE.Mesh(new THREE.BoxGeometry(0.22, 0.07, 0.08), new THREE.MeshStandardMaterial({ color: WOOD_DARK, roughness: 0.7 }));
211 scroll.position.set(this.xNut + 0.12, zTopNut - 0.02, 0);
212 this.root.add(scroll);
213 const tail = new THREE.Mesh(new THREE.BoxGeometry(0.28, 0.015, 0.09), ebony);
214 const xTail0 = 0.06, xTail1 = xTail0 + 0.28;
215 tail.position.set((xTail0 + xTail1) / 2, this.topZ(0.2, 0) + 0.05, 0);
216 this.root.add(tail);
217 // afterlengths (bridge to tailpiece) and the strings behind the stop are drawn by setupStrings
218 this.staticStrings.userData.tailX = xTail1;
219 this.staticStrings.userData.tailZ = this.topZ(0.2, 0) + 0.06;
220 this.root.add(this.staticStrings);
221 }
223 // ---- fingers
224 const skin = new THREE.MeshStandardMaterial({ color: 0xe0a98a, roughness: 0.55, transparent: true });
225 this.fingerMesh = new THREE.Mesh(new THREE.CapsuleGeometry(0.009, 0.05, 6, 12), skin);
226 this.stopMesh = new THREE.Mesh(new THREE.CapsuleGeometry(0.009, 0.04, 6, 12), skin.clone());
227 this.stopMesh.rotation.x = Math.PI / 2;
228 this.root.add(this.fingerMesh, this.stopMesh);
229 this.fingerMesh.visible = false;
230 this.stopMesh.visible = false;
232 // ---- camera
233 this.root.position.set(-(this.xNut + 0.1) / 2, -0.15, 0);
234 this.view("overview");
236 this.resizeObs = new ResizeObserver(() => this.resize());
237 this.resizeObs.observe(container);
238 this.resize();
239 const loop = () => {
240 if (!this.renderer.domElement.isConnected) return;
241 this.controls.update();
242 if (this.needsRender) { this.renderer.render(this.scene, this.camera); this.needsRender = false; }
243 requestAnimationFrame(loop);
244 };
245 requestAnimationFrame(loop);
246 }
248 dispose(): void {
249 this.resizeObs.disconnect();
250 this.renderer.dispose();
251 this.renderer.domElement.remove();
252 }
254 private resize(): void {
255 const w = this.container.clientWidth, h = this.container.clientHeight;
256 if (!(w > 0 && h > 0)) return;
257 this.renderer.setSize(w, h, false);
258 this.camera.aspect = w / h;
259 this.camera.updateProjectionMatrix();
260 this.needsRender = true;
261 }
263 /** Preset views. */
264 view(which: "overview" | "top" | "side" | "bridge" | "string" | "below"): void {
265 const c = this.root.position;
266 const at = (x: number, y: number, z: number) => new THREE.Vector3(x + c.x, y + c.y, z + c.z);
267 const xs = this.played ? this.xb + (this.played.length - this.played.pluckX) : this.xb + 0.2;
268 const presets = {
269 overview: [new THREE.Vector3(-1.35, 1.05, 1.0), new THREE.Vector3(-0.1, 0.1, 0)],
270 top: [at(this.xNut / 2, 2.6, 0.001), at(this.xNut / 2, 0, 0)],
271 side: [at(this.xNut / 2, 0.25, 2.2), at(this.xNut / 2, 0.2, 0)],
272 bridge: [at(this.xb - 0.25, this.zNotch + 0.25, 0.35), at(this.xb, this.zNotch - 0.05, 0)],
273 string: [at(xs - 0.12, this.zNotch + 0.32, 0.1), at(xs + 0.02, this.zNotch, 0)],
274 below: [at(this.xNut / 2 - 0.2, -1.6, 1.0), at(this.xNut / 2 - 0.2, 0, 0)],
275 } as const;
276 const [pos, tgt] = presets[which];
277 this.camera.position.copy(pos);
278 this.controls.target.copy(tgt);
279 this.controls.update();
280 this.needsRender = true;
281 }
283 setLayers(l: Partial<Layers>): void {
284 Object.assign(this.layers, l);
285 const L = this.layers;
286 if (this.top) {
287 this.top.mesh.visible = L.top && L.topOpacity > 0.02;
288 const m = this.top.mesh.material as THREE.MeshStandardMaterial;
289 m.transparent = L.topOpacity < 0.99; m.opacity = L.topOpacity; m.depthWrite = L.topOpacity >= 0.99;
290 }
291 if (this.back) this.back.mesh.visible = L.back;
292 if (this.airMesh) this.airMesh.visible = L.air;
293 this.ribs.visible = L.ribs;
294 for (const a of this.plugArrows) a.visible = L.plugs;
295 for (const s of this.strings) if (!s.setup.played) s.mesh.visible = L.symp;
296 this.needsRender = true;
297 }
299 private plate(grid: FEShapes["grid"], nodes: Int32Array, holes: Set<number>, zf: (x: number, s: number) => number): PlateMesh {
300 const { nx, ns, h, x0, s0 } = grid;
301 const srcOf = new Map<number, number>();
302 nodes.forEach((k, a) => srcOf.set(k, a));
303 const vid = new Map<number, number>();
304 const src: number[] = [], rest: number[] = [], idx: number[] = [];
305 const vert = (k: number) => {
306 let v = vid.get(k);
307 if (v !== undefined) return v;
308 const i = Math.floor(k / ns), j = k % ns;
309 let x = x0 + i * h, s = s0 + j * h;
310 const a = srcOf.get(k);
311 if (a === undefined) {
312 // a constrained node: pull it onto the rim so the plate edge follows the outline
313 x = Math.min(this.outline.L, Math.max(0, x));
314 const hw = this.outline.halfWidth(x);
315 s = Math.max(-hw, Math.min(hw, s));
316 }
317 v = src.length;
318 vid.set(k, v);
319 src.push(a ?? -1);
320 rest.push(x, zf(x, s), -s);
321 return v;
322 };
323 for (let i = 0; i + 1 < nx; i++)
324 for (let j = 0; j + 1 < ns; j++) {
325 const ks = [i * ns + j, (i + 1) * ns + j, (i + 1) * ns + j + 1, i * ns + j + 1];
326 if (!ks.some((k) => srcOf.has(k)) || ks.some((k) => holes.has(k))) continue;
327 const [a, b, c, d] = ks.map(vert);
328 idx.push(a, b, c, a, c, d);
329 }
330 const geo = new THREE.BufferGeometry();
331 const restA = Float32Array.from(rest);
332 geo.setAttribute("position", new THREE.BufferAttribute(Float32Array.from(restA), 3));
333 const col = new Float32Array(src.length * 3);
334 for (let v = 0; v < src.length; v++) diverge(col, 3 * v, 0, WOOD);
335 geo.setAttribute("color", new THREE.BufferAttribute(col, 3));
336 geo.setIndex(idx);
337 geo.computeVertexNormals();
338 const mesh = new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.75, side: THREE.DoubleSide }));
339 return { mesh, src: Int32Array.from(src), rest: restA, n: src.length };
340 }
342 private makeRibs(depth: number): THREE.Mesh {
343 const { px, ps } = this.outline;
344 const n = px.length, step = Math.max(1, Math.floor(n / 240));
345 const pts: [number, number][] = [];
346 for (let k = 0; k < n; k += step) pts.push([px[k], ps[k]]);
347 const pos: number[] = [], idx: number[] = [];
348 pts.forEach(([x, s]) => { pos.push(x, 0, -s, x, depth, -s); });
349 const m = pts.length;
350 for (let k = 0; k < m; k++) { const a = 2 * k, b = 2 * ((k + 1) % m); idx.push(a, b, b + 1, a, b + 1, a + 1); }
351 const geo = new THREE.BufferGeometry();
352 geo.setAttribute("position", new THREE.BufferAttribute(Float32Array.from(pos), 3));
353 geo.setIndex(idx);
354 geo.computeVertexNormals();
355 return new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ color: WOOD_DARK, roughness: 0.8, side: THREE.DoubleSide, transparent: true, opacity: 0.45, depthWrite: false }));
356 }
358 private makeAir(sh: FEShapes): THREE.Mesh {
359 const { nxA, nsA, x0, s0, ha, col, colH } = sh.cav;
360 const pos: number[] = [], cols: number[] = [], idx: number[] = [];
361 for (let i = 0; i < nxA; i++)
362 for (let j = 0; j < nsA; j++) {
363 const c = col[i * nsA + j];
364 if (c < 0) continue;
365 const y = colH[c] / 2, v = pos.length / 3;
366 for (const [dx, ds] of [[0, 0], [1, 0], [1, 1], [0, 1]]) { pos.push(x0 + (i + dx) * ha, y, -(s0 + (j + ds) * ha)); cols.push(c); }
367 idx.push(v, v + 1, v + 2, v, v + 2, v + 3);
368 }
369 this.airCols = Int32Array.from(cols);
370 const geo = new THREE.BufferGeometry();
371 geo.setAttribute("position", new THREE.BufferAttribute(Float32Array.from(pos), 3));
372 const c = new Float32Array(cols.length * 3);
373 for (let v = 0; v < cols.length; v++) diverge(c, 3 * v, 0, AIR0);
374 geo.setAttribute("color", new THREE.BufferAttribute(c, 3));
375 geo.setIndex(idx);
376 geo.computeVertexNormals();
377 const mesh = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({ vertexColors: true, side: THREE.DoubleSide }));
378 mesh.visible = false;
379 return mesh;
380 }
382 /** The strings to draw: the recorded ones (vibrating parts) plus static afterlengths and the part behind the stop. */
383 setupStrings(list: StringSetup[], played: PlayedSetup): void {
384 for (const s of this.strings) { this.root.remove(s.mesh); s.mesh.geometry.dispose(); }
385 this.strings = [];
386 this.staticStrings.clear();
387 this.played = played;
388 const R = 8;
389 const metal = new THREE.MeshStandardMaterial({ color: 0xb8b4ac, metalness: 0.85, roughness: 0.3 });
390 const metalPlayed = new THREE.MeshStandardMaterial({ color: 0xe8c47a, metalness: 0.8, roughness: 0.3 });
391 const radius = (i: number) => 0.0042 - 0.0005 * i;
392 const recorded = new Set(list.map((s) => s.string));
393 for (let i = 0; i < 4; i++) {
394 const rest = this.notchPos(i);
395 const nut = new THREE.Vector3(this.xNut, this.zNotch, -this.notchY[i] * this.nutFactor);
396 const dir = nut.clone().sub(rest).normalize();
397 const setup = list.find((s) => s.string === i) ?? { string: i, length: this.xNut - this.xb, points: 2, played: false };
398 if (recorded.has(i)) {
399 const P = setup.points;
400 const geo = new THREE.BufferGeometry();
401 geo.setAttribute("position", new THREE.BufferAttribute(new Float32Array(P * R * 3), 3));
402 const idx: number[] = [];
403 for (let j = 0; j + 1 < P; j++) for (let r = 0; r < R; r++) { const a = j * R + r, b = j * R + ((r + 1) % R); idx.push(a, a + R, b + R, a, b + R, b); }
404 geo.setIndex(idx);
405 const mesh = new THREE.Mesh(geo, setup.played ? metalPlayed : metal);
406 this.root.add(mesh);
407 this.strings.push({ setup, mesh, rest, dir, radius: radius(i) });
408 }
409 // static parts: bridge -> tailpiece, and stop -> nut (for the stopped played string, or every non-recorded string from the bridge)
410 const tail = new THREE.Vector3(this.staticStrings.userData.tailX, this.staticStrings.userData.tailZ, -this.notchY[i] * 0.7);
411 const segs: [THREE.Vector3, THREE.Vector3][] = [[rest, tail]];
412 const L = recorded.has(i) ? setup.length : 0;
413 const stopPt = rest.clone().addScaledVector(dir, L);
414 if (L < nut.distanceTo(rest) - 1e-3) segs.push([stopPt, nut]);
415 for (const [a, b] of segs) {
416 const len = a.distanceTo(b);
417 const cyl = new THREE.Mesh(new THREE.CylinderGeometry(radius(i), radius(i), len, 8), metal);
418 cyl.position.copy(a.clone().add(b).multiplyScalar(0.5));
419 cyl.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), b.clone().sub(a).normalize());
420 this.staticStrings.add(cyl);
421 }
422 }
423 const ps = this.strings.find((s) => s.setup.played);
424 if (played.stopped && ps) {
425 const pt = ps.rest.clone().addScaledVector(ps.dir, played.length + 0.006);
426 this.stopMesh.position.copy(pt).add(new THREE.Vector3(0, 0.012, 0));
427 this.stopMesh.visible = true;
428 } else this.stopMesh.visible = false;
429 this.setLayers({});
430 }
432 private notchPos(i: number): THREE.Vector3 { return new THREE.Vector3(this.xb, this.zNotch, -this.notchY[i]); }
434 update(f: Frame, sc: Scales): void {
435 // plates
436 for (const [pm, field] of [[this.top, f.top], [this.back, f.back]] as const) {
437 if (!pm) continue;
438 const pos = pm.mesh.geometry.getAttribute("position") as THREE.BufferAttribute;
439 const col = pm.mesh.geometry.getAttribute("color") as THREE.BufferAttribute;
440 const P = pos.array as Float32Array, C = col.array as Float32Array;
441 for (let v = 0; v < pm.n; v++) {
442 const a = pm.src[v];
443 const w = field && a >= 0 ? field[a] : 0;
444 P[3 * v + 1] = pm.rest[3 * v + 1] + sc.body * w;
445 diverge(C, 3 * v, w / sc.plate, WOOD);
446 }
447 pos.needsUpdate = true; col.needsUpdate = true;
448 pm.mesh.geometry.computeVertexNormals();
449 }
450 // air
451 if (this.airMesh && this.airCols && f.air) {
452 const col = this.airMesh.geometry.getAttribute("color") as THREE.BufferAttribute;
453 const C = col.array as Float32Array;
454 for (let v = 0; v < this.airCols.length; v++) diverge(C, 3 * v, f.air[this.airCols[v]] / sc.air, AIR0);
455 col.needsUpdate = true;
456 }
457 // f-hole plugs: arrows along the outward flow, length ~ displacement (clamped)
458 this.plugArrows.forEach((a, q) => {
459 const d = f.plug ? f.plug[q] * sc.body : 0;
460 const len = Math.min(0.25, Math.abs(d));
461 a.setDirection(new THREE.Vector3(0, d >= 0 ? 1 : -1, 0));
462 a.setLength(Math.max(1e-4, len), Math.min(0.03, 0.4 * len + 1e-4), Math.min(0.025, 0.3 * len + 1e-4));
463 a.visible = this.layers.plugs && len > 2e-3;
464 });
465 // bridge: line fit of the notch verticals across the strings, mean lateral
466 const ny = this.notchY;
467 let sy = 0, sty = 0, ss = 0, sz = 0;
468 for (let i = 0; i < 4; i++) { sy += f.notch[i]; sty += f.notch[i] * ny[i]; ss += ny[i] * ny[i]; sz += f.notch[4 + i]; }
469 const bV = sy / 4, bT = sty / ss, bZ = sz / 4;
470 {
471 const pos = this.bridge.geometry.getAttribute("position") as THREE.BufferAttribute;
472 const P = pos.array as Float32Array, R = this.bridgeRest;
473 const feet = [this.bridgeFoot[0], this.bridgeFoot[1]].map((a) => (f.top && a >= 0 ? f.top[a] : bV));
474 for (let v = 0; v < 8; v++) {
475 const s = -R[3 * v + 2], topV = v >= 4;
476 P[3 * v] = R[3 * v];
477 if (topV) { P[3 * v + 1] = R[3 * v + 1] + sc.body * (bV + bT * s); P[3 * v + 2] = R[3 * v + 2] - sc.body * bZ; }
478 else { P[3 * v + 1] = R[3 * v + 1] + sc.body * (s < 0 ? feet[0] : feet[1]); P[3 * v + 2] = R[3 * v + 2]; }
479 }
480 pos.needsUpdate = true;
481 this.bridge.geometry.computeVertexNormals();
482 }
483 // strings: string part exaggerated by sc.string, the bridge end (w x / L) by sc.body
484 const R = 8;
485 const V = new THREE.Vector3(0, 1, 0), W = new THREE.Vector3(0, 0, -1);
486 for (const st of this.strings) {
487 const fr = f.strings.find((s) => s.string === st.setup.string);
488 const P = st.setup.points, L = st.setup.length;
489 const pos = st.mesh.geometry.getAttribute("position") as THREE.BufferAttribute;
490 const A = pos.array as Float32Array;
491 const wy = fr ? fr.y[P - 1] : 0, wz = fr ? fr.z[P - 1] : 0;
492 for (let j = 0; j < P; j++) {
493 const x = (j / (P - 1)) * L, xi = x / L;
494 const uy = fr ? fr.y[j] : 0, uz = fr ? fr.z[j] : 0;
495 const dy = sc.string * (uy - wy * xi) + sc.body * wy * xi;
496 const dz = sc.string * (uz - wz * xi) + sc.body * wz * xi;
497 const cx = st.rest.x + st.dir.x * (L - x), cy = st.rest.y + st.dir.y * (L - x) + dy, cz = st.rest.z + st.dir.z * (L - x) - dz;
498 for (let r = 0; r < R; r++) {
499 const th = (2 * Math.PI * r) / R, c = Math.cos(th) * st.radius, s = Math.sin(th) * st.radius;
500 const o = 3 * (j * R + r);
501 A[o] = cx + c * V.x + s * W.x; A[o + 1] = cy + c * V.y + s * W.y; A[o + 2] = cz + c * V.z + s * W.z;
502 }
503 }
504 pos.needsUpdate = true;
505 st.mesh.geometry.computeVertexNormals();
506 }
507 // plucking finger: beside the string on the side away from the pull, moving with the pad
508 // (string part exaggerated like the string, plus the bridge-end part like the body)
509 const ps = this.strings.find((s) => s.setup.played);
510 if (f.finger && this.played && ps && f.finger.alpha > 0) {
511 const pl = this.played, L = pl.length, xi = pl.pluckX / L;
512 const fr = f.strings.find((s) => s.string === pl.string);
513 const P = ps.setup.points;
514 const wy = fr ? fr.y[P - 1] : 0, wz = fr ? fr.z[P - 1] : 0;
515 const base = ps.rest.clone().addScaledVector(ps.dir, L - pl.pluckX).addScaledVector(V, sc.body * wy * xi).addScaledVector(W, sc.body * wz * xi);
516 const dDisp = new THREE.Vector3().addScaledVector(V, pl.dirY).addScaledVector(W, pl.dirZ).normalize();
517 this.fingerMesh.position.copy(base).addScaledVector(dDisp, sc.string * f.finger.travel - (0.009 + ps.radius));
518 (this.fingerMesh.material as THREE.MeshStandardMaterial).opacity = f.finger.alpha;
519 this.fingerMesh.visible = true;
520 } else this.fingerMesh.visible = false;
521 this.needsRender = true;
522 }
523}