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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
background: #FFFEF4;
|
| 88 |
border: 1px solid #474558;
|
| 89 |
border-radius: 5px;
|
| 90 |
+
padding: 10px;
|
| 91 |
+
font-size: 15px;
|
| 92 |
+
line-height: 1.4;
|
| 93 |
+
color: #474558;
|
| 94 |
+
}
|
| 95 |
+
/* Button styling */
|
| 96 |
+
.widget-button {
|
| 97 |
+
position: absolute;
|
| 98 |
+
width: 45px;
|
| 99 |
+
height: 45px;
|
| 100 |
+
background-color: #FFFEF4;
|
| 101 |
+
border: 1px solid #474558;
|
| 102 |
+
border-radius: 50%;
|
| 103 |
+
cursor: pointer;
|
| 104 |
+
font-size: 14px;
|
| 105 |
+
color: #474558;
|
| 106 |
+
display: flex;
|
| 107 |
+
align-items: center;
|
| 108 |
+
justify-content: center;
|
| 109 |
+
}
|
| 110 |
+
/* Positions: Close at top-left, fullscreen at top-right, help (instructions) below fullscreen */
|
| 111 |
+
#close-btn {
|
| 112 |
+
top: 17px;
|
| 113 |
+
left: 15px;
|
| 114 |
+
}
|
| 115 |
+
#fullscreen-toggle {
|
| 116 |
+
top: 17px;
|
| 117 |
+
right: 15px;
|
| 118 |
+
}
|
| 119 |
+
#help-toggle {
|
| 120 |
+
top: 72px;
|
| 121 |
+
right: 15px;
|
| 122 |
+
}
|
| 123 |
+
`;
|
| 124 |
+
document.head.appendChild(styleEl);
|
| 125 |
+
|
| 126 |
+
// Create the widget container and set its inner HTML.
|
| 127 |
+
var widgetContainer = document.createElement('div');
|
| 128 |
+
widgetContainer.id = 'ply-widget-container';
|
| 129 |
+
widgetContainer.innerHTML = `
|
| 130 |
+
<!-- GIF Preview Container -->
|
| 131 |
+
<div id="gif-preview-container">
|
| 132 |
+
<img id="preview-image" alt="Preview" crossorigin="anonymous">
|
| 133 |
+
</div>
|
| 134 |
+
<!-- Viewer Container -->
|
| 135 |
+
<div id="viewer-container">
|
| 136 |
+
<canvas id="canvas"></canvas>
|
| 137 |
+
<div id="progress-dialog">
|
| 138 |
+
<progress id="progress-indicator" max="100" value="0"></progress>
|
| 139 |
+
</div>
|
| 140 |
+
<button id="close-btn" class="widget-button">X</button>
|
| 141 |
+
<button id="fullscreen-toggle" class="widget-button">⇱</button>
|
| 142 |
+
<button id="help-toggle" class="widget-button">?</button>
|
| 143 |
+
<div id="menu-content">
|
| 144 |
+
- Rotate with right click<br>
|
| 145 |
+
- Zoom in/out with middle click<br>
|
| 146 |
+
- Translate with left click
|
| 147 |
+
</div>
|
| 148 |
+
</div>
|
| 149 |
+
`;
|
| 150 |
+
// Append widgetContainer to the current script's parent so it appears in place.
|
| 151 |
+
document.currentScript.parentNode.appendChild(widgetContainer);
|
| 152 |
+
|
| 153 |
+
// Grab element references.
|
| 154 |
+
var gifPreview = document.getElementById('gif-preview-container');
|
| 155 |
+
var viewerContainer = document.getElementById('viewer-container');
|
| 156 |
+
var previewImage = document.getElementById('preview-image');
|
| 157 |
+
var closeBtn = document.getElementById('close-btn');
|
| 158 |
+
var fullscreenToggle = document.getElementById('fullscreen-toggle');
|
| 159 |
+
var helpToggle = document.getElementById('help-toggle');
|
| 160 |
+
var menuContent = document.getElementById('menu-content');
|
| 161 |
+
var canvas = document.getElementById('canvas');
|
| 162 |
+
var progressDialog = document.getElementById('progress-dialog');
|
| 163 |
+
var progressIndicator = document.getElementById('progress-indicator');
|
| 164 |
+
|
| 165 |
+
// Set the preview image if provided.
|
| 166 |
+
if (gifUrl) {
|
| 167 |
+
previewImage.src = gifUrl;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// --- Button Event Handlers ---
|
| 171 |
+
|
| 172 |
+
// When the preview image is clicked, hide it, show the viewer, and initialize the 3D viewer.
|
| 173 |
+
gifPreview.addEventListener('click', function() {
|
| 174 |
+
gifPreview.style.display = 'none';
|
| 175 |
+
viewerContainer.style.display = 'block';
|
| 176 |
+
initializeViewer();
|
| 177 |
+
});
|
| 178 |
+
|
| 179 |
+
// Close button: hide the viewer and show the preview.
|
| 180 |
+
closeBtn.addEventListener('click', function() {
|
| 181 |
+
// Exit fullscreen if active.
|
| 182 |
+
if (document.fullscreenElement === widgetContainer) {
|
| 183 |
+
if (document.exitFullscreen) {
|
| 184 |
+
document.exitFullscreen();
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
viewerContainer.style.display = 'none';
|
| 188 |
+
gifPreview.style.display = 'block';
|
| 189 |
+
});
|
| 190 |
+
|
| 191 |
+
// Fullscreen toggle: toggle fullscreen on the widget container.
|
| 192 |
+
fullscreenToggle.addEventListener('click', function() {
|
| 193 |
+
if (!document.fullscreenElement) {
|
| 194 |
+
if (widgetContainer.requestFullscreen) {
|
| 195 |
+
widgetContainer.requestFullscreen();
|
| 196 |
+
} else if (widgetContainer.webkitRequestFullscreen) {
|
| 197 |
+
widgetContainer.webkitRequestFullscreen();
|
| 198 |
+
} else if (widgetContainer.mozRequestFullScreen) {
|
| 199 |
+
widgetContainer.mozRequestFullScreen();
|
| 200 |
+
} else if (widgetContainer.msRequestFullscreen) {
|
| 201 |
+
widgetContainer.msRequestFullscreen();
|
| 202 |
+
}
|
| 203 |
+
} else {
|
| 204 |
+
if (document.exitFullscreen) {
|
| 205 |
+
document.exitFullscreen();
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
});
|
| 209 |
+
|
| 210 |
+
// Update the fullscreen button icon on fullscreen change.
|
| 211 |
+
document.addEventListener('fullscreenchange', function() {
|
| 212 |
+
if (document.fullscreenElement === widgetContainer) {
|
| 213 |
+
fullscreenToggle.textContent = '⇲';
|
| 214 |
+
} else {
|
| 215 |
+
fullscreenToggle.textContent = '⇱';
|
| 216 |
+
}
|
| 217 |
+
});
|
| 218 |
+
|
| 219 |
+
// Help (instructions) toggle: show/hide the instructions.
|
| 220 |
+
helpToggle.addEventListener('click', function(e) {
|
| 221 |
+
e.stopPropagation();
|
| 222 |
+
menuContent.style.display = (menuContent.style.display === 'block') ? 'none' : 'block';
|
| 223 |
+
});
|
| 224 |
+
|
| 225 |
+
// --- Initialize the 3D PLY Viewer ---
|
| 226 |
+
async function initializeViewer() {
|
| 227 |
+
// Dynamically import the gsplat library.
|
| 228 |
+
const SPLAT = await import("https://cdn.jsdelivr.net/npm/gsplat@latest");
|
| 229 |
+
|
| 230 |
+
// Display the progress dialog by setting its display style to 'block'
|
| 231 |
+
progressDialog.style.display = 'block';
|
| 232 |
+
|
| 233 |
+
// Create renderer, scene, camera, and controls.
|
| 234 |
+
const renderer = new SPLAT.WebGLRenderer(canvas);
|
| 235 |
+
const scene = new SPLAT.Scene();
|
| 236 |
+
const camera = new SPLAT.Camera();
|
| 237 |
+
const controls = new SPLAT.OrbitControls(camera, canvas);
|
| 238 |
+
|
| 239 |
+
canvas.style.background = "#FEFEFD";
|
| 240 |
+
controls.maxZoom = maxZoom;
|
| 241 |
+
controls.minZoom = minZoom;
|
| 242 |
+
controls.minAngle = minAngle;
|
| 243 |
+
controls.maxAngle = maxAngle;
|
| 244 |
+
|
| 245 |
+
controls.update();
|
| 246 |
+
|
| 247 |
+
// Load the PLY model from the provided URL.
|
| 248 |
+
try {
|
| 249 |
+
await SPLAT.PLYLoader.LoadAsync(
|
| 250 |
+
plyUrl,
|
| 251 |
+
scene,
|
| 252 |
+
(progress) => {
|
| 253 |
+
progressIndicator.value = progress * 100;
|
| 254 |
+
}
|
| 255 |
+
);
|
| 256 |
+
// Hide the progress dialog once the model is loaded.
|
| 257 |
+
progressDialog.style.display = 'none';
|
| 258 |
+
} catch (error) {
|
| 259 |
+
console.error("Error loading PLY file:", error);
|
| 260 |
+
progressDialog.innerHTML = `<p style="color: red">Error loading model: ${error.message}</p>`;
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
// Render loop and resize handling.
|
| 264 |
+
const handleResize = () => {
|
| 265 |
+
renderer.setSize(canvas.clientWidth, canvas.clientHeight);
|
| 266 |
+
};
|
| 267 |
+
|
| 268 |
+
const frame = () => {
|
| 269 |
+
controls.update();
|
| 270 |
+
renderer.render(scene, camera);
|
| 271 |
+
requestAnimationFrame(frame);
|
| 272 |
+
};
|
| 273 |
+
|
| 274 |
+
handleResize();
|
| 275 |
+
window.addEventListener("resize", handleResize);
|
| 276 |
+
requestAnimationFrame(frame);
|
| 277 |
+
}
|
| 278 |
+
})();
|