Skip to content

Commit

Permalink
Make passthrough a toggle on first person controller
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij authored and m4gr3d committed Mar 29, 2022
1 parent f1c4be8 commit 38a59fb
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
2 changes: 2 additions & 0 deletions demo/Main.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
extends Spatial

func _ready():
# Note that our initialised should have run by now, the _ready on FPController runs first!
var screen_instance = $Screen.get_scene_instance()
if screen_instance:
screen_instance.connect("toggle_bounds", self, "_on_toggle_bounds")
screen_instance.set_fp_controller($FPController)

func _on_toggle_bounds():
$FPController/ShowBounds.visible = !$FPController/ShowBounds.visible
Expand Down
3 changes: 3 additions & 0 deletions demo/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ directional_shadow_max_distance = 20.0

[node name="FPController" parent="." instance=ExtResource( 4 )]

[node name="ARVRCamera" parent="FPController" index="1"]
far = 1000.0

[node name="ShadowHead" type="MeshInstance" parent="FPController/ARVRCamera" index="0"]
transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, -0.0187781, 0.103895 )
layers = 524288
Expand Down
84 changes: 73 additions & 11 deletions demo/addons/godot-openxr/scenes/first_person_controller_vr.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tool
extends ARVROrigin

signal initialised
Expand All @@ -10,13 +11,35 @@ signal focused_state
signal visible_state
signal pose_recentered

export var auto_initialise = true
export var start_passthrough = false
export (NodePath) var viewport = null
export var auto_initialise = true setget set_auto_initialise
export var enable_passthrough = false setget set_enable_passthrough
export (NodePath) var viewport setget set_viewport
export var near_z = 0.1
export var far_z = 1000.0

var interface : ARVRInterface
var enabled_extensions : Array

func set_auto_initialise(p_new_value):
auto_initialise = p_new_value
update_configuration_warning()

func set_enable_passthrough(p_new_value):
enable_passthrough = p_new_value
update_configuration_warning()

# Only actually start our passthrough if our interface has been instanced
# if not this will be delayed until initialise is successfully called.
if interface:
if enable_passthrough:
# unset enable_passthrough if we can't start it.
enable_passthrough = _start_passthrough()
else:
_stop_passthrough()

func set_viewport(p_new_value):
viewport = p_new_value
update_configuration_warning()

func get_interface() -> ARVRInterface:
return interface
Expand All @@ -25,10 +48,14 @@ func _ready():
$ARVRCamera.near = near_z
$ARVRCamera.far = far_z

if auto_initialise:
if auto_initialise && !Engine.editor_hint:
initialise()

func initialise() -> bool:
if Engine.editor_hint:
print("Can't initialise while in the editor")
return false

if interface:
# we are already initialised
return true
Expand All @@ -40,8 +67,12 @@ func initialise() -> bool:
# Find the viewport we're using to render our XR output
var vp : Viewport = _get_xr_viewport()

# Obtain enabled extensions
enabled_extensions = $Configuration.get_enabled_extensions()

# Start passthrough?
_start_passthrough()
if enable_passthrough and is_passthrough_supported():
enable_passthrough = _start_passthrough()

# Connect to our plugin signals
_connect_plugin_signals()
Expand All @@ -68,6 +99,8 @@ func initialise() -> bool:
emit_signal("initialised")
return true
else:
interface = null

emit_signal("failed_initialisation")
return false

Expand All @@ -78,13 +111,22 @@ func _get_xr_viewport() -> Viewport:
else:
return get_viewport()

func _start_passthrough():
if start_passthrough:
# make sure our viewports background is transparent
_get_xr_viewport().transparent_bg = true
func is_passthrough_supported() -> bool:
var supported = enabled_extensions.find("XR_FB_passthrough") >= 0
return supported

# enable our passthrough
$Configuration.start_passthrough()
func _start_passthrough() -> bool:
# make sure our viewports background is transparent
_get_xr_viewport().transparent_bg = true

# enable our passthrough
return $Configuration.start_passthrough()

func _stop_passthrough():
# make sure our viewports background is not transparent
_get_xr_viewport().transparent_bg = false

$Configuration.stop_passthrough()

func _connect_plugin_signals():
ARVRServer.connect("openxr_session_begun", self, "_on_openxr_session_begun")
Expand Down Expand Up @@ -118,3 +160,23 @@ func _on_openxr_visible_state():
func _on_openxr_pose_recentered():
print("OpenXR pose recentered")
emit_signal("pose_recentered")

func _get_configuration_warning():
var version = Engine.get_version_info()

if viewport:
var vp = get_node(viewport)
if !vp:
return "Can't access assigned viewport"
if !(vp is Viewport):
return "Selected viewport is not a viewport"
if vp.render_target_update_mode != Viewport.UPDATE_ALWAYS:
return "Viewport update mode is not set to always, you may not get proper output"

if enable_passthrough and version['major'] == 3 and version['minor'] < 4:
return "Godot %s is too old for the passthrough version. Please upgrade to Godot 3.4 or later." % version['string']

if !auto_initialise:
return "You must call initialise() manually for VR to start"

return ""
12 changes: 12 additions & 0 deletions demo/scenes/Screen.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ extends Node2D
signal toggle_bounds

var count = 0
var fp_controller : ARVROrigin

func set_fp_controller(p_node : ARVROrigin):
fp_controller = p_node
if fp_controller and fp_controller.is_passthrough_supported():
$TogglePassthrough.disabled = false
else:
$TogglePassthrough.disabled = true

# Called when the node enters the scene tree for the first time.
func _ready():
Expand All @@ -14,3 +22,7 @@ func _on_Button_pressed():

func _on_ToggleBounds_pressed():
emit_signal("toggle_bounds")

func _on_TogglePassthrough_pressed():
if fp_controller:
fp_controller.enable_passthrough = !fp_controller.enable_passthrough
23 changes: 19 additions & 4 deletions demo/scenes/Screen.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,30 @@ __meta__ = {
}

[node name="ToggleBounds" type="Button" parent="."]
margin_left = 422.073
margin_top = 15.119
margin_right = 584.073
margin_bottom = 53.119
margin_left = 375.0
margin_top = 10.0
margin_right = 595.0
margin_bottom = 49.119
rect_min_size = Vector2( 220, 0 )
custom_fonts/font = ExtResource( 1 )
text = "Toggle bounds"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="TogglePassthrough" type="Button" parent="."]
margin_left = 375.0
margin_top = 55.0
margin_right = 595.0
margin_bottom = 93.0
rect_min_size = Vector2( 220, 0 )
custom_fonts/font = ExtResource( 1 )
disabled = true
text = "Toggle passthrough"
__meta__ = {
"_edit_use_anchors_": false
}

[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"]
[connection signal="pressed" from="ToggleBounds" to="." method="_on_ToggleBounds_pressed"]
[connection signal="pressed" from="TogglePassthrough" to="." method="_on_TogglePassthrough_pressed"]

0 comments on commit 38a59fb

Please sign in to comment.