Skip to content

Commit

Permalink
Revert "Save work."
Browse files Browse the repository at this point in the history
This reverts commit b156250.
  • Loading branch information
fire committed Feb 9, 2024
1 parent 734f02e commit 1d88951
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 14 deletions.
119 changes: 109 additions & 10 deletions src/many_bone_ik_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,83 @@ void ManyBoneIK3D::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(
PropertyInfo(Variant::TRANSFORM3D, "constraints/" + itos(constraint_i) + "/bone_direction", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
}
RBSet<StringName> existing_pins;
for (int32_t pin_i = 0; pin_i < get_pin_count(); pin_i++) {
const String name = get_pin_bone_name(pin_i);
existing_pins.insert(name);
}
const uint32_t pin_usage = PROPERTY_USAGE_DEFAULT;
p_list->push_back(
PropertyInfo(Variant::INT, "pin_count",
PROPERTY_HINT_RANGE, "0,65536,or_greater", pin_usage | PROPERTY_USAGE_ARRAY | PROPERTY_USAGE_READ_ONLY,
"Pins,pins/"));
for (int pin_i = 0; pin_i < get_pin_count(); pin_i++) {
PropertyInfo effector_name;
effector_name.type = Variant::STRING_NAME;
effector_name.name = "pins/" + itos(pin_i) + "/bone_name";
effector_name.usage = pin_usage;
if (get_skeleton()) {
String names;
for (int bone_i = 0; bone_i < get_skeleton()->get_bone_count(); bone_i++) {
String name = get_skeleton()->get_bone_name(bone_i);
StringName string_name = StringName(name);
if (existing_pins.has(string_name)) {
continue;
}
name += ",";
names += name;
existing_pins.insert(name);
}
effector_name.hint = PROPERTY_HINT_ENUM_SUGGESTION;
effector_name.hint_string = names;
} else {
effector_name.hint = PROPERTY_HINT_NONE;
effector_name.hint_string = "";
}
p_list->push_back(effector_name);
PropertyInfo pin_root_name;
pin_root_name.type = Variant::STRING_NAME;
pin_root_name.name = "pins/" + itos(pin_i) + "/root_bone";
pin_root_name.usage = pin_usage;
String pin_name = get_pin_bone_name(pin_i);
int pin_bone_idx = get_skeleton()->find_bone(pin_name);
if (get_skeleton()) {
String names;
for (int bone_i = 0; bone_i < get_skeleton()->get_bone_count(); bone_i++) {
bool is_parent = _is_ancestor_of(bone_i, pin_bone_idx);
if ((!is_parent || _is_descendant_of(bone_i, pin_bone_idx))) {
continue;
}
String name = get_skeleton()->get_bone_name(bone_i);
StringName string_name = StringName(name);
if (existing_pins.has(string_name)) {
continue;
}
name += ",";
names += name;
existing_pins.insert(name);
}
bool is_root = get_skeleton()->get_bone_parent(pin_bone_idx) == -1;
if (is_root) {
names += pin_name + ",";
existing_pins.insert(pin_name);
}
pin_root_name.hint = PROPERTY_HINT_ENUM_SUGGESTION;
pin_root_name.hint_string = names;
} else {
pin_root_name.hint = PROPERTY_HINT_NONE;
pin_root_name.hint_string = "";
}
p_list->push_back(pin_root_name);
p_list->push_back(
PropertyInfo(Variant::NODE_PATH, "pins/" + itos(pin_i) + "/target_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node3D", pin_usage));
p_list->push_back(
PropertyInfo(Variant::FLOAT, "pins/" + itos(pin_i) + "/passthrough_factor", PROPERTY_HINT_RANGE, "0,1,0.1,or_greater", pin_usage));
p_list->push_back(
PropertyInfo(Variant::FLOAT, "pins/" + itos(pin_i) + "/weight", PROPERTY_HINT_RANGE, "0,1,0.1,or_greater", pin_usage));
p_list->push_back(
PropertyInfo(Variant::VECTOR3, "pins/" + itos(pin_i) + "/direction_priorities", PROPERTY_HINT_RANGE, "0,1,0.1,or_greater", pin_usage));
}
}

