Three.js WebXR Controllers (Input Profiles & Haptics)
XR controller input in Three.js with procedural models whose trigger, grip, thumbstick and face buttons move with the xr-standard gamepad mapping, haptic pulses on trigger press, and input from real XR controllers, any connected gamepad, the keyboard or an auto demo.
Key topics: webxr controllers, xr gamepad, haptics, input profiles, xr-standard mapping, vr controller
Game Over
What this code does
- Spaces: getControllerGrip(i) holds the model in the hand pose, and getController(i) is the pointing (target-ray) space used for the laser.
- xr-standard Mapping: buttons[0] trigger, [1] squeeze, [3] thumbstick press, [4]/[5] A/X and B/Y, with axes[2,3] as the thumbstick.
- Input Profiles: inputSource.profiles (e.g. oculus-touch-v3 → generic-trigger-squeeze-thumbstick) select models and button layouts.
- Haptics: hapticActuators[0].pulse(intensity, ms) in WebXR, or vibrationActuator.playEffect('dual-rumble') for desktop gamepads, fired on trigger press edges.
- Fallbacks: with no XR session, navigator.getGamepads() is used, then keyboard (F/Space trigger, Shift squeeze, WASD/arrows stick), then the auto demo.
- Controls: haptic strength, simulated profile and auto demo.
Related Demos
Continue Learning
JavaScript Implementation
// XR controllers: grip space for the model, target-ray space for pointing, gamepad for state
const grip = renderer.xr.getControllerGrip(0); scene.add(grip)
grip.addEventListener('connected', (e) => { inputSource = e.data; grip.add(controllerModel) })
function frame () {
const gp = inputSource.gamepad // xr-standard mapping
trigger.rotation.x = gp.buttons[0].value * 0.5
stick.position.x = gp.axes[2] * 0.004; stick.position.z = gp.axes[3] * 0.004
if (gp.buttons[0].pressed && !wasPressed) gp.hapticActuators?.[0]?.pulse(0.6, 60) // rumble
}
// Desktop: navigator.getGamepads()[0].vibrationActuator.playEffect('dual-rumble', { duration: 60, strongMagnitude: 0.6 })