Use more reference and manage thread in engine notifications

This commit is contained in:
rodolpheh 2023-11-13 21:08:19 +00:00
parent f56398230a
commit 37cbc53fd0
12 changed files with 111 additions and 66 deletions

31
demo/scripts/AR.gd Normal file
View File

@ -0,0 +1,31 @@
extends Node3D
var xr_interface: XRInterface
func _ready():
xr_interface = XRServer.find_interface("OpenXR")
if xr_interface and xr_interface.is_initialized():
print("OpenXR initialised successfully")
# Turn off v-sync!
DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
# Change our main viewport to output to the HMD
get_viewport().use_xr = true
enable_passthrough()
else:
print("OpenXR not initialized, please check if your headset is connected")
func enable_passthrough() -> bool:
if xr_interface and xr_interface.is_passthrough_supported():
if !xr_interface.start_passthrough():
return false
else:
var modes = xr_interface.get_supported_environment_blend_modes()
if xr_interface.XR_ENV_BLEND_MODE_ALPHA_BLEND in modes:
xr_interface.set_environment_blend_mode(xr_interface.XR_ENV_BLEND_MODE_ALPHA_BLEND)
else:
return false
get_viewport().transparent_bg = true
return true

View File

@ -2,10 +2,10 @@ extends UltraleapDeviceNode
func _process(_delta):
if Input.is_action_just_pressed("switch_to_desktop"):
request_tracking_mode(UltraleapTypes.Desktop)
set_tracking_mode(UltraleapTypes.Desktop)
if Input.is_action_just_pressed("switch_to_hmd"):
request_tracking_mode(UltraleapTypes.HMD)
set_tracking_mode(UltraleapTypes.HMD)
if Input.is_action_just_pressed("switch_to_screentop"):
request_tracking_mode(UltraleapTypes.Screentop)
set_tracking_mode(UltraleapTypes.Screentop)

View File

@ -29,11 +29,6 @@ func _process(_delta):
func _on_device_selection(serial):
# if current_device != null:
# _close_device()
# current_device = _get_device_from_serial(serial)
# _open_device()
# current_device_changed.emit(current_device)
var dev : UltraleapDevice = _get_device_from_serial(serial)
var current_dev : UltraleapDevice = tracking.get_primary_device()
@ -62,10 +57,6 @@ func _on_hand_tracking_device_added(device):
if tracking.devices.size() > 0:
emit_signal("presence_of_device_changed", true)
no_device.hide()
# if current_device == null:
# current_device = device
# current_device_changed.emit(current_device)
# _open_device()
func _on_hand_tracking_device_removed(_device):

View File

@ -10,6 +10,9 @@
using namespace godot;
void UltraleapBone::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_type"), &UltraleapBone::get_type);
ClassDB::bind_method(D_METHOD("set_type", "type"), &UltraleapBone::set_type);
ClassDB::bind_method(D_METHOD("get_prev_joint"), &UltraleapBone::get_prev_joint);
ClassDB::bind_method(D_METHOD("set_prev_joint", "prev_joint"), &UltraleapBone::set_prev_joint);
@ -22,6 +25,18 @@ void UltraleapBone::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_rotation"), &UltraleapBone::get_rotation);
ClassDB::bind_method(D_METHOD("set_rotation", "rotation"), &UltraleapBone::set_rotation);
ClassDB::add_property(
"UltraleapBone",
PropertyInfo(
Variant::INT,
"type",
PROPERTY_HINT_ENUM,
"Metacarpal,Proximal,Intermediate,Distal,Arm,None"
),
"set_type",
"get_type"
);
ClassDB::add_property(
"UltraleapBone",
PropertyInfo(
@ -63,9 +78,10 @@ void UltraleapBone::_bind_methods() {
);
}
void UltraleapBone::fill_bone_data(UltraleapBone* ul_bone, LEAP_BONE* bone) {
void UltraleapBone::fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone, BoneType type) {
ul_bone->prev_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&bone->prev_joint);
ul_bone->next_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&bone->next_joint);
ul_bone->width = bone->width / 1000;
ul_bone->rotation = UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&bone->rotation);
ul_bone->type = type;
}

View File

@ -19,12 +19,27 @@ class UltraleapBone : public Resource {
GDCLASS(UltraleapBone, Resource);
public:
enum BoneType {
Metacarpal,
Proximal,
Intermediate,
Distal,
Arm,
None
};
BoneType type = BoneType::None;
Vector3 prev_joint;
Vector3 next_joint;
float width;
Quaternion rotation;
// Getters / Setters
BoneType get_type() { return type; }
void set_type(BoneType value) { type = value; }
Vector3 get_prev_joint() { return prev_joint; }
void set_prev_joint(Vector3 value) { prev_joint = value; }
@ -37,9 +52,11 @@ public:
Quaternion get_rotation() { return rotation; }
void set_rotation(Quaternion value) { rotation = value; }
static void fill_bone_data(UltraleapBone* ul_bone, LEAP_BONE* bone);
static void fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone, BoneType type);
protected:
static void _bind_methods();
};
VARIANT_ENUM_CAST(UltraleapBone::BoneType);
#endif