bool ManyBoneIK3D::_get(const StringName &p_name, Variant &r_ret) const {
Expand Down Expand Up @@ -383,6 +460,8 @@ void ManyBoneIK3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_bone_direction_transform", "index"), &ManyBoneIK3D::get_bone_direction_transform);
ClassDB::bind_method(D_METHOD("set_bone_direction_transform", "index", "transform"), &ManyBoneIK3D::set_bone_direction_transform);
ClassDB::bind_method(D_METHOD("remove_constraint", "index"), &ManyBoneIK3D::remove_constraint);
ClassDB::bind_method(D_METHOD("set_skeleton_node_path", "path"), &ManyBoneIK3D::set_skeleton_node_path);
ClassDB::bind_method(D_METHOD("get_skeleton_node_path"), &ManyBoneIK3D::get_skeleton_node_path);
ClassDB::bind_method(D_METHOD("register_skeleton"), &ManyBoneIK3D::register_skeleton);
ClassDB::bind_method(D_METHOD("reset_constraints"), &ManyBoneIK3D::register_skeleton);
ClassDB::bind_method(D_METHOD("set_dirty"), &ManyBoneIK3D::set_dirty);
Expand Down Expand Up @@ -423,6 +502,7 @@ void ManyBoneIK3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_stabilization_passes"), &ManyBoneIK3D::get_stabilization_passes);
ClassDB::bind_method(D_METHOD("set_pin_bone_name", "index", "name"), &ManyBoneIK3D::set_pin_bone_name);

ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "skeleton_node_path"), "set_skeleton_node_path", "get_skeleton_node_path");
ADD_PROPERTY(PropertyInfo(Variant::INT, "iterations_per_frame", PROPERTY_HINT_RANGE, "1,150,1,or_greater"), "set_iterations_per_frame", "get_iterations_per_frame");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "default_damp", PROPERTY_HINT_RANGE, "0.01,180.0,0.1,radians,exp", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_default_damp", "get_default_damp");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "constraint_mode"), "set_constraint_mode", "get_constraint_mode");
Expand Down Expand Up @@ -665,7 +745,7 @@ NodePath ManyBoneIK3D::get_pin_nodepath(int32_t p_effector_index) const {
return effector_template->get_target_node();
}

