API Reference

Class List

XrInputSource

Extends: EventHandler

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.

Summary

Properties

elementEntity

If XrInputSource#elementInput is true, this property will hold entity with Element component at which this input source is hovering, or null if not hovering over any element.

elementInput

Set to true to allow input source to interact with Element components.

gamepad

If input source has buttons, triggers, thumbstick or touchpad, then this object provides access to its states.

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 joysticks based on it.

hand

If input source is a tracked hand, then it will point to XrHand otherwise it is null.

handedness

Describes which hand input source is associated with.

hitTestSources

List of active XrHitTestSource instances created by this input source.

id

Unique number associated with instance of input source.

inputSource

XRInputSource object that is associated with this input source.

profiles

List of input profile names indicating both the preferred visual representation and behavior of the input source.

selecting

True if input source is in active primary action between selectstart and selectend events.

squeezing

True if input source is in active squeeze action between squeezestart and squeezeend events.

targetRayMode

Type of ray Input Device is based on.

Methods

getDirection

Get the world space direction of input source ray.

getLocalPosition

Get the local space position of input source if it is handheld (XrInputSource#grip is true).

getLocalRotation

Get the local space rotation of input source if it is handheld (XrInputSource#grip is true).

getOrigin

Get the world space origin of input source ray.

getPosition

Get the world space position of input source if it is handheld (XrInputSource#grip is true).

getRotation

Get the world space rotation of input source if it is handheld (XrInputSource#grip is true).

hitTestStart

Attempts to start hit test source based on this input source.

Events

remove

Fired when XrInputSource is removed.

select

Fired when input source has triggered primary action.

selectend

Fired when input source has ended triggering primary action.

selectstart

Fired when input source has started to trigger primary action.

squeeze

Fired when input source has triggered squeeze action.

squeezeend

Fired when input source has ended triggering squeeze action.

squeezestart

Fired when input source has started to trigger squeeze action.

hittest:add

Fired when new XrHitTestSource is added to the input source.

hittest:remove

Fired when XrHitTestSource is removed to the the input source.

hittest:result

Fired when hit test source receives new results.

Inherited

Methods

fire

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

hasEvent

Test if there are any handlers bound to an event name.

off

Detach an event handler from an event.

on

Attach an event handler to an event.

once

Attach an event handler to an event.

Details

Properties

Entity, nullelementEntity

If XrInputSource#elementInput is true, this property will hold entity with Element component at which this input source is hovering, or null if not hovering over any element.

booleanelementInput

Set to true to allow input source to interact with Element components. Defaults to true.

Gamepad, nullgamepad

If input source has buttons, triggers, thumbstick or touchpad, then this object provides access to its states.

booleangrip

If input source can be held, then it will have node with its world transformation, that can be used to position and rotate virtual joysticks based on it.

XrHand, nullhand

If input source is a tracked hand, then it will point to XrHand otherwise it is null.

stringhandedness

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.

XrHitTestSource[]hitTestSources

List of active XrHitTestSource instances created by this input source.

numberid

Unique number associated with instance of input source. Same physical devices when reconnected will not share this ID.

objectinputSource

XRInputSource object that is associated with this input source.

string[]profiles

List of input profile names indicating both the preferred visual representation and behavior of the input source.

booleanselecting

True if input source is in active primary action between selectstart and selectend events.

booleansqueezing

True if input source is in active squeeze action between squeezestart and squeezeend events.

stringtargetRayMode

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.

Methods

getDirection()

Get the world space direction of input source ray.

Returns

Vec3

The world space direction of input source ray.

getLocalPosition()

Get the local space position of input source if it is handheld (XrInputSource#grip is true). Local space is relative to parent of the XR camera. Otherwise it will return null.

Returns

Vec3, null

The world space position of handheld input source.

getLocalRotation()

Get the local space rotation of input source if it is handheld (XrInputSource#grip is true). Local space is relative to parent of the XR camera. Otherwise it will return null.

Returns

Vec3, null

The world space rotation of handheld input source.

getOrigin()

Get the world space origin of input source ray.

Returns

Vec3

The world space origin of input source ray.

getPosition()

Get the world space position of input source if it is handheld (XrInputSource#grip is true). Otherwise it will return null.

Returns

Vec3, null

The world space position of handheld input source.

getRotation()

Get the world space rotation of input source if it is handheld (XrInputSource#grip is true). Otherwise it will return null.

Returns

Quat, null

The world space rotation of handheld input source.

hitTestStart([options])

Attempts to start hit test source based on this input source.

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
            });
        }
    });
});

Parameters

optionsobject

Object for passing optional arguments.

options.entityTypesstring[]

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.
options.offsetRayRay

Optional ray by which hit test ray can be offset.

options.callbackXrHitTestStartCallback

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

Events

remove

Fired when XrInputSource is removed.

inputSource.once('remove', function () {
    // input source is not available anymore
});

select

Fired when input source has triggered primary action. This could be pressing a trigger button, or touching a screen.

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

Parameters

evtobject

XRInputSourceEvent event data from WebXR API.

selectend

Fired when input source has ended triggering primary action.

Parameters

evtobject

XRInputSourceEvent event data from WebXR API.

selectstart

Fired when input source has started to trigger primary action.

Parameters

evtobject

XRInputSourceEvent event data from WebXR API.

squeeze

Fired when input source has triggered squeeze action. This is associated with "grabbing" action on the controllers.

Parameters

evtobject

XRInputSourceEvent event data from WebXR API.

squeezeend

Fired when input source has ended triggering squeeze action.

Parameters

evtobject

XRInputSourceEvent event data from WebXR API.

squeezestart

Fired when input source has started to trigger squeeze action.

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

Parameters

evtobject

XRInputSourceEvent event data from WebXR API.

hittest:add

Fired when new XrHitTestSource is added to the input source.

inputSource.on('hittest:add', function (hitTestSource) {
    // new hit test source is added
});

Parameters

hitTestSourceXrHitTestSource

Hit test source that has been added.

hittest:remove

Fired when XrHitTestSource is removed to the the input source.

inputSource.on('remove', function (hitTestSource) {
    // hit test source is removed
});

Parameters

hitTestSourceXrHitTestSource

Hit test source that has been removed.

hittest:result

Fired when hit test source receives new results. It provides transform information that tries to match real world picked geometry.

inputSource.on('hittest:result', function (hitTestSource, position, rotation) {
    target.setPosition(position);
    target.setRotation(rotation);
});

Parameters

hitTestSourceXrHitTestSource

Hit test source that produced the hit result.

positionVec3

Position of hit test.

rotationQuat

Rotation of hit test.

Inherited

Methods

fire(name, [arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7], [arg8])

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

obj.fire('test', 'This is the message');

Parameters

namestring

Name of event to fire.

arg1*

First argument that is passed to the event handler.

arg2*

Second argument that is passed to the event handler.

arg3*

Third argument that is passed to the event handler.

arg4*

Fourth argument that is passed to the event handler.

arg5*

Fifth argument that is passed to the event handler.

arg6*

Sixth argument that is passed to the event handler.

arg7*

Seventh argument that is passed to the event handler.

arg8*

Eighth argument that is passed to the event handler.

Returns

EventHandler

Self for chaining.

hasEvent(name)

Test if there are any handlers bound to an event name.

obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false

Parameters

namestring

The name of the event to test.

Returns

boolean

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

off([name], [callback], [scope])

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.

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

Parameters

namestring

Name of the event to unbind.

callbackHandleEventCallback

Function to be unbound.

scopeobject

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

Returns

EventHandler

Self for chaining.

on(name, callback, [scope])

Attach an event handler to an event.

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();

Parameters

namestring

Name of the event to bind the callback to.

callbackHandleEventCallback

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

scopeobject

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.

once(name, callback, [scope])

Attach an event handler to an event. This handler will be removed after being fired once.

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

Parameters

namestring

Name of the event to bind the callback to.

callbackHandleEventCallback

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

scopeobject

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.