| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/3d/ik_modifier_3d.h" |
| 34 |
|
| 35 |
class ChainIK3D : public IKModifier3D { |
| 36 |
GDCLASS(ChainIK3D, IKModifier3D); |
| 37 |
|
| 38 |
public: |
| 39 |
struct ChainIK3DSetting : public IKModifier3DSetting { |
| 40 |
#ifdef TOOLS_ENABLED |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
Transform3D root_global_rest; |
| 47 |
#endif |
| 48 |
|
| 49 |
BoneJoint root_bone; |
| 50 |
BoneJoint end_bone; |
| 51 |
|
| 52 |
|
| 53 |
bool extend_end_bone = false; |
| 54 |
BoneDirection end_bone_direction = BONE_DIRECTION_FROM_PARENT; |
| 55 |
float end_bone_length = 0.0; |
| 56 |
|
| 57 |
LocalVector<BoneJoint> joints; |
| 58 |
LocalVector<IKModifier3DSolverInfo *> solver_info_list; |
| 59 |
LocalVector<Vector3> chain; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
void update_chain_coordinate(Skeleton3D *p_skeleton, int p_index, const Vector3 &p_position) { |
| 64 |
|
| 65 |
|
| 66 |
if (Math::is_zero_approx(chain[p_index].distance_to(p_position))) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
chain[p_index] = p_position; |
| 72 |
cache_current_vector(p_skeleton, p_index); |
| 73 |
} |
| 74 |
|
| 75 |
void update_chain_coordinate_bw(Skeleton3D *p_skeleton, int p_index, const Vector3 &p_position) { |
| 76 |
|
| 77 |
|
| 78 |
if (Math::is_zero_approx(chain[p_index].distance_to(p_position))) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
Vector3 result = p_position; |
| 84 |
int HEAD = p_index - 1; |
| 85 |
int TAIL = p_index; |
| 86 |
if (HEAD >= 0 && HEAD < (int)solver_info_list.size()) { |
| 87 |
IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD]; |
| 88 |
if (solver_info) { |
| 89 |
Vector3 old_head_to_tail = solver_info->current_vector; |
| 90 |
Vector3 new_head_to_tail = (result - chain[HEAD]).normalized(); |
| 91 |
if (Math::is_equal_approx((double)old_head_to_tail.dot(new_head_to_tail), -1.0)) { |
| 92 |
chain[TAIL] = chain[HEAD] + old_head_to_tail * solver_info->length; |
| 93 |
return; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
chain[p_index] = result; |
| 99 |
cache_current_vector(p_skeleton, p_index); |
| 100 |
} |
| 101 |
|
| 102 |
void update_chain_coordinate_fw(Skeleton3D *p_skeleton, int p_index, const Vector3 &p_position) { |
| 103 |
|
| 104 |
|
| 105 |
if (Math::is_zero_approx(chain[p_index].distance_to(p_position))) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
Vector3 result = p_position; |
| 111 |
int HEAD = p_index; |
| 112 |
int TAIL = p_index + 1; |
| 113 |
if (TAIL >= 0 && TAIL < (int)solver_info_list.size()) { |
| 114 |
IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD]; |
| 115 |
if (solver_info) { |
| 116 |
Vector3 old_head_to_tail = solver_info->current_vector; |
| 117 |
Vector3 new_head_to_tail = (chain[TAIL] - result).normalized(); |
| 118 |
if (Math::is_equal_approx((double)old_head_to_tail.dot(new_head_to_tail), -1.0)) { |
| 119 |
chain[HEAD] = chain[TAIL] - old_head_to_tail * solver_info->length; |
| 120 |
return; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
chain[p_index] = result; |
| 126 |
cache_current_vector(p_skeleton, p_index); |
| 127 |
} |
| 128 |
|
| 129 |
void cache_current_vector(Skeleton3D *p_skeleton, int p_index) { |
| 130 |
int cur_head = p_index - 1; |
| 131 |
int cur_tail = p_index; |
| 132 |
if (cur_head >= 0) { |
| 133 |
IKModifier3DSolverInfo *solver_info = solver_info_list[cur_head]; |
| 134 |
if (solver_info) { |
| 135 |
solver_info->current_vector = (chain[cur_tail] - chain[cur_head]).normalized(); |
| 136 |
} |
| 137 |
} |
| 138 |
cur_head = p_index; |
| 139 |
cur_tail = p_index + 1; |
| 140 |
if (cur_tail < (int)chain.size()) { |
| 141 |
IKModifier3DSolverInfo *solver_info = solver_info_list[cur_head]; |
| 142 |
if (solver_info) { |
| 143 |
solver_info->current_vector = (chain[cur_tail] - chain[cur_head]).normalized(); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void cache_current_vectors(Skeleton3D *p_skeleton) { |
| 149 |
for (uint32_t i = 0; i < joints.size(); i++) { |
| 150 |
int HEAD = i; |
| 151 |
int TAIL = i + 1; |
| 152 |
IKModifier3DSolverInfo *solver_info = solver_info_list[HEAD]; |
| 153 |
if (!solver_info) { |
| 154 |
continue; |
| 155 |
} |
| 156 |
solver_info->current_vector = (chain[TAIL] - chain[HEAD]).normalized(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void init_current_joint_rotations(Skeleton3D *p_skeleton) { |
| 161 |
if (root_bone.bone < 0) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
Quaternion parent_gpose; |
| 166 |
int parent = p_skeleton->get_bone_parent(root_bone.bone); |
| 167 |
if (parent >= 0) { |
| 168 |
parent_gpose = p_skeleton->get_bone_global_pose(parent).basis.get_rotation_quaternion(); |
| 169 |
} |
| 170 |
|
| 171 |
for (uint32_t i = 0; i < joints.size(); i++) { |
| 172 |
IKModifier3DSolverInfo *solver_info = solver_info_list[i]; |
| 173 |
if (!solver_info) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
solver_info->current_lrest = p_skeleton->get_bone_pose(joints[i].bone).basis.get_rotation_quaternion(); |
| 177 |
solver_info->current_grest = parent_gpose * solver_info->current_lrest; |
| 178 |
solver_info->current_grest.normalize(); |
| 179 |
solver_info->current_lpose = p_skeleton->get_bone_pose(joints[i].bone).basis.get_rotation_quaternion(); |
| 180 |
solver_info->current_gpose = parent_gpose * solver_info->current_lpose; |
| 181 |
solver_info->current_gpose.normalize(); |
| 182 |
parent_gpose = solver_info->current_gpose; |
| 183 |
} |
| 184 |
|
| 185 |
cache_current_vectors(p_skeleton); |
| 186 |
} |
| 187 |
|
| 188 |
~ChainIK3DSetting() { |
| 189 |
for (uint32_t i = 0; i < solver_info_list.size(); i++) { |
| 190 |
if (solver_info_list[i]) { |
| 191 |
memdelete(solver_info_list[i]); |
| 192 |
solver_info_list[i] = nullptr; |
| 193 |
} |
| 194 |
} |
| 195 |
solver_info_list.clear(); |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
protected: |
| 200 |
#ifdef TOOLS_ENABLED |
| 201 |
virtual void _update_mutable_info() override; |
| 202 |
#endif |
| 203 |
|
| 204 |
LocalVector<ChainIK3DSetting *> chain_settings; |
| 205 |
|
| 206 |
bool _get(const StringName &p_path, Variant &r_ret) const; |
| 207 |
bool _set(const StringName &p_path, const Variant &p_value); |
| 208 |
void get_property_list(List<PropertyInfo> *p_list) const; |
| 209 |
void _validate_dynamic_prop(PropertyInfo &p_property) const; |
| 210 |
|
| 211 |
static void _bind_methods(); |
| 212 |
|
| 213 |
virtual void _validate_bone_names() override; |
| 214 |
void _validate_axes(Skeleton3D *p_skeleton) const; |
| 215 |
virtual void _validate_axis(Skeleton3D *p_skeleton, int p_index, int p_joint) const; |
| 216 |
|
| 217 |
virtual void _make_all_joints_dirty() override; |
| 218 |
virtual void _update_joints(int p_index) override; |
| 219 |
void _set_joint_bone(int p_index, int p_joint, int p_bone); |
| 220 |
|
| 221 |
virtual void _process_ik(Skeleton3D *p_skeleton, double p_delta) override; |
| 222 |
|
| 223 |
virtual void _set_joint_count(int p_index, int p_count); |
| 224 |
|
| 225 |
public: |
| 226 |
virtual void set_setting_count(int p_count) override { |
| 227 |
_set_setting_count<ChainIK3DSetting>(p_count); |
| 228 |
chain_settings = _cast_settings<ChainIK3DSetting>(); |
| 229 |
} |
| 230 |
virtual void clear_settings() override { |
| 231 |
_set_setting_count<ChainIK3DSetting>(0); |
| 232 |
chain_settings.clear(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
void set_root_bone_name(int p_index, const String &p_bone_name); |
| 237 |
String get_root_bone_name(int p_index) const; |
| 238 |
void set_root_bone(int p_index, int p_bone); |
| 239 |
int get_root_bone(int p_index) const; |
| 240 |
|
| 241 |
void set_end_bone_name(int p_index, const String &p_bone_name); |
| 242 |
String get_end_bone_name(int p_index) const; |
| 243 |
void set_end_bone(int p_index, int p_bone); |
| 244 |
int get_end_bone(int p_index) const; |
| 245 |
|
| 246 |
void set_extend_end_bone(int p_index, bool p_enabled); |
| 247 |
bool is_end_bone_extended(int p_index) const; |
| 248 |
void set_end_bone_direction(int p_index, BoneDirection p_bone_direction); |
| 249 |
BoneDirection get_end_bone_direction(int p_index) const; |
| 250 |
void set_end_bone_length(int p_index, float p_length); |
| 251 |
float get_end_bone_length(int p_index) const; |
| 252 |
|
| 253 |
|
| 254 |
String get_joint_bone_name(int p_index, int p_joint) const; |
| 255 |
int get_joint_bone(int p_index, int p_joint) const; |
| 256 |
|
| 257 |
void set_joint_count(int p_index, int p_count); |
| 258 |
int get_joint_count(int p_index) const; |
| 259 |
|
| 260 |
#ifdef TOOLS_ENABLED |
| 261 |
|
| 262 |
static Transform3D get_bone_global_rest_mutable(Skeleton3D *p_skeleton, int p_bone); |
| 263 |
Transform3D get_chain_root_global_rest(int p_index); |
| 264 |
virtual Vector3 get_bone_vector(int p_index, int p_joint) const; |
| 265 |
#endif |
| 266 |
|
| 267 |
~ChainIK3D(); |
| 268 |
}; |