Skip to main content

XRInput

The XRInput class provides methods to capture and manage XR (Extended Reality) input from devices like VR controllers.

start()

Initializes the XR input system and begins listening for XR-related input events.

Example:

Input.xr.start();

stop()

Stops the XR input system and removes all event listeners related to XR input.

Example:

Input.xr.stop();

count(): number

Returns the number of active XR input sources currently being tracked.

Example:

const numControllers = Input.xr.count();
console.log(numControllers);

controller(index: number): THREE.Group | null

Returns a Group representing the target ray space of the XR controller.

Example:

const leftController = Input.xr.controller(0);

controllerGrip(index: number): THREE.Group | null

Returns a Group representing the grip space of the XR controller.

Example:

const grip = Input.xr.controllerGrip(0);

axes(index: number): number[]

Returns an array representing the axes present on the XR device (e.g., analog thumb sticks).

Example:

const axesValues = Input.xr.axes(0);

isButtonPressed(index: number, buttonIndex: number): boolean

Checks if a specific button on the XR controller is currently pressed.

Example:

if (Input.xr.isButtonPressed(0, 1)) {
console.log('Button pressed');
}

isButtonReleased(index: number, buttonIndex: number): boolean

Returns true only once if the button was previously pressed and then released.

Example:

if (Input.xr.isButtonReleased(0, 1)) {
console.log('Button released');
}

isButtonPressedFor(index: number, buttonIndex: number, durationInSeconds: number): boolean

Checks if a specific button has been pressed for a specified duration.

Example:

if (Input.xr.isButtonPressedFor(0, 1, 2)) {
console.log('Button pressed for 2 seconds');
}

position(index?: number): THREE.Vector3 | null

Returns the position of the XR controller in 3D space.

Example:

const pos = Input.xr.position(0);

orientation(index?: number): THREE.Quaternion | null

Returns the rotation of the XR controller in 3D space.

Example:

const rot = Input.xr.orientation(0);

raycast(index?: number): RaycastOutput

Computes the raycaster and cursor position for XR controllers.

Example:

const ray = Input.xr.raycast(0);

handedness(index?: number): string

Returns the handedness of the XR controller (e.g., 'left' or 'right').

Example:

console.log(Input.xr.handedness(0));

haptics(index: number, value?: number, duration?: number): void

Applies haptic feedback to the XR controller.

Example:

Input.xr.haptics(0, 1, 200);