+
+
+
+
+
+
diff --git a/src/callbacks.js b/src/callbacks.js
index 3acaa1aff73..fa39502c1bb 100644
--- a/src/callbacks.js
+++ b/src/callbacks.js
@@ -177,3 +177,10 @@
* @description Callback used by {@link pc.XrManager#endXr} and {@link pc.XrManager#startXr}.
* @param {Error|null} err - The Error object or null if operation was successfull.
*/
+
+/**
+ * @callback pc.callbacks.XrHitTestStart
+ * @description Callback used by {@link pc.XrHitTest#start} and {@link pc.XrHitTest#startForInputSource}.
+ * @param {Error|null} err - The Error object if failed to create hit test source or null.
+ * @param {pc.XrHitTestSource|null} hitTestSource - object that provides access to hit results against real world geometry.
+ */
diff --git a/src/xr/xr-hit-test-source.js b/src/xr/xr-hit-test-source.js
new file mode 100644
index 00000000000..794631eab3d
--- /dev/null
+++ b/src/xr/xr-hit-test-source.js
@@ -0,0 +1,117 @@
+Object.assign(pc, function () {
+ var poolVec3 = [];
+ var poolQuat = [];
+
+
+ /**
+ * @class
+ * @name pc.XrHitTestSource
+ * @augments pc.EventHandler
+ * @classdesc Represents XR hit test source, which provides access to hit results of real world geometry from AR session.
+ * @description Represents XR hit test source, which provides access to hit results of real world geometry from AR session.
+ * @param {pc.XrManager} manager - WebXR Manager.
+ * @param {object} xrHitTestSource - XRHitTestSource object that is created by WebXR API.
+ * @param {boolean} transient - True if XRHitTestSource created for input source profile.
+ * @example
+ * hitTestSource.on('result', function (position, rotation) {
+ * target.setPosition(position);
+ * });
+ */
+ var XrHitTestSource = function (manager, xrHitTestSource, transient) {
+ pc.EventHandler.call(this);
+
+ this.manager = manager;
+ this._xrHitTestSource = xrHitTestSource;
+ this._transient = transient;
+ };
+ XrHitTestSource.prototype = Object.create(pc.EventHandler.prototype);
+ XrHitTestSource.prototype.constructor = XrHitTestSource;
+
+ /**
+ * @event
+ * @name pc.XrHitTestSource#remove
+ * @description Fired when {pc.XrHitTestSource} is removed.
+ * @example
+ * hitTestSource.once('remove', function () {
+ * // hit test source has been removed
+ * });
+ */
+
+ /**
+ * @event
+ * @name pc.XrHitTestSource#result
+ * @description Fired when hit test source receives new results. It provides transform information that tries to match real world picked geometry.
+ * @param {pc.Vec3} position - Position of hit test
+ * @param {pc.Quat} rotation - Rotation of hit test
+ * @param {pc.XrInputSource|null} inputSource - If is transient hit test source, then it will provide related input source
+ * @example
+ * hitTestSource.on('result', function (position, rotation, inputSource) {
+ * target.setPosition(position);
+ * target.setRotation(rotation);
+ * });
+ */
+
+ /**
+ * @function
+ * @name pc.XrHitTestSource#remove
+ * @description Stop and remove hit test source.
+ */
+ XrHitTestSource.prototype.remove = function () {
+ if (! this._xrHitTestSource)
+ return;
+
+ var sources = this.manager.hitTest.sources;
+ var ind = sources.indexOf(this);
+ if (ind !== -1) sources.splice(ind, 1);
+
+ this.onStop();
+ };
+
+ XrHitTestSource.prototype.onStop = function () {
+ this._xrHitTestSource.cancel();
+ this._xrHitTestSource = null;
+
+ this.fire('remove');
+ this.manager.hitTest.fire('remove', this);
+ };
+
+ XrHitTestSource.prototype.update = function (frame) {
+ if (this._transient) {
+ var transientResults = frame.getHitTestResultsForTransientInput(this._xrHitTestSource);
+ for (var i = 0; i < transientResults.length; i++) {
+ var transientResult = transientResults[i];
+ var inputSource;
+
+ if (transientResult.inputSource)
+ inputSource = this.manager.input._getByInputSource(transientResult.inputSource);
+
+ this.updateHitResults(transientResult.results, inputSource);
+ }
+ } else {
+ this.updateHitResults(frame.getHitTestResults(this._xrHitTestSource));
+ }
+ };
+
+ XrHitTestSource.prototype.updateHitResults = function (results, inputSource) {
+ for (var i = 0; i < results.length; i++) {
+ var pose = results[i].getPose(this.manager._referenceSpace);
+
+ var position = poolVec3.pop();
+ if (! position) position = new pc.Vec3();
+ position.copy(pose.transform.position);
+
+ var rotation = poolQuat.pop();
+ if (! rotation) rotation = new pc.Quat();
+ rotation.copy(pose.transform.orientation);
+
+ this.fire('result', position, rotation, inputSource);
+ this.manager.hitTest.fire('result', this, position, rotation, inputSource);
+
+ poolVec3.push(position);
+ poolQuat.push(rotation);
+ }
+ };
+
+
+ return { XrHitTestSource: XrHitTestSource };
+}());
diff --git a/src/xr/xr-hit-test.js b/src/xr/xr-hit-test.js
new file mode 100644
index 00000000000..bd272331b9d
--- /dev/null
+++ b/src/xr/xr-hit-test.js
@@ -0,0 +1,287 @@
+Object.assign(pc, function () {
+ var hitTestTrackableTypes = {
+ /**
+ * @constant
+ * @type string
+ * @name pc.XRTRACKABLE_POINT
+ * @description Point - indicates that the hit test results will be computed based on the feature points detected by the underlying Augmented Reality system.
+ */
+ XRTRACKABLE_POINT: 'point',
+
+ /**
+ * @constant
+ * @type string
+ * @name pc.XRTRACKABLE_PLANE
+ * @description Plane - indicates that the hit test results will be computed based on the planes detected by the underlying Augmented Reality system.
+ */
+ XRTRACKABLE_PLANE: 'plane',
+
+ /**
+ * @constant
+ * @type string
+ * @name pc.XRTRACKABLE_MESH
+ * @description Mesh - indicates that the hit test results will be computed based on the meshes detected by the underlying Augmented Reality system.
+ */
+ XRTRACKABLE_MESH: 'mesh'
+ };
+
+
+ /**
+ * @class
+ * @name pc.XrHitTest
+ * @augments pc.EventHandler
+ * @classdesc Hit Test provides ability to get position and rotation of ray intersecting point with representation of real world geometry by underlying AR system.
+ * @description Hit Test provides ability to get position and rotation of ray intersecting point with representation of real world geometry by underlying AR system.
+ * @param {pc.XrManager} manager - WebXR Manager.
+ * @property {boolean} supported True if AR Hit Test is supported.
+ * @property {pc.XrHitTestSource[]} sources list of active {@link pc.XrHitTestSource}.
+ */
+ var XrHitTest = function (manager) {
+ pc.EventHandler.call(this);
+
+ this.manager = manager;
+ this._supported = !! window.XRSession.prototype.requestHitTestSource;
+
+ this._session = null;
+
+ this.sources = [];
+
+ if (this._supported) {
+ this.manager.on('start', this._onSessionStart, this);
+ this.manager.on('end', this._onSessionEnd, this);
+ }
+ };
+ XrHitTest.prototype = Object.create(pc.EventHandler.prototype);
+ XrHitTest.prototype.constructor = XrHitTest;
+
+ /**
+ * @event
+ * @name pc.XrHitTest#add
+ * @description Fired when new {@link pc.XrHitTestSource} is added to the list.
+ * @param {pc.XrHitTestSource} hitTestSource - Hit test source that has been added
+ * @example
+ * app.xr.hitTest.on('add', function (hitTestSource) {
+ * // new hit test source is added
+ * });
+ */
+
+ /**
+ * @event
+ * @name pc.XrHitTest#remove
+ * @description Fired when {@link pc.XrHitTestSource} is removed to the list.
+ * @param {pc.XrHitTestSource} hitTestSource - Hit test source that has been removed
+ * @example
+ * app.xr.hitTest.on('remove', function (hitTestSource) {
+ * // hit test source is removed
+ * });
+ */
+
+ /**
+ * @event
+ * @name pc.XrHitTest#result
+ * @description Fired when hit test source receives new results. It provides transform information that tries to match real world picked geometry.
+ * @param {pc.XrHitTestSource} hitTestSource - Hit test source that produced the hit result
+ * @param {pc.Vec3} position - Position of hit test
+ * @param {pc.Quat} rotation - Rotation of hit test
+ * @param {pc.XrInputSource|null} inputSource - If is transient hit test source, then it will provide related input source
+ * @example
+ * app.xr.hitTest.on('result', function (hitTestSource, position, rotation, inputSource) {
+ * target.setPosition(position);
+ * target.setRotation(rotation);
+ * });
+ */
+
+ /**
+ * @event
+ * @name pc.XrHitTest#error
+ * @param {Error} error - Error object related to failure of creating hit test source.
+ * @description Fired when failed create hit test source.
+ */
+
+ XrHitTest.prototype._onSessionStart = function () {
+ if (this.manager.type !== pc.XRTYPE_AR)
+ return;
+
+ this._session = this.manager.session;
+ };
+
+ XrHitTest.prototype._onSessionEnd = function () {
+ if (! this._session)
+ return;
+
+ this._session = null;
+
+ for (var i = 0; i < this.sources.length; i++) {
+ this.sources[i].onStop();
+ }
+ this.sources = [];
+ };
+
+ XrHitTest.prototype.isAvailable = function (callback, fireError) {
+ var err;
+
+ if (! this._supported)
+ err = new Error('XR HitTest is not supported');
+
+ if (! this._session)
+ err = new Error('XR Session is not started (1)');
+
+ if (this.manager.type !== pc.XRTYPE_AR)
+ err = new Error('XR HitTest is available only for AR');
+
+ if (err) {
+ if (callback) callback(err);
+ if (fireError) fireError.fire('error', err);
+ return false;
+ }
+
+ return true;
+ };
+
+ /**
+ * @function
+ * @name pc.XrHitTest#start
+ * @description Attempts to start hit test with provided reference space.
+ * @param {object} [options] - Optional object for passing arguments.
+ * @param {string} [options.spaceType] - Reference space type. Defaults to {@link pc.XRSPACE_VIEWER}. Can be one of the following:
+ *
+ * * {@link pc.XRSPACE_VIEWER}: Viewer - hit test will be facing relative to viewers space.
+ * * {@link pc.XRSPACE_LOCAL}: Local - represents a tracking space with a native origin near the viewer at the time of creation.
+ * * {@link pc.XRSPACE_LOCALFLOOR}: Local Floor - represents a tracking space with a native origin at the floor in a safe position for the user to stand. The y axis equals 0 at floor level. Floor level value might be estimated by the underlying platform.
+ * * {@link pc.XRSPACE_BOUNDEDFLOOR}: Bounded Floor - represents a tracking space with its native origin at the floor, where the user is expected to move within a pre-established boundary.
+ * * {@link pc.XRSPACE_UNBOUNDED}: Unbounded - represents a tracking space where the user is expected to move freely around their environment, potentially long distances from their starting point.
+ *
+ * @param {string} [options.profile] - if hit test source meant to match input source instead of reference space, then name of profile of the {@link pc.XrInputSource} should be provided.
+ * @param {string[]} [options.entityTypes] - Optional list of underlying entity types against which hit tests will be performed. Defaults to [ {pc.XRTRACKABLE_PLANE} ]. Can be any combination of the following:
+ *
+ * * {@link pc.XRTRACKABLE_POINT}: Point - indicates that the hit test results will be computed based on the feature points detected by the underlying Augmented Reality system.
+ * * {@link pc.XRTRACKABLE_PLANE}: Plane - indicates that the hit test results will be computed based on the planes detected by the underlying Augmented Reality system.
+ * * {@link pc.XRTRACKABLE_MESH}: Mesh - indicates that the hit test results will be computed based on the meshes detected by the underlying Augmented Reality system.
+ *
+ * @param {pc.Ray} [options.offsetRay] - Optional ray by which hit test ray can be offset.
+ * @param {pc.callbacks.XrHitTestStart} [options.callback] - Optional callback function called once hit test source is created or failed.
+ * @example
+ * app.xr.hitTest.start({
+ * spaceType: pc.XRSPACE_VIEWER,
+ * callback: function (err, hitTestSource) {
+ * if (err) return;
+ * hitTestSource.on('result', function (position, rotation) {
+ * // position and rotation of hit test result
+ * // based on Ray facing forward from the Viewer reference space
+ * });
+ * }
+ * });
+ * @example
+ * var ray = new pc.Ray(new pc.Vec3(0, 0, 0), new pc.Vec3(0, -1, 0));
+ * app.xr.hitTest.start({
+ * spaceType: pc.XRSPACE_LOCAL,
+ * offsetRay: ray,
+ * callback: function (err, hitTestSource) {
+ * // hit test source that will sample real world geometry straight down
+ * // from the position where AR session started
+ * }
+ * });
+ * @example
+ * app.xr.hitTest.start({
+ * profile: 'generic-touchscreen',
+ * callback: function (err, hitTestSource) {
+ * if (err) return;
+ * hitTestSource.on('result', function (position, rotation, inputSource) {
+ * // position and rotation of hit test result
+ * // that will be created from touch on mobile devices
+ * });
+ * }
+ * });
+ */
+ XrHitTest.prototype.start = function (options) {
+ var self = this;
+
+ options = options || { };
+
+ if (! this.isAvailable(options.callback, this))
+ return;
+
+ if (! options.profile && ! options.spaceType)
+ options.spaceType = pc.XRSPACE_VIEWER;
+
+ var xrRay;
+ var offsetRay = options.offsetRay;
+ if (offsetRay) xrRay = new XRRay(new DOMPoint(offsetRay.origin.x, offsetRay.origin.y, offsetRay.origin.z), new DOMPoint(offsetRay.direction.x, offsetRay.direction.y, offsetRay.direction.z));
+
+ var callback = options.callback;
+
+ if (options.spaceType) {
+ this._session.requestReferenceSpace(options.spaceType).then(function (referenceSpace) {
+ if (! self._session) {
+ var err = new Error('XR Session is not started (2)');
+ if (callback) callback(err);
+ self.fire('error', err);
+ return;
+ }
+
+ self._session.requestHitTestSource({
+ space: referenceSpace,
+ entityTypes: options.entityTypes || undefined,
+ offsetRay: xrRay
+ }).then(function (xrHitTestSource) {
+ self._onHitTestSource(xrHitTestSource, false, callback);
+ }).catch(function (ex) {
+ if (callback) callback(ex);
+ self.fire('error', ex);
+ });
+ }).catch(function (ex) {
+ if (callback) callback(ex);
+ self.fire('error', ex);
+ });
+ } else {
+ this._session.requestHitTestSourceForTransientInput({
+ profile: options.profile,
+ entityTypes: options.entityTypes || undefined,
+ offsetRay: xrRay
+ }).then(function (xrHitTestSource) {
+ self._onHitTestSource(xrHitTestSource, true, callback);
+ }).catch(function (ex) {
+ if (callback) callback(ex);
+ self.fire('error', ex);
+ });
+ }
+ };
+
+ XrHitTest.prototype._onHitTestSource = function (xrHitTestSource, transient, callback) {
+ if (! this._session) {
+ xrHitTestSource.cancel();
+ var err = new Error('XR Session is not started (3)');
+ if (callback) callback(err);
+ this.fire('error', err);
+ return;
+ }
+
+ var hitTestSource = new pc.XrHitTestSource(this.manager, xrHitTestSource, transient);
+ this.sources.push(hitTestSource);
+
+ if (callback) callback(null, hitTestSource);
+ this.fire('add', hitTestSource);
+ };
+
+ XrHitTest.prototype.update = function (frame) {
+ for (var i = 0; i < this.sources.length; i++) {
+ this.sources[i].update(frame);
+ }
+ };
+
+
+ Object.defineProperty(XrHitTest.prototype, 'supported', {
+ get: function () {
+ return this._supported;
+ }
+ });
+
+
+ var obj = {
+ XrHitTest: XrHitTest
+ };
+ Object.assign(obj, hitTestTrackableTypes);
+
+
+ return obj;
+}());
diff --git a/src/xr/xr-input-source.js b/src/xr/xr-input-source.js
index e0c453081e2..28a9da924e3 100644
--- a/src/xr/xr-input-source.js
+++ b/src/xr/xr-input-source.js
@@ -58,8 +58,8 @@ Object.assign(pc, function () {
* @class
* @name pc.XrInputSource
* @augments pc.EventHandler
- * @classdesc Represents an XR input source, which is any input mechanism which allows the user to perform targeted actions in the same virtual space as the viewer. Example XR input sources include, but are not limited to, handheld controllers, optically tracked hands, and gaze-based input methods that operate on the viewer's pose.
- * @description Represents an XR input source, which is any input mechanism which allows the user to perform targeted actions in the same virtual space as the viewer. Example XR input sources include, but are not limited to, handheld controllers, optically tracked hands, and gaze-based input methods that operate on the viewer's pose.
+ * @classdesc Represents XR input source, which is any input mechanism which allows the user to perform targeted actions in the same virtual space as the viewer. Example XR input sources include, but are not limited to, handheld controllers, optically tracked hands, and gaze-based input methods that operate on the viewer's pose.
+ * @description Represents XR input source, which is any input mechanism which allows the user to perform targeted actions in the same virtual space as the viewer. Example XR input sources include, but are not limited to, handheld controllers, optically tracked hands, and gaze-based input methods that operate on the viewer's pose.
* @param {pc.XrManager} manager - WebXR Manager.
* @param {object} xrInputSource - XRInputSource object that is created by WebXR API.
* @property {object} inputSource XRInputSource object that is associated with this input source.
@@ -76,12 +76,13 @@ Object.assign(pc, function () {
* * {@link pc.XRHAND_RIGHT}: Right - indicates that input source is meant to be held in right hand.
*
* @property {string[]} profiles List of input profile names indicating both the prefered visual representation and behavior of the input source.
- * @property {pc.Ray} ray Ray that is calculated based on {pc.XrInputSource#targetRayMode} that can be used for interacting with virtual objects. Its origin and direction are in local space of XR session.
+ * @property {pc.Ray} ray Ray that is calculated based on {@link pc.XrInputSource#targetRayMode} that can be used for interacting with virtual objects. Its origin and direction are in local space of XR session.
* @property {boolean} grip If input source can be held, then it will have node with its world transformation, that can be used to position and rotate virtual joystics based on it.
- * @property {pc.Vec3|null} position If {pc.XrInputSource#grip} is true, then position will represent position of handheld input source in local space of XR session.
- * @property {pc.Quat|null} rotation If {pc.XrInputSource#grip} is true, then rotation will represent rotation of handheld input source in local space of XR session.
+ * @property {pc.Vec3|null} position If {@link pc.XrInputSource#grip} is true, then position will represent position of handheld input source in local space of XR session.
+ * @property {pc.Quat|null} rotation If {@link pc.XrInputSource#grip} is true, then rotation will represent rotation of handheld input source in local space of XR session.
* @property {Gamepad|null} gamepad If input source has buttons, triggers, thumbstick or touchpad, then this object provides access to its states.
* @property {boolean} selecting True if input source is in active primary action between selectstart and selectend events.
+ * @property {pc.XrHitTestSource[]} hitTestSources list of active {@link pc.XrHitTestSource} created by this input source.
*/
var XrInputSource = function (manager, xrInputSource) {
pc.EventHandler.call(this);
@@ -94,6 +95,8 @@ Object.assign(pc, function () {
this._position = null;
this._rotation = null;
this._selecting = false;
+
+ this._hitTestSources = [];
};
XrInputSource.prototype = Object.create(pc.EventHandler.prototype);
XrInputSource.prototype.constructor = XrInputSource;
@@ -101,9 +104,9 @@ Object.assign(pc, function () {
/**
* @event
* @name pc.XrInputSource#remove
- * @description Fired when {pc.XrInputSource} is removed.
+ * @description Fired when {@link pc.XrInputSource} is removed.
* @example
- * inputSource.on('remove', function () {
+ * inputSource.once('remove', function () {
* // input source is not available anymore
* });
*/
@@ -135,6 +138,42 @@ Object.assign(pc, function () {
* @param {object} evt - XRInputSourceEvent event data from WebXR API
*/
+ /**
+ * @event
+ * @name pc.XrInputSource#hittest:add
+ * @description Fired when new {@link pc.XrHitTestSource} is added to the input source.
+ * @param {pc.XrHitTestSource} hitTestSource - Hit test source that has been added
+ * @example
+ * inputSource.on('hittest:add', function (hitTestSource) {
+ * // new hit test source is added
+ * });
+ */
+
+ /**
+ * @event
+ * @name pc.XrInputSource#hittest:remove
+ * @description Fired when {@link pc.XrHitTestSource} is removed to the the input source.
+ * @param {pc.XrHitTestSource} hitTestSource - Hit test source that has been removed
+ * @example
+ * inputSource.on('remove', function (hitTestSource) {
+ * // hit test source is removed
+ * });
+ */
+
+ /**
+ * @event
+ * @name pc.XrInputSource#hittest:result
+ * @description Fired when hit test source receives new results. It provides transform information that tries to match real world picked geometry.
+ * @param {pc.XrHitTestSource} hitTestSource - Hit test source that produced the hit result
+ * @param {pc.Vec3} position - Position of hit test
+ * @param {pc.Quat} rotation - Rotation of hit test
+ * @example
+ * inputSource.on('hittest:result', function (hitTestSource, position, rotation) {
+ * target.setPosition(position);
+ * target.setRotation(rotation);
+ * });
+ */
+
XrInputSource.prototype.update = function (frame) {
var targetRayPose = frame.getPose(this._xrInputSource.targetRaySpace, this._manager._referenceSpace);
if (! targetRayPose) return;
@@ -161,6 +200,75 @@ Object.assign(pc, function () {
}
};
+ /**
+ * @function
+ * @name pc.XrInputSource#hitTestStart
+ * @description Attempts to start hit test source based on this input source.
+ * @param {object} [options] - Object for passing optional arguments.
+ * @param {string[]} [options.entityTypes] - Optional list of underlying entity types
+ * against which hit tests will be performed. Defaults to [ {pc.XRTRACKABLE_PLANE} ].
+ * Can be any combination of the following:
+ *
+ * * {@link pc.XRTRACKABLE_POINT}: Point - indicates that the hit test results will be
+ * computed based on the feature points detected by the underlying Augmented Reality system.
+ * * {@link pc.XRTRACKABLE_PLANE}: Plane - indicates that the hit test results will be
+ * computed based on the planes detected by the underlying Augmented Reality system.
+ * * {@link pc.XRTRACKABLE_MESH}: Mesh - indicates that the hit test results will be
+ * computed based on the meshes detected by the underlying Augmented Reality system.
+ *
+ * @param {pc.Ray} [options.offsetRay] - Optional ray by which hit test ray can be offset.
+ * @param {pc.callbacks.XrHitTestStart} [options.callback] - Optional callback function
+ * called once hit test source is created or failed.
+ * @example
+ * app.xr.input.on('add', function (inputSource) {
+ * inputSource.hitTestStart({
+ * callback: function (err, hitTestSource) {
+ * if (err) return;
+ * hitTestSource.on('result', function (position, rotation) {
+ * // position and rotation of hit test result
+ * // that will be created from touch on mobile devices
+ * });
+ * }
+ * });
+ * });
+ */
+ XrInputSource.prototype.hitTestStart = function (options) {
+ var self = this;
+
+ options = options || { };
+ options.profile = this._xrInputSource.profiles[0];
+
+ var callback = options.callback;
+ options.callback = function (err, hitTestSource) {
+ if (hitTestSource) self.onHitTestSourceAdd(hitTestSource);
+ if (callback) callback(err, hitTestSource);
+ };
+
+ this._manager.hitTest.start(options);
+ };
+
+ XrInputSource.prototype.onHitTestSourceAdd = function (hitTestSource) {
+ this._hitTestSources.push(hitTestSource);
+
+ this.fire('hittest:add', hitTestSource);
+
+ hitTestSource.on('result', function (position, rotation, inputSource) {
+ if (inputSource !== this)
+ return;
+
+ this.fire('hittest:result', hitTestSource, position, rotation);
+ }, this);
+ hitTestSource.once('remove', function () {
+ this.onHitTestSourceRemove(hitTestSource);
+ this.fire('hittest:remove', hitTestSource);
+ }, this);
+ };
+
+ XrInputSource.prototype.onHitTestSourceRemove = function (hitTestSource) {
+ var ind = this._hitTestSources.indexOf(hitTestSource);
+ if (ind !== -1) this._hitTestSources.splice(ind, 1);
+ };
+
Object.defineProperty(XrInputSource.prototype, 'inputSource', {
get: function () {
return this._xrInputSource;
@@ -221,6 +329,12 @@ Object.assign(pc, function () {
}
});
+ Object.defineProperty(XrInputSource.prototype, 'hitTestSources', {
+ get: function () {
+ return this._hitTestSources;
+ }
+ });
+
var obj = {
XrInputSource: XrInputSource
diff --git a/src/xr/xr-input.js b/src/xr/xr-input.js
index 9ee367667b6..9ad6c2304d2 100644
--- a/src/xr/xr-input.js
+++ b/src/xr/xr-input.js
@@ -6,7 +6,7 @@ Object.assign(pc, function () {
* @classdesc Provides access to input sources for WebXR.
* @description Provides access to input sources for WebXR.
* @param {pc.XrManager} manager - WebXR Manager.
- * @property {pc.XrInputSource[]} inputSources List of active {pc.XrInputSource}
+ * @property {pc.XrInputSource[]} inputSources List of active {@link pc.XrInputSource}.
*/
var XrInput = function (manager) {
pc.EventHandler.call(this);
@@ -30,7 +30,7 @@ Object.assign(pc, function () {
/**
* @event
* @name pc.XrInput#add
- * @description Fired when new {pc.XrInputSource} is added to the list.
+ * @description Fired when new {@link pc.XrInputSource} is added to the list.
* @param {pc.XrInputSource} inputSource - Input source that has been added
* @example
* app.xr.input.on('add', function (inputSource) {
@@ -41,7 +41,7 @@ Object.assign(pc, function () {
/**
* @event
* @name pc.XrInput#remove
- * @description Fired when {pc.XrInputSource} is removed to the list.
+ * @description Fired when {@link pc.XrInputSource} is removed to the list.
* @param {pc.XrInputSource} inputSource - Input source that has been removed
* @example
* app.xr.input.on('remove', function (inputSource) {
@@ -52,7 +52,7 @@ Object.assign(pc, function () {
/**
* @event
* @name pc.XrInput#select
- * @description Fired when {pc.XrInputSource} has triggered primary action. This could be pressing a trigger button, or touching a screen.
+ * @description Fired when {@link pc.XrInputSource} has triggered primary action. This could be pressing a trigger button, or touching a screen.
* @param {pc.XrInputSource} inputSource - Input source that triggered select event
* @param {object} evt - XRInputSourceEvent event data from WebXR API
* @example
@@ -166,6 +166,12 @@ Object.assign(pc, function () {
var inputSource = this._inputSources[i];
this._inputSources.splice(i, 1);
+
+ var h = inputSource.hitTestSources.length;
+ while (h--) {
+ inputSource.hitTestSources[h].remove();
+ }
+
inputSource.fire('remove');
this.fire('remove', inputSource);
return;
diff --git a/src/xr/xr-manager.js b/src/xr/xr-manager.js
index 39a29d120f3..1f8578274aa 100644
--- a/src/xr/xr-manager.js
+++ b/src/xr/xr-manager.js
@@ -98,6 +98,8 @@ Object.assign(pc, function () {
* @property {string|null} spaceType Returns reference space type of currently running XR session or null if no session
* is running. Can be any of pc.XRSPACE_*.
* @property {pc.Entity|null} camera Active camera for which XR session is running or null.
+ * @property {pc.XrInput} input provides access to Input Sources.
+ * @property {pc.XrHitTest} hitTest provides ability to hit test representation of real world geometry of underlying AR system.
*/
var XrManager = function (app) {
pc.EventHandler.call(this);
@@ -118,7 +120,9 @@ Object.assign(pc, function () {
this._session = null;
this._baseLayer = null;
this._referenceSpace = null;
+
this.input = new pc.XrInput(this);
+ this.hitTest = new pc.XrHitTest(this);
this._camera = null;
this._pose = null;
@@ -504,6 +508,9 @@ Object.assign(pc, function () {
this._camera.camera._node.setLocalRotation(this.rotation);
this.input.update(frame);
+
+ if (this._type === pc.XRTYPE_AR && this.hitTest.supported)
+ this.hitTest.update(frame);
};
Object.defineProperty(XrManager.prototype, 'supported', {