-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1141 lines (997 loc) · 39 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Exported Scene</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<style>
/* Your existing CSS styles */
body, html { margin: 0; padding: 0; overflow: hidden; width: 100%; height: 100%; font-family: Arial, sans-serif; }
#renderCanvas { width: 100%; height: 100%; touch-action: none; }
.ui-overlay {
position: absolute;
top: 10px;
left: 10px;
background-color: rgba(0,0,0,0.7);
padding: 15px;
border-radius: 10px;
color: white;
z-index: 10;
font-size: 14px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
#hotspotContent {
position: fixed;
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 20px;
border-radius: 10px;
z-index: 1001;
max-width: 300px;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
display: none;
font-size: 14px;
}
#infoPopup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 20px;
border-radius: 10px;
z-index: 1002;
max-width: 80%;
box-shadow: 0 0 10px rgba(0,0,0,0.5);
display: none;
font-size: 16px;
}
#scrollControls {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#toggleCameraModeContainer {
margin-bottom: 10px;
}
#scrollControlsContent,
#exploreControlsContent {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
transition: opacity 0.3s ease-in-out;
width: 100%;
}
#scrollControlsContent.hidden,
#exploreControlsContent.hidden {
opacity: 0;
pointer-events: none;
position: absolute;
}
#exploreControlsContent p {
color: white;
font-size: 14px;
text-align: center;
margin: 0;
}
#scrollControls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background-color: rgba(0,0,0,0.7);
padding: 15px;
border-radius: 10px;
color: white;
z-index: 1000;
display: flex;
flex-direction: column;
align-items: center;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
#scrollPercentage {
font-size: 18px;
margin-bottom: 10px;
}
#progressBarContainer {
width: 200px;
height: 10px;
background-color: rgba(255,255,255,0.3);
border-radius: 5px;
overflow: hidden;
margin-bottom: 10px;
}
#progressBar {
width: 0%;
height: 100%;
background-color: #4CAF50;
transition: width 0.3s ease;
}
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s;
}
.button:hover {
background-color: #197c1d;
}
#scrollButtons {
display: flex;
justify-content: space-between;
width: 100%;
}
#muteButton {
position: absolute;
top: 10px;
right: 10px;
background-color: rgba(0,0,0,0.7);
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
z-index: 1000;
}
#preloader {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #1e1e1e;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 100000;
transition: opacity 0.5s ease-out;
}
#preloader.hidden {
opacity: 0;
pointer-events: none;
}
#preloader h1 {
font-size: 48px;
color: #ffffff;
text-align: center;
font-family: 'Courier New', monospace;
}
#preloader .spinner {
width: 25px;
height: 25px;
border: 5px solid #ffffff;
border-top: 5px solid #F76900;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* Start Screen Frosted Glass Effect */
#startButtonContainer {
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.2);
display: flex;
justify-content: center;
align-items: center;
z-index: 10000;
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
#startButton {
padding: 20px 40px;
font-size: 24px;
background-color: rgba(255, 255, 255, 0.25);
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.ui-overlay {
position: absolute;
top: 10px;
left: 10px;
background-color: rgba(0,0,0,0.7);
padding: 15px;
border-radius: 10px;
color: white;
z-index: 10;
font-size: 14px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
/* Ensure the toggleCameraMode button has consistent styling */
#toggleCameraMode {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s;
}
#toggleCameraMode:hover {
background-color: #197c1d;
}
/* Joystick Styles */
#joystickContainer {
position: absolute;
bottom: 50px;
left: 50px;
z-index: 1000;
}
#joystickCanvas {
width: 100px;
height: 100px;
touch-action: none;
}
</style>
</head>
<body>
<div id="preloader">
<h1>Story Splat</h1>
<div class="spinner"></div>
</div>
<!-- Start Screen -->
<div id="startButtonContainer">
<button id="startButton">Start Experience</button>
</div>
<canvas id="renderCanvas"></canvas>
<!-- Movement Instructions -->
<div class="ui-overlay">
<p><strong>Controls:</strong></p>
<p>• W/A/S/D: Move camera</p>
<p>• Q/E: Up and Down</p>
<p>• Mouse: Look around</p>
<p>• Scroll: Move along path</p>
</div>
<!-- Hotspot and Info Popup -->
<div id="hotspotContent"></div>
<div id="infoPopup"></div>
<!-- Scroll Controls -->
<div id="scrollControls">
<!-- Scroll Controls Content -->
<div id="toggleCameraModeContainer">
<button id="toggleCameraMode" onclick="toggleCameraMode()">Mode: Tour</button>
</div>
<div id="scrollControlsContent">
<div id="scrollPercentage">0%</div>
<div id="progressBarContainer">
<div id="progressBar"></div>
</div>
<div id="scrollButtons">
<button class="button" onclick="handleButtonScroll(-1)">◀ Prev</button>
<button class="button" onclick="handleButtonScroll(1)">Next ▶</button>
</div>
</div>
<div id="exploreControlsContent">
<p>Use WASD to move and mouse to look around</p>
</div>
</div>
<!-- Mute Button -->
<button id="muteButton">🔊 Mute</button>
<!-- Babylon.js CDN -->
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
<script>
// Initialize cameraMode and other variables
let cameraMode = 'tour';
const allowedCameraModes = ["tour","explore","auto"];
let loopMode = false;
let autoPlaySpeed = 0.05;
let autoPlayEnabled = false;
// Function to update scroll controls visibility
function updateScrollControlsVisibility() {
const scrollControlsContent = document.getElementById('scrollControlsContent');
const exploreControlsContent = document.getElementById('exploreControlsContent');
if (cameraMode === 'explore') {
scrollControlsContent.classList.add('hidden');
exploreControlsContent.classList.remove('hidden');
} else {
scrollControlsContent.classList.remove('hidden');
exploreControlsContent.classList.add('hidden');
}
}
// Function to toggle camera mode
function toggleCameraMode() {
const currentIndex = allowedCameraModes.indexOf(cameraMode);
const nextIndex = (currentIndex + 1) % allowedCameraModes.length;
cameraMode = allowedCameraModes[nextIndex];
const toggleButton = document.getElementById('toggleCameraMode');
if (toggleButton) {
toggleButton.textContent = 'Mode: ' + cameraMode.charAt(0).toUpperCase() + cameraMode.slice(1);
}
// Additional logic to handle camera mode change
if (cameraMode === 'tour') {
userControl = false;
} else if (cameraMode === 'explore') {
userControl = true;
}
if(BABYLON.VirtualJoystick.Canvas && (cameraMode === 'explore' || cameraMode === 'auto')){
BABYLON.VirtualJoystick.Canvas.style.zIndex = "10";
} else if (BABYLON.VirtualJoystick.Canvas && cameraMode === 'tour' ){
BABYLON.VirtualJoystick.Canvas.style.zIndex = "-10";
}
// For 'auto' mode, userControl will be dynamically updated based on user interactions
updateScrollControlsVisibility();
console.log('Camera Mode:', cameraMode);
}
updateScrollControlsVisibility();
const preloader = document.getElementById('preloader');
const canvas = document.getElementById('renderCanvas');
const engine = new BABYLON.Engine(canvas, true);
const scene = new BABYLON.Scene(engine);
window.scene = scene;
// Set the background color
scene.clearColor = BABYLON.Color3.FromHexString('#7D7D7D').toColor4(1);
// Create the camera
const camera = new BABYLON.UniversalCamera(
'camera',
new BABYLON.Vector3(1.9158737872562792, 1.215546252377102, 4.410765185258133),
scene
);
camera.attachControl(canvas, true);
// Adjust camera sensitivity
camera.speed = 0.2;
camera.angularSensibility = 4000;
// Initialize rotationQuaternion with the first waypoint's rotation
camera.rotationQuaternion = new BABYLON.Quaternion(
-0.004315008571729595,
0.9955545873581971,
-0.05796339021155113,
-0.07411276637084703
).normalize();
// Ensure Euler angles match the quaternion
camera.rotation = camera.rotationQuaternion.toEulerAngles();
// Enable WASD keys for movement
camera.keysUp.push(87); // W
camera.keysDown.push(83); // S
camera.keysLeft.push(65); // A
camera.keysRight.push(68); // D
// Add Q and E keys for vertical movement
camera.keysUpward.push(81); // Q
camera.keysDownward.push(69); // E
camera.inputs.addGamepad();
// Variables to manage camera control state
let userControl = cameraMode === 'explore';
let animatingToPath = false;
const isMobileDevice = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
navigator.userAgent.toLowerCase()
);
if (isMobileDevice) {
var leftJoystick = new BABYLON.VirtualJoystick(true);
var rightJoystick = new BABYLON.VirtualJoystick(false);
if('tour' === 'tour'){
if(BABYLON.VirtualJoystick.Canvas){
BABYLON.VirtualJoystick.Canvas.style.zIndex = "-10";
}} else {
if(BABYLON.VirtualJoystick.Canvas){
BABYLON.VirtualJoystick.Canvas.style.zIndex = "10";
}
}
scene.onBeforeRenderObservable.add(()=>{
if(leftJoystick.pressed){
userControl = true;
const deltaTime = engine.getDeltaTime() / 1000;
const moveSpeed = deltaTime * 5 * 0.2;
// Get the movement values from the joystick
const moveX = leftJoystick.deltaPosition.x * moveSpeed;
const moveZ = leftJoystick.deltaPosition.y * moveSpeed;
// Calculate the forward and right directions
const forward = camera.getDirection(BABYLON.Axis.Z);
const right = camera.getDirection(BABYLON.Axis.X);
// Calculate the movement vector
const move = forward.scale(moveZ).add(right.scale(moveX));
// Update the camera position
camera.position.addInPlace(move);
}
if (rightJoystick.pressed) {
const deltaX = rightJoystick.deltaPosition.x;
const deltaY = rightJoystick.deltaPosition.y;
const angularSensibility = 200 * 0.2; // Adjust rotation speed
// Get current yaw and pitch from the camera's rotation quaternion
const currentEuler = camera.rotationQuaternion.toEulerAngles();
let yaw = currentEuler.y;
let pitch = currentEuler.x;
// Update yaw and pitch based on joystick input
yaw += deltaX / angularSensibility;
pitch -= deltaY / angularSensibility;
// Clamp the pitch to prevent rolling
const maxPitch = Math.PI / 2; // Maximum pitch angle (90 degrees)
pitch = Math.max(-maxPitch, Math.min(maxPitch, pitch));
// Update the camera's rotation quaternion without introducing roll
camera.rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(yaw, pitch, 0);
}
})
}
// Enable camera collisions and gravity
camera.checkCollisions = true;
camera.applyGravity = false;
camera.ellipsoid = new BABYLON.Vector3(0.1, 0.1, 0.1);
// Create a basic light
new BABYLON.HemisphericLight('light', new BABYLON.Vector3(0, 1, 0), scene);
// Variables for scroll position and target
let scrollPosition = 0;
let scrollTarget = 0.01; // Start with a small value to enable scrolling
// Prepare waypoints and rotations
const waypoints = [{"x":1.9158737872562792,"y":1.215546252377102,"z":4.410765185258133,"rotation":{"_isDirty":true,"_x":-0.004315008571729595,"_y":0.9955545873581971,"_z":-0.05796339021155113,"_w":-0.07411276637084703},"interactions":[{"id":"interaction-1730060180650","type":"info","data":{"text":"The Ferrari Roma was introduced by the Italian car manufacturer in late 2019."}}],"triggerDistance":1},{"x":2.2137722959847443,"y":0.7344243027224736,"z":1.6518559167696782,"rotation":{"_isDirty":true,"_x":-0.05712675016104369,"_y":0.9261317414558283,"_z":-0.15629359225784173,"_w":-0.33850974688948243},"interactions":[{"id":"interaction-1730060334359","type":"info","data":{"text":"The exterior design was influenced by the Ferrari 250 GT Lusso and 250 GT 2+2 grand touring cars. "}}],"triggerDistance":1},{"x":-1.7980393295445076,"y":0.9710537858057341,"z":2.452787045658332,"rotation":{"_isDirty":true,"_x":0.024811493422088344,"_y":0.9541119952690545,"_z":-0.08254856441764032,"_w":0.28677593405651647},"interactions":[{"id":"interaction-1730060381356","type":"info","data":{"text":"The Roma's design features include flush door handles, slim LED lights at the front and rear, and an active rear spoiler that sits flush when the car is driven softly."}}],"triggerDistance":1},{"x":-2.5935250428435723,"y":0.41079662025065067,"z":-1.399984044425871,"rotation":{"_isDirty":true,"_x":-0.028113360393897644,"_y":0.5103910261516572,"_z":0.016697850268699386,"_w":0.8593205521414594},"interactions":[{"id":"interaction-1730060694172","type":"info","data":{}}],"triggerDistance":1},{"x":-2.3654964873462325,"y":0.9360532803328317,"z":-1.6929979601115996,"rotation":{"_isDirty":true,"_x":0.11141662548902771,"_y":0.4134251528955204,"_z":-0.051052704294329815,"_w":0.9022525146731548},"interactions":[{"id":"interaction-1730060449084","type":"info","data":{"text":"The Roma is powered by the Ferrari type F154 engine. It is a 3.9 litres (3,855 cc), turbocharged, 90 degree V-8, dual overhead cam (DOHC) design. The Roma's type 154BH is rated at 456 kW; 612 hp (620 PS) between 5,750 and 7,500 rpm and 761 N⋅m (561 lb⋅ft) of torque between 3,000 and 5,750 rpm. "}}],"triggerDistance":1},{"x":1.2909027813621659,"y":0.6163783506428221,"z":-2.74016269892382,"rotation":{"_isDirty":true,"_x":0.011400500961164902,"_y":-0.22099227777276803,"_z":0.0025834785132588097,"_w":0.9752055086060862},"interactions":[{"id":"interaction-1730060498303","type":"info","data":{"text":"The Roma was originally introduced with a base price of $218,670 (2020 USD). Typical final build-out configurations could range from $270k to $310k and up. Ferrari estimates 70 percent of buyers would be first-time Ferrari customers. They stated that the Roma is targeted squarely at the Porsche 911 and Aston Martin markets.[22] The overall design theme is \"understated elegance\" which is somewhat of a deviation from the traditional Ferrari road car. "}}],"triggerDistance":1},{"x":3.9120502375658486,"y":0.6163783505151927,"z":0.13392975492250753,"rotation":{"_isDirty":true,"_x":0.011891205643247141,"_y":-0.69985442786013,"_z":0.011654128788274746,"_w":0.714091405020037},"interactions":[{"id":"interaction-1730060534016","type":"info","data":{"text":"The Ferrari Roma was awarded a Red Dot in 2020, recognizing the car's design. Red Dot was quoted as saying, \"By renouncing all superfluous details, the design of the Ferrari Roma achieves a formal minimalism that characterizes the timeless elegance of this sports car.\""}}],"triggerDistance":1}];
const controlPoints = waypoints.map(
(wp) => new BABYLON.Vector3(wp.x, wp.y, wp.z)
);
const rotations = waypoints.map(
(wp) => new BABYLON.Quaternion(wp.rotation._x, wp.rotation._y, wp.rotation._z, wp.rotation._w).normalize()
);
let path = [];
if (controlPoints.length >= 2) {
const positionCurve = BABYLON.Curve3.CreateCatmullRomSpline(
controlPoints,
20, // Adjust resolution if needed
false //loopMode
);
path = positionCurve.getPoints();
} else if (controlPoints.length === 1) {
path = [controlPoints[0]];
}
// Create hotspots
const hotspots = [];
hotspots.forEach(hotspot => {
const scale = (hotspot.scale._x === 0 && hotspot.scale._y === 0 && hotspot.scale._z === 0)
? new BABYLON.Vector3(1, 1, 1)
: new BABYLON.Vector3(hotspot.scale._x, hotspot.scale._y, hotspot.scale._z);
const sphere = BABYLON.MeshBuilder.CreateSphere(`hotspot-${hotspot.id}`, { diameter: 0.2 }, scene);
sphere.position = new BABYLON.Vector3(hotspot.position._x, hotspot.position._y, hotspot.position._z);
sphere.scaling = scale;
const material = new BABYLON.StandardMaterial(`hotspot-material-${hotspot.id}`, scene);
material.diffuseColor = BABYLON.Color3.FromHexString(hotspot.color);
material.emissiveColor = BABYLON.Color3.FromHexString(hotspot.color).scale(0.5);
sphere.material = material;
sphere.isPickable = true;
sphere.actionManager = new BABYLON.ActionManager(scene);
sphere.actionManager.registerAction(
new BABYLON.ExecuteCodeAction(
BABYLON.ActionManager.OnPointerOverTrigger,
() => {
material.emissiveColor = BABYLON.Color3.FromHexString(hotspot.color);
if (hotspot.activationMode === 'hover') {
showHotspotContent(hotspot);
}
}
)
);
sphere.actionManager.registerAction(
new BABYLON.ExecuteCodeAction(
BABYLON.ActionManager.OnPointerOutTrigger,
() => {
material.emissiveColor = BABYLON.Color3.FromHexString(hotspot.color).scale(0.5);
if (hotspot.activationMode === 'hover') {
hideHotspotContent();
}
}
)
);
if (hotspot.activationMode === 'click') {
sphere.actionManager.registerAction(
new BABYLON.ExecuteCodeAction(
BABYLON.ActionManager.OnPickTrigger,
() => {
showHotspotContent(hotspot);
}
)
);
}
});
// Function to show hotspot content
function showHotspotContent(hotspot) {
const hotspotContent = document.getElementById('hotspotContent');
hotspotContent.innerHTML = `
<h3>${hotspot.title}</h3>
${hotspot.photoUrl ? `<img src="${hotspot.photoUrl}" alt="${hotspot.title}" style="width: 100%; margin-bottom: 10px; border-radius: 5px;">` : ''}
${hotspot.information ? `<p>${hotspot.information}</p>` : ''}
${hotspot.activationMode === 'click' ? '<button onclick="hideHotspotContent()" style="width: 100%; padding: 10px; background-color: #4CAF50; border: none; color: white; cursor: pointer; border-radius: 5px;">Close</button>' : ''}
`;
hotspotContent.style.display = 'block';
positionHotspotContent(hotspotContent);
}
// Function to hide hotspot content
function hideHotspotContent() {
const hotspotContent = document.getElementById('hotspotContent');
hotspotContent.style.display = 'none';
}
// Function to position hotspot content near the mouse
function positionHotspotContent(element) {
const rect = element.getBoundingClientRect();
let left = scene.pointerX + 10;
let top = scene.pointerY + 10;
if (left + rect.width > window.innerWidth) {
left = window.innerWidth - rect.width - 10;
}
if (top + rect.height > window.innerHeight) {
top = window.innerHeight - rect.height - 10;
}
element.style.left = `${left}px`;
element.style.top = `${top}px`;
}
// Collision Meshes Data
const collisionMeshesData = [];
// Function to reconstruct collision meshes
function createCollisionMeshes() {
collisionMeshesData.forEach((data, index) => {
const mesh = BABYLON.MeshBuilder.CreatePlane(
`collisionMesh-${index}`,
{ size: 3 },
scene
);
mesh.position = new BABYLON.Vector3(
data.position._x,
data.position._y,
data.position._z
);
mesh.rotation = new BABYLON.Vector3(
data.rotation._x,
data.rotation._y,
data.rotation._z
);
mesh.scaling = new BABYLON.Vector3(
data.scaling._x,
data.scaling._y,
data.scaling._z
);
mesh.checkCollisions = true;
mesh.isVisible = false; // Hide collision meshes
// Optional: Apply material if you want to visualize the collision meshes
/*
const material = new BABYLON.StandardMaterial(
`collisionMaterial-${index}`,
scene
);
material.backFaceCulling = false;
material.diffuseColor = BABYLON.Color3.FromHexString("#FF0000");
mesh.material = material;
*/
});
}
// Call the function to create collision meshes
createCollisionMeshes();
// Mute state
let isMuted = false;
const activeSounds = {};
// Updated playAudio function
function playAudio(interactionData, waypointIndex) {
if (isMuted) return;
const id = interactionData.id;
const url = interactionData.url;
const data = interactionData;
// If sound is already playing, do not play it again
if (activeSounds[id] && activeSounds[id].isPlaying) {
return;
}
if (activeSounds[id]) {
// Sound object already exists, play it if not playing
if (!activeSounds[id].isPlaying) {
activeSounds[id].play();
}
} else {
// Create new sound
const sound = new BABYLON.Sound(
id,
url,
scene,
() => {
// Play the sound once it's ready
sound.play();
},
{
loop: data.loop !== undefined ? data.loop : true,
volume: data.volume !== undefined ? data.volume : 1,
spatialSound: data.spatialSound !== undefined ? data.spatialSound : false,
distanceModel: data.distanceModel !== undefined ? data.distanceModel : "exponential",
maxDistance: data.maxDistance !== undefined ? data.maxDistance : 100,
refDistance: data.refDistance !== undefined ? data.refDistance : 1,
rolloffFactor: data.rolloffFactor !== undefined ? data.rolloffFactor : 1,
}
);
activeSounds[id] = sound;
if (data.spatialSound) {
let position;
if (waypointIndex !== undefined && waypoints[waypointIndex]) {
const waypoint = waypoints[waypointIndex];
position = new BABYLON.Vector3(waypoint.x, waypoint.y, waypoint.z);
} else {
position = new BABYLON.Vector3(0, 0, 0); // Default position if waypoint is undefined
}
sound.setPosition(position);
}
}
}
// Updated stopAudio function
function stopAudio(interactionData) {
const id = interactionData.id;
const sound = activeSounds[id];
if (sound && sound.isPlaying) {
sound.stop();
}
// Remove the sound from activeSounds to clean up
delete activeSounds[id];
}
// Function to execute interactions
const executeInteractions = (interactions, waypointIndex) => {
interactions.forEach((interaction) => {
switch (interaction.type) {
case "audio":
playAudio({ ...interaction.data, id: interaction.id }, waypointIndex);
break;
case "info":
showInfoPopup(interaction.data.text);
break;
// Add other interaction types here if needed
}
});
};
// Function to reverse interactions
const reverseInteractions = (interactions) => {
interactions.forEach((interaction) => {
switch (interaction.type) {
case "audio":
const data = interaction.data;
if (!data.spatialSound && data.stopOnExit) {
stopAudio({ ...data, id: interaction.id });
}
break;
case "info":
hideInfoPopup();
break;
// Add other interaction types here if needed
}
});
};
let transitionSpeed = 0.5;
function updateTransitionSpeed(value) {
transitionSpeed = parseFloat(value);
document.getElementById('transitionSpeedValue').textContent = 'Transition Speed: ' + transitionSpeed.toFixed(1);
}
// Function to show info popup
function showInfoPopup(text) {
const infoPopup = document.getElementById('infoPopup');
infoPopup.innerHTML = `
<p>${text}</p>
<button onclick="hideInfoPopup()" style="width: 100%; padding: 10px; background-color: #4CAF50; border: none; color: white; cursor: pointer; border-radius: 5px;">Close</button>
`;
infoPopup.style.display = 'block';
}
// Function to hide info popup
function hideInfoPopup() {
const infoPopup = document.getElementById('infoPopup');
infoPopup.style.display = 'none';
}
// Function to update scroll percentage and progress bar
function updateScrollUI(percentage) {
const scrollPercentage = document.getElementById('scrollPercentage');
const progressBar = document.getElementById('progressBar');
if (scrollPercentage && progressBar) {
scrollPercentage.textContent = `${Math.round(percentage)}%`;
progressBar.style.width = `${percentage}%`;
}
}
const scrollButtonMode = 'waypoint';
const scrollAmount = 10;
function handleButtonScroll(direction) {
if (cameraMode === 'explore') {
return;
}
if (scrollButtonMode === 'percentage') {
adjustScroll(direction * scrollAmount / 100);
} else {
moveToWaypoint(direction);
}
}
function adjustScroll(amount) {
const pathLength = path.length;
if (pathLength > 1) {
const scrollIncrement = (pathLength - 1) * amount;
scrollTarget += scrollIncrement;
if (scrollTarget < 0) scrollTarget = 0;
if (scrollTarget > path.length - 1) scrollTarget = path.length - 1;
userControl = false;
}
}
function moveToWaypoint(direction) {
const subdivisionsPerSegment = 20;
let currentWaypointIndex = Math.round(scrollPosition / subdivisionsPerSegment);
let nextWaypointIndex = currentWaypointIndex + direction;
nextWaypointIndex = Math.max(0, Math.min(nextWaypointIndex, waypoints.length - 1));
scrollTarget = nextWaypointIndex * subdivisionsPerSegment;
userControl = false;
}
// Handle scroll events
window.addEventListener('wheel', (event) => {
if (animatingToPath) return;
if (
(cameraMode === 'auto' && userControl) ||
(cameraMode === 'tour' && userControl)
) {
animatingToPath = true;
userControl = false;
if (!camera.rotationQuaternion) {
camera.rotationQuaternion = BABYLON.Quaternion.FromEulerAngles(
camera.rotation.x,
camera.rotation.y,
camera.rotation.z
).normalize();
camera.rotation.set(0, 0, 0);
}
// Use current scroll position instead of finding closest point
const targetT = scrollPosition;
const pathLength = path.length - 1;
const floorIndex = Math.floor(targetT);
const ceilIndex = Math.min(floorIndex + 1, pathLength);
const lerpFactor = targetT - floorIndex;
const targetPosition = BABYLON.Vector3.Lerp(
path[floorIndex],
path[ceilIndex],
lerpFactor
);
let targetRotation = camera.rotationQuaternion.clone();
if (rotations.length >= 2 && path.length >= 2) {
const totalSegments = waypoints.length - 1;
const segmentT = (targetT / pathLength) * totalSegments;
const segmentIndex = Math.floor(segmentT);
const clampedSegmentIndex = Math.min(segmentIndex, totalSegments - 1);
const lerpFactorRot = segmentT - clampedSegmentIndex;
const r1 = rotations[clampedSegmentIndex];
const nextIndex = (clampedSegmentIndex + 1) % rotations.length;
const r2 = rotations[nextIndex];
targetRotation = BABYLON.Quaternion.Slerp(r1, r2, lerpFactorRot).normalize();
} else if (rotations.length === 1) {
targetRotation = rotations[0];
}
const positionAnimation = new BABYLON.Animation(
'cameraPositionAnimation',
'position',
60,
BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT
);
const positionKeys = [];
positionKeys.push({ frame: 0, value: camera.position.clone() });
positionKeys.push({ frame: 120, value: targetPosition.clone() });
positionAnimation.setKeys(positionKeys);
const easingFunction = new BABYLON.CubicEase();
easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT);
positionAnimation.setEasingFunction(easingFunction);
const rotationAnimation = new BABYLON.Animation(
'cameraRotationAnimation',
'rotationQuaternion',
60,
BABYLON.Animation.ANIMATIONTYPE_QUATERNION,
BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT
);
const currentRotation = camera.rotationQuaternion.clone();
rotationAnimation.setKeys([
{ frame: 0, value: currentRotation },
{ frame: 120, value: targetRotation },
]);
rotationAnimation.setEasingFunction(easingFunction);
camera.animations = [];
camera.animations.push(positionAnimation);
camera.animations.push(rotationAnimation);
scene.beginAnimation(camera, 0, 120, false, 1, function () {
animatingToPath = false;
scrollPosition = targetT;
scrollTarget = targetT;
});
} else if (cameraMode !== 'explore') {
scrollTarget += event.deltaY * 0.29;
if (scrollTarget < 0) scrollTarget = 0;
if (scrollTarget > path.length - 1) scrollTarget = path.length - 1;
}
});
// Initialize target rotation and position
let targetRotation = camera.rotationQuaternion.clone();
let targetPosition = camera.position.clone();
// Active waypoints set
const activeWaypoints = new Set();
if(false) {
// Create floor mesh at y = 0 and hide it
const ground = BABYLON.MeshBuilder.CreateGround('ground', { width: 100, height: 100 }, scene);
ground.isVisible = false;
// WebXR setup
const xr = scene.createDefaultXRExperienceAsync({
floorMeshes: [ground],
});
}
// Load the model file
BABYLON.SceneLoader.ImportMeshAsync('', '', 'https://storage.googleapis.com/sonnys-bucket/rubloff/gs_car.splat', scene)
.then((result) => {
const loadedMeshes = result.meshes;
loadedMeshes.forEach((mesh) => {
if (mesh instanceof BABYLON.Mesh) {
mesh.position = BABYLON.Vector3.Zero();
}
});
// Hide the preloader after the model is loaded
preloader.classList.add('hidden');
})
.catch((error) => {
console.error('Error loading model file:', error);
alert('Error loading model file: ' + error.message);
preloader.classList.add('hidden');
});
// Start the render loop
engine.runRenderLoop(function () {
// Smoothly interpolate scrollPosition towards scrollTarget
const scrollInterpolationSpeed = 0.01 + transitionSpeed * 0.1;
// Autoplay handling
if (autoPlayEnabled && !userControl) {
scrollTarget += autoPlaySpeed;
}
scrollPosition += (scrollTarget - scrollPosition) * scrollInterpolationSpeed;
if (loopMode) {
if (scrollPosition >= path.length - 1.1) {
scrollPosition = 0.2;
scrollTarget = 0.2;
} else if (scrollPosition <= 0.1) {
scrollPosition = path.length - 1.11;
scrollTarget = path.length - 1.11;
}
}
// Clamp scroll position
scrollPosition = Math.max(0, Math.min(scrollPosition, path.length - 1));
// Calculate scroll percentage
const scrollPercentage = (scrollPosition / (path.length - 1 || 1)) * 100;
// Update UI only if not in explore mode
if (cameraMode !== 'explore') {
updateScrollUI(scrollPercentage);
}
// Determine if camera should follow the path
if (
(cameraMode === 'auto' && !userControl) ||
(cameraMode === 'tour' && !userControl)
) {
const t = scrollPosition / (path.length - 1 || 1);
const totalSegments = waypoints.length - 1;
if (totalSegments >= 1) {
const segmentT = t * totalSegments;
const segmentIndex = Math.floor(segmentT);
const clampedSegmentIndex = Math.min(segmentIndex, totalSegments - 1);
const lerpFactor = segmentT - clampedSegmentIndex;