UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

91 lines (88 loc) 2.45 kB
import { platform } from '../../core/platform.js'; import { EventHandler } from '../../core/event-handler.js'; import { XrPlane } from './xr-plane.js'; class XrPlaneDetection extends EventHandler { static{ this.EVENT_AVAILABLE = 'available'; } static{ this.EVENT_UNAVAILABLE = 'unavailable'; } static{ this.EVENT_ADD = 'add'; } static{ this.EVENT_REMOVE = 'remove'; } constructor(manager){ super(), this._supported = platform.browser && !!window.XRPlane, this._available = false, this._planesIndex = new Map(), this._planes = []; this._manager = manager; if (this._supported) { this._manager.on('start', this._onSessionStart, this); this._manager.on('end', this._onSessionEnd, this); } } _onSessionStart() { if (this._manager.session.enabledFeatures) { const available = this._manager.session.enabledFeatures.indexOf('plane-detection') !== -1; if (available) { this._available = true; this.fire('available'); } } } _onSessionEnd() { for(let i = 0; i < this._planes.length; i++){ this._planes[i].destroy(); this.fire('remove', this._planes[i]); } this._planesIndex.clear(); this._planes.length = 0; if (this._available) { this._available = false; this.fire('unavailable'); } } update(frame) { if (!this._available) { if (!this._manager.session.enabledFeatures && frame.detectedPlanes.size) { this._available = true; this.fire('available'); } else { return; } } const detectedPlanes = frame.detectedPlanes; for (const [xrPlane, plane] of this._planesIndex){ if (detectedPlanes.has(xrPlane)) { continue; } this._planesIndex.delete(xrPlane); this._planes.splice(this._planes.indexOf(plane), 1); plane.destroy(); this.fire('remove', plane); } for (const xrPlane of detectedPlanes){ let plane = this._planesIndex.get(xrPlane); if (!plane) { plane = new XrPlane(this, xrPlane); this._planesIndex.set(xrPlane, plane); this._planes.push(plane); plane.update(frame); this.fire('add', plane); } else { plane.update(frame); } } } get supported() { return this._supported; } get available() { return this._available; } get planes() { return this._planes; } } export { XrPlaneDetection };