diff --git a/ovr-utils/Overlay.tscn b/ovr-utils/DemoOverlay.tscn similarity index 70% rename from ovr-utils/Overlay.tscn rename to ovr-utils/DemoOverlay.tscn index a115deb..6b3f646 100644 --- a/ovr-utils/Overlay.tscn +++ b/ovr-utils/DemoOverlay.tscn @@ -3,8 +3,10 @@ [ext_resource path="res://icon.png" type="Texture" id=1] [node name="Overlay" type="Control"] -margin_right = 561.0 -margin_bottom = 391.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 __meta__ = { "_edit_use_anchors_": false } @@ -14,3 +16,6 @@ anchor_right = 1.0 anchor_bottom = 1.0 texture = ExtResource( 1 ) expand = true +__meta__ = { +"_edit_use_anchors_": false +} diff --git a/ovr-utils/Main.tscn b/ovr-utils/Main.tscn new file mode 100644 index 0000000..06abb20 --- /dev/null +++ b/ovr-utils/Main.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://addons/openvr_overlay/OverlayInstance.tscn" type="PackedScene" id=1] +[ext_resource path="res://DemoOverlay.tscn" type="PackedScene" id=2] + +[node name="Overlays" type="Node"] + +[node name="OverlayInstance" parent="." instance=ExtResource( 1 )] +target = 1 +overlay_scene = ExtResource( 2 ) +offset_pos = Vector3( 0, 0, 0.2 ) +offset_rot = Vector3( 0, -60, -45 ) +fallback_to_hmd = true + +[node name="OverlayInstance2" parent="." instance=ExtResource( 1 )] +target = 2 +offset_pos = Vector3( 0, 0, -0.2 ) diff --git a/ovr-utils/addons/openvr_overlay/MissingOverlay.tscn b/ovr-utils/addons/openvr_overlay/MissingOverlay.tscn new file mode 100644 index 0000000..1989b02 --- /dev/null +++ b/ovr-utils/addons/openvr_overlay/MissingOverlay.tscn @@ -0,0 +1,24 @@ +[gd_scene format=2] + +[node name="Overlay" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="ColorRect" type="ColorRect" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +color = Color( 0.601562, 0, 0, 0.501961 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Label" type="Label" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +rect_scale = Vector2( 8, 8 ) +text = "No overlay assigned" diff --git a/ovr-utils/addons/openvr_overlay/OverlayInstance.tscn b/ovr-utils/addons/openvr_overlay/OverlayInstance.tscn new file mode 100644 index 0000000..282e285 --- /dev/null +++ b/ovr-utils/addons/openvr_overlay/OverlayInstance.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://addons/openvr_overlay/overlay_instance.gd" type="Script" id=1] +[ext_resource path="res://addons/godot-openvr/OpenVROverlay.gdns" type="Script" id=3] + +[node name="OverlayInstance" type="Node"] +script = ExtResource( 1 ) + +[node name="OverlayViewport" type="Viewport" parent="."] +size = Vector2( 2160, 2160 ) +transparent_bg = true +handle_input_locally = false +msaa = 3 +hdr = false +render_target_update_mode = 3 +script = ExtResource( 3 ) +overlay_width_in_meters = 0.4 + +[node name="Offset" type="Spatial" parent="."] diff --git a/ovr-utils/addons/openvr_overlay/openvr_overlay.gd b/ovr-utils/addons/openvr_overlay/openvr_overlay.gd new file mode 100644 index 0000000..a69e513 --- /dev/null +++ b/ovr-utils/addons/openvr_overlay/openvr_overlay.gd @@ -0,0 +1,10 @@ +tool +extends EditorPlugin + + +func _enter_tree() -> void: + add_autoload_singleton("OverlayInit", "res://addons/openvr_overlay/overlay_init.gd") + + +func _exit_tree() -> void: + remove_autoload_singleton("OverlayInit") diff --git a/ovr-utils/addons/openvr_overlay/overlay_init.gd b/ovr-utils/addons/openvr_overlay/overlay_init.gd new file mode 100644 index 0000000..676549c --- /dev/null +++ b/ovr-utils/addons/openvr_overlay/overlay_init.gd @@ -0,0 +1,18 @@ +extends Node + +var ovr_interface: ARVRInterface + +func _init() -> void: + var ovr_config = preload("res://addons/godot-openvr/OpenVRConfig.gdns").new() + ovr_config.set_application_type(2) # Set to OVERLAY MODE = 2, NORMAL MODE = 1 + ovr_config.set_tracking_universe(1) # Set to SEATED MODE = 0, STANDING MODE = 1, RAW MODE = 2 + + # Find the OpenVR interface and initialise it + ovr_interface = ARVRServer.find_interface("OpenVR") + if ovr_interface and ovr_interface.initialize(): + pass + +func _ready() -> void: + for i in ARVRServer.get_tracker_count(): + var tracker = ARVRServer.get_tracker(i) + print(tracker.get_name(), ": hand ", tracker.get_hand()) diff --git a/ovr-utils/addons/openvr_overlay/overlay_instance.gd b/ovr-utils/addons/openvr_overlay/overlay_instance.gd new file mode 100644 index 0000000..c65c091 --- /dev/null +++ b/ovr-utils/addons/openvr_overlay/overlay_instance.gd @@ -0,0 +1,87 @@ +extends Node + +enum TARGETS { head, left, right, world } +export (TARGETS) var target = TARGETS.head +export var overlay_scene = preload("res://addons/openvr_overlay/MissingOverlay.tscn") setget _set_overlay_scene +export var offset_pos := Vector3(0, 0, -1) setget _set_offset_pos +export var offset_rot: Vector3 setget _set_offset_rot +export var width_meters = 0.4 +export var fallback_to_hmd = false + +var _tracker_id: int = 0 setget ,get_tracker_id + + +func _ready() -> void: + ARVRServer.connect("tracker_added", self, "_tracker_changed") + ARVRServer.connect("tracker_removed", self, "_tracker_changed") + + $OverlayViewport.overlay_width_in_meters = width_meters + $OverlayViewport.size = OverlayInit.ovr_interface.get_render_targetsize() + if overlay_scene: + $OverlayViewport.add_child(overlay_scene.instance()) + + update_tracker_id() + update_offset() + + +#func _process(_delta: float) -> void: +# update_offset() + + +func update_tracker_id() -> void: + _tracker_id = -1 + + if target in [TARGETS.left, TARGETS.right]: # target is a controller + for i in ARVRServer.get_tracker_count(): + var tracker = ARVRServer.get_tracker(i) + if tracker.get_hand() == target: + _tracker_id = int(tracker.get_name().split("_")[-1]) + + if _tracker_id == -1: + print("cound not find controller") + # could not find controller, overlay will revert to fallback + if fallback_to_hmd: + _tracker_id = 0 # HMD + else: + _tracker_id = 63 # World origin + + +func update_offset() -> void: + $Offset.translation = offset_pos + $Offset.rotation_degrees = offset_rot + print(_tracker_id) + match target: + TARGETS.head: + $OverlayViewport.track_relative_to_device(0, $Offset.transform) + TARGETS.world: + $OverlayViewport.overlay_position_absolute($Offset.transform) + _: + $OverlayViewport.track_relative_to_device(_tracker_id, $Offset.transform) + + +func _tracker_changed(tracker_name: String, type: int, id: int): + print("tracker changed: ", tracker_name) + update_tracker_id() + update_offset() + + +func get_tracker_id() -> int: + return _tracker_id + + +func _set_offset_pos(pos: Vector3): + offset_pos = pos + update_offset() + + +func _set_offset_rot(rot: Vector3): + offset_rot = rot + update_offset() + + +func _set_overlay_scene(scene: PackedScene): + overlay_scene = scene + if $OverlayViewport.get_children(): + $OverlayViewport.get_child(0).queue_free() + $OverlayViewport.add_child(overlay_scene.instance()) + diff --git a/ovr-utils/addons/openvr_overlay/plugin.cfg b/ovr-utils/addons/openvr_overlay/plugin.cfg new file mode 100644 index 0000000..fb5e03f --- /dev/null +++ b/ovr-utils/addons/openvr_overlay/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="OpenVR Overlay" +description="Toolkit for making OpenVR overlays in Godot." +author="CrispyPin" +version="" +script="openvr_overlay.gd" diff --git a/ovr-utils/main.gd b/ovr-utils/main.gd deleted file mode 100644 index 361b2e4..0000000 --- a/ovr-utils/main.gd +++ /dev/null @@ -1,21 +0,0 @@ -extends Spatial - -var size : Vector2 - -func _init() -> void: - #pass -#func _ready() -> void: - var OpenVRConfig = preload("res://addons/godot-openvr/OpenVRConfig.gdns").new() - OpenVRConfig.set_application_type(2) # Set to OVERLAY MODE = 2, NORMAL MODE = 1 - OpenVRConfig.set_tracking_universe(1) # Set to SEATED MODE = 0, STANDING MODE = 1, RAW MODE = 2 - - # Find the OpenVR interface and initialise it - var arvr_interface : ARVRInterface = ARVRServer.find_interface("OpenVR") - - if arvr_interface and arvr_interface.initialize(): - size = arvr_interface.get_render_targetsize() - -func _ready() -> void: - $Viewport.size = size - #$VRViewport.size = size - diff --git a/ovr-utils/main.tscn b/ovr-utils/main.tscn deleted file mode 100644 index fef13e3..0000000 --- a/ovr-utils/main.tscn +++ /dev/null @@ -1,17 +0,0 @@ -[gd_scene load_steps=4 format=2] - -[ext_resource path="res://main.gd" type="Script" id=1] -[ext_resource path="res://addons/godot-openvr/OpenVROverlay.gdns" type="Script" id=3] -[ext_resource path="res://Overlay.tscn" type="PackedScene" id=4] - -[node name="Spatial" type="Spatial"] -script = ExtResource( 1 ) - -[node name="Viewport" type="Viewport" parent="."] -arvr = true -size = Vector2( 100, 100 ) -transparent_bg = true -render_target_update_mode = 3 -script = ExtResource( 3 ) - -[node name="Overlay" parent="Viewport" instance=ExtResource( 4 )] diff --git a/ovr-utils/project.godot b/ovr-utils/project.godot index d58e965..dd7026b 100644 --- a/ovr-utils/project.godot +++ b/ovr-utils/project.godot @@ -11,9 +11,17 @@ config_version=4 [application] config/name="ovr-utils" -run/main_scene="res://main.tscn" +run/main_scene="res://Main.tscn" config/icon="res://icon.png" +[autoload] + +OverlayInit="*res://addons/openvr_overlay/overlay_init.gd" + +[editor_plugins] + +enabled=PoolStringArray( "res://addons/openvr_overlay/plugin.cfg" ) + [gdnative] singletons=[ "res://addons/godot-openvr/godot_openvr.gdnlib" ] diff --git a/ovr-utils/test.gd b/ovr-utils/test.gd new file mode 100644 index 0000000..29a2099 --- /dev/null +++ b/ovr-utils/test.gd @@ -0,0 +1,7 @@ +extends Node + + +func _ready() -> void: + $OverlayInstance.offset_pos = Vector3(1,2,3) + $OverlayInstance.offset_pos.x = 4 +