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 |
+
body {
|
| 7 |
+
margin: 0;
|
| 8 |
+
font-family: Arial, sans-serif;
|
| 9 |
+
background: #1a1a1a;
|
| 10 |
+
color: white;
|
| 11 |
+
text-align: center;
|
| 12 |
+
}
|
| 13 |
+
#header {
|
| 14 |
+
padding: 20px;
|
| 15 |
+
background: #282828;
|
| 16 |
+
border-bottom: 1px solid #444;
|
| 17 |
+
}
|
| 18 |
+
#header h1 {
|
| 19 |
+
margin: 0 0 10px 0;
|
| 20 |
+
font-size: 2em;
|
| 21 |
+
}
|
| 22 |
+
#header p {
|
| 23 |
+
margin: 0 0 20px 0;
|
| 24 |
+
color: #ccc;
|
| 25 |
+
}
|
| 26 |
+
#header a {
|
| 27 |
+
color: #61dafb;
|
| 28 |
+
text-decoration: none;
|
| 29 |
+
}
|
| 30 |
+
#header a:hover {
|
| 31 |
+
text-decoration: underline;
|
| 32 |
+
}
|
| 33 |
+
#examples-container {
|
| 34 |
+
display: flex;
|
| 35 |
+
flex-wrap: wrap;
|
| 36 |
+
justify-content: center;
|
| 37 |
+
padding: 20px;
|
| 38 |
+
gap: 20px;
|
| 39 |
+
background: #222;
|
| 40 |
+
}
|
| 41 |
+
.example-card {
|
| 42 |
+
background: #333;
|
| 43 |
+
border-radius: 8px;
|
| 44 |
+
overflow: hidden;
|
| 45 |
+
width: 200px;
|
| 46 |
+
cursor: pointer;
|
| 47 |
+
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
|
| 48 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
| 49 |
+
}
|
| 50 |
+
.example-card:hover {
|
| 51 |
+
transform: scale(1.05);
|
| 52 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.3);
|
| 53 |
+
}
|
| 54 |
+
.example-card img {
|
| 55 |
+
width: 100%;
|
| 56 |
+
height: 120px;
|
| 57 |
+
object-fit: cover;
|
| 58 |
+
display: block;
|
| 59 |
+
}
|
| 60 |
+
.example-card p {
|
| 61 |
+
margin: 0;
|
| 62 |
+
padding: 15px;
|
| 63 |
+
font-weight: bold;
|
| 64 |
+
}
|
| 65 |
+
#viewer-container {
|
| 66 |
+
position: relative;
|
| 67 |
+
width: 100%;
|
| 68 |
+
height: 65vh;
|
| 69 |
+
}
|
| 70 |
+
canvas {
|
| 71 |
+
display: block;
|
| 72 |
+
width: 100%;
|
| 73 |
+
height: 100%;
|
| 74 |
+
}
|
| 75 |
+
#upload-container {
|
| 76 |
+
margin-top: 15px;
|
| 77 |
+
}
|
| 78 |
+
#file-input {
|
| 79 |
+
display: none;
|
| 80 |
+
}
|
| 81 |
+
.upload-btn {
|
| 82 |
+
background: #4CAF50;
|
| 83 |
+
color: white;
|
| 84 |
+
padding: 10px 15px;
|
| 85 |
+
border: none;
|
| 86 |
+
border-radius: 4px;
|
| 87 |
+
cursor: pointer;
|
| 88 |
+
font-size: 16px;
|
| 89 |
+
}
|
| 90 |
+
.upload-btn:hover {
|
| 91 |
+
background: #45a049;
|
| 92 |
+
}
|
| 93 |
+
#loading {
|
| 94 |
+
display: none; /* Hidden by default */
|
| 95 |
+
padding: 15px;
|
| 96 |
+
}
|
| 97 |
+
#loading-text {
|
| 98 |
+
color: #aaa;
|
| 99 |
+
font-size: 18px;
|
| 100 |
+
margin-bottom: 10px;
|
| 101 |
+
}
|
| 102 |
+
#progress-container {
|
| 103 |
+
width: 80%;
|
| 104 |
+
max-width: 400px;
|
| 105 |
+
margin: 0 auto;
|
| 106 |
+
background-color: #555;
|
| 107 |
+
border-radius: 5px;
|
| 108 |
+
overflow: hidden;
|
| 109 |
+
display: none; /* Hidden by default, shown for web loads */
|
| 110 |
+
}
|
| 111 |
+
#progress-bar {
|
| 112 |
+
width: 0%;
|
| 113 |
+
height: 20px;
|
| 114 |
+
background-color: #4CAF50;
|
| 115 |
+
/* Smoother transition for the bar */
|
| 116 |
+
transition: width 0.2s ease-out;
|
| 117 |
+
}
|
| 118 |
+
#controls {
|
| 119 |
+
position: absolute;
|
| 120 |
+
top: 10px;
|
| 121 |
+
left: 10px;
|
| 122 |
+
z-index: 10;
|
| 123 |
+
}
|
| 124 |
+
.control-btn {
|
| 125 |
+
padding: 8px 12px;
|
| 126 |
+
margin-right: 5px;
|
| 127 |
+
border: none;
|
| 128 |
+
border-radius: 4px;
|
| 129 |
+
cursor: pointer;
|
| 130 |
+
background: rgba(85, 85, 85, 0.8);
|
| 131 |
+
color: white;
|
| 132 |
+
}
|
| 133 |
+
.control-btn:hover {
|
| 134 |
+
background: rgba(102, 102, 102, 0.9);
|
| 135 |
+
}
|
| 136 |
+
#instructions {
|
| 137 |
+
position: absolute;
|
| 138 |
+
bottom: 10px;
|
| 139 |
+
left: 10px;
|
| 140 |
+
color: white;
|
| 141 |
+
background: rgba(0,0,0,0.5);
|
| 142 |
+
padding: 10px;
|
| 143 |
+
border-radius: 5px;
|
| 144 |
+
font-size: 14px;
|
| 145 |
+
z-index: 10;
|
| 146 |
+
}
|
| 147 |
+
</style>
|
| 148 |
+
</head>
|
| 149 |
+
<body>
|
| 150 |
+
<div id="header">
|
| 151 |
+
<h1>Hunyuan World Navigator</h1>
|
| 152 |
+
<p>
|
| 153 |
+
<a href="https://huggingface.co/tencent/HunyuanWorld-1" target="_blank" rel="noopener noreferrer">HunyuanWorld-1 on Hugging Face</a> |
|
| 154 |
+
<a href="https://github.com/camenduru/HunyuanWorld-1.0-jupyter" target="_blank" rel="noopener noreferrer">Generate your own on Google Colab</a>
|
| 155 |
+
</p>
|
| 156 |
+
<p>Click an example below or upload your own files to begin.</p>
|
| 157 |
+
<div id="upload-container">
|
| 158 |
+
<label for="file-input" class="upload-btn">Select Custom PLY/DRC Files</label>
|
| 159 |
+
<input id="file-input" type="file" accept=".ply,.drc" multiple>
|
| 160 |
+
</div>
|
| 161 |
+
</div>
|
| 162 |
+
|
| 163 |
+
<div id="examples-container"></div>
|
| 164 |
+
|
| 165 |
+
<div id="loading">
|
| 166 |
+
<div id="loading-text">Loading...</div>
|
| 167 |
+
<div id="progress-container">
|
| 168 |
+
<div id="progress-bar"></div>
|
| 169 |
+
</div>
|
| 170 |
+
</div>
|
| 171 |
+
|
| 172 |
+
<div id="viewer-container">
|
| 173 |
+
<div id="controls">
|
| 174 |
+
<button id="rotate-toggle" class="control-btn">Pause Rotation</button>
|
| 175 |
+
<button id="reset-view" class="control-btn">Reset View</button>
|
| 176 |
+
</div>
|
| 177 |
+
<div id="instructions">
|
| 178 |
+
Controls: WASD to move, Mouse drag to look around
|
| 179 |
+
</div>
|
| 180 |
+
</div>
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
|
| 184 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/PLYLoader.js"></script>
|
| 185 |
+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/DRACOLoader.js"></script>
|
| 186 |
+
<script>
|
| 187 |
+
// --- CACHE & DATA ---
|
| 188 |
+
const modelCache = new Map();
|
| 189 |
+
const baseURL = 'https://huggingface.co/datasets/multimodalart/HunyuanWorld-panoramas/resolve/main/';
|
| 190 |
+
const examplesData = [
|
| 191 |
+
{ name: 'Cyberpunk City', previewImage: 'cyberpunk/cyberpunk.webp', files: ['cyberpunk/mesh_layer0.ply', 'cyberpunk/mesh_layer1.ply'] },
|
| 192 |
+
{ name: 'European Town', previewImage: 'european/european.webp', files: ['european/mesh_layer0.ply', 'european/mesh_layer1.ply'] },
|
| 193 |
+
{ name: 'Restaurant', previewImage: 'italian/italian.webp', files: ['italian/mesh_layer0.ply', 'italian/mesh_layer1.ply', 'italian/mesh_layer2.ply', 'italian/mesh_layer3.ply'] },
|
| 194 |
+
{ name: 'Mountain', previewImage: 'mountain/mountain.webp', files: ['mountain/mesh_layer0.ply', 'mountain/mesh_layer1.ply'] },
|
| 195 |
+
{ name: 'Windows XP', previewImage: 'wxp/wxp.webp', files: ['wxp/mesh_layer0.ply', 'wxp/mesh_layer1.ply', 'wxp/mesh_layer2.ply'] },
|
| 196 |
+
{ name: 'Zelda', previewImage: 'zld/zld.webp', files: ['zld/mesh_layer0.ply', 'zld/mesh_layer1.ply'] }
|
| 197 |
+
];
|
| 198 |
+
const examples = examplesData.map(ex => ({
|
| 199 |
+
name: ex.name,
|
| 200 |
+
previewImage: baseURL + ex.previewImage,
|
| 201 |
+
files: ex.files.map(file => baseURL + file)
|
| 202 |
+
}));
|
| 203 |
+
|
| 204 |
+
// --- UI & DOM ELEMENTS ---
|
| 205 |
+
const examplesContainer = document.getElementById('examples-container');
|
| 206 |
+
const loadingDiv = document.getElementById('loading');
|
| 207 |
+
const loadingText = document.getElementById('loading-text');
|
| 208 |
+
const progressContainer = document.getElementById('progress-container');
|
| 209 |
+
const progressBar = document.getElementById('progress-bar');
|
| 210 |
+
|
| 211 |
+
// --- UI SETUP ---
|
| 212 |
+
examples.forEach(example => {
|
| 213 |
+
const card = document.createElement('div');
|
| 214 |
+
card.className = 'example-card';
|
| 215 |
+
card.innerHTML = `<img src="${example.previewImage}" alt="${example.name}"><p>${example.name}</p>`;
|
| 216 |
+
card.addEventListener('click', () => loadExample(example));
|
| 217 |
+
examplesContainer.appendChild(card);
|
| 218 |
+
});
|
| 219 |
+
|
| 220 |
+
// --- THREE.JS INITIALIZATION ---
|
| 221 |
+
const viewerContainer = document.getElementById('viewer-container');
|
| 222 |
+
const scene = new THREE.Scene();
|
| 223 |
+
scene.background = new THREE.Color(0x222222);
|
| 224 |
+
const camera = new THREE.PerspectiveCamera(75, viewerContainer.clientWidth / viewerContainer.clientHeight, 0.1, 1000);
|
| 225 |
+
const renderer = new THREE.WebGLRenderer({ antialias: true });
|
| 226 |
+
renderer.setSize(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 227 |
+
viewerContainer.appendChild(renderer.domElement);
|
| 228 |
+
|
| 229 |
+
// --- LOADERS ---
|
| 230 |
+
const plyLoader = new THREE.PLYLoader();
|
| 231 |
+
const dracoLoader = new THREE.DRACOLoader();
|
| 232 |
+
dracoLoader.setDecoderPath('https://cdn.jsdelivr.net/npm/[email protected]/examples/js/libs/draco/');
|
| 233 |
+
|
| 234 |
+
// --- MOVEMENT & CONTROL VARIABLES ---
|
| 235 |
+
const moveSpeed = 0.01;
|
| 236 |
+
const maxDistance = 0.3;
|
| 237 |
+
const keys = { w: false, a: false, s: false, d: false };
|
| 238 |
+
let isMouseDown = false;
|
| 239 |
+
let previousMousePosition = { x: 0, y: 0 };
|
| 240 |
+
let isRotating = false;
|
| 241 |
+
let animationId = null;
|
| 242 |
+
|
| 243 |
+
// --- SCENE HELPER FUNCTIONS ---
|
| 244 |
+
function clearScene() {
|
| 245 |
+
scene.children.slice().forEach(child => {
|
| 246 |
+
if (child instanceof THREE.Mesh) {
|
| 247 |
+
if (child.geometry) child.geometry.dispose();
|
| 248 |
+
if (child.material) child.material.dispose();
|
| 249 |
+
scene.remove(child);
|
| 250 |
+
}
|
| 251 |
+
});
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
function onLoadingComplete() {
|
| 255 |
+
loadingDiv.style.display = 'none';
|
| 256 |
+
positionCamera();
|
| 257 |
+
isRotating = true;
|
| 258 |
+
document.getElementById('rotate-toggle').textContent = 'Pause Rotation';
|
| 259 |
+
if (!animationId) {
|
| 260 |
+
animate();
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
function positionCamera() {
|
| 265 |
+
scene.rotation.y = 0;
|
| 266 |
+
camera.position.set(0, 0, 0);
|
| 267 |
+
camera.quaternion.set(0, 0, 0, 1);
|
| 268 |
+
camera.lookAt(0, 0, -10);
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
// --- LOADING LOGIC ---
|
| 272 |
+
async function fetchWithProgress(url, onProgress) {
|
| 273 |
+
const response = await fetch(url);
|
| 274 |
+
if (!response.ok) throw new Error(`HTTP error! status: ${response.status} for ${url}`);
|
| 275 |
+
if (!response.body) throw new Error('Response body is null');
|
| 276 |
+
const reader = response.body.getReader();
|
| 277 |
+
const chunks = [];
|
| 278 |
+
while (true) {
|
| 279 |
+
const { done, value } = await reader.read();
|
| 280 |
+
if (done) break;
|
| 281 |
+
chunks.push(value);
|
| 282 |
+
onProgress(value.length);
|
| 283 |
+
}
|
| 284 |
+
let totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
| 285 |
+
const buffer = new Uint8Array(totalLength);
|
| 286 |
+
let offset = 0;
|
| 287 |
+
chunks.forEach(chunk => {
|
| 288 |
+
buffer.set(chunk, offset);
|
| 289 |
+
offset += chunk.length;
|
| 290 |
+
});
|
| 291 |
+
return buffer.buffer;
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
async function loadExample(example) {
|
| 295 |
+
clearScene();
|
| 296 |
+
loadingDiv.style.display = 'block';
|
| 297 |
+
|
| 298 |
+
if (modelCache.has(example.name)) {
|
| 299 |
+
loadingText.textContent = 'Loading from cache...';
|
| 300 |
+
progressContainer.style.display = 'none';
|
| 301 |
+
const cachedMeshes = modelCache.get(example.name);
|
| 302 |
+
cachedMeshes.forEach(mesh => scene.add(mesh.clone()));
|
| 303 |
+
setTimeout(onLoadingComplete, 50);
|
| 304 |
+
return;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
progressContainer.style.display = 'block';
|
| 308 |
+
progressBar.style.width = '0%';
|
| 309 |
+
loadingText.textContent = 'Calculating size...';
|
| 310 |
+
|
| 311 |
+
let loadedSize = 0;
|
| 312 |
+
let totalSize = 0;
|
| 313 |
+
let progressAnimationId = null;
|
| 314 |
+
|
| 315 |
+
try {
|
| 316 |
+
const headPromises = example.files.map(url => fetch(url, { method: 'HEAD' }));
|
| 317 |
+
const responses = await Promise.all(headPromises);
|
| 318 |
+
totalSize = responses.reduce((acc, res) => acc + Number(res.headers.get('Content-Length') || 0), 0);
|
| 319 |
+
|
| 320 |
+
const updateProgressUI = () => {
|
| 321 |
+
const percent = totalSize > 0 ? (loadedSize / totalSize) * 100 : 0;
|
| 322 |
+
progressBar.style.width = `${percent}%`;
|
| 323 |
+
loadingText.textContent = `Downloading... ${Math.round(percent)}%`;
|
| 324 |
+
if (loadedSize < totalSize) {
|
| 325 |
+
progressAnimationId = requestAnimationFrame(updateProgressUI);
|
| 326 |
+
}
|
| 327 |
+
};
|
| 328 |
+
progressAnimationId = requestAnimationFrame(updateProgressUI);
|
| 329 |
+
|
| 330 |
+
const onProgress = (chunkSize) => { loadedSize += chunkSize; };
|
| 331 |
+
const contentPromises = example.files.map(url => fetchWithProgress(url, onProgress));
|
| 332 |
+
const buffers = await Promise.all(contentPromises);
|
| 333 |
+
|
| 334 |
+
cancelAnimationFrame(progressAnimationId);
|
| 335 |
+
loadingText.textContent = `Processing files...`;
|
| 336 |
+
|
| 337 |
+
const newMeshes = [];
|
| 338 |
+
buffers.forEach(buffer => {
|
| 339 |
+
const geometry = plyLoader.parse(buffer);
|
| 340 |
+
const material = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, vertexColors: true });
|
| 341 |
+
const mesh = new THREE.Mesh(geometry, material);
|
| 342 |
+
mesh.rotateX(-Math.PI / 2);
|
| 343 |
+
mesh.rotateZ(-Math.PI / 2);
|
| 344 |
+
scene.add(mesh);
|
| 345 |
+
newMeshes.push(mesh);
|
| 346 |
+
});
|
| 347 |
+
|
| 348 |
+
modelCache.set(example.name, newMeshes);
|
| 349 |
+
onLoadingComplete();
|
| 350 |
+
|
| 351 |
+
} catch (error) {
|
| 352 |
+
console.error('Error loading example:', error);
|
| 353 |
+
alert('Failed to load example files. Check console for details.');
|
| 354 |
+
if (progressAnimationId) cancelAnimationFrame(progressAnimationId);
|
| 355 |
+
loadingDiv.style.display = 'none';
|
| 356 |
+
}
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
document.getElementById('file-input').addEventListener('change', function(e) {
|
| 360 |
+
const files = e.target.files;
|
| 361 |
+
if (files.length === 0) return;
|
| 362 |
+
loadingDiv.style.display = 'block';
|
| 363 |
+
loadingText.textContent = 'Loading...';
|
| 364 |
+
progressContainer.style.display = 'none';
|
| 365 |
+
clearScene();
|
| 366 |
+
let loadedCount = 0;
|
| 367 |
+
const totalFiles = files.length;
|
| 368 |
+
Array.from(files).forEach(file => {
|
| 369 |
+
const reader = new FileReader();
|
| 370 |
+
reader.onload = function(event) {
|
| 371 |
+
try {
|
| 372 |
+
const buffer = event.target.result;
|
| 373 |
+
let geometry;
|
| 374 |
+
if (file.name.endsWith('.ply')) {
|
| 375 |
+
geometry = plyLoader.parse(buffer);
|
| 376 |
+
} else if (file.name.endsWith('.drc')) {
|
| 377 |
+
dracoLoader.parse(buffer, (decodedGeometry) => {
|
| 378 |
+
geometry = decodedGeometry;
|
| 379 |
+
if (!geometry.attributes.normal) geometry.computeVertexNormals();
|
| 380 |
+
const material = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, vertexColors: true });
|
| 381 |
+
const mesh = new THREE.Mesh(geometry, material);
|
| 382 |
+
mesh.rotateX(-Math.PI / 2);
|
| 383 |
+
mesh.rotateZ(-Math.PI / 2);
|
| 384 |
+
scene.add(mesh);
|
| 385 |
+
loadedCount++;
|
| 386 |
+
if (loadedCount === totalFiles) onLoadingComplete();
|
| 387 |
+
});
|
| 388 |
+
return;
|
| 389 |
+
}
|
| 390 |
+
if (geometry) {
|
| 391 |
+
const material = new THREE.MeshBasicMaterial({ side: THREE.DoubleSide, vertexColors: true });
|
| 392 |
+
const mesh = new THREE.Mesh(geometry, material);
|
| 393 |
+
mesh.rotateX(-Math.PI / 2);
|
| 394 |
+
mesh.rotateZ(-Math.PI / 2);
|
| 395 |
+
scene.add(mesh);
|
| 396 |
+
}
|
| 397 |
+
} catch (error) {
|
| 398 |
+
console.error('Error loading file:', file.name, error);
|
| 399 |
+
}
|
| 400 |
+
loadedCount++;
|
| 401 |
+
if (loadedCount === totalFiles) onLoadingComplete();
|
| 402 |
+
};
|
| 403 |
+
reader.readAsArrayBuffer(file);
|
| 404 |
+
});
|
| 405 |
+
});
|
| 406 |
+
|
| 407 |
+
// --- CONTROLS & EVENT LISTENERS ---
|
| 408 |
+
document.getElementById('rotate-toggle').addEventListener('click', function() {
|
| 409 |
+
isRotating = !isRotating;
|
| 410 |
+
this.textContent = isRotating ? 'Pause Rotation' : 'Start Rotation';
|
| 411 |
+
});
|
| 412 |
+
document.getElementById('reset-view').addEventListener('click', () => {
|
| 413 |
+
positionCamera();
|
| 414 |
+
if (!animationId) animate();
|
| 415 |
+
});
|
| 416 |
+
document.addEventListener('keydown', (event) => {
|
| 417 |
+
if (event.key.toLowerCase() in keys) {
|
| 418 |
+
keys[event.key.toLowerCase()] = true;
|
| 419 |
+
// BUG FIX: Ensure the animation loop is running when a key is pressed.
|
| 420 |
+
if (!animationId) {
|
| 421 |
+
animate();
|
| 422 |
+
}
|
| 423 |
+
}
|
| 424 |
+
});
|
| 425 |
+
document.addEventListener('keyup', (event) => {
|
| 426 |
+
if (event.key.toLowerCase() in keys) keys[event.key.toLowerCase()] = false;
|
| 427 |
+
});
|
| 428 |
+
renderer.domElement.addEventListener('mousedown', (event) => {
|
| 429 |
+
isMouseDown = true;
|
| 430 |
+
previousMousePosition = { x: event.clientX, y: event.clientY };
|
| 431 |
+
event.preventDefault();
|
| 432 |
+
});
|
| 433 |
+
document.addEventListener('mouseup', () => { isMouseDown = false; });
|
| 434 |
+
document.addEventListener('mousemove', (event) => {
|
| 435 |
+
if (isMouseDown) {
|
| 436 |
+
const deltaMove = { x: event.clientX - previousMousePosition.x, y: event.clientY - previousMousePosition.y };
|
| 437 |
+
const up = new THREE.Vector3(0, 1, 0);
|
| 438 |
+
const right = new THREE.Vector3(1, 0, 0);
|
| 439 |
+
camera.rotateOnWorldAxis(up, -deltaMove.x * 0.002);
|
| 440 |
+
camera.rotateOnAxis(right, -deltaMove.y * 0.002);
|
| 441 |
+
previousMousePosition = { x: event.clientX, y: event.clientY };
|
| 442 |
+
}
|
| 443 |
+
});
|
| 444 |
+
renderer.domElement.addEventListener('contextmenu', (event) => event.preventDefault());
|
| 445 |
+
window.addEventListener('resize', function() {
|
| 446 |
+
camera.aspect = viewerContainer.clientWidth / viewerContainer.clientHeight;
|
| 447 |
+
camera.updateProjectionMatrix();
|
| 448 |
+
renderer.setSize(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 449 |
+
});
|
| 450 |
+
|
| 451 |
+
// --- ANIMATION LOOP ---
|
| 452 |
+
function animate() {
|
| 453 |
+
// Process movement
|
| 454 |
+
if (keys.w || keys.a || keys.s || keys.d) {
|
| 455 |
+
const forward = new THREE.Vector3(0, 0, -1).applyQuaternion(camera.quaternion);
|
| 456 |
+
const right = new THREE.Vector3(1, 0, 0).applyQuaternion(camera.quaternion);
|
| 457 |
+
forward.y = 0; right.y = 0;
|
| 458 |
+
forward.normalize(); right.normalize();
|
| 459 |
+
const movement = new THREE.Vector3();
|
| 460 |
+
if (keys.w) movement.add(forward);
|
| 461 |
+
if (keys.s) movement.sub(forward);
|
| 462 |
+
if (keys.a) movement.sub(right);
|
| 463 |
+
if (keys.d) movement.add(right);
|
| 464 |
+
if (movement.length() > 0) {
|
| 465 |
+
movement.normalize().multiplyScalar(moveSpeed);
|
| 466 |
+
camera.position.add(movement);
|
| 467 |
+
}
|
| 468 |
+
}
|
| 469 |
+
if (camera.position.length() > maxDistance) camera.position.setLength(maxDistance);
|
| 470 |
+
|
| 471 |
+
// Process rotation
|
| 472 |
+
if (isRotating && scene.children.some(c => c instanceof THREE.Mesh)) scene.rotation.y += 0.0005;
|
| 473 |
+
|
| 474 |
+
// Render the scene
|
| 475 |
+
renderer.render(scene, camera);
|
| 476 |
+
|
| 477 |
+
// BUG FIX: Request the next frame at the *end* of the function for robustness.
|
| 478 |
+
animationId = requestAnimationFrame(animate);
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
// Start the initial animation loop
|
| 482 |
+
animate();
|
| 483 |
+
</script>
|
| 484 |
+
</body>
|
| 485 |
+
</html>
|
style.css
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
body {
|
| 2 |
+
padding: 2rem;
|
| 3 |
+
font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
h1 {
|
| 7 |
+
font-size: 16px;
|
| 8 |
+
margin-top: 0;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
p {
|
| 12 |
+
color: rgb(107, 114, 128);
|
| 13 |
+
font-size: 15px;
|
| 14 |
+
margin-bottom: 10px;
|
| 15 |
+
margin-top: 5px;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
.card {
|
| 19 |
+
max-width: 620px;
|
| 20 |
+
margin: 0 auto;
|
| 21 |
+
padding: 16px;
|
| 22 |
+
border: 1px solid lightgray;
|
| 23 |
+
border-radius: 16px;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
.card p:last-child {
|
| 27 |
+
margin-bottom: 0;
|
| 28 |
+
}
|