-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
409 lines (315 loc) · 10.6 KB
/
main.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { CSS3DRenderer } from 'three/addons/renderers/CSS3DRenderer.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import WebGL from 'three/addons/capabilities/WebGL.js';
import * as utils from './utils.js';
import coordinates from './coordinates.json';
let glRenderer, glScene, cssRenderer, cssScene, controls, camera, manager;
/////////////////////////////
// Renderer Features //
/////////////////////////////
function createWebGLRenderer() {
glRenderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
glRenderer.shadowMap.enabled = true;
glRenderer.setClearColor( 0xADD8E6 );
glRenderer.setSize( window.innerWidth, window.innerHeight );
glRenderer.domElement.style.position = 'absolute';
glRenderer.domElement.style.top = 0;
cssRenderer.domElement.appendChild( glRenderer.domElement );
}
function createCSS3DRenderer() {
cssRenderer = new CSS3DRenderer();
cssRenderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( cssRenderer.domElement );
}
function createLoadingManager() {
const htmlLoader = document.querySelector( '#loader' );
htmlLoader.style.display = 'block';
manager = new THREE.LoadingManager();
manager.onStart = function ( url, itemsLoaded, itemsTotal ) {
const str = '<div>' + 'Started loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' + '</div>';
const div = document.createElement( 'div' );
div.innerHTML = str;
htmlLoader.appendChild( div );
};
manager.onLoad = function ( ) {
openDialog();
showBackButton();
htmlLoader.setAttribute( 'style', 'display: none;' );
};
manager.onProgress = function ( url, itemsLoaded, itemsTotal ) {
const str = '<div>' + 'Loading file: ' + url + '.\nLoaded ' + itemsLoaded + ' of ' + itemsTotal + ' files.' + '</div>';
const div = document.createElement( 'div' );
div.innerHTML = str;
htmlLoader.appendChild( div );
};
manager.onError = function ( url ) {
const str = '<div>' + 'There was an error loading ' + url + '</div>';
const div = document.createElement( 'div' );
div.innerHTML = str;
htmlLoader.appendChild( div );
};
}
/////////////////////////////
// Scene Features //
/////////////////////////////
function addLight() {
const directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
directionalLight.position.set( 3, 3, 3 );
directionalLight.shadow.bias = -0.00019;
directionalLight.castShadow = true;
glScene.add( directionalLight )
}
function addModel() {
const loader = new GLTFLoader( manager );
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath( 'https://www.gstatic.com/draco/versioned/decoders/1.5.7/' );
dracoLoader.setDecoderConfig( {type: 'js'} );
loader.setDRACOLoader( dracoLoader );
loader.load( 'static/low_poly_computer_desk/scene_optimized_blender.glb', function ( gltf ) {
const model = gltf.scene;
const screen = model.getObjectByName( 'screen' );
screen.position.x -= 1;
screen.material.transparent = true;
screen.material.opacity = 0.4;
model.traverse( ( child ) => {
if ( child.isMesh ) {
child.castShadow = true;
child.receiveShadow = true;
}
} );
model.scale.set( 1 / 50, 1 / 50, 1 / 50 );
model.position.set( 0, 0, 0 );
glScene.add( model );
}, undefined, function ( error ) {
console.error( error );
} );
}
function addCSS( url, width, height, position, rotation ) {
const plane = utils.createPlane( width, height, position, rotation );
glScene.add( plane );
const cssObject = utils.createCSSObject( url, width, height, position, rotation );
cssScene.add( cssObject );
}
/////////////////////////////
// Camera Functionality //
/////////////////////////////
function createOrbitControls() {
controls = new OrbitControls( camera, glRenderer.domElement );
controls.enableDamping = true;
// Starting camera position
controls.object.position.set(
coordinates.start.position.x,
coordinates.start.position.y,
coordinates.start.position.z
);
// Starting camera target
controls.target = new THREE.Vector3(
coordinates.start.target.x,
coordinates.start.target.y,
coordinates.start.target.z
);
controls.update();
// Disable or Enable OrbitControls mouse
controls.enabled = true;
}
function createCameraTimeline() {
document.querySelector( '#go-to-desk-button' ).addEventListener( 'click', () => {
utils.animateCameraToDesk( controls );
} );
document.querySelector( '#back-btn' ).addEventListener( 'click', () => {
// Go back to start
if ( utils.isInDesk ) {
utils.animateCameraToStart( controls );
document.querySelector( '#dialog-default' ).showModal();
// Go back to desk
} else {
utils.animateCameraToDesk( controls );
}
} );
}
/////////////////////////////
// 3D Object Functionality //
/////////////////////////////
let raycaster, mouse, objectInclusions, isAnimating = [];
function setupObjectFunctionality() {
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
objectInclusions = [ 'telephone', 'floppy_disk', 'cd', 'top_paper'];
glRenderer.domElement.addEventListener( 'click', onObjectClick, false );
glRenderer.domElement.addEventListener( 'mousemove', onObjectHover, false );
}
function onObjectClick(event) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( glScene.children, true );
if ( intersects.length > 0 ) {
const object = intersects[0].object;
if ( object.name === 'monitor' || object.name === 'screen' ) {
utils.animateCameraToMonitor( controls );
}
}
}
function onObjectHover( event ) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( glScene.children, true );
if ( intersects.length > 0 ) {
const object = intersects[0].object;
if ( validateObject( object.name ) && utils.isInDesk ) {
animateObject( object );
}
}
}
function validateObject( objectName ) {
return objectInclusions.includes( objectName ) && !isAnimating.includes( objectName );
}
function animateObject( object ) {
let tween;
const rootNode = object.parent.parent;
if ( object.name === 'floppy_disk' ) {
tween = utils.animateObject( object, 0, 0, -5, 0, 1.5 );
} else if ( object.name === 'top_paper' ) {
const pen = rootNode.getObjectByName( 'pen' );
const bottomPaper = rootNode.getObjectByName( 'bottom_paper' );
tween = utils.animateObject( object, 0, 45, 0, 0, 1 );
utils.animateObject( pen, 0, 2000, 0, 0, 1 );
utils.animateObject( bottomPaper, 0, 40, 0, .2, .5 );
} else {
tween = utils.animateObject( object, 0, 15, 0, 0, 1.5 );
}
tween.eventCallback("onReverseComplete", () => {
isAnimating = isAnimating.filter( e => e !== object.name );
});
isAnimating.push( object.name );
}
/////////////////////////////
// Helpers //
/////////////////////////////
function enableCameraHelper() {
controls.addEventListener("change", event => {
console.log("Position");
console.log( controls.object.position );
console.log("Target");
console.log( controls.target );
console.log( controls.object.rotation );
});
}
function enableGridHelper() {
const size = 10;
const divisions = 10;
const gridHelper = new THREE.GridHelper( size, divisions );
glScene.add( gridHelper );
const axesHelper = new THREE.AxesHelper( 5 );
glScene.add( axesHelper );
}
/////////////////////////////
// Initialization //
/////////////////////////////
function initialize() {
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
glScene = new THREE.Scene();
cssScene = new THREE.Scene();
cssScene.scale.set( 0.0005, 0.0005, 0.00065 );
createLoadingManager();
createCSS3DRenderer();
createWebGLRenderer();
createOrbitControls();
createCameraTimeline();
setupObjectFunctionality();
// Helpers
// enableCameraHelper();
// enableGridHelper();
// Add features
addLight();
addModel();
addCSS(
'static/screens/desktop.html',
1070,
780,
new THREE.Vector3( -156, 3905, 1181 ),
new THREE.Vector3( -0.1, 0, 0 ),
);
const blackPlane = utils.createBlackPlane(
1400,
1000,
new THREE.Vector3( -155, 3910, 1180 ),
new THREE.Vector3( -0.1, 0, 0 ),
);
glScene.add( blackPlane );
checkWebGL();
}
function correctOrientation() {
if ( window.matchMedia( "(orientation: portrait)" ).matches ) {
const div = document.createElement( 'div' );
const button = document.createElement( 'button' );
const warning = `
<div style="width: 50vw;">
This website is best experienced in a laptop or computer.
If you do still wish to continue, orient it to landscape.
Click the button after reorientating.
</div>
`;
button.onclick = () => { window.location.reload( true ) };
button.innerHTML = 'Reload page';
div.setAttribute( 'style', `
display: flex;
height: 50vh;
justify-content: center;
align-items: center;
flex-direction: column;
margin: 1rem;
` );
div.innerHTML = warning;
div.appendChild( button );
document.querySelector( '#warning' ).appendChild( div );
}
if ( window.matchMedia( "(orientation: landscape)" ).matches ) {
initialize();
}
}
function openDialog() {
document.querySelector( '#dialog-default' ).style.display = 'block';
document.querySelector( '#dialog-default' ).showModal();
};
function showBackButton() {
document.querySelector( '#back-btn' ).style.display = 'block';
}
correctOrientation();
/////////////////////////////
// Rendering //
/////////////////////////////
function animate() {
requestAnimationFrame( animate );
controls.update();
glRenderer.render( glScene, camera );
cssRenderer.render( cssScene, camera );
if ( utils.isInMonitor ) {
glRenderer.domElement.style.pointerEvents = 'none';
} else {
glRenderer.domElement.style.pointerEvents = 'auto';
}
}
function checkWebGL() {
if ( WebGL.isWebGLAvailable() ) {
animate();
} else {
const warning = WebGL.getWebGLErrorMessage();
document.querySelector( '#warning' ).appendChild( warning );
}
}
window.addEventListener( 'resize', () => {
if ( window.innerHeight > window.innerWidth ) {
window.location.reload( true );
}
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
glRenderer.setSize( window.innerWidth, window.innerHeight );
cssRenderer.setSize( window.innerWidth, window.innerHeight );
} )