Spaces:
Runtime error
Runtime error
Download lib/panorama.js from Scherzando/User_Study: direct link, hf CLI and curl.
- Browser
- Download file 4.95 kB
-
https://huggingface.co/spaces/Scherzando/User_Study/resolve/main/lib/panorama.js
- Command line
-
hf download hf://spaces/Scherzando/User_Study/lib/panorama.js
-
curl -L -o panorama.js https://huggingface.co/spaces/Scherzando/User_Study/resolve/main/lib/panorama.js
4.95 kB
| import {viewBasis, directionToUV, radians} from './foa.js'; | |
| export class Panorama { | |
| constructor(canvas, overview, video) { | |
| this.canvas = canvas; | |
| this.overview = overview; | |
| this.video = video; | |
| const gl = this.gl = canvas.getContext('webgl', {alpha: false, antialias: false}); | |
| if (!gl) throw new Error('WebGL is unavailable. Please enable hardware acceleration in your browser.'); | |
| const shader = (type, source) => { | |
| const item = gl.createShader(type); | |
| gl.shaderSource(item, source); | |
| gl.compileShader(item); | |
| if (!gl.getShaderParameter(item, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(item)); | |
| return item; | |
| }; | |
| const vertex = shader(gl.VERTEX_SHADER, ` | |
| attribute vec2 position; varying vec2 screen; | |
| void main() { screen = position; gl_Position = vec4(position, 0., 1.); } | |
| `); | |
| const fragment = shader(gl.FRAGMENT_SHADER, ` | |
| precision highp float; | |
| varying vec2 screen; uniform sampler2D panorama; | |
| uniform vec3 forward, left, up; uniform float halfFov, aspect; | |
| void main() { | |
| vec3 d = normalize(forward - left * screen.x * halfFov + up * screen.y * halfFov / aspect); | |
| float u = fract(0.5 - atan(d.y, d.x) / 6.28318530718); | |
| float v = 0.5 - asin(clamp(d.z, -1., 1.)) / 3.14159265359; | |
| gl_FragColor = texture2D(panorama, vec2(u, v)); | |
| } | |
| `); | |
| this.program = gl.createProgram(); | |
| gl.attachShader(this.program, vertex); | |
| gl.attachShader(this.program, fragment); | |
| gl.linkProgram(this.program); | |
| if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(this.program)); | |
| gl.useProgram(this.program); | |
| gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); | |
| gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1]), gl.STATIC_DRAW); | |
| const position = gl.getAttribLocation(this.program, 'position'); | |
| gl.enableVertexAttribArray(position); | |
| gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0); | |
| gl.bindTexture(gl.TEXTURE_2D, gl.createTexture()); | |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); | |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); | |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); | |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); | |
| this.uniforms = Object.fromEntries(['forward','left','up','halfFov','aspect'].map(key => [key, gl.getUniformLocation(this.program, key)])); | |
| this.dirty = true; | |
| if (video.requestVideoFrameCallback) { | |
| const next = () => { this.dirty = true; video.requestVideoFrameCallback(next); }; | |
| video.requestVideoFrameCallback(next); | |
| } | |
| video.addEventListener('loadeddata', () => { this.dirty = true; }); | |
| video.addEventListener('seeked', () => { this.dirty = true; }); | |
| } | |
| draw(yaw, pitch, fov) { | |
| if (this.video.readyState < 2) return; | |
| const {gl, canvas} = this; | |
| const scale = Math.min(devicePixelRatio || 1, 2); | |
| const width = Math.round(canvas.clientWidth * scale), height = Math.round(canvas.clientHeight * scale); | |
| if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; } | |
| gl.viewport(0, 0, width, height); | |
| if (this.dirty || !this.video.requestVideoFrameCallback) { | |
| gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.video); | |
| this.dirty = false; | |
| } | |
| const basis = viewBasis(yaw, pitch); | |
| for (const key of ['forward','left','up']) gl.uniform3fv(this.uniforms[key], basis[key]); | |
| gl.uniform1f(this.uniforms.halfFov, Math.tan(radians(fov) / 2)); | |
| gl.uniform1f(this.uniforms.aspect, width / height); | |
| gl.drawArrays(gl.TRIANGLES, 0, 6); | |
| this.drawOverview(basis, fov, width / height); | |
| } | |
| drawOverview(basis, fov, aspect) { | |
| const canvas = this.overview, ctx = canvas.getContext('2d'); | |
| ctx.drawImage(this.video, 0, 0, canvas.width, canvas.height); | |
| const edge = [], h = Math.tan(radians(fov) / 2); | |
| for (let side = 0; side < 4; side++) { | |
| for (let step = 0; step <= 32; step++) { | |
| const t = -1 + 2 * step / 32; | |
| const [x, y] = [[t,1],[1,-t],[-t,-1],[-1,t]][side]; | |
| const d = basis.forward.map((v, i) => v - basis.left[i] * x * h + basis.up[i] * y * h / aspect); | |
| const length = Math.hypot(...d); | |
| edge.push(directionToUV(d.map(v => v / length))); | |
| } | |
| } | |
| ctx.strokeStyle = '#d5ff91'; ctx.lineWidth = 2.5; ctx.beginPath(); | |
| let previous; | |
| for (const [u, v] of edge) { | |
| const x = u * canvas.width, y = v * canvas.height; | |
| if (!previous || Math.abs(u - previous) > 0.5) ctx.moveTo(x, y); | |
| else ctx.lineTo(x, y); | |
| previous = u; | |
| } | |
| ctx.stroke(); | |
| const [u,v] = directionToUV(basis.forward); | |
| ctx.fillStyle = '#d5ff91'; ctx.beginPath(); ctx.arc(u * canvas.width, v * canvas.height, 4, 0, 2 * Math.PI); ctx.fill(); | |
| } | |
| } | |