Skip to content

Commit 8b4ffc0

Browse files
committed
Add documentation about OpenXR render models
1 parent b2de954 commit 8b4ffc0

File tree

3 files changed

+267
-0
lines changed

3 files changed

+267
-0
lines changed
10.5 KB
Binary file not shown.

tutorials/xr/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Advanced topics
3333
openxr_composition_layers
3434
openxr_hand_tracking
3535
openxr_body_tracking
36+
openxr_render_models
3637

3738
Godot XR Tools
3839
--------------

tutorials/xr/openxr_render_models.rst

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
.. _doc_openxr_render_models:
2+
3+
OpenXR Render Models
4+
====================
5+
6+
A cornerstone in OpenXRs API design is being as platform agnostic as possible.
7+
A great example of this is OpenXRs action map system where XR runtimes
8+
have to support core interaction profiles to fall back on
9+
if no interaction profile exists for the hardware being used.
10+
This ensures that OpenXR applications keep functioning even when used on
11+
hardware that didn't exist when the application was released,
12+
or that the developers of the application do not have access too.
13+
14+
A consequence of this is that the application developer doesn't know with any
15+
certainty what hardware the user is using, as the XR runtime could be mimicking
16+
other hardware.
17+
The application developer thus can't show anything in relation to the actual
18+
hardware used, the most common use case being showing the controllers the user
19+
is currently holding.
20+
21+
Showing the correct controller models and having these models
22+
correctly positioned is important to a proper sense of immersion.
23+
24+
This is where OpenXRs `render models API <https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_EXT_render_models>`_ comes in.
25+
This API allows us to query the XR runtime for 3D assets that are correct
26+
for the physical hardware the user is using.
27+
The API also allows us to query the position of this hardware within the
28+
tracking volume and the correct positioning of subcomponents of this hardware.
29+
30+
For instance, we can correctly position and animate the trigger or show buttons
31+
being pressed.
32+
33+
For those runtimes that support the
34+
`controller data source for hand tracking <https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_EXT_hand_tracking_data_source>`_
35+
, we can also correctly position the users fingers and hand according to the
36+
shape of the controller.
37+
Do note that this works in combination with the
38+
`hand joints motion range extension <https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_EXT_hand_joints_motion_range>`_
39+
to prevent clipping of the fingers.
40+
41+
OpenXR Render models node
42+
-------------------------
43+
The :ref:`OpenXRRenderModelManager<class_OpenXRRenderModelManager>`
44+
node can be used to automate most of the render models functionality.
45+
This node keeps track of the active render models currently made
46+
available by the XR runtime.
47+
48+
It will create child nodes for each active render model resulting in
49+
that render model being displayed.
50+
51+
This node must have a :ref:`XROrigin3D<class_XROrigin3D>` node as an
52+
ancestor.
53+
54+
If ``tracker`` is set to ``Any`` our node will show all render models
55+
currently being tracked. In this scenario this node must be a direct
56+
child of our :ref:`XROrigin3D<class_XROrigin3D>` node.
57+
58+
If ``tracker`` is set to ``None set`` our node will only show render
59+
models for which no tracker has been identified. In this scenario this
60+
node must also be a direct child of our
61+
:ref:`XROrigin3D<class_XROrigin3D>` node.
62+
63+
If ``tracker`` is set to ``Left Hand`` or ``Right Hand`` our node will
64+
only show render models related to resp. our left hand or our right hand.
65+
In this scenario our node can be placed deeper in the scene tree.
66+
67+
.. warning::
68+
For most XR runtimes this means the render model represents a controller
69+
that is actually being held by the user but this is not a guarantee.
70+
Some XR runtimes will always set the tracker to either the left or right
71+
hand even if the controller is not currently held but is being tracked.
72+
You should always test this as this will lead to unwanted behavior.
73+
74+
In this scenario we can also specify an action for a pose in the action map
75+
by setting the ``make_local_to_pose`` to the pose action.
76+
Use this in combination with a :ref:`XRController3D<class_XRController3D>`
77+
node that is using the same pose and you can now add a layer that allows
78+
you to deviate from the tracked position of both your controller and the
79+
related render model (see example below).
80+
81+
.. note::
82+
Combining the above with hand tracking does introduce the problem
83+
that hand tracking is completely independent from the action map
84+
system. You will need to combine the hand tracking and controller
85+
tracking poses to properly offset the render models.
86+
87+
This falls beyond the scope of this document but an example of
88+
this implementation can be found in
89+
`our hand tracking demo <https://github.com/godotengine/godot-demo-projects/tree/master/xr/openxr_hand_tracking_demo>`_.
90+
91+
**Need to submit a PR to the hand tracking demo for this implementation once render models is merged**
92+
93+
Render models example
94+
~~~~~~~~~~~~~~~~~~~~~
95+
96+
.. image:: img/openxr_render_models_setup.webp
97+
98+
In this setup we find an :ref:`OpenXRRenderModelManager<class_OpenXRRenderModelManager>`
99+
node directly underneath our :ref:`XROrigin3D<class_XROrigin3D>` node.
100+
On this node our ``target`` property is set to ``None set`` and will handle
101+
showing all render models that are currently not related to our left hand
102+
or right hand controllers.
103+
104+
We then see the same setup for our left and right hand so we'll focus on
105+
just the left hand.
106+
107+
We have a :ref:`XRController3D<class_XRController3D>` that will track the
108+
location of our hand.
109+
110+
.. note::
111+
We are using the ``grip`` pose in this example. The ``palm`` pose is
112+
arguably more suitable and predictable however it is not supported
113+
by all XR runtimes. See the hand tracking demo project for a
114+
solution on switching between these poses based on what is supported.
115+
116+
As a child of the node we have a :ref:`AnimatableBody3D<class_AnimatableBody3D>`
117+
node that follows the tracked location of the hand **but** will interact
118+
with physics objects to stop the players hand from going through walls etc.
119+
This node has a collision shape that encapsulates the hand.
120+
121+
.. note::
122+
It is important to set the physics priority such that this logic runs
123+
after any physics logic that moves the XROrigin3D node or the hand
124+
will lag a frame behind.
125+
126+
The script below shows a basic implementation for this that you can build
127+
upon.
128+
129+
.. code-block:: gdscript
130+
131+
class_name CollisionHands3D
132+
extends AnimatableBody3D
133+
134+
func _ready():
135+
# Make sure these are set correctly
136+
top_level = true
137+
sync_to_physics = false
138+
process_physics_priority = -90
139+
140+
func _physics_process(_delta):
141+
# Follow our parent node around
142+
var dest_transform = get_parent().global_transform
143+
144+
# We just apply rotation for this example
145+
global_basis = dest_transform.basis
146+
147+
# Attempt to move to where our tracked hand is
148+
move_and_collide(dest_transform.origin - global_position)
149+
150+
151+
Finally we see another :ref:`OpenXRRenderModelManager<class_OpenXRRenderModelManager>`
152+
node, this one with ``target`` set to the appropriate hand and
153+
``make_local_to_pose`` set to the correct pose.
154+
This will ensure that the render models related to this hand are properly
155+
shown and offset if our collision handler has altered the location.
156+
157+
**Add video or animation to see this in practice**
158+
159+
Render model node
160+
-----------------
161+
162+
The :ref:`OpenXRRenderModel<class_OpenXRRenderModel>` node implements
163+
all the logic to display and position a given render model provided by
164+
the render models API.
165+
166+
Instances of this node are added to the render models node we used up
167+
above but you can interact with these directly if you wish.
168+
169+
Whenever Godot obtains information about a new render model a RID is
170+
created to reference that render model.
171+
172+
By assigning that RID to the ``render_model`` property on this node,
173+
the node will start displaying the render model and manage both the
174+
transform that places the render model in the correct place and
175+
animates all the sub objects.
176+
177+
The function ``get_top_level_path`` will return the top level path
178+
associated with this render model. This will point to either the
179+
left or right hand. As the top level path can be set or cleared
180+
depending on whether the user picks up, or puts down, the controller
181+
you an connect the ``render_model_top_level_path_changes`` signal
182+
and react to these changes.
183+
184+
Depending on your setup of the
185+
:ref:`OpenXRRenderModels<class_OpenXRRenderModels>` nodes, render
186+
models will be removed or added as their top level path changes.
187+
188+
Backend access
189+
--------------
190+
191+
The nodes we've detailed out above handle all the display logic
192+
for us but it is possible to interact with the data that drives
193+
this directly and create your own implementation.
194+
195+
For this you can access the
196+
:ref:`OpenXRRenderModelExtension<class_OpenXRRenderModelExtension>`
197+
singleton.
198+
199+
This object also lets you query whether render models are
200+
supported and enabled on the device currently being used by
201+
calling the ``is_active`` function on this object.
202+
203+
The built-in logic implements the
204+
`interaction render model API <https://registry.khronos.org/OpenXR/specs/1.1/html/xrspec.html#XR_EXT_interaction_render_model>`_
205+
that lists all render models related to controllers and similar
206+
devices that are present in the action map.
207+
It will automatically create and remove render model entities
208+
that are exposed through this API.
209+
210+
As other extensions become available these can be implemented
211+
in a GDExtension plugin. Such a plugin can call
212+
``render_model_create`` and ``render_model_destroy`` to
213+
create the object that will provide access to that render
214+
model through the core render models API.
215+
216+
You should not destroy a render model outside of this logic.
217+
218+
You can connect to the signals ``render_model_added`` and
219+
``render_model_removed`` to be informed when new render
220+
models are added or removed.
221+
222+
The core methods for working with this API are listed
223+
below:
224+
225+
.. list-table:: Title
226+
:header-rows: 1
227+
228+
* - Function
229+
- Description
230+
* - render_model_get_all
231+
- Provides an array of RIDs for all render models
232+
that are being tracked.
233+
* - render_model_new_scene_instance
234+
- Provides a new scene that contains all meshes
235+
needed to display the render model.
236+
* - render_model_get_subaction_paths
237+
- Provides a list of subaction paths from your
238+
action map related to this render mode.
239+
* - render_model_get_top_level_path
240+
- Returns the top level path associated with this
241+
render model (if any).
242+
Use the ``render_model_top_level_path_changed``
243+
signal to react to this changing.
244+
* - render_model_get_confidence
245+
- Returns the tracking confidence for the tracking
246+
data for this render model.
247+
* - render_model_get_root_transform
248+
- Returns the root transform for this render model
249+
within our current reference space. This can be
250+
used to place the render model in space.
251+
* - render_model_get_animatable_node_count
252+
- Returns the number of node in our render model
253+
scene that can be animated
254+
* - render_model_get_animatable_node_name
255+
- Returns the name of the node that we can animate.
256+
Note that this node can be any level deep within
257+
the scene.
258+
* - render_model_is_animatable_node_visible
259+
- Returns true if this animatable node should be
260+
visible
261+
* - render_model_get_animatable_node_transform
262+
- Returns the transform for this animatable node.
263+
This is a local transform that can be directly
264+
applied.
265+
266+

0 commit comments

Comments
 (0)