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, touch screen taps, and gaze-based input methods that operate on the viewer's pose.

Hierarchy (view full)

Accessors

  • get targetRayMode(): string
  • Type of ray Input Device is based on. Can be one of the following:

    • XRTARGETRAY_GAZE: Gaze - indicates the target ray will originate at the viewer and follow the direction it is facing. This is commonly referred to as a "gaze input" device in the context of head-mounted displays.
    • XRTARGETRAY_SCREEN: Screen - indicates that the input source was an interaction with the canvas element associated with an inline session's output context, such as a mouse click or touch event.
    • XRTARGETRAY_POINTER: Tracked Pointer - indicates that the target ray originates from either a handheld device or other hand-tracking mechanism and represents that the user is using their hands or the held device for pointing.

    Returns string

Methods

  • Fire an event, all additional arguments are passed on to the event listener.

    Parameters

    • name: string

      Name of event to fire.

    • Optionalarg1: any

      First argument that is passed to the event handler.

    • Optionalarg2: any

      Second argument that is passed to the event handler.

    • Optionalarg3: any

      Third argument that is passed to the event handler.

    • Optionalarg4: any

      Fourth argument that is passed to the event handler.

    • Optionalarg5: any

      Fifth argument that is passed to the event handler.

    • Optionalarg6: any

      Sixth argument that is passed to the event handler.

    • Optionalarg7: any

      Seventh argument that is passed to the event handler.

    • Optionalarg8: any

      Eighth argument that is passed to the event handler.

    Returns EventHandler

    Self for chaining.

    obj.fire('test', 'This is the message');
    
  • Test if there are any handlers bound to an event name.

    Parameters

    • name: string

      The name of the event to test.

    Returns boolean

    True if the object has handlers bound to the specified event name.

    obj.on('test', function () { }); // bind an event to 'test'
    obj.hasEvent('test'); // returns true
    obj.hasEvent('hello'); // returns false
  • Attempts to start hit test source based on this input source.

    Parameters

    • Optionaloptions: {
          callback: XrHitTestStartCallback;
          entityTypes: string[];
          offsetRay: Ray;
      } = {}

      Object for passing optional arguments.

      • callback: XrHitTestStartCallback

        Optional callback function called once hit test source is created or failed.

      • entityTypes: string[]

        Optional list of underlying entity types against which hit tests will be performed. Defaults to [ XRTRACKABLE_PLANE ]. Can be any combination of the following:

        • XRTRACKABLE_POINT: Point - indicates that the hit test results will be computed based on the feature points detected by the underlying Augmented Reality system.
        • XRTRACKABLE_PLANE: Plane - indicates that the hit test results will be computed based on the planes detected by the underlying Augmented Reality system.
        • XRTRACKABLE_MESH: Mesh - indicates that the hit test results will be computed based on the meshes detected by the underlying Augmented Reality system.
      • offsetRay: Ray

        Optional ray by which hit test ray can be offset.

    Returns void

    app.xr.input.on('add', function (inputSource) {
    inputSource.hitTestStart({
    callback: function (err, hitTestSource) {
    if (err) return;
    hitTestSource.on('result', function (position, rotation, inputSource, hitTestResult) {
    // position and rotation of hit test result
    // that will be created from touch on mobile devices
    });
    }
    });
    });
  • Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.

    Parameters

    • Optionalname: string

      Name of the event to unbind.

    • Optionalcallback: HandleEventCallback

      Function to be unbound.

    • Optionalscope: object

      Scope that was used as the this when the event is fired.

    Returns EventHandler

    Self for chaining.

    const handler = function () {
    };
    obj.on('test', handler);

    obj.off(); // Removes all events
    obj.off('test'); // Removes all events called 'test'
    obj.off('test', handler); // Removes all handler functions, called 'test'
    obj.off('test', handler, this); // Removes all handler functions, called 'test' with scope this
  • Attach an event handler to an event.

    Parameters

    • name: string

      Name of the event to bind the callback to.

    • callback: HandleEventCallback

      Function that is called when event is fired. Note the callback is limited to 8 arguments.

    • Optionalscope: object = ...

      Object to use as 'this' when the event is fired, defaults to current this.

    Returns EventHandle

    Can be used for removing event in the future.

    obj.on('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console
    const evt = obj.on('test', function (a, b) {
    console.log(a + b);
    });
    // some time later
    evt.off();
  • Attach an event handler to an event. This handler will be removed after being fired once.

    Parameters

    • name: string

      Name of the event to bind the callback to.

    • callback: HandleEventCallback

      Function that is called when event is fired. Note the callback is limited to 8 arguments.

    • Optionalscope: object = ...

      Object to use as 'this' when the event is fired, defaults to current this.

    Returns EventHandle

    • can be used for removing event in the future.
    obj.once('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console
    obj.fire('test', 1, 2); // not going to get handled

Events

EVENT_HITTESTADD: string = 'hittest:add'

Fired when new XrHitTestSource is added to the input source. The handler is passed the XrHitTestSource object that has been added.

inputSource.on('hittest:add', (hitTestSource) => {
// new hit test source is added
});
EVENT_HITTESTREMOVE: string = 'hittest:remove'

Fired when XrHitTestSource is removed to the the input source. The handler is passed the XrHitTestSource object that has been removed.

inputSource.on('remove', (hitTestSource) => {
// hit test source is removed
});
EVENT_HITTESTRESULT: string = 'hittest:result'

Fired when hit test source receives new results. It provides transform information that tries to match real world picked geometry. The handler is passed the XrHitTestSource object that produced the hit result, the Vec3 position, the Quat rotation and the XRHitTestResult object that is created by the WebXR API.

inputSource.on('hittest:result', (hitTestSource, position, rotation, hitTestResult) => {
target.setPosition(position);
target.setRotation(rotation);
});
EVENT_REMOVE: string = 'remove'

Fired when XrInputSource is removed.

inputSource.once('remove', () => {
// input source is not available anymore
});
EVENT_SELECT: string = 'select'

Fired when input source has triggered primary action. This could be pressing a trigger button, or touching a screen. The handler is passed an XRInputSourceEvent object from the WebXR API.

const ray = new pc.Ray();
inputSource.on('select', (evt) => {
ray.set(inputSource.getOrigin(), inputSource.getDirection());
if (obj.intersectsRay(ray)) {
// selected an object with input source
}
});
EVENT_SELECTEND: string = 'selectend'

Fired when input source has ended triggering primary action. The handler is passed an XRInputSourceEvent object from the WebXR API.

inputSource.on('selectend', (evt) => {
console.log('Select ended');
});
EVENT_SELECTSTART: string = 'selectstart'

Fired when input source has started to trigger primary action. The handler is passed an XRInputSourceEvent object from the WebXR API.

inputSource.on('selectstart', (evt) => {
console.log('Select started');
});
EVENT_SQUEEZE: string = 'squeeze'

Fired when input source has triggered squeeze action. This is associated with "grabbing" action on the controllers. The handler is passed an XRInputSourceEvent object from the WebXR API.

inputSource.on('squeeze', (evt) => {
console.log('Squeeze');
});
EVENT_SQUEEZEEND: string = 'squeezeend'

Fired when input source has ended triggering squeeze action. The handler is passed an XRInputSourceEvent object from the WebXR API.

inputSource.on('squeezeend', (evt) => {
console.log('Squeeze ended');
});
EVENT_SQUEEZESTART: string = 'squeezestart'

Fired when input source has started to trigger squeeze action. The handler is passed an XRInputSourceEvent object from the WebXR API.

inputSource.on('squeezestart', (evt) => {
if (obj.containsPoint(inputSource.getPosition())) {
// grabbed an object
}
});