UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

98 lines (97 loc) 2.39 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 EVENT_DESTROY = "destroy"; static EVENT_CHANGE = "change"; static EVENT_PERSIST = "persist"; static EVENT_FORGET = "forget"; _position = new Vec3(); _rotation = new Quat(); _uuid = null; _uuidRequests = null; constructor(anchors, xrAnchor, uuid = null) { super(); 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 };