create overlay plugin with support for tracking controllers or headset

This commit is contained in:
Crispy 2021-05-15 23:55:51 +02:00
parent e7fb97cf4d
commit a2aae82364
12 changed files with 205 additions and 41 deletions

View file

@ -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
}

17
ovr-utils/Main.tscn Normal file
View file

@ -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 )

View file

@ -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"

View file

@ -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="."]

View file

@ -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")

View file

@ -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())

View file

@ -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())

View file

@ -0,0 +1,7 @@
[plugin]
name="OpenVR Overlay"
description="Toolkit for making OpenVR overlays in Godot."
author="CrispyPin"
version=""
script="openvr_overlay.gd"

View file

@ -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

View file

@ -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 )]

View file

@ -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" ]

7
ovr-utils/test.gd Normal file
View file

@ -0,0 +1,7 @@
extends Node
func _ready() -> void:
$OverlayInstance.offset_pos = Vector3(1,2,3)
$OverlayInstance.offset_pos.x = 4