UNPKG

playcanvas

Version:

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

133 lines (132 loc) 3.55 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { EventHandler } from "../../core/event-handler.js"; import { Vec3 } from "../../core/math/vec3.js"; import { Quat } from "../../core/math/quat.js"; class XrMesh extends EventHandler { /** * Create a new XrMesh instance. * * @param {XrMeshDetection} meshDetection - Mesh Detection * interface. * @param {XRMesh} xrMesh - XRMesh that is instantiated by WebXR system. * @ignore */ constructor(meshDetection, xrMesh) { super(); /** * @type {XrMeshDetection} * @private */ __publicField(this, "_meshDetection"); /** * @type {XRMesh} * @private */ __publicField(this, "_xrMesh"); /** @private */ __publicField(this, "_lastChanged", 0); /** @private */ __publicField(this, "_position", new Vec3()); /** @private */ __publicField(this, "_rotation", new Quat()); this._meshDetection = meshDetection; this._xrMesh = xrMesh; this._lastChanged = this._xrMesh.lastChangedTime; } /** * @type {XRMesh} * @ignore */ get xrMesh() { return this._xrMesh; } /** * Semantic Label of a mesh that is provided by underlying system. Current list includes (but * not limited to): https://github.com/immersive-web/semantic-labels/blob/master/labels.json * * @type {string} */ get label() { return this._xrMesh.semanticLabel || ""; } /** * Array of mesh vertices. This array contains 3 components per vertex (`x, y, z`). * * @type {Float32Array} */ get vertices() { return this._xrMesh.vertices; } /** * Array of mesh indices. * * @type {Uint32Array} */ get indices() { return this._xrMesh.indices; } /** @ignore */ destroy() { if (!this._xrMesh) return; this._xrMesh = null; this.fire("remove"); } /** * @param {XRFrame} frame - XRFrame from requestAnimationFrame callback. * @ignore */ update(frame) { const manager = this._meshDetection._manager; const pose = frame.getPose(this._xrMesh.meshSpace, manager._referenceSpace); if (pose) { this._position.copy(pose.transform.position); this._rotation.copy(pose.transform.orientation); } if (this._lastChanged !== this._xrMesh.lastChangedTime) { this._lastChanged = this._xrMesh.lastChangedTime; this.fire("change"); } } /** * Get the world space position of a mesh. * * @returns {Vec3} The world space position of a mesh. */ getPosition() { return this._position; } /** * Get the world space rotation of a mesh. * * @returns {Quat} The world space rotation of a mesh. */ getRotation() { return this._rotation; } } /** * Fired when an {@link XrMesh} is removed. * * @event * @example * mesh.once('remove', () => { * // mesh is no longer available * }); */ __publicField(XrMesh, "EVENT_REMOVE", "remove"); /** * Fired when {@link XrMesh} attributes such as vertices, indices and/or label have been * changed. Position and rotation can change at any time without triggering a `change` event. * * @event * @example * mesh.on('change', () => { * // mesh attributes have been changed * }); */ __publicField(XrMesh, "EVENT_CHANGE", "change"); export { XrMesh };