Skip to content

Commit

Permalink
Move our staging logic into the plugin so it becomes reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Oct 26, 2022
1 parent 6a9390f commit 6b191f6
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 75 deletions.
6 changes: 6 additions & 0 deletions addons/godot-xr-tools/misc/arvr_helpers.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ static func get_arvr_origin(node: Node, path: NodePath = NodePath("")) -> ARVROr
return origin
current = current.get_parent()

# We check our children but only one level
for child in node.get_children():
origin = child as ARVROrigin
if origin:
return origin

# Could not find origin
return null

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ signal continue_pressed
# If enabled, rotate our screen to follow the camera

export (bool) var follow_camera = true setget set_follow_camera
export (NodePath) var camera
export (Curve) var follow_speed

var camera_node : ARVRCamera
var camera : ARVRCamera

func set_follow_camera(p_enabled):
follow_camera = p_enabled
_update_follow_camera()

func set_camera(p_camera : ARVRCamera):
camera = p_camera
_update_follow_camera()

func _update_follow_camera():
if camera_node and !Engine.is_editor_hint():
if camera and !Engine.is_editor_hint():
set_process(follow_camera)

## Splash screen
Expand Down Expand Up @@ -103,14 +106,16 @@ func _ready():

_update_enable_press_to_continue()

camera_node = get_node_or_null(camera)
_update_follow_camera()

func _process(delta):
if Engine.is_editor_hint():
return

var camera_dir = camera_node.global_transform.basis.z
if !camera:
return

var camera_dir = camera.global_transform.basis.z
camera_dir.y = 0.0
camera_dir = camera_dir.normalized()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[gd_scene load_steps=14 format=2]

[ext_resource path="res://assets/godot/splash.png" type="Texture" id=1]
[ext_resource path="res://loading_screen/loading_screen.gd" type="Script" id=2]
[ext_resource path="res://loading_screen/loading_screen_shader.tres" type="Shader" id=3]
[ext_resource path="res://addons/godot-xr-tools/staging/loading_screen/loading_screen.gd" type="Script" id=2]
[ext_resource path="res://addons/godot-xr-tools/staging/loading_screen/loading_screen_shader.tres" type="Shader" id=3]
[ext_resource path="res://assets/misc/progress_bar.png" type="Texture" id=4]
[ext_resource path="res://assets/misc/Hold trigger to continue.png" type="Texture" id=5]
[ext_resource path="res://addons/godot-xr-tools/misc/hold_button.tscn" type="PackedScene" id=6]
Expand Down Expand Up @@ -51,7 +51,6 @@ material/0 = SubResource( 1 )
[node name="ProgressBar" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, -10 )
mesh = SubResource( 3 )
material/0 = null

