-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3d-world-navigation.html
More file actions
878 lines (744 loc) · 36.1 KB
/
3d-world-navigation.html
File metadata and controls
878 lines (744 loc) · 36.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>World Navigator</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-tap-highlight-color: transparent;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: #000;
color: #fff;
overflow: hidden;
position: relative;
}
#three-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
touch-action: none;
}
.world-ui {
position: absolute;
top: 20px;
left: 20px;
z-index: 1001;
}
.world-title {
font-size: 3em;
font-weight: 100;
letter-spacing: 0.2em;
text-transform: uppercase;
background: linear-gradient(45deg, #ff006e, #8338ec, #3a86ff, #06ffa5);
background-size: 400% 100%;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
animation: shimmer 8s ease-in-out infinite;
margin-bottom: 10px;
}
@keyframes shimmer {
0%, 100% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
}
.world-description {
font-size: 1.2em;
color: rgba(255, 255, 255, 0.6);
max-width: 400px;
}
.controls-hint {
position: absolute;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.7);
padding: 15px 30px;
border-radius: 25px;
font-size: 0.9em;
color: rgba(255, 255, 255, 0.8);
z-index: 1001;
text-align: center;
backdrop-filter: blur(10px);
}
.portal-tooltip {
position: absolute;
background: rgba(0, 0, 0, 0.9);
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 15px;
padding: 20px;
max-width: 300px;
display: none;
z-index: 1002;
pointer-events: none;
backdrop-filter: blur(10px);
}
.portal-tooltip.visible {
display: block;
}
.tooltip-title {
font-size: 1.4em;
font-weight: 300;
margin-bottom: 10px;
color: #06ffa5;
}
.tooltip-description {
font-size: 0.9em;
color: rgba(255, 255, 255, 0.7);
line-height: 1.5;
}
.tooltip-instruction {
font-size: 0.85em;
color: #ff006e;
text-align: center;
margin-top: 15px;
padding-top: 15px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
/* Mobile controls */
.mobile-controls {
position: absolute;
bottom: 100px;
left: 50%;
transform: translateX(-50%);
display: none;
z-index: 1001;
gap: 20px;
}
.mobile-controls.show {
display: flex;
}
.joystick-container {
width: 120px;
height: 120px;
background: rgba(255, 255, 255, 0.1);
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 50%;
position: relative;
touch-action: none;
}
.joystick-handle {
width: 50px;
height: 50px;
background: rgba(255, 255, 255, 0.5);
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: none;
}
.loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
font-size: 1.2em;
color: rgba(255, 255, 255, 0.4);
font-weight: 200;
letter-spacing: 0.1em;
z-index: 2000;
}
.loading::after {
content: '';
display: block;
width: 60px;
height: 1px;
background: linear-gradient(90deg, transparent, #fff, transparent);
margin: 30px auto;
animation: scan 2s linear infinite;
}
@keyframes scan {
0% { transform: translateX(-100px); }
100% { transform: translateX(100px); }
}
@media (max-width: 768px) {
.world-title {
font-size: 2em;
}
.world-description {
font-size: 1em;
max-width: 300px;
}
}
</style>
</head>
<body>
<div id="three-container"></div>
<div class="world-ui">
<h1 class="world-title" id="world-title">LOADING WORLD</h1>
<p class="world-description" id="world-description">Discovering portals...</p>
</div>
<div class="controls-hint" id="controls-hint">Use WASD to move, Mouse to look around, Click on portals to travel</div>
<div class="mobile-controls" id="mobile-controls">
<div class="joystick-container" id="movement-joystick">
<div class="joystick-handle" id="movement-handle"></div>
</div>
</div>
<div class="portal-tooltip" id="portal-tooltip">
<h3 class="tooltip-title"></h3>
<p class="tooltip-description"></p>
<p class="tooltip-instruction">Click to enter world</p>
</div>
<div class="loading" id="loading">Initializing world...</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Configuration - Update these with your GitHub details
const REPO_OWNER = 'your-github-username'; // Replace with your GitHub username
const REPO_NAME = 'your-repo-name'; // Replace with your repository name
const BRANCH = 'main';
// World metadata embedded in this file
const CURRENT_WORLD = {
name: "Nexus Hub",
description: "The central hub connecting all worlds. Step through the portals to explore different dimensions.",
ambientColor: 0x0a0a0a,
fogColor: 0x000033,
groundColor: 0x1a1a2e,
skyColor: 0x16213e
};
// Check if device is mobile
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
(window.matchMedia && window.matchMedia('(max-width: 768px)').matches);
class WorldNavigator {
constructor() {
this.scene = null;
this.camera = null;
this.renderer = null;
this.portals = [];
this.worlds = [];
this.raycaster = new THREE.Raycaster();
this.clock = new THREE.Clock();
// Movement
this.moveSpeed = 0.15;
this.lookSpeed = 0.002;
this.keys = { w: false, a: false, s: false, d: false };
this.rotation = { x: 0, y: 0 };
// Mobile
this.isMobile = isMobile;
this.joystickActive = false;
this.joystickVector = new THREE.Vector2();
this.touches = { start: null, current: null };
// UI
this.hoveredPortal = null;
this.tooltip = document.getElementById('portal-tooltip');
this.tooltipTitle = this.tooltip.querySelector('.tooltip-title');
this.tooltipDescription = this.tooltip.querySelector('.tooltip-description');
this.isPointerLocked = false;
}
async init() {
// Hide loading after scene is ready
setTimeout(() => {
document.getElementById('loading').style.display = 'none';
}, 1000);
// Update UI
document.getElementById('world-title').textContent = CURRENT_WORLD.name;
document.getElementById('world-description').textContent = CURRENT_WORLD.description;
// Setup Three.js
this.setupScene();
this.setupLighting();
await this.loadWorlds();
this.createEnvironment();
this.createPortals();
this.setupEventListeners();
// Update controls hint for mobile
if (this.isMobile) {
document.getElementById('controls-hint').textContent = 'Swipe to look around, Use joystick to move, Tap on portals to travel';
document.getElementById('mobile-controls').classList.add('show');
}
this.animate();
}
setupScene() {
// Scene
this.scene = new THREE.Scene();
this.scene.fog = new THREE.Fog(CURRENT_WORLD.fogColor, 10, 100);
// Camera
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
this.camera.position.set(0, 2, 10);
// Renderer
this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.getElementById('three-container').appendChild(this.renderer.domElement);
}
setupLighting() {
// Ambient light
const ambientLight = new THREE.AmbientLight(CURRENT_WORLD.ambientColor, 0.5);
this.scene.add(ambientLight);
// Main directional light
const dirLight = new THREE.DirectionalLight(0xffffff, 0.5);
dirLight.position.set(10, 20, 10);
dirLight.castShadow = true;
dirLight.shadow.camera.near = 0.1;
dirLight.shadow.camera.far = 100;
dirLight.shadow.camera.left = -50;
dirLight.shadow.camera.right = 50;
dirLight.shadow.camera.top = 50;
dirLight.shadow.camera.bottom = -50;
this.scene.add(dirLight);
// Portal glow lights will be added with portals
}
async loadWorlds() {
try {
const apiUrl = `https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/git/trees/${BRANCH}?recursive=1`;
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const data = await response.json();
// Find all HTML files in root directory (these are world files)
this.worlds = data.tree
.filter(item =>
item.type === 'blob' &&
item.path.endsWith('.html') &&
!item.path.includes('/') && // Only root directory
!item.path.toLowerCase().includes('index') && // Exclude index files
!item.path.toLowerCase().includes('readme') // Exclude readme files
)
.map(item => ({
path: item.path,
name: this.formatWorldName(item.path),
url: `https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}/${BRANCH}/${item.path}`,
raw_url: `https://raw.githubusercontent.com/${REPO_OWNER}/${REPO_NAME}/${BRANCH}/${item.path}`
}));
console.log(`Found ${this.worlds.length} worlds to connect`);
} catch (error) {
console.error("Error loading worlds:", error);
// Continue with demo portals if GitHub fails
this.worlds = this.getDemoWorlds();
}
}
formatWorldName(filename) {
return filename
.replace('.html', '')
.replace(/[-_]/g, ' ')
.replace(/\b\w/g, l => l.toUpperCase());
}
getDemoWorlds() {
// Fallback demo worlds if GitHub fetch fails
return [
{ name: "Crystal Caves", description: "A world of glowing crystals and underground mysteries" },
{ name: "Sky Islands", description: "Floating islands connected by bridges of light" },
{ name: "Neon City", description: "A cyberpunk metropolis bathed in neon lights" },
{ name: "Ancient Ruins", description: "Explore the remnants of a lost civilization" },
{ name: "Ocean Depths", description: "An underwater realm of bioluminescent wonders" }
];
}
createEnvironment() {
// Ground
const groundGeometry = new THREE.PlaneGeometry(200, 200, 50, 50);
const groundMaterial = new THREE.MeshStandardMaterial({
color: CURRENT_WORLD.groundColor,
roughness: 0.8,
metalness: 0.2
});
// Add some vertex displacement for terrain
const vertices = groundGeometry.attributes.position.array;
for (let i = 0; i < vertices.length; i += 3) {
vertices[i + 2] = Math.sin(vertices[i] * 0.1) * Math.cos(vertices[i + 1] * 0.1) * 0.5;
}
groundGeometry.computeVertexNormals();
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
this.scene.add(ground);
// Grid overlay
const gridHelper = new THREE.GridHelper(200, 100, 0x444444, 0x222222);
gridHelper.position.y = 0.1;
this.scene.add(gridHelper);
// Skybox gradient
const skyGeometry = new THREE.SphereGeometry(500, 32, 32);
const skyMaterial = new THREE.ShaderMaterial({
uniforms: {
topColor: { value: new THREE.Color(CURRENT_WORLD.skyColor) },
bottomColor: { value: new THREE.Color(CURRENT_WORLD.fogColor) },
offset: { value: 33 },
exponent: { value: 0.6 }
},
vertexShader: `
varying vec3 vWorldPosition;
void main() {
vec4 worldPosition = modelMatrix * vec4(position, 1.0);
vWorldPosition = worldPosition.xyz;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform vec3 topColor;
uniform vec3 bottomColor;
uniform float offset;
uniform float exponent;
varying vec3 vWorldPosition;
void main() {
float h = normalize(vWorldPosition + offset).y;
gl_FragColor = vec4(mix(bottomColor, topColor, max(pow(max(h, 0.0), exponent), 0.0)), 1.0);
}
`,
side: THREE.BackSide
});
const sky = new THREE.Mesh(skyGeometry, skyMaterial);
this.scene.add(sky);
// Floating particles
const particleGeometry = new THREE.BufferGeometry();
const particleCount = 1000;
const positions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount * 3; i += 3) {
positions[i] = (Math.random() - 0.5) * 100;
positions[i + 1] = Math.random() * 50;
positions[i + 2] = (Math.random() - 0.5) * 100;
}
particleGeometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
const particleMaterial = new THREE.PointsMaterial({
color: 0xffffff,
size: 0.1,
transparent: true,
opacity: 0.6
});
const particles = new THREE.Points(particleGeometry, particleMaterial);
this.scene.add(particles);
}
createPortals() {
const numPortals = Math.min(this.worlds.length, 8); // Limit to 8 portals max
const radius = 15;
for (let i = 0; i < numPortals; i++) {
const world = this.worlds[i] || this.getDemoWorlds()[i];
const angle = (i / numPortals) * Math.PI * 2;
// Portal group
const portalGroup = new THREE.Group();
// Portal frame
const frameGeometry = new THREE.TorusGeometry(3, 0.3, 16, 32);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x8338ec,
emissive: 0x8338ec,
emissiveIntensity: 0.5,
metalness: 0.8,
roughness: 0.2
});
const frame = new THREE.Mesh(frameGeometry, frameMaterial);
// Portal surface
const portalGeometry = new THREE.CircleGeometry(2.7, 32);
const portalMaterial = new THREE.ShaderMaterial({
uniforms: {
time: { value: 0 },
color1: { value: new THREE.Color(0x00ffff) },
color2: { value: new THREE.Color(0xff00ff) }
},
vertexShader: `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: `
uniform float time;
uniform vec3 color1;
uniform vec3 color2;
varying vec2 vUv;
void main() {
vec2 center = vUv - 0.5;
float dist = length(center);
float wave = sin(dist * 10.0 - time * 2.0) * 0.5 + 0.5;
vec3 color = mix(color1, color2, wave);
float alpha = 1.0 - smoothstep(0.4, 0.5, dist);
gl_FragColor = vec4(color, alpha * 0.8);
}
`,
transparent: true,
side: THREE.DoubleSide
});
const portal = new THREE.Mesh(portalGeometry, portalMaterial);
portal.position.z = 0.1;
// Add to group
portalGroup.add(frame);
portalGroup.add(portal);
// Position portal
portalGroup.position.x = Math.cos(angle) * radius;
portalGroup.position.z = Math.sin(angle) * radius;
portalGroup.position.y = 3;
portalGroup.rotation.y = -angle + Math.PI;
// Add point light
const light = new THREE.PointLight(0x8338ec, 1, 10);
light.position.copy(portalGroup.position);
this.scene.add(light);
// Store portal data
portal.userData = {
world: world,
material: portalMaterial,
light: light,
url: world.url || world.raw_url,
name: world.name,
description: world.description || `Enter the ${world.name} world`
};
this.portals.push(portal);
this.scene.add(portalGroup);
// Floating platform under portal
const platformGeometry = new THREE.CylinderGeometry(4, 4, 0.5, 32);
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x444444,
metalness: 0.5,
roughness: 0.5
});
const platform = new THREE.Mesh(platformGeometry, platformMaterial);
platform.position.copy(portalGroup.position);
platform.position.y = 0.25;
platform.receiveShadow = true;
platform.castShadow = true;
this.scene.add(platform);
}
}
setupEventListeners() {
// Resize
window.addEventListener('resize', () => {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
});
if (this.isMobile) {
this.setupMobileControls();
} else {
this.setupDesktopControls();
}
}
setupDesktopControls() {
// Keyboard
window.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() in this.keys) {
this.keys[e.key.toLowerCase()] = true;
}
});
window.addEventListener('keyup', (e) => {
if (e.key.toLowerCase() in this.keys) {
this.keys[e.key.toLowerCase()] = false;
}
});
// Mouse
this.renderer.domElement.addEventListener('click', () => {
if (!this.isPointerLocked) {
this.renderer.domElement.requestPointerLock();
}
});
document.addEventListener('pointerlockchange', () => {
this.isPointerLocked = document.pointerLockElement === this.renderer.domElement;
});
document.addEventListener('mousemove', (e) => {
if (this.isPointerLocked) {
this.rotation.y -= e.movementX * this.lookSpeed;
this.rotation.x -= e.movementY * this.lookSpeed;
this.rotation.x = Math.max(-Math.PI/2, Math.min(Math.PI/2, this.rotation.x));
}
});
// Portal clicks
this.renderer.domElement.addEventListener('click', (e) => {
if (!this.isPointerLocked) return;
this.raycaster.setFromCamera(new THREE.Vector2(0, 0), this.camera);
const intersects = this.raycaster.intersectObjects(this.portals);
if (intersects.length > 0) {
const portal = intersects[0].object;
if (portal.userData.url) {
this.enterWorld(portal.userData.url);
}
}
});
}
setupMobileControls() {
const canvas = this.renderer.domElement;
// Touch controls for looking
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
const touch = e.touches[0];
this.touches.start = { x: touch.clientX, y: touch.clientY };
this.touches.current = { ...this.touches.start };
});
canvas.addEventListener('touchmove', (e) => {
e.preventDefault();
if (!this.touches.start) return;
const touch = e.touches[0];
const deltaX = touch.clientX - this.touches.current.x;
const deltaY = touch.clientY - this.touches.current.y;
this.rotation.y -= deltaX * this.lookSpeed * 2;
this.rotation.x -= deltaY * this.lookSpeed * 2;
this.rotation.x = Math.max(-Math.PI/2, Math.min(Math.PI/2, this.rotation.x));
this.touches.current = { x: touch.clientX, y: touch.clientY };
});
canvas.addEventListener('touchend', (e) => {
e.preventDefault();
// Check for tap
if (this.touches.start && this.touches.current) {
const dx = this.touches.current.x - this.touches.start.x;
const dy = this.touches.current.y - this.touches.start.y;
if (Math.sqrt(dx * dx + dy * dy) < 10) {
// It's a tap
const rect = canvas.getBoundingClientRect();
const x = ((this.touches.start.x - rect.left) / rect.width) * 2 - 1;
const y = -((this.touches.start.y - rect.top) / rect.height) * 2 + 1;
this.raycaster.setFromCamera(new THREE.Vector2(x, y), this.camera);
const intersects = this.raycaster.intersectObjects(this.portals);
if (intersects.length > 0) {
const portal = intersects[0].object;
if (portal.userData.url) {
this.enterWorld(portal.userData.url);
}
}
}
}
this.touches.start = null;
this.touches.current = null;
});
// Joystick
const joystick = document.getElementById('movement-joystick');
const handle = document.getElementById('movement-handle');
joystick.addEventListener('touchstart', (e) => {
e.preventDefault();
this.joystickActive = true;
this.updateJoystick(e.touches[0], joystick, handle);
});
joystick.addEventListener('touchmove', (e) => {
e.preventDefault();
if (this.joystickActive) {
this.updateJoystick(e.touches[0], joystick, handle);
}
});
joystick.addEventListener('touchend', () => {
this.joystickActive = false;
this.joystickVector.set(0, 0);
handle.style.transform = 'translate(-50%, -50%)';
});
}
updateJoystick(touch, joystick, handle) {
const rect = joystick.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
let deltaX = touch.clientX - centerX;
let deltaY = touch.clientY - centerY;
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
const maxDistance = rect.width / 2 - 25;
if (distance > maxDistance) {
deltaX = (deltaX / distance) * maxDistance;
deltaY = (deltaY / distance) * maxDistance;
}
handle.style.transform = `translate(calc(-50% + ${deltaX}px), calc(-50% + ${deltaY}px))`;
this.joystickVector.x = deltaX / maxDistance;
this.joystickVector.y = -deltaY / maxDistance;
}
updateMovement() {
const forward = new THREE.Vector3();
const right = new THREE.Vector3();
this.camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
right.crossVectors(forward, new THREE.Vector3(0, 1, 0));
if (this.isMobile) {
if (this.joystickActive) {
const moveForward = forward.multiplyScalar(this.joystickVector.y * this.moveSpeed);
const moveRight = right.multiplyScalar(this.joystickVector.x * this.moveSpeed);
this.camera.position.add(moveForward);
this.camera.position.add(moveRight);
}
} else {
if (this.keys.w) this.camera.position.add(forward.multiplyScalar(this.moveSpeed));
if (this.keys.s) this.camera.position.add(forward.multiplyScalar(-this.moveSpeed));
if (this.keys.a) this.camera.position.add(right.multiplyScalar(-this.moveSpeed));
if (this.keys.d) this.camera.position.add(right.multiplyScalar(this.moveSpeed));
}
// Update camera rotation
this.camera.rotation.order = 'YXZ';
this.camera.rotation.y = this.rotation.y;
this.camera.rotation.x = this.rotation.x;
}
updateHover() {
// Cast ray from camera center
this.raycaster.setFromCamera(new THREE.Vector2(0, 0), this.camera);
const intersects = this.raycaster.intersectObjects(this.portals);
if (intersects.length > 0) {
const portal = intersects[0].object;
if (portal !== this.hoveredPortal) {
this.hoveredPortal = portal;
// Show tooltip
this.tooltipTitle.textContent = portal.userData.name;
this.tooltipDescription.textContent = portal.userData.description;
this.tooltip.classList.add('visible');
// Position tooltip
const vector = new THREE.Vector3();
portal.getWorldPosition(vector);
vector.project(this.camera);
const x = (vector.x * 0.5 + 0.5) * window.innerWidth;
const y = (-vector.y * 0.5 + 0.5) * window.innerHeight;
this.tooltip.style.left = `${Math.min(x + 20, window.innerWidth - 320)}px`;
this.tooltip.style.top = `${Math.min(y - 50, window.innerHeight - 200)}px`;
}
} else {
if (this.hoveredPortal) {
this.hoveredPortal = null;
this.tooltip.classList.remove('visible');
}
}
}
async enterWorld(url) {
try {
// Show loading
document.getElementById('loading').style.display = 'block';
document.getElementById('loading').innerHTML = 'Entering new world...';
// Fetch the HTML content
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to load world (${response.status})`);
}
const htmlContent = await response.text();
// Replace current page with new world
// Create blob and navigate
const blob = new Blob([htmlContent], { type: 'text/html' });
const blobUrl = URL.createObjectURL(blob);
// Navigate to the new world
window.location.href = blobUrl;
} catch (error) {
console.error('Error entering world:', error);
document.getElementById('loading').innerHTML = 'Failed to enter world. Please try again.';
setTimeout(() => {
document.getElementById('loading').style.display = 'none';
}, 3000);
}
}
animate() {
requestAnimationFrame(() => this.animate());
const time = this.clock.getElapsedTime();
// Update movement
this.updateMovement();
// Update hover detection
this.updateHover();
// Animate portals
this.portals.forEach((portal, index) => {
if (portal.userData.material) {
portal.userData.material.uniforms.time.value = time;
// Pulsing glow
const intensity = 0.5 + Math.sin(time * 2 + index) * 0.3;
portal.userData.light.intensity = intensity;
}
// Gentle rotation
portal.rotation.z = Math.sin(time * 0.5 + index) * 0.1;
});
// Animate particles
const particles = this.scene.getObjectByProperty('type', 'Points');
if (particles) {
particles.rotation.y = time * 0.05;
particles.position.y = Math.sin(time * 0.2) * 2;
}
this.renderer.render(this.scene, this.camera);
}
}
// Initialize when DOM is loaded
window.addEventListener('DOMContentLoaded', () => {
const navigator = new WorldNavigator();
navigator.init();
});
</script>
</body>
</html>