Skip to content

Commit

Permalink
Move to SkeletonModifier3D.
Browse files Browse the repository at this point in the history
  • Loading branch information
fire committed Apr 5, 2024
1 parent 33a59eb commit 63b5e1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 38 deletions.
41 changes: 10 additions & 31 deletions src/many_bone_ik_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,6 @@ 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 @@ -426,7 +424,6 @@ 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 @@ -670,7 +667,7 @@ NodePath ManyBoneIK3D::get_pin_nodepath(int32_t p_effector_index) const {
return effector_template->get_target_node();
}

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

void ManyBoneIK3D::_skeleton_changed(Skeleton3D *p_skeleton) {
if (!p_skeleton) {
void ManyBoneIK3D::_reload() {
if (!get_skeleton()) {
return;
}
Vector<int32_t> roots = p_skeleton->get_parentless_bones();
Vector<int32_t> roots = get_skeleton()->get_parentless_bones();
if (roots.is_empty()) {
return;
}
bone_list.clear();
segmented_skeletons.clear();
for (BoneId root_bone_index : roots) {
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)));
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)));
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 @@ -748,11 +745,11 @@ void ManyBoneIK3D::_skeleton_changed(Skeleton3D *p_skeleton) {
}
_update_ik_bones_transform();
for (Ref<IKBone3D> &ik_bone_3d : bone_list) {
ik_bone_3d->update_default_bone_direction_transform(p_skeleton);
ik_bone_3d->update_default_bone_direction_transform(get_skeleton());
}
for (int constraint_i = 0; constraint_i < constraint_count; ++constraint_i) {
String bone = constraint_names[constraint_i];
BoneId bone_id = p_skeleton->find_bone(bone);
BoneId bone_id = get_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 @@ -835,29 +832,12 @@ 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 @@ -868,7 +848,6 @@ void ManyBoneIK3D::_notification(int p_what) {
update_gizmos();
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
_execute(get_process_delta_time());
update_gizmos();
} break;
}
Expand Down
11 changes: 4 additions & 7 deletions src/many_bone_ik_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
#include "scene/main/scene_tree.h"

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

bool is_constraint_mode = false;
NodePath skeleton_path;
Expand Down Expand Up @@ -87,9 +87,9 @@ class ManyBoneIK3D : public Node3D {
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 _skeleton_changed(Skeleton3D *skeleton);
virtual void _execute(real_t delta);
virtual void _process_modification(double delta) override;
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 @@ -105,11 +105,8 @@ class ManyBoneIK3D : public Node3D {
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 63b5e1e

Please sign in to comment.