UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

102 lines (99 loc) 2.64 kB
import { EventHandler } from '../../core/event-handler.js'; import { Vec3 } from '../../core/math/vec3.js'; import { Quat } from '../../core/math/quat.js'; class XrAnchor extends EventHandler { static{ this.EVENT_DESTROY = 'destroy'; } static{ this.EVENT_CHANGE = 'change'; } static{ this.EVENT_PERSIST = 'persist'; } static{ this.EVENT_FORGET = 'forget'; } constructor(anchors, xrAnchor, uuid = null){ super(), this._position = new Vec3(), this._rotation = new Quat(), this._uuid = null, this._uuidRequests = null; this._anchors = anchors; this._xrAnchor = xrAnchor; this._uuid = uuid; } destroy() { if (!this._xrAnchor) return; const xrAnchor = this._xrAnchor; this._xrAnchor.delete(); this._xrAnchor = null; this.fire('destroy', xrAnchor, this); } update(frame) { if (!this._xrAnchor) { return; } const pose = frame.getPose(this._xrAnchor.anchorSpace, this._anchors.manager._referenceSpace); if (pose) { if (this._position.equals(pose.transform.position) && this._rotation.equals(pose.transform.orientation)) { return; } this._position.copy(pose.transform.position); this._rotation.copy(pose.transform.orientation); this.fire('change'); } } getPosition() { return this._position; } getRotation() { return this._rotation; } persist(callback) { if (!this._anchors.persistence) { callback?.(new Error('Persistent Anchors are not supported'), null); return; } if (this._uuid) { callback?.(null, this._uuid); return; } if (this._uuidRequests) { if (callback) this._uuidRequests.push(callback); return; } this._uuidRequests = []; this._xrAnchor.requestPersistentHandle().then((uuid)=>{ this._uuid = uuid; this._anchors._indexByUuid.set(this._uuid, this); callback?.(null, uuid); for (const uuidRequest of this._uuidRequests){ uuidRequest(null, uuid); } this._uuidRequests = null; this.fire('persist', uuid); }).catch((ex)=>{ callback?.(ex, null); for (const uuidRequest of this._uuidRequests){ uuidRequest(ex, null); } this._uuidRequests = null; }); } forget(callback) { if (!this._uuid) { callback?.(new Error('Anchor is not persistent')); return; } this._anchors.forget(this._uuid, (ex)=>{ this._uuid = null; callback?.(ex); this.fire('forget'); }); } get uuid() { return this._uuid; } get persistent() { return !!this._uuid; } } export { XrAnchor };