storysplat-viewer 2.2.8 → 2.2.9
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 +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/storysplat-viewer.umd.js +1 -1
- package/dist/storysplat-viewer.umd.js.map +1 -1
- package/dist/types/dynamic-viewer/FrameSequencePlayer.d.ts +87 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/types/index.d.ts +17 -1
- package/package.json +1 -1
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,o={}){const{cdnUrl:n="https://unpkg.com/storysplat-viewer@2/dist/storysplat-viewer.umd.js",title:i=e.name||"StorySplat Scene",description:a=`Interactive 3D scene: ${e.name||"StorySplat"}`,faviconUrl:s,customCSS:r=""}=o,l=s?`<link rel="icon" href="${t(s)}" />`:"",c=r?`<style>${r}</style>`:"",p=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),d=p.replace(/<\/script/gi,"<\\/script");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(a)}">\n <title>${t(i)}</title>\n ${l}\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 ${c}\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(n)}"><\/script>\n <script>\n (function() {\n 'use strict';\n\n var sceneData = ${d};\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 });\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 n(e,t={}){const n=await fetch(e);if(!n.ok)throw new Error(`Failed to fetch scene: ${n.statusText}`);return o(await n.json(),t)}function i(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},defaultCameraMode:e.defaultCameraMode||"orbit",allowedCameraModes:e.allowedCameraModes||["orbit","first-person","drone"],cameraMovementSpeed:e.cameraMovementSpeed,cameraRotationSensitivity:e.cameraRotationSensitivity,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}}function a(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,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,includeXR:e.includeXR,xrMode:e.xrMode,customScript:e.customScript}}const s={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 r(e,t){return e?.[t]||s[t]}function l(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 }\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 .storysplat-hotspot-popup video {\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 video {\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 /* 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 .storysplat-hotspot-popup.fullscreen video {\n max-width: 95vw !important;\n max-height: 60vh !important;\n margin: 10px 0;\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 .storysplat-hotspot-popup.fullscreen video {\n max-width: 98vw !important;\n max-height: 55vh !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 /* 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-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 c(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 d(e,t,o="tour",n){const i=e.scrollControls?.querySelector(".storysplat-btn-prev"),a=e.scrollControls?.querySelector(".storysplat-btn-next"),r=e.scrollControls?.querySelector(".storysplat-btn-play");if(i&&i.addEventListener("click",(()=>t.prevWaypoint())),a&&a.addEventListener("click",(()=>t.nextWaypoint())),r){const e=e=>{r.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>'};r.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,p=-1;t.on("progressUpdate",(({progress:t})=>{const o=100*Math.max(0,Math.min(1,t)),i=Math.round(o),a=performance.now();var r,l;a-c<33||(c=a,e.progressBar&&(e.progressBar.style.width=`${o}%`),e.progressText&&i!==p&&(p=i,e.progressText.innerHTML="",e.progressText.textContent=(r=n,l=i,(r?.percentageFormat||s.percentageFormat).replace("{n}",String(l)))))}));let d=-1;t.on("waypointChange",(({index:o,waypoint:n})=>{if(o===d)return;if(d=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+=`<video src="${t.popupVideoUrl}" controls playsinline webkit-playsinline preload="metadata"></video>`),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 h(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 m(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 g(e,t){e.cameraModeToggle&&(t?e.cameraModeToggle.classList.add("visible"):e.cameraModeToggle.classList.remove("visible"))}function y(e,t){e.cameraModeToggle&&(e.cameraModeToggle.classList.remove("orbit","fly"),e.cameraModeToggle.classList.add(t))}function f(e,t){e.returnWaypointButton&&(t?e.returnWaypointButton.classList.add("visible"):e.returnWaypointButton.classList.remove("visible"))}const v=new e.Vec3,b=new e.Vec3,w=new e.Pose,x=new e.InputFrame({move:[0,0,0],rotate:[0,0,0]}),S=(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},_=(t,o,n,i,a=new e.Vec3)=>{const{fov:s,aspectRatio:r,horizontalFov:l,projection:c,orthoHeight:p}=t,d=t.system?.app,{width:u,height:h}=d?.graphicsDevice?.clientRect||{width:1920,height:1080};a.set(-o/u*2,n/h*2,0);const m=b.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(p*r,p,0);return a.mul(m),a};class M{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=.9,this._flyController.rotateDamping=.9,this._orbitController.rotateDamping=.9,this._orbitController.zoomDamping=.9,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(v)}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=v.copy(this.camera.forward).mulScalar(-o).add(e);this._controller.attach(w.look(n,e))}look(e,t=!1){this._setMode("focus");const o=t?v.copy(this.camera.getPosition()).sub(e).normalize().mulScalar(this._startZoomDist).add(e):this.camera.getPosition();this._controller.attach(w.look(o,e))}reset(e,t){this._setMode("focus"),this._controller.attach(w.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:p,rightInput:d}=this._flyMobileInput.read(),{leftStick:u,rightStick:h}=this._gamepadInput.read();S(u,this.gamepadDeadZone.x,this.gamepadDeadZone.y),S(h,this.gamepadDeadZone.x,this.gamepadDeadZone.y),this._state.axis.add(v.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]),w=+this._flyMobileInput.layout.endsWith("joystick"),M=(this._state.shift?this.moveFastSpeed:this._state.ctrl?this.moveSlowSpeed:this.moveSpeed)*t,C=60*this.zoomSpeed*t,E=C*this.zoomPinchSens,k=60*this.rotateSpeed*t,L=k*this.rotateTouchSens,T=this.rotateSpeed*this.rotateJoystickSens*60*t,{deltas:P}=x,A=v.set(0,0,0),R=this._state.axis.clone().normalize();A.add(R.mulScalar(g*M*this.keyboardSpeedMultiplier));const z=_(this.cameraComponent,a[0],a[1],this._pose.distance);A.add(z.mulScalar(m*f*+this.enablePan));const I=b.set(0,0,s[0]);A.add(I.mulScalar(m*C)),P.move.append([A.x,A.y,A.z]),A.set(0,0,0);const D=b.set(a[0],a[1],0);A.add(D.mulScalar((1-m*f)*k)),this.invertRotation&&(A.y=-A.y),P.rotate.append([A.x,A.y,A.z]),A.set(0,0,0);const V=b.set(p[0],0,-p[1]);A.add(V.mulScalar(g*M));const F=_(this.cameraComponent,r[0],r[1],this._pose.distance);A.add(F.mulScalar(m*y*+this.enablePan));const B=b.set(0,0,l[0]);A.add(B.mulScalar(m*y*E)),P.move.append([A.x,A.y,A.z]),A.set(0,0,0);const U=b.set(r[0],r[1],0);A.add(U.mulScalar(m*(1-y)*L));const H=b.set(d[0],d[1],0);A.add(H.mulScalar(g*(w?T:L))),this.invertRotation&&(A.y=-A.y),P.rotate.append([A.x,A.y,A.z]),A.set(0,0,0);const $=b.set(u[0],0,-u[1]);A.add($.mulScalar(g*M)),P.move.append([A.x,A.y,A.z]),A.set(0,0,0);const O=b.set(h[0],h[1],0);if(A.add(O.mulScalar(g*T)),this.invertRotation&&(A.y=-A.y),P.rotate.append([A.x,A.y,A.z]),this.app.xr?.active)x.read();else{if("focus"===this._mode){const e=P.move.length()+P.rotate.length()>0,t=this._focusController.complete?.()??!1;(e||t)&&this._setMode("orbit")}if(this._pose.copy(this._controller.update(x,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 C{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 p=Math.abs(e.x-n.x),d=Math.abs(e.y-n.y),u=Math.abs(e.z-n.z);if(p<s+t&&d<r+this.playerHeight/2&&u<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 p=n.y+r;Math.abs(e.x-n.x)<s+this.collisionRadius&&Math.abs(e.z-n.z)<l+this.collisionRadius&&e.y>=p-this.stepHeight&&(null===t||p>t)&&(t=p)}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 p=this.checkGround(c),d=c.y-this.playerHeight;null!==p&&d<=p+this.groundCheckDistance?(c.y=p+this.playerHeight,this.velocity.y=0,this.isGrounded=!0):(null===p||d>p+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 E=null;function k(){if(console.log("[RevealEffect] getGsplatRevealRadialClass called, cached:",!!E),E)return E;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()}}),E=t,t}const L={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 T(e){if("none"!==e)return L[e]}var P,A={},R={},z={};function I(){if(P)return z;P=1,Object.defineProperty(z,"__esModule",{value:!0}),z.loop=z.conditional=z.parse=void 0;z.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};z.conditional=function(e,t){return function(o,n,i,a){t(o,n,i)&&a(o,e,n,i)}};return z.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}},z}var D,V,F={};function B(){if(D)return F;D=1,Object.defineProperty(F,"__esModule",{value:!0}),F.readBits=F.readArray=F.readUnsigned=F.readString=F.peekBytes=F.readBytes=F.peekByte=F.readByte=F.buildStream=void 0;F.buildStream=function(e){return{data:e,pos:0}};var e=function(){return function(e){return e.data[e.pos++]}};F.readByte=e;F.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)}};F.readBytes=t;F.peekBytes=function(e){return function(t){return t.data.subarray(t.pos,t.pos+e)}};F.readString=function(e){return function(o){return Array.from(t(e)(o)).map((function(e){return String.fromCharCode(e)})).join("")}};F.readUnsigned=function(e){return function(o){var n=t(2)(o);return e?(n[1]<<8)+n[0]:(n[0]<<8)+n[1]}};F.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 F.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}),{})}},F}var U,H={};var $,O,W={};var N=function(){if(O)return A;O=1,Object.defineProperty(A,"__esModule",{value:!0}),A.decompressFrames=A.decompressFrame=A.parseGIF=void 0;var e,t=(V||(V=1,function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.default=void 0;var t=I(),o=B(),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}(R)),(e=R)&&e.__esModule?e:{default:e}),o=I(),n=B(),i=(U||(U=1,Object.defineProperty(H,"__esModule",{value:!0}),H.deinterlace=void 0,H.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}),H),a=($||($=1,Object.defineProperty(W,"__esModule",{value:!0}),W.lzw=void 0,W.lzw=function(e,t,o){var n,i,a,s,r,l,c,p,d,u,h,m,g,y,f,v,b=4096,w=o,x=new Array(o),S=new Array(b),_=new Array(b),M=new Array(4097);for(r=1+(i=1<<(u=e)),n=i+2,c=-1,a=(1<<(s=u+1))-1,p=0;p<i;p++)S[p]=0,_[p]=p;for(h=m=g=y=f=v=0,d=0;d<w;){if(0===y){if(m<s){h+=t[v]<<m,m+=8,v++;continue}if(p=h&a,h>>=s,m-=s,p>n||p==r)break;if(p==i){a=(1<<(s=u+1))-1,n=i+2,c=-1;continue}if(-1==c){M[y++]=_[p],c=p,g=p;continue}for(l=p,p==n&&(M[y++]=g,p=c);p>i;)M[y++]=_[p],p=S[p];g=255&_[p],M[y++]=g,n<b&&(S[n]=c,_[n]=g,!(++n&a)&&n<b&&(s++,a+=n)),c=l}y--,x[f++]=M[y],d++}for(d=f;d<w;d++)x[d]=0;return x}),W);A.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 A.decompressFrame=s,A.decompressFrames=function(e,t){return e.frames.filter((function(e){return e.image})).map((function(o){return s(o,e.gct,t)}))},A}();class G{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=N.parseGIF(o);if(this.frames=N.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 j{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 q{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:p,getSplats:d,getHTMLMeshes:u}=this.api,h=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=h(i,a,s,r,l,c,p,d,u,(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 X{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 Y(){const e=navigator.userAgent||navigator.vendor||window.opera||"";return/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(e)}const Z={"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 J(e){return e.includes("lod-meta.json")}function K(t,o,n={}){if(n.lazyLoad){const e=new X;let i=null;const a=n.lazyLoadThumbnail||o.thumbnailUrl,s=n.lazyLoadButtonText||"Start Experience",r=o.uiColor||"#4CAF50";console.log("[StorySplat Viewer] Lazy loading enabled, showing start button"),function(e,t){const{thumbnailUrl:o,buttonText:n="Start Experience",uiColor:i="#4CAF50",onStart:a}=t;l(i),e.classList.add("storysplat-viewer-container");const s=document.createElement("div");s.className="storysplat-lazy-load-container";let r="";o&&(r+=`<img class="storysplat-lazy-load-thumbnail" src="${o}" alt="Scene preview" />`),r+='<div class="storysplat-lazy-load-overlay"></div>',r+=`\n <div class="storysplat-lazy-load-content">\n <button class="storysplat-lazy-load-start-btn" style="background: ${i}">\n <svg viewBox="0 0 24 24">\n <path d="M8 5v14l11-7z"/>\n </svg>\n ${n}\n </button>\n </div>\n `,s.innerHTML=r,e.appendChild(s);const c=s.querySelector(".storysplat-lazy-load-start-btn");c?.addEventListener("click",(()=>{s.style.transition="opacity 0.3s ease-out",s.style.opacity="0",setTimeout((()=>{s.remove(),a()}),300)}))}(t,{thumbnailUrl:a,buttonText:s,uiColor:r,onStart:()=>{console.log("[StorySplat Viewer] User clicked start, initializing viewer..."),i=K(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,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 s=new X,v=a(i(o));console.log("[StorySplat Viewer] Creating viewer with config:",v),console.log("[StorySplat Viewer] Scale config:",v.scale),console.log("[StorySplat Viewer] Raw scene data:",{splatScale:o.splatScale,scale:o.scale});const b=!1!==n.showUI,w=v.uiColor||"#4CAF50",x=v.uiOptions||{},S=e=>"first-person"===e?"tour":"drone"===e?"explore":e,_=v.collisionMeshesData&&v.collisionMeshesData.length>0;let E=(v.allowedCameraModes||["orbit","first-person","drone"]).map(S).filter(((e,t,o)=>o.indexOf(e)===t));_&&!E.includes("walk")&&E.push("walk"),!_&&E.includes("walk")&&(E=E.filter((e=>"walk"!==e)));const L=S(v.defaultCameraMode||"orbit");let P={};b&&(P=function(e,t,o={}){const{uiColor:n="#4CAF50",showScrollControls:i=!0,showModeToggle:a=!0,showFullscreenButton:s=!0,showHelpButton:p=!1,showPreloader:d=!0,allowedCameraModes:u=["tour","explore"],defaultCameraMode:h="tour",customPreloaderLogoUrl:m,buttonLabels:g,hideWatermark:y=!1,watermarkText:f,watermarkLink:v,sceneId:b}=o,w={tour:r(g,"tour"),explore:r(g,"explore"),walk:r(g,"walk"),previous:r(g,"previous"),next:r(g,"next"),helpTitle:r(g,"helpTitle"),returnToTour:r(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())),l(n),e.classList.add("storysplat-viewer-container"),d&&(x.preloader=c(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&&u.length>1?`\n <div class="storysplat-mode-container">\n <div class="storysplat-mode-toggle">\n ${u.includes("tour")?`<button class="storysplat-mode-btn ${"tour"===h?"selected":""}" data-mode="tour">${w.tour}</button>`:""}\n ${u.includes("explore")?`<button class="storysplat-mode-btn ${"explore"===h?"selected":""}" data-mode="explore">${w.explore}</button>`:""}\n ${u.includes("walk")?`<button class="storysplat-mode-btn ${"walk"===h?"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 _=document.createElement("button");_.className="storysplat-xr-btn storysplat-vr-btn",_.setAttribute("aria-label","Enter VR"),_.textContent="VR",e.appendChild(_),x.vrButton=_;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,p){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 E=C.querySelector(".storysplat-hotspot-popup-close");E?.addEventListener("click",(()=>{C.classList.remove("visible","fullscreen")}));const k=document.createElement("div");k.className="storysplat-joystick-container",k.innerHTML='\n <div class="storysplat-joystick-base"></div>\n <div class="storysplat-joystick-thumb"></div>\n ',e.appendChild(k),x.joystick=k,x.joystickThumb=k.querySelector(".storysplat-joystick-thumb");const L=document.createElement("div");L.className="storysplat-look-zone",L.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(L),x.lookZone=L;const T=document.createElement("button");T.className="storysplat-camera-mode-toggle fly",T.setAttribute("aria-label","Toggle camera mode"),T.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(T),x.cameraModeToggle=T;const P=document.createElement("button");if(P.className="storysplat-return-waypoint-btn",P.setAttribute("aria-label",w.returnToTour),P.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(P),x.returnWaypointButton=P,!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,v,{uiColor:w,showScrollControls:v.waypoints&&v.waypoints.length>0,showModeToggle:E.length>1,showFullscreenButton:!x.hideFullscreenButton,showHelpButton:!x.hideHelpButton&&!x.hideInfoButton,showPreloader:!0,allowedCameraModes:E,defaultCameraMode:L,buttonLabels:x.buttonLabels,customPreloaderLogoUrl:x.customPreloaderLogoUrl,hideWatermark:x.hideWatermark,watermarkText:x.watermarkText,watermarkLink:x.watermarkLink,sceneId:o.sceneId}));const A=document.createElement("canvas");let R;A.id="storysplat-viewer-canvas",A.style.width="100%",A.style.height="100%",A.style.display="block",t.appendChild(A);const z={antialias:!1,alpha:!1,powerPreference:"high-performance"};try{R=new e.Application(A,{graphicsDeviceOptions:z,mouse:new e.Mouse(A),touch:new e.TouchDevice(A),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(A,{graphicsDeviceOptions:{...z,preferWebGl2:!1},mouse:new e.Mouse(A),touch:new e.TouchDevice(A),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")}}A.addEventListener("webglcontextlost",(e=>{e.preventDefault(),console.error("[StorySplat Viewer] WebGL context lost"),s.emit("error",new Error("WebGL context lost"))}),!1),A.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 I=Y(),D=I?"mobile":"desktop",V=Z[D];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=V.range[0],R.scene.gsplat.lodRangeMax=V.range[1],R.scene.gsplat.colorUpdateDistance=1,R.scene.gsplat.colorUpdateAngle=4,R.scene.gsplat.colorUpdateDistanceLodScale=2,R.scene.gsplat.colorUpdateAngleLodScale=2,console.log("[StorySplat Viewer] GSplat LOD configured:",{preset:D,lodRange:V.range,isMobile:I}));let F=0,B=!1,U=null,H=null,$=!1;const O=v.additionalSplats||[],W=v.keepMeshesInMemory??!1;let N=null,Q=!1;const te=new Map;let oe=-1,ne=-1;const ie=new e.Entity("camera");let ae=new e.Color(.1,.1,.1);if(v.backgroundColor){const t=v.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;ae=new e.Color(o,n,i),console.log("[StorySplat Viewer] Background color set from config:",v.backgroundColor)}}ie.addComponent("camera",{clearColor:ae,fov:v.fov||60,nearClip:v.nearClip||.1,farClip:v.farClip||1e3}),ie.addComponent("audiolistener"),console.log("[StorySplat Viewer] Camera settings:",{fov:v.fov,nearClip:v.nearClip,farClip:v.farClip,playerHeight:v.playerHeight});const se=v.playerHeight||1.6;if(v.waypoints&&v.waypoints.length>0){const e=v.waypoints[0];if(console.log("[StorySplat Viewer] First waypoint raw:",e),e.position){const t=e.position;ie.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 ie.setPosition(0,se,5);if(e.rotation){const t=fe(e.rotation);ie.setRotation(t),console.log("[StorySplat Viewer] Camera rotation set from waypoint")}}else ie.setPosition(0,se,5),ie.lookAt(new e.Vec3(0,0,0)),console.log("[StorySplat Viewer] Camera set to default position (0, playerHeight, 5)");R.root.addChild(ie);const re=new e.Entity("light");re.addComponent("light",{type:e.LIGHTTYPE_DIRECTIONAL,color:new e.Color(1,1,1),intensity:1,castShadows:!1}),re.setEulerAngles(45,45,0),R.root.addChild(re),console.log("[StorySplat Viewer] Light added");const le=new M(ie,R,{moveSpeed:2*(v.cameraMovementSpeed||1),moveFastSpeed:5*(v.cameraMovementSpeed||1),moveSlowSpeed:1*(v.cameraMovementSpeed||1),rotateSpeed:5e-4*(v.cameraRotationSensitivity||.2),enableOrbit:!0,enableFly:!0,enablePan:!0,invertRotation:v.invertCameraRotation});let ce=null;v.collisionMeshesData&&v.collisionMeshesData.length>0&&(ce=new C(ie,R,{moveSpeed:2*(v.cameraMovementSpeed||1),sprintMultiplier:2,lookSensitivity:.005*(v.cameraRotationSensitivity||.2),playerHeight:v.playerHeight||1.6,gravity:20,jumpVelocity:8,collisionRadius:.3,stepHeight:.3}),ce.createCollisionMeshes(v.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 pe=L,de=!0;"tour"===L&&le.disable();const ue=new e.Picker(R,1,1,!0);let he=!1,me=null;function ge(e){"walk"===pe&&ce?ce.disable():"explore"===pe&&le.disable(),pe=e,console.log("[StorySplat Viewer] Switching to mode:",e);const t=Y();if("walk"===e&&ce)de=!1,le.disable(),ce.enable(),h(P,!1),g(P,!1),v.waypoints&&v.waypoints.length>0&&f(P,!0);else if("explore"===e){ce&&ce.disable(),Ae=0,Re=0,ze=!1,de=!1,le.disable(),le.enable(),le.enableOrbit=!0,le.enableFly=!0,le.enablePan=!0,Te&&Pe?(le.syncFromPose(Te,Pe),console.log("[StorySplat Viewer] Synced camera from waypoint pose for explore mode")):le.syncFromCamera();(async()=>{try{const e=.25;ue.resize(Math.floor(It.clientWidth*e),Math.floor(It.clientHeight*e));const t=R.scene.layers.getLayerByName("World");if(t){ue.prepare(ie.camera,R.scene,[t]);const o=Math.floor(.5*It.clientWidth*e),n=Math.floor(.5*It.clientHeight*e),i=await ue.getWorldPointAsync(o,n);if(i){const e=ie.getPosition().distance(i);e>.5&&e<500&&(le.syncFromCamera(i),console.log("[StorySplat Viewer] Updated focus target at distance:",e.toFixed(2)))}}}catch(e){}})(),t?(h(P,!0),g(P,!0),le.setMode("fly"),y(P,"fly")):le.setMode("orbit"),v.waypoints&&v.waypoints.length>0&&f(P,!0)}else le.disable(),ce&&ce.disable(),de=!0,h(P,!1),g(P,!1),f(P,!1);s.emit("modeChange",{mode:e})}R.on("update",(t=>{"walk"===pe&&ce?ce.update(t):le.update(t),de||function(){const e=ie.getPosition();et.forEach((t=>{const o=t.hotspotData;if(!o||"video"!==o.type||!t.videoElement)return;if("proximity"!==(t.mediaTriggerMode||"click"))return;const n=t.getPosition(),i=e.distance(n),a=t.proximityDistance||5;i<=a&&!t.isVideoPlaying?Lt(t,o):i>a&&t.isVideoPlaying&&Tt(t)}))}(),function(){if(0===it.size)return;const e=ie.getPosition();it.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(!v.waypoints?.length)return;const t=ie.getPosition();v.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?_t.has(n)||(_t.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=it.get(t);if(o&&o.assetReady&&!o.playing){const e=o.entity.sound?.slot(o.slotId);e&&(e.play(),o.playing=!0)}}}))}(o)):_t.has(n)&&(_t.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=it.get(t);if(o&&o.playing){const e=o.entity.sound?.slot(o.slotId);e&&(e.stop(),o.playing=!1)}}}}))}(o))}))}()})),R.on("joystick:left",((e,t,o,n)=>{if(e<0||t<0)m(P,!1,0,0,60);else{m(P,!0,o-e,n-t,60)}})),P.cameraModeToggle&&P.cameraModeToggle.addEventListener("click",(()=>{if("explore"!==pe)return;const e=le.mode;"orbit"===e||"focus"===e?(le.setMode("fly"),y(P,"fly"),h(P,!0)):(le.setMode("orbit"),y(P,"orbit"),h(P,!1))})),R.on("cameracontrols:modechange",(e=>{"orbit"!==e&&"fly"!==e||(y(P,e),I&&"explore"===pe&&h(P,"fly"===e))}));const ye=(e,t)=>{P.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)}%`)}(P.preloader,e,t),s.emit("progress",{progress:e,text:t})};function fe(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 ve(e){e.enabled=!1,console.log("[SplatSwap] Splat hidden")}function be(e){const t=te.get(e);t&&(t.destroy(),te.delete(e),console.log("[SplatSwap] Splat disposed:",e))}async function we(t){if(!O||0===O.length)return;const o=(t+1)%O.length,n=O[o];n&&n.url&&await async function(t){if(!te.has(t)&&t!==N&&!$){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($)return void i(new Error("Viewer destroyed"));const a=new e.Entity("splat-preload");a.addComponent("gsplat",{asset:o,unified:!0});const s=v.scale||{x:1,y:1,z:1},r=v.invertXScale||!1,l=v.invertYScale||!1,c={x:r?-s.x:s.x,y:l?-s.y:s.y,z:r!==l?-s.z:s.z};a.setLocalScale(c.x,c.y,c.z);const p=v.position||[0,0,0];a.setPosition(p[0],p[1],-p[2]);const d=v.rotation||[0,0,0],u=0!==d[0]||0!==d[1]||0!==d[2]?[d[0]*(180/Math.PI),d[1]*(180/Math.PI),-d[2]*(180/Math.PI)]:[180,0,0];a.setEulerAngles(u[0],u[1],u[2]),a.enabled=!1,R.root.addChild(a),te.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 xe(t){if(t!==N&&!Q&&!$){Q=!0,console.log("[SplatSwap] Switching to splat:",t);try{if(N&&te.has(N))if(W){const e=te.get(N);e&&ve(e)}else be(N);else U&&(U.enabled=!1);if(te.has(t))!function(e){const t=te.get(e);!!t&&(t.enabled=!0,N=e,console.log("[SplatSwap] Splat shown:",e))}(t);else{const o=new e.Asset("splat-swap-"+Date.now(),"gsplat",{url:t});await new Promise(((n,i)=>{o.ready((()=>{if($)return void i(new Error("Viewer destroyed"));const a=new e.Entity("splat-swap");a.addComponent("gsplat",{asset:o,unified:!0});const s=v.scale||{x:1,y:1,z:1},r=v.invertXScale||!1,l=v.invertYScale||!1,c={x:r?-s.x:s.x,y:l?-s.y:s.y,z:r!==l?-s.z:s.z};a.setLocalScale(c.x,c.y,c.z);const p=v.position||[0,0,0];a.setPosition(p[0],p[1],-p[2]);const d=v.rotation||[0,0,0],u=0!==d[0]||0!==d[1]||0!==d[2]?[d[0]*(180/Math.PI),d[1]*(180/Math.PI),-d[2]*(180/Math.PI)]:[180,0,0];a.setEulerAngles(u[0],u[1],u[2]),R.root.addChild(a),te.set(t,a),N=t,console.log("[SplatSwap] New splat loaded and shown:",t),n()})),o.on("error",(e=>{console.error("[SplatSwap] Load error:",e),i(e)})),R.assets.add(o),R.assets.load(o)}))}const o=O.findIndex((e=>e.url===t));-1!==o&&we(o)}catch(e){console.error("[SplatSwap] Error switching splat:",e)}finally{Q=!1}}}function Se(){if(!O||0===O.length)return;const t=v.waypoints?.length||1,o=100*_e,n=Math.round(_e*Math.max(1,t-1));if(Math.abs(o-oe)<.1&&n===ne)return;oe=o,ne=n;let i=null,a=null,s=-1/0,r=-1/0;for(const e of O)-1!==e.waypointIndex?n>=e.waypointIndex&&e.waypointIndex>s&&(s=e.waypointIndex,i=e):-1!==e.percentage&&o>=e.percentage&&e.percentage>r&&(r=e.percentage,a=e);const l=a||i,c=v.sogUrl?v.sogUrl:v.splatUrl?v.splatUrl:v.fallbackUrls&&v.fallbackUrls.length>0?v.fallbackUrls[0]:"",p=l?l.url:c;p&&p!==N&&(p===c&&U&&!N?N=c:p===c&&U?(te.forEach(((e,t)=>{t!==c&&(W?ve(e):be(t))})),U&&(U.enabled=!0),N=c,console.log("[SplatSwap] Returned to primary splat")):xe(p),l&&l.skyboxUrl&&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(!$)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)}(l.skyboxUrl,l.skyboxRotation||0))}let _e=0;const Me=v.waypoints?.reduce(((e,t)=>e+(t.duration||2e3)),0)||1,Ce=void 0!==v.autoplaySpeed?v.autoplaySpeed:1e3/Me,Ee=v.loopMode;let ke;ke=!0===Ee?"loop":!1===Ee?"none":"loop"===Ee||"pingpong"===Ee||"none"===Ee?Ee:"loop";let Le=1;console.log("[StorySplat Viewer] Playback config:",{loopMode:ke,playbackSpeed:Ce,totalDuration:Me,autoPlay:v.autoPlay,rawLoopMode:v.loopMode});let Te=null,Pe=null;let Ae=0,Re=0;let ze=!1,Ie=0,De=0;const Ve=[],Fe=[],Be=[],Ue=v.fov||60;let He=Ue;function $e(t){if(!de||Ve.length<2)return;t=Math.max(0,Math.min(1,t));const o=Ve.length,n=t*(o-1),i=Math.min(Math.floor(n),o-2),a=n-i,r=Ve[i],l=Ve[i+1];Te=new e.Vec3(ee(r.x,l.x,a),ee(r.y,l.y,a),ee(r.z,l.z,a));const c=Fe[i],p=Fe[i+1];Pe=new e.Quat,Pe.slerp(c,p,a);const d=Be[i],u=Be[i+1];He=ee(d,u,a);const h=Math.round(t*(o-1));if(h!==F){const t=F;F=h;const o=v.waypoints[h],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(Ve[h].x,Ve[h].y,Ve[h].z)),s.emit("waypointChange",{index:h,waypoint:o,prevIndex:t,cameraMode:n}),m=h,g=t,it.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=g===n&&m!==n;m===n&&g!==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 m,g;s.emit("progressUpdate",{progress:Math.max(0,Math.min(1,t)),index:F}),Se()}function Oe(e,t=!1){t?Ne(e):(_e=Math.max(0,Math.min(1,e)),$e(_e))}v.waypoints&&v.waypoints.length>0&&(v.waypoints.forEach((t=>{const o=t.position?new e.Vec3(t.position.x,t.position.y,-t.position.z):new e.Vec3(0,v.playerHeight||1.6,0);Ve.push(o);const n=t.rotation?fe(t.rotation):new e.Quat;Fe.push(n);let i=t.fov||Ue;i<3.5&&(i*=180/Math.PI),Be.push(i)})),Ve.length>0&&(Te=Ve[0].clone(),Pe=Fe[0].clone(),He=Be[0])),R.on("update",(function(){if(!Te||!Pe)return;if(!de)return;const t=ie.getPosition(),o=new e.Vec3;o.lerp(t,Te,.1),ie.setPosition(o.x,o.y,o.z);const n=ie.camera;if(n&&Be.length>0){const e=ee(n.fov,He,.1);n.fov=e}ze||(Ae*=.95,Re*=.95,Math.abs(Ae)<.01&&(Ae=0),Math.abs(Re)<.01&&(Re=0));const i=new e.Quat;i.setFromEulerAngles(Re,Ae,0);const a=new e.Quat;a.mul2(Pe,i);const s=ie.getRotation(),r=new e.Quat;r.slerp(s,a,.1),ie.setRotation(r)}));const We=500/(v.transitionSpeed||1);function Ne(e,t=We){const o=_e,n=performance.now(),i=()=>{const a=performance.now()-n,s=Math.min(a/t,1);_e=o+(e-o)*(s<.5?2*s*s:(4-2*s)*s-1),$e(_e),s<1&&requestAnimationFrame(i)};requestAnimationFrame(i)}function Ge(e){if(!v.waypoints||e<0||e>=v.waypoints.length)return;if(!de)return;Ne(e/Math.max(1,v.waypoints.length-1))}function je(){if(!v.waypoints||0===v.waypoints.length)return;const e=v.scrollButtonMode||"waypoint";if("percentage"===e||"continuous"===e||"incremental"===e){const e=(v.scrollAmount||10)/100;Ne(Math.min(1,_e+e))}else{Ge(Math.min(F+1,v.waypoints.length-1))}}function qe(){if(!v.waypoints||0===v.waypoints.length)return;const e=v.scrollButtonMode||"waypoint";if("percentage"===e||"continuous"===e||"incremental"===e){const e=(v.scrollAmount||10)/100;Ne(Math.max(0,_e-e))}else{Ge(Math.max(F-1,0))}}let Xe=0,Ye=null;function Ze(e){if(!B)return;0===Xe&&(Xe=e);const t=(e-Xe)/1e3;if(Xe=e,_e+=Ce*t*Le,_e>=1)switch(console.log("[StorySplat Viewer] End of tour reached, loopMode:",ke),ke){case"loop":console.log("[StorySplat Viewer] Looping - restarting at beginning"),_e=0;break;case"pingpong":console.log("[StorySplat Viewer] Pingpong - reversing direction"),_e=1,Le=-1;break;case"none":return console.log("[StorySplat Viewer] No loop - stopping playback"),_e=1,Ke(),void s.emit("playbackComplete");default:return console.log("[StorySplat Viewer] Unknown loopMode, stopping:",ke),_e=1,Ke(),void s.emit("playbackComplete")}else if(_e<=0)if("pingpong"===ke)console.log("[StorySplat Viewer] Pingpong - reversing to forward"),_e=0,Le=1;else _e=0;$e(_e),Ye=requestAnimationFrame(Ze)}function Je(){B||!v.waypoints||v.waypoints.length<2||(B=!0,Xe=0,Le=1,s.emit("playbackStart"),Ye=requestAnimationFrame(Ze))}function Ke(){B=!1,Ye&&(cancelAnimationFrame(Ye),Ye=null),s.emit("playbackStop")}const Qe=R.graphicsDevice.canvas;Qe.addEventListener("wheel",(e=>{if(!de)return;e.preventDefault();const t=v.scrollSpeed||.1,o=(v.scrollAmount||100)/100*t*.05,n=e.deltaY>0?o:-o;Oe(Math.max(0,Math.min(1,_e+n)))}),{passive:!1}),Qe.addEventListener("pointerdown",(e=>{de&&(ze=!0,Ie=e.clientX,De=e.clientY)}),{capture:!0}),Qe.addEventListener("pointermove",(e=>{if(!de||!ze)return;const t=e.clientX-Ie,o=e.clientY-De;Ie=e.clientX,De=e.clientY;Ae+=.3*-t,Re+=.3*-o,Re=Math.max(-60,Math.min(60,Re))}),{capture:!0}),Qe.addEventListener("pointerup",(()=>{ze=!1}),{capture:!0}),Qe.addEventListener("pointerleave",(()=>{ze=!1}),{capture:!0});const et=[],tt=[];function ot(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 nt=[],it=new Map,at=new Map,st=new Map,rt={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 lt(t,o){return new Promise(((n,i)=>{if(st.has(t))return void n(st.get(t));const a=new e.Asset(t,"texture",{url:o});a.on("load",(()=>{if($)return console.log(`[Particle] Ignoring texture load - viewer was destroyed: ${t}`),void i(new Error("Viewer destroyed"));const e=a.resource;st.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 ct(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),p=n(t.direction1,0),d=n(t.direction2,0),u=((t.minLifeTime??1)+(t.maxLifeTime??3))/2;let h=e.EMITTERSHAPE_BOX,m=new e.Vec3(.1,.1,.1),g=new e.Vec3(0,0,0);if("sphere"===t.emitterType||"sphere"===t.emitterShape){h=e.EMITTERSHAPE_SPHERE;const o=t.emitterRadius||.1;m=new e.Vec3(o,o,o)}else if("box"===t.emitterShape&&t.emitBoxMin&&t.emitBoxMax){h=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?(h=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*u,S=b*u,_=w*u,M=t.minAngularSpeed??0,C=t.maxAngularSpeed??t.angularSpeed??0,E=t.minInitialRotation??0,k=t.maxInitialRotation??0,L=t.minSize??.1,T=t.maxSize??.3,P=t.minScaleX??1,A=t.maxScaleX??1,R=t.minScaleY??1,z=t.maxScaleY??1,I=t.minEmitPower??1,D=t.maxEmitPower??2,V=(p.x+d.x)/2,F=(p.y+d.y)/2,B=-(p.z+d.z)/2;return o.addComponent("particlesystem",{numParticles:t.numParticles||500,lifetime:u,rate:t.emitRate||50,emitterShape:h,emitterExtents:m,emitterRadius:t.emitterRadius||.1,startAngle:E,startAngle2:0!==k?k:360,radialSpeedGraph:new e.Curve([0,I,1,D]),localVelocityGraph:new e.CurveSet([[0,V*I],[0,F*I],[0,B*I]]),localVelocityGraph2:new e.CurveSet([[0,V*D],[0,F*D],[0,B*D]]),velocityGraph:new e.CurveSet([[0,0,1,x],[0,0,1,S],[0,0,1,_]]),scaleGraph:new e.Curve([0,L*P,1,T*A]),scaleGraph2:new e.Curve([0,L*R,1,T*z]),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),o.setPosition(a.x+g.x,a.y+g.y,-a.z+g.z),console.log(`[Particle] Entity configured at position: (${a.x+g.x}, ${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: ${E} - ${k}`),o}const pt=new Map;function dt(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 ut(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 ht(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 pt.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&&(vt(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=ie.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 p=[],d=i.anim;if(d&&(console.log("[CustomMesh] Found modelEntity.anim component - using simple approach"),p.push({type:"pc-anim",component:d,modelEntity:i})),0===p.length){function u(e,t=0){e.anim&&!p.find((t=>t.component===e.anim))&&(p.push({type:"anim",component:e.anim,node:e}),console.log("[CustomMesh] Found existing ANIM component on:",e.name)),e.animation&&(p.push({type:"animation",component:e.animation,node:e}),console.log("[CustomMesh] Found ANIMATION (legacy) component on:",e.name)),e.children&&e.children.forEach((e=>u(e,t+1)))}u(i)}if(0===p.length&&r){const h=o.resource.animations;console.log("[CustomMesh] No anim component found - falling back to GLB skeleton approach"),console.log("[CustomMesh] GLB has",h.length,"embedded animations");try{p.push({type:"glb-skeleton",modelEntity:i,asset:o,animations:h,animationNames:h.map((e=>e.resource?.name||e.name||"Animation")),isPlaying:!1,currentTime:0}),console.log("[CustomMesh] GLB skeleton animations stored:",h.map((e=>e.resource?.name||e.name)))}catch(m){console.error("[CustomMesh] Error setting up GLB animations:",m)}}if(p.length>0){l.allAnimComponents=p,l.animComponent=p[0].component,console.log("[CustomMesh] Total animation components for",t.name,":",p.length,"- Types:",p.map((e=>e.type)).join(", "));const g=t.interaction?.animationAutoPlay;g&&(p.forEach((e=>{dt(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=ie.camera.screenToWorld(s,r,ie.camera.nearClip),c=ie.camera.screenToWorld(s,r,ie.camera.farClip),p=(new e.Vec3).sub2(c,l).normalize(),d=t.modelEntity;if(d&&mt(d,l,p))return!0;if(mt(t,l,p))return!0;const u=t.getPosition(),h=(new e.Vec3).sub2(u,l).dot(p);if(h>0){const o=(new e.Vec3).add2(l,p.clone().mulScalar(h)).distance(u),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,p=o.interaction?.directLinkTriggerMode||i;let d=!1;const u=()=>{if(a.style.cursor="pointer","hover"===r&&o.interaction?.triggerUIPopup&&yt(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=>dt(e))),n.isAnimPlaying=!0,console.log("[CustomMesh] Playing animation on hover for:",o.name))}"hover"===p&&o.interaction?.triggerDirectLink&&ft(o)},h=()=>{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=>ut(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&&yt(o),"click"===c&&o.interaction?.playModelAnimation){const e=n.allAnimComponents;e&&e.length>0&&(n.isAnimPlaying?(e.forEach((e=>ut(e))),n.isAnimPlaying=!1,console.log("[CustomMesh] Paused",e.length,"animations on click for:",o.name)):(e.forEach((e=>dt(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"===p&&o.interaction?.triggerDirectLink&&ft(o)},g=e=>{s(e.clientX,e.clientY)&&m()},y=e=>{const t=s(e.clientX,e.clientY);t&&!d?(d=!0,u()):!t&&d&&(d=!1,h())},f=()=>{d&&(d=!1,h())};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 mt(t,o,n){const i=t.render;if(i&&i.meshInstances)for(const e of i.meshInstances)if(e.aabb){if(gt(o,n,e.aabb))return!0}const a=t.model;if(a&&a.meshInstances)for(const e of a.meshInstances)if(e.aabb){if(gt(o,n,e.aabb))return!0}const s=t.children;if(s)for(const t of s)if(t instanceof e.Entity&&mt(t,o,n))return!0;return!1}function gt(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],p=n[r]??n.data?.[o],d=i[r]??i.data?.[o];if(Math.abs(c)<1e-8){if(l<p||l>d)return!1}else{let e=(p-l)/c,t=(d-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 yt(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"};P.showHotspotPopup?P.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 ft(e){e.interaction?.triggerDirectLink&&e.interaction.directLinkUrl&&window.open(e.interaction.directLinkUrl,"_blank")}function vt(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 bt(){const e=R.graphicsDevice.canvas;pt.forEach((t=>{const o=t.entity;o.meshClickHandler&&e.removeEventListener("click",o.meshClickHandler),o.destroy()})),pt.clear()}function wt(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 xt(){v.lights&&0!==v.lights.length?(console.log(`[StorySplat Viewer] Creating ${v.lights.length} custom lights...`),v.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:wt(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:wt(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:wt(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=wt(t.groundColor),n=wt(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=wt(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:wt(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 St(e){switch(e){case"linear":return"linear";case"inverse":return"inverse";default:return"exponential"}}s.on("progressUpdate",(()=>{!function(){const e=100*_e,t=v.waypoints?.length||1,o=Math.round(_e*Math.max(1,t-1));pt.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);vt(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 _t=new Set;const Mt=[];function Ct(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 G(R,t,{autoPlay:!0,onReady:()=>{if($)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()}});Mt.push(n)}else{const n=new Image;n.crossOrigin="anonymous",n.onload=()=>{if($)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 Et(){v.hotspots&&0!==v.hotspots.length?(console.log(`[StorySplat Viewer] Creating ${v.hotspots.length} hotspots...`),v.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},p=180/Math.PI,d=(c._x??c.x??0)*p,u=(c._y??c.y??0)*p+180,h=(c._z??c.z??0)*p;if(n.setEulerAngles(d,u,h),"sphere"===t.type){n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=ot(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=Ct(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,p=(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}},d=p(i,!1,c),u=d.video,h=d.texture,m=new e.StandardMaterial;m.diffuseMap=h,m.emissiveMap=h,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=h,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=p(a,!0,!1);g=o.video,y=o.texture,m.opacityMap=y,m.opacityMapChannel="r",m.blendType=e.BLEND_PREMULTIPLIED,m.alphaTest=.01,u.addEventListener("play",(()=>{g&&g.paused&&(g.currentTime=u.currentTime,g.play().catch(console.warn))})),u.addEventListener("pause",(()=>{g&&!g.paused&&g.pause()})),u.addEventListener("seeked",(()=>{g&&(g.currentTime=u.currentTime)})),console.log(`[Hotspot] iOS alpha mask video enabled: ${t.title}, alphaUrl=${a.substring(0,50)}...`)}m.update(),R.on("update",(()=>{u.readyState===u.HAVE_ENOUGH_DATA&&h.upload(),g&&y&&g.readyState===g.HAVE_ENOUGH_DATA&&y.upload()}));const f={x:s,y:r,z:l};if(u.addEventListener("loadedmetadata",(()=>{const e=u.videoWidth,t=u.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=u,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);nt.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),ie&&ie.getPosition){const e=ie.getPosition(),t=ie.forward,o=ie.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,u,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 p=document.createElement("canvas"),d=p.getContext("2d"),u=new Image;u.crossOrigin="anonymous",u.onload=()=>{p.width=u.width||256,p.height=u.height||256,d.drawImage(u,0,0);const h=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});h.setSource(p),a.diffuseMap=h,i||(a.emissiveMap=h),a.opacityMap=h,a.alphaTest=.01,a.update(),n.render.material=a;const m=p.width/p.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=p,n.gifTexture=h,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&&(d.clearRect(0,0,p.width,p.height),d.drawImage(u,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}`)},u.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=ot(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},u.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=ot(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);nt.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),ie&&ie.getPosition){const e=ie.getPosition(),t=ie.forward,o=ie.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(ie.getPosition()),n.rotateLocal(90,180,0))}))}t.visibilityRange&&(n.visibilityRange=t.visibilityRange,n.enabled=!1),R.root.addChild(n),et.push(n),console.log(`[StorySplat Viewer] Created hotspot: ${t.title||"Untitled"}`)}))):console.log("[StorySplat Viewer] No hotspots to create")}function kt(){const e=100*_e,t=v.waypoints?.length||1,o=Math.round(_e*Math.max(1,t-1)),n=ie.getPosition();et.forEach((t=>{const i=t.hotspotData;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,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?(Lt(t,i),console.log(`[Hotspot] Proximity play: ${i.title}, distance=${o.toFixed(2)}`)):o>a&&t.isVideoPlaying&&(Tt(t),console.log(`[Hotspot] Proximity pause: ${i.title}, distance=${o.toFixed(2)}`))}"scroll"===o&&(a&&!t.isVideoPlaying?(Lt(t,i),console.log(`[Hotspot] Scroll play: ${i.title}, scroll=${e.toFixed(1)}%`)):!a&&t.isVideoPlaying&&(Tt(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 Lt(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 Tt(e){const t=e.videoElement,o=e.alphaVideoElement;t&&(t.pause(),o&&o.pause(),e.isVideoPlaying=!1)}function Pt(){const e=100*_e,t=v.waypoints?.length||1,o=Math.round(_e*Math.max(1,t-1)),n=ie.getPosition();tt.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}`),Rt(i)):o>a&&(t.proximityTriggered=!1)}}))}function At(t,o){const n=ie.camera.screenToWorld(t,o,ie.camera.nearClip),i=ie.camera.screenToWorld(t,o,ie.camera.farClip);let a=null;if(tt.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 Rt(e){if(e.targetSceneId){console.log(`[StorySplat Viewer] Portal activated: navigating to scene ${e.targetSceneId}`),s.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..."),Ke(),et.forEach((e=>{e.videoElement&&(e.videoElement.pause(),e.videoElement.src=""),e.alphaVideoElement&&(e.alphaVideoElement.pause(),e.alphaVideoElement.src="")})),Mt.forEach((e=>e.destroy())),Mt.length=0,bt(),et.forEach((e=>{e.destroy()})),et.length=0,tt.forEach((e=>{e.destroy()})),tt.length=0,U&&(U.destroy(),U=null);R.__htmlMeshManager&&R.__htmlMeshManager.destroy();R.__customScriptSystem&&R.__customScriptSystem.dispose();console.log("[Portal] Cleanup complete")}(),await new Promise((e=>setTimeout(e,100))),A&&A.parentNode&&A.remove();const s=await K(t,a,{});return zt(t),console.log(`[Portal] Successfully navigated to scene: ${e.targetSceneId}`),s}catch(e){console.error("[Portal] Navigation failed:",e),zt(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 zt(e){const t=e.querySelector(".storysplat-portal-loading");t&&t.remove()}s.on("progressUpdate",(()=>{kt()})),setTimeout((()=>{kt()}),100),s.on("progressUpdate",(()=>{Pt()})),setTimeout((()=>{Pt()}),100);const It=R.graphicsDevice.canvas;function Dt(t,o){const n=ie.camera.screenToWorld(t,o,ie.camera.nearClip),i=ie.camera.screenToWorld(t,o,ie.camera.farClip);let a=null;if(et.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 Vt=null,Ft=!1;const Bt=t.querySelector(".storysplat-hotspot-popup"),Ut=t.querySelector(".storysplat-hotspot-overlay");async function Ht(e,t){if("explore"!==pe)return;console.log("[StorySplat Viewer] Double-click focus at:",e,t);const o=Dt(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 le.focus(e,!1)}try{const o=.25;ue.resize(Math.floor(It.clientWidth*o),Math.floor(It.clientHeight*o));const n=R.scene.layers.getLayerByName("World");if(!n)return void console.warn("[StorySplat Viewer] World layer not found");ue.prepare(ie.camera,R.scene,[n]);const i=Math.floor(e*o),a=Math.floor(t*o),s=await ue.getWorldPointAsync(i,a);if(s){const e=ie.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 le.focus(s,!1)}const r=await ue.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)),le.focus(e,!1)}else console.log("[StorySplat Viewer] No pick result at click point")}catch(e){console.warn("[StorySplat Viewer] Picking failed:",e)}}Bt&&(Bt.addEventListener("mouseenter",(()=>{Ft=!0})),Bt.addEventListener("mouseleave",(()=>{Ft=!1,Vt&&"hover"===Vt.activationMode&&(Bt.classList.remove("visible"),Ut&&Ut.classList.remove("visible"),Vt=null)}))),It.addEventListener("mousemove",(e=>{const o=It.getBoundingClientRect(),n=e.clientX-o.left,i=e.clientY-o.top,a=At(n,i);if(a&&a.portal){const e=a.portal.activationMode||"click";return void(It.style.cursor="click"===e?"pointer":"default")}const s=Dt(n,i);if(s&&s.hotspot){const e=s.hotspot;"click"===e.activationMode||"hover"===e.activationMode||"video"===e.type?It.style.cursor="pointer":It.style.cursor="default","hover"===e.activationMode&&Vt!==e&&(Vt=e,(e.information||e.photoUrl||e.iframeUrl||e.externalLinkUrl)&&u(t,e))}else if(It.style.cursor="default",Vt&&"hover"===Vt.activationMode&&!Ft){const e=t.querySelector(".storysplat-hotspot-popup"),o=t.querySelector(".storysplat-hotspot-overlay");e&&e.classList.remove("visible"),o&&o.classList.remove("visible"),Vt=null}})),It.addEventListener("click",(e=>{const o=It.getBoundingClientRect(),n=e.clientX-o.left,i=e.clientY-o.top,a=At(n,i);if(null!==a&&a.portal){const e=a.portal;console.log("[StorySplat Viewer] Portal clicked:",e.title||e.targetSceneName);return void("click"===(e.activationMode||"click")&&Rt(e))}const s=Dt(n,i);if(null!==s){const e=s.entity,o=s.hotspot;if(console.log("[StorySplat Viewer] Hotspot clicked:",o.title),e.audioElements&&e.audioElements.audio){const t=e.audioElements,n=t.audio;n.paused?(t.audioCtx&&"suspended"===t.audioCtx.state&&t.audioCtx.resume(),n.play().catch((e=>console.error("[Audio] Hotspot audio play failed:",e))),console.log("[Audio] Hotspot audio started:",o.title)):(n.pause(),console.log("[Audio] Hotspot audio paused:",o.title))}if(e.videoElement&&("click"===e.mediaTriggerMode||"autoplay"===e.mediaTriggerMode)){const t=e.videoElement;e.alphaVideoElement,"autoplay"===e.mediaTriggerMode&&!t.paused&&t.muted?(t.muted=(o.videoMuted,!1),console.log("[Hotspot] Video unmuted by click")):t.paused?(Lt(e,o),console.log("[Hotspot] Video started")):(Tt(e),console.log("[Hotspot] Video paused"))}const n=o.activationMode||"click",i=o.title||o.information||o.photoUrl||o.iframeUrl||o.externalLinkUrl;"click"===n&&i&&u(t,o);const a=o.teleportWaypoint??o.teleportToWaypoint,r=o.teleportPercent??o.teleportToPercent,l=o.teleportMode||"animate";let c=null;if(void 0!==a&&-1!==a){const e=v.waypoints?.length||1,t=Math.max(0,Math.min(a,e-1));c=e>1?t/(e-1):0,console.log("[Hotspot] Teleporting to waypoint:",t,"(progress:",c,", mode:",l,")")}else void 0!==r&&-1!==r&&(c=Math.max(0,Math.min(r/100,1)),console.log("[Hotspot] Teleporting to percent:",r,"(progress:",c,", mode:",l,")"));null!==c&&("instant"===l?(_e=c,$e(_e)):Ne(c,800))}})),It.addEventListener("dblclick",(e=>{const t=It.getBoundingClientRect();Ht(e.clientX-t.left,e.clientY-t.top)}));let $t=0;It.addEventListener("touchend",(e=>{if(1!==e.changedTouches.length)return;const t=Date.now();if(t-$t<300){const t=e.changedTouches[0],o=It.getBoundingClientRect();Ht(t.clientX-o.left,t.clientY-o.top),$t=0}else $t=t})),ye(.2,"Initializing..."),async function(){const t=[];v.lodMetaUrl&&t.push(v.lodMetaUrl),v.sogUrl&&t.push(v.sogUrl),v.splatUrl&&t.push(v.splatUrl),v.fallbackUrls&&t.push(...v.fallbackUrls),console.log("[StorySplat Viewer] Loading splat from URLs:",t),console.log("[StorySplat Viewer] URL priorities: LOD streaming > SOG > PLY/Other"),ye(.3,"Loading splat...");for(const i of t)if(i)try{console.log("[StorySplat Viewer] Trying URL:",i);i.split(".").pop()?.toLowerCase();const t="gsplat",a=new e.Asset("splat-"+Date.now(),t,{url:i});return a.on("progress",((e,t)=>{if(t>0){ye(.3+e/t*.6,`Loading... ${Math.round(e/t*100)}%`)}})),await new Promise(((r,l)=>{let c=!1;const p=i.includes("lod-meta.json"),d=i.includes(".sog")||p,u=d?e=>{if(c)return;const t=e.reason?.message||String(e.reason);(t.includes("shape")||t.includes("upgradeMeta"))&&(console.warn("[StorySplat Viewer] Caught SOG parse error, will try fallback:",t),c=!0,e.preventDefault(),s.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:i}),l(new Error(`SOG parse error: ${t}`)))}:null;d&&u&&window.addEventListener("unhandledrejection",u);const h=()=>{d&&u&&window.removeEventListener("unhandledrejection",u)};a.ready((()=>{if($)return console.log("[StorySplat Viewer] Ignoring splat load - viewer was destroyed"),h(),void l(new Error("Viewer destroyed"));console.log("[StorySplat Viewer] Asset ready (loaded + resources ready)");try{U=new e.Entity("splat"),U.addComponent("gsplat",{asset:a,unified:!0});const t=U.gsplat;t&&J(i)&&(t.lodDistances=V.lodDistances,console.log("[StorySplat Viewer] LOD distances set for streaming format:",V.lodDistances));const s=v.scale||{x:1,y:1,z:1},l=v.invertXScale||!1,p=v.invertYScale||!1,d={x:l?-s.x:s.x,y:p?-s.y:s.y,z:l!==p?-s.z:s.z};U.setLocalScale(d.x,d.y,d.z);const u=v.position||[0,0,0];U.setPosition(u[0],u[1],-u[2]);const m=v.rotation||[0,0,0],g=0!==m[0]||0!==m[1]||0!==m[2];let y;y=g?[m[0]*(180/Math.PI),m[1]*(180/Math.PI),-m[2]*(180/Math.PI)]:[180,0,0],U.setEulerAngles(y[0],y[1],y[2]),console.log("[StorySplat Viewer] Splat transform applied:",{scale:d,position:[u[0],u[1],-u[2]],rotation:y,hasUserRotation:g,invertXScale:l,invertYScale:p}),R.root.addChild(U),console.log("[StorySplat Viewer] Splat entity added to scene"),U.gsplat?.material&&(U.gsplat.material.setParameter("alphaClip",.1),console.log("[StorySplat Viewer] GSplat alphaClip set for picking support"));const f=n.revealEffect||o.revealEffect||"medium",b=T(f);if(b){U.addComponent("script");const e=k();H=U.script?.create(e),H&&(H.enabled=!1,H.center.set(0,0,0),H.speed=b.speed,H.acceleration=b.acceleration,H.delay=b.delay,H.oscillationIntensity=b.oscillationIntensity,H.dotTint.set(b.dotTint.r,b.dotTint.g,b.dotTint.b),H.waveTint.set(b.waveTint.r,b.waveTint.g,b.waveTint.b),H.endRadius=b.endRadius,console.log("[StorySplat Viewer] Reveal effect configured (waiting to start):",f))}else console.log("[StorySplat Viewer] Reveal effect disabled");setTimeout((()=>{c||(h(),r())}),100)}catch(e){console.error("[StorySplat Viewer] Error during gsplat setup:",e),h(),l(e)}})),a.on("error",(e=>{console.error("[StorySplat Viewer] Asset load error:",{url:i,assetType:t,error:e,message:e?.message||"Unknown error",status:e?.status||e?.statusCode||"N/A",stack:e?.stack}),h(),l(e)})),R.assets.add(a),R.assets.load(a)})),s.emit("loaded"),void console.log("[StorySplat Viewer] Splat loaded successfully")}catch(e){console.warn("[StorySplat Viewer] Failed to load:",i,e)}console.error("[StorySplat Viewer] Failed to load splat from any URL"),s.emit("error",new Error("Failed to load splat from any URL"))}().then((()=>{if(ye(1,"Ready!"),Et(),v.portals&&0!==v.portals.length?(console.log(`[StorySplat Viewer] Creating ${v.portals.length} portals...`),v.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},p=180/Math.PI,d=(c._x??c.x??0)*p,u=(c._y??c.y??0)*p+180,h=(c._z??c.z??0)*p;if(n.setEulerAngles(d,u,h),"sphere"===t.type){n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=ot(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=Ct(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{n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=ot(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(ie.getPosition()),n.rotateLocal(90,180,0))})),t.visibilityRange&&(n.visibilityRange=t.visibilityRange,n.enabled=!1),R.root.addChild(n),tt.push(n),console.log(`[StorySplat Viewer] Created portal: ${t.title||t.targetSceneName||"Untitled"} -> ${t.targetSceneId}`)}))):console.log("[StorySplat Viewer] No portals to create"),v.waypoints&&0!==v.waypoints.length&&(v.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:St(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=it.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),it.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}`)}}))})),it.size>0&&console.log(`[StorySplat Viewer] Setup ${it.size} waypoint audio sources`)),console.log("[StorySplat Viewer] 🎆 Calling initParticleSystems..."),async function(){if(console.log("═══════════════════════════════════════"),console.log("🎆 PARTICLE SYSTEM INITIALIZATION"),console.log("═══════════════════════════════════════"),console.log("[Particle] config.particles:",v.particles),console.log("[Particle] Type:",typeof v.particles),console.log("[Particle] Is Array:",Array.isArray(v.particles)),!v.particles||0===v.particles.length)return console.log("[Particle] ⚠️ No particle systems to create (particles array is empty or undefined)"),void console.log("═══════════════════════════════════════");console.log(`[Particle] 📋 Found ${v.particles.length} particle system(s) to create`);for(let e=0;e<v.particles.length;e++){const t=v.particles[e];console.log(`[Particle] --- Particle System ${e+1}/${v.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=rt[o]||rt.flare,i=o,console.log(`[Particle] Texture: ${o} -> ${n.substring(0,60)}...`)),console.log("[Particle] Loading texture...");const a=await lt(i,n);console.log("[Particle] ✅ Texture loaded:",a.name),console.log("[Particle] Creating entity...");const s=ct(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-${at.size}`).replace(/[^a-zA-Z0-9]/g,"_");at.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: ${at.size}/${v.particles.length} particle systems created`),console.log("[Particle] Active particle systems:",Array.from(at.keys())),console.log("═══════════════════════════════════════")}(),async function(){v.customMeshes&&0!==v.customMeshes.length?(console.log(`[StorySplat Viewer] Creating ${v.customMeshes.length} custom meshes...`),console.log("[StorySplat Viewer] All custom mesh configs:",v.customMeshes),setTimeout((()=>{let e=0,t=0;v.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{ht(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] ${v.customMeshes.length} custom meshes queued for loading`)):console.log("[StorySplat Viewer] No custom meshes to create")}(),function(){const t=v.skybox?.url||v.skyboxUrl;if(!t)return void console.log("[StorySplat Viewer] No skybox configured");const o=v.skybox?.rotation??v.skyboxRotation??0,n=v.skybox?.intensity??1,i=v.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"),r=new e.StandardMaterial;r.useLighting=!1,r.cull=e.CULLFACE_FRONT;const l=new e.Asset("skybox-texture","texture",{url:t});if(R.assets.add(l),l.ready((t=>{const o=t.resource;if(r.emissiveMap=o,r.emissive=new e.Color(n,n,n),r.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)}})),l.on("error",(e=>{console.error("[StorySplat Viewer] Failed to load skybox texture:",e)})),R.assets.load(l),s.addComponent("render",{type:"sphere",material:r,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=ie.getPosition();s.setPosition(e.x,e.y,e.z)})),R.root.addChild(s),console.log("[StorySplat Viewer] Skybox created with rotation:",o,"intensity:",n)}(),xt(),H&&(H.enabled=!0,console.log("[StorySplat Viewer] Reveal effect started immediately")),setTimeout((()=>{P.preloader&&p(P.preloader)}),200),function(){if(!v.includeXR)return;if(!R.xr)return void console.warn("[StorySplat Viewer] WebXR not supported in this browser");const t=R.xr,o=v.xrMode||"both";"vr"!==o&&"both"!==o||(t.isAvailable(e.XRTYPE_VR)&&(P.vrButton?.classList.add("available"),console.log("[StorySplat Viewer] VR is available")),t.on("available:"+e.XRTYPE_VR,(e=>{e?P.vrButton?.classList.add("available"):P.vrButton?.classList.remove("available")}))),"ar"!==o&&"both"!==o||(t.isAvailable(e.XRTYPE_AR)&&(P.arButton?.classList.add("available"),console.log("[StorySplat Viewer] AR is available")),t.on("available:"+e.XRTYPE_AR,(e=>{e?P.arButton?.classList.add("available"):P.arButton?.classList.remove("available")}))),t.on("start",(()=>{he=!0,console.log("[StorySplat Viewer] XR session started"),"vr"===me?(P.vrButton?.classList.add("active"),P.vrButton.textContent="Exit VR"):"ar"===me&&(P.arButton?.classList.add("active"),P.arButton.textContent="Exit AR"),le.disable(),ce&&ce.disable(),s.emit("xrStart",{type:me})})),t.on("end",(()=>{he=!1,console.log("[StorySplat Viewer] XR session ended"),P.vrButton?.classList.remove("active"),P.arButton?.classList.remove("active"),P.vrButton&&(P.vrButton.textContent="VR"),P.arButton&&(P.arButton.textContent="AR"),me=null,"explore"===pe?le.enable():"walk"===pe&&ce&&ce.enable(),s.emit("xrEnd",{})})),P.vrButton&&P.vrButton.addEventListener("click",(()=>{he&&"vr"===me?t.end():!he&&t.isAvailable(e.XRTYPE_VR)&&(me="vr",ie.camera.startXr(e.XRTYPE_VR,e.XRSPACE_LOCALFLOOR,{callback:e=>{e&&(console.error("[StorySplat Viewer] Failed to start VR:",e),me=null)}}))})),P.arButton&&P.arButton.addEventListener("click",(()=>{he&&"ar"===me?t.end():!he&&t.isAvailable(e.XRTYPE_AR)&&(me="ar",ie.camera.startXr(e.XRTYPE_AR,e.XRSPACE_LOCALFLOOR,{callback:e=>{e&&(console.error("[StorySplat Viewer] Failed to start AR:",e),me=null)}}))}))}(),v.htmlMeshes&&v.htmlMeshes.length>0){console.log("[StorySplat Viewer] Setting up HTML meshes:",v.htmlMeshes.length);const e=function(e,t){const o=new j(e);for(const e of t)o.createMesh(e);return o}(R,v.htmlMeshes);R.__htmlMeshManager=e,s.on("progressUpdate",(()=>{const t=100*_e,o=v.waypoints?.length||1,n=Math.round(_e*Math.max(1,o-1));e.updateVisibility(t,n)}))}if(v.customScript&&""!==v.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 p=new q(i);return p.initialize({app:t,camera:o,pc:e,canvas:n,getScrollPercentage:a,getCurrentWaypointIndex:s,getHotspots:r,getSplats:l,getHTMLMeshes:c}),p.execute(),p}(R,ie,A,v.customScript,(()=>_e),(()=>F),(()=>et),(()=>U?[U]:[]),(()=>v.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&&v.waypoints&&v.waypoints.length>0){const e=parseInt(t,10);!isNaN(e)&&e>=0&&e<v.waypoints.length&&(console.log("[StorySplat Viewer] URL param: navigating to waypoint",e),Ge(e))}"true"!==o||n.autoPlay||v.autoPlay||(console.log("[StorySplat Viewer] URL param: starting autoplay"),Je())}catch(e){console.warn("[StorySplat Viewer] Could not parse URL parameters:",e)}s.emit("ready"),console.log("[StorySplat Viewer] Ready"),b&&(d(P,{nextWaypoint:je,prevWaypoint:qe,play:Je,pause:Ke,isPlaying:()=>B,getCurrentWaypointIndex:()=>F,getWaypointCount:()=>v.waypoints?.length||0,getWaypoints:()=>v.waypoints||[],setCameraMode:ge,on:(e,t)=>s.on(e,t)},L,x.buttonLabels),P.returnWaypointButton&&v.waypoints&&v.waypoints.length>0&&P.returnWaypointButton.addEventListener("click",(()=>{const e=ie.getPosition();let t=0,o=1/0;v.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)),ge("tour"),Ge(t);const n=P.scrollControls?.querySelectorAll(".storysplat-mode-btn");n?.forEach((e=>{const t=e.getAttribute("data-mode");e.classList.toggle("selected","tour"===t)}))})),s.emit("progressUpdate",{progress:_e,index:F}),v.waypoints&&v.waypoints.length>0&&s.emit("waypointChange",{index:0,waypoint:v.waypoints[0],prevIndex:-1})),(n.autoPlay||v.autoPlay)&&Je()})).catch((e=>{console.error("[StorySplat Viewer] Failed to initialize:",e),P.preloader&&p(P.preloader),s.emit("error",e)}));const Ot=()=>{R.resizeCanvas()};window.addEventListener("resize",Ot);const Wt={app:R,canvas:A,goToWaypoint:Ge,nextWaypoint:je,prevWaypoint:qe,getCurrentWaypointIndex:()=>F,getWaypointCount:()=>v.waypoints?.length||0,setPosition:(e,t,o)=>ie.setPosition(e,t,o),setRotation:(e,t,o)=>ie.setEulerAngles(e,t,o),getPosition:()=>{const e=ie.getPosition();return{x:e.x,y:e.y,z:e.z}},getRotation:()=>{const e=ie.getEulerAngles();return{x:e.x,y:e.y,z:e.z}},play:Je,pause:Ke,stop:function(){Ke(),Oe(0)},isPlaying:()=>B,destroy:()=>{$=!0,Ke(),window.removeEventListener("resize",Ot),P.preloader&&P.preloader.remove(),P.scrollControls&&P.scrollControls.remove(),P.fullscreenButton&&P.fullscreenButton.remove(),P.helpButton&&P.helpButton.remove(),P.helpPanel&&P.helpPanel.remove(),P.waypointInfo&&P.waypointInfo.remove(),P.watermark&&P.watermark.remove();const e=document.getElementById("storysplat-viewer-styles");e&&e.remove(),t.classList.remove("storysplat-viewer-container"),Mt.forEach((e=>e.destroy())),Mt.length=0,bt(),ce&&ce.destroy(),R.__customScriptSystem&&R.__customScriptSystem.dispose(),R.__htmlMeshManager&&R.__htmlMeshManager.destroy(),R.destroy(),A.remove()},resize:Ot,navigateToScene:async e=>{const t={targetSceneId:e,activationMode:"click"};await Rt(t)},on:(e,t)=>s.on(e,t),off:(e,t)=>s.off(e,t)};return Wt}async function Q(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),K(e,i,o)}function ee(e,t,o){return e+(t-e)*o}class te extends Error{constructor(e){super(`Scene not found: ${e}`),this.name="SceneNotFoundError"}}class oe extends Error{constructor(e,t){super(e),this.name="SceneApiError",this.statusCode=t}}const ne="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 ie(e,t,o={}){const n=o.baseUrl||ne;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 te(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 oe(o,s.status)}const r=await s.json();if(!r.success||!r.data)throw new oe("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:p,...d}=o;return K(e,l,d)}async function ae(e,t={}){const o=`${t.baseUrl||ne}/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 te(e);throw new oe(`API error: ${i.status}`,i.status)}const a=await i.json();return{...a,userName:a.userName||"Unknown",userSlug:a.userSlug||"unknown"}}export{L as REVEAL_PRESETS,oe as SceneApiError,te as SceneNotFoundError,K as createViewer,ie as createViewerFromSceneId,Q as createViewerFromUrl,a as exportPropsToViewerConfig,ae as fetchSceneMeta,o as generateHTML,n as generateHTMLFromUrl,T as getRevealPreset,i 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,o={}){const{cdnUrl:n="https://unpkg.com/storysplat-viewer@2/dist/storysplat-viewer.umd.js",title:i=e.name||"StorySplat Scene",description:a=`Interactive 3D scene: ${e.name||"StorySplat"}`,faviconUrl:s,customCSS:r=""}=o,l=s?`<link rel="icon" href="${t(s)}" />`:"",c=r?`<style>${r}</style>`:"",p=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),d=p.replace(/<\/script/gi,"<\\/script");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(a)}">\n <title>${t(i)}</title>\n ${l}\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 ${c}\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(n)}"><\/script>\n <script>\n (function() {\n 'use strict';\n\n var sceneData = ${d};\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 });\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 n(e,t={}){const n=await fetch(e);if(!n.ok)throw new Error(`Failed to fetch scene: ${n.statusText}`);return o(await n.json(),t)}function i(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},defaultCameraMode:e.defaultCameraMode||"orbit",allowedCameraModes:e.allowedCameraModes||["orbit","first-person","drone"],cameraMovementSpeed:e.cameraMovementSpeed,cameraRotationSensitivity:e.cameraRotationSensitivity,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}}function a(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,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,includeXR:e.includeXR,xrMode:e.xrMode,customScript:e.customScript}}const s={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 r(e,t){return e?.[t]||s[t]}function l(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 }\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 /* 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-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 c(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 d(e,t,o="tour",n){const i=e.scrollControls?.querySelector(".storysplat-btn-prev"),a=e.scrollControls?.querySelector(".storysplat-btn-next"),r=e.scrollControls?.querySelector(".storysplat-btn-play");if(i&&i.addEventListener("click",(()=>t.prevWaypoint())),a&&a.addEventListener("click",(()=>t.nextWaypoint())),r){const e=e=>{r.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>'};r.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,p=-1;t.on("progressUpdate",(({progress:t})=>{const o=100*Math.max(0,Math.min(1,t)),i=Math.round(o),a=performance.now();var r,l;a-c<33||(c=a,e.progressBar&&(e.progressBar.style.width=`${o}%`),e.progressText&&i!==p&&(p=i,e.progressText.innerHTML="",e.progressText.textContent=(r=n,l=i,(r?.percentageFormat||s.percentageFormat).replace("{n}",String(l)))))}));let d=-1;t.on("waypointChange",(({index:o,waypoint:n})=>{if(o===d)return;if(d=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 h(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 u(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 m(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 g(e,t){e.cameraModeToggle&&(t?e.cameraModeToggle.classList.add("visible"):e.cameraModeToggle.classList.remove("visible"))}function y(e,t){e.cameraModeToggle&&(e.cameraModeToggle.classList.remove("orbit","fly"),e.cameraModeToggle.classList.add(t))}function f(e,t){e.returnWaypointButton&&(t?e.returnWaypointButton.classList.add("visible"):e.returnWaypointButton.classList.remove("visible"))}const v=new e.Vec3,b=new e.Vec3,w=new e.Pose,x=new e.InputFrame({move:[0,0,0],rotate:[0,0,0]}),S=(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:p}=t,d=t.system?.app,{width:h,height:u}=d?.graphicsDevice?.clientRect||{width:1920,height:1080};a.set(-o/h*2,n/u*2,0);const m=b.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(p*r,p,0);return a.mul(m),a};class M{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=.9,this._flyController.rotateDamping=.9,this._orbitController.rotateDamping=.9,this._orbitController.zoomDamping=.9,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(v)}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=v.copy(this.camera.forward).mulScalar(-o).add(e);this._controller.attach(w.look(n,e))}look(e,t=!1){this._setMode("focus");const o=t?v.copy(this.camera.getPosition()).sub(e).normalize().mulScalar(this._startZoomDist).add(e):this.camera.getPosition();this._controller.attach(w.look(o,e))}reset(e,t){this._setMode("focus"),this._controller.attach(w.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:p,rightInput:d}=this._flyMobileInput.read(),{leftStick:h,rightStick:u}=this._gamepadInput.read();S(h,this.gamepadDeadZone.x,this.gamepadDeadZone.y),S(u,this.gamepadDeadZone.x,this.gamepadDeadZone.y),this._state.axis.add(v.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]),w=+this._flyMobileInput.layout.endsWith("joystick"),M=(this._state.shift?this.moveFastSpeed:this._state.ctrl?this.moveSlowSpeed:this.moveSpeed)*t,_=60*this.zoomSpeed*t,E=_*this.zoomPinchSens,k=60*this.rotateSpeed*t,L=k*this.rotateTouchSens,P=this.rotateSpeed*this.rotateJoystickSens*60*t,{deltas:T}=x,A=v.set(0,0,0),R=this._state.axis.clone().normalize();A.add(R.mulScalar(g*M*this.keyboardSpeedMultiplier));const z=C(this.cameraComponent,a[0],a[1],this._pose.distance);A.add(z.mulScalar(m*f*+this.enablePan));const I=b.set(0,0,s[0]);A.add(I.mulScalar(m*_)),T.move.append([A.x,A.y,A.z]),A.set(0,0,0);const F=b.set(a[0],a[1],0);A.add(F.mulScalar((1-m*f)*k)),this.invertRotation&&(A.y=-A.y),T.rotate.append([A.x,A.y,A.z]),A.set(0,0,0);const D=b.set(p[0],0,-p[1]);A.add(D.mulScalar(g*M));const V=C(this.cameraComponent,r[0],r[1],this._pose.distance);A.add(V.mulScalar(m*y*+this.enablePan));const U=b.set(0,0,l[0]);A.add(U.mulScalar(m*y*E)),T.move.append([A.x,A.y,A.z]),A.set(0,0,0);const B=b.set(r[0],r[1],0);A.add(B.mulScalar(m*(1-y)*L));const $=b.set(d[0],d[1],0);A.add($.mulScalar(g*(w?P:L))),this.invertRotation&&(A.y=-A.y),T.rotate.append([A.x,A.y,A.z]),A.set(0,0,0);const H=b.set(h[0],0,-h[1]);A.add(H.mulScalar(g*M)),T.move.append([A.x,A.y,A.z]),A.set(0,0,0);const O=b.set(u[0],u[1],0);if(A.add(O.mulScalar(g*P)),this.invertRotation&&(A.y=-A.y),T.rotate.append([A.x,A.y,A.z]),this.app.xr?.active)x.read();else{if("focus"===this._mode){const e=T.move.length()+T.rotate.length()>0,t=this._focusController.complete?.()??!1;(e||t)&&this._setMode("orbit")}if(this._pose.copy(this._controller.update(x,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 _{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 p=Math.abs(e.x-n.x),d=Math.abs(e.y-n.y),h=Math.abs(e.z-n.z);if(p<s+t&&d<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 p=n.y+r;Math.abs(e.x-n.x)<s+this.collisionRadius&&Math.abs(e.z-n.z)<l+this.collisionRadius&&e.y>=p-this.stepHeight&&(null===t||p>t)&&(t=p)}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 p=this.checkGround(c),d=c.y-this.playerHeight;null!==p&&d<=p+this.groundCheckDistance?(c.y=p+this.playerHeight,this.velocity.y=0,this.isGrounded=!0):(null===p||d>p+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 E=null;function k(){if(console.log("[RevealEffect] getGsplatRevealRadialClass called, cached:",!!E),E)return E;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()}}),E=t,t}const L={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 L[e]}var T,A={},R={},z={};function I(){if(T)return z;T=1,Object.defineProperty(z,"__esModule",{value:!0}),z.loop=z.conditional=z.parse=void 0;z.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};z.conditional=function(e,t){return function(o,n,i,a){t(o,n,i)&&a(o,e,n,i)}};return z.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}},z}var F,D,V={};function U(){if(F)return V;F=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 B,$={};var H,O,W={};var N=function(){if(O)return A;O=1,Object.defineProperty(A,"__esModule",{value:!0}),A.decompressFrames=A.decompressFrame=A.parseGIF=void 0;var e,t=(D||(D=1,function(e){Object.defineProperty(e,"__esModule",{value:!0}),e.default=void 0;var t=I(),o=U(),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}(R)),(e=R)&&e.__esModule?e:{default:e}),o=I(),n=U(),i=(B||(B=1,Object.defineProperty($,"__esModule",{value:!0}),$.deinterlace=void 0,$.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}),$),a=(H||(H=1,Object.defineProperty(W,"__esModule",{value:!0}),W.lzw=void 0,W.lzw=function(e,t,o){var n,i,a,s,r,l,c,p,d,h,u,m,g,y,f,v,b=4096,w=o,x=new Array(o),S=new Array(b),C=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,p=0;p<i;p++)S[p]=0,C[p]=p;for(u=m=g=y=f=v=0,d=0;d<w;){if(0===y){if(m<s){u+=t[v]<<m,m+=8,v++;continue}if(p=u&a,u>>=s,m-=s,p>n||p==r)break;if(p==i){a=(1<<(s=h+1))-1,n=i+2,c=-1;continue}if(-1==c){M[y++]=C[p],c=p,g=p;continue}for(l=p,p==n&&(M[y++]=g,p=c);p>i;)M[y++]=C[p],p=S[p];g=255&C[p],M[y++]=g,n<b&&(S[n]=c,C[n]=g,!(++n&a)&&n<b&&(s++,a+=n)),c=l}y--,x[f++]=M[y],d++}for(d=f;d<w;d++)x[d]=0;return x}),W);A.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 A.decompressFrame=s,A.decompressFrames=function(e,t){return e.frames.filter((function(e){return e.image})).map((function(o){return s(o,e.gct,t)}))},A}();class G{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=N.parseGIF(o);if(this.frames=N.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 j{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 q{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:p,getSplats:d,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,p,d,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 X{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 Y{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 Z(){const e=navigator.userAgent||navigator.vendor||window.opera||"";return/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(e)}const J={"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 K(e){return e.includes("lod-meta.json")}function Q(t,o,n={}){if(n.lazyLoad){const e=new Y;let i=null;const a=n.lazyLoadThumbnail||o.thumbnailUrl,s=n.lazyLoadButtonText||"Start Experience",r=o.uiColor||"#4CAF50";console.log("[StorySplat Viewer] Lazy loading enabled, showing start button"),function(e,t){const{thumbnailUrl:o,buttonText:n="Start Experience",uiColor:i="#4CAF50",onStart:a}=t;l(i),e.classList.add("storysplat-viewer-container");const s=document.createElement("div");s.className="storysplat-lazy-load-container";let r="";o&&(r+=`<img class="storysplat-lazy-load-thumbnail" src="${o}" alt="Scene preview" />`),r+='<div class="storysplat-lazy-load-overlay"></div>',r+=`\n <div class="storysplat-lazy-load-content">\n <button class="storysplat-lazy-load-start-btn" style="background: ${i}">\n <svg viewBox="0 0 24 24">\n <path d="M8 5v14l11-7z"/>\n </svg>\n ${n}\n </button>\n </div>\n `,s.innerHTML=r,e.appendChild(s);const c=s.querySelector(".storysplat-lazy-load-start-btn");c?.addEventListener("click",(()=>{s.style.transition="opacity 0.3s ease-out",s.style.opacity="0",setTimeout((()=>{s.remove(),a()}),300)}))}(t,{thumbnailUrl:a,buttonText:s,uiColor:r,onStart:()=>{console.log("[StorySplat Viewer] User clicked start, initializing viewer..."),i=Q(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,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 s=new Y,v=a(i(o));console.log("[StorySplat Viewer] Creating viewer with config:",v),console.log("[StorySplat Viewer] Scale config:",v.scale),console.log("[StorySplat Viewer] Raw scene data:",{splatScale:o.splatScale,scale:o.scale});const b=!1!==n.showUI,w=v.uiColor||"#4CAF50",x=v.uiOptions||{},S=e=>"first-person"===e?"tour":"drone"===e?"explore":e,C=v.collisionMeshesData&&v.collisionMeshesData.length>0;let E=(v.allowedCameraModes||["orbit","first-person","drone"]).map(S).filter(((e,t,o)=>o.indexOf(e)===t));C&&!E.includes("walk")&&E.push("walk"),!C&&E.includes("walk")&&(E=E.filter((e=>"walk"!==e)));const L=S(v.defaultCameraMode||"orbit");let T={};b&&(T=function(e,t,o={}){const{uiColor:n="#4CAF50",showScrollControls:i=!0,showModeToggle:a=!0,showFullscreenButton:s=!0,showHelpButton:p=!1,showPreloader:d=!0,allowedCameraModes:h=["tour","explore"],defaultCameraMode:u="tour",customPreloaderLogoUrl:m,buttonLabels:g,hideWatermark:y=!1,watermarkText:f,watermarkLink:v,sceneId:b}=o,w={tour:r(g,"tour"),explore:r(g,"explore"),walk:r(g,"walk"),previous:r(g,"previous"),next:r(g,"next"),helpTitle:r(g,"helpTitle"),returnToTour:r(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())),l(n),e.classList.add("storysplat-viewer-container"),d&&(x.preloader=c(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 C=document.createElement("button");C.className="storysplat-xr-btn storysplat-vr-btn",C.setAttribute("aria-label","Enter VR"),C.textContent="VR",e.appendChild(C),x.vrButton=C;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,p){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 _=document.createElement("div");_.className="storysplat-hotspot-popup",_.id="hotspotContent",_.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(_),x.hotspotPopup=_;const E=_.querySelector(".storysplat-hotspot-popup-close");E?.addEventListener("click",(()=>{_.classList.remove("visible","fullscreen")}));const k=document.createElement("div");k.className="storysplat-joystick-container",k.innerHTML='\n <div class="storysplat-joystick-base"></div>\n <div class="storysplat-joystick-thumb"></div>\n ',e.appendChild(k),x.joystick=k,x.joystickThumb=k.querySelector(".storysplat-joystick-thumb");const L=document.createElement("div");L.className="storysplat-look-zone",L.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(L),x.lookZone=L;const P=document.createElement("button");P.className="storysplat-camera-mode-toggle fly",P.setAttribute("aria-label","Toggle camera mode"),P.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(P),x.cameraModeToggle=P;const T=document.createElement("button");if(T.className="storysplat-return-waypoint-btn",T.setAttribute("aria-label",w.returnToTour),T.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(T),x.returnWaypointButton=T,!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,v,{uiColor:w,showScrollControls:v.waypoints&&v.waypoints.length>0,showModeToggle:E.length>1,showFullscreenButton:!x.hideFullscreenButton,showHelpButton:!x.hideHelpButton&&!x.hideInfoButton,showPreloader:!0,allowedCameraModes:E,defaultCameraMode:L,buttonLabels:x.buttonLabels,customPreloaderLogoUrl:x.customPreloaderLogoUrl,hideWatermark:x.hideWatermark,watermarkText:x.watermarkText,watermarkLink:x.watermarkLink,sceneId:o.sceneId}));const A=document.createElement("canvas");let R;A.id="storysplat-viewer-canvas",A.style.width="100%",A.style.height="100%",A.style.display="block",t.appendChild(A);const z={antialias:!1,alpha:!1,powerPreference:"high-performance"};try{R=new e.Application(A,{graphicsDeviceOptions:z,mouse:new e.Mouse(A),touch:new e.TouchDevice(A),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(A,{graphicsDeviceOptions:{...z,preferWebGl2:!1},mouse:new e.Mouse(A),touch:new e.TouchDevice(A),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")}}A.addEventListener("webglcontextlost",(e=>{e.preventDefault(),console.error("[StorySplat Viewer] WebGL context lost"),s.emit("error",new Error("WebGL context lost"))}),!1),A.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 I=Z(),F=I?"mobile":"desktop",D=J[F];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=D.range[0],R.scene.gsplat.lodRangeMax=D.range[1],R.scene.gsplat.colorUpdateDistance=1,R.scene.gsplat.colorUpdateAngle=4,R.scene.gsplat.colorUpdateDistanceLodScale=2,R.scene.gsplat.colorUpdateAngleLodScale=2,console.log("[StorySplat Viewer] GSplat LOD configured:",{preset:F,lodRange:D.range,isMobile:I}));let V=0,U=!1,B=null,$=null,H=!1,O=null;const W=v.additionalSplats||[],N=v.keepMeshesInMemory??!1;let ee=null,oe=!1;const ne=new Map;let ie=-1,ae=-1;const se=new e.Entity("camera");let re=new e.Color(.1,.1,.1);if(v.backgroundColor){const t=v.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;re=new e.Color(o,n,i),console.log("[StorySplat Viewer] Background color set from config:",v.backgroundColor)}}se.addComponent("camera",{clearColor:re,fov:v.fov||60,nearClip:v.nearClip||.1,farClip:v.farClip||1e3}),se.addComponent("audiolistener"),console.log("[StorySplat Viewer] Camera settings:",{fov:v.fov,nearClip:v.nearClip,farClip:v.farClip,playerHeight:v.playerHeight});const le=v.playerHeight||1.6;if(v.waypoints&&v.waypoints.length>0){const e=v.waypoints[0];if(console.log("[StorySplat Viewer] First waypoint raw:",e),e.position){const t=e.position;se.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 se.setPosition(0,le,5);if(e.rotation){const t=be(e.rotation);se.setRotation(t),console.log("[StorySplat Viewer] Camera rotation set from waypoint")}}else se.setPosition(0,le,5),se.lookAt(new e.Vec3(0,0,0)),console.log("[StorySplat Viewer] Camera set to default position (0, playerHeight, 5)");R.root.addChild(se);const ce=new e.Entity("light");ce.addComponent("light",{type:e.LIGHTTYPE_DIRECTIONAL,color:new e.Color(1,1,1),intensity:1,castShadows:!1}),ce.setEulerAngles(45,45,0),R.root.addChild(ce),console.log("[StorySplat Viewer] Light added");const pe=new M(se,R,{moveSpeed:2*(v.cameraMovementSpeed||1),moveFastSpeed:5*(v.cameraMovementSpeed||1),moveSlowSpeed:1*(v.cameraMovementSpeed||1),rotateSpeed:5e-4*(v.cameraRotationSensitivity||.2),enableOrbit:!0,enableFly:!0,enablePan:!0,invertRotation:v.invertCameraRotation});let de=null;v.collisionMeshesData&&v.collisionMeshesData.length>0&&(de=new _(se,R,{moveSpeed:2*(v.cameraMovementSpeed||1),sprintMultiplier:2,lookSensitivity:.005*(v.cameraRotationSensitivity||.2),playerHeight:v.playerHeight||1.6,gravity:20,jumpVelocity:8,collisionRadius:.3,stepHeight:.3}),de.createCollisionMeshes(v.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 he=L,ue=!0;"tour"===L&&pe.disable();const me=new e.Picker(R,1,1,!0);let ge=!1,ye=null;function fe(e){"walk"===he&&de?de.disable():"explore"===he&&pe.disable(),he=e,console.log("[StorySplat Viewer] Switching to mode:",e);const t=Z();if("walk"===e&&de)ue=!1,pe.disable(),de.enable(),u(T,!1),g(T,!1),v.waypoints&&v.waypoints.length>0&&f(T,!0);else if("explore"===e){de&&de.disable(),De=0,Ve=0,Ue=!1,ue=!1,pe.disable(),pe.enable(),pe.enableOrbit=!0,pe.enableFly=!0,pe.enablePan=!0,ze&&Ie?(pe.syncFromPose(ze,Ie),console.log("[StorySplat Viewer] Synced camera from waypoint pose for explore mode")):pe.syncFromCamera();(async()=>{try{const e=.25;me.resize(Math.floor(Bt.clientWidth*e),Math.floor(Bt.clientHeight*e));const t=R.scene.layers.getLayerByName("World");if(t){me.prepare(se.camera,R.scene,[t]);const o=Math.floor(.5*Bt.clientWidth*e),n=Math.floor(.5*Bt.clientHeight*e),i=await me.getWorldPointAsync(o,n);if(i){const e=se.getPosition().distance(i);e>.5&&e<500&&(pe.syncFromCamera(i),console.log("[StorySplat Viewer] Updated focus target at distance:",e.toFixed(2)))}}}catch(e){}})(),t?(u(T,!0),g(T,!0),pe.setMode("fly"),y(T,"fly")):pe.setMode("orbit"),v.waypoints&&v.waypoints.length>0&&f(T,!0)}else pe.disable(),de&&de.disable(),ue=!0,u(T,!1),g(T,!1),f(T,!1);s.emit("modeChange",{mode:e})}R.on("update",(t=>{"walk"===he&&de?de.update(t):pe.update(t),ue||function(){const e=se.getPosition();at.forEach((t=>{const o=t.hotspotData;if(!o||"video"!==o.type||!t.videoElement)return;if("proximity"!==(t.mediaTriggerMode||"click"))return;const n=t.getPosition(),i=e.distance(n),a=t.proximityDistance||5;i<=a&&!t.isVideoPlaying?zt(t,o):i>a&&t.isVideoPlaying&&It(t)}))}(),function(){if(0===ct.size)return;const e=se.getPosition();ct.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(!v.waypoints?.length)return;const t=se.getPosition();v.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?Lt.has(n)||(Lt.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=ct.get(t);if(o&&o.assetReady&&!o.playing){const e=o.entity.sound?.slot(o.slotId);e&&(e.play(),o.playing=!0)}}}))}(o)):Lt.has(n)&&(Lt.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=ct.get(t);if(o&&o.playing){const e=o.entity.sound?.slot(o.slotId);e&&(e.stop(),o.playing=!1)}}}}))}(o))}))}()})),R.on("joystick:left",((e,t,o,n)=>{if(e<0||t<0)m(T,!1,0,0,60);else{m(T,!0,o-e,n-t,60)}})),T.cameraModeToggle&&T.cameraModeToggle.addEventListener("click",(()=>{if("explore"!==he)return;const e=pe.mode;"orbit"===e||"focus"===e?(pe.setMode("fly"),y(T,"fly"),u(T,!0)):(pe.setMode("orbit"),y(T,"orbit"),u(T,!1))})),R.on("cameracontrols:modechange",(e=>{"orbit"!==e&&"fly"!==e||(y(T,e),I&&"explore"===he&&u(T,"fly"===e))}));const ve=(e,t)=>{T.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)}%`)}(T.preloader,e,t),s.emit("progress",{progress:e,text:t})};function be(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 we(e){e.enabled=!1,console.log("[SplatSwap] Splat hidden")}function xe(e){const t=ne.get(e);t&&(t.destroy(),ne.delete(e),console.log("[SplatSwap] Splat disposed:",e))}async function Se(t){if(!W||0===W.length)return;const o=(t+1)%W.length,n=W[o];n&&n.url&&await async function(t){if(!ne.has(t)&&t!==ee&&!H){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(H)return void i(new Error("Viewer destroyed"));const a=new e.Entity("splat-preload");a.addComponent("gsplat",{asset:o,unified:!0});const s=v.scale||{x:1,y:1,z:1},r=v.invertXScale||!1,l=v.invertYScale||!1,c={x:r?-s.x:s.x,y:l?-s.y:s.y,z:r!==l?-s.z:s.z};a.setLocalScale(c.x,c.y,c.z);const p=v.position||[0,0,0];a.setPosition(p[0],p[1],-p[2]);const d=v.rotation||[0,0,0],h=0!==d[0]||0!==d[1]||0!==d[2]?[d[0]*(180/Math.PI),d[1]*(180/Math.PI),-d[2]*(180/Math.PI)]:[180,0,0];a.setEulerAngles(h[0],h[1],h[2]),a.enabled=!1,R.root.addChild(a),ne.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 Ce(t){if(t!==ee&&!oe&&!H){oe=!0,console.log("[SplatSwap] Switching to splat:",t);try{if(ee&&ne.has(ee))if(N){const e=ne.get(ee);e&&we(e)}else xe(ee);else B&&(B.enabled=!1);if(ne.has(t))!function(e){const t=ne.get(e);!!t&&(t.enabled=!0,ee=e,console.log("[SplatSwap] Splat shown:",e))}(t);else{const o=new e.Asset("splat-swap-"+Date.now(),"gsplat",{url:t});await new Promise(((n,i)=>{o.ready((()=>{if(H)return void i(new Error("Viewer destroyed"));const a=new e.Entity("splat-swap");a.addComponent("gsplat",{asset:o,unified:!0});const s=v.scale||{x:1,y:1,z:1},r=v.invertXScale||!1,l=v.invertYScale||!1,c={x:r?-s.x:s.x,y:l?-s.y:s.y,z:r!==l?-s.z:s.z};a.setLocalScale(c.x,c.y,c.z);const p=v.position||[0,0,0];a.setPosition(p[0],p[1],-p[2]);const d=v.rotation||[0,0,0],h=0!==d[0]||0!==d[1]||0!==d[2]?[d[0]*(180/Math.PI),d[1]*(180/Math.PI),-d[2]*(180/Math.PI)]:[180,0,0];a.setEulerAngles(h[0],h[1],h[2]),R.root.addChild(a),ne.set(t,a),ee=t,console.log("[SplatSwap] New splat loaded and shown:",t),n()})),o.on("error",(e=>{console.error("[SplatSwap] Load error:",e),i(e)})),R.assets.add(o),R.assets.load(o)}))}const o=W.findIndex((e=>e.url===t));-1!==o&&Se(o)}catch(e){console.error("[SplatSwap] Error switching splat:",e)}finally{oe=!1}}}function Me(){if(!W||0===W.length)return;const t=v.waypoints?.length||1,o=100*_e,n=Math.round(_e*Math.max(1,t-1));if(Math.abs(o-ie)<.1&&n===ae)return;ie=o,ae=n;let i=null,a=null,s=-1/0,r=-1/0;for(const e of W)-1!==e.waypointIndex?n>=e.waypointIndex&&e.waypointIndex>s&&(s=e.waypointIndex,i=e):-1!==e.percentage&&o>=e.percentage&&e.percentage>r&&(r=e.percentage,a=e);const l=a||i,c=v.sogUrl?v.sogUrl:v.splatUrl?v.splatUrl:v.fallbackUrls&&v.fallbackUrls.length>0?v.fallbackUrls[0]:"",p=l?l.url:c;p&&p!==ee&&(p===c&&B&&!ee?ee=c:p===c&&B?(ne.forEach(((e,t)=>{t!==c&&(N?we(e):xe(t))})),B&&(B.enabled=!0),ee=c,console.log("[SplatSwap] Returned to primary splat")):Ce(p),l&&l.skyboxUrl&&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(!H)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)}(l.skyboxUrl,l.skyboxRotation||0))}let _e=0;const Ee=v.waypoints?.reduce(((e,t)=>e+(t.duration||2e3)),0)||1,ke=v.waypoints?.length||1,Le=Math.max(1,20*(ke-1)),Pe=void 0!==v.autoplaySpeed?60*v.autoplaySpeed/Le:1e3/Ee,Te=v.loopMode;let Ae;Ae=!0===Te?"loop":!1===Te?"none":"loop"===Te||"pingpong"===Te||"none"===Te?Te:"loop";let Re=1;console.log("[StorySplat Viewer] Playback config:",{loopMode:Ae,playbackSpeed:Pe,totalDuration:Ee,autoPlay:v.autoPlay,rawLoopMode:v.loopMode});let ze=null,Ie=null;const Fe=.01+.1*(v.transitionSpeed||1);let De=0,Ve=0;let Ue=!1,Be=0,$e=0;const He=[],Oe=[],We=[],Ne=v.fov||60;let Ge=Ne;function je(t){if(!ue||He.length<2)return;t=Math.max(0,Math.min(1,t));const o=He.length,n=t*(o-1),i=Math.min(Math.floor(n),o-2),a=n-i,r=He[i],l=He[i+1];ze=new e.Vec3(te(r.x,l.x,a),te(r.y,l.y,a),te(r.z,l.z,a));const c=Oe[i],p=Oe[i+1];Ie=new e.Quat,Ie.slerp(c,p,a);const d=We[i],h=We[i+1];Ge=te(d,h,a);const u=Math.round(t*(o-1));if(u!==V){const t=V;V=u;const o=v.waypoints[u],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(He[u].x,He[u].y,He[u].z)),s.emit("waypointChange",{index:u,waypoint:o,prevIndex:t,cameraMode:n}),m=u,g=t,ct.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=g===n&&m!==n;m===n&&g!==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 m,g;s.emit("progressUpdate",{progress:Math.max(0,Math.min(1,t)),index:V}),Me()}function qe(e,t=!1){t?Ye(e):(_e=Math.max(0,Math.min(1,e)),je(_e))}v.waypoints&&v.waypoints.length>0&&(v.waypoints.forEach((t=>{const o=t.position?new e.Vec3(t.position.x,t.position.y,-t.position.z):new e.Vec3(0,v.playerHeight||1.6,0);He.push(o);const n=t.rotation?be(t.rotation):new e.Quat;Oe.push(n);let i=t.fov||Ne;i<3.5&&(i*=180/Math.PI),We.push(i)})),He.length>0&&(ze=He[0].clone(),Ie=Oe[0].clone(),Ge=We[0])),R.on("update",(function(){if(!ze||!Ie)return;if(!ue)return;const t=se.getPosition(),o=new e.Vec3;o.lerp(t,ze,Fe),se.setPosition(o.x,o.y,o.z);const n=se.camera;if(n&&We.length>0){const e=te(n.fov,Ge,Fe);n.fov=e}Ue||(De*=.95,Ve*=.95,Math.abs(De)<.01&&(De=0),Math.abs(Ve)<.01&&(Ve=0));const i=new e.Quat;i.setFromEulerAngles(Ve,De,0);const a=new e.Quat;a.mul2(Ie,i);const s=se.getRotation(),r=new e.Quat;r.slerp(s,a,Fe),se.setRotation(r)}));const Xe=500*(v.transitionSpeed||1);function Ye(e,t=Xe){const o=_e,n=performance.now(),i=()=>{const a=performance.now()-n,s=Math.min(a/t,1);_e=o+(e-o)*(s<.5?2*s*s:(4-2*s)*s-1),je(_e),s<1&&requestAnimationFrame(i)};requestAnimationFrame(i)}function Ze(e){if(!v.waypoints||e<0||e>=v.waypoints.length)return;if(!ue)return;Ye(e/Math.max(1,v.waypoints.length-1))}function Je(){if(!v.waypoints||0===v.waypoints.length)return;const e=v.scrollButtonMode||"waypoint";if("percentage"===e||"continuous"===e||"incremental"===e){const e=v.scrollAmount||10;let t=_e+e/100;t>1&&(t="loop"===Ae?0:1),Ye(t)}else{let e=V+1;e>=v.waypoints.length&&(e="loop"===Ae?0:v.waypoints.length-1),Ze(e)}}function Ke(){if(!v.waypoints||0===v.waypoints.length)return;const e=v.scrollButtonMode||"waypoint";if("percentage"===e||"continuous"===e||"incremental"===e){const e=v.scrollAmount||10;let t=_e-e/100;t<0&&(t="loop"===Ae?1:0),Ye(t)}else{let e=V-1;e<0&&(e="loop"===Ae?v.waypoints.length-1:0),Ze(e)}}let Qe=0,et=null;function tt(e){if(!U)return;0===Qe&&(Qe=e);const t=(e-Qe)/1e3;if(Qe=e,_e+=Pe*t*Re,_e>=1)switch(console.log("[StorySplat Viewer] End of tour reached, loopMode:",Ae),Ae){case"loop":console.log("[StorySplat Viewer] Looping - restarting at beginning"),_e=0;break;case"pingpong":console.log("[StorySplat Viewer] Pingpong - reversing direction"),_e=1,Re=-1;break;case"none":return console.log("[StorySplat Viewer] No loop - stopping playback"),_e=1,nt(),void s.emit("playbackComplete");default:return console.log("[StorySplat Viewer] Unknown loopMode, stopping:",Ae),_e=1,nt(),void s.emit("playbackComplete")}else if(_e<=0)if("pingpong"===Ae)console.log("[StorySplat Viewer] Pingpong - reversing to forward"),_e=0,Re=1;else _e=0;je(_e),et=requestAnimationFrame(tt)}function ot(){if(O)return O.play(),void s.emit("playbackStart");U||!v.waypoints||v.waypoints.length<2||(U=!0,Qe=0,Re=1,s.emit("playbackStart"),et=requestAnimationFrame(tt))}function nt(){if(O)return O.pause(),void s.emit("playbackStop");U=!1,et&&(cancelAnimationFrame(et),et=null),s.emit("playbackStop")}const it=R.graphicsDevice.canvas;it.addEventListener("wheel",(e=>{if(!ue)return;e.preventDefault();const t=v.scrollSpeed||.1,o=(v.scrollAmount||100)/100*t*.05,n=e.deltaY>0?o:-o;qe(Math.max(0,Math.min(1,_e+n)))}),{passive:!1}),it.addEventListener("pointerdown",(e=>{ue&&(Ue=!0,Be=e.clientX,$e=e.clientY)}),{capture:!0}),it.addEventListener("pointermove",(e=>{if(!ue||!Ue)return;const t=e.clientX-Be,o=e.clientY-$e;Be=e.clientX,$e=e.clientY;De+=.3*-t,Ve+=.3*-o,Ve=Math.max(-60,Math.min(60,Ve))}),{capture:!0}),it.addEventListener("pointerup",(()=>{Ue=!1}),{capture:!0}),it.addEventListener("pointerleave",(()=>{Ue=!1}),{capture:!0});const at=[],st=[];function rt(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 lt=[],ct=new Map,pt=new Map,dt=new Map,ht={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 ut(t,o){return new Promise(((n,i)=>{if(dt.has(t))return void n(dt.get(t));const a=new e.Asset(t,"texture",{url:o});a.on("load",(()=>{if(H)return console.log(`[Particle] Ignoring texture load - viewer was destroyed: ${t}`),void i(new Error("Viewer destroyed"));const e=a.resource;dt.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 mt(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),p=n(t.direction1,0),d=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,C=w*h,M=t.minAngularSpeed??0,_=t.maxAngularSpeed??t.angularSpeed??0,E=t.minInitialRotation??0,k=t.maxInitialRotation??0,L=t.minSize??.1,P=t.maxSize??.3,T=t.minScaleX??1,A=t.maxScaleX??1,R=t.minScaleY??1,z=t.maxScaleY??1,I=t.minEmitPower??1,F=t.maxEmitPower??2,D=(p.x+d.x)/2,V=(p.y+d.y)/2,U=-(p.z+d.z)/2;return o.addComponent("particlesystem",{numParticles:t.numParticles||500,lifetime:h,rate:t.emitRate||50,emitterShape:u,emitterExtents:m,emitterRadius:t.emitterRadius||.1,startAngle:E,startAngle2:0!==k?k:360,radialSpeedGraph:new e.Curve([0,I,1,F]),localVelocityGraph:new e.CurveSet([[0,D*I],[0,V*I],[0,U*I]]),localVelocityGraph2:new e.CurveSet([[0,D*F],[0,V*F],[0,U*F]]),velocityGraph:new e.CurveSet([[0,0,1,x],[0,0,1,S],[0,0,1,C]]),scaleGraph:new e.Curve([0,L*T,1,P*A]),scaleGraph2:new e.Curve([0,L*R,1,P*z]),rotationSpeedGraph:new e.Curve([0,M]),rotationSpeedGraph2:_!==M?new e.Curve([0,_]):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),o.setPosition(a.x+g.x,a.y+g.y,-a.z+g.z),console.log(`[Particle] Entity configured at position: (${a.x+g.x}, ${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} - ${_}, Initial rotation: ${E} - ${k}`),o}const gt=new Map;function yt(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 ft(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 vt(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 gt.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&&(Ct(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=se.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 p=[],d=i.anim;if(d&&(console.log("[CustomMesh] Found modelEntity.anim component - using simple approach"),p.push({type:"pc-anim",component:d,modelEntity:i})),0===p.length){function h(e,t=0){e.anim&&!p.find((t=>t.component===e.anim))&&(p.push({type:"anim",component:e.anim,node:e}),console.log("[CustomMesh] Found existing ANIM component on:",e.name)),e.animation&&(p.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===p.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{p.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(p.length>0){l.allAnimComponents=p,l.animComponent=p[0].component,console.log("[CustomMesh] Total animation components for",t.name,":",p.length,"- Types:",p.map((e=>e.type)).join(", "));const g=t.interaction?.animationAutoPlay;g&&(p.forEach((e=>{yt(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=se.camera.screenToWorld(s,r,se.camera.nearClip),c=se.camera.screenToWorld(s,r,se.camera.farClip),p=(new e.Vec3).sub2(c,l).normalize(),d=t.modelEntity;if(d&&bt(d,l,p))return!0;if(bt(t,l,p))return!0;const h=t.getPosition(),u=(new e.Vec3).sub2(h,l).dot(p);if(u>0){const o=(new e.Vec3).add2(l,p.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,p=o.interaction?.directLinkTriggerMode||i;let d=!1;const h=()=>{if(a.style.cursor="pointer","hover"===r&&o.interaction?.triggerUIPopup&&xt(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=>yt(e))),n.isAnimPlaying=!0,console.log("[CustomMesh] Playing animation on hover for:",o.name))}"hover"===p&&o.interaction?.triggerDirectLink&&St(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=>ft(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&&xt(o),"click"===c&&o.interaction?.playModelAnimation){const e=n.allAnimComponents;e&&e.length>0&&(n.isAnimPlaying?(e.forEach((e=>ft(e))),n.isAnimPlaying=!1,console.log("[CustomMesh] Paused",e.length,"animations on click for:",o.name)):(e.forEach((e=>yt(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"===p&&o.interaction?.triggerDirectLink&&St(o)},g=e=>{s(e.clientX,e.clientY)&&m()},y=e=>{const t=s(e.clientX,e.clientY);t&&!d?(d=!0,h()):!t&&d&&(d=!1,u())},f=()=>{d&&(d=!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 bt(t,o,n){const i=t.render;if(i&&i.meshInstances)for(const e of i.meshInstances)if(e.aabb){if(wt(o,n,e.aabb))return!0}const a=t.model;if(a&&a.meshInstances)for(const e of a.meshInstances)if(e.aabb){if(wt(o,n,e.aabb))return!0}const s=t.children;if(s)for(const t of s)if(t instanceof e.Entity&&bt(t,o,n))return!0;return!1}function wt(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],p=n[r]??n.data?.[o],d=i[r]??i.data?.[o];if(Math.abs(c)<1e-8){if(l<p||l>d)return!1}else{let e=(p-l)/c,t=(d-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 xt(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"};T.showHotspotPopup?T.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 St(e){e.interaction?.triggerDirectLink&&e.interaction.directLinkUrl&&window.open(e.interaction.directLinkUrl,"_blank")}function Ct(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 Mt(){const e=R.graphicsDevice.canvas;gt.forEach((t=>{const o=t.entity;o.meshClickHandler&&e.removeEventListener("click",o.meshClickHandler),o.destroy()})),gt.clear()}function _t(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 Et(){v.lights&&0!==v.lights.length?(console.log(`[StorySplat Viewer] Creating ${v.lights.length} custom lights...`),v.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:_t(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:_t(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:_t(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=_t(t.groundColor),n=_t(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=_t(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:_t(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 kt(e){switch(e){case"linear":return"linear";case"inverse":return"inverse";default:return"exponential"}}s.on("progressUpdate",(()=>{!function(){const e=100*_e,t=v.waypoints?.length||1,o=Math.round(_e*Math.max(1,t-1));gt.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);Ct(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 Lt=new Set;const Pt=[];function Tt(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 G(R,t,{autoPlay:!0,onReady:()=>{if(H)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()}});Pt.push(n)}else{const n=new Image;n.crossOrigin="anonymous",n.onload=()=>{if(H)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 At(){v.hotspots&&0!==v.hotspots.length?(console.log(`[StorySplat Viewer] Creating ${v.hotspots.length} hotspots...`),v.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},p=180/Math.PI,d=(c._x??c.x??0)*p,h=(c._y??c.y??0)*p+180,u=(c._z??c.z??0)*p;if(n.setEulerAngles(d,h,u),"sphere"===t.type){n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=rt(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=Tt(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,p=(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}},d=p(i,!1,c),h=d.video,u=d.texture,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=p(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);lt.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),se&&se.getPosition){const e=se.getPosition(),t=se.forward,o=se.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 p=document.createElement("canvas"),d=p.getContext("2d"),h=new Image;h.crossOrigin="anonymous",h.onload=()=>{p.width=h.width||256,p.height=h.height||256,d.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(p),a.diffuseMap=u,i||(a.emissiveMap=u),a.opacityMap=u,a.alphaTest=.01,a.update(),n.render.material=a;const m=p.width/p.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=p,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&&(d.clearRect(0,0,p.width,p.height),d.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=rt(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=rt(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);lt.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),se&&se.getPosition){const e=se.getPosition(),t=se.forward,o=se.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(se.getPosition()),n.rotateLocal(90,180,0))}))}t.visibilityRange&&(n.visibilityRange=t.visibilityRange,n.enabled=!1),R.root.addChild(n),at.push(n),console.log(`[StorySplat Viewer] Created hotspot: ${t.title||"Untitled"}`)}))):console.log("[StorySplat Viewer] No hotspots to create")}function Rt(){const e=100*_e,t=v.waypoints?.length||1,o=Math.round(_e*Math.max(1,t-1)),n=se.getPosition();at.forEach((t=>{const i=t.hotspotData;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,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?(zt(t,i),console.log(`[Hotspot] Proximity play: ${i.title}, distance=${o.toFixed(2)}`)):o>a&&t.isVideoPlaying&&(It(t),console.log(`[Hotspot] Proximity pause: ${i.title}, distance=${o.toFixed(2)}`))}"scroll"===o&&(a&&!t.isVideoPlaying?(zt(t,i),console.log(`[Hotspot] Scroll play: ${i.title}, scroll=${e.toFixed(1)}%`)):!a&&t.isVideoPlaying&&(It(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 zt(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 It(e){const t=e.videoElement,o=e.alphaVideoElement;t&&(t.pause(),o&&o.pause(),e.isVideoPlaying=!1)}function Ft(){const e=100*_e,t=v.waypoints?.length||1,o=Math.round(_e*Math.max(1,t-1)),n=se.getPosition();st.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}`),Vt(i)):o>a&&(t.proximityTriggered=!1)}}))}function Dt(t,o){const n=se.camera.screenToWorld(t,o,se.camera.nearClip),i=se.camera.screenToWorld(t,o,se.camera.farClip);let a=null;if(st.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 Vt(e){if(e.targetSceneId){console.log(`[StorySplat Viewer] Portal activated: navigating to scene ${e.targetSceneId}`),s.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..."),nt(),at.forEach((e=>{e.videoElement&&(e.videoElement.pause(),e.videoElement.src=""),e.alphaVideoElement&&(e.alphaVideoElement.pause(),e.alphaVideoElement.src="")})),Pt.forEach((e=>e.destroy())),Pt.length=0,Mt(),at.forEach((e=>{e.destroy()})),at.length=0,st.forEach((e=>{e.destroy()})),st.length=0,B&&(B.destroy(),B=null);R.__htmlMeshManager&&R.__htmlMeshManager.destroy();R.__customScriptSystem&&R.__customScriptSystem.dispose();console.log("[Portal] Cleanup complete")}(),await new Promise((e=>setTimeout(e,100))),A&&A.parentNode&&A.remove();const s=await Q(t,a,{});return Ut(t),console.log(`[Portal] Successfully navigated to scene: ${e.targetSceneId}`),s}catch(e){console.error("[Portal] Navigation failed:",e),Ut(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 Ut(e){const t=e.querySelector(".storysplat-portal-loading");t&&t.remove()}s.on("progressUpdate",(()=>{Rt()})),setTimeout((()=>{Rt()}),100),s.on("progressUpdate",(()=>{Ft()})),setTimeout((()=>{Ft()}),100);const Bt=R.graphicsDevice.canvas;function $t(t,o){const n=se.camera.screenToWorld(t,o,se.camera.nearClip),i=se.camera.screenToWorld(t,o,se.camera.farClip);let a=null;if(at.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 Ht=null,Ot=!1;const Wt=t.querySelector(".storysplat-hotspot-popup"),Nt=t.querySelector(".storysplat-hotspot-overlay");async function Gt(e,t){if("explore"!==he)return;console.log("[StorySplat Viewer] Double-click focus at:",e,t);const o=$t(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 pe.focus(e,!1)}try{const o=.25;me.resize(Math.floor(Bt.clientWidth*o),Math.floor(Bt.clientHeight*o));const n=R.scene.layers.getLayerByName("World");if(!n)return void console.warn("[StorySplat Viewer] World layer not found");me.prepare(se.camera,R.scene,[n]);const i=Math.floor(e*o),a=Math.floor(t*o),s=await me.getWorldPointAsync(i,a);if(s){const e=se.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 pe.focus(s,!1)}const r=await me.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)),pe.focus(e,!1)}else console.log("[StorySplat Viewer] No pick result at click point")}catch(e){console.warn("[StorySplat Viewer] Picking failed:",e)}}Wt&&(Wt.addEventListener("mouseenter",(()=>{Ot=!0})),Wt.addEventListener("mouseleave",(()=>{Ot=!1,Ht&&"hover"===Ht.activationMode&&(Wt.classList.remove("visible"),Nt&&Nt.classList.remove("visible"),Ht=null)}))),Bt.addEventListener("mousemove",(e=>{const o=Bt.getBoundingClientRect(),n=e.clientX-o.left,i=e.clientY-o.top,a=Dt(n,i);if(a&&a.portal){const e=a.portal.activationMode||"click";return void(Bt.style.cursor="click"===e?"pointer":"default")}const s=$t(n,i);if(s&&s.hotspot){const e=s.hotspot;"click"===e.activationMode||"hover"===e.activationMode||"video"===e.type?Bt.style.cursor="pointer":Bt.style.cursor="default","hover"===e.activationMode&&Ht!==e&&(Ht=e,(e.information||e.photoUrl||e.iframeUrl||e.externalLinkUrl)&&h(t,e))}else if(Bt.style.cursor="default",Ht&&"hover"===Ht.activationMode&&!Ot){const e=t.querySelector(".storysplat-hotspot-popup"),o=t.querySelector(".storysplat-hotspot-overlay");e&&e.classList.remove("visible"),o&&o.classList.remove("visible"),Ht=null}})),Bt.addEventListener("click",(e=>{const o=Bt.getBoundingClientRect(),n=e.clientX-o.left,i=e.clientY-o.top,a=Dt(n,i);if(null!==a&&a.portal){const e=a.portal;console.log("[StorySplat Viewer] Portal clicked:",e.title||e.targetSceneName);return void("click"===(e.activationMode||"click")&&Vt(e))}const s=$t(n,i);if(null!==s){const e=s.entity,o=s.hotspot;if(console.log("[StorySplat Viewer] Hotspot clicked:",o.title),e.audioElements&&e.audioElements.audio){const t=e.audioElements,n=t.audio;n.paused?(t.audioCtx&&"suspended"===t.audioCtx.state&&t.audioCtx.resume(),n.play().catch((e=>console.error("[Audio] Hotspot audio play failed:",e))),console.log("[Audio] Hotspot audio started:",o.title)):(n.pause(),console.log("[Audio] Hotspot audio paused:",o.title))}if(e.videoElement&&("click"===e.mediaTriggerMode||"autoplay"===e.mediaTriggerMode)){const t=e.videoElement;e.alphaVideoElement,"autoplay"===e.mediaTriggerMode&&!t.paused&&t.muted?(t.muted=(o.videoMuted,!1),console.log("[Hotspot] Video unmuted by click")):t.paused?(zt(e,o),console.log("[Hotspot] Video started")):(It(e),console.log("[Hotspot] Video paused"))}const n=o.activationMode||"click",i=o.title||o.information||o.photoUrl||o.iframeUrl||o.externalLinkUrl;"click"===n&&i&&h(t,o);const a=o.teleportWaypoint??o.teleportToWaypoint,r=o.teleportPercent??o.teleportToPercent,l=o.teleportMode||"animate";let c=null;if(void 0!==a&&-1!==a){const e=v.waypoints?.length||1,t=Math.max(0,Math.min(a,e-1));c=e>1?t/(e-1):0,console.log("[Hotspot] Teleporting to waypoint:",t,"(progress:",c,", mode:",l,")")}else void 0!==r&&-1!==r&&(c=Math.max(0,Math.min(r/100,1)),console.log("[Hotspot] Teleporting to percent:",r,"(progress:",c,", mode:",l,")"));null!==c&&("instant"===l?(_e=c,je(_e)):Ye(c,800))}})),Bt.addEventListener("dblclick",(e=>{const t=Bt.getBoundingClientRect();Gt(e.clientX-t.left,e.clientY-t.top)}));let jt=0;Bt.addEventListener("touchend",(e=>{if(1!==e.changedTouches.length)return;const t=Date.now();if(t-jt<300){const t=e.changedTouches[0],o=Bt.getBoundingClientRect();Gt(t.clientX-o.left,t.clientY-o.top),jt=0}else jt=t})),ve(.2,"Initializing...");const qt=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"),ve(.3,"Loading 4DGS frames..."),O=new X(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)=>{s.emit("frameChange",e,t)},onLoadProgress:(e,t)=>{ve(.3+e/t*.6,`Loading frames... ${e}/${t}`)},onError:e=>{console.error("[StorySplat Viewer] Frame sequence error:",e),s.emit("error",new Error(e))}}),R.on("update",(e=>{O&&!H&&O.update(e)})),console.log("[StorySplat Viewer] 4DGS frame sequence player initialized"),s.emit("loaded")}:async function(){const t=[];v.lodMetaUrl&&t.push(v.lodMetaUrl),v.sogUrl&&t.push(v.sogUrl),v.splatUrl&&t.push(v.splatUrl),v.fallbackUrls&&t.push(...v.fallbackUrls),console.log("[StorySplat Viewer] Loading splat from URLs:",t),console.log("[StorySplat Viewer] URL priorities: LOD streaming > SOG > PLY/Other"),ve(.3,"Loading splat...");for(const i of t)if(i)try{console.log("[StorySplat Viewer] Trying URL:",i);i.split(".").pop()?.toLowerCase();const t="gsplat",a=new e.Asset("splat-"+Date.now(),t,{url:i});return a.on("progress",((e,t)=>{if(t>0){ve(.3+e/t*.6,`Loading... ${Math.round(e/t*100)}%`)}})),await new Promise(((r,l)=>{let c=!1;const p=i.includes("lod-meta.json"),d=i.includes(".sog")||p,h=d?e=>{if(c)return;const t=e.reason?.message||String(e.reason);(t.includes("shape")||t.includes("upgradeMeta"))&&(console.warn("[StorySplat Viewer] Caught SOG parse error, will try fallback:",t),c=!0,e.preventDefault(),s.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:i}),l(new Error(`SOG parse error: ${t}`)))}:null;d&&h&&window.addEventListener("unhandledrejection",h);const u=()=>{d&&h&&window.removeEventListener("unhandledrejection",h)};a.ready((()=>{if(H)return console.log("[StorySplat Viewer] Ignoring splat load - viewer was destroyed"),u(),void l(new Error("Viewer destroyed"));console.log("[StorySplat Viewer] Asset ready (loaded + resources ready)");try{B=new e.Entity("splat"),B.addComponent("gsplat",{asset:a,unified:!0});const t=B.gsplat;t&&K(i)&&(t.lodDistances=D.lodDistances,console.log("[StorySplat Viewer] LOD distances set for streaming format:",D.lodDistances));const s=v.scale||{x:1,y:1,z:1},l=v.invertXScale||!1,p=v.invertYScale||!1,d={x:l?-s.x:s.x,y:p?-s.y:s.y,z:l!==p?-s.z:s.z};B.setLocalScale(d.x,d.y,d.z);const h=v.position||[0,0,0];B.setPosition(h[0],h[1],-h[2]);const m=v.rotation||[0,0,0],g=0!==m[0]||0!==m[1]||0!==m[2];let y;y=g?[m[0]*(180/Math.PI),m[1]*(180/Math.PI),-m[2]*(180/Math.PI)]:[180,0,0],B.setEulerAngles(y[0],y[1],y[2]),console.log("[StorySplat Viewer] Splat transform applied:",{scale:d,position:[h[0],h[1],-h[2]],rotation:y,hasUserRotation:g,invertXScale:l,invertYScale:p}),R.root.addChild(B),console.log("[StorySplat Viewer] Splat entity added to scene"),B.gsplat?.material&&(B.gsplat.material.setParameter("alphaClip",.1),console.log("[StorySplat Viewer] GSplat alphaClip set for picking support"));const f=n.revealEffect||o.revealEffect||"medium",b=P(f);if(b){B.addComponent("script");const e=k();$=B.script?.create(e),$&&($.enabled=!1,$.center.set(0,0,0),$.speed=b.speed,$.acceleration=b.acceleration,$.delay=b.delay,$.oscillationIntensity=b.oscillationIntensity,$.dotTint.set(b.dotTint.r,b.dotTint.g,b.dotTint.b),$.waveTint.set(b.waveTint.r,b.waveTint.g,b.waveTint.b),$.endRadius=b.endRadius,console.log("[StorySplat Viewer] Reveal effect configured (waiting to start):",f))}else console.log("[StorySplat Viewer] Reveal effect disabled");setTimeout((()=>{c||(u(),r())}),100)}catch(e){console.error("[StorySplat Viewer] Error during gsplat setup:",e),u(),l(e)}})),a.on("error",(e=>{console.error("[StorySplat Viewer] Asset load error:",{url:i,assetType:t,error:e,message:e?.message||"Unknown error",status:e?.status||e?.statusCode||"N/A",stack:e?.stack}),u(),l(e)})),R.assets.add(a),R.assets.load(a)})),s.emit("loaded"),void console.log("[StorySplat Viewer] Splat loaded successfully")}catch(e){console.warn("[StorySplat Viewer] Failed to load:",i,e)}console.error("[StorySplat Viewer] Failed to load splat from any URL"),s.emit("error",new Error("Failed to load splat from any URL"))};qt().then((()=>{if(ve(1,"Ready!"),At(),v.portals&&0!==v.portals.length?(console.log(`[StorySplat Viewer] Creating ${v.portals.length} portals...`),v.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},p=180/Math.PI,d=(c._x??c.x??0)*p,h=(c._y??c.y??0)*p+180,u=(c._z??c.z??0)*p;if(n.setEulerAngles(d,h,u),"sphere"===t.type){n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=rt(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=Tt(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{n.addComponent("render",{type:"sphere",castShadows:!1,receiveShadows:!1});const o=new e.StandardMaterial,i=rt(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(se.getPosition()),n.rotateLocal(90,180,0))})),t.visibilityRange&&(n.visibilityRange=t.visibilityRange,n.enabled=!1),R.root.addChild(n),st.push(n),console.log(`[StorySplat Viewer] Created portal: ${t.title||t.targetSceneName||"Untitled"} -> ${t.targetSceneId}`)}))):console.log("[StorySplat Viewer] No portals to create"),v.waypoints&&0!==v.waypoints.length&&(v.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:kt(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=ct.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),ct.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}`)}}))})),ct.size>0&&console.log(`[StorySplat Viewer] Setup ${ct.size} waypoint audio sources`)),console.log("[StorySplat Viewer] 🎆 Calling initParticleSystems..."),async function(){if(console.log("═══════════════════════════════════════"),console.log("🎆 PARTICLE SYSTEM INITIALIZATION"),console.log("═══════════════════════════════════════"),console.log("[Particle] config.particles:",v.particles),console.log("[Particle] Type:",typeof v.particles),console.log("[Particle] Is Array:",Array.isArray(v.particles)),!v.particles||0===v.particles.length)return console.log("[Particle] ⚠️ No particle systems to create (particles array is empty or undefined)"),void console.log("═══════════════════════════════════════");console.log(`[Particle] 📋 Found ${v.particles.length} particle system(s) to create`);for(let e=0;e<v.particles.length;e++){const t=v.particles[e];console.log(`[Particle] --- Particle System ${e+1}/${v.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=ht[o]||ht.flare,i=o,console.log(`[Particle] Texture: ${o} -> ${n.substring(0,60)}...`)),console.log("[Particle] Loading texture...");const a=await ut(i,n);console.log("[Particle] ✅ Texture loaded:",a.name),console.log("[Particle] Creating entity...");const s=mt(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-${pt.size}`).replace(/[^a-zA-Z0-9]/g,"_");pt.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: ${pt.size}/${v.particles.length} particle systems created`),console.log("[Particle] Active particle systems:",Array.from(pt.keys())),console.log("═══════════════════════════════════════")}(),async function(){v.customMeshes&&0!==v.customMeshes.length?(console.log(`[StorySplat Viewer] Creating ${v.customMeshes.length} custom meshes...`),console.log("[StorySplat Viewer] All custom mesh configs:",v.customMeshes),setTimeout((()=>{let e=0,t=0;v.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{vt(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] ${v.customMeshes.length} custom meshes queued for loading`)):console.log("[StorySplat Viewer] No custom meshes to create")}(),function(){const t=v.skybox?.url||v.skyboxUrl;if(!t)return void console.log("[StorySplat Viewer] No skybox configured");const o=v.skybox?.rotation??v.skyboxRotation??0,n=v.skybox?.intensity??1,i=v.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"),r=new e.StandardMaterial;r.useLighting=!1,r.cull=e.CULLFACE_FRONT;const l=new e.Asset("skybox-texture","texture",{url:t});if(R.assets.add(l),l.ready((t=>{const o=t.resource;if(r.emissiveMap=o,r.emissive=new e.Color(n,n,n),r.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)}})),l.on("error",(e=>{console.error("[StorySplat Viewer] Failed to load skybox texture:",e)})),R.assets.load(l),s.addComponent("render",{type:"sphere",material:r,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=se.getPosition();s.setPosition(e.x,e.y,e.z)})),R.root.addChild(s),console.log("[StorySplat Viewer] Skybox created with rotation:",o,"intensity:",n)}(),Et(),$&&($.enabled=!0,console.log("[StorySplat Viewer] Reveal effect started immediately")),setTimeout((()=>{T.preloader&&p(T.preloader)}),200),function(){if(!v.includeXR)return;if(!R.xr)return void console.warn("[StorySplat Viewer] WebXR not supported in this browser");const t=R.xr,o=v.xrMode||"both";"vr"!==o&&"both"!==o||(t.isAvailable(e.XRTYPE_VR)&&(T.vrButton?.classList.add("available"),console.log("[StorySplat Viewer] VR is available")),t.on("available:"+e.XRTYPE_VR,(e=>{e?T.vrButton?.classList.add("available"):T.vrButton?.classList.remove("available")}))),"ar"!==o&&"both"!==o||(t.isAvailable(e.XRTYPE_AR)&&(T.arButton?.classList.add("available"),console.log("[StorySplat Viewer] AR is available")),t.on("available:"+e.XRTYPE_AR,(e=>{e?T.arButton?.classList.add("available"):T.arButton?.classList.remove("available")}))),t.on("start",(()=>{ge=!0,console.log("[StorySplat Viewer] XR session started"),"vr"===ye?(T.vrButton?.classList.add("active"),T.vrButton.textContent="Exit VR"):"ar"===ye&&(T.arButton?.classList.add("active"),T.arButton.textContent="Exit AR"),pe.disable(),de&&de.disable(),s.emit("xrStart",{type:ye})})),t.on("end",(()=>{ge=!1,console.log("[StorySplat Viewer] XR session ended"),T.vrButton?.classList.remove("active"),T.arButton?.classList.remove("active"),T.vrButton&&(T.vrButton.textContent="VR"),T.arButton&&(T.arButton.textContent="AR"),ye=null,"explore"===he?pe.enable():"walk"===he&&de&&de.enable(),s.emit("xrEnd",{})})),T.vrButton&&T.vrButton.addEventListener("click",(()=>{ge&&"vr"===ye?t.end():!ge&&t.isAvailable(e.XRTYPE_VR)&&(ye="vr",se.camera.startXr(e.XRTYPE_VR,e.XRSPACE_LOCALFLOOR,{callback:e=>{e&&(console.error("[StorySplat Viewer] Failed to start VR:",e),ye=null)}}))})),T.arButton&&T.arButton.addEventListener("click",(()=>{ge&&"ar"===ye?t.end():!ge&&t.isAvailable(e.XRTYPE_AR)&&(ye="ar",se.camera.startXr(e.XRTYPE_AR,e.XRSPACE_LOCALFLOOR,{callback:e=>{e&&(console.error("[StorySplat Viewer] Failed to start AR:",e),ye=null)}}))}))}(),v.htmlMeshes&&v.htmlMeshes.length>0){console.log("[StorySplat Viewer] Setting up HTML meshes:",v.htmlMeshes.length);const e=function(e,t){const o=new j(e);for(const e of t)o.createMesh(e);return o}(R,v.htmlMeshes);R.__htmlMeshManager=e,s.on("progressUpdate",(()=>{const t=100*_e,o=v.waypoints?.length||1,n=Math.round(_e*Math.max(1,o-1));e.updateVisibility(t,n)}))}if(v.customScript&&""!==v.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 p=new q(i);return p.initialize({app:t,camera:o,pc:e,canvas:n,getScrollPercentage:a,getCurrentWaypointIndex:s,getHotspots:r,getSplats:l,getHTMLMeshes:c}),p.execute(),p}(R,se,A,v.customScript,(()=>_e),(()=>V),(()=>at),(()=>B?[B]:[]),(()=>v.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&&v.waypoints&&v.waypoints.length>0){const e=parseInt(t,10);!isNaN(e)&&e>=0&&e<v.waypoints.length&&(console.log("[StorySplat Viewer] URL param: navigating to waypoint",e),Ze(e))}"true"!==o||n.autoPlay||v.autoPlay||(console.log("[StorySplat Viewer] URL param: starting autoplay"),ot())}catch(e){console.warn("[StorySplat Viewer] Could not parse URL parameters:",e)}s.emit("ready"),console.log("[StorySplat Viewer] Ready"),b&&(d(T,{nextWaypoint:Je,prevWaypoint:Ke,play:ot,pause:nt,isPlaying:()=>U,getCurrentWaypointIndex:()=>V,getWaypointCount:()=>v.waypoints?.length||0,getWaypoints:()=>v.waypoints||[],setCameraMode:fe,on:(e,t)=>s.on(e,t)},L,x.buttonLabels),T.returnWaypointButton&&v.waypoints&&v.waypoints.length>0&&T.returnWaypointButton.addEventListener("click",(()=>{const e=se.getPosition();let t=0,o=1/0;v.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)),fe("tour"),Ze(t);const n=T.scrollControls?.querySelectorAll(".storysplat-mode-btn");n?.forEach((e=>{const t=e.getAttribute("data-mode");e.classList.toggle("selected","tour"===t)}))})),s.emit("progressUpdate",{progress:_e,index:V}),v.waypoints&&v.waypoints.length>0&&s.emit("waypointChange",{index:0,waypoint:v.waypoints[0],prevIndex:-1})),(n.autoPlay||v.autoPlay)&&ot()})).catch((e=>{console.error("[StorySplat Viewer] Failed to initialize:",e),T.preloader&&p(T.preloader),s.emit("error",e)}));const Xt=()=>{R.resizeCanvas()};window.addEventListener("resize",Xt);const Yt={app:R,canvas:A,goToWaypoint:Ze,nextWaypoint:Je,prevWaypoint:Ke,getCurrentWaypointIndex:()=>V,getWaypointCount:()=>v.waypoints?.length||0,setPosition:(e,t,o)=>se.setPosition(e,t,o),setRotation:(e,t,o)=>se.setEulerAngles(e,t,o),getPosition:()=>{const e=se.getPosition();return{x:e.x,y:e.y,z:e.z}},getRotation:()=>{const e=se.getEulerAngles();return{x:e.x,y:e.y,z:e.z}},play:ot,pause:nt,stop:function(){if(O)return O.stop(),void s.emit("playbackStop");nt(),qe(0)},isPlaying:()=>U,setFrame:e=>O?.setFrame(e),getCurrentFrame:()=>O?.getCurrentFrame()??0,getTotalFrames:()=>O?.getTotalFrames()??0,setFps:e=>O?.setFps(e),getFps:()=>O?.getFps()??24,getFrameProgress:()=>O?.getProgress()??0,setFrameProgress:e=>O?.setProgress(e),destroy:()=>{H=!0,nt(),O&&(O.destroy(),O=null),window.removeEventListener("resize",Xt),T.preloader&&T.preloader.remove(),T.scrollControls&&T.scrollControls.remove(),T.fullscreenButton&&T.fullscreenButton.remove(),T.helpButton&&T.helpButton.remove(),T.helpPanel&&T.helpPanel.remove(),T.waypointInfo&&T.waypointInfo.remove(),T.watermark&&T.watermark.remove();const e=document.getElementById("storysplat-viewer-styles");e&&e.remove(),t.classList.remove("storysplat-viewer-container"),Pt.forEach((e=>e.destroy())),Pt.length=0,Mt(),de&&de.destroy(),R.__customScriptSystem&&R.__customScriptSystem.dispose(),R.__htmlMeshManager&&R.__htmlMeshManager.destroy(),R.destroy(),A.remove()},resize:Xt,navigateToScene:async e=>{const t={targetSceneId:e,activationMode:"click"};await Vt(t)},on:(e,t)=>s.on(e,t),off:(e,t)=>s.off(e,t)};return Yt}async function ee(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),Q(e,i,o)}function te(e,t,o){return e+(t-e)*o}class oe extends Error{constructor(e){super(`Scene not found: ${e}`),this.name="SceneNotFoundError"}}class ne extends Error{constructor(e,t){super(e),this.name="SceneApiError",this.statusCode=t}}const ie="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 ae(e,t,o={}){const n=o.baseUrl||ie;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 oe(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 ne(o,s.status)}const r=await s.json();if(!r.success||!r.data)throw new ne("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:p,...d}=o;return Q(e,l,d)}async function se(e,t={}){const o=`${t.baseUrl||ie}/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 oe(e);throw new ne(`API error: ${i.status}`,i.status)}const a=await i.json();return{...a,userName:a.userName||"Unknown",userSlug:a.userSlug||"unknown"}}export{X as FrameSequencePlayer,L as REVEAL_PRESETS,ne as SceneApiError,oe as SceneNotFoundError,Q as createViewer,ae as createViewerFromSceneId,ee as createViewerFromUrl,a as exportPropsToViewerConfig,se as fetchSceneMeta,o as generateHTML,n as generateHTMLFromUrl,P as getRevealPreset,i as transformSceneToExportProps};
|
|
2
2
|
//# sourceMappingURL=index.esm.js.map
|