Spaces:
Running
Running
(async function() { | |
// Retrieve the current script tag and load the JSON configuration file from the data-config attribute. | |
const scriptTag = document.currentScript; | |
const configUrl = scriptTag.getAttribute("data-config"); | |
let config = {}; | |
if (configUrl) { | |
try { | |
const response = await fetch(configUrl); | |
config = await response.json(); | |
} catch (error) { | |
console.error("Error loading config file:", error); | |
return; | |
} | |
} else { | |
console.error("No config file provided. Please set a data-config attribute on the script tag."); | |
return; | |
} | |
// --- Outer scope variables for camera state --- | |
let cameraInstance = null; | |
let controlsInstance = null; | |
let initialCameraPosition = null; | |
let initialCameraRotation = null; | |
let SPLAT = null; // Will store the imported SPLAT module | |
// Generate a unique identifier for this widget instance. | |
const instanceId = Math.random().toString(36).substr(2, 8); | |
// Read configuration values from the JSON file. | |
const gifUrl = config.gif_url; | |
const plyUrl = config.ply_url; | |
const minZoom = parseFloat(config.minZoom || "0"); | |
const maxZoom = parseFloat(config.maxZoom || "20"); | |
const minAngle = parseFloat(config.minAngle || "0"); | |
const maxAngle = parseFloat(config.maxAngle || "360"); | |
const minAzimuth = config.minAzimuth !== undefined ? parseFloat(config.minAzimuth) : -Infinity; | |
const maxAzimuth = config.maxAzimuth !== undefined ? parseFloat(config.maxAzimuth) : Infinity; | |
// Read initial orbit parameters. | |
const initAlphaDesktop = config.initAlpha !== undefined ? parseFloat(config.initAlpha) : 0.5; | |
const initBetaDesktop = config.initBeta !== undefined ? parseFloat(config.initBeta) : 0.5; | |
const initRadiusDesktop = config.initRadius !== undefined ? parseFloat(config.initRadius) : 5; | |
const initAlphaPhone = config.initAlphaPhone !== undefined ? parseFloat(config.initAlphaPhone) : initAlphaDesktop; | |
const initBetaPhone = config.initBetaPhone !== undefined ? parseFloat(config.initBetaPhone) : initBetaDesktop; | |
const initRadiusPhone = config.initRadiusPhone !== undefined ? parseFloat(config.initRadiusPhone) : initRadiusDesktop; | |
// Detect device type. | |
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; | |
const isMobile = isIOS || /Android/i.test(navigator.userAgent); | |
const chosenInitAlpha = isMobile ? initAlphaPhone : initAlphaDesktop; | |
const chosenInitBeta = isMobile ? initBetaPhone : initBetaDesktop; | |
const chosenInitRadius = isMobile ? initRadiusPhone : initRadiusDesktop; | |
// Inject CSS styles. | |
const styleEl = document.createElement('style'); | |
styleEl.textContent = ` | |
/* Widget container styling - height/width set to 100% (we will adjust height in script) */ | |
#ply-widget-container-${instanceId} { | |
position: relative; | |
width: 100%; | |
height: 100%; | |
} | |
/* Fake fullscreen (for iOS) */ | |
#ply-widget-container-${instanceId}.fake-fullscreen { | |
position: fixed !important; | |
top: 0 !important; | |
left: 0 !important; | |
width: 100vw !important; | |
height: 100vh !important; | |
z-index: 9999 !important; | |
} | |
/* GIF Preview styling */ | |
#gif-preview-container-${instanceId} { | |
position: absolute; | |
top: 0; left: 0; | |
width: 100%; | |
height: 100%; | |
border: 1px solid #474558; | |
border-radius: 10px; | |
overflow: hidden; | |
cursor: pointer; | |
} | |
#gif-preview-container-${instanceId} img { | |
width: 100%; | |
height: 100%; | |
object-fit: cover; | |
} | |
/* Viewer Container styling */ | |
#viewer-container-${instanceId} { | |
display: none; | |
position: absolute; | |
top: 0; left: 0; | |
width: 100%; | |
height: 100%; | |
background: #FEFEFD; | |
border: 1px solid #474558; | |
border-radius: 10px; | |
overflow: hidden; | |
box-sizing: border-box; | |
} | |
/* Canvas fills the viewer container */ | |
#canvas-${instanceId} { | |
width: 100%; | |
height: 100%; | |
display: block; | |
} | |
/* Progress dialog styling */ | |
#progress-dialog-${instanceId} { | |
position: absolute; | |
top: 50%; left: 50%; | |
transform: translate(-50%, -50%); | |
background: rgba(255,255,255,0.9); | |
padding: 20px; | |
border-radius: 5px; | |
z-index: 1000; | |
display: none; | |
} | |
/* Menu content styling */ | |
#menu-content-${instanceId} { | |
display: none; | |
position: absolute; | |
top: 70px; right: 15px; | |
background: #FFFEF4; | |
border: 1px solid #474558; | |
border-radius: 5px; | |
padding: 10px; | |
font-size: 15px; | |
line-height: 1.4; | |
color: #474558; | |
} | |
/* Button styling */ | |
.widget-button { | |
position: absolute; | |
width: 45px; | |
height: 45px; | |
background-color: #FFFEF4; | |
border: 1px solid #474558; | |
border-radius: 50%; | |
cursor: pointer; | |
font-size: 14px; | |
color: #474558; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
} | |
/* Button positions */ | |
#close-btn-${instanceId} { top: 17px; left: 15px; } | |
#fullscreen-toggle-${instanceId} { top: 17px; right: 15px; } | |
#help-toggle-${instanceId} { top: 17px; right: 70px; font-size: 22px; } | |
#reset-camera-btn-${instanceId} { top: 17px; right: 123px; font-size: 22px; } | |
.reset-icon { display: inline-block; transform: translateY(-3px); } | |
`; | |
document.head.appendChild(styleEl); | |
// Create widget container and inner HTML. | |
const widgetContainer = document.createElement('div'); | |
widgetContainer.id = 'ply-widget-container-' + instanceId; | |
widgetContainer.innerHTML = ` | |
<div id="gif-preview-container-${instanceId}"> | |
<img id="preview-image-${instanceId}" alt="Preview" crossorigin="anonymous"> | |
</div> | |
<div id="viewer-container-${instanceId}"> | |
<canvas id="canvas-${instanceId}"></canvas> | |
<div id="progress-dialog-${instanceId}"> | |
<progress id="progress-indicator-${instanceId}" max="100" value="0"></progress> | |
</div> | |
<button id="close-btn-${instanceId}" class="widget-button">X</button> | |
<button id="fullscreen-toggle-${instanceId}" class="widget-button">⇱</button> | |
<button id="help-toggle-${instanceId}" class="widget-button">?</button> | |
<button id="reset-camera-btn-${instanceId}" class="widget-button"> | |
<span class="reset-icon">⟲</span> | |
</button> | |
<div id="menu-content-${instanceId}"></div> | |
</div> | |
`; | |
scriptTag.parentNode.appendChild(widgetContainer); | |
// Update widget container height based on its parent's height using ResizeObserver if available. | |
const parentElem = scriptTag.parentNode; | |
function updateWidgetSize() { | |
const rect = parentElem.getBoundingClientRect(); | |
// Set the widget's height to match the parent's height. | |
widgetContainer.style.height = rect.height ? rect.height + "px" : "300px"; | |
} | |
updateWidgetSize(); | |
if (window.ResizeObserver) { | |
const observer = new ResizeObserver(() => updateWidgetSize()); | |
observer.observe(parentElem); | |
} else { | |
window.addEventListener("resize", updateWidgetSize); | |
} | |
// Grab element references. | |
const gifPreview = document.getElementById('gif-preview-container-' + instanceId); | |
const viewerContainer = document.getElementById('viewer-container-' + instanceId); | |
const previewImage = document.getElementById('preview-image-' + instanceId); | |
const closeBtn = document.getElementById('close-btn-' + instanceId); | |
const fullscreenToggle = document.getElementById('fullscreen-toggle-' + instanceId); | |
const helpToggle = document.getElementById('help-toggle-' + instanceId); | |
const resetCameraBtn = document.getElementById('reset-camera-btn-' + instanceId); | |
const menuContent = document.getElementById('menu-content-' + instanceId); | |
const canvas = document.getElementById('canvas-' + instanceId); | |
const progressDialog = document.getElementById('progress-dialog-' + instanceId); | |
const progressIndicator = document.getElementById('progress-indicator-' + instanceId); | |
// Set help instructions. | |
if (isMobile) { | |
menuContent.innerHTML = ` | |
- Pour vous déplacer, glissez deux doigts sur l'écran.<br> | |
- Pour orbiter, utilisez un doigt.<br> | |
- Pour zoomer, pincez avec deux doigts. | |
`; | |
} else { | |
menuContent.innerHTML = ` | |
- orbitez avec le clic droit<br> | |
- zoomez avec la molette<br> | |
- déplacez vous avec le clic gauche | |
`; | |
} | |
// Handle preview display. | |
if (gifUrl) { | |
previewImage.src = gifUrl; | |
} else { | |
gifPreview.style.display = 'none'; | |
viewerContainer.style.display = 'block'; | |
closeBtn.style.display = 'none'; | |
initializeViewer(); | |
} | |
// --- Button Event Handlers --- | |
if (gifUrl) { | |
gifPreview.addEventListener('click', function() { | |
gifPreview.style.display = 'none'; | |
viewerContainer.style.display = 'block'; | |
initializeViewer(); | |
}); | |
} | |
closeBtn.addEventListener('click', function() { | |
if (document.fullscreenElement === widgetContainer && document.exitFullscreen) { | |
document.exitFullscreen(); | |
} | |
if (widgetContainer.classList.contains('fake-fullscreen')) { | |
widgetContainer.classList.remove('fake-fullscreen'); | |
fullscreenToggle.textContent = '⇱'; | |
resetCamera(); | |
} | |
viewerContainer.style.display = 'none'; | |
gifPreview.style.display = 'block'; | |
}); | |
fullscreenToggle.addEventListener('click', function() { | |
if (isIOS) { | |
if (!widgetContainer.classList.contains('fake-fullscreen')) { | |
widgetContainer.classList.add('fake-fullscreen'); | |
} else { | |
widgetContainer.classList.remove('fake-fullscreen'); | |
resetCamera(); | |
} | |
fullscreenToggle.textContent = widgetContainer.classList.contains('fake-fullscreen') ? '⇲' : '⇱'; | |
} else { | |
if (!document.fullscreenElement) { | |
if (widgetContainer.requestFullscreen) { | |
widgetContainer.requestFullscreen(); | |
} else if (widgetContainer.webkitRequestFullscreen) { | |
widgetContainer.webkitRequestFullscreen(); | |
} else if (widgetContainer.mozRequestFullScreen) { | |
widgetContainer.mozRequestFullScreen(); | |
} else if (widgetContainer.msRequestFullscreen) { | |
widgetContainer.msRequestFullscreen(); | |
} | |
} else if (document.exitFullscreen) { | |
document.exitFullscreen(); | |
} | |
} | |
}); | |
document.addEventListener('fullscreenchange', function() { | |
if (document.fullscreenElement === widgetContainer) { | |
fullscreenToggle.textContent = '⇲'; | |
} else { | |
fullscreenToggle.textContent = '⇱'; | |
resetCamera(); | |
} | |
}); | |
helpToggle.addEventListener('click', function(e) { | |
e.stopPropagation(); | |
menuContent.style.display = (menuContent.style.display === 'block') ? 'none' : 'block'; | |
}); | |
// --- Camera Reset Function --- | |
function resetCamera() { | |
console.log("Resetting camera."); | |
if (cameraInstance && initialCameraPosition && initialCameraRotation) { | |
cameraInstance.position = initialCameraPosition.clone(); | |
cameraInstance.rotation = initialCameraRotation.clone(); | |
if (typeof cameraInstance.update === 'function') { | |
cameraInstance.update(); | |
} | |
if (controlsInstance && typeof controlsInstance.dispose === 'function') { | |
controlsInstance.dispose(); | |
} | |
controlsInstance = new SPLAT.OrbitControls( | |
cameraInstance, | |
canvas, | |
0.5, 0.5, 5, true, | |
new SPLAT.Vector3(), | |
chosenInitAlpha, chosenInitBeta, chosenInitRadius | |
); | |
controlsInstance.maxZoom = maxZoom; | |
controlsInstance.minZoom = minZoom; | |
controlsInstance.minAngle = minAngle; | |
controlsInstance.maxAngle = maxAngle; | |
controlsInstance.minAzimuth = minAzimuth; | |
controlsInstance.maxAzimuth = maxAzimuth; | |
controlsInstance.panSpeed = isMobile ? 0.5 : 1.2; | |
controlsInstance.update(); | |
} | |
} | |
resetCameraBtn.addEventListener('click', function() { | |
console.log("Reset camera button clicked."); | |
resetCamera(); | |
}); | |
document.addEventListener('keydown', function(e) { | |
if (e.key === 'Escape' || e.key === 'Esc') { | |
let wasFullscreen = false; | |
if (document.fullscreenElement === widgetContainer && document.exitFullscreen) { | |
wasFullscreen = true; | |
document.exitFullscreen(); | |
} | |
if (widgetContainer.classList.contains('fake-fullscreen')) { | |
wasFullscreen = true; | |
widgetContainer.classList.remove('fake-fullscreen'); | |
fullscreenToggle.textContent = '⇱'; | |
} | |
if (wasFullscreen) { | |
resetCamera(); | |
} | |
} | |
}); | |
// --- Initialize the 3D PLY Viewer --- | |
async function initializeViewer() { | |
SPLAT = await import("https://bilca-gsplat-library.static.hf.space/dist/index.js"); | |
progressDialog.style.display = 'block'; | |
const renderer = new SPLAT.WebGLRenderer(canvas); | |
const scene = new SPLAT.Scene(); | |
const camera = new SPLAT.Camera(); | |
controlsInstance = new SPLAT.OrbitControls( | |
camera, | |
canvas, | |
0.5, 0.5, 5, true, | |
new SPLAT.Vector3(), | |
chosenInitAlpha, chosenInitBeta, chosenInitRadius | |
); | |
cameraInstance = camera; | |
initialCameraPosition = camera.position.clone(); | |
initialCameraRotation = camera.rotation.clone(); | |
canvas.style.background = "#FEFEFD"; | |
controlsInstance.maxZoom = maxZoom; | |
controlsInstance.minZoom = minZoom; | |
controlsInstance.minAngle = minAngle; | |
controlsInstance.maxAngle = maxAngle; | |
controlsInstance.minAzimuth = minAzimuth; | |
controlsInstance.maxAzimuth = maxAzimuth; | |
controlsInstance.panSpeed = isMobile ? 0.5 : 1.2; | |
controlsInstance.update(); | |
try { | |
await SPLAT.PLYLoader.LoadAsync( | |
plyUrl, | |
scene, | |
(progress) => { progressIndicator.value = progress * 100; } | |
); | |
progressDialog.style.display = 'none'; | |
} catch (error) { | |
console.error("Error loading PLY file:", error); | |
progressDialog.innerHTML = `<p style="color: red">Error loading model: ${error.message}</p>`; | |
} | |
const frame = () => { | |
controlsInstance.update(); | |
renderer.render(scene, camera); | |
requestAnimationFrame(frame); | |
}; | |
const handleResize = () => { | |
renderer.setSize(canvas.clientWidth, canvas.clientHeight); | |
}; | |
handleResize(); | |
window.addEventListener("resize", handleResize); | |
requestAnimationFrame(frame); | |
} | |
})(); | |