|
import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js'; |
|
import {OrbitControls} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js'; |
|
import {TrackballControls} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/TrackballControls.js'; |
|
import {OBJLoader} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/OBJLoader.js'; |
|
import {MTLLoader} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/MTLLoader.js'; |
|
import {RGBELoader} from 'three/addons/loaders/RGBELoader.js'; |
|
import {GUI} from 'three/addons/libs/lil-gui.module.min.js'; |
|
|
|
|
|
const audioElement = document.getElementById("audio"); |
|
|
|
|
|
const urlParams = new URLSearchParams(window.location.search); |
|
let pieceId = parseInt(urlParams.get('id')); |
|
if (!pieceId) { |
|
pieceId = 0; |
|
} |
|
console.log("Piece ID:", pieceId); |
|
audioElement.src = `/audio/${pieceId}`; |
|
let totalFrames = 0; |
|
let totalDuration = -1; |
|
let loadedFrames = 0; |
|
let keyObjs = {}; |
|
|
|
|
|
document.getElementById('volumeControl').addEventListener('input', () => { |
|
audio.volume = volumeControl.value; |
|
if (audio.volume === 0) { |
|
volumeIcon.className = 'fas fa-volume-mute'; |
|
} else if (audio.volume <= 0.5) { |
|
volumeIcon.className = 'fas fa-volume-down'; |
|
} else { |
|
volumeIcon.className = 'fas fa-volume-up'; |
|
} |
|
}); |
|
|
|
async function fetchMetaData() { |
|
const url = `/metadata/${pieceId}`; |
|
|
|
const response = await fetch(url); |
|
const jsonData = await response.json(); |
|
return jsonData; |
|
} |
|
|
|
async function fetchFacesData() { |
|
const url = `/mano_faces_data`; |
|
const response = await fetch(url); |
|
const jsonData = await response.json(); |
|
return jsonData; |
|
} |
|
|
|
|
|
const dbName = "MeshFramesDB"; |
|
let db; |
|
|
|
|
|
let frames = new Array(2000); |
|
|
|
let preloadedFrames = {}; |
|
const preloadedWindowSize = 600; |
|
|
|
|
|
async function preloadFramesAround(currentFrameIndex) { |
|
const startFrame = currentFrameIndex |
|
const endFrame = Math.min(totalFrames, currentFrameIndex + preloadedWindowSize); |
|
|
|
|
|
for (let i = startFrame; i < endFrame; i++) { |
|
if (!preloadedFrames[i]) { |
|
try { |
|
const frameData = await getFrameFromDB(i); |
|
preloadedFrames[i] = frameData; |
|
} catch (error) { |
|
console.error(`Error preloading frame ${i}:`, error); |
|
} |
|
} |
|
} |
|
|
|
|
|
Object.keys(preloadedFrames).forEach(frameIndex => { |
|
if (frameIndex < startFrame || frameIndex > endFrame) { |
|
delete preloadedFrames[frameIndex]; |
|
} |
|
}); |
|
} |
|
|
|
function getPreloadedFrame(frameIndex) { |
|
return preloadedFrames[frameIndex]; |
|
} |
|
|
|
|
|
async function openDB() { |
|
await deleteDatabase(); |
|
return new Promise((resolve, reject) => { |
|
const request = indexedDB.open(dbName, 1); |
|
|
|
request.onupgradeneeded = (event) => { |
|
db = event.target.result; |
|
|
|
const objectStore = db.createObjectStore("frames", {keyPath: "index"}); |
|
}; |
|
|
|
request.onsuccess = (event) => { |
|
db = event.target.result; |
|
console.log("IndexedDB opened successfully"); |
|
resolve(); |
|
}; |
|
|
|
request.onerror = (event) => { |
|
console.error("Error opening IndexedDB:", event.target); |
|
reject(); |
|
}; |
|
}); |
|
} |
|
|
|
function deleteDatabase() { |
|
return new Promise((resolve, reject) => { |
|
const deleteRequest = indexedDB.deleteDatabase(dbName); |
|
|
|
deleteRequest.onsuccess = () => { |
|
console.log("Database deleted successfully."); |
|
resolve(); |
|
}; |
|
|
|
deleteRequest.onerror = (event) => { |
|
console.error("Error deleting database:", event.target.errorCode); |
|
reject(); |
|
}; |
|
|
|
deleteRequest.onblocked = () => { |
|
console.warn("Database deletion blocked. Close other tabs using this database."); |
|
}; |
|
}); |
|
} |
|
|
|
function printAllFramesInDB() { |
|
return new Promise((resolve, reject) => { |
|
const transaction = db.transaction(["frames"], "readonly"); |
|
const objectStore = transaction.objectStore("frames"); |
|
|
|
const allFrames = []; |
|
const request = objectStore.openCursor(); |
|
|
|
request.onsuccess = (event) => { |
|
const cursor = event.target.result; |
|
if (cursor) { |
|
|
|
allFrames.push(cursor.value); |
|
cursor.continue(); |
|
} else { |
|
console.log("All frames in the database:", allFrames); |
|
resolve(allFrames); |
|
} |
|
}; |
|
|
|
request.onerror = (event) => { |
|
console.error("Error reading from IndexedDB:", event.target.errorCode); |
|
reject(event.target.errorCode); |
|
}; |
|
}); |
|
} |
|
|
|
function storeFrameInDB_(frameIndex, frameData) { |
|
frames[frameIndex] = frameData; |
|
return Promise.resolve(); |
|
} |
|
|
|
|
|
function storeFrameInDB(frameIndex, frameData) { |
|
|
|
return new Promise((resolve, reject) => { |
|
const transaction = db.transaction(["frames"], "readwrite"); |
|
const objectStore = transaction.objectStore("frames"); |
|
const request = objectStore.add({index: frameIndex, data: frameData}); |
|
|
|
request.onsuccess = () => { |
|
resolve(); |
|
}; |
|
|
|
request.onerror = (event) => { |
|
console.error("Error storing frame:", event.target, event); |
|
reject(); |
|
}; |
|
}); |
|
} |
|
|
|
function getFrameFromDB_(frameIndex) { |
|
const frameData = frames[frameIndex]; |
|
if (frameData) { |
|
return Promise.resolve(frameData); |
|
} else { |
|
|
|
return Promise.reject("Frame not found in IndexedDB"); |
|
} |
|
} |
|
|
|
|
|
function getFrameFromDB(frameIndex) { |
|
return new Promise((resolve, reject) => { |
|
const transaction = db.transaction(["frames"], "readonly"); |
|
const objectStore = transaction.objectStore("frames"); |
|
const request = objectStore.get(frameIndex); |
|
|
|
request.onsuccess = (event) => { |
|
if (event.target.result) { |
|
resolve(event.target.result.data); |
|
} else { |
|
reject("Frame not found in IndexedDB"); |
|
} |
|
}; |
|
|
|
request.onerror = (event) => { |
|
console.error("Error retrieving frame:", event.target, "Frame index:", frameIndex); |
|
reject(); |
|
}; |
|
}); |
|
} |
|
|
|
|
|
async function fetchMeshData() { |
|
const url = `/mano_vertices_data/${pieceId}`; |
|
const response = await fetch(url); |
|
|
|
if (!response.ok) { |
|
throw new Error(`HTTP error! Status: ${response.status}`); |
|
} |
|
|
|
const reader = response.body.getReader(); |
|
const decoder = new TextDecoder("utf-8"); |
|
let buffer = ""; |
|
|
|
const loadingText = document.getElementById('loadingText'); |
|
|
|
let startTime = Date.now(); |
|
|
|
while (true) { |
|
const {done, value} = await reader.read(); |
|
|
|
if (done) { |
|
break; |
|
} |
|
|
|
const chunkText = decoder.decode(value, {stream: true}); |
|
buffer += chunkText; |
|
|
|
let parts = buffer.split('\n'); |
|
buffer = parts.pop(); |
|
|
|
|
|
for (const frameJson of parts) { |
|
try { |
|
const frameData = JSON.parse(frameJson); |
|
|
|
|
|
|
|
await storeFrameInDB(loadedFrames, frameData); |
|
loadedFrames++; |
|
|
|
const percentage = (loadedFrames / totalFrames) * 100; |
|
document.getElementById('bufferedBar').style.width = percentage + '%'; |
|
|
|
|
|
const elapsedTime = (Date.now() - startTime) / 1000; |
|
const loadRate = loadedFrames / elapsedTime; |
|
const framesRemaining = totalFrames - loadedFrames; |
|
const timeRemaining = framesRemaining / loadRate; |
|
const minutes = Math.floor(timeRemaining / 60); |
|
const seconds = Math.floor(timeRemaining % 60); |
|
|
|
const elapsedTimeMinutes = Math.floor(elapsedTime / 60); |
|
const elapsedTimeSeconds = Math.floor(elapsedTime % 60); |
|
|
|
let text = `Loading... ${Math.floor(percentage)}% <br />` |
|
text += `ETA: ${minutes}m ${seconds}s <br />` |
|
text += `Elapsed time: ${elapsedTimeMinutes}m ${elapsedTimeSeconds}s`; |
|
loadingText.innerHTML = text; |
|
} catch (e) { |
|
console.error("Error parsing/storing frame:", e); |
|
} |
|
} |
|
} |
|
|
|
loadingText.innerHTML = 'Loading... 100% <br /> Elapsed time: ' + Math.floor((Date.now() - startTime) / 1000) + 's'; |
|
} |
|
|
|
|
|
async function fetchFrameForRendering(frameIndex) { |
|
try { |
|
const frameData = await getFrameFromDB(frameIndex); |
|
return frameData; |
|
} catch (error) { |
|
console.error("Error fetching frame for rendering:", error, "Frame index:", frameIndex); |
|
return null; |
|
} |
|
} |
|
|
|
|
|
async function main() { |
|
console.log("Starting application"); |
|
|
|
const scene = new THREE.Scene(); |
|
const gui = new GUI(); |
|
const camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 1000); |
|
const renderer = new THREE.WebGLRenderer({canvas: document.getElementById('threeCanvas'), antialias: true}); |
|
renderer.setSize(window.innerWidth, window.innerHeight); |
|
document.body.appendChild(renderer.domElement); |
|
|
|
|
|
const pmremGenerator = new THREE.PMREMGenerator(renderer); |
|
|
|
const hdriLoader = new RGBELoader() |
|
hdriLoader.load("resources/envmap.hdr", function (texture) { |
|
const envMap = pmremGenerator.fromEquirectangular(texture).texture; |
|
texture.dispose(); |
|
scene.environment = envMap; |
|
scene.background = envMap; |
|
scene.backgroundBlurriness = 0.3; |
|
}); |
|
|
|
const renderingParams = { |
|
handColor: 0xefceb9, |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x8d8d8d, 3); |
|
hemiLight.position.set(0, 20, 0); |
|
|
|
|
|
const dirLight = new THREE.DirectionalLight(0xffffff, 3); |
|
dirLight.position.set(3, 10, 10); |
|
dirLight.castShadow = false; |
|
|
|
|
|
|
|
const pianoCenter = new THREE.Vector3(0.61431422, -0.074, -0.0055); |
|
const controls = new OrbitControls(camera, renderer.domElement); |
|
controls.rotateSpeed = 0.5; |
|
|
|
|
|
controls.enableDamping = true; |
|
controls.dampingFactor = 0.25; |
|
controls.screenSpacePanning = false; |
|
controls.minDistance = 0; |
|
controls.maxDistance = Infinity; |
|
|
|
const presets = { |
|
top: { |
|
position: { |
|
"x": 0.10415014734151176, |
|
"y": 1.2303206276916157, |
|
"z": -0.5999895298700443 |
|
}, target: { |
|
"x": 0.1, |
|
"y": 0, |
|
"z": -0.6 |
|
} |
|
}, |
|
left: { |
|
position: { |
|
"x": 0.6998938717565077, |
|
"y": 1.0171497080001928, |
|
"z": 0.5578656721849795 |
|
}, |
|
target: { |
|
"x": 0.13180072175419472, |
|
"y": 1.5691260771514555e-19, |
|
"z": -0.5241889198249452 |
|
}, |
|
}, |
|
right: { |
|
position: { |
|
"x": 0.6027349580836255, |
|
"y": 0.818198927359689, |
|
"z": -1.8632773667524538 |
|
}, target: { |
|
"x": 0.06154058571770953, |
|
"y": 1.6977216676322782e-18, |
|
"z": -0.8159637729174926 |
|
} |
|
} |
|
}; |
|
|
|
|
|
function updateCameraAndControls(preset) { |
|
camera.position.set(preset.position.x, preset.position.y, preset.position.z); |
|
controls.target.set(preset.target.x, preset.target.y, preset.target.z); |
|
controls.update(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
const cameraSettings = { |
|
preset: 'top' |
|
}; |
|
|
|
gui.add(cameraSettings, 'preset', Object.keys(presets)) |
|
.name('Camera Presets') |
|
.onChange((value) => { |
|
updateCameraAndControls(presets[value]); |
|
}); |
|
updateCameraAndControls(presets.top); |
|
|
|
|
|
const whiteMaterial = new THREE.MeshStandardMaterial({ |
|
color: 0xffffff, |
|
roughness: 0.3, |
|
metalness: 0.2 |
|
}); |
|
|
|
const blackMaterial = new THREE.MeshStandardMaterial({ |
|
color: 0x000000, |
|
roughness: 0.4, |
|
metalness: 0.1 |
|
}); |
|
for (let i = 0; i < 88; i++) { |
|
const objLoader = new OBJLoader(); |
|
const mtlLoader = new MTLLoader(); |
|
|
|
const mesh_url = `/piano_mesh/${i}`; |
|
mtlLoader.load(mesh_url + '.mtl', function (materials) { |
|
materials.preload(); |
|
const mat = Object.values(materials.materials)[0]; |
|
if (isBlackKey(i + 1)) { |
|
mat.color.setHex(0x000000); |
|
} else { |
|
mat.color.setHex(0xffffff); |
|
} |
|
objLoader.setMaterials(materials); |
|
objLoader.load(mesh_url + '.obj', function (object) { |
|
object.traverse((child) => { |
|
if (child instanceof THREE.Mesh) { |
|
|
|
child.material = isBlackKey(i + 1) ? blackMaterial.clone() : whiteMaterial.clone(); |
|
|
|
|
|
child.castShadow = true; |
|
child.receiveShadow = true; |
|
} |
|
}); |
|
|
|
object.rotation.x = Math.PI / 2; |
|
scene.add(object); |
|
keyObjs[i] = object; |
|
}); |
|
}); |
|
} |
|
|
|
const metadata = await fetchMetaData(); |
|
totalFrames = metadata.num_frames; |
|
document.getElementById('pieceInformation').innerHTML = `${metadata.name} <br /> by ${metadata.composer}` |
|
const fps = 60000 / 1001; |
|
totalDuration = totalFrames / fps; |
|
|
|
const minutes = Math.floor(totalDuration / 60); |
|
const seconds = Math.floor(totalDuration % 60).toString().padStart(2, '0'); |
|
document.getElementById('totalDuration').textContent = `${minutes}:${seconds}`; |
|
fetchMeshData(); |
|
|
|
const preloadFrames = 60 |
|
while (loadedFrames < preloadFrames) { |
|
await new Promise(resolve => setTimeout(resolve, 1000)); |
|
} |
|
|
|
const facesData = await fetchFacesData(); |
|
|
|
const leftFaces = facesData.left_faces.flat(); |
|
const rightFaces = facesData.right_faces.flat(); |
|
|
|
|
|
const leftGeometry = new THREE.BufferGeometry(); |
|
const rightGeometry = new THREE.BufferGeometry(); |
|
const numVertices = 778; |
|
|
|
const leftPositionAttribute = new Float32Array(numVertices * 3); |
|
const rightPositionAttribute = new Float32Array(numVertices * 3); |
|
|
|
leftGeometry.setAttribute('position', new THREE.BufferAttribute(leftPositionAttribute, 3)); |
|
rightGeometry.setAttribute('position', new THREE.BufferAttribute(rightPositionAttribute, 3)); |
|
|
|
leftGeometry.setIndex(leftFaces); |
|
rightGeometry.setIndex(rightFaces); |
|
|
|
|
|
const leftMaterial = new THREE.MeshStandardMaterial({ |
|
|
|
color: renderingParams.handColor, |
|
roughness: 1.0, |
|
metalness: 1.0 |
|
}); |
|
|
|
const rightMaterial = leftMaterial.clone(); |
|
|
|
|
|
|
|
|
|
const leftMesh = new THREE.Mesh(leftGeometry, leftMaterial); |
|
leftMesh.frustumCulled = false; |
|
const rightMesh = new THREE.Mesh(rightGeometry, rightMaterial); |
|
rightMesh.frustumCulled = false; |
|
|
|
scene.add(leftMesh); |
|
scene.add(rightMesh); |
|
|
|
|
|
|
|
console.log("Camera position:", camera.position); |
|
let axesHelper = new THREE.AxesHelper(20); |
|
axesHelper.rotation.x = Math.PI / 2; |
|
|
|
|
|
scene.rotation.x = -Math.PI / 2; |
|
|
|
let currentFrame = 0; |
|
|
|
|
|
const audio = document.getElementById('audio'); |
|
const playButton = document.getElementById('playPauseBtn'); |
|
const playPauseIcon = document.getElementById('playPauseIcon'); |
|
const scrubber = document.getElementById('scrubber'); |
|
const bufferedBar = document.getElementById('bufferedBar'); |
|
|
|
function isBlackKey(idx) { |
|
const blackKey = [1, 3, 6, 8, 10]; |
|
|
|
return blackKey.includes((idx + 8) % 12); |
|
} |
|
|
|
let isPlaying = false; |
|
|
|
playButton.addEventListener('click', () => { |
|
if (isPlaying) { |
|
audio.pause(); |
|
playPauseIcon.className = 'fas fa-play'; |
|
|
|
} else { |
|
audio.play(); |
|
playPauseIcon.className = 'fas fa-pause'; |
|
} |
|
isPlaying = !isPlaying; |
|
}); |
|
|
|
|
|
const interval = 1000 / fps; |
|
|
|
|
|
|
|
setInterval(() => { |
|
const currentTime = audio.currentTime; |
|
const frameIndex = Math.floor(currentTime * fps); |
|
preloadFramesAround(frameIndex); |
|
}, 500); |
|
|
|
|
|
async function playOnLoad(frameIndex) { |
|
if (!isPlaying) { |
|
return; |
|
} |
|
try { |
|
const frameData = await getFrameFromDB(frameIndex); |
|
if (frameData) { |
|
audio.play(); |
|
} else { |
|
setTimeout(() => { |
|
playOnLoad(frameIndex); |
|
}, 500); |
|
} |
|
} catch (error) { |
|
console.error("Error playing on load:", error); |
|
} |
|
} |
|
|
|
|
|
function syncAnimationAndAudio() { |
|
const currentTime = audio.currentTime; |
|
const frameIndex = Math.floor(currentTime * fps); |
|
if (frameIndex >= totalFrames) { |
|
audio.pause(); |
|
return; |
|
} |
|
const frameDataThis = getPreloadedFrame(frameIndex); |
|
if (!frameDataThis) { |
|
audio.pause(); |
|
playOnLoad(frameIndex); |
|
return; |
|
} |
|
|
|
for (let i = 0; i < numVertices; i++) { |
|
for (let j = 0; j < 3; j++) { |
|
leftPositionAttribute[i * 3 + j] = frameDataThis.left_vertices[i][j]; |
|
rightPositionAttribute[i * 3 + j] = frameDataThis.right_vertices[i][j]; |
|
} |
|
} |
|
|
|
|
|
leftGeometry.computeVertexNormals(); |
|
rightGeometry.computeVertexNormals(); |
|
|
|
leftGeometry.attributes.position.needsUpdate = true; |
|
rightGeometry.attributes.position.needsUpdate = true; |
|
|
|
|
|
for (let i = 0; i < 88; i++) { |
|
const obj = keyObjs[i]; |
|
const mat = keyObjs[i].children[0].material; |
|
if (frameDataThis.pressed_keys[i] > 0) { |
|
mat.color.setHex(0x00ff00); |
|
|
|
obj.rotation.z = -Math.PI / 45; |
|
} else { |
|
obj.rotation.z = 0; |
|
if (isBlackKey(i + 1)) { |
|
mat.color.setHex(0x000000); |
|
} else { |
|
mat.color.setHex(0xffffff); |
|
} |
|
} |
|
mat.needsUpdate = true; |
|
} |
|
|
|
scrubber.value = (currentTime / totalDuration) * 100; |
|
document.getElementById('timeDisplay').textContent = `${Math.floor(currentTime / 60)}:${Math.floor(currentTime % 60).toString().padStart(2, '0')}`; |
|
document.getElementById('playedBar').style.width = scrubber.value + "%"; |
|
} |
|
|
|
audio.ontimeupdate = syncAnimationAndAudio; |
|
|
|
scrubber.addEventListener('input', () => { |
|
const scrubberValue = scrubber.value; |
|
const newTime = scrubberValue / 100 * totalDuration; |
|
audio.currentTime = newTime; |
|
syncAnimationAndAudio(); |
|
}); |
|
|
|
|
|
function logCameraSettings() { |
|
console.log("Camera position:", camera.position); |
|
console.log("Camera rotation:", camera.rotation); |
|
console.log("Camera zoom", camera.zoom); |
|
console.log("Controls target", controls.target); |
|
} |
|
|
|
|
|
|
|
|
|
function animate() { |
|
syncAnimationAndAudio(); |
|
controls.update(); |
|
renderer.render(scene, camera); |
|
} |
|
|
|
let lastTime = 0; |
|
|
|
function animationLoop(time) { |
|
if (time - lastTime >= interval) { |
|
animate(); |
|
lastTime = time; |
|
} |
|
requestAnimationFrame(animationLoop); |
|
} |
|
|
|
animationLoop(0); |
|
|
|
|
|
window.addEventListener('resize', () => { |
|
camera.aspect = window.innerWidth / window.innerHeight; |
|
camera.updateProjectionMatrix(); |
|
renderer.setSize(window.innerWidth, window.innerHeight); |
|
}); |
|
} |
|
|
|
|
|
openDB().then(() => { |
|
main(); |
|
}); |
|
|