Repository navigation
Expand file tree
/
Copy pathvideo.js
More file actions
112 lines (104 loc) · 4.83 KB
/
Copy pathvideo.js
File metadata and controls
112 lines (104 loc) · 4.83 KB
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
// ============================================================================
// VIDEO SCREENS: OFFICIAL YOUTUBE EMBEDS PLAYING INSIDE THE 3D WORLD
// ============================================================================
// Screens register here with the YouTube embed they should play. When the
// avatar gets close and nothing blocks the view, a CSS3D iframe is placed
// exactly over the screen and autoplays muted (browsers only allow muted
// autoplay); click the video to turn the sound on. Far away it is removed.
const VideoScreens = (() => {
const PIXEL_WIDTH = 1280;
const ACTIVATE_DISTANCE = 70;
const screens = [];
let cssRenderer = null;
let cssScene = null;
let lastOcclusionCheck = 0;
const occlusionRay = new THREE.Raycaster();
const worldPosition = new THREE.Vector3();
const worldQuaternion = new THREE.Quaternion();
const worldScale = new THREE.Vector3();
// YouTube embed that autoplays muted and loops a list of official videos
function youtubeEmbed(videoIds) {
const [first, ...rest] = videoIds;
const playlist = [first, ...rest].join(',');
return `https://www.youtube.com/embed/${first}?autoplay=1&mute=1&loop=1&playlist=${playlist}&playsinline=1&rel=0&modestbranding=1`;
}
// Embed of a channel's uploads (UU + channel id without the leading UC)
function channelUploadsEmbed(channelId) {
return `https://www.youtube.com/embed/videoseries?list=UU${channelId.slice(2)}&autoplay=1&mute=1&loop=1&playsinline=1&rel=0`;
}
function register(anchor, width, height, source) {
const screen = { anchor, width, height, source, object: null, visible: true };
screens.push(screen);
return {
setSource(next) {
screen.source = next;
if (screen.object) { removeIframe(screen); }
}
};
}
function createIframe(screen) {
const element = document.createElement('div');
element.style.width = `${PIXEL_WIDTH}px`;
element.style.height = `${Math.round(PIXEL_WIDTH * screen.height / screen.width)}px`;
element.style.backfaceVisibility = 'hidden';
element.style.pointerEvents = 'auto';
element.style.background = '#000';
const iframe = document.createElement('iframe');
iframe.src = screen.source;
iframe.title = 'Video screen';
iframe.allow = 'autoplay; encrypted-media; picture-in-picture';
iframe.allowFullscreen = true;
iframe.style.cssText = 'width:100%;height:100%;border:0;';
element.appendChild(iframe);
screen.object = new THREE.CSS3DObject(element);
cssScene.add(screen.object);
}
function removeIframe(screen) {
cssScene.remove(screen.object);
screen.object.element.remove();
screen.object = null;
}
function init(container) {
cssScene = new THREE.Scene();
cssRenderer = new THREE.CSS3DRenderer();
cssRenderer.setSize(window.innerWidth, window.innerHeight);
const element = cssRenderer.domElement;
element.id = 'video-layer';
element.style.position = 'absolute';
element.style.inset = '0';
element.style.pointerEvents = 'none';
container.prepend(element);
window.addEventListener('resize', () => cssRenderer.setSize(window.innerWidth, window.innerHeight));
}
function update(camera, avatarPosition, collidables) {
if (!cssRenderer) return;
const now = performance.now();
const checkOcclusion = now - lastOcclusionCheck > 400;
if (checkOcclusion) lastOcclusionCheck = now;
screens.forEach((screen) => {
if (!screen.anchor.parent || !screen.source) return;
screen.anchor.getWorldPosition(worldPosition);
const near = worldPosition.distanceTo(avatarPosition) < ACTIVATE_DISTANCE;
if (near && !screen.object) createIframe(screen);
if (!near && screen.object) removeIframe(screen);
if (!screen.object) return;
screen.anchor.matrixWorld.decompose(worldPosition, worldQuaternion, worldScale);
screen.object.position.copy(worldPosition);
screen.object.quaternion.copy(worldQuaternion);
const pixelsToMeters = screen.width / PIXEL_WIDTH;
screen.object.scale.set(pixelsToMeters, pixelsToMeters, pixelsToMeters);
// Hide the iframe when a wall stands between the camera and the screen
if (checkOcclusion) {
const direction = worldPosition.clone().sub(camera.position);
const distance = direction.length();
occlusionRay.set(camera.position, direction.normalize());
occlusionRay.far = distance - 1;
const blocked = occlusionRay.intersectObjects(collidables(), true).some((hit) => hit.object.visible && !hit.object.isSprite && hit.object !== screen.anchor && !(hit.object.material && hit.object.material.transparent));
screen.visible = !blocked;
}
screen.object.element.style.visibility = screen.visible ? 'visible' : 'hidden';
});
cssRenderer.render(cssScene, camera);
}
return { init, update, register, youtubeEmbed, channelUploadsEmbed };
})();