mirror of
https://github.com/CrispyPin/ovr-utils.git
synced 2024-11-22 15:40:25 +01:00
store offsets for all tracking targets, for fallback; switch target when dragging
This commit is contained in:
parent
5b55d3ce81
commit
52a5e346f9
5 changed files with 108 additions and 57 deletions
|
@ -12,23 +12,19 @@
|
|||
|
||||
[node name="OverlayInstance FPS" parent="." instance=ExtResource( 1 )]
|
||||
transform = Transform( -4.37114e-08, -0.707107, -0.707107, 0, -0.707107, 0.707107, -1, 3.09086e-08, 3.09086e-08, 0.05, -0.01, 0.15 )
|
||||
target = 1
|
||||
overlay_scene = ExtResource( 2 )
|
||||
|
||||
[node name="OverlayInstance Time" parent="." instance=ExtResource( 1 )]
|
||||
transform = Transform( -4.37114e-08, -0.707107, -0.707107, 0, -0.707107, 0.707107, -1, 3.09086e-08, 3.09086e-08, -0.05, 0.02, 0.15 )
|
||||
target = 1
|
||||
overlay_scene = ExtResource( 3 )
|
||||
width_meters = 0.1
|
||||
|
||||
[node name="OverlayInstance Battery" parent="." instance=ExtResource( 1 )]
|
||||
transform = Transform( -4.37114e-08, -0.707107, -0.707107, 0, -0.707107, 0.707107, -1, 3.09086e-08, 3.09086e-08, -0.12, 0, 0.1 )
|
||||
target = 1
|
||||
overlay_scene = ExtResource( 7 )
|
||||
|
||||
[node name="OverlayInstance UI demo" parent="." instance=ExtResource( 1 )]
|
||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.25 )
|
||||
target = 1
|
||||
overlay_scene = ExtResource( 5 )
|
||||
|
||||
[node name="OverlayInteraction" parent="OverlayInstance UI demo" instance=ExtResource( 8 )]
|
||||
|
|
|
@ -8,19 +8,19 @@ script = ExtResource( 1 )
|
|||
|
||||
[node name="VR" type="ARVROrigin" parent="."]
|
||||
|
||||
[node name="LeftHand" type="ARVRController" parent="VR"]
|
||||
[node name="left" type="ARVRController" parent="VR"]
|
||||
|
||||
[node name="OverlayActivator" parent="VR/LeftHand" instance=ExtResource( 6 )]
|
||||
[node name="OverlayActivator" parent="VR/left" instance=ExtResource( 6 )]
|
||||
|
||||
[node name="RightHand" type="ARVRController" parent="VR"]
|
||||
[node name="right" type="ARVRController" parent="VR"]
|
||||
controller_id = 2
|
||||
|
||||
[node name="OverlayActivator" parent="VR/RightHand" instance=ExtResource( 6 )]
|
||||
[node name="OverlayActivator" parent="VR/right" instance=ExtResource( 6 )]
|
||||
|
||||
[node name="Head" type="ARVRCamera" parent="VR"]
|
||||
[node name="head" type="ARVRCamera" parent="VR"]
|
||||
visible = false
|
||||
|
||||
[connection signal="button_pressed" from="VR/LeftHand" to="." method="_on_LeftHand_button_pressed"]
|
||||
[connection signal="button_release" from="VR/LeftHand" to="." method="_on_LeftHand_button_release"]
|
||||
[connection signal="button_pressed" from="VR/RightHand" to="." method="_on_RightHand_button_pressed"]
|
||||
[connection signal="button_release" from="VR/RightHand" to="." method="_on_RightHand_button_release"]
|
||||
[connection signal="button_pressed" from="VR/left" to="." method="_on_LeftHand_button_pressed"]
|
||||
[connection signal="button_release" from="VR/left" to="." method="_on_LeftHand_button_release"]
|
||||
[connection signal="button_pressed" from="VR/right" to="." method="_on_RightHand_button_pressed"]
|
||||
[connection signal="button_release" from="VR/right" to="." method="_on_RightHand_button_release"]
|
||||
|
|
|
@ -17,10 +17,17 @@ var _right_is_activator = false
|
|||
var _left_is_activator = false
|
||||
|
||||
var is_moving := false
|
||||
var move_grab_pos: Vector3
|
||||
var move_grab_rot: Vector3
|
||||
var _pre_move_target = ""# what offset is being changed
|
||||
var _mover_hand_name = ""# left or right, which hand is grabbing (/active controller)
|
||||
var _mover_hand_offsets = {"pos": Vector3(), "rot": Vector3()} # original offsets for grabbing hand
|
||||
|
||||
onready var viewport: Viewport = get_node("../OverlayViewport")
|
||||
onready var tracker_nodes = {
|
||||
"head": $VR/head,
|
||||
"left": $VR/left,
|
||||
"right": $VR/right,
|
||||
"world": $VR
|
||||
}
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
|
@ -40,11 +47,6 @@ func _ready() -> void:
|
|||
|
||||
func _process(_delta: float) -> void:
|
||||
_update_cursor()
|
||||
if is_moving:
|
||||
var oc:Transform=_overlay_area.get_parent().transform # controller that overlay is attached to
|
||||
var global_target = move_grab_pos + _active_controller.translation
|
||||
global_target = oc.xform_inv(global_target)
|
||||
get_parent().translation = global_target# - _overlay_area.get_parent().translation
|
||||
|
||||
|
||||
func begin_move():
|
||||
|
@ -52,14 +54,36 @@ func begin_move():
|
|||
print("Could not begin moving overlay, no controller active. ", get_parent().name)
|
||||
return
|
||||
is_moving = true
|
||||
_pre_move_target = get_parent().current_target
|
||||
_mover_hand_name = _active_controller.name
|
||||
_mover_hand_offsets.pos = get_parent().translation
|
||||
_mover_hand_offsets.rot = get_parent().rotation_degrees
|
||||
|
||||
# offset from active controller to overlay
|
||||
# move_grab_pos = _active_controller.transform.xform_inv(_overlay_area.global_transform.origin)
|
||||
move_grab_pos = _overlay_area.global_transform.origin - _active_controller.translation
|
||||
move_grab_rot = Vector3()
|
||||
var rel_t = _active_controller.transform * _overlay_area.global_transform.inverse()
|
||||
# get_parent().offsets[_mover_hand_name].rot = (_overlay_area.global_transform.basis * _active_controller.transform.basis).get_euler() * 57.295
|
||||
get_parent().offsets[_mover_hand_name].rot = _active_controller.transform.xform_inv(_overlay_area.global_transform.basis.get_euler())*57
|
||||
|
||||
|
||||
get_parent().offsets[_mover_hand_name].pos = _active_controller.transform.xform_inv(_overlay_area.global_transform.origin)
|
||||
# get_parent().offsets[_mover_hand_name].pos = _active_controller.transform.xform_inv(_overlay_area.global_transform.origin)
|
||||
# get_parent().offsets[_mover_hand_name].rot = _overlay_area.get_parent().rotation_degrees + _overlay_area.rotation_degrees - _active_controller.rotation_degrees
|
||||
|
||||
get_parent().current_target = _mover_hand_name
|
||||
get_parent().update_tracker_id()
|
||||
get_parent().update_offset()
|
||||
|
||||
|
||||
func finish_move():
|
||||
is_moving = false
|
||||
var new_pos = tracker_nodes[_pre_move_target].transform.xform_inv(_overlay_area.global_transform.origin)
|
||||
# this is only local rotations??
|
||||
var new_rot = _overlay_area.get_parent().rotation_degrees + _overlay_area.rotation_degrees - tracker_nodes[_pre_move_target].rotation_degrees
|
||||
get_parent().update_current_target()
|
||||
get_parent().update_tracker_id()
|
||||
get_parent().offsets[get_parent().current_target].pos = new_pos
|
||||
get_parent().offsets[get_parent().current_target].rot = new_rot
|
||||
get_parent().update_offset()
|
||||
|
||||
|
||||
#get canvas position of active controller
|
||||
|
@ -139,40 +163,44 @@ func _update_offset():
|
|||
func _update_target():
|
||||
# move _overlay_area
|
||||
_overlay_area.get_parent().remove_child(_overlay_area)
|
||||
match get_parent().target:
|
||||
OverlayInstance.TARGETS.head:
|
||||
$VR/Head.add_child(_overlay_area)
|
||||
OverlayInstance.TARGETS.left:
|
||||
$VR/LeftHand.add_child(_overlay_area)
|
||||
OverlayInstance.TARGETS.right:
|
||||
$VR/RightHand.add_child(_overlay_area)
|
||||
OverlayInstance.TARGETS.world:
|
||||
$VR.add_child(_overlay_area)
|
||||
tracker_nodes[get_parent().current_target].add_child(_overlay_area)
|
||||
# match get_parent().target:
|
||||
# "head":
|
||||
# $VR/Head.add_child(_overlay_area)
|
||||
# "left":
|
||||
# $VR/LeftHand.add_child(_overlay_area)
|
||||
# "right":
|
||||
# $VR/RightHand.add_child(_overlay_area)
|
||||
# "world":
|
||||
# $VR.add_child(_overlay_area)
|
||||
|
||||
_left_is_activator = get_parent().target != OverlayInstance.TARGETS.left
|
||||
_right_is_activator = get_parent().target != OverlayInstance.TARGETS.right
|
||||
if is_moving:
|
||||
return
|
||||
|
||||
_left_is_activator = get_parent().current_target != "left"
|
||||
_right_is_activator = get_parent().current_target != "right"
|
||||
# toggle appropriate colliders
|
||||
$VR/LeftHand/OverlayActivator/Collision.disabled = !_left_is_activator
|
||||
$VR/RightHand/OverlayActivator/Collision.disabled = !_right_is_activator
|
||||
$VR/left/OverlayActivator/Collision.disabled = !_left_is_activator
|
||||
$VR/right/OverlayActivator/Collision.disabled = !_right_is_activator
|
||||
|
||||
|
||||
func _on_RightHand_button_pressed(button: int) -> void:
|
||||
if button == JOY_VR_TRIGGER and _right_is_activator:
|
||||
_trigger_on($VR/RightHand)
|
||||
_trigger_on(tracker_nodes.right)
|
||||
|
||||
|
||||
func _on_RightHand_button_release(button: int) -> void:
|
||||
if button == JOY_VR_TRIGGER and _active_controller == $VR/RightHand:
|
||||
if button == JOY_VR_TRIGGER and _active_controller == tracker_nodes.right:
|
||||
_trigger_off()
|
||||
|
||||
|
||||
func _on_LeftHand_button_pressed(button: int) -> void:
|
||||
if button == JOY_VR_TRIGGER and _left_is_activator:
|
||||
_trigger_on($VR/LeftHand)
|
||||
_trigger_on(tracker_nodes.left)
|
||||
|
||||
|
||||
func _on_LeftHand_button_release(button: int) -> void:
|
||||
if button == JOY_VR_TRIGGER and _active_controller == $VR/LeftHand:
|
||||
if button == JOY_VR_TRIGGER and _active_controller == tracker_nodes.right:
|
||||
_trigger_off()
|
||||
|
||||
|
||||
|
|
|
@ -5,13 +5,21 @@ signal width_changed
|
|||
signal offset_changed
|
||||
signal target_changed
|
||||
|
||||
enum TARGETS { head, left, right, world }
|
||||
export (TARGETS) var target = TARGETS.head setget set_target
|
||||
export var overlay_scene = preload("res://addons/openvr_overlay/MissingOverlay.tscn")\
|
||||
setget set_overlay_scene
|
||||
const TARGETS = ["head", "left", "right", "world"]
|
||||
export (String, "head", "left", "right", "world") var target = "left" setget set_target
|
||||
export var overlay_scene =\
|
||||
preload("res://addons/openvr_overlay/MissingOverlay.tscn") setget set_overlay_scene
|
||||
export var width_meters = 0.4 setget set_width_in_meters
|
||||
export var fallback_to_hmd = false # fallback is only applied if tracker is not present at startup
|
||||
# so this is not fully implemented
|
||||
|
||||
export var offsets:Dictionary = {
|
||||
"head": {"pos": Vector3(), "rot": Vector3()},
|
||||
"left": {"pos": Vector3(), "rot": Vector3()},
|
||||
"right": {"pos": Vector3(), "rot": Vector3()},
|
||||
"world": {"pos": Vector3(), "rot": Vector3()}
|
||||
}# also contains temp offset that is created when dragged
|
||||
|
||||
# what's actually tracking
|
||||
var current_target = target# most of the time the actual target, but will fall back
|
||||
|
||||
var _tracker_id: int = 0
|
||||
|
||||
|
@ -19,6 +27,11 @@ onready var container = $OverlayViewport/Container
|
|||
|
||||
|
||||
func _ready() -> void:
|
||||
# TEMP
|
||||
offsets.left.pos = translation
|
||||
offsets.left.rot = rotation_degrees
|
||||
###
|
||||
|
||||
ARVRServer.connect("tracker_added", self, "_tracker_changed")
|
||||
ARVRServer.connect("tracker_removed", self, "_tracker_changed")
|
||||
|
||||
|
@ -34,31 +47,37 @@ func _ready() -> void:
|
|||
|
||||
func update_tracker_id() -> void:
|
||||
_tracker_id = -1
|
||||
if target in [TARGETS.left, TARGETS.right]: # target is a controller
|
||||
if current_target in ["left", "right"]: # target is a controller
|
||||
var target_id = TARGETS.find(current_target)
|
||||
|
||||
for i in ARVRServer.get_tracker_count():
|
||||
var tracker = ARVRServer.get_tracker(i)
|
||||
if tracker.get_hand() == target:
|
||||
if tracker.get_hand() == target_id:
|
||||
_tracker_id = int(tracker.get_name().split("_")[-1])
|
||||
|
||||
if _tracker_id == -1:
|
||||
# could not find controller, overlay will revert to fallback
|
||||
# only happens if controller is missing on startup, otherwise it will register as being at origin
|
||||
if fallback_to_hmd:
|
||||
_tracker_id = 0 # HMD
|
||||
else:
|
||||
_tracker_id = 63 # World origin
|
||||
_tracker_id = 63 # highest tracker id (unused, at origin)
|
||||
|
||||
|
||||
func update_offset() -> void:
|
||||
match target:
|
||||
TARGETS.head:
|
||||
translation = offsets[current_target].pos
|
||||
rotation_degrees = offsets[current_target].rot
|
||||
|
||||
match current_target:
|
||||
"head":
|
||||
$OverlayViewport.track_relative_to_device(0, global_transform)
|
||||
TARGETS.world:
|
||||
"world":
|
||||
$OverlayViewport.overlay_position_absolute(global_transform)
|
||||
_:
|
||||
$OverlayViewport.track_relative_to_device(_tracker_id, global_transform)
|
||||
|
||||
|
||||
func update_current_target():
|
||||
current_target = target
|
||||
# TODO fallback if not found
|
||||
|
||||
|
||||
func _tracker_changed(tracker_name: String, type: int, id: int):
|
||||
update_tracker_id()
|
||||
update_offset()
|
||||
|
@ -68,7 +87,7 @@ func get_tracker_id() -> int:
|
|||
return _tracker_id
|
||||
|
||||
|
||||
func set_target(new: int):
|
||||
func set_target(new: String):
|
||||
target = new
|
||||
update_tracker_id()
|
||||
call_deferred("update_offset")
|
||||
|
|
|
@ -34,6 +34,14 @@ __meta__ = {
|
|||
"_edit_use_anchors_": false
|
||||
}
|
||||
|
||||
[node name="ColorRect" type="ColorRect" parent="."]
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
color = Color( 0.407843, 0, 1, 0.482353 )
|
||||
__meta__ = {
|
||||
"_edit_use_anchors_": false
|
||||
}
|
||||
|
||||
[node name="OptionButton" type="OptionButton" parent="."]
|
||||
margin_right = 111.0
|
||||
margin_bottom = 41.0
|
||||
|
|
Loading…
Reference in a new issue