xr_action_map.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. .. _doc_xr_action_map:
  2. The XR action map
  3. =================
  4. Godot has an action map feature as part of the XR system.
  5. At this point in time this system is part of the OpenXR module.
  6. There are plans to encompass WebXR into this in the near future hence we call it the XR action map system in this document.
  7. It implements the built-in action map system of OpenXR mostly exactly as it is offered.
  8. The XR action map system exposes input, positional data and output for XR controllers to your game/application.
  9. It does this by exposing named actions that can be tailored to your game/application and binding these to the actual inputs and outputs on your XR devices.
  10. As the XR action map is currently part of the OpenXR module, OpenXR needs to be enabled in your project settings to expose it:
  11. .. image:: img/openxr_settings.png
  12. You will then find the XR Action Map interface in the bottom of the screen:
  13. .. image:: img/xr_action_map.webp
  14. .. note::
  15. Godot's built-in input system has many things in common with the XR action map system.
  16. In fact our original idea was to add functionality to the existing input system and expose the data to the OpenXR action map system.
  17. We may revisit that idea at some point but as it turns out there were just too many problems to overcome.
  18. To name a few:
  19. * Godot's input system mainly centers around button inputs, XR adds triggers, axis, poses and haptics (output) into the mix.
  20. This would greatly complicate the input system with features that won't work for normal controllers or contrast with the current approach.
  21. It was felt this would lead to confusion for the majority of Godot users.
  22. * Godot's input system works with raw input data that is parsed and triggers emitting actions.
  23. This input data is made available to the end user.
  24. OpenXR completely hides raw data and does all the parsing for us, we only get access to already parsed action data.
  25. This inconsistency is likely to lead to bugs when an unsuspecting user tries to use an XR device as a normal input device.
  26. * Godot's input system allows changes to what inputs are bound to actions in runtime, OpenXR does not.
  27. * Godot's input system is based on device ids which are meaningless in OpenXR.
  28. This does mean that a game/application that mixes traditional inputs with XR controllers will have a separation.
  29. For most applications either one or the other is used and this is not seen as a problem.
  30. In the end, it's a limitation of the system.
  31. The default action map
  32. ----------------------
  33. Godot will automatically create a default action map if no action map file is found.
  34. .. warning::
  35. This default map was designed to help developers port their XR games/applications from Godot 3 to Godot 4.
  36. As a result this map essentially binds all known inputs on all controllers supported by default, to actions one on one.
  37. This is not a good example of setting up an action map.
  38. It does allow a new developer to have a starting point when they want to become familiar with Godot XR.
  39. It prevents having to design a proper action map for their game/application first.
  40. For this walkthrough we're going to start with a blank action map.
  41. You can simply delete the "Godot action set" entry at the top by pressing the trash can icon.
  42. This will clear out all actions.
  43. You might also want to remove the controllers that you do not wish to setup, more on this later.
  44. Action sets
  45. -----------
  46. .. note::
  47. Before we dive in, you will see the term XR runtime used throughout this document.
  48. With XR runtime we mean the software that is controlling and interacting with the AR or VR headset.
  49. The XR runtime then exposes this to us through an API such as OpenXR.
  50. So:
  51. * for Steam this is SteamVR,
  52. * for Meta on desktop this is the Oculus Client (including when using Quest link),
  53. * for Meta on Quest this is the Quest's native OpenXR client,
  54. * on Linux this could be Monado, etc.
  55. The action map allows us to organize our actions in sets.
  56. Each set can be enabled or disabled on its own.
  57. The concept here is that you could have different sets that provide bindings in different scenarios.
  58. You could have:
  59. * a ``Character control`` set for when you're walking around,
  60. * a ``Vehicle control`` set for when you're operating a vehicle,
  61. * a ``Menu`` set for when a menu is open.
  62. Only the action set applicable to the current state of your game/application can then be enabled.
  63. This is especially important if you wish to bind the same input on a controller to a different action.
  64. For instance:
  65. * in your ``Character control`` set you may have an action ``Jump``,
  66. * in your ``Vehicle control`` set you may have an action ``Accelerate``,
  67. * in your ``Menu`` set you may have an action ``Select``.
  68. All are bound to the trigger on your controller.
  69. OpenXR will only bind an input or output to a single action.
  70. If the same input or output is bound to multiple actions the one in the active action set with the highest priority will be the one updated/used.
  71. So in our above example it will thus be important that only one action set is active.
  72. For your first XR game/application we highly recommend starting with just a single action set and to not over-engineer things.
  73. For our walkthrough in this document we will thus create a single action set called ``my_first_action_set``.
  74. We do this by pressing the ``Add action set`` button:
  75. .. image:: img/xr_my_first_action_set.webp
  76. The columns in our table are as follows:
  77. .. list-table::
  78. :class: wrap-normal
  79. :width: 100%
  80. :widths: 7 23 70
  81. :header-rows: 1
  82. * - Col
  83. - Value
  84. - Description
  85. * - 1
  86. - my_first_action_set
  87. - This is the internal name of the action set.
  88. OpenXR doesn't specify specific restrictions on this name other then size, however some XR runtimes will not like spaces or special characters.
  89. * - 2
  90. - My first action set
  91. - This is a human-readable name for the action set.
  92. Some XR runtimes will display this name to the end user, for example in configuration dialogs.
  93. * - 3
  94. - 0
  95. - This is the priority of the action set.
  96. If multiple active action sets have actions bound to the same controllers inputs or outputs, the action set with the highest priority value will determine the action that is updated.
  97. Actions
  98. -------
  99. In the XR action map, actions are the entities that your game/application will interact with.
  100. For instance, we can define an action ``Shoot`` and the input bound to that action will trigger the ``button_pressed`` signal on the relevant :ref:`XRController3D <class_xrcontroller3d>` node in your scene with ``Shoot`` as the ``name`` parameter of the signal.
  101. You can also poll the current state of an action.
  102. :ref:`XRController3D <class_xrcontroller3d>` for instance has an ``is_button_pressed`` method.
  103. Actions can be used for both input and output and each action has a type that defines its behavior.
  104. * The ``Bool`` type is used for discrete input like buttons.
  105. * The ``Float`` type is used for analogue input like triggers.
  106. These two are special as they are the only ones that are interchangeable.
  107. OpenXR will handle conversions between ``Bool`` and ``Float`` inputs and actions.
  108. You can get the value of a ``Float`` type action by calling the method ``get_float`` on your :ref:`XRController3D <class_xrcontroller3d>` node.
  109. It emits the ``input_float_changed`` signal when changed.
  110. .. note::
  111. Where analogue inputs are queried as buttons a threshold is applied.
  112. This threshold is currently managed exclusively by the XR runtime.
  113. There are plans to extend Godot to provide some level of control over these thresholds in the future.
  114. The ``Vector2`` type defines the input as an axis input.
  115. Touchpads, thumbsticks and similar inputs are exposed as vectors.
  116. You can get the value of a ``Vector2`` type action by calling the method ``get_vector2`` on your :ref:`XRController3D <class_xrcontroller3d>` node.
  117. It emits the ``input_vector2_changed`` signal when changed.
  118. The ``Pose`` type defines a spatially tracked input.
  119. Multiple "pose" inputs are available in OpenXR: ``aim``, ``grip`` and ``palm``.
  120. Your :ref:`XRController3D <class_xrcontroller3d>` node is automatically positioned based on the pose action assigned to ``pose`` property of this node.
  121. More about poses later.
  122. .. note::
  123. The OpenXR implementation in Godot also exposes a special pose called ``Skeleton``.
  124. This is part of the hand tracking implementation.
  125. This pose is exposed through the ``skeleton`` action that is supported outside of the action map system.
  126. It is thus always present if hand tracking is supported.
  127. You don't need to bind actions to this pose to use it.
  128. Finally, the only output type is ``Haptic`` and it allows us to set the intensity of haptic feedback, such as controller vibration.
  129. Controllers can have multiple haptic outputs and support for haptic vests is coming to OpenXR.
  130. So lets add an action for our aim pose, we do this by clicking on the ``+`` button for our action set:
  131. .. image:: img/xr_aim_pose.webp
  132. The columns in our table are as follows:
  133. .. list-table::
  134. :class: wrap-normal
  135. :width: 100%
  136. :widths: 7 23 70
  137. :header-rows: 1
  138. * - Col
  139. - Value
  140. - Description
  141. * - 1
  142. - aim_pose
  143. - This is the internal name of the action.
  144. OpenXR doesn't specify specific restrictions on this name other then size, however some XR runtimes will not like spaces or special characters.
  145. * - 2
  146. - Aim pose
  147. - This is a human-readable name for the action.
  148. Some XR runtimes will display this name to the end user, for example in configuration dialogs.
  149. * - 3
  150. - Pose
  151. - The type of this action.
  152. OpenXR defines a number of bindable input poses that are commonly available for controllers.
  153. There are no rules for which poses are supported for different controllers.
  154. The poses OpenXR currently defines are:
  155. * The aim pose on most controllers is positioned slightly infront of the controller and aims forward.
  156. This is a great pose to use for laser pointers or to align the muzzle of a weapon with.
  157. * The grip pose on most controllers is positioned where the grip button is placed on the controller.
  158. The orientation of this pose differs between controllers and can differ for the same controller on different XR runtimes.
  159. * The palm pose on most controllers is positioned in the center of the palm of the hand holding the controller.
  160. This is a new pose that is not available on all XR runtimes.
  161. .. note::
  162. If hand tracking is used, there are currently big differences in implementations between the different XR runtimes.
  163. As a result the action map is currently not suitable for hand tracking. Work is being done on this so stay tuned.
  164. Let's complete our list of actions for a very simple shooting game/application:
  165. .. image:: img/xr_all_actions.webp
  166. The actions we have added are:
  167. * movement, which allows the user to move around outside of normal room scale tracking.
  168. * grab, which detects that the user wants to hold something.
  169. * shoot, which detects that the user wants to fire the weapon they are holding.
  170. * haptic, which allows us to output haptic feedback.
  171. Now note that we don't distinguish between the left and right hand.
  172. This is something that is determined at the next stage.
  173. We've implemented the action system in such a way that you can bind the same action to both hands.
  174. The appropriate :ref:`XRController3D <class_xrcontroller3d>` node will emit the signal.
  175. .. warning::
  176. For both grab and shoot we've used the ``Bool`` type.
  177. As mentioned before, OpenXR does automatic conversions from an analogue controls however not all XR Runtimes currently apply sensible thresholds.
  178. We recommend as a workaround to use the ``Float`` type when interacting with triggers and grip buttons and apply your own threshold.
  179. For buttons like A/B/X/Y and similar where there is no analogue option, the ``Bool`` type works fine.
  180. .. note::
  181. You can bind the same action to multiple inputs for the same controller on the same profile.
  182. In this case the XR runtime will attempt to combine the inputs.
  183. * For ``Bool`` inputs, this will perform an ``OR`` operation between the buttons.
  184. * For ``Float`` inputs, this will take the highest value of the bound inputs.
  185. * The behavior for ``Pose`` inputs is undefined, but the first bound input is likely to be used.
  186. You shouldn't bind multiple actions of the same action set to the same controller input.
  187. If you do this, or if actions are bound from multiple action sets but they have overlapping priorities, the behavior is undefined.
  188. The XR runtime may simply not accept your action map, or it may take this on a first come first serve basis.
  189. We are still investigating the restrictions around binding multiple actions to the same output as this scenario makes sense.
  190. The OpenXR specification seems to not allow this.
  191. Now that we have our basic actions defined, it's time to hook them up.
  192. Profiles
  193. --------
  194. In OpenXR controller bindings are captured in so-called "Interaction Profiles".
  195. We've simply shortened it to "Profiles" because it takes up less space.
  196. This generic name is chosen because controllers don't cover the entire system.
  197. Currently there are also profiles for trackers, remotes and tracked pens.
  198. There are also provisions for devices such as treadmills, haptic vests and such even though those are not part of the specification yet.
  199. .. warning::
  200. It is important to know that OpenXR has strict checking on supported devices.
  201. The core specification identifies a number of controllers and similar devices with their supported inputs and outputs.
  202. Every XR runtime must accept these interaction profiles even if they aren't applicable.
  203. New devices are added through extensions and XR runtimes must specify which ones they support.
  204. XR runtimes that do not support a device added through extensions will not accept these profiles.
  205. XR runtimes that do not support added input or output types will often crash if supplied.
  206. As such Godot keeps meta data of all available devices, their inputs and outputs and which extension adds support for them.
  207. You can create interaction profiles for all devices you wish to support.
  208. Godot will filter out those not supported by the XR runtime the user is using.
  209. This does mean that in order to support new devices, you might need to update to a more recent version of Godot.
  210. It is however also important to note that the action map has been designed with this in mind.
  211. When new devices enter the market, or when your users use devices that you do not have access to, the action map system relies on the XR runtime.
  212. It is the XR runtime's job to choose the best fitting interaction profile that has been specified and adapt it for the controller the user is using.
  213. How the XR runtime does this is left to the implementation of the runtime and there are thus vast differences between the runtimes.
  214. Some runtimes might even permit users to edit the bindings themselves.
  215. A common approach for a runtime is to look for a matching interaction profile first.
  216. If this is not found it will check the most common profiles such as that of the "Touch controller" and do a conversion.
  217. If all else fails, it will check the generic :ref:`"Simple controller" <doc_xr_action_map_simple>`.
  218. .. note::
  219. There is an important conclusion to be made here:
  220. When a controller is found, and the action map is applied to it, the XR runtime is not limited to the exact configurations you set up in Godot's action map editor.
  221. While the runtime will generally choose a suitable mapping based on one of the bindings you set up in the action map, it can deviate from it.
  222. For example, when the Touch controller profile is used any of the following scenarios could be true:
  223. * we could be using a Quest 1 controller,
  224. * we could be using a Quest 2 controller,
  225. * we could be using a Quest Pro controller but no Quest Pro profile was given or the XR runtime being used does not support the Quest Pro controller,
  226. * it could be a completely different controller for which no profile was given but the XR runtime is using the touch bindings as a base.
  227. Ergo, there currently is no way to know with certainty, which controller the user is actually using.
  228. .. warning::
  229. Finally, and this trips up a lot of people, the bindings aren't set in stone.
  230. It is fully allowed, and even expected, that an XR runtime allows a user to customise the bindings.
  231. At the moment none of the XR runtimes offer this functionality though SteamVR has an existing UI from OpenVRs action map system that is still accessible.
  232. This is actively being worked on however.
  233. Our first controller binding
  234. ----------------------------
  235. Let's set up our first controller binding, using the Touch controller as an example.
  236. Press "Add profile", find the Touch controller, and add it.
  237. If it is not in the list, then it may already have been added.
  238. .. image:: img/xr_add_touch_controller.webp
  239. Our UI now shows panels for both the left and right controllers.
  240. The panels contain all of the possible inputs and outputs for each controller.
  241. We can use the ``+`` next to each entry to bind it to an action:
  242. .. image:: img/xr_select_action.webp
  243. Let's finish our configuration:
  244. .. image:: img/xr_touch_completed.webp
  245. Each action is bound the given input or output for both controllers to indicate that we support the action on either controller.
  246. The exception is the movement action which is bound only to the right hand controller.
  247. It is likely that we would want to use the left hand thumbstick for a different purpose, say a teleport function.
  248. In developing your game/application you have to account for the possibility that the user changes the binding and binds the movement to the left hand thumbstick.
  249. Also note that our shoot and grab boolean actions are linked to inputs of type ``Float``.
  250. As mentioned before OpenXR will do conversions between the two, but do read the warning given on that subject earlier in this document.
  251. .. note::
  252. Some of the inputs seem to appear in our list multiple times.
  253. For instance we can find the ``X`` button twice, once as ``X click`` and then as ``X touch``.
  254. This is due to the Touch controller having a capacitive sensor.
  255. * ``X touch`` will be true if the user is merely touching the X button.
  256. * ``X click`` will be true when the user is actually pressing down on the button.
  257. Similarly for the thumbstick we have:
  258. * ``Thumbstick touch`` which will be true if the user is touching the thumbstick.
  259. * ``Thumbstick`` which gives a value for the direction the thumbstick is pushed to.
  260. * ``Thumbstick click`` which is true when the user is pressing down on the thumbstick.
  261. It is important to note that only a select number of XR controllers support touch sensors or have click features on thumbsticks.
  262. Keep that in mind when designing your game/application.
  263. Make sure these are used for optional features of your game/application.
  264. .. _doc_xr_action_map_simple:
  265. The simple controller
  266. ---------------------
  267. The "Simple controller" is a generic controller that OpenXR offers as a fallback.
  268. We'll apply our mapping:
  269. .. image:: img/xr_simple_controller.webp
  270. As becomes painfully clear, the simple controller is often far too simple and falls short for anything but the simplest of VR games/applications.
  271. This is why many XR runtimes only use it as a last resort and will attempt to use bindings from one of the more popular systems as a fallback first.
  272. .. note::
  273. Due to the simple controller likely not covering the needs of your game, it is tempting to provide bindings for every controller supported by OpenXR.
  274. The default action map seems to suggest this as a valid course of action.
  275. As mentioned before, the default action map was designed for ease of migration from Godot 3.
  276. It is the recommendation from the OpenXR Working Group that only bindings for controllers actually tested by the developer are setup.
  277. The XR runtimes are designed with this in mind.
  278. They can perform a better job of rebinding a provided binding than a developer can make educated guesses.
  279. Especially as the developer can't test if this leads to a comfortable experience for the end user.
  280. This is our advice as well: limit your action map to the interaction profiles for devices you have actually tested your game with.
  281. The Oculus Touch controller is widely used as a fallback controller by many runtimes.
  282. If you are able to test your game using a Meta Rift or Quest and add this profile there is a high probability your game will work with other headsets.