-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
944 lines (765 loc) · 26.3 KB
/
Copy pathmain.js
File metadata and controls
944 lines (765 loc) · 26.3 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
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
import * as THREE from "three";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import * as TWEEN from "three/examples/jsm/libs/tween.module.js";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";
import { MeshoptDecoder } from 'meshoptimizer';
const canvas = document.getElementById("webgl");
// ========================= Inisiasi threeJS ====================
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 1000);
let renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
powerPreference: "high-performance",
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.antialias = true;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.6;
renderer.setClearColor(0x000000, 0);
renderer.setPixelRatio(window.devicePixelRatio);
//=============== pencahayaan pakek .hdri ===============
let rt;
const loaderx = new THREE.TextureLoader();
loaderx.load("/hdri/background-liminal-hqhan.webp", function (texture) {
texture.encoding = THREE.sRGBEncoding;
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.LinearFilter;
texture.minFilter = THREE.LinearMipmapLinearFilter;
rt = new THREE.WebGLCubeRenderTarget(texture.image.height/1.8);
rt.fromEquirectangularTexture(renderer, texture);
scene.environment = rt.texture;
scene.environmentRotation = new THREE.Euler(0,0,0);
scene.backgroundRotation = new THREE.Euler(0,0,0);
});
//======== draco decoder inisialization ========
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath("/decoder/");
dracoLoader.setDecoderConfig({ type: "wasm" });
// ============================ setting start posisi camera & size objek ===============
camera.position.set(0, 0, 30);
let scene2Scale = { x: 0, y: 0, z: 0 };
Object.assign(scene2Scale, window.matchMedia("(min-width: 1024px)").matches
? { x: 8, y: 8, z: 8 }
: { x: 5.6, y: 5.6, z: 5.6 }
);
let scene1Scale = { x: 0, y: 0, z: 0 };
Object.assign(scene1Scale, window.matchMedia("(min-width: 1024px)").matches
? { x: 9.5, y: 9.5, z: 9.5 }
: { x: 7.4, y: 7.4, z: 7.4 }
);
//========== bg particles using shader ================
const particleCount = 290;
const positions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount; i++) {
positions[i * 3] = (Math.random() - 0.5) * 10;
positions[i * 3 + 1] = (Math.random() - 0.5) * 10;
positions[i * 3 + 2] = (Math.random() - 0.5) * 10;
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.BufferAttribute(positions, 3));
const uniforms = {
u_time: { value: 0 },
u_pixelRatio: { value: Math.min(window.devicePixelRatio, 2) }
};
const particleMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: `
uniform float u_time;
varying float vOpacity;
uniform float u_pixelRatio;
void main() {
vec3 newPosition = position;
newPosition.y += sin(u_time * 4.0 + position.x * 5.0) * 0.2;
vOpacity = 0.5 + 0.5 * sin(u_time + position.y/(-0.1));
float baseSize = 2.6;
if (u_pixelRatio > 1.5) {
baseSize = 2.8;
}
gl_PointSize = baseSize * u_pixelRatio*1.1;
gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);
}
`,
fragmentShader: `
varying float vOpacity;
void main() {
vec2 uv = gl_PointCoord - vec2(0.5);
float alpha = 1.0 - smoothstep(0.45, 0.5, length(uv));
if (alpha < 0.1) discard; // Buat bagian luar transparan
gl_FragColor = vec4(1.0, 0.0, 0.0, alpha * vOpacity); // Warna merah dengan transparansi
}
`,
transparent: true
});
const particles = new THREE.Points(geometry, particleMaterial);
particles.scale.set(24,10)
particles.position.set(-3,0,-20)
scene.add(particles);
//========================= Scene 1 ======================
let mixer1;
let slide1;
const loader1 = new GLTFLoader();
loader1.setDRACOLoader(dracoLoader);
loader1.load("/gltf/scene1DracoV2.glb", function (gltf) {
slide1 = gltf.scene;
slide1.position.set(0,0.2,0);
slide1.rotation.set(0.1,4.9,0)
slide1.scale.set(scene1Scale.x, scene1Scale.y, scene1Scale.z);
mixer1 = new THREE.AnimationMixer(slide1);
if (gltf.animations.length > 0) {
gltf.animations.forEach((anim) => {
const action = mixer1.clipAction(anim);
action.play();
});
}
scene.add(slide1);
},
undefined,
function (error) {
console.error(error);
}
);
//========================= Scene 2 ======================
const loader2 = new GLTFLoader();
loader2.setMeshoptDecoder(MeshoptDecoder);
let slide2;
let mixer2;
let animateSlide2;
let unanimateSlide2;
loader2.load("/gltf/scene2MeshoptV3.glb", function (gltf) {
slide2 = gltf.scene;
slide2.position.set(0, 3, 0);
slide2.scale.set(scene2Scale.x, scene2Scale.y, scene2Scale.z);
slide2.name = "Slide2";
//buat load pertama kali
slide2.traverse((object) => {
if (object.isMesh) {
object.frustumCulled = false;
object.material.transparent = true;
object.material.depthWrite = false;
object.material.opacity = 0;
}
});
scene.add(slide2);
mixer2 = new THREE.AnimationMixer(slide2);
if (gltf.animations.length > 0) {
const animationsMap = {};
gltf.animations.forEach(clip => {
animationsMap[clip.name] = clip;
});
const openingBottleAction = mixer2.clipAction(animationsMap["opening botol"]);
const openingBajuHitamAction = mixer2.clipAction(animationsMap["opening baju hitam"]);
const openingBajuPutihAction = mixer2.clipAction(animationsMap["opening baju putih"]);
const iddleBottleAction = mixer2.clipAction(animationsMap["iddle bottle"]);
const iddleBajuPutihAction = mixer2.clipAction(animationsMap["baju putih iddle"]);
const iddleBajuHitamAction = mixer2.clipAction(animationsMap["baju hitam iddle"]);
const clothSimBajuPutih = mixer2.clipAction(animationsMap["Key.001Action"]);
const clothSimBajuHitam = mixer2.clipAction(animationsMap["Key.002Action"]);
[openingBottleAction, openingBajuHitamAction, openingBajuPutihAction].forEach(action => {
action.setLoop(THREE.LoopOnce, 0);
action.clampWhenFinished = true;
});
[iddleBottleAction, iddleBajuPutihAction, iddleBajuHitamAction, clothSimBajuPutih, clothSimBajuHitam].forEach(action => {
action.setLoop(THREE.LoopRepeat);
});
mixer2.addEventListener('finished', function (e) {
if (e.action === openingBajuPutihAction) {
iddleBottleAction.play();
iddleBajuPutihAction.play();
iddleBajuHitamAction.play();
}
});
animateSlide2 = ()=>{
slide2.traverse((object) => {
if (object.isMesh) {
object.material.transparent = true;
object.material.opacity = 0;
}
});
slide2.traverse((child) => {
if (child.isMesh) {
new TWEEN.Tween(child.material)
.to({ opacity: 1 }, 1500)
.easing(TWEEN.Easing.Quadratic.Out)
.start();
}
});
clothSimBajuHitam.play();
clothSimBajuPutih.play();
openingBajuHitamAction.play();
openingBajuPutihAction.play();
openingBottleAction.play();
}
unanimateSlide2 = () => {
clothSimBajuHitam.stop();
clothSimBajuPutih.stop();
openingBajuHitamAction.stop();
openingBajuPutihAction.stop();
openingBottleAction.stop();
iddleBottleAction.stop();
iddleBajuPutihAction.stop();
iddleBajuHitamAction.stop();
}
}else{
console.log("Errorrr")
}
},
undefined,
function (error) {
console.error(error);
}
);
//======== mouse controller ======
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2(-2, -2);
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);
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 rotasi kamera ============
function updateCameraPosition() {
let pivot = new THREE.Vector3(0, 0, 0)
let distance = 45;
const angleX = 1.54 + Math.min(Math.max(mouse.x, -0.18), 0.18) * (Math.PI / 4);
const angleY = Math.min(Math.max(-mouse.y, -0.15), 0.15) * (Math.PI / 4);
const newX = pivot.x + distance * Math.cos(angleX);
const newY = pivot.y + distance * Math.sin(angleY);
const newZ = pivot.z + distance * Math.sin(angleX);
new TWEEN.Tween(camera.position)
.to({ x: newX, y: newY, z: newZ }, 1000)
.easing(TWEEN.Easing.Quadratic.Out)
.start();
camera.lookAt(pivot);
}
//=========== grid latar animasi 3d ==========
const size = 180;
const divisions = 15;
const color1 = 0x770000;
const color2 = 0x770000;
const gridHelper = new THREE.GridHelper(size, divisions, color1, color2);
gridHelper.material.depthWrite = false;
gridHelper.position.y = -8;
scene.add(gridHelper);
//============ cek mouse ada diatas objek =========
function onMouseMoveOnBox() {
if (isModalShow) return;
raycaster.setFromCamera(mouse, camera);
let opacityIsNotZero = () => {
let found = false;
slide2.traverse((child) => {
if (child.isMesh && child.material.opacity === 1) {
found = true;
}
});
return found;
};
let intersects = raycaster.intersectObjects([slide2], true);
if (intersects.length > 0 && currentSlide === 2 && opacityIsNotZero()) {
document.body.style.cursor = "pointer";
let intersectBottle =false;
slide2.traverse((child) => {
if (child.isMesh) {
child.material.color.set(0xfffffff);
}
});
let objek = intersects[0].object;
if(objek.name === "Plane006_1" || objek.name === "Plane006"){
intersectBottle = true;
} else if (objek.name === "Plane003_1") {
objek.material.color.set(0xff9999);
} else if (objek.name === "Plane001") {
objek.material.color.set(0xff9999);
}
if (intersectBottle) {
slide2.traverse((child) => {
if (child.isMesh && (child.name === "Plane006_1" || child.name === "Plane006")) {
child.material.color.set(0xff9999);
}
});
}
}else{
document.body.style.cursor = "default";
slide2.traverse((child) => {if (child.isMesh) child.material.color.set(0xfffffff);});
}
}
//========== modal when klick object ============
let isModalShow = false;
function onMouseClickOnBox(event) {
if (isAnimating) return;
raycaster.setFromCamera(mouse, camera);
let intersects = raycaster.intersectObjects([slide2], true);
if (intersects.length > 0 && currentSlide === 2) {
let objek = intersects[0].object;
let modalTitle = "";
let modalDescription = "";
if (objek.name === "Plane006_1" || objek.name === "Plane006") {
modalTitle = "LIMITED H-FLASK \"TORTURE SERIES\"";
modalDescription = "Stainless steel pocket bottle with metal engraving custom design.\n\n- Material : Stainless Steel\n- Dimension: 8cm x 9cm\n- Overall height: 9.5cm\n- Wide mouth: 1.2cm\n- Capacity: ±140ml (5oz)";
slide2.traverse(child => child.isMesh && (child.name === "Plane006_1" || child.name === "Plane006") && child.material.color.set(0xff9999));
} else if (objek.name === "Plane003_1") {
modalTitle = "LIMITED TSHIRT \"TORTURE\" UNISEX";
modalDescription = "Boxy fit, short sleeve tshirt meet a plastisol ink screen.\n\n- Material : cotton 20s";
objek.material.color.set(0xff9999);
} else if (objek.name === "Plane001") {
modalTitle = "LIMITED TSHIRT \"UNDYING\" UNISEX";
modalDescription = "Reguler custom fit, short sleeve tshirt meet a plastisol ink screen\n\n- Material : cotton 20s";
objek.material.color.set(0xff9999);
}
if (modalTitle && modalDescription && !isModalShow) {
showModal(modalTitle, modalDescription);
}
}
}
document.addEventListener("click", onMouseClickOnBox);
let showTimeout = null;
let hideTimeout = null;
function showModal(title, description) {
let modal = document.getElementById("modal-text");
if (modal) {
modal.querySelector("h1").innerText = title;
modal.querySelector("p").innerText = description;
modal.classList.remove("hidden");
clearTimeout(showTimeout);
showTimeout = setTimeout(() => {
modal.classList.remove("opacity-0");
modal.classList.add("opacity-100", "pointer-events-auto");
}, 10);
document.getElementById("close-modal").addEventListener("click", closeModal);
modal.addEventListener("click", (event) => {
if (event.target === modal) {
closeModal();
}
});
isAnimating = true;
isModalShow = true;
}
}
function closeModal() {
let modal = document.getElementById("modal-text");
if (modal) {
modal.classList.remove("opacity-100", "pointer-events-auto");
modal.classList.add("opacity-0");
clearTimeout(hideTimeout);
hideTimeout = setTimeout(() => {
modal.classList.add("hidden");
isModalShow = false;
isAnimating = false;
mouse.set(0, -5);
}, 250);
document.getElementById("close-modal").removeEventListener("click", closeModal);
modal.removeEventListener("click", (event) => {
if (event.target === modal) {
closeModal();
}
});
}
}
//=========== Modal when first time in scene 2 ==========
let isFirstTimeScene2 = true;
function modalFirstTimeToScene2() {
if (isAnimating) return;
if(isFirstTimeScene2){
showModal("Objective", "Touch/Klick the 3D object for product detail !!!");
isFirstTimeScene2 = false;
}
}
//============ handle teks transition ========================
let previousSlide = 1;
let activeAnimation = null;
let timeouts = [];
function textSceneShow(currentSlide) {
return new Promise((resolve) => {
const scenes = [
document.getElementById("text-scene-1"),
document.getElementById("text-scene-2"),
document.getElementById("text-scene-3"),
];
if (currentSlide === previousSlide) return resolve();
const oldScene = scenes[previousSlide - 1];
const newScene = scenes[currentSlide - 1];
previousSlide = currentSlide;
if (activeAnimation) {
activeAnimation();
}
timeouts.forEach(clearTimeout);
timeouts = [];
oldScene.classList.remove("opacity-100");
oldScene.classList.add("opacity-0");
function handleFadeOut() {
oldScene.classList.add("hidden");
newScene.classList.remove("hidden");
requestAnimationFrame(() => {
newScene.offsetHeight;
let newTimeout = setTimeout(() => {
newScene.classList.remove("opacity-0");
newScene.classList.add("opacity-100");
resolve("animation done");
activeAnimation = null;
}, 10);
timeouts.push(newTimeout);
});
oldScene.removeEventListener("transitionend", handleFadeOut);
}
oldScene.addEventListener("transitionend", handleFadeOut, { once: true });
let safetyTimeout = setTimeout(() => {
handleFadeOut();
resolve("animation done");
activeAnimation = null;
}, 500);
timeouts.push(safetyTimeout);
activeAnimation = () => {
clearTimeout(safetyTimeout);
handleFadeOut();
};
});
}
//========= deteksi scroll in slide 3 ========
let scrollTopCounter = 0;
const productListContainer = document.getElementById("product-list-container");
let resetTimeout = null;
let isScrolling = false;
let isChecking = false;
let killChecking = false;
productListContainer.addEventListener("scroll", function () {
console.log("killChecking"+isChecking + "isScrolling :" + isScrolling);
if (productListContainer.scrollTop > 0) {
isAnimating = true;
if (!killChecking) isScrolling = true;
} else {
scrollTopCounter++;
if (scrollTopCounter > 0) {
if (resetTimeout) {
clearTimeout(resetTimeout);
}
resetTimeout = setTimeout(() => {
isScrolling = false;
if (currentSlide === 3 && isAnimating) {
if (!isChecking) {
isChecking = true;
checkStopScrolling().finally(() => {
isChecking = false;
});
}
} else {
clearTimeout(resetTimeout);
}
}, 100);
}
}
});
//kalo udah slide baru kembalikan promise tapi
async function checkStopScrolling() {
if (!isScrolling && currentSlide === 3) {
if (!killChecking) isAnimating = false;
scrollTopCounter = 0;
await new Promise(resolve => setTimeout(resolve, 500));
}
}
//======= bg-color change for each slide =======
let bgTimeouts = [];
function updateBackgroundColor() {
const bgOld = document.getElementById("bg-old");
const bgNew = document.getElementById("bg-new");
bgTimeouts.forEach(clearTimeout);
bgTimeouts = [];
let newClass = "";
if (currentSlide === 1) {
newClass = "bg-gradient-to-bl from-black/90 via-red-950/95 to-black/90";
} else if (currentSlide === 2) {
newClass = "bg-gradient-to-t from-black/90 via-red-950/95 to-black/90";
} else if (currentSlide === 3) {
newClass = "bg-gradient-to-br from-black/90 via-red-950/95 to-black/90 ";
}
bgNew.className = `absolute w-full h-full transition-opacity duration-500 opacity-0 ${newClass}`;
bgNew.classList.add("opacity-100");
bgOld.classList.add("opacity-0");
let newTimeout = setTimeout(() => {
bgOld.className = bgNew.className;
bgOld.classList.remove("opacity-0");
bgNew.classList.remove("opacity-100");
}, 500);
bgTimeouts.push(newTimeout);
}
//========= update indicator footer ==============
function updateIndicator() {
const indicators = document.querySelectorAll("#slide-indicator span");
indicators.forEach((dot, index) => {
if (index === currentSlide - 1) {
dot.classList.add("w-3", "h-3", "bg-white");
dot.classList.remove("w-2", "h-2", "bg-gray-500");
} else {
dot.classList.add("w-2", "h-2", "bg-gray-500");
dot.classList.remove("w-3", "h-3", "bg-white");
}
});
}
//========== handle animation each slide ==========
let currentSlide = 1;
let isAnimating = false;
let lastScrollTime = 0;
let touchStartY = 0;
function setSlide(duration = 1000) {
if (isAnimating) return;
isAnimating = true;
if (currentSlide === 1 && previousSlide===2) {
textSceneShow(currentSlide);
updateBackgroundColor();
if (mixer1) mixer1.setTime(0);
if (slide2 && slide2.visible) {
slide2.traverse((child) => {
if (child.isMesh) {
child.material.transparent = true;
new TWEEN.Tween(child.material)
.to({ opacity: 0 }, 500)
.easing(TWEEN.Easing.Quadratic.Out)
.onComplete(() => {
slide2.visible = false;
unanimateSlide2();
// Slide 1 masuk
if (slide1) {
slide1.visible = true;
new TWEEN.Tween(slide1.position)
.to({ x: 0, y: 0, z: 0 }, duration)
.easing(TWEEN.Easing.Quadratic.Out)
.onComplete(() => {
isAnimating = false;
})
.start();
}
})
.start();
}
});
}
} else if (currentSlide === 2) {
updateBackgroundColor();
if (previousSlide === 3) {
killChecking = true; //kill scroll slide 3 behavior
productListContainer.style.overflowY = "hidden"; //hilangin slider
textSceneShow(currentSlide).then(() => {
handleSlide2();
});
} else if (previousSlide === 1) {
textSceneShow(currentSlide);
handleSlide2();
if (slide1) {
new TWEEN.Tween(slide1.position)
.to({ x: 0, y: 50, z: 0 }, duration)
.easing(TWEEN.Easing.Quadratic.In)
.onComplete(() => (slide1.visible = false))
.start();
}
}
function handleSlide2() {
if (mixer2) {
mixer2.stopAllAction();
mixer2.setTime(0);
if (slide2) {
slide2.visible = true;
animateSlide2();
}
mixer2.addEventListener("finished", function onFinish(e) {
mouse.set(0, -5);
if (e.action.getClip().name === "opening botol") {
isAnimating = false;
modalFirstTimeToScene2();
}
}, { once: true });
}
}
} else if (currentSlide === 3 && previousSlide === 2) {
productListContainer.style.overflowY = "auto"; //kasih slide lagi
textSceneShow(currentSlide).then(() => {
killChecking = true; //kill scroll slide 3 behavior
productListContainer.scrollTop = 0;
updateBackgroundColor();
if (slide2 && slide2.visible) {
slide2.traverse((child) => {
if (child.isMesh) {
child.material.transparent = true;
new TWEEN.Tween(child.material)
.to({ opacity: 0 }, 1000)
.easing(TWEEN.Easing.Quadratic.Out)
.onComplete(() => {
slide2.visible = false;
unanimateSlide2();
isAnimating = false;
killChecking= false; //add scroll slide 3 behavior
})
.start();
}
});
}
})
}else{
console.log("eroorrr")
}
updateIndicator();
}
//=============== alert handler func ===============
let isAnimatingWarning = false;
let warningTimeout;
let counterToMuchCall = 0;
function alertWhenIsAnimate() {
if (isScrolling || isModalShow) return; //avoid if scroll scene 3 & ada modal
if (!isAnimating || isAnimatingWarning) return;
counterToMuchCall++;
if (counterToMuchCall < 3) return;
const warningText = document.getElementById("warning-text");
if (!warningText) return;
if (warningText.classList.contains("hidden")) {
clearTimeout(warningTimeout);
isAnimatingWarning = true;
warningText.classList.remove("hidden");
warningText.classList.remove("opacity-0");
warningText.classList.add("opacity-100");
// requestAnimationFrame(() => {
// });
function handleTransitionEnd(event) {
if (event.propertyName !== "opacity") return;
warningText.classList.add("hidden");
warningText.removeEventListener("transitionend", handleTransitionEnd);
isAnimatingWarning = false;
}
warningTimeout = setTimeout(() => {
warningText.classList.remove("opacity-100");
warningText.classList.add("opacity-0");
warningText.addEventListener("transitionend", handleTransitionEnd, { once: true });
}, 500);
}
console.log(counterToMuchCall);
}
//======== handle slide move in desktop ========
function scrollSlide(event) {
const now = Date.now();
if (now - lastScrollTime < 800 || isAnimating) return;
lastScrollTime = now;
if (event.deltaY < 0) {
if (currentSlide > 1) {
currentSlide--;
setSlide();
counterToMuchCall = 0;
}
} else {
if (currentSlide < 3) {
currentSlide++;
setSlide();
counterToMuchCall = 0;
}
}
}
// let scrollcounter=0;
function wheelStopListener(element, callback, timeout) {
var handle = null;
var onScroll = function (event) {
scrollSlide(event);
if (handle) {
// console.log("berenti" + scrollcounter)
clearTimeout(handle);
}
handle = setTimeout(callback, timeout || 80);
};
element.addEventListener('wheel', onScroll);
return function () {
element.removeEventListener('wheel', onScroll);
};
}
wheelStopListener(document, function () {
alertWhenIsAnimate();
// scrollcounter++;
});
//======== handle slide move in mobile ========
let lastSlideTime = 0;
const SLIDE_DELAY = 500;
document.addEventListener("touchstart", (event) => {
touchStartY = event.touches[0].clientY;
});
document.addEventListener("touchend", (event) => {
if (isAnimating) alertWhenIsAnimate();
let touchEndY = event.changedTouches[0].clientY;
let touchDiff = touchStartY - touchEndY;
let now = Date.now();
if (Math.abs(touchDiff) > 50 && !isAnimating && (now - lastSlideTime > SLIDE_DELAY)) {
lastSlideTime = now;
if (touchDiff > 0 && currentSlide < 3) {
currentSlide++;
setSlide();
counterToMuchCall = 0;
} else if (touchDiff < 0 && currentSlide > 1) {
currentSlide--;
setSlide();
counterToMuchCall = 0;
}
}
});
//======== animate looping ========
let clock1 = new THREE.Clock();
let clock2 = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
// console.log(isAnimating);
uniforms.u_time.value += 0.01;
if (slide2) {
onMouseMoveOnBox();
}
if (mixer1) {
const delta1 = clock1.getDelta();
mixer1.update(delta1);
}
if(mixer2){
const delta2 = clock2.getDelta();
mixer2.update(delta2);
}
TWEEN.update();
updateCameraPosition();
renderer.render(scene, camera);
}
animate();
//===== screen loader =====
isAnimating = true;
let startTimeout = null;
window.onload = () => {
currentSlide = 0;
const loadScreen = document.getElementById("load-screen");
if (loadScreen) {
startTimeout = setTimeout(() => {
loadScreen.classList.add("opacity-0");
const transitionHandler = () => {
loadScreen.classList.add("hidden");
loadScreen.removeEventListener("transitionend", transitionHandler);
currentSlide = 1;
isAnimating = false;
};
loadScreen.addEventListener("transitionend", transitionHandler);
loadScreen.dataset.transitionHandler = transitionHandler;
}, 500);
}
window.onbeforeunload = () => {
if (startTimeout) {
clearTimeout(startTimeout);
}
if (loadScreen && loadScreen.dataset.transitionHandler) {
loadScreen.removeEventListener("transitionend", loadScreen.dataset.transitionHandler);
}
};
};
//========= handle resize screen ===========
function updateSize() {
let width = window.innerWidth;
let height = window.innerHeight;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
camera.aspect = width / height;
camera.updateProjectionMatrix();
uniforms.u_pixelRatio.value = Math.min(window.devicePixelRatio, 2);
uniforms.u_resolution.value.set(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", updateSize);