// three.js view of one bar with its resonator tube: click the top face to strike. import * as THREE from 'three'; import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; import { thickness } from './bar.js'; const S = 0.004; // scene units per mm function rosewoodTexture() { const c = document.createElement('canvas'); c.width = 1024; c.height = 256; const g = c.getContext('2d'); g.fillStyle = '#6a2c1c'; g.fillRect(0, 0, c.width, c.height); for (let y = 0; y < c.height; y++) { const v = Math.sin(y * 0.21 + Math.sin(y * 0.031) * 4) * 0.5 + 0.5; g.fillStyle = `rgba(35,10,5,${0.06 + 0.3 * v * v * v})`; g.fillRect(0, y, c.width, 1); } for (let i = 0; i < 90; i++) { g.strokeStyle = `rgba(${i % 3 ? '25,8,4' : '150,80,50'},${0.05 + Math.random() * 0.12})`; g.lineWidth = 1 + Math.random() * 1.5; g.beginPath(); const y = Math.random() * c.height; g.moveTo(0, y); for (let x = 0; x <= c.width; x += 32) g.lineTo(x, y + Math.sin(x * 0.006 + i) * 5); g.stroke(); } const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; t.wrapS = t.wrapT = THREE.RepeatWrapping; return t; } export class BarView { constructor(container, onPick) { this.container = container; this.onPick = onPick; const r = (this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })); r.setPixelRatio(Math.min(2, window.devicePixelRatio)); container.appendChild(r.domElement); this.scene = new THREE.Scene(); this.camera = new THREE.PerspectiveCamera(35, 1, 0.01, 100); this.camera.position.set(0.9, 1.1, 2.4); this.controls = new OrbitControls(this.camera, r.domElement); this.controls.enableDamping = true; this.scene.add(new THREE.HemisphereLight(0xffffff, 0x404050, 1.3)); const sun = new THREE.DirectionalLight(0xffffff, 2.4); sun.position.set(2, 4, 3); this.scene.add(sun); const fill = new THREE.DirectionalLight(0xffffff, 0.6); fill.position.set(-3, -1, -2); this.scene.add(fill); this.tex = rosewoodTexture(); this.group = new THREE.Group(); this.scene.add(this.group); // mallet: yarn head on a rattan stick this.mallet = new THREE.Group(); const R = 17 * S; const head = new THREE.Mesh(new THREE.SphereGeometry(R, 24, 16), new THREE.MeshStandardMaterial({ color: 0x5b6f8f, roughness: 0.95 })); head.position.y = R; const stick = new THREE.Mesh(new THREE.CylinderGeometry(4 * S, 4 * S, 380 * S, 12), new THREE.MeshStandardMaterial({ color: 0xd8c39a })); stick.position.set(0, R + 80 * S, -175 * S); stick.rotation.x = -Math.PI / 2.6; this.mallet.add(head, stick); this.scene.add(this.mallet); this.mic = new THREE.Mesh(new THREE.SphereGeometry(9 * S, 16, 12), new THREE.MeshStandardMaterial({ color: 0x3d8bfd, emissive: 0x112244 })); this.scene.add(this.mic); this.hitT = -1; const ray = new THREE.Raycaster(); let down = null; r.domElement.addEventListener('pointerdown', (e) => (down = [e.clientX, e.clientY])); r.domElement.addEventListener('pointerup', (e) => { if (!down || Math.hypot(e.clientX - down[0], e.clientY - down[1]) > 4 || !this.topPick) return; const rect = r.domElement.getBoundingClientRect(); const ndc = new THREE.Vector2(((e.clientX - rect.left) / rect.width) * 2 - 1, -((e.clientY - rect.top) / rect.height) * 2 + 1); ray.setFromCamera(ndc, this.camera); const hit = ray.intersectObject(this.topPick)[0]; if (hit) { const p = this.toBar(hit.point); this.onPick(p[0], p[1]); } }); new ResizeObserver(() => this.resize()).observe(container); this.resize(); const loop = () => { requestAnimationFrame(loop); this.animate(); this.controls.update(); r.render(this.scene, this.camera); }; loop(); } // bar mm -> scene (bar centre at the origin) toScene(x, y, z) { const b = this.bar; return new THREE.Vector3((x - b.L / 2) * S, (z - b.h0 / 2) * S, (b.W / 2 - y) * S); } toBar(v) { const b = this.bar; return [v.x / S + b.L / 2, b.W / 2 - v.z / S, v.y / S + b.h0 / 2]; } /** * bar: design; tube: { a, L (mm), gap } or null; mic: [x,y,z] mm; nodes: x positions (mm) */ setScene(bar, tube, mic, nodes = []) { const first = !this.bar; const resized = !this.bar || Math.abs(this.bar.L - bar.L) > 1; this.bar = bar; this.group.clear(); // side profile in the x-z plane, extruded across the width const sh = new THREE.Shape(); const P = (x, z) => [(x - bar.L / 2) * S, (z - bar.h0 / 2) * S]; sh.moveTo(...P(0, bar.h0)); sh.lineTo(...P(bar.L, bar.h0)); const n = 160; for (let i = n; i >= 0; i--) { const x = (i / n) * bar.L; sh.lineTo(...P(x, bar.h0 - thickness(bar, x))); } sh.closePath(); const geo = new THREE.ExtrudeGeometry(sh, { depth: bar.W * S, bevelEnabled: false, curveSegments: 1 }); geo.translate(0, 0, (-bar.W * S) / 2); const tex = this.tex.clone(); tex.repeat.set(1.2, 4); tex.needsUpdate = true; const mesh = new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ map: tex, roughness: 0.45 })); this.group.add(mesh); // support cords at the f1 nodes const cordMat = new THREE.MeshStandardMaterial({ color: 0x222222, roughness: 0.9 }); for (const x of nodes) { const c = new THREE.Mesh(new THREE.CylinderGeometry(1.6 * S, 1.6 * S, (bar.W + 60) * S, 8), cordMat); c.rotation.x = Math.PI / 2; c.position.copy(this.toScene(x, bar.W / 2, -1.6)); this.group.add(c); } // resonator if (tube) { const metal = new THREE.MeshStandardMaterial({ color: 0xc9a75a, metalness: 0.85, roughness: 0.32, side: THREE.DoubleSide }); const pipe = new THREE.Mesh(new THREE.CylinderGeometry(tube.a * S, tube.a * S, tube.L * S, 48, 1, true), metal); pipe.position.copy(this.toScene(bar.L / 2, bar.W / 2, -tube.gap - tube.L / 2)); const inner = new THREE.Mesh( new THREE.CylinderGeometry(tube.a * S * 0.985, tube.a * S * 0.985, tube.L * S, 48, 1, true), new THREE.MeshStandardMaterial({ color: 0x1b140c, roughness: 1, side: THREE.BackSide }), ); inner.position.copy(pipe.position); const cap = new THREE.Mesh(new THREE.CircleGeometry(tube.a * S, 48), metal); cap.rotation.x = Math.PI / 2; cap.position.copy(this.toScene(bar.L / 2, bar.W / 2, -tube.gap - tube.L)); this.group.add(pipe, inner, cap); } // invisible pick plane + mode overlay on the top face this.topPick = new THREE.Mesh(new THREE.PlaneGeometry(bar.L * S, bar.W * S), new THREE.MeshBasicMaterial({ visible: false })); this.topPick.rotation.x = -Math.PI / 2; this.topPick.position.copy(this.toScene(bar.L / 2, bar.W / 2, bar.h0 + 0.2)); this.group.add(this.topPick); this.overlay = new THREE.Mesh( new THREE.PlaneGeometry(bar.L * S, bar.W * S), new THREE.MeshBasicMaterial({ transparent: true, opacity: 0.85, depthWrite: false }), ); this.overlay.rotation.x = -Math.PI / 2; this.overlay.position.copy(this.toScene(bar.L / 2, bar.W / 2, bar.h0 + 0.3)); this.overlay.visible = false; this.group.add(this.overlay); this.mic.position.copy(this.toScene(...mic)); if (this.strike) this.setStrike(...this.strike); if (resized) this.frame(first); } // keep the camera direction, set the distance so the bar fills the view frame(first) { const target = new THREE.Vector3(0, -40 * S, 0); const d = Math.max(0.8, this.bar.L * S * 2.0) * Math.max(1, 1.3 / this.camera.aspect); const dir = first ? new THREE.Vector3(0.3, 0.85, 1).normalize() : this.camera.position.clone().sub(this.controls.target).normalize(); this.controls.target.copy(target); this.camera.position.copy(target).addScaledVector(dir, d); } setStrike(x, y) { this.strike = [x, y]; if (!this.bar) return; this.malletRest = this.toScene(x, y, this.bar.h0); this.mallet.position.copy(this.malletRest).add(new THREE.Vector3(0, 60 * S, 0)); } hit() { this.hitT = performance.now(); } animate() { if (!this.malletRest) return; const lift = 60 * S; let h = lift; if (this.hitT > 0) { const t = (performance.now() - this.hitT) / 1000; h = t < 0.03 ? lift * (1 - t / 0.03) : t < 0.25 ? lift * Math.sin(((t - 0.03) / 0.22) * Math.PI / 2) : lift; } this.mallet.position.copy(this.malletRest).add(new THREE.Vector3(0, h, 0)); } // values[ny][nx] in [-1,1] (row j = y from the front edge), or null to hide setOverlay(nx, ny, values) { if (!values) { this.overlay.visible = false; return; } const c = document.createElement('canvas'); c.width = nx; c.height = ny; const g = c.getContext('2d'); const img = g.createImageData(nx, ny); for (let j = 0; j < ny; j++) { for (let i = 0; i < nx; i++) { const v = values[j * nx + i]; // canvas row 0 is the plane's far (back) edge, i.e. y = W const o = ((ny - 1 - j) * nx + i) * 4; const [r, gg, b] = colormap(v); img.data[o] = r; img.data[o + 1] = gg; img.data[o + 2] = b; img.data[o + 3] = 255; } } g.putImageData(img, 0, 0); const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; t.magFilter = THREE.LinearFilter; this.overlay.material.map?.dispose(); this.overlay.material.map = t; this.overlay.material.needsUpdate = true; this.overlay.visible = true; } resize() { const w = this.container.clientWidth, h = this.container.clientHeight; if (!w || !h) return; this.renderer.setSize(w, h); this.camera.aspect = w / h; this.camera.updateProjectionMatrix(); if (this.bar) this.frame(false); } } // signed value in [-1,1] -> blue / white / red function colormap(v) { const a = Math.min(1, Math.abs(v)); return v >= 0 ? [255, 255 * (1 - a), 255 * (1 - a)] : [255 * (1 - a), 255 * (1 - a), 255]; }