Spaces:
Running
Running
Update js_scripts/index.js
Browse files- js_scripts/index.js +248 -252
js_scripts/index.js
CHANGED
|
@@ -24,16 +24,14 @@
|
|
| 24 |
document.head.appendChild(linkEl);
|
| 25 |
}
|
| 26 |
|
| 27 |
-
// --- Outer scope variables for viewer state ---
|
| 28 |
-
let SPLAT = null;
|
| 29 |
-
let viewerActive = false;
|
| 30 |
-
|
| 31 |
// Generate a unique identifier for this widget instance.
|
| 32 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
| 33 |
|
| 34 |
// Read configuration values from the JSON file.
|
| 35 |
const gifUrl = config.gif_url;
|
| 36 |
const plyUrl = config.ply_url;
|
|
|
|
|
|
|
| 37 |
const minZoom = parseFloat(config.minZoom || "0");
|
| 38 |
const maxZoom = parseFloat(config.maxZoom || "20");
|
| 39 |
const minAngle = parseFloat(config.minAngle || "0");
|
|
@@ -147,6 +145,16 @@
|
|
| 147 |
`;
|
| 148 |
}
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
// If a gif_url is provided, set the preview image.
|
| 151 |
// Otherwise, hide the preview container, show the viewer immediately,
|
| 152 |
// and hide the "close" button since there's no preview to return to.
|
|
@@ -158,22 +166,23 @@
|
|
| 158 |
gifPreview.style.display = 'none';
|
| 159 |
viewerContainer.style.display = 'block';
|
| 160 |
closeBtn.style.display = 'none';
|
| 161 |
-
// Need to load the PLY without a GIF preview
|
| 162 |
setTimeout(() => {
|
| 163 |
-
|
| 164 |
}, 100);
|
| 165 |
}
|
| 166 |
|
| 167 |
// --- Button Event Handlers ---
|
| 168 |
if (gifUrl) {
|
|
|
|
| 169 |
gifPreview.addEventListener('click', function() {
|
| 170 |
console.log("GIF preview clicked, showing 3D viewer");
|
| 171 |
gifPreview.style.display = 'none';
|
| 172 |
viewerContainer.style.display = 'block';
|
| 173 |
-
|
| 174 |
});
|
| 175 |
}
|
| 176 |
|
|
|
|
| 177 |
closeBtn.addEventListener('click', function() {
|
| 178 |
console.log("Close button clicked");
|
| 179 |
|
|
@@ -183,28 +192,33 @@
|
|
| 183 |
document.exitFullscreen();
|
| 184 |
}
|
| 185 |
}
|
|
|
|
| 186 |
if (widgetContainer.classList.contains('fake-fullscreen')) {
|
| 187 |
widgetContainer.classList.remove('fake-fullscreen');
|
| 188 |
fullscreenToggle.textContent = '⇱';
|
| 189 |
}
|
| 190 |
|
| 191 |
-
//
|
| 192 |
-
|
| 193 |
|
| 194 |
-
//
|
| 195 |
viewerContainer.style.display = 'none';
|
| 196 |
gifPreview.style.display = 'block';
|
| 197 |
});
|
| 198 |
|
|
|
|
| 199 |
fullscreenToggle.addEventListener('click', function() {
|
| 200 |
if (isIOS) {
|
|
|
|
| 201 |
if (!widgetContainer.classList.contains('fake-fullscreen')) {
|
| 202 |
widgetContainer.classList.add('fake-fullscreen');
|
| 203 |
} else {
|
| 204 |
widgetContainer.classList.remove('fake-fullscreen');
|
|
|
|
| 205 |
}
|
| 206 |
fullscreenToggle.textContent = widgetContainer.classList.contains('fake-fullscreen') ? '⇲' : '⇱';
|
| 207 |
} else {
|
|
|
|
| 208 |
if (!document.fullscreenElement) {
|
| 209 |
if (widgetContainer.requestFullscreen) {
|
| 210 |
widgetContainer.requestFullscreen();
|
|
@@ -229,25 +243,28 @@
|
|
| 229 |
fullscreenToggle.textContent = '⇲';
|
| 230 |
widgetContainer.style.height = '100%';
|
| 231 |
widgetContainer.style.paddingBottom = '0';
|
|
|
|
| 232 |
} else {
|
| 233 |
fullscreenToggle.textContent = '⇱';
|
| 234 |
widgetContainer.style.height = '0';
|
| 235 |
widgetContainer.style.paddingBottom = aspectPercent;
|
|
|
|
| 236 |
}
|
| 237 |
});
|
| 238 |
|
|
|
|
| 239 |
helpToggle.addEventListener('click', function(e) {
|
| 240 |
e.stopPropagation();
|
| 241 |
menuContent.style.display = (menuContent.style.display === 'block') ? 'none' : 'block';
|
| 242 |
});
|
| 243 |
|
|
|
|
| 244 |
resetCameraBtn.addEventListener('click', function() {
|
| 245 |
-
console.log("Reset camera button clicked
|
| 246 |
-
|
| 247 |
-
window.viewer.resetCamera();
|
| 248 |
-
}
|
| 249 |
});
|
| 250 |
|
|
|
|
| 251 |
document.addEventListener('keydown', function(e) {
|
| 252 |
if (e.key === 'Escape' || e.key === 'Esc') {
|
| 253 |
let wasFullscreen = false;
|
|
@@ -262,261 +279,106 @@
|
|
| 262 |
widgetContainer.classList.remove('fake-fullscreen');
|
| 263 |
fullscreenToggle.textContent = '⇱';
|
| 264 |
}
|
|
|
|
|
|
|
|
|
|
| 265 |
}
|
| 266 |
});
|
| 267 |
|
| 268 |
-
// ---
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
| 273 |
return;
|
| 274 |
}
|
| 275 |
|
| 276 |
-
console.log("Initializing PLY viewer...");
|
| 277 |
-
progressDialog.style.display = 'block';
|
| 278 |
-
progressIndicator.value = 0;
|
| 279 |
-
|
| 280 |
try {
|
| 281 |
-
//
|
| 282 |
-
if (
|
| 283 |
-
|
| 284 |
}
|
| 285 |
|
| 286 |
-
//
|
| 287 |
-
|
| 288 |
-
// Properties
|
| 289 |
-
renderer: null,
|
| 290 |
-
scene: null,
|
| 291 |
-
camera: null,
|
| 292 |
-
controls: null,
|
| 293 |
-
animFrameId: null,
|
| 294 |
-
resizeObserver: null,
|
| 295 |
-
initialCameraPosition: null,
|
| 296 |
-
initialCameraRotation: null,
|
| 297 |
-
|
| 298 |
-
// Initialize the viewer
|
| 299 |
-
async init() {
|
| 300 |
-
// Create renderer and scene
|
| 301 |
-
this.renderer = new SPLAT.WebGLRenderer(canvas);
|
| 302 |
-
this.scene = new SPLAT.Scene();
|
| 303 |
-
|
| 304 |
-
// Create camera
|
| 305 |
-
this.camera = new SPLAT.Camera();
|
| 306 |
-
|
| 307 |
-
// Store initial camera state
|
| 308 |
-
this.initialCameraPosition = this.camera.position.clone();
|
| 309 |
-
this.initialCameraRotation = this.camera.rotation.clone();
|
| 310 |
-
|
| 311 |
-
// Create controls
|
| 312 |
-
this.controls = new SPLAT.OrbitControls(
|
| 313 |
-
this.camera,
|
| 314 |
-
canvas,
|
| 315 |
-
0.5,
|
| 316 |
-
0.5,
|
| 317 |
-
5,
|
| 318 |
-
true,
|
| 319 |
-
new SPLAT.Vector3(),
|
| 320 |
-
chosenInitAlpha,
|
| 321 |
-
chosenInitBeta,
|
| 322 |
-
chosenInitRadius
|
| 323 |
-
);
|
| 324 |
-
|
| 325 |
-
// Set control constraints
|
| 326 |
-
this.controls.maxZoom = maxZoom;
|
| 327 |
-
this.controls.minZoom = minZoom;
|
| 328 |
-
this.controls.minAngle = minAngle;
|
| 329 |
-
this.controls.maxAngle = maxAngle;
|
| 330 |
-
this.controls.minAzimuth = minAzimuth;
|
| 331 |
-
this.controls.maxAzimuth = maxAzimuth;
|
| 332 |
-
this.controls.panSpeed = isMobile ? 0.5 : 1.2;
|
| 333 |
-
|
| 334 |
-
// Set canvas background
|
| 335 |
-
canvas.style.background = config.canvas_background || "#FEFEFD";
|
| 336 |
-
|
| 337 |
-
// Handle resize
|
| 338 |
-
this.handleResize();
|
| 339 |
-
|
| 340 |
-
// Set up resize observer
|
| 341 |
-
this.resizeObserver = new ResizeObserver(() => {
|
| 342 |
-
this.handleResize();
|
| 343 |
-
});
|
| 344 |
-
this.resizeObserver.observe(canvas);
|
| 345 |
-
|
| 346 |
-
// Update controls
|
| 347 |
-
this.controls.update();
|
| 348 |
-
|
| 349 |
-
// Load the PLY file
|
| 350 |
-
try {
|
| 351 |
-
await SPLAT.PLYLoader.LoadAsync(
|
| 352 |
-
plyUrl,
|
| 353 |
-
this.scene,
|
| 354 |
-
(progress) => {
|
| 355 |
-
progressIndicator.value = progress * 100;
|
| 356 |
-
}
|
| 357 |
-
);
|
| 358 |
-
progressDialog.style.display = 'none';
|
| 359 |
-
} catch (error) {
|
| 360 |
-
console.error("Error loading PLY file:", error);
|
| 361 |
-
progressDialog.innerHTML = `<p style="color: red">Error loading model: ${error.message}</p>`;
|
| 362 |
-
return false;
|
| 363 |
-
}
|
| 364 |
-
|
| 365 |
-
// Start rendering
|
| 366 |
-
this.startRenderLoop();
|
| 367 |
-
return true;
|
| 368 |
-
},
|
| 369 |
-
|
| 370 |
-
// Handle resize
|
| 371 |
-
handleResize() {
|
| 372 |
-
if (this.renderer) {
|
| 373 |
-
this.renderer.setSize(canvas.clientWidth, canvas.clientHeight);
|
| 374 |
-
}
|
| 375 |
-
},
|
| 376 |
-
|
| 377 |
-
// Start the render loop
|
| 378 |
-
startRenderLoop() {
|
| 379 |
-
// Animation frame loop
|
| 380 |
-
const renderFrame = () => {
|
| 381 |
-
if (this.controls && this.scene && this.camera && this.renderer) {
|
| 382 |
-
this.controls.update();
|
| 383 |
-
this.renderer.render(this.scene, this.camera);
|
| 384 |
-
this.animFrameId = requestAnimationFrame(renderFrame);
|
| 385 |
-
}
|
| 386 |
-
};
|
| 387 |
-
|
| 388 |
-
this.animFrameId = requestAnimationFrame(renderFrame);
|
| 389 |
-
},
|
| 390 |
-
|
| 391 |
-
// Reset camera to initial position
|
| 392 |
-
resetCamera() {
|
| 393 |
-
if (!this.camera || !this.initialCameraPosition || !this.initialCameraRotation) {
|
| 394 |
-
console.log("Cannot reset camera - missing camera instance or initial positions");
|
| 395 |
-
return;
|
| 396 |
-
}
|
| 397 |
-
|
| 398 |
-
try {
|
| 399 |
-
// Dispose previous controls if they exist
|
| 400 |
-
if (this.controls && typeof this.controls.dispose === 'function') {
|
| 401 |
-
this.controls.dispose();
|
| 402 |
-
}
|
| 403 |
-
|
| 404 |
-
// Set camera back to initial position
|
| 405 |
-
this.camera.position = this.initialCameraPosition.clone();
|
| 406 |
-
this.camera.rotation = this.initialCameraRotation.clone();
|
| 407 |
-
|
| 408 |
-
// Create new controls
|
| 409 |
-
this.controls = new SPLAT.OrbitControls(
|
| 410 |
-
this.camera,
|
| 411 |
-
canvas,
|
| 412 |
-
0.5,
|
| 413 |
-
0.5,
|
| 414 |
-
5,
|
| 415 |
-
true,
|
| 416 |
-
new SPLAT.Vector3(),
|
| 417 |
-
chosenInitAlpha,
|
| 418 |
-
chosenInitBeta,
|
| 419 |
-
chosenInitRadius
|
| 420 |
-
);
|
| 421 |
-
|
| 422 |
-
// Set control constraints
|
| 423 |
-
this.controls.maxZoom = maxZoom;
|
| 424 |
-
this.controls.minZoom = minZoom;
|
| 425 |
-
this.controls.minAngle = minAngle;
|
| 426 |
-
this.controls.maxAngle = maxAngle;
|
| 427 |
-
this.controls.minAzimuth = minAzimuth;
|
| 428 |
-
this.controls.maxAzimuth = maxAzimuth;
|
| 429 |
-
this.controls.panSpeed = isMobile ? 0.5 : 1.2;
|
| 430 |
-
|
| 431 |
-
// Update controls
|
| 432 |
-
this.controls.update();
|
| 433 |
-
|
| 434 |
-
console.log("Camera reset successful");
|
| 435 |
-
} catch (error) {
|
| 436 |
-
console.error("Error resetting camera:", error);
|
| 437 |
-
}
|
| 438 |
-
},
|
| 439 |
-
|
| 440 |
-
// Clean up resources
|
| 441 |
-
dispose() {
|
| 442 |
-
// Cancel animation frame
|
| 443 |
-
if (this.animFrameId) {
|
| 444 |
-
cancelAnimationFrame(this.animFrameId);
|
| 445 |
-
this.animFrameId = null;
|
| 446 |
-
}
|
| 447 |
-
|
| 448 |
-
// Remove resize observer
|
| 449 |
-
if (this.resizeObserver) {
|
| 450 |
-
this.resizeObserver.disconnect();
|
| 451 |
-
this.resizeObserver = null;
|
| 452 |
-
}
|
| 453 |
-
|
| 454 |
-
// Dispose controls
|
| 455 |
-
if (this.controls && typeof this.controls.dispose === 'function') {
|
| 456 |
-
try {
|
| 457 |
-
this.controls.dispose();
|
| 458 |
-
} catch (e) {
|
| 459 |
-
console.warn("Error disposing controls:", e);
|
| 460 |
-
}
|
| 461 |
-
this.controls = null;
|
| 462 |
-
}
|
| 463 |
-
|
| 464 |
-
// Dispose renderer
|
| 465 |
-
if (this.renderer && typeof this.renderer.dispose === 'function') {
|
| 466 |
-
try {
|
| 467 |
-
this.renderer.dispose();
|
| 468 |
-
} catch (e) {
|
| 469 |
-
console.warn("Error disposing renderer:", e);
|
| 470 |
-
}
|
| 471 |
-
this.renderer = null;
|
| 472 |
-
}
|
| 473 |
-
|
| 474 |
-
// Clear scene
|
| 475 |
-
if (this.scene) {
|
| 476 |
-
this.scene = null;
|
| 477 |
-
}
|
| 478 |
-
|
| 479 |
-
// Clear camera
|
| 480 |
-
this.camera = null;
|
| 481 |
-
this.initialCameraPosition = null;
|
| 482 |
-
this.initialCameraRotation = null;
|
| 483 |
-
|
| 484 |
-
console.log("Viewer resources disposed");
|
| 485 |
-
}
|
| 486 |
-
};
|
| 487 |
|
| 488 |
-
//
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
|
| 498 |
} catch (error) {
|
| 499 |
-
console.error("Error
|
| 500 |
-
progressDialog.innerHTML = `<p style="color: red">Error initializing viewer: ${error.message}</p>`;
|
| 501 |
-
viewerActive = false;
|
| 502 |
}
|
| 503 |
}
|
| 504 |
|
| 505 |
-
//
|
| 506 |
-
function
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
}
|
| 511 |
|
| 512 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
|
| 514 |
-
// Dispose
|
| 515 |
-
if (
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
}
|
| 518 |
|
| 519 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
if (canvas) {
|
| 521 |
const ctx = canvas.getContext('webgl') || canvas.getContext('webgl2');
|
| 522 |
if (ctx && ctx.getExtension('WEBGL_lose_context')) {
|
|
@@ -528,9 +390,143 @@
|
|
| 528 |
}
|
| 529 |
}
|
| 530 |
|
| 531 |
-
// Mark viewer as not
|
| 532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
|
| 534 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
}
|
| 536 |
})();
|
|
|
|
| 24 |
document.head.appendChild(linkEl);
|
| 25 |
}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
// Generate a unique identifier for this widget instance.
|
| 28 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
| 29 |
|
| 30 |
// Read configuration values from the JSON file.
|
| 31 |
const gifUrl = config.gif_url;
|
| 32 |
const plyUrl = config.ply_url;
|
| 33 |
+
|
| 34 |
+
// Camera constraint parameters
|
| 35 |
const minZoom = parseFloat(config.minZoom || "0");
|
| 36 |
const maxZoom = parseFloat(config.maxZoom || "20");
|
| 37 |
const minAngle = parseFloat(config.minAngle || "0");
|
|
|
|
| 145 |
`;
|
| 146 |
}
|
| 147 |
|
| 148 |
+
// Global variables to track state
|
| 149 |
+
let SPLAT = null;
|
| 150 |
+
let cameraInstance = null;
|
| 151 |
+
let controlsInstance = null;
|
| 152 |
+
let rendererInstance = null;
|
| 153 |
+
let sceneInstance = null;
|
| 154 |
+
let animFrameId = null;
|
| 155 |
+
let isViewerInitialized = false;
|
| 156 |
+
let resizeHandler = null;
|
| 157 |
+
|
| 158 |
// If a gif_url is provided, set the preview image.
|
| 159 |
// Otherwise, hide the preview container, show the viewer immediately,
|
| 160 |
// and hide the "close" button since there's no preview to return to.
|
|
|
|
| 166 |
gifPreview.style.display = 'none';
|
| 167 |
viewerContainer.style.display = 'block';
|
| 168 |
closeBtn.style.display = 'none';
|
|
|
|
| 169 |
setTimeout(() => {
|
| 170 |
+
initializeViewer();
|
| 171 |
}, 100);
|
| 172 |
}
|
| 173 |
|
| 174 |
// --- Button Event Handlers ---
|
| 175 |
if (gifUrl) {
|
| 176 |
+
// When GIF is clicked, hide it and initialize the viewer
|
| 177 |
gifPreview.addEventListener('click', function() {
|
| 178 |
console.log("GIF preview clicked, showing 3D viewer");
|
| 179 |
gifPreview.style.display = 'none';
|
| 180 |
viewerContainer.style.display = 'block';
|
| 181 |
+
initializeViewer();
|
| 182 |
});
|
| 183 |
}
|
| 184 |
|
| 185 |
+
// Close button handler - hide viewer and show GIF
|
| 186 |
closeBtn.addEventListener('click', function() {
|
| 187 |
console.log("Close button clicked");
|
| 188 |
|
|
|
|
| 192 |
document.exitFullscreen();
|
| 193 |
}
|
| 194 |
}
|
| 195 |
+
|
| 196 |
if (widgetContainer.classList.contains('fake-fullscreen')) {
|
| 197 |
widgetContainer.classList.remove('fake-fullscreen');
|
| 198 |
fullscreenToggle.textContent = '⇱';
|
| 199 |
}
|
| 200 |
|
| 201 |
+
// Clean up the viewer resources
|
| 202 |
+
cleanupViewer();
|
| 203 |
|
| 204 |
+
// Hide viewer and show GIF
|
| 205 |
viewerContainer.style.display = 'none';
|
| 206 |
gifPreview.style.display = 'block';
|
| 207 |
});
|
| 208 |
|
| 209 |
+
// Fullscreen toggle handler
|
| 210 |
fullscreenToggle.addEventListener('click', function() {
|
| 211 |
if (isIOS) {
|
| 212 |
+
// iOS doesn't support proper fullscreen, so we use CSS
|
| 213 |
if (!widgetContainer.classList.contains('fake-fullscreen')) {
|
| 214 |
widgetContainer.classList.add('fake-fullscreen');
|
| 215 |
} else {
|
| 216 |
widgetContainer.classList.remove('fake-fullscreen');
|
| 217 |
+
resetCamera();
|
| 218 |
}
|
| 219 |
fullscreenToggle.textContent = widgetContainer.classList.contains('fake-fullscreen') ? '⇲' : '⇱';
|
| 220 |
} else {
|
| 221 |
+
// Standard fullscreen API for other browsers
|
| 222 |
if (!document.fullscreenElement) {
|
| 223 |
if (widgetContainer.requestFullscreen) {
|
| 224 |
widgetContainer.requestFullscreen();
|
|
|
|
| 243 |
fullscreenToggle.textContent = '⇲';
|
| 244 |
widgetContainer.style.height = '100%';
|
| 245 |
widgetContainer.style.paddingBottom = '0';
|
| 246 |
+
resetCamera();
|
| 247 |
} else {
|
| 248 |
fullscreenToggle.textContent = '⇱';
|
| 249 |
widgetContainer.style.height = '0';
|
| 250 |
widgetContainer.style.paddingBottom = aspectPercent;
|
| 251 |
+
resetCamera();
|
| 252 |
}
|
| 253 |
});
|
| 254 |
|
| 255 |
+
// Help toggle button
|
| 256 |
helpToggle.addEventListener('click', function(e) {
|
| 257 |
e.stopPropagation();
|
| 258 |
menuContent.style.display = (menuContent.style.display === 'block') ? 'none' : 'block';
|
| 259 |
});
|
| 260 |
|
| 261 |
+
// Reset camera button
|
| 262 |
resetCameraBtn.addEventListener('click', function() {
|
| 263 |
+
console.log("Reset camera button clicked");
|
| 264 |
+
resetCamera();
|
|
|
|
|
|
|
| 265 |
});
|
| 266 |
|
| 267 |
+
// Handle Escape key for fullscreen exit
|
| 268 |
document.addEventListener('keydown', function(e) {
|
| 269 |
if (e.key === 'Escape' || e.key === 'Esc') {
|
| 270 |
let wasFullscreen = false;
|
|
|
|
| 279 |
widgetContainer.classList.remove('fake-fullscreen');
|
| 280 |
fullscreenToggle.textContent = '⇱';
|
| 281 |
}
|
| 282 |
+
if (wasFullscreen) {
|
| 283 |
+
resetCamera();
|
| 284 |
+
}
|
| 285 |
}
|
| 286 |
});
|
| 287 |
|
| 288 |
+
// --- Camera Reset Function ---
|
| 289 |
+
function resetCamera() {
|
| 290 |
+
console.log("Resetting camera to initial position");
|
| 291 |
+
|
| 292 |
+
if (!SPLAT || !cameraInstance || !controlsInstance || !sceneInstance) {
|
| 293 |
+
console.log("Cannot reset camera - SPLAT not loaded or camera/controls not initialized");
|
| 294 |
return;
|
| 295 |
}
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
try {
|
| 298 |
+
// Dispose the current controls
|
| 299 |
+
if (controlsInstance && typeof controlsInstance.dispose === 'function') {
|
| 300 |
+
controlsInstance.dispose();
|
| 301 |
}
|
| 302 |
|
| 303 |
+
// Reset camera to default position
|
| 304 |
+
cameraInstance = new SPLAT.Camera();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
+
// Create new controls
|
| 307 |
+
controlsInstance = new SPLAT.OrbitControls(
|
| 308 |
+
cameraInstance,
|
| 309 |
+
canvas,
|
| 310 |
+
0.5,
|
| 311 |
+
0.5,
|
| 312 |
+
5,
|
| 313 |
+
true,
|
| 314 |
+
new SPLAT.Vector3(),
|
| 315 |
+
chosenInitAlpha,
|
| 316 |
+
chosenInitBeta,
|
| 317 |
+
chosenInitRadius
|
| 318 |
+
);
|
| 319 |
+
|
| 320 |
+
// Set control constraints
|
| 321 |
+
controlsInstance.maxZoom = maxZoom;
|
| 322 |
+
controlsInstance.minZoom = minZoom;
|
| 323 |
+
controlsInstance.minAngle = minAngle;
|
| 324 |
+
controlsInstance.maxAngle = maxAngle;
|
| 325 |
+
controlsInstance.minAzimuth = minAzimuth;
|
| 326 |
+
controlsInstance.maxAzimuth = maxAzimuth;
|
| 327 |
+
controlsInstance.panSpeed = isMobile ? 0.5 : 1.2;
|
| 328 |
+
|
| 329 |
+
// Update controls
|
| 330 |
+
controlsInstance.update();
|
| 331 |
+
|
| 332 |
+
console.log("Camera reset complete");
|
| 333 |
|
| 334 |
} catch (error) {
|
| 335 |
+
console.error("Error resetting camera:", error);
|
|
|
|
|
|
|
| 336 |
}
|
| 337 |
}
|
| 338 |
|
| 339 |
+
// Clean up all viewer resources
|
| 340 |
+
function cleanupViewer() {
|
| 341 |
+
console.log("Cleaning up viewer resources...");
|
| 342 |
+
|
| 343 |
+
// Stop animation frame
|
| 344 |
+
if (animFrameId) {
|
| 345 |
+
cancelAnimationFrame(animFrameId);
|
| 346 |
+
animFrameId = null;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
// Remove resize handler
|
| 350 |
+
if (resizeHandler) {
|
| 351 |
+
window.removeEventListener('resize', resizeHandler);
|
| 352 |
+
resizeHandler = null;
|
| 353 |
}
|
| 354 |
|
| 355 |
+
// Dispose controls
|
| 356 |
+
if (controlsInstance && typeof controlsInstance.dispose === 'function') {
|
| 357 |
+
try {
|
| 358 |
+
controlsInstance.dispose();
|
| 359 |
+
} catch (e) {
|
| 360 |
+
console.warn("Error disposing controls:", e);
|
| 361 |
+
}
|
| 362 |
+
controlsInstance = null;
|
| 363 |
+
}
|
| 364 |
|
| 365 |
+
// Dispose renderer
|
| 366 |
+
if (rendererInstance && typeof rendererInstance.dispose === 'function') {
|
| 367 |
+
try {
|
| 368 |
+
rendererInstance.dispose();
|
| 369 |
+
} catch (e) {
|
| 370 |
+
console.warn("Error disposing renderer:", e);
|
| 371 |
+
}
|
| 372 |
+
rendererInstance = null;
|
| 373 |
}
|
| 374 |
|
| 375 |
+
// Clear scene
|
| 376 |
+
sceneInstance = null;
|
| 377 |
+
|
| 378 |
+
// Clear camera
|
| 379 |
+
cameraInstance = null;
|
| 380 |
+
|
| 381 |
+
// Reset WebGL context
|
| 382 |
if (canvas) {
|
| 383 |
const ctx = canvas.getContext('webgl') || canvas.getContext('webgl2');
|
| 384 |
if (ctx && ctx.getExtension('WEBGL_lose_context')) {
|
|
|
|
| 390 |
}
|
| 391 |
}
|
| 392 |
|
| 393 |
+
// Mark viewer as not initialized
|
| 394 |
+
isViewerInitialized = false;
|
| 395 |
+
|
| 396 |
+
console.log("Viewer cleanup complete");
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
// --- Initialize the 3D PLY Viewer ---
|
| 400 |
+
async function initializeViewer() {
|
| 401 |
+
// Skip initialization if already initialized
|
| 402 |
+
if (isViewerInitialized) {
|
| 403 |
+
console.log("Viewer already initialized, skipping initialization");
|
| 404 |
+
return;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
console.log("Initializing PLY viewer...");
|
| 408 |
+
progressDialog.style.display = 'block';
|
| 409 |
+
progressIndicator.value = 0;
|
| 410 |
|
| 411 |
+
try {
|
| 412 |
+
// Load the SPLAT library if not already loaded
|
| 413 |
+
if (!SPLAT) {
|
| 414 |
+
console.log("Loading SPLAT library...");
|
| 415 |
+
SPLAT = await import("https://bilca-gsplat-library.static.hf.space/dist/index.js");
|
| 416 |
+
console.log("SPLAT library loaded successfully:", SPLAT);
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
// Create renderer
|
| 420 |
+
console.log("Creating WebGL renderer...");
|
| 421 |
+
rendererInstance = new SPLAT.WebGLRenderer(canvas);
|
| 422 |
+
console.log("Renderer created:", rendererInstance);
|
| 423 |
+
|
| 424 |
+
// Create scene
|
| 425 |
+
console.log("Creating scene...");
|
| 426 |
+
sceneInstance = new SPLAT.Scene();
|
| 427 |
+
console.log("Scene created:", sceneInstance);
|
| 428 |
+
|
| 429 |
+
// Create camera
|
| 430 |
+
console.log("Creating camera...");
|
| 431 |
+
cameraInstance = new SPLAT.Camera();
|
| 432 |
+
console.log("Camera created:", cameraInstance);
|
| 433 |
+
|
| 434 |
+
// Set canvas background
|
| 435 |
+
console.log("Setting canvas background...");
|
| 436 |
+
canvas.style.background = config.canvas_background || "#FEFEFD";
|
| 437 |
+
|
| 438 |
+
// Create controls
|
| 439 |
+
console.log("Creating orbit controls...");
|
| 440 |
+
controlsInstance = new SPLAT.OrbitControls(
|
| 441 |
+
cameraInstance,
|
| 442 |
+
canvas,
|
| 443 |
+
0.5,
|
| 444 |
+
0.5,
|
| 445 |
+
5,
|
| 446 |
+
true,
|
| 447 |
+
new SPLAT.Vector3(),
|
| 448 |
+
chosenInitAlpha,
|
| 449 |
+
chosenInitBeta,
|
| 450 |
+
chosenInitRadius
|
| 451 |
+
);
|
| 452 |
+
|
| 453 |
+
// Set control constraints
|
| 454 |
+
controlsInstance.maxZoom = maxZoom;
|
| 455 |
+
controlsInstance.minZoom = minZoom;
|
| 456 |
+
controlsInstance.minAngle = minAngle;
|
| 457 |
+
controlsInstance.maxAngle = maxAngle;
|
| 458 |
+
controlsInstance.minAzimuth = minAzimuth;
|
| 459 |
+
controlsInstance.maxAzimuth = maxAzimuth;
|
| 460 |
+
controlsInstance.panSpeed = isMobile ? 0.5 : 1.2;
|
| 461 |
+
|
| 462 |
+
controlsInstance.update();
|
| 463 |
+
console.log("Orbit controls created and configured:", controlsInstance);
|
| 464 |
+
|
| 465 |
+
// Handle resize
|
| 466 |
+
console.log("Setting up resize handler...");
|
| 467 |
+
const handleResize = () => {
|
| 468 |
+
if (rendererInstance) {
|
| 469 |
+
rendererInstance.setSize(canvas.clientWidth, canvas.clientHeight);
|
| 470 |
+
}
|
| 471 |
+
};
|
| 472 |
+
|
| 473 |
+
// Initial resize
|
| 474 |
+
handleResize();
|
| 475 |
+
|
| 476 |
+
// Add resize event listener
|
| 477 |
+
resizeHandler = handleResize;
|
| 478 |
+
window.addEventListener('resize', resizeHandler);
|
| 479 |
+
console.log("Resize handler set up");
|
| 480 |
+
|
| 481 |
+
// Load PLY file
|
| 482 |
+
console.log("Loading PLY file:", plyUrl);
|
| 483 |
+
try {
|
| 484 |
+
await SPLAT.PLYLoader.LoadAsync(
|
| 485 |
+
plyUrl,
|
| 486 |
+
sceneInstance,
|
| 487 |
+
(progress) => {
|
| 488 |
+
progressIndicator.value = progress * 100;
|
| 489 |
+
console.log(`Loading progress: ${Math.round(progress * 100)}%`);
|
| 490 |
+
}
|
| 491 |
+
);
|
| 492 |
+
console.log("PLY file loaded successfully");
|
| 493 |
+
progressDialog.style.display = 'none';
|
| 494 |
+
|
| 495 |
+
// Check if scene has content
|
| 496 |
+
console.log("Checking scene content...");
|
| 497 |
+
if (sceneInstance && sceneInstance.getChildren) {
|
| 498 |
+
const children = sceneInstance.getChildren();
|
| 499 |
+
console.log("Scene children:", children);
|
| 500 |
+
console.log("Scene children count:", children ? children.length : 0);
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
} catch (error) {
|
| 504 |
+
console.error("Error loading PLY file:", error);
|
| 505 |
+
progressDialog.innerHTML = `<p style="color: red">Error loading model: ${error.message}</p>`;
|
| 506 |
+
return;
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
// Start animation loop
|
| 510 |
+
console.log("Starting animation loop...");
|
| 511 |
+
const animate = () => {
|
| 512 |
+
if (controlsInstance && sceneInstance && cameraInstance && rendererInstance) {
|
| 513 |
+
controlsInstance.update();
|
| 514 |
+
rendererInstance.render(sceneInstance, cameraInstance);
|
| 515 |
+
animFrameId = requestAnimationFrame(animate);
|
| 516 |
+
}
|
| 517 |
+
};
|
| 518 |
+
|
| 519 |
+
animFrameId = requestAnimationFrame(animate);
|
| 520 |
+
console.log("Animation loop started with frame ID:", animFrameId);
|
| 521 |
+
|
| 522 |
+
// Mark viewer as initialized
|
| 523 |
+
isViewerInitialized = true;
|
| 524 |
+
console.log("PLY viewer initialization complete");
|
| 525 |
+
|
| 526 |
+
} catch (error) {
|
| 527 |
+
console.error("Error initializing PLY viewer:", error);
|
| 528 |
+
progressDialog.innerHTML = `<p style="color: red">Error initializing viewer: ${error.message}</p>`;
|
| 529 |
+
cleanupViewer();
|
| 530 |
+
}
|
| 531 |
}
|
| 532 |
})();
|