View File

@ -13,6 +13,13 @@
using namespace godot;
UltraleapDigit::UltraleapDigit() {
metacarpal_ref = Ref<UltraleapBone>(memnew(UltraleapBone));
proximal_ref = Ref<UltraleapBone>(memnew(UltraleapBone));
intermediate_ref = Ref<UltraleapBone>(memnew(UltraleapBone));
distal_ref = Ref<UltraleapBone>(memnew(UltraleapBone));
}
void UltraleapDigit::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_type"), &UltraleapDigit::get_type);
ClassDB::bind_method(D_METHOD("set_type", "type"), &UltraleapDigit::set_type);
@ -103,11 +110,12 @@ void UltraleapDigit::_bind_methods() {
);
}
void UltraleapDigit::fill_digit_data(UltraleapDigit* ul_digit, LEAP_DIGIT* digit) {
void UltraleapDigit::fill_digit_data(Ref<UltraleapDigit> ul_digit, LEAP_DIGIT* digit, FingerType type) {
ul_digit->is_extended = digit->is_extended == 1;
ul_digit->type = type;
UltraleapBone::fill_bone_data(ul_digit->metacarpal, &digit->metacarpal);
UltraleapBone::fill_bone_data(ul_digit->proximal, &digit->proximal);
UltraleapBone::fill_bone_data(ul_digit->intermediate, &digit->intermediate);
UltraleapBone::fill_bone_data(ul_digit->distal, &digit->distal);
UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal);
UltraleapBone::fill_bone_data(ul_digit->proximal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal);
UltraleapBone::fill_bone_data(ul_digit->intermediate_ref, &digit->intermediate, UltraleapBone::BoneType::Intermediate);
UltraleapBone::fill_bone_data(ul_digit->distal_ref, &digit->distal, UltraleapBone::BoneType::Distal);
}

View File

@ -12,6 +12,7 @@
#include <LeapC.h>
#include "bone.h"
#include "types.h"
using namespace godot;
@ -19,6 +20,8 @@ class UltraleapDigit : public Resource {
GDCLASS(UltraleapDigit, Resource);
public:
UltraleapDigit();
enum FingerType {
Thumb,
Index,
@ -30,10 +33,6 @@ public:
FingerType type = None;
UltraleapBone* metacarpal = memnew(UltraleapBone);
UltraleapBone* proximal = memnew(UltraleapBone);
UltraleapBone* intermediate = memnew(UltraleapBone);
UltraleapBone* distal = memnew(UltraleapBone);
bool is_extended;
// Getters / Setters
@ -55,15 +54,15 @@ public:
bool get_is_extended() { return is_extended; }
void set_is_extended(bool value) { is_extended = value; }
static void fill_digit_data(UltraleapDigit* ul_digit, LEAP_DIGIT* digit);
static void fill_digit_data(Ref<UltraleapDigit> ul_digit, LEAP_DIGIT* digit, FingerType type);
protected:
static void _bind_methods();
private:
Ref<UltraleapBone> metacarpal_ref = Ref<UltraleapBone>(metacarpal);
Ref<UltraleapBone> proximal_ref = Ref<UltraleapBone>(proximal);
Ref<UltraleapBone> intermediate_ref = Ref<UltraleapBone>(intermediate);
Ref<UltraleapBone> distal_ref = Ref<UltraleapBone>(distal);
Ref<UltraleapBone> metacarpal_ref;
Ref<UltraleapBone> proximal_ref;
Ref<UltraleapBone> intermediate_ref;
Ref<UltraleapBone> distal_ref;
};
VARIANT_ENUM_CAST(UltraleapDigit::FingerType);

View File

@ -232,12 +232,13 @@ void UltraleapHand::fill_hand_data(Ref<UltraleapHand> ul_hand, LEAP_HAND* hand)
ul_hand->grab_angle = hand->grab_angle;
ul_hand->grab_strength = hand->grab_strength;
UltraleapPalm::fill_palm_data(ul_hand->palm, &hand->palm);
UltraleapPalm::fill_palm_data(ul_hand->palm_ref, &hand->palm);
for (size_t i = 0; i < 5; i++)
{
UltraleapDigit::fill_digit_data(ul_hand->digits[i], &hand->digits[i]);
}
UltraleapDigit::fill_digit_data(ul_hand->get_thumb(), &hand->thumb, UltraleapDigit::FingerType::Thumb);
UltraleapDigit::fill_digit_data(ul_hand->get_index(), &hand->index, UltraleapDigit::FingerType::Index);
UltraleapDigit::fill_digit_data(ul_hand->get_middle(), &hand->middle, UltraleapDigit::FingerType::Middle);
UltraleapDigit::fill_digit_data(ul_hand->get_ring(), &hand->ring, UltraleapDigit::FingerType::Ring);
UltraleapDigit::fill_digit_data(ul_hand->get_pinky(), &hand->pinky, UltraleapDigit::FingerType::Pinky);
UltraleapBone::fill_bone_data(ul_hand->arm, &hand->arm);
UltraleapBone::fill_bone_data(ul_hand->arm_ref, &hand->arm, UltraleapBone::BoneType::Arm);
}