[node name="PressToContinue" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -1, 0, -10 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ condition = 1

[resource]
code = "shader_type spatial;
render_mode specular_schlick_ggx, unshaded;
render_mode specular_schlick_ggx, async_visible, unshaded;

uniform vec4 bar_color : hint_color;
uniform sampler2D bar_texture : hint_albedo;
Expand Down
90 changes: 67 additions & 23 deletions staging.gd → addons/godot-xr-tools/staging/staging.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tool
extends Spatial

## Introduction
Expand Down Expand Up @@ -55,11 +56,16 @@ func set_fade(p_value : float):
#
# Note ResourceQueue is an autoloaded script, see project settings.

export (String, FILE, '*.tscn') var main_scene

var arvr_origin : ARVROrigin
var arvr_camera : ARVRCamera

var current_scene : SceneBase
var current_scene_path : String

func _on_exit_to_main_menu():
load_scene("res://scenes/main_menu/main_menu_level.tscn")
load_scene(main_scene)

func _on_load_scene(p_scene_path : String):
load_scene(p_scene_path)
Expand All @@ -73,94 +79,132 @@ func _remove_signals(p_scene : SceneBase):
p_scene.disconnect("load_scene", self, "_on_load_scene")

func load_scene(p_scene_path : String):
# Do not load if in the editor
if Engine.editor_hint:
return

# Check if it's already loaded...
if p_scene_path == current_scene_path:
return

if !arvr_origin:
return

if !arvr_camera:
return

if current_scene:
# Start by unloading our scene

# First remove signals
_remove_signals(current_scene)

# Fade to black
$Tween.remove_all()
$Tween.interpolate_method(self, "set_fade", 0.0, 1.0, 1.0)
$Tween.start()
yield($Tween, "tween_all_completed")

# Now we remove our scene
current_scene.scene_exiting()
$Scene.remove_child(current_scene)
current_scene.queue_free()
current_scene = null

# Make our loading screen visible again and reset some stuff
$FPController.set_process_internal(true)
$FPController/ARVRCamera.current = true
arvr_origin.set_process_internal(true)
arvr_camera.current = true
$WorldEnvironment.environment = loading_screen_environment
$LoadingScreen.progress = 0.0
$LoadingScreen.enable_press_to_continue = false
$LoadingScreen.follow_camera = true
$LoadingScreen.visible = true

# Fade to visible
$Tween.remove_all()
$Tween.interpolate_method(self, "set_fade", 1.0, 0.0, 1.0)
$Tween.start()
yield($Tween, "tween_all_completed")

# Attempt to load our scene
ResourceQueue.queue_resource(p_scene_path)
while !ResourceQueue.is_ready(p_scene_path):
# wait one second
yield(get_tree().create_timer(1.0), "timeout")
# wait a bit
yield(get_tree().create_timer(0.3), "timeout")

$LoadingScreen.progress = ResourceQueue.get_progress(p_scene_path)

var new_scene : PackedScene = ResourceQueue.get_resource(p_scene_path)

# Wait for user to be ready
$LoadingScreen.enable_press_to_continue = true
yield($LoadingScreen, "continue_pressed")

# Fade to black
$Tween.remove_all()
$Tween.interpolate_method(self, "set_fade", 0.0, 1.0, 1.0)
$Tween.start()
yield($Tween, "tween_all_completed")

# Hide our loading screen
$LoadingScreen.follow_camera = false
$LoadingScreen.visible = false
# Turn off internal process on our FPController, the internal process

# Turn off internal process on our ARVROrigin node, the internal process
# of our ARVROrigin will submit its positioning data to the ARVRServer.
# With two ARVROrigin nodes we'll get competing data.
$FPController.set_process_internal(false)
arvr_origin.set_process_internal(false)

# Setup our new scene
current_scene = new_scene.instance()
current_scene_path = p_scene_path
$Scene.add_child(current_scene)
$WorldEnvironment.environment = current_scene.environment
_add_signals(current_scene)

# We create a small delay here to give tracking some time to update our nodes...
yield(get_tree().create_timer(0.1), "timeout")
current_scene.scene_loaded()

# Fade to visible
$Tween.remove_all()
$Tween.interpolate_method(self, "set_fade", 1.0, 0.0, 1.0)
$Tween.start()
yield($Tween, "tween_all_completed")

## Verifies our staging has a valid configuration.
func _get_configuration_warning():
var arvr_origin : ARVROrigin = ARVRHelpers.get_arvr_origin(self)
if !arvr_origin:
return "No ARVROrigin node found, please add one"

var arvr_camera : ARVRCamera = ARVRHelpers.get_arvr_camera(self)
if !arvr_camera:
return "No ARVRCamera node found, please add one to your ARVROrigin node"

var file = File.new()
if main_scene == "":
return "No main scene selected"
elif !file.file_exists(main_scene):
return "Main scene doesn't exist"

return ""

## interface

func _ready():
# Do not initialise if in the editor
if Engine.editor_hint:
return

arvr_origin = ARVRHelpers.get_arvr_origin(self)

arvr_camera = ARVRHelpers.get_arvr_camera(self)
if arvr_camera:
$LoadingScreen.set_camera(arvr_camera)

# Start our resource loader
ResourceQueue.start()

# We start by loading our main level scene
load_scene("res://scenes/main_menu/main_menu_level.tscn")
load_scene(main_scene)
37 changes: 37 additions & 0 deletions addons/godot-xr-tools/staging/staging.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[gd_scene load_steps=8 format=2]

[ext_resource path="res://addons/godot-xr-tools/staging/staging.gd" type="Script" id=2]
[ext_resource path="res://addons/godot-xr-tools/staging/loading_screen/loading_screen.tscn" type="PackedScene" id=3]
[ext_resource path="res://addons/godot-xr-tools/staging/loading_screen/fade.gdshader" type="Shader" id=4]

[sub_resource type="QuadMesh" id=4]
custom_aabb = AABB( -5000, -5000, -5000, 10000, 10000, 10000 )
size = Vector2( 2, 2 )

[sub_resource type="ShaderMaterial" id=3]
shader = ExtResource( 4 )
shader_param/alpha = 0.0

[sub_resource type="ProceduralSky" id=1]

[sub_resource type="Environment" id=2]
background_mode = 1
background_sky = SubResource( 1 )

[node name="Staging" type="Spatial"]
script = ExtResource( 2 )

[node name="Fade" type="MeshInstance" parent="."]
mesh = SubResource( 4 )
material/0 = SubResource( 3 )

[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource( 2 )

[node name="LoadingScreen" parent="." instance=ExtResource( 3 )]
splash_screen = null
progress = 0.0

[node name="Scene" type="Spatial" parent="."]

[node name="Tween" type="Tween" parent="."]
53 changes: 10 additions & 43 deletions staging.tscn
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
[gd_scene load_steps=10 format=2]
[gd_scene load_steps=5 format=2]

[ext_resource path="res://addons/godot-openxr/scenes/first_person_controller_vr.tscn" type="PackedScene" id=1]
[ext_resource path="res://staging.gd" type="Script" id=2]
[ext_resource path="res://loading_screen/loading_screen.tscn" type="PackedScene" id=3]
[ext_resource path="res://loading_screen/fade.gdshader" type="Shader" id=4]
[ext_resource path="res://addons/godot-xr-tools/staging/staging.tscn" type="PackedScene" id=1]
[ext_resource path="res://addons/godot-openxr/scenes/first_person_controller_vr.tscn" type="PackedScene" id=2]
[ext_resource path="res://assets/godot/splash.png" type="Texture" id=3]
[ext_resource path="res://addons/godot-xr-tools/misc/vr_common_shader_cache.tscn" type="PackedScene" id=9]

[sub_resource type="QuadMesh" id=4]
custom_aabb = AABB( -5000, -5000, -5000, 10000, 10000, 10000 )
size = Vector2( 2, 2 )
[node name="Staging" instance=ExtResource( 1 )]
main_scene = "res://scenes/main_menu/main_menu_level.tscn"

[sub_resource type="ShaderMaterial" id=3]
shader = ExtResource( 4 )
shader_param/alpha = 0.0
[node name="LoadingScreen" parent="." index="2"]
splash_screen = ExtResource( 3 )

[sub_resource type="ProceduralSky" id=1]

[sub_resource type="Environment" id=2]
background_mode = 1
background_sky = SubResource( 1 )

[node name="Staging" type="Spatial"]
script = ExtResource( 2 )

[node name="Fade" type="MeshInstance" parent="."]
mesh = SubResource( 4 )
material/0 = SubResource( 3 )

[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource( 2 )

[node name="FPController" parent="." instance=ExtResource( 1 )]
[node name="FPController" parent="." index="5" instance=ExtResource( 2 )]

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

[node name="vr_common_shader_cache" parent="FPController/ARVRCamera" index="0" instance=ExtResource( 9 )]

[node name="LeftHandController" parent="FPController" index="2"]
hide_for_no_tracking_confidence = true

[node name="RightHandController" parent="FPController" index="3"]
hide_for_no_tracking_confidence = true

[node name="LoadingScreen" parent="." instance=ExtResource( 3 )]
camera = NodePath("../FPController/ARVRCamera")
progress = 0.0

[node name="Scene" type="Spatial" parent="."]

[node name="Tween" type="Tween" parent="."]
[node name="VRCommonShaderCache" parent="FPController/ARVRCamera" index="0" instance=ExtResource( 9 )]

[editable path="FPController"]

0 comments on commit 6b191f6

Please sign in to comment.