-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
337 lines (250 loc) · 9.73 KB
/
app.js
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { FontLoader } from 'three/addons/loaders/FontLoader.js';
import { TextGeometry } from 'three/addons/geometries/TextGeometry.js';
import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
let camera, scene, renderer, stats;
let fbxModel;
let cubeCamera, cubeRenderTarget;
let controls;
const mouse = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
let textMesh;
let lastTime = 0;
const updateInterval = 1000 / 60;
let videoPlaying = false;
let currentVideo = null;
init();
function init() {
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setAnimationLoop(animate);
renderer.setClearColor(0x00000, 1);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResized);
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 75;
scene = new THREE.Scene();
cubeRenderTarget = new THREE.WebGLCubeRenderTarget(34, {
format: THREE.RGBAFormat,
generateMipmaps: true,
minFilter: THREE.LinearMipmapLinearFilter,
encoding: THREE.sRGBEncoding
});
cubeCamera = new THREE.CubeCamera(1, 1000, cubeRenderTarget);
scene.add(cubeCamera);
let loader = new FBXLoader();
loader.load('texture/hex_earth.fbx', function (object) {
fbxModel = object;
fbxModel.scale.set(0.04, 0.04, 0.04);
scene.add(fbxModel);
hideLoader();
});
loader = new FontLoader();
loader.load('https://threejs.org/examples/fonts/helvetiker_regular.typeface.json', function (font) {
addTextToCubeFace(font, 'Start', new THREE.Vector3(0, 0, 15), 5);
});
const ambientLight = new THREE.AmbientLight(0xffffff, 4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 5);
directionalLight.position.set(15, 15, 15);
scene.add(directionalLight);
controls = new OrbitControls(camera, renderer.domElement);
controls.autoRotate = false;
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = true;
const firstVideo = document.getElementById('myVideo');
const secondVideo = document.getElementById('mySecondVideo');
const thirdVideo = document.getElementById('myThirdVideo')
firstVideo.addEventListener('ended', onFirstVideoEnded);
secondVideo.addEventListener('ended', onSecondVideoEnded);
thirdVideo.addEventListener('ended', onThirdVideoEnded)
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('click', onMouseClick);
}
function addTextToCubeFace(font, text, direction, offset) {
const textGeometry = new TextGeometry(text, {
font: font,
size: 2,
depth: 1,
curveSegments: 6,
});
textGeometry.center();
const textMaterial = new THREE.MeshBasicMaterial({ color: 0x000000 });
textMesh = new THREE.Mesh(textGeometry, textMaterial);
textMesh.position.set(0, 0, 15);
scene.add(textMesh);
}
function onMouseMove(event) {
const rect = renderer.domElement.getBoundingClientRect();
mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
}
function onMouseClick(event) {
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
const rect = renderer.domElement.getBoundingClientRect();
mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObject(textMesh);
if (intersects.length > 0) {
if (!videoPlaying) {
zoomInCamera();
setTimeout(() => {
var firstVideo = document.getElementById('myVideo');
firstVideo.muted = false; // Unmute the second video
firstVideo.play(); // Play the second video
document.getElementById('videoContainer').style.display = 'block'
videoPlaying = true;
currentVideo = 'first';
}, 1000);
}
}
}
function animate(time) {
requestAnimationFrame(animate);
const deltaTime = time - lastTime;
if (deltaTime > updateInterval) {
lastTime = time - (deltaTime % updateInterval); // Adjust lastTime to account for potential frame lag
if (fbxModel) {
fbxModel.updateMatrixWorld();
}
cubeCamera.update(renderer, scene);
if (controls && typeof controls.update === 'function') {
controls.update();
} else {
console.error('controls is undefined or does not have an update method');
}
renderer.render(scene, camera);
}
}
function hideLoader() {
const loader = document.querySelector('.progress-loader');
loader.style.display = 'none';
}
function zoomInCamera(callback) {
const duration = 1000;
const start = performance.now();
const initialCameraPosition = camera.position.clone();
const targetCameraPosition = new THREE.Vector3(0, 0, 10);
function animateZoom(time) {
const elapsed = time - start;
const progress = Math.min(elapsed / duration, 1);
camera.position.z = initialCameraPosition.z + (targetCameraPosition.z - initialCameraPosition.z) * progress;
if (progress < 1) {
requestAnimationFrame(animateZoom);
} else {
camera.position.copy(targetCameraPosition);
if (callback) callback();
}
}
requestAnimationFrame(animateZoom);
}
function zoomOutCamera(callback) {
const duration = 3000;
const start = performance.now();
const initialCameraPosition = camera.position.clone();
const targetCameraPosition = new THREE.Vector3(0, 0, 50);
function animateZoomOut(time) {
const elapsed = time - start;
const progress = Math.min(elapsed / duration, 1);
camera.position.z = initialCameraPosition.z + (targetCameraPosition.z - initialCameraPosition.z) * progress;
camera.updateProjectionMatrix();
if (progress < 1) {
requestAnimationFrame(animateZoomOut);
} else {
camera.position.copy(targetCameraPosition);
if (callback) callback();
}
}
requestAnimationFrame(animateZoomOut);
}
function onFirstVideoEnded() {
try {
zoomOutCamera();
rotateGlobe();
setTimeout(() => {
zoomInCamera();
}, 7000)
} catch (error) {
console.error('Error during camera operations:', error);
}
document.getElementById('videoContainer').style.display = 'none';
setTimeout(() => {
let secondVideo = document.getElementById('mySecondVideo');
secondVideo.style.display = 'block';
secondVideo.muted = false; // Unmute the second video
secondVideo.play(); // Play the second video
videoPlaying = true;
currentVideo = 'second';
}, 8000);
}
function onSecondVideoEnded() {
try {
zoomOutCamera();
rotateGlobe();
setTimeout(() => {
zoomInCamera();
}, 7000)
} catch (error) {
console.error('Error during camera operations:', error);
}
document.getElementById('mySecondVideo').style.display = 'none';
setTimeout(() => {
let thirdVideo = document.getElementById('myThirdVideo');
thirdVideo.style.display = 'block';
thirdVideo.muted = false; // Unmute the second video
thirdVideo.play(); // Play the second video
videoPlaying = true;
currentVideo = 'third';
}, 8000);
}
function onThirdVideoEnded() {
document.getElementById('mySecondVideo').style.display = 'none';
document.getElementById('videoContainer').style.display = 'none';
document.getElementById('myThirdVideo').style.display = 'none'
zoomOutCamera(() => {
rotateGlobe(() => {
videoPlaying = false;
currentVideo = null;
document.addEventListener('click', onMouseClick);
});
});
}
function rotateGlobe(callback) {
controls.autoRotate = false;
controls.update();
function onZoomOutComplete() {
controls.autoRotate = true;
controls.autoRotateSpeed = 30;
const duration = 4000;
const start = performance.now();
const initialRotation = fbxModel.rotation.y;
const targetRotation = initialRotation + Math.PI * 2;
function animateRotation(time) {
const elapsed = time - start;
const progress = Math.min(elapsed / duration, 1);
fbxModel.rotation.y = initialRotation + (targetRotation - initialRotation) * progress;
if (progress < 1) {
requestAnimationFrame(animateRotation);
} else {
controls.autoRotate = false;
if (callback) callback();
}
}
requestAnimationFrame(animateRotation);
}
zoomOutCamera(onZoomOutComplete);
}
function onWindowResized() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.near = 1;
camera.far = 5000;
camera.updateProjectionMatrix();
}