File size: 4,951 Bytes
804f155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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();
  }
}