diff --git a/main.js b/main.js index 94d6b1a..857b88b 100644 --- a/main.js +++ b/main.js @@ -639,7 +639,7 @@ function createWorker(self) { runSort(viewProj); buffer = processPlyBuffer(e.data.ply); vertexCount = Math.floor(buffer.byteLength / rowLength); - postMessage({ buffer: buffer }); + postMessage({ buffer: buffer, save: !!e.data.save }); } else if (e.data.buffer) { buffer = e.data.buffer; vertexCount = e.data.vertexCount; @@ -684,8 +684,8 @@ void main () { mat3 Vrk = mat3(u1.x, u1.y, u2.x, u1.y, u2.y, u3.x, u2.x, u3.x, u3.y); mat3 J = mat3( - focal.x / cam.z, 0., -(focal.x * cam.x) / (cam.z * cam.z), - 0., -focal.y / cam.z, (focal.y * cam.y) / (cam.z * cam.z), + focal.x / cam.z, 0., -(focal.x * cam.x) / (cam.z * cam.z), + 0., -focal.y / cam.z, (focal.y * cam.y) / (cam.z * cam.z), 0., 0., 0. ); @@ -706,8 +706,8 @@ void main () { vec2 vCenter = vec2(pos2d) / pos2d.w; gl_Position = vec4( - vCenter - + position.x * majorAxis / viewport + vCenter + + position.x * majorAxis / viewport + position.y * minorAxis / viewport, 0.0, 1.0); } @@ -869,14 +869,16 @@ async function main() { worker.onmessage = (e) => { if (e.data.buffer) { splatData = new Uint8Array(e.data.buffer); - const blob = new Blob([splatData.buffer], { - type: "application/octet-stream", - }); - const link = document.createElement("a"); - link.download = "model.splat"; - link.href = URL.createObjectURL(blob); - document.body.appendChild(link); - link.click(); + if (e.data.save) { + const blob = new Blob([splatData.buffer], { + type: "application/octet-stream", + }); + const link = document.createElement("a"); + link.download = "model.splat"; + link.href = URL.createObjectURL(blob); + document.body.appendChild(link); + link.click(); + } } else if (e.data.texdata) { const { texdata, texwidth, texheight } = e.data; // console.log(texdata) @@ -916,25 +918,26 @@ async function main() { }; let activeKeys = []; - let currentCameraIndex = 0; + let currentCameraIndex = 0; window.addEventListener("keydown", (e) => { // if (document.activeElement != document.body) return; carousel = false; if (!activeKeys.includes(e.code)) activeKeys.push(e.code); if (/\d/.test(e.key)) { - currentCameraIndex = parseInt(e.key) + currentCameraIndex = parseInt(e.key); camera = cameras[currentCameraIndex]; viewMatrix = getViewMatrix(camera); } - if (['-', '_'].includes(e.key)){ - currentCameraIndex = (currentCameraIndex + cameras.length - 1) % cameras.length; - viewMatrix = getViewMatrix(cameras[currentCameraIndex]); - } - if (['+', '='].includes(e.key)){ - currentCameraIndex = (currentCameraIndex + 1) % cameras.length; - viewMatrix = getViewMatrix(cameras[currentCameraIndex]); - } + if (["-", "_"].includes(e.key)) { + currentCameraIndex = + (currentCameraIndex + cameras.length - 1) % cameras.length; + viewMatrix = getViewMatrix(cameras[currentCameraIndex]); + } + if (["+", "="].includes(e.key)) { + currentCameraIndex = (currentCameraIndex + 1) % cameras.length; + viewMatrix = getViewMatrix(cameras[currentCameraIndex]); + } camid.innerText = "cam " + currentCameraIndex; if (e.code == "KeyV") { location.hash = @@ -942,10 +945,10 @@ async function main() { JSON.stringify( viewMatrix.map((k) => Math.round(k * 100) / 100), ); - camid.innerText ="" + camid.innerText = ""; } else if (e.code === "KeyP") { carousel = true; - camid.innerText ="" + camid.innerText = ""; } }); window.addEventListener("keyup", (e) => { @@ -965,8 +968,8 @@ async function main() { e.deltaMode == 1 ? lineHeight : e.deltaMode == 2 - ? innerHeight - : 1; + ? innerHeight + : 1; let inv = invert4(viewMatrix); if (e.shiftKey) { inv = translate4( @@ -1178,7 +1181,10 @@ async function main() { const frame = (now) => { let inv = invert4(viewMatrix); - let shiftKey = activeKeys.includes("Shift") || activeKeys.includes("ShiftLeft") || activeKeys.includes("ShiftRight") + let shiftKey = + activeKeys.includes("Shift") || + activeKeys.includes("ShiftLeft") || + activeKeys.includes("ShiftRight"); if (activeKeys.includes("ArrowUp")) { if (shiftKey) { @@ -1225,13 +1231,27 @@ async function main() { inv = translate4(inv, 0, 0, -moveSpeed * gamepad.axes[1]); carousel = false; } - if(gamepad.buttons[12].pressed || gamepad.buttons[13].pressed){ - inv = translate4(inv, 0, -moveSpeed*(gamepad.buttons[12].pressed - gamepad.buttons[13].pressed), 0); + if (gamepad.buttons[12].pressed || gamepad.buttons[13].pressed) { + inv = translate4( + inv, + 0, + -moveSpeed * + (gamepad.buttons[12].pressed - + gamepad.buttons[13].pressed), + 0, + ); carousel = false; } - if(gamepad.buttons[14].pressed || gamepad.buttons[15].pressed){ - inv = translate4(inv, -moveSpeed*(gamepad.buttons[14].pressed - gamepad.buttons[15].pressed), 0, 0); + if (gamepad.buttons[14].pressed || gamepad.buttons[15].pressed) { + inv = translate4( + inv, + -moveSpeed * + (gamepad.buttons[14].pressed - + gamepad.buttons[15].pressed), + 0, + 0, + ); carousel = false; } @@ -1251,12 +1271,17 @@ async function main() { carousel = false; } if (gamepad.buttons[4].pressed && !leftGamepadTrigger) { - camera = cameras[(cameras.indexOf(camera)+1)%cameras.length] + camera = + cameras[(cameras.indexOf(camera) + 1) % cameras.length]; inv = invert4(getViewMatrix(camera)); carousel = false; } if (gamepad.buttons[5].pressed && !rightGamepadTrigger) { - camera = cameras[(cameras.indexOf(camera)+cameras.length-1)%cameras.length] + camera = + cameras[ + (cameras.indexOf(camera) + cameras.length - 1) % + cameras.length + ]; inv = invert4(getViewMatrix(camera)); carousel = false; } @@ -1266,7 +1291,7 @@ async function main() { isJumping = true; carousel = false; } - if(gamepad.buttons[3].pressed){ + if (gamepad.buttons[3].pressed) { carousel = true; } } @@ -1281,8 +1306,8 @@ async function main() { activeKeys.includes("KeyJ") ? -0.05 : activeKeys.includes("KeyL") - ? 0.05 - : 0, + ? 0.05 + : 0, 0, 1, 0, @@ -1292,8 +1317,8 @@ async function main() { activeKeys.includes("KeyI") ? 0.05 : activeKeys.includes("KeyK") - ? -0.05 - : 0, + ? -0.05 + : 0, 1, 0, 0, @@ -1347,7 +1372,7 @@ async function main() { document.getElementById("progress").style.display = "none"; } fps.innerText = Math.round(avgFps) + " fps"; - if (isNaN(currentCameraIndex)){ + if (isNaN(currentCameraIndex)) { camid.innerText = ""; } lastFrame = now; @@ -1356,6 +1381,12 @@ async function main() { frame(); + const isPly = (splatData) => + splatData[0] == 112 && + splatData[1] == 108 && + splatData[2] == 121 && + splatData[3] == 10; + const selectFile = (file) => { const fr = new FileReader(); if (/\.json$/i.test(file.name)) { @@ -1379,14 +1410,9 @@ async function main() { splatData = new Uint8Array(fr.result); console.log("Loaded", Math.floor(splatData.length / rowLength)); - if ( - splatData[0] == 112 && - splatData[1] == 108 && - splatData[2] == 121 && - splatData[3] == 10 - ) { + if (isPly(splatData)) { // ply file magic header means it should be handled differently - worker.postMessage({ ply: splatData.buffer }); + worker.postMessage({ ply: splatData.buffer, save: true }); } else { worker.postMessage({ buffer: splatData.buffer, @@ -1430,18 +1456,26 @@ async function main() { bytesRead += value.length; if (vertexCount > lastVertexCount) { + if (!isPly(splatData)) { + worker.postMessage({ + buffer: splatData.buffer, + vertexCount: Math.floor(bytesRead / rowLength), + }); + } + lastVertexCount = vertexCount; + } + } + if (!stopLoading) { + if (isPly(splatData)) { + // ply file magic header means it should be handled differently + worker.postMessage({ ply: splatData.buffer, save: false }); + } else { worker.postMessage({ buffer: splatData.buffer, vertexCount: Math.floor(bytesRead / rowLength), }); - lastVertexCount = vertexCount; } } - if (!stopLoading) - worker.postMessage({ - buffer: splatData.buffer, - vertexCount: Math.floor(bytesRead / rowLength), - }); } main().catch((err) => {