Skip to content

Commit

Permalink
Add OpenXR demo project showing off local reference space
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Mar 18, 2024
1 parent 71eea49 commit d77ba52
Show file tree
Hide file tree
Showing 72 changed files with 8,349 additions and 10 deletions.
10 changes: 5 additions & 5 deletions xr/openxr_character_centric_movement/start_vr.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func _ready():
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)

# Connect the OpenXR events
xr_interface.connect("session_begun", _on_openxr_session_begun)
xr_interface.connect("session_visible", _on_openxr_visible_state)
xr_interface.connect("session_focussed", _on_openxr_focused_state)
xr_interface.connect("session_stopping", _on_openxr_stopping)
xr_interface.connect("pose_recentered", _on_openxr_pose_recentered)
xr_interface.session_begun.connect(_on_openxr_session_begun)
xr_interface.session_visible.connect(_on_openxr_visible_state)
xr_interface.session_focussed.connect(_on_openxr_focused_state)
xr_interface.session_stopping.connect(_on_openxr_stopping)
xr_interface.pose_recentered.connect(_on_openxr_pose_recentered)
else:
# We couldn't start OpenXR.
print("OpenXR not instantiated!")
Expand Down
10 changes: 5 additions & 5 deletions xr/openxr_origin_centric_movement/start_vr.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func _ready():
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)

# Connect the OpenXR events
xr_interface.connect("session_begun", _on_openxr_session_begun)
xr_interface.connect("session_visible", _on_openxr_visible_state)
xr_interface.connect("session_focussed", _on_openxr_focused_state)
xr_interface.connect("session_stopping", _on_openxr_stopping)
xr_interface.connect("pose_recentered", _on_openxr_pose_recentered)
xr_interface.session_begun.connect(_on_openxr_session_begun)
xr_interface.session_visible.connect(_on_openxr_visible_state)
xr_interface.session_focussed.connect(_on_openxr_focused_state)
xr_interface.session_stopping.connect(_on_openxr_stopping)
xr_interface.pose_recentered.connect(_on_openxr_pose_recentered)
else:
# We couldn't start OpenXR.
print("OpenXR not instantiated!")
Expand Down
2 changes: 2 additions & 0 deletions xr/openxr_vehicle_movement/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Normalize EOL for all files that Git considers text files.
* text=auto eol=lf
2 changes: 2 additions & 0 deletions xr/openxr_vehicle_movement/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Godot 4+ specific ignores
.godot/
8 changes: 8 additions & 0 deletions xr/openxr_vehicle_movement/InfoUI.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extends CanvasLayer

func set_velocity(p_velocity : float):
%Velocity.text = "Velocity: %0.2f kmph (%0.2f m/s)" % [ p_velocity * 3.6, p_velocity ]

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
%FPS.text = "FPS: " + str(Engine.get_frames_per_second())
30 changes: 30 additions & 0 deletions xr/openxr_vehicle_movement/InfoUI.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[gd_scene load_steps=2 format=3 uid="uid://dal5i1c6mh07h"]

[ext_resource type="Script" path="res://InfoUI.gd" id="1_sj0oq"]

[node name="InfoUI" type="CanvasLayer"]
script = ExtResource("1_sj0oq")

[node name="ColorRect" type="ColorRect" parent="."]
custom_minimum_size = Vector2(512, 256)
offset_right = 40.0
offset_bottom = 40.0
color = Color(0.341176, 0.580392, 0.368627, 1)

[node name="VBoxContainer" type="VBoxContainer" parent="."]
offset_left = 25.0
offset_top = 10.0
offset_right = 499.0
offset_bottom = 158.0

