setting_up_xr.rst 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. .. _doc_setting_up_xr:
  2. Setting up XR
  3. =============
  4. Introduction to the XR system in Godot
  5. --------------------------------------
  6. Godot provides a modular XR system that abstracts many of the different XR platform specifics away from the user.
  7. At the core sits the :ref:`XRServer <class_xrserver>` which acts as a central interface to the XR system that allows users to discover interfaces and interact with the components of the XR system.
  8. Each supported XR platform is implemented as an :ref:`XRInterface <class_xrinterface>`. Supported interfaces register themselves with the :ref:`XRServer <class_xrserver>` and can be queried with the ``find_interface`` method on the :ref:`XRServer <class_xrserver>`. When the desired interface is found it can be initialized by calling ``initialize`` on the interface.
  9. .. warning::
  10. A registered interface means nothing more than that the interface is available, if the interface is not supported by the host system, initialization may fail and return ``false``. This can have many reasons and sadly the reasons differ from platform to platform. It can be because the user hasn't installed the required software, or that the user simply hasn't plugged in their headset. You as a developer must thus react properly on an interface failing to initialize.
  11. Due to the special requirements for output in XR, especially for head mounted devices that supply different images to each eye, the :ref:`XRServer <class_xrserver>` in Godot will override various features in the rendering system. For stand-alone devices this means the final output is handled by the :ref:`XRInterface <class_xrinterface>` and Godot's usual output system is disabled. For desktop XR devices that work as a second screen it is possible to dedicate a separate :ref:`Viewport <class_viewport>` to handle the XR output, leaving the main Godot window available for displaying alternative content.
  12. .. note::
  13. Note that only one interface can be responsible for handling the output to an XR device, this is known as the primary interface and by default will be the first interface that is initialized. Godot currently thus only supports implementations with a single headset.
  14. It is possible, but increasingly uncommon, to have a secondary interface, for example to add tracking to an otherwise 3DOF only device.
  15. There are three XR specific node types that you will find in nearly all XR applications:
  16. - :ref:`XROrigin3D <class_xrorigin3d>` represents, for all intents and purposes, the center point of your play space. That is an oversimplified statement but we'll go into more detail later. All objects tracked in physical space by the XR platform are positioned in relation to this point.
  17. - :ref:`XRCamera3D <class_xrcamera3d>` represents the (stereo) camera that is used when rendering output for the XR device. The positioning of this node is controlled by the XR system and updated automatically using the tracking information provided by the XR platform.
  18. - :ref:`XRController3D <class_xrcontroller3d>` represents a controller used by the player, commonly there are two, one held in each hand. These nodes give access to various states on these controllers and send out signals when the player presses buttons on them. The positioning of this node is controlled by the XR system and updated automatically using the tracking information provided by the XR platform.
  19. There are other XR related nodes and there is much more to say about these three nodes, but we'll get into that later on.
  20. Prerequisites for XR in Godot 4
  21. -------------------------------
  22. While in Godot 3 most things worked out of the box, Godot 4 needs a little more setup. This is mainly due to the more advanced nature of the Vulkan renderer. There are many rendering features in Vulkan the XR system uses that aren't enabled by default. They are very easy to turn on, simply open up your project settings and tick the XR shaders tickbox in the XR section:
  23. .. image:: img/xr_shaders.png
  24. .. UPDATE: Not supported yet. When all or most post process effects work in
  25. .. stereoscopic rendering, remove or update this note.
  26. .. warning::
  27. As Godot 4 is still in development, many post process effects have not yet been updated to support stereoscopic rendering. Using these will have adverse effects.
  28. .. UPDATE: Recommended renderer may change. If the Mobile renderer (or any other
  29. .. renderer) is recommended for all XR projects, update this note.
  30. .. note::
  31. Godot 4 has 3 renderer options, Compatibility, Mobile, and Forward+. In the future XR desktop projects should use Forward+, and projects for standalone headsets
  32. should use Mobile. Currently Compatibility is the recommended renderer for standalone headsets, and ironically Mobile is the recommended renderer for desktop.
  33. This is based on current XR performance on the different devices with each renderer.
  34. OpenXR
  35. ------
  36. OpenXR is a new industry standard that allows different XR platforms to present themselves through a standardised API to XR applications. This standard is an open standard maintained by the Khronos Group and thus aligns very well with Godot's interests.
  37. The Vulkan implementation of OpenXR is closely integrated with Vulkan, taking over part of the Vulkan system. This requires tight integration of certain core graphics features in the Vulkan renderer which are needed before the XR system is setup. This was one of the main deciding factors to include OpenXR as a core interface.
  38. This also means OpenXR needs to be enabled when Godot starts in order to set things up correctly. The required setting can be found in your project settings:
  39. .. image:: img/openxr_settings.png
  40. As setup is brought forward with OpenXR you can find several other settings related to OpenXR here as well. These can't be changed while your application is running.
  41. The default settings will get us started and we will go into detail in another section of the documentation.
  42. Setting up the XR scene
  43. -----------------------
  44. Every XR application needs at least an :ref:`XROrigin3D <class_xrorigin3d>` and an :ref:`XRCamera3D <class_xrcamera3d>` node. Most will have two :ref:`XRController3D <class_xrcontroller3d>`, one for the left hand and one for the right. Keep in mind that the camera and controller nodes should be children of the origin node. Add these nodes to a new scene and rename the controller nodes to ``LeftHand`` and ``RightHand``, your scene should look something like this:
  45. .. image:: img/xr_basic_scene.png
  46. Next you need to configure the controllers, select the left hand and set it up as follows:
  47. .. image:: img/xr_left_hand.png
  48. And the right hand:
  49. .. image:: img/xr_right_hand.png
  50. Right now all these nodes are on the floor, they will be positioned correctly in runtime. To help during development, it can be helpful to move the camera upwards so its ``y`` is set to ``1.7``, and move the controller nodes to ``-0.5, 1.0, -0.5`` and ``0.5, 1.0, -0.5`` for respectively the left and right hand.
  51. Next we need to add a script to our root node. Add the following code into this script:
  52. .. tabs::
  53. .. code-tab:: gdscript GDScript
  54. extends Node3D
  55. var xr_interface: XRInterface
  56. func _ready():
  57. xr_interface = XRServer.find_interface("OpenXR")
  58. if xr_interface and xr_interface.is_initialized():
  59. print("OpenXR initialized successfully")
  60. # Turn off v-sync!
  61. DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
  62. # Change our main viewport to output to the HMD
  63. get_viewport().use_xr = true
  64. else:
  65. print("OpenXR not initialized, please check if your headset is connected")
  66. .. code-tab:: csharp
  67. using Godot;
  68. public partial class MyNode3D : Node3D
  69. {
  70. private XRInterface _xrInterface;
  71. public override void _Ready()
  72. {
  73. _xrInterface = XRServer.FindInterface("OpenXR");
  74. if(_xrInterface != null && _xrInterface.IsInitialized())
  75. {
  76. GD.Print("OpenXR initialized successfully");
  77. // Turn off v-sync!
  78. DisplayServer.WindowSetVsyncMode(DisplayServer.VSyncMode.Disabled);
  79. // Change our main viewport to output to the HMD
  80. GetViewport().UseXR = true;
  81. }
  82. else
  83. {
  84. GD.Print("OpenXR not initialized, please check if your headset is connected");
  85. }
  86. }
  87. }
  88. This code fragment assumes we are using OpenXR, if you wish to use any of the other interfaces you can change the ``find_interface`` call.
  89. .. warning::
  90. As you can see in the code snippet above, we turn off v-sync.
  91. When using OpenXR you are outputting the rendering results to an HMD that often requires us to run at 90Hz or higher.
  92. If your monitor is a 60hz monitor and v-sync is turned on, you will limit the output to 60 frames per second.
  93. XR interfaces like OpenXR perform their own sync.
  94. Also note that by default the physics engine runs at 60Hz as well and this can result in choppy physics.
  95. You should set ``Engine.physics_ticks_per_second`` to a higher value.
  96. If you run your project at this point in time, everything will work but you will be in a dark world. So to finish off our starting point add a :ref:`DirectionalLight3D <class_directionallight3d>` and a :ref:`WorldEnvironment <class_worldenvironment>` node to your scene.
  97. You may wish to also add a mesh instance as a child to each controller node just to temporarily visualise them.
  98. Make sure you configure a sky in your world environment.
  99. Now run your project, you should be floating somewhere in space and be able to look around.
  100. .. note::
  101. While traditional level switching can definitely be used with XR applications, where this scene setup is repeated in each level, most find it easier to set this up once and loading levels as a subscene. If you do switch scenes and replicate the XR setup in each one, do make sure you do not run ``initialize`` multiple times. The effect can be unpredictable depending on the XR interface used.
  102. For the rest of this basic tutorial series we will create a game that uses a single scene.