This commit is contained in:
Sveske-Juice 2025-04-05 18:50:26 +02:00
parent c351ec7f4a
commit 5913a5c314
7 changed files with 276 additions and 162 deletions

View file

@ -0,0 +1,266 @@
extends Node
enum ControllerSelectionMode {Hold, Toggle}
@export var enabled: bool
@export var controller_selection_mode: ControllerSelectionMode = ControllerSelectionMode.Hold
@export var device_x_sensitivity: float = 1
@export var device_y_sensitivity: float = 1
@export var scroll_sensitivity: float = 1
@export var is_camera_height_limited: bool = true
@export var min_camera_height: float = 0.5
@export var max_camera_height: float = 2.0
var camera: XRCamera3D
var left_controller: XRController3D
var right_controller: XRController3D
var left_tracker: XRPositionalTracker
var right_tracker: XRPositionalTracker
var toggle_left_controller = false
var toggle_right_controller = false
var toggle_shift = false
var key_map = {
KEY_1: "by_button",
KEY_2: "ax_button",
KEY_3: "by_touch",
KEY_4: "ax_touch",
KEY_5: "trigger_touch",
KEY_6: "grip_touch",
KEY_7: "secondary_click",
KEY_8: "secondary_touch",
KEY_9: "",
KEY_0: "",
KEY_MINUS: "primary_click",
KEY_EQUAL: "primary_touch",
KEY_BACKSPACE: "",
KEY_ENTER: "menu_button"
}
@onready var viewport: Viewport = get_viewport()
func _on_node_added(node: Node):
if node is XRCamera3D:
camera = node
camera.rotate_y(deg_to_rad(1.0))
elif node is XRController3D:
var pose = node.pose
if node.tracker == "left_hand":
node.position.x += -0.1
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":
node.position.x += 0.1
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
var left_hand = XRServer.get_tracker("left_hand")
if left_hand == null:
left_tracker = XRPositionalTracker.new()
left_tracker.type = XRServer.TRACKER_CONTROLLER
left_tracker.hand = XRPositionalTracker.TRACKER_HAND_LEFT
left_tracker.name = "left_hand"
else:
left_tracker = left_hand
var right_hand = XRServer.get_tracker("right_hand")
if right_hand == null:
right_tracker = XRPositionalTracker.new()
right_tracker.type = XRServer.TRACKER_CONTROLLER
right_tracker.hand = XRPositionalTracker.TRACKER_HAND_RIGHT
right_tracker.name = "right_hand"
else:
right_tracker = right_hand
get_tree().node_added.connect(_on_node_added)
_search_first_xr_nodes(get_tree().root)
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _input(event):
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
elif Input.mouse_mode != Input.MOUSE_MODE_CAPTURED and event is InputEventMouseButton:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
if Input.mouse_mode != Input.MOUSE_MODE_CAPTURED:
return
simulate_joysticks()
var is_any_controller_selected = false
if event is InputEventMouseMotion:
if Input.is_physical_key_pressed(KEY_Q) or toggle_left_controller:
is_any_controller_selected = true
if Input.is_key_pressed(KEY_SHIFT):
rotate_device(event, left_controller)
else:
move_controller(event, left_controller)
if Input.is_physical_key_pressed(KEY_E) or toggle_right_controller:
is_any_controller_selected = true
if Input.is_key_pressed(KEY_SHIFT):
rotate_device(event, right_controller)
else:
move_controller(event, right_controller)
if not is_any_controller_selected:
rotate_device(event, camera)
elif event is InputEventMouseButton:
if Input.is_physical_key_pressed(KEY_Q) or toggle_left_controller:
is_any_controller_selected = true
if not Input.is_key_pressed(KEY_SHIFT):
attract_controller(event, left_controller)
else:
rotate_z_axis(event, left_controller)
simulate_trigger(event, left_controller)
simulate_grip(event, left_controller)
if Input.is_physical_key_pressed(KEY_E) or toggle_right_controller:
is_any_controller_selected = true
if not Input.is_key_pressed(KEY_SHIFT):
attract_controller(event, right_controller)
else:
rotate_z_axis(event, right_controller)
simulate_trigger(event, right_controller)
simulate_grip(event, right_controller)
if not is_any_controller_selected:
camera_height(event)
elif event is InputEventKey:
if controller_selection_mode == ControllerSelectionMode.Toggle and event.pressed:
if event.keycode == KEY_Q:
toggle_left_controller = !toggle_left_controller
elif event.keycode == KEY_E:
toggle_right_controller = !toggle_right_controller
if Input.is_physical_key_pressed(KEY_Q) or toggle_left_controller:
simulate_buttons(event, left_controller)
if Input.is_physical_key_pressed(KEY_E) or toggle_right_controller:
simulate_buttons(event, right_controller)
func camera_height(event: InputEventMouseButton):
var direction = -1
if not event.pressed:
return
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
direction = 1
elif event.button_index != MOUSE_BUTTON_WHEEL_DOWN:
return
var pos = camera.transform.origin
var camera_y = pos.y + (scroll_sensitivity * direction)/20
if (camera_y >= max_camera_height or camera_y <= min_camera_height) and is_camera_height_limited:
camera_y = pos.y
camera.transform.origin = Vector3(pos.x, camera_y , pos.z)
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)
func simulate_trigger(event: InputEventMouseButton, controller: XRController3D):
if event.button_index == MOUSE_BUTTON_LEFT:
if controller.tracker == "left_hand":
left_tracker.set_input("trigger", float(event.pressed))
left_tracker.set_input("trigger_click", event.pressed)
else:
right_tracker.set_input("trigger", float(event.pressed))
right_tracker.set_input("trigger_click", event.pressed)
func simulate_grip(event: InputEventMouseButton, controller: XRController3D):
if event.button_index == MOUSE_BUTTON_RIGHT:
if controller.tracker == "left_hand":
left_tracker.set_input("grip", float(event.pressed))
left_tracker.set_input("grip_click", event.pressed)
else:
right_tracker.set_input("grip", float(event.pressed))
right_tracker.set_input("grip_click", event.pressed)
func simulate_buttons(event: InputEventKey, controller: XRController3D):
if key_map.has(event.keycode):
var button = key_map[event.keycode]
if controller.tracker == "left_hand":
left_tracker.set_input(button, event.pressed)
else:
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:
return
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
direction = 1
elif event.button_index != MOUSE_BUTTON_WHEEL_DOWN:
return
var distance_vector = controller.global_transform.origin - camera.global_transform.origin
var forward = distance_vector.normalized() * direction
var movement = distance_vector + forward * (scroll_sensitivity/20)
if distance_vector.length() > 0.1 and movement.length() > 0.1:
controller.global_translate(forward * (scroll_sensitivity/20))
func rotate_z_axis(event: InputEventMouseButton, controller: XRController3D):
var direction = -1
if not event.pressed:
return
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
direction = 1
elif event.button_index != MOUSE_BUTTON_WHEEL_DOWN:
return
controller.rotate_z(scroll_sensitivity * direction/20.0)
func rotate_device(event: InputEventMouseMotion, device: Node3D):
var motion = event.relative
device.rotate_y(motion.x * -device_x_sensitivity/1000)
device.rotate(device.transform.basis.x, motion.y * -device_y_sensitivity/1000)
func vector_key_mapping(key_positive_x: int, key_negative_x: int, key_positive_y: int, key_negative_y: int):
var x = 0
var y = 0
if Input.is_physical_key_pressed (key_positive_y):
y = 1
elif Input.is_physical_key_pressed (key_negative_y):
y = -1
if Input.is_physical_key_pressed (key_positive_x):
x = 1
elif Input.is_physical_key_pressed (key_negative_x):
x = -1
var vec = Vector2(x, y)
if vec:
vec = vec.normalized()
return vec

