storysplat-viewer 2.4.1 → 2.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import*as e from"playcanvas";function t(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function o(e){return e.replace(/<\/script/gi,"<\\/script")}function n(e,n={}){const{cdnUrl:i="https://unpkg.com/storysplat-viewer@2/dist/storysplat-viewer.umd.js",title:a=e.name||"StorySplat Scene",description:s=`Interactive 3D scene: ${e.name||"StorySplat"}`,faviconUrl:r,customCSS:l="",lazyLoad:c,lazyLoadButtonText:d}=n,p=c??e.uiOptions?.lazyLoad??!1,h=d??e.uiOptions?.lazyLoadButtonText,u=r?`<link rel="icon" href="${t(r)}" />`:"",m=l?`<style>${l}</style>`:"",g=o(JSON.stringify(e,(e,t)=>{if(!("undefined"!=typeof HTMLElement&&t instanceof HTMLElement||"function"==typeof t||t&&"object"==typeof t&&"nodeType"in t))return t},2));return`<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">\n <meta name="description" content="${t(s)}">\n <title>${t(a)}</title>\n ${u}\n <style>\n * { margin: 0; padding: 0; box-sizing: border-box; }\n html, body {\n width: 100%;\n /* Use 100dvh for iOS address bar support, with 100vh fallback */\n height: 100vh;\n height: 100dvh;\n overflow: hidden;\n background: #111;\n }\n #app {\n width: 100%;\n /* Use 100dvh for iOS address bar support, with 100vh fallback */\n height: 100vh;\n height: 100dvh;\n position: relative;\n }\n /* Loading state */\n #app:empty::after {\n content: 'Loading...';\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n color: #666;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n font-size: 16px;\n }\n </style>\n ${m}\n</head>\n<body>\n <div id="app"></div>\n\n <script src="https://cdn.jsdelivr.net/npm/playcanvas@2.14.3/build/playcanvas.min.js"><\/script>\n <script src="${t(i)}"><\/script>\n <script>\n (function() {\n 'use strict';\n\n var sceneData = ${g};\n\n // Wait for DOM and viewer to be ready\n function init() {\n var container = document.getElementById('app');\n if (!container) {\n console.error('[StorySplat] Container #app not found');\n return;\n }\n\n if (typeof StorySplatViewer === 'undefined' || !StorySplatViewer.createViewer) {\n console.error('[StorySplat] Viewer not loaded. Check CDN URL.');\n return;\n }\n\n try {\n var viewer = StorySplatViewer.createViewer(container, sceneData, {\n showUI: true,\n autoPlay: false,\n lazyLoad: ${p},\n lazyLoadButtonText: ${h?`'${o(h)}'`:"undefined"}\n });\n\n viewer.on('ready', function() {\n console.log('[StorySplat] Scene ready');\n });\n\n viewer.on('error', function(err) {\n console.error('[StorySplat] Error:', err);\n });\n\n // Expose viewer globally for debugging\n window.storySplatViewer = viewer;\n } catch (err) {\n console.error('[StorySplat] Failed to create viewer:', err);\n }\n }\n\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', init);\n } else {\n init();\n }\n })();\n <\/script>\n</body>\n</html>`}async function i(e,t={}){const o=await fetch(e);if(!o.ok)throw new Error(`Failed to fetch scene: ${o.statusText}`);return n(await o.json(),t)}function a(e){const t=e.loadedModelUrl||e.splatUrl||"",o=e.sogModelUrl||e.sogUrl,n=e.compressedPlyUrl,i=e.lodMetaUrl,a=t.split("?")[0].split(".").pop()?.toLowerCase(),s="ply"===a||t.includes(".compressed.ply");let r=t;o?r=o:n?r=n:s||console.warn("[StorySplat Viewer] Original file format may not be compatible with PlayCanvas:",a),console.log("[StorySplat Viewer] URL selection:",{original:t,lodMetaUrl:i,sogUrl:o,compressedPlyUrl:n,selected:r});const l=(e.waypoints||[]).map(e=>{let t=e.fov||60;t<3.5&&(t*=180/Math.PI);let o=e.info||e.description||"";if(!o&&e.interactions){const t=e.interactions.find(e=>"info"===e.type);t&&t.data&&t.data.text&&(o=t.data.text)}return{position:e.position||{x:e.x||0,y:e.y||0,z:e.z||0},rotation:e.rotation||{x:0,y:0,z:0},fov:t,duration:e.duration||2e3,name:e.name||e.title||"",info:o,interactions:e.interactions||[],triggerDistance:e.triggerDistance??1}});return{name:e.name||"StorySplat Scene",sceneId:e.sceneId,userId:e.userId,userName:e.userName||"Unknown",thumbnailUrl:e.thumbnailUrl,splatUrl:r,sogUrl:o,lodMetaUrl:i,fallbackUrls:[...e.fallbackUrls||[],o!==r?o:null,n!==r?n:null,t!==r?t:null].filter(e=>null!=e&&""!==e),scale:e.scale||(null!=e.splatScale?{x:e.splatScale,y:e.splatScale,z:e.splatScale}:{x:1,y:1,z:1}),splatPosition:e.splatPosition||e.position||e.cameraPosition||[0,0,0],splatRotation:e.splatRotation||e.rotation||e.cameraRotation||[0,0,0],invertXScale:e.invertXScale,invertYScale:e.invertYScale,waypoints:l,hotspots:e.hotspots||[],portals:e.portals||[],skybox:e.skybox,customMeshes:e.customMeshes||[],htmlMeshes:e.htmlMeshes||[],lights:e.lights||[],particles:e.particles||[],collisionMeshesData:e.collisionMeshesData||[],playerHeight:e.playerHeight,uiColor:e.uiColor||"#ffffff",uiOptions:{showStartExperience:e.uiOptions?.showStartExperience??!0,showWatermark:e.uiOptions?.showWatermark??!0,hideFullscreenButton:e.uiOptions?.hideFullscreenButton??!1,hideInfoButton:e.uiOptions?.hideInfoButton??!1,hideMuteButton:e.uiOptions?.hideMuteButton??!1,hideHelpButton:e.uiOptions?.hideHelpButton??!1,hideWatermark:e.uiOptions?.hideWatermark??!1,watermarkText:e.uiOptions?.watermarkText,watermarkLink:e.uiOptions?.watermarkLink,buttonPosition:e.uiOptions?.buttonPosition||"inline",buttonLabels:e.uiOptions?.buttonLabels,customPreloaderLogoUrl:e.uiOptions?.customPreloaderLogoUrl,lazyLoad:e.uiOptions?.lazyLoad,lazyLoadButtonText:e.uiOptions?.lazyLoadButtonText},defaultCameraMode:e.defaultCameraMode||"orbit",allowedCameraModes:e.allowedCameraModes||["orbit","first-person","drone"],cameraMovementSpeed:e.cameraMovementSpeed,cameraRotationSensitivity:e.cameraRotationSensitivity,cameraDamping:e.cameraDamping,invertCameraRotation:e.invertCameraRotation,includeScrollControls:e.includeScrollControls??!0,scrollButtonMode:e.scrollButtonMode||"continuous",scrollAmount:e.scrollAmount||100,scrollSpeed:e.scrollSpeed,transitionSpeed:e.transitionSpeed,autoPlayEnabled:e.autoPlayEnabled??!1,autoplaySpeed:e.autoplaySpeed,loopMode:e.loopMode,includeXR:e.includeXR,xrMode:e.xrMode,customScript:e.customScript,templateType:"standard",additionalSplats:e.additionalSplats||[],keepMeshesInMemory:e.keepMeshesInMemory??!1,initialSplatExploreMode:e.initialSplatExploreMode,audioEmitters:e.audioEmitters||[]}}function s(e){const t=e.waypoints?.[0]?.fov||60;return{splatUrl:e.splatUrl,sogUrl:e.sogUrl,lodMetaUrl:e.lodMetaUrl,fallbackUrls:e.fallbackUrls,scale:e.scale,position:e.splatPosition||[0,0,0],rotation:e.splatRotation||[0,0,0],invertXScale:e.invertXScale,invertYScale:e.invertYScale,waypoints:e.waypoints,hotspots:e.hotspots,portals:e.portals,skybox:e.skybox,customMeshes:e.customMeshes,htmlMeshes:e.htmlMeshes,lights:e.lights,particles:e.particles,collisionMeshesData:e.collisionMeshesData,cameraMode:e.defaultCameraMode,defaultCameraMode:e.defaultCameraMode,allowedCameraModes:e.allowedCameraModes,autoPlay:e.autoPlayEnabled,uiColor:e.uiColor,uiOptions:e.uiOptions,fov:t,nearClip:e.minClipPlane||.1,farClip:e.maxClipPlane||1e3,playerHeight:e.playerHeight||1.6,cameraMovementSpeed:e.cameraMovementSpeed||1,cameraRotationSensitivity:e.cameraRotationSensitivity||.2,cameraDamping:e.cameraDamping||.75,invertCameraRotation:e.invertCameraRotation,scrollSpeed:e.scrollSpeed,scrollAmount:e.scrollAmount,scrollButtonMode:e.scrollButtonMode,transitionSpeed:e.transitionSpeed,autoplaySpeed:e.autoplaySpeed,loopMode:e.loopMode,additionalSplats:e.additionalSplats,keepMeshesInMemory:e.keepMeshesInMemory??!1,initialSplatExploreMode:e.initialSplatExploreMode,includeXR:e.includeXR,xrMode:e.xrMode,customScript:e.customScript,audioEmitters:e.audioEmitters||[]}}const r={tour:"Tour",explore:"Explore",hybrid:"Hybrid",walk:"Walk",orbit:"Orbit",next:"Next",previous:"Prev",startExperience:"Start Experience",returnToTour:"Return to Tour",fullscreen:"Fullscreen",mute:"Mute",unmute:"Unmute",helpTitle:"Controls & Help",percentageFormat:"{n}%"};function l(e,t){return e?.[t]||r[t]}function c(e="#4CAF50"){const t=document.getElementById("storysplat-viewer-styles");t&&t.remove();const o=document.createElement("style");return o.id="storysplat-viewer-styles",o.textContent=function(e="#4CAF50"){return`\n /* Container - CRITICAL: must be position relative and contained */\n .storysplat-viewer-container {\n position: relative;\n width: 100%;\n height: 100%;\n overflow: hidden;\n font-family: system-ui, -apple-system, sans-serif;\n box-sizing: border-box;\n }\n\n .storysplat-viewer-container * {\n box-sizing: border-box;\n }\n\n .storysplat-viewer-container canvas {\n position: absolute;\n top: 0;\n left: 0;\n width: 100% !important;\n height: 100% !important;\n display: block;\n touch-action: none;\n }\n\n /* Preloader - semi-transparent to see loading/reveal effect */\n .storysplat-preloader {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background-color: rgba(30, 30, 30, 0.85);\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n z-index: 100000;\n transition: opacity 0.5s ease-out;\n }\n\n .storysplat-preloader.hidden {\n opacity: 0;\n pointer-events: none;\n }\n\n .storysplat-preloader-content {\n display: flex;\n flex-direction: column;\n align-items: center;\n padding: 40px;\n gap: 20px;\n }\n\n .storysplat-preloader-media {\n display: flex;\n align-items: center;\n gap: 40px;\n }\n\n .storysplat-preloader-image {\n height: 200px;\n width: auto;\n object-fit: contain;\n }\n\n .storysplat-preloader-image-inverted {\n height: 200px;\n width: auto;\n object-fit: contain;\n filter: invert(1);\n margin-right: -100px;\n }\n\n .storysplat-preloader-lottie {\n height: 200px;\n width: 200px;\n }\n\n @media (max-width: 768px) {\n .storysplat-preloader-image,\n .storysplat-preloader-image-inverted {\n height: 100px;\n }\n .storysplat-preloader-image-inverted {\n margin-right: -50px;\n }\n .storysplat-preloader-lottie {\n height: 100px;\n width: 100px;\n }\n }\n\n .storysplat-preloader-progress {\n width: 200px;\n height: 4px;\n background: rgba(255, 255, 255, 0.1);\n border-radius: 2px;\n margin-top: 20px;\n position: relative;\n }\n\n .storysplat-preloader-bar {\n width: 0%;\n height: 100%;\n background: ${e};\n border-radius: 2px;\n transition: width 0.3s ease-out;\n }\n\n .storysplat-preloader-text {\n position: absolute;\n top: -25px;\n left: 50%;\n transform: translateX(-50%);\n color: white;\n font-size: 14px;\n white-space: nowrap;\n }\n\n /* Minimal Scroll Controls */\n .storysplat-scroll-controls {\n position: absolute;\n bottom: 0;\n left: 50%;\n transform: translateX(-50%);\n width: 150px;\n padding: 10px;\n display: flex;\n flex-direction: column;\n align-items: center;\n gap: 5px;\n z-index: 1000;\n }\n\n .storysplat-scroll-content {\n width: 100%;\n display: flex;\n flex-direction: column;\n align-items: center;\n gap: 5px;\n }\n\n .storysplat-progress-text {\n font-size: 14px;\n color: white;\n font-variant-numeric: tabular-nums;\n min-width: 40px;\n text-align: center;\n }\n\n .storysplat-progress-container {\n width: 90%;\n max-width: 150px;\n height: 3px;\n background: rgba(255, 255, 255, 0.2);\n border-radius: 2px;\n overflow: hidden;\n }\n\n .storysplat-progress-bar {\n height: 100%;\n background: ${e};\n transition: width 0.3s ease-out;\n width: 0%;\n }\n\n .storysplat-scroll-buttons {\n display: flex;\n justify-content: center;\n gap: 5px;\n z-index: 1000;\n }\n\n /* Minimal Button Style */\n .storysplat-btn {\n background: rgba(0, 0, 0, 0.3);\n border: none;\n color: white;\n padding: 4px 8px;\n font-size: 12px;\n border-radius: 3px;\n cursor: pointer;\n transition: background 0.2s;\n }\n\n .storysplat-btn:hover {\n background: rgba(0, 0, 0, 0.5);\n }\n\n .storysplat-btn-play {\n background: rgba(0, 0, 0, 0.3);\n border: none;\n color: white;\n padding: 4px 8px;\n border-radius: 3px;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n }\n\n .storysplat-btn-play svg {\n width: 12px;\n height: 12px;\n fill: white;\n }\n\n /* Mode Toggle - Minimal */\n .storysplat-mode-container {\n margin-top: 5px;\n }\n\n .storysplat-mode-toggle {\n display: flex;\n gap: 5px;\n }\n\n .storysplat-mode-btn {\n background: rgba(0, 0, 0, 0.3);\n border: none;\n color: white;\n padding: 3px 6px;\n font-size: 11px;\n border-radius: 2px;\n cursor: pointer;\n transition: all 0.2s ease;\n }\n\n .storysplat-mode-btn.selected {\n background: ${e} !important;\n }\n\n .storysplat-mode-btn:hover {\n background: rgba(0, 0, 0, 0.5);\n }\n\n /* Fullscreen Button - Minimal */\n .storysplat-fullscreen-btn {\n position: absolute;\n top: 10px;\n right: 10px;\n background: rgba(0, 0, 0, 0);\n border: none;\n padding: 5px;\n border-radius: 3px;\n cursor: pointer;\n z-index: 1000;\n }\n\n .storysplat-fullscreen-btn svg {\n width: 20px;\n height: 20px;\n fill: white;\n }\n\n /* Help Button - Minimal */\n .storysplat-help-btn {\n position: absolute;\n top: 10px;\n left: 10px;\n width: 30px;\n height: 30px;\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.3);\n color: white;\n border: none;\n cursor: pointer;\n font-size: 16px;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 1000;\n transition: all 0.2s ease;\n }\n\n .storysplat-help-btn:hover {\n background: rgba(0, 0, 0, 0.5);\n }\n\n .storysplat-help-panel {\n position: absolute;\n top: 50px;\n left: 10px;\n background: rgba(0, 0, 0, 0.8);\n color: white;\n padding: 15px;\n border-radius: 5px;\n max-width: 280px;\n z-index: 999;\n display: none;\n font-size: 12px;\n }\n\n .storysplat-help-panel.visible {\n display: block;\n }\n\n /* XR (VR/AR) Buttons - Minimal */\n .storysplat-xr-btn {\n position: absolute;\n top: 10px;\n right: 45px;\n background: rgba(0, 0, 0, 0.5);\n border: none;\n padding: 6px 10px;\n border-radius: 4px;\n cursor: pointer;\n z-index: 1000;\n color: white;\n font-size: 11px;\n font-weight: bold;\n text-transform: uppercase;\n transition: all 0.2s ease;\n display: none;\n }\n\n .storysplat-xr-btn.available {\n display: block;\n }\n\n .storysplat-xr-btn.active {\n background: ${e};\n }\n\n .storysplat-xr-btn:hover {\n background: rgba(0, 0, 0, 0.7);\n }\n\n .storysplat-xr-btn.active:hover {\n background: ${e};\n opacity: 0.9;\n }\n\n .storysplat-ar-btn {\n right: 85px;\n }\n\n .storysplat-help-panel h3 {\n margin: 0 0 10px 0;\n font-size: 14px;\n font-weight: 600;\n }\n\n .storysplat-help-panel p {\n margin: 5px 0;\n line-height: 1.4;\n }\n\n /* Waypoint Info - Top Banner Style (matching BabylonJS minimal export) */\n .storysplat-waypoint-info {\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n background: rgba(0, 0, 0, 0.5);\n color: white;\n text-align: center;\n z-index: 1000;\n pointer-events: none;\n display: none;\n }\n\n .storysplat-waypoint-info.hasContent {\n display: block;\n padding: 50px 30px 30px 30px;\n }\n\n .storysplat-waypoint-info h2 {\n margin: 0 0 8px 0;\n font-size: 18px;\n font-weight: bold;\n }\n\n .storysplat-waypoint-info p {\n margin: 0;\n font-size: 14px;\n line-height: 1.5;\n opacity: 0.9;\n }\n\n /* Responsive waypoint info */\n @media (max-height: 600px) {\n .storysplat-waypoint-info.hasContent {\n padding: 20px 20px 15px 20px;\n font-size: 13px;\n }\n }\n\n @media (max-height: 500px) {\n .storysplat-waypoint-info.hasContent {\n padding: 10px 15px 10px 15px;\n font-size: 12px;\n }\n }\n\n /* Hotspot Popup - Matching BabylonJS HTML export styles */\n /* Uses position: absolute so popup stays within container when embedded */\n .storysplat-hotspot-popup {\n position: absolute;\n background-color: rgba(0, 0, 0, 0.8);\n color: white;\n padding: 20px;\n border-radius: 10px;\n z-index: 100001;\n box-shadow: 0 0 10px rgba(0,0,0,0.5);\n display: none;\n font-size: 14px;\n flex-direction: column;\n font-family: system-ui, -apple-system, sans-serif;\n /* Default centered positioning */\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n max-width: 80%;\n max-height: 80%;\n overflow: auto;\n }\n\n .storysplat-hotspot-popup.visible {\n display: flex;\n }\n\n /* Fullscreen mode for click activation with image/iframe content */\n .storysplat-hotspot-popup.fullscreen {\n top: 0 !important;\n left: 0 !important;\n right: 0 !important;\n bottom: 0 !important;\n transform: none !important;\n width: 100% !important;\n height: 100% !important;\n max-width: 100% !important;\n max-height: 100% !important;\n margin: 0 !important;\n border-radius: 0 !important;\n display: flex !important;\n align-items: center !important;\n justify-content: center !important;\n flex-direction: column !important;\n padding: 0 !important;\n }\n\n .storysplat-hotspot-popup h2,\n .storysplat-hotspot-popup-title {\n margin: 0 0 10px 0;\n font-size: 18px;\n font-weight: 600;\n padding-right: 30px;\n }\n\n .storysplat-hotspot-popup p {\n margin: 0 0 10px 0;\n line-height: 1.5;\n font-size: 14px;\n white-space: pre-wrap;\n max-width: 500px;\n }\n\n .storysplat-hotspot-popup-content {\n max-width: 500px;\n }\n\n .storysplat-hotspot-popup.fullscreen .storysplat-hotspot-popup-content {\n max-width: none;\n }\n\n .storysplat-hotspot-popup img {\n max-width: 100%;\n height: auto;\n border-radius: 5px;\n margin-bottom: 10px;\n display: block;\n }\n\n .storysplat-hotspot-popup.fullscreen img {\n width: auto !important;\n height: auto !important;\n max-width: 90vw !important;\n max-height: 70vh !important;\n object-fit: contain !important;\n border-radius: 8px;\n margin: 20px 0;\n }\n\n .storysplat-hotspot-popup iframe {\n width: 100%;\n height: 400px;\n border: none;\n border-radius: 5px;\n margin-bottom: 10px;\n }\n\n .storysplat-hotspot-popup.fullscreen iframe {\n width: 90vw !important;\n max-width: 90vw !important;\n height: 70vh !important;\n max-height: 70vh !important;\n margin: 20px 0;\n border-radius: 8px;\n }\n\n /* Video container - matches HTML export sizing */\n .storysplat-hotspot-popup .video-container {\n display: flex;\n align-items: center;\n justify-content: center;\n width: 300px;\n max-width: 100%;\n height: auto;\n margin: 10px 0;\n }\n\n .storysplat-hotspot-popup video {\n max-width: 100%;\n max-height: 100%;\n object-fit: contain;\n border-radius: 5px;\n border: 2px solid rgba(255, 255, 255, 0.3);\n }\n\n /* Fullscreen popup video - matches HTML export (80% width, 50vh height) */\n .storysplat-hotspot-popup.fullscreen .video-container {\n width: 80% !important;\n max-width: 80% !important;\n height: 50vh !important;\n margin: 20px 0 !important;\n }\n\n .storysplat-hotspot-popup.fullscreen video {\n max-width: 100% !important;\n max-height: 100% !important;\n object-fit: contain !important;\n border-radius: 8px;\n }\n\n /* Close button - matching BabylonJS export (green button at bottom) */\n .storysplat-hotspot-popup-close {\n width: auto;\n padding: 10px 20px;\n background-color: #4CAF50;\n border: none;\n color: white;\n cursor: pointer;\n border-radius: 5px;\n margin: 10px 0 0 0;\n font-size: 14px;\n font-weight: 500;\n transition: background-color 0.2s;\n }\n\n .storysplat-hotspot-popup-close:hover {\n background-color: #45a049;\n }\n\n .storysplat-hotspot-popup-link {\n display: block;\n padding: 8px 16px;\n background: #007bff;\n color: white;\n text-decoration: none;\n border-radius: 4px;\n text-align: center;\n font-weight: 500;\n margin: 0 0 10px 0;\n cursor: pointer;\n transition: background-color 0.2s;\n }\n\n .storysplat-hotspot-popup-link:hover {\n background: #0056b3;\n }\n\n /* Mobile Responsive */\n @media (max-width: 768px) {\n .storysplat-scroll-controls {\n margin-bottom: 10px;\n }\n\n .storysplat-hotspot-popup.fullscreen img {\n max-width: 95vw !important;\n max-height: 60vh !important;\n margin: 10px 0;\n }\n\n .storysplat-hotspot-popup.fullscreen .video-container {\n width: 95vw !important;\n max-width: 95vw !important;\n height: 45vh !important;\n margin: 10px 0 !important;\n }\n\n .storysplat-hotspot-popup.fullscreen iframe {\n width: 95vw !important;\n max-width: 95vw !important;\n height: 60vh !important;\n max-height: 60vh !important;\n margin: 10px 0;\n }\n\n .storysplat-hotspot-popup-title {\n font-size: 16px !important;\n padding: 10px 15px !important;\n }\n }\n\n @media (max-width: 540px) {\n .storysplat-scroll-controls {\n margin-bottom: 45px;\n }\n\n .storysplat-hotspot-popup.fullscreen img {\n max-width: 98vw !important;\n max-height: 55vh !important;\n }\n\n .storysplat-hotspot-popup.fullscreen .video-container {\n width: 98vw !important;\n max-width: 98vw !important;\n height: 40vh !important;\n }\n\n .storysplat-hotspot-popup.fullscreen iframe {\n width: 98vw !important;\n max-width: 98vw !important;\n height: 55vh !important;\n max-height: 55vh !important;\n }\n }\n\n /* Virtual Joystick Overlay - visual indicator only, touches pass through to canvas */\n .storysplat-joystick-container {\n position: absolute;\n bottom: 80px;\n left: 40px;\n width: 120px;\n height: 120px;\n z-index: 2000;\n pointer-events: none;\n touch-action: none;\n display: none;\n }\n\n .storysplat-joystick-container.visible {\n display: block;\n }\n\n .storysplat-joystick-base {\n position: absolute;\n top: 0;\n left: 0;\n width: 120px;\n height: 120px;\n background: rgba(255, 255, 255, 0.2);\n border: 3px solid rgba(255, 255, 255, 0.4);\n border-radius: 50%;\n pointer-events: none;\n }\n\n .storysplat-joystick-thumb {\n position: absolute;\n top: 50%;\n left: 50%;\n width: 50px;\n height: 50px;\n margin-left: -25px;\n margin-top: -25px;\n background: rgba(255, 255, 255, 0.6);\n border: 3px solid rgba(255, 255, 255, 0.8);\n border-radius: 50%;\n pointer-events: none;\n transition: transform 0.05s ease-out;\n }\n\n .storysplat-joystick-thumb.active {\n background: rgba(255, 255, 255, 0.8);\n border-color: white;\n }\n\n /* Look Zone Indicator (right side) */\n .storysplat-look-zone {\n position: absolute;\n bottom: 80px;\n right: 40px;\n width: 100px;\n height: 100px;\n z-index: 1000;\n pointer-events: none;\n display: none;\n }\n\n .storysplat-look-zone.visible {\n display: flex;\n align-items: center;\n justify-content: center;\n }\n\n .storysplat-look-zone-icon {\n width: 60px;\n height: 60px;\n background: rgba(255, 255, 255, 0.15);\n border: 2px solid rgba(255, 255, 255, 0.25);\n border-radius: 50%;\n display: flex;\n align-items: center;\n justify-content: center;\n }\n\n .storysplat-look-zone-icon svg {\n width: 30px;\n height: 30px;\n fill: rgba(255, 255, 255, 0.5);\n }\n\n /* Look Zone Active State */\n .storysplat-look-zone.active .storysplat-look-zone-icon {\n background: rgba(255, 255, 255, 0.25);\n border-color: rgba(255, 255, 255, 0.5);\n }\n\n .storysplat-look-zone.active .storysplat-look-zone-icon svg {\n fill: rgba(255, 255, 255, 0.8);\n }\n\n /* Camera Mode Toggle (Orbit/Fly) - Mobile only in explore mode */\n .storysplat-camera-mode-toggle {\n position: absolute;\n bottom: 210px;\n left: 40px;\n width: 44px;\n height: 44px;\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.5);\n border: 2px solid rgba(255, 255, 255, 0.3);\n cursor: pointer;\n z-index: 2001;\n display: none;\n align-items: center;\n justify-content: center;\n touch-action: manipulation;\n -webkit-tap-highlight-color: transparent;\n }\n\n .storysplat-camera-mode-toggle.visible {\n display: flex;\n }\n\n .storysplat-camera-mode-toggle:active {\n background: rgba(0, 0, 0, 0.7);\n transform: scale(0.95);\n }\n\n .storysplat-camera-mode-toggle svg {\n width: 24px;\n height: 24px;\n fill: white;\n }\n\n .storysplat-camera-mode-toggle .orbit-icon,\n .storysplat-camera-mode-toggle .fly-icon {\n display: none;\n }\n\n .storysplat-camera-mode-toggle.orbit .orbit-icon {\n display: block;\n }\n\n .storysplat-camera-mode-toggle.fly .fly-icon {\n display: block;\n }\n\n /* Return to Waypoint Button - Explore mode only */\n .storysplat-return-waypoint-btn {\n position: absolute;\n bottom: 20px;\n left: 20px;\n padding: 10px 16px;\n background: rgba(0, 0, 0, 0.6);\n border: 1px solid rgba(255, 255, 255, 0.3);\n border-radius: 8px;\n color: white;\n font-size: 13px;\n font-weight: 500;\n cursor: pointer;\n z-index: 2001;\n display: none;\n align-items: center;\n gap: 8px;\n transition: all 0.2s ease;\n backdrop-filter: blur(4px);\n -webkit-backdrop-filter: blur(4px);\n }\n\n .storysplat-return-waypoint-btn.visible {\n display: flex;\n }\n\n .storysplat-return-waypoint-btn:hover {\n background: rgba(0, 0, 0, 0.8);\n border-color: rgba(255, 255, 255, 0.5);\n }\n\n .storysplat-return-waypoint-btn:active {\n transform: scale(0.98);\n }\n\n .storysplat-return-waypoint-btn svg {\n width: 16px;\n height: 16px;\n fill: white;\n }\n\n @media (max-width: 768px) {\n .storysplat-return-waypoint-btn {\n bottom: auto;\n top: 60px;\n left: 10px;\n padding: 8px 12px;\n font-size: 12px;\n }\n }\n\n /* Lazy Load Container */\n .storysplat-lazy-load-container {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n display: flex;\n justify-content: center;\n align-items: center;\n background: #1a1a1a;\n z-index: 10001;\n }\n\n .storysplat-lazy-load-thumbnail {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n object-fit: cover;\n opacity: 0.7;\n }\n\n .storysplat-lazy-load-thumbnail-video {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n object-fit: cover;\n opacity: 0.7;\n }\n\n .storysplat-lazy-load-overlay {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: linear-gradient(\n to bottom,\n rgba(0, 0, 0, 0.3) 0%,\n rgba(0, 0, 0, 0.5) 50%,\n rgba(0, 0, 0, 0.7) 100%\n );\n }\n\n .storysplat-lazy-load-content {\n position: relative;\n z-index: 1;\n text-align: center;\n display: flex;\n flex-direction: column;\n align-items: center;\n gap: 16px;\n }\n\n .storysplat-lazy-load-start-btn {\n padding: 16px 32px;\n background: ${e};\n border: none;\n border-radius: 50px;\n color: white;\n font-size: 18px;\n font-weight: 600;\n cursor: pointer;\n display: flex;\n align-items: center;\n gap: 12px;\n transition: all 0.3s ease;\n box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);\n }\n\n .storysplat-lazy-load-start-btn:hover {\n transform: scale(1.05);\n box-shadow: 0 6px 30px rgba(0, 0, 0, 0.4);\n }\n\n .storysplat-lazy-load-start-btn:active {\n transform: scale(0.98);\n }\n\n .storysplat-lazy-load-start-btn svg {\n width: 24px;\n height: 24px;\n fill: white;\n }\n\n @media (max-width: 768px) {\n .storysplat-lazy-load-start-btn {\n padding: 12px 24px;\n font-size: 16px;\n }\n }\n\n /* Watermark - matches BabylonJS HTML export */\n .storysplat-watermark {\n position: fixed;\n bottom: 10px;\n right: 10px;\n background-color: rgba(0, 0, 0, 0.5);\n color: white;\n padding: 5px 10px;\n border-radius: 5px;\n font-size: 12px;\n z-index: 1000;\n pointer-events: auto; /* Allow pointer events for the entire watermark so links work */\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;\n cursor: default;\n }\n\n .storysplat-watermark a {\n color: ${e};\n text-decoration: none;\n cursor: pointer;\n }\n\n .storysplat-watermark a:hover {\n text-decoration: underline;\n }\n `}(e),document.head.appendChild(o),o}function d(e,t){const o=document.createElement("div");o.className="storysplat-preloader";const n=!!t;return o.innerHTML=`\n <div class="storysplat-preloader-content">\n <div class="storysplat-preloader-media">\n ${n?`<img class="storysplat-preloader-image" src="${t}" alt="Custom Logo" />`:'<img class="storysplat-preloader-image-inverted" src="https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fimages%2FStorySplat.webp?alt=media&token=953e8ab3-1865-4ac1-a98d-b548b7066bda" alt="StorySplat Logo" />'}\n ${n?"":'<lottie-player class="storysplat-preloader-lottie"\n src="https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Flotties%2FstorySplatLottie.json?alt=media&token=d7edc19d-9cb8-4c6e-a94c-cba1d2b65d5e"\n background="transparent"\n speed="1"\n loop\n autoplay>\n </lottie-player>'}\n </div>\n <div class="storysplat-preloader-progress">\n <div class="storysplat-preloader-text">Loading... 0%</div>\n <div class="storysplat-preloader-bar"></div>\n </div>\n </div>\n `,e.appendChild(o),n||new Promise(e=>{if(customElements.get("lottie-player"))return void e();const t=document.querySelector('script[src*="lottie-player"]');if(t)return void t.addEventListener("load",()=>e());const o=document.createElement("script");o.src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js",o.onload=()=>e(),document.head.appendChild(o)}),o}function p(e){e.classList.add("hidden"),setTimeout(()=>e.remove(),500)}function h(e,t,o="tour",n){const i=e.scrollControls?.querySelector(".storysplat-btn-prev"),a=e.scrollControls?.querySelector(".storysplat-btn-next"),s=e.scrollControls?.querySelector(".storysplat-btn-play");if(i&&i.addEventListener("click",()=>t.prevWaypoint()),a&&a.addEventListener("click",()=>t.nextWaypoint()),s){const e=e=>{s.innerHTML=e?'<svg viewBox="0 0 24 24"><path d="M6 4h4v16H6zm8 0h4v16h-4z"/></svg>':'<svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>'};s.addEventListener("click",()=>{t.isPlaying()?t.pause():t.play()}),t.on("playbackStart",()=>e(!0)),t.on("playbackStop",()=>e(!1)),e(t.isPlaying())}if(e.helpButton&&e.helpPanel&&e.helpButton.addEventListener("click",()=>{e.helpPanel.classList.toggle("visible")}),e.fullscreenButton){if(/iPad|iPhone|iPod/.test(navigator.userAgent)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1)e.fullscreenButton.style.display="none",console.log("[StorySplat Viewer] Fullscreen button hidden on iOS (API not supported)");else{const t=e.fullscreenButton.parentElement;e.fullscreenButton.addEventListener("click",()=>{const o=document;if(o.fullscreenElement||o.webkitFullscreenElement){o.exitFullscreen?o.exitFullscreen():o.webkitExitFullscreen&&o.webkitExitFullscreen();const t=e.fullscreenButton.querySelector(".storysplat-expand-icon"),n=e.fullscreenButton.querySelector(".storysplat-compress-icon");t&&(t.style.display="block"),n&&(n.style.display="none")}else{t?.requestFullscreen?t.requestFullscreen():t?.webkitRequestFullscreen&&t.webkitRequestFullscreen();const o=e.fullscreenButton.querySelector(".storysplat-expand-icon"),n=e.fullscreenButton.querySelector(".storysplat-compress-icon");o&&(o.style.display="none"),n&&(n.style.display="block")}});const o=()=>{const t=document,o=t.fullscreenElement||t.webkitFullscreenElement,n=e.fullscreenButton.querySelector(".storysplat-expand-icon"),i=e.fullscreenButton.querySelector(".storysplat-compress-icon");n&&(n.style.display=o?"none":"block"),i&&(i.style.display=o?"block":"none")};document.addEventListener("fullscreenchange",o),document.addEventListener("webkitfullscreenchange",o)}}const l=t=>{const o=e.scrollControls?.querySelector(".storysplat-progress-text"),n=e.scrollControls?.querySelector(".storysplat-progress-container"),i=e.scrollControls?.querySelector(".storysplat-scroll-buttons"),a=t?"":"none";o&&(o.style.display=a),n&&(n.style.display=a),i&&(i.style.display=a)};if(l("tour"===o),t.setCameraMode){const o=e.scrollControls?.querySelectorAll(".storysplat-mode-btn");o?.forEach(e=>{e.addEventListener("click",()=>{const n=e.getAttribute("data-mode");n&&t.setCameraMode&&(t.setCameraMode(n),o.forEach(e=>e.classList.remove("selected")),e.classList.add("selected"),l("tour"===n))})}),t.on("modeChange",({mode:e})=>{l("tour"===e),o?.forEach(t=>{const o=t.getAttribute("data-mode");t.classList.toggle("selected",o===e)})})}let c=0,d=-1;t.on("progressUpdate",({progress:t})=>{const o=100*Math.max(0,Math.min(1,t)),i=Math.round(o),a=performance.now();var s,l;a-c<33||(c=a,e.progressBar&&(e.progressBar.style.width=`${o}%`),e.progressText&&i!==d&&(d=i,e.progressText.innerHTML="",e.progressText.textContent=(s=n,l=i,(s?.percentageFormat||r.percentageFormat).replace("{n}",String(l)))))});let p=-1;t.on("waypointChange",({index:o,waypoint:n})=>{if(o===p)return;if(p=o,!e.waypointInfo)return;const i=e.waypointInfo.querySelector(".storysplat-waypoint-title"),a=e.waypointInfo.querySelector(".storysplat-waypoint-description");if(!i||!a)return;const s=n||t.getWaypoints?.()[o];s&&(s.name||s.info)?(i.textContent=s.name||"",a.textContent=s.info||"",s.name||s.info?e.waypointInfo.classList.add("hasContent"):e.waypointInfo.classList.remove("hasContent")):(i.textContent="",a.textContent="",e.waypointInfo.classList.remove("hasContent"))})}function u(e,t){const o=e.querySelector(".storysplat-hotspot-popup");if(!o)return;const n=o.querySelector(".storysplat-hotspot-popup-title"),i=o.querySelector(".storysplat-hotspot-popup-content"),a=o.querySelector(".storysplat-hotspot-popup-close");o.style.cssText="",o.classList.remove("fullscreen");const s=t.activationMode||"click",r=t.photoUrl||t.popupVideoUrl||"iframe"===t.contentType&&t.iframeUrl;"click"===s&&r&&o.classList.add("fullscreen"),t.backgroundColor&&(o.style.backgroundColor=t.backgroundColor),t.textColor&&(o.style.color=t.textColor,n&&(n.style.color=t.textColor)),t.fontFamily&&(o.style.fontFamily=t.fontFamily),t.fontSize&&(o.style.fontSize=`${t.fontSize}px`),a&&t.closeButtonColor&&(a.style.backgroundColor=t.closeButtonColor),n&&(n.textContent=t.title||"Hotspot");let l="";if("iframe"===t.contentType&&t.iframeUrl&&(l+=`<iframe src="${t.iframeUrl}" title="${t.title||"Embedded content"}"></iframe>`),t.popupVideoUrl&&(l+=`<div class="video-container"><video src="${t.popupVideoUrl}" controls playsinline webkit-playsinline preload="metadata"></video></div>`),t.photoUrl&&(l+=`<img src="${t.photoUrl}" alt="${t.title||"Hotspot image"}" />`),t.information&&(l+=`<p>${t.information}</p>`),t.externalLinkUrl){const e=t.externalLinkButtonColor||"#007bff";l+=`\n <div onclick="window.open('${t.externalLinkUrl}', '_blank', 'noopener,noreferrer')"\n class="storysplat-hotspot-popup-link" style="background-color: ${e}">\n ${t.externalLinkText||"Open External Link"}\n </div>\n `}i&&(i.innerHTML=l),o.classList.add("visible")}function m(e,t){e.joystick&&(t?e.joystick.classList.add("visible"):e.joystick.classList.remove("visible")),e.lookZone&&(t?e.lookZone.classList.add("visible"):e.lookZone.classList.remove("visible"))}function g(e,t,o,n,i){if(e.joystickThumb)if(t){e.joystickThumb.classList.add("active");const t=Math.sqrt(o*o+n*n),a=Math.min(t,i),s=t>0?a/t:0,r=o*s,l=n*s;e.joystickThumb.style.transform=`translate(${r}px, ${l}px)`}else e.joystickThumb.classList.remove("active"),e.joystickThumb.style.transform="translate(0, 0)"}function y(e,t){e.lookZone&&(t?e.lookZone.classList.add("active"):e.lookZone.classList.remove("active"))}function f(e,t){e.cameraModeToggle&&(t?e.cameraModeToggle.classList.add("visible"):e.cameraModeToggle.classList.remove("visible"))}function v(e,t){e.cameraModeToggle&&(e.cameraModeToggle.classList.remove("orbit","fly"),e.cameraModeToggle.classList.add(t))}function b(e,t){e.returnWaypointButton&&(t?e.returnWaypointButton.classList.add("visible"):e.returnWaypointButton.classList.remove("visible"))}const w=new e.Vec3,x=new e.Vec3,S=new e.Pose,E=new e.InputFrame({move:[0,0,0],rotate:[0,0,0]}),M=(e,t,o)=>{const n=Math.sqrt(e[0]*e[0]+e[1]*e[1]);if(n<t)return void e.fill(0);const i=(n-t)/(o-t);e[0]*=i/n,e[1]*=i/n},C=(t,o,n,i,a=new e.Vec3)=>{const{fov:s,aspectRatio:r,horizontalFov:l,projection:c,orthoHeight:d}=t,p=t.system?.app,{width:h,height:u}=p?.graphicsDevice?.clientRect||{width:1920,height:1080};a.set(-o/h*2,n/u*2,0);const m=x.set(0,0,0);if(c===e.PROJECTION_PERSPECTIVE){const t=i*Math.tan(.5*s*e.math.DEG_TO_RAD);l?m.set(t,t/r,0):m.set(t*r,t,0)}else m.set(d*r,d,0);return a.mul(m),a};class _{constructor(t,o,n={}){this.enabled=!0,this._mode="orbit",this._enableOrbit=!0,this._enableFly=!0,this.enablePan=!0,this._pose=new e.Pose,this._startZoomDist=0,this._pitchRange=new e.Vec2(-89,89),this._yawRange=new e.Vec2(-1/0,1/0),this._lastFocusPoint=new e.Vec3(0,0,0),this._zoomRange=new e.Vec2(.01,1/0),this._state={axis:new e.Vec3,shift:0,ctrl:0,mouse:[0,0,0],touches:0},this.moveSpeed=25,this.moveFastSpeed=50,this.moveSlowSpeed=10,this.rotateSpeed=.05,this.rotateTouchSens=.1,this.rotateJoystickSens=1,this.zoomSpeed=.001,this.zoomPinchSens=5,this.keyboardSpeedMultiplier=1.5,this.gamepadDeadZone=new e.Vec2(.3,.6),this.invertRotation=!1,this.joystickEventName="joystick",this._destroyHandler=null,this.camera=t,this.app=o;const i=t.camera;if(!i)throw new Error("CameraControls: camera component not found on entity");this.cameraComponent=i,this._flyController=new e.FlyController,this._orbitController=new e.OrbitController,this._focusController=new e.FocusController,this._flyController.moveDamping=.75,this._flyController.rotateDamping=.75,this._orbitController.rotateDamping=.75,this._orbitController.zoomDamping=.8,this._orbitController.zoomRange=new e.Vec2(.01,1/0);const a=o.graphicsDevice.canvas;this._desktopInput=new e.KeyboardMouseSource,this._orbitMobileInput=new e.MultiTouchSource,this._flyMobileInput=new e.DualGestureSource,this._gamepadInput=new e.GamepadSource,this._desktopInput.attach(a),this._orbitMobileInput.attach(a),this._flyMobileInput.attach(a),this._gamepadInput.attach(a),this._flyMobileInput.on("joystick:position:left",([e,t,o,n])=>{"fly"===this._mode&&this.app.fire(`${this.joystickEventName}:left`,e,t,o,n)}),this._flyMobileInput.on("joystick:position:right",([e,t,o,n])=>{"fly"===this._mode&&this.app.fire(`${this.joystickEventName}:right`,e,t,o,n)}),this._pose.look(this.camera.getPosition(),e.Vec3.ZERO),this._setMode("orbit"),this._controller=this._orbitController,void 0!==n.enableOrbit&&(this.enableOrbit=n.enableOrbit),void 0!==n.enableFly&&(this.enableFly=n.enableFly),void 0!==n.enablePan&&(this.enablePan=n.enablePan),n.focusPoint&&(this.focusPoint=n.focusPoint),void 0!==n.moveSpeed&&(this.moveSpeed=n.moveSpeed),void 0!==n.moveFastSpeed&&(this.moveFastSpeed=n.moveFastSpeed),void 0!==n.moveSlowSpeed&&(this.moveSlowSpeed=n.moveSlowSpeed),void 0!==n.rotateSpeed&&(this.rotateSpeed=n.rotateSpeed),void 0!==n.rotateTouchSens&&(this.rotateTouchSens=n.rotateTouchSens),void 0!==n.rotateJoystickSens&&(this.rotateJoystickSens=n.rotateJoystickSens),void 0!==n.zoomSpeed&&(this.zoomSpeed=n.zoomSpeed),void 0!==n.zoomPinchSens&&(this.zoomPinchSens=n.zoomPinchSens),void 0!==n.focusDamping&&(this.focusDamping=n.focusDamping),void 0!==n.rotateDamping&&(this.rotateDamping=n.rotateDamping),void 0!==n.moveDamping&&(this.moveDamping=n.moveDamping),void 0!==n.zoomDamping&&(this.zoomDamping=n.zoomDamping),n.pitchRange&&(this.pitchRange=n.pitchRange),n.yawRange&&(this.yawRange=n.yawRange),n.zoomRange&&(this.zoomRange=n.zoomRange),n.gamepadDeadZone&&(this.gamepadDeadZone=n.gamepadDeadZone),n.mobileInputLayout&&(this.mobileInputLayout=n.mobileInputLayout),void 0!==n.invertRotation&&(this.invertRotation=n.invertRotation)}set enableFly(e){this._enableFly=e,this._enableFly||"fly"!==this._mode||this._setMode("orbit")}get enableFly(){return this._enableFly}set enableOrbit(e){this._enableOrbit=e,this._enableOrbit||"orbit"!==this._mode||this._setMode("fly")}get enableOrbit(){return this._enableOrbit}set focusDamping(e){this._focusController.focusDamping=e}get focusDamping(){return this._focusController.focusDamping}set moveDamping(e){this._flyController.moveDamping=e}get moveDamping(){return this._flyController.moveDamping}set rotateDamping(e){this._flyController.rotateDamping=e,this._orbitController.rotateDamping=e}get rotateDamping(){return this._orbitController.rotateDamping}set zoomDamping(e){this._orbitController.zoomDamping=e}get zoomDamping(){return this._orbitController.zoomDamping}set focusPoint(e){const t=this.camera.getPosition();this._startZoomDist=t.distance(e),this._controller.attach(this._pose.look(t,e),!1)}get focusPoint(){return this._pose.getFocus(w)}set pitchRange(e){this._pitchRange.copy(e),this._flyController.pitchRange=this._pitchRange,this._orbitController.pitchRange=this._pitchRange}get pitchRange(){return this._pitchRange}set yawRange(t){this._yawRange.x=e.math.clamp(t.x,-360,360),this._yawRange.y=e.math.clamp(t.y,-360,360),this._flyController.yawRange=this._yawRange,this._orbitController.yawRange=this._yawRange}get yawRange(){return this._yawRange}set zoomRange(e){this._zoomRange.x=e.x,this._zoomRange.y=e.y<=e.x?1/0:e.y,this._orbitController.zoomRange=this._zoomRange}get zoomRange(){return this._zoomRange}set mobileInputLayout(e){/(?:joystick|touch)-(?:joystick|touch)/.test(e)?this._flyMobileInput.layout=e:console.warn(`CameraControls: invalid mobile input layout: ${e}`)}get mobileInputLayout(){return this._flyMobileInput.layout}get mode(){return this._mode}_setMode(e){if(this._enableFly&&!this._enableOrbit)e="fly";else if(!this._enableFly&&this._enableOrbit)e="orbit";else if(!this._enableFly&&!this._enableOrbit)return void console.warn("CameraControls: both fly and orbit modes are disabled");const t=this._mode;if(t!==e){switch(this._mode=e,this._controller&&this._controller.detach(),this._mode){case"orbit":if(this._controller=this._orbitController,"focus"===t){const e=this.camera.getPosition();this._pose.look(e,this._lastFocusPoint)}this._lastYaw=this._pose.angles.y;break;case"fly":this._controller=this._flyController;break;case"focus":this._controller=this._focusController}this._controller.attach(this._pose,!1),this.app.fire("cameracontrols:modechange",this._mode)}}setMode(e){this._setMode(e)}focus(e,t=!1){this._lastFocusPoint.copy(e),this._setMode("focus");const o=t?this._startZoomDist:this.camera.getPosition().distance(e);this._startZoomDist=o;const n=w.copy(this.camera.forward).mulScalar(-o).add(e);this._controller.attach(S.look(n,e))}look(e,t=!1){this._setMode("focus");const o=t?w.copy(this.camera.getPosition()).sub(e).normalize().mulScalar(this._startZoomDist).add(e):this.camera.getPosition();this._controller.attach(S.look(o,e))}reset(e,t){this._setMode("focus"),this._controller.attach(S.look(t,e))}syncFromCamera(e){const t=this.camera.getPosition().clone();if(e){this._lastFocusPoint.copy(e);const o=t.distance(e);this._startZoomDist=o,this._pose.distance=o,this._pose.look(t,e);let n=this._pose.angles.y;for(;n>180;)n-=360;for(;n<-180;)n+=360;this._pose.angles.y=n,this._lastYaw=n,this._pose.angles.x=Math.max(-89,Math.min(89,this._pose.angles.x)),this._pose.angles.z=0}else{const e=this.camera.getEulerAngles();this._pose.position.copy(t),this._pose.angles.x=e.x,this._pose.angles.y=e.y,this._pose.angles.z=0;let o=this._pose.angles.y;for(;o>180;)o-=360;for(;o<-180;)o+=360;this._pose.angles.y=o,this._lastYaw=o,this._pose.angles.x=Math.max(-89,Math.min(89,this._pose.angles.x));const n=this.camera.forward.clone();this._lastFocusPoint.copy(t).add(n.mulScalar(10));const i=10;this._startZoomDist=i,this._pose.distance=i}this._controller.attach(this._pose,!1)}syncFromPose(t,o,n){this.camera.setPosition(t),this.camera.setRotation(o);const i=new e.Entity;i.setRotation(o);const a=i.getEulerAngles();this._pose.position.copy(t),this._pose.angles.x=a.x,this._pose.angles.y=a.y,this._pose.angles.z=0;let s=this._pose.angles.y;for(;s>180;)s-=360;for(;s<-180;)s+=360;if(this._pose.angles.y=s,this._lastYaw=s,this._pose.angles.x=Math.max(-89,Math.min(89,this._pose.angles.x)),n)this._lastFocusPoint.copy(n);else{const e=this.camera.forward.clone();this._lastFocusPoint.copy(t).add(e.mulScalar(10))}const r=t.distance(this._lastFocusPoint);this._startZoomDist=r,this._pose.distance=r,this._controller.attach(this._pose,!1)}enable(){this.enabled=!0}disable(){this.enabled=!1,this._desktopInput.read(),this._orbitMobileInput.read(),this._flyMobileInput.read(),this._gamepadInput.read()}update(t){if(!this.enabled)return;const{keyCode:o}=e.KeyboardMouseSource,{key:n,button:i,mouse:a,wheel:s}=this._desktopInput.read(),{touch:r,pinch:l,count:c}=this._orbitMobileInput.read(),{leftInput:d,rightInput:p}=this._flyMobileInput.read(),{leftStick:h,rightStick:u}=this._gamepadInput.read();M(h,this.gamepadDeadZone.x,this.gamepadDeadZone.y),M(u,this.gamepadDeadZone.x,this.gamepadDeadZone.y),this._state.axis.add(w.set(n[o.D]-n[o.A]+(n[o.RIGHT]-n[o.LEFT]),n[o.E]-n[o.Q],n[o.W]-n[o.S]+(n[o.UP]-n[o.DOWN])));for(let e=0;e<this._state.mouse.length;e++)this._state.mouse[e]+=i[e];this._state.shift+=n[o.SHIFT],this._state.ctrl+=n[o.CTRL],this._state.touches+=c[0],1===i[0]||1===i[1]||0!==s[0]?this._setMode("orbit"):(1===i[2]||this._state.axis.length()>0)&&this._setMode("fly");const m=+("orbit"===this._mode),g=+("fly"===this._mode),y=+(this._state.touches>1),f=+(this._state.shift||this._state.mouse[1]),v=+this._flyMobileInput.layout.endsWith("joystick"),b=(this._state.shift?this.moveFastSpeed:this._state.ctrl?this.moveSlowSpeed:this.moveSpeed)*t,S=60*this.zoomSpeed*t,_=S*this.zoomPinchSens,L=60*this.rotateSpeed*t,T=L*this.rotateTouchSens,k=this.rotateSpeed*this.rotateJoystickSens*60*t,{deltas:A}=E,P=w.set(0,0,0),z=this._state.axis.clone().normalize();P.add(z.mulScalar(g*b*this.keyboardSpeedMultiplier));const R=C(this.cameraComponent,a[0],a[1],this._pose.distance);P.add(R.mulScalar(m*f*+this.enablePan));const I=x.set(0,0,s[0]);P.add(I.mulScalar(m*S)),A.move.append([P.x,P.y,P.z]),P.set(0,0,0);const D=x.set(a[0],a[1],0);P.add(D.mulScalar((1-m*f)*L)),this.invertRotation&&(P.y=-P.y),A.rotate.append([P.x,P.y,P.z]),P.set(0,0,0);const F=x.set(d[0],0,-d[1]);P.add(F.mulScalar(g*b));const U=C(this.cameraComponent,r[0],r[1],this._pose.distance);P.add(U.mulScalar(m*y*+this.enablePan));const B=x.set(0,0,l[0]);P.add(B.mulScalar(m*y*_)),A.move.append([P.x,P.y,P.z]),P.set(0,0,0);const V=x.set(r[0],r[1],0);P.add(V.mulScalar(m*(1-y)*T));const $=x.set(p[0],p[1],0);P.add($.mulScalar(g*(v?k:T))),this.invertRotation&&(P.y=-P.y),A.rotate.append([P.x,P.y,P.z]),P.set(0,0,0);const H=x.set(h[0],0,-h[1]);P.add(H.mulScalar(g*b)),A.move.append([P.x,P.y,P.z]),P.set(0,0,0);const O=x.set(u[0],u[1],0);if(P.add(O.mulScalar(g*k)),this.invertRotation&&(P.y=-P.y),A.rotate.append([P.x,P.y,P.z]),this.app.xr?.active)E.read();else{if("focus"===this._mode){const e=A.move.length()+A.rotate.length()>0,t=this._focusController.complete?.()??!1;(e||t)&&this._setMode("orbit")}if(this._pose.copy(this._controller.update(E,t)),"orbit"===this._mode){let e=this._pose.angles.y;for(;e>180;)e-=360;for(;e<-180;)e+=360;if(void 0!==this._lastYaw){const t=e-this._lastYaw;t>180?e-=360:t<-180&&(e+=360)}this._pose.angles.y=e,this._lastYaw=e}this.camera.setPosition(this._pose.position),this.camera.setEulerAngles(this._pose.angles)}}destroy(){this._desktopInput.destroy(),this._orbitMobileInput.destroy(),this._flyMobileInput.destroy(),this._gamepadInput.destroy(),this._flyController.destroy(),this._orbitController.destroy()}}class L{constructor(t,o,n={}){this.enabled=!1,this.velocity=new e.Vec3,this.isGrounded=!1,this.yaw=0,this.pitch=0,this.keys={},this.mouseLocked=!1,this.moveSpeed=8,this.sprintMultiplier=2,this.lookSensitivity=.002,this.playerHeight=1.6,this.gravity=20,this.maxFallSpeed=50,this.jumpVelocity=8,this.collisionRadius=.3,this.stepHeight=.3,this.groundCheckDistance=.1,this.moveDamping=.9,this.horizontalVelocity=new e.Vec3,this.targetVelocity=new e.Vec3,this.collisionEntities=[],this.floorEntity=null,this.keydownHandler=null,this.keyupHandler=null,this.mousemoveHandler=null,this.clickHandler=null,this.pointerlockchangeHandler=null,this.tmpVec=new e.Vec3,this.tmpVec2=new e.Vec3,this.forward=new e.Vec3,this.right=new e.Vec3,this.camera=t,this.app=o,void 0!==n.moveSpeed&&(this.moveSpeed=n.moveSpeed),void 0!==n.sprintMultiplier&&(this.sprintMultiplier=n.sprintMultiplier),void 0!==n.lookSensitivity&&(this.lookSensitivity=n.lookSensitivity),void 0!==n.playerHeight&&(this.playerHeight=n.playerHeight),void 0!==n.gravity&&(this.gravity=n.gravity),void 0!==n.maxFallSpeed&&(this.maxFallSpeed=n.maxFallSpeed),void 0!==n.jumpVelocity&&(this.jumpVelocity=n.jumpVelocity),void 0!==n.collisionRadius&&(this.collisionRadius=n.collisionRadius),void 0!==n.stepHeight&&(this.stepHeight=n.stepHeight),void 0!==n.groundCheckDistance&&(this.groundCheckDistance=n.groundCheckDistance),void 0!==n.moveDamping&&(this.moveDamping=n.moveDamping);const i=this.camera.getEulerAngles();this.pitch=i.x,this.yaw=i.y}async createCollisionMeshes(t){if(!t||0===t.length)return;console.log("[CharacterController] Creating collision meshes:",t.length);const o=[];t.forEach((t,n)=>{if("custom"===t.meshType&&t.customMeshUrl){const e=this.loadCustomCollisionMesh(t,n);return void o.push(e)}let i=null;switch(t.meshType){case"cube":i=new e.Entity(`collision-cube-${n}`),i.addComponent("render",{type:"box"});break;case"sphere":i=new e.Entity(`collision-sphere-${n}`),i.addComponent("render",{type:"sphere"});break;case"floor":i=new e.Entity(`collision-floor-${n}`),i.addComponent("render",{type:"plane"}),this.floorEntity=i;break;default:i=new e.Entity(`collision-plane-${n}`),i.addComponent("render",{type:"plane"})}i&&this.configureCollisionEntity(i,t)}),o.length>0&&await Promise.all(o),console.log("[CharacterController] Created",this.collisionEntities.length,"collision entities")}async loadCustomCollisionMesh(t,o){const n=t.customMeshUrl;console.log("[CharacterController] Loading custom collision mesh:",n);try{const i=n.split("?")[0].split(".").pop()?.toLowerCase()||"glb",a="gltf"===i||"glb"===i?"container":"model",s=new e.Asset(`collision-custom-${o}`,a,{url:n});await new Promise((i,r)=>{s.ready(()=>{try{const n=new e.Entity(`collision-custom-${o}`);if("container"===a){const e=s.resource;if(e&&e.instantiateRenderEntity){const t=e.instantiateRenderEntity();for(;t.children.length>0;)n.addChild(t.children[0]);t.destroy()}}else n.addComponent("model",{asset:s});this.configureCollisionEntity(n,t),this.computeAndStoreBounds(n),i()}catch(e){console.error("[CharacterController] Error setting up custom mesh:",e),r(e)}}),s.on("error",e=>{console.error("[CharacterController] Error loading custom mesh:",n,e),r(e)}),this.app.assets.add(s),this.app.assets.load(s)})}catch(e){console.error("[CharacterController] Failed to load custom collision mesh:",n,e)}}computeAndStoreBounds(t){const o=new e.BoundingBox;let n=!1;const i=t=>{if(t.render&&t.render.meshInstances)for(const e of t.render.meshInstances)e.aabb&&(n?o.add(e.aabb):(o.copy(e.aabb),n=!0));for(const o of t.children)o instanceof e.Entity&&i(o)};i(t),n&&(t._collisionBounds=o,console.log("[CharacterController] Computed bounds for custom mesh:",o.halfExtents))}configureCollisionEntity(e,t){const o=t.position||[0,0,0];e.setPosition(o[0],o[1],-o[2]);const n=t.rotation||[0,0,0];e.setEulerAngles(n[0]*(180/Math.PI),n[1]*(180/Math.PI),-n[2]*(180/Math.PI));const i=t.scaling||[1,1,1];e.setLocalScale(i[0],i[1],i[2]),this.setEntityVisibility(e,!1),e._collisionMeshType=t.meshType,this.app.root.addChild(e),this.collisionEntities.push(e)}setEntityVisibility(t,o){t.render&&(t.render.enabled=o);for(const n of t.children)n instanceof e.Entity&&this.setEntityVisibility(n,o)}enable(){if(this.enabled)return;this.enabled=!0;const e=this.camera.getEulerAngles();this.pitch=e.x,this.yaw=e.y,this.velocity.set(0,0,0),this.horizontalVelocity.set(0,0,0),this.targetVelocity.set(0,0,0),this.setupInputHandlers(),console.log("[CharacterController] Enabled")}disable(){this.enabled&&(this.enabled=!1,this.removeInputHandlers(),document.pointerLockElement&&document.exitPointerLock(),console.log("[CharacterController] Disabled"))}setupInputHandlers(){const e=this.app.graphicsDevice.canvas;this.keydownHandler=e=>{this.keys[e.code]=!0,"Space"===e.code&&this.isGrounded&&(this.velocity.y=this.jumpVelocity,this.isGrounded=!1)},this.keyupHandler=e=>{this.keys[e.code]=!1},this.mousemoveHandler=e=>{this.mouseLocked&&(this.yaw-=e.movementX*this.lookSensitivity*100,this.pitch-=e.movementY*this.lookSensitivity*100,this.pitch=Math.max(-89,Math.min(89,this.pitch)))},this.clickHandler=()=>{this.mouseLocked||e.requestPointerLock()},this.pointerlockchangeHandler=()=>{this.mouseLocked=document.pointerLockElement===e},document.addEventListener("keydown",this.keydownHandler),document.addEventListener("keyup",this.keyupHandler),document.addEventListener("mousemove",this.mousemoveHandler),e.addEventListener("click",this.clickHandler),document.addEventListener("pointerlockchange",this.pointerlockchangeHandler)}removeInputHandlers(){const e=this.app.graphicsDevice.canvas;this.keydownHandler&&document.removeEventListener("keydown",this.keydownHandler),this.keyupHandler&&document.removeEventListener("keyup",this.keyupHandler),this.mousemoveHandler&&document.removeEventListener("mousemove",this.mousemoveHandler),this.clickHandler&&e.removeEventListener("click",this.clickHandler),this.pointerlockchangeHandler&&document.removeEventListener("pointerlockchange",this.pointerlockchangeHandler),this.keys={}}checkCollision(e,t){for(const o of this.collisionEntities){const n=o.getPosition(),i=o.getLocalScale(),a=o._collisionMeshType;if("floor"===a||"plane"===a)continue;let s,r,l;const c=o._collisionBounds;c?(s=c.halfExtents.x*i.x,r=c.halfExtents.y*i.y,l=c.halfExtents.z*i.z):(s=i.x/2,r=i.y/2,l=i.z/2);const d=Math.abs(e.x-n.x),p=Math.abs(e.y-n.y),h=Math.abs(e.z-n.z);if(d<s+t&&p<r+this.playerHeight/2&&h<l+t)return!0}return!1}checkGround(e){if(this.floorEntity){const t=this.floorEntity.getPosition(),o=this.floorEntity.getLocalScale(),n=o.x/2*100,i=o.z/2*100;if(Math.abs(e.x-t.x)<n&&Math.abs(e.z-t.z)<i)return t.y}let t=null;for(const o of this.collisionEntities){const n=o.getPosition(),i=o.getLocalScale(),a=o._collisionMeshType;if("floor"===a||"plane"===a||"sphere"===a)continue;let s,r,l;const c=o._collisionBounds;c?(s=c.halfExtents.x*i.x,r=c.halfExtents.y*i.y,l=c.halfExtents.z*i.z):(s=i.x/2,r=i.y/2,l=i.z/2);const d=n.y+r;Math.abs(e.x-n.x)<s+this.collisionRadius&&Math.abs(e.z-n.z)<l+this.collisionRadius&&e.y>=d-this.stepHeight&&(null===t||d>t)&&(t=d)}return t}update(t){if(!this.enabled)return;const o=(this.keys.KeyD||this.keys.ArrowRight?1:0)-(this.keys.KeyA||this.keys.ArrowLeft?1:0),n=(this.keys.KeyW||this.keys.ArrowUp?1:0)-(this.keys.KeyS||this.keys.ArrowDown?1:0),i=this.keys.ShiftLeft||this.keys.ShiftRight,a=this.yaw*(Math.PI/180);this.forward.set(-Math.sin(a),0,-Math.cos(a)),this.right.set(Math.cos(a),0,-Math.sin(a));const s=this.moveSpeed*(i?this.sprintMultiplier:1);this.targetVelocity.set(0,0,0),this.targetVelocity.add(this.tmpVec2.copy(this.forward).mulScalar(n*s)),this.targetVelocity.add(this.tmpVec2.copy(this.right).mulScalar(o*s));const r=((e,t)=>1-Math.pow(e,1e3*t))(this.moveDamping,t);this.horizontalVelocity.lerp(this.horizontalVelocity,this.targetVelocity,r),this.isGrounded||(this.velocity.y-=this.gravity*t,this.velocity.y=Math.max(-this.maxFallSpeed,this.velocity.y));const l=this.camera.getPosition().clone();l.y,this.playerHeight;const c=new e.Vec3;c.x=l.x+this.horizontalVelocity.x*t,c.y=l.y+this.velocity.y*t,c.z=l.z+this.horizontalVelocity.z*t,this.tmpVec2.set(c.x,l.y,l.z),this.checkCollision(this.tmpVec2,this.collisionRadius)&&(c.x=l.x),this.tmpVec2.set(c.x,l.y,c.z),this.checkCollision(this.tmpVec2,this.collisionRadius)&&(c.z=l.z);const d=this.checkGround(c),p=c.y-this.playerHeight;null!==d&&p<=d+this.groundCheckDistance?(c.y=d+this.playerHeight,this.velocity.y=0,this.isGrounded=!0):(null===d||p>d+this.stepHeight)&&(this.isGrounded=!1),this.camera.setPosition(c.x,c.y,c.z),this.camera.setEulerAngles(this.pitch,this.yaw,0)}destroy(){this.disable();for(const e of this.collisionEntities)e.destroy();this.collisionEntities=[],this.floorEntity=null}get grounded(){return this.isGrounded}getVelocity(){return this.velocity.clone()}setPosition(e,t,o){this.camera.setPosition(e,t,o)}setRotation(e,t){this.pitch=e,this.yaw=t,this.camera.setEulerAngles(e,t,0)}}let T=null;function k(){if(console.log("[RevealEffect] getGsplatRevealRadialClass called, cached:",!!T),T)return T;console.log("[RevealEffect] Creating new script class via pc.createScript");const t=e.createScript("gsplatRevealRadial");return console.log("[RevealEffect] Script class created:",t),Object.assign(t.prototype,{effectTime:0,_materialsApplied:null,_shadersApplied:!1,_retryCount:0,_maxRetries:100,_materialCreatedHandler:null,_systemMaterialHandler:null,_centerArray:[0,0,0],_dotTintArray:[0,0,0],_waveTintArray:[0,0,0],center:null,speed:5,acceleration:0,delay:2,dotTint:null,waveTint:null,oscillationIntensity:.2,endRadius:25,initialize(){console.log("[RevealEffect] initialize() called"),this.effectTime=0,this._materialsApplied=new Set,this._shadersApplied=!1,this._retryCount=0,this._centerArray=[0,0,0],this._dotTintArray=[0,0,0],this._waveTintArray=[0,0,0],this.center||(this.center=new e.Vec3(0,0,0)),this.dotTint||(this.dotTint=new e.Color(0,1,1)),this.waveTint||(this.waveTint=new e.Color(1,.5,0)),this.on("enable",()=>{console.log("[RevealEffect] enabled event fired"),this.effectTime=0,this._applyShaders()}),this.on("disable",()=>{console.log("[RevealEffect] disabled event fired"),this._removeShaders()}),this.enabled?(console.log("[RevealEffect] Starting enabled, applying shaders"),this._applyShaders()):console.log("[RevealEffect] Starting disabled, waiting for enable")},update(e){if(!this._shadersApplied&&this._retryCount<this._maxRetries&&(this._retryCount++,this._retryCount%20==0&&console.log(`[RevealEffect] Retry ${this._retryCount}/${this._maxRetries} to apply shaders`),this._applyShaders()),this.effectTime+=e,Math.floor(this.effectTime)!==Math.floor(this.effectTime-e)&&console.log(`[RevealEffect] effectTime: ${this.effectTime.toFixed(2)}s, shadersApplied: ${this._shadersApplied}, materialsCount: ${this._materialsApplied?.size||0}`),this._isEffectComplete())return console.log("[RevealEffect] Effect complete, disabling"),void(this.enabled=!1);this._updateUniforms()},_updateUniforms(){this._setUniform("uTime",this.effectTime),this._centerArray[0]=this.center.x,this._centerArray[1]=this.center.y,this._centerArray[2]=this.center.z,this._setUniform("uCenter",this._centerArray),this._setUniform("uSpeed",this.speed),this._setUniform("uAcceleration",this.acceleration),this._setUniform("uDelay",this.delay),this._dotTintArray[0]=this.dotTint.r,this._dotTintArray[1]=this.dotTint.g,this._dotTintArray[2]=this.dotTint.b,this._setUniform("uDotTint",this._dotTintArray),this._waveTintArray[0]=this.waveTint.r,this._waveTintArray[1]=this.waveTint.g,this._waveTintArray[2]=this.waveTint.b,this._setUniform("uWaveTint",this._waveTintArray),this._setUniform("uOscillationIntensity",this.oscillationIntensity),this._setUniform("uEndRadius",this.endRadius)},_getCompletionTime(){const e=this.delay;if(0===this.acceleration)return e+this.endRadius/this.speed;const t=this.speed*this.speed+2*this.acceleration*this.endRadius;if(t<0)return 1/0;return e+(-this.speed+Math.sqrt(t))/this.acceleration},_isEffectComplete(){return this.effectTime>=this._getCompletionTime()},getShaderGLSL:()=>"\nuniform float uTime;\nuniform vec3 uCenter;\nuniform float uSpeed;\nuniform float uAcceleration;\nuniform float uDelay;\nuniform vec3 uDotTint;\nuniform vec3 uWaveTint;\nuniform float uOscillationIntensity;\nuniform float uEndRadius;\n\n// Shared globals (initialized once per vertex)\nfloat g_dist;\nfloat g_dotWavePos;\nfloat g_liftTime;\nfloat g_liftWavePos;\n\nvoid initShared(vec3 center) {\n g_dist = length(center - uCenter);\n g_dotWavePos = uSpeed * uTime + 0.5 * uAcceleration * uTime * uTime;\n g_liftTime = max(0.0, uTime - uDelay);\n g_liftWavePos = uSpeed * g_liftTime + 0.5 * uAcceleration * g_liftTime * g_liftTime;\n}\n\n// Hash function for per-splat randomization\nfloat hash(vec3 p) {\n return fract(sin(dot(p, vec3(127.1, 311.7, 74.7))) * 43758.5453);\n}\n\nvoid modifyCenter(inout vec3 center) {\n initShared(center);\n\n // Early exit optimization\n if (g_dist > uEndRadius) return;\n\n // Only apply oscillation if lift wave hasn't fully passed\n bool wavesActive = g_liftTime <= 0.0 || g_dist > g_liftWavePos - 1.5;\n if (wavesActive) {\n // Apply oscillation with per-splat phase offset\n float phase = hash(center) * 6.28318;\n center.y += sin(uTime * 3.0 + phase) * uOscillationIntensity * 0.25;\n }\n\n // Apply lift effect near the wave edge\n float distToLiftWave = abs(g_dist - g_liftWavePos);\n if (distToLiftWave < 1.0 && g_liftTime > 0.0) {\n // Create a smooth lift curve (peaks at wave edge)\n // Lift is 0.9x the oscillation intensity (30% of original 3x)\n float liftAmount = (1.0 - distToLiftWave) * sin(distToLiftWave * 3.14159);\n center.y += liftAmount * uOscillationIntensity * 0.9;\n }\n}\n\nvoid modifyCovariance(vec3 originalCenter, vec3 modifiedCenter, inout vec3 covA, inout vec3 covB) {\n // Early exit for distant splats - hide them\n if (g_dist > uEndRadius) {\n gsplatMakeRound(covA, covB, 0.0);\n return;\n }\n\n // Determine scale and phase\n float scale;\n bool isLiftWave = g_liftTime > 0.0 && g_liftWavePos > g_dist;\n\n if (isLiftWave) {\n // Lift wave: transition from dots to full size\n scale = (g_liftWavePos >= g_dist + 2.0) ? 1.0 : mix(0.1, 1.0, (g_liftWavePos - g_dist) * 0.5);\n } else if (g_dist > g_dotWavePos + 1.0) {\n // Before dot wave: invisible\n gsplatMakeRound(covA, covB, 0.0);\n return;\n } else if (g_dist > g_dotWavePos - 1.0) {\n // Dot wave front: scale from 0 to 0.1 with 2x peak at center\n float distToWave = abs(g_dist - g_dotWavePos);\n scale = (distToWave < 0.5)\n ? mix(0.1, 0.2, 1.0 - distToWave * 2.0)\n : mix(0.0, 0.1, smoothstep(g_dotWavePos + 1.0, g_dotWavePos - 1.0, g_dist));\n } else {\n // After dot wave, before lift: small dots\n scale = 0.1;\n }\n\n // Apply scale to covariance\n if (scale >= 1.0) {\n // Fully revealed: original shape and size (no-op)\n return;\n } else if (isLiftWave) {\n // Lift wave: lerp from round dots to original shape\n float t = (scale - 0.1) * 1.111111; // normalize [0.1, 1.0] to [0, 1]\n float dotSize = scale * 0.05;\n float originalSize = gsplatExtractSize(covA, covB);\n float finalSize = mix(dotSize, originalSize, t);\n\n // Lerp between round and scaled original\n vec3 origCovA = covA * (scale * scale);\n vec3 origCovB = covB * (scale * scale);\n gsplatMakeRound(covA, covB, finalSize);\n covA = mix(covA, origCovA, t);\n covB = mix(covB, origCovB, t);\n } else {\n // Dot phase: round with absolute size, but don't make small splats larger\n float originalSize = gsplatExtractSize(covA, covB);\n gsplatMakeRound(covA, covB, min(scale * 0.05, originalSize));\n }\n}\n\nvoid modifyColor(vec3 center, inout vec4 color) {\n // Use shared globals\n if (g_dist > uEndRadius) return;\n\n // Lift wave tint takes priority (active during lift)\n if (g_liftTime > 0.0 && g_dist >= g_liftWavePos - 1.5 && g_dist <= g_liftWavePos + 0.5) {\n float distToLift = abs(g_dist - g_liftWavePos);\n float liftIntensity = smoothstep(1.5, 0.0, distToLift);\n color.rgb += uWaveTint * liftIntensity;\n }\n // Dot wave tint (active in dot phase, but not where lift wave is active)\n else if (g_dist <= g_dotWavePos && (g_liftTime <= 0.0 || g_dist > g_liftWavePos + 0.5)) {\n float distToDot = abs(g_dist - g_dotWavePos);\n float dotIntensity = smoothstep(1.0, 0.0, distToDot);\n color.rgb += uDotTint * dotIntensity;\n }\n}\n",getShaderWGSL:()=>"\nuniform uTime: f32;\nuniform uCenter: vec3f;\nuniform uSpeed: f32;\nuniform uAcceleration: f32;\nuniform uDelay: f32;\nuniform uDotTint: vec3f;\nuniform uWaveTint: vec3f;\nuniform uOscillationIntensity: f32;\nuniform uEndRadius: f32;\n\n// Shared globals (initialized once per vertex)\nvar<private> g_dist: f32;\nvar<private> g_dotWavePos: f32;\nvar<private> g_liftTime: f32;\nvar<private> g_liftWavePos: f32;\n\nfn initShared(center: vec3f) {\n g_dist = length(center - uniform.uCenter);\n g_dotWavePos = uniform.uSpeed * uniform.uTime + 0.5 * uniform.uAcceleration * uniform.uTime * uniform.uTime;\n g_liftTime = max(0.0, uniform.uTime - uniform.uDelay);\n g_liftWavePos = uniform.uSpeed * g_liftTime + 0.5 * uniform.uAcceleration * g_liftTime * g_liftTime;\n}\n\n// Hash function for per-splat randomization\nfn hash(p: vec3f) -> f32 {\n return fract(sin(dot(p, vec3f(127.1, 311.7, 74.7))) * 43758.5453);\n}\n\nfn modifyCenter(center: ptr<function, vec3f>) {\n initShared(*center);\n\n // Early exit optimization\n if (g_dist > uniform.uEndRadius) {\n return;\n }\n\n // Only apply oscillation if lift wave hasn't fully passed\n let wavesActive = g_liftTime <= 0.0 || g_dist > g_liftWavePos - 1.5;\n if (wavesActive) {\n // Apply oscillation with per-splat phase offset\n let phase = hash(*center) * 6.28318;\n (*center).y += sin(uniform.uTime * 3.0 + phase) * uniform.uOscillationIntensity * 0.25;\n }\n\n // Apply lift effect near the wave edge\n let distToLiftWave = abs(g_dist - g_liftWavePos);\n if (distToLiftWave < 1.0 && g_liftTime > 0.0) {\n // Create a smooth lift curve (peaks at wave edge)\n // Lift is 0.9x the oscillation intensity (30% of original 3x)\n let liftAmount = (1.0 - distToLiftWave) * sin(distToLiftWave * 3.14159);\n (*center).y += liftAmount * uniform.uOscillationIntensity * 0.9;\n }\n}\n\nfn modifyCovariance(originalCenter: vec3f, modifiedCenter: vec3f, covA: ptr<function, vec3f>, covB: ptr<function, vec3f>) {\n // Early exit for distant splats - hide them\n if (g_dist > uniform.uEndRadius) {\n gsplatMakeRound(covA, covB, 0.0);\n return;\n }\n\n // Determine scale and phase\n var scale: f32;\n let isLiftWave = g_liftTime > 0.0 && g_liftWavePos > g_dist;\n\n if (isLiftWave) {\n // Lift wave: transition from dots to full size\n scale = select(mix(0.1, 1.0, (g_liftWavePos - g_dist) * 0.5), 1.0, g_liftWavePos >= g_dist + 2.0);\n } else if (g_dist > g_dotWavePos + 1.0) {\n // Before dot wave: invisible\n gsplatMakeRound(covA, covB, 0.0);\n return;\n } else if (g_dist > g_dotWavePos - 1.0) {\n // Dot wave front: scale from 0 to 0.1 with 2x peak at center\n let distToWave = abs(g_dist - g_dotWavePos);\n scale = select(\n mix(0.0, 0.1, smoothstep(g_dotWavePos + 1.0, g_dotWavePos - 1.0, g_dist)),\n mix(0.1, 0.2, 1.0 - distToWave * 2.0),\n distToWave < 0.5\n );\n } else {\n // After dot wave, before lift: small dots\n scale = 0.1;\n }\n\n // Apply scale to covariance\n if (scale >= 1.0) {\n // Fully revealed: original shape and size (no-op)\n return;\n } else if (isLiftWave) {\n // Lift wave: lerp from round dots to original shape\n let t = (scale - 0.1) * 1.111111; // normalize [0.1, 1.0] to [0, 1]\n let dotSize = scale * 0.05;\n let originalSize = gsplatExtractSize(*covA, *covB);\n let finalSize = mix(dotSize, originalSize, t);\n\n // Lerp between round and scaled original\n let origCovA = *covA * (scale * scale);\n let origCovB = *covB * (scale * scale);\n gsplatMakeRound(covA, covB, finalSize);\n *covA = mix(*covA, origCovA, t);\n *covB = mix(*covB, origCovB, t);\n } else {\n // Dot phase: round with absolute size, but don't make small splats larger\n let originalSize = gsplatExtractSize(*covA, *covB);\n gsplatMakeRound(covA, covB, min(scale * 0.05, originalSize));\n }\n}\n\nfn modifyColor(center: vec3f, color: ptr<function, vec4f>) {\n // Use shared globals\n if (g_dist > uniform.uEndRadius) {\n return;\n }\n\n // Lift wave tint takes priority (active during lift)\n if (g_liftTime > 0.0 && g_dist >= g_liftWavePos - 1.5 && g_dist <= g_liftWavePos + 0.5) {\n let distToLift = abs(g_dist - g_liftWavePos);\n let liftIntensity = smoothstep(1.5, 0.0, distToLift);\n (*color) = vec4f((*color).rgb + uniform.uWaveTint * liftIntensity, (*color).a);\n }\n // Dot wave tint (active in dot phase, but not where lift wave is active)\n else if (g_dist <= g_dotWavePos && (g_liftTime <= 0.0 || g_dist > g_liftWavePos + 0.5)) {\n let distToDot = abs(g_dist - g_dotWavePos);\n let dotIntensity = smoothstep(1.0, 0.0, distToDot);\n (*color) = vec4f((*color).rgb + uniform.uDotTint * dotIntensity, (*color).a);\n }\n}\n",_applyShaders(){const e=this.entity.gsplat;if(!e)return void console.log("[RevealEffect] _applyShaders: No gsplat component found on entity");const t=!0===e.unified,o=this.app;if(t){console.log("[RevealEffect] Unified mode detected, using GSplatComponentSystem");const t=o?.systems?.gsplat;if(t){this._systemMaterialHandler||(this._systemMaterialHandler=(e,t,o)=>{console.log("[RevealEffect] material:created event from GSplatComponentSystem"),this._applyShaderToMaterial(e),this._shadersApplied=!0},t.on("material:created",this._systemMaterialHandler),console.log("[RevealEffect] Subscribed to GSplatComponentSystem material:created event"));const n=o.root?.findComponents("camera")||[],i=e.layers||[0];for(const e of n)for(const n of i){const i=o.scene?.layers?.getLayerById(n);if(i){const o=t.getGSplatMaterial?.(e.camera,i);o&&(console.log("[RevealEffect] Found unified material via getGSplatMaterial:",o),this._applyShaderToMaterial(o),this._shadersApplied=!0)}}}return}const n=e.instance||e._instance;if(n)return console.log("[RevealEffect] Instance found via component:",n),void this._applyToInstance(n);if(o&&o.scene){const e=o.scene.layers?.layerList||[];for(const t of e)if(t.meshInstances)for(const e of t.meshInstances){if(e.gsplatInstance||e._gsplatInstance){const t=e.gsplatInstance||e._gsplatInstance;return console.log("[RevealEffect] Found gsplat instance via mesh instance:",t),void this._applyToInstance(t)}const t=e.material;if(t&&t.gsplat)return console.log("[RevealEffect] Found gsplat material via mesh instance:",t),this._applyShaderToMaterial(t),void(this._shadersApplied=!0)}const t=e=>{if(e.gsplat){const t=e.gsplat.instance||e.gsplat._instance;if(t)return console.log("[RevealEffect] Found gsplat instance via entity search:",t),this._applyToInstance(t),!0}for(const o of e.children||[])if(t(o))return!0;return!1};o.root&&t(o.root)}this._retryCount%50==0&&(console.log("[RevealEffect] Still searching for gsplat materials..."),console.log("[RevealEffect] gsplatComponent.unified:",e.unified),console.log("[RevealEffect] gsplatComponent.asset:",e.asset))},_applyToInstance(e){if(this._shadersApplied)return;console.log("[RevealEffect] Applying shaders to instance"),console.log("[RevealEffect] Instance type:",e.constructor?.name),console.log("[RevealEffect] Instance keys:",Object.keys(e));const t=e.materials||e._materials;if(console.log("[RevealEffect] Instance materials:",t),t&&(t instanceof Map||t.forEach&&void 0!==t.size?(console.log("[RevealEffect] Materials is a Map/Set with size:",t.size),t.size>0&&(t.forEach(e=>{this._applyShaderToMaterial(e)}),this._shadersApplied=!0,console.log("[RevealEffect] SUCCESS: Shaders applied to",t.size,"materials"))):Array.isArray(t)&&(console.log("[RevealEffect] Materials is array with length:",t.length),t.forEach(e=>{this._applyShaderToMaterial(e)}),this._shadersApplied=!0)),!this._shadersApplied){const t=e.material||e._material;t&&(console.log("[RevealEffect] Found single material on instance"),this._applyShaderToMaterial(t),this._shadersApplied=!0)}e.on&&!this._materialCreatedHandler&&(this._materialCreatedHandler=e=>{console.log("[RevealEffect] material:created event received"),this._applyShaderToMaterial(e),this._shadersApplied=!0},e.on("material:created",this._materialCreatedHandler),console.log("[RevealEffect] Subscribed to material:created event"))},_applyShaderToMaterial(e){if(this._materialsApplied.has(e))return void console.log("[RevealEffect] Material already has shader applied, skipping");console.log("[RevealEffect] Applying shader to material:",e),console.log("[RevealEffect] Material constructor:",e.constructor?.name),console.log("[RevealEffect] Material keys:",Object.keys(e));const t=[];let o=e;for(;o&&o!==Object.prototype;){const e=Object.getOwnPropertyNames(o);for(const n of e)try{"function"!=typeof o[n]||t.includes(n)||t.push(n)}catch(e){}o=Object.getPrototypeOf(o)}console.log("[RevealEffect] Material methods:",t.filter(e=>!e.startsWith("_")).join(", "));const n=this.getShaderGLSL(),i=this.getShaderWGSL(),a="function"==typeof e.setShaderChunk;console.log("[RevealEffect] material.setShaderChunk exists:",a),a?(n&&(e.setShaderChunk("gsplatEffectGLSL",n),console.log("[RevealEffect] GLSL shader chunk set via setShaderChunk")),i&&(e.setShaderChunk("gsplatEffectWGSL",i),console.log("[RevealEffect] WGSL shader chunk set via setShaderChunk"))):(e.chunks&&(console.log("[RevealEffect] Material has chunks property"),e.chunks.gsplatEffectGLSL=n,e.chunks.gsplatEffectWGSL=i,console.log("[RevealEffect] Set chunks directly")),e.options&&console.log("[RevealEffect] Material has options:",e.options),e.shader&&console.log("[RevealEffect] Material has shader:",e.shader),"function"==typeof e.setParameter&&console.log("[RevealEffect] material.setParameter exists - will use for uniforms")),e.update?.(),this._materialsApplied.add(e),console.log("[RevealEffect] Material added to applied set, total:",this._materialsApplied.size)},_removeShaders(){if(this._materialsApplied&&(this._materialsApplied.forEach(e=>{e.setShaderChunk?.("gsplatEffectGLSL",""),e.setShaderChunk?.("gsplatEffectWGSL",""),e.update?.()}),this._materialsApplied.clear()),this._shadersApplied=!1,this._materialCreatedHandler){const e=this.entity.gsplat,t=e?.instance;t?.off&&t.off("material:created",this._materialCreatedHandler),this._materialCreatedHandler=null}if(this._systemMaterialHandler){const e=this.app?.systems?.gsplat;e?.off&&e.off("material:created",this._systemMaterialHandler),this._systemMaterialHandler=null}},_setUniform(e,t){this._materialsApplied&&this._materialsApplied.forEach(o=>{o.setParameter?.(e,t)})},destroy(){this._removeShaders()}}),T=t,t}const A={fast:{speed:10,acceleration:2,delay:.5,oscillationIntensity:.1,dotTint:{r:0,g:1,b:1},waveTint:{r:1,g:.5,b:0},endRadius:50},medium:{speed:5,acceleration:0,delay:2,oscillationIntensity:.2,dotTint:{r:0,g:1,b:1},waveTint:{r:1,g:.5,b:0},endRadius:50},slow:{speed:3,acceleration:0,delay:3,oscillationIntensity:.25,dotTint:{r:0,g:1,b:1},waveTint:{r:1,g:.5,b:0},endRadius:50}};function P(e){if("none"!==e)return A[e]}var z,R={},I={},D={};function F(){if(z)return D;z=1,Object.defineProperty(D,"__esModule",{value:!0}),D.loop=D.conditional=D.parse=void 0;D.parse=function e(t,o){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:n;if(Array.isArray(o))o.forEach(function(o){return e(t,o,n,i)});else if("function"==typeof o)o(t,n,i,e);else{var a=Object.keys(o)[0];Array.isArray(o[a])?(i[a]={},e(t,o[a],n,i[a])):i[a]=o[a](t,n,i,e)}return n};D.conditional=function(e,t){return function(o,n,i,a){t(o,n,i)&&a(o,e,n,i)}};return D.loop=function(e,t){return function(o,n,i,a){for(var s=[],r=o.pos;t(o,n,i);){var l={};if(a(o,e,n,l),o.pos===r)break;r=o.pos,s.push(l)}return s}},D}var U,B,V={};function $(){if(U)return V;U=1,Object.defineProperty(V,"__esModule",{value:!0}),V.readBits=V.readArray=V.readUnsigned=V.readString=V.peekBytes=V.readBytes=V.peekByte=V.readByte=V.buildStream=void 0;V.buildStream=function(e){return{data:e,pos:0}};var e=function(){return function(e){return e.data[e.pos++]}};V.readByte=e;V.peekByte=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return function(t){return t.data[t.pos+e]}};var t=function(e){return function(t){return t.data.subarray(t.pos,t.pos+=e)}};V.readBytes=t;V.peekBytes=function(e){return function(t){return t.data.subarray(t.pos,t.pos+e)}};V.readString=function(e){return function(o){return Array.from(t(e)(o)).map(function(e){return String.fromCharCode(e)}).join("")}};V.readUnsigned=function(e){return function(o){var n=t(2)(o);return e?(n[1]<<8)+n[0]:(n[0]<<8)+n[1]}};V.readArray=function(e,o){return function(n,i,a){for(var s="function"==typeof o?o(n,i,a):o,r=t(e),l=new Array(s),c=0;c<s;c++)l[c]=r(n);return l}};return V.readBits=function(e){return function(t){for(var o=function(e){return e.data[e.pos++]}(t),n=new Array(8),i=0;i<8;i++)n[7-i]=!!(o&1<<i);return Object.keys(e).reduce(function(t,o){var i=e[o];return i.length?t[o]=function(e,t,o){for(var n=0,i=0;i<o;i++)n+=e[t+i]&&Math.pow(2,o-i-1);return n}(n,i.index,i.length):t[o]=n[i.index],t},{})}},V}var H,O={};var W,G,N={};var j=function(){if(G)return R;G=1,Object.defineProperty(R,"__esModule",{value:!0}),R.decompressFrames=R.decompressFrame=R.parseGIF=void 0;var e,t=(B||(B=1,function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.default=void 0;var t=F(),o=$(),n={blocks:function(e){for(var t=[],n=e.data.length,i=0,a=(0,o.readByte)()(e);0!==a&&a;a=(0,o.readByte)()(e)){if(e.pos+a>=n){var s=n-e.pos;t.push((0,o.readBytes)(s)(e)),i+=s;break}t.push((0,o.readBytes)(a)(e)),i+=a}for(var r=new Uint8Array(i),l=0,c=0;c<t.length;c++)r.set(t[c],l),l+=t[c].length;return r}},i=(0,t.conditional)({gce:[{codes:(0,o.readBytes)(2)},{byteSize:(0,o.readByte)()},{extras:(0,o.readBits)({future:{index:0,length:3},disposal:{index:3,length:3},userInput:{index:6},transparentColorGiven:{index:7}})},{delay:(0,o.readUnsigned)(!0)},{transparentColorIndex:(0,o.readByte)()},{terminator:(0,o.readByte)()}]},function(e){var t=(0,o.peekBytes)(2)(e);return 33===t[0]&&249===t[1]}),a=(0,t.conditional)({image:[{code:(0,o.readByte)()},{descriptor:[{left:(0,o.readUnsigned)(!0)},{top:(0,o.readUnsigned)(!0)},{width:(0,o.readUnsigned)(!0)},{height:(0,o.readUnsigned)(!0)},{lct:(0,o.readBits)({exists:{index:0},interlaced:{index:1},sort:{index:2},future:{index:3,length:2},size:{index:5,length:3}})}]},(0,t.conditional)({lct:(0,o.readArray)(3,function(e,t,o){return Math.pow(2,o.descriptor.lct.size+1)})},function(e,t,o){return o.descriptor.lct.exists}),{data:[{minCodeSize:(0,o.readByte)()},n]}]},function(e){return 44===(0,o.peekByte)()(e)}),s=(0,t.conditional)({text:[{codes:(0,o.readBytes)(2)},{blockSize:(0,o.readByte)()},{preData:function(e,t,n){return(0,o.readBytes)(n.text.blockSize)(e)}},n]},function(e){var t=(0,o.peekBytes)(2)(e);return 33===t[0]&&1===t[1]}),r=(0,t.conditional)({application:[{codes:(0,o.readBytes)(2)},{blockSize:(0,o.readByte)()},{id:function(e,t,n){return(0,o.readString)(n.blockSize)(e)}},n]},function(e){var t=(0,o.peekBytes)(2)(e);return 33===t[0]&&255===t[1]}),l=(0,t.conditional)({comment:[{codes:(0,o.readBytes)(2)},n]},function(e){var t=(0,o.peekBytes)(2)(e);return 33===t[0]&&254===t[1]}),c=[{header:[{signature:(0,o.readString)(3)},{version:(0,o.readString)(3)}]},{lsd:[{width:(0,o.readUnsigned)(!0)},{height:(0,o.readUnsigned)(!0)},{gct:(0,o.readBits)({exists:{index:0},resolution:{index:1,length:3},sort:{index:4},size:{index:5,length:3}})},{backgroundColorIndex:(0,o.readByte)()},{pixelAspectRatio:(0,o.readByte)()}]},(0,t.conditional)({gct:(0,o.readArray)(3,function(e,t){return Math.pow(2,t.lsd.gct.size+1)})},function(e,t){return t.lsd.gct.exists}),{frames:(0,t.loop)([i,r,l,a,s],function(e){var t=(0,o.peekByte)()(e);return 33===t||44===t})}];e.default=c}(I)),(e=I)&&e.__esModule?e:{default:e}),o=F(),n=$(),i=(H||(H=1,Object.defineProperty(O,"__esModule",{value:!0}),O.deinterlace=void 0,O.deinterlace=function(e,t){for(var o=new Array(e.length),n=e.length/t,i=function(n,i){var a=e.slice(i*t,(i+1)*t);o.splice.apply(o,[n*t,t].concat(a))},a=[0,4,2,1],s=[8,8,4,2],r=0,l=0;l<4;l++)for(var c=a[l];c<n;c+=s[l])i(c,r),r++;return o}),O),a=(W||(W=1,Object.defineProperty(N,"__esModule",{value:!0}),N.lzw=void 0,N.lzw=function(e,t,o){var n,i,a,s,r,l,c,d,p,h,u,m,g,y,f,v,b=4096,w=o,x=new Array(o),S=new Array(b),E=new Array(b),M=new Array(4097);for(r=1+(i=1<<(h=e)),n=i+2,c=-1,a=(1<<(s=h+1))-1,d=0;d<i;d++)S[d]=0,E[d]=d;for(u=m=g=y=f=v=0,p=0;p<w;){if(0===y){if(m<s){u+=t[v]<<m,m+=8,v++;continue}if(d=u&a,u>>=s,m-=s,d>n||d==r)break;if(d==i){a=(1<<(s=h+1))-1,n=i+2,c=-1;continue}if(-1==c){M[y++]=E[d],c=d,g=d;continue}for(l=d,d==n&&(M[y++]=g,d=c);d>i;)M[y++]=E[d],d=S[d];g=255&E[d],M[y++]=g,n<b&&(S[n]=c,E[n]=g,0===(++n&a)&&n<b&&(s++,a+=n)),c=l}y--,x[f++]=M[y],p++}for(p=f;p<w;p++)x[p]=0;return x}),N);R.parseGIF=function(e){var i=new Uint8Array(e);return(0,o.parse)((0,n.buildStream)(i),t.default)};var s=function(e,t,o){if(e.image){var n=e.image,s=n.descriptor.width*n.descriptor.height,r=(0,a.lzw)(n.data.minCodeSize,n.data.blocks,s);n.descriptor.lct.interlaced&&(r=(0,i.deinterlace)(r,n.descriptor.width));var l={pixels:r,dims:{top:e.image.descriptor.top,left:e.image.descriptor.left,width:e.image.descriptor.width,height:e.image.descriptor.height}};return n.descriptor.lct&&n.descriptor.lct.exists?l.colorTable=n.lct:l.colorTable=t,e.gce&&(l.delay=10*(e.gce.delay||10),l.disposalType=e.gce.extras.disposal,e.gce.extras.transparentColorGiven&&(l.transparentIndex=e.gce.transparentColorIndex)),o&&(l.patch=function(e){for(var t=e.pixels.length,o=new Uint8ClampedArray(4*t),n=0;n<t;n++){var i=4*n,a=e.pixels[n],s=e.colorTable[a]||[0,0,0];o[i]=s[0],o[i+1]=s[1],o[i+2]=s[2],o[i+3]=a!==e.transparentIndex?255:0}return o}(l)),l}console.warn("gif frame does not have associated image.")};return R.decompressFrame=s,R.decompressFrames=function(e,t){return e.frames.filter(function(e){return e.image}).map(function(o){return s(o,e.gct,t)})},R}();class q{constructor(e,t,o={}){this.frames=[],this.currentFrameIndex=0,this.isPlaying=!1,this.isLoaded=!1,this.lastFrameTime=0,this.updateHandler=null,this.texture=null,this.gifWidth=0,this.gifHeight=0,this.update=()=>{if(!this.isPlaying||!this.isLoaded||this.frames.length<=1)return;const e=performance.now(),t=this.frames[this.currentFrameIndex].delay||100;e-this.lastFrameTime>=t&&(this.currentFrameIndex=(this.currentFrameIndex+1)%this.frames.length,this.drawFrame(this.currentFrameIndex),this.lastFrameTime=e)},this.app=e,this.url=t,this.options=o,this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d",{willReadFrequently:!0}),this.load()}async load(){try{const t=await fetch(this.url);if(!t.ok)throw new Error(`Failed to fetch GIF: ${t.statusText}`);const o=await t.arrayBuffer(),n=j.parseGIF(o);if(this.frames=j.decompressFrames(n,!0),0===this.frames.length)throw new Error("GIF has no frames");this.gifWidth=n.lsd.width,this.gifHeight=n.lsd.height,this.canvas.width=this.gifWidth,this.canvas.height=this.gifHeight,this.texture=new e.Texture(this.app.graphicsDevice,{width:this.gifWidth,height:this.gifHeight,format:e.PIXELFORMAT_RGBA8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE}),this.drawFrame(0),this.isLoaded=!0,console.log(`[AnimatedGif] Loaded GIF: ${this.url}, ${this.frames.length} frames, ${this.gifWidth}x${this.gifHeight}`),this.options.onReady&&this.options.onReady(),this.options.autoPlay&&this.play()}catch(e){console.error("[AnimatedGif] Error loading GIF:",e),this.options.onError&&this.options.onError(e instanceof Error?e:new Error(String(e)))}}drawFrame(e){if(!this.texture||e>=this.frames.length)return;const t=this.frames[e],o=e>0?this.frames[e-1]:null;o&&2===o.disposalType&&this.ctx.clearRect(o.dims.left,o.dims.top,o.dims.width,o.dims.height);const n=new ImageData(new Uint8ClampedArray(t.patch),t.dims.width,t.dims.height),i=document.createElement("canvas");i.width=t.dims.width,i.height=t.dims.height;i.getContext("2d").putImageData(n,0,0),this.ctx.drawImage(i,t.dims.left,t.dims.top),this.updateTexture()}updateTexture(){if(!this.texture)return;const e=this.ctx.getImageData(0,0,this.gifWidth,this.gifHeight),t=this.texture.lock();t&&t.set(e.data),this.texture.unlock(),this.texture.upload()}play(){this.isPlaying||(this.isPlaying=!0,this.lastFrameTime=performance.now(),this.updateHandler||(this.updateHandler=this.update,this.app.on("update",this.updateHandler)))}pause(){this.isPlaying&&(this.isPlaying=!1,this.updateHandler&&(this.app.off("update",this.updateHandler),this.updateHandler=null))}stop(){this.pause(),this.currentFrameIndex=0,this.isLoaded&&(this.ctx.clearRect(0,0,this.gifWidth,this.gifHeight),this.drawFrame(0))}get playing(){return this.isPlaying}get loaded(){return this.isLoaded}destroy(){this.pause(),this.texture&&(this.texture.destroy(),this.texture=null),this.frames=[],this.isLoaded=!1}}class Y{constructor(t){this.meshes=new Map,this.updateHandler=null,this.useTexElement2D=!1,this.app=t,this.canvas=t.graphicsDevice.canvas,function(){const t=e.GraphicsDevice;if(!t)return void console.warn("[HtmlMeshHelper] Could not find pc.GraphicsDevice to patch");if("function"==typeof t.prototype._isHTMLElementInterface)return;t.prototype._isHTMLElementInterface=function(e){return!(!("undefined"!=typeof HTMLElement&&e instanceof HTMLElement)||e instanceof HTMLImageElement||e instanceof HTMLCanvasElement||e instanceof HTMLVideoElement)};const o=t.prototype._isBrowserInterface;o&&(t.prototype._isBrowserInterface=function(e){return o.call(this,e)||this._isHTMLElementInterface(e)}),console.log("[HtmlMeshHelper] Patched PlayCanvas GraphicsDevice for HTML-in-Canvas support")}();const o=t.graphicsDevice;this.useTexElement2D=!0===o.supportsTexElement2D,console.log(`[HtmlMeshManager] texElement2D support: ${this.useTexElement2D}`)}createMesh(e){const t=e.width||512,o=e.height||512,n=this.createHtmlElement(e,t,o),i=this.createTexture(n,t,o,e),a=this.createMaterial(i,e),s={entity:this.createEntity(e,a,t,o),texture:i,material:a,htmlElement:n,config:e,destroy:()=>this.destroyMesh(e.id),update:()=>this.updateMeshTexture(e.id)};return this.meshes.set(e.id,s),e.animated&&!this.updateHandler&&this.startUpdateLoop(),s}createHtmlElement(e,t,o){const n=document.createElement("div");if(n.id=`html-mesh-${e.id}`,n.style.width=`${t}px`,n.style.height=`${o}px`,n.style.position="absolute",n.style.top="0",n.style.left="0",n.style.pointerEvents="none",n.style.zIndex="-1",n.style.overflow="hidden",e.css){const t=document.createElement("style");t.textContent=e.css,n.appendChild(t)}return n.innerHTML+=e.html,this.useTexElement2D?(this.canvas.setAttribute("layoutsubtree",""),this.canvas.setAttribute("data-layoutsubtree",""),this.canvas.appendChild(n)):(n.style.visibility="hidden",document.body.appendChild(n)),n}createTexture(t,o,n,i){const a=new e.Texture(this.app.graphicsDevice,{width:o,height:n,format:e.PIXELFORMAT_RGBA8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE,name:`htmlMesh-${i.id}`});if(this.useTexElement2D)try{a.setSource(t),console.log(`[HtmlMeshManager] Using texElement2D for mesh ${i.id}`)}catch(e){console.warn(`[HtmlMeshManager] texElement2D failed, falling back to canvas: ${e}`),this.renderToCanvas(a,t,o,n)}else this.renderToCanvas(a,t,o,n);return a}renderToCanvas(e,t,o,n){const i=document.createElement("canvas");i.width=o,i.height=n;const a=i.getContext("2d",{willReadFrequently:!0}),s=`\n <svg xmlns="http://www.w3.org/2000/svg" width="${o}" height="${n}">\n <foreignObject width="100%" height="100%">\n <div xmlns="http://www.w3.org/1999/xhtml" style="width:${o}px;height:${n}px;">\n ${t.innerHTML}\n </div>\n </foreignObject>\n </svg>\n `,r=new Image,l=new Blob([s],{type:"image/svg+xml;charset=utf-8"}),c=URL.createObjectURL(l);r.onload=()=>{a.drawImage(r,0,0),URL.revokeObjectURL(c),e.setSource(i)},r.onerror=()=>{a.fillStyle="#333",a.fillRect(0,0,o,n),a.fillStyle="#fff",a.font="20px Arial",a.textAlign="center",a.fillText("HTML Mesh",o/2,n/2),URL.revokeObjectURL(c),e.setSource(i)},r.src=c}createMaterial(t,o){const n=new e.StandardMaterial;return n.diffuseMap=t,n.emissiveMap=t,n.emissive=new e.Color(.5,.5,.5),n.opacity=o.opacity??1,n.blendType=void 0!==o.opacity&&o.opacity<1?e.BLEND_NORMAL:e.BLEND_NONE,n.cull=o.doubleSided?e.CULLFACE_NONE:e.CULLFACE_BACK,n.update(),n}createEntity(t,o,n,i){const a=new e.Entity(`htmlMesh-${t.id}`),s=n/i;a.addComponent("render",{type:"plane",material:o,castShadows:t.castShadows??!1,receiveShadows:t.receiveShadows??!1}),a.setPosition(t.position.x,t.position.y,t.position.z);const r=t.rotation||{x:0,y:0,z:0};a.setEulerAngles(r.x,r.y,r.z);const l=t.scale||{x:1,y:1};return a.setLocalScale(l.x*s,1,l.y),this.app.root.addChild(a),t.billboard&&this.app.on("update",()=>{if(!a.enabled)return;const e=this.app.root.findComponent("camera")?.entity;e&&a.lookAt(e.getPosition())}),a}updateMeshTexture(e){const t=this.meshes.get(e);t&&(this.useTexElement2D?t.texture.upload():this.renderToCanvas(t.texture,t.htmlElement,t.config.width||512,t.config.height||512))}startUpdateLoop(){let e={};this.updateHandler=()=>{const t=performance.now();this.meshes.forEach((o,n)=>{if(!o.config.animated)return;const i=o.config.updateRate||100,a=e[n]||0;t-a>=i&&(this.updateMeshTexture(n),e[n]=t)})},this.app.on("update",this.updateHandler)}destroyMesh(e){const t=this.meshes.get(e);if(!t)return;t.entity.destroy(),t.texture.destroy(),t.htmlElement.remove(),this.meshes.delete(e);!Array.from(this.meshes.values()).some(e=>e.config.animated)&&this.updateHandler&&(this.app.off("update",this.updateHandler),this.updateHandler=null)}getMesh(e){return this.meshes.get(e)}updateVisibility(e,t){this.meshes.forEach(o=>{const n=o.config;if(n.visibilityRange){const i=n.visibilityRange;let a=!0;"percentage"===i.type?a=e>=i.start&&e<=i.end:"waypoint"===i.type&&(a=t>=i.start&&t<=i.end),o.entity.enabled=a}if(n.billboard&&n.billboardRange){const i=n.billboardRange;let a=!1;"percentage"===i.type?a=e>=i.start&&e<=i.end:"waypoint"===i.type&&(a=t>=i.start&&t<=i.end),o.entity._billboardActive=a}else n.billboard&&(o.entity._billboardActive=!0)})}getAllMeshes(){return this.meshes}destroy(){this.meshes.forEach((e,t)=>this.destroyMesh(t)),this.updateHandler&&(this.app.off("update",this.updateHandler),this.updateHandler=null)}}class X{constructor(e){this.isInitialized=!1,this.scriptCleanup=[],this.lastError=null,this.updateCallbacks=[],this.customScript=e,this.api={}}initialize(e){this.api={...e,registerCleanup:e=>this.addCleanup(e)},this.isInitialized=!0}updateScript(e){e!==this.customScript&&(this.customScript=e,this.execute())}addCleanup(e){"function"==typeof e&&this.scriptCleanup.push(e)}sanitizeScript(e){if(!e)return"";let t="function"==typeof e.normalize?e.normalize("NFC"):e;return t=t.replace(/\uFEFF/g,""),t=t.replace(/\u00A0/g," "),t=t.replace(/[\u2028\u2029]/g,"\n"),t=t.replace(/[\u200B-\u200D\u2060]/g,""),t=t.replace(/[\u2018\u2019\u201B]/g,"'").replace(/[\u201C\u201D\u201E]/g,'"'),t}preprocessScript(e){if(!e)return"";let t=this.sanitizeScript(e).trim().replace(/\r\n/g,"\n");return/console\/log\s*\(/.test(t)&&(console.warn("[Custom Script] Detected 'console/log(...)'. Did you mean 'console.log(...)'?"),t=t.replace(/console\/log\s*\(/g,"console.log(")),t="try {\n"+t+"\n} catch (error) {\n console.error('[Custom Script] Runtime error:', error);\n}",t}execute(){if(!this.isInitialized||!this.customScript)return;if(this.customScript.length>2e5)return void console.warn("[Custom Script] Script too large, aborting execution.");this.cleanup();const e=this.preprocessScript(this.customScript);try{const t=this.api.app;let o=!1;const n=()=>{o||(this.updateCallbacks.length>200?(console.warn("[Custom Script] Too many update callbacks; further callbacks ignored."),o=!0):requestAnimationFrame(n))};requestAnimationFrame(n);const i=Object.create(t);i.registerUpdate=e=>{if("function"!=typeof e||o)return;const n=()=>{try{e()}catch(e){console.error("[Custom Script] Error in update callback:",e)}};this.updateCallbacks.push(n),t.on("update",n),this.addCleanup(()=>{t.off("update",n);const e=this.updateCallbacks.indexOf(n);-1!==e&&this.updateCallbacks.splice(e,1)})},i.registerBeforeRender=i.registerUpdate;const{camera:a,pc:s,canvas:r,getScrollPercentage:l,getCurrentWaypointIndex:c,getHotspots:d,getSplats:p,getHTMLMeshes:h}=this.api,u=new Function("app","camera","pc","canvas","getScrollPercentage","getCurrentWaypointIndex","getHotspots","getSplats","getHTMLMeshes","registerCleanup","registerUpdate","exports","module","require","globalThis","window","document","self","'use strict';\n"+e),m={},g={exports:m},y=()=>{throw new Error("require not available in custom script")},f=new Proxy({},{get:()=>{},set:()=>!1}),v=u(i,a,s,r,l,c,d,p,h,e=>this.addCleanup(e),i.registerUpdate.bind(i),m,g,y,f,void 0,void 0,void 0)||g.exports||m.default||m.cleanup;"function"==typeof v&&this.addCleanup(v),console.log("[Custom Script] Executed successfully")}catch(e){this.lastError=e,console.error("[Custom Script] Execution error:",e)}}cleanup(){this.scriptCleanup.forEach(e=>{try{e()}catch(e){console.error("[Custom Script] Cleanup error:",e)}}),this.scriptCleanup=[],this.updateCallbacks=[]}getLastError(){return this.lastError}dispose(){this.cleanup(),this.isInitialized=!1}}class Z{constructor(e,t,o={}){this.options=o,this.frameAssets=new Map,this.activeEntityIndex=0,this.currentFrame=0,this.isPlaying=!1,this.lastFrameTime=0,this.loadingFrames=new Set,this.destroyed=!1,this.listeners=new Map,this.app=e,this.frameUrls=t.frameUrls,this.fps=t.fps||24,this.loop=!1!==t.loop,this.preloadCount=t.preloadCount||5,this.frameInterval=1e3/this.fps,this.entities=[this.createSplatEntity("frameEntityA"),this.createSplatEntity("frameEntityB")],this.entities[0].enabled=!1,this.entities[1].enabled=!1,this.preloadInitialFrames(),t.autoplay&&this.preloadFrame(0).then(()=>{this.destroyed||this.play()})}createSplatEntity(t){const o=new e.Entity(t);return o.addComponent("gsplat",{}),this.app.root.addChild(o),o}async preloadInitialFrames(){const e=Math.min(this.preloadCount,this.frameUrls.length),t=[];for(let o=0;o<e;o++)t.push(this.preloadFrame(o));await Promise.all(t),this.options.onLoadProgress?.(e,this.frameUrls.length)}async preloadFrame(t){return this.destroyed||t<0||t>=this.frameUrls.length?null:this.frameAssets.has(t)?this.frameAssets.get(t):this.loadingFrames.has(t)?null:(this.loadingFrames.add(t),new Promise(o=>{const n=this.frameUrls[t],i=new e.Asset(`frame_${t}`,"gsplat",{url:n});i.on("load",()=>{this.destroyed||(this.frameAssets.set(t,i),this.loadingFrames.delete(t)),o(i)}),i.on("error",e=>{console.error(`Failed to load frame ${t}:`,e),this.loadingFrames.delete(t),this.options.onError?.(`Failed to load frame ${t}: ${e}`),o(null)}),this.app.assets.add(i),this.app.assets.load(i)}))}unloadFrame(e){const t=this.frameAssets.get(e);t&&(this.app.assets.remove(t),t.unload(),this.frameAssets.delete(e))}updatePreloadWindow(){for(let e=1;e<=this.preloadCount;e++){const t=this.loop?(this.currentFrame+e)%this.frameUrls.length:this.currentFrame+e;t<this.frameUrls.length&&this.preloadFrame(t)}for(const[e]of this.frameAssets){const t=this.currentFrame-e;t>2&&t<this.frameUrls.length-this.preloadCount&&this.unloadFrame(e)}}async displayFrame(e){if(this.destroyed)return;let t=this.frameAssets.get(e);if(!t&&(t=await this.preloadFrame(e),!t||this.destroyed))return;const o=(this.activeEntityIndex+1)%2,n=this.entities[this.activeEntityIndex],i=this.entities[o],a=i.gsplat;a&&(a.asset=t),i.enabled=!0,n.enabled=!1,this.activeEntityIndex=o,this.currentFrame=e,this.emit("frameChange",e,this.frameUrls.length),this.options.onFrameChange?.(e,this.frameUrls.length),this.updatePreloadWindow()}update(e){if(!this.isPlaying||this.destroyed)return;const t=performance.now(),o=t-this.lastFrameTime;if(o>=this.frameInterval){this.lastFrameTime=t-o%this.frameInterval;let e=this.currentFrame+1;if(e>=this.frameUrls.length){if(!this.loop)return this.pause(),void this.emit("complete");e=0}this.displayFrame(e)}}play(){this.isPlaying||this.destroyed||(this.isPlaying=!0,this.lastFrameTime=performance.now(),this.entities[this.activeEntityIndex].enabled||this.displayFrame(this.currentFrame),this.emit("play"))}pause(){this.isPlaying=!1,this.emit("pause")}stop(){this.isPlaying=!1,this.currentFrame=0,this.displayFrame(0),this.emit("stop")}setFrame(e){e<0&&(e=0),e>=this.frameUrls.length&&(e=this.frameUrls.length-1),this.displayFrame(e)}nextFrame(){let e=this.currentFrame+1;e>=this.frameUrls.length&&(e=this.loop?0:this.frameUrls.length-1),this.setFrame(e)}previousFrame(){let e=this.currentFrame-1;e<0&&(e=this.loop?this.frameUrls.length-1:0),this.setFrame(e)}getCurrentFrame(){return this.currentFrame}getTotalFrames(){return this.frameUrls.length}getProgress(){return this.frameUrls.length>1?this.currentFrame/(this.frameUrls.length-1):0}setProgress(e){const t=Math.round(e*(this.frameUrls.length-1));this.setFrame(t)}getFps(){return this.fps}setFps(e){this.fps=e,this.frameInterval=1e3/e}getIsPlaying(){return this.isPlaying}setLoop(e){this.loop=e}getLoop(){return this.loop}setPosition(e,t,o){this.entities[0].setPosition(e,t,o),this.entities[1].setPosition(e,t,o)}setRotation(e,t,o){this.entities[0].setEulerAngles(e,t,o),this.entities[1].setEulerAngles(e,t,o)}setScale(e,t,o){this.entities[0].setLocalScale(e,t,o),this.entities[1].setLocalScale(e,t,o)}on(e,t){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(t)}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,...t){this.listeners.get(e)?.forEach(e=>e(...t))}destroy(){this.destroyed=!0,this.isPlaying=!1;for(const[e]of this.frameAssets)this.unloadFrame(e);this.entities[0].destroy(),this.entities[1].destroy(),this.listeners.clear()}}class J{constructor(){this.listeners=new Map}on(e,t){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(t)}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,...t){this.listeners.get(e)?.forEach(e=>e(...t))}}function K(){const e=navigator.userAgent||navigator.vendor||window.opera||"";return/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(e)}const Q={"desktop-max":{range:[0,5],lodDistances:[15,30,80,250,300]},desktop:{range:[0,2],lodDistances:[15,30,80,250,300]},"mobile-max":{range:[1,2],lodDistances:[15,30,80,250,300]},mobile:{range:[2,5],lodDistances:[15,30,80,250,300]}};function ee(e){const t=e.includes("lod-meta.json");return t&&console.log("[SPLAT] Detected LOD streaming format (lod-meta.json)"),t}function te(t,o,n={}){if(n.lazyLoad){const e=new J;let i=null;const a=n.lazyLoadThumbnail||o.thumbnailUrl,s=n.lazyLoadButtonText||o.uiOptions?.lazyLoadButtonText||o.uiOptions?.buttonLabels?.startExperience||"Start Experience",r=o.uiColor||"#4CAF50";console.log("[StorySplat Viewer] Lazy loading enabled, showing start button"),function(e,t){const{thumbnailUrl:o,thumbnailType:n,buttonText:i="Start Experience",uiColor:a="#4CAF50",onStart:s}=t;c(a),e.classList.add("storysplat-viewer-container");const r=document.createElement("div");r.className="storysplat-lazy-load-container";const l=n||(o?function(e){const t=e.toLowerCase();return t.includes(".mp4")||t.includes(".webm")||t.includes(".mov")||t.includes(".ogg")?"video":t.includes(".gif")?"gif":"image"}(o):"image");let d="";o&&(d+="video"===l?`<video class="storysplat-lazy-load-thumbnail-video" src="${o}" autoplay muted loop playsinline webkit-playsinline></video>`:`<img class="storysplat-lazy-load-thumbnail" src="${o}" alt="Scene preview" />`),d+='<div class="storysplat-lazy-load-overlay"></div>',d+=`\n <div class="storysplat-lazy-load-content">\n <button class="storysplat-lazy-load-start-btn" style="background: ${a}">\n <svg viewBox="0 0 24 24">\n <path d="M8 5v14l11-7z"/>\n </svg>\n ${i}\n </button>\n </div>\n `,r.innerHTML=d,e.appendChild(r);const p=r.querySelector(".storysplat-lazy-load-start-btn");p?.addEventListener("click",()=>{const e=r.querySelector("video");e&&(e.pause(),e.src=""),r.style.transition="opacity 0.3s ease-out",r.style.opacity="0",setTimeout(()=>{r.remove(),s()},300)})}(t,{thumbnailUrl:a,thumbnailType:n.lazyLoadThumbnailType,buttonText:s,uiColor:r,onStart:()=>{console.log("[StorySplat Viewer] User clicked start, initializing viewer..."),i=te(t,o,{...n,lazyLoad:!1}),i.on("ready",()=>e.emit("ready")),i.on("error",t=>e.emit("error",t)),i.on("waypointChange",t=>e.emit("waypointChange",t)),i.on("playbackStart",()=>e.emit("playbackStart")),i.on("playbackStop",()=>e.emit("playbackStop")),i.on("loaded",()=>e.emit("loaded")),i.on("progress",t=>e.emit("progress",t))}});return{app:null,canvas:null,goToWaypoint:e=>i?.goToWaypoint(e),nextWaypoint:()=>i?.nextWaypoint(),prevWaypoint:()=>i?.prevWaypoint(),getCurrentWaypointIndex:()=>i?.getCurrentWaypointIndex()??0,getWaypointCount:()=>i?.getWaypointCount()??0,setPosition:(e,t,o)=>i?.setPosition(e,t,o),setRotation:(e,t,o)=>i?.setRotation(e,t,o),getPosition:()=>i?.getPosition()??{x:0,y:0,z:0},getRotation:()=>i?.getRotation()??{x:0,y:0,z:0},play:()=>i?.play(),pause:()=>i?.pause(),stop:()=>i?.stop(),isPlaying:()=>i?.isPlaying()??!1,goToOriginalSplat:()=>i?.goToOriginalSplat(),getCurrentSplatUrl:()=>i?.getCurrentSplatUrl()??"",isShowingOriginalSplat:()=>i?.isShowingOriginalSplat()??!0,destroy:()=>{i?i.destroy():(t.querySelectorAll(".storysplat-lazy-load-container, .storysplat-viewer-container").forEach(e=>e.remove()),t.classList.remove("storysplat-viewer-container"))},resize:()=>i?.resize(),navigateToScene:async e=>{if(i)return i.navigateToScene(e)},on:(t,o)=>e.on(t,o),off:(t,o)=>e.off(t,o)}}const i=new J,r=s(a(o));console.log("[StorySplat Viewer] Creating viewer with config:",r),console.log("[StorySplat Viewer] Scale config:",r.scale),console.log("[StorySplat Viewer] Raw scene data:",{splatScale:o.splatScale,scale:o.scale});const w=!1!==n.showUI,x=r.uiColor||"#4CAF50",S=r.uiOptions||{},E=e=>"first-person"===e?"tour":"drone"===e?"explore":e,M=r.collisionMeshesData&&r.collisionMeshesData.length>0;let C=(r.allowedCameraModes||["orbit","first-person","drone"]).map(E).filter((e,t,o)=>o.indexOf(e)===t);M&&!C.includes("walk")&&C.push("walk"),!M&&C.includes("walk")&&(C=C.filter(e=>"walk"!==e));const T=E(r.defaultCameraMode||"orbit");let A={};w&&(A=function(e,t,o={}){const{uiColor:n="#4CAF50",showScrollControls:i=!0,showModeToggle:a=!0,showFullscreenButton:s=!0,showHelpButton:r=!1,showPreloader:p=!0,allowedCameraModes:h=["tour","explore"],defaultCameraMode:u="tour",customPreloaderLogoUrl:m,buttonLabels:g,hideWatermark:y=!1,watermarkText:f,watermarkLink:v,sceneId:b}=o,w={tour:l(g,"tour"),explore:l(g,"explore"),walk:l(g,"walk"),previous:l(g,"previous"),next:l(g,"next"),helpTitle:l(g,"helpTitle"),returnToTour:l(g,"returnToTour")},x={};e.querySelectorAll(".storysplat-preloader, .storysplat-scroll-controls, .storysplat-waypoint-info, .storysplat-fullscreen-btn, .storysplat-help-btn, .storysplat-help-panel, .storysplat-watermark").forEach(e=>e.remove()),c(n),e.classList.add("storysplat-viewer-container"),p&&(x.preloader=d(e,m));const S=document.createElement("div");if(S.className="storysplat-waypoint-info",S.innerHTML='\n <h2 class="storysplat-waypoint-title"></h2>\n <p class="storysplat-waypoint-description"></p>\n ',e.appendChild(S),x.waypointInfo=S,i&&t.waypoints&&t.waypoints.length>0){const t=document.createElement("div");t.className="storysplat-scroll-controls",t.innerHTML=`\n <div class="storysplat-scroll-content">\n <div class="storysplat-progress-text">0%</div>\n <div class="storysplat-progress-container">\n <div class="storysplat-progress-bar"></div>\n </div>\n <div class="storysplat-scroll-buttons">\n <button class="storysplat-btn storysplat-btn-prev">${w.previous}</button>\n <button class="storysplat-btn storysplat-btn-play">\n <svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>\n </button>\n <button class="storysplat-btn storysplat-btn-next">${w.next}</button>\n </div>\n ${a&&h.length>1?`\n <div class="storysplat-mode-container">\n <div class="storysplat-mode-toggle">\n ${h.includes("tour")?`<button class="storysplat-mode-btn ${"tour"===u?"selected":""}" data-mode="tour">${w.tour}</button>`:""}\n ${h.includes("explore")?`<button class="storysplat-mode-btn ${"explore"===u?"selected":""}" data-mode="explore">${w.explore}</button>`:""}\n ${h.includes("walk")?`<button class="storysplat-mode-btn ${"walk"===u?"selected":""}" data-mode="walk">${w.walk}</button>`:""}\n </div>\n </div>\n `:""}\n </div>\n `,e.appendChild(t),x.scrollControls=t,x.progressBar=t.querySelector(".storysplat-progress-bar"),x.progressText=t.querySelector(".storysplat-progress-text")}if(s){const t=document.createElement("button");t.className="storysplat-fullscreen-btn",t.setAttribute("aria-label","Toggle Fullscreen"),t.innerHTML='\n <svg class="storysplat-expand-icon" viewBox="0 0 24 24">\n <path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/>\n </svg>\n <svg class="storysplat-compress-icon" viewBox="0 0 24 24" style="display: none;">\n <path d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z"/>\n </svg>\n ',e.appendChild(t),x.fullscreenButton=t}const E=document.createElement("button");E.className="storysplat-xr-btn storysplat-vr-btn",E.setAttribute("aria-label","Enter VR"),E.textContent="VR",e.appendChild(E),x.vrButton=E;const M=document.createElement("button");if(M.className="storysplat-xr-btn storysplat-ar-btn",M.setAttribute("aria-label","Enter AR"),M.textContent="AR",e.appendChild(M),x.arButton=M,r){const t=document.createElement("button");t.className="storysplat-help-btn",t.setAttribute("title","Toggle Help"),t.textContent="?",e.appendChild(t),x.helpButton=t;const o=document.createElement("div");o.className="storysplat-help-panel",o.innerHTML=`\n <h3>${w.helpTitle}</h3>\n <p><strong>Camera Modes:</strong></p>\n <p>• ${w.tour} - Follow predefined path</p>\n <p>• ${w.explore} - Free movement</p>\n <p>• ${w.walk} - First-person walking</p>\n <br/>\n <p><strong>${w.tour} Mode:</strong></p>\n <p>• Scroll - Move along path</p>\n <p>• Drag - Look around</p>\n <br/>\n <p><strong>${w.explore} Mode:</strong></p>\n <p>• LMB Drag - Orbit camera</p>\n <p>• RMB Drag - Fly/look</p>\n <p>• WASD/QE - Move camera</p>\n <p>• Shift - Move fast</p>\n <p>• Scroll/Pinch - Zoom</p>\n <p>• Double-click - Focus</p>\n <br/>\n <p><strong>${w.walk} Mode:</strong></p>\n <p>• Click to lock mouse</p>\n <p>• WASD/Arrows - Move</p>\n <p>• Mouse - Look around</p>\n <p>• Shift - Sprint</p>\n <p>• Space - Jump</p>\n `,e.appendChild(o),x.helpPanel=o}const C=document.createElement("div");C.className="storysplat-hotspot-popup",C.id="hotspotContent",C.innerHTML='\n <h2 class="storysplat-hotspot-popup-title"></h2>\n <div class="storysplat-hotspot-popup-content"></div>\n <button class="storysplat-hotspot-popup-close">Close</button>\n ',e.appendChild(C),x.hotspotPopup=C;const _=C.querySelector(".storysplat-hotspot-popup-close");_?.addEventListener("click",()=>{C.classList.remove("visible","fullscreen")});const L=document.createElement("div");L.className="storysplat-joystick-container",L.innerHTML='\n <div class="storysplat-joystick-base"></div>\n <div class="storysplat-joystick-thumb"></div>\n ',e.appendChild(L),x.joystick=L,x.joystickThumb=L.querySelector(".storysplat-joystick-thumb");const T=document.createElement("div");T.className="storysplat-look-zone",T.innerHTML='\n <div class="storysplat-look-zone-icon">\n <svg viewBox="0 0 24 24">\n <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/>\n </svg>\n </div>\n ',e.appendChild(T),x.lookZone=T;const k=document.createElement("button");k.className="storysplat-camera-mode-toggle fly",k.setAttribute("aria-label","Toggle camera mode"),k.innerHTML='\n <svg class="orbit-icon" viewBox="0 0 24 24">\n <path d="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01-.25 1.97-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0-4.42-3.58-8-8-8zm0 14c-3.31 0-6-2.69-6-6 0-1.01.25-1.97.7-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4-4-4-4v3z"/>\n </svg>\n <svg class="fly-icon" viewBox="0 0 24 24">\n <path d="M12 2L4.5 20.29l.71.71L12 18l6.79 3 .71-.71z"/>\n </svg>\n ',e.appendChild(k),x.cameraModeToggle=k;const A=document.createElement("button");if(A.className="storysplat-return-waypoint-btn",A.setAttribute("aria-label",w.returnToTour),A.innerHTML=`\n <svg viewBox="0 0 24 24">\n <path d="M9 11H7v2h2v-2zm4 0h-2v2h2v-2zm4 0h-2v2h2v-2zm2-7h-1V2h-2v2H8V2H6v2H5c-1.11 0-1.99.9-1.99 2L3 20c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11z"/>\n </svg>\n ${w.returnToTour}\n `,e.appendChild(A),x.returnWaypointButton=A,!y){const t=document.createElement("div");t.className="storysplat-watermark";const o=v||(b?`https://storysplat.com?ref=${b}`:"https://storysplat.com");t.innerHTML=f?`<a href="${o}" target="_blank">${f}</a>`:`Created with <a href="${o}" target="_blank">StorySplat</a>`,e.appendChild(t),x.watermark=t}return x}(t,r,{uiColor:x,showScrollControls:r.waypoints&&r.waypoints.length>0,showModeToggle:C.length>1,showFullscreenButton:!S.hideFullscreenButton,showHelpButton:!S.hideHelpButton&&!S.hideInfoButton,showPreloader:!0,allowedCameraModes:C,defaultCameraMode:T,buttonLabels:S.buttonLabels,customPreloaderLogoUrl:S.customPreloaderLogoUrl,hideWatermark:S.hideWatermark,watermarkText:S.watermarkText,watermarkLink:S.watermarkLink,sceneId:o.sceneId}));const z=document.createElement("canvas");let R;z.id="storysplat-viewer-canvas",z.style.width="100%",z.style.height="100%",z.style.display="block",t.appendChild(z);const I={antialias:!1,alpha:!1,powerPreference:"high-performance"};try{R=new e.Application(z,{graphicsDeviceOptions:I,mouse:new e.Mouse(z),touch:new e.TouchDevice(z),keyboard:new e.Keyboard(window)}),console.log("[StorySplat Viewer] Graphics initialized successfully")}catch(o){console.warn("[StorySplat Viewer] WebGL2 initialization failed, trying WebGL1 fallback:",o);try{R=new e.Application(z,{graphicsDeviceOptions:{...I,preferWebGl2:!1},mouse:new e.Mouse(z),touch:new e.TouchDevice(z),keyboard:new e.Keyboard(window)}),console.log("[StorySplat Viewer] WebGL1 fallback successful")}catch(e){console.error("[StorySplat Viewer] WebGL initialization failed completely:",e);const o=document.createElement("div");o.style.cssText="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);color:#fff;background:rgba(0,0,0,0.8);padding:20px;border-radius:10px;text-align:center;font-family:sans-serif;";const n=document.createElement("h3");n.style.cssText="margin:0 0 10px 0;",n.textContent="Unable to Initialize 3D Graphics";const i=document.createElement("p");throw i.style.cssText="margin:0;",i.textContent="Your browser or device may not support WebGL. Please try a different browser or device.",o.appendChild(n),o.appendChild(i),t.appendChild(o),new Error("WebGL initialization failed - browser may not support WebGL")}}z.addEventListener("webglcontextlost",e=>{e.preventDefault(),console.error("[StorySplat Viewer] WebGL context lost"),i.emit("error",new Error("WebGL context lost"))},!1),z.addEventListener("webglcontextrestored",()=>{console.log("[StorySplat Viewer] WebGL context restored")},!1),R.setCanvasFillMode(e.FILLMODE_FILL_WINDOW),R.setCanvasResolution(e.RESOLUTION_AUTO),R.start(),console.log("[StorySplat Viewer] App started");const D=K(),F=D?"mobile":"desktop",U=Q[F];console.log("[SPLAT] Initializing LOD system for device:",D?"mobile":"desktop"),R.scene.gsplat?(R.scene.gsplat.lodUpdateAngle=90,R.scene.gsplat.lodBehindPenalty=2,R.scene.gsplat.radialSorting=!0,R.scene.gsplat.lodUpdateDistance=1,R.scene.gsplat.lodUnderfillLimit=10,R.scene.gsplat.lodRangeMin=U.range[0],R.scene.gsplat.lodRangeMax=U.range[1],R.scene.gsplat.colorUpdateDistance=1,R.scene.gsplat.colorUpdateAngle=4,R.scene.gsplat.colorUpdateDistanceLodScale=2,R.scene.gsplat.colorUpdateAngleLodScale=2,console.log("[SPLAT] LOD system configured:",{preset:F,lodRangeMin:U.range[0],lodRangeMax:U.range[1],lodDistances:U.lodDistances,lodUpdateAngle:90,lodUpdateDistance:1,isMobile:D})):console.warn("[SPLAT] GSplat scene settings not available - LOD may not work optimally");let B=0,V=!1,$=null,H=null,O=!1,W=null;const G=r.additionalSplats||[],N=r.keepMeshesInMemory??!1,j=r.initialSplatExploreMode||"fly";let oe=null,ie=!1;const ae=new Map;let se=-1,re=-1;const le=new e.Entity("camera");let ce=new e.Color(.1,.1,.1);if(r.backgroundColor){const t=r.backgroundColor.replace("#","");if(6===t.length){const o=parseInt(t.substring(0,2),16)/255,n=parseInt(t.substring(2,4),16)/255,i=parseInt(t.substring(4,6),16)/255;ce=new e.Color(o,n,i),console.log("[StorySplat Viewer] Background color set from config:",r.backgroundColor)}}le.addComponent("camera",{clearColor:ce,fov:r.fov||60,nearClip:r.nearClip||.1,farClip:r.farClip||1e3}),le.addComponent("audiolistener"),console.log("[StorySplat Viewer] Camera settings:",{fov:r.fov,nearClip:r.nearClip,farClip:r.farClip,playerHeight:r.playerHeight});const de=r.playerHeight||1.6;if(r.waypoints&&r.waypoints.length>0){const e=r.waypoints[0];if(console.log("[StorySplat Viewer] First waypoint raw:",e),e.position){const t=e.position;le.setPosition(t.x,t.y,-t.z),console.log("[StorySplat Viewer] Camera position (Z negated):",{x:t.x,y:t.y,z:-t.z})}else le.setPosition(0,de,5);if(e.rotation){const t=Ce(e.rotation);le.setRotation(t),console.log("[StorySplat Viewer] Camera rotation set from waypoint")}}else le.setPosition(0,de,5),le.lookAt(new e.Vec3(0,0,0)),console.log("[StorySplat Viewer] Camera set to default position (0, playerHeight, 5)");R.root.addChild(le);const pe=new e.Entity("light");pe.addComponent("light",{type:e.LIGHTTYPE_DIRECTIONAL,color:new e.Color(1,1,1),intensity:1,castShadows:!1}),pe.setEulerAngles(45,45,0),R.root.addChild(pe),console.log("[StorySplat Viewer] Light added");const he=new _(le,R,{moveSpeed:2*(r.cameraMovementSpeed||1),moveFastSpeed:5*(r.cameraMovementSpeed||1),moveSlowSpeed:1*(r.cameraMovementSpeed||1),rotateSpeed:5e-4*(r.cameraRotationSensitivity||.2),enableOrbit:!0,enableFly:!0,enablePan:!0,invertRotation:r.invertCameraRotation,moveDamping:.75,rotateDamping:.75,zoomDamping:.8});let ue=null;r.collisionMeshesData&&r.collisionMeshesData.length>0&&(ue=new L(le,R,{moveSpeed:2*(r.cameraMovementSpeed||1),sprintMultiplier:2,lookSensitivity:.005*(r.cameraRotationSensitivity||.2),playerHeight:r.playerHeight||1.6,gravity:20,jumpVelocity:8,collisionRadius:.3,stepHeight:.3}),ue.createCollisionMeshes(r.collisionMeshesData).then(()=>{console.log("[StorySplat Viewer] Collision meshes loaded for walk mode")}).catch(e=>{console.error("[StorySplat Viewer] Failed to load collision meshes:",e)}));let me=T,ge=!0;"tour"===T&&he.disable();const ye=new e.Picker(R,1,1,!0);let fe=!1,ve=null;let be=null,we=null,xe=!1;function Se(){be&&(be.enabled=!1,xe=!1)}function Ee(e){"walk"===me&&ue?ue.disable():"explore"===me&&he.disable(),me=e,console.log("[StorySplat Viewer] Switching to mode:",e);const t=K();if("walk"===e&&ue)ge=!1,he.disable(),ue.enable(),m(A,!1),f(A,!1),r.waypoints&&r.waypoints.length>0&&b(A,!0);else if("explore"===e){ue&&ue.disable(),Ge=0,Ne=0,je=!1,ge=!1,he.disable(),he.enable(),he.enableOrbit=!0,he.enableFly=!0,he.enablePan=!0,He&&Oe?(he.syncFromPose(He,Oe),console.log("[StorySplat Viewer] Synced camera from waypoint pose for explore mode")):he.syncFromCamera();(async()=>{try{const e=.25;ye.resize(Math.floor(Yt.clientWidth*e),Math.floor(Yt.clientHeight*e));const t=R.scene.layers.getLayerByName("World");if(t){ye.prepare(le.camera,R.scene,[t]);const o=Math.floor(.5*Yt.clientWidth*e),n=Math.floor(.5*Yt.clientHeight*e),i=await ye.getWorldPointAsync(o,n);if(i){const e=le.getPosition().distance(i);e>.5&&e<500&&(he.syncFromCamera(i),console.log("[StorySplat Viewer] Updated focus target at distance:",e.toFixed(2)))}}}catch(e){}})(),t?(m(A,!0),f(A,!0),he.setMode("fly"),v(A,"fly")):he.setMode("orbit"),r.waypoints&&r.waypoints.length>0&&b(A,!0)}else he.disable(),ue&&ue.disable(),ge=!0,m(A,!1),f(A,!1),b(A,!1);i.emit("modeChange",{mode:e})}R.on("update",t=>{"walk"===me&&ue?ue.update(t):he.update(t),ge||function(){const e=le.getPosition();ut.forEach(t=>{const o=t.hotspotData;if(!o)return;if("proximity"!==(t.mediaTriggerMode||"click"))return;const n=t.getPosition(),i=e.distance(n)<=(t.proximityDistance||5),a=t.wasInProximity||!1;if(i&&!a){if("video"===o.type&&t.videoElement)Ot(t,o);else if("gif"===o.type)t.enabled=!0;else if(("sphere"===o.type||"image"===o.type)&&t.audioElements){const e=t.audioElements,n=e.audio;n&&n.paused&&(e.audioCtx&&"suspended"===e.audioCtx.state&&e.audioCtx.resume(),n.play().catch(e=>console.error("[Audio] Hotspot audio play failed (proximity):",e)),console.log("[Audio] Hotspot audio started (proximity):",o.title))}t.wasInProximity=!0}else if(!i&&a){if("video"===o.type&&t.videoElement)Wt(t);else if("gif"===o.type)t.enabled=!1;else if(("sphere"===o.type||"image"===o.type)&&t.audioElements){const e=t.audioElements.audio;e&&!e.paused&&(e.pause(),console.log("[Audio] Hotspot audio stopped (proximity):",o.title))}t.wasInProximity=!1}})}(),function(){if(0===ft.size)return;const e=le.getPosition();ft.forEach((t,o)=>{const{entity:n,config:i,slotId:a,assetReady:s,playing:r}=t;if(!s)return;const l=n.sound?.slot(a);if(l&&i.spatialSound){const a=n.getPosition(),s=e.distance(a),c=i.maxDistance||20;s<=c&&!r?(console.log(`[Audio] Proximity play: ${o}, distance=${s.toFixed(2)}, maxDistance=${c}`),l.play(),t.playing=!0):s>c&&r&&i.stopOnExit&&(console.log(`[Audio] Proximity stop: ${o}, distance=${s.toFixed(2)}`),l.stop(),t.playing=!1)}})}(),function(){if(0===Ft.size)return;const e=le.getPosition();Ft.forEach((t,o)=>{const{entity:n,config:i,slotId:a,assetReady:s,playing:r}=t;if(!s)return;const l=n.sound?.slot(a);if(l&&!1!==i.spatialSound){const o=n.getPosition();e.distance(o)<=(i.maxDistance||100)&&!r&&!1!==i.autoplay&&(l.isPlaying||(l.play(),t.playing=!0))}})}(),function(){if(!r.waypoints?.length)return;const t=le.getPosition();r.waypoints.forEach((o,n)=>{const i=new e.Vec3(o.position?.x??0,o.position?.y??0,-(o.position?.z??0)),a=t.distance(i),s=o.triggerDistance??1;a<=s?Ut.has(n)||(Ut.add(n),console.log(`[StorySplat] Waypoint ${n} triggered (distance: ${a.toFixed(2)}, threshold: ${s})`),function(e){if(!e.interactions?.length)return;e.interactions.forEach(e=>{if("audio"===e.type){const t=e.id,o=ft.get(t);if(o&&o.assetReady&&!o.playing){const e=o.entity.sound?.slot(o.slotId);e&&(e.play(),o.playing=!0)}}})}(o)):Ut.has(n)&&(Ut.delete(n),console.log(`[StorySplat] Waypoint ${n} exited`),function(e){if(!e.interactions?.length)return;e.interactions.forEach(e=>{if("audio"===e.type){if(e.data?.stopOnExit??!1){const t=e.id,o=ft.get(t);if(o&&o.playing){const e=o.entity.sound?.slot(o.slotId);e&&(e.stop(),o.playing=!1)}}}})}(o))})}(),fe&&xe&&function(){if(be?.enabled&&le){const e=le.forward.clone(),t=le.getPosition(),o=t.clone().add(e.mulScalar(1.5));o.y-=.1,be.setPosition(o),be.lookAt(t),be.rotateLocal(90,0,0)}}()}),R.on("joystick:left",(e,t,o,n)=>{if(e<0||t<0)g(A,!1,0,0,60);else{g(A,!0,o-e,n-t,60)}}),R.on("joystick:right",(e,t,o,n)=>{y(A,!(e<0||t<0))}),A.cameraModeToggle&&A.cameraModeToggle.addEventListener("click",()=>{if("explore"!==me)return;const e=he.mode;"orbit"===e||"focus"===e?(he.setMode("fly"),v(A,"fly"),m(A,!0)):(he.setMode("orbit"),v(A,"orbit"),m(A,!1))}),R.on("cameracontrols:modechange",e=>{"orbit"!==e&&"fly"!==e||(v(A,e),D&&"explore"===me&&m(A,"fly"===e))});const Me=(e,t)=>{A.preloader&&function(e,t,o){const n=e.querySelector(".storysplat-preloader-bar"),i=e.querySelector(".storysplat-preloader-text"),a=Math.max(0,Math.min(100,100*t));n&&(n.style.width=`${a}%`),i&&(i.textContent=o||`Loading... ${Math.round(a)}%`)}(A.preloader,e,t),i.emit("progress",{progress:e,text:t})};function Ce(t){if("_w"in t||"w"in t){const o=t._x??t.x??0,n=t._y??t.y??0,i=t._z??t.z??0,a=t._w??t.w??1;return new e.Quat(-o,-n,i,a)}if("x"in t&&"y"in t&&"z"in t){const o=new e.Quat;return o.setFromEulerAngles(t.x||0,t.y||0,t.z||0),o}return new e.Quat}function _e(e){e.enabled=!1,console.log("[SplatSwap] Splat hidden")}function Le(e){const t=ae.get(e);t&&(t.destroy(),ae.delete(e),console.log("[SplatSwap] Splat disposed:",e))}function Te(e,t=!1){if("explore"!==me)return;const o=t?j:e||j;if(o&&he){he.mode!==o&&(he.setMode(o),console.log(`[SplatSwap] Switching explore sub-mode to: ${o}`),D&&(v(A,o),m(A,"fly"===o)))}}async function ke(t){if(!G||0===G.length)return;const o=(t+1)%G.length,n=G[o];n&&n.url&&await async function(t){if(!ae.has(t)&&t!==oe&&!O){console.log("[SplatSwap] Preloading splat:",t);try{const o=new e.Asset("splat-preload-"+Date.now(),"gsplat",{url:t});await new Promise((n,i)=>{o.ready(()=>{if(O)return void i(new Error("Viewer destroyed"));const a=new e.Entity("splat-preload");a.addComponent("gsplat",{asset:o,unified:!0});const s=r.scale||{x:1,y:1,z:1},l=r.invertXScale||!1,c=r.invertYScale||!1,d={x:l?-s.x:s.x,y:c?-s.y:s.y,z:l!==c?-s.z:s.z};a.setLocalScale(d.x,d.y,d.z);const p=r.position||[0,0,0];a.setPosition(p[0],p[1],-p[2]);const h=r.rotation||[0,0,0],u=0!==h[0]||0!==h[1]||0!==h[2]?[h[0]*(180/Math.PI),h[1]*(180/Math.PI),-h[2]*(180/Math.PI)]:[180,0,0];a.setEulerAngles(u[0],u[1],u[2]),a.enabled=!1,R.root.addChild(a),ae.set(t,a),console.log("[SplatSwap] Preload complete:",t),n()}),o.on("error",e=>{console.error("[SplatSwap] Preload error:",e),i(e)}),R.assets.add(o),R.assets.load(o)})}catch(e){console.error("[SplatSwap] Error preloading:",t,e)}}}(n.url)}async function Ae(t){if(t!==oe&&!ie&&!O){ie=!0,console.log("[SplatSwap] Switching to splat:",t);try{if(oe&&ae.has(oe))if(N){const e=ae.get(oe);e&&_e(e)}else Le(oe);else $&&($.enabled=!1);if(ae.has(t))!function(e){const t=ae.get(e);!!t&&(t.enabled=!0,oe=e,console.log("[SplatSwap] Splat shown:",e))}(t),i.emit("splatChange",{url:t,isOriginal:!1});else{const o=new e.Asset("splat-swap-"+Date.now(),"gsplat",{url:t});await new Promise((n,a)=>{o.ready(()=>{if(O)return void a(new Error("Viewer destroyed"));const s=new e.Entity("splat-swap");s.addComponent("gsplat",{asset:o,unified:!0});const l=r.scale||{x:1,y:1,z:1},c=r.invertXScale||!1,d=r.invertYScale||!1,p={x:c?-l.x:l.x,y:d?-l.y:l.y,z:c!==d?-l.z:l.z};s.setLocalScale(p.x,p.y,p.z);const h=r.position||[0,0,0];s.setPosition(h[0],h[1],-h[2]);const u=r.rotation||[0,0,0],m=0!==u[0]||0!==u[1]||0!==u[2]?[u[0]*(180/Math.PI),u[1]*(180/Math.PI),-u[2]*(180/Math.PI)]:[180,0,0];s.setEulerAngles(m[0],m[1],m[2]),R.root.addChild(s),ae.set(t,s),oe=t,i.emit("splatChange",{url:t,isOriginal:!1}),console.log("[SplatSwap] New splat loaded and shown:",t),n()}),o.on("error",e=>{console.error("[SplatSwap] Load error:",e),a(e)}),R.assets.add(o),R.assets.load(o)})}const o=G.findIndex(e=>e.url===t);-1!==o&&ke(o)}catch(e){console.error("[SplatSwap] Error switching splat:",e)}finally{ie=!1}}}function Pe(){return r.sogUrl?r.sogUrl:r.splatUrl?r.splatUrl:r.fallbackUrls&&r.fallbackUrls.length>0?r.fallbackUrls[0]:""}function ze(){if(!G||0===G.length)return;const t=r.waypoints?.length||1,o=100*Re,n=Math.round(Re*Math.max(1,t-1));if(Math.abs(o-se)<.1&&n===re)return;se=o,re=n;let a=null,s=null,l=-1/0,c=-1/0;for(const e of G)-1!==e.waypointIndex?n>=e.waypointIndex&&e.waypointIndex>l&&(l=e.waypointIndex,a=e):-1!==e.percentage&&o>=e.percentage&&e.percentage>c&&(c=e.percentage,s=e);const d=s||a,p=d&&"__ORIGINAL__"===d.url,h=Pe(),u=d?p?h:d.url:h;u&&u!==oe&&(u===h&&$&&!oe?oe=h:u===h&&$?(ae.forEach((e,t)=>{t!==h&&(N?_e(e):Le(t))}),$&&($.enabled=!0),oe=h,i.emit("splatChange",{url:h,isOriginal:!0}),console.log("[SplatSwap] Returned to primary splat"),Te(void 0,!0)):(Ae(u),d&&Te(d.defaultExploreMode,!1)),d&&d.skyboxUrl&&!p&&function(t,o=0){console.log("[SplatSwap] Applying skybox:",t,"rotation:",o);const n=new e.Asset("skybox-swap-"+Date.now(),"cubemap",{url:t},{type:e.TEXTURETYPE_RGBM,mipmaps:!0});n.ready(()=>{if(!O)try{if(R.scene.skybox=n.resource,R.scene.skyboxMip=0,0!==o){const t=new e.Quat;t.setFromEulerAngles(0,o*(180/Math.PI),0),R.scene.skyboxRotation=t}console.log("[SplatSwap] Skybox applied successfully")}catch(e){console.error("[SplatSwap] Error applying skybox:",e)}}),n.on("error",e=>{console.error("[SplatSwap] Skybox load error:",e)}),R.assets.add(n),R.assets.load(n)}(d.skyboxUrl,d.skyboxRotation||0))}let Re=0;const Ie=r.waypoints?.reduce((e,t)=>e+(t.duration||2e3),0)||1,De=r.waypoints?.length||1,Fe=Math.max(1,20*(De-1)),Ue=void 0!==r.autoplaySpeed?60*r.autoplaySpeed/Fe:1e3/Ie,Be=r.loopMode;let Ve;Ve=!0===Be?"loop":!1===Be?"none":"loop"===Be||"pingpong"===Be||"none"===Be?Be:"loop";let $e=1;console.log("[StorySplat Viewer] Playback config:",{loopMode:Ve,playbackSpeed:Ue,totalDuration:Ie,autoPlay:r.autoPlay,rawLoopMode:r.loopMode});let He=null,Oe=null;const We=.01+.1*(r.transitionSpeed||1);let Ge=0,Ne=0;let je=!1,qe=0,Ye=0;const Xe=[],Ze=[],Je=[],Ke=r.fov||60;let Qe=Ke;function et(t){if(!ge||Xe.length<2)return;t=Math.max(0,Math.min(1,t));const o=Xe.length,n=t*(o-1),a=Math.min(Math.floor(n),o-2),s=n-a,l=Xe[a],c=Xe[a+1];He=new e.Vec3(ne(l.x,c.x,s),ne(l.y,c.y,s),ne(l.z,c.z,s));const d=Ze[a],p=Ze[a+1];Oe=new e.Quat,Oe.slerp(d,p,s);const h=Je[a],u=Je[a+1];Qe=ne(h,u,s);const m=Math.round(t*(o-1));if(m!==B){const t=B;B=m;const o=r.waypoints[m],n=o.cameraMode||"first-person";"orbit"===n&&(o.orbitTarget?new e.Vec3(o.orbitTarget.x,o.orbitTarget.y,-(o.orbitTarget.z||0)):new e.Vec3(Xe[m].x,Xe[m].y,Xe[m].z)),i.emit("waypointChange",{index:m,waypoint:o,prevIndex:t,cameraMode:n}),g=m,y=t,ft.forEach((e,t)=>{const{entity:o,waypointIndex:n,config:i,slotId:a,autoplayTriggered:s}=e,r=o.sound?.slot(a);if(!r)return;const l=y===n&&g!==n;g===n&&y!==n&&i.autoplay&&!s&&(console.log(`[Audio] Autoplay waypoint audio: ${t} at waypoint ${n}`),r.isPlaying||(r.play(),e.playing=!0,e.autoplayTriggered=!0)),l&&i.stopOnExit&&e.playing&&(console.log(`[Audio] Stopping waypoint audio on exit: ${t}`),r.isPlaying&&(r.stop(),e.playing=!1,e.autoplayTriggered=!1))})}var g,y;i.emit("progressUpdate",{progress:Math.max(0,Math.min(1,t)),index:B}),ze()}function tt(e,t=!1){t?nt(e):(Re=Math.max(0,Math.min(1,e)),et(Re))}r.waypoints&&r.waypoints.length>0&&(r.waypoints.forEach(t=>{const o=t.position?new e.Vec3(t.position.x,t.position.y,-t.position.z):new e.Vec3(0,r.playerHeight||1.6,0);Xe.push(o);const n=t.rotation?Ce(t.rotation):new e.Quat;Ze.push(n);let i=t.fov||Ke;i<3.5&&(i*=180/Math.PI),Je.push(i)}),Xe.length>0&&(He=Xe[0].clone(),Oe=Ze[0].clone(),Qe=Je[0])),R.on("update",function(){if(!He||!Oe)return;if(!ge)return;const t=le.getPosition(),o=new e.Vec3;o.lerp(t,He,We),le.setPosition(o.x,o.y,o.z);const n=le.camera;if(n&&Je.length>0){const e=ne(n.fov,Qe,We);n.fov=e}je||(Ge*=.95,Ne*=.95,Math.abs(Ge)<.01&&(Ge=0),Math.abs(Ne)<.01&&(Ne=0));const i=new e.Quat;i.setFromEulerAngles(Ne,Ge,0);const a=new e.Quat;a.mul2(Oe,i);const s=le.getRotation(),r=new e.Quat;r.slerp(s,a,We),le.setRotation(r)});const ot=500*(r.transitionSpeed||1);function nt(e,t=ot){const o=Re,n=performance.now(),i=()=>{const a=performance.now()-n,s=Math.min(a/t,1);Re=o+(e-o)*(s<.5?2*s*s:(4-2*s)*s-1),et(Re),s<1&&requestAnimationFrame(i)};requestAnimationFrame(i)}function it(e){if(!r.waypoints||e<0||e>=r.waypoints.length)return;if(!ge)return;nt(e/Math.max(1,r.waypoints.length-1))}function at(){if(!r.waypoints||0===r.waypoints.length)return;const e=r.scrollButtonMode||"waypoint";if("percentage"===e||"continuous"===e||"incremental"===e){const e=r.scrollAmount||10;let t=Re+e/100;t>1&&(t="loop"===Ve?0:1),nt(t)}else{let e=B+1;e>=r.waypoints.length&&(e="loop"===Ve?0:r.waypoints.length-1),it(e)}}function st(){if(!r.waypoints||0===r.waypoints.length)return;const e=r.scrollButtonMode||"waypoint";if("percentage"===e||"continuous"===e||"incremental"===e){const e=r.scrollAmount||10;let t=Re-e/100;t<0&&(t="loop"===Ve?1:0),nt(t)}else{let e=B-1;e<0&&(e="loop"===Ve?r.waypoints.length-1:0),it(e)}}let rt=0,lt=null;function ct(e){if(!V)return;0===rt&&(rt=e);const t=(e-rt)/1e3;if(rt=e,Re+=Ue*t*$e,Re>=1)switch(console.log("[StorySplat Viewer] End of tour reached, loopMode:",Ve),Ve){case"loop":console.log("[StorySplat Viewer] Looping - restarting at beginning"),Re=0;break;case"pingpong":console.log("[StorySplat Viewer] Pingpong - reversing direction"),Re=1,$e=-1;break;case"none":return console.log("[StorySplat Viewer] No loop - stopping playback"),Re=1,pt(),void i.emit("playbackComplete");default:return console.log("[StorySplat Viewer] Unknown loopMode, stopping:",Ve),Re=1,pt(),void i.emit("playbackComplete")}else if(Re<=0)if("pingpong"===Ve)console.log("[StorySplat Viewer] Pingpong - reversing to forward"),Re=0,$e=1;else Re=0;et(Re),lt=requestAnimationFrame(ct)}function dt(){if(W)return W.play(),void i.emit("playbackStart");V||!r.waypoints||r.waypoints.length<2||(V=!0,rt=0,$e=1,i.emit("playbackStart"),lt=requestAnimationFrame(ct))}function pt(){if(W)return W.pause(),void i.emit("playbackStop");V=!1,lt&&(cancelAnimationFrame(lt),lt=null),i.emit("playbackStop")}const ht=R.graphicsDevice.canvas;ht.addEventListener("wheel",e=>{if(!ge)return;e.preventDefault();const t=r.scrollSpeed||.1,o=(r.scrollAmount||100)/100*t*.05,n=e.deltaY>0?o:-o;tt(Math.max(0,Math.min(1,Re+n)))},{passive:!1}),ht.addEventListener("pointerdown",e=>{ge&&(je=!0,qe=e.clientX,Ye=e.clientY)},{capture:!0}),ht.addEventListener("pointermove",e=>{if(!ge||!je)return;const t=e.clientX-qe,o=e.clientY-Ye;qe=e.clientX,Ye=e.clientY;Ge+=.3*-t,Ne+=.3*-o,Ne=Math.max(-60,Math.min(60,Ne))},{capture:!0}),ht.addEventListener("pointerup",()=>{je=!1},{capture:!0}),ht.addEventListener("pointerleave",()=>{je=!1},{capture:!0});const ut=[],mt=[];function gt(t){const o=parseInt(t.slice(1,3),16)/255,n=parseInt(t.slice(3,5),16)/255,i=parseInt(t.slice(5,7),16)/255;return new e.Color(o,n,i)}const yt=[],ft=new Map,vt=new Map,bt=new Map,wt={flare:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Fflare.png?alt=media&token=ce114781-2ac3-41b2-b9c2-34bda0f6eb13",circle:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Fcircle.png?alt=media&token=fd01b475-2b94-4c24-bc83-2a907045715f",spark:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Fsparkle.png?alt=media&token=466739a4-ccd7-4295-88c2-dedd681a34f0",rain:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Frain.png?alt=media&token=13478487-6259-4906-838c-ad592db893e4",smoke:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Fsmoke.png?alt=media&token=6cece4f8-87cf-47f9-974d-c4dd6a649f0f"};function xt(t,o){return new Promise((n,i)=>{if(bt.has(t))return void n(bt.get(t));const a=new e.Asset(t,"texture",{url:o});a.on("load",()=>{if(O)return console.log(`[Particle] Ignoring texture load - viewer was destroyed: ${t}`),void i(new Error("Viewer destroyed"));const e=a.resource;bt.set(t,e),n(e)}),a.on("error",e=>{console.error(`[Particle] Failed to load texture: ${t}`,e),i(e)}),R.assets.add(a),R.assets.load(a)})}function St(t){const o=new e.Entity(t.name||"particle-system"),n=(e,t=0)=>({x:e?._x??e?.x??t,y:e?._y??e?.y??t,z:e?._z??e?.z??t}),i=e=>e||{r:1,g:1,b:1,a:1},a=n(t.emitterPosition,0),s=n(t.gravity,0),r=i(t.color1),l=i(t.color2),c=i(t.colorDead),d=n(t.direction1,0),p=n(t.direction2,0),h=((t.minLifeTime??1)+(t.maxLifeTime??3))/2;let u=e.EMITTERSHAPE_BOX,m=new e.Vec3(.1,.1,.1),g=new e.Vec3(0,0,0);if("sphere"===t.emitterType||"sphere"===t.emitterShape){u=e.EMITTERSHAPE_SPHERE;const o=t.emitterRadius||.1;m=new e.Vec3(o,o,o)}else if("box"===t.emitterShape&&t.emitBoxMin&&t.emitBoxMax){u=e.EMITTERSHAPE_BOX;const o=n(t.emitBoxMin,0),i=n(t.emitBoxMax,0);m=new e.Vec3(Math.abs(i.x-o.x)/2,Math.abs(i.y-o.y)/2,Math.abs(i.z-o.z)/2),g=new e.Vec3((o.x+i.x)/2,(o.y+i.y)/2,-(o.z+i.z)/2)}else"point"===t.emitterShape?(u=e.EMITTERSHAPE_BOX,m=new e.Vec3(.01,.01,.01)):m=new e.Vec3(t.emitterExtents?.x||t.emitterRadius||.1,t.emitterExtents?.y||t.emitterRadius||.1,t.emitterExtents?.z||t.emitterRadius||.1);let y=e.BLEND_ADDITIVEALPHA;const f=t.blendMode||"";"BLENDMODE_STANDARD"===f||"normal"===f||"alpha"===f?y=e.BLEND_NORMAL:"BLENDMODE_MULTIPLY"===f||"multiply"===f?y=e.BLEND_MULTIPLICATIVE:"BLENDMODE_ONEONE"===f?y=e.BLEND_ADDITIVE:"BLENDMODE_ADD"!==f&&"BLENDMODE_MULTIPLYADD"!==f||(y=e.BLEND_ADDITIVEALPHA);const v=s.x||0,b=s.y||0,w=-(s.z||0),x=v*h,S=b*h,E=w*h,M=t.minAngularSpeed??0,C=t.maxAngularSpeed??t.angularSpeed??0,_=t.minInitialRotation??0,L=t.maxInitialRotation??0,T=t.minSize??.1,k=t.maxSize??.3,A=t.minScaleX??1,P=t.maxScaleX??1,z=t.minScaleY??1,R=t.maxScaleY??1,I=t.minEmitPower??1,D=t.maxEmitPower??2,F=(d.x+p.x)/2,U=(d.y+p.y)/2,B=-(d.z+p.z)/2;o.addComponent("particlesystem",{numParticles:t.numParticles||500,lifetime:h,rate:t.emitRate||50,emitterShape:u,emitterExtents:m,emitterRadius:t.emitterRadius||.1,startAngle:_,startAngle2:0!==L?L:360,radialSpeedGraph:new e.Curve([0,I,1,D]),localVelocityGraph:new e.CurveSet([[0,F*I],[0,U*I],[0,B*I]]),localVelocityGraph2:new e.CurveSet([[0,F*D],[0,U*D],[0,B*D]]),velocityGraph:new e.CurveSet([[0,0,1,x],[0,0,1,S],[0,0,1,E]]),scaleGraph:new e.Curve([0,T*A,1,k*P]),scaleGraph2:new e.Curve([0,T*z,1,k*R]),rotationSpeedGraph:new e.Curve([0,M]),rotationSpeedGraph2:C!==M?new e.Curve([0,C]):void 0,colorGraph:new e.CurveSet([[0,r.r,.5,l.r,1,c.r],[0,r.g,.5,l.g,1,c.g],[0,r.b,.5,l.b,1,c.b]]),alphaGraph:new e.Curve([0,r.a??1,.5,l.a??.8,1,c.a??0]),blend:y,depthWrite:t.depthWrite??!1,depthSoftening:t.softParticles??0,lighting:t.lighting??!1,halfLambert:t.halfLambert??!1,alignToMotion:t.alignToMotion??!1,stretch:t.stretch||0,preWarm:t.preWarm??!1,loop:t.loop??!0,autoPlay:t.autoPlay??!0,sort:t.sort??0,orientation:t.orientation??0}),o.particlesystem&&(o.particlesystem.localSpace=t.localSpace??!1);const V=t.translationPivot?.x??0,$=t.translationPivot?.y??0;o.setPosition(a.x+g.x+V,a.y+g.y+$,-a.z+g.z);const H=t.renderingGroupId??3;return o.particlesystem&&void 0!==H&&(o.particlesystem.drawOrder=H),console.log(`[Particle] Entity configured at position: (${a.x+g.x+V}, ${a.y+g.y+$}, ${-a.z+g.z})`),console.log(`[Particle] Emitter shape: ${t.emitterShape||"box"}, extents: ${m.x}, ${m.y}, ${m.z}`),console.log(`[Particle] Gravity: (${v}, ${b}, ${w})`),console.log(`[Particle] Colors: start=${JSON.stringify(r)}, mid=${JSON.stringify(l)}, dead=${JSON.stringify(c)}`),console.log(`[Particle] Angular speed: ${M} - ${C}, Initial rotation: ${_} - ${L}`),console.log(`[Particle] TranslationPivot: (${V}, ${$}), RenderingGroupId: ${H}`),o}const Et=new Map;function Mt(t){const{type:o,component:n,node:i,animations:a}=t;console.log("[CustomMesh] Playing animation, type:",o,"node:",i?.name,"animations:",a?.length);try{if("pc-anim"===o)console.log("[CustomMesh] Using simple pc-anim approach (like HTML export)"),n.playing=!0,t.isPlaying=!0,console.log("[CustomMesh] Animation started - playing:",n.playing);else if("animation"===o){if(n.playing=!0,n.loop=!0,"function"==typeof n.play){const e=Object.keys(n.animations||{});e.length>0?(n.play(e[0],1),console.log("[CustomMesh] Playing legacy animation clip:",e[0])):n.play()}}else if("glb-skeleton"===o){console.log("[CustomMesh] Starting GLB skeleton animation playback");const{modelEntity:o,asset:n,animations:i}=t;if(i&&i.length>0){const n=i[0];console.log("[CustomMesh] Animation clip:",n),console.log("[CustomMesh] Animation name:",n._name||n.name),console.log("[CustomMesh] Animation duration:",n._duration||n.duration);try{if(!o.anim){console.log("[CustomMesh] Adding AnimComponent to modelEntity");const e={layers:[{name:"Base",states:[{name:"START",speed:1},{name:"Idle",speed:1,loop:!0}],transitions:[{from:"START",to:"Idle",time:0,conditions:[]}]}]};if(o.addComponent("anim",{activate:!0,speed:1}),o.anim){o.anim.loadStateGraph(e);const t=n;o.anim.assignAnimation("Base.Idle",t,"Base"),console.log("[CustomMesh] AnimComponent added and animation assigned")}}if(o.anim)o.anim.playing=!0,o.anim.speed=1,t.animComponent=o.anim,t.isPlaying=!0,console.log("[CustomMesh] GLB animation playing via AnimComponent");else{console.log("[CustomMesh] Falling back to manual curve sampling"),t.isPlaying=!0,t.startTime=Date.now();const i=i=>{if(!t.isPlaying)return;const a=(Date.now()-t.startTime)/1e3%(n._duration||n.duration||1);try{(n._curves||[]).forEach((t,i)=>{if(!t)return;const s=n._paths?.[i];if(!s||!s.entityPath)return;let r=o.findByPath(s.entityPath.join("/"));if(r||(r=o.findByName(s.entityPath[s.entityPath.length-1])),!r)return;const l=t.evaluate?.(a);if(null==l)return;const c=s.component||s.propertyPath?.[0];"localPosition"===c||"position"===c?Array.isArray(l)&&r.setLocalPosition(l[0],l[1],l[2]):"localRotation"===c||"rotation"===c?Array.isArray(l)&&l.length>=4&&r.setLocalRotation(new e.Quat(l[0],l[1],l[2],l[3])):"localScale"!==c&&"scale"!==c||Array.isArray(l)&&r.setLocalScale(l[0],l[1],l[2])})}catch(e){}};t.updateHandler=i,R.on("update",i),console.log("[CustomMesh] Manual animation playback started")}}catch(e){console.error("[CustomMesh] Error setting up GLB animation:",e)}}}else if("anim"===o&&(n.playing=!0,n.speed=1,n.baseLayer)){const e=(n.baseLayer.states||[]).find(e=>"START"!==e&&"END"!==e&&"ANY"!==e);e&&(n.baseLayer.play(e),console.log("[CustomMesh] Playing anim state:",e))}n&&console.log("[CustomMesh] Animation component state - playing:",n.playing,"speed:",n.speed)}catch(e){console.error("[CustomMesh] Error in playAnimComponentV2:",e)}}function Ct(e){const{type:t,component:o}=e;try{"pc-anim"===t?(o.playing=!1,e.isPlaying=!1,console.log("[CustomMesh] pc-anim animation paused")):"glb-skeleton"===t?(e.isPlaying=!1,e.animComponent&&(e.animComponent.playing=!1,console.log("[CustomMesh] GLB skeleton animation paused via AnimComponent")),e.updateHandler&&(R.off("update",e.updateHandler),e.updateHandler=null,console.log("[CustomMesh] GLB manual animation paused"))):o&&(o.playing=!1,o.speed=0,"animation"===t&&"function"==typeof o.pause&&o.pause())}catch(e){console.error("[CustomMesh] Error pausing animation:",e)}}function _t(t,o){if(console.log("[CustomMesh] Loading mesh",o,":",t.name,t),!t.modelUrl||"string"!=typeof t.modelUrl||""===t.modelUrl.trim())return console.warn("[CustomMesh] Skipping mesh",t.name,"- no valid modelUrl provided. Config:",t),null;const n=new e.Entity("custom-mesh-"+o),i=t.position||{x:0,y:0,z:0};if(n.setPosition(i._x??i.x??0,i._y??i.y??0,-(i._z??i.z??0)),t.rotation){const e=t.rotation,o=180/Math.PI;n.setEulerAngles((e._x??e.x??0)*o,(e._y??e.y??0)*o,(e._z??e.z??0)*o)}if(t.scale){const e=t.scale;n.setLocalScale(e._x??e.x??1,e._y??e.y??1,e._z??e.z??1)}const a=t.modelUrl.trim();console.log("[CustomMesh] Loading model from URL:",a);const s=new e.Asset("mesh-model-"+o,"container",{url:a});R.assets.add(s);const r=t.id||`mesh-${o}`,l={entity:n,config:t,isAnimPlaying:!1,audioPlaying:!1};return Et.set(r,l),s.ready(o=>{try{if(console.log("[CustomMesh] Model loaded:",t.name),!o||!o.resource)return void console.error("[CustomMesh] Invalid asset resource for mesh:",t.name);const i=o.resource.instantiateRenderEntity();if(!i)return void console.error("[CustomMesh] Failed to instantiate render entity for mesh:",t.name);function a(e){e.enabled=!0,e.children&&e.children.forEach(e=>a(e))}n.addChild(i),n.modelEntity=i,a(i);let s=0;if(i.forEach(()=>{s++}),console.log("[CustomMesh] Enabled all children for:",t.name,"- Total nodes:",s),"animated"!==t.opacityMode&&void 0!==t.opacity&&t.opacity<1&&(Pt(n,t.opacity),console.log("[CustomMesh] Applied static opacity:",t.opacity,"to",t.name)),t.billboard){const c=n.getEulerAngles().clone();n._billboardActive=!t.billboardRange,R.on("update",()=>{if(!n.enabled)return;if(!n._billboardActive)return void n.setEulerAngles(c.x,c.y,c.z);const e=le.getPosition(),t=n.getPosition(),o=e.x-t.x,i=e.z-t.z,a=Math.atan2(o,i)*(180/Math.PI),s=n.getEulerAngles();n.setEulerAngles(s.x,a,s.z)}),console.log("[CustomMesh] Billboard enabled for:",t.name,t.billboardRange?"(with range control)":"(always active)")}const r=o.resource?.animations?.length>0;if(console.log("[CustomMesh] Asset resource for",t.name,":",{hasAnimations:r,animationCount:o.resource?.animations?.length||0,animations:o.resource?.animations?.map(e=>e?.name||e?.resource?.name||"unnamed")||[],interactionConfig:t.interaction}),r||t.interaction&&t.interaction.playModelAnimation){const d=[],p=i.anim;if(p&&(console.log("[CustomMesh] Found modelEntity.anim component - using simple approach"),d.push({type:"pc-anim",component:p,modelEntity:i})),0===d.length){function h(e,t=0){e.anim&&!d.find(t=>t.component===e.anim)&&(d.push({type:"anim",component:e.anim,node:e}),console.log("[CustomMesh] Found existing ANIM component on:",e.name)),e.animation&&(d.push({type:"animation",component:e.animation,node:e}),console.log("[CustomMesh] Found ANIMATION (legacy) component on:",e.name)),e.children&&e.children.forEach(e=>h(e,t+1))}h(i)}if(0===d.length&&r){const u=o.resource.animations;console.log("[CustomMesh] No anim component found - falling back to GLB skeleton approach"),console.log("[CustomMesh] GLB has",u.length,"embedded animations");try{d.push({type:"glb-skeleton",modelEntity:i,asset:o,animations:u,animationNames:u.map(e=>e.resource?.name||e.name||"Animation"),isPlaying:!1,currentTime:0}),console.log("[CustomMesh] GLB skeleton animations stored:",u.map(e=>e.resource?.name||e.name))}catch(m){console.error("[CustomMesh] Error setting up GLB animations:",m)}}if(d.length>0){l.allAnimComponents=d,l.animComponent=d[0].component,console.log("[CustomMesh] Total animation components for",t.name,":",d.length,"- Types:",d.map(e=>e.type).join(", "));const g=t.interaction?.animationAutoPlay;g&&(d.forEach(e=>{Mt(e)}),l.isAnimPlaying=!0,console.log("[CustomMesh] Auto-playing all animations for:",t.name))}else console.warn("[CustomMesh] No animation components could be set up for mesh:",t.name)}else console.log("[CustomMesh] No animations to setup for:",t.name,"(no GLB animations and playModelAnimation not enabled)");t.interaction&&t.interaction.playAudio&&t.interaction.audioUrl&&function(t,o,n){const i=o.interaction,a=o.id||o.name,s=`mesh-audio-${a}`;t.addComponent("sound",{positional:i.audioSpatial||!1,distanceModel:i.audioDistanceModel||"exponential",refDistance:i.audioRefDistance||1,maxDistance:i.audioMaxDistance||100,rollOffFactor:i.audioRollOffFactor||1,slots:{[s]:{name:s,loop:i.audioLoop||!1,autoPlay:!1,volume:void 0!==i.audioVolume?i.audioVolume:1,pitch:1}}}),n.audioSlotId=s;const r=new e.Asset(`mesh-audio-asset-${a}`,"audio",{url:i.audioUrl});R.assets.add(r),r.ready(()=>{const e=t.sound?.slot(s);e&&(e.asset=r.id),console.log("[CustomMesh] Audio loaded for mesh:",o.name,"Spatial:",i.audioSpatial)}),r.on("error",e=>{console.error("[CustomMesh] Failed to load mesh audio:",o.name,e)}),R.assets.load(r)}(n,t,l),t.interaction&&function(t,o,n){if(!function(e){const t=e.interaction;return!!t&&!!(t.triggerUIPopup||t.playAudio||t.playModelAnimation||t.triggerDirectLink)}(o))return void console.log("[CustomMesh] Skipping interaction setup - no active interactions for:",o.name);const i=o.interaction?.activationMode||"click",a=R.graphicsDevice.canvas,s=(o,n)=>{const i=a.getBoundingClientRect(),s=o-i.left,r=n-i.top,l=le.camera.screenToWorld(s,r,le.camera.nearClip),c=le.camera.screenToWorld(s,r,le.camera.farClip),d=(new e.Vec3).sub2(c,l).normalize(),p=t.modelEntity;if(p&&Lt(p,l,d))return!0;if(Lt(t,l,d))return!0;const h=t.getPosition(),u=(new e.Vec3).sub2(h,l).dot(d);if(u>0){const o=(new e.Vec3).add2(l,d.clone().mulScalar(u)).distance(h),n=t.getLocalScale();if(o<1.5*Math.max(n.x,n.y,n.z))return!0}return!1},r=o.interaction?.popupTriggerMode||i,l=o.interaction?.audioTriggerMode||i,c=o.interaction?.animationTriggerMode||i,d=o.interaction?.directLinkTriggerMode||i;let p=!1;const h=()=>{if(a.style.cursor="pointer","hover"===r&&o.interaction?.triggerUIPopup&&kt(o),"hover"===l&&o.interaction?.playAudio&&n.audioSlotId){const e=t.sound?.slot(n.audioSlotId);e&&!e.isPlaying&&(e.play(),n.audioPlaying=!0,console.log("[CustomMesh] Playing audio on hover for:",o.name))}if("hover"===c&&o.interaction?.playModelAnimation){const e=n.allAnimComponents;e&&e.length>0&&!n.isAnimPlaying&&(e.forEach(e=>Mt(e)),n.isAnimPlaying=!0,console.log("[CustomMesh] Playing animation on hover for:",o.name))}"hover"===d&&o.interaction?.triggerDirectLink&&At(o)},u=()=>{if(a.style.cursor="","hover"===r&&o.interaction?.triggerUIPopup&&function(){const e=document.querySelector(".storysplat-hotspot-popup");e&&e.remove();const t=document.querySelector(".storysplat-mesh-popup");t&&t.remove()}(),"hover"===l&&o.interaction?.playAudio&&n.audioSlotId){const e=t.sound?.slot(n.audioSlotId);e&&e.isPlaying&&(e.stop(),n.audioPlaying=!1,console.log("[CustomMesh] Stopped audio on hover out for:",o.name))}if("hover"===c&&o.interaction?.playModelAnimation){const e=n.allAnimComponents;e&&e.length>0&&n.isAnimPlaying&&(e.forEach(e=>Ct(e)),n.isAnimPlaying=!1,console.log("[CustomMesh] Paused animation on hover out for:",o.name))}},m=()=>{if(console.log("[CustomMesh] Clicked mesh:",o.name),"click"===r&&o.interaction?.triggerUIPopup&&kt(o),"click"===c&&o.interaction?.playModelAnimation){const e=n.allAnimComponents;e&&e.length>0&&(n.isAnimPlaying?(e.forEach(e=>Ct(e)),n.isAnimPlaying=!1,console.log("[CustomMesh] Paused",e.length,"animations on click for:",o.name)):(e.forEach(e=>Mt(e)),n.isAnimPlaying=!0,console.log("[CustomMesh] Playing",e.length,"animations on click for:",o.name)))}if("click"===l&&o.interaction?.playAudio&&n.audioSlotId){const e=t.sound?.slot(n.audioSlotId);e&&(e.isPlaying?(e.pause(),n.audioPlaying=!1,console.log("[CustomMesh] Paused audio on click for:",o.name)):(e.play(),n.audioPlaying=!0,console.log("[CustomMesh] Playing audio on click for:",o.name)))}"click"===d&&o.interaction?.triggerDirectLink&&At(o)},g=e=>{s(e.clientX,e.clientY)&&m()},y=e=>{const t=s(e.clientX,e.clientY);t&&!p?(p=!0,h()):!t&&p&&(p=!1,u())},f=()=>{p&&(p=!1,u())};a.addEventListener("click",g),a.addEventListener("mousemove",y),a.addEventListener("mouseleave",f),t.meshClickHandler=g,t.meshHoverHandler=y,t.meshLeaveHandler=f}(n,t,l),console.log("[CustomMesh] Mesh fully initialized:",t.name)}catch(y){console.error("[CustomMesh] Error processing loaded mesh:",t.name,y)}}),s.on("error",e=>{console.error("[CustomMesh] Failed to load mesh:",t.name,e),R.assets.remove(s)}),R.assets.load(s),t.visibilityRange?(n.visibilityRange=t.visibilityRange,n.enabled=!1!==t.enabled):n.enabled=!1!==t.enabled,n.opacityConfig={mode:t.opacityMode||"static",value:void 0!==t.opacity?t.opacity:1},R.root.addChild(n),n}function Lt(t,o,n){const i=t.render;if(i&&i.meshInstances)for(const e of i.meshInstances)if(e.aabb){if(Tt(o,n,e.aabb))return!0}const a=t.model;if(a&&a.meshInstances)for(const e of a.meshInstances)if(e.aabb){if(Tt(o,n,e.aabb))return!0}const s=t.children;if(s)for(const t of s)if(t instanceof e.Entity&&Lt(t,o,n))return!0;return!1}function Tt(e,t,o){const n=o.getMin?o.getMin():o.min,i=o.getMax?o.getMax():o.max;if(!n||!i)return!1;let a=-1/0,s=1/0;for(let o=0;o<3;o++){const r=["x","y","z"][o],l=e[r],c=t[r],d=n[r]??n.data?.[o],p=i[r]??i.data?.[o];if(Math.abs(c)<1e-8){if(l<d||l>p)return!1}else{let e=(d-l)/c,t=(p-l)/c;if(e>t&&([e,t]=[t,e]),a=Math.max(a,e),s=Math.min(s,t),a>s)return!1}}return s>=0}function kt(e){if(!e.interaction)return;const t={id:`mesh-content-${e.id}`,title:e.interaction.title||e.name,information:e.interaction.information,photoUrl:e.interaction.photoUrl,iframeUrl:e.interaction.iframeUrl,externalLinkUrl:e.interaction.externalLinkUrl,externalLinkText:e.interaction.externalLinkText,backgroundColor:e.interaction.backgroundColor||"#000000",textColor:e.interaction.textColor||"#ffffff"};A.showHotspotPopup?A.showHotspotPopup(t):function(e){const t=document.querySelector(".storysplat-mesh-popup");t&&t.remove();const o=document.createElement("div");o.className="storysplat-mesh-popup",o.style.cssText="\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n background: rgba(0, 0, 0, 0.9);\n padding: 30px;\n border-radius: 12px;\n max-width: 600px;\n max-height: 80vh;\n overflow: auto;\n z-index: 100001;\n color: white;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n ";const n=document.createElement("h2");if(n.textContent=e.name||"Custom Mesh",n.style.cssText="margin-top: 0; margin-bottom: 16px;",o.appendChild(n),e.interaction.popupContent){const t=document.createElement("p");t.textContent=e.interaction.popupContent,t.style.cssText="margin: 0; line-height: 1.6;",o.appendChild(t)}const i=document.createElement("button");i.textContent="× Close",i.style.cssText="\n position: absolute;\n top: 10px;\n right: 10px;\n background: rgba(255, 255, 255, 0.2);\n border: none;\n color: white;\n padding: 8px 16px;\n border-radius: 6px;\n cursor: pointer;\n font-size: 16px;\n ",i.onclick=()=>o.remove(),o.appendChild(i),document.body.appendChild(o)}(e)}function At(e){e.interaction?.triggerDirectLink&&e.interaction.directLinkUrl&&window.open(e.interaction.directLinkUrl,"_blank")}function Pt(t,o){!function t(n){n.render&&n.render.meshInstances&&n.render.meshInstances.forEach(t=>{t.material&&(t.material._isCloned||(t.material=t.material.clone(),t.material._isCloned=!0),t.material.opacity=o,t.material.blendType=e.BLEND_PREMULTIPLIED,t.material.depthTest=!0,t.material.depthWrite=!0,t.material.alphaTest=.01,t.material.update())}),n.children.forEach(e=>t(e))}(t)}function zt(){const e=R.graphicsDevice.canvas;Et.forEach(t=>{const o=t.entity;o.meshClickHandler&&e.removeEventListener("click",o.meshClickHandler),o.destroy()}),Et.clear()}function Rt(t){const o=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return o?new e.Color(parseInt(o[1],16)/255,parseInt(o[2],16)/255,parseInt(o[3],16)/255):new e.Color(1,1,1)}function It(){r.lights&&0!==r.lights.length?(console.log(`[StorySplat Viewer] Creating ${r.lights.length} custom lights...`),r.lights.forEach((t,o)=>{let n=null;switch(t.type){case"point":n=function(t){const o=new e.Entity(t.name||"Point Light");o.addComponent("light",{type:e.LIGHTTYPE_POINT,color:Rt(t.color||"#ffffff"),intensity:t.intensity||1,range:t.range||10,castShadows:t.castShadows||!1});const n=t.position;return n&&o.setPosition(n._x??n.x??0,n._y??n.y??0,-(n._z??n.z??0)),o.enabled=!1!==t.enabled,o}(t);break;case"directional":n=function(t){const o=new e.Entity(t.name||"Directional Light");o.addComponent("light",{type:e.LIGHTTYPE_DIRECTIONAL,color:Rt(t.color||"#ffffff"),intensity:t.intensity||1,castShadows:t.castShadows||!1});const n=t.position;n&&o.setPosition(n._x??n.x??0,n._y??n.y??0,-(n._z??n.z??0));const i=t.rotation;if(i){const e=180/Math.PI;o.setEulerAngles((i._x??i.x??0)*e,(i._y??i.y??0)*e,(i._z??i.z??0)*e)}else o.setEulerAngles(45,0,0);return o.enabled=!1!==t.enabled,o}(t);break;case"hemispheric":n=function(t){const o=new e.Entity(t.name||"Hemispheric Light");o.addComponent("light",{type:e.LIGHTTYPE_DIRECTIONAL,color:Rt(t.color||"#ffffff"),intensity:t.intensity||1,castShadows:!1});const n=t.position;if(n&&o.setPosition(n._x??n.x??0,n._y??n.y??0,-(n._z??n.z??0)),o.setEulerAngles(-90,0,0),o.enabled=!1!==t.enabled,t.groundColor){const o=Rt(t.groundColor),n=Rt(t.color||"#ffffff");R.scene.ambientLight=new e.Color((n.r+.3*o.r)/1.3,(n.g+.3*o.g)/1.3,(n.b+.3*o.b)/1.3)}return o}(t);break;case"ambient":!function(t){const o=Rt(t.color||"#404040"),n=t.intensity||.4;R.scene.ambientLight=new e.Color(o.r*n,o.g*n,o.b*n),console.log("[StorySplat Viewer] Set ambient light:",t.name||"Ambient Light")}(t);break;case"spot":n=function(t){const o=new e.Entity(t.name||"Spot Light"),n=180/Math.PI,i=(t.angle??45*Math.PI/180)*n;t.exponent;const a=t.innerConeAngle??t.innerAngle??.8*i,s=t.outerConeAngle??t.outerAngle??i;o.addComponent("light",{type:e.LIGHTTYPE_SPOT,color:Rt(t.color||"#ffffff"),intensity:t.intensity||1,range:t.range||10,innerConeAngle:a,outerConeAngle:s,castShadows:t.castShadows||!1,shadowBias:t.shadowBias??.05,normalOffsetBias:t.normalOffsetBias??.05});const r=t.position;r&&o.setPosition(r._x??r.x??0,r._y??r.y??0,-(r._z??r.z??0));const l=t.rotation;if(l){const e=180/Math.PI;o.setEulerAngles((l._x??l.x??0)*e,(l._y??l.y??0)*e,(l._z??l.z??0)*e)}else if(t.direction){const n=t.direction,i=new e.Vec3(n._x??n.x??0,n._y??n.y??-1,-(n._z??n.z??0)).normalize();o.lookAt(o.getPosition().x+i.x,o.getPosition().y+i.y,o.getPosition().z+i.z)}else o.setEulerAngles(90,0,0);return o.enabled=!1!==t.enabled,o}(t);break;default:return void console.warn("[StorySplat Viewer] Unknown light type:",t.type)}n&&(R.root.addChild(n),console.log(`[StorySplat Viewer] Created ${t.type} light:`,t.name||`Light ${o}`))}),console.log("[StorySplat Viewer] Lighting setup complete")):console.log("[StorySplat Viewer] No custom lights to create")}function Dt(e){switch(e){case"linear":return"linear";case"inverse":return"inverse";default:return"exponential"}}i.on("progressUpdate",()=>{!function(){const e=100*Re,t=r.waypoints?.length||1,o=Math.round(Re*Math.max(1,t-1));Et.forEach(t=>{const{entity:n,config:i}=t,a=n.visibilityRange;if(a){let t=!0;"waypoint"===a.type?t=o>=a.start&&o<=a.end:"percentage"===a.type&&(t=e>=a.start&&e<=a.end),n.enabled=t}if("animated"===i.opacityMode&&i.opacityAnimation){const t=i.opacityAnimation;if(e>=t.startPercent&&e<=t.endPercent){const o=(e-t.startPercent)/(t.endPercent-t.startPercent);Pt(n,t.startOpacity+(t.endOpacity-t.startOpacity)*o)}}if(i.billboard&&i.billboardRange){const t=i.billboardRange;let a=!1;"percentage"===t.type?a=e>=t.start&&e<=t.end:"waypoint"===t.type&&(a=o>=t.start&&o<=t.end),n._billboardActive=a}else i.billboard&&(n._billboardActive=!0)})}()});const Ft=new Map;const Ut=new Set;const Bt=[];function Vt(t,o=!1,n=1,i){const a=new e.StandardMaterial;if(a.blendType=e.BLEND_PREMULTIPLIED,a.depthTest=!0,a.depthWrite=!0,a.cull=e.CULLFACE_NONE,a.twoSidedLighting=!0,a.alphaTest=.01,o?a.diffuse=new e.Color(1,1,1):(a.diffuse=new e.Color(0,0,0),a.emissive=new e.Color(1,1,1),a.specular=new e.Color(0,0,0),a.useLighting=!1),a.opacity=n,a.update(),function(e){const t=e.toLowerCase();return t.endsWith(".gif")||t.includes("image/gif")||t.includes("format=gif")}(t)){console.log(`[Hotspot] Loading animated GIF: ${t}`);const n=new q(R,t,{autoPlay:!0,onReady:()=>{if(O)return console.log(`[Hotspot] Ignoring GIF load - viewer was destroyed: ${t}`),void n.destroy();n.texture&&(o?(a.diffuseMap=n.texture,a.opacityMap=n.texture):(a.emissiveMap=n.texture,a.opacityMap=n.texture),a.update(),console.log(`[Hotspot] GIF texture loaded: ${t}, useLighting=${o}`),i&&i())},onError:o=>{console.error(`[Hotspot] Failed to load GIF: ${t}`,o),a.opacity=.3,a.emissive=new e.Color(1,0,0),a.update(),i&&i()}});Bt.push(n)}else{const n=new Image;n.crossOrigin="anonymous",n.onload=()=>{if(O)return void console.log(`[Hotspot] Ignoring texture load - viewer was destroyed: ${t}`);const s=new e.Texture(R.graphicsDevice,{width:n.width,height:n.height,format:e.PIXELFORMAT_RGBA8,mipmaps:!0,minFilter:e.FILTER_LINEAR_MIPMAP_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE});s.setSource(n),o?(a.diffuseMap=s,a.opacityMap=s):(a.emissiveMap=s,a.opacityMap=s),a.update(),console.log(`[Hotspot] Texture loaded for: ${t}, useLighting=${o}`),i&&i()},n.onerror=o=>{console.error(`[Hotspot] Failed to load texture: ${t}`,o),a.opacity=.3,a.emissive=new e.Color(1,0,0),a.update(),i&&i()},n.src=t}return a}function $t(){r.hotspots&&0!==r.hotspots.length?(console.log(`[StorySplat Viewer] Creating ${r.hotspots.length} hotspots...`),r.hotspots.forEach((t,o)=>{const n=new e.Entity(`hotspot-${o}`),i=t.position||{_x:0,_y:0,_z:0};n.setPosition(i._x??i.x??0,i._y??i.y??0,-(i._z??i.z??0));const a=t.scale||{_x:1,_y:1,_z:1},s=Math.abs(a._x??a.x??1),r=Math.abs(a._y??a.y??1),l=a._z??a.z??1,c=t.rotation||{_x:0,_y:0,_z:0},d=180/Math.PI,p=(c._x??c.x??0)*d,h=(c._y??c.y??0)*d+180,u=(c._z??c.z??0)*d;if(n.setEulerAngles(p,h,u),"sphere"===t.type){n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=gt(t.color||"#ffffff");o.diffuse=i,o.emissive=i.clone(),o.emissive.mulScalar(.5);const a=t.opacity??.8;a>=.95?(o.opacity=1,o.blendType=e.BLEND_NONE,o.depthTest=!0,o.depthWrite=!0):(o.opacity=a,o.blendType=e.BLEND_ADDITIVEALPHA,o.depthTest=!0,o.depthWrite=!0),o.update(),n.render.material=o,n.setLocalScale(.2*s,.2*r,.2*l)}else if("image"===t.type&&t.imageUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});let e=1;"animated"===t.opacityMode&&t.opacityAnimation?e=void 0!==t.opacityAnimation.startOpacity?t.opacityAnimation.startOpacity:1:void 0!==t.opacity&&(e=t.opacity),n.targetOpacity=e,n.textureLoaded=!1,n.hiddenUntilTextureLoaded=!0,n.enabled=!1;const o=!0===t.useLighting,i=Vt(t.imageUrl,o,e,()=>{n.textureLoaded=!0,n.visibilityRange&&!n.shouldBeVisible||(n.enabled=!0),n.hiddenUntilTextureLoaded=!1});n.render.material=i,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.hotspotMaterial=i,console.log(`[Hotspot] Created image hotspot: ${t.title}, opacity=${e}, opacityMode=${t.opacityMode}, useLighting=${o}`)}else if("video"===t.type&&t.videoUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});const o=/iPad|iPhone|iPod/.test(navigator.userAgent)&&t.useIOSVideoAlphaMethod||t.forceIOSVideoAlphaMethodForAllDevices,i=o&&t.iosMainVideoUrl?t.iosMainVideoUrl:t.videoUrl,a=o&&t.alphaMaskVideoUrl||null,c=(e=>{const t=e.toLowerCase();return t.endsWith(".webm")||t.includes("format=webm")||t.includes("video/webm")})(i)&&!1!==t.webmHasAlpha,d=(o,n,i=!1)=>{const a=document.createElement("video");a.src=o,a.loop=!1!==t.videoLoop,a.crossOrigin="anonymous",a.playsInline=!0,a.muted=!!n||!1!==t.videoMuted,"autoplay"===t.mediaTriggerMode&&(a.autoplay=!0,a.muted=!0);const s=new e.Texture(R.graphicsDevice,{format:i?e.PIXELFORMAT_R8_G8_B8_A8:e.PIXELFORMAT_R8_G8_B8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE});return s.setSource(a),{video:a,texture:s}},p=d(i,!1,c),h=p.video,u=p.texture;t.videoBackupUrl&&(h.onerror=()=>{console.log(`[Hotspot] Main video failed, trying backup: ${t.videoBackupUrl}`),h.src=t.videoBackupUrl,h.load()});const m=new e.StandardMaterial;m.diffuseMap=u,m.emissiveMap=u,m.emissive=new e.Color(1,1,1),m.depthTest=!0,m.depthWrite=!0,m.cull=e.CULLFACE_NONE,m.twoSidedLighting=!0,m.blendType=e.BLEND_PREMULTIPLIED,m.alphaTest=.01,c&&(m.opacityMap=u,m.blendType=e.BLEND_PREMULTIPLIED,m.alphaTest=.01,console.log(`[Hotspot] WebM video with alpha enabled: ${t.title}`));let g=null,y=null;if(a){const o=d(a,!0,!1);g=o.video,y=o.texture,m.opacityMap=y,m.opacityMapChannel="r",m.blendType=e.BLEND_PREMULTIPLIED,m.alphaTest=.01,h.addEventListener("play",()=>{g&&g.paused&&(g.currentTime=h.currentTime,g.play().catch(console.warn))}),h.addEventListener("pause",()=>{g&&!g.paused&&g.pause()}),h.addEventListener("seeked",()=>{g&&(g.currentTime=h.currentTime)}),console.log(`[Hotspot] iOS alpha mask video enabled: ${t.title}, alphaUrl=${a.substring(0,50)}...`)}m.update(),R.on("update",()=>{h.readyState===h.HAVE_ENOUGH_DATA&&u.upload(),g&&y&&g.readyState===g.HAVE_ENOUGH_DATA&&y.upload()});const f={x:s,y:r,z:l};if(h.addEventListener("loadedmetadata",()=>{const e=h.videoWidth,t=h.videoHeight;if(e>0&&t>0){const o=e/t;1===f.x&&1===f.y&&n.setLocalScale(o*f.y,f.y,f.z)}}),n.render.material=m,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.videoElement=h,n.alphaVideoElement=g,n.hotspotMaterial=m,n.mediaTriggerMode=t.mediaTriggerMode||"click",n.proximityDistance=t.proximityDistance||5,n.isVideoPlaying=!1,!0!==t.videoMuted){const e=function(e,t,o){if(!o.videoSpatialAudio&&!1!==o.videoMuted)return null;try{const n=new(window.AudioContext||window.webkitAudioContext);yt.push(n);const i=n.createMediaElementSource(t),a=n.createPanner();a.panningModel="HRTF",a.distanceModel=o.videoDistanceModel||"linear",a.refDistance=void 0!==o.videoRefDistance?o.videoRefDistance:1,a.maxDistance=void 0!==o.videoMaxDistance?o.videoMaxDistance:100,a.rolloffFactor=void 0!==o.videoRolloffFactor?o.videoRolloffFactor:1;const s=e.getPosition();return a.setPosition(s.x,s.y,s.z),i.connect(a),a.connect(n.destination),R.on("update",()=>{if(!e||!e.getPosition)return;const t=e.getPosition();if(a.setPosition(t.x,t.y,t.z),le&&le.getPosition){const e=le.getPosition(),t=le.forward,o=le.up;n.listener.positionX?(n.listener.positionX.value=e.x,n.listener.positionY.value=e.y,n.listener.positionZ.value=e.z,n.listener.forwardX.value=t.x,n.listener.forwardY.value=t.y,n.listener.forwardZ.value=t.z,n.listener.upX.value=o.x,n.listener.upY.value=o.y,n.listener.upZ.value=o.z):(n.listener.setPosition(e.x,e.y,e.z),n.listener.setOrientation(t.x,t.y,t.z,o.x,o.y,o.z))}}),console.log(`[Audio] Video spatial audio setup for hotspot: ${o.title}, refDist=${a.refDistance}, maxDist=${a.maxDistance}`),{audioCtx:n,source:i,panner:a}}catch(e){return console.warn("[Audio] Failed to setup video spatial audio:",e),null}}(n,h,t);e&&(n.videoSpatialAudio=e)}console.log(`[Hotspot] Created video hotspot: ${t.title}, mode=${t.mediaTriggerMode}, useAlpha=${!!a}, spatialAudio=${!!n.videoSpatialAudio}`)}else if("gif"===t.type&&t.gifUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});let o=1;"animated"===t.opacityMode&&t.opacityAnimation?o=void 0!==t.opacityAnimation.startOpacity?t.opacityAnimation.startOpacity:1:void 0!==t.opacity&&(o=t.opacity),n.targetOpacity=o,n.textureLoaded=!1,n.hiddenUntilTextureLoaded=!0,n.enabled=!1;const i=!0===t.useLighting,a=new e.StandardMaterial;a.blendType=e.BLEND_PREMULTIPLIED,a.opacity=o,a.depthTest=!0,a.depthWrite=!0,a.cull=e.CULLFACE_NONE,a.twoSidedLighting=!0,a.alphaTest=.01,i||(a.emissive=new e.Color(1,1,1),a.diffuse=new e.Color(0,0,0));const c=async c=>{try{const d=document.createElement("canvas"),p=d.getContext("2d"),h=new Image;h.crossOrigin="anonymous",h.onload=()=>{d.width=h.width||256,d.height=h.height||256,p.drawImage(h,0,0);const u=new e.Texture(R.graphicsDevice,{format:e.PIXELFORMAT_R8_G8_B8_A8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE});u.setSource(d),a.diffuseMap=u,i||(a.emissiveMap=u),a.opacityMap=u,a.alphaTest=.01,a.update(),n.render.material=a;const m=d.width/d.height;1===s&&1===r?n.setLocalScale(m,1,l):n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.textureLoaded=!0,n.gifCanvas=d,n.gifTexture=u,n.hotspotMaterial=a,n.visibilityRange&&!n.shouldBeVisible||(n.enabled=!0),n.hiddenUntilTextureLoaded=!1,fetch(c).then(e=>e.arrayBuffer()).then(e=>{const t=()=>{n.enabled&&n.gifTexture&&(p.clearRect(0,0,d.width,d.height),p.drawImage(h,0,0),n.gifTexture.upload(),requestAnimationFrame(t))};n.gifAnimationFrame=requestAnimationFrame(t)}).catch(e=>{console.warn("[Hotspot] GIF animation load failed, using static frame:",e)}),console.log(`[Hotspot] Created GIF hotspot: ${t.title}, opacity=${o}, useLighting=${i}`)},h.onerror=o=>{console.error("[Hotspot] Failed to load GIF:",t.gifUrl,o),n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const i=new e.StandardMaterial;i.diffuse=gt(t.color||"#FF00FF"),i.opacity=.8,i.blendType=e.BLEND_NORMAL,i.update(),n.render.material=i,n.setLocalScale(.2*s,.2*r,.2*l),n.enabled=!0,n.hiddenUntilTextureLoaded=!1},h.src=c}catch(e){console.error("[Hotspot] GIF hotspot creation failed:",e)}};c(t.gifUrl)}else{n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=gt(t.color||"#4CAF50");o.diffuse=i,o.emissive=i.clone(),o.emissive.mulScalar(.5);const a=t.opacity??.8;o.opacity=a,o.blendType=e.BLEND_ADDITIVEALPHA,o.depthTest=!0,o.depthWrite=!0,o.update(),n.render.material=o,n.setLocalScale(.2*s,.2*r,.2*l)}n.addComponent("collision",{type:"sphere"===t.type?"sphere":"box",radius:.1,halfExtents:new e.Vec3(.5,.5,.05)}),n.hotspotData=t;const m=function(e,t){if(!t.audioUrl)return null;const o=document.createElement("audio");if(o.src=t.audioUrl,o.loop=t.audioLoop||!1,o.volume=void 0!==t.audioVolume?t.audioVolume:1,o.crossOrigin="anonymous",t.audioSpatial){const n=new(window.AudioContext||window.webkitAudioContext);yt.push(n);const i=n.createMediaElementSource(o),a=n.createPanner();a.panningModel="HRTF",a.distanceModel=t.audioDistanceModel||"linear",a.refDistance=void 0!==t.audioRefDistance?t.audioRefDistance:1,a.maxDistance=void 0!==t.audioMaxDistance?t.audioMaxDistance:100,a.rolloffFactor=void 0!==t.audioRolloffFactor?t.audioRolloffFactor:1;const s=e.getPosition();a.setPosition(s.x,s.y,s.z),i.connect(a),a.connect(n.destination);const r=()=>{if(!e||!e.getPosition)return;const t=e.getPosition();if(a.setPosition(t.x,t.y,t.z),le&&le.getPosition){const e=le.getPosition(),t=le.forward,o=le.up;n.listener.positionX?(n.listener.positionX.value=e.x,n.listener.positionY.value=e.y,n.listener.positionZ.value=e.z,n.listener.forwardX.value=t.x,n.listener.forwardY.value=t.y,n.listener.forwardZ.value=t.z,n.listener.upX.value=o.x,n.listener.upY.value=o.y,n.listener.upZ.value=o.z):(n.listener.setPosition(e.x,e.y,e.z),n.listener.setOrientation(t.x,t.y,t.z,o.x,o.y,o.z))}};return R.on("update",r),console.log(`[Audio] Spatial audio setup for hotspot: ${t.title}, refDist=${a.refDistance}, maxDist=${a.maxDistance}`),{audio:o,audioCtx:n,source:i,panner:a,updateAudioPosition:r}}return console.log(`[Audio] Non-spatial audio setup for hotspot: ${t.title}`),{audio:o}}(n,t);if(m&&(n.audioElements=m,console.log(`[StorySplat Viewer] Audio setup for hotspot: ${t.title||"Untitled"}`)),t.billboard){const e=n.getEulerAngles().clone(),o=void 0!==t.billboardRangeStart||void 0!==t.billboardRangeEnd;n._billboardActive=!o,n._billboardOriginalRotation=e,R.on("update",()=>{n._billboardActive&&(n.lookAt(le.getPosition()),n.rotateLocal(90,180,0))})}t.visibilityRange&&(n.visibilityRange=t.visibilityRange,n.enabled=!1),R.root.addChild(n),ut.push(n),console.log(`[StorySplat Viewer] Created hotspot: ${t.title||"Untitled"}`)})):console.log("[StorySplat Viewer] No hotspots to create")}function Ht(){const e=100*Re,t=r.waypoints?.length||1,o=Math.round(Re*Math.max(1,t-1)),n=le.getPosition();ut.forEach(t=>{const i=t.hotspotData;if(!i)return;let a=!0;if(i.alwaysVisible)a=!0;else{const n=t.visibilityRange;n&&("waypoint"===n.type?a=o>=n.start&&o<=n.end:"percentage"===n.type&&(a=e>=n.start&&e<=n.end))}if(t.shouldBeVisible=a,i.billboard&&(void 0!==i.billboardRangeStart||void 0!==i.billboardRangeEnd)){const o=i.billboardRangeStart??0,n=i.billboardRangeEnd??100,a=e>=o&&e<=n;if(t._billboardActive=a,!a&&t._billboardOriginalRotation){const e=t._billboardOriginalRotation;t.setEulerAngles(e.x,e.y,e.z)}}if(t.hiddenUntilTextureLoaded?t.enabled=!1:t.enabled=a,t.videoElement&&"video"===i.type){const o=t.mediaTriggerMode||"click";if("proximity"===o){const e=t.getPosition(),o=n.distance(e),a=t.proximityDistance||5;o<=a&&!t.isVideoPlaying?(Ot(t,i),console.log(`[Hotspot] Proximity play: ${i.title}, distance=${o.toFixed(2)}`)):o>a&&t.isVideoPlaying&&(Wt(t),console.log(`[Hotspot] Proximity pause: ${i.title}, distance=${o.toFixed(2)}`))}"scroll"===o&&(a&&!t.isVideoPlaying?(Ot(t,i),console.log(`[Hotspot] Scroll play: ${i.title}, scroll=${e.toFixed(1)}%`)):!a&&t.isVideoPlaying&&(Wt(t),console.log(`[Hotspot] Scroll pause: ${i.title}, scroll=${e.toFixed(1)}%`)))}if("animated"===i.opacityMode&&i.opacityAnimation){if("image"===i.type&&!t.textureLoaded)return;const o=i.opacityAnimation,n=o.startPercent??0,a=o.endPercent??100,s=void 0!==o.startOpacity?o.startOpacity:1,r=void 0!==o.endOpacity?o.endOpacity:1;let l;if(e<=n)l=s;else if(e>=a)l=r;else{l=s+(r-s)*((e-n)/(a-n))}l=Math.max(0,Math.min(1,l)),t.hotspotMaterial?(t.hotspotMaterial.opacity=l,t.hotspotMaterial.update()):t.render&&t.render.material&&(t.render.material.opacity=l,t.render.material.update())}})}function Ot(e,t){const o=e.videoElement,n=e.alphaVideoElement;if(o){if("autoplay"!==e.mediaTriggerMode&&(o.muted=!1!==t.videoMuted),e.videoSpatialAudio&&e.videoSpatialAudio.audioCtx){const t=e.videoSpatialAudio.audioCtx;"suspended"===t.state&&t.resume().then(()=>{console.log("[Audio] Video spatial audio context resumed")}).catch(e=>console.warn("[Audio] Failed to resume video audio context:",e))}o.play().catch(e=>console.warn("Video play failed:",e)),n&&n.play().catch(e=>console.warn("Alpha video play failed:",e)),e.isVideoPlaying=!0}}function Wt(e){const t=e.videoElement,o=e.alphaVideoElement;t&&(t.pause(),o&&o.pause(),e.isVideoPlaying=!1)}function Gt(){const e=100*Re,t=r.waypoints?.length||1,o=Math.round(Re*Math.max(1,t-1)),n=le.getPosition();mt.forEach(t=>{const i=t.portalData;if(!i)return;let a=!0;const s=t.visibilityRange;if(s&&("waypoint"===s.type?a=o>=s.start&&o<=s.end:"percentage"===s.type&&(a=e>=s.start&&e<=s.end)),t.shouldBeVisible=a,t.hiddenUntilTextureLoaded?t.enabled=!1:t.enabled=a,"proximity"===i.activationMode&&a){const e=t.getPosition(),o=n.distance(e),a=i.proximityDistance||2;o<=a&&!t.proximityTriggered?(t.proximityTriggered=!0,console.log(`[Portal] Proximity triggered: ${i.title||i.targetSceneName}, navigating to scene ${i.targetSceneId}`),jt(i)):o>a&&(t.proximityTriggered=!1)}})}function Nt(t,o){const n=le.camera.screenToWorld(t,o,le.camera.nearClip),i=le.camera.screenToWorld(t,o,le.camera.farClip);let a=null;if(mt.forEach(t=>{if(!t.enabled)return;const o=t.getPosition(),s=(new e.Vec3).sub2(i,n).normalize(),r=(new e.Vec3).sub2(o,n).dot(s);if(r<0)return;const l=(new e.Vec3).add2(n,s.clone().mulScalar(r)).distance(o),c=t.getLocalScale();l<.6*Math.max(c.x,c.y,.3)&&(!a||r<a.distance)&&(a={entity:t,distance:r})}),null!==a){const e=a.entity;return{entity:e,portal:e.portalData}}return null}async function jt(e){if(e.targetSceneId){console.log(`[StorySplat Viewer] Portal activated: navigating to scene ${e.targetSceneId}`),i.emit("portalActivated",{portalId:e.id,targetSceneId:e.targetSceneId,targetSceneName:e.targetSceneName}),function(e,t){const o=e.querySelector(".storysplat-portal-loading");o&&o.remove();const n=document.createElement("div");n.className="storysplat-portal-loading";const i=document.createElement("div");i.className="storysplat-portal-spinner";const a=document.createElement("div");if(a.className="storysplat-portal-loading-text",a.textContent=`Loading ${t}...`,n.appendChild(i),n.appendChild(a),Object.assign(n.style,{position:"fixed",top:"0",left:"0",width:"100%",height:"100%",background:"rgba(0, 0, 0, 0.85)",display:"flex",flexDirection:"column",alignItems:"center",justifyContent:"center",zIndex:"10000",fontFamily:"system-ui, sans-serif"}),Object.assign(i.style,{width:"50px",height:"50px",border:"4px solid rgba(255, 255, 255, 0.2)",borderTop:"4px solid #9C27B0",borderRadius:"50%",animation:"storysplat-portal-spin 1s linear infinite",marginBottom:"20px"}),Object.assign(a.style,{color:"#ffffff",fontSize:"18px"}),!document.getElementById("storysplat-portal-styles")){const e=document.createElement("style");e.id="storysplat-portal-styles",e.textContent="\n @keyframes storysplat-portal-spin {\n 0% { transform: rotate(0deg); }\n 100% { transform: rotate(360deg); }\n }\n ",document.head.appendChild(e)}e.appendChild(n)}(t,e.targetSceneName||e.targetSceneId);try{const o=`https://discover.storysplat.com/api/scene/${e.targetSceneId}`;console.log(`[Portal] Fetching scene from: ${o}`);const n=await fetch(o);if(!n.ok)throw new Error(`Failed to fetch scene: ${n.status} ${n.statusText}`);const i=await n.json(),a=i.data||i;!function(){console.log("[Portal] Cleaning up current scene for navigation..."),pt(),ut.forEach(e=>{e.videoElement&&(e.videoElement.pause(),e.videoElement.src=""),e.alphaVideoElement&&(e.alphaVideoElement.pause(),e.alphaVideoElement.src="")}),Bt.forEach(e=>e.destroy()),Bt.length=0,zt(),ut.forEach(e=>{e.destroy()}),ut.length=0,mt.forEach(e=>{e.destroy()}),mt.length=0,$&&($.destroy(),$=null);R.__htmlMeshManager&&R.__htmlMeshManager.destroy();R.__customScriptSystem&&R.__customScriptSystem.dispose();console.log("[Portal] Cleanup complete")}(),await new Promise(e=>setTimeout(e,100)),z&&z.parentNode&&z.remove();const s=await te(t,a,{});return qt(t),console.log(`[Portal] Successfully navigated to scene: ${e.targetSceneId}`),s}catch(e){console.error("[Portal] Navigation failed:",e),qt(t);const o=document.createElement("div");o.className="storysplat-portal-error",o.textContent=`Failed to load scene: ${e.message}`,Object.assign(o.style,{position:"fixed",top:"50%",left:"50%",transform:"translate(-50%, -50%)",background:"rgba(0,0,0,0.9)",color:"#ff6b6b",padding:"20px",borderRadius:"8px",zIndex:"10000",fontFamily:"system-ui, sans-serif"}),t.appendChild(o),setTimeout(()=>o.remove(),3e3)}}else console.warn("[Portal] No target scene ID specified")}function qt(e){const t=e.querySelector(".storysplat-portal-loading");t&&t.remove()}i.on("progressUpdate",()=>{Ht()}),setTimeout(()=>{Ht()},100),i.on("progressUpdate",()=>{Gt()}),setTimeout(()=>{Gt()},100);const Yt=R.graphicsDevice.canvas;function Xt(t,o){const n=le.camera.screenToWorld(t,o,le.camera.nearClip),i=le.camera.screenToWorld(t,o,le.camera.farClip);let a=null;if(ut.forEach(t=>{if(!t.enabled)return;const o=t.getPosition(),s=(new e.Vec3).sub2(i,n).normalize(),r=(new e.Vec3).sub2(o,n).dot(s);if(r<0)return;const l=(new e.Vec3).add2(n,s.clone().mulScalar(r)).distance(o),c=t.getLocalScale();l<.6*Math.max(c.x,c.y,.3)&&(!a||r<a.distance)&&(a={entity:t,distance:r})}),null!==a){const e=a.entity;return{entity:e,hotspot:e.hotspotData}}return null}let Zt=null,Jt=!1;const Kt=t.querySelector(".storysplat-hotspot-popup"),Qt=t.querySelector(".storysplat-hotspot-overlay");async function eo(e,t){if("explore"!==me)return;console.log("[StorySplat Viewer] Double-click focus at:",e,t);const o=Xt(e,t);if(o){const e=o.entity.getPosition();return console.log("[StorySplat Viewer] Focusing on hotspot at:",e.x,e.y,e.z),void he.focus(e,!1)}try{const o=.25;ye.resize(Math.floor(Yt.clientWidth*o),Math.floor(Yt.clientHeight*o));const n=R.scene.layers.getLayerByName("World");if(!n)return void console.warn("[StorySplat Viewer] World layer not found");ye.prepare(le.camera,R.scene,[n]);const i=Math.floor(e*o),a=Math.floor(t*o),s=await ye.getWorldPointAsync(i,a);if(s){const e=le.getPosition().distance(s);if(e>.1&&e<1e3)return console.log("[StorySplat Viewer] Focusing on GSplat at:",s.x.toFixed(2),s.y.toFixed(2),s.z.toFixed(2),"distance:",e.toFixed(2)),void he.focus(s,!1)}const r=await ye.getSelectionAsync(i,a,1,1);if(r.length>0){const e=r[0].aabb.center.clone();console.log("[StorySplat Viewer] Focusing on mesh AABB center:",e.x.toFixed(2),e.y.toFixed(2),e.z.toFixed(2)),he.focus(e,!1)}else console.log("[StorySplat Viewer] No pick result at click point")}catch(e){console.warn("[StorySplat Viewer] Picking failed:",e)}}Kt&&(Kt.addEventListener("mouseenter",()=>{Jt=!0}),Kt.addEventListener("mouseleave",()=>{Jt=!1,Zt&&"hover"===Zt.activationMode&&(Kt.classList.remove("visible"),Qt&&Qt.classList.remove("visible"),Zt=null)})),Yt.addEventListener("mousemove",e=>{const o=Yt.getBoundingClientRect(),n=e.clientX-o.left,i=e.clientY-o.top,a=Nt(n,i);if(a&&a.portal){const e=a.portal.activationMode||"click";return void(Yt.style.cursor="click"===e?"pointer":"default")}const s=Xt(n,i);if(s&&s.hotspot){const e=s.hotspot;"click"===e.activationMode||"hover"===e.activationMode||"video"===e.type?Yt.style.cursor="pointer":Yt.style.cursor="default","hover"===e.activationMode&&Zt!==e&&(Zt=e,(e.information||e.photoUrl||e.iframeUrl||e.externalLinkUrl)&&u(t,e))}else if(Yt.style.cursor="default",Zt&&"hover"===Zt.activationMode&&!Jt){const e=t.querySelector(".storysplat-hotspot-popup"),o=t.querySelector(".storysplat-hotspot-overlay");e&&e.classList.remove("visible"),o&&o.classList.remove("visible"),Zt=null}}),Yt.addEventListener("click",o=>{const n=Yt.getBoundingClientRect(),i=o.clientX-n.left,a=o.clientY-n.top,s=Nt(i,a);if(null!==s&&s.portal){const e=s.portal;console.log("[StorySplat Viewer] Portal clicked:",e.title||e.targetSceneName);return void("click"===(e.activationMode||"click")&&jt(e))}const l=Xt(i,a);if(null!==l){const o=l.entity,n=l.hotspot;if(console.log("[StorySplat Viewer] Hotspot clicked:",n.title),o.audioElements&&o.audioElements.audio){const e=o.audioElements,t=e.audio;t.paused?(e.audioCtx&&"suspended"===e.audioCtx.state&&e.audioCtx.resume(),t.play().catch(e=>console.error("[Audio] Hotspot audio play failed:",e)),console.log("[Audio] Hotspot audio started:",n.title)):(t.pause(),console.log("[Audio] Hotspot audio paused:",n.title))}if(o.videoElement&&("click"===o.mediaTriggerMode||"autoplay"===o.mediaTriggerMode)){const e=o.videoElement;o.alphaVideoElement,"autoplay"===o.mediaTriggerMode&&!e.paused&&e.muted?(e.muted=(n.videoMuted,!1),console.log("[Hotspot] Video unmuted by click")):e.paused?(Ot(o,n),console.log("[Hotspot] Video started")):(Wt(o),console.log("[Hotspot] Video paused"))}const i=n.activationMode||"click",a=n.title||n.information||n.photoUrl||n.iframeUrl||n.externalLinkUrl;"click"===i&&a&&(fe?xe?Se():function(t){if(!fe)return;be||(be=new e.Entity("arContentPlane"),be.addComponent("render",{type:"plane"}),be.setLocalScale(.8,1,.45),R.root.addChild(be));const o=512,n=288,i=document.createElement("canvas");i.width=o,i.height=n;const a=i.getContext("2d"),s=t.backgroundColor||"rgba(20, 20, 20, 0.95)";a.fillStyle=s,a.fillRect(0,0,o,n),a.strokeStyle="rgba(255, 255, 255, 0.3)",a.lineWidth=2,a.strokeRect(1,1,510,286);const r=t.textColor||"#ffffff";let l=35;t.title&&(a.fillStyle=r,a.font="bold 28px Arial, sans-serif",a.fillText(t.title,20,l),l+=40),t.information&&(a.fillStyle=r,a.font="18px Arial, sans-serif",l=function(e,t,o,n,i,a){const s=t.split(" ");let r="",l=n;for(const t of s){const n=r+t+" ";e.measureText(n).width>i&&r?(e.fillText(r.trim(),o,l),r=t+" ",l+=a):r=n}return e.fillText(r.trim(),o,l),l+a}(a,t.information,20,l,472,24)),a.fillStyle="rgba(255, 255, 255, 0.5)",a.font="14px Arial, sans-serif",a.textAlign="center",a.fillText("Tap to close",256,273),a.textAlign="left",we||(we=new e.Texture(R.graphicsDevice,{width:o,height:n,format:e.PIXELFORMAT_RGBA8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR})),we.setSource(i);const c=new e.StandardMaterial;c.diffuse=new e.Color(0,0,0),c.diffuseMap=we,c.emissive=new e.Color(1,1,1),c.emissiveMap=we,c.useLighting=!1,c.blendType=e.BLEND_NORMAL,c.cull=e.CULLFACE_NONE,c.depthWrite=!0,c.update(),be.render&&be.render.meshInstances[0]&&(be.render.meshInstances[0].material=c),be.enabled=!0,xe=!0,console.log("[StorySplat Viewer] AR content displayed for hotspot:",t.title)}(n):u(t,n));const s=n.teleportWaypoint??n.teleportToWaypoint,c=n.teleportPercent??n.teleportToPercent,d=n.teleportMode||"animate";let p=null;if(void 0!==s&&-1!==s){const e=r.waypoints?.length||1,t=Math.max(0,Math.min(s,e-1));p=e>1?t/(e-1):0,console.log("[Hotspot] Teleporting to waypoint:",t,"(progress:",p,", mode:",d,")")}else void 0!==c&&-1!==c&&(p=Math.max(0,Math.min(c/100,1)),console.log("[Hotspot] Teleporting to percent:",c,"(progress:",p,", mode:",d,")"));null!==p&&("instant"===d?(Re=p,et(Re)):nt(p,800))}}),Yt.addEventListener("dblclick",e=>{const t=Yt.getBoundingClientRect();eo(e.clientX-t.left,e.clientY-t.top)});let to=0;Yt.addEventListener("touchend",e=>{if(1!==e.changedTouches.length)return;const t=Date.now();if(t-to<300){const t=e.changedTouches[0],o=Yt.getBoundingClientRect();eo(t.clientX-o.left,t.clientY-o.top),to=0}else to=t}),Me(.2,"Initializing...");const oo=o.frameSequence&&o.frameSequence.frameUrls&&o.frameSequence.frameUrls.length>0?async function(){if(!o.frameSequence||!o.frameSequence.frameUrls||0===o.frameSequence.frameUrls.length)throw new Error("No frame sequence URLs provided");console.log("[StorySplat Viewer] Loading 4DGS frame sequence:",o.frameSequence.frameUrls.length,"frames"),Me(.3,"Loading 4DGS frames..."),W=new Z(R,{frameUrls:o.frameSequence.frameUrls,fps:o.frameSequence.fps||24,loop:!1!==o.frameSequence.loop,preloadCount:o.frameSequence.preloadCount||5,autoplay:o.frameSequence.autoplay||!1},{onFrameChange:(e,t)=>{i.emit("frameChange",e,t)},onLoadProgress:(e,t)=>{Me(.3+e/t*.6,`Loading frames... ${e}/${t}`)},onError:e=>{console.error("[StorySplat Viewer] Frame sequence error:",e),i.emit("error",new Error(e))}}),R.on("update",e=>{W&&!O&&W.update(e)}),console.log("[StorySplat Viewer] 4DGS frame sequence player initialized"),i.emit("loaded")}:async function(){const t=[];console.log("[SPLAT] Building URL priority list..."),console.log("[SPLAT] Available URLs:",{lodMetaUrl:r.lodMetaUrl||"(none)",sogUrl:r.sogUrl||"(none)",splatUrl:r.splatUrl||"(none)",fallbackUrls:r.fallbackUrls?.length||0}),r.lodMetaUrl&&(t.push(r.lodMetaUrl),console.log("[SPLAT] ✓ LOD streaming URL added (highest priority):",r.lodMetaUrl)),r.sogUrl&&(t.push(r.sogUrl),console.log("[SPLAT] ✓ SOG URL added (second priority):",r.sogUrl)),r.splatUrl&&(t.push(r.splatUrl),console.log("[SPLAT] ✓ Original splat URL added (third priority):",r.splatUrl)),r.fallbackUrls&&(t.push(...r.fallbackUrls),console.log("[SPLAT] ✓ Fallback URLs added:",r.fallbackUrls)),console.log("[SPLAT] Final URL priority order:",t),console.log("[SPLAT] Will try URLs in order: LOD streaming > SOG > PLY/Other"),Me(.3,"Loading splat...");for(const a of t)if(a)try{const t=a.split(".").pop()?.toLowerCase()||"splat",s="gsplat",l=a.includes("lod-meta.json"),c=a.includes(".sog")||l;console.log("[SPLAT] Attempting to load URL:",a),console.log("[SPLAT] Format detection:",{extension:t,isLodStreaming:l,isSogFormat:c,assetType:s});const d=new e.Asset("splat-"+Date.now(),s,{url:a});return d.on("progress",(e,t)=>{if(t>0){const o=.3+e/t*.6,n=Math.round(e/t*100);n%25!=0&&100!==n||console.log(`[SPLAT] Loading progress: ${n}% (${(e/1024/1024).toFixed(2)}MB / ${(t/1024/1024).toFixed(2)}MB)`),Me(o,`Loading... ${n}%`)}}),await new Promise((t,p)=>{let h=!1;const u=c?e=>{if(h)return;const t=e.reason?.message||String(e.reason);(t.includes("shape")||t.includes("upgradeMeta"))&&(console.warn("[SPLAT] ⚠️ Parse error detected (likely deprecated v1 format):",t),h=!0,e.preventDefault(),i.emit("warning",{type:"deprecated_format",message:"This scene uses an outdated SOG format. Please re-upload your scene with the latest tools for better performance.",details:"SOG v1 format is deprecated. Use splat-transform v2+ to convert your PLY files.",url:a}),console.log("[SPLAT] Will try fallback URL if available..."),p(new Error(`SOG parse error: ${t}`)))}:null;c&&u&&(console.log("[SPLAT] Registered error handler for SOG format parsing"),window.addEventListener("unhandledrejection",u));const m=()=>{c&&u&&window.removeEventListener("unhandledrejection",u)};d.ready(()=>{if(O)return console.log("[SPLAT] Ignoring load - viewer was destroyed during loading"),m(),void p(new Error("Viewer destroyed"));console.log("[SPLAT] ✓ Asset loaded and resources ready");try{$=new e.Entity("splat"),$.addComponent("gsplat",{asset:d,unified:!0});const i=$.gsplat;i&&ee(a)?(i.lodDistances=U.lodDistances,console.log("[SPLAT] ✓ LOD streaming enabled for this splat"),console.log("[SPLAT] LOD distances configured:",{distances:U.lodDistances,description:"Quality levels switch at these camera distances",preset:F})):c?console.log("[SPLAT] ✓ Single SOG file loaded (no LOD streaming)"):console.log("[SPLAT] Standard format loaded (PLY/SPLAT)");const s=r.scale||{x:1,y:1,z:1},l=r.invertXScale||!1,p=r.invertYScale||!1,u={x:l?-s.x:s.x,y:p?-s.y:s.y,z:l!==p?-s.z:s.z};$.setLocalScale(u.x,u.y,u.z);const g=r.position||[0,0,0];$.setPosition(g[0],g[1],-g[2]);const y=r.rotation||[0,0,0],f=0!==y[0]||0!==y[1]||0!==y[2];let v;v=f?[y[0]*(180/Math.PI),y[1]*(180/Math.PI),-y[2]*(180/Math.PI)]:[180,0,0],$.setEulerAngles(v[0],v[1],v[2]),console.log("[StorySplat Viewer] Splat transform applied:",{scale:u,position:[g[0],g[1],-g[2]],rotation:v,hasUserRotation:f,invertXScale:l,invertYScale:p}),R.root.addChild($),console.log("[StorySplat Viewer] Splat entity added to scene"),$.gsplat?.material&&($.gsplat.material.setParameter("alphaClip",.1),console.log("[StorySplat Viewer] GSplat alphaClip set for picking support"));const b=n.revealEffect||o.revealEffect||"medium",w=P(b);if(w){$.addComponent("script");const e=k();H=$.script?.create(e),H&&(H.enabled=!1,H.center.set(0,0,0),H.speed=w.speed,H.acceleration=w.acceleration,H.delay=w.delay,H.oscillationIntensity=w.oscillationIntensity,H.dotTint.set(w.dotTint.r,w.dotTint.g,w.dotTint.b),H.waveTint.set(w.waveTint.r,w.waveTint.g,w.waveTint.b),H.endRadius=w.endRadius,console.log("[StorySplat Viewer] Reveal effect configured (waiting to start):",b))}else console.log("[StorySplat Viewer] Reveal effect disabled");setTimeout(()=>{h||(m(),t())},100)}catch(e){console.error("[StorySplat Viewer] Error during gsplat setup:",e),m(),p(e)}}),d.on("error",e=>{console.error("[SPLAT] ✗ Asset load error:",{url:a,assetType:s,isSogFormat:c,isLodFormat:l,error:e,message:e?.message||"Unknown error",status:e?.status||e?.statusCode||"N/A"}),m(),p(e)}),R.assets.add(d),R.assets.load(d)}),i.emit("loaded"),console.log("[SPLAT] ✓✓✓ SPLAT LOADED SUCCESSFULLY ✓✓✓"),void console.log("[SPLAT] Load summary:",{url:a,format:l?"LOD streaming (lod-meta.json)":c?"SOG (compressed)":"Standard (PLY/SPLAT)",lodEnabled:l,lodPreset:l?F:"N/A"})}catch(e){console.warn("[SPLAT] ✗ Failed to load URL, trying next fallback:",a),console.warn("[SPLAT] Error:",e)}console.error("[SPLAT] ✗✗✗ FAILED TO LOAD SPLAT FROM ANY URL ✗✗✗"),console.error("[SPLAT] Tried URLs:",t),i.emit("error",new Error("Failed to load splat from any URL"))};oo().then(()=>{if(Me(1,"Ready!"),$t(),r.portals&&0!==r.portals.length?(console.log(`[StorySplat Viewer] Creating ${r.portals.length} portals...`),r.portals.forEach((t,o)=>{const n=new e.Entity(`portal-${o}`),i=t.position||{_x:0,_y:0,_z:0};n.setPosition(i._x??i.x??0,i._y??i.y??0,-(i._z??i.z??0));const a=t.scale||{_x:1,_y:1,_z:1},s=Math.abs(a._x??a.x??1),r=Math.abs(a._y??a.y??1),l=a._z??a.z??1,c=t.rotation||{_x:0,_y:0,_z:0},d=180/Math.PI,p=(c._x??c.x??0)*d,h=(c._y??c.y??0)*d+180,u=(c._z??c.z??0)*d;if(n.setEulerAngles(p,h,u),"sphere"===t.type){n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=gt(t.color||"#9C27B0");o.diffuse=i,o.emissive=i.clone(),o.emissive.mulScalar(.5);const a=t.opacity??.8;a>=.95?(o.opacity=1,o.blendType=e.BLEND_NONE,o.depthTest=!0,o.depthWrite=!0):(o.opacity=a,o.blendType=e.BLEND_ADDITIVEALPHA,o.depthTest=!0,o.depthWrite=!0),o.update(),n.render.material=o,n.setLocalScale(.2*s,.2*r,.2*l)}else if("image"===t.type&&t.imageUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});const e=!0===t.useLighting,o=t.opacity??1;n.textureLoaded=!1,n.hiddenUntilTextureLoaded=!0,n.enabled=!1;const i=Vt(t.imageUrl,e,o,()=>{n.textureLoaded=!0,n.visibilityRange&&!n.shouldBeVisible||(n.enabled=!0),n.hiddenUntilTextureLoaded=!1});n.render.material=i,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.portalMaterial=i,console.log(`[Portal] Created image portal: ${t.title||t.targetSceneName||"Untitled"}`)}else if("video"===t.type&&t.videoUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});const o=document.createElement("video");o.src=t.videoUrl,o.loop=!0,o.muted=!0,o.crossOrigin="anonymous",o.playsInline=!0,o.autoplay=!0;const i=new e.Texture(R.graphicsDevice,{format:e.PIXELFORMAT_R8_G8_B8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE});i.setSource(o);const a=new e.StandardMaterial;a.diffuseMap=i,a.emissiveMap=i,a.emissive=new e.Color(1,1,1),a.depthTest=!0,a.depthWrite=!0,a.cull=e.CULLFACE_NONE,a.blendType=e.BLEND_PREMULTIPLIED,a.opacity=t.opacity??1,a.update(),n.render.material=a,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),R.on("update",()=>{o.readyState>=o.HAVE_CURRENT_DATA&&i.setSource(o)}),o.play().catch(e=>console.log("[Portal] Video autoplay blocked:",e)),n.videoElement=o,n.portalMaterial=a,console.log(`[Portal] Created video portal: ${t.title||t.targetSceneName||"Untitled"}`)}else if("gif"===t.type&&t.gifUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});const e=!0===t.useLighting,o=t.opacity??1,i=Vt(t.gifUrl,e,o,()=>{n.textureLoaded=!0,n.visibilityRange&&!n.shouldBeVisible||(n.enabled=!0),n.hiddenUntilTextureLoaded=!1});n.render.material=i,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.textureLoaded=!1,n.hiddenUntilTextureLoaded=!0,n.enabled=!1,n.portalMaterial=i,console.log(`[Portal] Created GIF portal: ${t.title||t.targetSceneName||"Untitled"}`)}else{n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=gt(t.color||"#9C27B0");o.diffuse=i,o.emissive=i.clone(),o.emissive.mulScalar(.5);const a=t.opacity??.8;o.opacity=a,o.blendType=e.BLEND_ADDITIVEALPHA,o.depthTest=!0,o.depthWrite=!0,o.update(),n.render.material=o,n.setLocalScale(.2*s,.2*r,.2*l)}n.addComponent("collision",{type:"sphere"===t.type?"sphere":"box",radius:.1,halfExtents:new e.Vec3(.5,.5,.05)}),n.portalData=t,t.billboard&&R.on("update",()=>{n.enabled&&(n.lookAt(le.getPosition()),n.rotateLocal(90,180,0))}),t.visibilityRange&&(n.visibilityRange=t.visibilityRange,n.enabled=!1),R.root.addChild(n),mt.push(n),console.log(`[StorySplat Viewer] Created portal: ${t.title||t.targetSceneName||"Untitled"} -> ${t.targetSceneId}`)})):console.log("[StorySplat Viewer] No portals to create"),r.waypoints&&0!==r.waypoints.length&&(r.waypoints.forEach((t,o)=>{t.interactions&&Array.isArray(t.interactions)&&t.interactions.forEach(n=>{if("audio"===n.type&&n.data){const i=n.data,a=n.id,s=new e.Entity(`waypoint-audio-${a}`),r=t.position||{x:0,y:0,z:0};s.setPosition(r._x??r.x??t.x??0,r._y??r.y??t.y??1.6,-(r._z??r.z??t.z??0));const l={slots:{[a]:{name:a,loop:i.loop||!1,autoPlay:!1,volume:void 0!==i.volume?i.volume:1,pitch:1,positional:i.spatialSound||!1,distanceModel:Dt(i.distanceModel||"exponential"),maxDistance:i.maxDistance||1e4,refDistance:i.refDistance||1,rollOffFactor:i.rolloffFactor||1}}};s.addComponent("sound",l);const c=new e.Asset(`waypoint-audio-asset-${a}`,"audio",{url:i.url});R.assets.add(c),c.ready(()=>{const e=s.sound?.slot(a);e&&(e.asset=c.id);const t=ft.get(a);t&&(t.assetReady=!0),console.log(`[Audio] Waypoint audio loaded: ${a}, spatialSound=${i.spatialSound}, maxDistance=${i.maxDistance}`)}),R.assets.load(c),R.root.addChild(s),ft.set(a,{entity:s,waypointIndex:o,config:i,slotId:a,playing:!1,autoplayTriggered:!1,assetReady:!1}),console.log(`[Audio] Waypoint audio created: ${a} at waypoint ${o}, spatial=${i.spatialSound}`)}})}),ft.size>0&&console.log(`[StorySplat Viewer] Setup ${ft.size} waypoint audio sources`)),function(){const t=r.audioEmitters;t&&0!==t.length&&(console.log(`[Audio] Setting up ${t.length} standalone audio emitters`),t.forEach(t=>{if(!t.enabled)return void console.log(`[Audio] Skipping disabled emitter: ${t.name||t.id}`);if(!t.url)return void console.warn(`[Audio] Emitter has no URL: ${t.name||t.id}`);const o=t.id||`emitter-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,n=t.position||{x:0,y:0,z:0},i=new e.Entity(`audio-emitter-${o}`);i.setPosition(n.x,n.y,n.z);const a=Dt(t.distanceModel||"linear");i.addComponent("sound",{positional:!1!==t.spatialSound,refDistance:t.refDistance||1,maxDistance:t.maxDistance||100,rollOffFactor:t.rolloffFactor||1,distanceModel:a,volume:t.volume??.5}),i.sound?.addSlot(o,{volume:t.volume??.5,loop:!1!==t.loop,autoPlay:!1,overlap:!1});const s=new e.Asset(`audio-emitter-${o}`,"audio",{url:t.url});s.on("load",()=>{if(O)return;const e=i.sound?.slot(o);e&&(e.asset=s.id);const n=Ft.get(o);n&&(n.assetReady=!0,!1!==t.autoplay&&e&&(console.log(`[Audio] Autoplay emitter: ${t.name||o}`),e.play(),n.playing=!0)),console.log(`[Audio] Emitter loaded: ${t.name||o}, spatial=${!1!==t.spatialSound}, maxDistance=${t.maxDistance||100}`)}),R.assets.add(s),R.assets.load(s),R.root.addChild(i),Ft.set(o,{entity:i,config:t,slotId:o,playing:!1,assetReady:!1}),console.log(`[Audio] Emitter created: ${t.name||o} at (${n.x}, ${n.y}, ${n.z})`)}),Ft.size>0&&console.log(`[StorySplat Viewer] Setup ${Ft.size} standalone audio emitters`))}(),console.log("[StorySplat Viewer] 🎆 Calling initParticleSystems..."),async function(){if(console.log("═══════════════════════════════════════"),console.log("🎆 PARTICLE SYSTEM INITIALIZATION"),console.log("═══════════════════════════════════════"),console.log("[Particle] config.particles:",r.particles),console.log("[Particle] Type:",typeof r.particles),console.log("[Particle] Is Array:",Array.isArray(r.particles)),!r.particles||0===r.particles.length)return console.log("[Particle] ⚠️ No particle systems to create (particles array is empty or undefined)"),void console.log("═══════════════════════════════════════");console.log(`[Particle] 📋 Found ${r.particles.length} particle system(s) to create`);for(let e=0;e<r.particles.length;e++){const t=r.particles[e];console.log(`[Particle] --- Particle System ${e+1}/${r.particles.length} ---`),console.log("[Particle] Raw config:",JSON.stringify(t,null,2));try{const o=t.particleTexture||"flare";let n,i;"custom"===o&&t.customTextureUrl?(n=t.customTextureUrl,i=`custom_${t.id||t.name||e}`,console.log(`[Particle] Custom texture: ${n.substring(0,60)}...`)):(n=wt[o]||wt.flare,i=o,console.log(`[Particle] Texture: ${o} -> ${n.substring(0,60)}...`)),console.log("[Particle] Loading texture...");const a=await xt(i,n);console.log("[Particle] ✅ Texture loaded:",a.name),console.log("[Particle] Creating entity...");const s=St(t);console.log("[Particle] ✅ Entity created:",s.name),s.particlesystem?(s.particlesystem.colorMap=a,console.log("[Particle] ✅ Texture applied to particle system"),console.log("[Particle] Particle system properties:",{numParticles:s.particlesystem.numParticles,lifetime:s.particlesystem.lifetime,rate:s.particlesystem.rate,loop:s.particlesystem.loop,autoPlay:s.particlesystem.autoPlay})):console.warn("[Particle] ⚠️ Entity has no particlesystem component!"),R.root.addChild(s),console.log("[Particle] ✅ Entity added to scene");const r=s.getPosition();console.log(`[Particle] Position: (${r.x.toFixed(2)}, ${r.y.toFixed(2)}, ${r.z.toFixed(2)})`);const l=(t.id||t.name||`particle-${vt.size}`).replace(/[^a-zA-Z0-9]/g,"_");vt.set(l,s),console.log(`[Particle] ✅ SUCCESS: Created "${t.name||l}"`)}catch(e){console.error(`[Particle] ❌ FAILED to create particle system: ${t.name}`),console.error(`[Particle] Error message: ${e?.message||"No message"}`),console.error(`[Particle] Error stack: ${e?.stack||"No stack"}`),console.error("[Particle] Error object:",e)}}console.log("═══════════════════════════════════════"),console.log(`[Particle] 🎉 COMPLETE: ${vt.size}/${r.particles.length} particle systems created`),console.log("[Particle] Active particle systems:",Array.from(vt.keys())),console.log("═══════════════════════════════════════")}(),async function(){r.customMeshes&&0!==r.customMeshes.length?(console.log(`[StorySplat Viewer] Creating ${r.customMeshes.length} custom meshes...`),console.log("[StorySplat Viewer] All custom mesh configs:",r.customMeshes),setTimeout(()=>{let e=0,t=0;r.customMeshes.forEach((o,n)=>{if(console.log(`[CustomMesh] Processing mesh ${n}:`,{name:o.name,enabled:o.enabled,hasModelUrl:!!o.modelUrl,modelUrl:o.modelUrl?.substring(0,100)+"..."}),!1!==o.enabled)try{_t(o,n)?e++:(t++,console.warn(`[CustomMesh] Mesh ${o.name} returned null (likely missing modelUrl)`))}catch(e){console.error("[CustomMesh] Error loading mesh",o.name,":",e),t++}else console.log("[CustomMesh] Skipping disabled mesh:",o.name,"(enabled =",o.enabled,")"),t++}),console.log(`[CustomMesh] Summary: ${e} loaded, ${t} skipped`)},100),console.log(`[StorySplat Viewer] ${r.customMeshes.length} custom meshes queued for loading`)):console.log("[StorySplat Viewer] No custom meshes to create")}(),function(){const t=r.skybox?.url||r.skyboxUrl;if(!t)return void console.log("[StorySplat Viewer] No skybox configured");const o=r.skybox?.rotation??r.skyboxRotation??0,n=r.skybox?.intensity??1,i=r.skybox?.enableIBL??!0;console.log("[StorySplat Viewer] Creating skybox:",t,"rotation:",o,"rad =",o*(180/Math.PI),"deg","IBL:",i);const a=t.toLowerCase().includes(".hdr")||t.toLowerCase().includes(".exr"),s=new e.Entity("skybox"),l=new e.StandardMaterial;l.useLighting=!1,l.cull=e.CULLFACE_FRONT;const c=new e.Asset("skybox-texture","texture",{url:t});if(R.assets.add(c),c.ready(t=>{const o=t.resource;if(l.emissiveMap=o,l.emissive=new e.Color(n,n,n),l.update(),console.log("[StorySplat Viewer] Skybox texture loaded"),i)try{a&&(R.scene.exposure=n,R.scene.toneMapping=e.TONEMAP_ACES,console.log("[StorySplat Viewer] HDR tone mapping enabled")),o&&(R.scene.ambientLight=new e.Color(.3*n,.3*n,.35*n),console.log("[StorySplat Viewer] IBL ambient lighting applied with intensity:",n))}catch(e){console.warn("[StorySplat Viewer] Failed to apply IBL:",e)}}),c.on("error",e=>{console.error("[StorySplat Viewer] Failed to load skybox texture:",e)}),R.assets.load(c),s.addComponent("render",{type:"sphere",material:l,castShadows:!1,receiveShadows:!1}),s.setLocalScale(500,500,500),0!==o){const e=o*(180/Math.PI);s.setEulerAngles(0,e,0)}R.on("update",()=>{const e=le.getPosition();s.setPosition(e.x,e.y,e.z)}),R.root.addChild(s),console.log("[StorySplat Viewer] Skybox created with rotation:",o,"intensity:",n)}(),It(),H&&(H.enabled=!0,console.log("[StorySplat Viewer] Reveal effect started immediately")),setTimeout(()=>{A.preloader&&p(A.preloader)},200),function(){if(!r.includeXR)return;if(!R.xr)return void console.warn("[StorySplat Viewer] WebXR not supported in this browser");const t=R.xr,o=r.xrMode||"both";"vr"!==o&&"both"!==o||(t.isAvailable(e.XRTYPE_VR)&&(A.vrButton?.classList.add("available"),console.log("[StorySplat Viewer] VR is available")),t.on("available:"+e.XRTYPE_VR,e=>{e?A.vrButton?.classList.add("available"):A.vrButton?.classList.remove("available")})),"ar"!==o&&"both"!==o||(t.isAvailable(e.XRTYPE_AR)&&(A.arButton?.classList.add("available"),console.log("[StorySplat Viewer] AR is available")),t.on("available:"+e.XRTYPE_AR,e=>{e?A.arButton?.classList.add("available"):A.arButton?.classList.remove("available")})),t.on("start",()=>{fe=!0,console.log("[StorySplat Viewer] XR session started"),"vr"===ve?(A.vrButton?.classList.add("active"),A.vrButton.textContent="Exit VR"):"ar"===ve&&(A.arButton?.classList.add("active"),A.arButton.textContent="Exit AR"),he.disable(),ue&&ue.disable(),i.emit("xrStart",{type:ve})}),t.on("end",()=>{fe=!1,console.log("[StorySplat Viewer] XR session ended"),Se(),A.vrButton?.classList.remove("active"),A.arButton?.classList.remove("active"),A.vrButton&&(A.vrButton.textContent="VR"),A.arButton&&(A.arButton.textContent="AR"),ve=null,"explore"===me?he.enable():"walk"===me&&ue&&ue.enable(),i.emit("xrEnd",{})}),A.vrButton&&A.vrButton.addEventListener("click",()=>{fe&&"vr"===ve?t.end():!fe&&t.isAvailable(e.XRTYPE_VR)&&(ve="vr",le.camera.startXr(e.XRTYPE_VR,e.XRSPACE_LOCALFLOOR,{callback:e=>{e&&(console.error("[StorySplat Viewer] Failed to start VR:",e),ve=null)}}))}),A.arButton&&A.arButton.addEventListener("click",()=>{fe&&"ar"===ve?t.end():!fe&&t.isAvailable(e.XRTYPE_AR)&&(ve="ar",le.camera.startXr(e.XRTYPE_AR,e.XRSPACE_LOCALFLOOR,{callback:e=>{e&&(console.error("[StorySplat Viewer] Failed to start AR:",e),ve=null)}}))})}(),r.htmlMeshes&&r.htmlMeshes.length>0){console.log("[StorySplat Viewer] Setting up HTML meshes:",r.htmlMeshes.length);const e=function(e,t){const o=new Y(e);for(const e of t)o.createMesh(e);return o}(R,r.htmlMeshes);R.__htmlMeshManager=e,i.on("progressUpdate",()=>{const t=100*Re,o=r.waypoints?.length||1,n=Math.round(Re*Math.max(1,o-1));e.updateVisibility(t,n)})}if(r.customScript&&""!==r.customScript.trim()){console.log("[StorySplat Viewer] Initializing custom script system...");const t=function(t,o,n,i,a,s,r,l,c){if(!i||""===i.trim())return console.log("[Custom Script] No custom script provided"),null;console.log("[Custom Script] Initializing custom script system..."),console.log("[Custom Script] Script length:",i.length);const d=new X(i);return d.initialize({app:t,camera:o,pc:e,canvas:n,getScrollPercentage:a,getCurrentWaypointIndex:s,getHotspots:r,getSplats:l,getHTMLMeshes:c}),d.execute(),d}(R,le,z,r.customScript,()=>Re,()=>B,()=>ut,()=>$?[$]:[],()=>r.htmlMeshes||[]);t&&(R.__customScriptSystem=t,console.log("[StorySplat Viewer] Custom script system initialized"))}try{const e=new URLSearchParams(window.location.search),t=e.get("waypoint"),o=e.get("autoplay");if(null!==t&&r.waypoints&&r.waypoints.length>0){const e=parseInt(t,10);!isNaN(e)&&e>=0&&e<r.waypoints.length&&(console.log("[StorySplat Viewer] URL param: navigating to waypoint",e),it(e))}"true"!==o||n.autoPlay||r.autoPlay||(console.log("[StorySplat Viewer] URL param: starting autoplay"),dt())}catch(e){console.warn("[StorySplat Viewer] Could not parse URL parameters:",e)}i.emit("ready"),console.log("[StorySplat Viewer] Ready"),w&&(h(A,{nextWaypoint:at,prevWaypoint:st,play:dt,pause:pt,isPlaying:()=>V,getCurrentWaypointIndex:()=>B,getWaypointCount:()=>r.waypoints?.length||0,getWaypoints:()=>r.waypoints||[],setCameraMode:Ee,on:(e,t)=>i.on(e,t)},T,S.buttonLabels),A.returnWaypointButton&&r.waypoints&&r.waypoints.length>0&&A.returnWaypointButton.addEventListener("click",()=>{const e=le.getPosition();let t=0,o=1/0;r.waypoints.forEach((n,i)=>{const a=n.position||[0,0,0],s=e.x-a[0],r=e.y-a[1],l=e.z- -a[2],c=Math.sqrt(s*s+r*r+l*l);c<o&&(o=c,t=i)}),console.log("[StorySplat Viewer] Returning to nearest waypoint:",t,"distance:",o.toFixed(2)),Ee("tour"),it(t);const n=A.scrollControls?.querySelectorAll(".storysplat-mode-btn");n?.forEach(e=>{const t=e.getAttribute("data-mode");e.classList.toggle("selected","tour"===t)})}),i.emit("progressUpdate",{progress:Re,index:B}),r.waypoints&&r.waypoints.length>0&&i.emit("waypointChange",{index:0,waypoint:r.waypoints[0],prevIndex:-1})),(n.autoPlay||r.autoPlay)&&dt()}).catch(e=>{console.error("[StorySplat Viewer] Failed to initialize:",e),A.preloader&&p(A.preloader),i.emit("error",e)});const no=()=>{R.resizeCanvas()};window.addEventListener("resize",no);const io={app:R,canvas:z,goToWaypoint:it,nextWaypoint:at,prevWaypoint:st,getCurrentWaypointIndex:()=>B,getWaypointCount:()=>r.waypoints?.length||0,setPosition:(e,t,o)=>le.setPosition(e,t,o),setRotation:(e,t,o)=>le.setEulerAngles(e,t,o),getPosition:()=>{const e=le.getPosition();return{x:e.x,y:e.y,z:e.z}},getRotation:()=>{const e=le.getEulerAngles();return{x:e.x,y:e.y,z:e.z}},play:dt,pause:pt,stop:function(){if(W)return W.stop(),void i.emit("playbackStop");pt(),tt(0)},isPlaying:()=>V,setFrame:e=>W?.setFrame(e),getCurrentFrame:()=>W?.getCurrentFrame()??0,getTotalFrames:()=>W?.getTotalFrames()??0,setFps:e=>W?.setFps(e),getFps:()=>W?.getFps()??24,getFrameProgress:()=>W?.getProgress()??0,setFrameProgress:e=>W?.setProgress(e),goToOriginalSplat:function(){const e=Pe();oe!==e&&(ae.forEach((t,o)=>{o!==e&&(N?_e(t):Le(o))}),$&&($.enabled=!0),oe=e,i.emit("splatChange",{url:e,isOriginal:!0}),console.log("[SplatSwap] Manually returned to original splat"),Te(void 0,!0))},getCurrentSplatUrl:function(){return oe||Pe()},isShowingOriginalSplat:function(){return oe===Pe()||null===oe},destroy:()=>{O=!0,pt(),W&&(W.destroy(),W=null),window.removeEventListener("resize",no),A.preloader&&A.preloader.remove(),A.scrollControls&&A.scrollControls.remove(),A.fullscreenButton&&A.fullscreenButton.remove(),A.helpButton&&A.helpButton.remove(),A.helpPanel&&A.helpPanel.remove(),A.waypointInfo&&A.waypointInfo.remove(),A.watermark&&A.watermark.remove();const e=document.getElementById("storysplat-viewer-styles");e&&e.remove(),t.classList.remove("storysplat-viewer-container"),Bt.forEach(e=>e.destroy()),Bt.length=0,zt(),ue&&ue.destroy(),R.__customScriptSystem&&R.__customScriptSystem.dispose(),R.__htmlMeshManager&&R.__htmlMeshManager.destroy(),be&&(be.destroy(),be=null),we&&(we.destroy(),we=null),R.destroy(),z.remove()},resize:no,navigateToScene:async e=>{const t={targetSceneId:e,activationMode:"click"};await jt(t)},on:(e,t)=>i.on(e,t),off:(e,t)=>i.off(e,t)};return io}async function oe(e,t,o){console.log("[StorySplat Viewer] Fetching scene from:",t);const n=await fetch(t);if(!n.ok)throw new Error(`Failed to fetch scene: ${n.statusText}`);const i=await n.json();return console.log("[StorySplat Viewer] Scene data loaded:",i),te(e,i,o)}function ne(e,t,o){return e+(t-e)*o}class ie extends Error{constructor(e){super(`Scene not found: ${e}`),this.name="SceneNotFoundError"}}class ae extends Error{constructor(e,t){super(e),this.name="SceneApiError",this.statusCode=t}}const se="undefined"!=typeof process&&process.env?.STORYSPLAT_API_URL?process.env.STORYSPLAT_API_URL:"undefined"!=typeof window&&window.__STORYSPLAT_API_URL__?window.__STORYSPLAT_API_URL__:"https://discover.storysplat.com";async function re(e,t,o={}){const n=o.baseUrl||se;console.log(`[StorySplat Viewer] Fetching scene: ${t}`);const i=`${n}/api/scene/${encodeURIComponent(t)}`,a={"Content-Type":"application/json"};o.apiKey&&(a.Authorization=`Bearer ${o.apiKey}`);const s=await fetch(i,{method:"GET",headers:a});if(!s.ok){if(404===s.status)throw new ie(t);const e=await s.text();let o;try{o=JSON.parse(e).error||`API error: ${s.status}`}catch{o=`API error: ${s.status}`}throw new ae(o,s.status)}const r=await s.json();if(!r.success||!r.data)throw new ae("Invalid API response format",500);console.log(`[StorySplat Viewer] Scene loaded: "${r.meta.name}"`);const l={...r.data,name:r.data.name||r.meta.name,thumbnailUrl:r.data.thumbnailUrl||r.meta.thumbnailUrl},{baseUrl:c,apiKey:d,...p}=o;return te(e,l,p)}async function le(e,t={}){const o=`${t.baseUrl||se}/api/scene/${encodeURIComponent(e)}/meta`,n={"Content-Type":"application/json"};t.apiKey&&(n.Authorization=`Bearer ${t.apiKey}`);const i=await fetch(o,{method:"GET",headers:n});if(!i.ok){if(404===i.status)throw new ie(e);throw new ae(`API error: ${i.status}`,i.status)}const a=await i.json();return{...a,userName:a.userName||"Unknown",userSlug:a.userSlug||"unknown"}}class ce{constructor(t,o,n={}){this.translateGizmo=null,this.rotateGizmo=null,this.scaleGizmo=null,this.activeGizmo=null,this.attachedEntity=null,this._mode="none",this.app=t,this.camera=o,this.gizmoLayer=e.Gizmo.createLayer(t),this.translateGizmo=new e.TranslateGizmo(o,this.gizmoLayer),this.rotateGizmo=new e.RotateGizmo(o,this.gizmoLayer),this.scaleGizmo=new e.ScaleGizmo(o,this.gizmoLayer),this.setSnap(n.snap??!1,n.snapIncrement??1),this.setCoordSpace(n.coordSpace??"world"),this.setupEvents(this.translateGizmo),this.setupEvents(this.rotateGizmo),this.setupEvents(this.scaleGizmo)}setupEvents(e){e.on("transform:start",()=>{this.onTransformStart?.({entity:this.attachedEntity,position:this.attachedEntity?.getPosition().clone(),rotation:this.attachedEntity?.getRotation().clone(),scale:this.attachedEntity?.getLocalScale().clone()})}),e.on("transform:move",()=>{this.onTransformMove?.({entity:this.attachedEntity,position:this.attachedEntity?.getPosition().clone(),rotation:this.attachedEntity?.getRotation().clone(),scale:this.attachedEntity?.getLocalScale().clone()})}),e.on("transform:end",()=>{this.onTransformEnd?.({entity:this.attachedEntity,position:this.attachedEntity?.getPosition().clone(),rotation:this.attachedEntity?.getRotation().clone(),scale:this.attachedEntity?.getLocalScale().clone()})})}get mode(){return this._mode}setMode(e){switch(this._mode=e,this.activeGizmo?.detach(),this.activeGizmo=null,e){case"translate":this.activeGizmo=this.translateGizmo;break;case"rotate":this.activeGizmo=this.rotateGizmo;break;case"scale":this.activeGizmo=this.scaleGizmo}this.activeGizmo&&this.attachedEntity&&this.activeGizmo.attach([this.attachedEntity])}attach(e){this.attachedEntity=e,this.activeGizmo&&(e?this.activeGizmo.attach([e]):this.activeGizmo.detach())}attachToMesh(e){this.attach(e)}detach(){this.attachedEntity=null,this.activeGizmo?.detach()}set positionGizmoEnabled(e){e&&"translate"!==this._mode?this.setMode("translate"):e||"translate"!==this._mode||this.setMode("none")}get positionGizmoEnabled(){return"translate"===this._mode}set rotationGizmoEnabled(e){e&&"rotate"!==this._mode?this.setMode("rotate"):e||"rotate"!==this._mode||this.setMode("none")}get rotationGizmoEnabled(){return"rotate"===this._mode}set scaleGizmoEnabled(e){e&&"scale"!==this._mode?this.setMode("scale"):e||"scale"!==this._mode||this.setMode("none")}get scaleGizmoEnabled(){return"scale"===this._mode}setSnap(e,t){this.translateGizmo&&(this.translateGizmo.snap=e,void 0!==t&&(this.translateGizmo.snapIncrement=t)),this.rotateGizmo&&(this.rotateGizmo.snap=e,void 0!==t&&(this.rotateGizmo.snapIncrement=t)),this.scaleGizmo&&(this.scaleGizmo.snap=e,void 0!==t&&(this.scaleGizmo.snapIncrement=t))}setCoordSpace(e){this.translateGizmo&&(this.translateGizmo.coordSpace=e),this.rotateGizmo&&(this.rotateGizmo.coordSpace=e),this.scaleGizmo&&(this.scaleGizmo.coordSpace=e)}onTransform(e){this.onTransformStart=e.start,this.onTransformMove=e.move,this.onTransformEnd=e.end}update(){this.activeGizmo?.update()}destroy(){this.translateGizmo?.destroy(),this.rotateGizmo?.destroy(),this.scaleGizmo?.destroy(),this.translateGizmo=null,this.rotateGizmo=null,this.scaleGizmo=null,this.activeGizmo=null,this.attachedEntity=null}}class de{constructor(t,o,n={}){this.selectedEntities=new Set,this.highlightedEntity=null,this.originalMaterials=new Map,this.app=t,this.camera=o,this.config={highlightColor:n.highlightColor??new e.Color(.2,.5,1,1),multiSelect:n.multiSelect??!1},this.picker=new e.Picker(t,1,1,!0)}async pickAtScreenPosition(e,t){const o=this.app.graphicsDevice.canvas,n=this.camera.camera;if(!n||!o)return null;const i=.25,a=Math.max(1,Math.floor(o.clientWidth*i)),s=Math.max(1,Math.floor(o.clientHeight*i));this.picker.resize(a,s);const r=this.app.scene.layers.getLayerByName("World");if(!r)return null;this.picker.prepare(n,this.app.scene,[r]);const l=Math.floor(e*i),c=Math.floor(t*i),d=this.picker.getSelection(l,c);if(d&&d.length>0){const e=d[0];if(e.node){let t=e.node;for(;t.parent&&t.parent!==this.app.root;){const e=t.parent;if(e.tags?.has("selectable")||e.tags?.has("hotspot")||e.tags?.has("waypoint")||e.tags?.has("light")){t=e;break}t=e}return t}}return null}async getWorldPointAtScreen(e,t){const o=this.app.graphicsDevice.canvas,n=this.camera.camera;if(!n||!o)return null;const i=.25,a=Math.max(1,Math.floor(o.clientWidth*i)),s=Math.max(1,Math.floor(o.clientHeight*i));this.picker.resize(a,s);const r=this.app.scene.layers.getLayerByName("World");if(!r)return null;this.picker.prepare(n,this.app.scene,[r]);const l=Math.floor(e*i),c=Math.floor(t*i);try{return await this.picker.getWorldPointAsync(l,c)||null}catch(e){return null}}select(e,t=!1){const o=this.getSelectedEntity();t||this.deselectAll(),e&&(this.selectedEntities.add(e),this.applyHighlight(e)),this.onSelectionChange?.({entity:e,previousEntity:o})}deselect(e){this.selectedEntities.has(e)&&(this.selectedEntities.delete(e),this.removeHighlight(e),this.onSelectionChange?.({entity:this.getSelectedEntity(),previousEntity:e}))}deselectAll(){const e=this.getSelectedEntity();this.selectedEntities.forEach(e=>{this.removeHighlight(e)}),this.selectedEntities.clear(),e&&this.onSelectionChange?.({entity:null,previousEntity:e})}isSelected(e){return this.selectedEntities.has(e)}getSelectedEntity(){const e=Array.from(this.selectedEntities);return e.length>0?e[0]:null}getSelectedEntities(){return Array.from(this.selectedEntities)}applyHighlight(t){if(!t.render)return;const o=new Map;t.render.meshInstances.forEach((t,n)=>{if(t.material){o.set(n,t.material);const i=t.material.clone();i instanceof e.StandardMaterial&&(i.emissive=this.config.highlightColor,i.emissiveIntensity=.3,i.update()),t.material=i}}),this.originalMaterials.set(t,o)}removeHighlight(e){const t=this.originalMaterials.get(e);t&&e.render&&(e.render.meshInstances.forEach((e,o)=>{const n=t.get(o);n&&(e.material=n)}),this.originalMaterials.delete(e))}onSelect(e){this.onSelectionChange=e}async handlePointerDown(e,t=!1){let o,n;if(e instanceof MouseEvent)o=e.clientX,n=e.clientY;else{const t=e.touches[0];o=t.clientX,n=t.clientY}const i=await this.pickAtScreenPosition(o,n);return i?this.select(i,t):t||this.deselectAll(),i}destroy(){this.deselectAll(),this.selectedEntities.clear(),this.originalMaterials.clear()}}export{_ as CameraControls,Z as FrameSequencePlayer,ce as GizmoManager,A as REVEAL_PRESETS,ae as SceneApiError,ie as SceneNotFoundError,de as SelectionManager,te as createViewer,re as createViewerFromSceneId,oe as createViewerFromUrl,s as exportPropsToViewerConfig,le as fetchSceneMeta,n as generateHTML,i as generateHTMLFromUrl,P as getRevealPreset,a as transformSceneToExportProps};
|
|
1
|
+
import*as e from"playcanvas";function t(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")}function o(e){return e.replace(/<\/script/gi,"<\\/script")}function n(e,n={}){const{cdnUrl:i="https://unpkg.com/storysplat-viewer@2/dist/storysplat-viewer.umd.js",title:a=e.name||"StorySplat Scene",description:s=`Interactive 3D scene: ${e.name||"StorySplat"}`,faviconUrl:r,customCSS:l="",lazyLoad:c,lazyLoadButtonText:d}=n,p=c??e.uiOptions?.lazyLoad??!1,h=d??e.uiOptions?.lazyLoadButtonText,u=r?`<link rel="icon" href="${t(r)}" />`:"",m=l?`<style>${l}</style>`:"",g=o(JSON.stringify(e,(e,t)=>{if(!("undefined"!=typeof HTMLElement&&t instanceof HTMLElement||"function"==typeof t||t&&"object"==typeof t&&"nodeType"in t))return t},2));return`<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">\n <meta name="description" content="${t(s)}">\n <title>${t(a)}</title>\n ${u}\n <style>\n * { margin: 0; padding: 0; box-sizing: border-box; }\n html, body {\n width: 100%;\n /* Use 100dvh for iOS address bar support, with 100vh fallback */\n height: 100vh;\n height: 100dvh;\n overflow: hidden;\n background: #111;\n }\n #app {\n width: 100%;\n /* Use 100dvh for iOS address bar support, with 100vh fallback */\n height: 100vh;\n height: 100dvh;\n position: relative;\n }\n /* Loading state */\n #app:empty::after {\n content: 'Loading...';\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n color: #666;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n font-size: 16px;\n }\n </style>\n ${m}\n</head>\n<body>\n <div id="app"></div>\n\n <script src="https://cdn.jsdelivr.net/npm/playcanvas@2.14.3/build/playcanvas.min.js"><\/script>\n <script src="${t(i)}"><\/script>\n <script>\n (function() {\n 'use strict';\n\n var sceneData = ${g};\n\n // Wait for DOM and viewer to be ready\n function init() {\n var container = document.getElementById('app');\n if (!container) {\n console.error('[StorySplat] Container #app not found');\n return;\n }\n\n if (typeof StorySplatViewer === 'undefined' || !StorySplatViewer.createViewer) {\n console.error('[StorySplat] Viewer not loaded. Check CDN URL.');\n return;\n }\n\n try {\n var viewer = StorySplatViewer.createViewer(container, sceneData, {\n showUI: true,\n autoPlay: false,\n lazyLoad: ${p},\n lazyLoadButtonText: ${h?`'${o(h)}'`:"undefined"}\n });\n\n viewer.on('ready', function() {\n console.log('[StorySplat] Scene ready');\n });\n\n viewer.on('error', function(err) {\n console.error('[StorySplat] Error:', err);\n });\n\n // Expose viewer globally for debugging\n window.storySplatViewer = viewer;\n } catch (err) {\n console.error('[StorySplat] Failed to create viewer:', err);\n }\n }\n\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', init);\n } else {\n init();\n }\n })();\n <\/script>\n</body>\n</html>`}async function i(e,t={}){const o=await fetch(e);if(!o.ok)throw new Error(`Failed to fetch scene: ${o.statusText}`);return n(await o.json(),t)}function a(e){const t=e.loadedModelUrl||e.splatUrl||"",o=e.sogModelUrl||e.sogUrl,n=e.compressedPlyUrl,i=e.lodMetaUrl,a=t.split("?")[0].split(".").pop()?.toLowerCase(),s="ply"===a||t.includes(".compressed.ply");let r=t;o?r=o:n?r=n:s||console.warn("[StorySplat Viewer] Original file format may not be compatible with PlayCanvas:",a),console.log("[StorySplat Viewer] URL selection:",{original:t,lodMetaUrl:i,sogUrl:o,compressedPlyUrl:n,selected:r});const l=(e.waypoints||[]).map(e=>{let t=e.fov||60;t<3.5&&(t*=180/Math.PI);let o=e.info||e.description||"";if(!o&&e.interactions){const t=e.interactions.find(e=>"info"===e.type);t&&t.data&&t.data.text&&(o=t.data.text)}return{position:e.position||{x:e.x||0,y:e.y||0,z:e.z||0},rotation:e.rotation||{x:0,y:0,z:0},fov:t,duration:e.duration||2e3,name:e.name||e.title||"",info:o,interactions:e.interactions||[],triggerDistance:e.triggerDistance??1}});return{name:e.name||"StorySplat Scene",sceneId:e.sceneId,userId:e.userId,userName:e.userName||"Unknown",thumbnailUrl:e.thumbnailUrl,splatUrl:r,sogUrl:o,lodMetaUrl:i,fallbackUrls:[...e.fallbackUrls||[],o!==r?o:null,n!==r?n:null,t!==r?t:null].filter(e=>null!=e&&""!==e),scale:e.scale||(null!=e.splatScale?{x:e.splatScale,y:e.splatScale,z:e.splatScale}:{x:1,y:1,z:1}),splatPosition:e.splatPosition||e.position||e.cameraPosition||[0,0,0],splatRotation:e.splatRotation||e.rotation||e.cameraRotation||[0,0,0],invertXScale:e.invertXScale,invertYScale:e.invertYScale,waypoints:l,hotspots:e.hotspots||[],portals:e.portals||[],skybox:e.skybox,customMeshes:e.customMeshes||[],htmlMeshes:e.htmlMeshes||[],lights:e.lights||[],particles:e.particles||[],collisionMeshesData:e.collisionMeshesData||[],playerHeight:e.playerHeight,uiColor:e.uiColor||"#ffffff",uiOptions:{showStartExperience:e.uiOptions?.showStartExperience??!0,showWatermark:e.uiOptions?.showWatermark??!0,hideFullscreenButton:e.uiOptions?.hideFullscreenButton??!1,hideInfoButton:e.uiOptions?.hideInfoButton??!1,hideMuteButton:e.uiOptions?.hideMuteButton??!1,hideHelpButton:e.uiOptions?.hideHelpButton??!1,hideWatermark:e.uiOptions?.hideWatermark??!1,watermarkText:e.uiOptions?.watermarkText,watermarkLink:e.uiOptions?.watermarkLink,buttonPosition:e.uiOptions?.buttonPosition||"inline",buttonLabels:e.uiOptions?.buttonLabels,customPreloaderLogoUrl:e.uiOptions?.customPreloaderLogoUrl,lazyLoad:e.uiOptions?.lazyLoad,lazyLoadButtonText:e.uiOptions?.lazyLoadButtonText},defaultCameraMode:e.defaultCameraMode||"orbit",allowedCameraModes:e.allowedCameraModes||["orbit","first-person","drone"],cameraMovementSpeed:e.cameraMovementSpeed,cameraRotationSensitivity:e.cameraRotationSensitivity,cameraDamping:e.cameraDamping,invertCameraRotation:e.invertCameraRotation,includeScrollControls:e.includeScrollControls??!0,scrollButtonMode:e.scrollButtonMode||"continuous",scrollAmount:e.scrollAmount||100,scrollSpeed:e.scrollSpeed,transitionSpeed:e.transitionSpeed,autoPlayEnabled:e.autoPlayEnabled??!1,autoplaySpeed:e.autoplaySpeed,loopMode:e.loopMode,includeXR:e.includeXR,xrMode:e.xrMode,customScript:e.customScript,templateType:"standard",additionalSplats:e.additionalSplats||[],keepMeshesInMemory:e.keepMeshesInMemory??!1,initialSplatExploreMode:e.initialSplatExploreMode,audioEmitters:e.audioEmitters||[]}}function s(e){const t=e.waypoints?.[0]?.fov||60;return{splatUrl:e.splatUrl,sogUrl:e.sogUrl,lodMetaUrl:e.lodMetaUrl,fallbackUrls:e.fallbackUrls,scale:e.scale,position:e.splatPosition||[0,0,0],rotation:e.splatRotation||[0,0,0],invertXScale:e.invertXScale,invertYScale:e.invertYScale,waypoints:e.waypoints,hotspots:e.hotspots,portals:e.portals,skybox:e.skybox,customMeshes:e.customMeshes,htmlMeshes:e.htmlMeshes,lights:e.lights,particles:e.particles,collisionMeshesData:e.collisionMeshesData,cameraMode:e.defaultCameraMode,defaultCameraMode:e.defaultCameraMode,allowedCameraModes:e.allowedCameraModes,autoPlay:e.autoPlayEnabled,uiColor:e.uiColor,uiOptions:e.uiOptions,fov:t,nearClip:e.minClipPlane||.1,farClip:e.maxClipPlane||1e3,playerHeight:e.playerHeight||1.6,cameraMovementSpeed:e.cameraMovementSpeed||1,cameraRotationSensitivity:e.cameraRotationSensitivity||.2,cameraDamping:e.cameraDamping||.75,invertCameraRotation:e.invertCameraRotation,scrollSpeed:e.scrollSpeed,scrollAmount:e.scrollAmount,scrollButtonMode:e.scrollButtonMode,transitionSpeed:e.transitionSpeed,autoplaySpeed:e.autoplaySpeed,loopMode:e.loopMode,additionalSplats:e.additionalSplats,keepMeshesInMemory:e.keepMeshesInMemory??!1,initialSplatExploreMode:e.initialSplatExploreMode,includeXR:e.includeXR,xrMode:e.xrMode,customScript:e.customScript,audioEmitters:e.audioEmitters||[]}}const r={tour:"Tour",explore:"Explore",hybrid:"Hybrid",walk:"Walk",orbit:"Orbit",next:"Next",previous:"Prev",startExperience:"Start Experience",returnToTour:"Return to Tour",fullscreen:"Fullscreen",mute:"Mute",unmute:"Unmute",helpTitle:"Controls & Help",percentageFormat:"{n}%"};function l(e,t){return e?.[t]||r[t]}function c(e="#4CAF50"){const t=document.getElementById("storysplat-viewer-styles");t&&t.remove();const o=document.createElement("style");return o.id="storysplat-viewer-styles",o.textContent=function(e="#4CAF50"){return`\n /* Container - CRITICAL: must be position relative and contained */\n .storysplat-viewer-container {\n position: relative;\n width: 100%;\n height: 100%;\n overflow: hidden;\n font-family: system-ui, -apple-system, sans-serif;\n box-sizing: border-box;\n }\n\n .storysplat-viewer-container * {\n box-sizing: border-box;\n }\n\n .storysplat-viewer-container canvas {\n position: absolute;\n top: 0;\n left: 0;\n width: 100% !important;\n height: 100% !important;\n display: block;\n touch-action: none;\n }\n\n /* Preloader - semi-transparent to see loading/reveal effect */\n .storysplat-preloader {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background-color: rgba(30, 30, 30, 0.85);\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n z-index: 100000;\n transition: opacity 0.5s ease-out;\n }\n\n .storysplat-preloader.hidden {\n opacity: 0;\n pointer-events: none;\n }\n\n .storysplat-preloader-content {\n display: flex;\n flex-direction: column;\n align-items: center;\n padding: 40px;\n gap: 20px;\n }\n\n .storysplat-preloader-media {\n display: flex;\n align-items: center;\n gap: 40px;\n }\n\n .storysplat-preloader-image {\n height: 200px;\n width: auto;\n object-fit: contain;\n }\n\n .storysplat-preloader-image-inverted {\n height: 200px;\n width: auto;\n object-fit: contain;\n filter: invert(1);\n margin-right: -100px;\n }\n\n .storysplat-preloader-lottie {\n height: 200px;\n width: 200px;\n }\n\n @media (max-width: 768px) {\n .storysplat-preloader-image,\n .storysplat-preloader-image-inverted {\n height: 100px;\n }\n .storysplat-preloader-image-inverted {\n margin-right: -50px;\n }\n .storysplat-preloader-lottie {\n height: 100px;\n width: 100px;\n }\n }\n\n .storysplat-preloader-progress {\n width: 200px;\n height: 4px;\n background: rgba(255, 255, 255, 0.1);\n border-radius: 2px;\n margin-top: 20px;\n position: relative;\n }\n\n .storysplat-preloader-bar {\n width: 0%;\n height: 100%;\n background: ${e};\n border-radius: 2px;\n transition: width 0.3s ease-out;\n }\n\n .storysplat-preloader-text {\n position: absolute;\n top: -25px;\n left: 50%;\n transform: translateX(-50%);\n color: white;\n font-size: 14px;\n white-space: nowrap;\n }\n\n /* Minimal Scroll Controls */\n .storysplat-scroll-controls {\n position: absolute;\n bottom: 0;\n left: 50%;\n transform: translateX(-50%);\n width: 150px;\n padding: 10px;\n display: flex;\n flex-direction: column;\n align-items: center;\n gap: 5px;\n z-index: 1000;\n }\n\n .storysplat-scroll-content {\n width: 100%;\n display: flex;\n flex-direction: column;\n align-items: center;\n gap: 5px;\n }\n\n .storysplat-progress-text {\n font-size: 14px;\n color: white;\n font-variant-numeric: tabular-nums;\n min-width: 40px;\n text-align: center;\n }\n\n .storysplat-progress-container {\n width: 90%;\n max-width: 150px;\n height: 3px;\n background: rgba(255, 255, 255, 0.2);\n border-radius: 2px;\n overflow: hidden;\n }\n\n .storysplat-progress-bar {\n height: 100%;\n background: ${e};\n transition: width 0.3s ease-out;\n width: 0%;\n }\n\n .storysplat-scroll-buttons {\n display: flex;\n justify-content: center;\n gap: 5px;\n z-index: 1000;\n }\n\n /* Minimal Button Style */\n .storysplat-btn {\n background: rgba(0, 0, 0, 0.3);\n border: none;\n color: white;\n padding: 4px 8px;\n font-size: 12px;\n border-radius: 3px;\n cursor: pointer;\n transition: background 0.2s;\n }\n\n .storysplat-btn:hover {\n background: rgba(0, 0, 0, 0.5);\n }\n\n .storysplat-btn-play {\n background: rgba(0, 0, 0, 0.3);\n border: none;\n color: white;\n padding: 4px 8px;\n border-radius: 3px;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n }\n\n .storysplat-btn-play svg {\n width: 12px;\n height: 12px;\n fill: white;\n }\n\n /* Mode Toggle - Minimal */\n .storysplat-mode-container {\n margin-top: 5px;\n }\n\n .storysplat-mode-toggle {\n display: flex;\n gap: 5px;\n }\n\n .storysplat-mode-btn {\n background: rgba(0, 0, 0, 0.3);\n border: none;\n color: white;\n padding: 3px 6px;\n font-size: 11px;\n border-radius: 2px;\n cursor: pointer;\n transition: all 0.2s ease;\n }\n\n .storysplat-mode-btn.selected {\n background: ${e} !important;\n }\n\n .storysplat-mode-btn:hover {\n background: rgba(0, 0, 0, 0.5);\n }\n\n /* Fullscreen Button - Minimal */\n .storysplat-fullscreen-btn {\n position: absolute;\n top: 10px;\n right: 10px;\n background: rgba(0, 0, 0, 0);\n border: none;\n padding: 5px;\n border-radius: 3px;\n cursor: pointer;\n z-index: 1000;\n }\n\n .storysplat-fullscreen-btn svg {\n width: 20px;\n height: 20px;\n fill: white;\n }\n\n /* Help Button - Minimal */\n .storysplat-help-btn {\n position: absolute;\n top: 10px;\n left: 10px;\n width: 30px;\n height: 30px;\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.3);\n color: white;\n border: none;\n cursor: pointer;\n font-size: 16px;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 1000;\n transition: all 0.2s ease;\n }\n\n .storysplat-help-btn:hover {\n background: rgba(0, 0, 0, 0.5);\n }\n\n .storysplat-help-panel {\n position: absolute;\n top: 50px;\n left: 10px;\n background: rgba(0, 0, 0, 0.8);\n color: white;\n padding: 15px;\n border-radius: 5px;\n max-width: 280px;\n z-index: 999;\n display: none;\n font-size: 12px;\n }\n\n .storysplat-help-panel.visible {\n display: block;\n }\n\n /* XR (VR/AR) Buttons - Minimal */\n .storysplat-xr-btn {\n position: absolute;\n top: 10px;\n right: 45px;\n background: rgba(0, 0, 0, 0.5);\n border: none;\n padding: 6px 10px;\n border-radius: 4px;\n cursor: pointer;\n z-index: 1000;\n color: white;\n font-size: 11px;\n font-weight: bold;\n text-transform: uppercase;\n transition: all 0.2s ease;\n display: none;\n }\n\n .storysplat-xr-btn.available {\n display: block;\n }\n\n .storysplat-xr-btn.active {\n background: ${e};\n }\n\n .storysplat-xr-btn:hover {\n background: rgba(0, 0, 0, 0.7);\n }\n\n .storysplat-xr-btn.active:hover {\n background: ${e};\n opacity: 0.9;\n }\n\n .storysplat-ar-btn {\n right: 85px;\n }\n\n .storysplat-help-panel h3 {\n margin: 0 0 10px 0;\n font-size: 14px;\n font-weight: 600;\n }\n\n .storysplat-help-panel p {\n margin: 5px 0;\n line-height: 1.4;\n }\n\n /* Waypoint Info - Top Banner Style (matching BabylonJS minimal export) */\n .storysplat-waypoint-info {\n position: absolute;\n top: 0;\n left: 0;\n right: 0;\n background: rgba(0, 0, 0, 0.5);\n color: white;\n text-align: center;\n z-index: 1000;\n pointer-events: none;\n display: none;\n }\n\n .storysplat-waypoint-info.hasContent {\n display: block;\n padding: 50px 30px 30px 30px;\n }\n\n .storysplat-waypoint-info h2 {\n margin: 0 0 8px 0;\n font-size: 18px;\n font-weight: bold;\n }\n\n .storysplat-waypoint-info p {\n margin: 0;\n font-size: 14px;\n line-height: 1.5;\n opacity: 0.9;\n }\n\n /* Responsive waypoint info */\n @media (max-height: 600px) {\n .storysplat-waypoint-info.hasContent {\n padding: 20px 20px 15px 20px;\n font-size: 13px;\n }\n }\n\n @media (max-height: 500px) {\n .storysplat-waypoint-info.hasContent {\n padding: 10px 15px 10px 15px;\n font-size: 12px;\n }\n }\n\n /* Hotspot Popup - Matching BabylonJS HTML export styles */\n /* Uses position: absolute so popup stays within container when embedded */\n .storysplat-hotspot-popup {\n position: absolute;\n background-color: rgba(0, 0, 0, 0.8);\n color: white;\n padding: 20px;\n border-radius: 10px;\n z-index: 100001;\n box-shadow: 0 0 10px rgba(0,0,0,0.5);\n display: none;\n font-size: 14px;\n flex-direction: column;\n font-family: system-ui, -apple-system, sans-serif;\n /* Default centered positioning */\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n max-width: 80%;\n max-height: 80%;\n overflow: auto;\n }\n\n .storysplat-hotspot-popup.visible {\n display: flex;\n }\n\n /* Fullscreen mode for click activation with image/iframe content */\n .storysplat-hotspot-popup.fullscreen {\n top: 0 !important;\n left: 0 !important;\n right: 0 !important;\n bottom: 0 !important;\n transform: none !important;\n width: 100% !important;\n height: 100% !important;\n max-width: 100% !important;\n max-height: 100% !important;\n margin: 0 !important;\n border-radius: 0 !important;\n display: flex !important;\n align-items: center !important;\n justify-content: center !important;\n flex-direction: column !important;\n padding: 0 !important;\n }\n\n .storysplat-hotspot-popup h2,\n .storysplat-hotspot-popup-title {\n margin: 0 0 10px 0;\n font-size: 18px;\n font-weight: 600;\n padding-right: 30px;\n }\n\n .storysplat-hotspot-popup p {\n margin: 0 0 10px 0;\n line-height: 1.5;\n font-size: 14px;\n white-space: pre-wrap;\n max-width: 500px;\n }\n\n .storysplat-hotspot-popup-content {\n max-width: 500px;\n }\n\n .storysplat-hotspot-popup.fullscreen .storysplat-hotspot-popup-content {\n max-width: none;\n }\n\n .storysplat-hotspot-popup img {\n max-width: 100%;\n height: auto;\n border-radius: 5px;\n margin-bottom: 10px;\n display: block;\n }\n\n .storysplat-hotspot-popup.fullscreen img {\n width: auto !important;\n height: auto !important;\n max-width: 90vw !important;\n max-height: 70vh !important;\n object-fit: contain !important;\n border-radius: 8px;\n margin: 20px 0;\n }\n\n .storysplat-hotspot-popup iframe {\n width: 100%;\n height: 400px;\n border: none;\n border-radius: 5px;\n margin-bottom: 10px;\n }\n\n .storysplat-hotspot-popup.fullscreen iframe {\n width: 90vw !important;\n max-width: 90vw !important;\n height: 70vh !important;\n max-height: 70vh !important;\n margin: 20px 0;\n border-radius: 8px;\n }\n\n /* Video container - matches HTML export sizing */\n .storysplat-hotspot-popup .video-container {\n display: flex;\n align-items: center;\n justify-content: center;\n width: 300px;\n max-width: 100%;\n height: auto;\n margin: 10px 0;\n }\n\n .storysplat-hotspot-popup video {\n max-width: 100%;\n max-height: 100%;\n object-fit: contain;\n border-radius: 5px;\n border: 2px solid rgba(255, 255, 255, 0.3);\n }\n\n /* Fullscreen popup video - matches HTML export (80% width, 50vh height) */\n .storysplat-hotspot-popup.fullscreen .video-container {\n width: 80% !important;\n max-width: 80% !important;\n height: 50vh !important;\n margin: 20px 0 !important;\n }\n\n .storysplat-hotspot-popup.fullscreen video {\n max-width: 100% !important;\n max-height: 100% !important;\n object-fit: contain !important;\n border-radius: 8px;\n }\n\n /* Close button - matching BabylonJS export (green button at bottom) */\n .storysplat-hotspot-popup-close {\n width: auto;\n padding: 10px 20px;\n background-color: #4CAF50;\n border: none;\n color: white;\n cursor: pointer;\n border-radius: 5px;\n margin: 10px 0 0 0;\n font-size: 14px;\n font-weight: 500;\n transition: background-color 0.2s;\n }\n\n .storysplat-hotspot-popup-close:hover {\n background-color: #45a049;\n }\n\n .storysplat-hotspot-popup-link {\n display: block;\n padding: 8px 16px;\n background: #007bff;\n color: white;\n text-decoration: none;\n border-radius: 4px;\n text-align: center;\n font-weight: 500;\n margin: 0 0 10px 0;\n cursor: pointer;\n transition: background-color 0.2s;\n }\n\n .storysplat-hotspot-popup-link:hover {\n background: #0056b3;\n }\n\n /* Mobile Responsive */\n @media (max-width: 768px) {\n .storysplat-scroll-controls {\n margin-bottom: 10px;\n }\n\n .storysplat-hotspot-popup.fullscreen img {\n max-width: 95vw !important;\n max-height: 60vh !important;\n margin: 10px 0;\n }\n\n .storysplat-hotspot-popup.fullscreen .video-container {\n width: 95vw !important;\n max-width: 95vw !important;\n height: 45vh !important;\n margin: 10px 0 !important;\n }\n\n .storysplat-hotspot-popup.fullscreen iframe {\n width: 95vw !important;\n max-width: 95vw !important;\n height: 60vh !important;\n max-height: 60vh !important;\n margin: 10px 0;\n }\n\n .storysplat-hotspot-popup-title {\n font-size: 16px !important;\n padding: 10px 15px !important;\n }\n }\n\n @media (max-width: 540px) {\n .storysplat-scroll-controls {\n margin-bottom: 45px;\n }\n\n .storysplat-hotspot-popup.fullscreen img {\n max-width: 98vw !important;\n max-height: 55vh !important;\n }\n\n .storysplat-hotspot-popup.fullscreen .video-container {\n width: 98vw !important;\n max-width: 98vw !important;\n height: 40vh !important;\n }\n\n .storysplat-hotspot-popup.fullscreen iframe {\n width: 98vw !important;\n max-width: 98vw !important;\n height: 55vh !important;\n max-height: 55vh !important;\n }\n }\n\n /* Virtual Joystick Overlay - visual indicator only, touches pass through to canvas */\n .storysplat-joystick-container {\n position: absolute;\n bottom: 80px;\n left: 40px;\n width: 120px;\n height: 120px;\n z-index: 2000;\n pointer-events: none;\n touch-action: none;\n display: none;\n }\n\n .storysplat-joystick-container.visible {\n display: block;\n }\n\n .storysplat-joystick-base {\n position: absolute;\n top: 0;\n left: 0;\n width: 120px;\n height: 120px;\n background: rgba(255, 255, 255, 0.2);\n border: 3px solid rgba(255, 255, 255, 0.4);\n border-radius: 50%;\n pointer-events: none;\n }\n\n .storysplat-joystick-thumb {\n position: absolute;\n top: 50%;\n left: 50%;\n width: 50px;\n height: 50px;\n margin-left: -25px;\n margin-top: -25px;\n background: rgba(255, 255, 255, 0.6);\n border: 3px solid rgba(255, 255, 255, 0.8);\n border-radius: 50%;\n pointer-events: none;\n transition: transform 0.05s ease-out;\n }\n\n .storysplat-joystick-thumb.active {\n background: rgba(255, 255, 255, 0.8);\n border-color: white;\n }\n\n /* Look Zone Indicator (right side) */\n .storysplat-look-zone {\n position: absolute;\n bottom: 80px;\n right: 40px;\n width: 100px;\n height: 100px;\n z-index: 1000;\n pointer-events: none;\n display: none;\n }\n\n .storysplat-look-zone.visible {\n display: flex;\n align-items: center;\n justify-content: center;\n }\n\n .storysplat-look-zone-icon {\n width: 60px;\n height: 60px;\n background: rgba(255, 255, 255, 0.15);\n border: 2px solid rgba(255, 255, 255, 0.25);\n border-radius: 50%;\n display: flex;\n align-items: center;\n justify-content: center;\n }\n\n .storysplat-look-zone-icon svg {\n width: 30px;\n height: 30px;\n fill: rgba(255, 255, 255, 0.5);\n }\n\n /* Look Zone Active State */\n .storysplat-look-zone.active .storysplat-look-zone-icon {\n background: rgba(255, 255, 255, 0.25);\n border-color: rgba(255, 255, 255, 0.5);\n }\n\n .storysplat-look-zone.active .storysplat-look-zone-icon svg {\n fill: rgba(255, 255, 255, 0.8);\n }\n\n /* Camera Mode Toggle (Orbit/Fly) - Mobile only in explore mode */\n .storysplat-camera-mode-toggle {\n position: absolute;\n bottom: 210px;\n left: 40px;\n width: 44px;\n height: 44px;\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.5);\n border: 2px solid rgba(255, 255, 255, 0.3);\n cursor: pointer;\n z-index: 2001;\n display: none;\n align-items: center;\n justify-content: center;\n touch-action: manipulation;\n -webkit-tap-highlight-color: transparent;\n }\n\n .storysplat-camera-mode-toggle.visible {\n display: flex;\n }\n\n .storysplat-camera-mode-toggle:active {\n background: rgba(0, 0, 0, 0.7);\n transform: scale(0.95);\n }\n\n .storysplat-camera-mode-toggle svg {\n width: 24px;\n height: 24px;\n fill: white;\n }\n\n .storysplat-camera-mode-toggle .orbit-icon,\n .storysplat-camera-mode-toggle .fly-icon {\n display: none;\n }\n\n .storysplat-camera-mode-toggle.orbit .orbit-icon {\n display: block;\n }\n\n .storysplat-camera-mode-toggle.fly .fly-icon {\n display: block;\n }\n\n /* Return to Waypoint Button - Explore mode only */\n .storysplat-return-waypoint-btn {\n position: absolute;\n bottom: 20px;\n left: 20px;\n padding: 10px 16px;\n background: rgba(0, 0, 0, 0.6);\n border: 1px solid rgba(255, 255, 255, 0.3);\n border-radius: 8px;\n color: white;\n font-size: 13px;\n font-weight: 500;\n cursor: pointer;\n z-index: 2001;\n display: none;\n align-items: center;\n gap: 8px;\n transition: all 0.2s ease;\n backdrop-filter: blur(4px);\n -webkit-backdrop-filter: blur(4px);\n }\n\n .storysplat-return-waypoint-btn.visible {\n display: flex;\n }\n\n .storysplat-return-waypoint-btn:hover {\n background: rgba(0, 0, 0, 0.8);\n border-color: rgba(255, 255, 255, 0.5);\n }\n\n .storysplat-return-waypoint-btn:active {\n transform: scale(0.98);\n }\n\n .storysplat-return-waypoint-btn svg {\n width: 16px;\n height: 16px;\n fill: white;\n }\n\n @media (max-width: 768px) {\n .storysplat-return-waypoint-btn {\n bottom: auto;\n top: 60px;\n left: 10px;\n padding: 8px 12px;\n font-size: 12px;\n }\n }\n\n /* Lazy Load Container */\n .storysplat-lazy-load-container {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n display: flex;\n justify-content: center;\n align-items: center;\n background: #1a1a1a;\n z-index: 10001;\n }\n\n .storysplat-lazy-load-thumbnail {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n object-fit: cover;\n opacity: 0.7;\n }\n\n .storysplat-lazy-load-thumbnail-video {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n object-fit: cover;\n opacity: 0.7;\n }\n\n .storysplat-lazy-load-overlay {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: linear-gradient(\n to bottom,\n rgba(0, 0, 0, 0.3) 0%,\n rgba(0, 0, 0, 0.5) 50%,\n rgba(0, 0, 0, 0.7) 100%\n );\n }\n\n .storysplat-lazy-load-content {\n position: relative;\n z-index: 1;\n text-align: center;\n display: flex;\n flex-direction: column;\n align-items: center;\n gap: 16px;\n }\n\n .storysplat-lazy-load-start-btn {\n padding: 16px 32px;\n background: ${e};\n border: none;\n border-radius: 50px;\n color: white;\n font-size: 18px;\n font-weight: 600;\n cursor: pointer;\n display: flex;\n align-items: center;\n gap: 12px;\n transition: all 0.3s ease;\n box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);\n }\n\n .storysplat-lazy-load-start-btn:hover {\n transform: scale(1.05);\n box-shadow: 0 6px 30px rgba(0, 0, 0, 0.4);\n }\n\n .storysplat-lazy-load-start-btn:active {\n transform: scale(0.98);\n }\n\n .storysplat-lazy-load-start-btn svg {\n width: 24px;\n height: 24px;\n fill: white;\n }\n\n @media (max-width: 768px) {\n .storysplat-lazy-load-start-btn {\n padding: 12px 24px;\n font-size: 16px;\n }\n }\n\n /* Watermark - matches BabylonJS HTML export */\n .storysplat-watermark {\n position: fixed;\n bottom: 10px;\n right: 10px;\n background-color: rgba(0, 0, 0, 0.5);\n color: white;\n padding: 5px 10px;\n border-radius: 5px;\n font-size: 12px;\n z-index: 1000;\n pointer-events: auto; /* Allow pointer events for the entire watermark so links work */\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;\n cursor: default;\n }\n\n .storysplat-watermark a {\n color: ${e};\n text-decoration: none;\n cursor: pointer;\n }\n\n .storysplat-watermark a:hover {\n text-decoration: underline;\n }\n `}(e),document.head.appendChild(o),o}function d(e,t){const o=document.createElement("div");o.className="storysplat-preloader";const n=!!t;return o.innerHTML=`\n <div class="storysplat-preloader-content">\n <div class="storysplat-preloader-media">\n ${n?`<img class="storysplat-preloader-image" src="${t}" alt="Custom Logo" />`:'<img class="storysplat-preloader-image-inverted" src="https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fimages%2FStorySplat.webp?alt=media&token=953e8ab3-1865-4ac1-a98d-b548b7066bda" alt="StorySplat Logo" />'}\n ${n?"":'<lottie-player class="storysplat-preloader-lottie"\n src="https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Flotties%2FstorySplatLottie.json?alt=media&token=d7edc19d-9cb8-4c6e-a94c-cba1d2b65d5e"\n background="transparent"\n speed="1"\n loop\n autoplay>\n </lottie-player>'}\n </div>\n <div class="storysplat-preloader-progress">\n <div class="storysplat-preloader-text">Loading... 0%</div>\n <div class="storysplat-preloader-bar"></div>\n </div>\n </div>\n `,e.appendChild(o),n||new Promise(e=>{if(customElements.get("lottie-player"))return void e();const t=document.querySelector('script[src*="lottie-player"]');if(t)return void t.addEventListener("load",()=>e());const o=document.createElement("script");o.src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js",o.onload=()=>e(),document.head.appendChild(o)}),o}function p(e){e.classList.add("hidden"),setTimeout(()=>e.remove(),500)}function h(e,t,o="tour",n){const i=e.scrollControls?.querySelector(".storysplat-btn-prev"),a=e.scrollControls?.querySelector(".storysplat-btn-next"),s=e.scrollControls?.querySelector(".storysplat-btn-play");if(i&&i.addEventListener("click",()=>t.prevWaypoint()),a&&a.addEventListener("click",()=>t.nextWaypoint()),s){const e=e=>{s.innerHTML=e?'<svg viewBox="0 0 24 24"><path d="M6 4h4v16H6zm8 0h4v16h-4z"/></svg>':'<svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>'};s.addEventListener("click",()=>{t.isPlaying()?t.pause():t.play()}),t.on("playbackStart",()=>e(!0)),t.on("playbackStop",()=>e(!1)),e(t.isPlaying())}if(e.helpButton&&e.helpPanel&&e.helpButton.addEventListener("click",()=>{e.helpPanel.classList.toggle("visible")}),e.fullscreenButton){if(/iPad|iPhone|iPod/.test(navigator.userAgent)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1)e.fullscreenButton.style.display="none",console.log("[StorySplat Viewer] Fullscreen button hidden on iOS (API not supported)");else{const t=e.fullscreenButton.parentElement;e.fullscreenButton.addEventListener("click",()=>{const o=document;if(o.fullscreenElement||o.webkitFullscreenElement){o.exitFullscreen?o.exitFullscreen():o.webkitExitFullscreen&&o.webkitExitFullscreen();const t=e.fullscreenButton.querySelector(".storysplat-expand-icon"),n=e.fullscreenButton.querySelector(".storysplat-compress-icon");t&&(t.style.display="block"),n&&(n.style.display="none")}else{t?.requestFullscreen?t.requestFullscreen():t?.webkitRequestFullscreen&&t.webkitRequestFullscreen();const o=e.fullscreenButton.querySelector(".storysplat-expand-icon"),n=e.fullscreenButton.querySelector(".storysplat-compress-icon");o&&(o.style.display="none"),n&&(n.style.display="block")}});const o=()=>{const t=document,o=t.fullscreenElement||t.webkitFullscreenElement,n=e.fullscreenButton.querySelector(".storysplat-expand-icon"),i=e.fullscreenButton.querySelector(".storysplat-compress-icon");n&&(n.style.display=o?"none":"block"),i&&(i.style.display=o?"block":"none")};document.addEventListener("fullscreenchange",o),document.addEventListener("webkitfullscreenchange",o)}}const l=t=>{const o=e.scrollControls?.querySelector(".storysplat-progress-text"),n=e.scrollControls?.querySelector(".storysplat-progress-container"),i=e.scrollControls?.querySelector(".storysplat-scroll-buttons"),a=t?"":"none";o&&(o.style.display=a),n&&(n.style.display=a),i&&(i.style.display=a)};if(l("tour"===o),t.setCameraMode){const o=e.scrollControls?.querySelectorAll(".storysplat-mode-btn");o?.forEach(e=>{e.addEventListener("click",()=>{const n=e.getAttribute("data-mode");n&&t.setCameraMode&&(t.setCameraMode(n),o.forEach(e=>e.classList.remove("selected")),e.classList.add("selected"),l("tour"===n))})}),t.on("modeChange",({mode:e})=>{l("tour"===e),o?.forEach(t=>{const o=t.getAttribute("data-mode");t.classList.toggle("selected",o===e)})})}let c=0,d=-1;t.on("progressUpdate",({progress:t})=>{const o=100*Math.max(0,Math.min(1,t)),i=Math.round(o),a=performance.now();var s,l;a-c<33||(c=a,e.progressBar&&(e.progressBar.style.width=`${o}%`),e.progressText&&i!==d&&(d=i,e.progressText.innerHTML="",e.progressText.textContent=(s=n,l=i,(s?.percentageFormat||r.percentageFormat).replace("{n}",String(l)))))});let p=-1;t.on("waypointChange",({index:o,waypoint:n})=>{if(o===p)return;if(p=o,!e.waypointInfo)return;const i=e.waypointInfo.querySelector(".storysplat-waypoint-title"),a=e.waypointInfo.querySelector(".storysplat-waypoint-description");if(!i||!a)return;const s=n||t.getWaypoints?.()[o];s&&(s.name||s.info)?(i.textContent=s.name||"",a.textContent=s.info||"",s.name||s.info?e.waypointInfo.classList.add("hasContent"):e.waypointInfo.classList.remove("hasContent")):(i.textContent="",a.textContent="",e.waypointInfo.classList.remove("hasContent"))})}function u(e,t){const o=e.querySelector(".storysplat-hotspot-popup");if(!o)return;const n=o.querySelector(".storysplat-hotspot-popup-title"),i=o.querySelector(".storysplat-hotspot-popup-content"),a=o.querySelector(".storysplat-hotspot-popup-close");o.style.cssText="",o.classList.remove("fullscreen");const s=t.activationMode||"click",r=t.photoUrl||t.popupVideoUrl||"iframe"===t.contentType&&t.iframeUrl;"click"===s&&r&&o.classList.add("fullscreen"),t.backgroundColor&&(o.style.backgroundColor=t.backgroundColor),t.textColor&&(o.style.color=t.textColor,n&&(n.style.color=t.textColor)),t.fontFamily&&(o.style.fontFamily=t.fontFamily),t.fontSize&&(o.style.fontSize=`${t.fontSize}px`),a&&t.closeButtonColor&&(a.style.backgroundColor=t.closeButtonColor),n&&(n.textContent=t.title||"Hotspot");let l="";if("iframe"===t.contentType&&t.iframeUrl&&(l+=`<iframe src="${t.iframeUrl}" title="${t.title||"Embedded content"}"></iframe>`),t.popupVideoUrl&&(l+=`<div class="video-container"><video src="${t.popupVideoUrl}" controls playsinline webkit-playsinline preload="metadata"></video></div>`),t.photoUrl&&(l+=`<img src="${t.photoUrl}" alt="${t.title||"Hotspot image"}" />`),t.information&&(l+=`<p>${t.information}</p>`),t.externalLinkUrl){const e=t.externalLinkButtonColor||"#007bff";l+=`\n <div onclick="window.open('${t.externalLinkUrl}', '_blank', 'noopener,noreferrer')"\n class="storysplat-hotspot-popup-link" style="background-color: ${e}">\n ${t.externalLinkText||"Open External Link"}\n </div>\n `}i&&(i.innerHTML=l),o.classList.add("visible")}function m(e,t){e.joystick&&(t?e.joystick.classList.add("visible"):e.joystick.classList.remove("visible")),e.lookZone&&(t?e.lookZone.classList.add("visible"):e.lookZone.classList.remove("visible"))}function g(e,t,o,n,i){if(e.joystickThumb)if(t){e.joystickThumb.classList.add("active");const t=Math.sqrt(o*o+n*n),a=Math.min(t,i),s=t>0?a/t:0,r=o*s,l=n*s;e.joystickThumb.style.transform=`translate(${r}px, ${l}px)`}else e.joystickThumb.classList.remove("active"),e.joystickThumb.style.transform="translate(0, 0)"}function y(e,t){e.lookZone&&(t?e.lookZone.classList.add("active"):e.lookZone.classList.remove("active"))}function f(e,t){e.cameraModeToggle&&(t?e.cameraModeToggle.classList.add("visible"):e.cameraModeToggle.classList.remove("visible"))}function v(e,t){e.cameraModeToggle&&(e.cameraModeToggle.classList.remove("orbit","fly"),e.cameraModeToggle.classList.add(t))}function b(e,t){e.returnWaypointButton&&(t?e.returnWaypointButton.classList.add("visible"):e.returnWaypointButton.classList.remove("visible"))}const w=new e.Vec3,x=new e.Vec3,S=new e.Pose,E=new e.InputFrame({move:[0,0,0],rotate:[0,0,0]}),M=(e,t,o)=>{const n=Math.sqrt(e[0]*e[0]+e[1]*e[1]);if(n<t)return void e.fill(0);const i=(n-t)/(o-t);e[0]*=i/n,e[1]*=i/n},C=(t,o,n,i,a=new e.Vec3)=>{const{fov:s,aspectRatio:r,horizontalFov:l,projection:c,orthoHeight:d}=t,p=t.system?.app,{width:h,height:u}=p?.graphicsDevice?.clientRect||{width:1920,height:1080};a.set(-o/h*2,n/u*2,0);const m=x.set(0,0,0);if(c===e.PROJECTION_PERSPECTIVE){const t=i*Math.tan(.5*s*e.math.DEG_TO_RAD);l?m.set(t,t/r,0):m.set(t*r,t,0)}else m.set(d*r,d,0);return a.mul(m),a};class _{constructor(t,o,n={}){this.enabled=!0,this._mode="orbit",this._enableOrbit=!0,this._enableFly=!0,this.enablePan=!0,this._pose=new e.Pose,this._startZoomDist=0,this._pitchRange=new e.Vec2(-89,89),this._yawRange=new e.Vec2(-1/0,1/0),this._lastFocusPoint=new e.Vec3(0,0,0),this._zoomRange=new e.Vec2(.01,1/0),this._state={axis:new e.Vec3,shift:0,ctrl:0,mouse:[0,0,0],touches:0},this.moveSpeed=25,this.moveFastSpeed=50,this.moveSlowSpeed=10,this.rotateSpeed=.05,this.rotateTouchSens=.1,this.rotateJoystickSens=1,this.zoomSpeed=.001,this.zoomPinchSens=5,this.keyboardSpeedMultiplier=1.5,this.gamepadDeadZone=new e.Vec2(.3,.6),this.invertRotation=!1,this.joystickEventName="joystick",this._destroyHandler=null,this.camera=t,this.app=o;const i=t.camera;if(!i)throw new Error("CameraControls: camera component not found on entity");this.cameraComponent=i,this._flyController=new e.FlyController,this._orbitController=new e.OrbitController,this._focusController=new e.FocusController,this._flyController.moveDamping=.75,this._flyController.rotateDamping=.75,this._orbitController.rotateDamping=.75,this._orbitController.zoomDamping=.8,this._orbitController.zoomRange=new e.Vec2(.01,1/0);const a=o.graphicsDevice.canvas;this._desktopInput=new e.KeyboardMouseSource,this._orbitMobileInput=new e.MultiTouchSource,this._flyMobileInput=new e.DualGestureSource,this._gamepadInput=new e.GamepadSource,this._desktopInput.attach(a),this._orbitMobileInput.attach(a),this._flyMobileInput.attach(a),this._gamepadInput.attach(a),this._flyMobileInput.on("joystick:position:left",([e,t,o,n])=>{"fly"===this._mode&&this.app.fire(`${this.joystickEventName}:left`,e,t,o,n)}),this._flyMobileInput.on("joystick:position:right",([e,t,o,n])=>{"fly"===this._mode&&this.app.fire(`${this.joystickEventName}:right`,e,t,o,n)}),this._pose.look(this.camera.getPosition(),e.Vec3.ZERO),this._setMode("orbit"),this._controller=this._orbitController,void 0!==n.enableOrbit&&(this.enableOrbit=n.enableOrbit),void 0!==n.enableFly&&(this.enableFly=n.enableFly),void 0!==n.enablePan&&(this.enablePan=n.enablePan),n.focusPoint&&(this.focusPoint=n.focusPoint),void 0!==n.moveSpeed&&(this.moveSpeed=n.moveSpeed),void 0!==n.moveFastSpeed&&(this.moveFastSpeed=n.moveFastSpeed),void 0!==n.moveSlowSpeed&&(this.moveSlowSpeed=n.moveSlowSpeed),void 0!==n.rotateSpeed&&(this.rotateSpeed=n.rotateSpeed),void 0!==n.rotateTouchSens&&(this.rotateTouchSens=n.rotateTouchSens),void 0!==n.rotateJoystickSens&&(this.rotateJoystickSens=n.rotateJoystickSens),void 0!==n.zoomSpeed&&(this.zoomSpeed=n.zoomSpeed),void 0!==n.zoomPinchSens&&(this.zoomPinchSens=n.zoomPinchSens),void 0!==n.focusDamping&&(this.focusDamping=n.focusDamping),void 0!==n.rotateDamping&&(this.rotateDamping=n.rotateDamping),void 0!==n.moveDamping&&(this.moveDamping=n.moveDamping),void 0!==n.zoomDamping&&(this.zoomDamping=n.zoomDamping),n.pitchRange&&(this.pitchRange=n.pitchRange),n.yawRange&&(this.yawRange=n.yawRange),n.zoomRange&&(this.zoomRange=n.zoomRange),n.gamepadDeadZone&&(this.gamepadDeadZone=n.gamepadDeadZone),n.mobileInputLayout&&(this.mobileInputLayout=n.mobileInputLayout),void 0!==n.invertRotation&&(this.invertRotation=n.invertRotation)}set enableFly(e){this._enableFly=e,this._enableFly||"fly"!==this._mode||this._setMode("orbit")}get enableFly(){return this._enableFly}set enableOrbit(e){this._enableOrbit=e,this._enableOrbit||"orbit"!==this._mode||this._setMode("fly")}get enableOrbit(){return this._enableOrbit}set focusDamping(e){this._focusController.focusDamping=e}get focusDamping(){return this._focusController.focusDamping}set moveDamping(e){this._flyController.moveDamping=e}get moveDamping(){return this._flyController.moveDamping}set rotateDamping(e){this._flyController.rotateDamping=e,this._orbitController.rotateDamping=e}get rotateDamping(){return this._orbitController.rotateDamping}set zoomDamping(e){this._orbitController.zoomDamping=e}get zoomDamping(){return this._orbitController.zoomDamping}set focusPoint(e){const t=this.camera.getPosition();this._startZoomDist=t.distance(e),this._controller.attach(this._pose.look(t,e),!1)}get focusPoint(){return this._pose.getFocus(w)}set pitchRange(e){this._pitchRange.copy(e),this._flyController.pitchRange=this._pitchRange,this._orbitController.pitchRange=this._pitchRange}get pitchRange(){return this._pitchRange}set yawRange(t){this._yawRange.x=e.math.clamp(t.x,-360,360),this._yawRange.y=e.math.clamp(t.y,-360,360),this._flyController.yawRange=this._yawRange,this._orbitController.yawRange=this._yawRange}get yawRange(){return this._yawRange}set zoomRange(e){this._zoomRange.x=e.x,this._zoomRange.y=e.y<=e.x?1/0:e.y,this._orbitController.zoomRange=this._zoomRange}get zoomRange(){return this._zoomRange}set mobileInputLayout(e){/(?:joystick|touch)-(?:joystick|touch)/.test(e)?this._flyMobileInput.layout=e:console.warn(`CameraControls: invalid mobile input layout: ${e}`)}get mobileInputLayout(){return this._flyMobileInput.layout}get mode(){return this._mode}_setMode(e){if(this._enableFly&&!this._enableOrbit)e="fly";else if(!this._enableFly&&this._enableOrbit)e="orbit";else if(!this._enableFly&&!this._enableOrbit)return void console.warn("CameraControls: both fly and orbit modes are disabled");const t=this._mode;if(t!==e){switch(this._mode=e,this._controller&&this._controller.detach(),this._mode){case"orbit":if(this._controller=this._orbitController,"focus"===t){const e=this.camera.getPosition();this._pose.look(e,this._lastFocusPoint)}this._lastYaw=this._pose.angles.y;break;case"fly":this._controller=this._flyController;break;case"focus":this._controller=this._focusController}this._controller.attach(this._pose,!1),this.app.fire("cameracontrols:modechange",this._mode)}}setMode(e){this._setMode(e)}focus(e,t=!1){this._lastFocusPoint.copy(e),this._setMode("focus");const o=t?this._startZoomDist:this.camera.getPosition().distance(e);this._startZoomDist=o;const n=w.copy(this.camera.forward).mulScalar(-o).add(e);this._controller.attach(S.look(n,e))}look(e,t=!1){this._setMode("focus");const o=t?w.copy(this.camera.getPosition()).sub(e).normalize().mulScalar(this._startZoomDist).add(e):this.camera.getPosition();this._controller.attach(S.look(o,e))}reset(e,t){this._setMode("focus"),this._controller.attach(S.look(t,e))}syncFromCamera(e){const t=this.camera.getPosition().clone();if(e){this._lastFocusPoint.copy(e);const o=t.distance(e);this._startZoomDist=o,this._pose.distance=o,this._pose.look(t,e);let n=this._pose.angles.y;for(;n>180;)n-=360;for(;n<-180;)n+=360;this._pose.angles.y=n,this._lastYaw=n,this._pose.angles.x=Math.max(-89,Math.min(89,this._pose.angles.x)),this._pose.angles.z=0}else{const e=this.camera.getEulerAngles();this._pose.position.copy(t),this._pose.angles.x=e.x,this._pose.angles.y=e.y,this._pose.angles.z=0;let o=this._pose.angles.y;for(;o>180;)o-=360;for(;o<-180;)o+=360;this._pose.angles.y=o,this._lastYaw=o,this._pose.angles.x=Math.max(-89,Math.min(89,this._pose.angles.x));const n=this.camera.forward.clone();this._lastFocusPoint.copy(t).add(n.mulScalar(10));const i=10;this._startZoomDist=i,this._pose.distance=i}this._controller.attach(this._pose,!1)}syncFromPose(t,o,n){this.camera.setPosition(t),this.camera.setRotation(o);const i=new e.Entity;i.setRotation(o);const a=i.getEulerAngles();this._pose.position.copy(t),this._pose.angles.x=a.x,this._pose.angles.y=a.y,this._pose.angles.z=0;let s=this._pose.angles.y;for(;s>180;)s-=360;for(;s<-180;)s+=360;if(this._pose.angles.y=s,this._lastYaw=s,this._pose.angles.x=Math.max(-89,Math.min(89,this._pose.angles.x)),n)this._lastFocusPoint.copy(n);else{const e=this.camera.forward.clone();this._lastFocusPoint.copy(t).add(e.mulScalar(10))}const r=t.distance(this._lastFocusPoint);this._startZoomDist=r,this._pose.distance=r,this._controller.attach(this._pose,!1)}enable(){this.enabled=!0}disable(){this.enabled=!1,this._desktopInput.read(),this._orbitMobileInput.read(),this._flyMobileInput.read(),this._gamepadInput.read()}update(t){if(!this.enabled)return;const{keyCode:o}=e.KeyboardMouseSource,{key:n,button:i,mouse:a,wheel:s}=this._desktopInput.read(),{touch:r,pinch:l,count:c}=this._orbitMobileInput.read(),{leftInput:d,rightInput:p}=this._flyMobileInput.read(),{leftStick:h,rightStick:u}=this._gamepadInput.read();M(h,this.gamepadDeadZone.x,this.gamepadDeadZone.y),M(u,this.gamepadDeadZone.x,this.gamepadDeadZone.y),this._state.axis.add(w.set(n[o.D]-n[o.A]+(n[o.RIGHT]-n[o.LEFT]),n[o.E]-n[o.Q],n[o.W]-n[o.S]+(n[o.UP]-n[o.DOWN])));for(let e=0;e<this._state.mouse.length;e++)this._state.mouse[e]+=i[e];this._state.shift+=n[o.SHIFT],this._state.ctrl+=n[o.CTRL],this._state.touches+=c[0],1===i[0]||1===i[1]||0!==s[0]?this._setMode("orbit"):(1===i[2]||this._state.axis.length()>0)&&this._setMode("fly");const m=+("orbit"===this._mode),g=+("fly"===this._mode),y=+(this._state.touches>1),f=+(this._state.shift||this._state.mouse[1]),v=+this._flyMobileInput.layout.endsWith("joystick"),b=(this._state.shift?this.moveFastSpeed:this._state.ctrl?this.moveSlowSpeed:this.moveSpeed)*t,S=60*this.zoomSpeed*t,_=S*this.zoomPinchSens,L=60*this.rotateSpeed*t,T=L*this.rotateTouchSens,k=this.rotateSpeed*this.rotateJoystickSens*60*t,{deltas:A}=E,P=w.set(0,0,0),z=this._state.axis.clone().normalize();P.add(z.mulScalar(g*b*this.keyboardSpeedMultiplier));const R=C(this.cameraComponent,a[0],a[1],this._pose.distance);P.add(R.mulScalar(m*f*+this.enablePan));const I=x.set(0,0,s[0]);P.add(I.mulScalar(m*S)),A.move.append([P.x,P.y,P.z]),P.set(0,0,0);const D=x.set(a[0],a[1],0);P.add(D.mulScalar((1-m*f)*L)),this.invertRotation&&(P.y=-P.y),A.rotate.append([P.x,P.y,P.z]),P.set(0,0,0);const F=x.set(d[0],0,-d[1]);P.add(F.mulScalar(g*b));const U=C(this.cameraComponent,r[0],r[1],this._pose.distance);P.add(U.mulScalar(m*y*+this.enablePan));const B=x.set(0,0,l[0]);P.add(B.mulScalar(m*y*_)),A.move.append([P.x,P.y,P.z]),P.set(0,0,0);const V=x.set(r[0],r[1],0);P.add(V.mulScalar(m*(1-y)*T));const $=x.set(p[0],p[1],0);P.add($.mulScalar(g*(v?k:T))),this.invertRotation&&(P.y=-P.y),A.rotate.append([P.x,P.y,P.z]),P.set(0,0,0);const H=x.set(h[0],0,-h[1]);P.add(H.mulScalar(g*b)),A.move.append([P.x,P.y,P.z]),P.set(0,0,0);const O=x.set(u[0],u[1],0);if(P.add(O.mulScalar(g*k)),this.invertRotation&&(P.y=-P.y),A.rotate.append([P.x,P.y,P.z]),this.app.xr?.active)E.read();else{if("focus"===this._mode){const e=A.move.length()+A.rotate.length()>0,t=this._focusController.complete?.()??!1;(e||t)&&this._setMode("orbit")}if(this._pose.copy(this._controller.update(E,t)),"orbit"===this._mode){let e=this._pose.angles.y;for(;e>180;)e-=360;for(;e<-180;)e+=360;if(void 0!==this._lastYaw){const t=e-this._lastYaw;t>180?e-=360:t<-180&&(e+=360)}this._pose.angles.y=e,this._lastYaw=e}this.camera.setPosition(this._pose.position),this.camera.setEulerAngles(this._pose.angles)}}destroy(){this._desktopInput.destroy(),this._orbitMobileInput.destroy(),this._flyMobileInput.destroy(),this._gamepadInput.destroy(),this._flyController.destroy(),this._orbitController.destroy()}}class L{constructor(t,o,n={}){this.enabled=!1,this.velocity=new e.Vec3,this.isGrounded=!1,this.yaw=0,this.pitch=0,this.keys={},this.mouseLocked=!1,this.moveSpeed=8,this.sprintMultiplier=2,this.lookSensitivity=.002,this.playerHeight=1.6,this.gravity=20,this.maxFallSpeed=50,this.jumpVelocity=8,this.collisionRadius=.3,this.stepHeight=.3,this.groundCheckDistance=.1,this.moveDamping=.9,this.horizontalVelocity=new e.Vec3,this.targetVelocity=new e.Vec3,this.collisionEntities=[],this.floorEntity=null,this.keydownHandler=null,this.keyupHandler=null,this.mousemoveHandler=null,this.clickHandler=null,this.pointerlockchangeHandler=null,this.tmpVec=new e.Vec3,this.tmpVec2=new e.Vec3,this.forward=new e.Vec3,this.right=new e.Vec3,this.camera=t,this.app=o,void 0!==n.moveSpeed&&(this.moveSpeed=n.moveSpeed),void 0!==n.sprintMultiplier&&(this.sprintMultiplier=n.sprintMultiplier),void 0!==n.lookSensitivity&&(this.lookSensitivity=n.lookSensitivity),void 0!==n.playerHeight&&(this.playerHeight=n.playerHeight),void 0!==n.gravity&&(this.gravity=n.gravity),void 0!==n.maxFallSpeed&&(this.maxFallSpeed=n.maxFallSpeed),void 0!==n.jumpVelocity&&(this.jumpVelocity=n.jumpVelocity),void 0!==n.collisionRadius&&(this.collisionRadius=n.collisionRadius),void 0!==n.stepHeight&&(this.stepHeight=n.stepHeight),void 0!==n.groundCheckDistance&&(this.groundCheckDistance=n.groundCheckDistance),void 0!==n.moveDamping&&(this.moveDamping=n.moveDamping);const i=this.camera.getEulerAngles();this.pitch=i.x,this.yaw=i.y}async createCollisionMeshes(t){if(!t||0===t.length)return;console.log("[CharacterController] Creating collision meshes:",t.length);const o=[];t.forEach((t,n)=>{if("custom"===t.meshType&&t.customMeshUrl){const e=this.loadCustomCollisionMesh(t,n);return void o.push(e)}let i=null;switch(t.meshType){case"cube":i=new e.Entity(`collision-cube-${n}`),i.addComponent("render",{type:"box"});break;case"sphere":i=new e.Entity(`collision-sphere-${n}`),i.addComponent("render",{type:"sphere"});break;case"floor":i=new e.Entity(`collision-floor-${n}`),i.addComponent("render",{type:"plane"}),this.floorEntity=i;break;default:i=new e.Entity(`collision-plane-${n}`),i.addComponent("render",{type:"plane"})}i&&this.configureCollisionEntity(i,t)}),o.length>0&&await Promise.all(o),console.log("[CharacterController] Created",this.collisionEntities.length,"collision entities")}async loadCustomCollisionMesh(t,o){const n=t.customMeshUrl;console.log("[CharacterController] Loading custom collision mesh:",n);try{const i=n.split("?")[0].split(".").pop()?.toLowerCase()||"glb",a="gltf"===i||"glb"===i?"container":"model",s=new e.Asset(`collision-custom-${o}`,a,{url:n});await new Promise((i,r)=>{s.ready(()=>{try{const n=new e.Entity(`collision-custom-${o}`);if("container"===a){const e=s.resource;if(e&&e.instantiateRenderEntity){const t=e.instantiateRenderEntity();for(;t.children.length>0;)n.addChild(t.children[0]);t.destroy()}}else n.addComponent("model",{asset:s});this.configureCollisionEntity(n,t),this.computeAndStoreBounds(n),i()}catch(e){console.error("[CharacterController] Error setting up custom mesh:",e),r(e)}}),s.on("error",e=>{console.error("[CharacterController] Error loading custom mesh:",n,e),r(e)}),this.app.assets.add(s),this.app.assets.load(s)})}catch(e){console.error("[CharacterController] Failed to load custom collision mesh:",n,e)}}computeAndStoreBounds(t){const o=new e.BoundingBox;let n=!1;const i=t=>{if(t.render&&t.render.meshInstances)for(const e of t.render.meshInstances)e.aabb&&(n?o.add(e.aabb):(o.copy(e.aabb),n=!0));for(const o of t.children)o instanceof e.Entity&&i(o)};i(t),n&&(t._collisionBounds=o,console.log("[CharacterController] Computed bounds for custom mesh:",o.halfExtents))}configureCollisionEntity(e,t){const o=t.position||[0,0,0];e.setPosition(o[0],o[1],-o[2]);const n=t.rotation||[0,0,0];e.setEulerAngles(n[0]*(180/Math.PI),n[1]*(180/Math.PI),-n[2]*(180/Math.PI));const i=t.scaling||[1,1,1];e.setLocalScale(i[0],i[1],i[2]),this.setEntityVisibility(e,!1),e._collisionMeshType=t.meshType,this.app.root.addChild(e),this.collisionEntities.push(e)}setEntityVisibility(t,o){t.render&&(t.render.enabled=o);for(const n of t.children)n instanceof e.Entity&&this.setEntityVisibility(n,o)}enable(){if(this.enabled)return;this.enabled=!0;const e=this.camera.getEulerAngles();this.pitch=e.x,this.yaw=e.y,this.velocity.set(0,0,0),this.horizontalVelocity.set(0,0,0),this.targetVelocity.set(0,0,0),this.setupInputHandlers(),console.log("[CharacterController] Enabled")}disable(){this.enabled&&(this.enabled=!1,this.removeInputHandlers(),document.pointerLockElement&&document.exitPointerLock(),console.log("[CharacterController] Disabled"))}setupInputHandlers(){const e=this.app.graphicsDevice.canvas;this.keydownHandler=e=>{this.keys[e.code]=!0,"Space"===e.code&&this.isGrounded&&(this.velocity.y=this.jumpVelocity,this.isGrounded=!1)},this.keyupHandler=e=>{this.keys[e.code]=!1},this.mousemoveHandler=e=>{this.mouseLocked&&(this.yaw-=e.movementX*this.lookSensitivity*100,this.pitch-=e.movementY*this.lookSensitivity*100,this.pitch=Math.max(-89,Math.min(89,this.pitch)))},this.clickHandler=()=>{this.mouseLocked||e.requestPointerLock()},this.pointerlockchangeHandler=()=>{this.mouseLocked=document.pointerLockElement===e},document.addEventListener("keydown",this.keydownHandler),document.addEventListener("keyup",this.keyupHandler),document.addEventListener("mousemove",this.mousemoveHandler),e.addEventListener("click",this.clickHandler),document.addEventListener("pointerlockchange",this.pointerlockchangeHandler)}removeInputHandlers(){const e=this.app.graphicsDevice.canvas;this.keydownHandler&&document.removeEventListener("keydown",this.keydownHandler),this.keyupHandler&&document.removeEventListener("keyup",this.keyupHandler),this.mousemoveHandler&&document.removeEventListener("mousemove",this.mousemoveHandler),this.clickHandler&&e.removeEventListener("click",this.clickHandler),this.pointerlockchangeHandler&&document.removeEventListener("pointerlockchange",this.pointerlockchangeHandler),this.keys={}}checkCollision(e,t){for(const o of this.collisionEntities){const n=o.getPosition(),i=o.getLocalScale(),a=o._collisionMeshType;if("floor"===a||"plane"===a)continue;let s,r,l;const c=o._collisionBounds;c?(s=c.halfExtents.x*i.x,r=c.halfExtents.y*i.y,l=c.halfExtents.z*i.z):(s=i.x/2,r=i.y/2,l=i.z/2);const d=Math.abs(e.x-n.x),p=Math.abs(e.y-n.y),h=Math.abs(e.z-n.z);if(d<s+t&&p<r+this.playerHeight/2&&h<l+t)return!0}return!1}checkGround(e){if(this.floorEntity){const t=this.floorEntity.getPosition(),o=this.floorEntity.getLocalScale(),n=o.x/2*100,i=o.z/2*100;if(Math.abs(e.x-t.x)<n&&Math.abs(e.z-t.z)<i)return t.y}let t=null;for(const o of this.collisionEntities){const n=o.getPosition(),i=o.getLocalScale(),a=o._collisionMeshType;if("floor"===a||"plane"===a||"sphere"===a)continue;let s,r,l;const c=o._collisionBounds;c?(s=c.halfExtents.x*i.x,r=c.halfExtents.y*i.y,l=c.halfExtents.z*i.z):(s=i.x/2,r=i.y/2,l=i.z/2);const d=n.y+r;Math.abs(e.x-n.x)<s+this.collisionRadius&&Math.abs(e.z-n.z)<l+this.collisionRadius&&e.y>=d-this.stepHeight&&(null===t||d>t)&&(t=d)}return t}update(t){if(!this.enabled)return;const o=(this.keys.KeyD||this.keys.ArrowRight?1:0)-(this.keys.KeyA||this.keys.ArrowLeft?1:0),n=(this.keys.KeyW||this.keys.ArrowUp?1:0)-(this.keys.KeyS||this.keys.ArrowDown?1:0),i=this.keys.ShiftLeft||this.keys.ShiftRight,a=this.yaw*(Math.PI/180);this.forward.set(-Math.sin(a),0,-Math.cos(a)),this.right.set(Math.cos(a),0,-Math.sin(a));const s=this.moveSpeed*(i?this.sprintMultiplier:1);this.targetVelocity.set(0,0,0),this.targetVelocity.add(this.tmpVec2.copy(this.forward).mulScalar(n*s)),this.targetVelocity.add(this.tmpVec2.copy(this.right).mulScalar(o*s));const r=((e,t)=>1-Math.pow(e,1e3*t))(this.moveDamping,t);this.horizontalVelocity.lerp(this.horizontalVelocity,this.targetVelocity,r),this.isGrounded||(this.velocity.y-=this.gravity*t,this.velocity.y=Math.max(-this.maxFallSpeed,this.velocity.y));const l=this.camera.getPosition().clone();l.y,this.playerHeight;const c=new e.Vec3;c.x=l.x+this.horizontalVelocity.x*t,c.y=l.y+this.velocity.y*t,c.z=l.z+this.horizontalVelocity.z*t,this.tmpVec2.set(c.x,l.y,l.z),this.checkCollision(this.tmpVec2,this.collisionRadius)&&(c.x=l.x),this.tmpVec2.set(c.x,l.y,c.z),this.checkCollision(this.tmpVec2,this.collisionRadius)&&(c.z=l.z);const d=this.checkGround(c),p=c.y-this.playerHeight;null!==d&&p<=d+this.groundCheckDistance?(c.y=d+this.playerHeight,this.velocity.y=0,this.isGrounded=!0):(null===d||p>d+this.stepHeight)&&(this.isGrounded=!1),this.camera.setPosition(c.x,c.y,c.z),this.camera.setEulerAngles(this.pitch,this.yaw,0)}destroy(){this.disable();for(const e of this.collisionEntities)e.destroy();this.collisionEntities=[],this.floorEntity=null}get grounded(){return this.isGrounded}getVelocity(){return this.velocity.clone()}setPosition(e,t,o){this.camera.setPosition(e,t,o)}setRotation(e,t){this.pitch=e,this.yaw=t,this.camera.setEulerAngles(e,t,0)}}let T=null;function k(){if(console.log("[RevealEffect] getGsplatRevealRadialClass called, cached:",!!T),T)return T;console.log("[RevealEffect] Creating new script class via pc.createScript");const t=e.createScript("gsplatRevealRadial");return console.log("[RevealEffect] Script class created:",t),Object.assign(t.prototype,{effectTime:0,_materialsApplied:null,_shadersApplied:!1,_retryCount:0,_maxRetries:100,_materialCreatedHandler:null,_systemMaterialHandler:null,_centerArray:[0,0,0],_dotTintArray:[0,0,0],_waveTintArray:[0,0,0],center:null,speed:5,acceleration:0,delay:2,dotTint:null,waveTint:null,oscillationIntensity:.2,endRadius:25,initialize(){console.log("[RevealEffect] initialize() called"),this.effectTime=0,this._materialsApplied=new Set,this._shadersApplied=!1,this._retryCount=0,this._centerArray=[0,0,0],this._dotTintArray=[0,0,0],this._waveTintArray=[0,0,0],this.center||(this.center=new e.Vec3(0,0,0)),this.dotTint||(this.dotTint=new e.Color(0,1,1)),this.waveTint||(this.waveTint=new e.Color(1,.5,0)),this.on("enable",()=>{console.log("[RevealEffect] enabled event fired"),this.effectTime=0,this._applyShaders()}),this.on("disable",()=>{console.log("[RevealEffect] disabled event fired"),this._removeShaders()}),this.enabled?(console.log("[RevealEffect] Starting enabled, applying shaders"),this._applyShaders()):console.log("[RevealEffect] Starting disabled, waiting for enable")},update(e){if(!this._shadersApplied&&this._retryCount<this._maxRetries&&(this._retryCount++,this._retryCount%20==0&&console.log(`[RevealEffect] Retry ${this._retryCount}/${this._maxRetries} to apply shaders`),this._applyShaders()),this.effectTime+=e,Math.floor(this.effectTime)!==Math.floor(this.effectTime-e)&&console.log(`[RevealEffect] effectTime: ${this.effectTime.toFixed(2)}s, shadersApplied: ${this._shadersApplied}, materialsCount: ${this._materialsApplied?.size||0}`),this._isEffectComplete())return console.log("[RevealEffect] Effect complete, disabling"),void(this.enabled=!1);this._updateUniforms()},_updateUniforms(){this._setUniform("uTime",this.effectTime),this._centerArray[0]=this.center.x,this._centerArray[1]=this.center.y,this._centerArray[2]=this.center.z,this._setUniform("uCenter",this._centerArray),this._setUniform("uSpeed",this.speed),this._setUniform("uAcceleration",this.acceleration),this._setUniform("uDelay",this.delay),this._dotTintArray[0]=this.dotTint.r,this._dotTintArray[1]=this.dotTint.g,this._dotTintArray[2]=this.dotTint.b,this._setUniform("uDotTint",this._dotTintArray),this._waveTintArray[0]=this.waveTint.r,this._waveTintArray[1]=this.waveTint.g,this._waveTintArray[2]=this.waveTint.b,this._setUniform("uWaveTint",this._waveTintArray),this._setUniform("uOscillationIntensity",this.oscillationIntensity),this._setUniform("uEndRadius",this.endRadius)},_getCompletionTime(){const e=this.delay;if(0===this.acceleration)return e+this.endRadius/this.speed;const t=this.speed*this.speed+2*this.acceleration*this.endRadius;if(t<0)return 1/0;return e+(-this.speed+Math.sqrt(t))/this.acceleration},_isEffectComplete(){return this.effectTime>=this._getCompletionTime()},getShaderGLSL:()=>"\nuniform float uTime;\nuniform vec3 uCenter;\nuniform float uSpeed;\nuniform float uAcceleration;\nuniform float uDelay;\nuniform vec3 uDotTint;\nuniform vec3 uWaveTint;\nuniform float uOscillationIntensity;\nuniform float uEndRadius;\n\n// Shared globals (initialized once per vertex)\nfloat g_dist;\nfloat g_dotWavePos;\nfloat g_liftTime;\nfloat g_liftWavePos;\n\nvoid initShared(vec3 center) {\n g_dist = length(center - uCenter);\n g_dotWavePos = uSpeed * uTime + 0.5 * uAcceleration * uTime * uTime;\n g_liftTime = max(0.0, uTime - uDelay);\n g_liftWavePos = uSpeed * g_liftTime + 0.5 * uAcceleration * g_liftTime * g_liftTime;\n}\n\n// Hash function for per-splat randomization\nfloat hash(vec3 p) {\n return fract(sin(dot(p, vec3(127.1, 311.7, 74.7))) * 43758.5453);\n}\n\nvoid modifyCenter(inout vec3 center) {\n initShared(center);\n\n // Early exit optimization\n if (g_dist > uEndRadius) return;\n\n // Only apply oscillation if lift wave hasn't fully passed\n bool wavesActive = g_liftTime <= 0.0 || g_dist > g_liftWavePos - 1.5;\n if (wavesActive) {\n // Apply oscillation with per-splat phase offset\n float phase = hash(center) * 6.28318;\n center.y += sin(uTime * 3.0 + phase) * uOscillationIntensity * 0.25;\n }\n\n // Apply lift effect near the wave edge\n float distToLiftWave = abs(g_dist - g_liftWavePos);\n if (distToLiftWave < 1.0 && g_liftTime > 0.0) {\n // Create a smooth lift curve (peaks at wave edge)\n // Lift is 0.9x the oscillation intensity (30% of original 3x)\n float liftAmount = (1.0 - distToLiftWave) * sin(distToLiftWave * 3.14159);\n center.y += liftAmount * uOscillationIntensity * 0.9;\n }\n}\n\nvoid modifyCovariance(vec3 originalCenter, vec3 modifiedCenter, inout vec3 covA, inout vec3 covB) {\n // Early exit for distant splats - hide them\n if (g_dist > uEndRadius) {\n gsplatMakeRound(covA, covB, 0.0);\n return;\n }\n\n // Determine scale and phase\n float scale;\n bool isLiftWave = g_liftTime > 0.0 && g_liftWavePos > g_dist;\n\n if (isLiftWave) {\n // Lift wave: transition from dots to full size\n scale = (g_liftWavePos >= g_dist + 2.0) ? 1.0 : mix(0.1, 1.0, (g_liftWavePos - g_dist) * 0.5);\n } else if (g_dist > g_dotWavePos + 1.0) {\n // Before dot wave: invisible\n gsplatMakeRound(covA, covB, 0.0);\n return;\n } else if (g_dist > g_dotWavePos - 1.0) {\n // Dot wave front: scale from 0 to 0.1 with 2x peak at center\n float distToWave = abs(g_dist - g_dotWavePos);\n scale = (distToWave < 0.5)\n ? mix(0.1, 0.2, 1.0 - distToWave * 2.0)\n : mix(0.0, 0.1, smoothstep(g_dotWavePos + 1.0, g_dotWavePos - 1.0, g_dist));\n } else {\n // After dot wave, before lift: small dots\n scale = 0.1;\n }\n\n // Apply scale to covariance\n if (scale >= 1.0) {\n // Fully revealed: original shape and size (no-op)\n return;\n } else if (isLiftWave) {\n // Lift wave: lerp from round dots to original shape\n float t = (scale - 0.1) * 1.111111; // normalize [0.1, 1.0] to [0, 1]\n float dotSize = scale * 0.05;\n float originalSize = gsplatExtractSize(covA, covB);\n float finalSize = mix(dotSize, originalSize, t);\n\n // Lerp between round and scaled original\n vec3 origCovA = covA * (scale * scale);\n vec3 origCovB = covB * (scale * scale);\n gsplatMakeRound(covA, covB, finalSize);\n covA = mix(covA, origCovA, t);\n covB = mix(covB, origCovB, t);\n } else {\n // Dot phase: round with absolute size, but don't make small splats larger\n float originalSize = gsplatExtractSize(covA, covB);\n gsplatMakeRound(covA, covB, min(scale * 0.05, originalSize));\n }\n}\n\nvoid modifyColor(vec3 center, inout vec4 color) {\n // Use shared globals\n if (g_dist > uEndRadius) return;\n\n // Lift wave tint takes priority (active during lift)\n if (g_liftTime > 0.0 && g_dist >= g_liftWavePos - 1.5 && g_dist <= g_liftWavePos + 0.5) {\n float distToLift = abs(g_dist - g_liftWavePos);\n float liftIntensity = smoothstep(1.5, 0.0, distToLift);\n color.rgb += uWaveTint * liftIntensity;\n }\n // Dot wave tint (active in dot phase, but not where lift wave is active)\n else if (g_dist <= g_dotWavePos && (g_liftTime <= 0.0 || g_dist > g_liftWavePos + 0.5)) {\n float distToDot = abs(g_dist - g_dotWavePos);\n float dotIntensity = smoothstep(1.0, 0.0, distToDot);\n color.rgb += uDotTint * dotIntensity;\n }\n}\n",getShaderWGSL:()=>"\nuniform uTime: f32;\nuniform uCenter: vec3f;\nuniform uSpeed: f32;\nuniform uAcceleration: f32;\nuniform uDelay: f32;\nuniform uDotTint: vec3f;\nuniform uWaveTint: vec3f;\nuniform uOscillationIntensity: f32;\nuniform uEndRadius: f32;\n\n// Shared globals (initialized once per vertex)\nvar<private> g_dist: f32;\nvar<private> g_dotWavePos: f32;\nvar<private> g_liftTime: f32;\nvar<private> g_liftWavePos: f32;\n\nfn initShared(center: vec3f) {\n g_dist = length(center - uniform.uCenter);\n g_dotWavePos = uniform.uSpeed * uniform.uTime + 0.5 * uniform.uAcceleration * uniform.uTime * uniform.uTime;\n g_liftTime = max(0.0, uniform.uTime - uniform.uDelay);\n g_liftWavePos = uniform.uSpeed * g_liftTime + 0.5 * uniform.uAcceleration * g_liftTime * g_liftTime;\n}\n\n// Hash function for per-splat randomization\nfn hash(p: vec3f) -> f32 {\n return fract(sin(dot(p, vec3f(127.1, 311.7, 74.7))) * 43758.5453);\n}\n\nfn modifyCenter(center: ptr<function, vec3f>) {\n initShared(*center);\n\n // Early exit optimization\n if (g_dist > uniform.uEndRadius) {\n return;\n }\n\n // Only apply oscillation if lift wave hasn't fully passed\n let wavesActive = g_liftTime <= 0.0 || g_dist > g_liftWavePos - 1.5;\n if (wavesActive) {\n // Apply oscillation with per-splat phase offset\n let phase = hash(*center) * 6.28318;\n (*center).y += sin(uniform.uTime * 3.0 + phase) * uniform.uOscillationIntensity * 0.25;\n }\n\n // Apply lift effect near the wave edge\n let distToLiftWave = abs(g_dist - g_liftWavePos);\n if (distToLiftWave < 1.0 && g_liftTime > 0.0) {\n // Create a smooth lift curve (peaks at wave edge)\n // Lift is 0.9x the oscillation intensity (30% of original 3x)\n let liftAmount = (1.0 - distToLiftWave) * sin(distToLiftWave * 3.14159);\n (*center).y += liftAmount * uniform.uOscillationIntensity * 0.9;\n }\n}\n\nfn modifyCovariance(originalCenter: vec3f, modifiedCenter: vec3f, covA: ptr<function, vec3f>, covB: ptr<function, vec3f>) {\n // Early exit for distant splats - hide them\n if (g_dist > uniform.uEndRadius) {\n gsplatMakeRound(covA, covB, 0.0);\n return;\n }\n\n // Determine scale and phase\n var scale: f32;\n let isLiftWave = g_liftTime > 0.0 && g_liftWavePos > g_dist;\n\n if (isLiftWave) {\n // Lift wave: transition from dots to full size\n scale = select(mix(0.1, 1.0, (g_liftWavePos - g_dist) * 0.5), 1.0, g_liftWavePos >= g_dist + 2.0);\n } else if (g_dist > g_dotWavePos + 1.0) {\n // Before dot wave: invisible\n gsplatMakeRound(covA, covB, 0.0);\n return;\n } else if (g_dist > g_dotWavePos - 1.0) {\n // Dot wave front: scale from 0 to 0.1 with 2x peak at center\n let distToWave = abs(g_dist - g_dotWavePos);\n scale = select(\n mix(0.0, 0.1, smoothstep(g_dotWavePos + 1.0, g_dotWavePos - 1.0, g_dist)),\n mix(0.1, 0.2, 1.0 - distToWave * 2.0),\n distToWave < 0.5\n );\n } else {\n // After dot wave, before lift: small dots\n scale = 0.1;\n }\n\n // Apply scale to covariance\n if (scale >= 1.0) {\n // Fully revealed: original shape and size (no-op)\n return;\n } else if (isLiftWave) {\n // Lift wave: lerp from round dots to original shape\n let t = (scale - 0.1) * 1.111111; // normalize [0.1, 1.0] to [0, 1]\n let dotSize = scale * 0.05;\n let originalSize = gsplatExtractSize(*covA, *covB);\n let finalSize = mix(dotSize, originalSize, t);\n\n // Lerp between round and scaled original\n let origCovA = *covA * (scale * scale);\n let origCovB = *covB * (scale * scale);\n gsplatMakeRound(covA, covB, finalSize);\n *covA = mix(*covA, origCovA, t);\n *covB = mix(*covB, origCovB, t);\n } else {\n // Dot phase: round with absolute size, but don't make small splats larger\n let originalSize = gsplatExtractSize(*covA, *covB);\n gsplatMakeRound(covA, covB, min(scale * 0.05, originalSize));\n }\n}\n\nfn modifyColor(center: vec3f, color: ptr<function, vec4f>) {\n // Use shared globals\n if (g_dist > uniform.uEndRadius) {\n return;\n }\n\n // Lift wave tint takes priority (active during lift)\n if (g_liftTime > 0.0 && g_dist >= g_liftWavePos - 1.5 && g_dist <= g_liftWavePos + 0.5) {\n let distToLift = abs(g_dist - g_liftWavePos);\n let liftIntensity = smoothstep(1.5, 0.0, distToLift);\n (*color) = vec4f((*color).rgb + uniform.uWaveTint * liftIntensity, (*color).a);\n }\n // Dot wave tint (active in dot phase, but not where lift wave is active)\n else if (g_dist <= g_dotWavePos && (g_liftTime <= 0.0 || g_dist > g_liftWavePos + 0.5)) {\n let distToDot = abs(g_dist - g_dotWavePos);\n let dotIntensity = smoothstep(1.0, 0.0, distToDot);\n (*color) = vec4f((*color).rgb + uniform.uDotTint * dotIntensity, (*color).a);\n }\n}\n",_applyShaders(){const e=this.entity.gsplat;if(!e)return void console.log("[RevealEffect] _applyShaders: No gsplat component found on entity");const t=!0===e.unified,o=this.app;if(t){console.log("[RevealEffect] Unified mode detected, using GSplatComponentSystem");const t=o?.systems?.gsplat;if(t){this._systemMaterialHandler||(this._systemMaterialHandler=(e,t,o)=>{console.log("[RevealEffect] material:created event from GSplatComponentSystem"),this._applyShaderToMaterial(e),this._shadersApplied=!0},t.on("material:created",this._systemMaterialHandler),console.log("[RevealEffect] Subscribed to GSplatComponentSystem material:created event"));const n=o.root?.findComponents("camera")||[],i=e.layers||[0];for(const e of n)for(const n of i){const i=o.scene?.layers?.getLayerById(n);if(i){const o=t.getGSplatMaterial?.(e.camera,i);o&&(console.log("[RevealEffect] Found unified material via getGSplatMaterial:",o),this._applyShaderToMaterial(o),this._shadersApplied=!0)}}}return}const n=e.instance||e._instance;if(n)return console.log("[RevealEffect] Instance found via component:",n),void this._applyToInstance(n);if(o&&o.scene){const e=o.scene.layers?.layerList||[];for(const t of e)if(t.meshInstances)for(const e of t.meshInstances){if(e.gsplatInstance||e._gsplatInstance){const t=e.gsplatInstance||e._gsplatInstance;return console.log("[RevealEffect] Found gsplat instance via mesh instance:",t),void this._applyToInstance(t)}const t=e.material;if(t&&t.gsplat)return console.log("[RevealEffect] Found gsplat material via mesh instance:",t),this._applyShaderToMaterial(t),void(this._shadersApplied=!0)}const t=e=>{if(e.gsplat){const t=e.gsplat.instance||e.gsplat._instance;if(t)return console.log("[RevealEffect] Found gsplat instance via entity search:",t),this._applyToInstance(t),!0}for(const o of e.children||[])if(t(o))return!0;return!1};o.root&&t(o.root)}this._retryCount%50==0&&(console.log("[RevealEffect] Still searching for gsplat materials..."),console.log("[RevealEffect] gsplatComponent.unified:",e.unified),console.log("[RevealEffect] gsplatComponent.asset:",e.asset))},_applyToInstance(e){if(this._shadersApplied)return;console.log("[RevealEffect] Applying shaders to instance"),console.log("[RevealEffect] Instance type:",e.constructor?.name),console.log("[RevealEffect] Instance keys:",Object.keys(e));const t=e.materials||e._materials;if(console.log("[RevealEffect] Instance materials:",t),t&&(t instanceof Map||t.forEach&&void 0!==t.size?(console.log("[RevealEffect] Materials is a Map/Set with size:",t.size),t.size>0&&(t.forEach(e=>{this._applyShaderToMaterial(e)}),this._shadersApplied=!0,console.log("[RevealEffect] SUCCESS: Shaders applied to",t.size,"materials"))):Array.isArray(t)&&(console.log("[RevealEffect] Materials is array with length:",t.length),t.forEach(e=>{this._applyShaderToMaterial(e)}),this._shadersApplied=!0)),!this._shadersApplied){const t=e.material||e._material;t&&(console.log("[RevealEffect] Found single material on instance"),this._applyShaderToMaterial(t),this._shadersApplied=!0)}e.on&&!this._materialCreatedHandler&&(this._materialCreatedHandler=e=>{console.log("[RevealEffect] material:created event received"),this._applyShaderToMaterial(e),this._shadersApplied=!0},e.on("material:created",this._materialCreatedHandler),console.log("[RevealEffect] Subscribed to material:created event"))},_applyShaderToMaterial(e){if(this._materialsApplied.has(e))return void console.log("[RevealEffect] Material already has shader applied, skipping");console.log("[RevealEffect] Applying shader to material:",e),console.log("[RevealEffect] Material constructor:",e.constructor?.name),console.log("[RevealEffect] Material keys:",Object.keys(e));const t=[];let o=e;for(;o&&o!==Object.prototype;){const e=Object.getOwnPropertyNames(o);for(const n of e)try{"function"!=typeof o[n]||t.includes(n)||t.push(n)}catch(e){}o=Object.getPrototypeOf(o)}console.log("[RevealEffect] Material methods:",t.filter(e=>!e.startsWith("_")).join(", "));const n=this.getShaderGLSL(),i=this.getShaderWGSL(),a="function"==typeof e.setShaderChunk;console.log("[RevealEffect] material.setShaderChunk exists:",a),a?(n&&(e.setShaderChunk("gsplatEffectGLSL",n),console.log("[RevealEffect] GLSL shader chunk set via setShaderChunk")),i&&(e.setShaderChunk("gsplatEffectWGSL",i),console.log("[RevealEffect] WGSL shader chunk set via setShaderChunk"))):(e.chunks&&(console.log("[RevealEffect] Material has chunks property"),e.chunks.gsplatEffectGLSL=n,e.chunks.gsplatEffectWGSL=i,console.log("[RevealEffect] Set chunks directly")),e.options&&console.log("[RevealEffect] Material has options:",e.options),e.shader&&console.log("[RevealEffect] Material has shader:",e.shader),"function"==typeof e.setParameter&&console.log("[RevealEffect] material.setParameter exists - will use for uniforms")),e.update?.(),this._materialsApplied.add(e),console.log("[RevealEffect] Material added to applied set, total:",this._materialsApplied.size)},_removeShaders(){if(this._materialsApplied&&(this._materialsApplied.forEach(e=>{e.setShaderChunk?.("gsplatEffectGLSL",""),e.setShaderChunk?.("gsplatEffectWGSL",""),e.update?.()}),this._materialsApplied.clear()),this._shadersApplied=!1,this._materialCreatedHandler){const e=this.entity.gsplat,t=e?.instance;t?.off&&t.off("material:created",this._materialCreatedHandler),this._materialCreatedHandler=null}if(this._systemMaterialHandler){const e=this.app?.systems?.gsplat;e?.off&&e.off("material:created",this._systemMaterialHandler),this._systemMaterialHandler=null}},_setUniform(e,t){this._materialsApplied&&this._materialsApplied.forEach(o=>{o.setParameter?.(e,t)})},destroy(){this._removeShaders()}}),T=t,t}const A={fast:{speed:10,acceleration:2,delay:.5,oscillationIntensity:.1,dotTint:{r:0,g:1,b:1},waveTint:{r:1,g:.5,b:0},endRadius:50},medium:{speed:5,acceleration:0,delay:2,oscillationIntensity:.2,dotTint:{r:0,g:1,b:1},waveTint:{r:1,g:.5,b:0},endRadius:50},slow:{speed:3,acceleration:0,delay:3,oscillationIntensity:.25,dotTint:{r:0,g:1,b:1},waveTint:{r:1,g:.5,b:0},endRadius:50}};function P(e){if("none"!==e)return A[e]}var z,R={},I={},D={};function F(){if(z)return D;z=1,Object.defineProperty(D,"__esModule",{value:!0}),D.loop=D.conditional=D.parse=void 0;D.parse=function e(t,o){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:n;if(Array.isArray(o))o.forEach(function(o){return e(t,o,n,i)});else if("function"==typeof o)o(t,n,i,e);else{var a=Object.keys(o)[0];Array.isArray(o[a])?(i[a]={},e(t,o[a],n,i[a])):i[a]=o[a](t,n,i,e)}return n};D.conditional=function(e,t){return function(o,n,i,a){t(o,n,i)&&a(o,e,n,i)}};return D.loop=function(e,t){return function(o,n,i,a){for(var s=[],r=o.pos;t(o,n,i);){var l={};if(a(o,e,n,l),o.pos===r)break;r=o.pos,s.push(l)}return s}},D}var U,B,V={};function $(){if(U)return V;U=1,Object.defineProperty(V,"__esModule",{value:!0}),V.readBits=V.readArray=V.readUnsigned=V.readString=V.peekBytes=V.readBytes=V.peekByte=V.readByte=V.buildStream=void 0;V.buildStream=function(e){return{data:e,pos:0}};var e=function(){return function(e){return e.data[e.pos++]}};V.readByte=e;V.peekByte=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return function(t){return t.data[t.pos+e]}};var t=function(e){return function(t){return t.data.subarray(t.pos,t.pos+=e)}};V.readBytes=t;V.peekBytes=function(e){return function(t){return t.data.subarray(t.pos,t.pos+e)}};V.readString=function(e){return function(o){return Array.from(t(e)(o)).map(function(e){return String.fromCharCode(e)}).join("")}};V.readUnsigned=function(e){return function(o){var n=t(2)(o);return e?(n[1]<<8)+n[0]:(n[0]<<8)+n[1]}};V.readArray=function(e,o){return function(n,i,a){for(var s="function"==typeof o?o(n,i,a):o,r=t(e),l=new Array(s),c=0;c<s;c++)l[c]=r(n);return l}};return V.readBits=function(e){return function(t){for(var o=function(e){return e.data[e.pos++]}(t),n=new Array(8),i=0;i<8;i++)n[7-i]=!!(o&1<<i);return Object.keys(e).reduce(function(t,o){var i=e[o];return i.length?t[o]=function(e,t,o){for(var n=0,i=0;i<o;i++)n+=e[t+i]&&Math.pow(2,o-i-1);return n}(n,i.index,i.length):t[o]=n[i.index],t},{})}},V}var H,O={};var W,G,N={};var j=function(){if(G)return R;G=1,Object.defineProperty(R,"__esModule",{value:!0}),R.decompressFrames=R.decompressFrame=R.parseGIF=void 0;var e,t=(B||(B=1,function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.default=void 0;var t=F(),o=$(),n={blocks:function(e){for(var t=[],n=e.data.length,i=0,a=(0,o.readByte)()(e);0!==a&&a;a=(0,o.readByte)()(e)){if(e.pos+a>=n){var s=n-e.pos;t.push((0,o.readBytes)(s)(e)),i+=s;break}t.push((0,o.readBytes)(a)(e)),i+=a}for(var r=new Uint8Array(i),l=0,c=0;c<t.length;c++)r.set(t[c],l),l+=t[c].length;return r}},i=(0,t.conditional)({gce:[{codes:(0,o.readBytes)(2)},{byteSize:(0,o.readByte)()},{extras:(0,o.readBits)({future:{index:0,length:3},disposal:{index:3,length:3},userInput:{index:6},transparentColorGiven:{index:7}})},{delay:(0,o.readUnsigned)(!0)},{transparentColorIndex:(0,o.readByte)()},{terminator:(0,o.readByte)()}]},function(e){var t=(0,o.peekBytes)(2)(e);return 33===t[0]&&249===t[1]}),a=(0,t.conditional)({image:[{code:(0,o.readByte)()},{descriptor:[{left:(0,o.readUnsigned)(!0)},{top:(0,o.readUnsigned)(!0)},{width:(0,o.readUnsigned)(!0)},{height:(0,o.readUnsigned)(!0)},{lct:(0,o.readBits)({exists:{index:0},interlaced:{index:1},sort:{index:2},future:{index:3,length:2},size:{index:5,length:3}})}]},(0,t.conditional)({lct:(0,o.readArray)(3,function(e,t,o){return Math.pow(2,o.descriptor.lct.size+1)})},function(e,t,o){return o.descriptor.lct.exists}),{data:[{minCodeSize:(0,o.readByte)()},n]}]},function(e){return 44===(0,o.peekByte)()(e)}),s=(0,t.conditional)({text:[{codes:(0,o.readBytes)(2)},{blockSize:(0,o.readByte)()},{preData:function(e,t,n){return(0,o.readBytes)(n.text.blockSize)(e)}},n]},function(e){var t=(0,o.peekBytes)(2)(e);return 33===t[0]&&1===t[1]}),r=(0,t.conditional)({application:[{codes:(0,o.readBytes)(2)},{blockSize:(0,o.readByte)()},{id:function(e,t,n){return(0,o.readString)(n.blockSize)(e)}},n]},function(e){var t=(0,o.peekBytes)(2)(e);return 33===t[0]&&255===t[1]}),l=(0,t.conditional)({comment:[{codes:(0,o.readBytes)(2)},n]},function(e){var t=(0,o.peekBytes)(2)(e);return 33===t[0]&&254===t[1]}),c=[{header:[{signature:(0,o.readString)(3)},{version:(0,o.readString)(3)}]},{lsd:[{width:(0,o.readUnsigned)(!0)},{height:(0,o.readUnsigned)(!0)},{gct:(0,o.readBits)({exists:{index:0},resolution:{index:1,length:3},sort:{index:4},size:{index:5,length:3}})},{backgroundColorIndex:(0,o.readByte)()},{pixelAspectRatio:(0,o.readByte)()}]},(0,t.conditional)({gct:(0,o.readArray)(3,function(e,t){return Math.pow(2,t.lsd.gct.size+1)})},function(e,t){return t.lsd.gct.exists}),{frames:(0,t.loop)([i,r,l,a,s],function(e){var t=(0,o.peekByte)()(e);return 33===t||44===t})}];e.default=c}(I)),(e=I)&&e.__esModule?e:{default:e}),o=F(),n=$(),i=(H||(H=1,Object.defineProperty(O,"__esModule",{value:!0}),O.deinterlace=void 0,O.deinterlace=function(e,t){for(var o=new Array(e.length),n=e.length/t,i=function(n,i){var a=e.slice(i*t,(i+1)*t);o.splice.apply(o,[n*t,t].concat(a))},a=[0,4,2,1],s=[8,8,4,2],r=0,l=0;l<4;l++)for(var c=a[l];c<n;c+=s[l])i(c,r),r++;return o}),O),a=(W||(W=1,Object.defineProperty(N,"__esModule",{value:!0}),N.lzw=void 0,N.lzw=function(e,t,o){var n,i,a,s,r,l,c,d,p,h,u,m,g,y,f,v,b=4096,w=o,x=new Array(o),S=new Array(b),E=new Array(b),M=new Array(4097);for(r=1+(i=1<<(h=e)),n=i+2,c=-1,a=(1<<(s=h+1))-1,d=0;d<i;d++)S[d]=0,E[d]=d;for(u=m=g=y=f=v=0,p=0;p<w;){if(0===y){if(m<s){u+=t[v]<<m,m+=8,v++;continue}if(d=u&a,u>>=s,m-=s,d>n||d==r)break;if(d==i){a=(1<<(s=h+1))-1,n=i+2,c=-1;continue}if(-1==c){M[y++]=E[d],c=d,g=d;continue}for(l=d,d==n&&(M[y++]=g,d=c);d>i;)M[y++]=E[d],d=S[d];g=255&E[d],M[y++]=g,n<b&&(S[n]=c,E[n]=g,0===(++n&a)&&n<b&&(s++,a+=n)),c=l}y--,x[f++]=M[y],p++}for(p=f;p<w;p++)x[p]=0;return x}),N);R.parseGIF=function(e){var i=new Uint8Array(e);return(0,o.parse)((0,n.buildStream)(i),t.default)};var s=function(e,t,o){if(e.image){var n=e.image,s=n.descriptor.width*n.descriptor.height,r=(0,a.lzw)(n.data.minCodeSize,n.data.blocks,s);n.descriptor.lct.interlaced&&(r=(0,i.deinterlace)(r,n.descriptor.width));var l={pixels:r,dims:{top:e.image.descriptor.top,left:e.image.descriptor.left,width:e.image.descriptor.width,height:e.image.descriptor.height}};return n.descriptor.lct&&n.descriptor.lct.exists?l.colorTable=n.lct:l.colorTable=t,e.gce&&(l.delay=10*(e.gce.delay||10),l.disposalType=e.gce.extras.disposal,e.gce.extras.transparentColorGiven&&(l.transparentIndex=e.gce.transparentColorIndex)),o&&(l.patch=function(e){for(var t=e.pixels.length,o=new Uint8ClampedArray(4*t),n=0;n<t;n++){var i=4*n,a=e.pixels[n],s=e.colorTable[a]||[0,0,0];o[i]=s[0],o[i+1]=s[1],o[i+2]=s[2],o[i+3]=a!==e.transparentIndex?255:0}return o}(l)),l}console.warn("gif frame does not have associated image.")};return R.decompressFrame=s,R.decompressFrames=function(e,t){return e.frames.filter(function(e){return e.image}).map(function(o){return s(o,e.gct,t)})},R}();class q{constructor(e,t,o={}){this.frames=[],this.currentFrameIndex=0,this.isPlaying=!1,this.isLoaded=!1,this.lastFrameTime=0,this.updateHandler=null,this.texture=null,this.gifWidth=0,this.gifHeight=0,this.update=()=>{if(!this.isPlaying||!this.isLoaded||this.frames.length<=1)return;const e=performance.now(),t=this.frames[this.currentFrameIndex].delay||100;e-this.lastFrameTime>=t&&(this.currentFrameIndex=(this.currentFrameIndex+1)%this.frames.length,this.drawFrame(this.currentFrameIndex),this.lastFrameTime=e)},this.app=e,this.url=t,this.options=o,this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d",{willReadFrequently:!0}),this.load()}async load(){try{const t=await fetch(this.url);if(!t.ok)throw new Error(`Failed to fetch GIF: ${t.statusText}`);const o=await t.arrayBuffer(),n=j.parseGIF(o);if(this.frames=j.decompressFrames(n,!0),0===this.frames.length)throw new Error("GIF has no frames");this.gifWidth=n.lsd.width,this.gifHeight=n.lsd.height,this.canvas.width=this.gifWidth,this.canvas.height=this.gifHeight,this.texture=new e.Texture(this.app.graphicsDevice,{width:this.gifWidth,height:this.gifHeight,format:e.PIXELFORMAT_RGBA8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE}),this.drawFrame(0),this.isLoaded=!0,console.log(`[AnimatedGif] Loaded GIF: ${this.url}, ${this.frames.length} frames, ${this.gifWidth}x${this.gifHeight}`),this.options.onReady&&this.options.onReady(),this.options.autoPlay&&this.play()}catch(e){console.error("[AnimatedGif] Error loading GIF:",e),this.options.onError&&this.options.onError(e instanceof Error?e:new Error(String(e)))}}drawFrame(e){if(!this.texture||e>=this.frames.length)return;const t=this.frames[e],o=e>0?this.frames[e-1]:null;o&&2===o.disposalType&&this.ctx.clearRect(o.dims.left,o.dims.top,o.dims.width,o.dims.height);const n=new ImageData(new Uint8ClampedArray(t.patch),t.dims.width,t.dims.height),i=document.createElement("canvas");i.width=t.dims.width,i.height=t.dims.height;i.getContext("2d").putImageData(n,0,0),this.ctx.drawImage(i,t.dims.left,t.dims.top),this.updateTexture()}updateTexture(){if(!this.texture)return;const e=this.ctx.getImageData(0,0,this.gifWidth,this.gifHeight),t=this.texture.lock();t&&t.set(e.data),this.texture.unlock(),this.texture.upload()}play(){this.isPlaying||(this.isPlaying=!0,this.lastFrameTime=performance.now(),this.updateHandler||(this.updateHandler=this.update,this.app.on("update",this.updateHandler)))}pause(){this.isPlaying&&(this.isPlaying=!1,this.updateHandler&&(this.app.off("update",this.updateHandler),this.updateHandler=null))}stop(){this.pause(),this.currentFrameIndex=0,this.isLoaded&&(this.ctx.clearRect(0,0,this.gifWidth,this.gifHeight),this.drawFrame(0))}get playing(){return this.isPlaying}get loaded(){return this.isLoaded}destroy(){this.pause(),this.texture&&(this.texture.destroy(),this.texture=null),this.frames=[],this.isLoaded=!1}}class Y{constructor(t){this.meshes=new Map,this.updateHandler=null,this.useTexElement2D=!1,this.app=t,this.canvas=t.graphicsDevice.canvas,function(){const t=e.GraphicsDevice;if(!t)return void console.warn("[HtmlMeshHelper] Could not find pc.GraphicsDevice to patch");if("function"==typeof t.prototype._isHTMLElementInterface)return;t.prototype._isHTMLElementInterface=function(e){return!(!("undefined"!=typeof HTMLElement&&e instanceof HTMLElement)||e instanceof HTMLImageElement||e instanceof HTMLCanvasElement||e instanceof HTMLVideoElement)};const o=t.prototype._isBrowserInterface;o&&(t.prototype._isBrowserInterface=function(e){return o.call(this,e)||this._isHTMLElementInterface(e)}),console.log("[HtmlMeshHelper] Patched PlayCanvas GraphicsDevice for HTML-in-Canvas support")}();const o=t.graphicsDevice;this.useTexElement2D=!0===o.supportsTexElement2D,console.log(`[HtmlMeshManager] texElement2D support: ${this.useTexElement2D}`)}createMesh(e){const t=e.width||512,o=e.height||512,n=this.createHtmlElement(e,t,o),i=this.createTexture(n,t,o,e),a=this.createMaterial(i,e),s={entity:this.createEntity(e,a,t,o),texture:i,material:a,htmlElement:n,config:e,destroy:()=>this.destroyMesh(e.id),update:()=>this.updateMeshTexture(e.id)};return this.meshes.set(e.id,s),e.animated&&!this.updateHandler&&this.startUpdateLoop(),s}createHtmlElement(e,t,o){const n=document.createElement("div");if(n.id=`html-mesh-${e.id}`,n.style.width=`${t}px`,n.style.height=`${o}px`,n.style.position="absolute",n.style.top="0",n.style.left="0",n.style.pointerEvents="none",n.style.zIndex="-1",n.style.overflow="hidden",e.css){const t=document.createElement("style");t.textContent=e.css,n.appendChild(t)}return n.innerHTML+=e.html,this.useTexElement2D?(this.canvas.setAttribute("layoutsubtree",""),this.canvas.setAttribute("data-layoutsubtree",""),this.canvas.appendChild(n)):(n.style.visibility="hidden",document.body.appendChild(n)),n}createTexture(t,o,n,i){const a=new e.Texture(this.app.graphicsDevice,{width:o,height:n,format:e.PIXELFORMAT_RGBA8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE,name:`htmlMesh-${i.id}`});if(this.useTexElement2D)try{a.setSource(t),console.log(`[HtmlMeshManager] Using texElement2D for mesh ${i.id}`)}catch(e){console.warn(`[HtmlMeshManager] texElement2D failed, falling back to canvas: ${e}`),this.renderToCanvas(a,t,o,n)}else this.renderToCanvas(a,t,o,n);return a}renderToCanvas(e,t,o,n){const i=document.createElement("canvas");i.width=o,i.height=n;const a=i.getContext("2d",{willReadFrequently:!0}),s=`\n <svg xmlns="http://www.w3.org/2000/svg" width="${o}" height="${n}">\n <foreignObject width="100%" height="100%">\n <div xmlns="http://www.w3.org/1999/xhtml" style="width:${o}px;height:${n}px;">\n ${t.innerHTML}\n </div>\n </foreignObject>\n </svg>\n `,r=new Image,l=new Blob([s],{type:"image/svg+xml;charset=utf-8"}),c=URL.createObjectURL(l);r.onload=()=>{a.drawImage(r,0,0),URL.revokeObjectURL(c),e.setSource(i)},r.onerror=()=>{a.fillStyle="#333",a.fillRect(0,0,o,n),a.fillStyle="#fff",a.font="20px Arial",a.textAlign="center",a.fillText("HTML Mesh",o/2,n/2),URL.revokeObjectURL(c),e.setSource(i)},r.src=c}createMaterial(t,o){const n=new e.StandardMaterial;return n.diffuseMap=t,n.emissiveMap=t,n.emissive=new e.Color(.5,.5,.5),n.opacity=o.opacity??1,n.blendType=void 0!==o.opacity&&o.opacity<1?e.BLEND_NORMAL:e.BLEND_NONE,n.cull=o.doubleSided?e.CULLFACE_NONE:e.CULLFACE_BACK,n.update(),n}createEntity(t,o,n,i){const a=new e.Entity(`htmlMesh-${t.id}`),s=n/i;a.addComponent("render",{type:"plane",material:o,castShadows:t.castShadows??!1,receiveShadows:t.receiveShadows??!1}),a.setPosition(t.position.x,t.position.y,t.position.z);const r=t.rotation||{x:0,y:0,z:0};a.setEulerAngles(r.x,r.y,r.z);const l=t.scale||{x:1,y:1};return a.setLocalScale(l.x*s,1,l.y),this.app.root.addChild(a),t.billboard&&this.app.on("update",()=>{if(!a.enabled)return;const e=this.app.root.findComponent("camera")?.entity;e&&a.lookAt(e.getPosition())}),a}updateMeshTexture(e){const t=this.meshes.get(e);t&&(this.useTexElement2D?t.texture.upload():this.renderToCanvas(t.texture,t.htmlElement,t.config.width||512,t.config.height||512))}startUpdateLoop(){let e={};this.updateHandler=()=>{const t=performance.now();this.meshes.forEach((o,n)=>{if(!o.config.animated)return;const i=o.config.updateRate||100,a=e[n]||0;t-a>=i&&(this.updateMeshTexture(n),e[n]=t)})},this.app.on("update",this.updateHandler)}destroyMesh(e){const t=this.meshes.get(e);if(!t)return;t.entity.destroy(),t.texture.destroy(),t.htmlElement.remove(),this.meshes.delete(e);!Array.from(this.meshes.values()).some(e=>e.config.animated)&&this.updateHandler&&(this.app.off("update",this.updateHandler),this.updateHandler=null)}getMesh(e){return this.meshes.get(e)}updateVisibility(e,t){this.meshes.forEach(o=>{const n=o.config;if(n.visibilityRange){const i=n.visibilityRange;let a=!0;"percentage"===i.type?a=e>=i.start&&e<=i.end:"waypoint"===i.type&&(a=t>=i.start&&t<=i.end),o.entity.enabled=a}if(n.billboard&&n.billboardRange){const i=n.billboardRange;let a=!1;"percentage"===i.type?a=e>=i.start&&e<=i.end:"waypoint"===i.type&&(a=t>=i.start&&t<=i.end),o.entity._billboardActive=a}else n.billboard&&(o.entity._billboardActive=!0)})}getAllMeshes(){return this.meshes}destroy(){this.meshes.forEach((e,t)=>this.destroyMesh(t)),this.updateHandler&&(this.app.off("update",this.updateHandler),this.updateHandler=null)}}class X{constructor(e){this.isInitialized=!1,this.scriptCleanup=[],this.lastError=null,this.updateCallbacks=[],this.customScript=e,this.api={}}initialize(e){this.api={...e,registerCleanup:e=>this.addCleanup(e)},this.isInitialized=!0}updateScript(e){e!==this.customScript&&(this.customScript=e,this.execute())}addCleanup(e){"function"==typeof e&&this.scriptCleanup.push(e)}sanitizeScript(e){if(!e)return"";let t="function"==typeof e.normalize?e.normalize("NFC"):e;return t=t.replace(/\uFEFF/g,""),t=t.replace(/\u00A0/g," "),t=t.replace(/[\u2028\u2029]/g,"\n"),t=t.replace(/[\u200B-\u200D\u2060]/g,""),t=t.replace(/[\u2018\u2019\u201B]/g,"'").replace(/[\u201C\u201D\u201E]/g,'"'),t}preprocessScript(e){if(!e)return"";let t=this.sanitizeScript(e).trim().replace(/\r\n/g,"\n");return/console\/log\s*\(/.test(t)&&(console.warn("[Custom Script] Detected 'console/log(...)'. Did you mean 'console.log(...)'?"),t=t.replace(/console\/log\s*\(/g,"console.log(")),t="try {\n"+t+"\n} catch (error) {\n console.error('[Custom Script] Runtime error:', error);\n}",t}execute(){if(!this.isInitialized||!this.customScript)return;if(this.customScript.length>2e5)return void console.warn("[Custom Script] Script too large, aborting execution.");this.cleanup();const e=this.preprocessScript(this.customScript);try{const t=this.api.app;let o=!1;const n=()=>{o||(this.updateCallbacks.length>200?(console.warn("[Custom Script] Too many update callbacks; further callbacks ignored."),o=!0):requestAnimationFrame(n))};requestAnimationFrame(n);const i=Object.create(t);i.registerUpdate=e=>{if("function"!=typeof e||o)return;const n=()=>{try{e()}catch(e){console.error("[Custom Script] Error in update callback:",e)}};this.updateCallbacks.push(n),t.on("update",n),this.addCleanup(()=>{t.off("update",n);const e=this.updateCallbacks.indexOf(n);-1!==e&&this.updateCallbacks.splice(e,1)})},i.registerBeforeRender=i.registerUpdate;const{camera:a,pc:s,canvas:r,getScrollPercentage:l,getCurrentWaypointIndex:c,getHotspots:d,getSplats:p,getHTMLMeshes:h}=this.api,u=new Function("app","camera","pc","canvas","getScrollPercentage","getCurrentWaypointIndex","getHotspots","getSplats","getHTMLMeshes","registerCleanup","registerUpdate","exports","module","require","globalThis","window","document","self","'use strict';\n"+e),m={},g={exports:m},y=()=>{throw new Error("require not available in custom script")},f=new Proxy({},{get:()=>{},set:()=>!1}),v=u(i,a,s,r,l,c,d,p,h,e=>this.addCleanup(e),i.registerUpdate.bind(i),m,g,y,f,void 0,void 0,void 0)||g.exports||m.default||m.cleanup;"function"==typeof v&&this.addCleanup(v),console.log("[Custom Script] Executed successfully")}catch(e){this.lastError=e,console.error("[Custom Script] Execution error:",e)}}cleanup(){this.scriptCleanup.forEach(e=>{try{e()}catch(e){console.error("[Custom Script] Cleanup error:",e)}}),this.scriptCleanup=[],this.updateCallbacks=[]}getLastError(){return this.lastError}dispose(){this.cleanup(),this.isInitialized=!1}}class Z{constructor(e,t,o={}){this.options=o,this.frameAssets=new Map,this.activeEntityIndex=0,this.currentFrame=0,this.isPlaying=!1,this.lastFrameTime=0,this.loadingFrames=new Set,this.destroyed=!1,this.listeners=new Map,this.app=e,this.frameUrls=t.frameUrls,this.fps=t.fps||24,this.loop=!1!==t.loop,this.preloadCount=t.preloadCount||5,this.frameInterval=1e3/this.fps,this.entities=[this.createSplatEntity("frameEntityA"),this.createSplatEntity("frameEntityB")],this.entities[0].enabled=!1,this.entities[1].enabled=!1,this.preloadInitialFrames(),t.autoplay&&this.preloadFrame(0).then(()=>{this.destroyed||this.play()})}createSplatEntity(t){const o=new e.Entity(t);return o.addComponent("gsplat",{}),this.app.root.addChild(o),o}async preloadInitialFrames(){const e=Math.min(this.preloadCount,this.frameUrls.length),t=[];for(let o=0;o<e;o++)t.push(this.preloadFrame(o));await Promise.all(t),this.options.onLoadProgress?.(e,this.frameUrls.length)}async preloadFrame(t){return this.destroyed||t<0||t>=this.frameUrls.length?null:this.frameAssets.has(t)?this.frameAssets.get(t):this.loadingFrames.has(t)?null:(this.loadingFrames.add(t),new Promise(o=>{const n=this.frameUrls[t],i=new e.Asset(`frame_${t}`,"gsplat",{url:n});i.on("load",()=>{this.destroyed||(this.frameAssets.set(t,i),this.loadingFrames.delete(t)),o(i)}),i.on("error",e=>{console.error(`Failed to load frame ${t}:`,e),this.loadingFrames.delete(t),this.options.onError?.(`Failed to load frame ${t}: ${e}`),o(null)}),this.app.assets.add(i),this.app.assets.load(i)}))}unloadFrame(e){const t=this.frameAssets.get(e);t&&(this.app.assets.remove(t),t.unload(),this.frameAssets.delete(e))}updatePreloadWindow(){for(let e=1;e<=this.preloadCount;e++){const t=this.loop?(this.currentFrame+e)%this.frameUrls.length:this.currentFrame+e;t<this.frameUrls.length&&this.preloadFrame(t)}for(const[e]of this.frameAssets){const t=this.currentFrame-e;t>2&&t<this.frameUrls.length-this.preloadCount&&this.unloadFrame(e)}}async displayFrame(e){if(this.destroyed)return;let t=this.frameAssets.get(e);if(!t&&(t=await this.preloadFrame(e),!t||this.destroyed))return;const o=(this.activeEntityIndex+1)%2,n=this.entities[this.activeEntityIndex],i=this.entities[o],a=i.gsplat;a&&(a.asset=t),i.enabled=!0,n.enabled=!1,this.activeEntityIndex=o,this.currentFrame=e,this.emit("frameChange",e,this.frameUrls.length),this.options.onFrameChange?.(e,this.frameUrls.length),this.updatePreloadWindow()}update(e){if(!this.isPlaying||this.destroyed)return;const t=performance.now(),o=t-this.lastFrameTime;if(o>=this.frameInterval){this.lastFrameTime=t-o%this.frameInterval;let e=this.currentFrame+1;if(e>=this.frameUrls.length){if(!this.loop)return this.pause(),void this.emit("complete");e=0}this.displayFrame(e)}}play(){this.isPlaying||this.destroyed||(this.isPlaying=!0,this.lastFrameTime=performance.now(),this.entities[this.activeEntityIndex].enabled||this.displayFrame(this.currentFrame),this.emit("play"))}pause(){this.isPlaying=!1,this.emit("pause")}stop(){this.isPlaying=!1,this.currentFrame=0,this.displayFrame(0),this.emit("stop")}setFrame(e){e<0&&(e=0),e>=this.frameUrls.length&&(e=this.frameUrls.length-1),this.displayFrame(e)}nextFrame(){let e=this.currentFrame+1;e>=this.frameUrls.length&&(e=this.loop?0:this.frameUrls.length-1),this.setFrame(e)}previousFrame(){let e=this.currentFrame-1;e<0&&(e=this.loop?this.frameUrls.length-1:0),this.setFrame(e)}getCurrentFrame(){return this.currentFrame}getTotalFrames(){return this.frameUrls.length}getProgress(){return this.frameUrls.length>1?this.currentFrame/(this.frameUrls.length-1):0}setProgress(e){const t=Math.round(e*(this.frameUrls.length-1));this.setFrame(t)}getFps(){return this.fps}setFps(e){this.fps=e,this.frameInterval=1e3/e}getIsPlaying(){return this.isPlaying}setLoop(e){this.loop=e}getLoop(){return this.loop}setPosition(e,t,o){this.entities[0].setPosition(e,t,o),this.entities[1].setPosition(e,t,o)}setRotation(e,t,o){this.entities[0].setEulerAngles(e,t,o),this.entities[1].setEulerAngles(e,t,o)}setScale(e,t,o){this.entities[0].setLocalScale(e,t,o),this.entities[1].setLocalScale(e,t,o)}on(e,t){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(t)}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,...t){this.listeners.get(e)?.forEach(e=>e(...t))}destroy(){this.destroyed=!0,this.isPlaying=!1;for(const[e]of this.frameAssets)this.unloadFrame(e);this.entities[0].destroy(),this.entities[1].destroy(),this.listeners.clear()}}class J{constructor(){this.listeners=new Map}on(e,t){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(t)}off(e,t){this.listeners.get(e)?.delete(t)}emit(e,...t){this.listeners.get(e)?.forEach(e=>e(...t))}}function K(){const e=navigator.userAgent||navigator.vendor||window.opera||"";return/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(e)}const Q={"desktop-max":{range:[0,5],lodDistances:[15,30,80,250,300]},desktop:{range:[0,2],lodDistances:[15,30,80,250,300]},"mobile-max":{range:[1,2],lodDistances:[15,30,80,250,300]},mobile:{range:[2,5],lodDistances:[15,30,80,250,300]}};function ee(e){const t=e.includes("lod-meta.json");return t&&console.log("[SPLAT] Detected LOD streaming format (lod-meta.json)"),t}function te(t,o,n={}){if(n.lazyLoad){const e=new J;let i=null;const a=n.lazyLoadThumbnail||o.thumbnailUrl,s=n.lazyLoadButtonText||o.uiOptions?.lazyLoadButtonText||o.uiOptions?.buttonLabels?.startExperience||"Start Experience",r=o.uiColor||"#4CAF50";console.log("[StorySplat Viewer] Lazy loading enabled, showing start button"),function(e,t){const{thumbnailUrl:o,thumbnailType:n,buttonText:i="Start Experience",uiColor:a="#4CAF50",onStart:s}=t;c(a),e.classList.add("storysplat-viewer-container");const r=document.createElement("div");r.className="storysplat-lazy-load-container";const l=n||(o?function(e){const t=e.toLowerCase();return t.includes(".mp4")||t.includes(".webm")||t.includes(".mov")||t.includes(".ogg")?"video":t.includes(".gif")?"gif":"image"}(o):"image");let d="";o&&(d+="video"===l?`<video class="storysplat-lazy-load-thumbnail-video" src="${o}" autoplay muted loop playsinline webkit-playsinline></video>`:`<img class="storysplat-lazy-load-thumbnail" src="${o}" alt="Scene preview" />`),d+='<div class="storysplat-lazy-load-overlay"></div>',d+=`\n <div class="storysplat-lazy-load-content">\n <button class="storysplat-lazy-load-start-btn" style="background: ${a}">\n <svg viewBox="0 0 24 24">\n <path d="M8 5v14l11-7z"/>\n </svg>\n ${i}\n </button>\n </div>\n `,r.innerHTML=d,e.appendChild(r);const p=r.querySelector(".storysplat-lazy-load-start-btn");p?.addEventListener("click",()=>{const e=r.querySelector("video");e&&(e.pause(),e.src=""),r.style.transition="opacity 0.3s ease-out",r.style.opacity="0",setTimeout(()=>{r.remove(),s()},300)})}(t,{thumbnailUrl:a,thumbnailType:n.lazyLoadThumbnailType,buttonText:s,uiColor:r,onStart:()=>{console.log("[StorySplat Viewer] User clicked start, initializing viewer..."),i=te(t,o,{...n,lazyLoad:!1}),i.on("ready",()=>e.emit("ready")),i.on("error",t=>e.emit("error",t)),i.on("waypointChange",t=>e.emit("waypointChange",t)),i.on("playbackStart",()=>e.emit("playbackStart")),i.on("playbackStop",()=>e.emit("playbackStop")),i.on("loaded",()=>e.emit("loaded")),i.on("progress",t=>e.emit("progress",t))}});return{app:null,canvas:null,goToWaypoint:e=>i?.goToWaypoint(e),nextWaypoint:()=>i?.nextWaypoint(),prevWaypoint:()=>i?.prevWaypoint(),getCurrentWaypointIndex:()=>i?.getCurrentWaypointIndex()??0,getWaypointCount:()=>i?.getWaypointCount()??0,setPosition:(e,t,o)=>i?.setPosition(e,t,o),setRotation:(e,t,o)=>i?.setRotation(e,t,o),getPosition:()=>i?.getPosition()??{x:0,y:0,z:0},getRotation:()=>i?.getRotation()??{x:0,y:0,z:0},play:()=>i?.play(),pause:()=>i?.pause(),stop:()=>i?.stop(),isPlaying:()=>i?.isPlaying()??!1,goToOriginalSplat:()=>i?.goToOriginalSplat(),getCurrentSplatUrl:()=>i?.getCurrentSplatUrl()??"",isShowingOriginalSplat:()=>i?.isShowingOriginalSplat()??!0,destroy:()=>{i?i.destroy():(t.querySelectorAll(".storysplat-lazy-load-container, .storysplat-viewer-container").forEach(e=>e.remove()),t.classList.remove("storysplat-viewer-container"))},resize:()=>i?.resize(),navigateToScene:async e=>{if(i)return i.navigateToScene(e)},on:(t,o)=>e.on(t,o),off:(t,o)=>e.off(t,o)}}const i=new J,r=s(a(o));console.log("[StorySplat Viewer] Creating viewer with config:",r),console.log("[StorySplat Viewer] Scale config:",r.scale),console.log("[StorySplat Viewer] Raw scene data:",{splatScale:o.splatScale,scale:o.scale});const w=!1!==n.showUI,x=r.uiColor||"#4CAF50",S=r.uiOptions||{},E=e=>"first-person"===e?"tour":"drone"===e?"explore":e,M=r.collisionMeshesData&&r.collisionMeshesData.length>0;let C=(r.allowedCameraModes||["orbit","first-person","drone"]).map(E).filter((e,t,o)=>o.indexOf(e)===t);M&&!C.includes("walk")&&C.push("walk"),!M&&C.includes("walk")&&(C=C.filter(e=>"walk"!==e));const T=E(r.defaultCameraMode||"orbit");let A={};w&&(A=function(e,t,o={}){const{uiColor:n="#4CAF50",showScrollControls:i=!0,showModeToggle:a=!0,showFullscreenButton:s=!0,showHelpButton:r=!1,showPreloader:p=!0,allowedCameraModes:h=["tour","explore"],defaultCameraMode:u="tour",customPreloaderLogoUrl:m,buttonLabels:g,hideWatermark:y=!1,watermarkText:f,watermarkLink:v,sceneId:b}=o,w={tour:l(g,"tour"),explore:l(g,"explore"),walk:l(g,"walk"),previous:l(g,"previous"),next:l(g,"next"),helpTitle:l(g,"helpTitle"),returnToTour:l(g,"returnToTour")},x={};e.querySelectorAll(".storysplat-preloader, .storysplat-scroll-controls, .storysplat-waypoint-info, .storysplat-fullscreen-btn, .storysplat-help-btn, .storysplat-help-panel, .storysplat-watermark").forEach(e=>e.remove()),c(n),e.classList.add("storysplat-viewer-container"),p&&(x.preloader=d(e,m));const S=document.createElement("div");if(S.className="storysplat-waypoint-info",S.innerHTML='\n <h2 class="storysplat-waypoint-title"></h2>\n <p class="storysplat-waypoint-description"></p>\n ',e.appendChild(S),x.waypointInfo=S,i&&t.waypoints&&t.waypoints.length>0){const t=document.createElement("div");t.className="storysplat-scroll-controls",t.innerHTML=`\n <div class="storysplat-scroll-content">\n <div class="storysplat-progress-text">0%</div>\n <div class="storysplat-progress-container">\n <div class="storysplat-progress-bar"></div>\n </div>\n <div class="storysplat-scroll-buttons">\n <button class="storysplat-btn storysplat-btn-prev">${w.previous}</button>\n <button class="storysplat-btn storysplat-btn-play">\n <svg viewBox="0 0 24 24"><path d="M8 5v14l11-7z"/></svg>\n </button>\n <button class="storysplat-btn storysplat-btn-next">${w.next}</button>\n </div>\n ${a&&h.length>1?`\n <div class="storysplat-mode-container">\n <div class="storysplat-mode-toggle">\n ${h.includes("tour")?`<button class="storysplat-mode-btn ${"tour"===u?"selected":""}" data-mode="tour">${w.tour}</button>`:""}\n ${h.includes("explore")?`<button class="storysplat-mode-btn ${"explore"===u?"selected":""}" data-mode="explore">${w.explore}</button>`:""}\n ${h.includes("walk")?`<button class="storysplat-mode-btn ${"walk"===u?"selected":""}" data-mode="walk">${w.walk}</button>`:""}\n </div>\n </div>\n `:""}\n </div>\n `,e.appendChild(t),x.scrollControls=t,x.progressBar=t.querySelector(".storysplat-progress-bar"),x.progressText=t.querySelector(".storysplat-progress-text")}if(s){const t=document.createElement("button");t.className="storysplat-fullscreen-btn",t.setAttribute("aria-label","Toggle Fullscreen"),t.innerHTML='\n <svg class="storysplat-expand-icon" viewBox="0 0 24 24">\n <path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/>\n </svg>\n <svg class="storysplat-compress-icon" viewBox="0 0 24 24" style="display: none;">\n <path d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z"/>\n </svg>\n ',e.appendChild(t),x.fullscreenButton=t}const E=document.createElement("button");E.className="storysplat-xr-btn storysplat-vr-btn",E.setAttribute("aria-label","Enter VR"),E.textContent="VR",e.appendChild(E),x.vrButton=E;const M=document.createElement("button");if(M.className="storysplat-xr-btn storysplat-ar-btn",M.setAttribute("aria-label","Enter AR"),M.textContent="AR",e.appendChild(M),x.arButton=M,r){const t=document.createElement("button");t.className="storysplat-help-btn",t.setAttribute("title","Toggle Help"),t.textContent="?",e.appendChild(t),x.helpButton=t;const o=document.createElement("div");o.className="storysplat-help-panel",o.innerHTML=`\n <h3>${w.helpTitle}</h3>\n <p><strong>Camera Modes:</strong></p>\n <p>• ${w.tour} - Follow predefined path</p>\n <p>• ${w.explore} - Free movement</p>\n <p>• ${w.walk} - First-person walking</p>\n <br/>\n <p><strong>${w.tour} Mode:</strong></p>\n <p>• Scroll - Move along path</p>\n <p>• Drag - Look around</p>\n <br/>\n <p><strong>${w.explore} Mode:</strong></p>\n <p>• LMB Drag - Orbit camera</p>\n <p>• RMB Drag - Fly/look</p>\n <p>• WASD/QE - Move camera</p>\n <p>• Shift - Move fast</p>\n <p>• Scroll/Pinch - Zoom</p>\n <p>• Double-click - Focus</p>\n <br/>\n <p><strong>${w.walk} Mode:</strong></p>\n <p>• Click to lock mouse</p>\n <p>• WASD/Arrows - Move</p>\n <p>• Mouse - Look around</p>\n <p>• Shift - Sprint</p>\n <p>• Space - Jump</p>\n `,e.appendChild(o),x.helpPanel=o}const C=document.createElement("div");C.className="storysplat-hotspot-popup",C.id="hotspotContent",C.innerHTML='\n <h2 class="storysplat-hotspot-popup-title"></h2>\n <div class="storysplat-hotspot-popup-content"></div>\n <button class="storysplat-hotspot-popup-close">Close</button>\n ',e.appendChild(C),x.hotspotPopup=C;const _=C.querySelector(".storysplat-hotspot-popup-close");_?.addEventListener("click",()=>{C.classList.remove("visible","fullscreen")});const L=document.createElement("div");L.className="storysplat-joystick-container",L.innerHTML='\n <div class="storysplat-joystick-base"></div>\n <div class="storysplat-joystick-thumb"></div>\n ',e.appendChild(L),x.joystick=L,x.joystickThumb=L.querySelector(".storysplat-joystick-thumb");const T=document.createElement("div");T.className="storysplat-look-zone",T.innerHTML='\n <div class="storysplat-look-zone-icon">\n <svg viewBox="0 0 24 24">\n <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/>\n </svg>\n </div>\n ',e.appendChild(T),x.lookZone=T;const k=document.createElement("button");k.className="storysplat-camera-mode-toggle fly",k.setAttribute("aria-label","Toggle camera mode"),k.innerHTML='\n <svg class="orbit-icon" viewBox="0 0 24 24">\n <path d="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01-.25 1.97-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0-4.42-3.58-8-8-8zm0 14c-3.31 0-6-2.69-6-6 0-1.01.25-1.97.7-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4-4-4-4v3z"/>\n </svg>\n <svg class="fly-icon" viewBox="0 0 24 24">\n <path d="M12 2L4.5 20.29l.71.71L12 18l6.79 3 .71-.71z"/>\n </svg>\n ',e.appendChild(k),x.cameraModeToggle=k;const A=document.createElement("button");if(A.className="storysplat-return-waypoint-btn",A.setAttribute("aria-label",w.returnToTour),A.innerHTML=`\n <svg viewBox="0 0 24 24">\n <path d="M9 11H7v2h2v-2zm4 0h-2v2h2v-2zm4 0h-2v2h2v-2zm2-7h-1V2h-2v2H8V2H6v2H5c-1.11 0-1.99.9-1.99 2L3 20c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V9h14v11z"/>\n </svg>\n ${w.returnToTour}\n `,e.appendChild(A),x.returnWaypointButton=A,!y){const t=document.createElement("div");t.className="storysplat-watermark";const o=v||(b?`https://storysplat.com?ref=${b}`:"https://storysplat.com");t.innerHTML=f?`<a href="${o}" target="_blank">${f}</a>`:`Created with <a href="${o}" target="_blank">StorySplat</a>`,e.appendChild(t),x.watermark=t}return x}(t,r,{uiColor:x,showScrollControls:r.waypoints&&r.waypoints.length>0,showModeToggle:C.length>1,showFullscreenButton:!S.hideFullscreenButton,showHelpButton:!S.hideHelpButton&&!S.hideInfoButton,showPreloader:!0,allowedCameraModes:C,defaultCameraMode:T,buttonLabels:S.buttonLabels,customPreloaderLogoUrl:S.customPreloaderLogoUrl,hideWatermark:S.hideWatermark,watermarkText:S.watermarkText,watermarkLink:S.watermarkLink,sceneId:o.sceneId}));const z=document.createElement("canvas");let R;z.id="storysplat-viewer-canvas",z.style.width="100%",z.style.height="100%",z.style.display="block",t.appendChild(z);const I={antialias:!1,alpha:!1,powerPreference:"high-performance"};try{R=new e.Application(z,{graphicsDeviceOptions:I,mouse:new e.Mouse(z),touch:new e.TouchDevice(z),keyboard:new e.Keyboard(window)}),console.log("[StorySplat Viewer] Graphics initialized successfully")}catch(o){console.warn("[StorySplat Viewer] WebGL2 initialization failed, trying WebGL1 fallback:",o);try{R=new e.Application(z,{graphicsDeviceOptions:{...I,preferWebGl2:!1},mouse:new e.Mouse(z),touch:new e.TouchDevice(z),keyboard:new e.Keyboard(window)}),console.log("[StorySplat Viewer] WebGL1 fallback successful")}catch(e){console.error("[StorySplat Viewer] WebGL initialization failed completely:",e);const o=document.createElement("div");o.style.cssText="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);color:#fff;background:rgba(0,0,0,0.8);padding:20px;border-radius:10px;text-align:center;font-family:sans-serif;";const n=document.createElement("h3");n.style.cssText="margin:0 0 10px 0;",n.textContent="Unable to Initialize 3D Graphics";const i=document.createElement("p");throw i.style.cssText="margin:0;",i.textContent="Your browser or device may not support WebGL. Please try a different browser or device.",o.appendChild(n),o.appendChild(i),t.appendChild(o),new Error("WebGL initialization failed - browser may not support WebGL")}}z.addEventListener("webglcontextlost",e=>{e.preventDefault(),console.error("[StorySplat Viewer] WebGL context lost"),i.emit("error",new Error("WebGL context lost"))},!1),z.addEventListener("webglcontextrestored",()=>{console.log("[StorySplat Viewer] WebGL context restored")},!1),R.setCanvasFillMode(e.FILLMODE_FILL_WINDOW),R.setCanvasResolution(e.RESOLUTION_AUTO),R.start(),console.log("[StorySplat Viewer] App started");const D=K(),F=D?"mobile":"desktop",U=Q[F];console.log("[SPLAT] Initializing LOD system for device:",D?"mobile":"desktop"),R.scene.gsplat?(R.scene.gsplat.lodUpdateAngle=90,R.scene.gsplat.lodBehindPenalty=2,R.scene.gsplat.radialSorting=!0,R.scene.gsplat.lodUpdateDistance=1,R.scene.gsplat.lodUnderfillLimit=10,R.scene.gsplat.lodRangeMin=U.range[0],R.scene.gsplat.lodRangeMax=U.range[1],R.scene.gsplat.colorUpdateDistance=1,R.scene.gsplat.colorUpdateAngle=4,R.scene.gsplat.colorUpdateDistanceLodScale=2,R.scene.gsplat.colorUpdateAngleLodScale=2,console.log("[SPLAT] LOD system configured:",{preset:F,lodRangeMin:U.range[0],lodRangeMax:U.range[1],lodDistances:U.lodDistances,lodUpdateAngle:90,lodUpdateDistance:1,isMobile:D})):console.warn("[SPLAT] GSplat scene settings not available - LOD may not work optimally");let B=0,V=!1,$=null,H=null,O=!1,W=null;const G=r.additionalSplats||[],N=r.keepMeshesInMemory??!1,j=r.initialSplatExploreMode||"fly";let oe=null,ie=!1;const ae=new Map;let se=-1,re=-1;const le=new e.Entity("camera");let ce=new e.Color(.1,.1,.1);if(r.backgroundColor){const t=r.backgroundColor.replace("#","");if(6===t.length){const o=parseInt(t.substring(0,2),16)/255,n=parseInt(t.substring(2,4),16)/255,i=parseInt(t.substring(4,6),16)/255;ce=new e.Color(o,n,i),console.log("[StorySplat Viewer] Background color set from config:",r.backgroundColor)}}le.addComponent("camera",{clearColor:ce,fov:r.fov||60,nearClip:r.nearClip||.1,farClip:r.farClip||1e3}),le.addComponent("audiolistener"),console.log("[StorySplat Viewer] Camera settings:",{fov:r.fov,nearClip:r.nearClip,farClip:r.farClip,playerHeight:r.playerHeight});const de=r.playerHeight||1.6;if(r.waypoints&&r.waypoints.length>0){const e=r.waypoints[0];if(console.log("[StorySplat Viewer] First waypoint raw:",e),e.position){const t=e.position;le.setPosition(t.x,t.y,-t.z),console.log("[StorySplat Viewer] Camera position (Z negated):",{x:t.x,y:t.y,z:-t.z})}else le.setPosition(0,de,5);if(e.rotation){const t=Ce(e.rotation);le.setRotation(t),console.log("[StorySplat Viewer] Camera rotation set from waypoint")}}else le.setPosition(0,de,5),le.lookAt(new e.Vec3(0,0,0)),console.log("[StorySplat Viewer] Camera set to default position (0, playerHeight, 5)");R.root.addChild(le);const pe=new e.Entity("light");pe.addComponent("light",{type:e.LIGHTTYPE_DIRECTIONAL,color:new e.Color(1,1,1),intensity:1,castShadows:!1}),pe.setEulerAngles(45,45,0),R.root.addChild(pe),console.log("[StorySplat Viewer] Light added");const he=new _(le,R,{moveSpeed:2*(r.cameraMovementSpeed||1),moveFastSpeed:5*(r.cameraMovementSpeed||1),moveSlowSpeed:1*(r.cameraMovementSpeed||1),rotateSpeed:5e-4*(r.cameraRotationSensitivity||.2),enableOrbit:!0,enableFly:!0,enablePan:!0,invertRotation:r.invertCameraRotation,moveDamping:.75,rotateDamping:.75,zoomDamping:.8});let ue=null;r.collisionMeshesData&&r.collisionMeshesData.length>0&&(ue=new L(le,R,{moveSpeed:2*(r.cameraMovementSpeed||1),sprintMultiplier:2,lookSensitivity:.005*(r.cameraRotationSensitivity||.2),playerHeight:r.playerHeight||1.6,gravity:20,jumpVelocity:8,collisionRadius:.3,stepHeight:.3}),ue.createCollisionMeshes(r.collisionMeshesData).then(()=>{console.log("[StorySplat Viewer] Collision meshes loaded for walk mode")}).catch(e=>{console.error("[StorySplat Viewer] Failed to load collision meshes:",e)}));let me=T,ge=!0;"tour"===T&&he.disable();const ye=new e.Picker(R,1,1,!0);let fe=!1,ve=null;let be=null,we=null,xe=!1;function Se(){be&&(be.enabled=!1,xe=!1)}function Ee(e){"walk"===me&&ue?ue.disable():"explore"===me&&he.disable(),me=e,console.log("[StorySplat Viewer] Switching to mode:",e);const t=K();if("walk"===e&&ue)ge=!1,he.disable(),ue.enable(),m(A,!1),f(A,!1),r.waypoints&&r.waypoints.length>0&&b(A,!0);else if("explore"===e){ue&&ue.disable(),Ge=0,Ne=0,je=!1,ge=!1,he.disable(),he.enable(),he.enableOrbit=!0,he.enableFly=!0,he.enablePan=!0,He&&Oe?(he.syncFromPose(He,Oe),console.log("[StorySplat Viewer] Synced camera from waypoint pose for explore mode")):he.syncFromCamera();(async()=>{try{const e=.25;ye.resize(Math.floor(Yt.clientWidth*e),Math.floor(Yt.clientHeight*e));const t=R.scene.layers.getLayerByName("World");if(t){ye.prepare(le.camera,R.scene,[t]);const o=Math.floor(.5*Yt.clientWidth*e),n=Math.floor(.5*Yt.clientHeight*e),i=await ye.getWorldPointAsync(o,n);if(i){const e=le.getPosition().distance(i);e>.5&&e<500&&(he.syncFromCamera(i),console.log("[StorySplat Viewer] Updated focus target at distance:",e.toFixed(2)))}}}catch(e){}})(),t?(m(A,!0),f(A,!0),he.setMode("fly"),v(A,"fly")):he.setMode("orbit"),r.waypoints&&r.waypoints.length>0&&b(A,!0)}else he.disable(),ue&&ue.disable(),ge=!0,m(A,!1),f(A,!1),b(A,!1);i.emit("modeChange",{mode:e})}R.on("update",t=>{"walk"===me&&ue?ue.update(t):he.update(t),ge||function(){const e=le.getPosition();ut.forEach(t=>{const o=t.hotspotData;if(!o)return;if("proximity"!==(t.mediaTriggerMode||"click"))return;const n=t.getPosition(),i=e.distance(n)<=(t.proximityDistance||5),a=t.wasInProximity||!1;if(i&&!a){if("video"===o.type&&t.videoElement)Ot(t,o);else if("gif"===o.type)t.enabled=!0;else if(("sphere"===o.type||"image"===o.type)&&t.audioElements){const e=t.audioElements,n=e.audio;n&&n.paused&&(e.audioCtx&&"suspended"===e.audioCtx.state&&e.audioCtx.resume(),n.play().catch(e=>console.error("[Audio] Hotspot audio play failed (proximity):",e)),console.log("[Audio] Hotspot audio started (proximity):",o.title))}t.wasInProximity=!0}else if(!i&&a){if("video"===o.type&&t.videoElement)Wt(t);else if("gif"===o.type)t.enabled=!1;else if(("sphere"===o.type||"image"===o.type)&&t.audioElements){const e=t.audioElements.audio;e&&!e.paused&&(e.pause(),console.log("[Audio] Hotspot audio stopped (proximity):",o.title))}t.wasInProximity=!1}})}(),function(){if(0===ft.size)return;const e=le.getPosition();ft.forEach((t,o)=>{const{entity:n,config:i,slotId:a,assetReady:s,playing:r}=t;if(!s)return;const l=n.sound?.slot(a);if(l&&i.spatialSound){const a=n.getPosition(),s=e.distance(a),c=i.maxDistance||20;s<=c&&!r?(console.log(`[Audio] Proximity play: ${o}, distance=${s.toFixed(2)}, maxDistance=${c}`),l.play(),t.playing=!0):s>c&&r&&i.stopOnExit&&(console.log(`[Audio] Proximity stop: ${o}, distance=${s.toFixed(2)}`),l.stop(),t.playing=!1)}})}(),function(){if(0===Ft.size)return;const e=le.getPosition();Ft.forEach((t,o)=>{const{entity:n,config:i,slotId:a,assetReady:s,playing:r}=t;if(!s)return;const l=n.sound?.slot(a);if(l&&!1!==i.spatialSound){const o=n.getPosition();e.distance(o)<=(i.maxDistance||100)&&!r&&!1!==i.autoplay&&(l.isPlaying||(l.play(),t.playing=!0))}})}(),function(){if(!r.waypoints?.length)return;const t=le.getPosition();r.waypoints.forEach((o,n)=>{const i=new e.Vec3(o.position?.x??0,o.position?.y??0,-(o.position?.z??0)),a=t.distance(i),s=o.triggerDistance??1;a<=s?Ut.has(n)||(Ut.add(n),console.log(`[StorySplat] Waypoint ${n} triggered (distance: ${a.toFixed(2)}, threshold: ${s})`),function(e){if(!e.interactions?.length)return;e.interactions.forEach(e=>{if("audio"===e.type){const t=e.id,o=ft.get(t);if(o&&o.assetReady&&!o.playing){const e=o.entity.sound?.slot(o.slotId);e&&(e.play(),o.playing=!0)}}})}(o)):Ut.has(n)&&(Ut.delete(n),console.log(`[StorySplat] Waypoint ${n} exited`),function(e){if(!e.interactions?.length)return;e.interactions.forEach(e=>{if("audio"===e.type){if(e.data?.stopOnExit??!1){const t=e.id,o=ft.get(t);if(o&&o.playing){const e=o.entity.sound?.slot(o.slotId);e&&(e.stop(),o.playing=!1)}}}})}(o))})}(),fe&&xe&&function(){if(be?.enabled&&le){const e=le.forward.clone(),t=le.getPosition(),o=t.clone().add(e.mulScalar(1.5));o.y-=.1,be.setPosition(o),be.lookAt(t),be.rotateLocal(90,0,0)}}()}),R.on("joystick:left",(e,t,o,n)=>{if(e<0||t<0)g(A,!1,0,0,60);else{g(A,!0,o-e,n-t,60)}}),R.on("joystick:right",(e,t,o,n)=>{y(A,!(e<0||t<0))}),A.cameraModeToggle&&A.cameraModeToggle.addEventListener("click",()=>{if("explore"!==me)return;const e=he.mode;"orbit"===e||"focus"===e?(he.setMode("fly"),v(A,"fly"),m(A,!0)):(he.setMode("orbit"),v(A,"orbit"),m(A,!1))}),R.on("cameracontrols:modechange",e=>{"orbit"!==e&&"fly"!==e||(v(A,e),D&&"explore"===me&&m(A,"fly"===e))});const Me=(e,t)=>{A.preloader&&function(e,t,o){const n=e.querySelector(".storysplat-preloader-bar"),i=e.querySelector(".storysplat-preloader-text"),a=Math.max(0,Math.min(100,100*t));n&&(n.style.width=`${a}%`),i&&(i.textContent=o||`Loading... ${Math.round(a)}%`)}(A.preloader,e,t),i.emit("progress",{progress:e,text:t})};function Ce(t){if("_w"in t||"w"in t){const o=t._x??t.x??0,n=t._y??t.y??0,i=t._z??t.z??0,a=t._w??t.w??1;return new e.Quat(-o,-n,i,a)}if("x"in t&&"y"in t&&"z"in t){const o=new e.Quat;return o.setFromEulerAngles(t.x||0,t.y||0,t.z||0),o}return new e.Quat}function _e(e){e.enabled=!1,console.log("[SplatSwap] Splat hidden")}function Le(e){const t=ae.get(e);t&&(t.destroy(),ae.delete(e),console.log("[SplatSwap] Splat disposed:",e))}function Te(e,t=!1){if("explore"!==me)return;const o=t?j:e||j;if(o&&he){he.mode!==o&&(he.setMode(o),console.log(`[SplatSwap] Switching explore sub-mode to: ${o}`),D&&(v(A,o),m(A,"fly"===o)))}}async function ke(t){if(!G||0===G.length)return;const o=(t+1)%G.length,n=G[o];n&&n.url&&await async function(t){if(!ae.has(t)&&t!==oe&&!O){console.log("[SplatSwap] Preloading splat:",t);try{const o=new e.Asset("splat-preload-"+Date.now(),"gsplat",{url:t});await new Promise((n,i)=>{o.ready(()=>{if(O)return void i(new Error("Viewer destroyed"));const a=new e.Entity("splat-preload");a.addComponent("gsplat",{asset:o,unified:!0});const s=r.scale||{x:1,y:1,z:1},l=r.invertXScale||!1,c=r.invertYScale||!1,d={x:l?-s.x:s.x,y:c?-s.y:s.y,z:l!==c?-s.z:s.z};a.setLocalScale(d.x,d.y,d.z);const p=r.position||[0,0,0];a.setPosition(p[0],p[1],-p[2]);const h=r.rotation||[0,0,0],u=0!==h[0]||0!==h[1]||0!==h[2]?[h[0]*(180/Math.PI),h[1]*(180/Math.PI),-h[2]*(180/Math.PI)]:[180,0,0];a.setEulerAngles(u[0],u[1],u[2]),a.enabled=!1,R.root.addChild(a),ae.set(t,a),console.log("[SplatSwap] Preload complete:",t),n()}),o.on("error",e=>{console.error("[SplatSwap] Preload error:",e),i(e)}),R.assets.add(o),R.assets.load(o)})}catch(e){console.error("[SplatSwap] Error preloading:",t,e)}}}(n.url)}async function Ae(t){if(t!==oe&&!ie&&!O){ie=!0,console.log("[SplatSwap] Switching to splat:",t);try{if(oe&&ae.has(oe))if(N){const e=ae.get(oe);e&&_e(e)}else Le(oe);else $&&($.enabled=!1);if(ae.has(t))!function(e){const t=ae.get(e);!!t&&(t.enabled=!0,oe=e,console.log("[SplatSwap] Splat shown:",e))}(t),i.emit("splatChange",{url:t,isOriginal:!1});else{const o=new e.Asset("splat-swap-"+Date.now(),"gsplat",{url:t});await new Promise((n,a)=>{o.ready(()=>{if(O)return void a(new Error("Viewer destroyed"));const s=new e.Entity("splat-swap");s.addComponent("gsplat",{asset:o,unified:!0});const l=r.scale||{x:1,y:1,z:1},c=r.invertXScale||!1,d=r.invertYScale||!1,p={x:c?-l.x:l.x,y:d?-l.y:l.y,z:c!==d?-l.z:l.z};s.setLocalScale(p.x,p.y,p.z);const h=r.position||[0,0,0];s.setPosition(h[0],h[1],-h[2]);const u=r.rotation||[0,0,0],m=0!==u[0]||0!==u[1]||0!==u[2]?[u[0]*(180/Math.PI),u[1]*(180/Math.PI),-u[2]*(180/Math.PI)]:[180,0,0];s.setEulerAngles(m[0],m[1],m[2]),R.root.addChild(s),ae.set(t,s),oe=t,i.emit("splatChange",{url:t,isOriginal:!1}),console.log("[SplatSwap] New splat loaded and shown:",t),n()}),o.on("error",e=>{console.error("[SplatSwap] Load error:",e),a(e)}),R.assets.add(o),R.assets.load(o)})}const o=G.findIndex(e=>e.url===t);-1!==o&&ke(o)}catch(e){console.error("[SplatSwap] Error switching splat:",e)}finally{ie=!1}}}function Pe(){return r.sogUrl?r.sogUrl:r.splatUrl?r.splatUrl:r.fallbackUrls&&r.fallbackUrls.length>0?r.fallbackUrls[0]:""}function ze(){if(!G||0===G.length)return;const t=r.waypoints?.length||1,o=100*Re,n=Math.round(Re*Math.max(1,t-1));if(Math.abs(o-se)<.1&&n===re)return;se=o,re=n;let a=null,s=null,l=-1/0,c=-1/0;for(const e of G)-1!==e.waypointIndex?n>=e.waypointIndex&&e.waypointIndex>l&&(l=e.waypointIndex,a=e):-1!==e.percentage&&o>=e.percentage&&e.percentage>c&&(c=e.percentage,s=e);const d=s||a,p=d&&"__ORIGINAL__"===d.url,h=Pe(),u=d?p?h:d.url:h;u&&u!==oe&&(u===h&&$&&!oe?oe=h:u===h&&$?(ae.forEach((e,t)=>{t!==h&&(N?_e(e):Le(t))}),$&&($.enabled=!0),oe=h,i.emit("splatChange",{url:h,isOriginal:!0}),console.log("[SplatSwap] Returned to primary splat"),Te(void 0,!0)):(Ae(u),d&&Te(d.defaultExploreMode,!1)),d&&d.skyboxUrl&&!p&&function(t,o=0){console.log("[SplatSwap] Applying skybox:",t,"rotation:",o);const n=new e.Asset("skybox-swap-"+Date.now(),"cubemap",{url:t},{type:e.TEXTURETYPE_RGBM,mipmaps:!0});n.ready(()=>{if(!O)try{if(R.scene.skybox=n.resource,R.scene.skyboxMip=0,0!==o){const t=new e.Quat;t.setFromEulerAngles(0,o*(180/Math.PI),0),R.scene.skyboxRotation=t}console.log("[SplatSwap] Skybox applied successfully")}catch(e){console.error("[SplatSwap] Error applying skybox:",e)}}),n.on("error",e=>{console.error("[SplatSwap] Skybox load error:",e)}),R.assets.add(n),R.assets.load(n)}(d.skyboxUrl,d.skyboxRotation||0))}let Re=0;const Ie=r.waypoints?.reduce((e,t)=>e+(t.duration||2e3),0)||1,De=r.waypoints?.length||1,Fe=Math.max(1,20*(De-1)),Ue=void 0!==r.autoplaySpeed?60*r.autoplaySpeed/Fe:1e3/Ie,Be=r.loopMode;let Ve;Ve=!0===Be?"loop":!1===Be?"none":"loop"===Be||"pingpong"===Be||"none"===Be?Be:"loop";let $e=1;console.log("[StorySplat Viewer] Playback config:",{loopMode:Ve,playbackSpeed:Ue,totalDuration:Ie,autoPlay:r.autoPlay,rawLoopMode:r.loopMode});let He=null,Oe=null;const We=.01+.1*(r.transitionSpeed||1);let Ge=0,Ne=0;let je=!1,qe=0,Ye=0;const Xe=[],Ze=[],Je=[],Ke=r.fov||60;let Qe=Ke;function et(t){if(!ge||Xe.length<2)return;t=Math.max(0,Math.min(1,t));const o=Xe.length,n=t*(o-1),a=Math.min(Math.floor(n),o-2),s=n-a,l=Xe[a],c=Xe[a+1];He=new e.Vec3(ne(l.x,c.x,s),ne(l.y,c.y,s),ne(l.z,c.z,s));const d=Ze[a],p=Ze[a+1];Oe=new e.Quat,Oe.slerp(d,p,s);const h=Je[a],u=Je[a+1];Qe=ne(h,u,s);const m=Math.round(t*(o-1));if(m!==B){const t=B;B=m;const o=r.waypoints[m],n=o.cameraMode||"first-person";"orbit"===n&&(o.orbitTarget?new e.Vec3(o.orbitTarget.x,o.orbitTarget.y,-(o.orbitTarget.z||0)):new e.Vec3(Xe[m].x,Xe[m].y,Xe[m].z)),i.emit("waypointChange",{index:m,waypoint:o,prevIndex:t,cameraMode:n}),g=m,y=t,ft.forEach((e,t)=>{const{entity:o,waypointIndex:n,config:i,slotId:a,autoplayTriggered:s}=e,r=o.sound?.slot(a);if(!r)return;const l=y===n&&g!==n;g===n&&y!==n&&i.autoplay&&!s&&(console.log(`[Audio] Autoplay waypoint audio: ${t} at waypoint ${n}`),r.isPlaying||(r.play(),e.playing=!0,e.autoplayTriggered=!0)),l&&i.stopOnExit&&e.playing&&(console.log(`[Audio] Stopping waypoint audio on exit: ${t}`),r.isPlaying&&(r.stop(),e.playing=!1,e.autoplayTriggered=!1))})}var g,y;i.emit("progressUpdate",{progress:Math.max(0,Math.min(1,t)),index:B}),ze()}function tt(e,t=!1){t?nt(e):(Re=Math.max(0,Math.min(1,e)),et(Re))}r.waypoints&&r.waypoints.length>0&&(r.waypoints.forEach(t=>{const o=t.position?new e.Vec3(t.position.x,t.position.y,-t.position.z):new e.Vec3(0,r.playerHeight||1.6,0);Xe.push(o);const n=t.rotation?Ce(t.rotation):new e.Quat;Ze.push(n);let i=t.fov||Ke;i<3.5&&(i*=180/Math.PI),Je.push(i)}),Xe.length>0&&(He=Xe[0].clone(),Oe=Ze[0].clone(),Qe=Je[0])),R.on("update",function(){if(!He||!Oe)return;if(!ge)return;const t=le.getPosition(),o=new e.Vec3;o.lerp(t,He,We),le.setPosition(o.x,o.y,o.z);const n=le.camera;if(n&&Je.length>0){const e=ne(n.fov,Qe,We);n.fov=e}je||(Ge*=.95,Ne*=.95,Math.abs(Ge)<.01&&(Ge=0),Math.abs(Ne)<.01&&(Ne=0));const i=new e.Quat;i.setFromEulerAngles(Ne,Ge,0);const a=new e.Quat;a.mul2(Oe,i);const s=le.getRotation(),r=new e.Quat;r.slerp(s,a,We),le.setRotation(r)});const ot=500*(r.transitionSpeed||1);function nt(e,t=ot){const o=Re,n=performance.now(),i=()=>{const a=performance.now()-n,s=Math.min(a/t,1);Re=o+(e-o)*(s<.5?2*s*s:(4-2*s)*s-1),et(Re),s<1&&requestAnimationFrame(i)};requestAnimationFrame(i)}function it(e){if(!r.waypoints||e<0||e>=r.waypoints.length)return;if(!ge)return;nt(e/Math.max(1,r.waypoints.length-1))}function at(){if(!r.waypoints||0===r.waypoints.length)return;const e=r.scrollButtonMode||"waypoint";if("percentage"===e||"continuous"===e||"incremental"===e){const e=r.scrollAmount||10;let t=Re+e/100;t>1&&(t="loop"===Ve?0:1),nt(t)}else{let e=B+1;e>=r.waypoints.length&&(e="loop"===Ve?0:r.waypoints.length-1),it(e)}}function st(){if(!r.waypoints||0===r.waypoints.length)return;const e=r.scrollButtonMode||"waypoint";if("percentage"===e||"continuous"===e||"incremental"===e){const e=r.scrollAmount||10;let t=Re-e/100;t<0&&(t="loop"===Ve?1:0),nt(t)}else{let e=B-1;e<0&&(e="loop"===Ve?r.waypoints.length-1:0),it(e)}}let rt=0,lt=null;function ct(e){if(!V)return;0===rt&&(rt=e);const t=(e-rt)/1e3;if(rt=e,Re+=Ue*t*$e,Re>=1)switch(console.log("[StorySplat Viewer] End of tour reached, loopMode:",Ve),Ve){case"loop":console.log("[StorySplat Viewer] Looping - restarting at beginning"),Re=0;break;case"pingpong":console.log("[StorySplat Viewer] Pingpong - reversing direction"),Re=1,$e=-1;break;case"none":return console.log("[StorySplat Viewer] No loop - stopping playback"),Re=1,pt(),void i.emit("playbackComplete");default:return console.log("[StorySplat Viewer] Unknown loopMode, stopping:",Ve),Re=1,pt(),void i.emit("playbackComplete")}else if(Re<=0)if("pingpong"===Ve)console.log("[StorySplat Viewer] Pingpong - reversing to forward"),Re=0,$e=1;else Re=0;et(Re),lt=requestAnimationFrame(ct)}function dt(){if(W)return W.play(),void i.emit("playbackStart");V||!r.waypoints||r.waypoints.length<2||(V=!0,rt=0,$e=1,i.emit("playbackStart"),lt=requestAnimationFrame(ct))}function pt(){if(W)return W.pause(),void i.emit("playbackStop");V=!1,lt&&(cancelAnimationFrame(lt),lt=null),i.emit("playbackStop")}const ht=R.graphicsDevice.canvas;ht.addEventListener("wheel",e=>{if(!ge)return;e.preventDefault();const t=r.scrollSpeed||.1,o=r.scrollAmount||100,n=Math.abs(e.deltaY)/100*(o/100)*t*.5,i=e.deltaY>0?n:-n;tt(Math.max(0,Math.min(1,Re+i)))},{passive:!1}),ht.addEventListener("pointerdown",e=>{ge&&(je=!0,qe=e.clientX,Ye=e.clientY)},{capture:!0}),ht.addEventListener("pointermove",e=>{if(!ge||!je)return;const t=e.clientX-qe,o=e.clientY-Ye;qe=e.clientX,Ye=e.clientY;Ge+=.3*-t,Ne+=.3*-o,Ne=Math.max(-60,Math.min(60,Ne))},{capture:!0}),ht.addEventListener("pointerup",()=>{je=!1},{capture:!0}),ht.addEventListener("pointerleave",()=>{je=!1},{capture:!0});const ut=[],mt=[];function gt(t){const o=parseInt(t.slice(1,3),16)/255,n=parseInt(t.slice(3,5),16)/255,i=parseInt(t.slice(5,7),16)/255;return new e.Color(o,n,i)}const yt=[],ft=new Map,vt=new Map,bt=new Map,wt={flare:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Fflare.png?alt=media&token=ce114781-2ac3-41b2-b9c2-34bda0f6eb13",circle:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Fcircle.png?alt=media&token=fd01b475-2b94-4c24-bc83-2a907045715f",spark:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Fsparkle.png?alt=media&token=466739a4-ccd7-4295-88c2-dedd681a34f0",rain:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Frain.png?alt=media&token=13478487-6259-4906-838c-ad592db893e4",smoke:"https://firebasestorage.googleapis.com/v0/b/story-splat.firebasestorage.app/o/public%2Fparticles%2Fsmoke.png?alt=media&token=6cece4f8-87cf-47f9-974d-c4dd6a649f0f"};function xt(t,o){return new Promise((n,i)=>{if(bt.has(t))return void n(bt.get(t));const a=new e.Asset(t,"texture",{url:o});a.on("load",()=>{if(O)return console.log(`[Particle] Ignoring texture load - viewer was destroyed: ${t}`),void i(new Error("Viewer destroyed"));const e=a.resource;bt.set(t,e),n(e)}),a.on("error",e=>{console.error(`[Particle] Failed to load texture: ${t}`,e),i(e)}),R.assets.add(a),R.assets.load(a)})}function St(t){const o=new e.Entity(t.name||"particle-system"),n=(e,t=0)=>({x:e?._x??e?.x??t,y:e?._y??e?.y??t,z:e?._z??e?.z??t}),i=e=>e||{r:1,g:1,b:1,a:1},a=n(t.emitterPosition,0),s=n(t.gravity,0),r=i(t.color1),l=i(t.color2),c=i(t.colorDead),d=n(t.direction1,0),p=n(t.direction2,0),h=((t.minLifeTime??1)+(t.maxLifeTime??3))/2;let u=e.EMITTERSHAPE_BOX,m=new e.Vec3(.1,.1,.1),g=new e.Vec3(0,0,0);if("sphere"===t.emitterType||"sphere"===t.emitterShape){u=e.EMITTERSHAPE_SPHERE;const o=t.emitterRadius||.1;m=new e.Vec3(o,o,o)}else if("box"===t.emitterShape&&t.emitBoxMin&&t.emitBoxMax){u=e.EMITTERSHAPE_BOX;const o=n(t.emitBoxMin,0),i=n(t.emitBoxMax,0);m=new e.Vec3(Math.abs(i.x-o.x)/2,Math.abs(i.y-o.y)/2,Math.abs(i.z-o.z)/2),g=new e.Vec3((o.x+i.x)/2,(o.y+i.y)/2,-(o.z+i.z)/2)}else"point"===t.emitterShape?(u=e.EMITTERSHAPE_BOX,m=new e.Vec3(.01,.01,.01)):m=new e.Vec3(t.emitterExtents?.x||t.emitterRadius||.1,t.emitterExtents?.y||t.emitterRadius||.1,t.emitterExtents?.z||t.emitterRadius||.1);let y=e.BLEND_ADDITIVEALPHA;const f=t.blendMode||"";"BLENDMODE_STANDARD"===f||"normal"===f||"alpha"===f?y=e.BLEND_NORMAL:"BLENDMODE_MULTIPLY"===f||"multiply"===f?y=e.BLEND_MULTIPLICATIVE:"BLENDMODE_ONEONE"===f?y=e.BLEND_ADDITIVE:"BLENDMODE_ADD"!==f&&"BLENDMODE_MULTIPLYADD"!==f||(y=e.BLEND_ADDITIVEALPHA);const v=s.x||0,b=s.y||0,w=-(s.z||0),x=v*h,S=b*h,E=w*h,M=t.minAngularSpeed??0,C=t.maxAngularSpeed??t.angularSpeed??0,_=t.minInitialRotation??0,L=t.maxInitialRotation??0,T=t.minSize??.1,k=t.maxSize??.3,A=t.minScaleX??1,P=t.maxScaleX??1,z=t.minScaleY??1,R=t.maxScaleY??1,I=t.minEmitPower??1,D=t.maxEmitPower??2,F=(d.x+p.x)/2,U=(d.y+p.y)/2,B=-(d.z+p.z)/2;o.addComponent("particlesystem",{numParticles:t.numParticles||500,lifetime:h,rate:t.emitRate||50,emitterShape:u,emitterExtents:m,emitterRadius:t.emitterRadius||.1,startAngle:_,startAngle2:0!==L?L:360,radialSpeedGraph:new e.Curve([0,I,1,D]),localVelocityGraph:new e.CurveSet([[0,F*I],[0,U*I],[0,B*I]]),localVelocityGraph2:new e.CurveSet([[0,F*D],[0,U*D],[0,B*D]]),velocityGraph:new e.CurveSet([[0,0,1,x],[0,0,1,S],[0,0,1,E]]),scaleGraph:new e.Curve([0,T*A,1,k*P]),scaleGraph2:new e.Curve([0,T*z,1,k*R]),rotationSpeedGraph:new e.Curve([0,M]),rotationSpeedGraph2:C!==M?new e.Curve([0,C]):void 0,colorGraph:new e.CurveSet([[0,r.r,.5,l.r,1,c.r],[0,r.g,.5,l.g,1,c.g],[0,r.b,.5,l.b,1,c.b]]),alphaGraph:new e.Curve([0,r.a??1,.5,l.a??.8,1,c.a??0]),blend:y,depthWrite:t.depthWrite??!1,depthSoftening:t.softParticles??0,lighting:t.lighting??!1,halfLambert:t.halfLambert??!1,alignToMotion:t.alignToMotion??!1,stretch:t.stretch||0,preWarm:t.preWarm??!1,loop:t.loop??!0,autoPlay:t.autoPlay??!0,sort:t.sort??0,orientation:t.orientation??0}),o.particlesystem&&(o.particlesystem.localSpace=t.localSpace??!1);const V=t.translationPivot?.x??0,$=t.translationPivot?.y??0;o.setPosition(a.x+g.x+V,a.y+g.y+$,-a.z+g.z);const H=t.renderingGroupId??3;return o.particlesystem&&void 0!==H&&(o.particlesystem.drawOrder=H),console.log(`[Particle] Entity configured at position: (${a.x+g.x+V}, ${a.y+g.y+$}, ${-a.z+g.z})`),console.log(`[Particle] Emitter shape: ${t.emitterShape||"box"}, extents: ${m.x}, ${m.y}, ${m.z}`),console.log(`[Particle] Gravity: (${v}, ${b}, ${w})`),console.log(`[Particle] Colors: start=${JSON.stringify(r)}, mid=${JSON.stringify(l)}, dead=${JSON.stringify(c)}`),console.log(`[Particle] Angular speed: ${M} - ${C}, Initial rotation: ${_} - ${L}`),console.log(`[Particle] TranslationPivot: (${V}, ${$}), RenderingGroupId: ${H}`),o}const Et=new Map;function Mt(t){const{type:o,component:n,node:i,animations:a}=t;console.log("[CustomMesh] Playing animation, type:",o,"node:",i?.name,"animations:",a?.length);try{if("pc-anim"===o)console.log("[CustomMesh] Using simple pc-anim approach (like HTML export)"),n.playing=!0,t.isPlaying=!0,console.log("[CustomMesh] Animation started - playing:",n.playing);else if("animation"===o){if(n.playing=!0,n.loop=!0,"function"==typeof n.play){const e=Object.keys(n.animations||{});e.length>0?(n.play(e[0],1),console.log("[CustomMesh] Playing legacy animation clip:",e[0])):n.play()}}else if("glb-skeleton"===o){console.log("[CustomMesh] Starting GLB skeleton animation playback");const{modelEntity:o,asset:n,animations:i}=t;if(i&&i.length>0){const n=i[0];console.log("[CustomMesh] Animation clip:",n),console.log("[CustomMesh] Animation name:",n._name||n.name),console.log("[CustomMesh] Animation duration:",n._duration||n.duration);try{if(!o.anim){console.log("[CustomMesh] Adding AnimComponent to modelEntity");const e={layers:[{name:"Base",states:[{name:"START",speed:1},{name:"Idle",speed:1,loop:!0}],transitions:[{from:"START",to:"Idle",time:0,conditions:[]}]}]};if(o.addComponent("anim",{activate:!0,speed:1}),o.anim){o.anim.loadStateGraph(e);const t=n;o.anim.assignAnimation("Base.Idle",t,"Base"),console.log("[CustomMesh] AnimComponent added and animation assigned")}}if(o.anim)o.anim.playing=!0,o.anim.speed=1,t.animComponent=o.anim,t.isPlaying=!0,console.log("[CustomMesh] GLB animation playing via AnimComponent");else{console.log("[CustomMesh] Falling back to manual curve sampling"),t.isPlaying=!0,t.startTime=Date.now();const i=i=>{if(!t.isPlaying)return;const a=(Date.now()-t.startTime)/1e3%(n._duration||n.duration||1);try{(n._curves||[]).forEach((t,i)=>{if(!t)return;const s=n._paths?.[i];if(!s||!s.entityPath)return;let r=o.findByPath(s.entityPath.join("/"));if(r||(r=o.findByName(s.entityPath[s.entityPath.length-1])),!r)return;const l=t.evaluate?.(a);if(null==l)return;const c=s.component||s.propertyPath?.[0];"localPosition"===c||"position"===c?Array.isArray(l)&&r.setLocalPosition(l[0],l[1],l[2]):"localRotation"===c||"rotation"===c?Array.isArray(l)&&l.length>=4&&r.setLocalRotation(new e.Quat(l[0],l[1],l[2],l[3])):"localScale"!==c&&"scale"!==c||Array.isArray(l)&&r.setLocalScale(l[0],l[1],l[2])})}catch(e){}};t.updateHandler=i,R.on("update",i),console.log("[CustomMesh] Manual animation playback started")}}catch(e){console.error("[CustomMesh] Error setting up GLB animation:",e)}}}else if("anim"===o&&(n.playing=!0,n.speed=1,n.baseLayer)){const e=(n.baseLayer.states||[]).find(e=>"START"!==e&&"END"!==e&&"ANY"!==e);e&&(n.baseLayer.play(e),console.log("[CustomMesh] Playing anim state:",e))}n&&console.log("[CustomMesh] Animation component state - playing:",n.playing,"speed:",n.speed)}catch(e){console.error("[CustomMesh] Error in playAnimComponentV2:",e)}}function Ct(e){const{type:t,component:o}=e;try{"pc-anim"===t?(o.playing=!1,e.isPlaying=!1,console.log("[CustomMesh] pc-anim animation paused")):"glb-skeleton"===t?(e.isPlaying=!1,e.animComponent&&(e.animComponent.playing=!1,console.log("[CustomMesh] GLB skeleton animation paused via AnimComponent")),e.updateHandler&&(R.off("update",e.updateHandler),e.updateHandler=null,console.log("[CustomMesh] GLB manual animation paused"))):o&&(o.playing=!1,o.speed=0,"animation"===t&&"function"==typeof o.pause&&o.pause())}catch(e){console.error("[CustomMesh] Error pausing animation:",e)}}function _t(t,o){if(console.log("[CustomMesh] Loading mesh",o,":",t.name,t),!t.modelUrl||"string"!=typeof t.modelUrl||""===t.modelUrl.trim())return console.warn("[CustomMesh] Skipping mesh",t.name,"- no valid modelUrl provided. Config:",t),null;const n=new e.Entity("custom-mesh-"+o),i=t.position||{x:0,y:0,z:0};if(n.setPosition(i._x??i.x??0,i._y??i.y??0,-(i._z??i.z??0)),t.rotation){const e=t.rotation,o=180/Math.PI;n.setEulerAngles((e._x??e.x??0)*o,(e._y??e.y??0)*o,(e._z??e.z??0)*o)}if(t.scale){const e=t.scale;n.setLocalScale(e._x??e.x??1,e._y??e.y??1,e._z??e.z??1)}const a=t.modelUrl.trim();console.log("[CustomMesh] Loading model from URL:",a);const s=new e.Asset("mesh-model-"+o,"container",{url:a});R.assets.add(s);const r=t.id||`mesh-${o}`,l={entity:n,config:t,isAnimPlaying:!1,audioPlaying:!1};return Et.set(r,l),s.ready(o=>{try{if(console.log("[CustomMesh] Model loaded:",t.name),!o||!o.resource)return void console.error("[CustomMesh] Invalid asset resource for mesh:",t.name);const i=o.resource.instantiateRenderEntity();if(!i)return void console.error("[CustomMesh] Failed to instantiate render entity for mesh:",t.name);function a(e){e.enabled=!0,e.children&&e.children.forEach(e=>a(e))}n.addChild(i),n.modelEntity=i,a(i);let s=0;if(i.forEach(()=>{s++}),console.log("[CustomMesh] Enabled all children for:",t.name,"- Total nodes:",s),"animated"!==t.opacityMode&&void 0!==t.opacity&&t.opacity<1&&(Pt(n,t.opacity),console.log("[CustomMesh] Applied static opacity:",t.opacity,"to",t.name)),t.billboard){const c=n.getEulerAngles().clone();n._billboardActive=!t.billboardRange,R.on("update",()=>{if(!n.enabled)return;if(!n._billboardActive)return void n.setEulerAngles(c.x,c.y,c.z);const e=le.getPosition(),t=n.getPosition(),o=e.x-t.x,i=e.z-t.z,a=Math.atan2(o,i)*(180/Math.PI),s=n.getEulerAngles();n.setEulerAngles(s.x,a,s.z)}),console.log("[CustomMesh] Billboard enabled for:",t.name,t.billboardRange?"(with range control)":"(always active)")}const r=o.resource?.animations?.length>0;if(console.log("[CustomMesh] Asset resource for",t.name,":",{hasAnimations:r,animationCount:o.resource?.animations?.length||0,animations:o.resource?.animations?.map(e=>e?.name||e?.resource?.name||"unnamed")||[],interactionConfig:t.interaction}),r||t.interaction&&t.interaction.playModelAnimation){const d=[],p=i.anim;if(p&&(console.log("[CustomMesh] Found modelEntity.anim component - using simple approach"),d.push({type:"pc-anim",component:p,modelEntity:i})),0===d.length){function h(e,t=0){e.anim&&!d.find(t=>t.component===e.anim)&&(d.push({type:"anim",component:e.anim,node:e}),console.log("[CustomMesh] Found existing ANIM component on:",e.name)),e.animation&&(d.push({type:"animation",component:e.animation,node:e}),console.log("[CustomMesh] Found ANIMATION (legacy) component on:",e.name)),e.children&&e.children.forEach(e=>h(e,t+1))}h(i)}if(0===d.length&&r){const u=o.resource.animations;console.log("[CustomMesh] No anim component found - falling back to GLB skeleton approach"),console.log("[CustomMesh] GLB has",u.length,"embedded animations");try{d.push({type:"glb-skeleton",modelEntity:i,asset:o,animations:u,animationNames:u.map(e=>e.resource?.name||e.name||"Animation"),isPlaying:!1,currentTime:0}),console.log("[CustomMesh] GLB skeleton animations stored:",u.map(e=>e.resource?.name||e.name))}catch(m){console.error("[CustomMesh] Error setting up GLB animations:",m)}}if(d.length>0){l.allAnimComponents=d,l.animComponent=d[0].component,console.log("[CustomMesh] Total animation components for",t.name,":",d.length,"- Types:",d.map(e=>e.type).join(", "));const g=t.interaction?.animationAutoPlay;g&&(d.forEach(e=>{Mt(e)}),l.isAnimPlaying=!0,console.log("[CustomMesh] Auto-playing all animations for:",t.name))}else console.warn("[CustomMesh] No animation components could be set up for mesh:",t.name)}else console.log("[CustomMesh] No animations to setup for:",t.name,"(no GLB animations and playModelAnimation not enabled)");t.interaction&&t.interaction.playAudio&&t.interaction.audioUrl&&function(t,o,n){const i=o.interaction,a=o.id||o.name,s=`mesh-audio-${a}`;t.addComponent("sound",{positional:i.audioSpatial||!1,distanceModel:i.audioDistanceModel||"exponential",refDistance:i.audioRefDistance||1,maxDistance:i.audioMaxDistance||100,rollOffFactor:i.audioRollOffFactor||1,slots:{[s]:{name:s,loop:i.audioLoop||!1,autoPlay:!1,volume:void 0!==i.audioVolume?i.audioVolume:1,pitch:1}}}),n.audioSlotId=s;const r=new e.Asset(`mesh-audio-asset-${a}`,"audio",{url:i.audioUrl});R.assets.add(r),r.ready(()=>{const e=t.sound?.slot(s);e&&(e.asset=r.id),console.log("[CustomMesh] Audio loaded for mesh:",o.name,"Spatial:",i.audioSpatial)}),r.on("error",e=>{console.error("[CustomMesh] Failed to load mesh audio:",o.name,e)}),R.assets.load(r)}(n,t,l),t.interaction&&function(t,o,n){if(!function(e){const t=e.interaction;return!!t&&!!(t.triggerUIPopup||t.playAudio||t.playModelAnimation||t.triggerDirectLink)}(o))return void console.log("[CustomMesh] Skipping interaction setup - no active interactions for:",o.name);const i=o.interaction?.activationMode||"click",a=R.graphicsDevice.canvas,s=(o,n)=>{const i=a.getBoundingClientRect(),s=o-i.left,r=n-i.top,l=le.camera.screenToWorld(s,r,le.camera.nearClip),c=le.camera.screenToWorld(s,r,le.camera.farClip),d=(new e.Vec3).sub2(c,l).normalize(),p=t.modelEntity;if(p&&Lt(p,l,d))return!0;if(Lt(t,l,d))return!0;const h=t.getPosition(),u=(new e.Vec3).sub2(h,l).dot(d);if(u>0){const o=(new e.Vec3).add2(l,d.clone().mulScalar(u)).distance(h),n=t.getLocalScale();if(o<1.5*Math.max(n.x,n.y,n.z))return!0}return!1},r=o.interaction?.popupTriggerMode||i,l=o.interaction?.audioTriggerMode||i,c=o.interaction?.animationTriggerMode||i,d=o.interaction?.directLinkTriggerMode||i;let p=!1;const h=()=>{if(a.style.cursor="pointer","hover"===r&&o.interaction?.triggerUIPopup&&kt(o),"hover"===l&&o.interaction?.playAudio&&n.audioSlotId){const e=t.sound?.slot(n.audioSlotId);e&&!e.isPlaying&&(e.play(),n.audioPlaying=!0,console.log("[CustomMesh] Playing audio on hover for:",o.name))}if("hover"===c&&o.interaction?.playModelAnimation){const e=n.allAnimComponents;e&&e.length>0&&!n.isAnimPlaying&&(e.forEach(e=>Mt(e)),n.isAnimPlaying=!0,console.log("[CustomMesh] Playing animation on hover for:",o.name))}"hover"===d&&o.interaction?.triggerDirectLink&&At(o)},u=()=>{if(a.style.cursor="","hover"===r&&o.interaction?.triggerUIPopup&&function(){const e=document.querySelector(".storysplat-hotspot-popup");e&&e.remove();const t=document.querySelector(".storysplat-mesh-popup");t&&t.remove()}(),"hover"===l&&o.interaction?.playAudio&&n.audioSlotId){const e=t.sound?.slot(n.audioSlotId);e&&e.isPlaying&&(e.stop(),n.audioPlaying=!1,console.log("[CustomMesh] Stopped audio on hover out for:",o.name))}if("hover"===c&&o.interaction?.playModelAnimation){const e=n.allAnimComponents;e&&e.length>0&&n.isAnimPlaying&&(e.forEach(e=>Ct(e)),n.isAnimPlaying=!1,console.log("[CustomMesh] Paused animation on hover out for:",o.name))}},m=()=>{if(console.log("[CustomMesh] Clicked mesh:",o.name),"click"===r&&o.interaction?.triggerUIPopup&&kt(o),"click"===c&&o.interaction?.playModelAnimation){const e=n.allAnimComponents;e&&e.length>0&&(n.isAnimPlaying?(e.forEach(e=>Ct(e)),n.isAnimPlaying=!1,console.log("[CustomMesh] Paused",e.length,"animations on click for:",o.name)):(e.forEach(e=>Mt(e)),n.isAnimPlaying=!0,console.log("[CustomMesh] Playing",e.length,"animations on click for:",o.name)))}if("click"===l&&o.interaction?.playAudio&&n.audioSlotId){const e=t.sound?.slot(n.audioSlotId);e&&(e.isPlaying?(e.pause(),n.audioPlaying=!1,console.log("[CustomMesh] Paused audio on click for:",o.name)):(e.play(),n.audioPlaying=!0,console.log("[CustomMesh] Playing audio on click for:",o.name)))}"click"===d&&o.interaction?.triggerDirectLink&&At(o)},g=e=>{s(e.clientX,e.clientY)&&m()},y=e=>{const t=s(e.clientX,e.clientY);t&&!p?(p=!0,h()):!t&&p&&(p=!1,u())},f=()=>{p&&(p=!1,u())};a.addEventListener("click",g),a.addEventListener("mousemove",y),a.addEventListener("mouseleave",f),t.meshClickHandler=g,t.meshHoverHandler=y,t.meshLeaveHandler=f}(n,t,l),console.log("[CustomMesh] Mesh fully initialized:",t.name)}catch(y){console.error("[CustomMesh] Error processing loaded mesh:",t.name,y)}}),s.on("error",e=>{console.error("[CustomMesh] Failed to load mesh:",t.name,e),R.assets.remove(s)}),R.assets.load(s),t.visibilityRange?(n.visibilityRange=t.visibilityRange,n.enabled=!1!==t.enabled):n.enabled=!1!==t.enabled,n.opacityConfig={mode:t.opacityMode||"static",value:void 0!==t.opacity?t.opacity:1},R.root.addChild(n),n}function Lt(t,o,n){const i=t.render;if(i&&i.meshInstances)for(const e of i.meshInstances)if(e.aabb){if(Tt(o,n,e.aabb))return!0}const a=t.model;if(a&&a.meshInstances)for(const e of a.meshInstances)if(e.aabb){if(Tt(o,n,e.aabb))return!0}const s=t.children;if(s)for(const t of s)if(t instanceof e.Entity&&Lt(t,o,n))return!0;return!1}function Tt(e,t,o){const n=o.getMin?o.getMin():o.min,i=o.getMax?o.getMax():o.max;if(!n||!i)return!1;let a=-1/0,s=1/0;for(let o=0;o<3;o++){const r=["x","y","z"][o],l=e[r],c=t[r],d=n[r]??n.data?.[o],p=i[r]??i.data?.[o];if(Math.abs(c)<1e-8){if(l<d||l>p)return!1}else{let e=(d-l)/c,t=(p-l)/c;if(e>t&&([e,t]=[t,e]),a=Math.max(a,e),s=Math.min(s,t),a>s)return!1}}return s>=0}function kt(e){if(!e.interaction)return;const t={id:`mesh-content-${e.id}`,title:e.interaction.title||e.name,information:e.interaction.information,photoUrl:e.interaction.photoUrl,iframeUrl:e.interaction.iframeUrl,externalLinkUrl:e.interaction.externalLinkUrl,externalLinkText:e.interaction.externalLinkText,backgroundColor:e.interaction.backgroundColor||"#000000",textColor:e.interaction.textColor||"#ffffff"};A.showHotspotPopup?A.showHotspotPopup(t):function(e){const t=document.querySelector(".storysplat-mesh-popup");t&&t.remove();const o=document.createElement("div");o.className="storysplat-mesh-popup",o.style.cssText="\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n background: rgba(0, 0, 0, 0.9);\n padding: 30px;\n border-radius: 12px;\n max-width: 600px;\n max-height: 80vh;\n overflow: auto;\n z-index: 100001;\n color: white;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n ";const n=document.createElement("h2");if(n.textContent=e.name||"Custom Mesh",n.style.cssText="margin-top: 0; margin-bottom: 16px;",o.appendChild(n),e.interaction.popupContent){const t=document.createElement("p");t.textContent=e.interaction.popupContent,t.style.cssText="margin: 0; line-height: 1.6;",o.appendChild(t)}const i=document.createElement("button");i.textContent="× Close",i.style.cssText="\n position: absolute;\n top: 10px;\n right: 10px;\n background: rgba(255, 255, 255, 0.2);\n border: none;\n color: white;\n padding: 8px 16px;\n border-radius: 6px;\n cursor: pointer;\n font-size: 16px;\n ",i.onclick=()=>o.remove(),o.appendChild(i),document.body.appendChild(o)}(e)}function At(e){e.interaction?.triggerDirectLink&&e.interaction.directLinkUrl&&window.open(e.interaction.directLinkUrl,"_blank")}function Pt(t,o){!function t(n){n.render&&n.render.meshInstances&&n.render.meshInstances.forEach(t=>{t.material&&(t.material._isCloned||(t.material=t.material.clone(),t.material._isCloned=!0),t.material.opacity=o,t.material.blendType=e.BLEND_PREMULTIPLIED,t.material.depthTest=!0,t.material.depthWrite=!0,t.material.alphaTest=.01,t.material.update())}),n.children.forEach(e=>t(e))}(t)}function zt(){const e=R.graphicsDevice.canvas;Et.forEach(t=>{const o=t.entity;o.meshClickHandler&&e.removeEventListener("click",o.meshClickHandler),o.destroy()}),Et.clear()}function Rt(t){const o=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);return o?new e.Color(parseInt(o[1],16)/255,parseInt(o[2],16)/255,parseInt(o[3],16)/255):new e.Color(1,1,1)}function It(){r.lights&&0!==r.lights.length?(console.log(`[StorySplat Viewer] Creating ${r.lights.length} custom lights...`),r.lights.forEach((t,o)=>{let n=null;switch(t.type){case"point":n=function(t){const o=new e.Entity(t.name||"Point Light");o.addComponent("light",{type:e.LIGHTTYPE_POINT,color:Rt(t.color||"#ffffff"),intensity:t.intensity||1,range:t.range||10,castShadows:t.castShadows||!1});const n=t.position;return n&&o.setPosition(n._x??n.x??0,n._y??n.y??0,-(n._z??n.z??0)),o.enabled=!1!==t.enabled,o}(t);break;case"directional":n=function(t){const o=new e.Entity(t.name||"Directional Light");o.addComponent("light",{type:e.LIGHTTYPE_DIRECTIONAL,color:Rt(t.color||"#ffffff"),intensity:t.intensity||1,castShadows:t.castShadows||!1});const n=t.position;n&&o.setPosition(n._x??n.x??0,n._y??n.y??0,-(n._z??n.z??0));const i=t.rotation;if(i){const e=180/Math.PI;o.setEulerAngles((i._x??i.x??0)*e,(i._y??i.y??0)*e,(i._z??i.z??0)*e)}else o.setEulerAngles(45,0,0);return o.enabled=!1!==t.enabled,o}(t);break;case"hemispheric":n=function(t){const o=new e.Entity(t.name||"Hemispheric Light");o.addComponent("light",{type:e.LIGHTTYPE_DIRECTIONAL,color:Rt(t.color||"#ffffff"),intensity:t.intensity||1,castShadows:!1});const n=t.position;if(n&&o.setPosition(n._x??n.x??0,n._y??n.y??0,-(n._z??n.z??0)),o.setEulerAngles(-90,0,0),o.enabled=!1!==t.enabled,t.groundColor){const o=Rt(t.groundColor),n=Rt(t.color||"#ffffff");R.scene.ambientLight=new e.Color((n.r+.3*o.r)/1.3,(n.g+.3*o.g)/1.3,(n.b+.3*o.b)/1.3)}return o}(t);break;case"ambient":!function(t){const o=Rt(t.color||"#404040"),n=t.intensity||.4;R.scene.ambientLight=new e.Color(o.r*n,o.g*n,o.b*n),console.log("[StorySplat Viewer] Set ambient light:",t.name||"Ambient Light")}(t);break;case"spot":n=function(t){const o=new e.Entity(t.name||"Spot Light"),n=180/Math.PI,i=(t.angle??45*Math.PI/180)*n;t.exponent;const a=t.innerConeAngle??t.innerAngle??.8*i,s=t.outerConeAngle??t.outerAngle??i;o.addComponent("light",{type:e.LIGHTTYPE_SPOT,color:Rt(t.color||"#ffffff"),intensity:t.intensity||1,range:t.range||10,innerConeAngle:a,outerConeAngle:s,castShadows:t.castShadows||!1,shadowBias:t.shadowBias??.05,normalOffsetBias:t.normalOffsetBias??.05});const r=t.position;r&&o.setPosition(r._x??r.x??0,r._y??r.y??0,-(r._z??r.z??0));const l=t.rotation;if(l){const e=180/Math.PI;o.setEulerAngles((l._x??l.x??0)*e,(l._y??l.y??0)*e,(l._z??l.z??0)*e)}else if(t.direction){const n=t.direction,i=new e.Vec3(n._x??n.x??0,n._y??n.y??-1,-(n._z??n.z??0)).normalize();o.lookAt(o.getPosition().x+i.x,o.getPosition().y+i.y,o.getPosition().z+i.z)}else o.setEulerAngles(90,0,0);return o.enabled=!1!==t.enabled,o}(t);break;default:return void console.warn("[StorySplat Viewer] Unknown light type:",t.type)}n&&(R.root.addChild(n),console.log(`[StorySplat Viewer] Created ${t.type} light:`,t.name||`Light ${o}`))}),console.log("[StorySplat Viewer] Lighting setup complete")):console.log("[StorySplat Viewer] No custom lights to create")}function Dt(e){switch(e){case"linear":return"linear";case"inverse":return"inverse";default:return"exponential"}}i.on("progressUpdate",()=>{!function(){const e=100*Re,t=r.waypoints?.length||1,o=Math.round(Re*Math.max(1,t-1));Et.forEach(t=>{const{entity:n,config:i}=t,a=n.visibilityRange;if(a){let t=!0;"waypoint"===a.type?t=o>=a.start&&o<=a.end:"percentage"===a.type&&(t=e>=a.start&&e<=a.end),n.enabled=t}if("animated"===i.opacityMode&&i.opacityAnimation){const t=i.opacityAnimation;if(e>=t.startPercent&&e<=t.endPercent){const o=(e-t.startPercent)/(t.endPercent-t.startPercent);Pt(n,t.startOpacity+(t.endOpacity-t.startOpacity)*o)}}if(i.billboard&&i.billboardRange){const t=i.billboardRange;let a=!1;"percentage"===t.type?a=e>=t.start&&e<=t.end:"waypoint"===t.type&&(a=o>=t.start&&o<=t.end),n._billboardActive=a}else i.billboard&&(n._billboardActive=!0)})}()});const Ft=new Map;const Ut=new Set;const Bt=[];function Vt(t,o=!1,n=1,i){const a=new e.StandardMaterial;if(a.blendType=e.BLEND_PREMULTIPLIED,a.depthTest=!0,a.depthWrite=!0,a.cull=e.CULLFACE_NONE,a.twoSidedLighting=!0,a.alphaTest=.01,o?a.diffuse=new e.Color(1,1,1):(a.diffuse=new e.Color(0,0,0),a.emissive=new e.Color(1,1,1),a.specular=new e.Color(0,0,0),a.useLighting=!1),a.opacity=n,a.update(),function(e){const t=e.toLowerCase();return t.endsWith(".gif")||t.includes("image/gif")||t.includes("format=gif")}(t)){console.log(`[Hotspot] Loading animated GIF: ${t}`);const n=new q(R,t,{autoPlay:!0,onReady:()=>{if(O)return console.log(`[Hotspot] Ignoring GIF load - viewer was destroyed: ${t}`),void n.destroy();n.texture&&(o?(a.diffuseMap=n.texture,a.opacityMap=n.texture):(a.emissiveMap=n.texture,a.opacityMap=n.texture),a.update(),console.log(`[Hotspot] GIF texture loaded: ${t}, useLighting=${o}`),i&&i())},onError:o=>{console.error(`[Hotspot] Failed to load GIF: ${t}`,o),a.opacity=.3,a.emissive=new e.Color(1,0,0),a.update(),i&&i()}});Bt.push(n)}else{const n=new Image;n.crossOrigin="anonymous",n.onload=()=>{if(O)return void console.log(`[Hotspot] Ignoring texture load - viewer was destroyed: ${t}`);const s=new e.Texture(R.graphicsDevice,{width:n.width,height:n.height,format:e.PIXELFORMAT_RGBA8,mipmaps:!0,minFilter:e.FILTER_LINEAR_MIPMAP_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE});s.setSource(n),o?(a.diffuseMap=s,a.opacityMap=s):(a.emissiveMap=s,a.opacityMap=s),a.update(),console.log(`[Hotspot] Texture loaded for: ${t}, useLighting=${o}`),i&&i()},n.onerror=o=>{console.error(`[Hotspot] Failed to load texture: ${t}`,o),a.opacity=.3,a.emissive=new e.Color(1,0,0),a.update(),i&&i()},n.src=t}return a}function $t(){r.hotspots&&0!==r.hotspots.length?(console.log(`[StorySplat Viewer] Creating ${r.hotspots.length} hotspots...`),r.hotspots.forEach((t,o)=>{const n=new e.Entity(`hotspot-${o}`),i=t.position||{_x:0,_y:0,_z:0};n.setPosition(i._x??i.x??0,i._y??i.y??0,-(i._z??i.z??0));const a=t.scale||{_x:1,_y:1,_z:1},s=Math.abs(a._x??a.x??1),r=Math.abs(a._y??a.y??1),l=a._z??a.z??1,c=t.rotation||{_x:0,_y:0,_z:0},d=180/Math.PI,p=(c._x??c.x??0)*d,h=(c._y??c.y??0)*d+180,u=(c._z??c.z??0)*d;if(n.setEulerAngles(p,h,u),"sphere"===t.type){n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=gt(t.color||"#ffffff");o.diffuse=i,o.emissive=i.clone(),o.emissive.mulScalar(.5);const a=t.opacity??.8;a>=.95?(o.opacity=1,o.blendType=e.BLEND_NONE,o.depthTest=!0,o.depthWrite=!0):(o.opacity=a,o.blendType=e.BLEND_ADDITIVEALPHA,o.depthTest=!0,o.depthWrite=!0),o.update(),n.render.material=o,n.setLocalScale(.2*s,.2*r,.2*l)}else if("image"===t.type&&t.imageUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});let e=1;"animated"===t.opacityMode&&t.opacityAnimation?e=void 0!==t.opacityAnimation.startOpacity?t.opacityAnimation.startOpacity:1:void 0!==t.opacity&&(e=t.opacity),n.targetOpacity=e,n.textureLoaded=!1,n.hiddenUntilTextureLoaded=!0,n.enabled=!1;const o=!0===t.useLighting,i=Vt(t.imageUrl,o,e,()=>{n.textureLoaded=!0,n.visibilityRange&&!n.shouldBeVisible||(n.enabled=!0),n.hiddenUntilTextureLoaded=!1});n.render.material=i,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.hotspotMaterial=i,console.log(`[Hotspot] Created image hotspot: ${t.title}, opacity=${e}, opacityMode=${t.opacityMode}, useLighting=${o}`)}else if("video"===t.type&&t.videoUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});const o=/iPad|iPhone|iPod/.test(navigator.userAgent)&&t.useIOSVideoAlphaMethod||t.forceIOSVideoAlphaMethodForAllDevices,i=o&&t.iosMainVideoUrl?t.iosMainVideoUrl:t.videoUrl,a=o&&t.alphaMaskVideoUrl||null,c=(e=>{const t=e.toLowerCase();return t.endsWith(".webm")||t.includes("format=webm")||t.includes("video/webm")})(i)&&!1!==t.webmHasAlpha,d=(o,n,i=!1)=>{const a=document.createElement("video");a.src=o,a.loop=!1!==t.videoLoop,a.crossOrigin="anonymous",a.playsInline=!0,a.muted=!!n||!1!==t.videoMuted,"autoplay"===t.mediaTriggerMode&&(a.autoplay=!0,a.muted=!0);const s=new e.Texture(R.graphicsDevice,{format:i?e.PIXELFORMAT_R8_G8_B8_A8:e.PIXELFORMAT_R8_G8_B8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE});return s.setSource(a),{video:a,texture:s}},p=d(i,!1,c),h=p.video,u=p.texture;t.videoBackupUrl&&(h.onerror=()=>{console.log(`[Hotspot] Main video failed, trying backup: ${t.videoBackupUrl}`),h.src=t.videoBackupUrl,h.load()});const m=new e.StandardMaterial;m.diffuseMap=u,m.emissiveMap=u,m.emissive=new e.Color(1,1,1),m.depthTest=!0,m.depthWrite=!0,m.cull=e.CULLFACE_NONE,m.twoSidedLighting=!0,m.blendType=e.BLEND_PREMULTIPLIED,m.alphaTest=.01,c&&(m.opacityMap=u,m.blendType=e.BLEND_PREMULTIPLIED,m.alphaTest=.01,console.log(`[Hotspot] WebM video with alpha enabled: ${t.title}`));let g=null,y=null;if(a){const o=d(a,!0,!1);g=o.video,y=o.texture,m.opacityMap=y,m.opacityMapChannel="r",m.blendType=e.BLEND_PREMULTIPLIED,m.alphaTest=.01,h.addEventListener("play",()=>{g&&g.paused&&(g.currentTime=h.currentTime,g.play().catch(console.warn))}),h.addEventListener("pause",()=>{g&&!g.paused&&g.pause()}),h.addEventListener("seeked",()=>{g&&(g.currentTime=h.currentTime)}),console.log(`[Hotspot] iOS alpha mask video enabled: ${t.title}, alphaUrl=${a.substring(0,50)}...`)}m.update(),R.on("update",()=>{h.readyState===h.HAVE_ENOUGH_DATA&&u.upload(),g&&y&&g.readyState===g.HAVE_ENOUGH_DATA&&y.upload()});const f={x:s,y:r,z:l};if(h.addEventListener("loadedmetadata",()=>{const e=h.videoWidth,t=h.videoHeight;if(e>0&&t>0){const o=e/t;1===f.x&&1===f.y&&n.setLocalScale(o*f.y,f.y,f.z)}}),n.render.material=m,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.videoElement=h,n.alphaVideoElement=g,n.hotspotMaterial=m,n.mediaTriggerMode=t.mediaTriggerMode||"click",n.proximityDistance=t.proximityDistance||5,n.isVideoPlaying=!1,!0!==t.videoMuted){const e=function(e,t,o){if(!o.videoSpatialAudio&&!1!==o.videoMuted)return null;try{const n=new(window.AudioContext||window.webkitAudioContext);yt.push(n);const i=n.createMediaElementSource(t),a=n.createPanner();a.panningModel="HRTF",a.distanceModel=o.videoDistanceModel||"linear",a.refDistance=void 0!==o.videoRefDistance?o.videoRefDistance:1,a.maxDistance=void 0!==o.videoMaxDistance?o.videoMaxDistance:100,a.rolloffFactor=void 0!==o.videoRolloffFactor?o.videoRolloffFactor:1;const s=e.getPosition();return a.setPosition(s.x,s.y,s.z),i.connect(a),a.connect(n.destination),R.on("update",()=>{if(!e||!e.getPosition)return;const t=e.getPosition();if(a.setPosition(t.x,t.y,t.z),le&&le.getPosition){const e=le.getPosition(),t=le.forward,o=le.up;n.listener.positionX?(n.listener.positionX.value=e.x,n.listener.positionY.value=e.y,n.listener.positionZ.value=e.z,n.listener.forwardX.value=t.x,n.listener.forwardY.value=t.y,n.listener.forwardZ.value=t.z,n.listener.upX.value=o.x,n.listener.upY.value=o.y,n.listener.upZ.value=o.z):(n.listener.setPosition(e.x,e.y,e.z),n.listener.setOrientation(t.x,t.y,t.z,o.x,o.y,o.z))}}),console.log(`[Audio] Video spatial audio setup for hotspot: ${o.title}, refDist=${a.refDistance}, maxDist=${a.maxDistance}`),{audioCtx:n,source:i,panner:a}}catch(e){return console.warn("[Audio] Failed to setup video spatial audio:",e),null}}(n,h,t);e&&(n.videoSpatialAudio=e)}console.log(`[Hotspot] Created video hotspot: ${t.title}, mode=${t.mediaTriggerMode}, useAlpha=${!!a}, spatialAudio=${!!n.videoSpatialAudio}`)}else if("gif"===t.type&&t.gifUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});let o=1;"animated"===t.opacityMode&&t.opacityAnimation?o=void 0!==t.opacityAnimation.startOpacity?t.opacityAnimation.startOpacity:1:void 0!==t.opacity&&(o=t.opacity),n.targetOpacity=o,n.textureLoaded=!1,n.hiddenUntilTextureLoaded=!0,n.enabled=!1;const i=!0===t.useLighting,a=new e.StandardMaterial;a.blendType=e.BLEND_PREMULTIPLIED,a.opacity=o,a.depthTest=!0,a.depthWrite=!0,a.cull=e.CULLFACE_NONE,a.twoSidedLighting=!0,a.alphaTest=.01,i||(a.emissive=new e.Color(1,1,1),a.diffuse=new e.Color(0,0,0));const c=async c=>{try{const d=document.createElement("canvas"),p=d.getContext("2d"),h=new Image;h.crossOrigin="anonymous",h.onload=()=>{d.width=h.width||256,d.height=h.height||256,p.drawImage(h,0,0);const u=new e.Texture(R.graphicsDevice,{format:e.PIXELFORMAT_R8_G8_B8_A8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE});u.setSource(d),a.diffuseMap=u,i||(a.emissiveMap=u),a.opacityMap=u,a.alphaTest=.01,a.update(),n.render.material=a;const m=d.width/d.height;1===s&&1===r?n.setLocalScale(m,1,l):n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.textureLoaded=!0,n.gifCanvas=d,n.gifTexture=u,n.hotspotMaterial=a,n.visibilityRange&&!n.shouldBeVisible||(n.enabled=!0),n.hiddenUntilTextureLoaded=!1,fetch(c).then(e=>e.arrayBuffer()).then(e=>{const t=()=>{n.enabled&&n.gifTexture&&(p.clearRect(0,0,d.width,d.height),p.drawImage(h,0,0),n.gifTexture.upload(),requestAnimationFrame(t))};n.gifAnimationFrame=requestAnimationFrame(t)}).catch(e=>{console.warn("[Hotspot] GIF animation load failed, using static frame:",e)}),console.log(`[Hotspot] Created GIF hotspot: ${t.title}, opacity=${o}, useLighting=${i}`)},h.onerror=o=>{console.error("[Hotspot] Failed to load GIF:",t.gifUrl,o),n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const i=new e.StandardMaterial;i.diffuse=gt(t.color||"#FF00FF"),i.opacity=.8,i.blendType=e.BLEND_NORMAL,i.update(),n.render.material=i,n.setLocalScale(.2*s,.2*r,.2*l),n.enabled=!0,n.hiddenUntilTextureLoaded=!1},h.src=c}catch(e){console.error("[Hotspot] GIF hotspot creation failed:",e)}};c(t.gifUrl)}else{n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=gt(t.color||"#4CAF50");o.diffuse=i,o.emissive=i.clone(),o.emissive.mulScalar(.5);const a=t.opacity??.8;o.opacity=a,o.blendType=e.BLEND_ADDITIVEALPHA,o.depthTest=!0,o.depthWrite=!0,o.update(),n.render.material=o,n.setLocalScale(.2*s,.2*r,.2*l)}n.addComponent("collision",{type:"sphere"===t.type?"sphere":"box",radius:.1,halfExtents:new e.Vec3(.5,.5,.05)}),n.hotspotData=t;const m=function(e,t){if(!t.audioUrl)return null;const o=document.createElement("audio");if(o.src=t.audioUrl,o.loop=t.audioLoop||!1,o.volume=void 0!==t.audioVolume?t.audioVolume:1,o.crossOrigin="anonymous",t.audioSpatial){const n=new(window.AudioContext||window.webkitAudioContext);yt.push(n);const i=n.createMediaElementSource(o),a=n.createPanner();a.panningModel="HRTF",a.distanceModel=t.audioDistanceModel||"linear",a.refDistance=void 0!==t.audioRefDistance?t.audioRefDistance:1,a.maxDistance=void 0!==t.audioMaxDistance?t.audioMaxDistance:100,a.rolloffFactor=void 0!==t.audioRolloffFactor?t.audioRolloffFactor:1;const s=e.getPosition();a.setPosition(s.x,s.y,s.z),i.connect(a),a.connect(n.destination);const r=()=>{if(!e||!e.getPosition)return;const t=e.getPosition();if(a.setPosition(t.x,t.y,t.z),le&&le.getPosition){const e=le.getPosition(),t=le.forward,o=le.up;n.listener.positionX?(n.listener.positionX.value=e.x,n.listener.positionY.value=e.y,n.listener.positionZ.value=e.z,n.listener.forwardX.value=t.x,n.listener.forwardY.value=t.y,n.listener.forwardZ.value=t.z,n.listener.upX.value=o.x,n.listener.upY.value=o.y,n.listener.upZ.value=o.z):(n.listener.setPosition(e.x,e.y,e.z),n.listener.setOrientation(t.x,t.y,t.z,o.x,o.y,o.z))}};return R.on("update",r),console.log(`[Audio] Spatial audio setup for hotspot: ${t.title}, refDist=${a.refDistance}, maxDist=${a.maxDistance}`),{audio:o,audioCtx:n,source:i,panner:a,updateAudioPosition:r}}return console.log(`[Audio] Non-spatial audio setup for hotspot: ${t.title}`),{audio:o}}(n,t);if(m&&(n.audioElements=m,console.log(`[StorySplat Viewer] Audio setup for hotspot: ${t.title||"Untitled"}`)),t.billboard){const e=n.getEulerAngles().clone(),o=void 0!==t.billboardRangeStart||void 0!==t.billboardRangeEnd;n._billboardActive=!o,n._billboardOriginalRotation=e,R.on("update",()=>{n._billboardActive&&(n.lookAt(le.getPosition()),n.rotateLocal(90,180,0))})}t.visibilityRange&&(n.visibilityRange=t.visibilityRange,n.enabled=!1),R.root.addChild(n),ut.push(n),console.log(`[StorySplat Viewer] Created hotspot: ${t.title||"Untitled"}`)})):console.log("[StorySplat Viewer] No hotspots to create")}function Ht(){const e=100*Re,t=r.waypoints?.length||1,o=Math.round(Re*Math.max(1,t-1)),n=le.getPosition();ut.forEach(t=>{const i=t.hotspotData;if(!i)return;let a=!0;if(i.alwaysVisible)a=!0;else{const n=t.visibilityRange;n&&("waypoint"===n.type?a=o>=n.start&&o<=n.end:"percentage"===n.type&&(a=e>=n.start&&e<=n.end))}if(t.shouldBeVisible=a,i.billboard&&(void 0!==i.billboardRangeStart||void 0!==i.billboardRangeEnd)){const o=i.billboardRangeStart??0,n=i.billboardRangeEnd??100,a=e>=o&&e<=n;if(t._billboardActive=a,!a&&t._billboardOriginalRotation){const e=t._billboardOriginalRotation;t.setEulerAngles(e.x,e.y,e.z)}}if(t.hiddenUntilTextureLoaded?t.enabled=!1:t.enabled=a,t.videoElement&&"video"===i.type){const o=t.mediaTriggerMode||"click";if("proximity"===o){const e=t.getPosition(),o=n.distance(e),a=t.proximityDistance||5;o<=a&&!t.isVideoPlaying?(Ot(t,i),console.log(`[Hotspot] Proximity play: ${i.title}, distance=${o.toFixed(2)}`)):o>a&&t.isVideoPlaying&&(Wt(t),console.log(`[Hotspot] Proximity pause: ${i.title}, distance=${o.toFixed(2)}`))}"scroll"===o&&(a&&!t.isVideoPlaying?(Ot(t,i),console.log(`[Hotspot] Scroll play: ${i.title}, scroll=${e.toFixed(1)}%`)):!a&&t.isVideoPlaying&&(Wt(t),console.log(`[Hotspot] Scroll pause: ${i.title}, scroll=${e.toFixed(1)}%`)))}if("animated"===i.opacityMode&&i.opacityAnimation){if("image"===i.type&&!t.textureLoaded)return;const o=i.opacityAnimation,n=o.startPercent??0,a=o.endPercent??100,s=void 0!==o.startOpacity?o.startOpacity:1,r=void 0!==o.endOpacity?o.endOpacity:1;let l;if(e<=n)l=s;else if(e>=a)l=r;else{l=s+(r-s)*((e-n)/(a-n))}l=Math.max(0,Math.min(1,l)),t.hotspotMaterial?(t.hotspotMaterial.opacity=l,t.hotspotMaterial.update()):t.render&&t.render.material&&(t.render.material.opacity=l,t.render.material.update())}})}function Ot(e,t){const o=e.videoElement,n=e.alphaVideoElement;if(o){if("autoplay"!==e.mediaTriggerMode&&(o.muted=!1!==t.videoMuted),e.videoSpatialAudio&&e.videoSpatialAudio.audioCtx){const t=e.videoSpatialAudio.audioCtx;"suspended"===t.state&&t.resume().then(()=>{console.log("[Audio] Video spatial audio context resumed")}).catch(e=>console.warn("[Audio] Failed to resume video audio context:",e))}o.play().catch(e=>console.warn("Video play failed:",e)),n&&n.play().catch(e=>console.warn("Alpha video play failed:",e)),e.isVideoPlaying=!0}}function Wt(e){const t=e.videoElement,o=e.alphaVideoElement;t&&(t.pause(),o&&o.pause(),e.isVideoPlaying=!1)}function Gt(){const e=100*Re,t=r.waypoints?.length||1,o=Math.round(Re*Math.max(1,t-1)),n=le.getPosition();mt.forEach(t=>{const i=t.portalData;if(!i)return;let a=!0;const s=t.visibilityRange;if(s&&("waypoint"===s.type?a=o>=s.start&&o<=s.end:"percentage"===s.type&&(a=e>=s.start&&e<=s.end)),t.shouldBeVisible=a,t.hiddenUntilTextureLoaded?t.enabled=!1:t.enabled=a,"proximity"===i.activationMode&&a){const e=t.getPosition(),o=n.distance(e),a=i.proximityDistance||2;o<=a&&!t.proximityTriggered?(t.proximityTriggered=!0,console.log(`[Portal] Proximity triggered: ${i.title||i.targetSceneName}, navigating to scene ${i.targetSceneId}`),jt(i)):o>a&&(t.proximityTriggered=!1)}})}function Nt(t,o){const n=le.camera.screenToWorld(t,o,le.camera.nearClip),i=le.camera.screenToWorld(t,o,le.camera.farClip);let a=null;if(mt.forEach(t=>{if(!t.enabled)return;const o=t.getPosition(),s=(new e.Vec3).sub2(i,n).normalize(),r=(new e.Vec3).sub2(o,n).dot(s);if(r<0)return;const l=(new e.Vec3).add2(n,s.clone().mulScalar(r)).distance(o),c=t.getLocalScale();l<.6*Math.max(c.x,c.y,.3)&&(!a||r<a.distance)&&(a={entity:t,distance:r})}),null!==a){const e=a.entity;return{entity:e,portal:e.portalData}}return null}async function jt(e){if(e.targetSceneId){console.log(`[StorySplat Viewer] Portal activated: navigating to scene ${e.targetSceneId}`),i.emit("portalActivated",{portalId:e.id,targetSceneId:e.targetSceneId,targetSceneName:e.targetSceneName}),function(e,t){const o=e.querySelector(".storysplat-portal-loading");o&&o.remove();const n=document.createElement("div");n.className="storysplat-portal-loading";const i=document.createElement("div");i.className="storysplat-portal-spinner";const a=document.createElement("div");if(a.className="storysplat-portal-loading-text",a.textContent=`Loading ${t}...`,n.appendChild(i),n.appendChild(a),Object.assign(n.style,{position:"fixed",top:"0",left:"0",width:"100%",height:"100%",background:"rgba(0, 0, 0, 0.85)",display:"flex",flexDirection:"column",alignItems:"center",justifyContent:"center",zIndex:"10000",fontFamily:"system-ui, sans-serif"}),Object.assign(i.style,{width:"50px",height:"50px",border:"4px solid rgba(255, 255, 255, 0.2)",borderTop:"4px solid #9C27B0",borderRadius:"50%",animation:"storysplat-portal-spin 1s linear infinite",marginBottom:"20px"}),Object.assign(a.style,{color:"#ffffff",fontSize:"18px"}),!document.getElementById("storysplat-portal-styles")){const e=document.createElement("style");e.id="storysplat-portal-styles",e.textContent="\n @keyframes storysplat-portal-spin {\n 0% { transform: rotate(0deg); }\n 100% { transform: rotate(360deg); }\n }\n ",document.head.appendChild(e)}e.appendChild(n)}(t,e.targetSceneName||e.targetSceneId);try{const o=`https://discover.storysplat.com/api/scene/${e.targetSceneId}`;console.log(`[Portal] Fetching scene from: ${o}`);const n=await fetch(o);if(!n.ok)throw new Error(`Failed to fetch scene: ${n.status} ${n.statusText}`);const i=await n.json(),a=i.data||i;!function(){console.log("[Portal] Cleaning up current scene for navigation..."),pt(),ut.forEach(e=>{e.videoElement&&(e.videoElement.pause(),e.videoElement.src=""),e.alphaVideoElement&&(e.alphaVideoElement.pause(),e.alphaVideoElement.src="")}),Bt.forEach(e=>e.destroy()),Bt.length=0,zt(),ut.forEach(e=>{e.destroy()}),ut.length=0,mt.forEach(e=>{e.destroy()}),mt.length=0,$&&($.destroy(),$=null);R.__htmlMeshManager&&R.__htmlMeshManager.destroy();R.__customScriptSystem&&R.__customScriptSystem.dispose();console.log("[Portal] Cleanup complete")}(),await new Promise(e=>setTimeout(e,100)),z&&z.parentNode&&z.remove();const s=await te(t,a,{});return qt(t),console.log(`[Portal] Successfully navigated to scene: ${e.targetSceneId}`),s}catch(e){console.error("[Portal] Navigation failed:",e),qt(t);const o=document.createElement("div");o.className="storysplat-portal-error",o.textContent=`Failed to load scene: ${e.message}`,Object.assign(o.style,{position:"fixed",top:"50%",left:"50%",transform:"translate(-50%, -50%)",background:"rgba(0,0,0,0.9)",color:"#ff6b6b",padding:"20px",borderRadius:"8px",zIndex:"10000",fontFamily:"system-ui, sans-serif"}),t.appendChild(o),setTimeout(()=>o.remove(),3e3)}}else console.warn("[Portal] No target scene ID specified")}function qt(e){const t=e.querySelector(".storysplat-portal-loading");t&&t.remove()}i.on("progressUpdate",()=>{Ht()}),setTimeout(()=>{Ht()},100),i.on("progressUpdate",()=>{Gt()}),setTimeout(()=>{Gt()},100);const Yt=R.graphicsDevice.canvas;function Xt(t,o){const n=le.camera.screenToWorld(t,o,le.camera.nearClip),i=le.camera.screenToWorld(t,o,le.camera.farClip);let a=null;if(ut.forEach(t=>{if(!t.enabled)return;const o=t.getPosition(),s=(new e.Vec3).sub2(i,n).normalize(),r=(new e.Vec3).sub2(o,n).dot(s);if(r<0)return;const l=(new e.Vec3).add2(n,s.clone().mulScalar(r)).distance(o),c=t.getLocalScale();l<.6*Math.max(c.x,c.y,.3)&&(!a||r<a.distance)&&(a={entity:t,distance:r})}),null!==a){const e=a.entity;return{entity:e,hotspot:e.hotspotData}}return null}let Zt=null,Jt=!1;const Kt=t.querySelector(".storysplat-hotspot-popup"),Qt=t.querySelector(".storysplat-hotspot-overlay");async function eo(e,t){if("explore"!==me)return;console.log("[StorySplat Viewer] Double-click focus at:",e,t);const o=Xt(e,t);if(o){const e=o.entity.getPosition();return console.log("[StorySplat Viewer] Focusing on hotspot at:",e.x,e.y,e.z),void he.focus(e,!1)}try{const o=.25;ye.resize(Math.floor(Yt.clientWidth*o),Math.floor(Yt.clientHeight*o));const n=R.scene.layers.getLayerByName("World");if(!n)return void console.warn("[StorySplat Viewer] World layer not found");ye.prepare(le.camera,R.scene,[n]);const i=Math.floor(e*o),a=Math.floor(t*o),s=await ye.getWorldPointAsync(i,a);if(s){const e=le.getPosition().distance(s);if(e>.1&&e<1e3)return console.log("[StorySplat Viewer] Focusing on GSplat at:",s.x.toFixed(2),s.y.toFixed(2),s.z.toFixed(2),"distance:",e.toFixed(2)),void he.focus(s,!1)}const r=await ye.getSelectionAsync(i,a,1,1);if(r.length>0){const e=r[0].aabb.center.clone();console.log("[StorySplat Viewer] Focusing on mesh AABB center:",e.x.toFixed(2),e.y.toFixed(2),e.z.toFixed(2)),he.focus(e,!1)}else console.log("[StorySplat Viewer] No pick result at click point")}catch(e){console.warn("[StorySplat Viewer] Picking failed:",e)}}Kt&&(Kt.addEventListener("mouseenter",()=>{Jt=!0}),Kt.addEventListener("mouseleave",()=>{Jt=!1,Zt&&"hover"===Zt.activationMode&&(Kt.classList.remove("visible"),Qt&&Qt.classList.remove("visible"),Zt=null)})),Yt.addEventListener("mousemove",e=>{const o=Yt.getBoundingClientRect(),n=e.clientX-o.left,i=e.clientY-o.top,a=Nt(n,i);if(a&&a.portal){const e=a.portal.activationMode||"click";return void(Yt.style.cursor="click"===e?"pointer":"default")}const s=Xt(n,i);if(s&&s.hotspot){const e=s.hotspot;"click"===e.activationMode||"hover"===e.activationMode||"video"===e.type?Yt.style.cursor="pointer":Yt.style.cursor="default","hover"===e.activationMode&&Zt!==e&&(Zt=e,(e.information||e.photoUrl||e.iframeUrl||e.externalLinkUrl)&&u(t,e))}else if(Yt.style.cursor="default",Zt&&"hover"===Zt.activationMode&&!Jt){const e=t.querySelector(".storysplat-hotspot-popup"),o=t.querySelector(".storysplat-hotspot-overlay");e&&e.classList.remove("visible"),o&&o.classList.remove("visible"),Zt=null}}),Yt.addEventListener("click",o=>{const n=Yt.getBoundingClientRect(),i=o.clientX-n.left,a=o.clientY-n.top,s=Nt(i,a);if(null!==s&&s.portal){const e=s.portal;console.log("[StorySplat Viewer] Portal clicked:",e.title||e.targetSceneName);return void("click"===(e.activationMode||"click")&&jt(e))}const l=Xt(i,a);if(null!==l){const o=l.entity,n=l.hotspot;if(console.log("[StorySplat Viewer] Hotspot clicked:",n.title),o.audioElements&&o.audioElements.audio){const e=o.audioElements,t=e.audio;t.paused?(e.audioCtx&&"suspended"===e.audioCtx.state&&e.audioCtx.resume(),t.play().catch(e=>console.error("[Audio] Hotspot audio play failed:",e)),console.log("[Audio] Hotspot audio started:",n.title)):(t.pause(),console.log("[Audio] Hotspot audio paused:",n.title))}if(o.videoElement&&("click"===o.mediaTriggerMode||"autoplay"===o.mediaTriggerMode)){const e=o.videoElement;o.alphaVideoElement,"autoplay"===o.mediaTriggerMode&&!e.paused&&e.muted?(e.muted=(n.videoMuted,!1),console.log("[Hotspot] Video unmuted by click")):e.paused?(Ot(o,n),console.log("[Hotspot] Video started")):(Wt(o),console.log("[Hotspot] Video paused"))}const i=n.activationMode||"click",a=n.title||n.information||n.photoUrl||n.iframeUrl||n.externalLinkUrl;"click"===i&&a&&(fe?xe?Se():function(t){if(!fe)return;be||(be=new e.Entity("arContentPlane"),be.addComponent("render",{type:"plane"}),be.setLocalScale(.8,1,.45),R.root.addChild(be));const o=512,n=288,i=document.createElement("canvas");i.width=o,i.height=n;const a=i.getContext("2d"),s=t.backgroundColor||"rgba(20, 20, 20, 0.95)";a.fillStyle=s,a.fillRect(0,0,o,n),a.strokeStyle="rgba(255, 255, 255, 0.3)",a.lineWidth=2,a.strokeRect(1,1,510,286);const r=t.textColor||"#ffffff";let l=35;t.title&&(a.fillStyle=r,a.font="bold 28px Arial, sans-serif",a.fillText(t.title,20,l),l+=40),t.information&&(a.fillStyle=r,a.font="18px Arial, sans-serif",l=function(e,t,o,n,i,a){const s=t.split(" ");let r="",l=n;for(const t of s){const n=r+t+" ";e.measureText(n).width>i&&r?(e.fillText(r.trim(),o,l),r=t+" ",l+=a):r=n}return e.fillText(r.trim(),o,l),l+a}(a,t.information,20,l,472,24)),a.fillStyle="rgba(255, 255, 255, 0.5)",a.font="14px Arial, sans-serif",a.textAlign="center",a.fillText("Tap to close",256,273),a.textAlign="left",we||(we=new e.Texture(R.graphicsDevice,{width:o,height:n,format:e.PIXELFORMAT_RGBA8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR})),we.setSource(i);const c=new e.StandardMaterial;c.diffuse=new e.Color(0,0,0),c.diffuseMap=we,c.emissive=new e.Color(1,1,1),c.emissiveMap=we,c.useLighting=!1,c.blendType=e.BLEND_NORMAL,c.cull=e.CULLFACE_NONE,c.depthWrite=!0,c.update(),be.render&&be.render.meshInstances[0]&&(be.render.meshInstances[0].material=c),be.enabled=!0,xe=!0,console.log("[StorySplat Viewer] AR content displayed for hotspot:",t.title)}(n):u(t,n));const s=n.teleportWaypoint??n.teleportToWaypoint,c=n.teleportPercent??n.teleportToPercent,d=n.teleportMode||"animate";let p=null;if(void 0!==s&&-1!==s){const e=r.waypoints?.length||1,t=Math.max(0,Math.min(s,e-1));p=e>1?t/(e-1):0,console.log("[Hotspot] Teleporting to waypoint:",t,"(progress:",p,", mode:",d,")")}else void 0!==c&&-1!==c&&(p=Math.max(0,Math.min(c/100,1)),console.log("[Hotspot] Teleporting to percent:",c,"(progress:",p,", mode:",d,")"));null!==p&&("instant"===d?(Re=p,et(Re)):nt(p,800))}}),Yt.addEventListener("dblclick",e=>{const t=Yt.getBoundingClientRect();eo(e.clientX-t.left,e.clientY-t.top)});let to=0;Yt.addEventListener("touchend",e=>{if(1!==e.changedTouches.length)return;const t=Date.now();if(t-to<300){const t=e.changedTouches[0],o=Yt.getBoundingClientRect();eo(t.clientX-o.left,t.clientY-o.top),to=0}else to=t}),Me(.2,"Initializing...");const oo=o.frameSequence&&o.frameSequence.frameUrls&&o.frameSequence.frameUrls.length>0?async function(){if(!o.frameSequence||!o.frameSequence.frameUrls||0===o.frameSequence.frameUrls.length)throw new Error("No frame sequence URLs provided");console.log("[StorySplat Viewer] Loading 4DGS frame sequence:",o.frameSequence.frameUrls.length,"frames"),Me(.3,"Loading 4DGS frames..."),W=new Z(R,{frameUrls:o.frameSequence.frameUrls,fps:o.frameSequence.fps||24,loop:!1!==o.frameSequence.loop,preloadCount:o.frameSequence.preloadCount||5,autoplay:o.frameSequence.autoplay||!1},{onFrameChange:(e,t)=>{i.emit("frameChange",e,t)},onLoadProgress:(e,t)=>{Me(.3+e/t*.6,`Loading frames... ${e}/${t}`)},onError:e=>{console.error("[StorySplat Viewer] Frame sequence error:",e),i.emit("error",new Error(e))}}),R.on("update",e=>{W&&!O&&W.update(e)}),console.log("[StorySplat Viewer] 4DGS frame sequence player initialized"),i.emit("loaded")}:async function(){const t=[];console.log("[SPLAT] Building URL priority list..."),console.log("[SPLAT] Available URLs:",{lodMetaUrl:r.lodMetaUrl||"(none)",sogUrl:r.sogUrl||"(none)",splatUrl:r.splatUrl||"(none)",fallbackUrls:r.fallbackUrls?.length||0}),r.lodMetaUrl&&(t.push(r.lodMetaUrl),console.log("[SPLAT] ✓ LOD streaming URL added (highest priority):",r.lodMetaUrl)),r.sogUrl&&(t.push(r.sogUrl),console.log("[SPLAT] ✓ SOG URL added (second priority):",r.sogUrl)),r.splatUrl&&(t.push(r.splatUrl),console.log("[SPLAT] ✓ Original splat URL added (third priority):",r.splatUrl)),r.fallbackUrls&&(t.push(...r.fallbackUrls),console.log("[SPLAT] ✓ Fallback URLs added:",r.fallbackUrls)),console.log("[SPLAT] Final URL priority order:",t),console.log("[SPLAT] Will try URLs in order: LOD streaming > SOG > PLY/Other"),Me(.3,"Loading splat...");for(const a of t)if(a)try{const t=a.split(".").pop()?.toLowerCase()||"splat",s="gsplat",l=a.includes("lod-meta.json"),c=a.includes(".sog")||l;console.log("[SPLAT] Attempting to load URL:",a),console.log("[SPLAT] Format detection:",{extension:t,isLodStreaming:l,isSogFormat:c,assetType:s});const d=new e.Asset("splat-"+Date.now(),s,{url:a});return d.on("progress",(e,t)=>{if(t>0){const o=.3+e/t*.6,n=Math.round(e/t*100);n%25!=0&&100!==n||console.log(`[SPLAT] Loading progress: ${n}% (${(e/1024/1024).toFixed(2)}MB / ${(t/1024/1024).toFixed(2)}MB)`),Me(o,`Loading... ${n}%`)}}),await new Promise((t,p)=>{let h=!1;const u=c?e=>{if(h)return;const t=e.reason?.message||String(e.reason);(t.includes("shape")||t.includes("upgradeMeta"))&&(console.warn("[SPLAT] ⚠️ Parse error detected (likely deprecated v1 format):",t),h=!0,e.preventDefault(),i.emit("warning",{type:"deprecated_format",message:"This scene uses an outdated SOG format. Please re-upload your scene with the latest tools for better performance.",details:"SOG v1 format is deprecated. Use splat-transform v2+ to convert your PLY files.",url:a}),console.log("[SPLAT] Will try fallback URL if available..."),p(new Error(`SOG parse error: ${t}`)))}:null;c&&u&&(console.log("[SPLAT] Registered error handler for SOG format parsing"),window.addEventListener("unhandledrejection",u));const m=()=>{c&&u&&window.removeEventListener("unhandledrejection",u)};d.ready(()=>{if(O)return console.log("[SPLAT] Ignoring load - viewer was destroyed during loading"),m(),void p(new Error("Viewer destroyed"));console.log("[SPLAT] ✓ Asset loaded and resources ready");try{$=new e.Entity("splat"),$.addComponent("gsplat",{asset:d,unified:!0});const i=$.gsplat;i&&ee(a)?(i.lodDistances=U.lodDistances,console.log("[SPLAT] ✓ LOD streaming enabled for this splat"),console.log("[SPLAT] LOD distances configured:",{distances:U.lodDistances,description:"Quality levels switch at these camera distances",preset:F})):c?console.log("[SPLAT] ✓ Single SOG file loaded (no LOD streaming)"):console.log("[SPLAT] Standard format loaded (PLY/SPLAT)");const s=r.scale||{x:1,y:1,z:1},l=r.invertXScale||!1,p=r.invertYScale||!1,u={x:l?-s.x:s.x,y:p?-s.y:s.y,z:l!==p?-s.z:s.z};$.setLocalScale(u.x,u.y,u.z);const g=r.position||[0,0,0];$.setPosition(g[0],g[1],-g[2]);const y=r.rotation||[0,0,0],f=0!==y[0]||0!==y[1]||0!==y[2];let v;v=f?[y[0]*(180/Math.PI),y[1]*(180/Math.PI),-y[2]*(180/Math.PI)]:[180,0,0],$.setEulerAngles(v[0],v[1],v[2]),console.log("[StorySplat Viewer] Splat transform applied:",{scale:u,position:[g[0],g[1],-g[2]],rotation:v,hasUserRotation:f,invertXScale:l,invertYScale:p}),R.root.addChild($),console.log("[StorySplat Viewer] Splat entity added to scene"),$.gsplat?.material&&($.gsplat.material.setParameter("alphaClip",.1),console.log("[StorySplat Viewer] GSplat alphaClip set for picking support"));const b=n.revealEffect||o.revealEffect||"medium",w=P(b);if(w){$.addComponent("script");const e=k();H=$.script?.create(e),H&&(H.enabled=!1,H.center.set(0,0,0),H.speed=w.speed,H.acceleration=w.acceleration,H.delay=w.delay,H.oscillationIntensity=w.oscillationIntensity,H.dotTint.set(w.dotTint.r,w.dotTint.g,w.dotTint.b),H.waveTint.set(w.waveTint.r,w.waveTint.g,w.waveTint.b),H.endRadius=w.endRadius,console.log("[StorySplat Viewer] Reveal effect configured (waiting to start):",b))}else console.log("[StorySplat Viewer] Reveal effect disabled");setTimeout(()=>{h||(m(),t())},100)}catch(e){console.error("[StorySplat Viewer] Error during gsplat setup:",e),m(),p(e)}}),d.on("error",e=>{console.error("[SPLAT] ✗ Asset load error:",{url:a,assetType:s,isSogFormat:c,isLodFormat:l,error:e,message:e?.message||"Unknown error",status:e?.status||e?.statusCode||"N/A"}),m(),p(e)}),R.assets.add(d),R.assets.load(d)}),i.emit("loaded"),console.log("[SPLAT] ✓✓✓ SPLAT LOADED SUCCESSFULLY ✓✓✓"),void console.log("[SPLAT] Load summary:",{url:a,format:l?"LOD streaming (lod-meta.json)":c?"SOG (compressed)":"Standard (PLY/SPLAT)",lodEnabled:l,lodPreset:l?F:"N/A"})}catch(e){console.warn("[SPLAT] ✗ Failed to load URL, trying next fallback:",a),console.warn("[SPLAT] Error:",e)}console.error("[SPLAT] ✗✗✗ FAILED TO LOAD SPLAT FROM ANY URL ✗✗✗"),console.error("[SPLAT] Tried URLs:",t),i.emit("error",new Error("Failed to load splat from any URL"))};oo().then(()=>{if(Me(1,"Ready!"),$t(),r.portals&&0!==r.portals.length?(console.log(`[StorySplat Viewer] Creating ${r.portals.length} portals...`),r.portals.forEach((t,o)=>{const n=new e.Entity(`portal-${o}`),i=t.position||{_x:0,_y:0,_z:0};n.setPosition(i._x??i.x??0,i._y??i.y??0,-(i._z??i.z??0));const a=t.scale||{_x:1,_y:1,_z:1},s=Math.abs(a._x??a.x??1),r=Math.abs(a._y??a.y??1),l=a._z??a.z??1,c=t.rotation||{_x:0,_y:0,_z:0},d=180/Math.PI,p=(c._x??c.x??0)*d,h=(c._y??c.y??0)*d+180,u=(c._z??c.z??0)*d;if(n.setEulerAngles(p,h,u),"sphere"===t.type){n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=gt(t.color||"#9C27B0");o.diffuse=i,o.emissive=i.clone(),o.emissive.mulScalar(.5);const a=t.opacity??.8;a>=.95?(o.opacity=1,o.blendType=e.BLEND_NONE,o.depthTest=!0,o.depthWrite=!0):(o.opacity=a,o.blendType=e.BLEND_ADDITIVEALPHA,o.depthTest=!0,o.depthWrite=!0),o.update(),n.render.material=o,n.setLocalScale(.2*s,.2*r,.2*l)}else if("image"===t.type&&t.imageUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});const e=!0===t.useLighting,o=t.opacity??1;n.textureLoaded=!1,n.hiddenUntilTextureLoaded=!0,n.enabled=!1;const i=Vt(t.imageUrl,e,o,()=>{n.textureLoaded=!0,n.visibilityRange&&!n.shouldBeVisible||(n.enabled=!0),n.hiddenUntilTextureLoaded=!1});n.render.material=i,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.portalMaterial=i,console.log(`[Portal] Created image portal: ${t.title||t.targetSceneName||"Untitled"}`)}else if("video"===t.type&&t.videoUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});const o=document.createElement("video");o.src=t.videoUrl,o.loop=!0,o.muted=!0,o.crossOrigin="anonymous",o.playsInline=!0,o.autoplay=!0;const i=new e.Texture(R.graphicsDevice,{format:e.PIXELFORMAT_R8_G8_B8,mipmaps:!1,minFilter:e.FILTER_LINEAR,magFilter:e.FILTER_LINEAR,addressU:e.ADDRESS_CLAMP_TO_EDGE,addressV:e.ADDRESS_CLAMP_TO_EDGE});i.setSource(o);const a=new e.StandardMaterial;a.diffuseMap=i,a.emissiveMap=i,a.emissive=new e.Color(1,1,1),a.depthTest=!0,a.depthWrite=!0,a.cull=e.CULLFACE_NONE,a.blendType=e.BLEND_PREMULTIPLIED,a.opacity=t.opacity??1,a.update(),n.render.material=a,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),R.on("update",()=>{o.readyState>=o.HAVE_CURRENT_DATA&&i.setSource(o)}),o.play().catch(e=>console.log("[Portal] Video autoplay blocked:",e)),n.videoElement=o,n.portalMaterial=a,console.log(`[Portal] Created video portal: ${t.title||t.targetSceneName||"Untitled"}`)}else if("gif"===t.type&&t.gifUrl){n.addComponent("render",{type:"plane",castShadows:!1,receiveShadows:!1});const e=!0===t.useLighting,o=t.opacity??1,i=Vt(t.gifUrl,e,o,()=>{n.textureLoaded=!0,n.visibilityRange&&!n.shouldBeVisible||(n.enabled=!0),n.hiddenUntilTextureLoaded=!1});n.render.material=i,n.setLocalScale(s,r,l),n.rotateLocal(90,180,0),n.textureLoaded=!1,n.hiddenUntilTextureLoaded=!0,n.enabled=!1,n.portalMaterial=i,console.log(`[Portal] Created GIF portal: ${t.title||t.targetSceneName||"Untitled"}`)}else{n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=gt(t.color||"#9C27B0");o.diffuse=i,o.emissive=i.clone(),o.emissive.mulScalar(.5);const a=t.opacity??.8;o.opacity=a,o.blendType=e.BLEND_ADDITIVEALPHA,o.depthTest=!0,o.depthWrite=!0,o.update(),n.render.material=o,n.setLocalScale(.2*s,.2*r,.2*l)}n.addComponent("collision",{type:"sphere"===t.type?"sphere":"box",radius:.1,halfExtents:new e.Vec3(.5,.5,.05)}),n.portalData=t,t.billboard&&R.on("update",()=>{n.enabled&&(n.lookAt(le.getPosition()),n.rotateLocal(90,180,0))}),t.visibilityRange&&(n.visibilityRange=t.visibilityRange,n.enabled=!1),R.root.addChild(n),mt.push(n),console.log(`[StorySplat Viewer] Created portal: ${t.title||t.targetSceneName||"Untitled"} -> ${t.targetSceneId}`)})):console.log("[StorySplat Viewer] No portals to create"),r.waypoints&&0!==r.waypoints.length&&(r.waypoints.forEach((t,o)=>{t.interactions&&Array.isArray(t.interactions)&&t.interactions.forEach(n=>{if("audio"===n.type&&n.data){const i=n.data,a=n.id,s=new e.Entity(`waypoint-audio-${a}`),r=t.position||{x:0,y:0,z:0};s.setPosition(r._x??r.x??t.x??0,r._y??r.y??t.y??1.6,-(r._z??r.z??t.z??0));const l={slots:{[a]:{name:a,loop:i.loop||!1,autoPlay:!1,volume:void 0!==i.volume?i.volume:1,pitch:1,positional:i.spatialSound||!1,distanceModel:Dt(i.distanceModel||"exponential"),maxDistance:i.maxDistance||1e4,refDistance:i.refDistance||1,rollOffFactor:i.rolloffFactor||1}}};s.addComponent("sound",l);const c=new e.Asset(`waypoint-audio-asset-${a}`,"audio",{url:i.url});R.assets.add(c),c.ready(()=>{const e=s.sound?.slot(a);e&&(e.asset=c.id);const t=ft.get(a);t&&(t.assetReady=!0),console.log(`[Audio] Waypoint audio loaded: ${a}, spatialSound=${i.spatialSound}, maxDistance=${i.maxDistance}`)}),R.assets.load(c),R.root.addChild(s),ft.set(a,{entity:s,waypointIndex:o,config:i,slotId:a,playing:!1,autoplayTriggered:!1,assetReady:!1}),console.log(`[Audio] Waypoint audio created: ${a} at waypoint ${o}, spatial=${i.spatialSound}`)}})}),ft.size>0&&console.log(`[StorySplat Viewer] Setup ${ft.size} waypoint audio sources`)),function(){const t=r.audioEmitters;t&&0!==t.length&&(console.log(`[Audio] Setting up ${t.length} standalone audio emitters`),t.forEach(t=>{if(!t.enabled)return void console.log(`[Audio] Skipping disabled emitter: ${t.name||t.id}`);if(!t.url)return void console.warn(`[Audio] Emitter has no URL: ${t.name||t.id}`);const o=t.id||`emitter-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,n=t.position||{x:0,y:0,z:0},i=new e.Entity(`audio-emitter-${o}`);i.setPosition(n.x,n.y,n.z);const a=Dt(t.distanceModel||"linear");i.addComponent("sound",{positional:!1!==t.spatialSound,refDistance:t.refDistance||1,maxDistance:t.maxDistance||100,rollOffFactor:t.rolloffFactor||1,distanceModel:a,volume:t.volume??.5}),i.sound?.addSlot(o,{volume:t.volume??.5,loop:!1!==t.loop,autoPlay:!1,overlap:!1});const s=new e.Asset(`audio-emitter-${o}`,"audio",{url:t.url});s.on("load",()=>{if(O)return;const e=i.sound?.slot(o);e&&(e.asset=s.id);const n=Ft.get(o);n&&(n.assetReady=!0,!1!==t.autoplay&&e&&(console.log(`[Audio] Autoplay emitter: ${t.name||o}`),e.play(),n.playing=!0)),console.log(`[Audio] Emitter loaded: ${t.name||o}, spatial=${!1!==t.spatialSound}, maxDistance=${t.maxDistance||100}`)}),R.assets.add(s),R.assets.load(s),R.root.addChild(i),Ft.set(o,{entity:i,config:t,slotId:o,playing:!1,assetReady:!1}),console.log(`[Audio] Emitter created: ${t.name||o} at (${n.x}, ${n.y}, ${n.z})`)}),Ft.size>0&&console.log(`[StorySplat Viewer] Setup ${Ft.size} standalone audio emitters`))}(),console.log("[StorySplat Viewer] 🎆 Calling initParticleSystems..."),async function(){if(console.log("═══════════════════════════════════════"),console.log("🎆 PARTICLE SYSTEM INITIALIZATION"),console.log("═══════════════════════════════════════"),console.log("[Particle] config.particles:",r.particles),console.log("[Particle] Type:",typeof r.particles),console.log("[Particle] Is Array:",Array.isArray(r.particles)),!r.particles||0===r.particles.length)return console.log("[Particle] ⚠️ No particle systems to create (particles array is empty or undefined)"),void console.log("═══════════════════════════════════════");console.log(`[Particle] 📋 Found ${r.particles.length} particle system(s) to create`);for(let e=0;e<r.particles.length;e++){const t=r.particles[e];console.log(`[Particle] --- Particle System ${e+1}/${r.particles.length} ---`),console.log("[Particle] Raw config:",JSON.stringify(t,null,2));try{const o=t.particleTexture||"flare";let n,i;"custom"===o&&t.customTextureUrl?(n=t.customTextureUrl,i=`custom_${t.id||t.name||e}`,console.log(`[Particle] Custom texture: ${n.substring(0,60)}...`)):(n=wt[o]||wt.flare,i=o,console.log(`[Particle] Texture: ${o} -> ${n.substring(0,60)}...`)),console.log("[Particle] Loading texture...");const a=await xt(i,n);console.log("[Particle] ✅ Texture loaded:",a.name),console.log("[Particle] Creating entity...");const s=St(t);console.log("[Particle] ✅ Entity created:",s.name),s.particlesystem?(s.particlesystem.colorMap=a,console.log("[Particle] ✅ Texture applied to particle system"),console.log("[Particle] Particle system properties:",{numParticles:s.particlesystem.numParticles,lifetime:s.particlesystem.lifetime,rate:s.particlesystem.rate,loop:s.particlesystem.loop,autoPlay:s.particlesystem.autoPlay})):console.warn("[Particle] ⚠️ Entity has no particlesystem component!"),R.root.addChild(s),console.log("[Particle] ✅ Entity added to scene");const r=s.getPosition();console.log(`[Particle] Position: (${r.x.toFixed(2)}, ${r.y.toFixed(2)}, ${r.z.toFixed(2)})`);const l=(t.id||t.name||`particle-${vt.size}`).replace(/[^a-zA-Z0-9]/g,"_");vt.set(l,s),console.log(`[Particle] ✅ SUCCESS: Created "${t.name||l}"`)}catch(e){console.error(`[Particle] ❌ FAILED to create particle system: ${t.name}`),console.error(`[Particle] Error message: ${e?.message||"No message"}`),console.error(`[Particle] Error stack: ${e?.stack||"No stack"}`),console.error("[Particle] Error object:",e)}}console.log("═══════════════════════════════════════"),console.log(`[Particle] 🎉 COMPLETE: ${vt.size}/${r.particles.length} particle systems created`),console.log("[Particle] Active particle systems:",Array.from(vt.keys())),console.log("═══════════════════════════════════════")}(),async function(){r.customMeshes&&0!==r.customMeshes.length?(console.log(`[StorySplat Viewer] Creating ${r.customMeshes.length} custom meshes...`),console.log("[StorySplat Viewer] All custom mesh configs:",r.customMeshes),setTimeout(()=>{let e=0,t=0;r.customMeshes.forEach((o,n)=>{if(console.log(`[CustomMesh] Processing mesh ${n}:`,{name:o.name,enabled:o.enabled,hasModelUrl:!!o.modelUrl,modelUrl:o.modelUrl?.substring(0,100)+"..."}),!1!==o.enabled)try{_t(o,n)?e++:(t++,console.warn(`[CustomMesh] Mesh ${o.name} returned null (likely missing modelUrl)`))}catch(e){console.error("[CustomMesh] Error loading mesh",o.name,":",e),t++}else console.log("[CustomMesh] Skipping disabled mesh:",o.name,"(enabled =",o.enabled,")"),t++}),console.log(`[CustomMesh] Summary: ${e} loaded, ${t} skipped`)},100),console.log(`[StorySplat Viewer] ${r.customMeshes.length} custom meshes queued for loading`)):console.log("[StorySplat Viewer] No custom meshes to create")}(),function(){const t=r.skybox?.url||r.skyboxUrl;if(!t)return void console.log("[StorySplat Viewer] No skybox configured");const o=r.skybox?.rotation??r.skyboxRotation??0,n=r.skybox?.intensity??1,i=r.skybox?.enableIBL??!0;console.log("[StorySplat Viewer] Creating skybox:",t,"rotation:",o,"rad =",o*(180/Math.PI),"deg","IBL:",i);const a=t.toLowerCase().includes(".hdr")||t.toLowerCase().includes(".exr"),s=new e.Entity("skybox"),l=new e.StandardMaterial;l.useLighting=!1,l.cull=e.CULLFACE_FRONT;const c=new e.Asset("skybox-texture","texture",{url:t});if(R.assets.add(c),c.ready(t=>{const o=t.resource;if(l.emissiveMap=o,l.emissive=new e.Color(n,n,n),l.update(),console.log("[StorySplat Viewer] Skybox texture loaded"),i)try{a&&(R.scene.exposure=n,R.scene.toneMapping=e.TONEMAP_ACES,console.log("[StorySplat Viewer] HDR tone mapping enabled")),o&&(R.scene.ambientLight=new e.Color(.3*n,.3*n,.35*n),console.log("[StorySplat Viewer] IBL ambient lighting applied with intensity:",n))}catch(e){console.warn("[StorySplat Viewer] Failed to apply IBL:",e)}}),c.on("error",e=>{console.error("[StorySplat Viewer] Failed to load skybox texture:",e)}),R.assets.load(c),s.addComponent("render",{type:"sphere",material:l,castShadows:!1,receiveShadows:!1}),s.setLocalScale(500,500,500),0!==o){const e=o*(180/Math.PI);s.setEulerAngles(0,e,0)}R.on("update",()=>{const e=le.getPosition();s.setPosition(e.x,e.y,e.z)}),R.root.addChild(s),console.log("[StorySplat Viewer] Skybox created with rotation:",o,"intensity:",n)}(),It(),H&&(H.enabled=!0,console.log("[StorySplat Viewer] Reveal effect started immediately")),setTimeout(()=>{A.preloader&&p(A.preloader)},200),function(){if(!r.includeXR)return;if(!R.xr)return void console.warn("[StorySplat Viewer] WebXR not supported in this browser");const t=R.xr,o=r.xrMode||"both";"vr"!==o&&"both"!==o||(t.isAvailable(e.XRTYPE_VR)&&(A.vrButton?.classList.add("available"),console.log("[StorySplat Viewer] VR is available")),t.on("available:"+e.XRTYPE_VR,e=>{e?A.vrButton?.classList.add("available"):A.vrButton?.classList.remove("available")})),"ar"!==o&&"both"!==o||(t.isAvailable(e.XRTYPE_AR)&&(A.arButton?.classList.add("available"),console.log("[StorySplat Viewer] AR is available")),t.on("available:"+e.XRTYPE_AR,e=>{e?A.arButton?.classList.add("available"):A.arButton?.classList.remove("available")})),t.on("start",()=>{fe=!0,console.log("[StorySplat Viewer] XR session started"),"vr"===ve?(A.vrButton?.classList.add("active"),A.vrButton.textContent="Exit VR"):"ar"===ve&&(A.arButton?.classList.add("active"),A.arButton.textContent="Exit AR"),he.disable(),ue&&ue.disable(),i.emit("xrStart",{type:ve})}),t.on("end",()=>{fe=!1,console.log("[StorySplat Viewer] XR session ended"),Se(),A.vrButton?.classList.remove("active"),A.arButton?.classList.remove("active"),A.vrButton&&(A.vrButton.textContent="VR"),A.arButton&&(A.arButton.textContent="AR"),ve=null,"explore"===me?he.enable():"walk"===me&&ue&&ue.enable(),i.emit("xrEnd",{})}),A.vrButton&&A.vrButton.addEventListener("click",()=>{fe&&"vr"===ve?t.end():!fe&&t.isAvailable(e.XRTYPE_VR)&&(ve="vr",le.camera.startXr(e.XRTYPE_VR,e.XRSPACE_LOCALFLOOR,{callback:e=>{e&&(console.error("[StorySplat Viewer] Failed to start VR:",e),ve=null)}}))}),A.arButton&&A.arButton.addEventListener("click",()=>{fe&&"ar"===ve?t.end():!fe&&t.isAvailable(e.XRTYPE_AR)&&(ve="ar",le.camera.startXr(e.XRTYPE_AR,e.XRSPACE_LOCALFLOOR,{callback:e=>{e&&(console.error("[StorySplat Viewer] Failed to start AR:",e),ve=null)}}))})}(),r.htmlMeshes&&r.htmlMeshes.length>0){console.log("[StorySplat Viewer] Setting up HTML meshes:",r.htmlMeshes.length);const e=function(e,t){const o=new Y(e);for(const e of t)o.createMesh(e);return o}(R,r.htmlMeshes);R.__htmlMeshManager=e,i.on("progressUpdate",()=>{const t=100*Re,o=r.waypoints?.length||1,n=Math.round(Re*Math.max(1,o-1));e.updateVisibility(t,n)})}if(r.customScript&&""!==r.customScript.trim()){console.log("[StorySplat Viewer] Initializing custom script system...");const t=function(t,o,n,i,a,s,r,l,c){if(!i||""===i.trim())return console.log("[Custom Script] No custom script provided"),null;console.log("[Custom Script] Initializing custom script system..."),console.log("[Custom Script] Script length:",i.length);const d=new X(i);return d.initialize({app:t,camera:o,pc:e,canvas:n,getScrollPercentage:a,getCurrentWaypointIndex:s,getHotspots:r,getSplats:l,getHTMLMeshes:c}),d.execute(),d}(R,le,z,r.customScript,()=>Re,()=>B,()=>ut,()=>$?[$]:[],()=>r.htmlMeshes||[]);t&&(R.__customScriptSystem=t,console.log("[StorySplat Viewer] Custom script system initialized"))}try{const e=new URLSearchParams(window.location.search),t=e.get("waypoint"),o=e.get("autoplay");if(null!==t&&r.waypoints&&r.waypoints.length>0){const e=parseInt(t,10);!isNaN(e)&&e>=0&&e<r.waypoints.length&&(console.log("[StorySplat Viewer] URL param: navigating to waypoint",e),it(e))}"true"!==o||n.autoPlay||r.autoPlay||(console.log("[StorySplat Viewer] URL param: starting autoplay"),dt())}catch(e){console.warn("[StorySplat Viewer] Could not parse URL parameters:",e)}i.emit("ready"),console.log("[StorySplat Viewer] Ready"),w&&(h(A,{nextWaypoint:at,prevWaypoint:st,play:dt,pause:pt,isPlaying:()=>V,getCurrentWaypointIndex:()=>B,getWaypointCount:()=>r.waypoints?.length||0,getWaypoints:()=>r.waypoints||[],setCameraMode:Ee,on:(e,t)=>i.on(e,t)},T,S.buttonLabels),A.returnWaypointButton&&r.waypoints&&r.waypoints.length>0&&A.returnWaypointButton.addEventListener("click",()=>{const e=le.getPosition();let t=0,o=1/0;r.waypoints.forEach((n,i)=>{const a=n.position||[0,0,0],s=e.x-a[0],r=e.y-a[1],l=e.z- -a[2],c=Math.sqrt(s*s+r*r+l*l);c<o&&(o=c,t=i)}),console.log("[StorySplat Viewer] Returning to nearest waypoint:",t,"distance:",o.toFixed(2)),Ee("tour"),it(t);const n=A.scrollControls?.querySelectorAll(".storysplat-mode-btn");n?.forEach(e=>{const t=e.getAttribute("data-mode");e.classList.toggle("selected","tour"===t)})}),i.emit("progressUpdate",{progress:Re,index:B}),r.waypoints&&r.waypoints.length>0&&i.emit("waypointChange",{index:0,waypoint:r.waypoints[0],prevIndex:-1})),(n.autoPlay||r.autoPlay)&&dt()}).catch(e=>{console.error("[StorySplat Viewer] Failed to initialize:",e),A.preloader&&p(A.preloader),i.emit("error",e)});const no=()=>{R.resizeCanvas()};window.addEventListener("resize",no);const io={app:R,canvas:z,goToWaypoint:it,nextWaypoint:at,prevWaypoint:st,getCurrentWaypointIndex:()=>B,getWaypointCount:()=>r.waypoints?.length||0,setPosition:(e,t,o)=>le.setPosition(e,t,o),setRotation:(e,t,o)=>le.setEulerAngles(e,t,o),getPosition:()=>{const e=le.getPosition();return{x:e.x,y:e.y,z:e.z}},getRotation:()=>{const e=le.getEulerAngles();return{x:e.x,y:e.y,z:e.z}},play:dt,pause:pt,stop:function(){if(W)return W.stop(),void i.emit("playbackStop");pt(),tt(0)},isPlaying:()=>V,setFrame:e=>W?.setFrame(e),getCurrentFrame:()=>W?.getCurrentFrame()??0,getTotalFrames:()=>W?.getTotalFrames()??0,setFps:e=>W?.setFps(e),getFps:()=>W?.getFps()??24,getFrameProgress:()=>W?.getProgress()??0,setFrameProgress:e=>W?.setProgress(e),goToOriginalSplat:function(){const e=Pe();oe!==e&&(ae.forEach((t,o)=>{o!==e&&(N?_e(t):Le(o))}),$&&($.enabled=!0),oe=e,i.emit("splatChange",{url:e,isOriginal:!0}),console.log("[SplatSwap] Manually returned to original splat"),Te(void 0,!0))},getCurrentSplatUrl:function(){return oe||Pe()},isShowingOriginalSplat:function(){return oe===Pe()||null===oe},destroy:()=>{O=!0,pt(),W&&(W.destroy(),W=null),window.removeEventListener("resize",no),A.preloader&&A.preloader.remove(),A.scrollControls&&A.scrollControls.remove(),A.fullscreenButton&&A.fullscreenButton.remove(),A.helpButton&&A.helpButton.remove(),A.helpPanel&&A.helpPanel.remove(),A.waypointInfo&&A.waypointInfo.remove(),A.watermark&&A.watermark.remove();const e=document.getElementById("storysplat-viewer-styles");e&&e.remove(),t.classList.remove("storysplat-viewer-container"),Bt.forEach(e=>e.destroy()),Bt.length=0,zt(),ue&&ue.destroy(),R.__customScriptSystem&&R.__customScriptSystem.dispose(),R.__htmlMeshManager&&R.__htmlMeshManager.destroy(),be&&(be.destroy(),be=null),we&&(we.destroy(),we=null),R.destroy(),z.remove()},resize:no,navigateToScene:async e=>{const t={targetSceneId:e,activationMode:"click"};await jt(t)},on:(e,t)=>i.on(e,t),off:(e,t)=>i.off(e,t)};return io}async function oe(e,t,o){console.log("[StorySplat Viewer] Fetching scene from:",t);const n=await fetch(t);if(!n.ok)throw new Error(`Failed to fetch scene: ${n.statusText}`);const i=await n.json();return console.log("[StorySplat Viewer] Scene data loaded:",i),te(e,i,o)}function ne(e,t,o){return e+(t-e)*o}class ie extends Error{constructor(e){super(`Scene not found: ${e}`),this.name="SceneNotFoundError"}}class ae extends Error{constructor(e,t){super(e),this.name="SceneApiError",this.statusCode=t}}const se="undefined"!=typeof process&&process.env?.STORYSPLAT_API_URL?process.env.STORYSPLAT_API_URL:"undefined"!=typeof window&&window.__STORYSPLAT_API_URL__?window.__STORYSPLAT_API_URL__:"https://discover.storysplat.com";async function re(e,t,o={}){const n=o.baseUrl||se;console.log(`[StorySplat Viewer] Fetching scene: ${t}`);const i=`${n}/api/scene/${encodeURIComponent(t)}`,a={"Content-Type":"application/json"};o.apiKey&&(a.Authorization=`Bearer ${o.apiKey}`);const s=await fetch(i,{method:"GET",headers:a});if(!s.ok){if(404===s.status)throw new ie(t);const e=await s.text();let o;try{o=JSON.parse(e).error||`API error: ${s.status}`}catch{o=`API error: ${s.status}`}throw new ae(o,s.status)}const r=await s.json();if(!r.success||!r.data)throw new ae("Invalid API response format",500);console.log(`[StorySplat Viewer] Scene loaded: "${r.meta.name}"`);const l={...r.data,name:r.data.name||r.meta.name,thumbnailUrl:r.data.thumbnailUrl||r.meta.thumbnailUrl},{baseUrl:c,apiKey:d,...p}=o;return te(e,l,p)}async function le(e,t={}){const o=`${t.baseUrl||se}/api/scene/${encodeURIComponent(e)}/meta`,n={"Content-Type":"application/json"};t.apiKey&&(n.Authorization=`Bearer ${t.apiKey}`);const i=await fetch(o,{method:"GET",headers:n});if(!i.ok){if(404===i.status)throw new ie(e);throw new ae(`API error: ${i.status}`,i.status)}const a=await i.json();return{...a,userName:a.userName||"Unknown",userSlug:a.userSlug||"unknown"}}class ce{constructor(t,o,n={}){this.translateGizmo=null,this.rotateGizmo=null,this.scaleGizmo=null,this.activeGizmo=null,this.attachedEntity=null,this._mode="none",this.app=t,this.camera=o,this.gizmoLayer=e.Gizmo.createLayer(t),this.translateGizmo=new e.TranslateGizmo(o,this.gizmoLayer),this.rotateGizmo=new e.RotateGizmo(o,this.gizmoLayer),this.scaleGizmo=new e.ScaleGizmo(o,this.gizmoLayer),this.setSnap(n.snap??!1,n.snapIncrement??1),this.setCoordSpace(n.coordSpace??"world"),this.setupEvents(this.translateGizmo),this.setupEvents(this.rotateGizmo),this.setupEvents(this.scaleGizmo)}setupEvents(e){e.on("transform:start",()=>{this.onTransformStart?.({entity:this.attachedEntity,position:this.attachedEntity?.getPosition().clone(),rotation:this.attachedEntity?.getRotation().clone(),scale:this.attachedEntity?.getLocalScale().clone()})}),e.on("transform:move",()=>{this.onTransformMove?.({entity:this.attachedEntity,position:this.attachedEntity?.getPosition().clone(),rotation:this.attachedEntity?.getRotation().clone(),scale:this.attachedEntity?.getLocalScale().clone()})}),e.on("transform:end",()=>{this.onTransformEnd?.({entity:this.attachedEntity,position:this.attachedEntity?.getPosition().clone(),rotation:this.attachedEntity?.getRotation().clone(),scale:this.attachedEntity?.getLocalScale().clone()})})}get mode(){return this._mode}setMode(e){switch(this._mode=e,this.activeGizmo?.detach(),this.activeGizmo=null,e){case"translate":this.activeGizmo=this.translateGizmo;break;case"rotate":this.activeGizmo=this.rotateGizmo;break;case"scale":this.activeGizmo=this.scaleGizmo}this.activeGizmo&&this.attachedEntity&&this.activeGizmo.attach([this.attachedEntity])}attach(e){this.attachedEntity=e,this.activeGizmo&&(e?this.activeGizmo.attach([e]):this.activeGizmo.detach())}attachToMesh(e){this.attach(e)}detach(){this.attachedEntity=null,this.activeGizmo?.detach()}set positionGizmoEnabled(e){e&&"translate"!==this._mode?this.setMode("translate"):e||"translate"!==this._mode||this.setMode("none")}get positionGizmoEnabled(){return"translate"===this._mode}set rotationGizmoEnabled(e){e&&"rotate"!==this._mode?this.setMode("rotate"):e||"rotate"!==this._mode||this.setMode("none")}get rotationGizmoEnabled(){return"rotate"===this._mode}set scaleGizmoEnabled(e){e&&"scale"!==this._mode?this.setMode("scale"):e||"scale"!==this._mode||this.setMode("none")}get scaleGizmoEnabled(){return"scale"===this._mode}setSnap(e,t){this.translateGizmo&&(this.translateGizmo.snap=e,void 0!==t&&(this.translateGizmo.snapIncrement=t)),this.rotateGizmo&&(this.rotateGizmo.snap=e,void 0!==t&&(this.rotateGizmo.snapIncrement=t)),this.scaleGizmo&&(this.scaleGizmo.snap=e,void 0!==t&&(this.scaleGizmo.snapIncrement=t))}setCoordSpace(e){this.translateGizmo&&(this.translateGizmo.coordSpace=e),this.rotateGizmo&&(this.rotateGizmo.coordSpace=e),this.scaleGizmo&&(this.scaleGizmo.coordSpace=e)}onTransform(e){this.onTransformStart=e.start,this.onTransformMove=e.move,this.onTransformEnd=e.end}update(){this.activeGizmo?.update()}destroy(){this.translateGizmo?.destroy(),this.rotateGizmo?.destroy(),this.scaleGizmo?.destroy(),this.translateGizmo=null,this.rotateGizmo=null,this.scaleGizmo=null,this.activeGizmo=null,this.attachedEntity=null}}class de{constructor(t,o,n={}){this.selectedEntities=new Set,this.highlightedEntity=null,this.originalMaterials=new Map,this.app=t,this.camera=o,this.config={highlightColor:n.highlightColor??new e.Color(.2,.5,1,1),multiSelect:n.multiSelect??!1},this.picker=new e.Picker(t,1,1,!0)}async pickAtScreenPosition(e,t){const o=this.app.graphicsDevice.canvas,n=this.camera.camera;if(!n||!o)return null;const i=.25,a=Math.max(1,Math.floor(o.clientWidth*i)),s=Math.max(1,Math.floor(o.clientHeight*i));this.picker.resize(a,s);const r=this.app.scene.layers.getLayerByName("World");if(!r)return null;this.picker.prepare(n,this.app.scene,[r]);const l=Math.floor(e*i),c=Math.floor(t*i),d=this.picker.getSelection(l,c);if(d&&d.length>0){const e=d[0];if(e.node){let t=e.node;for(;t.parent&&t.parent!==this.app.root;){const e=t.parent;if(e.tags?.has("selectable")||e.tags?.has("hotspot")||e.tags?.has("waypoint")||e.tags?.has("light")){t=e;break}t=e}return t}}return null}async getWorldPointAtScreen(e,t){const o=this.app.graphicsDevice.canvas,n=this.camera.camera;if(!n||!o)return null;const i=.25,a=Math.max(1,Math.floor(o.clientWidth*i)),s=Math.max(1,Math.floor(o.clientHeight*i));this.picker.resize(a,s);const r=this.app.scene.layers.getLayerByName("World");if(!r)return null;this.picker.prepare(n,this.app.scene,[r]);const l=Math.floor(e*i),c=Math.floor(t*i);try{return await this.picker.getWorldPointAsync(l,c)||null}catch(e){return null}}select(e,t=!1){const o=this.getSelectedEntity();t||this.deselectAll(),e&&(this.selectedEntities.add(e),this.applyHighlight(e)),this.onSelectionChange?.({entity:e,previousEntity:o})}deselect(e){this.selectedEntities.has(e)&&(this.selectedEntities.delete(e),this.removeHighlight(e),this.onSelectionChange?.({entity:this.getSelectedEntity(),previousEntity:e}))}deselectAll(){const e=this.getSelectedEntity();this.selectedEntities.forEach(e=>{this.removeHighlight(e)}),this.selectedEntities.clear(),e&&this.onSelectionChange?.({entity:null,previousEntity:e})}isSelected(e){return this.selectedEntities.has(e)}getSelectedEntity(){const e=Array.from(this.selectedEntities);return e.length>0?e[0]:null}getSelectedEntities(){return Array.from(this.selectedEntities)}applyHighlight(t){if(!t.render)return;const o=new Map;t.render.meshInstances.forEach((t,n)=>{if(t.material){o.set(n,t.material);const i=t.material.clone();i instanceof e.StandardMaterial&&(i.emissive=this.config.highlightColor,i.emissiveIntensity=.3,i.update()),t.material=i}}),this.originalMaterials.set(t,o)}removeHighlight(e){const t=this.originalMaterials.get(e);t&&e.render&&(e.render.meshInstances.forEach((e,o)=>{const n=t.get(o);n&&(e.material=n)}),this.originalMaterials.delete(e))}onSelect(e){this.onSelectionChange=e}async handlePointerDown(e,t=!1){let o,n;if(e instanceof MouseEvent)o=e.clientX,n=e.clientY;else{const t=e.touches[0];o=t.clientX,n=t.clientY}const i=await this.pickAtScreenPosition(o,n);return i?this.select(i,t):t||this.deselectAll(),i}destroy(){this.deselectAll(),this.selectedEntities.clear(),this.originalMaterials.clear()}}export{_ as CameraControls,Z as FrameSequencePlayer,ce as GizmoManager,A as REVEAL_PRESETS,ae as SceneApiError,ie as SceneNotFoundError,de as SelectionManager,te as createViewer,re as createViewerFromSceneId,oe as createViewerFromUrl,s as exportPropsToViewerConfig,le as fetchSceneMeta,n as generateHTML,i as generateHTMLFromUrl,P as getRevealPreset,a as transformSceneToExportProps};
|
|
2
2
|
//# sourceMappingURL=index.esm.js.map
|