openxr_composition_layers.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. .. _doc_openxr_composition_layers:
  2. OpenXR composition layers
  3. =========================
  4. Introduction
  5. ------------
  6. In XR games you generally want to create user interactions that happen in 3D space
  7. and involve users touching objects as if they are touching them in real life.
  8. Sometimes however creating a more traditional 2D interface is unavoidable.
  9. In XR however you can't just add 2D components to your scene.
  10. Godot needs depth information to properly position these elements so they appear at
  11. a comfortable place for the user.
  12. Even with depth information there are headsets with slanted displays that make it impossible
  13. for the standard 2D pipeline to correctly render the 2D elements.
  14. The solution then is to render the UI to a :ref:`SubViewport <class_subviewport>`
  15. and display the result of this using a :ref:`ViewportTexture <class_viewporttexture>` on a 3D mesh.
  16. The :ref:`QuadMesh <class_quadmesh>` is a suitable option for this.
  17. .. note::
  18. See the `GUI in 3D <https://github.com/godotengine/godot-demo-projects/tree/master/viewport/gui_in_3d>`_
  19. example project for an example of this approach.
  20. The problem with displaying the viewport in this way is that the rendered result
  21. is sampled for lens distortion by the XR runtime and the resulting quality loss
  22. can make UI text hard to read.
  23. OpenXR offers a solution to this problem through composition layers.
  24. With composition layers it is possible for the contents of a viewport to be projected
  25. on a surface after lens distortion resulting in a much higher quality end result.
  26. .. note::
  27. As not all XR runtimes support all composition layer types,
  28. Godot implements a fallback solution where we render the viewport
  29. as part of the normal scene but with the aforementioned quality
  30. limitations.
  31. .. warning::
  32. When the composition layer is supported,
  33. it is the XR runtime that presents the subviewport.
  34. This means the UI is only visible in the headset,
  35. it will not be accessible by Godot and will thus
  36. not be shown when you have a spectator view on the desktop.
  37. There are currently 3 nodes that expose this functionality:
  38. - :ref:`OpenXRCompositionLayerCylinder <class_OpenXRCompositionLayerCylinder>` shows the contents of the SubViewport on the inside of a cylinder (or "slice" of a cylinder).
  39. - :ref:`OpenXRCompositionLayerEquirect <class_OpenXRCompositionLayerEquirect>` shows the contents of the SubViewport on the interior of a sphere (or "slice" of a sphere).
  40. - :ref:`OpenXRCompositionLayerQuad <class_OpenXRCompositionLayerQuad>` shows the contents of the SubViewport on a flat rectangle.
  41. Setting up the SubViewport
  42. --------------------------
  43. The first step is adding a SubViewport for our 2D UI,
  44. this doesn't require any specific steps.
  45. For our example we do mark the viewport as transparent.
  46. You can now create the 2D UI by adding child nodes to the SubViewport as you normally would.
  47. It is advisable to save the 2D UI in a subscene, this makes it easier to do your layout.
  48. .. image:: img/openxr_composition_layer_subviewport.webp
  49. .. warning::
  50. The update mode "When Visible" will not work as Godot can't determine whether
  51. the viewport is visible to the user.
  52. When assigning our viewport to a composition layer Godot will automatically adjust this.
  53. Adding a composition layer
  54. --------------------------
  55. The second step is adding our composition layer.
  56. We can add the correct composition layer node as a child node of
  57. our :ref:`XROrigin3D <class_xrorigin3d>` node.
  58. This is very important as the XR runtime positions everything in relation to our origin.
  59. We want to position the composition layer so it is at eye height and roughly 1 to 1.5 meters
  60. away from the player.
  61. We now assign the SubViewport to the ``Layer Viewport`` property and enable Alpha Blend.
  62. .. image:: img/openxr_composition_layer_quad.webp
  63. .. note::
  64. As the player can walk away from the origin point,
  65. you will want to reposition the composition layer when the player recenters the view.
  66. Using the reference space ``Local Floor`` will apply this logic automatically.
  67. Making the interface work
  68. -------------------------
  69. So far we're only displaying our UI, to make it work we need to add some code.
  70. For this example we're going to keep things simple and
  71. make one of the controllers work as a pointer.
  72. We'll then simulate mouse actions with this pointer.
  73. This code also requires a ``MeshInstance3D`` node called ``Pointer`` to be added
  74. as a child to our ``OpenXRCompositionLayerQuad`` node.
  75. We configure a ``SphereMesh`` with a radius ``0.01`` meters.
  76. We'll be using this as a helper to visualize where the user is pointing.
  77. The main function that drives this functionality is the ``intersects_ray``
  78. function on our composition layer node.
  79. This function takes the global position and orientation of our pointer and returns
  80. the UV where our ray intersects our viewport.
  81. It returns ``Vector2(-1.0, -1.0)`` if we're not pointing at our viewport.
  82. We start with setting up some variables, important here are the export variables
  83. which identify our controller node with which we point to our screen.
  84. .. code:: gdscript
  85. extends OpenXRCompositionLayerQuad
  86. const NO_INTERSECTION = Vector2(-1.0, -1.0)
  87. @export var controller : XRController3D
  88. @export var button_action : String = "trigger_click"
  89. var was_pressed : bool = false
  90. var was_intersect : Vector2 = NO_INTERSECTION
  91. ...
  92. Next we define a helper function that takes the value returned from ``intersects_ray``
  93. and gives us the global position for that intersection point.
  94. This implementation only works for our ``OpenXRCompositionLayerQuad`` node.
  95. .. code:: gdscript
  96. ...
  97. func _intersect_to_global_pos(intersect : Vector2) -> Vector3:
  98. if intersect != NO_INTERSECTION:
  99. var local_pos : Vector2 = (intersect - Vector2(0.5, 0.5)) * quad_size
  100. return global_transform * Vector3(local_pos.x, -local_pos.y, 0.0)
  101. else:
  102. return Vector3()
  103. ...
  104. We also define a helper function that takes our ``intersect`` value and
  105. returns our location in the viewports local coordinate system:
  106. .. code:: gdscript
  107. ...
  108. func _intersect_to_viewport_pos(intersect : Vector2) -> Vector2i:
  109. if layer_viewport and intersect != NO_INTERSECTION:
  110. var pos : Vector2 = intersect * Vector2(layer_viewport.size)
  111. return Vector2i(pos)
  112. else:
  113. return Vector2i(-1, -1)
  114. ...
  115. The main logic happens in our ``_process`` function.
  116. Here we start by hiding our pointer,
  117. we then check if we have a valid controller and viewport,
  118. and we call ``intersects_ray`` with the position and orientation of our controller:
  119. .. code:: gdscript
  120. ...
  121. # Called every frame. 'delta' is the elapsed time since the previous frame.
  122. func _process(_delta):
  123. # Hide our pointer, we'll make it visible if we're interacting with the viewport.
  124. $Pointer.visible = false
  125. if controller and layer_viewport:
  126. var controller_t : Transform3D = controller.global_transform
  127. var intersect : Vector2 = intersects_ray(controller_t.origin, -controller_t.basis.z)
  128. ...
  129. Next we check if we're intersecting with our viewport.
  130. If so, we check if our button is pressed and place our pointer at our intersection point.
  131. .. code:: gdscript
  132. ...
  133. if intersect != NO_INTERSECTION:
  134. var is_pressed : bool = controller.is_button_pressed(button_action)
  135. # Place our pointer where we're pointing
  136. var pos : Vector3 = _intersect_to_global_pos(intersect)
  137. $Pointer.visible = true
  138. $Pointer.global_position = pos
  139. ...
  140. If we were intersecting in our previous process call and our pointer has moved,
  141. we prepare a :ref:`InputEventMouseMotion <class_InputEventMouseMotion>` object
  142. to simulate our mouse moving and send that to our viewport for further processing.
  143. .. code:: gdscript
  144. ...
  145. if was_intersect != NO_INTERSECTION and intersect != was_intersect:
  146. # Pointer moved
  147. var event : InputEventMouseMotion = InputEventMouseMotion.new()
  148. var from : Vector2 = _intersect_to_viewport_pos(was_intersect)
  149. var to : Vector2 = _intersect_to_viewport_pos(intersect)
  150. if was_pressed:
  151. event.button_mask = MOUSE_BUTTON_MASK_LEFT
  152. event.relative = to - from
  153. event.position = to
  154. layer_viewport.push_input(event)
  155. ...
  156. If we've just released our button we also prepare
  157. a :ref:`InputEventMouseButton <class_InputEventMouseButton>` object
  158. to simulate a button release and send that to our viewport for further processing.
  159. .. code:: gdscript
  160. ...
  161. if not is_pressed and was_pressed:
  162. # Button was let go?
  163. var event : InputEventMouseButton = InputEventMouseButton.new()
  164. event.button_index = 1
  165. event.pressed = false
  166. event.position = _intersect_to_viewport_pos(intersect)
  167. layer_viewport.push_input(event)
  168. ...
  169. Or if we've just pressed our button we prepare
  170. a :ref:`InputEventMouseButton <class_InputEventMouseButton>` object
  171. to simulate a button press and send that to our viewport for further processing.
  172. .. code:: gdscript
  173. ...
  174. elif is_pressed and not was_pressed:
  175. # Button was pressed?
  176. var event : InputEventMouseButton = InputEventMouseButton.new()
  177. event.button_index = 1
  178. event.button_mask = MOUSE_BUTTON_MASK_LEFT
  179. event.pressed = true
  180. event.position = _intersect_to_viewport_pos(intersect)
  181. layer_viewport.push_input(event)
  182. ...
  183. Next we remember our state for next frame.
  184. .. code:: gdscript
  185. ...
  186. was_pressed = is_pressed
  187. was_intersect = intersect
  188. ...
  189. Finally, if we aren't intersecting, we clear our state.
  190. .. code:: gdscript
  191. ...
  192. else:
  193. was_pressed = false
  194. was_intersect = NO_INTERSECTION
  195. Hole punching
  196. -------------
  197. As the composition layer is composited on top of the render result,
  198. it can be rendered in front of objects that are actually forward of the viewport.
  199. By enabling hole punch you instruct Godot to render a transparent object
  200. where our viewport is displayed.
  201. It does this in a way that fills the depth buffer and clears the current rendering result.
  202. Anything behind our viewport will now be cleared,
  203. while anything in front of our viewport will be rendered as usual.
  204. You also need to set ``Sort Order`` to a negative value,
  205. the XR compositor will now draw the viewport first, and then overlay our rendering result.
  206. .. figure:: img/openxr_composition_layer_hole_punch.webp
  207. :align: center
  208. Use case showing how the users hand is incorrectly obscured
  209. by a composition layer when hole punching is not used.