Add data to bones to help rigging

This commit is contained in:
rodolpheh 2023-11-20 22:35:47 +00:00
parent e98177faa4
commit dfa6243636
5 changed files with 108 additions and 16 deletions

View File

@ -19,12 +19,18 @@ void UltraleapBone::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_next_joint"), &UltraleapBone::get_next_joint);
ClassDB::bind_method(D_METHOD("set_next_joint", "next_joint"), &UltraleapBone::set_next_joint);
ClassDB::bind_method(D_METHOD("get_translation"), &UltraleapBone::get_translation);
ClassDB::bind_method(D_METHOD("set_translation", "translation"), &UltraleapBone::set_translation);
ClassDB::bind_method(D_METHOD("get_width"), &UltraleapBone::get_width);
ClassDB::bind_method(D_METHOD("set_width", "width"), &UltraleapBone::set_width);
ClassDB::bind_method(D_METHOD("get_orientation"), &UltraleapBone::get_orientation);
ClassDB::bind_method(D_METHOD("set_orientation", "orientation"), &UltraleapBone::set_orientation);
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(
@ -57,6 +63,16 @@ void UltraleapBone::_bind_methods() {
"get_next_joint"
);
ClassDB::add_property(
"UltraleapBone",
PropertyInfo(
Variant::VECTOR3,
"translation"
),
"set_translation",
"get_translation"
);
ClassDB::add_property(
"UltraleapBone",
PropertyInfo(
@ -76,12 +92,63 @@ void UltraleapBone::_bind_methods() {
"set_orientation",
"get_orientation"
);
ClassDB::add_property(
"UltraleapBone",
PropertyInfo(
Variant::QUATERNION,
"rotation"
),
"set_rotation",
"get_rotation"
);
BIND_ENUM_CONSTANT(Metacarpal);
BIND_ENUM_CONSTANT(Proximal);
BIND_ENUM_CONSTANT(Intermediate);
BIND_ENUM_CONSTANT(Distal);
BIND_ENUM_CONSTANT(Arm);
BIND_ENUM_CONSTANT(None);
}
void UltraleapBone::fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone, BoneType type) {
void UltraleapBone::fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone, BoneType type, LEAP_BONE* previous_bone, LEAP_PALM* palm) {
ul_bone->type = 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->orientation = UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&bone->rotation);
ul_bone->type = type;
// If previous_bone is defined, we compute the transform from the previous bone to the current bone
if (previous_bone != NULL) {
Transform3D previous_bone_transform = Transform3D(
Basis(UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&previous_bone->rotation)),
UltraleapTypes::ultraleap_vector3_to_godot_vector3(&previous_bone->prev_joint)
);
Transform3D current_bone_transform = Transform3D(
Basis(ul_bone->orientation),
ul_bone->prev_joint
);
Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform;
ul_bone->rotation = relative_bone_transform.basis.get_quaternion();
ul_bone->translation = relative_bone_transform.origin;
}
// If palm is defined, this bone is either a metacarpal or an arm. We still compute relative transform though.
if (palm != NULL) {
Transform3D previous_bone_transform = Transform3D(
Basis(UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&palm->orientation)),
UltraleapTypes::ultraleap_vector3_to_godot_vector3(&palm->position)
);
Transform3D current_bone_transform = Transform3D(
Basis(ul_bone->orientation),
type == UltraleapBone::BoneType::Arm ? ul_bone->next_joint : ul_bone->prev_joint
);
Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform;
ul_bone->rotation = relative_bone_transform.basis.get_quaternion();
ul_bone->translation = relative_bone_transform.origin;
}
}

View File

@ -33,8 +33,10 @@ public:
Vector3 prev_joint;
Vector3 next_joint;
Vector3 translation;
float width;
Quaternion orientation;
Quaternion rotation;
// Getters / Setters
BoneType get_type() { return type; }
@ -46,13 +48,19 @@ public:
Vector3 get_next_joint() { return next_joint; }
void set_next_joint(Vector3 value) { next_joint = value; }
Vector3 get_translation() { return translation; }
void set_translation(Vector3 value) { translation = value; }
float get_width() { return width; }
void set_width(float value) { width = value; }
Quaternion get_orientation() { return orientation; }
void set_orientation(Quaternion value) { orientation = value; }
static void fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone, BoneType type);
Quaternion get_rotation() { return rotation; }
void set_rotation(Quaternion value) { rotation = value; }
static void fill_bone_data(Ref<UltraleapBone> ul_bone, LEAP_BONE* bone, BoneType type, LEAP_BONE* previous_bone, LEAP_PALM* palm);
protected:
static void _bind_methods();
};

