class_webxrinterface.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the WebXRInterface.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_WebXRInterface:
  6. WebXRInterface
  7. ==============
  8. **Inherits:** :ref:`ARVRInterface<class_ARVRInterface>` **<** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. AR/VR interface using WebXR.
  10. Description
  11. -----------
  12. WebXR is an open standard that allows creating VR and AR applications that run in the web browser.
  13. As such, this interface is only available when running in an HTML5 export.
  14. WebXR supports a wide range of devices, from the very capable (like Valve Index, HTC Vive, Oculus Rift and Quest) down to the much less capable (like Google Cardboard, Oculus Go, GearVR, or plain smartphones).
  15. Since WebXR is based on Javascript, it makes extensive use of callbacks, which means that ``WebXRInterface`` is forced to use signals, where other AR/VR interfaces would instead use functions that return a result immediately. This makes ``WebXRInterface`` quite a bit more complicated to initialize than other AR/VR interfaces.
  16. Here's the minimum code required to start an immersive VR session:
  17. ::
  18. extends Spatial
  19. var webxr_interface
  20. var vr_supported = false
  21. func _ready():
  22. # We assume this node has a button as a child.
  23. # This button is for the user to consent to entering immersive VR mode.
  24. $Button.connect("pressed", self, "_on_Button_pressed")
  25. webxr_interface = ARVRServer.find_interface("WebXR")
  26. if webxr_interface:
  27. # WebXR uses a lot of asynchronous callbacks, so we connect to various
  28. # signals in order to receive them.
  29. webxr_interface.connect("session_supported", self, "_webxr_session_supported")
  30. webxr_interface.connect("session_started", self, "_webxr_session_started")
  31. webxr_interface.connect("session_ended", self, "_webxr_session_ended")
  32. webxr_interface.connect("session_failed", self, "_webxr_session_failed")
  33. # This returns immediately - our _webxr_session_supported() method
  34. # (which we connected to the "session_supported" signal above) will
  35. # be called sometime later to let us know if it's supported or not.
  36. webxr_interface.is_session_supported("immersive-vr")
  37. func _webxr_session_supported(session_mode, supported):
  38. if session_mode == 'immersive-vr':
  39. vr_supported = supported
  40. func _on_Button_pressed():
  41. if not vr_supported:
  42. OS.alert("Your browser doesn't support VR")
  43. return
  44. # We want an immersive VR session, as opposed to AR ('immersive-ar') or a
  45. # simple 3DoF viewer ('viewer').
  46. webxr_interface.session_mode = 'immersive-vr'
  47. # 'bounded-floor' is room scale, 'local-floor' is a standing or sitting
  48. # experience (it puts you 1.6m above the ground if you have 3DoF headset),
  49. # whereas as 'local' puts you down at the ARVROrigin.
  50. # This list means it'll first try to request 'bounded-floor', then
  51. # fallback on 'local-floor' and ultimately 'local', if nothing else is
  52. # supported.
  53. webxr_interface.requested_reference_space_types = 'bounded-floor, local-floor, local'
  54. # In order to use 'local-floor' or 'bounded-floor' we must also
  55. # mark the features as required or optional.
  56. webxr_interface.required_features = 'local-floor'
  57. webxr_interface.optional_features = 'bounded-floor'
  58. # This will return false if we're unable to even request the session,
  59. # however, it can still fail asynchronously later in the process, so we
  60. # only know if it's really succeeded or failed when our
  61. # _webxr_session_started() or _webxr_session_failed() methods are called.
  62. if not webxr_interface.initialize():
  63. OS.alert("Failed to initialize")
  64. return
  65. func _webxr_session_started():
  66. $Button.visible = false
  67. # This tells Godot to start rendering to the headset.
  68. get_viewport().arvr = true
  69. # This will be the reference space type you ultimately got, out of the
  70. # types that you requested above. This is useful if you want the game to
  71. # work a little differently in 'bounded-floor' versus 'local-floor'.
  72. print ("Reference space type: " + webxr_interface.reference_space_type)
  73. func _webxr_session_ended():
  74. $Button.visible = true
  75. # If the user exits immersive mode, then we tell Godot to render to the web
  76. # page again.
  77. get_viewport().arvr = false
  78. func _webxr_session_failed(message):
  79. OS.alert("Failed to initialize: " + message)
  80. There are several ways to handle "controller" input:
  81. - Using :ref:`ARVRController<class_ARVRController>` nodes and their :ref:`ARVRController.button_pressed<class_ARVRController_signal_button_pressed>` and :ref:`ARVRController.button_release<class_ARVRController_signal_button_release>` signals. This is how controllers are typically handled in AR/VR apps in Godot, however, this will only work with advanced VR controllers like the Oculus Touch or Index controllers, for example. The buttons codes are defined by `Section 3.3 of the WebXR Gamepads Module <https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping>`__.
  82. - Using :ref:`Node._unhandled_input<class_Node_method__unhandled_input>` and :ref:`InputEventJoypadButton<class_InputEventJoypadButton>` or :ref:`InputEventJoypadMotion<class_InputEventJoypadMotion>`. This works the same as normal joypads, except the :ref:`InputEvent.device<class_InputEvent_property_device>` starts at 100, so the left controller is 100 and the right controller is 101, and the button codes are also defined by `Section 3.3 of the WebXR Gamepads Module <https://immersive-web.github.io/webxr-gamepads-module/#xr-standard-gamepad-mapping>`__.
  83. - Using the :ref:`select<class_WebXRInterface_signal_select>`, :ref:`squeeze<class_WebXRInterface_signal_squeeze>` and related signals. This method will work for both advanced VR controllers, and non-traditional "controllers" like a tap on the screen, a spoken voice command or a button press on the device itself. The ``controller_id`` passed to these signals is the same id as used in :ref:`ARVRController.controller_id<class_ARVRController_property_controller_id>`.
  84. You can use one or all of these methods to allow your game or app to support a wider or narrower set of devices and input methods, or to allow more advanced interations with more advanced devices.
  85. Tutorials
  86. ---------
  87. - `How to make a VR game for WebXR with Godot <https://www.snopekgames.com/blog/2020/how-make-vr-game-webxr-godot>`__
  88. Properties
  89. ----------
  90. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------+
  91. | :ref:`PoolVector3Array<class_PoolVector3Array>` | :ref:`bounds_geometry<class_WebXRInterface_property_bounds_geometry>` |
  92. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------+
  93. | :ref:`String<class_String>` | :ref:`optional_features<class_WebXRInterface_property_optional_features>` |
  94. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------+
  95. | :ref:`String<class_String>` | :ref:`reference_space_type<class_WebXRInterface_property_reference_space_type>` |
  96. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------+
  97. | :ref:`String<class_String>` | :ref:`requested_reference_space_types<class_WebXRInterface_property_requested_reference_space_types>` |
  98. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------+
  99. | :ref:`String<class_String>` | :ref:`required_features<class_WebXRInterface_property_required_features>` |
  100. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------+
  101. | :ref:`String<class_String>` | :ref:`session_mode<class_WebXRInterface_property_session_mode>` |
  102. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------+
  103. | :ref:`String<class_String>` | :ref:`visibility_state<class_WebXRInterface_property_visibility_state>` |
  104. +-------------------------------------------------+-------------------------------------------------------------------------------------------------------+
  105. Methods
  106. -------
  107. +-----------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  108. | :ref:`ARVRPositionalTracker<class_ARVRPositionalTracker>` | :ref:`get_controller<class_WebXRInterface_method_get_controller>` **(** :ref:`int<class_int>` controller_id **)** |const| |
  109. +-----------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  110. | void | :ref:`is_session_supported<class_WebXRInterface_method_is_session_supported>` **(** :ref:`String<class_String>` session_mode **)** |
  111. +-----------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------+
  112. Signals
  113. -------
  114. .. _class_WebXRInterface_signal_reference_space_reset:
  115. - **reference_space_reset** **(** **)**
  116. Emitted to indicate that the reference space has been reset or reconfigured.
  117. When (or whether) this is emitted depends on the user's browser or device, but may include when the user has changed the dimensions of their play space (which you may be able to access via :ref:`bounds_geometry<class_WebXRInterface_property_bounds_geometry>`) or pressed/held a button to recenter their position.
  118. See `WebXR's XRReferenceSpace reset event <https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpace/reset_event>`__ for more information.
  119. ----
  120. .. _class_WebXRInterface_signal_select:
  121. - **select** **(** :ref:`int<class_int>` controller_id **)**
  122. Emitted after one of the "controllers" has finished its "primary action".
  123. Use :ref:`get_controller<class_WebXRInterface_method_get_controller>` to get more information about the controller.
  124. ----
  125. .. _class_WebXRInterface_signal_selectend:
  126. - **selectend** **(** :ref:`int<class_int>` controller_id **)**
  127. Emitted when one of the "controllers" has finished its "primary action".
  128. Use :ref:`get_controller<class_WebXRInterface_method_get_controller>` to get more information about the controller.
  129. ----
  130. .. _class_WebXRInterface_signal_selectstart:
  131. - **selectstart** **(** :ref:`int<class_int>` controller_id **)**
  132. Emitted when one of the "controllers" has started its "primary action".
  133. Use :ref:`get_controller<class_WebXRInterface_method_get_controller>` to get more information about the controller.
  134. ----
  135. .. _class_WebXRInterface_signal_session_ended:
  136. - **session_ended** **(** **)**
  137. Emitted when the user ends the WebXR session (which can be done using UI from the browser or device).
  138. At this point, you should do ``get_viewport().arvr = false`` to instruct Godot to resume rendering to the screen.
  139. ----
  140. .. _class_WebXRInterface_signal_session_failed:
  141. - **session_failed** **(** :ref:`String<class_String>` message **)**
  142. Emitted by :ref:`ARVRInterface.initialize<class_ARVRInterface_method_initialize>` if the session fails to start.
  143. ``message`` may optionally contain an error message from WebXR, or an empty string if no message is available.
  144. ----
  145. .. _class_WebXRInterface_signal_session_started:
  146. - **session_started** **(** **)**
  147. Emitted by :ref:`ARVRInterface.initialize<class_ARVRInterface_method_initialize>` if the session is successfully started.
  148. At this point, it's safe to do ``get_viewport().arvr = true`` to instruct Godot to start rendering to the AR/VR device.
  149. ----
  150. .. _class_WebXRInterface_signal_session_supported:
  151. - **session_supported** **(** :ref:`String<class_String>` session_mode, :ref:`bool<class_bool>` supported **)**
  152. Emitted by :ref:`is_session_supported<class_WebXRInterface_method_is_session_supported>` to indicate if the given ``session_mode`` is supported or not.
  153. ----
  154. .. _class_WebXRInterface_signal_squeeze:
  155. - **squeeze** **(** :ref:`int<class_int>` controller_id **)**
  156. Emitted after one of the "controllers" has finished its "primary squeeze action".
  157. Use :ref:`get_controller<class_WebXRInterface_method_get_controller>` to get more information about the controller.
  158. ----
  159. .. _class_WebXRInterface_signal_squeezeend:
  160. - **squeezeend** **(** :ref:`int<class_int>` controller_id **)**
  161. Emitted when one of the "controllers" has finished its "primary squeeze action".
  162. Use :ref:`get_controller<class_WebXRInterface_method_get_controller>` to get more information about the controller.
  163. ----
  164. .. _class_WebXRInterface_signal_squeezestart:
  165. - **squeezestart** **(** :ref:`int<class_int>` controller_id **)**
  166. Emitted when one of the "controllers" has started its "primary squeeze action".
  167. Use :ref:`get_controller<class_WebXRInterface_method_get_controller>` to get more information about the controller.
  168. ----
  169. .. _class_WebXRInterface_signal_visibility_state_changed:
  170. - **visibility_state_changed** **(** **)**
  171. Emitted when :ref:`visibility_state<class_WebXRInterface_property_visibility_state>` has changed.
  172. Property Descriptions
  173. ---------------------
  174. .. _class_WebXRInterface_property_bounds_geometry:
  175. - :ref:`PoolVector3Array<class_PoolVector3Array>` **bounds_geometry**
  176. +----------+-----------------------+
  177. | *Getter* | get_bounds_geometry() |
  178. +----------+-----------------------+
  179. The vertices of a polygon which defines the boundaries of the user's play area.
  180. This will only be available if :ref:`reference_space_type<class_WebXRInterface_property_reference_space_type>` is ``"bounded-floor"`` and only on certain browsers and devices that support it.
  181. The :ref:`reference_space_reset<class_WebXRInterface_signal_reference_space_reset>` signal may indicate when this changes.
  182. ----
  183. .. _class_WebXRInterface_property_optional_features:
  184. - :ref:`String<class_String>` **optional_features**
  185. +----------+------------------------------+
  186. | *Setter* | set_optional_features(value) |
  187. +----------+------------------------------+
  188. | *Getter* | get_optional_features() |
  189. +----------+------------------------------+
  190. A comma-seperated list of optional features used by :ref:`ARVRInterface.initialize<class_ARVRInterface_method_initialize>` when setting up the WebXR session.
  191. If a user's browser or device doesn't support one of the given features, initialization will continue, but you won't be able to use the requested feature.
  192. This doesn't have any effect on the interface when already initialized.
  193. Possible values come from `WebXR's XRReferenceSpaceType <https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType>`__. If you want to use a particular reference space type, it must be listed in either :ref:`required_features<class_WebXRInterface_property_required_features>` or :ref:`optional_features<class_WebXRInterface_property_optional_features>`.
  194. ----
  195. .. _class_WebXRInterface_property_reference_space_type:
  196. - :ref:`String<class_String>` **reference_space_type**
  197. +----------+----------------------------+
  198. | *Getter* | get_reference_space_type() |
  199. +----------+----------------------------+
  200. The reference space type (from the list of requested types set in the :ref:`requested_reference_space_types<class_WebXRInterface_property_requested_reference_space_types>` property), that was ultimately used by :ref:`ARVRInterface.initialize<class_ARVRInterface_method_initialize>` when setting up the WebXR session.
  201. Possible values come from `WebXR's XRReferenceSpaceType <https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType>`__. If you want to use a particular reference space type, it must be listed in either :ref:`required_features<class_WebXRInterface_property_required_features>` or :ref:`optional_features<class_WebXRInterface_property_optional_features>`.
  202. ----
  203. .. _class_WebXRInterface_property_requested_reference_space_types:
  204. - :ref:`String<class_String>` **requested_reference_space_types**
  205. +----------+--------------------------------------------+
  206. | *Setter* | set_requested_reference_space_types(value) |
  207. +----------+--------------------------------------------+
  208. | *Getter* | get_requested_reference_space_types() |
  209. +----------+--------------------------------------------+
  210. A comma-seperated list of reference space types used by :ref:`ARVRInterface.initialize<class_ARVRInterface_method_initialize>` when setting up the WebXR session.
  211. The reference space types are requested in order, and the first on supported by the users device or browser will be used. The :ref:`reference_space_type<class_WebXRInterface_property_reference_space_type>` property contains the reference space type that was ultimately used.
  212. This doesn't have any effect on the interface when already initialized.
  213. Possible values come from `WebXR's XRReferenceSpaceType <https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType>`__. If you want to use a particular reference space type, it must be listed in either :ref:`required_features<class_WebXRInterface_property_required_features>` or :ref:`optional_features<class_WebXRInterface_property_optional_features>`.
  214. ----
  215. .. _class_WebXRInterface_property_required_features:
  216. - :ref:`String<class_String>` **required_features**
  217. +----------+------------------------------+
  218. | *Setter* | set_required_features(value) |
  219. +----------+------------------------------+
  220. | *Getter* | get_required_features() |
  221. +----------+------------------------------+
  222. A comma-seperated list of required features used by :ref:`ARVRInterface.initialize<class_ARVRInterface_method_initialize>` when setting up the WebXR session.
  223. If a user's browser or device doesn't support one of the given features, initialization will fail and :ref:`session_failed<class_WebXRInterface_signal_session_failed>` will be emitted.
  224. This doesn't have any effect on the interface when already initialized.
  225. Possible values come from `WebXR's XRReferenceSpaceType <https://developer.mozilla.org/en-US/docs/Web/API/XRReferenceSpaceType>`__. If you want to use a particular reference space type, it must be listed in either :ref:`required_features<class_WebXRInterface_property_required_features>` or :ref:`optional_features<class_WebXRInterface_property_optional_features>`.
  226. ----
  227. .. _class_WebXRInterface_property_session_mode:
  228. - :ref:`String<class_String>` **session_mode**
  229. +----------+-------------------------+
  230. | *Setter* | set_session_mode(value) |
  231. +----------+-------------------------+
  232. | *Getter* | get_session_mode() |
  233. +----------+-------------------------+
  234. The session mode used by :ref:`ARVRInterface.initialize<class_ARVRInterface_method_initialize>` when setting up the WebXR session.
  235. This doesn't have any effect on the interface when already initialized.
  236. Possible values come from `WebXR's XRSessionMode <https://developer.mozilla.org/en-US/docs/Web/API/XRSessionMode>`__, including: ``"immersive-vr"``, ``"immersive-ar"``, and ``"inline"``.
  237. ----
  238. .. _class_WebXRInterface_property_visibility_state:
  239. - :ref:`String<class_String>` **visibility_state**
  240. +----------+------------------------+
  241. | *Getter* | get_visibility_state() |
  242. +----------+------------------------+
  243. Indicates if the WebXR session's imagery is visible to the user.
  244. Possible values come from `WebXR's XRVisibilityState <https://developer.mozilla.org/en-US/docs/Web/API/XRVisibilityState>`__, including ``"hidden"``, ``"visible"``, and ``"visible-blurred"``.
  245. Method Descriptions
  246. -------------------
  247. .. _class_WebXRInterface_method_get_controller:
  248. - :ref:`ARVRPositionalTracker<class_ARVRPositionalTracker>` **get_controller** **(** :ref:`int<class_int>` controller_id **)** |const|
  249. Gets an :ref:`ARVRPositionalTracker<class_ARVRPositionalTracker>` for the given ``controller_id``.
  250. In the context of WebXR, a "controller" can be an advanced VR controller like the Oculus Touch or Index controllers, or even a tap on the screen, a spoken voice command or a button press on the device itself. When a non-traditional controller is used, interpret the position and orientation of the :ref:`ARVRPositionalTracker<class_ARVRPositionalTracker>` as a ray pointing at the object the user wishes to interact with.
  251. Use this method to get information about the controller that triggered one of these signals:
  252. - :ref:`selectstart<class_WebXRInterface_signal_selectstart>`
  253. - :ref:`select<class_WebXRInterface_signal_select>`
  254. - :ref:`selectend<class_WebXRInterface_signal_selectend>`
  255. - :ref:`squeezestart<class_WebXRInterface_signal_squeezestart>`
  256. - :ref:`squeeze<class_WebXRInterface_signal_squeeze>`
  257. - :ref:`squeezestart<class_WebXRInterface_signal_squeezestart>`
  258. ----
  259. .. _class_WebXRInterface_method_is_session_supported:
  260. - void **is_session_supported** **(** :ref:`String<class_String>` session_mode **)**
  261. Checks if the given ``session_mode`` is supported by the user's browser.
  262. Possible values come from `WebXR's XRSessionMode <https://developer.mozilla.org/en-US/docs/Web/API/XRSessionMode>`__, including: ``"immersive-vr"``, ``"immersive-ar"``, and ``"inline"``.
  263. This method returns nothing, instead it emits the :ref:`session_supported<class_WebXRInterface_signal_session_supported>` signal with the result.
  264. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  265. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  266. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`