UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

120 lines (117 loc) 3.6 kB
import { EventHandler } from '../../core/event-handler.js'; import { Vec3 } from '../../core/math/vec3.js'; import { Quat } from '../../core/math/quat.js'; /** * @import { XrMeshDetection } from './xr-mesh-detection.js' */ /** * Detected Mesh instance that provides its transform (position, rotation), triangles (vertices, * indices) and its semantic label. Any of its properties can change during its lifetime. * * @category XR */ class XrMesh extends EventHandler { /** * @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) { var manager = this._meshDetection._manager; var pose = frame.getPose(this._xrMesh.meshSpace, manager._referenceSpace); if (pose) { this._position.copy(pose.transform.position); this._rotation.copy(pose.transform.orientation); } // attributes have been changed 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; } /** * 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 {number} * @private */ this._lastChanged = 0, /** * @type {Vec3} * @private */ this._position = new Vec3(), /** * @type {Quat} * @private */ this._rotation = new Quat(); this._meshDetection = meshDetection; this._xrMesh = xrMesh; this._lastChanged = this._xrMesh.lastChangedTime; } } /** * Fired when an {@link XrMesh} is removed. * * @event * @example * mesh.once('remove', () => { * // mesh is no longer available * }); */ 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 * }); */ XrMesh.EVENT_CHANGE = 'change'; export { XrMesh };