openxr_hand_tracking.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. .. _doc_openxr_hand_tracking:
  2. OpenXR hand tracking
  3. ====================
  4. Introduction
  5. ------------
  6. .. note::
  7. This page focuses specifically on the feature set exposed through OpenXR.
  8. Parts of the functionality presented here also applies to WebXR and can by provided
  9. by other XR interfaces.
  10. When discussing hand tracking it is important to know that there are differences of opinion as to where lines are drawn.
  11. The practical result of this is that there are differences in implementation between the different OpenXR runtimes.
  12. You may find yourself in a place where chosen hardware doesn't support a piece of the puzzle or does things differently
  13. enough from the other platforms that you need to do extra work.
  14. That said, recent improvements to the OpenXR specification are closing these gaps and as platforms implement
  15. these improvements we are getting closer to a future where we have either full portability between platforms
  16. or at least a clear way to detect the capabilities of a platform.
  17. When we look at the early days of VR the focus of the major platforms was on tracked controller based input.
  18. Here we are tracking a physical device that also has buttons for further input.
  19. From the tracking data we can infer the location of the player's hands but no further information is known,
  20. traditionally it was left up to the game to implement a mechanism to display the player's hand and animate
  21. the fingers based on further input from the controller, be it due to buttons being pressed or through proximity
  22. sensors.
  23. Often fingers are also placed based on context, what the user is holding, and what action a user is performing.
  24. More recently optical hand tracking has become a popular solution, where cameras track the user's hands
  25. and full tracking data for the hand and finger positions becomes available.
  26. Many vendors saw this as completely separate from controller tracking and introduced independent APIs to
  27. access hand and finger positions and orientation data.
  28. When handling input, it was up to the game developer to implement a gesture detection mechanism.
  29. This split also exists in OpenXR, where controller tracking is handled primarily by the action map system,
  30. while optical hand tracking is primarily handled by the hand tracking API extension.
  31. However, the world is not that black and white and we're seeing a number of scenarios
  32. where we cross the line:
  33. * Devices that fit in both categories, such as tracked gloves
  34. and controllers such as the Index controller that also perform finger tracking.
  35. * XR Runtimes that implement inferred hand tracking from controller data as a means
  36. to solve proper finger placement for multiple controllers.
  37. * XR applications that wish to seamlessly switch between controller and hand tracking
  38. offering the same user experience regardless of approach used.
  39. OpenXR is answering this call by introducing further extensions that lets us query the capabilities of
  40. the XR runtime/hardware or that add further functionality across this divide.
  41. The problem that currently does remain is that there are gaps in adopting these extensions,
  42. with some platforms thus not reporting capabilities to their full extent.
  43. As such you may need to test for the features available on specific hardware
  44. and adjust your approach accordingly.
  45. Demo project
  46. ------------
  47. The information presented on this page was used to create a demo project that can be found
  48. `here <https://github.com/godotengine/godot-demo-projects/tree/master/xr/openxr_hand_tracking_demo>`_.
  49. The Hand Tracking API
  50. ---------------------
  51. As mentioned in our introduction, the hand tracking API is primarily used with optical hand tracking
  52. and on many platforms only works when the user is not holding a controller.
  53. Some platforms support controller inferred hand tracking meaning that you will get hand tracking data
  54. even if the user is holding a controller.
  55. This includes SteamVR, Meta Quest (currently native only but Meta link support is likely coming),
  56. and hopefully soon others as well.
  57. The hand tracking implementation in Godot has been standardized around the Godot Humanoid Skeleton
  58. and works both in OpenXR and WebXR. The instructions below will thus work in both environments.
  59. In order to use the hand tracking API with OpenXR you first need to enable it.
  60. This can be done in the project settings:
  61. .. image:: img/xr_enable_handtracking.webp
  62. For some standalone XR devices you also need to configure the hand tracking extension in export settings,
  63. for instance for Meta Quest:
  64. .. image:: img/openxr_enable_hand_tracking_meta.webp
  65. Now you need to add 3 components into your scene for each hand:
  66. * A tracked node to position the hand.
  67. * A properly skinned hand mesh with skeleton.
  68. * A skeleton modifier that applies finger tracking data to the skeleton.
  69. .. image:: img/openxr_hand_tracking_nodes.webp
  70. Hand tracking node
  71. ^^^^^^^^^^^^^^^^^^
  72. The hand tracking system uses separate hand trackers to track the position of the player's hands
  73. within our tracking space.
  74. This information has been separated out for the following use cases:
  75. * Tracking happens in the local space of the :ref:`XROrigin3D <class_xrorigin3d>` node.
  76. This node must be a child of the `XROrigin3D` node in order to be correctly placed.
  77. * This node can be used as an IK target when an upper body mesh with arms is used instead
  78. of separate hand meshes.
  79. * Actual placement of the hands may be loosely bound to the tracking in scenarios such as
  80. avatar creation UIs, fake mirrors, or similar situations
  81. resulting in the hand mesh and finger tracking being localized elsewhere.
  82. We'll concentrate on the first use case only.
  83. For this you need to add an :ref:`XRNode3D <class_xrnode3d>` node to your ``XROrigin3D`` node.
  84. * On this node the ``tracker`` should be set to ``/user/hand_tracker/left`` or ``/user/hand_tracker/right``
  85. for the left or right hand respectively.
  86. * The ``pose`` should remain set to ``default``, no other option will work here.
  87. * The checkbox ``Show When Tracked`` will automatically hide this node if no tracking data is available,
  88. or make this node visible if tracking data is available.
  89. Rigged hand mesh
  90. ^^^^^^^^^^^^^^^^
  91. In order to display our hand we need a hand mesh that is properly rigged and skinned.
  92. For this Godot uses the hand bone structure as defined for the :ref:`Godot Humanoid <class_skeletonprofilehumanoid>`
  93. but optionally supporting an extra tip bone for each finger.
  94. The `OpenXR hand tracking demo <https://github.com/godotengine/godot-demo-projects/tree/master/xr/openxr_hand_tracking_demo>`_
  95. contains example glTF files of properly rigged hands.
  96. We will be using those here and add them as a child to our ``XRNode3D`` node.
  97. We also need to enable editable children to gain access to our :ref:`Skeleton3D <class_skeleton3d>` node.
  98. The hand skeleton modifier
  99. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  100. Finally we need to add a :ref:`XRHandModifier3D <class_xrhandmodifier3d>` node as a child to our ``Skeleton3D`` node.
  101. This node will obtain the finger tracking data from OpenXR and apply it the hand model.
  102. You need to set the ``Hand Tracker`` property to either ``/user/hand_tracker/left`` or ``/user/hand_tracker/right``
  103. depending on whether we are apply the tracking data of respectively the left or right hand.
  104. You can also set the ``Bone Update`` mode on this node.
  105. * ``Full`` applies the hand tracking data fully.
  106. This does mean that the skeleton positioning will potentially reflect the size of the actual hand of the user.
  107. This can lead to scrunching effect if meshes aren't weighted properly to account for this.
  108. Make sure you test your game with players of all sizes when optical hand tracking is used!
  109. * ``Rotation Only`` will only apply rotation to the bones of the hands and keep the bone length as is.
  110. In this mode the size of the hand mesh doesn't change.
  111. With this added, when we run the project we should see the hand correctly displayed if hand tracking is supported.
  112. The hand tracking data source
  113. -----------------------------
  114. This is an OpenXR extension that provides information about the source of the hand tracking data.
  115. At this moment only a few runtimes implement it but if it is available, Godot will activate it.
  116. If this extension is not supported and thus unknown is returned, you can make the following assumptions:
  117. * If you are using SteamVR (including Steam link), only controller based hand tracking is supported.
  118. * For any other runtime, if hand tracking is supported, only optical hand tracking is supported
  119. (Note, Meta Link currently fall into this category).
  120. * In all other cases, no hand tracking is supported at all.
  121. You can access this information through code:
  122. .. code-block:: gdscript
  123. var hand_tracker : XRHandTracker = XRServer.get_tracker('/user/hand_tracker/left')
  124. if hand_tracker:
  125. if hand_tracker.has_tracking_data:
  126. if hand_tracker.hand_tracking_source == XRHandTracker.HAND_TRACKING_SOURCE_UNKNOWN:
  127. print("Hand tracking source unknown")
  128. elif hand_tracker.hand_tracking_source == XRHandTracker.HAND_TRACKING_SOURCE_UNOBSTRUCTED:
  129. print("Hand tracking source is optical hand tracking")
  130. elif hand_tracker.hand_tracking_source == XRHandTracker.HAND_TRACKING_SOURCE_CONTROLLER:
  131. print("Hand tracking data is inferred from controller data")
  132. else:
  133. print("Unknown hand tracking source ", hand_tracker.hand_tracking_source)
  134. else:
  135. print("Hand is currently not being tracked")
  136. else:
  137. print("No hand tracker registered")
  138. This example logs the state for the left hand.
  139. If in this example no hand tracker is returned by ``get_tracker``,
  140. this means the hand tracking API is not supported on the XR runtime at all.
  141. If there is a tracker but `has_tracking_data` is false, the user's hand is currently not being tracked.
  142. This is likely caused by one of the following reasons:
  143. * The player's hand is not visible by any of the tracking cameras on the headset
  144. * The player is currently using a controller and the headset only supports optical hand tracking
  145. * The controller is turned off and only controller hand tracking is supported.
  146. Handling user input
  147. -------------------
  148. Reacting to actions performed by the user is handled through :ref:`doc_xr_action_map`
  149. if controllers are used.
  150. In the action map you can map various inputs like the trigger or joystick on the controller
  151. to an action. This can then drive logic in your game.
  152. When hand tracking is used we originally had no such inputs,
  153. inputs are driven by gestures made by the user such as making a fist to grab
  154. or pinching the thumb and index finger together to select something.
  155. It was up to the game developer to implement this.
  156. Recognizing that there is an increasing demand for applications that can switch seamlessly
  157. between controller and hand tracking and the need some form of basic input capability,
  158. a number of extensions were added to the specification that provide some basic gesture recognition
  159. and can be used with the action map.
  160. The hand interaction profile
  161. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  162. The `hand interaction profile extension <https://github.khronos.org/OpenXR-Inventory/extension_support.html#XR_EXT_hand_interaction>`_
  163. is a new core extension which supports pinch, grasp, and poke gestures and related poses.
  164. There is still limited support for this extension but it should become available in more
  165. runtimes in the near future.
  166. .. image:: img/openxr_hand_interaction_profile.webp
  167. The pinch gesture is triggered by pinching your thumb and index finger together.
  168. This is often used as a select gesture for menu systems, similar to using your controller
  169. to point at an object and press the trigger to select and is thus often mapped as such.
  170. * The ``pinch pose`` is a pose positioned in the middle between the tip of the thumb and
  171. the tip of the index finger and oriented such that a ray cast can be used to identify a target.
  172. * The ``pinch`` float input is a value between 0.0 (the tip of the thumb and index finger are apart)
  173. and 1.0 (the tip of the thumb and index finger are touching).
  174. * The ``pinch ready`` input is true when the tips of the fingers are (close to) touching.
  175. The grasp gesture is triggered by making a fist and is often used to pick items up,
  176. similar to engaging the squeeze input on controllers.
  177. * The ``grasp`` float input is a value between 0.0 (open hand) and 1.0 (fist).
  178. * The ``grasp ready`` input is true when the user made a fist.
  179. The poke gesture is triggered by extending your index finger, this one is a bit
  180. of an exception as the pose at the tip of your index finger is often used to poke
  181. an interactable object. The ``poke pose`` is a pose positioned on the tip of the index finger.
  182. Finally the ``aim activate (ready)`` input is defined as an input that is 1.0/true
  183. when the index finger is extended and pointing at a target that can be activated.
  184. How runtimes interpret this, is not clear.
  185. With this setup the normal ``left_hand`` and ``right_hand`` trackers are used and you can
  186. thus seamlessly switch between controller and hand tracking input.
  187. .. note::
  188. You need to enable the hand interaction profile extension in the OpenXR project settings.
  189. Microsoft hand interaction profile
  190. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  191. The `Microsoft hand interaction profile extension <https://github.khronos.org/OpenXR-Inventory/extension_support.html#XR_MSFT_hand_interaction>`_
  192. was introduced by Microsoft and loosely mimics the simple controller profile.
  193. Meta has also added support for this extension but only on their native OpenXR client,
  194. it is currently not available over Meta Link.
  195. .. image:: img/openxr_msft_hand_interaction_profile.webp
  196. Pinch support is exposed through the ``select`` input, the value of which
  197. is 0.0 when the tip of the thumb and index finger are apart
  198. and 1.0 when they are together.
  199. Note that in this profile the ``aim pose`` is redefined as a pose between thumb
  200. and index finger, oriented so a ray cast can be used to identify a target.
  201. Grasp support is exposed through the ``squeeze`` input, the value of which
  202. is 0.0 when the hand is open, and 1.0 when a fist is made.
  203. With this setup the normal ``left_hand`` and ``right_hand`` trackers are used and you can
  204. thus seamlessly switch between controller and hand tracking input.
  205. HTC hand interaction profile
  206. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  207. The `HTC hand interaction profile extension <https://github.khronos.org/OpenXR-Inventory/extension_support.html#XR_HTC_hand_interaction>`_
  208. was introduced by HTC and is defined similarly to the Microsoft extension.
  209. It is only supported by HTC for the Focus 3 and Elite XR headsets.
  210. .. image:: img/openxr_htc_hand_interaction_profile.webp
  211. See the Microsoft hand interaction profile for the gesture support.
  212. The defining difference is that this extension introduces two new trackers,
  213. ``/user/hand_htc/left`` and ``/user/hand_htc/right``.
  214. This means that extra logic needs to be implemented to switch between the default trackers
  215. and the HTC specific trackers when the user puts down, or picks up, their controller.
  216. Simple controller profile
  217. ^^^^^^^^^^^^^^^^^^^^^^^^^
  218. The simple controller profile is a standard core profile defined as a fallback profile
  219. when a controller is used for which no profile exists.
  220. There are a number of OpenXR runtimes that will mimic controllers through
  221. the simple controller profile when hand tracking is used.
  222. Unfortunately there is no sound way to determine whether an unknown controller is used
  223. or whether hand tracking is emulating a controller through this profile.
  224. .. image:: img/openxr_simple_controller_hand.webp
  225. XR runtimes are free to define how the simple controller profile operates,
  226. so there is also no certainty to how this profile is mapped to gestures.
  227. The most common mapping seems to be that ``select click`` is true
  228. when the tip of the thumb and index fingers are touching while the
  229. user's palm is facing away from the user.
  230. ``menu click`` will be true when tip of the thumb and index fingers
  231. are touching while the user's palm is facing towards the user.
  232. With this setup the normal ``left_hand`` and ``right_hand`` trackers are used and you can
  233. thus seamlessly switch between controller and hand tracking input.
  234. .. note::
  235. As some of these interaction profiles have overlap it is important to know
  236. that you can add each profile to your action map and the XR runtime will choose
  237. the best fitting profile.
  238. For instance, a Meta Quest supports both the Microsoft hand interaction profile
  239. and simple controller profile.
  240. If both are specified the Microsoft hand interaction profile will take precedence
  241. and will be used.
  242. The expectation is that once Meta supports the core hand interaction profile
  243. extension, that profile will take precedence over both Microsoft
  244. and simple controller profiles.
  245. Gesture based input
  246. ^^^^^^^^^^^^^^^^^^^
  247. If the platform doesn't support any interaction profiles when hand tracking is used,
  248. or if you're building an application where you need more complicated gesture support
  249. you're going to need to build your own gesture recognition system.
  250. You can obtain the full hand tracking data through the :ref:`XRHandTracker <class_xrhandtracker>`
  251. resource for each hand. You can obtain the hand tracker by calling ``XRServer.get_tracker``
  252. and using either ``/user/hand_tracker/left`` or ``/user/hand_tracker/left`` as the tracker.
  253. This resource provides access to all the joint information for the given hand.
  254. Detailing out a full gesture recognition algorithm goes beyond the scope of this manual
  255. however there are a number of community projects you can look at:
  256. * `Julian Todd's Auto hands library <https://github.com/Godot-Dojo/Godot-XR-AH>`_
  257. * `Malcolm Nixons Hand Pose Detector <https://github.com/Malcolmnixon/GodotXRHandPoseDetector>`_