Spaces:
Running
Running
Update js_scripts/index.js
Browse files- js_scripts/index.js +35 -24
js_scripts/index.js
CHANGED
|
@@ -61,7 +61,22 @@ const currentScriptTag = document.currentScript;
|
|
| 61 |
let viewerInitialized = false;
|
| 62 |
let wheelHandlers = [];
|
| 63 |
let resizeHandler = null;
|
| 64 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
// Generate a unique identifier for this widget instance.
|
| 67 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
|
@@ -109,6 +124,11 @@ const currentScriptTag = document.currentScript;
|
|
| 109 |
const chosenCameraY = isMobile ? cameraYPhone : cameraY;
|
| 110 |
const chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
// Determine the aspect ratio.
|
| 113 |
let aspectPercent = "100%";
|
| 114 |
if (config.aspect) {
|
|
@@ -231,10 +251,8 @@ const currentScriptTag = document.currentScript;
|
|
| 231 |
gifPreview.style.display = 'none';
|
| 232 |
viewerContainer.style.display = 'block';
|
| 233 |
|
| 234 |
-
//
|
| 235 |
-
|
| 236 |
-
initializeViewer();
|
| 237 |
-
}
|
| 238 |
});
|
| 239 |
}
|
| 240 |
|
|
@@ -287,7 +305,7 @@ const currentScriptTag = document.currentScript;
|
|
| 287 |
cameraEntity = null;
|
| 288 |
modelEntity = null;
|
| 289 |
|
| 290 |
-
// Mark the viewer as not initialized
|
| 291 |
viewerInitialized = false;
|
| 292 |
|
| 293 |
console.log("Viewer cleanup complete");
|
|
@@ -381,6 +399,8 @@ const currentScriptTag = document.currentScript;
|
|
| 381 |
return;
|
| 382 |
}
|
| 383 |
|
|
|
|
|
|
|
| 384 |
// Store model position
|
| 385 |
const modelPos = modelEntity.getPosition();
|
| 386 |
|
|
@@ -517,8 +537,8 @@ const currentScriptTag = document.currentScript;
|
|
| 517 |
// --- Initialize the 3D PLY Viewer using PlayCanvas ---
|
| 518 |
async function initializeViewer() {
|
| 519 |
// Skip initialization if already initialized
|
| 520 |
-
if (viewerInitialized
|
| 521 |
-
console.log("Viewer already initialized
|
| 522 |
return;
|
| 523 |
}
|
| 524 |
|
|
@@ -672,6 +692,9 @@ const currentScriptTag = document.currentScript;
|
|
| 672 |
});
|
| 673 |
|
| 674 |
// Set camera position directly using X, Y, Z coordinates from config
|
|
|
|
|
|
|
|
|
|
| 675 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 676 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 677 |
|
|
@@ -727,6 +750,9 @@ const currentScriptTag = document.currentScript;
|
|
| 727 |
cameraEntity.script.orbitCamera.pivotPoint.copy(modelPos);
|
| 728 |
cameraEntity.script.orbitCamera.distance = distance;
|
| 729 |
cameraEntity.script.orbitCamera._removeInertia();
|
|
|
|
|
|
|
|
|
|
| 730 |
}
|
| 731 |
}, 100);
|
| 732 |
|
|
@@ -739,19 +765,4 @@ const currentScriptTag = document.currentScript;
|
|
| 739 |
setupWheelHandlers();
|
| 740 |
|
| 741 |
// Hide progress dialog when everything is set up
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
// Mark viewer as initialized
|
| 745 |
-
viewerInitialized = true;
|
| 746 |
-
|
| 747 |
-
console.log("PLY viewer initialization complete");
|
| 748 |
-
});
|
| 749 |
-
|
| 750 |
-
} catch (error) {
|
| 751 |
-
console.error("Error initializing PlayCanvas viewer:", error);
|
| 752 |
-
progressDialog.innerHTML = `<p style="color: red">Error loading viewer: ${error.message}</p>`;
|
| 753 |
-
viewerInitialized = false;
|
| 754 |
-
app = null;
|
| 755 |
-
}
|
| 756 |
-
}
|
| 757 |
-
})();
|
|
|
|
| 61 |
let viewerInitialized = false;
|
| 62 |
let wheelHandlers = [];
|
| 63 |
let resizeHandler = null;
|
| 64 |
+
let progressDialog.style.display = 'none';
|
| 65 |
+
|
| 66 |
+
// Mark viewer as initialized
|
| 67 |
+
viewerInitialized = true;
|
| 68 |
+
|
| 69 |
+
console.log("PLY viewer initialization complete");
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
} catch (error) {
|
| 73 |
+
console.error("Error initializing PlayCanvas viewer:", error);
|
| 74 |
+
progressDialog.innerHTML = `<p style="color: red">Error loading viewer: ${error.message}</p>`;
|
| 75 |
+
viewerInitialized = false;
|
| 76 |
+
app = null;
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
})();Checker = null;
|
| 80 |
|
| 81 |
// Generate a unique identifier for this widget instance.
|
| 82 |
const instanceId = Math.random().toString(36).substr(2, 8);
|
|
|
|
| 124 |
const chosenCameraY = isMobile ? cameraYPhone : cameraY;
|
| 125 |
const chosenCameraZ = isMobile ? cameraZPhone : cameraZ;
|
| 126 |
|
| 127 |
+
// Log device detection and camera coordinates for debugging
|
| 128 |
+
console.log(`Device detected: ${isMobile ? 'Mobile' : 'Desktop'}`);
|
| 129 |
+
console.log(`Camera coordinates: Desktop (${cameraX}, ${cameraY}, ${cameraZ}), Mobile (${cameraXPhone}, ${cameraYPhone}, ${cameraZPhone})`);
|
| 130 |
+
console.log(`Chosen camera coordinates: (${chosenCameraX}, ${chosenCameraY}, ${chosenCameraZ})`);
|
| 131 |
+
|
| 132 |
// Determine the aspect ratio.
|
| 133 |
let aspectPercent = "100%";
|
| 134 |
if (config.aspect) {
|
|
|
|
| 251 |
gifPreview.style.display = 'none';
|
| 252 |
viewerContainer.style.display = 'block';
|
| 253 |
|
| 254 |
+
// Always initialize the viewer when the GIF is clicked
|
| 255 |
+
initializeViewer();
|
|
|
|
|
|
|
| 256 |
});
|
| 257 |
}
|
| 258 |
|
|
|
|
| 305 |
cameraEntity = null;
|
| 306 |
modelEntity = null;
|
| 307 |
|
| 308 |
+
// Mark the viewer as not initialized - this is crucial for proper reinitialization
|
| 309 |
viewerInitialized = false;
|
| 310 |
|
| 311 |
console.log("Viewer cleanup complete");
|
|
|
|
| 399 |
return;
|
| 400 |
}
|
| 401 |
|
| 402 |
+
console.log(`Resetting camera to: (${chosenCameraX}, ${chosenCameraY}, ${chosenCameraZ}) for ${isMobile ? 'mobile' : 'desktop'}`);
|
| 403 |
+
|
| 404 |
// Store model position
|
| 405 |
const modelPos = modelEntity.getPosition();
|
| 406 |
|
|
|
|
| 537 |
// --- Initialize the 3D PLY Viewer using PlayCanvas ---
|
| 538 |
async function initializeViewer() {
|
| 539 |
// Skip initialization if already initialized
|
| 540 |
+
if (viewerInitialized && app) {
|
| 541 |
+
console.log("Viewer already initialized and app exists, skipping initialization");
|
| 542 |
return;
|
| 543 |
}
|
| 544 |
|
|
|
|
| 692 |
});
|
| 693 |
|
| 694 |
// Set camera position directly using X, Y, Z coordinates from config
|
| 695 |
+
// Log the chosen camera position for debugging
|
| 696 |
+
console.log(`Setting camera position for ${isMobile ? 'mobile' : 'desktop'}: (${chosenCameraX}, ${chosenCameraY}, ${chosenCameraZ})`);
|
| 697 |
+
|
| 698 |
cameraEntity.setPosition(chosenCameraX, chosenCameraY, chosenCameraZ);
|
| 699 |
cameraEntity.lookAt(modelEntity.getPosition());
|
| 700 |
|
|
|
|
| 750 |
cameraEntity.script.orbitCamera.pivotPoint.copy(modelPos);
|
| 751 |
cameraEntity.script.orbitCamera.distance = distance;
|
| 752 |
cameraEntity.script.orbitCamera._removeInertia();
|
| 753 |
+
|
| 754 |
+
// Log camera setup for debugging
|
| 755 |
+
console.log(`Camera initialized with pivot: (${modelPos.x}, ${modelPos.y}, ${modelPos.z}), distance: ${distance}`);
|
| 756 |
}
|
| 757 |
}, 100);
|
| 758 |
|
|
|
|
| 765 |
setupWheelHandlers();
|
| 766 |
|
| 767 |
// Hide progress dialog when everything is set up
|
| 768 |
+
progress
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|