| 1 | /**************************************************************************/ |
| 2 | /* spline_ik_3d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "spline_ik_3d.h" |
| 32 | |
| 33 | bool SplineIK3D::_set(const StringName &p_path, const Variant &p_value) { |
| 34 | String path = p_path; |
| 35 | |
| 36 | if (path.begins_with("settings/")) { |
| 37 | int which = path.get_slicec('/', 1).to_int(); |
| 38 | String what = path.get_slicec('/', 2); |
| 39 | ERR_FAIL_INDEX_V(which, (int)settings.size(), false); |
| 40 | |
| 41 | if (what == "path_3d") { |
| 42 | set_path_3d(which, p_value); |
| 43 | } else if (what == "tilt_enabled") { |
| 44 | set_tilt_enabled(which, p_value); |
| 45 | } else if (what == "tilt_fade_in") { |
| 46 | set_tilt_fade_in(which, p_value); |
| 47 | } else if (what == "tilt_fade_out") { |
| 48 | set_tilt_fade_out(which, p_value); |
| 49 | } else { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | bool SplineIK3D::_get(const StringName &p_path, Variant &r_ret) const { |
| 57 | String path = p_path; |
| 58 | |
| 59 | if (path.begins_with("settings/")) { |
| 60 | int which = path.get_slicec('/', 1).to_int(); |
| 61 | String what = path.get_slicec('/', 2); |
| 62 | ERR_FAIL_INDEX_V(which, (int)settings.size(), false); |
| 63 | |
| 64 | if (what == "path_3d") { |
| 65 | r_ret = get_path_3d(which); |
| 66 | } else if (what == "tilt_enabled") { |
| 67 | r_ret = is_tilt_enabled(which); |
| 68 | } else if (what == "tilt_fade_in") { |
| 69 | r_ret = get_tilt_fade_in(which); |
| 70 | } else if (what == "tilt_fade_out") { |
| 71 | r_ret = get_tilt_fade_out(which); |
| 72 | } else { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | void SplineIK3D::_get_property_list(List<PropertyInfo> *p_list) const { |
| 80 | LocalVector<PropertyInfo> props; |
| 81 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 82 | String path = "settings/" + itos(i) + "/"; |
| 83 | props.push_back(PropertyInfo(Variant::NODE_PATH, path + "path_3d", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Path3D")); |
| 84 | props.push_back(PropertyInfo(Variant::BOOL, path + "tilt_enabled")); |
| 85 | props.push_back(PropertyInfo(Variant::INT, path + "tilt_fade_in", PROPERTY_HINT_RANGE, "-1,100,1,or_greater")); |
| 86 | props.push_back(PropertyInfo(Variant::INT, path + "tilt_fade_out", PROPERTY_HINT_RANGE, "-1,100,1,or_greater")); |
| 87 | } |
| 88 | |
| 89 | for (PropertyInfo &p : props) { |
| 90 | _validate_dynamic_prop(p); |
| 91 | p_list->push_back(p); |
| 92 | } |
| 93 | |
| 94 | ChainIK3D::get_property_list(p_list); |
| 95 | } |
| 96 | |
| 97 | void SplineIK3D::_validate_dynamic_prop(PropertyInfo &p_property) const { |
| 98 | PackedStringArray split = p_property.name.split("/"); |
| 99 | if (split.size() > 2 && split[0] == "settings") { |
| 100 | int which = split[1].to_int(); |
| 101 | if (split[2].begins_with("tilt_") && get_path_3d(which).is_empty()) { |
| 102 | p_property.usage = PROPERTY_USAGE_NONE; |
| 103 | } else if (split[2].begins_with("tilt_fade_") && !is_tilt_enabled(which)) { |
| 104 | p_property.usage = PROPERTY_USAGE_NONE; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | PackedStringArray SplineIK3D::get_configuration_warnings() const { |
| 110 | PackedStringArray warnings = SkeletonModifier3D::get_configuration_warnings(); |
| 111 | for (uint32_t i = 0; i < sp_settings.size(); i++) { |
| 112 | if (sp_settings[i]->path_3d.is_empty()) { |
| 113 | warnings.push_back(RTR("Detecting settings with no Path3D set! SplineIK3D must have a Path3D to work.")); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | return warnings; |
| 118 | } |
| 119 | |
| 120 | // Setting. |
| 121 | |
| 122 | void SplineIK3D::set_path_3d(int p_index, const NodePath &p_path_3d) { |
| 123 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 124 | sp_settings[p_index]->path_3d = p_path_3d; |
| 125 | notify_property_list_changed(); |
| 126 | update_configuration_warnings(); |
| 127 | } |
| 128 | |
| 129 | NodePath SplineIK3D::get_path_3d(int p_index) const { |
| 130 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), NodePath()); |
| 131 | return sp_settings[p_index]->path_3d; |
| 132 | } |
| 133 | |
| 134 | void SplineIK3D::set_tilt_enabled(int p_index, bool p_enabled) { |
| 135 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 136 | sp_settings[p_index]->tilt_enabled = p_enabled; |
| 137 | notify_property_list_changed(); |
| 138 | } |
| 139 | |
| 140 | bool SplineIK3D::is_tilt_enabled(int p_index) const { |
| 141 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 142 | return sp_settings[p_index]->tilt_enabled; |
| 143 | } |
| 144 | |
| 145 | void SplineIK3D::set_tilt_fade_in(int p_index, int p_size) { |
| 146 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 147 | sp_settings[p_index]->tilt_fade_in = p_size; |
| 148 | } |
| 149 | |
| 150 | int SplineIK3D::get_tilt_fade_in(int p_index) const { |
| 151 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 152 | return sp_settings[p_index]->tilt_fade_in; |
| 153 | } |
| 154 | |
| 155 | void SplineIK3D::set_tilt_fade_out(int p_index, int p_size) { |
| 156 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 157 | sp_settings[p_index]->tilt_fade_out = p_size; |
| 158 | } |
| 159 | |
| 160 | int SplineIK3D::get_tilt_fade_out(int p_index) const { |
| 161 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 162 | return sp_settings[p_index]->tilt_fade_out; |
| 163 | } |
| 164 | |
| 165 | // Individual joints. |
| 166 | |
| 167 | void SplineIK3D::_set_joint_count(int p_index, int p_count) { |
| 168 | LocalVector<double> &twists = sp_settings[p_index]->twists; |
| 169 | twists.resize(p_count); |
| 170 | LocalVector<double> &accum = sp_settings[p_index]->chain_length_accum; |
| 171 | accum.resize(p_count); |
| 172 | } |
| 173 | |
| 174 | void SplineIK3D::_bind_methods() { |
| 175 | // Setting. |
| 176 | ClassDB::bind_method(D_METHOD("set_path_3d", "index", "path_3d"), &SplineIK3D::set_path_3d); |
| 177 | ClassDB::bind_method(D_METHOD("get_path_3d", "index"), &SplineIK3D::get_path_3d); |
| 178 | ClassDB::bind_method(D_METHOD("set_tilt_enabled", "index", "enabled"), &SplineIK3D::set_tilt_enabled); |
| 179 | ClassDB::bind_method(D_METHOD("is_tilt_enabled", "index"), &SplineIK3D::is_tilt_enabled); |
| 180 | ClassDB::bind_method(D_METHOD("set_tilt_fade_in", "index", "size"), &SplineIK3D::set_tilt_fade_in); |
| 181 | ClassDB::bind_method(D_METHOD("get_tilt_fade_in", "index"), &SplineIK3D::get_tilt_fade_in); |
| 182 | ClassDB::bind_method(D_METHOD("set_tilt_fade_out", "index", "size"), &SplineIK3D::set_tilt_fade_out); |
| 183 | ClassDB::bind_method(D_METHOD("get_tilt_fade_out", "index"), &SplineIK3D::get_tilt_fade_out); |
| 184 | |
| 185 | ADD_ARRAY_COUNT("Settings", "setting_count", "set_setting_count", "get_setting_count", "settings/"); |
| 186 | } |
| 187 | |
| 188 | void SplineIK3D::_init_joints(Skeleton3D *p_skeleton, int p_index) { |
| 189 | SplineIK3DSetting *setting = sp_settings[p_index]; |
| 190 | cached_space = p_skeleton->get_global_transform_interpolated(); |
| 191 | if (!setting->simulation_dirty) { |
| 192 | if (mutable_bone_axes) { |
| 193 | _update_bone_axis(p_skeleton, p_index); |
| 194 | } |
| 195 | return; |
| 196 | } |
| 197 | for (uint32_t i = 0; i < setting->solver_info_list.size(); i++) { |
| 198 | if (setting->solver_info_list[i]) { |
| 199 | memdelete(setting->solver_info_list[i]); |
| 200 | } |
| 201 | } |
| 202 | setting->solver_info_list.clear(); |
| 203 | setting->solver_info_list.resize_initialized(setting->joints.size()); |
| 204 | setting->chain.clear(); |
| 205 | bool extend_end_bone = setting->extend_end_bone && setting->end_bone_length > 0; |
| 206 | double accum = 0.0; |
| 207 | for (uint32_t i = 0; i < setting->joints.size(); i++) { |
| 208 | setting->chain.push_back(p_skeleton->get_bone_global_pose(setting->joints[i].bone).origin); |
| 209 | bool last = i == setting->joints.size() - 1; |
| 210 | if (last && extend_end_bone && setting->end_bone_length > 0) { |
| 211 | Vector3 axis = IKModifier3D::get_bone_axis(p_skeleton, setting->end_bone.bone, setting->end_bone_direction, mutable_bone_axes); |
| 212 | if (axis.is_zero_approx()) { |
| 213 | setting->chain_length_accum[i] = accum; |
| 214 | continue; |
| 215 | } |
| 216 | setting->solver_info_list[i] = memnew(IKModifier3DSolverInfo); |
| 217 | setting->solver_info_list[i]->forward_vector = axis.normalized(); |
| 218 | setting->solver_info_list[i]->length = setting->end_bone_length; |
| 219 | setting->chain.push_back(p_skeleton->get_bone_global_pose(setting->joints[i].bone).xform(axis * setting->end_bone_length)); |
| 220 | } else if (!last) { |
| 221 | Vector3 axis = p_skeleton->get_bone_rest(setting->joints[i + 1].bone).origin; |
| 222 | if (axis.is_zero_approx()) { |
| 223 | setting->chain_length_accum[i] = accum; |
| 224 | continue; // Means always we need to check solver info, but `!solver_info` means that the bone is zero length, so IK should skip it in the all process. |
| 225 | } |
| 226 | setting->solver_info_list[i] = memnew(IKModifier3DSolverInfo); |
| 227 | setting->solver_info_list[i]->forward_vector = axis.normalized(); |
| 228 | setting->solver_info_list[i]->length = axis.length(); |
| 229 | } |
| 230 | if (setting->solver_info_list[i]) { |
| 231 | accum += setting->solver_info_list[i]->length; |
| 232 | } |
| 233 | setting->chain_length_accum[i] = accum; |
| 234 | } |
| 235 | |
| 236 | if (mutable_bone_axes) { |
| 237 | _update_bone_axis(p_skeleton, p_index); |
| 238 | #ifdef TOOLS_ENABLED |
| 239 | } else { |
| 240 | _make_gizmo_dirty(); |
| 241 | #endif // TOOLS_ENABLED |
| 242 | } |
| 243 | |
| 244 | setting->init_current_joint_rotations(p_skeleton); |
| 245 | |
| 246 | setting->simulation_dirty = false; |
| 247 | } |
| 248 | |
| 249 | void SplineIK3D::_make_simulation_dirty(int p_index) { |
| 250 | SplineIK3DSetting *setting = sp_settings[p_index]; |
| 251 | if (!setting) { |
| 252 | return; |
| 253 | } |
| 254 | setting->simulation_dirty = true; |
| 255 | } |
| 256 | |
| 257 | void SplineIK3D::_update_bone_axis(Skeleton3D *p_skeleton, int p_index) { |
| 258 | #ifdef TOOLS_ENABLED |
| 259 | bool changed = false; |
| 260 | #endif // TOOLS_ENABLED |
| 261 | SplineIK3DSetting *setting = sp_settings[p_index]; |
| 262 | const LocalVector<BoneJoint> &joints = setting->joints; |
| 263 | const LocalVector<IKModifier3DSolverInfo *> &solver_info_list = setting->solver_info_list; |
| 264 | int len = (int)solver_info_list.size() - 1; |
| 265 | for (int j = 0; j < len; j++) { |
| 266 | if (!solver_info_list[j]) { |
| 267 | continue; |
| 268 | } |
| 269 | Vector3 axis = p_skeleton->get_bone_pose(joints[j + 1].bone).origin; |
| 270 | if (axis.is_zero_approx()) { |
| 271 | continue; |
| 272 | } |
| 273 | // Less computing. |
| 274 | #ifdef TOOLS_ENABLED |
| 275 | if (!changed) { |
| 276 | Vector3 old_v = solver_info_list[j]->forward_vector; |
| 277 | solver_info_list[j]->forward_vector = axis.normalized(); |
| 278 | changed = changed || !old_v.is_equal_approx(solver_info_list[j]->forward_vector); |
| 279 | float old_l = solver_info_list[j]->length; |
| 280 | solver_info_list[j]->length = axis.length(); |
| 281 | changed = changed || !Math::is_equal_approx(old_l, solver_info_list[j]->length); |
| 282 | } else { |
| 283 | solver_info_list[j]->forward_vector = axis.normalized(); |
| 284 | solver_info_list[j]->length = axis.length(); |
| 285 | } |
| 286 | #else |
| 287 | solver_info_list[j]->forward_vector = axis.normalized(); |
| 288 | solver_info_list[j]->length = axis.length(); |
| 289 | #endif // TOOLS_ENABLED |
| 290 | } |
| 291 | if (setting->extend_end_bone && len >= 0) { |
| 292 | if (solver_info_list[len]) { |
| 293 | Vector3 axis = IKModifier3D::get_bone_axis(p_skeleton, setting->end_bone.bone, setting->end_bone_direction, mutable_bone_axes); |
| 294 | if (!axis.is_zero_approx()) { |
| 295 | solver_info_list[len]->forward_vector = axis.normalized(); |
| 296 | solver_info_list[len]->length = setting->end_bone_length; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | #ifdef TOOLS_ENABLED |
| 301 | if (changed) { |
| 302 | _make_gizmo_dirty(); |
| 303 | } |
| 304 | #endif // TOOLS_ENABLED |
| 305 | } |
| 306 | |
| 307 | void SplineIK3D::_process_ik(Skeleton3D *p_skeleton, double p_delta) { |
| 308 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 309 | _init_joints(p_skeleton, i); |
| 310 | if (sp_settings[i]->joints.is_empty()) { |
| 311 | continue; // Abort. |
| 312 | } |
| 313 | Path3D *path_3d = Object::cast_to<Path3D>(get_node_or_null(sp_settings[i]->path_3d)); |
| 314 | if (!path_3d) { |
| 315 | continue; // Abort. |
| 316 | } |
| 317 | Ref<Curve3D> curve = path_3d->get_curve(); |
| 318 | if (curve.is_null() || curve->get_point_count() == 0) { |
| 319 | continue; // Abort. |
| 320 | } |
| 321 | sp_settings[i]->cache_current_joint_rotations(p_skeleton); // Iterate over first to detect parent (outside of the chain) bone pose changes. |
| 322 | _process_joints(p_delta, p_skeleton, sp_settings[i], curve, cached_space.affine_inverse() * path_3d->get_global_transform_interpolated()); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void SplineIK3D::_process_joints(double p_delta, Skeleton3D *p_skeleton, SplineIK3DSetting *p_setting, Ref<Curve3D> p_curve, const Transform3D &p_curve_space) { |
| 327 | if (p_setting->solver_info_list.is_empty()) { |
| 328 | return; |
| 329 | } |
| 330 | uint32_t joint_count = p_setting->joints.size(); |
| 331 | uint32_t joint_last = joint_count - 1; |
| 332 | |
| 333 | double path_length = p_curve->get_baked_length(); |
| 334 | PackedVector3Array points = p_curve->get_baked_points(); |
| 335 | Vector<real_t> tilts = p_curve->get_baked_tilts(); |
| 336 | Vector<real_t> dists = p_curve->get_baked_dist_cache(); |
| 337 | uint32_t point_count = points.size(); |
| 338 | uint32_t point_last = point_count - 1; |
| 339 | |
| 340 | // Make straight segment from root joint to start point. |
| 341 | Vector3 start_point = p_curve_space.xform(points[0]); |
| 342 | Vector3 start_vector = start_point - p_skeleton->get_bone_global_pose(p_setting->joints[0].bone).origin; |
| 343 | double start_dist = start_vector.length(); |
| 344 | |
| 345 | // Find first joint on the path. |
| 346 | uint32_t chain_path_start = 0; |
| 347 | while (chain_path_start < joint_count) { |
| 348 | if (p_setting->chain_length_accum[chain_path_start] >= start_dist) { |
| 349 | break; |
| 350 | } |
| 351 | chain_path_start++; |
| 352 | } |
| 353 | chain_path_start = (uint32_t)CLAMP((int)chain_path_start, 0, (int)joint_last); |
| 354 | |
| 355 | // For tilt fade-in, get bones length not on the path as denominator. |
| 356 | double fade_in_denom = 0.0; |
| 357 | int denom_start = p_setting->tilt_fade_in > 0 ? CLAMP(p_setting->tilt_fade_in - 1, (int)chain_path_start, (int)joint_count) : -1; |
| 358 | int denom_start_to = denom_start - p_setting->tilt_fade_in; |
| 359 | if (denom_start >= 0) { |
| 360 | for (int i = denom_start; i > denom_start_to; i--) { |
| 361 | if (i < 0) { |
| 362 | break; |
| 363 | } |
| 364 | IKModifier3DSolverInfo *solver_info = p_setting->solver_info_list[i]; |
| 365 | if (!solver_info || Math::is_zero_approx(solver_info->length)) { |
| 366 | continue; |
| 367 | } |
| 368 | fade_in_denom += solver_info->length; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Prepare for fade-out. |
| 373 | uint32_t ended = 0; |
| 374 | Vector3 end_point = p_curve_space.xform(points[point_last]); |
| 375 | Vector3 end_vector; |
| 376 | double end_to_end_length = 0.0; |
| 377 | double fade_out_denom = 0.0; |
| 378 | |
| 379 | uint32_t last_nearest = 0; |
| 380 | uint32_t last_nearest_next = 0; |
| 381 | double last_interpolate = 0.0; |
| 382 | |
| 383 | for (uint32_t i = 0; i < p_setting->solver_info_list.size(); i++) { |
| 384 | IKModifier3DSolverInfo *solver_info = p_setting->solver_info_list[i]; |
| 385 | if (!solver_info || Math::is_zero_approx(solver_info->length)) { |
| 386 | continue; |
| 387 | } |
| 388 | uint32_t HEAD = i; |
| 389 | uint32_t TAIL = i + 1; |
| 390 | |
| 391 | bool is_fitting_first = HEAD == chain_path_start; |
| 392 | |
| 393 | // Special case for out of path joints. |
| 394 | if (ended > 0) { |
| 395 | p_setting->update_chain_coordinate(p_skeleton, TAIL, limit_length(p_setting->chain[HEAD], p_setting->chain[HEAD] + end_vector, solver_info->length)); |
| 396 | if (p_setting->tilt_enabled) { |
| 397 | if (p_setting->tilt_fade_out < 0) { |
| 398 | p_setting->twists[HEAD] = 0.0; |
| 399 | } else if (p_setting->tilt_fade_out == 0) { |
| 400 | p_setting->twists[HEAD] = tilts[point_last]; |
| 401 | } else { |
| 402 | // Increases monotonically in a bended line, accumulate the distances. |
| 403 | if (ended == 1) { |
| 404 | end_to_end_length = p_setting->chain[TAIL].distance_to(end_point); |
| 405 | } else { |
| 406 | end_to_end_length += solver_info->length; |
| 407 | } |
| 408 | double fade_out_dumping = CLAMP(end_to_end_length / fade_out_denom, 0.0, 1.0); |
| 409 | p_setting->twists[HEAD] = Math::lerp(ended == 1 ? Math::lerp((double)tilts[last_nearest], (double)tilts[last_nearest_next], last_interpolate) : (double)tilts[point_last], 0.0, fade_out_dumping); |
| 410 | ended = 2; |
| 411 | } |
| 412 | } |
| 413 | continue; |
| 414 | } else if (point_count == 1 || HEAD <= chain_path_start) { |
| 415 | // Set twist only for first fitting joint. |
| 416 | bool update_coordinate = !is_fitting_first || point_count == 1; |
| 417 | if (update_coordinate) { |
| 418 | p_setting->update_chain_coordinate(p_skeleton, TAIL, limit_length(p_setting->chain[HEAD], p_setting->chain[HEAD] + start_vector, solver_info->length)); |
| 419 | } |
| 420 | if (p_setting->tilt_enabled) { |
| 421 | if (p_setting->tilt_fade_in < 0) { |
| 422 | p_setting->twists[HEAD] = 0.0; |
| 423 | } else if (p_setting->tilt_fade_in == 0) { |
| 424 | p_setting->twists[HEAD] = tilts[0]; |
| 425 | } else { |
| 426 | // Decreases monotonically in a straight line, fetch the distance. |
| 427 | double fade_in_dumping = CLAMP((double)(p_setting->chain[HEAD].distance_to(start_point) / fade_in_denom), 0.0, 1.0); |
| 428 | p_setting->twists[HEAD] = Math::lerp((double)tilts[0], 0.0, fade_in_dumping); |
| 429 | } |
| 430 | } |
| 431 | if (update_coordinate) { |
| 432 | continue; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // General case. |
| 437 | double lsq = solver_info->length * solver_info->length; |
| 438 | Vector3 head_in_chain_space = p_curve_space.xform_inv(p_setting->chain[HEAD]); |
| 439 | double interpolate = 0.0; |
| 440 | uint32_t nearest = p_setting->find_nearest_point(head_in_chain_space, lsq, points, p_curve->is_closed(), last_nearest, &interpolate); |
| 441 | if (nearest >= point_count) { |
| 442 | if (HEAD == 0) { |
| 443 | nearest = point_count - 2; |
| 444 | interpolate = 1.0; |
| 445 | } else { |
| 446 | Vector3 chain_end = (p_setting->chain[HEAD] - p_setting->chain[HEAD - 1]).normalized(); |
| 447 | Vector3 path_end = (p_curve_space.xform(points[point_last]) - p_setting->chain[HEAD]).normalized(); |
| 448 | double rest_path_length = path_length - Math::lerp((double)dists[last_nearest], (double)dists[last_nearest_next], last_interpolate); |
| 449 | interpolate = CLAMP(rest_path_length / solver_info->length, 0.0, 1.0); // End vector should be defined only one end bone to make neat interpolating. |
| 450 | end_vector = chain_end.lerp(path_end, interpolate); |
| 451 | |
| 452 | int denom_end = p_setting->tilt_fade_out > 0 ? CLAMP((int)joint_last - p_setting->tilt_fade_out, 0, (int)last_nearest) : -1; |
| 453 | int denom_end_to = denom_end + p_setting->tilt_fade_out; |
| 454 | if (denom_end >= 0) { |
| 455 | for (int e = denom_end; e < denom_end_to; e++) { |
| 456 | if (e >= (int)joint_count) { |
| 457 | break; |
| 458 | } |
| 459 | IKModifier3DSolverInfo *end_solver_info = p_setting->solver_info_list[e]; |
| 460 | if (!end_solver_info || Math::is_zero_approx(end_solver_info->length)) { |
| 461 | continue; |
| 462 | } |
| 463 | fade_out_denom += end_solver_info->length; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | ended = 1; |
| 468 | i--; // Will be processed above special case. |
| 469 | continue; |
| 470 | } |
| 471 | } |
| 472 | uint32_t nearest_next = p_curve->is_closed() ? Math::posmod(nearest + 1, point_count) : CLAMP(nearest, (uint32_t)0, point_last); |
| 473 | p_setting->update_chain_coordinate(p_skeleton, TAIL, limit_length(p_setting->chain[HEAD], p_curve_space.xform(points[nearest].lerp(points[nearest_next], interpolate)), solver_info->length)); |
| 474 | if (!is_fitting_first) { |
| 475 | p_setting->twists[HEAD] = Math::lerp((double)tilts[last_nearest], (double)tilts[last_nearest_next], last_interpolate); |
| 476 | } |
| 477 | last_nearest = nearest; |
| 478 | last_nearest_next = nearest_next; |
| 479 | last_interpolate = interpolate; |
| 480 | } |
| 481 | |
| 482 | // Update virtual bone rest/poses. |
| 483 | p_setting->cache_current_joint_rotations(p_skeleton, p_setting->tilt_enabled); // Pass p_setting->tilt_enabled to skip unneeded rotate process. |
| 484 | |
| 485 | // Apply the virtual bone rest/poses to the actual bones. |
| 486 | for (uint32_t i = 0; i < p_setting->solver_info_list.size(); i++) { |
| 487 | IKModifier3DSolverInfo *solver_info = p_setting->solver_info_list[i]; |
| 488 | if (!solver_info || Math::is_zero_approx(solver_info->length)) { |
| 489 | continue; |
| 490 | } |
| 491 | p_skeleton->set_bone_pose_rotation(p_setting->joints[i].bone, solver_info->current_lpose); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | #ifdef TOOLS_ENABLED |
| 496 | Vector3 SplineIK3D::get_bone_vector(int p_index, int p_joint) const { |
| 497 | Skeleton3D *skeleton = get_skeleton(); |
| 498 | if (!skeleton) { |
| 499 | return Vector3(); |
| 500 | } |
| 501 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3()); |
| 502 | SplineIK3DSetting *setting = sp_settings[p_index]; |
| 503 | if (!setting) { |
| 504 | return Vector3(); |
| 505 | } |
| 506 | const LocalVector<BoneJoint> &joints = setting->joints; |
| 507 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), Vector3()); |
| 508 | const LocalVector<IKModifier3DSolverInfo *> &solver_info_list = setting->solver_info_list; |
| 509 | if (p_joint >= (int)solver_info_list.size() || !solver_info_list[p_joint]) { |
| 510 | if (p_joint == (int)joints.size() - 1) { |
| 511 | return IKModifier3D::get_bone_axis(skeleton, setting->end_bone.bone, setting->end_bone_direction, mutable_bone_axes) * setting->end_bone_length; |
| 512 | } |
| 513 | return mutable_bone_axes ? skeleton->get_bone_pose(joints[p_joint + 1].bone).origin : skeleton->get_bone_rest(joints[p_joint + 1].bone).origin; |
| 514 | } |
| 515 | return solver_info_list[p_joint]->forward_vector * solver_info_list[p_joint]->length; |
| 516 | } |
| 517 | #endif // TOOLS_ENABLED |
| 518 | |
| 519 | SplineIK3D::~SplineIK3D() { |
| 520 | clear_settings(); |
| 521 | } |