/* Real-time planar reflection for the existing MR mirror-door meshes. Geometry stays in the GLB. */ const MirrorReflections=(()=>{ function reflect(point,normal,origin){return point.clone().addScaledVector(normal,-2*normal.dot(point.clone().sub(origin)));} function attach(viewer){ const mirrors=[]; viewer.model.traverse(object=>{if(object.isMesh&&String(object.userData.material||'').startsWith('MR-'))mirrors.push(object);}); if(!mirrors.length)return null; const groups=new Map(); for(const mesh of mirrors){ const box=new THREE.Box3().setFromObject(mesh),center=box.getCenter(new THREE.Vector3()),extent=box.getSize(new THREE.Vector3()); const axis=extent.x\n#include \n}', }); for(const mesh of group.meshes){mesh.userData.mirrorMaterial=material;mesh.material=material;} entries.push({...group,target,material}); } return {entries,mirrors}; } function update(viewer){ const state=viewer.mirrors;if(!state||viewer.white)return; const {renderer,scene,camera}=viewer; camera.updateMatrixWorld(); const originalTarget=renderer.getRenderTarget(),originalClipping=renderer.clippingPlanes; const aspect=Math.max(0.5,Math.min(3,camera.aspect||renderer.domElement.clientWidth/Math.max(1,renderer.domElement.clientHeight))); const visibility=state.mirrors.map(mesh=>mesh.visible); for(const mirror of state.mirrors)mirror.visible=false; try{ for(const entry of state.entries){ if(!entry.meshes.some((mesh,i)=>visibility[state.mirrors.indexOf(mesh)]))continue; const height=512,width=Math.max(256,Math.min(1536,Math.round(height*aspect))); if(entry.target.width!==width||entry.target.height!==height)entry.target.setSize(width,height); const normal=new THREE.Vector3(entry.axis==='x'?1:0,0,entry.axis==='z'?1:0); if(normal.dot(camera.position.clone().sub(entry.point))<0)normal.negate(); const reflected=camera.clone(),direction=new THREE.Vector3();camera.getWorldDirection(direction); reflected.position.copy(reflect(camera.position,normal,entry.point)); reflected.up.copy(camera.up).reflect(normal); reflected.lookAt(reflect(camera.position.clone().add(direction),normal,entry.point)); reflected.aspect=width/height;reflected.updateProjectionMatrix();reflected.updateMatrixWorld(); entry.material.uniforms.projection.value.multiplyMatrices(reflected.projectionMatrix,reflected.matrixWorldInverse); renderer.clippingPlanes=[new THREE.Plane(normal,-normal.dot(entry.point))]; renderer.setRenderTarget(entry.target);renderer.clear();renderer.render(scene,reflected); const stage=renderer.domElement.parentElement;if(stage){stage.dataset.mirrorPlanes=String(state.entries.length);stage.dataset.mirrorUpdates=String((Number(stage.dataset.mirrorUpdates)||0)+1);} } }finally{ renderer.setRenderTarget(originalTarget);renderer.clippingPlanes=originalClipping; state.mirrors.forEach((mesh,i)=>mesh.visible=visibility[i]); } } function restore(viewer){if(!viewer.mirrors)return;for(const mesh of viewer.mirrors.mirrors)mesh.material=viewer.white?mesh.userData.whiteMaterial:mesh.userData.mirrorMaterial;} function dispose(viewer){if(!viewer.mirrors)return;for(const entry of viewer.mirrors.entries){entry.target.dispose();entry.material.dispose();}viewer.mirrors=null;} return {attach,update,restore,dispose}; })();