xr_primer.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. .. _doc_xr_primer:
  2. AR/VR primer
  3. ============
  4. This tutorial gives you a springboard into the world of AR and VR in the Godot game engine.
  5. A new architecture was introduced in Godot 3 called the AR/VR Server. On top of this
  6. architecture, specific implementations are available as interfaces, most of which are plugins
  7. based on GDNative. This tutorial focuses purely on the core elements abstracted by the core
  8. architecture. This architecture has enough features for you to create an entire VR experience
  9. that can then be deployed for various interfaces. However, each platform often has some unique
  10. features that are impossible to abstract. Such features will be documented on the relevant
  11. interfaces and fall outside of the scope of this primer.
  12. AR/VR server
  13. ------------
  14. When Godot starts, each available interface will make itself known to the AR/VR server.
  15. GDNative interfaces are setup as singletons; as long as they are added to the list of
  16. GDNative singletons in your project, they will make themselves known to the server.
  17. You can use the function :ref:`get_interfaces() <class_ARVRServer_method_get_interfaces>`
  18. to return a list of available interfaces, but for this tutorial, we're going to use the
  19. :ref:`native mobile VR interface <class_MobileVRInterface>` in our examples. This interface
  20. is a straightforward implementation that uses the 3DOF sensors on your phone for orientation
  21. and outputs a stereoscopic image to the screen. It is also available in the Godot core and
  22. outputs to screen on desktop, which makes it ideal for prototyping or a tutorial such as
  23. this one.
  24. To enable an interface, you execute the following code:
  25. .. tabs::
  26. .. code-tab:: gdscript GDScript
  27. var arvr_interface = ARVRServer.find_interface("Native mobile")
  28. if arvr_interface and arvr_interface.initialize():
  29. get_viewport().arvr = true
  30. .. code-tab:: csharp
  31. var arvrInterface = ARVRServer.FindInterface("Native mobile");
  32. if (arvrInterface != null && arvrInterface.Initialize())
  33. {
  34. GetViewport().Arvr = true;
  35. }
  36. This code finds the interface we wish to use, initializes it and, if that is successful, binds
  37. the main viewport to the interface. This last step gives some control over the viewport to the
  38. interface, which automatically enables things like stereoscopic rendering on the viewport.
  39. For our mobile VR interface, and any interface where the main input is directly displayed on
  40. screen, the main viewport needs to be the viewport where :ref:`arvr<class_Viewport_property_arvr>`
  41. is set to ``true``. But for interfaces that render on an externally attached device, you can use
  42. a secondary viewport. In the latter case, a viewport that shows its output on screen will show an
  43. undistorted version of the left eye, while showing the fully processed stereoscopic output on the
  44. device.
  45. Finally, you should only initialize an interface once; switching scenes and reinitializing interfaces
  46. will just introduce a lot of overhead. If you want to turn the headset off temporarily, just disable
  47. the viewport or set :ref:`arvr<class_Viewport_property_arvr>` to ``false`` on the viewport. In most
  48. scenarios though, you wouldn't disable the headset once you're in VR, this can be disconcerting to
  49. the gamer.
  50. New AR/VR nodes
  51. ---------------
  52. Three new node types have been added for supporting AR and VR in Godot and one additional
  53. node type especially for AR. These are:
  54. * :ref:`ARVROrigin <class_ARVROrigin>` - our origin point in the world
  55. * :ref:`ARVRCamera <class_ARVRCamera>` - a special subclass of the camera, which is positionally tracked
  56. * :ref:`ARVRController <class_ARVRController>` - a new spatial class, which tracks the location of a controller
  57. * :ref:`ARVRAnchor <class_ARVRAnchor>` - an anchor point for an AR implementation mapping a real world location into your virtual world
  58. The first two must exist in your scene for AR/VR to work and this tutorial focuses purely
  59. on them.
  60. :ref:`ARVROrigin <class_ARVROrigin>` is an important node, you must have one and only one
  61. of these somewhere in your scene. This node maps the center of your real world tracking
  62. space to a location in your virtual world. Everything else is positionally tracked in
  63. relation to this point. Where this point lies exactly differs from one implementation to
  64. another, but the best example to understand how this node works is to take a look at a room
  65. scale location. While we have functions to adjust the point to center it on the player by
  66. default, the origin point will be the center location of the room you are in. As you
  67. physically walk around the room, the location of the HMD is tracked in relation to this
  68. center position and the tracking is mirror in the virtual world.
  69. To keep things simple, when you physically move around your room, the ARVR Origin point stays
  70. where it is, the position of the camera and controllers will be adjusted according to your
  71. movements. When you move through the virtual world, either through controller input or when
  72. you implement a teleport system, it is the position of the origin point which you will
  73. have to adjust.
  74. :ref:`ARVRCamera <class_ARVRCamera>` is the second node that must always be a part of your
  75. scene and it must always be a child node of your origin node. It is a subclass of Godot's
  76. normal camera. However, its position is automatically updated each frame based on the physical
  77. orientation and position of the HMD. Also due to the precision required for rendering to an
  78. HMD or rendering an AR overlay over a real world camera, most of the standard camera properties
  79. are ignored. The only properties of the camera that are used are the near and far plane
  80. settings. The FOV, aspect ratio and projection mode are all ignored.
  81. Note that, for our native mobile VR implementation, there is no positional tracking, only
  82. the orientation of the phone and by extension, the HMD is tracked. This implementation
  83. artificially places the camera at a height (Y) of 1.85.
  84. Conclusion: your minimum setup in your scene to make AR or VR work should look like this:
  85. .. image:: img/minimum_setup.png
  86. And that's all you need to get started with the native mobile interface. Obviously, you need
  87. to add something more into your scene, so there is something to see, but after that, you can
  88. export the game to your phone of choice, pop it into a viewer and away you go.
  89. Official plugins and resources
  90. ------------------------------
  91. As mentioned earlier, Godot does not support the various VR and AR SDKs out of the box, you
  92. need a plugin for the specific SDK you want to use. There are several official plugins available
  93. in the `GodotVR Repository <https://github.com/GodotVR>`__.
  94. * `Godot OpenXR <https://github.com/GodotVR/godot_openxr>`_: This is the **official XR plugin**
  95. starting with Godot **3.4**. It supports OpenXR, an open standard for designing and building
  96. cross-platform VR and AR software.
  97. Tested with SteamVR, Monada and Oculus OpenXR (desktop and mobile) runtimes.
  98. * See :ref:`doc_openxr_introduction`.
  99. * `Godot Oculus Mobile <https://github.com/GodotVR/godot_oculus_mobile>`_ provides :ref:`support for
  100. the Meta Quest <doc_developing_for_oculus_quest>`.
  101. * **Note**: This plugin has been deprecated starting with Godot 3.4.
  102. We recommend migrating to the `Godot OpenXR <https://github.com/GodotVR/godot_openxr>`_ plugin instead.
  103. * `Godot OpenVR <https://github.com/GodotVR/godot_openvr>`_ (not to be confused with OpenXR)
  104. supports the OpenVR SDK used by Steam.
  105. * `Godot Oculus <https://github.com/GodotVR/godot_oculus>`__ supports the Oculus SDK
  106. (desktop headsets only).
  107. * **Note**: This plugin has been deprecated starting with Godot 3.4.
  108. We recommend migrating to the `Godot OpenXR <https://github.com/GodotVR/godot_openxr>`_ plugin instead.
  109. * `Godot OpenHMD <https://github.com/GodotVR/godot_openhmd>`_ supports OpenHMD, an open source
  110. API and drivers for headsets.
  111. These plugins can be downloaded from GitHub or the Godot Asset Library.
  112. In addition to the plugins, there are several official demos.
  113. * `Godot Oculus Demo <https://github.com/GodotVR/godot-oculus-demo>`__.
  114. * `Godot OpenVR FPS <https://github.com/GodotVR/godot_openvr_fps>`__ (the tutorial for this project
  115. is :ref:`doc_vr_starter_tutorial_part_one`).
  116. * `Godot XR tools <https://github.com/GodotVR/godot-xr-tools>`__, which shows implementations for VR
  117. features such as movement and picking up objects.
  118. Other things to consider
  119. ------------------------
  120. There are a few other subjects that we need to briefly touch upon in this primer that are important
  121. to know.
  122. The first are our units. In normal 3D games, you don't have to think a lot about units. As long as
  123. everything is at the same scale, a box sized 1 unit by 1 unit by 1 unit can be any size from a cub
  124. you can hold in your hand to something the size of a building. In AR and VR, this changes because
  125. things in your virtual world are mapped to things in the real world. If you step 1 meter forward in
  126. the real world, but you only move 1 cm forward in your virtual world, you have a problem. The same
  127. with the position of your controllers; if they don't appear in the right relative space, it breaks
  128. the immersion for the player. Most VR platforms, including our AR/VR Server, assume that 1 unit = 1
  129. meter. The AR/VR server, however, has a property that, for convenience, is also exposed on the
  130. ARVROrigin node called world scale. For instance, setting this to a value of 10 changes our coordinate
  131. system so 10 units = 1 meter.
  132. Performance is another thing that needs to be carefully considered. Especially VR taxes your game
  133. a lot more than most people realize. For mobile VR, you have to be extra careful here, but even for
  134. desktop games, there are three factors that make life extra difficult:
  135. * You are rendering stereoscopic, two for the price of one. While not exactly doubling the work load
  136. and with things in the pipeline such as supporting the new MultiView OpenGL extension in mind, there
  137. still is an extra workload in rendering images for both eyes
  138. * A normal game will run acceptably on 30fps and ideally manages 60fps. That gives you a big range to
  139. play with between lower end and higher end hardware. For any HMD application of AR or VR, however,
  140. 60fps is the absolute minimum and you should target your games to run at a stable 90fps to ensure your
  141. users don't get motion sickness right off the bat.
  142. * The high FOV and related lens distortion effect require many VR experiences to render at double the
  143. resolution. Yes a VIVE may only have a resolution of 1080x1200 per eye, we're rendering each eye at
  144. 2160x2400 as a result. This is less of an issue for most AR applications.
  145. All in all, the workload your GPU has in comparison with a normal 3D game is a fair amount
  146. higher. While things are in the pipeline to improve this, such as MultiView and foveated rendering,
  147. these aren't supported on all devices. This is why you see many VR games using a more art style
  148. and if you pay close attention to those VR games that go for realism, you'll probably notice they're
  149. a bit more conservative on the effects or use some good old optical trickery.