Spaces:
Running
Running
File size: 5,558 Bytes
5e1643b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import * as THREE from 'three';
import {PilotFlight} from './pilot-flight.js?v=20261019explore1';
import {PilotThumbsticks} from './pilot-thumbsticks.js?v=20261019explore1';
const started=performance.now(),mobile=matchMedia('(pointer:coarse)').matches;
document.body.dataset.touch=String(mobile);
const report=(type,detail={})=>parent.postMessage({type,...detail},location.origin);
try{
const response=await fetch('../data/explore-scenes.json?v=20261019explore1');if(!response.ok)throw new Error('Scene list unavailable');
const data=await response.json(),params=new URLSearchParams(location.search),c=data.cases.find(c=>c.id===params.get('case'));
const scene=c?.models[params.get('model')],quality=params.get('quality')==='high'?'high':'standard',variant=scene?.variants[quality];
if(!variant)throw new Error('This scene is not available yet');
const high=quality==='high',url=new URL(variant.path,new URL(data.asset_base,location.origin)).href+'?v='+variant.sha256.slice(0,12);
window.C4S_VIEWER_CONFIG={textureSize:variant.texture_size|| (high?1024:256),shadowSize:high?2048:1024,maxWidth:high?1920:1280,maxHeight:high?1200:800,exposure:1.08};
window.BB_DATA={rig:{sun_azimuth_deg:300,sun_elevation_deg:45,sun_intensity:3.8,sun_colour:[1,1,1],sky_intensity:.65,environment_intensity:.7}};
window.SITE={cases:[{id:'pilot',agents:[{key:'pilot',glb:url}],ref_cam:scene.ref_cam}]};
const element=document.querySelector('.live-scene');element.classList.remove('live-scene');
await import('./scene-cards.js?v=20261019explore1');element.classList.add('live-scene');
const instance=new window.GLBViewer(element.querySelector('canvas'),{assetKey:url,refCam:scene.ref_cam,rotationX:.55,rotationY:-.62,speed:0});instance.setVisible(true);
let controls=null,thumbsticks=null,finished=false,lastProgress='';
window.destroyPilot=()=>{
if(finished)return;finished=true;thumbsticks?.destroy();controls?.destroy();
for(const v of [...window.activeViewers]){
const asset=v.asset;
if(asset){asset.materials?.dispose();const textures=new Set(),geometries=new Set(),materials=new Set();asset.scene.traverse(n=>{if(n.geometry)geometries.add(n.geometry);for(const mat of [].concat(n.material||[])){materials.add(mat);for(const t of Object.values(mat))if(t?.isTexture)textures.add(t);}});geometries.forEach(g=>g.dispose());textures.forEach(t=>{t.dispose();t.source?.data?.close?.();});materials.forEach(m=>m.dispose());}
v.destroy();
}
const r=window.BB_ENGINE.renderer();r.dispose();r.forceContextLoss();
};
window.explorerSnapshot=()=>controls?{position:controls.viewer.camera.position.toArray(),quaternion:controls.viewer.camera.quaternion.toArray(),mode:controls.mode,speed:controls.speed}:null;
window.addEventListener('keydown',event=>{if(event.key==='Escape')parent.pilotMobileScreen?.leave();});
const inspect=()=>{
if(finished)return;
const viewer=[...window.activeViewers][0],message=element.querySelector('.scene-texture-status')?.textContent||element.querySelector('.scene-loading')?.textContent||'Preparing scene…';
if(message!==lastProgress){lastProgress=message;report('pilot-progress',{message});}
if(element.dataset.statePhase==='error'){report('pilot-error',{message});return;}
if(viewer?.hasDrawn&&viewer.asset&&!controls){
const renderer=window.BB_ENGINE.renderer(),anisotropy=Math.min(high?8:4,renderer.capabilities.getMaxAnisotropy());
let meshes=0,instances=0,triangles=0;const textures=new Set();
viewer.asset.root.traverse(n=>{
if(!n.isMesh)return;meshes++;instances+=n.isInstancedMesh?n.count:1;triangles+=(n.geometry.index?.count??n.geometry.attributes.position?.count??0)/3*(n.isInstancedMesh?n.count:1);
for(const mat of [].concat(n.material||[])){mat.envMapIntensity=.7;if(mat.alphaTest>0)mat.alphaToCoverage=true;for(const t of Object.values(mat))if(t?.isTexture){textures.add(t);t.anisotropy=anisotropy;t.needsUpdate=true;}mat.needsUpdate=true;}
});
controls=new PilotFlight(viewer,{modes:document.querySelectorAll('[data-mode]'),reset:document.getElementById('reset-view'),speed:document.getElementById('fly-speed'),help:document.getElementById('camera-help')});
window.pilotControls=controls;viewer.dirty=true;
if(mobile){thumbsticks=new PilotThumbsticks(controls,{look:document.getElementById('look-stick'),move:document.getElementById('move-stick')});window.pilotThumbsticks=thumbsticks;}
const saved=parent.explorerRestore;
if(saved){if(saved.mode==='orbit')controls.setMode('orbit');viewer.camera.position.fromArray(saved.position);viewer.camera.quaternion.fromArray(saved.quaternion);controls.euler.setFromQuaternion(viewer.camera.quaternion,'YXZ');controls.speed=saved.speed;document.getElementById('fly-speed').value=String(saved.speed);viewer.dirty=true;parent.explorerRestore=null;}
window.pilotMetrics={load_ms:performance.now()-started,meshes,instances,triangles,textures:textures.size,textures_ready:false,quality,canvas:[viewer.canvas.width,viewer.canvas.height],camera:viewer.camera.position.toArray(),geometry_bytes:viewer.asset.geometry,texture_bytes:viewer.asset.textures};
report('pilot-ready',{metrics:window.pilotMetrics});
}
if(controls&&element.dataset.texturesLoaded===element.dataset.texturesTotal){
window.pilotMetrics.textures_ready=true;report('pilot-ready',{metrics:window.pilotMetrics});return;
}
setTimeout(inspect,150);
};inspect();
}catch(error){report('pilot-error',{message:error.message});console.error(error);}
|