-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
1535 lines (1259 loc) · 47.4 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
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
import * as THREE from "three";
import { CSS2DRenderer, CSS2DObject } from "three/examples/jsm/renderers/CSS2DRenderer.js";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import * as TWEEN from "three/examples/jsm/libs/tween.module.js";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { RGBELoader } from "three/addons/loaders/RGBELoader.js";
import { CSS3DObject } from "three/examples/jsm/Addons.js";
import { CSS3DRenderer } from "three/examples/jsm/Addons.js";
//ambil komponen canvas dari html
const canvas = document.getElementById("webgl");
// ========================= Inisiasi threeJS ====================
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
powerPreference: "high-performance",
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.antialias = true;
//============================ Audio untuk web ==================================
const music = new Audio("audio/3D-Nostalgia.mp3");
music.loop = true;
music.volume = 0.5;
canvas.addEventListener('click', () => {
if (music.paused) {
music.play();
}
})
const mouseSfx = new Audio("audio/mouse-klick.mp3");
mouseSfx.loop = false;
mouseSfx.volume = 0.7;
document.addEventListener('mousedown', () => {
mouseSfx.currentTime = 0;
mouseSfx.play();
});
//================== Loader untuk jpeg jadi hdri juga ada cube untuk scene awal ==================
let rt;
const loaderx = new THREE.TextureLoader();
loaderx.load("hdri/background-liminal-hqhan.webp", function (texture) {
//buat texture agar ditampilkan sesuai aslinya
texture.encoding = THREE.sRGBEncoding;
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.LinearFilter;
texture.minFilter = THREE.LinearMipmapLinearFilter;
//konversi tekstur menjadi cube render target
rt = new THREE.WebGLCubeRenderTarget(texture.image.height/1.8); //size direduce krn tidak semua browser support
rt.fromEquirectangularTexture(renderer, texture);
scene.background = rt.texture;
scene.environment = rt.texture;
scene.environmentRotation = new THREE.Euler(0,0,0);
scene.backgroundRotation = new THREE.Euler(0,0,0);
});
let cube;
const loader1 = new GLTFLoader();
loader1.load("gltf/kaleng.glb", function (gltf) {
cube = gltf.scene;
cube.position.set(0,-1,0);
cube.scale.set(13, 13, 13);
scene.add(cube);
new TWEEN.Tween(cube.rotation)
.to({ y: cube.rotation.y + Math.PI * 4}, 10000)
.repeat(Infinity)
.start();
new TWEEN.Tween(cube.rotation)
.to({ z: cube.rotation.z + Math.PI * 0.15, x: cube.rotation.x + Math.PI * 0.1}, 2000)
.easing(TWEEN.Easing.Quadratic.InOut)
.repeat(Infinity)
.yoyo(true)
.delay(1000)
.start();
},
undefined,
function (error) {
console.error(error);
}
);
//========================= Disini adalah bagian GLTF Loader ======================
const loader = new GLTFLoader();
var model;
let mixer;
loader.load("gltf/computerCustom3.glb", function (gltf) {
model = gltf.scene;
model.position.set(400, -9, 4);
model.scale.set(10, 10, 10);
model.name = "Cube013";
//hilangkan frustumCulled agar selalu dirender tanpa menunggu muncul pada jarak pandang
model.traverse((object) => {
if (object.isMesh) {
object.frustumCulled = false;
}
});
scene.add(model);
//inisialisasi AnimationMixer dengan scene dari model (glb)
mixer = new THREE.AnimationMixer(model);
//ambil animasi pertama (jika ada) dan mulai
if (gltf.animations.length > 0) {
const action = mixer.clipAction(gltf.animations[0]);
action.play();
}else{
console.log("Errorrr")
}
},
undefined,
function (error) {
console.error(error);
}
);
// ============================ set awal posisi dan rotasi kamera ===============
camera.position.set(0, 0, 30);
camera.rotation.set(0, 0, 0);
//========================= Disini adalah bagian CSS2DRenderer ======================
//buat container untuk renderer CSS2D
var cssContainer = document.createElement("div");
cssContainer.style.position = "fixed";
cssContainer.style.top = 0;
cssContainer.style.pointerEvents = "none"; //memastikan bahwa elemen ini tidak menghalangi interaksi dengan elemen di bawahnya (ini nanti diubah2)
document.body.appendChild(cssContainer);
//buat renderer CSS2D di dalam container
var cssRenderer = new CSS2DRenderer();
cssRenderer.setSize(window.innerWidth, window.innerHeight);
cssContainer.appendChild(cssRenderer.domElement);
var text5 = document.createElement("div");
text5.innerHTML = `
<div id="typedText">This app is half-finished because the developer is tired. Want to go to a nostalgic place? </div>
<div id="buttonContainer" style="display:flex;justify-content:center;margin-top: 20px;">
<button id="button1" class="saira-condensed-light" style="margin-right:0.3rem;cursor:pointer;">Let's go there</button><button id="button2" class="saira-condensed-light" style="margin-left:0.3rem;cursor:pointer;">I just want to be here</button>
</div>
`;
text5.style.width = "80vw"; //ukuran teks responsif berdasarkan lebar layar
text5.style.maxWidth = "400px"; //batas lebar maksimum
text5.style.height = "auto"; //inggi otomatis
text5.style.backgroundColor = "rgba(255, 255, 255, 0.5)";
text5.style.textAlign = "justify";
text5.style.padding = "1rem";
text5.style.lineHeight = "1.5";
text5.style.transition = "opacity 0.3s ease-in-out"; //animasi opacity transisi
text5.style.opacity = "0"; //set opacity awal menjadi 0
var label5 = new CSS2DObject(text5);
label5.position.set(0, 0, 5);
//label1
var textContent = `Hello, I am Habbatul Qolbi H the creator of this game. Welcome to this very empty place.`;
var text = document.createElement("div");
text.innerHTML = `
<div id="typedText"></div>
<div id="clickHere" style="color: red;margin-top:20px;text-align:center;">Click to continue...</div>
`;
text.style.width = "80vw"; //ukuran teks responsif berdasarkan lebar layar
text.style.maxWidth = "400px"; //batas lebar maksimum
text.style.height = "auto"; //tinggi otomatis
text.style.backgroundColor = "rgba(255, 255, 255, 0.5)";
text.style.textAlign = "justify";
text.style.padding = "1rem";
text.style.lineHeight = "1.5";
text.style.transition = "opacity 0.3s ease-in-out"; //animasi opacity
text.style.opacity = "0"; //set opacity awal menjadi 0
var label = new CSS2DObject(text);
label.position.set(0, 0, 5);
scene.add(label);
var textContent2 = `The music is generated with my.soundful.com, and the 3D assets are my own.`;
var text2 = document.createElement("div");
text2.innerHTML = `
<div id="typedText"></div>
<div id="clickHere" style="color: red; margin-top: 20px;text-align:center;">Click to continue...</div>
`;
text2.style.width = "80vw"; //ukuran teks responsif berdasarkan lebar layar
text2.style.maxWidth = "400px"; //batas lebar maksimum
text2.style.height = "auto"; //inggi otomatis
text2.style.backgroundColor = "rgba(255, 255, 255, 0.5)";
text2.style.textAlign = "justify";
text2.style.padding = "1rem";
text2.style.lineHeight = "1.5";
text2.style.transition = "opacity 0.3s ease-in-out"; //animasi opacity transisi
text2.style.opacity = "0"; //set opacity awal menjadi 0
var label2 = new CSS2DObject(text2);
label2.position.set(0, 0, 5);
var textContent3 = `Sometimes, the presence of people around us is not enough to fill the emptiness inside.`;
var text3 = document.createElement("div");
text3.innerHTML = `
<div id="typedText"></div>
<div id="clickHere" style="color: red; margin-top: 20px;text-align:center;">Click to continue...</div>
`;
text3.style.width = "80vw"; //ukuran teks responsif berdasarkan lebar layar
text3.style.maxWidth = "400px"; //batas lebar maksimum
text3.style.height = "auto"; //inggi otomatis
text3.style.backgroundColor = "rgba(255, 255, 255, 0.5)";
text3.style.textAlign = "justify";
text3.style.padding = "1rem";
text3.style.lineHeight = "1.5";
text3.style.transition = "opacity 0.3s ease-in-out"; //animasi opacity transisi
text3.style.opacity = "0"; //set opacity awal menjadi 0
var label3 = new CSS2DObject(text3);
label3.position.set(0, 0, 5);
var textContent4 = `Let's take a little nostalgia to the old devices that have been abandoned. We will explore about the good times together.`;
var text4 = document.createElement("div");
text4.innerHTML = `
<div id="typedText"></div>
<div id="clickHere" style="color: red; margin-top: 20px;text-align:center;margin-bottom:10px;">Wait for the exit button..</div>
<div id="buttonContainer" class="hidden" style="justify-content:center;margin-top: 20px;">
<button id="button1" class="saira-condensed-light" style="margin-right:0.3rem;cursor:pointer;">Let's go there</button><button id="button2" class="saira-condensed-light" style="margin-left:0.3rem;cursor:pointer;">I just want to be here</button>
</div>
`;
text4.style.width = "80vw"; //ukuran teks responsif berdasarkan lebar layar
text4.style.maxWidth = "400px"; //batas lebar maksimum
text4.style.height = "auto"; //inggi otomatis
text4.style.backgroundColor = "rgba(255, 255, 255, 0.5)";
text4.style.textAlign = "justify";
text4.style.padding = "1rem";
text4.style.lineHeight = "1.5";
text4.style.transition = "opacity 0.3s ease-in-out"; //animasi opacity transisi
text4.style.opacity = "0"; //set opacity awal menjadi 0
var label4 = new CSS2DObject(text4);
label4.position.set(0, 0, 5);
//-========================== handle animasi ketik ===========================
//kondisional, digunakan untuk mencegah kodingan dalam eventhandler label bekerja ketika animasi berjalan
var isTyping = true;
//inisialisasi soundeffect
const typingSound = new Audio("audio/label-typing.mp3");
typingSound.loop = true;
typingSound.volume = 0.5;
//animasi type writer untuk label yang pada css2drenderer
function typeWriter(textElement, text, speed, clickHereElement) {
return new Promise((resolve) => {
let i = 0;
let lastTime = 0;
function type(timestamp) {
if (timestamp - lastTime > speed) {
textElement.textContent += text.charAt(i);
i++;
lastTime = timestamp;
//play audio
typingSound.play();
if (i < text.length) {
requestAnimationFrame(type);
} else {
typingSound.pause();
isTyping = false;
console.log(isTyping);
resolve();
clickHereElement.style.color = "blue";
}
} else {
requestAnimationFrame(type);
}
}
type(0);
});
}
//animasi ketik untuk menampilkan teks
async function animateText(textElement, textContent, speed, clickHereElement) {
//hapus teks sebelum memulai animasi ketik
textElement.textContent = "";
await typeWriter(textElement, textContent, speed, clickHereElement);
}
//Funsgi ketika opacity mencapai 1, jalankan animasi mengetik
function opacityTransitionEndHandler(labelElement, textContent, speed) {
//kondisi typing true sebelum ke animasi typing (agar tidak terjadi glitch animasi)
isTyping = true;
console.log(isTyping);
var typedTextElement = labelElement.querySelector("#typedText");
var clickHereElement = labelElement.querySelector("#clickHere");
clickHereElement.style.color = "red";
if (labelElement.style.opacity === "1") {
animateText(typedTextElement, textContent, speed, clickHereElement);
} else if (labelElement.style.opacity === "0") {
typedTextElement.textContent = ""; //kosongkan teks saat opacity kembali menjadi 0
}
}
//event listener untuk label2
label2.element.addEventListener("transitionend", (event) => {
if (event.propertyName === "opacity") {
opacityTransitionEndHandler(label2.element, textContent2, 10);
}
});
//event listener untuk label
label.element.addEventListener("transitionend", (event) => {
if (event.propertyName === "opacity") {
opacityTransitionEndHandler(label.element, textContent, 10);
}
});
//event listener untuk label
label3.element.addEventListener("transitionend", (event) => {
if (event.propertyName === "opacity") {
opacityTransitionEndHandler(label3.element, textContent3, 10);
}
});
//khusus untuk label 4 karena ada event untuk button (ketika opacity mencapai 1, jalankan animasi mengetik)
function opacityTransitionEndHandlerForLabel4(labelElement, textContent, speed, buttonContainer) {
//Kondisi typing true sebelum ke animasi typing (agar tidak terjadi glitch animasi)
isTyping = true;
console.log(isTyping);
var typedTextElement = labelElement.querySelector("#typedText");
var clickHereElement = labelElement.querySelector("#clickHere");
clickHereElement.style.color = "red";
if (labelElement.style.opacity === "1") {
//jalankan animasi typing
typeWriter(typedTextElement, textContent, speed, clickHereElement).then(() => {
//Animasi selesai, hilangkan kelas hidden dari button1 dan button2
buttonContainer.classList.remove("hidden");
buttonContainer.classList.add("flex");
clickHereElement.classList.add("hidden");
});
} else if (labelElement.style.opacity === "0") {
typedTextElement.textContent = ""; //Kosongkan teks saat opacity kembali menjadi 0
}
}
//event listener untuk label
label4.element.addEventListener("transitionend", (event) => {
if (event.propertyName === "opacity") {
let buttonContainer =
label4.element.querySelector("#buttonContainer");
buttonContainer.classList.remove("flex");
buttonContainer.classList.add("hidden");
label4.element.querySelector("#clickHere").classList.remove("hidden");
opacityTransitionEndHandlerForLabel4(
label4.element,
textContent4,
10,
buttonContainer
);
}
});
//================= Hanlder animasi menuju komputer dari button label 4/label 5 =================
var started = false;
var kameraMenunjuMonitor = false;
label4.element.querySelector("#button1").addEventListener("click", function (event) {
label4.element.style.opacity = "0";
label4.element.addEventListener("transitionend", function (event) {
scene.remove(label4);
console.log("tutup step kembali ke step 0");
gotoStep = 0;
//kondisi step sudah sampai akhir
gotoDone = true;
cssContainer.style.pointerEvents = "none";
scene.add(label5);
//setelah step selesai atur kursor agar tidak hover otomatis di sumbu (0,0)
mouse.set(-2, -2);
});
gotoComputerPosition();
});
label5.element.querySelector("#button1").addEventListener("click", function (event) {
//Behavior sama pada step 4 untuk urutan label
label5.element.style.opacity = "0";
console.log("tutup step kembali ke step 0");
gotoStep = 0;
cssContainer.style.pointerEvents = "none";
//setelah step selesai atur kursor agar tidak hover otomatis di sumbu (0,0)
mouse.set(-2, -2);
gotoComputerPosition();
});
function gotoComputerPosition() {
//tammbahan rotate background
new TWEEN.Tween(scene.backgroundRotation)
.to({y:1.6}, 1500).start();
//jarak camera pada saat tampilan komputer
rotateCameraDistance = 30;
//matikan objective diatas cube (tulisan h1)
objectiveVisible = false;
kameraMenunjuMonitor = false;
started = false;
var pivot = new THREE.Vector3(400, 0, -8); //biar lebih pas tadinya itu 400, -9, 4 sesuai posisi model tapi ini lebih pas untuk dijadikan pivot karena kan monitor punya tinggi
var initialPosition = new THREE.Vector3(400, 20, 80);
var distance = initialPosition.distanceTo(pivot);
var angleValue = 1.5;
new TWEEN.Tween(camera.position)
.to(
{
x: pivot.x + distance * Math.cos(angleValue),
y: initialPosition.y,
z: pivot.z + distance * Math.sin(angleValue),
},
1500
)
.easing(TWEEN.Easing.Quadratic.InOut)
.onComplete(() => {
var startRotation = new THREE.Euler().copy(camera.rotation);
camera.lookAt(pivot);
var endRotation = new THREE.Euler().copy(camera.rotation);
camera.rotation.copy(startRotation);
new TWEEN.Tween(camera.rotation)
.to({ x: endRotation.x, y: endRotation.y, z: endRotation.z }, 800)
.easing(TWEEN.Easing.Quadratic.InOut)
.onUpdate(function () {
// console.log(camera.rotation);
})
.onComplete(() => {
//buat cube hidden
cube.visible = false;
new TWEEN.Tween({ angle: angleValue })
.to({ angle: Math.PI * 2.5 }, 1600)
.onUpdate((obj) => {
var angle = obj.angle;
var newX = pivot.x + distance * Math.cos(angle);
var newZ = pivot.z + distance * Math.sin(angle);
camera.position.set(newX, initialPosition.y, newZ);
camera.lookAt(pivot);
// console.log(camera.rotation);
})
.easing(TWEEN.Easing.Quadratic.InOut)
.onComplete(() => {
new TWEEN.Tween(camera.position)
// .to({ x: 400, y: 20, z: 40 }, 700)
.to({ x: 400, y: 8, z: 25 }, 700)
.easing(TWEEN.Easing.Quadratic.InOut)
.onUpdate(() => {
camera.lookAt(pivot);
})
.onComplete(() => {
mouse.set(0, 0);
kameraMenunjuMonitor = true;
//set frustumCulled jadi true lagi
if (model) {
model.traverse((object) => {
if (object.isMesh) {
console.log(object.frustumCulled)
object.frustumCulled = true
console.log(object.frustumCulled)
}
});
}
})
.start();
})
.start();
})
.start();
})
.start();
}
//Buat secondary scene, camera, dan render target
const renderTarget = new THREE.WebGLRenderTarget(512, 512); //Ubah ukuran render target menjadi 512x512
const secondaryCamera = new THREE.PerspectiveCamera(
75,
renderTarget.width / renderTarget.height,
0.1,
1000
);
const secondaryScene = new THREE.Scene();
//Ini untuk frame bentuk windows xp dan paint
var textureLoader = new THREE.TextureLoader();
var textureXp = textureLoader.load('texture/paint-xp.png');
var materialjpeg = new THREE.MeshBasicMaterial({
map: textureXp,
transparent: true
});
var geometryXp = new THREE.PlaneGeometry(5, 4);
var PlaneFrame = new THREE.Mesh(geometryXp, materialjpeg);
secondaryCamera.add(PlaneFrame);
secondaryScene.add(secondaryCamera);
PlaneFrame.scale.set(3.02, 3.8)
PlaneFrame.position.set(0, -0.1, -10);
//Muat HDR sebagai env map
new RGBELoader()
.setDataType(THREE.FloatType)
.load("hdri/terrain.hdr", function (hdrCubeMap) {
hdrCubeMap.encoding = THREE.LinearEncoding;
const pmremGenerator = new THREE.PMREMGenerator(renderer);
pmremGenerator.compileEquirectangularShader();
const envMap = pmremGenerator.fromEquirectangular(hdrCubeMap).texture;
envMap.format = THREE.RGBAFormat;
secondaryScene.environment = envMap; //Gunakan gambar HDRI sebagai env map
//Hapus HDR equirectangular setelah selesai menggunakan pmremGenerator
hdrCubeMap.dispose();
//Gunakan gambar HDRI sebagai background
secondaryScene.background = envMap;
const ambientLight = new THREE.AmbientLight(0xffffff, 1.5);
secondaryScene.add(ambientLight);
});
//================ Penerapan shader untuk layar crt + vignette pada plane ======================
const crtTVShader = {
uniforms: {
tDiffuse: { value: null },
time: { value: 0 },
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform sampler2D tDiffuse;
uniform float time;
varying vec2 vUv;
void main() {
vec2 uv = vUv;
//Efek vignette
float radius = 0.85;
float softness = 0.4;
vec2 center = vec2(0.5, 0.5);
float vignette = smoothstep(radius, radius - softness, length(uv - center));
//Efek garis lurus
vec2 p = mod(uv * vec2(100.0, 60.0), vec2(1.0));
vec3 col = texture2D(tDiffuse, uv).rgb;
col *= 0.9 + 0.1 * sin(30.0 * p.x * sin(time) + 30.0 * p.y * cos(time));
col *= 0.95 + 0.05 * sin(32.0 * p.x * sin(time) + 32.0 * p.y * cos(time));
//Gabungkan efek vignette dan garis lurus
col *= vignette;
gl_FragColor = vec4(col, 1.0);
}
`,
};
//Material dengan shader CRT TV
var materialMonitor = new THREE.ShaderMaterial({
uniforms: THREE.UniformsUtils.clone(crtTVShader.uniforms),
vertexShader: crtTVShader.vertexShader,
fragmentShader: crtTVShader.fragmentShader,
});
//Set texture sebagai input untuk shader
materialMonitor.uniforms.tDiffuse.value = renderTarget.texture;
//Mesh untuk renderTarget
var geometry = new THREE.PlaneGeometry(5, 4);
var mesh = new THREE.Mesh(geometry, materialMonitor);
mesh.position.set(400, 3.4, -4.56);
mesh.scale.set(3.5, 3, 1);
mesh.rotation.set(-0.139, 0, 0);
scene.add(mesh);
//Buat orbit control untuk render target (secondary camera)
const controls = new OrbitControls(secondaryCamera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = true;
controls.enablePan = false;
controls.target.set(0, 0, -25);
controls.minDistance = 24;
controls.maxDistance = 120;
//============================== gltf loader untuk rendertarget
loader.load(
"gltf/SeabedLamp3D_Lite.glb",
function (gltf) {
const model = gltf.scene;
model.position.set(0, -8, -25);
model.scale.set(30, 30, 30);
model.name = "uhuy";
secondaryScene.add(model);
},
undefined,
function (error) {
console.error(error);
}
);
//================ Disini adalah bagian event Handler untuk label teksbox ================
//definisikan object raycaster dan mouse untuk interaksi mouse dengan scene
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2(-2, -2);
//deteksi mouse agar bisa realtime
function onMouseMove(event) {
const rect = canvas.getBoundingClientRect();
mouse.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
window.addEventListener("mousemove", onMouseMove, false);
//deteksi rouchscreen agar bisa realitme
function onTouchMove(event) {
const rect = canvas.getBoundingClientRect();
mouse.x = ((event.touches[0].clientX - rect.left) / rect.width) * 2 - 1;
mouse.y =
-((event.touches[0].clientY - rect.top) / rect.height) * 2 + 1;
}
window.addEventListener("touchmove", onTouchMove, false);
//================== animasi untuk rotasi terabatas mengikuti kursor mouse/touch =================
var rotateCameraDistance = 30;
var isWebMode = false;
//saat tekan tombol menuju monitor
function rotateCamera() {
if (kameraMenunjuMonitor) {
if(isWebMode){
var pivot = new THREE.Vector3(400, -5, 4);
var pivot2 = new THREE.Vector3(400, 4, -8);
var pivot3 = new THREE.Vector3(400, 4, 0);
}else{
var pivot = new THREE.Vector3(400, -9, 4);
var pivot2 = new THREE.Vector3(400, 0, -8);
var pivot3 = new THREE.Vector3(400, 0, 0);
}
var distance = rotateCameraDistance;
//Membatasi rotasi
var angleX = 1.5 + Math.min(Math.max(mouse.x, -0.5), 0.5) * (Math.PI / 4);
var angleY = Math.min(Math.max(-mouse.y + 1, 0.5), 10) * (Math.PI / 4);
var newX = pivot.x + distance * Math.cos(angleX);
var newY = pivot.y + distance * Math.sin(angleY);
var newZ = pivot3.z + distance * Math.sin(angleX);
//Tween animasi rotasi kamera
new TWEEN.Tween(camera.position)
.to({ x: newX, y: newY, z: newZ }, 1000)
.easing(TWEEN.Easing.Quadratic.Out)
.start();
camera.lookAt(pivot2);
}
}
//================================Disini untuk textbox pembuka ========================
//kondisional untuk textbox
var gotoStep = 0;
var gotoDone = false;
//step 1
label.element.addEventListener("click", function (event) {
//jika animasi typing tidak berjalan
if (!isTyping) {
label.element.style.opacity = "0";
scene.add(label2);
gotoStep = 2;
//ini dijalankan ketika label ada bukan ketika gotoStep=1
label.element.addEventListener("transitionend", function (event) {
if (gotoStep == 2) {
console.log("step dua dijalankan");
scene.remove(label);
//gunakan disini karena pasti tidak bisa dihindarkan dari hover jadi nantinya animasi akan tetap berjalan
label2.element.style.opacity = "1";
}
});
}
});
//step 1
label.element.addEventListener("mouseover", function (event) {
label.element.style.cursor = "pointer";
});
//step 2
label2.element.addEventListener("click", function (event) {
//jika animasi typing tidak berjalan
if (!isTyping) {
label2.element.style.opacity = "0";
scene.add(label3);
gotoStep = 3;
//ini dijalankan ketika label ada bukan ketika gotoStep=1
label2.element.addEventListener("transitionend", function (event) {
if (gotoStep == 3) {
console.log("step tiga dijalankan");
scene.remove(label2);
//gunakan disini karena pasti tidak bisa dihindarkan dari hover jadi nantinya animasi akan tetap berjalan
label3.element.style.opacity = "1";
}
});
}
});
//step 2
label2.element.addEventListener("mouseover", function (event) {
label2.element.style.cursor = "pointer";
});
//step 3
label3.element.addEventListener("click", function (event) {
//jika animasi typing tidak berjalan
if (!isTyping) {
label3.element.style.opacity = "0";
scene.add(label4);
gotoStep = 4;
//ini dijalankan ketika label ada bukan ketika gotoStep=1
label3.element.addEventListener("transitionend", function (event) {
if (gotoStep == 4) {
console.log("step empat dijalankan");
scene.remove(label3);
//gunakan disini karena pasti tidak bisa dihindarkan dari hover jadi nantinya animasi akan tetap berjalan
label4.element.style.opacity = "1";
}
});
}
});
//step 3
label3.element.addEventListener("mouseover", function (event) {
label3.element.style.cursor = "pointer";
});
//step 4
label4.element
.querySelector("#button2")
.addEventListener("click", function (event) {
//jika animasi typing tidak berjalan
if (!isTyping) {
gotoStep = 3;
label4.element.style.opacity = "0";
label4.element.addEventListener("transitionend", function (event) {
if (gotoStep == 3) {
scene.remove(label4);
console.log("tutup step kembali ke step 0");
gotoStep = 0;
//kondisi step sudah sampai akhir
gotoDone = true;
cssContainer.style.pointerEvents = "none";
scene.add(label5);
//setelah step selesai atur kursor agar tidak hover otomatis di sumbu (0,0)
mouse.set(-2, -2);
//hidupkan objective diatas cube (tulisan h1)
objectiveVisible = true;
}
});
}
});
//step selesai (done) untuk label done
label5.element
.querySelector("#button2")
.addEventListener("click", function (event) {
label5.element.style.opacity = "0";
console.log("tutup step kembali ke step 0");
gotoStep = 0;
cssContainer.style.pointerEvents = "none";
//setelah step selesai atur kursor agar tidak hover otomatis di sumbu (0,0)
mouse.set(-2, -2);
//hidupkan objective diatas cube (tulisan h1)
objectiveVisible = true;
});
//step 0
//tambahkan event listener untuk mousedown pada saat kursor berada pada kubus
function onMouseDownOnBox(event) {
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects([cube], true);
if (intersects.length > 0) {
//matikan objective diatas cube (tulisan h1)
objectiveVisible = false;
if (!gotoDone && gotoStep === 0) {
cube.traverse(function (child) {
if (child.isMesh) {
child.material.color.set(0xff0000);
}
});
gotoStep = 1;
label.element.style.opacity = "1";
label.element.addEventListener("transitionend", function (event) {
console.log("step pertama dijalankan");
cssContainer.style.pointerEvents = "auto";
});
} else if (gotoDone && gotoStep === 0) {
//menuju ke step done
cube.traverse(function (child) {
if (child.isMesh) {
child.material.color.set(0xff0000);
}
});
gotoStep = 5;
label5.element.style.opacity = "1";
cssContainer.style.pointerEvents = "auto";
}
}
}
document.addEventListener("click", onMouseDownOnBox, false);
//tambahkan event listener ketika kursor mouse bergerak dan berada pada kubus
//ini dijalankan difungsi animate (agar realtime)
function onMouseMoveOnBox() {
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects([cube], true);
//memperhatikan kondisi jumlah pesan
if (intersects.length > 0) {
if (gotoStep == 0) {
canvas.style.cursor = "pointer";
cube.traverse(function (child) {
if (child.isMesh) {
child.material.color.set(0xff0000);
}
});
} else {
cssContainer.style.pointerEvents = "auto";
//kondisional agar box tetap merah saat box ada
if (gotoStep === 0){
cube.traverse(function (child) {
if (child.isMesh) {
child.material.color.set(0xffffff);
}
});
}
}
} else {
canvas.style.cursor = "auto";
//kondisional agar box tetap merah saat box ada
if (gotoStep === 0){
cube.traverse(function (child) {
if (child.isMesh) {
child.material.color.set(0xffffff);
}
});
}
}
}
//=================== animasi gerakan pada render target (secondary camera) ======================
var pivot1 = new THREE.Vector3(0, -8, -25);
var initialPosition1 = new THREE.Vector3().copy(secondaryCamera.position);
var distance1 = initialPosition1.distanceTo(pivot1);
var InitialAngle1 = 0;
var tween;
var rotate1;
var isMouseDown = false;
var isZooming = false;
function initialAnimation(distanceFromPivot, currentAngle) {
tween = new TWEEN.Tween(secondaryCamera.position)
.to({ x: pivot1.x + distanceFromPivot * Math.cos(currentAngle), y: initialPosition1.y, z: pivot1.z + distanceFromPivot * Math.sin(currentAngle) }, 500)
.onComplete(() => {
rotate1 = new TWEEN.Tween({ angle: currentAngle })
.to({ angle: currentAngle + Math.PI * 2 }, 4000)
.onUpdate((obj) => {
var angle = obj.angle;
var newX = pivot1.x + distanceFromPivot * Math.cos(angle);
var newZ = pivot1.z + distanceFromPivot * Math.sin(angle);
secondaryCamera.position.set(newX, initialPosition1.y, newZ);
secondaryCamera.lookAt(pivot1);
})
.repeat(Infinity)
.start();
})
.start();
}
initialAnimation(distance1, InitialAngle1);
//mendapatkan angle saat ini
function getCurrentAngle(objectPosition, pivotPosition) {
return Math.atan2(objectPosition.z - pivotPosition.z, objectPosition.x - pivotPosition.x);
}
//Memulai Tween jika tidak sedang menahan tombol mouse
function startTween(distance, currentAngle) {
if (!isMouseDown && !isZooming) {
initialAnimation(distance, currentAngle);
}
}
//Menghentikan Tween saat menahan tombol mouse
function stopTween() {
if (isMouseDown || isZooming) {
tween.stop();
if (rotate1) rotate1.stop(); //Menghentikan tween rotate jika ada
}
}
function onMouseDown(event) {
isMouseDown = true;
stopTween();
}
function onMouseUp(event) {
isMouseDown = false;
startTween(new THREE.Vector3().copy(secondaryCamera.position).distanceTo(pivot1), getCurrentAngle(new THREE.Vector3().copy(secondaryCamera.position), pivot1));
}
//Event listener untuk mouse move, mouse down, dan mouse up
document.addEventListener("mousedown", onMouseDown, false);
document.addEventListener("mouseup", onMouseUp, false);
//Event listener untuk touch start
document.addEventListener("touchstart", function (event) {
if (!isZooming) {
if (event.touches.length > 2) {
onMouseDown(event.touches[0]);
}
}
}, false);
//Event listener untuk touch end
document.addEventListener("touchend", function (event) {
if (!isZooming) {
if (event.changedTouches.length > 2) {
onMouseUp(event.changedTouches[0]);
}
}
}, false);
//Event listener untuk menghentikan animasi saat zoom dimulai
controls.addEventListener("start", function () {
isZooming = true;
stopTween();
});