-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
1907 lines (1606 loc) · 53.1 KB
/
index.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (THREE, window, document, PI) {
try {
// $$$_INJECT_VR_$$$
// $$$_INJECT_AUDIO_$$$
// $$$_INJECT_EMOJI_$$$
// $$$_INJECT_TUTORIAL_$$$
// tutorialCompleted = true;
var getElementById = function (id) {
return document.getElementById(id);
};
var STR_BLOCK = 'block';
var STR_NONE = 'none';
var STR_DIV = 'div';
var STR_IMG = 'img';
var COLOR_WHITE = '#fff';
var W = window.innerWidth;
var H = window.innerHeight;
var Dpr = 2;
var RADIUS_EARTH = 10;
var RADIUS_LAND = 10.1;
var RADIUS_OCEAN = 9.8;
var RADIUS_UFO_POS = 11;
var SPECIMENS_AMOUNT = 10;
var ANGULAR_VEL = PI / 600;
var ANGULAR_ACC = ANGULAR_VEL / 30;
var UFO_PHI = PI * 0.42;
var UFO_THETA = 0;
var LAYER_DEFAULT = 0;
var LAYER_EARTH = 2;
// var LAYER_BLOOM = 3;
var MAX_TRACK_POINTS = 9;
var MAX_MEDIUM = 8;
var MAX_MEDIUM_PRESSURE = 5e6;
var SPECIMEN_NEAR_THRES = 0.5;
var SPECIMEN_AVAILABLE_THRES = 0.045;
var CAMERA_DISTANT_Z = 20;
var CAMERA_CLOSE_Z = 15;
var CAMERA_ZOOM_VEL = (CAMERA_DISTANT_Z - CAMERA_CLOSE_Z) / 20;
var CAMERA_ROT_MAX_X = 0.36;
var CAMERA_ROT_MIN_X = 0;
var CAMERA_ROT_VEL = (CAMERA_ROT_MAX_X - CAMERA_ROT_MIN_X) / 20;
var CAMERA_STATES = {
distant$: 0,
close$: 1,
zoomingIn$: 2,
zoomingOut$: 3
};
var UFO_STATES = {
idle$: 0,
flying$: 1,
increasingRay$: 2,
reducingRay$: 3,
raying$: 4,
takingSpec$: 5,
rayFailed$: 6,
increasingLaser$: 7,
reducingLaser$: 8,
lasing$: 9,
laseCompleted$: 10
};
var GAME_STATES = {
welcome$: 0, // display only once when web page loads
welcomeEasingOut$: 1, // animation from welcom to inGame
inGame$: 2,
gameOverEasingIn$: 3,
gameOver$: 4,
gameOverEasingOut$: 5 // from gameOver to inGame
};
var BEFORE_GAME_ANIMATION_DURATION = 3;
// DEBUG
BEFORE_GAME_ANIMATION_DURATION = 0;
// DEBUG END
var GAME_OVER_ANIMATION_DURATION = 60;
var baseAxisX = new THREE.Vector3(1, 0, 0);
var baseAxisY = new THREE.Vector3(0, 1, 0);
var minScale = new THREE.Vector3(0, 0, 0);
var maxScale = new THREE.Vector3(1, 1, 1);
// var resources = {
// earthTexture: null
// };
var renderer, scene, sceneRTT, camera, cameraRTT, lights, vrControls;
var rtTexture, rtMesh;
var rttDprRatio = Math.max(2, Math.round(H / 250));
window.rttOn = true;
var uiCanvas, uiCtx;
// var composer;
var keys = [];
var pivot = new THREE.Group();
var earth, earthSurface;
var clouds;
var land, landSurface;
var ufo = new THREE.Group();
var ufoRay, ufoIndicator, ufoLaser;
var ufoMixer, ufoIndicatorMixer, ufoRayMixer, ufoLaserMixer;
var ufoIdleAction, ufoIndicatorAction, ufoRayAction, ufoLaserAction;
var track = new THREE.Group();
var pathLength = 0;
var lastPosition;
var trackMediaMap = {};
var angularVel = { phi: 0, theta: 0 };
var ufoOriginRotation;
var clock;
var trackTime;
var gameState = GAME_STATES.welcome$;
var cameraState = CAMERA_STATES.distant$;
var ufoState = UFO_STATES.idle$;
var cameraBeforeGamePosition = new THREE.Vector3(-50, -0.65, RADIUS_UFO_POS + 2);
var cameraInGamePosition = new THREE.Vector3(0, 0, CAMERA_DISTANT_Z);
var pivotInGamePosition = new THREE.Vector3(0, 0, 0);
var pivotGameOverPosition = new THREE.Vector3(-100, 0, -250);
var ufoBeforeGamePosition = new THREE.Vector3(-50, 0, RADIUS_UFO_POS);
var ufoInGamePosition = getVectorFromSphCoord(RADIUS_UFO_POS, UFO_PHI, UFO_THETA);
var ufoGameOverPosition = new THREE.Vector3(-3, 1, 16);
var inGameKeyPressed = false;
var colors = {
primary$: '#DD4391',
bgTop$: '#0e1a25',
oceanLevels$: ['#31d9d9', '#32c5d9', '#44a9c8', '#2694b9', '#067499'],
land$: '#9be889'
};
var inGameUi = getElementById('g');
var spaceKeyBreak = true;
var directionKeys = [87, 38, 83, 40, 65, 37, 68, 39];
// DEBUG
var stats;
// DEBUG END
var specimens = {
group$: new THREE.Group(),
geometry$: new THREE.SphereGeometry(0.1, 16, 16),
material$: new THREE.MeshToonMaterial({ color: '#ffadd2' }),
minAngle$: Infinity,
near$: false,
available$: false,
targetItem$: null,
init$() {
pivot.add(this.group$);
this.group$.layers.set(LAYER_EARTH);
this.add$(UFO_PHI + 0.42, UFO_THETA + 0.42);
var origin = worldToLocal(RADIUS_EARTH, UFO_PHI, UFO_THETA);
for (var i = 0; i < SPECIMENS_AMOUNT - 1; ++i) {
do {
var phi = randRad();
var theta = randRad();
var pos = getVectorFromSphCoord(RADIUS_EARTH, phi, theta);
} while (pos.angleTo(origin) <= SPECIMEN_NEAR_THRES);
this.add$(phi, theta);
}
},
reset$() {
this.group$.remove(...this.group$.children);
for (var i = 0; i < SPECIMENS_AMOUNT; ++i) {
this.add$(randRad(), randRad());
}
this.targetItem$ = null;
},
add$(phi, theta) {
var point = new THREE.Mesh(this.geometry$, this.material$);
point.position.setFromSphericalCoords(RADIUS_EARTH, phi, theta);
point.visible = false;
this.group$.add(point);
},
remove$(item) {
this.group$.remove(item);
},
count$() {
return this.group$.children.length;
},
update$() {
this.minAngle$ = calcMinAngle(this.group$.children);
this.near$ = this.minAngle$ <= SPECIMEN_NEAR_THRES;
this.available$ = this.minAngle$ <= SPECIMEN_AVAILABLE_THRES;
this.updateTargetItem$();
},
updateTargetItem$() {
if (ufoState === UFO_STATES.takingSpec$ && this.available$ && !this.targetItem$) {
this.targetItem$ = getNearest(this.group$.children);
if (!this.targetItem$) return;
var pos = worldToLocal(RADIUS_EARTH, UFO_PHI, UFO_THETA);
this.targetItem$.position.set(pos.x, pos.y, pos.z);
this.targetItem$.visible = true;
}
if (this.targetItem$) {
const sph = new THREE.Spherical().setFromVector3(this.targetItem$.position);
if (sph.radius < RADIUS_UFO_POS) {
sph.radius += 0.02;
this.targetItem$.position.setFromSpherical(sph);
} else {
// Catch a DNA
this.remove$(this.targetItem$);
this.targetItem$ = null;
updateCanvas();
dnaCollection.add$();
!this.count$() && setTimeout(() => updateGameState(GAME_STATES.gameOverEasingIn$, 1), 500);
}
}
}
};
var medium = {
group$: new THREE.Group(),
geometry$: new THREE.SphereGeometry(0.15, 16, 16),
material$: new THREE.MeshToonMaterial({ color: '#ff4d4f', transparent: true, opacity: 0.7 }),
minAngle$: Infinity,
targetItem$: null,
popupsEl$: getElementById('p'),
lastUpdated$: Date.now(),
progress$: {
_clock$: null,
running$: false,
result$: null,
},
news$: null,
init$({ news$ }) {
this.news$ = news$;
this.group$.layers.set(LAYER_EARTH);
pivot.add(this.group$);
// DEBUG
// this.add$(UFO_PHI, UFO_THETA);
// DEBUG END
},
reset$() {
var children = this.group$.children;
for (var i = children.length - 1; i >= 0; i--) {
this.remove$(children[i]);
}
this.targetItem$ = null;
},
count$() {
return this.group$.children.length;
},
add$(phi, theta) {
var media = new THREE.Mesh(this.geometry$, this.material$);
media.position.setFromSphericalCoords(RADIUS_EARTH, phi, theta);
media._viewed = getRandimInt(1, 10);
media._maxV = getRandimInt(100, 1e5);
media._p = createElement(STR_DIV, this.popupsEl$, 'p');
this.group$.add(media);
this.updateText$(media);
var _n = this.news$.add$(media._viewed);
media._n = _n;
},
remove$(item) {
if (item) {
item._p && this.popupsEl$.removeChild(item._p)
item._p = null;
item._n = null;
this.group$.remove(item);
}
},
getTotalViewed$() {
return this.group$.children.reduce((prev, curr) => (
prev + (curr._d ? 0 :curr._viewed)
), 0);
},
update$() {
if (!canMediaGenerate()) return;
if (this.count$() < MAX_MEDIUM) {
var key = Math.floor(pathLength / 30);
if (key && !trackMediaMap[key]) {
var point = track.children.sort((a, b) => a._t - b._t)[0];
if (point) {
var sph = new THREE.Spherical();
sph.setFromVector3(point.position);
sph.phi += randFloatSpread(0.6);
sph.theta += randFloatSpread(0.6);
this.add$(sph.phi, sph.theta);
track.remove(point);
}
}
trackMediaMap[key] = true;
}
this.minAngle$ = calcMinAngle(this.group$.children);
this.updateTargetItem$();
this.updatePopups$();
},
updateTargetItem$() {
this.targetItem$ = this.minAngle$ < 0.03 ? getNearest(this.group$.children) : null;
if (ufoState === UFO_STATES.lasing$) {
var progress = this.progress$;
if (keys[32]) {
if (!progress.running$) {
this.runProgress$();
} else if (Date.now() - progress._clock$ >= 1e3) {
this.finishProgress$();
}
} else {
this.stopProgress$();
}
}
},
runProgress$() {
var { progress$, targetItem$ } = this;
if (targetItem$) {
progress$.running$ = true;
targetItem$._p.classList.add('pl');
progress$._clock$ = Date.now();
}
},
stopProgress$() {
var { progress$, targetItem$ } = this;
if (targetItem$) {
targetItem$._p.classList.remove('pl');
progress$.running$ = false;
progress$.result$ = false;
}
},
finishProgress$() {
var { progress$, targetItem$, news$ } = this;
if (!targetItem$) return;
progress$.running$ = false;
progress$.result$ = true;
targetItem$._p.classList.add('pf');
targetItem$.visible = false;
targetItem$._d = true;
news$.set404$(targetItem$._n);
setTimeout(() => {
if (targetItem$) {
targetItem$._p && targetItem$._p.classList.add('po');
setTimeout(() => this.remove$(targetItem$), 500);
}
}, 3e3);
audio.playEffect$(EFFECT_MEDIA);
if (tutorialState === TUTORIAL.AFTER_MEDIUM_APPEAR$
|| tutorialState === TUTORIAL.AFTER_MEDIA$
) {
setTimeout(() => setTutorial(TUTORIAL.AFTER_MEDIA_CAUGHT$), 1e3);
}
updateCanvas();
},
updatePopups$() {
var updateNumber = Date.now() - this.lastUpdated$ > 1e3;
if (updateNumber) {
this.lastUpdated$ = Date.now();
}
var updated = false;
this.group$.children.forEach(media => {
var pos = worldToScreen(media);
// uiCtx.fillRect(pos.x / uiDprRatio, pos.y / uiDprRatio, 2, 2);
var popup = media._p;
var { style } = popup;
style.left = Math.round(pos.x) + 'px';
style.top = Math.round(pos.y + 10) + 'px';
style.opacity = pos.z < 5 ? 0.2 : 1;
if (!media._d
&& (media !== this.targetItem$ || ufoState !== UFO_STATES.lasing$)
&& updateNumber
) {
media._viewed += getRandimInt(0, media._maxV);
this.updateText$(media);
this.news$.updateViewed$(media._n, popup.innerText);
updated = true;
}
});
if (updated) {
updateCanvas();
medium.getTotalViewed$() >= MAX_MEDIUM_PRESSURE
&& setTimeout(() => updateGameState(GAME_STATES.gameOverEasingIn$), 500);
}
},
updateText$(item) {
var popup = item._p;
if (item._viewed >= 1e6) {
const text = Math.round(item._viewed / 1e5) / 10 + 'M';
popup.setAttribute('class', 'p R');
popup.innerText = text + ' VIEWED';
}
else if (item._viewed >= 1e4) {
const text = Math.round(item._viewed / 100) / 10 + 'K';
popup.setAttribute('class', 'p y');
popup.innerText = text + ' VIEWED';
}
else {
popup.innerText = item._viewed + ' VIEWED';
}
}
};
function calcMinAngle(children) {
return children.reduce(function (min, item) {
var angle = ufo.position.angleTo(item.localToWorld(new THREE.Vector3()));
item.userData.angle = angle;
return Math.min(min, angle);
}, Infinity);
}
function getNearest(children) {
return children.reduce(function (a, b) {
if (!b._d && (!a || b.userData.angle < a.userData.angle)) {
return b;
}
return a;
}, null);
}
var news = {
el$: getElementById('t'),
show$() {
this.el$.style.display = STR_BLOCK;
},
hide$() {
this.el$.style.display = STR_NONE;
},
reset$() {
this.hide$();
this.el$.innerHTML = '';
},
add$() {
var tweet = createElement(STR_DIV, this.el$, 'T');
var left = createElement(STR_DIV, tweet, 'l');
var avatar = createElement(STR_IMG, left, 'a');
avatar.setAttribute('src', getEmojiAvatar());
createElement(STR_DIV, left, 'v');
// viewed.innerText = '12K VIEWED';
var right = createElement(STR_DIV, tweet, 'r');
var name = createElement(STR_DIV, right, 'n');
name.innerText = '@' + getRandomName();
var content = createElement(STR_DIV, right, 'c');
content.innerText = getRandomTweet();
tweet.parentNode.scrollTop = tweet.offsetTop;
this.show$();
audio.playEffect$(EFFECT_TWEET);
// tweetList.push(tweet);
return tweet;
},
updateViewed$(dom, text) {
dom.getElementsByClassName('v')[0].innerText = text;
},
set404$(dom) {
dom.className = 'T TT';
this.updateViewed$(dom, 'NA');
dom.children[1].children[1].innerText = '(404) NOT FOUND';
dom.parentNode.scrollTop = dom.offsetTop;
}
};
var dnaCollection = {
el$: getElementById('h'),
show$() {
this.el$.style.display = STR_BLOCK;
},
hide$() {
this.el$.style.display = STR_NONE;
},
reset$() {
this.hide$();
this.el$.innerText = '';
},
add$() {
var img = createElement(STR_IMG, this.el$, 'a d');
img.setAttribute('src', getEmojiDna());
this.show$();
}
};
var wiggler = {
el$: getElementById('w'),
targetEl$: getElementById('wt'),
pointerEl$: getElementById('wp'),
length$: 16,
targetStart$: 0,
targetEnd$: 0,
pointerLength$: 0.3,
result$: null,
initData$(angle) {
var targetLen = 1.5 + 0.075 / (0.03 + angle);
this.targetStart$ = (this.length$ - targetLen) / 2;
this.targetEnd$ = this.length$ - this.targetStart$;
var style = this.targetEl$.style;
style.marginLeft = this.targetStart$ + 'vh';
style.width = targetLen + 'vh';
},
checkResult$() {
this.pointerEl$.style.animationPlayState = 'paused';
var pointerPos = parseFloat(window.getComputedStyle(this.pointerEl$).left, 10) / window.innerHeight * 100;
return pointerPos >= this.targetStart$ - this.pointerLength$
&& pointerPos <= this.targetEnd$;
},
update$() {
this.updatePos$();
switch (ufoState) {
case UFO_STATES.raying$:
this.el$.style.opacity = 1;
if (!keys[32]) {
this.result$ = this.checkResult$();
}
break;
case UFO_STATES.idle$:
case UFO_STATES.rayFailed$:
this.el$.style.opacity = 0;
this.pointerEl$.style.animationPlayState = 'running';
break;
}
if (ufoState !== UFO_STATES.raying$) {
this.result$ = null;
}
},
updatePos$() {
if ([UFO_STATES.raying$, UFO_STATES.rayFailed$, UFO_STATES.takingSpec$].includes(ufoState)) {
var pos = worldToScreen(ufo);
var style = this.el$.style;
style.left = Math.round(pos.x) + 'px';
style.top = Math.round(pos.y) + 'px';
}
}
}
var failMsg = {
el$: getElementById('f'),
_clock$: null,
running$: false,
update$() {
if (ufoState === UFO_STATES.rayFailed$) {
var el = this.el$;
var pos = worldToScreen(ufo);
var style = this.el$.style;
style.left = Math.round(pos.x) + 'px';
style.top = Math.round(pos.y - window.innerHeight / 12) + 'px';
if (!el.className || el.className === 'o') {
this.running$ = true;
el.className = 'i';
this._clock$ = Date.now();
} else if (Date.now() - this._clock$ >= 1e3) {
el.className = 'o';
this.running$ = false;
}
}
}
};
main();
function main() {
// DEBUG
initDebug();
// DEBUG END
initEmoji();
initScene();
initLight();
createEarth();
createUfo();
createClouds();
createLand();
createSky();
pivot.add(track);
scene.add(pivot);
specimens.init$();
medium.init$({ news$: news });
initRenderer();
clock = new THREE.Clock();
window.addEventListener('resize', onWindowResize, false);
initControl();
vrControls = new THREE.VRControls(camera);
updateGameState(GAME_STATES.welcome$);
animate();
var loading = getElementById('x');
loading.innerHTML = 'PRESS ENTER';
}
function initScene() {
// ====== Main ======
scene = new THREE.Scene();
sceneRTT = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, W / H, 0.1, 1e6);
// camera.position.z = CAMERA_DISTANT_Z;
camera.layers.enable(LAYER_EARTH);
var bg = new THREE.BoxGeometry(5e3, 5e3, 5e3);
var bgMat = new THREE.MeshBasicMaterial({
color: colors.bgTop$,
side: THREE.BackSide
});
var bgMesh = new THREE.Mesh(bg, bgMat);
scene.add(bgMesh);
// ====== RTT ======
var width = W / rttDprRatio;
var height = H / rttDprRatio;
cameraRTT = new THREE.OrthographicCamera(
width / - 2,
width / 2,
height / 2,
height / - 2,
-1e4,
1e4
);
cameraRTT.position.z = 100;
rtTexture = new THREE.WebGLRenderTarget(
width,
height,
{
minFilter: THREE.NearestFilter,
magFilter: THREE.NearestFilter,
format: THREE.RGBFormat
}
);
var plane = new THREE.PlaneBufferGeometry(width, height);
var mat = new THREE.MeshBasicMaterial({
map: rtTexture.texture
});
rtMesh = new THREE.Mesh(plane, mat);
rtMesh.position.z = -100;
sceneRTT.add(rtMesh);
}
function initLight() {
lights = {};
lights.ambient = new THREE.AmbientLight(COLOR_WHITE, 0.7);
lights.ambient.layers.enable(LAYER_EARTH);
lights.ambient.layers.disable(LAYER_DEFAULT);
scene.add(lights.ambient);
lights.key = new THREE.DirectionalLight(COLOR_WHITE, 0.4);
lights.key.position.set(0, 0.5, 1);
lights.key.layers.enableAll();
lights.key.castShadow = true;
scene.add(lights.key);
lights.spot = new THREE.SpotLight('#fc6', 0.25, 100, PI / 12, 0.5, 2);
lights.spot.position.set(0, 5, 20);
lights.spot.lookAt(0, 0, 0);
lights.spot.shadow.mapSize.width = 1024;
lights.spot.shadow.mapSize.height = 1024;
lights.spot.layers.enableAll();
lights.fillTop = new THREE.DirectionalLight('#888', 1);
lights.fillTop.position.set(0.5, 1, 0.75);
lights.fillBottom = new THREE.DirectionalLight('#555', 1);
lights.fillBottom.position.set(-0.5, -1, -0.75);
lights.fillTop.layers.enable(LAYER_DEFAULT);
lights.fillBottom.layers.enable(LAYER_DEFAULT);
lights.fillTop.layers.disable(LAYER_EARTH);
lights.fillBottom.layers.disable(LAYER_EARTH);
pivot.add(lights.fillTop);
pivot.add(lights.fillBottom);
}
function initRenderer() {
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
Dpr = (window.devicePixelRatio) ? window.devicePixelRatio : 1;
renderer.setPixelRatio(Dpr);
renderer.autoClear = false;
renderer.setClearColor(colors.bgTop$, 0.0);
renderer.xr.enabled = true;
document.body.appendChild(renderer.domElement);
// ====== UI ======
uiCanvas = getElementById('u');
uiCtx = uiCanvas.getContext('2d');
onWindowResize();
}
function showInGameUI() {
inGameUi.style.display = STR_BLOCK;
}
function hideInGameUI() {
inGameUi.style.display = STR_NONE;
}
function createEarth() {
var geo = new THREE.IcosahedronGeometry(RADIUS_OCEAN, 4);
earthSurface = [];
for (var i = 0; i < geo.vertices.length; ++i) {
earthSurface.push({
x: geo.vertices[i].x,
y: geo.vertices[i].y,
z: geo.vertices[i].z,
delta: Math.random() * PI * 2
});
}
var mat = new THREE.MeshPhongMaterial({
color: colors.oceanLevels$[0],
flatShading: true,
vertexColors: true,
shininess: 0.8
});
earth = new THREE.Mesh(geo, mat);
earth.layers.set(LAYER_EARTH);
earth.receiveShadow = true;
pivot.add(earth);
}
function createUfo() {
var ufoCore = new THREE.Mesh(
new THREE.SphereGeometry(0.25, 32, 32),
new THREE.MeshToonMaterial({ color: '#bfbfbf' })
);
ufoCore.position.y = -0.05;
var ufoPlate = new THREE.Mesh(
new THREE.ConeGeometry(0.5, 0.25, 32),
new THREE.MeshToonMaterial({ color: '#8c8c8c' })
);
ufoIndicator = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.05, 32, 64),
new THREE.MeshBasicMaterial({
color: colors.oceanLevels$[0],
transparent: true,
opacity: 0
})
);
ufoIndicator.rotateX(Math.PI / 2);
ufoIndicator.position.y = -0.06;
ufoRay = new THREE.Mesh(
new THREE.ConeGeometry(0.45, 0.8, 32),
new THREE.MeshToonMaterial({ color: '#faad14', transparent: true, opacity: 0.5 })
);
ufoRay.position.y = -0.35;
ufoRay.scale.set(0, 0, 0);
ufoLaser = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.15, 0.76, 32),
new THREE.MeshToonMaterial({ color: '#dd4491', transparent: true, opacity: 0.5 })
);
ufoLaser.position.y = -0.4;
ufoLaser.scale.set(0, 0, 0);
ufo.position.set(...ufoInGamePosition.toArray());
ufo.rotation.x = 1;
ufo.layers.set(LAYER_DEFAULT);
ufo.add(ufoCore, ufoPlate, ufoIndicator, ufoRay, ufoLaser);
scene.add(ufo);
ufoOriginRotation = ufo.rotation.clone();
initUfoMixer();
initUfoIndicatorMixer();
initUfoRayMixer();
initUfoLaserMixer();
}
function initUfoMixer() {
ufoMixer = new THREE.AnimationMixer(ufo);
var pos1 = ufo.position;
var pos2 = getVectorFromSphCoord(RADIUS_UFO_POS + 0.28, UFO_PHI, UFO_THETA);
var posTrack = new THREE.VectorKeyframeTrack(
'.position',
[0, 0.8],
[pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z],
// THREE.InterpolateSmooth
);
var clip = new THREE.AnimationClip('UfoIdle', 0.8, [posTrack]);
ufoIdleAction = ufoMixer.clipAction(clip);
ufoIdleAction.loop = THREE.LoopPingPong;
ufoIdleAction.play();
}
function initUfoIndicatorMixer() {
ufoIndicatorMixer = new THREE.AnimationMixer(ufoIndicator);
ufoIndicatorAction = ufoIndicatorMixer.clipAction(
new THREE.AnimationClip('UfoIndicator', 1, [
new THREE.NumberKeyframeTrack('.material.opacity', [0, 1], [0, 1])
])
);
ufoIndicatorAction.loop = THREE.LoopPingPong;
ufoIndicatorAction.play();
}
function initUfoRayMixer() {
ufoRayMixer = new THREE.AnimationMixer(ufoRay);
ufoRayAction = ufoRayMixer.clipAction(
new THREE.AnimationClip('UfoRay', 1.2, [
new THREE.VectorKeyframeTrack(
'.scale',
[0, 1.2],
[1, 1, 1, 0.5, 1, 0.5]
)
])
);
ufoRayAction.loop = THREE.LoopPingPong;
}
function initUfoLaserMixer() {
ufoLaserMixer = new THREE.AnimationMixer(ufoLaser);
ufoLaserAction = ufoLaserMixer.clipAction(
new THREE.AnimationClip('UfoLaser', 0.3, [
new THREE.VectorKeyframeTrack(
'.scale',
[0, 0.3],
[1, 1, 1, 0.4, 1, 0.4]
)
])
);
ufoLaserAction.loop = THREE.LoopPingPong;
}
function addPointToTrack() {
var point;
if (track.children.length < MAX_TRACK_POINTS) {
point = new THREE.Object3D();
updatePos();
track.add(point);
} else {
point = track.children[0];
updatePos();
}
function updatePos() {
point.position.setFromSphericalCoords(RADIUS_EARTH, UFO_PHI, UFO_THETA);
pivot.worldToLocal(point.position);
point._t = Date.now();
}
}
function createLand() {
var mat = new THREE.MeshPhongMaterial({
color: colors.land$,
flatShading: true,
shininess: 0
// wireframe: true
});
var geo = new THREE.IcosahedronGeometry(RADIUS_LAND, 4);
land = new THREE.Mesh(geo, mat);
land.layers.set(LAYER_EARTH);
land.receiveShadow = true;
pivot.add(land);
var isVLeveled = {};
var vLevel = [];
for (var i = 0; i < geo.vertices.length; ++i) {
var vertex = geo.vertices[i];
// Some random functions to calculate land and ocean
if (
vertex.x * vertex.x + vertex.y * vertex.y > 100
&& (vertex.x * vertex.y - vertex.z > 14)
|| vertex.y * vertex.x + vertex.y * vertex.z > 50
&& (vertex.y * vertex.x + vertex.y * vertex.z < 65)
|| vertex.y * vertex.z * vertex.z - vertex.x * vertex.x < -300
|| vertex.x * vertex.z - vertex.x * vertex.y > 60
|| vertex.x - vertex.y + vertex.x * vertex.z > 55
|| vertex.x - (vertex.y + 50) * (vertex.z - 20) > 1400
&& vertex.y * vertex.x > 200
|| vertex.x * vertex.y - vertex.x < -50
|| (vertex.x - 50) * vertex.z - vertex.x * vertex.y * 8 < -500
|| vertex.y * vertex.y - vertex.z * 30 - vertex.y * 50 + vertex.x * 20 < -490
&& vertex.y > 6
|| vertex.z < -8 && vertex.x > 2
|| vertex.y * vertex.y - vertex.z + vertex.x * 10 > 110
) {
// Ocean
geo.vertices[i].multiplyScalar(0.6);
vLevel.push(0);
}
else {
// Land
vLevel.push(1);
}
}
landSurface = [];
for (i = 0; i < geo.faces.length; ++i) {
var f = geo.faces[i];
if (vLevel[f.a] && vLevel[f.b] && vLevel[f.c]) {
// Land
landSurface.push(5);
isVLeveled[f.a] = 2;
isVLeveled[f.b] = 2;
isVLeveled[f.c] = 2;
}
else {
landSurface.push(-10);
}
}
for (var level = 1; level > -4; --level) {