(() => { const canvas = document.querySelector('#model-canvas'); const stage = document.querySelector('.rov-stage'); const ctx = canvas.getContext('2d'); let vertices = null, frame = 0; const renderScale = .62; function parseStl(buffer) { const view = new DataView(buffer); let raw = []; const binaryCount = buffer.byteLength >= 84 ? view.getUint32(80, true) : 0; const isBinary = binaryCount > 0 && 84 + binaryCount * 50 <= buffer.byteLength; if (isBinary) { const stride = Math.max(1, Math.ceil(binaryCount / 14000)); for (let t = 0; t < binaryCount; t += stride) { const offset = 84 + t * 50 + 12; for (let v = 0; v < 3; v++) { raw.push(view.getFloat32(offset + v * 12, true)); raw.push(view.getFloat32(offset + v * 12 + 4, true)); raw.push(view.getFloat32(offset + v * 12 + 8, true)); } } } else { const text = new TextDecoder().decode(buffer); const matches = [...text.matchAll(/vertex\s+([-+\deE.]+)\s+([-+\deE.]+)\s+([-+\deE.]+)/gi)]; if (matches.length < 3) throw new Error('This file does not contain a readable STL mesh'); const stride = Math.max(1, Math.ceil(matches.length / 42000)); for (let i = 0; i + 2 < matches.length; i += 3 * stride) { for (let j = 0; j < 3; j++) raw.push(+matches[i+j][1], +matches[i+j][2], +matches[i+j][3]); } } if (raw.length < 9) throw new Error('The STL mesh is empty'); let minX=Infinity,minY=Infinity,minZ=Infinity,maxX=-Infinity,maxY=-Infinity,maxZ=-Infinity; for(let i=0;i