Skip to content

Commit

Permalink
Cleanup teleport
Browse files Browse the repository at this point in the history
Added teleport logic to XRToolsPlayerBody
Modified XRToolsFunctionTeleport to use players teleport logic
Added XRToolsTeleportArea to teleport player to target
Added demo teleport area to teleport demo scene
  • Loading branch information
Malcolmnixon committed Aug 20, 2023
1 parent 154e861 commit 526525d
Show file tree
Hide file tree
Showing 11 changed files with 733 additions and 47 deletions.
1 change: 1 addition & 0 deletions VERSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Modified pickup highlighting to support pickables in snap-zones
- Added "UI Objects" layer 23 for viewports to support interaction by pointer and poking
- Fixed player scaling issues with crouching and poke
- Moved teleport logic to player and added teleport area node

# 4.1.0
- Enhanced grappling to support collision and target layers
Expand Down
29 changes: 7 additions & 22 deletions addons/godot-xr-tools/functions/function_teleport.gd
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ var player : Node3D
## Capsule shown when not using a custom player mesh
@onready var capsule : MeshInstance3D = $Target/Player_figure/Capsule

## [XROrigin3D] node.
@onready var origin_node := XRHelpers.get_xr_origin(self)
## [XRToolsPlayerBody] node.
@onready var player_node := XRToolsPlayerBody.find_instance(self)

## [XRCamera3D] node.
@onready var camera_node := XRHelpers.get_xr_camera(self)
Expand Down Expand Up @@ -151,7 +151,7 @@ func _physics_process(delta):
return

# Skip if required nodes are missing
if !origin_node or !camera_node or !controller:
if !player_node or !camera_node or !controller:
return

# if we're not enabled no point in doing mode
Expand Down Expand Up @@ -326,29 +326,14 @@ func _physics_process(delta):
elif is_teleporting:
if can_teleport:

# make our target horizontal again
# Make our target using the players up vector
var new_transform = last_target_transform
new_transform.basis.y = Vector3(0.0, 1.0, 0.0)
new_transform.basis.y = player_node.up_player
new_transform.basis.x = new_transform.basis.y.cross(new_transform.basis.z).normalized()
new_transform.basis.z = new_transform.basis.x.cross(new_transform.basis.y).normalized()

# Find out our user's feet's transformation.
# The feet are on the ground, but have the same X,Z as the camera
var cam_transform = camera_node.transform
var user_feet_transform = Transform3D()
user_feet_transform.origin = cam_transform.origin
user_feet_transform.origin.y = 0

# ensure this transform is upright
user_feet_transform.basis.y = Vector3(0.0, 1.0, 0.0)
user_feet_transform.basis.x = user_feet_transform.basis.y.cross(
cam_transform.basis.z).normalized()
user_feet_transform.basis.z = user_feet_transform.basis.x.cross(
user_feet_transform.basis.y).normalized()

# now move the origin such that the new global user_feet_transform
# would be == new_transform
origin_node.global_transform = new_transform * user_feet_transform.inverse()
# Teleport the player
player_node.teleport(new_transform)

# and disable
is_teleporting = false;
Expand Down
29 changes: 29 additions & 0 deletions addons/godot-xr-tools/objects/teleport_area.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@tool
class_name XRToolsTeleportArea
extends Area3D


## Target node
@export var target : Node3D


# Add support for is_xr_class on XRTools classes
func is_xr_class(name : String) -> bool:
return name == "XRToolsTeleportArea"


# Called when the node enters the scene tree for the first time.
func _ready():
# Handle body entered
body_entered.connect(_on_body_entered)


# Handle body entering area
func _on_body_entered(body : Node3D) -> void:
# Test if the body is the player
var player_body := body as XRToolsPlayerBody
if not player_body:
return

# Teleport the player
player_body.teleport(target.global_transform)
8 changes: 8 additions & 0 deletions addons/godot-xr-tools/objects/teleport_area.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_scene load_steps=2 format=3 uid="uid://dpy1eg3i331se"]

