OpenXR composition layers

前言

In XR games you generally want to create user interactions that happen in 3D space and involve users touching objects as if they are touching them in real life.

Sometimes however creating a more traditional 2D interface is unavoidable. In XR however you can’t just add 2D components to your scene. Godot needs depth information to properly position these elements so they appear at a comfortable place for the user. Even with depth information there are headsets with slanted displays that make it impossible for the standard 2D pipeline to correctly render the 2D elements.

The solution then is to render the UI to a SubViewport and display the result of this using a ViewportTexture on a 3D mesh. The QuadMesh is a suitable option for this.

备注

See the GUI in 3D example project for an example of this approach.

The problem with displaying the viewport in this way is that the rendered result is sampled for lens distortion by the XR runtime and the resulting quality loss can make UI text hard to read.

OpenXR offers a solution to this problem through composition layers. With composition layers it is possible for the contents of a viewport to be projected on a surface after lens distortion resulting in a much higher quality end result.

备注

As not all XR runtimes support all composition layer types, Godot implements a fallback solution where we render the viewport as part of the normal scene but with the aforementioned quality limitations.

警告

When the composition layer is supported, it is the XR runtime that presents the subviewport. This means the UI is only visible in the headset, it will not be accessible by Godot and will thus not be shown when you have a spectator view on the desktop.

There are currently 3 nodes that expose this functionality:

设置 SubViewport

The first step is adding a SubViewport for our 2D UI, this doesn’t require any specific steps. For our example we do mark the viewport as transparent.

You can now create the 2D UI by adding child nodes to the SubViewport as you normally would. It is advisable to save the 2D UI in a subscene, this makes it easier to do your layout.

../../_images/openxr_composition_layer_subviewport.webp

警告

The update mode “When Visible” will not work as Godot can’t determine whether the viewport is visible to the user. When assigning our viewport to a composition layer Godot will automatically adjust this.

Adding a composition layer

The second step is adding our composition layer. We simply add the correct composition layer node as a child node of our XROrigin3D node. This is very important as the XR runtime positions everything in relation to our origin.

We want to position the composition layer so it is at eye height and roughly 1 to 1.5 meters away from the player.

We now assign the SubViewport to the Layer Viewport property and enable Alpha Blend.

../../_images/openxr_composition_layer_quad.webp

备注

As the player can walk away from the origin point, you will want to reposition the composition layer when the player recenters the view. Using the reference space Local Floor will apply this logic automatically.

Making the interface work

So far we’re only displaying our UI, to make it work we need to add some code. For this example we’re going to keep things simple and make one of the controllers work as a pointer. We’ll then simulate mouse actions with this pointer.

This code also requires a MeshInstance3D node called Pointer to be added as a child to our OpenXRCompositionLayerQuad node. We configure a SphereMesh with a radius 0.01 meters. We’ll be using this as a helper to visualize where the user is pointing.

The main function that drives this functionality is the intersects_ray function on our composition layer node. This function takes the global position and orientation of our pointer and returns the UV where our ray intersects our viewport. It returns Vector2(-1.0, -1.0) if we’re not pointing at our viewport.

We start with setting up some variables, important here are the export variables which identify our controller node with which we point to our screen.

  1. extends OpenXRCompositionLayerQuad
  2. const NO_INTERSECTION = Vector2(-1.0, -1.0)
  3. @export var controller : XRController3D
  4. @export var button_action : String = "trigger_click"
  5. var was_pressed : bool = false
  6. var was_intersect : Vector2 = NO_INTERSECTION
  7. ...

Next we define a helper function that takes the value returned from intersects_ray and gives us the global position for that intersection point. This implementation only works for our OpenXRCompositionLayerQuad node.

  1. ...
  2. func _intersect_to_global_pos(intersect : Vector2) -> Vector3:
  3. if intersect != NO_INTERSECTION:
  4. var local_pos : Vector2 = (intersect - Vector2(0.5, 0.5)) * quad_size
  5. return global_transform * Vector3(local_pos.x, -local_pos.y, 0.0)
  6. else:
  7. return Vector3()
  8. ...

