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)

Properties

_dirtyLocal: boolean = true
_dirtyRay: boolean = false
_elementEntity: Entity = null
_elementInput: boolean = true
_grip: boolean = false
_hand: XrHand = null
_hitTestSources: XrHitTestSource[] = []
_id: number
_linearVelocity: Vec3 = null
_localPosition: Vec3 = null
_localPositionLast: Vec3 = null
_localRotation: Quat = null
_localTransform: Mat4 = null
_manager: XrManager
_position: Vec3 = ...
_ray: Ray = ...
_rayLocal: Ray = ...
_rotation: Quat = ...
_selecting: boolean = false
_squeezing: boolean = false
_velocitiesAvailable: boolean = false
_velocitiesTimestamp: number = ...
_worldTransform: Mat4 = null
_xrInputSource: XRInputSource

Accessors

  • get grip(): boolean
  • If input source can be held, then it will have node with its world transformation, that can be used to position and rotate visual object based on it.

    Returns boolean

  • get handedness(): string
  • Describes which hand input source is associated with. Can be one of the following:

    • XRHAND_NONE: None - input source is not meant to be held in hands.
    • XRHAND_LEFT: Left - indicates that input source is meant to be held in left hand.
    • XRHAND_RIGHT: Right - indicates that input source is meant to be held in right hand.

    Returns string

  • 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.

    • Optional arg1: any

      First argument that is passed to the event handler.

    • Optional arg2: any

      Second argument that is passed to the event handler.

    • Optional arg3: any

      Third argument that is passed to the event handler.

    • Optional arg4: any

      Fourth argument that is passed to the event handler.

    • Optional arg5: any

      Fifth argument that is passed to the event handler.

    • Optional arg6: any

      Sixth argument that is passed to the event handler.

    • Optional arg7: any

      Seventh argument that is passed to the event handler.

    • Optional arg8: any

      Eighth argument that is passed to the event handler.

    Returns EventHandler

    Self for chaining.

    Example

    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.

    Example

    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

    • Optional options: {
          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

    Example

    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

    • Optional name: string

      Name of the event to unbind.

    • Optional callback: HandleEventCallback

      Function to be unbound.

    • Optional scope: object

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

    Returns EventHandler

    Self for chaining.

    Example

    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.

    • Optional scope: 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.

    Example

    obj.on('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console

    Example

    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.

    • Optional scope: 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.

    Example

    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.

Example

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.

Example

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.

Example

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

Fired when XrInputSource is removed.

Example

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.

Example

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.

Example

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.

Example

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.

Example

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.

Example

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.

Example

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