[ext_resource type="Script" path="res://addons/godot-xr-tools/objects/teleport_area.gd" id="1_0awk1"]

[node name="TeleportArea" type="Area3D"]
collision_layer = 0
collision_mask = 524288
script = ExtResource("1_0awk1")
18 changes: 18 additions & 0 deletions addons/godot-xr-tools/player/player_body.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ extends CharacterBody3D
## Signal emitted when the player jumps
signal player_jumped()

## Signal emitted when the player teleports
signal player_teleported()

## Signal emitted when the player bounces
signal player_bounced(collider, magnitude)

Expand Down Expand Up @@ -305,6 +308,21 @@ func _physics_process(delta: float):
slew_up(-gravity.normalized(), 5.0 * delta)


## Teleport the player body
func teleport(target : Transform3D) -> void:
# Get the player-to-origin transform
var player_to_origin = global_transform.inverse() * origin_node.global_transform

# Set the player
global_transform = target

# Set the origin
origin_node.global_transform = target * player_to_origin

# Report the player teleported
player_teleported.emit()


## Request a jump
func request_jump(skip_jump_velocity := false):
# Skip if cooling down from a previous jump
Expand Down
149 changes: 149 additions & 0 deletions scenes/teleport_demo/objects/teleport_area_end.tscn

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions scenes/teleport_demo/objects/teleport_area_metal.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://k4vatr17hqlp"]

[resource]
albedo_color = Color(0.677504, 0.784652, 1, 1)
metallic = 1.0
metallic_specular = 1.0
roughness = 0.25
149 changes: 149 additions & 0 deletions scenes/teleport_demo/objects/teleport_area_start.tscn

Large diffs are not rendered by default.

158 changes: 158 additions & 0 deletions scenes/teleport_demo/objects/teleport_end.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
[gd_resource type="ShaderMaterial" load_steps=14 format=3 uid="uid://og4bdoukiu8l"]

[sub_resource type="VisualShaderNodeFloatFunc" id="8"]
output_port_for_preview = 0
function = 17

[sub_resource type="VisualShaderNodeInput" id="14"]
input_name = "time"

[sub_resource type="VisualShaderNodeFloatOp" id="19"]
default_input_values = [0, 0.0, 1, 0.8]
operator = 2

[sub_resource type="VisualShaderNodeVectorOp" id="23"]
default_input_values = [0, Vector2(0, 0), 1, Vector2(1.25, 0)]
op_type = 0
operator = 2

[sub_resource type="VisualShaderNodeFloatOp" id="24"]

[sub_resource type="VisualShaderNodeVectorOp" id="25"]
operator = 2

[sub_resource type="VisualShaderNodeSmoothStep" id="28"]
output_port_for_preview = 0
default_input_values = [0, 0.4, 1, 0.5, 2, 0.0]

[sub_resource type="VisualShaderNodeSmoothStep" id="29"]
output_port_for_preview = 0
default_input_values = [0, 1.0, 1, 0.4, 2, 0.0]

[sub_resource type="VisualShaderNodeFloatOp" id="30"]
output_port_for_preview = 0
operator = 7

[sub_resource type="VisualShaderNodeFloatOp" id="31"]
default_input_values = [0, 0.0, 1, 0.8]
operator = 2

[sub_resource type="VisualShaderNodeInput" id="21"]
output_port_for_preview = 0
expanded_output_ports = [0]
input_name = "uv"

[sub_resource type="VisualShaderNodeColorParameter" id="VisualShaderNodeColorParameter_sreim"]
expanded_output_ports = [0]
parameter_name = "beam_color"
default_value_enabled = true
default_value = Color(0.168627, 0.25098, 0.972549, 1)

[sub_resource type="VisualShader" id="22"]
code = "shader_type spatial;
render_mode blend_add, depth_draw_opaque, cull_disabled, diffuse_lambert, specular_schlick_ggx, unshaded;

