diff --git a/Spatial.tscn b/Spatial.tscn index 4823d84..96b784d 100644 --- a/Spatial.tscn +++ b/Spatial.tscn @@ -1,4 +1,4 @@ -[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"] @@ -6,7 +6,6 @@ [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"] @@ -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"] diff --git a/addons/xr-simulator/XRSimulator.gd b/addons/xr-simulator/XRSimulator.gd index cedeec3..6941e1f 100644 --- a/addons/xr-simulator/XRSimulator.gd +++ b/addons/xr-simulator/XRSimulator.gd @@ -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 @@ -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: @@ -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 @@ -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) @@ -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: diff --git a/github-assets/assigngif.gif b/github-assets/assigngif.gif index 10e1d03..5696f8c 100644 Binary files a/github-assets/assigngif.gif and b/github-assets/assigngif.gif differ diff --git a/project.godot b/project.godot index 9115f50..4fa1bb5 100644 --- a/project.godot +++ b/project.godot @@ -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]