View file

@ -0,0 +1 @@
uid://d3jtwphbxfpde

View file

@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://ctltchlf2j2r4"]
[ext_resource type="Script" path="res://addons/xr-simulator/XRSimulator.gd" id="1_uljju"]
[node name="XRSimulator" type="Node"]
script = ExtResource("1_uljju")
enabled = true

BIN
models/blender/desk.blend Normal file

Binary file not shown.

BIN
models/blender/desk.blend1 Normal file

Binary file not shown.

BIN
models/desk.fbx Normal file

Binary file not shown.

View file

@ -1,4 +1,4 @@
[gd_resource type="OpenXRActionMap" load_steps=111 format=3 uid="uid://bcubmw1ntpto1"]
[gd_resource type="OpenXRActionMap" load_steps=71 format=3 uid="uid://bcubmw1ntpto1"]
[sub_resource type="OpenXRAction" id="OpenXRAction_6ivru"]
resource_name = "trigger"
@ -200,158 +200,6 @@ binding_path = "/user/hand/right/output/haptic"
interaction_profile_path = "/interaction_profiles/khr/simple_controller"
bindings = [SubResource("OpenXRIPBinding_r3qn1"), SubResource("OpenXRIPBinding_n01b8"), SubResource("OpenXRIPBinding_pjtev"), SubResource("OpenXRIPBinding_nqyri"), SubResource("OpenXRIPBinding_86uui"), SubResource("OpenXRIPBinding_nrtxc"), SubResource("OpenXRIPBinding_qovyo"), SubResource("OpenXRIPBinding_d6uso"), SubResource("OpenXRIPBinding_hvi7v"), SubResource("OpenXRIPBinding_7dxun"), SubResource("OpenXRIPBinding_rp8ih"), SubResource("OpenXRIPBinding_0uca0"), SubResource("OpenXRIPBinding_rjtq8"), SubResource("OpenXRIPBinding_lce2q")]
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_lng5j"]
action = SubResource("OpenXRAction_oi0ij")
binding_path = "/user/hand/left/input/aim/pose"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_aeeoj"]
action = SubResource("OpenXRAction_oi0ij")
binding_path = "/user/hand/right/input/aim/pose"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_gosqu"]
action = SubResource("OpenXRAction_m08eo")
binding_path = "/user/hand/left/input/aim/pose"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_n52fm"]
action = SubResource("OpenXRAction_m08eo")
binding_path = "/user/hand/right/input/aim/pose"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_vushy"]
action = SubResource("OpenXRAction_c4j1d")
binding_path = "/user/hand/left/input/grip/pose"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_lbhgg"]
action = SubResource("OpenXRAction_c4j1d")
binding_path = "/user/hand/right/input/grip/pose"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_m1cgb"]
action = SubResource("OpenXRAction_sopde")
binding_path = "/user/hand/left/input/palm_ext/pose"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_yfktj"]
action = SubResource("OpenXRAction_sopde")
binding_path = "/user/hand/right/input/palm_ext/pose"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_kjhen"]
action = SubResource("OpenXRAction_3p2as")
binding_path = "/user/hand/left/input/system/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_32kw4"]
action = SubResource("OpenXRAction_3p2as")
binding_path = "/user/hand/right/input/system/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_ktbxl"]
action = SubResource("OpenXRAction_iphn4")
binding_path = "/user/hand/left/input/menu/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_8ldfe"]
action = SubResource("OpenXRAction_wdehm")
binding_path = "/user/hand/left/input/x/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_nueak"]
action = SubResource("OpenXRAction_wdehm")
binding_path = "/user/hand/right/input/a/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_vopyr"]
action = SubResource("OpenXRAction_clfly")
binding_path = "/user/hand/left/input/x/touch"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_rgbyv"]
action = SubResource("OpenXRAction_clfly")
binding_path = "/user/hand/right/input/a/touch"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_bflds"]
action = SubResource("OpenXRAction_e1frq")
binding_path = "/user/hand/left/input/y/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_pueci"]
action = SubResource("OpenXRAction_e1frq")
binding_path = "/user/hand/right/input/b/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_jn5l0"]
action = SubResource("OpenXRAction_l7aq8")
binding_path = "/user/hand/left/input/y/touch"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_44ra8"]
action = SubResource("OpenXRAction_l7aq8")
binding_path = "/user/hand/right/input/b/touch"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_bh82f"]
action = SubResource("OpenXRAction_6ivru")
binding_path = "/user/hand/left/input/trigger/value"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_7b312"]
action = SubResource("OpenXRAction_6ivru")
binding_path = "/user/hand/right/input/trigger/value"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_ajt26"]
action = SubResource("OpenXRAction_vfhwq")
binding_path = "/user/hand/left/input/trigger/value"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_grl1h"]
action = SubResource("OpenXRAction_vfhwq")
binding_path = "/user/hand/right/input/trigger/value"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_dlpx3"]
action = SubResource("OpenXRAction_5w03k")
binding_path = "/user/hand/left/input/trigger/touch"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_s4h6a"]
action = SubResource("OpenXRAction_5w03k")
binding_path = "/user/hand/right/input/trigger/touch"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_0njdn"]
action = SubResource("OpenXRAction_typ1r")
binding_path = "/user/hand/left/input/squeeze/value"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_xtpgr"]
action = SubResource("OpenXRAction_typ1r")
binding_path = "/user/hand/right/input/squeeze/value"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_sm8ja"]
action = SubResource("OpenXRAction_clvbf")
binding_path = "/user/hand/left/input/squeeze/value"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_fyyqw"]
action = SubResource("OpenXRAction_clvbf")
binding_path = "/user/hand/right/input/squeeze/value"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_6yfaw"]
action = SubResource("OpenXRAction_3k6la")
binding_path = "/user/hand/left/input/thumbstick"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_x7rhh"]
action = SubResource("OpenXRAction_3k6la")
binding_path = "/user/hand/right/input/thumbstick"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_8cuio"]
action = SubResource("OpenXRAction_i8esw")
binding_path = "/user/hand/left/input/thumbstick/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_v0kom"]
action = SubResource("OpenXRAction_i8esw")
binding_path = "/user/hand/right/input/thumbstick/click"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_v1men"]
action = SubResource("OpenXRAction_um1hv")
binding_path = "/user/hand/left/input/thumbstick/touch"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_qujgh"]
action = SubResource("OpenXRAction_um1hv")
binding_path = "/user/hand/right/input/thumbstick/touch"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_8xxre"]
action = SubResource("OpenXRAction_sow2k")
binding_path = "/user/hand/left/output/haptic"
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_jceb4"]
action = SubResource("OpenXRAction_sow2k")
binding_path = "/user/hand/right/output/haptic"
[sub_resource type="OpenXRInteractionProfile" id="OpenXRInteractionProfile_lvl5r"]
interaction_profile_path = "/interaction_profiles/bytedance/pico4_controller"
bindings = [SubResource("OpenXRIPBinding_lng5j"), SubResource("OpenXRIPBinding_aeeoj"), SubResource("OpenXRIPBinding_gosqu"), SubResource("OpenXRIPBinding_n52fm"), SubResource("OpenXRIPBinding_vushy"), SubResource("OpenXRIPBinding_lbhgg"), SubResource("OpenXRIPBinding_m1cgb"), SubResource("OpenXRIPBinding_yfktj"), SubResource("OpenXRIPBinding_kjhen"), SubResource("OpenXRIPBinding_32kw4"), SubResource("OpenXRIPBinding_ktbxl"), SubResource("OpenXRIPBinding_8ldfe"), SubResource("OpenXRIPBinding_nueak"), SubResource("OpenXRIPBinding_vopyr"), SubResource("OpenXRIPBinding_rgbyv"), SubResource("OpenXRIPBinding_bflds"), SubResource("OpenXRIPBinding_pueci"), SubResource("OpenXRIPBinding_jn5l0"), SubResource("OpenXRIPBinding_44ra8"), SubResource("OpenXRIPBinding_bh82f"), SubResource("OpenXRIPBinding_7b312"), SubResource("OpenXRIPBinding_ajt26"), SubResource("OpenXRIPBinding_grl1h"), SubResource("OpenXRIPBinding_dlpx3"), SubResource("OpenXRIPBinding_s4h6a"), SubResource("OpenXRIPBinding_0njdn"), SubResource("OpenXRIPBinding_xtpgr"), SubResource("OpenXRIPBinding_sm8ja"), SubResource("OpenXRIPBinding_fyyqw"), SubResource("OpenXRIPBinding_6yfaw"), SubResource("OpenXRIPBinding_x7rhh"), SubResource("OpenXRIPBinding_8cuio"), SubResource("OpenXRIPBinding_v0kom"), SubResource("OpenXRIPBinding_v1men"), SubResource("OpenXRIPBinding_qujgh"), SubResource("OpenXRIPBinding_8xxre"), SubResource("OpenXRIPBinding_jceb4")]
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_61gsj"]
action = SubResource("OpenXRAction_oi0ij")
binding_path = "/user/hand/left/input/aim/pose"
@ -476,14 +324,6 @@ binding_path = "/user/hand/right/output/haptic"
interaction_profile_path = "/interaction_profiles/htc/vive_cosmos_controller"
bindings = [SubResource("OpenXRIPBinding_61gsj"), SubResource("OpenXRIPBinding_f63eo"), SubResource("OpenXRIPBinding_3megw"), SubResource("OpenXRIPBinding_1875n"), SubResource("OpenXRIPBinding_jrx7l"), SubResource("OpenXRIPBinding_sddo8"), SubResource("OpenXRIPBinding_5idg5"), SubResource("OpenXRIPBinding_5vlhu"), SubResource("OpenXRIPBinding_yto2p"), SubResource("OpenXRIPBinding_35s7d"), SubResource("OpenXRIPBinding_cukgo"), SubResource("OpenXRIPBinding_58wje"), SubResource("OpenXRIPBinding_x1ifb"), SubResource("OpenXRIPBinding_hw16p"), SubResource("OpenXRIPBinding_gdlwa"), SubResource("OpenXRIPBinding_drau7"), SubResource("OpenXRIPBinding_0vq03"), SubResource("OpenXRIPBinding_y7ek0"), SubResource("OpenXRIPBinding_cy6rb"), SubResource("OpenXRIPBinding_6r8a6"), SubResource("OpenXRIPBinding_vmwlg"), SubResource("OpenXRIPBinding_qcgh6"), SubResource("OpenXRIPBinding_bwwah"), SubResource("OpenXRIPBinding_rtyas"), SubResource("OpenXRIPBinding_hk5ci"), SubResource("OpenXRIPBinding_4j055"), SubResource("OpenXRIPBinding_awtpp"), SubResource("OpenXRIPBinding_xh6fl"), SubResource("OpenXRIPBinding_ixewl"), SubResource("OpenXRIPBinding_qwqvw")]
[sub_resource type="OpenXRIPBinding" id="OpenXRIPBinding_g2awd"]
action = SubResource("OpenXRAction_oi0ij")
binding_path = "/user/eyes_ext/input/gaze_ext/pose"
[sub_resource type="OpenXRInteractionProfile" id="OpenXRInteractionProfile_asb4g"]
interaction_profile_path = "/interaction_profiles/ext/eye_gaze_interaction"
bindings = [SubResource("OpenXRIPBinding_g2awd")]
[resource]
action_sets = [SubResource("OpenXRActionSet_ngwcy")]
interaction_profiles = [SubResource("OpenXRInteractionProfile_ckeh6"), SubResource("OpenXRInteractionProfile_lvl5r"), SubResource("OpenXRInteractionProfile_oqlrv"), SubResource("OpenXRInteractionProfile_asb4g")]
interaction_profiles = [SubResource("OpenXRInteractionProfile_ckeh6"), SubResource("OpenXRInteractionProfile_oqlrv")]