[node name="FPS" type="Label" parent="VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
theme_override_font_sizes/font_size = 32
text = "FPS: 000"

[node name="Velocity" type="Label" parent="VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
theme_override_font_sizes/font_size = 32
text = "Velocity: 000"
67 changes: 67 additions & 0 deletions xr/openxr_vehicle_movement/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# XR Vehicle Movement demo

This is a demo for an OpenXR project where the player is positioned in a vehicle.

Godot version: 4.2.x

Language: GDScript

Renderer: compatibility

## How does it work?

Controlling a vehicle in VR is a bit of a special case.
Modern VR setups are all about freely walking around your room.
With vehicles however your player is confined in a small space, and you want to ensure the player isn't standing up in their virtual seat.
You do want the player to have some freedom of motion so they can easily look around.

There are two ways to ensure the player is in the right spot.

The first is calling [XRServer.center_on_hmd](https://docs.godotengine.org/en/stable/classes/class_xrserver.html#class-xrserver-method-center-on-hmd) with `rotation_mode` set to `XRServer.RESET_BUT_KEEP_TILT` and `keep_height` set to false. This will place the `XRCamera3D` node at the center of your `XROrigin3D` node and allows you to place the origin node where the players head should be. You use this while keeping the reference space set to the default `Stage` mode.
You will need to react to the [pose_recentered](https://docs.godotengine.org/en/stable/classes/class_openxrinterface.html#signals) signal when the user requests to recenter themselves.
This approach is useful when you switch between driving a vehicle and freely walking around. You can use `center_on_hmd` with `keep_height` set to true to switch back to roomscale mode.

The second option we use in this demo, here we leave it up to the XR runtime to handle centering our headset. To enable this we simply set the OpenXR reference space to `Local`:

![Screenshot](screenshots/openxr_settings.png)

In this mode the headset will use the players head position as the center of the tracking space.
Behavior can be slighly different between different XR runtimes, some will take the position at game start, others will remember the position accross gaming sessions.
When the user triggers a recenter this is automatically handled by the headset. We do still get at `pose_recentered` signal but do not need to react to it.

The second option is preferable for games that only work in this mode.

## Input and the action map

This demo uses Godots standard input map to allow for keyboard and gamepad input.

There is also an action map setup so you can use the XR controllers.
Move your hands close enough to the steering wheel so they grab the wheel,
this will disable traditional input and enable you to control the steering wheel with your hands.
You can then use the trigger on the left controller to accellerate, and the trigger on the right controller to break.

The way the steering works in XR mode is that we take the position of the hands in the local space of the steering wheel.
We then get the vector from the left hand to the right hand.
If only one controller is used we take the vector relative to the center of the steering wheel.
Now we simply calculate the angle of that vector to get our steering angle.

## Running on PCVR

This project can be run as normal for PCVR. Ensure that an OpenXR runtime has been installed.
This project has been tested with the Oculus client and SteamVR OpenXR runtimes.
Note that Godot currently can't run using the WMR OpenXR runtime. Install SteamVR with WMR support.

## Running on standalone VR

You must install the Android build templates and OpenXR loader plugin and configure an export template for your device.
Please follow [the instructions for deploying on Android in the manual](https://docs.godotengine.org/en/stable/tutorials/xr/deploying_to_android.html).

## Additional licenses

See license files in the asset subfolder for any assets that have additional licenses.

## Screenshots

![Screenshot](screenshots/vehicle_demo.png)

![Video](https://youtu.be/zhwjFSqNifE)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 11 additions & 0 deletions xr/openxr_vehicle_movement/assets/gokart/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Model Information:
* title: Gokart
* source: https://sketchfab.com/3d-models/gokart-b60878a0f04d43d388dd2a57be419a72
* author: AwEsZ (https://sketchfab.com/AwEsZ)

Model License:
* license type: CC-BY-4.0 (http://creativecommons.org/licenses/by/4.0/)
* requirements: Author must be credited. Commercial use is allowed.

If you use this 3D model in your project be sure to copy paste this credit wherever you share it:
This work is based on "Gokart" (https://sketchfab.com/3d-models/gokart-b60878a0f04d43d388dd2a57be419a72) by AwEsZ (https://sketchfab.com/AwEsZ) licensed under CC-BY-4.0 (http://creativecommons.org/licenses/by/4.0/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://dqaeublsfgxet"]

[resource]
resource_name = "Cube.014__0"
vertex_color_use_as_albedo = true
roughness = 0.6
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://bqhatpgnbfayc"]

[resource]
resource_name = "Material.001"
vertex_color_use_as_albedo = true
albedo_color = Color(0.906332, 0.906332, 0.906332, 1)
roughness = 0.821115
emission_enabled = true
emission = Color(0.594203, 0.594203, 0.594203, 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://cv2hslabq63af"]

[resource]
resource_name = "Material.002"
vertex_color_use_as_albedo = true
albedo_color = Color(0.345106, 0.345106, 0.345106, 1)
roughness = 0.978178
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://c768tjddf3eq"]

[resource]
resource_name = "Material.003"
vertex_color_use_as_albedo = true
albedo_color = Color(0.409976, 0.514569, 0.985366, 1)
roughness = 0.276188
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://thtm6w6l6x3g"]

[resource]
resource_name = "Material.004"
vertex_color_use_as_albedo = true
albedo_color = Color(0.519994, 0.519994, 0.519994, 1)
roughness = 0.939756
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://vxoj0lynkbts"]

[resource]
resource_name = "Material.005"
vertex_color_use_as_albedo = true
albedo_color = Color(0.129709, 0.129709, 0.129709, 1)
roughness = 0.996586
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://csd0uv3t1t6ul"]

[resource]
resource_name = "Material.006"
vertex_color_use_as_albedo = true
albedo_color = Color(0.635294, 0.635294, 0.635294, 1)
metallic = 0.73
roughness = 0.12
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://usphfm8cljtl"]

[resource]
resource_name = "Material.007"
vertex_color_use_as_albedo = true
albedo_color = Color(0.339284, 0.339284, 0.339284, 1)
roughness = 0.978932
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://2e3ewnm4kmwc"]

[resource]
resource_name = "Material.008"
vertex_color_use_as_albedo = true
albedo_color = Color(0.822033, 0.822033, 0.822033, 1)
roughness = 0.81088
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://buhaihrem52b"]

[resource]
resource_name = "Material.009"
vertex_color_use_as_albedo = true
albedo_color = Color(0.595431, 0.595431, 0.595431, 1)
roughness = 0.929957
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://dy8fo1wov4fb2"]

[resource]
resource_name = "Material.010"
vertex_color_use_as_albedo = true
albedo_color = Color(0.906332, 0.906332, 0.906332, 1)
roughness = 0.747018
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://da1iluolhagux"]

[resource]
resource_name = "Material.011"
vertex_color_use_as_albedo = true
albedo_color = Color(0.653458, 0.653458, 0.653458, 1)
roughness = 0.914013
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://52y8qfvj7pys"]

[resource]
resource_name = "Material.012"
vertex_color_use_as_albedo = true
albedo_color = Color(0.448832, 0.448832, 0.448832, 1)
roughness = 0.958275
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://d4jv657hekp3u"]

[resource]
resource_name = "Material.013"
vertex_color_use_as_albedo = true
albedo_color = Color(0.61038, 0.61038, 0.61038, 1)
roughness = 0.919279
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://cwyqdq8n52kxf"]

[resource]
resource_name = "Material.014"
vertex_color_use_as_albedo = true
albedo_color = Color(0.766543, 0.312911, 0.328656, 1)
roughness = 0.957246
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://2xqcil35ckmf"]

[resource]
resource_name = "Material.015"
cull_mode = 2
vertex_color_use_as_albedo = true
albedo_color = Color(0.691153, 0.691153, 0.691153, 1)
roughness = 0.902621
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://0y4xq7jk6jgw"]

[resource]
resource_name = "Material.015"
vertex_color_use_as_albedo = true
albedo_color = Color(0.691153, 0.691153, 0.691153, 1)
roughness = 0.902621
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://bgvmnvdrkutv7"]

[resource]
resource_name = "Material.016"
vertex_color_use_as_albedo = true
albedo_color = Color(0.594224, 0.594224, 0.594224, 1)
roughness = 0.930269
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://cstrxte8w75kg"]

[resource]
resource_name = "Material.017"
vertex_color_use_as_albedo = true
albedo_color = Color(0.172139, 0.172139, 0.172139, 1)
roughness = 0.994392
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://boblofr4cshco"]

[resource]
resource_name = "Material.018"
vertex_color_use_as_albedo = true
albedo_color = Color(0.603091, 0.603091, 0.603091, 1)
roughness = 0.915597
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://mob83pxu5ilv"]

[resource]
resource_name = "Material.019"
vertex_color_use_as_albedo = true
albedo_color = Color(0.704722, 0.743734, 0.610886, 1)
roughness = 0.891021
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://b1sxts1b0crfs"]

[resource]
resource_name = "Material.020"
vertex_color_use_as_albedo = true
albedo_color = Color(0.394256, 0.394256, 0.394256, 1)
roughness = 0.971182
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://cyswii48wfoct"]

[resource]
resource_name = "Material.021"
vertex_color_use_as_albedo = true
albedo_color = Color(0.813327, 0.813327, 0.813327, 1)
roughness = 0.859874
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://b7iab86bemdgw"]

[resource]
resource_name = "Material.022"
vertex_color_use_as_albedo = true
albedo_color = Color(0.42666, 0.42666, 0.42666, 1)
roughness = 0.965939
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://d2rqx5bspsj21"]

[resource]
resource_name = "Material.023"
vertex_color_use_as_albedo = true
albedo_color = Color(0.646661, 0.646661, 0.646661, 1)
roughness = 0.91598
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://kqy0u8xxds35"]

[resource]
resource_name = "Material.024"
vertex_color_use_as_albedo = true
albedo_color = Color(0.60166, 0.77659, 0.790989, 1)
roughness = 0.598928
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://d2bj1ek3oad08"]

[resource]
resource_name = "Material"
vertex_color_use_as_albedo = true
albedo_color = Color(0.411765, 0.513726, 0.984314, 1)
roughness = 0.440603
Binary file not shown.
Loading

0 comments on commit d77ba52

Please sign in to comment.