@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 1.59 kB
JavaScript
import{platform as e}from"../../core/platform.js";import{EventHandler as s}from"../../core/event-handler.js";import{XrPlane as a}from"./xr-plane.js";class t extends s{constructor(s){super(),this._manager=void 0,this._supported=e.browser&&!!window.XRPlane,this._available=!1,this._planesIndex=new Map,this._planes=[],this._manager=s,this._supported&&(this._manager.on("start",this._onSessionStart,this),this._manager.on("end",this._onSessionEnd,this))}_onSessionStart(){if(this._manager.session.enabledFeatures){-1!==this._manager.session.enabledFeatures.indexOf("plane-detection")&&(this._available=!0,this.fire("available"))}}_onSessionEnd(){for(let e=0;e<this._planes.length;e++)this._planes[e].destroy(),this.fire("remove",this._planes[e]);this._planesIndex.clear(),this._planes.length=0,this._available&&(this._available=!1,this.fire("unavailable"))}update(e){if(!this._available){if(this._manager.session.enabledFeatures||!e.detectedPlanes.size)return;this._available=!0,this.fire("available")}const s=e.detectedPlanes;for(const[e,a]of this._planesIndex)s.has(e)||(this._planesIndex.delete(e),this._planes.splice(this._planes.indexOf(a),1),a.destroy(),this.fire("remove",a));for(const t of s){let s=this._planesIndex.get(t);s?s.update(e):(s=new a(this,t),this._planesIndex.set(t,s),this._planes.push(s),s.update(e),this.fire("add",s))}}get supported(){return this._supported}get available(){return this._available}get planes(){return this._planes}}t.EVENT_AVAILABLE="available",t.EVENT_UNAVAILABLE="unavailable",t.EVENT_ADD="add",t.EVENT_REMOVE="remove";export{t as XrPlaneDetection};