// three.js view of the block: click the top face to set the strike point. import * as THREE from 'three'; import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; import { blockBoxes, micPos } from './block.js'; const S = 0.01; // scene units per mm function woodTexture() { const c = document.createElement('canvas'); c.width = 512; c.height = 128; const g = c.getContext('2d'); g.fillStyle = '#b5743c'; g.fillRect(0, 0, c.width, c.height); for (let y = 0; y < c.height; y++) { const v = Math.sin(y * 0.35 + Math.sin(y * 0.05) * 3) * 0.5 + 0.5; g.fillStyle = `rgba(90,45,15,${0.08 + 0.22 * v * v})`; g.fillRect(0, y, c.width, 1); } for (let i = 0; i < 60; i++) { g.strokeStyle = `rgba(70,35,10,${0.05 + Math.random() * 0.12})`; 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.01 + i) * 3); g.stroke(); } const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; t.wrapS = t.wrapT = THREE.RepeatWrapping; return t; } export class BlockView { 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(1.6, 1.5, 3.0); this.controls = new OrbitControls(this.camera, r.domElement); this.controls.enableDamping = true; this.scene.add(new THREE.HemisphereLight(0xffffff, 0x404050, 1.4)); const sun = new THREE.DirectionalLight(0xffffff, 2.2); sun.position.set(2, 4, 3); this.scene.add(sun); this.tex = woodTexture(); this.group = new THREE.Group(); this.scene.add(this.group); // mallet this.mallet = new THREE.Group(); const head = new THREE.Mesh(new THREE.SphereGeometry(0.09, 24, 16), new THREE.MeshStandardMaterial({ color: 0x333a44, roughness: 0.5 })); head.position.y = 0.09; const stick = new THREE.Mesh(new THREE.CylinderGeometry(0.018, 0.018, 1.0, 12), new THREE.MeshStandardMaterial({ color: 0xd8c39a })); stick.position.set(0.0, 0.2, 0.45); 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(0.03, 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) 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.toBlock(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(); } // block mm -> scene toScene(x, y, z) { const b = this.block; return new THREE.Vector3((x - b.L / 2) * S, (z - b.H / 2) * S, (b.W / 2 - y) * S); } toBlock(v) { const b = this.block; return [v.x / S + b.L / 2, b.W / 2 - v.z / S, v.y / S + b.H / 2]; } setBlock(b) { this.block = b; this.group.clear(); const mat = new THREE.MeshStandardMaterial({ map: this.tex, roughness: 0.65 }); for (const [x0, x1, y0, y1, z0, z1] of blockBoxes(b)) { const geo = new THREE.BoxGeometry((x1 - x0) * S, (z1 - z0) * S, (y1 - y0) * S); const m = new THREE.Mesh(geo, mat); m.position.copy(this.toScene((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2)); this.group.add(m); } // dark cavity interior (slightly inset so it doesn't z-fight) const e = 0.3; const cav = new THREE.Mesh( 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), new THREE.MeshBasicMaterial({ color: 0x140a04, side: THREE.BackSide }), ); cav.position.copy(this.toScene(b.L / 2, b.W / 2, (b.wall + b.H - b.top) / 2)); this.group.add(cav); // invisible pick plane + mode overlay on top face this.topPick = new THREE.Mesh(new THREE.PlaneGeometry(b.L * S, b.W * S), new THREE.MeshBasicMaterial({ visible: false })); this.topPick.rotation.x = -Math.PI / 2; this.topPick.position.copy(this.toScene(b.L / 2, b.W / 2, b.H + 0.2)); this.group.add(this.topPick); this.overlay = new THREE.Mesh( new THREE.PlaneGeometry(b.L * S, b.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(b.L / 2, b.W / 2, b.H + 0.3)); this.overlay.visible = false; this.group.add(this.overlay); this.mic.position.copy(this.toScene(...micPos(b))); if (this.strike) this.setStrike(...this.strike); } setStrike(x, y) { this.strike = [x, y]; this.malletRest = this.toScene(x, y, this.block.H); this.mallet.position.copy(this.malletRest).add(new THREE.Vector3(0, 0.12, 0)); } hit() { this.hitT = performance.now(); } animate() { if (!this.malletRest) return; const lift = 0.12; 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[nby][nbx] in [0,1] or null to hide setOverlay(nbx, nby, values) { if (!values) { this.overlay.visible = false; return; } const c = document.createElement('canvas'); c.width = nbx; c.height = nby; const g = c.getContext('2d'); const img = g.createImageData(nbx, nby); for (let j = 0; j < nby; j++) { for (let i = 0; i < nbx; i++) { const v = values[j * nbx + i]; const o = ((nby - 1 - j) * nbx + 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; // back off on narrow screens so the whole block fits const d = 3.75 * Math.max(1, 1.4 / this.camera.aspect); this.camera.position.sub(this.controls.target).setLength(d).add(this.controls.target); this.camera.updateProjectionMatrix(); } } // 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]; }