API Reference

Class List

pc.XrInputSource

Extends: pc.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 pc.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 joystics based on it.

handedness

Describes which hand input source is associated with.

hitTestSources

list of active pc.XrHitTestSource 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 prefered visual representation and behavior of the input source.

selecting

True if input source is in active primary action between selectstart and selectend 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 (pc.XrInputSource#grip is true).

getLocalRotation

Get the local space rotation of input source if it is handheld (pc.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 (pc.XrInputSource#grip is true).

getRotation

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

hitTestStart

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

Events

remove

Fired when pc.XrInputSource is removed.

select

Fired when input source has triggered primary action.

selectend

Fired when input source has ended triggerring primary action.

selectstart

Fired when input source has started to trigger primary action.

hittest:add

Fired when new pc.XrHitTestSource is added to the input source.

hittest:remove

Fired when pc.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

Constructor

XrInputSource(manager, xrInputSource)

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.

Parameters

managerpc.XrManager

WebXR Manager.

xrInputSourceobject

XRInputSource object that is created by WebXR API.

Properties

pc.EntityelementEntity

If pc.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 joystics based on it.

stringhandedness

Describes which hand input source is associated with. Can be one of the following:

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

pc.XrHitTestSource[]hitTestSources

list of active pc.XrHitTestSource 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 prefered visual representation and behavior of the input source.

booleanselecting

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

stringtargetRayMode

Type of ray Input Device is based on. Can be one of the following:

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

pc.Vec3

The world space direction of input source ray.

getLocalPosition()

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

Returns

pc.Vec3, null

The world space position of handheld input source.

getLocalRotation()

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

Returns

pc.Vec3, null

The world space rotation of handheld input source.

getOrigin()

Get the world space origin of input source ray.

Returns

pc.Vec3

The world space origin of input source ray.

getPosition()

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

Returns

pc.Vec3, null

The world space position of handheld input source.

getRotation()

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

Returns

pc.Vec3, 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 [ {pc.XRTRACKABLE_PLANE} ]. Can be any combination of the following:

  • 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.
  • pc.XRTRACKABLE_PLANE: Plane - indicates that the hit test results will be computed based on the planes detected by the underlying Augmented Reality system.
  • pc.XRTRACKABLE_MESH: Mesh - indicates that the hit test results will be computed based on the meshes detected by the underlying Augmented Reality system.
options.offsetRaypc.Ray

Optional ray by which hit test ray can be offset.

options.callbackpc.callbacks.XrHitTestStart

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

Events

remove

Fired when pc.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.

var 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 triggerring 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

hittest:add

Fired when new pc.XrHitTestSource is added to the input source.

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

Parameters

hitTestSourcepc.XrHitTestSource

Hit test source that has been added

hittest:remove

Fired when pc.XrHitTestSource is removed to the the input source.

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

Parameters

hitTestSourcepc.XrHitTestSource

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

hitTestSourcepc.XrHitTestSource

Hit test source that produced the hit result

positionpc.Vec3

Position of hit test

rotationpc.Quat

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

nameobject

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

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

var 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 hander functions, called 'test' with scope this

Parameters

namestring

Name of the event to unbind.

callbackpc.callbacks.HandleEvent

Function to be unbound.

scopeobject

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

Returns

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

Parameters

namestring

Name of the event to bind the callback to.

callbackpc.callbacks.HandleEvent

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

pc.EventHandler

Self for chaining.

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.

callbackpc.callbacks.HandleEvent

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

pc.EventHandler

Self for chaining.