|
| 1 | +import { GUI } from 'dat.gui'; |
| 2 | +import panoramaExternalTextureWGSL from './sampleExternalTextureAsPanorama.wgsl'; |
| 3 | +import { quitIfWebGPUNotAvailable } from '../util'; |
| 4 | +import { mat4 } from 'wgpu-matrix'; |
| 5 | + |
| 6 | +const adapter = await navigator.gpu?.requestAdapter(); |
| 7 | +const device = await adapter?.requestDevice(); |
| 8 | +quitIfWebGPUNotAvailable(adapter, device); |
| 9 | + |
| 10 | +// Set video element |
| 11 | +const video = document.createElement('video'); |
| 12 | +video.loop = true; |
| 13 | +video.playsInline = true; |
| 14 | +video.autoplay = true; |
| 15 | +video.muted = true; |
| 16 | +video.src = |
| 17 | + '../../assets/video/Video_360°._Timelapse._Bled_Lake_in_Slovenia..webm.720p.vp9.webm'; |
| 18 | +await video.play(); |
| 19 | + |
| 20 | +const canvas = document.querySelector('canvas') as HTMLCanvasElement; |
| 21 | +const context = canvas.getContext('webgpu') as GPUCanvasContext; |
| 22 | +const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); |
| 23 | + |
| 24 | +context.configure({ |
| 25 | + device, |
| 26 | + format: presentationFormat, |
| 27 | +}); |
| 28 | + |
| 29 | +const module = device.createShaderModule({ |
| 30 | + code: panoramaExternalTextureWGSL, |
| 31 | +}); |
| 32 | + |
| 33 | +const pipeline = device.createRenderPipeline({ |
| 34 | + layout: 'auto', |
| 35 | + vertex: { module }, |
| 36 | + fragment: { |
| 37 | + module, |
| 38 | + targets: [ |
| 39 | + { |
| 40 | + format: presentationFormat, |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + primitive: { |
| 45 | + topology: 'triangle-list', |
| 46 | + }, |
| 47 | +}); |
| 48 | + |
| 49 | +const sampler = device.createSampler({ |
| 50 | + magFilter: 'linear', |
| 51 | + minFilter: 'linear', |
| 52 | +}); |
| 53 | + |
| 54 | +const uniformBuffer = device.createBuffer({ |
| 55 | + size: (16 + 2 + 2) * 4, |
| 56 | + usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, |
| 57 | +}); |
| 58 | + |
| 59 | +const settings = { |
| 60 | + fov: 90, |
| 61 | +}; |
| 62 | + |
| 63 | +const gui = new GUI(); |
| 64 | +gui.add(settings, 'fov', 3, 179).step(1).name('Field of View'); |
| 65 | +let yRotation = 0; |
| 66 | +let xRotation = 0; |
| 67 | + |
| 68 | +function frame() { |
| 69 | + const time = performance.now() / 1000; |
| 70 | + |
| 71 | + canvas.width = canvas.clientWidth; |
| 72 | + canvas.height = canvas.clientHeight; |
| 73 | + |
| 74 | + const bindGroup = device.createBindGroup({ |
| 75 | + layout: pipeline.getBindGroupLayout(0), |
| 76 | + entries: [ |
| 77 | + { |
| 78 | + binding: 0, |
| 79 | + resource: uniformBuffer, |
| 80 | + }, |
| 81 | + { |
| 82 | + binding: 1, |
| 83 | + resource: device.importExternalTexture({ |
| 84 | + source: video, |
| 85 | + }), |
| 86 | + }, |
| 87 | + { |
| 88 | + binding: 2, |
| 89 | + resource: sampler, |
| 90 | + }, |
| 91 | + ], |
| 92 | + }); |
| 93 | + |
| 94 | + const commandEncoder = device.createCommandEncoder(); |
| 95 | + const canvasTexture = context.getCurrentTexture(); |
| 96 | + |
| 97 | + const rotation = time * 0.1 + yRotation; |
| 98 | + const projection = mat4.perspective( |
| 99 | + (settings.fov * Math.PI) / 180, |
| 100 | + canvas.clientWidth / canvas.clientHeight, |
| 101 | + 0.5, |
| 102 | + 100 |
| 103 | + ); |
| 104 | + |
| 105 | + // Note: You can use any method you want to compute a view matrix, |
| 106 | + // just be sure to zero out the translation. |
| 107 | + const camera = mat4.identity(); |
| 108 | + mat4.rotateY(camera, rotation, camera); |
| 109 | + mat4.rotateX(camera, xRotation, camera); |
| 110 | + mat4.setTranslation(camera, [0, 0, 0], camera); |
| 111 | + const view = mat4.inverse(camera); |
| 112 | + const viewDirectionProjection = mat4.multiply(projection, view); |
| 113 | + const viewDirectionProjectionInverse = mat4.inverse(viewDirectionProjection); |
| 114 | + |
| 115 | + const uniforms = new Float32Array([ |
| 116 | + ...viewDirectionProjectionInverse, |
| 117 | + canvasTexture.width, |
| 118 | + canvasTexture.height, |
| 119 | + ]); |
| 120 | + device.queue.writeBuffer(uniformBuffer, 0, uniforms); |
| 121 | + |
| 122 | + const renderPassDescriptor: GPURenderPassDescriptor = { |
| 123 | + colorAttachments: [ |
| 124 | + { |
| 125 | + view: canvasTexture.createView(), |
| 126 | + clearValue: [0, 0, 0, 1], |
| 127 | + loadOp: 'clear', |
| 128 | + storeOp: 'store', |
| 129 | + }, |
| 130 | + ], |
| 131 | + }; |
| 132 | + |
| 133 | + const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor); |
| 134 | + passEncoder.setPipeline(pipeline); |
| 135 | + passEncoder.setBindGroup(0, bindGroup); |
| 136 | + passEncoder.draw(3); |
| 137 | + passEncoder.end(); |
| 138 | + device.queue.submit([commandEncoder.finish()]); |
| 139 | + |
| 140 | + requestAnimationFrame(frame); |
| 141 | + |
| 142 | + let startX = 0; |
| 143 | + let startY = 0; |
| 144 | + let startYRotation = 0; |
| 145 | + let startTarget = 0; |
| 146 | + |
| 147 | + const clamp = (value: number, min: number, max: number) => { |
| 148 | + return Math.max(min, Math.min(max, value)); |
| 149 | + }; |
| 150 | + |
| 151 | + const drag = (e: PointerEvent) => { |
| 152 | + const deltaX = e.clientX - startX; |
| 153 | + const deltaY = e.clientY - startY; |
| 154 | + yRotation = startYRotation + deltaX * 0.01; |
| 155 | + xRotation = clamp( |
| 156 | + startTarget + deltaY * -0.01, |
| 157 | + -Math.PI * 0.4, |
| 158 | + Math.PI * 0.4 |
| 159 | + ); |
| 160 | + }; |
| 161 | + |
| 162 | + const stopDrag = () => { |
| 163 | + window.removeEventListener('pointermove', drag); |
| 164 | + window.removeEventListener('pointerup', stopDrag); |
| 165 | + }; |
| 166 | + |
| 167 | + const startDrag = (e: PointerEvent) => { |
| 168 | + window.addEventListener('pointermove', drag); |
| 169 | + window.addEventListener('pointerup', stopDrag); |
| 170 | + e.preventDefault(); |
| 171 | + startX = e.clientX; |
| 172 | + startY = e.clientY; |
| 173 | + startYRotation = yRotation; |
| 174 | + startTarget = xRotation; |
| 175 | + }; |
| 176 | + canvas.addEventListener('pointerdown', startDrag); |
| 177 | +} |
| 178 | + |
| 179 | +requestAnimationFrame(frame); |
0 commit comments