-
Notifications
You must be signed in to change notification settings - Fork 2
/
webgpu_video_nodes.html
260 lines (159 loc) · 6.37 KB
/
webgpu_video_nodes.html
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - equirectangular video panorama</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="../three.js/examples/main.css">
<style>
body {
touch-action: none;
}
</style>
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - video panorama
</div>
<div id="container"></div>
<video id="video" loop muted autoplay crossOrigin="anonymous" preload="metadata" playsinline style="display:none">
</video>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../three.js/build/three.module.js",
"three/addons/": "../three.js/examples/jsm/",
"three-webgpu": "../build/webgpu-renderer.module.js",
"linearsrgb-material": "../build/linearsrgb-material.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { WebGPU, WebGPURenderer, texture, uv, min, max, MeshBasicNodeMaterial, tslFn, uniform, clamp, fwidth, vec4 } from 'three-webgpu';
let _requestVideoFrameCallback = false;
class CustomVideoTexture extends THREE.Texture {
constructor(video) {
super(video, THREE.Texture.DEFAULT_MAPPING, THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.LinearFilter, THREE.LinearFilter, THREE.RGBAFormat);
this.generateMipmaps = false;
const updateVideo = () => {
this.needsUpdate = true;
video.requestVideoFrameCallback(updateVideo);
}
if ('requestVideoFrameCallback' in video) {
_requestVideoFrameCallback = true;
video.requestVideoFrameCallback(updateVideo);
}
}
get isVideoTexture() {
return true;
}
update() {
if (!_requestVideoFrameCallback) this.needsUpdate = true;
}
}
let camera, scene, renderer;
let isUserInteracting = false,
lon = 0, lat = 0,
phi = 0, theta = 0,
onPointerDownPointerX = 0,
onPointerDownPointerY = 0,
onPointerDownLon = 0,
onPointerDownLat = 0;
const distance = 50;
const video = document.getElementById('video');
video.src = "//videos.electroteque.org/360/ultra_light_flight_720p.mp4";
video.play();
video.addEventListener("loadedmetadata", () => {
console.log("loadedmetadata");
init();
});
async function init() {
const container = document.getElementById('container');
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1100);
scene = new THREE.Scene();
const geometry = new THREE.SphereGeometry(500, 60, 40);
// invert the geometry on the x-axis so that all of the faces point inward
geometry.scale(- 1, 1, 1);
const videoTexture = new CustomVideoTexture(video);
//texture.colorSpace = THREE.SRGBColorSpace;
//texture.colorSpace = THREE.LinearSRGBColorSpace;
let material = new MeshBasicNodeMaterial({ map: videoTexture, color: new THREE.Color( 0x0066ff ), opacity: 0.5 });
renderer = new WebGPURenderer();
videoTexture.colorSpace = THREE.SRGBColorSpace;
const colorShaderNode = tslFn( ( input ) => {
const tex = texture(input.texture);
const color = uniform(input.color);
const opacity = uniform(input.opacity);
const sigDist = max(min(tex.r, tex.g), min(max(tex.r, tex.g), tex.b));
const alpha = clamp(sigDist.div(fwidth(sigDist)).add(0.5), 0.0, 1.0);
//return vec4(color.xyz, 1);
return vec4(color.xyz, alpha.mul(opacity));
//return vec3( 0.299, 0.587, 0.114 ).dot( input.color.xyz );
});
const opacityShaderNode = tslFn( ( input ) => {
const tex = texture(input.texture);
const color = uniform(input.color);
const opacity = uniform(input.opacity);
const sigDist = max(min(tex.r, tex.g), min(max(tex.r, tex.g), tex.b));
const alpha = clamp(sigDist.div(fwidth(sigDist)).add(0.5), 0.0, 1.0);
return alpha.mul(opacity);
//return vec4(color.xyz, 1);
//return vec4(color.xyz, alpha.mul(opacity));
//return vec3( 0.299, 0.587, 0.114 ).dot( input.color.xyz );
});
material.colorNode = colorShaderNode( { texture: videoTexture, color: material.color, opacity: material.opacity });
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
//renderer.setSize(video.videoWidth, video.videoHeight );
container.appendChild(renderer.domElement);
document.addEventListener('pointerdown', onPointerDown);
document.addEventListener('pointermove', onPointerMove);
document.addEventListener('pointerup', onPointerUp);
//window.addEventListener( 'resize', onWindowResize );
renderer.setAnimationLoop( update);
//animate();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function onPointerDown(event) {
isUserInteracting = true;
onPointerDownPointerX = event.clientX;
onPointerDownPointerY = event.clientY;
onPointerDownLon = lon;
onPointerDownLat = lat;
}
function onPointerMove(event) {
if (isUserInteracting === true) {
lon = (onPointerDownPointerX - event.clientX) * 0.1 + onPointerDownLon;
lat = (onPointerDownPointerY - event.clientY) * 0.1 + onPointerDownLat;
}
}
function onPointerUp() {
isUserInteracting = false;
}
function animate() {
requestAnimationFrame(animate);
update();
}
function update() {
lat = Math.max(- 85, Math.min(85, lat));
phi = THREE.MathUtils.degToRad(90 - lat);
theta = THREE.MathUtils.degToRad(lon);
camera.position.x = distance * Math.sin(phi) * Math.cos(theta);
camera.position.y = distance * Math.cos(phi);
camera.position.z = distance * Math.sin(phi) * Math.sin(theta);
camera.lookAt(0, 0, 0);
renderer.render(scene, camera);
}
</script>
</body>
</html>