// The 3D view of the bass: plates (coloured and displaced by their normal motion), ribs, the air // cavity slice (coloured by pressure), the f-hole air plugs (arrows), the bridge, the strings, the // plucking and stopping fingers, and the static neck / fingerboard / tailpiece. // // Body coordinates (x along the body toward the neck, s across toward the treble side, z out of // the top, back plate at z = 0) map to three.js as X = x, Y = z, Z = -s. Displacements are drawn // exaggerated: the strings by `string`, the body (plates, bridge, the bridge end of the strings) // by `body`. import * as THREE from "three"; import { OrbitControls } from "three/addons/controls/OrbitControls.js"; import type { Params } from "../../core/params.ts"; import { makeArch, makeBodyGrid, makeFHoles, makeOutline, markFHoles, HOLE, IN, type Outline } from "../../fe/geometry.ts"; import type { FEShapes } from "../../fe/shapes.ts"; export interface StringFrame { string: number; length: number; y: Float32Array; z: Float32Array } export interface Frame { /** per top / back plate node (m), per air column (Pa), per f-hole plug (m); null = not shown */ top: Float32Array | null; back: Float32Array | null; air: Float32Array | null; plug: Float32Array | null; /** notch displacement: y of strings 0..3, z of strings 0..3 (m) */ notch: ArrayLike; strings: StringFrame[]; /** the plucking finger: pad travel along the pull direction (m, real), visibility 0..1 */ finger: { travel: number; alpha: number } | null; } export interface Scales { /** display exaggeration of string and body motion */ string: number; body: number; /** displacement (m) and pressure (Pa) shown at full colour */ plate: number; air: number; } export interface StringSetup { string: number; length: number; points: number; played: boolean; } export interface PlayedSetup { string: number; length: number; stopped: boolean; /** pluck point, m from the stop; pull direction (y, z) */ pluckX: number; dirY: number; dirZ: number; } export interface Layers { top: boolean; topOpacity: number; back: boolean; air: boolean; ribs: boolean; plugs: boolean; symp: boolean } const WOOD = new THREE.Color(0.80, 0.62, 0.40); const WOOD_DARK = new THREE.Color(0.45, 0.27, 0.13); const POS = new THREE.Color(0.86, 0.16, 0.10); const NEG = new THREE.Color(0.12, 0.36, 0.86); const AIR0 = new THREE.Color(0.93, 0.93, 0.90); const EBONY = 0x1c1a18; /** Diverging colour: v in [-1, 1] from NEG through base to POS. */ function diverge(out: Float32Array, o: number, v: number, base: THREE.Color): void { const a = Math.min(1, Math.abs(v)), c = v >= 0 ? POS : NEG; out[o] = base.r + (c.r - base.r) * a; out[o + 1] = base.g + (c.g - base.g) * a; out[o + 2] = base.b + (c.b - base.b) * a; } interface PlateMesh { mesh: THREE.Mesh; src: Int32Array; rest: Float32Array; n: number } export class BassScene { readonly renderer: THREE.WebGLRenderer; private scene = new THREE.Scene(); private camera: THREE.PerspectiveCamera; private controls: OrbitControls; private root = new THREE.Group(); private outline: Outline; private topZ: (x: number, s: number) => number; private xb: number; private notchY: number[]; private zNotch: number; private xNut: number; private nutFactor = 0.42; private top: PlateMesh | null = null; private back: PlateMesh | null = null; private airMesh: THREE.Mesh | null = null; private airCols: Int32Array | null = null; // per air-quad vertex: column private ribs: THREE.Mesh; private plugArrows: THREE.ArrowHelper[] = []; private plugInfo: { x: number; s: number; z: number }[] = []; private bridge: THREE.Mesh; private bridgeRest: Float32Array; private bridgeFoot: [number, number] = [-1, -1]; // top-plate node index under each foot private strings: { setup: StringSetup; mesh: THREE.Mesh; rest: THREE.Vector3; dir: THREE.Vector3; radius: number }[] = []; private staticStrings = new THREE.Group(); private played: PlayedSetup | null = null; private fingerMesh: THREE.Mesh; private stopMesh: THREE.Mesh; private resizeObs: ResizeObserver; private layers: Layers = { top: true, topOpacity: 1, back: true, air: false, ribs: true, plugs: true, symp: true }; private needsRender = true; private container: HTMLElement; constructor(container: HTMLElement, p: Params, shapes: FEShapes | null) { this.container = container; const fe = p.fe; this.renderer = new THREE.WebGLRenderer({ antialias: true }); this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio || 1)); container.append(this.renderer.domElement); const bg = getComputedStyle(document.documentElement).getPropertyValue("--panel").trim() || "#ffffff"; this.scene.background = new THREE.Color(bg); this.camera = new THREE.PerspectiveCamera(35, 1, 0.01, 50); this.controls = new OrbitControls(this.camera, this.renderer.domElement); this.controls.enableDamping = true; this.controls.addEventListener("change", () => { this.needsRender = true; }); this.scene.add(new THREE.HemisphereLight(0xffffff, 0x444444, 1.6)); const sun = new THREE.DirectionalLight(0xffffff, 1.6); sun.position.set(-1, 3, 2); this.scene.add(sun); this.scene.add(this.root); this.outline = makeOutline(fe); const arch = makeArch(fe, this.outline); this.topZ = (x, s) => fe.ribDepth + arch(x, s); this.xb = fe.bridgePos * fe.bodyLength; this.notchY = [0, 1, 2, 3].map((i) => (i - 1.5) * fe.stringSpacing); this.zNotch = this.topZ(this.xb, 0) + fe.bridgeHeight; this.xNut = this.xb + p.strings.scaleLength; // ---- plates let grid: FEShapes["grid"], topNodes: Int32Array, backNodes: Int32Array; if (shapes) ({ grid, topNodes, backNodes } = shapes); else { const g = makeBodyGrid(fe, this.outline, arch, fe.gridH); markFHoles(g, makeFHoles(fe)); grid = { nx: g.nx, ns: g.ns, h: g.h, x0: g.x0, s0: g.s0 }; const t: number[] = [], b: number[] = []; 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); } topNodes = Int32Array.from(t); backNodes = Int32Array.from(b); } const topSet = new Set(topNodes), holes = new Set([...backNodes].filter((k) => !topSet.has(k))); this.top = this.plate(grid, topNodes, holes, (x, s) => this.topZ(x, s)); this.back = this.plate(grid, backNodes, new Set(), () => 0); this.root.add(this.top.mesh, this.back.mesh); // the nodes under the bridge feet for (const [q, sf] of [[0, -fe.footSpan / 2], [1, fe.footSpan / 2]] as const) { let best = -1, bd = Infinity; topNodes.forEach((k, a) => { const x = grid.x0 + Math.floor(k / grid.ns) * grid.h, s = grid.s0 + (k % grid.ns) * grid.h; const d = (x - this.xb) ** 2 + (s - sf) ** 2; if (d < bd) { bd = d; best = a; } }); this.bridgeFoot[q] = best; } // ---- ribs this.ribs = this.makeRibs(fe.ribDepth); this.root.add(this.ribs); // ---- air slice and plugs if (shapes) { this.airMesh = this.makeAir(shapes); this.root.add(this.airMesh); for (const pl of shapes.plugs) { const z = this.topZ(pl.xc, pl.sc); this.plugInfo.push({ x: pl.xc, s: pl.sc, z }); 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); this.plugArrows.push(a); this.root.add(a); } } // ---- bridge (tapered slab across the strings) { const wB = fe.footSpan + 0.05, wT = 3 * fe.stringSpacing + 0.035, th = 0.022; const z0 = this.topZ(this.xb, 0), z1 = this.zNotch; const v: number[] = []; for (const [z, w] of [[z0, wB], [z1, wT]] as const) for (const s of [-w / 2, w / 2]) for (const dx of [-th / 2, th / 2]) v.push(this.xb + dx, z, -s); const geo = new THREE.BufferGeometry(); this.bridgeRest = Float32Array.from(v); geo.setAttribute("position", new THREE.BufferAttribute(Float32Array.from(v), 3)); // vertices: bottom (0..3), top (4..7); within each: s-, dx- / dx+, s+, dx- / dx+ 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]); geo.computeVertexNormals(); this.bridge = new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ color: 0xd9b98a, roughness: 0.8, side: THREE.DoubleSide, flatShading: true })); this.root.add(this.bridge); } // ---- neck, fingerboard, tailpiece, end pin (static) const ebony = new THREE.MeshStandardMaterial({ color: EBONY, roughness: 0.6 }); { const xEnd = Math.max(fe.bodyLength * 0.72, this.xb + 0.3); const len = this.xNut - xEnd; const zTopEnd = this.zNotch - 0.022, zTopNut = this.zNotch - 0.006; const board = new THREE.Mesh(new THREE.BoxGeometry(len, 0.02, 0.1), ebony); board.position.set((xEnd + this.xNut) / 2, (zTopEnd + zTopNut) / 2 - 0.01, 0); board.rotation.z = Math.atan2(zTopNut - zTopEnd, len); board.scale.set(1, 1, 1); this.root.add(board); 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 })); neck.position.set((fe.bodyLength + this.xNut) / 2, zTopNut - 0.045, 0); this.root.add(neck); const scroll = new THREE.Mesh(new THREE.BoxGeometry(0.22, 0.07, 0.08), new THREE.MeshStandardMaterial({ color: WOOD_DARK, roughness: 0.7 })); scroll.position.set(this.xNut + 0.12, zTopNut - 0.02, 0); this.root.add(scroll); const tail = new THREE.Mesh(new THREE.BoxGeometry(0.28, 0.015, 0.09), ebony); const xTail0 = 0.06, xTail1 = xTail0 + 0.28; tail.position.set((xTail0 + xTail1) / 2, this.topZ(0.2, 0) + 0.05, 0); this.root.add(tail); // afterlengths (bridge to tailpiece) and the strings behind the stop are drawn by setupStrings this.staticStrings.userData.tailX = xTail1; this.staticStrings.userData.tailZ = this.topZ(0.2, 0) + 0.06; this.root.add(this.staticStrings); } // ---- fingers const skin = new THREE.MeshStandardMaterial({ color: 0xe0a98a, roughness: 0.55, transparent: true }); this.fingerMesh = new THREE.Mesh(new THREE.CapsuleGeometry(0.009, 0.05, 6, 12), skin); this.stopMesh = new THREE.Mesh(new THREE.CapsuleGeometry(0.009, 0.04, 6, 12), skin.clone()); this.stopMesh.rotation.x = Math.PI / 2; this.root.add(this.fingerMesh, this.stopMesh); this.fingerMesh.visible = false; this.stopMesh.visible = false; // ---- camera this.root.position.set(-(this.xNut + 0.1) / 2, -0.15, 0); this.view("overview"); this.resizeObs = new ResizeObserver(() => this.resize()); this.resizeObs.observe(container); this.resize(); const loop = () => { if (!this.renderer.domElement.isConnected) return; this.controls.update(); if (this.needsRender) { this.renderer.render(this.scene, this.camera); this.needsRender = false; } requestAnimationFrame(loop); }; requestAnimationFrame(loop); } dispose(): void { this.resizeObs.disconnect(); this.renderer.dispose(); this.renderer.domElement.remove(); } private resize(): void { const w = this.container.clientWidth, h = this.container.clientHeight; if (!(w > 0 && h > 0)) return; this.renderer.setSize(w, h, false); this.camera.aspect = w / h; this.camera.updateProjectionMatrix(); this.needsRender = true; } /** Preset views. */ view(which: "overview" | "top" | "side" | "bridge" | "string" | "below"): void { const c = this.root.position; const at = (x: number, y: number, z: number) => new THREE.Vector3(x + c.x, y + c.y, z + c.z); const xs = this.played ? this.xb + (this.played.length - this.played.pluckX) : this.xb + 0.2; const presets = { overview: [new THREE.Vector3(-1.35, 1.05, 1.0), new THREE.Vector3(-0.1, 0.1, 0)], top: [at(this.xNut / 2, 2.6, 0.001), at(this.xNut / 2, 0, 0)], side: [at(this.xNut / 2, 0.25, 2.2), at(this.xNut / 2, 0.2, 0)], bridge: [at(this.xb - 0.25, this.zNotch + 0.25, 0.35), at(this.xb, this.zNotch - 0.05, 0)], string: [at(xs - 0.12, this.zNotch + 0.32, 0.1), at(xs + 0.02, this.zNotch, 0)], below: [at(this.xNut / 2 - 0.2, -1.6, 1.0), at(this.xNut / 2 - 0.2, 0, 0)], } as const; const [pos, tgt] = presets[which]; this.camera.position.copy(pos); this.controls.target.copy(tgt); this.controls.update(); this.needsRender = true; } setLayers(l: Partial): void { Object.assign(this.layers, l); const L = this.layers; if (this.top) { this.top.mesh.visible = L.top && L.topOpacity > 0.02; const m = this.top.mesh.material as THREE.MeshStandardMaterial; m.transparent = L.topOpacity < 0.99; m.opacity = L.topOpacity; m.depthWrite = L.topOpacity >= 0.99; } if (this.back) this.back.mesh.visible = L.back; if (this.airMesh) this.airMesh.visible = L.air; this.ribs.visible = L.ribs; for (const a of this.plugArrows) a.visible = L.plugs; for (const s of this.strings) if (!s.setup.played) s.mesh.visible = L.symp; this.needsRender = true; } private plate(grid: FEShapes["grid"], nodes: Int32Array, holes: Set, zf: (x: number, s: number) => number): PlateMesh { const { nx, ns, h, x0, s0 } = grid; const srcOf = new Map(); nodes.forEach((k, a) => srcOf.set(k, a)); const vid = new Map(); const src: number[] = [], rest: number[] = [], idx: number[] = []; const vert = (k: number) => { let v = vid.get(k); if (v !== undefined) return v; const i = Math.floor(k / ns), j = k % ns; let x = x0 + i * h, s = s0 + j * h; const a = srcOf.get(k); if (a === undefined) { // a constrained node: pull it onto the rim so the plate edge follows the outline x = Math.min(this.outline.L, Math.max(0, x)); const hw = this.outline.halfWidth(x); s = Math.max(-hw, Math.min(hw, s)); } v = src.length; vid.set(k, v); src.push(a ?? -1); rest.push(x, zf(x, s), -s); return v; }; for (let i = 0; i + 1 < nx; i++) for (let j = 0; j + 1 < ns; j++) { const ks = [i * ns + j, (i + 1) * ns + j, (i + 1) * ns + j + 1, i * ns + j + 1]; if (!ks.some((k) => srcOf.has(k)) || ks.some((k) => holes.has(k))) continue; const [a, b, c, d] = ks.map(vert); idx.push(a, b, c, a, c, d); } const geo = new THREE.BufferGeometry(); const restA = Float32Array.from(rest); geo.setAttribute("position", new THREE.BufferAttribute(Float32Array.from(restA), 3)); const col = new Float32Array(src.length * 3); for (let v = 0; v < src.length; v++) diverge(col, 3 * v, 0, WOOD); geo.setAttribute("color", new THREE.BufferAttribute(col, 3)); geo.setIndex(idx); geo.computeVertexNormals(); const mesh = new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.75, side: THREE.DoubleSide })); return { mesh, src: Int32Array.from(src), rest: restA, n: src.length }; } private makeRibs(depth: number): THREE.Mesh { const { px, ps } = this.outline; const n = px.length, step = Math.max(1, Math.floor(n / 240)); const pts: [number, number][] = []; for (let k = 0; k < n; k += step) pts.push([px[k], ps[k]]); const pos: number[] = [], idx: number[] = []; pts.forEach(([x, s]) => { pos.push(x, 0, -s, x, depth, -s); }); const m = pts.length; 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); } const geo = new THREE.BufferGeometry(); geo.setAttribute("position", new THREE.BufferAttribute(Float32Array.from(pos), 3)); geo.setIndex(idx); geo.computeVertexNormals(); return new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ color: WOOD_DARK, roughness: 0.8, side: THREE.DoubleSide, transparent: true, opacity: 0.45, depthWrite: false })); } private makeAir(sh: FEShapes): THREE.Mesh { const { nxA, nsA, x0, s0, ha, col, colH } = sh.cav; const pos: number[] = [], cols: number[] = [], idx: number[] = []; for (let i = 0; i < nxA; i++) for (let j = 0; j < nsA; j++) { const c = col[i * nsA + j]; if (c < 0) continue; const y = colH[c] / 2, v = pos.length / 3; 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); } idx.push(v, v + 1, v + 2, v, v + 2, v + 3); } this.airCols = Int32Array.from(cols); const geo = new THREE.BufferGeometry(); geo.setAttribute("position", new THREE.BufferAttribute(Float32Array.from(pos), 3)); const c = new Float32Array(cols.length * 3); for (let v = 0; v < cols.length; v++) diverge(c, 3 * v, 0, AIR0); geo.setAttribute("color", new THREE.BufferAttribute(c, 3)); geo.setIndex(idx); geo.computeVertexNormals(); const mesh = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({ vertexColors: true, side: THREE.DoubleSide })); mesh.visible = false; return mesh; } /** The strings to draw: the recorded ones (vibrating parts) plus static afterlengths and the part behind the stop. */ setupStrings(list: StringSetup[], played: PlayedSetup): void { for (const s of this.strings) { this.root.remove(s.mesh); s.mesh.geometry.dispose(); } this.strings = []; this.staticStrings.clear(); this.played = played; const R = 8; const metal = new THREE.MeshStandardMaterial({ color: 0xb8b4ac, metalness: 0.85, roughness: 0.3 }); const metalPlayed = new THREE.MeshStandardMaterial({ color: 0xe8c47a, metalness: 0.8, roughness: 0.3 }); const radius = (i: number) => 0.0042 - 0.0005 * i; const recorded = new Set(list.map((s) => s.string)); for (let i = 0; i < 4; i++) { const rest = this.notchPos(i); const nut = new THREE.Vector3(this.xNut, this.zNotch, -this.notchY[i] * this.nutFactor); const dir = nut.clone().sub(rest).normalize(); const setup = list.find((s) => s.string === i) ?? { string: i, length: this.xNut - this.xb, points: 2, played: false }; if (recorded.has(i)) { const P = setup.points; const geo = new THREE.BufferGeometry(); geo.setAttribute("position", new THREE.BufferAttribute(new Float32Array(P * R * 3), 3)); const idx: number[] = []; 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); } geo.setIndex(idx); const mesh = new THREE.Mesh(geo, setup.played ? metalPlayed : metal); this.root.add(mesh); this.strings.push({ setup, mesh, rest, dir, radius: radius(i) }); } // static parts: bridge -> tailpiece, and stop -> nut (for the stopped played string, or every non-recorded string from the bridge) const tail = new THREE.Vector3(this.staticStrings.userData.tailX, this.staticStrings.userData.tailZ, -this.notchY[i] * 0.7); const segs: [THREE.Vector3, THREE.Vector3][] = [[rest, tail]]; const L = recorded.has(i) ? setup.length : 0; const stopPt = rest.clone().addScaledVector(dir, L); if (L < nut.distanceTo(rest) - 1e-3) segs.push([stopPt, nut]); for (const [a, b] of segs) { const len = a.distanceTo(b); const cyl = new THREE.Mesh(new THREE.CylinderGeometry(radius(i), radius(i), len, 8), metal); cyl.position.copy(a.clone().add(b).multiplyScalar(0.5)); cyl.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), b.clone().sub(a).normalize()); this.staticStrings.add(cyl); } } const ps = this.strings.find((s) => s.setup.played); if (played.stopped && ps) { const pt = ps.rest.clone().addScaledVector(ps.dir, played.length + 0.006); this.stopMesh.position.copy(pt).add(new THREE.Vector3(0, 0.012, 0)); this.stopMesh.visible = true; } else this.stopMesh.visible = false; this.setLayers({}); } private notchPos(i: number): THREE.Vector3 { return new THREE.Vector3(this.xb, this.zNotch, -this.notchY[i]); } update(f: Frame, sc: Scales): void { // plates for (const [pm, field] of [[this.top, f.top], [this.back, f.back]] as const) { if (!pm) continue; const pos = pm.mesh.geometry.getAttribute("position") as THREE.BufferAttribute; const col = pm.mesh.geometry.getAttribute("color") as THREE.BufferAttribute; const P = pos.array as Float32Array, C = col.array as Float32Array; for (let v = 0; v < pm.n; v++) { const a = pm.src[v]; const w = field && a >= 0 ? field[a] : 0; P[3 * v + 1] = pm.rest[3 * v + 1] + sc.body * w; diverge(C, 3 * v, w / sc.plate, WOOD); } pos.needsUpdate = true; col.needsUpdate = true; pm.mesh.geometry.computeVertexNormals(); } // air if (this.airMesh && this.airCols && f.air) { const col = this.airMesh.geometry.getAttribute("color") as THREE.BufferAttribute; const C = col.array as Float32Array; for (let v = 0; v < this.airCols.length; v++) diverge(C, 3 * v, f.air[this.airCols[v]] / sc.air, AIR0); col.needsUpdate = true; } // f-hole plugs: arrows along the outward flow, length ~ displacement (clamped) this.plugArrows.forEach((a, q) => { const d = f.plug ? f.plug[q] * sc.body : 0; const len = Math.min(0.25, Math.abs(d)); a.setDirection(new THREE.Vector3(0, d >= 0 ? 1 : -1, 0)); 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)); a.visible = this.layers.plugs && len > 2e-3; }); // bridge: line fit of the notch verticals across the strings, mean lateral const ny = this.notchY; let sy = 0, sty = 0, ss = 0, sz = 0; 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]; } const bV = sy / 4, bT = sty / ss, bZ = sz / 4; { const pos = this.bridge.geometry.getAttribute("position") as THREE.BufferAttribute; const P = pos.array as Float32Array, R = this.bridgeRest; const feet = [this.bridgeFoot[0], this.bridgeFoot[1]].map((a) => (f.top && a >= 0 ? f.top[a] : bV)); for (let v = 0; v < 8; v++) { const s = -R[3 * v + 2], topV = v >= 4; P[3 * v] = R[3 * v]; 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; } 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]; } } pos.needsUpdate = true; this.bridge.geometry.computeVertexNormals(); } // strings: string part exaggerated by sc.string, the bridge end (w x / L) by sc.body const R = 8; const V = new THREE.Vector3(0, 1, 0), W = new THREE.Vector3(0, 0, -1); for (const st of this.strings) { const fr = f.strings.find((s) => s.string === st.setup.string); const P = st.setup.points, L = st.setup.length; const pos = st.mesh.geometry.getAttribute("position") as THREE.BufferAttribute; const A = pos.array as Float32Array; const wy = fr ? fr.y[P - 1] : 0, wz = fr ? fr.z[P - 1] : 0; for (let j = 0; j < P; j++) { const x = (j / (P - 1)) * L, xi = x / L; const uy = fr ? fr.y[j] : 0, uz = fr ? fr.z[j] : 0; const dy = sc.string * (uy - wy * xi) + sc.body * wy * xi; const dz = sc.string * (uz - wz * xi) + sc.body * wz * xi; 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; for (let r = 0; r < R; r++) { const th = (2 * Math.PI * r) / R, c = Math.cos(th) * st.radius, s = Math.sin(th) * st.radius; const o = 3 * (j * R + r); 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; } } pos.needsUpdate = true; st.mesh.geometry.computeVertexNormals(); } // plucking finger: beside the string on the side away from the pull, moving with the pad // (string part exaggerated like the string, plus the bridge-end part like the body) const ps = this.strings.find((s) => s.setup.played); if (f.finger && this.played && ps && f.finger.alpha > 0) { const pl = this.played, L = pl.length, xi = pl.pluckX / L; const fr = f.strings.find((s) => s.string === pl.string); const P = ps.setup.points; const wy = fr ? fr.y[P - 1] : 0, wz = fr ? fr.z[P - 1] : 0; const base = ps.rest.clone().addScaledVector(ps.dir, L - pl.pluckX).addScaledVector(V, sc.body * wy * xi).addScaledVector(W, sc.body * wz * xi); const dDisp = new THREE.Vector3().addScaledVector(V, pl.dirY).addScaledVector(W, pl.dirZ).normalize(); this.fingerMesh.position.copy(base).addScaledVector(dDisp, sc.string * f.finger.travel - (0.009 + ps.radius)); (this.fingerMesh.material as THREE.MeshStandardMaterial).opacity = f.finger.alpha; this.fingerMesh.visible = true; } else this.fingerMesh.visible = false; this.needsRender = true; } }