Thursday, 12 October, 2017 UTC

Summary

Introduction

When we were developing WebVR applications such as a-painter, a-blast, and a-saturday-night that were designed to work across different hardware, we realized the importance of having a convenient way to map inputs to application-specific actions. It used to be that Vive controllers were the only hand input available so our logic was hard wired to that. As Oculus, Google, and Microsoft started releasing their platforms, we needed a scalable and convenient solution to support multiple input methods. Inspired by the Steam Controller API (This talk is a great introduction), I developed a more convenient way to map inputs to application-specific actions.

Today, A-Frame supports Vive, Oculus Touch, Microsoft Mixed Reality, GearVR, and Daydream controllers. You don’t have to deal with controller IDs or identify which indices correspond to which button. We still need to manually map particular actions to each of the controller inputs. Here’s an example from a-painter showing how tedious and error prone that can be:

We have to listen for the controllerconnected event and then add a listener depending on the button we want to use for a specific action. More info about interactions & controllers can be found on the A-Frame.io docs.

Introducing the input-mapping system

Components should be able to communicate with each other using high level action events instead of specific hardware events from the controllers like triggerdown, trackpadchanged, or gripup.
For example, the “paint-controls” component on a-painter listens for a triggerdown event to start painting. Wouldn’t it make more sense to listen for a paint event instead that is agnostic from the input method?
For the paint action from a-painter makes sense to use the trigger button that is available in all controllers. It is a rare scenario where all controllers have the same button available and remapping action can get complicated.

This will help simplify our code a lot and make it more clean and readable. For example compare the old version of the code to toggle the menu in a-painter:

As you may guess if we want to add a new controller we just need to modify the mapping without touching the actual menu code.

Mapping format

You can organize mappings in groups, ideally representing different states of our application. An experience could have a playing state where the trigger is used to grab things and another state called menu where the same button should be used to select an item from the settings menu.
In the following example we have two groups: default (As you may guess is the one active by default) and painting.
Each group contains a list of controllers and the reserved keywords common (to define common mappings for all the controllers) and keyboard (for mappings events on key down, up and press).

Controllers: Each controller has a mapping between button events and actions that our application understands. For example, on vive-controls we could map trackpaddown to teleport and gripdown to undo.

common: Some mappings between buttons and actions might be common across all controllers. For example, triggerdown is emitted by vive, oculus and microsoft controllers, so we could include just one map from triggerdown to grab instead of duplicating it on each controller’s section.

keyboard: Mapping between keyboard keys and actions. The event names should start with the key value and the suffix _up, _down and _press. For example we could map s_up to save.

Activate a mapping group

To activate one group of mappings we just need to set the AFRAME.currentMapping variable:

AFRAME.currentMapping = ‘erasing';

Updating your demos and components

I recommend anyone to start using mappings and stop registering listeners for each combination of buttons and controllers.
For example, I’ve modified (PR #30) aframe-teleport-controls to listen to events instead of buttons for aim and teleport, with this change I don’t need to update the code if new webvr compatible controller appears. I’ll just need to include a new mapping on my app (or just leave it in common if the mapping is similar to an already existing controller).
If you want to see a migration use case using the component please take a look at the changes in A-Painter (PR #227) and A-Blast (PR #128).

Perhaps we could…

Some ideas and features:

Create a component that opens a configuration panel to show and let the user modify the mappings, storing them locally using LocalStorage.

Include a common state that contains all the mapping that are common for all the states.

Use the mapping data to auto generate tooltips for the controllers in our application.

Final words

Currently this is maintained as a third-party component at https://github.com/fernandojsg/aframe-input-mapping-component It will likely ship with A-Frame by default in the 0.8.0 release.
Please give it a try with your current project and send me any feedback. I hope this system will help you create more responsive VR experiences and components.