View File

@ -37,8 +37,6 @@ public:
memnew(UltraleapDigit),
memnew(UltraleapDigit)
};
UltraleapBone* arm = memnew(UltraleapBone);
UltraleapPalm* palm = memnew(UltraleapPalm);
// Getters / Setters
float get_confidence() { return confidence; }
@ -100,8 +98,8 @@ protected:
static void _bind_methods();
private:
Ref<UltraleapBone> arm_ref = Ref<UltraleapBone>(arm);
Ref<UltraleapPalm> palm_ref = Ref<UltraleapPalm>(palm);
Ref<UltraleapBone> arm_ref = Ref<UltraleapBone>(memnew(UltraleapBone));
Ref<UltraleapPalm> palm_ref = Ref<UltraleapPalm>(memnew(UltraleapPalm));
Array digits_array = Array();
Ref<UltraleapDigit> digits_ref[5] = {
Ref<UltraleapDigit>(digits[0]),

View File

@ -106,7 +106,7 @@ void UltraleapPalm::_bind_methods() {
);
}
void UltraleapPalm::fill_palm_data(UltraleapPalm* ul_palm, LEAP_PALM* palm) {
void UltraleapPalm::fill_palm_data(Ref<UltraleapPalm> ul_palm, LEAP_PALM* palm) {
ul_palm->position = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&palm->position);
ul_palm->stabilized_position = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&palm->stabilized_position);
ul_palm->velocity = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&palm->velocity);

View File

@ -48,7 +48,7 @@ public:
Quaternion get_orientation() { return orientation; }
void set_orientation(Quaternion value) { orientation = value; }
static void fill_palm_data(UltraleapPalm* ul_palm, LEAP_PALM* palm);
static void fill_palm_data(Ref<UltraleapPalm> ul_palm, LEAP_PALM* palm);
protected:
static void _bind_methods();
};

View File

@ -34,24 +34,11 @@ UltraleapHandTracking::UltraleapHandTracking() {
binder = Engine::get_singleton()->get_singleton("UltraleapBinder");
}
#endif
// For now we will also start automatically if we're in the editor, so that we can have data for tool scripts
if (autostart || Engine::get_singleton()->is_editor_hint()) {
start();
}
}
UltraleapHandTracking::~UltraleapHandTracking() {
UtilityFunctions::print("Destroying UltraleapHandTracking");
if (is_running || messageLoop.joinable()) {
UtilityFunctions::print("Stopping the tracking thread");
keep_running = false;
messageLoop.join();
}
dispose_ultraleap();
#if ANDROID_ENABLED
binder = NULL;
#endif
@ -367,22 +354,12 @@ void UltraleapHandTracking::stop() {
return;
}
UtilityFunctions::print("Trying to stop tracking...");
if (keep_running) {
UtilityFunctions::print("Turning off flag of thread");
if (is_running || messageLoop.joinable()) {
UtilityFunctions::print("Stopping the tracking thread");
keep_running = false;
messageLoop.join();
}
// If the polling thread is still alive, we defer calling the stop function
// again on the next frame, and this until it's dead, then we will clean
// the connection
if (is_running) {
call_deferred("stop");
return;
}
messageLoop.join();
dispose_ultraleap();
started = false;
@ -785,6 +762,10 @@ void UltraleapHandTracking::_notification(int p_what) {
//UtilityFunctions::print(p_what);
if (p_what == Node::NOTIFICATION_ENTER_TREE) {
set_process_internal(true);
// For now we will also start automatically if we're in the editor, so that we can have data for tool scripts
if (autostart || Engine::get_singleton()->is_editor_hint()) {
start();
}
}
else if (p_what == Node::NOTIFICATION_INTERNAL_PROCESS) {
while (!events.empty()) {
@ -793,6 +774,9 @@ void UltraleapHandTracking::_notification(int p_what) {
events.pop();
}
}
else if (p_what == Node::NOTIFICATION_EXIT_TREE) {
stop();
}
}
String UltraleapHandTracking::generate_connection_payload() {