void ManyBoneIK3D::_process_modification(double delta) {
void ManyBoneIK3D::_execute(real_t delta) {
if (!get_skeleton()) {
return;
}
Expand All @@ -676,7 +756,7 @@ void ManyBoneIK3D::_process_modification(double delta) {
set_dirty();
}
if (is_dirty) {
_reload();
_skeleton_changed(get_skeleton());
is_dirty = false;
}
if (bone_list.size()) {
Expand Down Expand Up @@ -717,19 +797,19 @@ void ManyBoneIK3D::_process_modification(double delta) {
_update_skeleton_bones_transform();
}

void ManyBoneIK3D::_reload() {
if (!get_skeleton()) {
void ManyBoneIK3D::_skeleton_changed(Skeleton3D *p_skeleton) {
if (!p_skeleton) {
return;
}
Vector<int32_t> roots = get_skeleton()->get_parentless_bones();
Vector<int32_t> roots = p_skeleton->get_parentless_bones();
if (roots.is_empty()) {
return;
}
bone_list.clear();
segmented_skeletons.clear();
for (BoneId root_bone_index : roots) {
String parentless_bone = get_skeleton()->get_bone_name(root_bone_index);
Ref<IKBoneSegment3D> segmented_skeleton = Ref<IKBoneSegment3D>(memnew(IKBoneSegment3D(get_skeleton(), parentless_bone, pins, this, nullptr, root_bone_index, -1, stabilize_passes)));
String parentless_bone = p_skeleton->get_bone_name(root_bone_index);
Ref<IKBoneSegment3D> segmented_skeleton = Ref<IKBoneSegment3D>(memnew(IKBoneSegment3D(p_skeleton, parentless_bone, pins, this, nullptr, root_bone_index, -1, stabilize_passes)));
ik_origin.instantiate();
segmented_skeleton->get_root()->get_ik_transform()->set_parent(ik_origin);
segmented_skeleton->generate_default_segments(pins, root_bone_index, -1, this);
Expand All @@ -743,11 +823,11 @@ void ManyBoneIK3D::_reload() {
}
_update_ik_bones_transform();
for (Ref<IKBone3D> &ik_bone_3d : bone_list) {
ik_bone_3d->update_default_bone_direction_transform(get_skeleton());
ik_bone_3d->update_default_bone_direction_transform(p_skeleton);
}
for (int constraint_i = 0; constraint_i < constraint_count; ++constraint_i) {
String bone = constraint_names[constraint_i];
BoneId bone_id = get_skeleton()->find_bone(bone);
BoneId bone_id = p_skeleton->find_bone(bone);
for (Ref<IKBone3D> &ik_bone_3d : bone_list) {
if (ik_bone_3d->get_bone_id() != bone_id) {
continue;
Expand Down Expand Up @@ -830,12 +910,29 @@ int32_t ManyBoneIK3D::find_constraint(String p_string) const {
return -1;
}

Skeleton3D *ManyBoneIK3D::get_skeleton() const {
Node *node = get_node_or_null(skeleton_node_path);
if (!node) {
return nullptr;
}
return cast_to<Skeleton3D>(node);
}

NodePath ManyBoneIK3D::get_skeleton_node_path() {
return skeleton_node_path;
}

void ManyBoneIK3D::set_skeleton_node_path(NodePath p_skeleton_node_path) {
skeleton_node_path = p_skeleton_node_path;
register_skeleton();
set_dirty(); // Duplicated for ease of verification.
}

void ManyBoneIK3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY: {
set_process_priority(1);
set_notify_transform(true);
_reload();
} break;
case NOTIFICATION_ENTER_TREE: {
set_process_internal(true);
Expand All @@ -846,6 +943,7 @@ void ManyBoneIK3D::_notification(int p_what) {
update_gizmos();
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
_execute(get_process_delta_time());
update_gizmos();
} break;
}
Expand Down Expand Up @@ -1108,3 +1206,4 @@ bool ManyBoneIK3D::_is_ancestor_of(int potential_ancestor, int bone_idx) const {
}
return false;
}

11 changes: 7 additions & 4 deletions src/many_bone_ik_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
#include "scene/main/scene_tree.h"

class ManyBoneIK3DState;
class ManyBoneIK3D : public SkeletonModifier3D {
GDCLASS(ManyBoneIK3D, SkeletonModifier3D);
class ManyBoneIK3D : public Node3D {
GDCLASS(ManyBoneIK3D, Node3D);

bool is_constraint_mode = false;
NodePath skeleton_path;
Expand Down Expand Up @@ -88,9 +88,9 @@ class ManyBoneIK3D : public SkeletonModifier3D {
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
static void _bind_methods();
virtual void _process_modification(double delta) override;
virtual void _skeleton_changed(Skeleton3D *skeleton);
virtual void _execute(real_t delta);
void _notification(int p_what);
virtual void _reload();

public:
void set_pin_bone_name(int32_t p_effector_index, StringName p_name) const;
Expand All @@ -106,8 +106,11 @@ class ManyBoneIK3D : public SkeletonModifier3D {
void set_constraint_mode(bool p_enabled);
bool get_constraint_mode() const;
bool get_pin_enabled(int32_t p_effector_index) const;
void set_skeleton_node_path(NodePath p_skeleton_node_path);
void register_skeleton();
void reset_constraints();
NodePath get_skeleton_node_path();
Skeleton3D *get_skeleton() const;
Vector<Ref<IKBone3D>> get_bone_list() const;
Vector<Ref<IKBoneSegment3D>> get_segmented_skeletons();
float get_iterations_per_frame() const;
Expand Down

0 comments on commit 1d88951

Please sign in to comment.