Skip to content

Commit

Permalink
simulator now works on all scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafezinhu committed Jan 18, 2024
1 parent 498f163 commit efd5194
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
7 changes: 1 addition & 6 deletions Spatial.tscn
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[gd_scene load_steps=20 format=3 uid="uid://o224y2d50yl1"]
[gd_scene load_steps=19 format=3 uid="uid://o224y2d50yl1"]

[ext_resource type="PackedScene" uid="uid://bl2nuu3qhlb5k" path="res://addons/godot-xr-tools/functions/movement_direct.tscn" id="1_ywmh7"]
[ext_resource type="PackedScene" uid="uid://b4ysuy43poobf" path="res://addons/godot-xr-tools/functions/function_pickup.tscn" id="2_nloek"]
[ext_resource type="PackedScene" uid="uid://b6bk2pj8vbj28" path="res://addons/godot-xr-tools/functions/movement_turn.tscn" id="3_7xdfk"]
[ext_resource type="PackedScene" uid="uid://fiul51tsyoop" path="res://addons/godot-xr-tools/functions/function_teleport.tscn" id="3_isyx6"]
[ext_resource type="PackedScene" uid="uid://diyu06cw06syv" path="res://addons/godot-xr-tools/player/player_body.tscn" id="3_m4pts"]
[ext_resource type="PackedScene" uid="uid://c8l60rnugru40" path="res://addons/godot-xr-tools/objects/pickable.tscn" id="5_r3xxt"]
[ext_resource type="PackedScene" uid="uid://ctltchlf2j2r4" path="res://addons/xr-simulator/XRSimulator.tscn" id="7_yovyb"]
[ext_resource type="PackedScene" uid="uid://clc5dre31iskm" path="res://addons/godot-xr-tools/xr/start_xr.tscn" id="8_qoqsw"]

[sub_resource type="BoxShape3D" id="1"]
Expand Down Expand Up @@ -116,10 +115,6 @@ environment = SubResource("Environment_71xw7")
transform = Transform3D(0.402254, -0.874811, 0.269994, -0.530985, 0.0173175, 0.847205, -0.74582, -0.484154, -0.457545, 0, -0.516751, 0.31203)
shadow_enabled = true

[node name="XRSimulator" parent="." instance=ExtResource("7_yovyb")]
xr_origin = NodePath("../XROrigin3D")

[node name="StartXR" parent="." instance=ExtResource("8_qoqsw")]
physics_rate_multiplier = 1

[editable path="PickableObject"]
53 changes: 31 additions & 22 deletions addons/xr-simulator/XRSimulator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ enum ControllerSelectionMode {Hold, Toggle}
@export var is_camera_height_limited: bool = true
@export var min_camera_height: float = 0.5
@export var max_camera_height: float = 2.0
@export var xr_origin: NodePath

var origin: XROrigin3D
var camera: XRCamera3D
var left_controller: XRController3D
var right_controller: XRController3D
Expand Down Expand Up @@ -43,16 +41,30 @@ var key_map = {

@onready var viewport: Viewport = get_viewport()

func _on_node_added(node: Node):
print("node added:", node.name)
if node is XRCamera3D:
camera = node
elif node is XRController3D:
var pose = node.pose
if node.tracker == "left_hand":
left_controller = node
left_tracker.set_pose(pose, node.transform, Vector3.ZERO, Vector3.ZERO, XRPose.XR_TRACKING_CONFIDENCE_HIGH)
XRServer.add_tracker(left_tracker)
elif node.tracker == "right_hand":
right_controller = node
right_tracker.set_pose(pose, node.transform, Vector3.ZERO, Vector3.ZERO, XRPose.XR_TRACKING_CONFIDENCE_HIGH)
XRServer.add_tracker(right_tracker)

func _search_first_xr_nodes(node: Node):
for child in node.get_children():
_search_first_xr_nodes(child)
_on_node_added(child)

func _ready():
if not enabled or not OS.has_feature("editor"):
enabled = false
return

Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

origin = get_node(xr_origin)

camera = origin.get_node("XRCamera3D")

var left_hand = XRServer.get_tracker("left_hand")
if left_hand == null:
Expand All @@ -72,25 +84,18 @@ func _ready():
else:
right_tracker = right_hand

for child in origin.get_children():
if child.get("tracker"):
var pose = child.pose
if child.tracker == "left_hand":
left_controller = child
left_tracker.set_pose(pose, child.transform, Vector3.ZERO, Vector3.ZERO, XRPose.XR_TRACKING_CONFIDENCE_HIGH)
XRServer.add_tracker(left_tracker)
elif child.tracker == "right_hand":
right_controller = child
right_tracker.set_pose(pose, child.transform, Vector3.ZERO, Vector3.ZERO, XRPose.XR_TRACKING_CONFIDENCE_HIGH)
XRServer.add_tracker(right_tracker)

get_tree().node_added.connect(_on_node_added)
_search_first_xr_nodes(get_tree().root)
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

func _process(_delta):
if enabled and disable_xr_in_editor and OS.has_feature("editor") and viewport.use_xr:
viewport.use_xr = false

func _input(event):
if not enabled or not origin.current or not OS.has_feature("editor"):
if not enabled or not OS.has_feature("editor"):
return
if not left_tracker or not right_tracker or not camera:
return
if Input.is_key_pressed(KEY_ESCAPE):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
Expand Down Expand Up @@ -162,7 +167,7 @@ func camera_height(event: InputEventMouseButton):
func simulate_joysticks():
var vec_left = vector_key_mapping(KEY_D, KEY_A, KEY_W, KEY_S)
left_tracker.set_input("primary", vec_left)

var vec_right = vector_key_mapping(KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN)

right_tracker.set_input("primary", vec_right)
Expand Down Expand Up @@ -194,12 +199,16 @@ func simulate_buttons(event: InputEventKey, controller: XRController3D):
right_tracker.set_input(button, event.pressed)

func move_controller(event: InputEventMouseMotion, controller: XRController3D):
if not camera:
return
var movement = Vector3()
movement += camera.global_transform.basis.x * event.relative.x * device_x_sensitivity/1000
movement += camera.global_transform.basis.y * event.relative.y * -device_y_sensitivity/1000
controller.global_translate(movement)

func attract_controller(event: InputEventMouseButton, controller: XRController3D):
if not camera:
return
var direction = -1

if not event.pressed:
Expand Down
Binary file modified github-assets/assigngif.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ config_version=5

config/name="vremulator"
run/main_scene="res://Spatial.tscn"
config/features=PackedStringArray("4.0")
config/features=PackedStringArray("4.2")
config/icon="res://icon.png"

[autoload]

XRToolsUserSettings="*res://addons/godot-xr-tools/user_settings/user_settings.gd"
XrSimulator="*res://addons/xr-simulator/XRSimulator.tscn"

[editor_plugins]

Expand Down

0 comments on commit efd5194

Please sign in to comment.