File size: 5,519 Bytes
d2b44a7 1280b84 bc6a8d1 1280b84 bc6a8d1 cb4742d bc6a8d1 1280b84 f8b0052 cb4742d bc6a8d1 cb4742d bc6a8d1 cb4742d bc6a8d1 cb4742d e82e5a4 cb4742d bc6a8d1 cb4742d bc6a8d1 cb4742d bc6a8d1 cb4742d bc6a8d1 1280b84 | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>3D Map Viewer</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #000;
}
canvas {
display: block;
width: 100%;
height: 100%;
}
#loading {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.8);
color: #fff;
padding: 20px 40px;
border-radius: 8px;
text-align: center;
z-index: 100;
font-size: 18px;
}
</style>
</head>
<body>
<div id="loading">Loading...</div>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.128.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.128.0/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
let scene, camera, renderer, modelLoaded = false;
let rotation = { x: 0, y: 0 };
let zoom = 5;
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x111111);
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = zoom;
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Lighting
scene.add(new THREE.AmbientLight(0xffffff, 1.2));
const dirLight = new THREE.DirectionalLight(0xffffff, 1);
dirLight.position.set(10, 10, 10);
scene.add(dirLight);
// Load map.glb
const loader = new GLTFLoader();
loader.load('./map.glb', (gltf) => {
const model = gltf.scene;
scene.add(model);
modelLoaded = true;
document.getElementById('loading').style.display = 'none';
console.log('✅ Map loaded!');
}, undefined, (error) => {
console.error('Error loading map:', error);
// Fallback: create test cube
const geometry = new THREE.BoxGeometry(2, 2, 2);
const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
modelLoaded = true;
document.getElementById('loading').textContent = 'Test Cube (map.glb not found)';
});
// Mouse/touch controls
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
renderer.domElement.addEventListener('mousedown', (e) => {
isDragging = true;
previousMousePosition = { x: e.clientX, y: e.clientY };
});
renderer.domElement.addEventListener('mousemove', (e) => {
if (isDragging && modelLoaded) {
const deltaX = e.clientX - previousMousePosition.x;
const deltaY = e.clientY - previousMousePosition.y;
rotation.y += deltaX * 0.01;
rotation.x += deltaY * 0.01;
previousMousePosition = { x: e.clientX, y: e.clientY };
}
});
renderer.domElement.addEventListener('mouseup', () => {
isDragging = false;
});
renderer.domElement.addEventListener('wheel', (e) => {
e.preventDefault();
zoom += e.deltaY * 0.01;
zoom = Math.max(1, Math.min(20, zoom));
});
// Touch pinch to zoom
let lastDistance = 0;
renderer.domElement.addEventListener('touchmove', (e) => {
if (e.touches.length === 2) {
e.preventDefault();
const touch1 = e.touches[0];
const touch2 = e.touches[1];
const dx = touch1.clientX - touch2.clientX;
const dy = touch1.clientY - touch2.clientY;
const distance = Math.sqrt(dx * dx + dy * dy);
if (lastDistance > 0) {
const delta = distance - lastDistance;
zoom -= delta * 0.02;
zoom = Math.max(1, Math.min(20, zoom));
}
lastDistance = distance;
} else if (e.touches.length === 1) {
// Single finger drag
const touch = e.touches[0];
const deltaX = touch.clientX - previousMousePosition.x;
const deltaY = touch.clientY - previousMousePosition.y;
rotation.y += deltaX * 0.01;
rotation.x += deltaY * 0.01;
previousMousePosition = { x: touch.clientX, y: touch.clientY };
}
}, false);
renderer.domElement.addEventListener('touchend', () => {
lastDistance = 0;
});
function animate() {
requestAnimationFrame(animate);
if (modelLoaded) {
camera.position.x = zoom * Math.sin(rotation.y);
camera.position.y = zoom * Math.sin(rotation.x);
camera.position.z = zoom * Math.cos(rotation.y);
camera.lookAt(0, 0, 0);
}
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
}
init();
</script>
</body>
</html> |