UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 1.53 kB
import{platform as e}from"../../core/platform.js";import{EventHandler as s}from"../../core/event-handler.js";import{XrMesh as t}from"./xr-mesh.js";class i extends s{constructor(s){super(),this._manager=void 0,this._supported=e.browser&&!!window.XRMesh,this._available=!1,this._index=new Map,this._list=[],this._manager=s,this._supported&&(this._manager.on("start",this._onSessionStart,this),this._manager.on("end",this._onSessionEnd,this))}update(e){if(!this._available){if(this._manager.session.enabledFeatures||!e.detectedMeshes.size)return;this._available=!0,this.fire("available")}for(const s of e.detectedMeshes){let i=this._index.get(s);i?i.update(e):(i=new t(this,s),this._index.set(s,i),this._list.push(i),i.update(e),this.fire("add",i))}for(const s of this._index.values())e.detectedMeshes.has(s.xrMesh)||this._removeMesh(s)}_removeMesh(e){this._index.delete(e.xrMesh),this._list.splice(this._list.indexOf(e),1),e.destroy(),this.fire("remove",e)}_onSessionStart(){if(this._manager.session.enabledFeatures){const e=-1!==this._manager.session.enabledFeatures.indexOf("mesh-detection");if(!e)return;this._available=e,this.fire("available")}}_onSessionEnd(){if(this._available){this._available=!1;for(const e of this._index.values())this._removeMesh(e);this.fire("unavailable")}}get supported(){return this._supported}get available(){return this._available}get meshes(){return this._list}}i.EVENT_AVAILABLE="available",i.EVENT_UNAVAILABLE="unavailable",i.EVENT_ADD="add",i.EVENT_REMOVE="remove";export{i as XrMeshDetection};