We also define a helper function that takes our intersect value and returns our location in the viewports local coordinate system:

  1. ...
  2. func _intersect_to_viewport_pos(intersect : Vector2) -> Vector2i:
  3. if layer_viewport and intersect != NO_INTERSECTION:
  4. var pos : Vector2 = intersect * Vector2(layer_viewport.size)
  5. return Vector2i(pos)
  6. else:
  7. return Vector2i(-1, -1)
  8. ...

The main logic happens in our _process function. Here we start by hiding our pointer, we then check if we have a valid controller and viewport, and we call intersects_ray with the position and orientation of our controller:

  1. ...
  2. # Called every frame. 'delta' is the elapsed time since the previous frame.
  3. func _process(_delta):
  4. # Hide our pointer, we'll make it visible if we're interacting with the viewport.
  5. $Pointer.visible = false
  6. if controller and layer_viewport:
  7. var controller_t : Transform3D = controller.global_transform
  8. var intersect : Vector2 = intersects_ray(controller_t.origin, -controller_t.basis.z)
  9. ...

Next we check if we’re intersecting with our viewport. If so, we check if our button is pressed and place our pointer at our intersection point.

  1. ...
  2. if intersect != NO_INTERSECTION:
  3. var is_pressed : bool = controller.is_button_pressed(button_action)
  4. # Place our pointer where we're pointing
  5. var pos : Vector3 = _intersect_to_global_pos(intersect)
  6. $Pointer.visible = true
  7. $Pointer.global_position = pos
  8. ...

If we were intersecting in our previous process call and our pointer has moved, we prepare a InputEventMouseMotion object to simulate our mouse moving and send that to our viewport for further processing.

  1. ...
  2. if was_intersect != NO_INTERSECTION and intersect != was_intersect:
  3. # Pointer moved
  4. var event : InputEventMouseMotion = InputEventMouseMotion.new()
  5. var from : Vector2 = _intersect_to_viewport_pos(was_intersect)
  6. var to : Vector2 = _intersect_to_viewport_pos(intersect)
  7. if was_pressed:
  8. event.button_mask = MOUSE_BUTTON_MASK_LEFT
  9. event.relative = to - from
  10. event.position = to
  11. layer_viewport.push_input(event)
  12. ...

If we’ve just released our button we also prepare a InputEventMouseButton object to simulate a button release and send that to our viewport for further processing.

  1. ...
  2. if not is_pressed and was_pressed:
  3. # Button was let go?
  4. var event : InputEventMouseButton = InputEventMouseButton.new()
  5. event.button_index = 1
  6. event.pressed = false
  7. event.position = _intersect_to_viewport_pos(intersect)
  8. layer_viewport.push_input(event)
  9. ...

Or if we’ve just pressed our button we prepare a InputEventMouseButton object to simulate a button press and send that to our viewport for further processing.

  1. ...
  2. elif is_pressed and not was_pressed:
  3. # Button was pressed?
  4. var event : InputEventMouseButton = InputEventMouseButton.new()
  5. event.button_index = 1
  6. event.button_mask = MOUSE_BUTTON_MASK_LEFT
  7. event.pressed = true
  8. event.position = _intersect_to_viewport_pos(intersect)
  9. layer_viewport.push_input(event)
  10. ...

Next we remember our state for next frame.

  1. ...
  2. was_pressed = is_pressed
  3. was_intersect = intersect
  4. ...

Finally, if we aren’t intersecting, we simply clear our state.

  1. ...
  2. else:
  3. was_pressed = false
  4. was_intersect = NO_INTERSECTION

Hole punching

As the composition layer is composited on top of the render result, it can be rendered in front of objects that are actually forward of the viewport.

By enabling hole punch you instruct Godot to render a transparent object where our viewport is displayed. It does this in a way that fills the depth buffer and clears the current rendering result. Anything behind our viewport will now be cleared, while anything in front of our viewport will be rendered as usual.

You also need to set Sort Order to a negative value, the XR compositor will now draw the viewport first, and then overlay our rendering result.

../../_images/openxr_composition_layer_hole_punch.webp

Use case showing how the users hand is incorrectly obscured by a composition layer when hole punching is not used.