uniform vec4 beam_color : source_color = vec4(0.168627, 0.250980, 0.972549, 1.000000);



void fragment() {
// ColorParameter:50
vec4 n_out50p0 = beam_color;


// Input:2
float n_out2p0 = TIME;


// FloatOp:25
float n_in25p1 = 0.80000;
float n_out25p0 = n_out2p0 * n_in25p1;


// Input:5
vec2 n_out5p0 = UV;
float n_out5p2 = n_out5p0.g;


// VectorOp:26
vec2 n_in26p1 = vec2(1.25000, 0.00000);
vec2 n_out26p0 = vec2(n_out5p2) * n_in26p1;


// FloatOp:27
float n_out27p0 = n_out25p0 + n_out26p0.x;


// FloatFunc:12
float n_out12p0 = fract(n_out27p0);


// SmoothStep:46
float n_in46p0 = 0.40000;
float n_in46p1 = 0.50000;
float n_out46p0 = smoothstep(n_in46p0, n_in46p1, n_out12p0);


// SmoothStep:47
float n_in47p0 = 1.00000;
float n_in47p1 = 0.40000;
float n_out47p0 = smoothstep(n_in47p0, n_in47p1, n_out12p0);


// FloatOp:48
float n_out48p0 = min(n_out46p0, n_out47p0);


// FloatOp:49
float n_in49p1 = 0.80000;
float n_out49p0 = n_out48p0 * n_in49p1;


// VectorOp:40
vec3 n_out40p0 = vec3(n_out50p0.xyz) * vec3(n_out49p0);


// Output:0
ALBEDO = n_out40p0;
ALPHA = n_out49p0;


}
"
graph_offset = Vector2(118.2, -413.764)
modes/blend = 1
modes/cull = 2
flags/unshaded = true
nodes/fragment/0/position = Vector2(1820, 0)
nodes/fragment/2/node = SubResource("14")
nodes/fragment/2/position = Vector2(-520, 40)
nodes/fragment/5/node = SubResource("21")
nodes/fragment/5/position = Vector2(-580, 280)
nodes/fragment/12/node = SubResource("8")
nodes/fragment/12/position = Vector2(360, 160)
nodes/fragment/25/node = SubResource("19")
nodes/fragment/25/position = Vector2(-80, 20)
nodes/fragment/26/node = SubResource("23")
nodes/fragment/26/position = Vector2(-220, 280)
nodes/fragment/27/node = SubResource("24")
nodes/fragment/27/position = Vector2(126, 105)
nodes/fragment/40/node = SubResource("25")
nodes/fragment/40/position = Vector2(1617, -84)
nodes/fragment/46/node = SubResource("28")
nodes/fragment/46/position = Vector2(800, -100)
nodes/fragment/47/node = SubResource("29")
nodes/fragment/47/position = Vector2(800, 400)
nodes/fragment/48/node = SubResource("30")
nodes/fragment/48/position = Vector2(1071, 42)
nodes/fragment/49/node = SubResource("31")
nodes/fragment/49/position = Vector2(1302, 84)
nodes/fragment/50/node = SubResource("VisualShaderNodeColorParameter_sreim")
nodes/fragment/50/position = Vector2(1160, -460)
nodes/fragment/connections = PackedInt32Array(2, 0, 25, 0, 25, 0, 27, 0, 26, 0, 27, 1, 27, 0, 12, 0, 12, 0, 47, 2, 46, 0, 48, 0, 47, 0, 48, 1, 48, 0, 49, 0, 49, 0, 0, 1, 40, 0, 0, 0, 49, 0, 40, 1, 5, 2, 26, 0, 12, 0, 46, 2, 50, 0, 40, 0)

[resource]
resource_local_to_scene = true
render_priority = 0
shader = SubResource("22")
shader_parameter/beam_color = Color(0.972549, 0.168627, 0.25098, 1)
Loading

0 comments on commit 526525d

Please sign in to comment.