a_better_xr_start_script.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. .. _doc_a_better_xr_start_script:
  2. A better XR start script
  3. ========================
  4. In :ref:`doc_setting_up_xr` we introduced a startup script that initialises our setup which we used as our script on our main node.
  5. This script performs the minimum steps required for any given interface.
  6. When using OpenXR there are a number of improvements we should do here.
  7. For this we've created a more elaborate starting script.
  8. You will find these used in our demo projects.
  9. Alternatively, if you are using XR Tools (see :ref:`doc_introducing_xr_tools`) it contains a version of this script updated with some features related to XR tools.
  10. Below we will detail out the script used in our demos and explain the parts that are added.
  11. Signals for our script
  12. ----------------------
  13. We are introducing 3 signals to our script so that our game can add further logic:
  14. - ``focus_lost`` is emitted when the player takes off their headset or when the player enters the menu system of the headset.
  15. - ``focus_gained`` is emitted when the player puts their headset back on or exists the menu system and returns to the game.
  16. - ``pose_recentered`` is emitted when the headset requests the players position to be reset.
  17. Our game should react accordingly to these signals.
  18. .. tabs::
  19. .. code-tab:: gdscript GDScript
  20. extends Node3D
  21. signal focus_lost
  22. signal focus_gained
  23. signal pose_recentered
  24. ...
  25. .. code-tab:: csharp
  26. using Godot;
  27. public partial class MyNode3D : Node3D
  28. {
  29. [Signal]
  30. public delegate void FocusLostEventHandler();
  31. [Signal]
  32. public delegate void FocusGainedEventHandler();
  33. [Signal]
  34. public delegate void PoseRecenteredEventHandler();
  35. ...
  36. Variables for our script
  37. ------------------------
  38. We introduce a few new variables to our script as well:
  39. - ``maximum_refresh_rate`` will control the headsets refresh rate if this is supported by the headset.
  40. - ``xr_interface`` holds a reference to our XR interface, this already existed but we now type it to get full access to our :ref:`XRInterface <class_xrinterface>` API.
  41. - ``xr_is_focussed`` will be set to true whenever our game has focus.
  42. .. tabs::
  43. .. code-tab:: gdscript GDScript
  44. ...
  45. @export var maximum_refresh_rate : int = 90
  46. var xr_interface : OpenXRInterface
  47. var xr_is_focussed = false
  48. ...
  49. .. code-tab:: csharp
  50. ...
  51. [Export]
  52. public int MaximumRefreshRate { get; set; } = 90;
  53. private OpenXRInterface _xrInterface;
  54. private bool _xrIsFocused;
  55. ...
  56. Our updated ready function
  57. --------------------------
  58. The ready function mostly remains the same but we hook up a number of signals that will be emitted by the :ref:`XRInterface <class_xrinterface>`.
  59. We'll provide more detail about these signals as we implement them.
  60. We also quit our application if we couldn't successfully initialise OpenXR.
  61. Now this can be a choice.
  62. If you are making a mixed mode game you setup the VR mode of your game on success,
  63. and setup the non-VR mode of your game on failure.
  64. However, when running a VR only application on a standalone headset,
  65. it is nicer to exit on failure than to hang the system.
  66. .. tabs::
  67. .. code-tab:: gdscript GDScript
  68. ...
  69. # Called when the node enters the scene tree for the first time.
  70. func _ready():
  71. xr_interface = XRServer.find_interface("OpenXR")
  72. if xr_interface and xr_interface.is_initialized():
  73. print("OpenXR instantiated successfully.")
  74. var vp : Viewport = get_viewport()
  75. # Enable XR on our viewport
  76. vp.use_xr = true
  77. # Make sure v-sync is off, v-sync is handled by OpenXR
  78. DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
  79. # Connect the OpenXR events
  80. xr_interface.connect("session_begun", _on_openxr_session_begun)
  81. xr_interface.connect("session_visible", _on_openxr_visible_state)
  82. xr_interface.connect("session_focussed", _on_openxr_focused_state)
  83. xr_interface.connect("session_stopping", _on_openxr_stopping)
  84. xr_interface.connect("pose_recentered", _on_openxr_pose_recentered)
  85. else:
  86. # We couldn't start OpenXR.
  87. print("OpenXR not instantiated!")
  88. get_tree().quit()
  89. ...
  90. .. code-tab:: csharp
  91. ...
  92. /// <summary>
  93. /// Called when the node enters the scene tree for the first time.
  94. /// </summary>
  95. public override void _Ready()
  96. {
  97. _xrInterface = (OpenXRInterface)XRServer.FindInterface("OpenXR");
  98. if (_xrInterface != null && _xrInterface.IsInitialized())
  99. {
  100. GD.Print("OpenXR instantiated successfully.");
  101. var vp = GetViewport();
  102. // Enable XR on our viewport
  103. vp.UseXR = true;
  104. // Make sure v-sync is off, v-sync is handled by OpenXR
  105. DisplayServer.WindowSetVsyncMode(DisplayServer.VSyncMode.Disabled);
  106. // Connect the OpenXR events
  107. _xrInterface.SessionBegun += OnOpenXRSessionBegun;
  108. _xrInterface.SessionVisible += OnOpenXRVisibleState;
  109. _xrInterface.SessionFocussed += OnOpenXRFocusedState;
  110. _xrInterface.SessionStopping += OnOpenXRStopping;
  111. _xrInterface.PoseRecentered += OnOpenXRPoseRecentered;
  112. }
  113. else
  114. {
  115. // We couldn't start OpenXR.
  116. GD.Print("OpenXR not instantiated!");
  117. GetTree().Quit();
  118. }
  119. }
  120. ...
  121. On session begun
  122. ----------------
  123. This signal is emitted by OpenXR when our session is setup.
  124. This means the headset has run through setting everything up and is ready to begin receiving content from us.
  125. Only at this time various information is properly available.
  126. The main thing we do here is to check our headsets refresh rate.
  127. We also check the available refresh rates reported by the XR runtime to determine if we want to set our headset to a higher refresh rate.
  128. Finally we match our physics update rate to our headset update rate.
  129. Godot runs at a physics update rate of 60 updates per second by default while headsets run at a minimum of 72,
  130. and for modern headsets often up to 144 frames per second.
  131. Not matching the physics update rate will cause stuttering as frames are rendered without objects moving.
  132. .. tabs::
  133. .. code-tab:: gdscript GDScript
  134. ...
  135. # Handle OpenXR session ready
  136. func _on_openxr_session_begun() -> void:
  137. # Get the reported refresh rate
  138. var current_refresh_rate = xr_interface.get_display_refresh_rate()
  139. if current_refresh_rate > 0:
  140. print("OpenXR: Refresh rate reported as ", str(current_refresh_rate))
  141. else:
  142. print("OpenXR: No refresh rate given by XR runtime")
  143. # See if we have a better refresh rate available
  144. var new_rate = current_refresh_rate
  145. var available_rates : Array = xr_interface.get_available_display_refresh_rates()
  146. if available_rates.size() == 0:
  147. print("OpenXR: Target does not support refresh rate extension")
  148. elif available_rates.size() == 1:
  149. # Only one available, so use it
  150. new_rate = available_rates[0]
  151. else:
  152. for rate in available_rates:
  153. if rate > new_rate and rate <= maximum_refresh_rate:
  154. new_rate = rate
  155. # Did we find a better rate?
  156. if current_refresh_rate != new_rate:
  157. print("OpenXR: Setting refresh rate to ", str(new_rate))
  158. xr_interface.set_display_refresh_rate(new_rate)
  159. current_refresh_rate = new_rate
  160. # Now match our physics rate
  161. Engine.physics_ticks_per_second = current_refresh_rate
  162. ...
  163. .. code-tab:: csharp
  164. ...
  165. /// <summary>
  166. /// Handle OpenXR session ready
  167. /// </summary>
  168. private void OnOpenXRSessionBegun()
  169. {
  170. // Get the reported refresh rate
  171. var currentRefreshRate = _xrInterface.DisplayRefreshRate;
  172. GD.Print(currentRefreshRate > 0.0F
  173. ? $"OpenXR: Refresh rate reported as {currentRefreshRate}"
  174. : "OpenXR: No refresh rate given by XR runtime");
  175. // See if we have a better refresh rate available
  176. var newRate = currentRefreshRate;
  177. var availableRates = _xrInterface.GetAvailableDisplayRefreshRates();
  178. if (availableRates.Count == 0)
  179. {
  180. GD.Print("OpenXR: Target does not support refresh rate extension");
  181. }
  182. else if (availableRates.Count == 1)
  183. {
  184. // Only one available, so use it
  185. newRate = (float)availableRates[0];
  186. }
  187. else
  188. {
  189. GD.Print("OpenXR: Available refresh rates: ", availableRates);
  190. foreach (float rate in availableRates)
  191. if (rate > newRate && rate <= MaximumRefreshRate)
  192. newRate = rate;
  193. }
  194. // Did we find a better rate?
  195. if (currentRefreshRate != newRate)
  196. {
  197. GD.Print($"OpenXR: Setting refresh rate to {newRate}");
  198. _xrInterface.DisplayRefreshRate = newRate;
  199. currentRefreshRate = newRate;
  200. }
  201. // Now match our physics rate
  202. Engine.PhysicsTicksPerSecond = (int)currentRefreshRate;
  203. }
  204. ...
  205. On visible state
  206. ----------------
  207. This signal is emitted by OpenXR when our game becomes visible but is not focussed.
  208. This is a bit of a weird description in OpenXR but it basically means that our game has just started
  209. and we're about to switch to the focussed state next,
  210. that the user has opened a system menu or the users has just took their headset off.
  211. On receiving this signal we'll update our focussed state,
  212. we'll change the process mode of our node to disabled which will pause processing on this node and it's children,
  213. and emit our ``focus_lost`` signal.
  214. If you've added this script to your root node,
  215. this means your game will automatically pause when required.
  216. If you haven't, you can connect a method to the signal that performs additional changes.
  217. .. note::
  218. While your game is in visible state because the user has opened a system menu,
  219. Godot will keep rendering frames and head tracking will remain active so your game will remain visible in the background.
  220. However controller and hand tracking will be disabled until the user exits the system menu.
  221. .. tabs::
  222. .. code-tab:: gdscript GDScript
  223. ...
  224. # Handle OpenXR visible state
  225. func _on_openxr_visible_state() -> void:
  226. # We always pass this state at startup,
  227. # but the second time we get this it means our player took off their headset
  228. if xr_is_focussed:
  229. print("OpenXR lost focus")
  230. xr_is_focussed = false
  231. # pause our game
  232. process_mode = Node.PROCESS_MODE_DISABLED
  233. emit_signal("focus_lost")
  234. ...
  235. .. code-tab:: csharp
  236. ...
  237. /// <summary>
  238. /// Handle OpenXR visible state
  239. /// </summary>
  240. private void OnOpenXRVisibleState()
  241. {
  242. // We always pass this state at startup,
  243. // but the second time we get this it means our player took off their headset
  244. if (_xrIsFocused)
  245. {
  246. GD.Print("OpenXR lost focus");
  247. _xrIsFocused = false;
  248. // Pause our game
  249. ProcessMode = ProcessModeEnum.Disabled;
  250. EmitSignal(SignalName.FocusLost);
  251. }
  252. }
  253. ...
  254. On focussed state
  255. -----------------
  256. This signal is emitted by OpenXR when our game gets focus.
  257. This is done at the completion of our startup,
  258. but it can also be emitted when the user exits a system menu, or put their headset back on.
  259. Note also that when your game starts while the user is not wearing their headset,
  260. the game stays in 'visible' state until the user puts their headset on.
  261. .. warning::
  262. It is thus important to keep your game paused while in visible mode.
  263. If you don't the game will keep on running while your user isn't interacting with your game.
  264. Also when the game returns to focussed mode,
  265. suddenly all controller and hand tracking is re-enabled and could have game breaking consequences
  266. if you do not react to this accordingly.
  267. Be sure to test this behaviour in your game!
  268. While handling our signal we will update the focusses state, unpause our node and emit our ``focus_gained`` signal.
  269. .. tabs::
  270. .. code-tab:: gdscript GDScript
  271. ...
  272. # Handle OpenXR focused state
  273. func _on_openxr_focused_state() -> void:
  274. print("OpenXR gained focus")
  275. xr_is_focussed = true
  276. # unpause our game
  277. process_mode = Node.PROCESS_MODE_INHERIT
  278. emit_signal("focus_gained")
  279. ...
  280. .. code-tab:: csharp
  281. ...
  282. /// <summary>
  283. /// Handle OpenXR focused state
  284. /// </summary>
  285. private void OnOpenXRFocusedState()
  286. {
  287. GD.Print("OpenXR gained focus");
  288. _xrIsFocused = true;
  289. // Un-pause our game
  290. ProcessMode = ProcessModeEnum.Inherit;
  291. EmitSignal(SignalName.FocusGained);
  292. }
  293. ...
  294. On stopping state
  295. -----------------
  296. This signal is emitted by OpenXR when we enter our stop state.
  297. There are some differences between platforms when this happens.
  298. On some platforms this is only emitted when the game is being closed.
  299. But on other platforms this will also be emitted every time the player takes off their headset.
  300. For now this method is only a place holder.
  301. .. tabs::
  302. .. code-tab:: gdscript GDScript
  303. ...
  304. # Handle OpenXR stopping state
  305. func _on_openxr_stopping() -> void:
  306. # Our session is being stopped.
  307. print("OpenXR is stopping")
  308. ...
  309. .. code-tab:: csharp
  310. ...
  311. /// <summary>
  312. /// Handle OpenXR stopping state
  313. /// </summary>
  314. private void OnOpenXRStopping()
  315. {
  316. // Our session is being stopped.
  317. GD.Print("OpenXR is stopping");
  318. }
  319. ...
  320. On pose recentered
  321. ------------------
  322. This signal is emitted by OpenXR when the user requests their view to be recentered.
  323. Basically this communicates to your game that the user is now facing forward
  324. and you should re-orient the player so they are facing forward in the virtual world.
  325. As doing so is dependent on your game, your game needs to react accordingly.
  326. All we do here is emit the ``pose_recentered`` signal.
  327. You can connect to this signal and implement the actual recenter code.
  328. Often it is enough to call :ref:`center_on_hmd() <class_XRServer_method_center_on_hmd>`.
  329. .. tabs::
  330. .. code-tab:: gdscript GDScript
  331. ...
  332. # Handle OpenXR pose recentered signal
  333. func _on_openxr_pose_recentered() -> void:
  334. # User recentered view, we have to react to this by recentering the view.
  335. # This is game implementation dependent.
  336. emit_signal("pose_recentered")
  337. .. code-tab:: csharp
  338. ...
  339. /// <summary>
  340. /// Handle OpenXR pose recentered signal
  341. /// </summary>
  342. private void OnOpenXRPoseRecentered()
  343. {
  344. // User recentered view, we have to react to this by recentering the view.
  345. // This is game implementation dependent.
  346. EmitSignal(SignalName.PoseRecentered);
  347. }
  348. }
  349. And that finished our script. It was written so that it can be re-used over multiple projects.
  350. Just add it as the script on your main node (and extend it if needed)
  351. or add it on a child node specific for this script.