7b06bdfWoodblock strike simulator: modal model, FDTD mode analysis, SoundFont exportJeremy Magland 1// three.js view of the block: click the top face to set the strike point.
3import * as THREE from 'three';
4import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
5import { blockBoxes, micPos } from './block.js';
7const S = 0.01; // scene units per mm
9function woodTexture() {
10 const c = document.createElement('canvas');
11 c.width = 512; c.height = 128;
12 const g = c.getContext('2d');
13 g.fillStyle = '#b5743c';
14 g.fillRect(0, 0, c.width, c.height);
15 for (let y = 0; y < c.height; y++) {
16 const v = Math.sin(y * 0.35 + Math.sin(y * 0.05) * 3) * 0.5 + 0.5;
17 g.fillStyle = `rgba(90,45,15,${0.08 + 0.22 * v * v})`;
18 g.fillRect(0, y, c.width, 1);
19 }
20 for (let i = 0; i < 60; i++) {
21 g.strokeStyle = `rgba(70,35,10,${0.05 + Math.random() * 0.12})`;
22 g.beginPath();
23 const y = Math.random() * c.height;
24 g.moveTo(0, y);
25 for (let x = 0; x <= c.width; x += 32) g.lineTo(x, y + Math.sin(x * 0.01 + i) * 3);
26 g.stroke();
27 }
28 const t = new THREE.CanvasTexture(c);
29 t.colorSpace = THREE.SRGBColorSpace;
30 t.wrapS = t.wrapT = THREE.RepeatWrapping;
31 return t;
32}
34export class BlockView {
35 constructor(container, onPick) {
36 this.container = container;
37 this.onPick = onPick;
38 const r = (this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }));
39 r.setPixelRatio(Math.min(2, window.devicePixelRatio));
40 container.appendChild(r.domElement);
41 this.scene = new THREE.Scene();
42 this.camera = new THREE.PerspectiveCamera(35, 1, 0.01, 100);
43 this.camera.position.set(1.6, 1.5, 3.0);
44 this.controls = new OrbitControls(this.camera, r.domElement);
45 this.controls.enableDamping = true;
46 this.scene.add(new THREE.HemisphereLight(0xffffff, 0x404050, 1.4));
47 const sun = new THREE.DirectionalLight(0xffffff, 2.2);
48 sun.position.set(2, 4, 3);
49 this.scene.add(sun);
50 this.tex = woodTexture();
51 this.group = new THREE.Group();
52 this.scene.add(this.group);
54 // mallet
55 this.mallet = new THREE.Group();
56 const head = new THREE.Mesh(new THREE.SphereGeometry(0.09, 24, 16), new THREE.MeshStandardMaterial({ color: 0x333a44, roughness: 0.5 }));
57 head.position.y = 0.09;
58 const stick = new THREE.Mesh(new THREE.CylinderGeometry(0.018, 0.018, 1.0, 12), new THREE.MeshStandardMaterial({ color: 0xd8c39a }));
59 stick.position.set(0.0, 0.2, 0.45);
60 stick.rotation.x = Math.PI / 2.6;
61 this.mallet.add(head, stick);
62 this.scene.add(this.mallet);
63 this.mic = new THREE.Mesh(new THREE.SphereGeometry(0.03, 16, 12), new THREE.MeshStandardMaterial({ color: 0x3d8bfd, emissive: 0x112244 }));
64 this.scene.add(this.mic);
65 this.hitT = -1;
67 const ray = new THREE.Raycaster();
68 let down = null;
69 r.domElement.addEventListener('pointerdown', (e) => (down = [e.clientX, e.clientY]));
70 r.domElement.addEventListener('pointerup', (e) => {
71 if (!down || Math.hypot(e.clientX - down[0], e.clientY - down[1]) > 4) return;
72 const rect = r.domElement.getBoundingClientRect();
73 const ndc = new THREE.Vector2(((e.clientX - rect.left) / rect.width) * 2 - 1, -((e.clientY - rect.top) / rect.height) * 2 + 1);
74 ray.setFromCamera(ndc, this.camera);
75 const hit = ray.intersectObject(this.topPick)[0];
76 if (hit) {
77 const p = this.toBlock(hit.point);
78 this.onPick(p[0], p[1]);
79 }
80 });
81 new ResizeObserver(() => this.resize()).observe(container);
82 this.resize();
83 const loop = () => {
84 requestAnimationFrame(loop);
85 this.animate();
86 this.controls.update();
87 r.render(this.scene, this.camera);
88 };
89 loop();
90 }
92 // block mm -> scene
93 toScene(x, y, z) {
94 const b = this.block;
95 return new THREE.Vector3((x - b.L / 2) * S, (z - b.H / 2) * S, (b.W / 2 - y) * S);
96 }
97 toBlock(v) {
98 const b = this.block;
99 return [v.x / S + b.L / 2, b.W / 2 - v.z / S, v.y / S + b.H / 2];
100 }
102 setBlock(b) {
103 this.block = b;
104 this.group.clear();
105 const mat = new THREE.MeshStandardMaterial({ map: this.tex, roughness: 0.65 });
106 for (const [x0, x1, y0, y1, z0, z1] of blockBoxes(b)) {
107 const geo = new THREE.BoxGeometry((x1 - x0) * S, (z1 - z0) * S, (y1 - y0) * S);
108 const m = new THREE.Mesh(geo, mat);
109 m.position.copy(this.toScene((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2));
110 this.group.add(m);
111 }
112 // dark cavity interior (slightly inset so it doesn't z-fight)
113 const e = 0.3;
114 const cav = new THREE.Mesh(
115 new THREE.BoxGeometry((b.L - 2 * b.wall - e) * S, (b.H - b.top - b.wall - e) * S, (b.W - 2 * b.wall - e) * S),
116 new THREE.MeshBasicMaterial({ color: 0x140a04, side: THREE.BackSide }),
117 );
118 cav.position.copy(this.toScene(b.L / 2, b.W / 2, (b.wall + b.H - b.top) / 2));
119 this.group.add(cav);
120 // invisible pick plane + mode overlay on top face
121 this.topPick = new THREE.Mesh(new THREE.PlaneGeometry(b.L * S, b.W * S), new THREE.MeshBasicMaterial({ visible: false }));
122 this.topPick.rotation.x = -Math.PI / 2;
123 this.topPick.position.copy(this.toScene(b.L / 2, b.W / 2, b.H + 0.2));
124 this.group.add(this.topPick);
125 this.overlay = new THREE.Mesh(
126 new THREE.PlaneGeometry(b.L * S, b.W * S),
127 new THREE.MeshBasicMaterial({ transparent: true, opacity: 0.85, depthWrite: false }),
128 );
129 this.overlay.rotation.x = -Math.PI / 2;
130 this.overlay.position.copy(this.toScene(b.L / 2, b.W / 2, b.H + 0.3));
131 this.overlay.visible = false;
132 this.group.add(this.overlay);
133 this.mic.position.copy(this.toScene(...micPos(b)));
134 if (this.strike) this.setStrike(...this.strike);
135 }
137 setStrike(x, y) {
138 this.strike = [x, y];
139 this.malletRest = this.toScene(x, y, this.block.H);
140 this.mallet.position.copy(this.malletRest).add(new THREE.Vector3(0, 0.12, 0));
141 }
143 hit() { this.hitT = performance.now(); }
145 animate() {
146 if (!this.malletRest) return;
147 const lift = 0.12;
148 let h = lift;
149 if (this.hitT > 0) {
150 const t = (performance.now() - this.hitT) / 1000;
151 h = t < 0.03 ? lift * (1 - t / 0.03) : t < 0.25 ? lift * Math.sin(((t - 0.03) / 0.22) * Math.PI / 2) : lift;
152 }
153 this.mallet.position.copy(this.malletRest).add(new THREE.Vector3(0, h, 0));
154 }
156 // values[nby][nbx] in [0,1] or null to hide
157 setOverlay(nbx, nby, values) {
158 if (!values) { this.overlay.visible = false; return; }
159 const c = document.createElement('canvas');
160 c.width = nbx; c.height = nby;
161 const g = c.getContext('2d');
162 const img = g.createImageData(nbx, nby);
163 for (let j = 0; j < nby; j++) {
164 for (let i = 0; i < nbx; i++) {
165 const v = values[j * nbx + i];
166 const o = ((nby - 1 - j) * nbx + i) * 4;
167 const [r, gg, b] = colormap(v);
168 img.data[o] = r; img.data[o + 1] = gg; img.data[o + 2] = b; img.data[o + 3] = 255;
169 }
170 }
171 g.putImageData(img, 0, 0);
172 const t = new THREE.CanvasTexture(c);
173 t.colorSpace = THREE.SRGBColorSpace;
174 t.magFilter = THREE.LinearFilter;
175 this.overlay.material.map?.dispose();
176 this.overlay.material.map = t;
177 this.overlay.material.needsUpdate = true;
178 this.overlay.visible = true;
179 }
181 resize() {
182 const w = this.container.clientWidth, h = this.container.clientHeight;
183 if (!w || !h) return;
184 this.renderer.setSize(w, h);
185 this.camera.aspect = w / h;
186 // back off on narrow screens so the whole block fits
187 const d = 3.75 * Math.max(1, 1.4 / this.camera.aspect);
188 this.camera.position.sub(this.controls.target).setLength(d).add(this.controls.target);
189 this.camera.updateProjectionMatrix();
190 }
191}
193// signed value in [-1,1] -> blue / white / red
194function colormap(v) {
195 const a = Math.min(1, Math.abs(v));
196 return v >= 0 ? [255, 255 * (1 - a), 255 * (1 - a)] : [255 * (1 - a), 255 * (1 - a), 255];
197}