View File

@ -117,9 +117,16 @@ void UltraleapDigit::_bind_methods() {
"",
"get_bones"
);
BIND_ENUM_CONSTANT(Thumb);
BIND_ENUM_CONSTANT(Index);
BIND_ENUM_CONSTANT(Middle);
BIND_ENUM_CONSTANT(Ring);
BIND_ENUM_CONSTANT(Pinky);
BIND_ENUM_CONSTANT(None);
}
void UltraleapDigit::fill_digit_data(Ref<UltraleapDigit> ul_digit, LEAP_DIGIT* digit, FingerType type) {
void UltraleapDigit::fill_digit_data(Ref<UltraleapDigit> ul_digit, LEAP_DIGIT* digit, FingerType type, LEAP_PALM* palm) {
if (ul_digit->metacarpal_ref == NULL) {
ul_digit->metacarpal_ref = Ref<UltraleapBone>(memnew(UltraleapBone));
ul_digit->proximal_ref = Ref<UltraleapBone>(memnew(UltraleapBone));
@ -129,9 +136,19 @@ void UltraleapDigit::fill_digit_data(Ref<UltraleapDigit> ul_digit, LEAP_DIGIT* d
ul_digit->is_extended = digit->is_extended == 1;
ul_digit->type = type;
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);
// We need some extra logic here because the metacarpal doesn't exist in Ultraleap's hand data
if (type == UltraleapDigit::FingerType::Thumb) {
UltraleapBone::fill_bone_data(ul_digit->proximal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal, NULL, palm);
// Still filling it out but shouldn't be used
UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, NULL, palm);
}
else {
UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, NULL, palm);
UltraleapBone::fill_bone_data(ul_digit->proximal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal, &digit->metacarpal, NULL);
}
UltraleapBone::fill_bone_data(ul_digit->intermediate_ref, &digit->intermediate, UltraleapBone::BoneType::Intermediate, &digit->proximal, NULL);
UltraleapBone::fill_bone_data(ul_digit->distal_ref, &digit->distal, UltraleapBone::BoneType::Distal, &digit->intermediate, NULL);
}

View File

@ -66,7 +66,7 @@ public:
return bones;
}
static void fill_digit_data(Ref<UltraleapDigit> ul_digit, LEAP_DIGIT* digit, FingerType type);
static void fill_digit_data(Ref<UltraleapDigit> ul_digit, LEAP_DIGIT* digit, FingerType type, LEAP_PALM* palm);
protected:
static void _bind_methods();

View File

@ -247,11 +247,11 @@ void UltraleapHand::fill_hand_data(Ref<UltraleapHand> ul_hand, LEAP_HAND* hand)
UltraleapPalm::fill_palm_data(ul_hand->palm_ref, &hand->palm);
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);
UltraleapDigit::fill_digit_data(ul_hand->get_thumb(), &hand->thumb, UltraleapDigit::FingerType::Thumb, &hand->palm);
UltraleapDigit::fill_digit_data(ul_hand->get_index(), &hand->index, UltraleapDigit::FingerType::Index, &hand->palm);
UltraleapDigit::fill_digit_data(ul_hand->get_middle(), &hand->middle, UltraleapDigit::FingerType::Middle, &hand->palm);
UltraleapDigit::fill_digit_data(ul_hand->get_ring(), &hand->ring, UltraleapDigit::FingerType::Ring, &hand->palm);
UltraleapDigit::fill_digit_data(ul_hand->get_pinky(), &hand->pinky, UltraleapDigit::FingerType::Pinky, &hand->palm);
UltraleapBone::fill_bone_data(ul_hand->arm_ref, &hand->arm, UltraleapBone::BoneType::Arm);
UltraleapBone::fill_bone_data(ul_hand->arm_ref, &hand->arm, UltraleapBone::BoneType::Arm, NULL, &hand->palm);
}