| 1 | /**************************************************************************/ |
| 2 | /* spring_bone_simulator_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 "spring_bone_simulator_3d.h" |
| 32 | #include "spring_bone_simulator_3d.compat.inc" |
| 33 | |
| 34 | #include "scene/3d/spring_bone_collision_3d.h" |
| 35 | |
| 36 | // Original VRM Spring Bone movement logic was distributed by (c) VRM Consortium. Licensed under the MIT license. |
| 37 | |
| 38 | bool SpringBoneSimulator3D::_set(const StringName &p_path, const Variant &p_value) { |
| 39 | String path = p_path; |
| 40 | |
| 41 | if (path.begins_with("settings/")) { |
| 42 | int which = path.get_slicec('/', 1).to_int(); |
| 43 | String what = path.get_slicec('/', 2); |
| 44 | ERR_FAIL_INDEX_V(which, (int)settings.size(), false); |
| 45 | |
| 46 | if (what == "root_bone_name") { |
| 47 | set_root_bone_name(which, p_value); |
| 48 | } else if (what == "root_bone") { |
| 49 | set_root_bone(which, p_value); |
| 50 | } else if (what == "end_bone_name") { |
| 51 | set_end_bone_name(which, p_value); |
| 52 | } else if (what == "end_bone") { |
| 53 | String opt = path.get_slicec('/', 3); |
| 54 | if (opt.is_empty()) { |
| 55 | set_end_bone(which, p_value); |
| 56 | } else if (opt == "direction") { |
| 57 | set_end_bone_direction(which, static_cast<BoneDirection>((int)p_value)); |
| 58 | } else if (opt == "length") { |
| 59 | set_end_bone_length(which, p_value); |
| 60 | } else { |
| 61 | return false; |
| 62 | } |
| 63 | } else if (what == "extend_end_bone") { |
| 64 | set_extend_end_bone(which, p_value); |
| 65 | } else if (what == "center_from") { |
| 66 | set_center_from(which, static_cast<CenterFrom>((int)p_value)); |
| 67 | } else if (what == "center_node") { |
| 68 | set_center_node(which, p_value); |
| 69 | } else if (what == "center_bone") { |
| 70 | set_center_bone(which, p_value); |
| 71 | } else if (what == "center_bone_name") { |
| 72 | set_center_bone_name(which, p_value); |
| 73 | } else if (what == "individual_config") { |
| 74 | set_individual_config(which, p_value); |
| 75 | } else if (what == "rotation_axis") { |
| 76 | set_rotation_axis(which, static_cast<RotationAxis>((int)p_value)); |
| 77 | } else if (what == "rotation_axis_vector") { |
| 78 | set_rotation_axis_vector(which, p_value); |
| 79 | } else if (what == "radius") { |
| 80 | String opt = path.get_slicec('/', 3); |
| 81 | if (opt == "value") { |
| 82 | set_radius(which, p_value); |
| 83 | } else if (opt == "damping_curve") { |
| 84 | set_radius_damping_curve(which, p_value); |
| 85 | } else { |
| 86 | return false; |
| 87 | } |
| 88 | } else if (what == "stiffness") { |
| 89 | String opt = path.get_slicec('/', 3); |
| 90 | if (opt == "value") { |
| 91 | set_stiffness(which, p_value); |
| 92 | } else if (opt == "damping_curve") { |
| 93 | set_stiffness_damping_curve(which, p_value); |
| 94 | } else { |
| 95 | return false; |
| 96 | } |
| 97 | } else if (what == "drag") { |
| 98 | String opt = path.get_slicec('/', 3); |
| 99 | if (opt == "value") { |
| 100 | set_drag(which, p_value); |
| 101 | } else if (opt == "damping_curve") { |
| 102 | set_drag_damping_curve(which, p_value); |
| 103 | } else { |
| 104 | return false; |
| 105 | } |
| 106 | } else if (what == "gravity") { |
| 107 | String opt = path.get_slicec('/', 3); |
| 108 | if (opt == "value") { |
| 109 | set_gravity(which, p_value); |
| 110 | } else if (opt == "damping_curve") { |
| 111 | set_gravity_damping_curve(which, p_value); |
| 112 | } else if (opt == "direction") { |
| 113 | set_gravity_direction(which, p_value); |
| 114 | } else { |
| 115 | return false; |
| 116 | } |
| 117 | } else if (what == "enable_all_child_collisions") { |
| 118 | set_enable_all_child_collisions(which, p_value); |
| 119 | } else if (what == "joint_count") { |
| 120 | set_joint_count(which, p_value); |
| 121 | } else if (what == "joints") { |
| 122 | int idx = path.get_slicec('/', 3).to_int(); |
| 123 | String prop = path.get_slicec('/', 4); |
| 124 | if (prop == "rotation_axis") { |
| 125 | set_joint_rotation_axis(which, idx, static_cast<RotationAxis>((int)p_value)); |
| 126 | } else if (prop == "rotation_axis_vector") { |
| 127 | set_joint_rotation_axis_vector(which, idx, p_value); |
| 128 | } else if (prop == "radius") { |
| 129 | set_joint_radius(which, idx, p_value); |
| 130 | } else if (prop == "stiffness") { |
| 131 | set_joint_stiffness(which, idx, p_value); |
| 132 | } else if (prop == "drag") { |
| 133 | set_joint_drag(which, idx, p_value); |
| 134 | } else if (prop == "gravity") { |
| 135 | set_joint_gravity(which, idx, p_value); |
| 136 | } else if (prop == "gravity_direction") { |
| 137 | set_joint_gravity_direction(which, idx, p_value); |
| 138 | } else { |
| 139 | return false; |
| 140 | } |
| 141 | } else if (what == "exclude_collision_count") { |
| 142 | set_exclude_collision_count(which, p_value); |
| 143 | } else if (what == "exclude_collisions") { |
| 144 | int idx = path.get_slicec('/', 3).to_int(); |
| 145 | set_exclude_collision_path(which, idx, p_value); |
| 146 | } else if (what == "collision_count") { |
| 147 | set_collision_count(which, p_value); |
| 148 | } else if (what == "collisions") { |
| 149 | int idx = path.get_slicec('/', 3).to_int(); |
| 150 | set_collision_path(which, idx, p_value); |
| 151 | } else { |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | bool SpringBoneSimulator3D::_get(const StringName &p_path, Variant &r_ret) const { |
| 159 | String path = p_path; |
| 160 | |
| 161 | if (path.begins_with("settings/")) { |
| 162 | int which = path.get_slicec('/', 1).to_int(); |
| 163 | String what = path.get_slicec('/', 2); |
| 164 | ERR_FAIL_INDEX_V(which, (int)settings.size(), false); |
| 165 | |
| 166 | if (what == "root_bone_name") { |
| 167 | r_ret = get_root_bone_name(which); |
| 168 | } else if (what == "root_bone") { |
| 169 | r_ret = get_root_bone(which); |
| 170 | } else if (what == "end_bone_name") { |
| 171 | r_ret = get_end_bone_name(which); |
| 172 | } else if (what == "end_bone") { |
| 173 | String opt = path.get_slicec('/', 3); |
| 174 | if (opt.is_empty()) { |
| 175 | r_ret = get_end_bone(which); |
| 176 | } else if (opt == "direction") { |
| 177 | r_ret = (int)get_end_bone_direction(which); |
| 178 | } else if (opt == "length") { |
| 179 | r_ret = get_end_bone_length(which); |
| 180 | } else { |
| 181 | return false; |
| 182 | } |
| 183 | } else if (what == "extend_end_bone") { |
| 184 | r_ret = is_end_bone_extended(which); |
| 185 | } else if (what == "center_from") { |
| 186 | r_ret = (int)get_center_from(which); |
| 187 | } else if (what == "center_node") { |
| 188 | r_ret = get_center_node(which); |
| 189 | } else if (what == "center_bone") { |
| 190 | r_ret = get_center_bone(which); |
| 191 | } else if (what == "center_bone_name") { |
| 192 | r_ret = get_center_bone_name(which); |
| 193 | } else if (what == "individual_config") { |
| 194 | r_ret = is_config_individual(which); |
| 195 | } else if (what == "rotation_axis") { |
| 196 | r_ret = (int)get_rotation_axis(which); |
| 197 | } else if (what == "rotation_axis_vector") { |
| 198 | r_ret = get_rotation_axis_vector(which); |
| 199 | } else if (what == "radius") { |
| 200 | String opt = path.get_slicec('/', 3); |
| 201 | if (opt == "value") { |
| 202 | r_ret = get_radius(which); |
| 203 | } else if (opt == "damping_curve") { |
| 204 | r_ret = get_radius_damping_curve(which); |
| 205 | } else { |
| 206 | return false; |
| 207 | } |
| 208 | } else if (what == "stiffness") { |
| 209 | String opt = path.get_slicec('/', 3); |
| 210 | if (opt == "value") { |
| 211 | r_ret = get_stiffness(which); |
| 212 | } else if (opt == "damping_curve") { |
| 213 | r_ret = get_stiffness_damping_curve(which); |
| 214 | } else { |
| 215 | return false; |
| 216 | } |
| 217 | } else if (what == "drag") { |
| 218 | String opt = path.get_slicec('/', 3); |
| 219 | if (opt == "value") { |
| 220 | r_ret = get_drag(which); |
| 221 | } else if (opt == "damping_curve") { |
| 222 | r_ret = get_drag_damping_curve(which); |
| 223 | } else { |
| 224 | return false; |
| 225 | } |
| 226 | } else if (what == "gravity") { |
| 227 | String opt = path.get_slicec('/', 3); |
| 228 | if (opt == "value") { |
| 229 | r_ret = get_gravity(which); |
| 230 | } else if (opt == "damping_curve") { |
| 231 | r_ret = get_gravity_damping_curve(which); |
| 232 | } else if (opt == "direction") { |
| 233 | r_ret = get_gravity_direction(which); |
| 234 | } else { |
| 235 | return false; |
| 236 | } |
| 237 | } else if (what == "enable_all_child_collisions") { |
| 238 | r_ret = are_all_child_collisions_enabled(which); |
| 239 | } else if (what == "joint_count") { |
| 240 | r_ret = get_joint_count(which); |
| 241 | } else if (what == "joints") { |
| 242 | int idx = path.get_slicec('/', 3).to_int(); |
| 243 | String prop = path.get_slicec('/', 4); |
| 244 | if (prop == "bone_name") { |
| 245 | r_ret = get_joint_bone_name(which, idx); |
| 246 | } else if (prop == "bone") { |
| 247 | r_ret = get_joint_bone(which, idx); |
| 248 | } else if (prop == "rotation_axis") { |
| 249 | r_ret = (int)get_joint_rotation_axis(which, idx); |
| 250 | } else if (prop == "rotation_axis_vector") { |
| 251 | r_ret = get_joint_rotation_axis_vector(which, idx); |
| 252 | } else if (prop == "radius") { |
| 253 | r_ret = get_joint_radius(which, idx); |
| 254 | } else if (prop == "stiffness") { |
| 255 | r_ret = get_joint_stiffness(which, idx); |
| 256 | } else if (prop == "drag") { |
| 257 | r_ret = get_joint_drag(which, idx); |
| 258 | } else if (prop == "gravity") { |
| 259 | r_ret = get_joint_gravity(which, idx); |
| 260 | } else if (prop == "gravity_direction") { |
| 261 | r_ret = get_joint_gravity_direction(which, idx); |
| 262 | } else { |
| 263 | return false; |
| 264 | } |
| 265 | } else if (what == "exclude_collision_count") { |
| 266 | r_ret = get_exclude_collision_count(which); |
| 267 | } else if (what == "exclude_collisions") { |
| 268 | int idx = path.get_slicec('/', 3).to_int(); |
| 269 | r_ret = get_exclude_collision_path(which, idx); |
| 270 | } else if (what == "collision_count") { |
| 271 | r_ret = get_collision_count(which); |
| 272 | } else if (what == "collisions") { |
| 273 | int idx = path.get_slicec('/', 3).to_int(); |
| 274 | r_ret = get_collision_path(which, idx); |
| 275 | } else { |
| 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | void SpringBoneSimulator3D::_get_property_list(List<PropertyInfo> *p_list) const { |
| 283 | String enum_hint; |
| 284 | Skeleton3D *skeleton = get_skeleton(); |
| 285 | if (skeleton) { |
| 286 | enum_hint = skeleton->get_concatenated_bone_names(); |
| 287 | } |
| 288 | |
| 289 | LocalVector<PropertyInfo> props; |
| 290 | |
| 291 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 292 | String path = "settings/" + itos(i) + "/"; |
| 293 | props.push_back(PropertyInfo(Variant::STRING, path + "root_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint)); |
| 294 | props.push_back(PropertyInfo(Variant::INT, path + "root_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); |
| 295 | props.push_back(PropertyInfo(Variant::STRING, path + "end_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint)); |
| 296 | props.push_back(PropertyInfo(Variant::INT, path + "end_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); |
| 297 | props.push_back(PropertyInfo(Variant::BOOL, path + "extend_end_bone")); |
| 298 | props.push_back(PropertyInfo(Variant::INT, path + "end_bone/direction", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_bone_direction())); |
| 299 | props.push_back(PropertyInfo(Variant::FLOAT, path + "end_bone/length", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m")); |
| 300 | props.push_back(PropertyInfo(Variant::INT, path + "center_from", PROPERTY_HINT_ENUM, "WorldOrigin,Node,Bone")); |
| 301 | props.push_back(PropertyInfo(Variant::NODE_PATH, path + "center_node")); |
| 302 | props.push_back(PropertyInfo(Variant::STRING, path + "center_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint)); |
| 303 | props.push_back(PropertyInfo(Variant::INT, path + "center_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR)); |
| 304 | props.push_back(PropertyInfo(Variant::BOOL, path + "individual_config")); |
| 305 | props.push_back(PropertyInfo(Variant::INT, path + "rotation_axis", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_rotation_axis())); |
| 306 | props.push_back(PropertyInfo(Variant::VECTOR3, path + "rotation_axis_vector")); |
| 307 | props.push_back(PropertyInfo(Variant::FLOAT, path + "radius/value", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m")); |
| 308 | props.push_back(PropertyInfo(Variant::OBJECT, path + "radius/damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve")); |
| 309 | props.push_back(PropertyInfo(Variant::FLOAT, path + "stiffness/value", PROPERTY_HINT_RANGE, "0,4,0.01,or_greater")); |
| 310 | props.push_back(PropertyInfo(Variant::OBJECT, path + "stiffness/damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve")); |
| 311 | props.push_back(PropertyInfo(Variant::FLOAT, path + "drag/value", PROPERTY_HINT_RANGE, "0,1,0.01,or_greater")); |
| 312 | props.push_back(PropertyInfo(Variant::OBJECT, path + "drag/damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve")); |
| 313 | props.push_back(PropertyInfo(Variant::FLOAT, path + "gravity/value", PROPERTY_HINT_RANGE, "0,1,0.01,or_greater,or_less,suffix:m/s")); |
| 314 | props.push_back(PropertyInfo(Variant::OBJECT, path + "gravity/damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve")); |
| 315 | props.push_back(PropertyInfo(Variant::VECTOR3, path + "gravity/direction")); |
| 316 | props.push_back(PropertyInfo(Variant::INT, path + "joint_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY, "Joints," + path + "joints/,static,const")); |
| 317 | for (uint32_t j = 0; j < settings[i]->joints.size(); j++) { |
| 318 | String joint_path = path + "joints/" + itos(j) + "/"; |
| 319 | props.push_back(PropertyInfo(Variant::STRING, joint_path + "bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint, PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY)); |
| 320 | props.push_back(PropertyInfo(Variant::INT, joint_path + "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY)); |
| 321 | props.push_back(PropertyInfo(Variant::INT, joint_path + "rotation_axis", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_rotation_axis())); |
| 322 | props.push_back(PropertyInfo(Variant::VECTOR3, joint_path + "rotation_axis_vector")); |
| 323 | props.push_back(PropertyInfo(Variant::FLOAT, joint_path + "radius", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m")); |
| 324 | props.push_back(PropertyInfo(Variant::FLOAT, joint_path + "stiffness", PROPERTY_HINT_RANGE, "0,4,0.01,or_greater")); |
| 325 | props.push_back(PropertyInfo(Variant::FLOAT, joint_path + "drag", PROPERTY_HINT_RANGE, "0,1,0.01,or_greater")); |
| 326 | props.push_back(PropertyInfo(Variant::FLOAT, joint_path + "gravity", PROPERTY_HINT_RANGE, "0,1,0.01,or_greater,or_less,suffix:m/s")); |
| 327 | props.push_back(PropertyInfo(Variant::VECTOR3, joint_path + "gravity_direction")); |
| 328 | } |
| 329 | props.push_back(PropertyInfo(Variant::BOOL, path + "enable_all_child_collisions")); |
| 330 | props.push_back(PropertyInfo(Variant::INT, path + "exclude_collision_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY, "Exclude Collisions," + path + "exclude_collisions/")); |
| 331 | for (uint32_t j = 0; j < settings[i]->exclude_collisions.size(); j++) { |
| 332 | String collision_path = path + "exclude_collisions/" + itos(j); |
| 333 | props.push_back(PropertyInfo(Variant::NODE_PATH, collision_path, PROPERTY_HINT_NODE_PATH_VALID_TYPES, "SpringBoneCollision3D")); |
| 334 | } |
| 335 | props.push_back(PropertyInfo(Variant::INT, path + "collision_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY, "Collisions," + path + "collisions/")); |
| 336 | for (uint32_t j = 0; j < settings[i]->collisions.size(); j++) { |
| 337 | String collision_path = path + "collisions/" + itos(j); |
| 338 | props.push_back(PropertyInfo(Variant::NODE_PATH, collision_path, PROPERTY_HINT_NODE_PATH_VALID_TYPES, "SpringBoneCollision3D")); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | for (PropertyInfo &p : props) { |
| 343 | _validate_dynamic_prop(p); |
| 344 | p_list->push_back(p); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void SpringBoneSimulator3D::_validate_dynamic_prop(PropertyInfo &p_property) const { |
| 349 | PackedStringArray split = p_property.name.split("/"); |
| 350 | if (split.size() > 2 && split[0] == "settings") { |
| 351 | int which = split[1].to_int(); |
| 352 | |
| 353 | // Extended end bone option. |
| 354 | bool force_hide = false; |
| 355 | if (split[2] == "extend_end_bone" && get_end_bone(which) == -1) { |
| 356 | p_property.usage = PROPERTY_USAGE_NONE; |
| 357 | force_hide = true; |
| 358 | } |
| 359 | if (force_hide || (split[2] == "end_bone" && !is_end_bone_extended(which) && split.size() > 3)) { |
| 360 | p_property.usage = PROPERTY_USAGE_NONE; |
| 361 | } |
| 362 | |
| 363 | // Center option. |
| 364 | if (get_center_from(which) != CENTER_FROM_BONE && (split[2] == "center_bone" || split[2] == "center_bone_name")) { |
| 365 | p_property.usage = PROPERTY_USAGE_NONE; |
| 366 | } |
| 367 | if (get_center_from(which) != CENTER_FROM_NODE && split[2] == "center_node") { |
| 368 | p_property.usage = PROPERTY_USAGE_NONE; |
| 369 | } |
| 370 | |
| 371 | // Joints option. |
| 372 | if (is_config_individual(which)) { |
| 373 | if (split[2] == "rotation_axis" || split[2] == "rotation_axis_vector" || split[2] == "radius" || split[2] == "radius_damping_curve" || |
| 374 | split[2] == "stiffness" || split[2] == "stiffness_damping_curve" || |
| 375 | split[2] == "drag" || split[2] == "drag_damping_curve" || |
| 376 | split[2] == "gravity" || split[2] == "gravity_damping_curve" || split[2] == "gravity_direction") { |
| 377 | p_property.usage = PROPERTY_USAGE_NONE; |
| 378 | } |
| 379 | } else { |
| 380 | if (split[2] == "joints" || split[2] == "joint_count") { |
| 381 | // Don't storage them since they are overridden by _update_joints(). |
| 382 | p_property.usage ^= PROPERTY_USAGE_STORAGE; |
| 383 | p_property.usage |= PROPERTY_USAGE_READ_ONLY; |
| 384 | } |
| 385 | if (split[2] == "rotation_axis_vector" && get_rotation_axis(which) != ROTATION_AXIS_CUSTOM) { |
| 386 | p_property.usage = PROPERTY_USAGE_NONE; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // Collisions option. |
| 391 | if (are_all_child_collisions_enabled(which)) { |
| 392 | if (split[2] == "collisions" || split[2] == "collision_count") { |
| 393 | p_property.usage = PROPERTY_USAGE_NONE; |
| 394 | } |
| 395 | } else { |
| 396 | if (split[2] == "exclude_collisions" || split[2] == "exclude_collision_count") { |
| 397 | p_property.usage = PROPERTY_USAGE_NONE; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | if (split.size() > 3 && split[0] == "settings") { |
| 402 | int which = split[1].to_int(); |
| 403 | int joint = split[3].to_int(); |
| 404 | // Joints option. |
| 405 | if (split[2] == "joints" && split.size() > 4) { |
| 406 | if (split[4] == "rotation_axis_vector" && get_joint_rotation_axis(which, joint) != ROTATION_AXIS_CUSTOM) { |
| 407 | p_property.usage = PROPERTY_USAGE_NONE; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | void SpringBoneSimulator3D::_notification(int p_what) { |
| 414 | switch (p_what) { |
| 415 | case NOTIFICATION_ENTER_TREE: { |
| 416 | #ifdef TOOLS_ENABLED |
| 417 | if (Engine::get_singleton()->is_editor_hint()) { |
| 418 | set_notify_local_transform(true); // Used for updating gizmo in editor. |
| 419 | } |
| 420 | #endif // TOOLS_ENABLED |
| 421 | _make_collisions_dirty(); |
| 422 | _make_all_joints_dirty(); |
| 423 | } break; |
| 424 | #ifdef TOOLS_ENABLED |
| 425 | case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: { |
| 426 | _make_gizmo_dirty(); |
| 427 | } break; |
| 428 | case NOTIFICATION_EDITOR_PRE_SAVE: { |
| 429 | saving = true; |
| 430 | } break; |
| 431 | case NOTIFICATION_EDITOR_POST_SAVE: { |
| 432 | saving = false; |
| 433 | } break; |
| 434 | #endif // TOOLS_ENABLED |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Setting. |
| 439 | |
| 440 | void SpringBoneSimulator3D::set_root_bone_name(int p_index, const String &p_bone_name) { |
| 441 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 442 | settings[p_index]->root_bone_name = p_bone_name; |
| 443 | Skeleton3D *sk = get_skeleton(); |
| 444 | if (sk) { |
| 445 | set_root_bone(p_index, sk->find_bone(settings[p_index]->root_bone_name)); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | String SpringBoneSimulator3D::get_root_bone_name(int p_index) const { |
| 450 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String()); |
| 451 | return settings[p_index]->root_bone_name; |
| 452 | } |
| 453 | |
| 454 | void SpringBoneSimulator3D::set_root_bone(int p_index, int p_bone) { |
| 455 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 456 | bool changed = settings[p_index]->root_bone != p_bone; |
| 457 | settings[p_index]->root_bone = p_bone; |
| 458 | Skeleton3D *sk = get_skeleton(); |
| 459 | if (sk) { |
| 460 | if (settings[p_index]->root_bone <= -1 || settings[p_index]->root_bone >= sk->get_bone_count()) { |
| 461 | WARN_PRINT("Root bone index out of range!"); |
| 462 | settings[p_index]->root_bone = -1; |
| 463 | } else { |
| 464 | settings[p_index]->root_bone_name = sk->get_bone_name(settings[p_index]->root_bone); |
| 465 | } |
| 466 | } |
| 467 | if (changed) { |
| 468 | _update_joint_array(p_index); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | int SpringBoneSimulator3D::get_root_bone(int p_index) const { |
| 473 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 474 | return settings[p_index]->root_bone; |
| 475 | } |
| 476 | |
| 477 | void SpringBoneSimulator3D::set_end_bone_name(int p_index, const String &p_bone_name) { |
| 478 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 479 | settings[p_index]->end_bone_name = p_bone_name; |
| 480 | Skeleton3D *sk = get_skeleton(); |
| 481 | if (sk) { |
| 482 | set_end_bone(p_index, sk->find_bone(settings[p_index]->end_bone_name)); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | String SpringBoneSimulator3D::get_end_bone_name(int p_index) const { |
| 487 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String()); |
| 488 | return settings[p_index]->end_bone_name; |
| 489 | } |
| 490 | |
| 491 | void SpringBoneSimulator3D::set_end_bone(int p_index, int p_bone) { |
| 492 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 493 | bool changed = settings[p_index]->end_bone != p_bone; |
| 494 | settings[p_index]->end_bone = p_bone; |
| 495 | Skeleton3D *sk = get_skeleton(); |
| 496 | if (sk) { |
| 497 | if (settings[p_index]->end_bone <= -1 || settings[p_index]->end_bone >= sk->get_bone_count()) { |
| 498 | WARN_PRINT("End bone index out of range!"); |
| 499 | settings[p_index]->end_bone = -1; |
| 500 | } else { |
| 501 | settings[p_index]->end_bone_name = sk->get_bone_name(settings[p_index]->end_bone); |
| 502 | } |
| 503 | } |
| 504 | if (changed) { |
| 505 | _update_joint_array(p_index); |
| 506 | } |
| 507 | notify_property_list_changed(); |
| 508 | } |
| 509 | |
| 510 | int SpringBoneSimulator3D::get_end_bone(int p_index) const { |
| 511 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 512 | return settings[p_index]->end_bone; |
| 513 | } |
| 514 | |
| 515 | void SpringBoneSimulator3D::set_extend_end_bone(int p_index, bool p_enabled) { |
| 516 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 517 | settings[p_index]->extend_end_bone = p_enabled; |
| 518 | _make_joints_dirty(p_index, true); |
| 519 | notify_property_list_changed(); |
| 520 | } |
| 521 | |
| 522 | bool SpringBoneSimulator3D::is_end_bone_extended(int p_index) const { |
| 523 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 524 | return settings[p_index]->extend_end_bone; |
| 525 | } |
| 526 | |
| 527 | void SpringBoneSimulator3D::set_end_bone_direction(int p_index, BoneDirection p_bone_direction) { |
| 528 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 529 | settings[p_index]->end_bone_direction = p_bone_direction; |
| 530 | #ifdef TOOLS_ENABLED |
| 531 | _make_gizmo_dirty(); |
| 532 | #endif // TOOLS_ENABLED |
| 533 | if (mutable_bone_axes) { |
| 534 | return; // Chain dir will be recaluclated in _update_bone_axis(). |
| 535 | } |
| 536 | _make_joints_dirty(p_index, true); |
| 537 | } |
| 538 | |
| 539 | SkeletonModifier3D::BoneDirection SpringBoneSimulator3D::get_end_bone_direction(int p_index) const { |
| 540 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), BONE_DIRECTION_FROM_PARENT); |
| 541 | return settings[p_index]->end_bone_direction; |
| 542 | } |
| 543 | |
| 544 | void SpringBoneSimulator3D::set_end_bone_length(int p_index, float p_length) { |
| 545 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 546 | float old = settings[p_index]->end_bone_length; |
| 547 | settings[p_index]->end_bone_length = p_length; |
| 548 | #ifdef TOOLS_ENABLED |
| 549 | _make_gizmo_dirty(); |
| 550 | #endif // TOOLS_ENABLED |
| 551 | if (mutable_bone_axes && Math::is_zero_approx(old) == Math::is_zero_approx(p_length)) { |
| 552 | return; // If chain size is not changed, length will be recaluclated in _update_bone_axis(). |
| 553 | } |
| 554 | _make_joints_dirty(p_index, true); |
| 555 | } |
| 556 | |
| 557 | float SpringBoneSimulator3D::get_end_bone_length(int p_index) const { |
| 558 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 559 | return settings[p_index]->end_bone_length; |
| 560 | } |
| 561 | |
| 562 | Vector3 SpringBoneSimulator3D::get_end_bone_axis(int p_end_bone, BoneDirection p_direction) const { |
| 563 | Vector3 axis; |
| 564 | if (p_direction == BONE_DIRECTION_FROM_PARENT) { |
| 565 | Skeleton3D *sk = get_skeleton(); |
| 566 | if (sk) { |
| 567 | axis = sk->get_bone_rest(p_end_bone).basis.xform_inv(mutable_bone_axes ? sk->get_bone_pose(p_end_bone).origin : sk->get_bone_rest(p_end_bone).origin); |
| 568 | axis.normalize(); |
| 569 | } |
| 570 | } else { |
| 571 | axis = get_vector_from_bone_axis(static_cast<BoneAxis>((int)p_direction)); |
| 572 | } |
| 573 | return axis; |
| 574 | } |
| 575 | |
| 576 | void SpringBoneSimulator3D::set_center_from(int p_index, CenterFrom p_center_from) { |
| 577 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 578 | bool center_changed = settings[p_index]->center_from != p_center_from; |
| 579 | settings[p_index]->center_from = p_center_from; |
| 580 | if (center_changed) { |
| 581 | reset(); |
| 582 | } |
| 583 | notify_property_list_changed(); |
| 584 | } |
| 585 | |
| 586 | SpringBoneSimulator3D::CenterFrom SpringBoneSimulator3D::get_center_from(int p_index) const { |
| 587 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), CENTER_FROM_WORLD_ORIGIN); |
| 588 | return settings[p_index]->center_from; |
| 589 | } |
| 590 | |
| 591 | void SpringBoneSimulator3D::set_center_node(int p_index, const NodePath &p_node_path) { |
| 592 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 593 | bool center_changed = settings[p_index]->center_node != p_node_path; |
| 594 | settings[p_index]->center_node = p_node_path; |
| 595 | if (center_changed) { |
| 596 | reset(); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | NodePath SpringBoneSimulator3D::get_center_node(int p_index) const { |
| 601 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), NodePath()); |
| 602 | return settings[p_index]->center_node; |
| 603 | } |
| 604 | |
| 605 | void SpringBoneSimulator3D::set_center_bone_name(int p_index, const String &p_bone_name) { |
| 606 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 607 | settings[p_index]->center_bone_name = p_bone_name; |
| 608 | Skeleton3D *sk = get_skeleton(); |
| 609 | if (sk) { |
| 610 | set_center_bone(p_index, sk->find_bone(settings[p_index]->center_bone_name)); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | String SpringBoneSimulator3D::get_center_bone_name(int p_index) const { |
| 615 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String()); |
| 616 | return settings[p_index]->center_bone_name; |
| 617 | } |
| 618 | |
| 619 | void SpringBoneSimulator3D::set_center_bone(int p_index, int p_bone) { |
| 620 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 621 | bool center_changed = settings[p_index]->center_bone != p_bone; |
| 622 | settings[p_index]->center_bone = p_bone; |
| 623 | Skeleton3D *sk = get_skeleton(); |
| 624 | if (sk) { |
| 625 | if (settings[p_index]->center_bone <= -1 || settings[p_index]->center_bone >= sk->get_bone_count()) { |
| 626 | WARN_PRINT("Center bone index out of range!"); |
| 627 | settings[p_index]->center_bone = -1; |
| 628 | } else { |
| 629 | settings[p_index]->center_bone_name = sk->get_bone_name(settings[p_index]->center_bone); |
| 630 | } |
| 631 | } |
| 632 | if (center_changed) { |
| 633 | reset(); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | int SpringBoneSimulator3D::get_center_bone(int p_index) const { |
| 638 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 639 | return settings[p_index]->center_bone; |
| 640 | } |
| 641 | |
| 642 | void SpringBoneSimulator3D::set_radius(int p_index, float p_radius) { |
| 643 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 644 | if (is_config_individual(p_index)) { |
| 645 | return; // Joint config is individual mode. |
| 646 | } |
| 647 | settings[p_index]->radius = p_radius; |
| 648 | _make_joints_dirty(p_index); |
| 649 | } |
| 650 | |
| 651 | float SpringBoneSimulator3D::get_radius(int p_index) const { |
| 652 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 653 | return settings[p_index]->radius; |
| 654 | } |
| 655 | |
| 656 | void SpringBoneSimulator3D::set_radius_damping_curve(int p_index, const Ref<Curve> &p_damping_curve) { |
| 657 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 658 | if (is_config_individual(p_index)) { |
| 659 | return; // Joint config is individual mode. |
| 660 | } |
| 661 | if (settings[p_index]->radius_damping_curve.is_valid()) { |
| 662 | settings[p_index]->radius_damping_curve->disconnect_changed(callable_mp(this, &SpringBoneSimulator3D::_make_joints_dirty)); |
| 663 | } |
| 664 | settings[p_index]->radius_damping_curve = p_damping_curve; |
| 665 | if (settings[p_index]->radius_damping_curve.is_valid()) { |
| 666 | settings[p_index]->radius_damping_curve->connect_changed(callable_mp(this, &SpringBoneSimulator3D::_make_joints_dirty).bind(p_index)); |
| 667 | } |
| 668 | _make_joints_dirty(p_index); |
| 669 | } |
| 670 | |
| 671 | Ref<Curve> SpringBoneSimulator3D::get_radius_damping_curve(int p_index) const { |
| 672 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Ref<Curve>()); |
| 673 | return settings[p_index]->radius_damping_curve; |
| 674 | } |
| 675 | |
| 676 | void SpringBoneSimulator3D::set_stiffness(int p_index, float p_stiffness) { |
| 677 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 678 | if (is_config_individual(p_index)) { |
| 679 | return; // Joint config is individual mode. |
| 680 | } |
| 681 | settings[p_index]->stiffness = p_stiffness; |
| 682 | _make_joints_dirty(p_index); |
| 683 | } |
| 684 | |
| 685 | float SpringBoneSimulator3D::get_stiffness(int p_index) const { |
| 686 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 687 | return settings[p_index]->stiffness; |
| 688 | } |
| 689 | |
| 690 | void SpringBoneSimulator3D::set_stiffness_damping_curve(int p_index, const Ref<Curve> &p_damping_curve) { |
| 691 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 692 | if (is_config_individual(p_index)) { |
| 693 | return; // Joint config is individual mode. |
| 694 | } |
| 695 | if (settings[p_index]->stiffness_damping_curve.is_valid()) { |
| 696 | settings[p_index]->stiffness_damping_curve->disconnect_changed(callable_mp(this, &SpringBoneSimulator3D::_make_joints_dirty)); |
| 697 | } |
| 698 | settings[p_index]->stiffness_damping_curve = p_damping_curve; |
| 699 | if (settings[p_index]->stiffness_damping_curve.is_valid()) { |
| 700 | settings[p_index]->stiffness_damping_curve->connect_changed(callable_mp(this, &SpringBoneSimulator3D::_make_joints_dirty).bind(p_index)); |
| 701 | } |
| 702 | _make_joints_dirty(p_index); |
| 703 | } |
| 704 | |
| 705 | Ref<Curve> SpringBoneSimulator3D::get_stiffness_damping_curve(int p_index) const { |
| 706 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Ref<Curve>()); |
| 707 | return settings[p_index]->stiffness_damping_curve; |
| 708 | } |
| 709 | |
| 710 | void SpringBoneSimulator3D::set_drag(int p_index, float p_drag) { |
| 711 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 712 | if (is_config_individual(p_index)) { |
| 713 | return; // Joint config is individual mode. |
| 714 | } |
| 715 | settings[p_index]->drag = p_drag; |
| 716 | _make_joints_dirty(p_index); |
| 717 | } |
| 718 | |
| 719 | float SpringBoneSimulator3D::get_drag(int p_index) const { |
| 720 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 721 | return settings[p_index]->drag; |
| 722 | } |
| 723 | |
| 724 | void SpringBoneSimulator3D::set_drag_damping_curve(int p_index, const Ref<Curve> &p_damping_curve) { |
| 725 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 726 | if (is_config_individual(p_index)) { |
| 727 | return; // Joint config is individual mode. |
| 728 | } |
| 729 | if (settings[p_index]->drag_damping_curve.is_valid()) { |
| 730 | settings[p_index]->drag_damping_curve->disconnect_changed(callable_mp(this, &SpringBoneSimulator3D::_make_joints_dirty)); |
| 731 | } |
| 732 | settings[p_index]->drag_damping_curve = p_damping_curve; |
| 733 | if (settings[p_index]->drag_damping_curve.is_valid()) { |
| 734 | settings[p_index]->drag_damping_curve->connect_changed(callable_mp(this, &SpringBoneSimulator3D::_make_joints_dirty).bind(p_index)); |
| 735 | } |
| 736 | _make_joints_dirty(p_index); |
| 737 | } |
| 738 | |
| 739 | Ref<Curve> SpringBoneSimulator3D::get_drag_damping_curve(int p_index) const { |
| 740 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Ref<Curve>()); |
| 741 | return settings[p_index]->drag_damping_curve; |
| 742 | } |
| 743 | |
| 744 | void SpringBoneSimulator3D::set_gravity(int p_index, float p_gravity) { |
| 745 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 746 | if (is_config_individual(p_index)) { |
| 747 | return; // Joint config is individual mode. |
| 748 | } |
| 749 | settings[p_index]->gravity = p_gravity; |
| 750 | _make_joints_dirty(p_index); |
| 751 | } |
| 752 | |
| 753 | float SpringBoneSimulator3D::get_gravity(int p_index) const { |
| 754 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 755 | return settings[p_index]->gravity; |
| 756 | } |
| 757 | |
| 758 | void SpringBoneSimulator3D::set_gravity_damping_curve(int p_index, const Ref<Curve> &p_damping_curve) { |
| 759 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 760 | if (is_config_individual(p_index)) { |
| 761 | return; // Joint config is individual mode. |
| 762 | } |
| 763 | if (settings[p_index]->gravity_damping_curve.is_valid()) { |
| 764 | settings[p_index]->gravity_damping_curve->disconnect_changed(callable_mp(this, &SpringBoneSimulator3D::_make_joints_dirty)); |
| 765 | } |
| 766 | settings[p_index]->gravity_damping_curve = p_damping_curve; |
| 767 | if (settings[p_index]->gravity_damping_curve.is_valid()) { |
| 768 | settings[p_index]->gravity_damping_curve->connect_changed(callable_mp(this, &SpringBoneSimulator3D::_make_joints_dirty).bind(p_index)); |
| 769 | } |
| 770 | _make_joints_dirty(p_index); |
| 771 | } |
| 772 | |
| 773 | Ref<Curve> SpringBoneSimulator3D::get_gravity_damping_curve(int p_index) const { |
| 774 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Ref<Curve>()); |
| 775 | return settings[p_index]->gravity_damping_curve; |
| 776 | } |
| 777 | |
| 778 | void SpringBoneSimulator3D::set_gravity_direction(int p_index, const Vector3 &p_gravity_direction) { |
| 779 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 780 | ERR_FAIL_COND(p_gravity_direction.is_zero_approx()); |
| 781 | if (is_config_individual(p_index)) { |
| 782 | return; // Joint config is individual mode. |
| 783 | } |
| 784 | settings[p_index]->gravity_direction = p_gravity_direction; |
| 785 | _make_joints_dirty(p_index); |
| 786 | } |
| 787 | |
| 788 | Vector3 SpringBoneSimulator3D::get_gravity_direction(int p_index) const { |
| 789 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3(0, -1, 0)); |
| 790 | return settings[p_index]->gravity_direction; |
| 791 | } |
| 792 | |
| 793 | void SpringBoneSimulator3D::set_rotation_axis(int p_index, RotationAxis p_axis) { |
| 794 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 795 | if (is_config_individual(p_index)) { |
| 796 | return; // Joint config is individual mode. |
| 797 | } |
| 798 | settings[p_index]->rotation_axis = p_axis; |
| 799 | _make_joints_dirty(p_index); |
| 800 | notify_property_list_changed(); |
| 801 | } |
| 802 | |
| 803 | SkeletonModifier3D::RotationAxis SpringBoneSimulator3D::get_rotation_axis(int p_index) const { |
| 804 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), ROTATION_AXIS_ALL); |
| 805 | return settings[p_index]->rotation_axis; |
| 806 | } |
| 807 | |
| 808 | void SpringBoneSimulator3D::set_rotation_axis_vector(int p_index, const Vector3 &p_vector) { |
| 809 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 810 | if (is_config_individual(p_index) || settings[p_index]->rotation_axis != ROTATION_AXIS_CUSTOM) { |
| 811 | return; // Joint config is individual mode. |
| 812 | } |
| 813 | settings[p_index]->rotation_axis_vector = p_vector; |
| 814 | _make_joints_dirty(p_index); |
| 815 | } |
| 816 | |
| 817 | Vector3 SpringBoneSimulator3D::get_rotation_axis_vector(int p_index) const { |
| 818 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3()); |
| 819 | Vector3 ret; |
| 820 | switch (settings[p_index]->rotation_axis) { |
| 821 | case ROTATION_AXIS_X: |
| 822 | ret = Vector3(1, 0, 0); |
| 823 | break; |
| 824 | case ROTATION_AXIS_Y: |
| 825 | ret = Vector3(0, 1, 0); |
| 826 | break; |
| 827 | case ROTATION_AXIS_Z: |
| 828 | ret = Vector3(0, 0, 1); |
| 829 | break; |
| 830 | case ROTATION_AXIS_ALL: |
| 831 | ret = Vector3(0, 0, 0); |
| 832 | break; |
| 833 | case ROTATION_AXIS_CUSTOM: |
| 834 | ret = settings[p_index]->rotation_axis_vector; |
| 835 | break; |
| 836 | } |
| 837 | return ret; |
| 838 | } |
| 839 | |
| 840 | void SpringBoneSimulator3D::set_setting_count(int p_count) { |
| 841 | ERR_FAIL_COND(p_count < 0); |
| 842 | |
| 843 | int delta = p_count - (int)settings.size(); |
| 844 | if (delta < 0) { |
| 845 | for (int i = delta; i < 0; i++) { |
| 846 | memdelete(settings[(int)settings.size() + i]); |
| 847 | settings[(int)settings.size() + i] = nullptr; |
| 848 | } |
| 849 | } |
| 850 | settings.resize(p_count); |
| 851 | delta++; |
| 852 | if (delta > 1) { |
| 853 | for (int i = 1; i < delta; i++) { |
| 854 | settings[p_count - i] = memnew(SpringBone3DSetting); |
| 855 | } |
| 856 | } |
| 857 | notify_property_list_changed(); |
| 858 | } |
| 859 | |
| 860 | int SpringBoneSimulator3D::get_setting_count() const { |
| 861 | return settings.size(); |
| 862 | } |
| 863 | |
| 864 | void SpringBoneSimulator3D::clear_settings() { |
| 865 | set_setting_count(0); |
| 866 | } |
| 867 | |
| 868 | // Individual joints. |
| 869 | |
| 870 | void SpringBoneSimulator3D::set_individual_config(int p_index, bool p_enabled) { |
| 871 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 872 | settings[p_index]->individual_config = p_enabled; |
| 873 | _make_joints_dirty(p_index, true); |
| 874 | notify_property_list_changed(); |
| 875 | } |
| 876 | |
| 877 | bool SpringBoneSimulator3D::is_config_individual(int p_index) const { |
| 878 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 879 | return settings[p_index]->individual_config; |
| 880 | } |
| 881 | |
| 882 | String SpringBoneSimulator3D::get_joint_bone_name(int p_index, int p_joint) const { |
| 883 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String()); |
| 884 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 885 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), String()); |
| 886 | return joints[p_joint]->bone_name; |
| 887 | } |
| 888 | |
| 889 | void SpringBoneSimulator3D::_set_joint_bone(int p_index, int p_joint, int p_bone) { |
| 890 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 891 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 892 | ERR_FAIL_INDEX(p_joint, (int)joints.size()); |
| 893 | joints[p_joint]->bone = p_bone; |
| 894 | Skeleton3D *sk = get_skeleton(); |
| 895 | if (sk) { |
| 896 | if (joints[p_joint]->bone <= -1 || joints[p_joint]->bone >= sk->get_bone_count()) { |
| 897 | WARN_PRINT("Joint bone index out of range!"); |
| 898 | joints[p_joint]->bone = -1; |
| 899 | } else { |
| 900 | joints[p_joint]->bone_name = sk->get_bone_name(joints[p_joint]->bone); |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | int SpringBoneSimulator3D::get_joint_bone(int p_index, int p_joint) const { |
| 906 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1); |
| 907 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 908 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), -1); |
| 909 | return joints[p_joint]->bone; |
| 910 | } |
| 911 | |
| 912 | void SpringBoneSimulator3D::set_joint_radius(int p_index, int p_joint, float p_radius) { |
| 913 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 914 | if (!is_config_individual(p_index)) { |
| 915 | return; // Joints are read-only. |
| 916 | } |
| 917 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 918 | ERR_FAIL_INDEX(p_joint, (int)joints.size()); |
| 919 | joints[p_joint]->radius = p_radius; |
| 920 | #ifdef TOOLS_ENABLED |
| 921 | _make_gizmo_dirty(); |
| 922 | #endif // TOOLS_ENABLED |
| 923 | } |
| 924 | |
| 925 | float SpringBoneSimulator3D::get_joint_radius(int p_index, int p_joint) const { |
| 926 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 927 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 928 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), 0); |
| 929 | return joints[p_joint]->radius; |
| 930 | } |
| 931 | |
| 932 | void SpringBoneSimulator3D::set_joint_stiffness(int p_index, int p_joint, float p_stiffness) { |
| 933 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 934 | if (!is_config_individual(p_index)) { |
| 935 | return; // Joints are read-only. |
| 936 | } |
| 937 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 938 | ERR_FAIL_INDEX(p_joint, (int)joints.size()); |
| 939 | joints[p_joint]->stiffness = p_stiffness; |
| 940 | } |
| 941 | |
| 942 | float SpringBoneSimulator3D::get_joint_stiffness(int p_index, int p_joint) const { |
| 943 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 944 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 945 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), 0); |
| 946 | return joints[p_joint]->stiffness; |
| 947 | } |
| 948 | |
| 949 | void SpringBoneSimulator3D::set_joint_drag(int p_index, int p_joint, float p_drag) { |
| 950 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 951 | if (!is_config_individual(p_index)) { |
| 952 | return; // Joints are read-only. |
| 953 | } |
| 954 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 955 | ERR_FAIL_INDEX(p_joint, (int)joints.size()); |
| 956 | joints[p_joint]->drag = p_drag; |
| 957 | } |
| 958 | |
| 959 | float SpringBoneSimulator3D::get_joint_drag(int p_index, int p_joint) const { |
| 960 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 961 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 962 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), 0); |
| 963 | return joints[p_joint]->drag; |
| 964 | } |
| 965 | |
| 966 | void SpringBoneSimulator3D::set_joint_gravity(int p_index, int p_joint, float p_gravity) { |
| 967 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 968 | if (!is_config_individual(p_index)) { |
| 969 | return; // Joints are read-only. |
| 970 | } |
| 971 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 972 | ERR_FAIL_INDEX(p_joint, (int)joints.size()); |
| 973 | joints[p_joint]->gravity = p_gravity; |
| 974 | } |
| 975 | |
| 976 | float SpringBoneSimulator3D::get_joint_gravity(int p_index, int p_joint) const { |
| 977 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 978 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 979 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), 0); |
| 980 | return joints[p_joint]->gravity; |
| 981 | } |
| 982 | |
| 983 | void SpringBoneSimulator3D::set_joint_gravity_direction(int p_index, int p_joint, const Vector3 &p_gravity_direction) { |
| 984 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 985 | ERR_FAIL_COND(p_gravity_direction.is_zero_approx()); |
| 986 | if (!is_config_individual(p_index)) { |
| 987 | return; // Joints are read-only. |
| 988 | } |
| 989 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 990 | ERR_FAIL_INDEX(p_joint, (int)joints.size()); |
| 991 | joints[p_joint]->gravity_direction = p_gravity_direction; |
| 992 | } |
| 993 | |
| 994 | Vector3 SpringBoneSimulator3D::get_joint_gravity_direction(int p_index, int p_joint) const { |
| 995 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3(0, -1, 0)); |
| 996 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 997 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), Vector3(0, -1, 0)); |
| 998 | return joints[p_joint]->gravity_direction; |
| 999 | } |
| 1000 | |
| 1001 | void SpringBoneSimulator3D::set_joint_rotation_axis(int p_index, int p_joint, RotationAxis p_axis) { |
| 1002 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1003 | if (!is_config_individual(p_index)) { |
| 1004 | return; // Joints are read-only. |
| 1005 | } |
| 1006 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 1007 | ERR_FAIL_INDEX(p_joint, (int)joints.size()); |
| 1008 | joints[p_joint]->rotation_axis = p_axis; |
| 1009 | Skeleton3D *sk = get_skeleton(); |
| 1010 | if (sk) { |
| 1011 | _validate_rotation_axis(sk, p_index, p_joint); |
| 1012 | } |
| 1013 | notify_property_list_changed(); |
| 1014 | settings[p_index]->simulation_dirty = true; |
| 1015 | #ifdef TOOLS_ENABLED |
| 1016 | _make_gizmo_dirty(); |
| 1017 | #endif // TOOLS_ENABLED |
| 1018 | } |
| 1019 | |
| 1020 | SkeletonModifier3D::RotationAxis SpringBoneSimulator3D::get_joint_rotation_axis(int p_index, int p_joint) const { |
| 1021 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), ROTATION_AXIS_ALL); |
| 1022 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 1023 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), ROTATION_AXIS_ALL); |
| 1024 | return joints[p_joint]->rotation_axis; |
| 1025 | } |
| 1026 | |
| 1027 | void SpringBoneSimulator3D::set_joint_rotation_axis_vector(int p_index, int p_joint, const Vector3 &p_vector) { |
| 1028 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1029 | if (!is_config_individual(p_index) || settings[p_index]->rotation_axis != ROTATION_AXIS_CUSTOM) { |
| 1030 | return; // Joints are read-only. |
| 1031 | } |
| 1032 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 1033 | ERR_FAIL_INDEX(p_joint, (int)joints.size()); |
| 1034 | joints[p_joint]->rotation_axis_vector = p_vector; |
| 1035 | Skeleton3D *sk = get_skeleton(); |
| 1036 | if (sk) { |
| 1037 | _validate_rotation_axis(sk, p_index, p_joint); |
| 1038 | } |
| 1039 | settings[p_index]->simulation_dirty = true; |
| 1040 | #ifdef TOOLS_ENABLED |
| 1041 | _make_gizmo_dirty(); |
| 1042 | #endif // TOOLS_ENABLED |
| 1043 | } |
| 1044 | |
| 1045 | Vector3 SpringBoneSimulator3D::get_joint_rotation_axis_vector(int p_index, int p_joint) const { |
| 1046 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3()); |
| 1047 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 1048 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), Vector3()); |
| 1049 | return joints[p_joint]->get_rotation_axis_vector(); |
| 1050 | } |
| 1051 | |
| 1052 | void SpringBoneSimulator3D::set_joint_count(int p_index, int p_count) { |
| 1053 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1054 | ERR_FAIL_COND(p_count < 0); |
| 1055 | LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 1056 | int delta = p_count - joints.size(); |
| 1057 | if (delta < 0) { |
| 1058 | for (int i = delta; i < 0; i++) { |
| 1059 | memdelete(joints[joints.size() + i]); |
| 1060 | joints[joints.size() + i] = nullptr; |
| 1061 | } |
| 1062 | } |
| 1063 | joints.resize(p_count); |
| 1064 | delta++; |
| 1065 | if (delta > 1) { |
| 1066 | for (int i = 1; i < delta; i++) { |
| 1067 | joints[p_count - i] = memnew(SpringBone3DJointSetting); |
| 1068 | } |
| 1069 | } |
| 1070 | notify_property_list_changed(); |
| 1071 | } |
| 1072 | |
| 1073 | int SpringBoneSimulator3D::get_joint_count(int p_index) const { |
| 1074 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 1075 | const LocalVector<SpringBone3DJointSetting *> &joints = settings[p_index]->joints; |
| 1076 | return joints.size(); |
| 1077 | } |
| 1078 | |
| 1079 | // Individual collisions. |
| 1080 | |
| 1081 | void SpringBoneSimulator3D::set_enable_all_child_collisions(int p_index, bool p_enabled) { |
| 1082 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1083 | settings[p_index]->enable_all_child_collisions = p_enabled; |
| 1084 | notify_property_list_changed(); |
| 1085 | } |
| 1086 | |
| 1087 | bool SpringBoneSimulator3D::are_all_child_collisions_enabled(int p_index) const { |
| 1088 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false); |
| 1089 | return settings[p_index]->enable_all_child_collisions; |
| 1090 | } |
| 1091 | |
| 1092 | void SpringBoneSimulator3D::set_exclude_collision_path(int p_index, int p_collision, const NodePath &p_node_path) { |
| 1093 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1094 | if (!are_all_child_collisions_enabled(p_index)) { |
| 1095 | return; // Exclude collision list is disabled. |
| 1096 | } |
| 1097 | LocalVector<NodePath> &setting_exclude_collisions = settings[p_index]->exclude_collisions; |
| 1098 | ERR_FAIL_INDEX(p_collision, (int)setting_exclude_collisions.size()); |
| 1099 | setting_exclude_collisions[p_collision] = NodePath(); // Reset first. |
| 1100 | if (is_inside_tree()) { |
| 1101 | Node *node = get_node_or_null(p_node_path); |
| 1102 | if (!node) { |
| 1103 | _make_collisions_dirty(); |
| 1104 | return; |
| 1105 | } |
| 1106 | node = node->get_parent(); |
| 1107 | if (!node || node != this) { |
| 1108 | _make_collisions_dirty(); |
| 1109 | ERR_FAIL_EDMSG("Collision must be child of current SpringBoneSimulator3D."); |
| 1110 | } |
| 1111 | } |
| 1112 | setting_exclude_collisions[p_collision] = p_node_path; |
| 1113 | _make_collisions_dirty(); |
| 1114 | } |
| 1115 | |
| 1116 | NodePath SpringBoneSimulator3D::get_exclude_collision_path(int p_index, int p_collision) const { |
| 1117 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), NodePath()); |
| 1118 | const LocalVector<NodePath> &setting_exclude_collisions = settings[p_index]->exclude_collisions; |
| 1119 | ERR_FAIL_INDEX_V(p_collision, (int)setting_exclude_collisions.size(), NodePath()); |
| 1120 | return setting_exclude_collisions[p_collision]; |
| 1121 | } |
| 1122 | |
| 1123 | void SpringBoneSimulator3D::set_exclude_collision_count(int p_index, int p_count) { |
| 1124 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1125 | if (!are_all_child_collisions_enabled(p_index)) { |
| 1126 | return; // Exclude collision list is disabled. |
| 1127 | } |
| 1128 | LocalVector<NodePath> &setting_exclude_collisions = settings[p_index]->exclude_collisions; |
| 1129 | setting_exclude_collisions.resize(p_count); |
| 1130 | _make_collisions_dirty(); |
| 1131 | notify_property_list_changed(); |
| 1132 | } |
| 1133 | |
| 1134 | int SpringBoneSimulator3D::get_exclude_collision_count(int p_index) const { |
| 1135 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 1136 | const LocalVector<NodePath> &setting_exclude_collisions = settings[p_index]->exclude_collisions; |
| 1137 | return setting_exclude_collisions.size(); |
| 1138 | } |
| 1139 | |
| 1140 | void SpringBoneSimulator3D::clear_exclude_collisions(int p_index) { |
| 1141 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1142 | if (!are_all_child_collisions_enabled(p_index)) { |
| 1143 | return; // Exclude collision list is disabled. |
| 1144 | } |
| 1145 | set_exclude_collision_count(p_index, 0); |
| 1146 | } |
| 1147 | |
| 1148 | void SpringBoneSimulator3D::set_collision_path(int p_index, int p_collision, const NodePath &p_node_path) { |
| 1149 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1150 | if (are_all_child_collisions_enabled(p_index)) { |
| 1151 | return; // Collision list is disabled. |
| 1152 | } |
| 1153 | LocalVector<NodePath> &setting_collisions = settings[p_index]->collisions; |
| 1154 | ERR_FAIL_INDEX(p_collision, (int)setting_collisions.size()); |
| 1155 | setting_collisions[p_collision] = NodePath(); // Reset first. |
| 1156 | if (is_inside_tree()) { |
| 1157 | Node *node = get_node_or_null(p_node_path); |
| 1158 | if (!node) { |
| 1159 | _make_collisions_dirty(); |
| 1160 | return; |
| 1161 | } |
| 1162 | node = node->get_parent(); |
| 1163 | if (!node || node != this) { |
| 1164 | _make_collisions_dirty(); |
| 1165 | ERR_FAIL_EDMSG("Collision must be child of current SpringBoneSimulator3D."); |
| 1166 | } |
| 1167 | } |
| 1168 | setting_collisions[p_collision] = p_node_path; |
| 1169 | _make_collisions_dirty(); |
| 1170 | } |
| 1171 | |
| 1172 | NodePath SpringBoneSimulator3D::get_collision_path(int p_index, int p_collision) const { |
| 1173 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), NodePath()); |
| 1174 | const LocalVector<NodePath> &setting_collisions = settings[p_index]->collisions; |
| 1175 | ERR_FAIL_INDEX_V(p_collision, (int)setting_collisions.size(), NodePath()); |
| 1176 | return setting_collisions[p_collision]; |
| 1177 | } |
| 1178 | |
| 1179 | void SpringBoneSimulator3D::set_collision_count(int p_index, int p_count) { |
| 1180 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1181 | if (are_all_child_collisions_enabled(p_index)) { |
| 1182 | return; // Collision list is disabled. |
| 1183 | } |
| 1184 | LocalVector<NodePath> &setting_collisions = settings[p_index]->collisions; |
| 1185 | setting_collisions.resize(p_count); |
| 1186 | _make_collisions_dirty(); |
| 1187 | notify_property_list_changed(); |
| 1188 | } |
| 1189 | |
| 1190 | int SpringBoneSimulator3D::get_collision_count(int p_index) const { |
| 1191 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0); |
| 1192 | const LocalVector<NodePath> &setting_collisions = settings[p_index]->collisions; |
| 1193 | return setting_collisions.size(); |
| 1194 | } |
| 1195 | |
| 1196 | void SpringBoneSimulator3D::clear_collisions(int p_index) { |
| 1197 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1198 | if (are_all_child_collisions_enabled(p_index)) { |
| 1199 | return; // Collision list is disabled. |
| 1200 | } |
| 1201 | set_collision_count(p_index, 0); |
| 1202 | } |
| 1203 | |
| 1204 | LocalVector<ObjectID> SpringBoneSimulator3D::get_valid_collision_instance_ids(int p_index) { |
| 1205 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), LocalVector<ObjectID>()); |
| 1206 | if (collisions_dirty) { |
| 1207 | _find_collisions(); |
| 1208 | } |
| 1209 | return settings[p_index]->cached_collisions; |
| 1210 | } |
| 1211 | |
| 1212 | void SpringBoneSimulator3D::set_external_force(const Vector3 &p_force) { |
| 1213 | external_force = p_force; |
| 1214 | } |
| 1215 | |
| 1216 | Vector3 SpringBoneSimulator3D::get_external_force() const { |
| 1217 | return external_force; |
| 1218 | } |
| 1219 | |
| 1220 | void SpringBoneSimulator3D::set_mutable_bone_axes(bool p_enabled) { |
| 1221 | mutable_bone_axes = p_enabled; |
| 1222 | for (SpringBone3DSetting *setting : settings) { |
| 1223 | setting->simulation_dirty = true; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | bool SpringBoneSimulator3D::are_bone_axes_mutable() const { |
| 1228 | return mutable_bone_axes; |
| 1229 | } |
| 1230 | |
| 1231 | void SpringBoneSimulator3D::_bind_methods() { |
| 1232 | // Setting. |
| 1233 | ClassDB::bind_method(D_METHOD("set_root_bone_name", "index", "bone_name"), &SpringBoneSimulator3D::set_root_bone_name); |
| 1234 | ClassDB::bind_method(D_METHOD("get_root_bone_name", "index"), &SpringBoneSimulator3D::get_root_bone_name); |
| 1235 | ClassDB::bind_method(D_METHOD("set_root_bone", "index", "bone"), &SpringBoneSimulator3D::set_root_bone); |
| 1236 | ClassDB::bind_method(D_METHOD("get_root_bone", "index"), &SpringBoneSimulator3D::get_root_bone); |
| 1237 | |
| 1238 | ClassDB::bind_method(D_METHOD("set_end_bone_name", "index", "bone_name"), &SpringBoneSimulator3D::set_end_bone_name); |
| 1239 | ClassDB::bind_method(D_METHOD("get_end_bone_name", "index"), &SpringBoneSimulator3D::get_end_bone_name); |
| 1240 | ClassDB::bind_method(D_METHOD("set_end_bone", "index", "bone"), &SpringBoneSimulator3D::set_end_bone); |
| 1241 | ClassDB::bind_method(D_METHOD("get_end_bone", "index"), &SpringBoneSimulator3D::get_end_bone); |
| 1242 | |
| 1243 | ClassDB::bind_method(D_METHOD("set_extend_end_bone", "index", "enabled"), &SpringBoneSimulator3D::set_extend_end_bone); |
| 1244 | ClassDB::bind_method(D_METHOD("is_end_bone_extended", "index"), &SpringBoneSimulator3D::is_end_bone_extended); |
| 1245 | ClassDB::bind_method(D_METHOD("set_end_bone_direction", "index", "bone_direction"), &SpringBoneSimulator3D::set_end_bone_direction); |
| 1246 | ClassDB::bind_method(D_METHOD("get_end_bone_direction", "index"), &SpringBoneSimulator3D::get_end_bone_direction); |
| 1247 | ClassDB::bind_method(D_METHOD("set_end_bone_length", "index", "length"), &SpringBoneSimulator3D::set_end_bone_length); |
| 1248 | ClassDB::bind_method(D_METHOD("get_end_bone_length", "index"), &SpringBoneSimulator3D::get_end_bone_length); |
| 1249 | |
| 1250 | ClassDB::bind_method(D_METHOD("set_center_from", "index", "center_from"), &SpringBoneSimulator3D::set_center_from); |
| 1251 | ClassDB::bind_method(D_METHOD("get_center_from", "index"), &SpringBoneSimulator3D::get_center_from); |
| 1252 | ClassDB::bind_method(D_METHOD("set_center_node", "index", "node_path"), &SpringBoneSimulator3D::set_center_node); |
| 1253 | ClassDB::bind_method(D_METHOD("get_center_node", "index"), &SpringBoneSimulator3D::get_center_node); |
| 1254 | ClassDB::bind_method(D_METHOD("set_center_bone_name", "index", "bone_name"), &SpringBoneSimulator3D::set_center_bone_name); |
| 1255 | ClassDB::bind_method(D_METHOD("get_center_bone_name", "index"), &SpringBoneSimulator3D::get_center_bone_name); |
| 1256 | ClassDB::bind_method(D_METHOD("set_center_bone", "index", "bone"), &SpringBoneSimulator3D::set_center_bone); |
| 1257 | ClassDB::bind_method(D_METHOD("get_center_bone", "index"), &SpringBoneSimulator3D::get_center_bone); |
| 1258 | |
| 1259 | ClassDB::bind_method(D_METHOD("set_radius", "index", "radius"), &SpringBoneSimulator3D::set_radius); |
| 1260 | ClassDB::bind_method(D_METHOD("get_radius", "index"), &SpringBoneSimulator3D::get_radius); |
| 1261 | ClassDB::bind_method(D_METHOD("set_rotation_axis", "index", "axis"), &SpringBoneSimulator3D::set_rotation_axis); |
| 1262 | ClassDB::bind_method(D_METHOD("get_rotation_axis", "index"), &SpringBoneSimulator3D::get_rotation_axis); |
| 1263 | ClassDB::bind_method(D_METHOD("set_rotation_axis_vector", "index", "vector"), &SpringBoneSimulator3D::set_rotation_axis_vector); |
| 1264 | ClassDB::bind_method(D_METHOD("get_rotation_axis_vector", "index"), &SpringBoneSimulator3D::get_rotation_axis_vector); |
| 1265 | ClassDB::bind_method(D_METHOD("set_radius_damping_curve", "index", "curve"), &SpringBoneSimulator3D::set_radius_damping_curve); |
| 1266 | ClassDB::bind_method(D_METHOD("get_radius_damping_curve", "index"), &SpringBoneSimulator3D::get_radius_damping_curve); |
| 1267 | ClassDB::bind_method(D_METHOD("set_stiffness", "index", "stiffness"), &SpringBoneSimulator3D::set_stiffness); |
| 1268 | ClassDB::bind_method(D_METHOD("get_stiffness", "index"), &SpringBoneSimulator3D::get_stiffness); |
| 1269 | ClassDB::bind_method(D_METHOD("set_stiffness_damping_curve", "index", "curve"), &SpringBoneSimulator3D::set_stiffness_damping_curve); |
| 1270 | ClassDB::bind_method(D_METHOD("get_stiffness_damping_curve", "index"), &SpringBoneSimulator3D::get_stiffness_damping_curve); |
| 1271 | ClassDB::bind_method(D_METHOD("set_drag", "index", "drag"), &SpringBoneSimulator3D::set_drag); |
| 1272 | ClassDB::bind_method(D_METHOD("get_drag", "index"), &SpringBoneSimulator3D::get_drag); |
| 1273 | ClassDB::bind_method(D_METHOD("set_drag_damping_curve", "index", "curve"), &SpringBoneSimulator3D::set_drag_damping_curve); |
| 1274 | ClassDB::bind_method(D_METHOD("get_drag_damping_curve", "index"), &SpringBoneSimulator3D::get_drag_damping_curve); |
| 1275 | ClassDB::bind_method(D_METHOD("set_gravity", "index", "gravity"), &SpringBoneSimulator3D::set_gravity); |
| 1276 | ClassDB::bind_method(D_METHOD("get_gravity", "index"), &SpringBoneSimulator3D::get_gravity); |
| 1277 | ClassDB::bind_method(D_METHOD("set_gravity_damping_curve", "index", "curve"), &SpringBoneSimulator3D::set_gravity_damping_curve); |
| 1278 | ClassDB::bind_method(D_METHOD("get_gravity_damping_curve", "index"), &SpringBoneSimulator3D::get_gravity_damping_curve); |
| 1279 | ClassDB::bind_method(D_METHOD("set_gravity_direction", "index", "gravity_direction"), &SpringBoneSimulator3D::set_gravity_direction); |
| 1280 | ClassDB::bind_method(D_METHOD("get_gravity_direction", "index"), &SpringBoneSimulator3D::get_gravity_direction); |
| 1281 | |
| 1282 | ClassDB::bind_method(D_METHOD("set_setting_count", "count"), &SpringBoneSimulator3D::set_setting_count); |
| 1283 | ClassDB::bind_method(D_METHOD("get_setting_count"), &SpringBoneSimulator3D::get_setting_count); |
| 1284 | ClassDB::bind_method(D_METHOD("clear_settings"), &SpringBoneSimulator3D::clear_settings); |
| 1285 | |
| 1286 | // Individual joints. |
| 1287 | ClassDB::bind_method(D_METHOD("set_individual_config", "index", "enabled"), &SpringBoneSimulator3D::set_individual_config); |
| 1288 | ClassDB::bind_method(D_METHOD("is_config_individual", "index"), &SpringBoneSimulator3D::is_config_individual); |
| 1289 | |
| 1290 | ClassDB::bind_method(D_METHOD("get_joint_bone_name", "index", "joint"), &SpringBoneSimulator3D::get_joint_bone_name); |
| 1291 | ClassDB::bind_method(D_METHOD("get_joint_bone", "index", "joint"), &SpringBoneSimulator3D::get_joint_bone); |
| 1292 | ClassDB::bind_method(D_METHOD("set_joint_rotation_axis", "index", "joint", "axis"), &SpringBoneSimulator3D::set_joint_rotation_axis); |
| 1293 | ClassDB::bind_method(D_METHOD("get_joint_rotation_axis", "index", "joint"), &SpringBoneSimulator3D::get_joint_rotation_axis); |
| 1294 | ClassDB::bind_method(D_METHOD("set_joint_rotation_axis_vector", "index", "joint", "vector"), &SpringBoneSimulator3D::set_joint_rotation_axis_vector); |
| 1295 | ClassDB::bind_method(D_METHOD("get_joint_rotation_axis_vector", "index", "joint"), &SpringBoneSimulator3D::get_joint_rotation_axis_vector); |
| 1296 | ClassDB::bind_method(D_METHOD("set_joint_radius", "index", "joint", "radius"), &SpringBoneSimulator3D::set_joint_radius); |
| 1297 | ClassDB::bind_method(D_METHOD("get_joint_radius", "index", "joint"), &SpringBoneSimulator3D::get_joint_radius); |
| 1298 | ClassDB::bind_method(D_METHOD("set_joint_stiffness", "index", "joint", "stiffness"), &SpringBoneSimulator3D::set_joint_stiffness); |
| 1299 | ClassDB::bind_method(D_METHOD("get_joint_stiffness", "index", "joint"), &SpringBoneSimulator3D::get_joint_stiffness); |
| 1300 | ClassDB::bind_method(D_METHOD("set_joint_drag", "index", "joint", "drag"), &SpringBoneSimulator3D::set_joint_drag); |
| 1301 | ClassDB::bind_method(D_METHOD("get_joint_drag", "index", "joint"), &SpringBoneSimulator3D::get_joint_drag); |
| 1302 | ClassDB::bind_method(D_METHOD("set_joint_gravity", "index", "joint", "gravity"), &SpringBoneSimulator3D::set_joint_gravity); |
| 1303 | ClassDB::bind_method(D_METHOD("get_joint_gravity", "index", "joint"), &SpringBoneSimulator3D::get_joint_gravity); |
| 1304 | ClassDB::bind_method(D_METHOD("set_joint_gravity_direction", "index", "joint", "gravity_direction"), &SpringBoneSimulator3D::set_joint_gravity_direction); |
| 1305 | ClassDB::bind_method(D_METHOD("get_joint_gravity_direction", "index", "joint"), &SpringBoneSimulator3D::get_joint_gravity_direction); |
| 1306 | |
| 1307 | ClassDB::bind_method(D_METHOD("get_joint_count", "index"), &SpringBoneSimulator3D::get_joint_count); |
| 1308 | |
| 1309 | // Individual collisions. |
| 1310 | ClassDB::bind_method(D_METHOD("set_enable_all_child_collisions", "index", "enabled"), &SpringBoneSimulator3D::set_enable_all_child_collisions); |
| 1311 | ClassDB::bind_method(D_METHOD("are_all_child_collisions_enabled", "index"), &SpringBoneSimulator3D::are_all_child_collisions_enabled); |
| 1312 | |
| 1313 | ClassDB::bind_method(D_METHOD("set_exclude_collision_path", "index", "collision", "node_path"), &SpringBoneSimulator3D::set_exclude_collision_path); |
| 1314 | ClassDB::bind_method(D_METHOD("get_exclude_collision_path", "index", "collision"), &SpringBoneSimulator3D::get_exclude_collision_path); |
| 1315 | |
| 1316 | ClassDB::bind_method(D_METHOD("set_exclude_collision_count", "index", "count"), &SpringBoneSimulator3D::set_exclude_collision_count); |
| 1317 | ClassDB::bind_method(D_METHOD("get_exclude_collision_count", "index"), &SpringBoneSimulator3D::get_exclude_collision_count); |
| 1318 | ClassDB::bind_method(D_METHOD("clear_exclude_collisions", "index"), &SpringBoneSimulator3D::clear_exclude_collisions); |
| 1319 | |
| 1320 | ClassDB::bind_method(D_METHOD("set_collision_path", "index", "collision", "node_path"), &SpringBoneSimulator3D::set_collision_path); |
| 1321 | ClassDB::bind_method(D_METHOD("get_collision_path", "index", "collision"), &SpringBoneSimulator3D::get_collision_path); |
| 1322 | |
| 1323 | ClassDB::bind_method(D_METHOD("set_collision_count", "index", "count"), &SpringBoneSimulator3D::set_collision_count); |
| 1324 | ClassDB::bind_method(D_METHOD("get_collision_count", "index"), &SpringBoneSimulator3D::get_collision_count); |
| 1325 | ClassDB::bind_method(D_METHOD("clear_collisions", "index"), &SpringBoneSimulator3D::clear_collisions); |
| 1326 | |
| 1327 | ClassDB::bind_method(D_METHOD("set_external_force", "force"), &SpringBoneSimulator3D::set_external_force); |
| 1328 | ClassDB::bind_method(D_METHOD("get_external_force"), &SpringBoneSimulator3D::get_external_force); |
| 1329 | |
| 1330 | ClassDB::bind_method(D_METHOD("set_mutable_bone_axes", "enabled"), &SpringBoneSimulator3D::set_mutable_bone_axes); |
| 1331 | ClassDB::bind_method(D_METHOD("are_bone_axes_mutable"), &SpringBoneSimulator3D::are_bone_axes_mutable); |
| 1332 | |
| 1333 | // To process manually. |
| 1334 | ClassDB::bind_method(D_METHOD("reset"), &SpringBoneSimulator3D::reset); |
| 1335 | |
| 1336 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "external_force", PROPERTY_HINT_RANGE, "-99999,99999,or_greater,or_less,hide_control,suffix:m/s"), "set_external_force", "get_external_force"); |
| 1337 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mutable_bone_axes"), "set_mutable_bone_axes", "are_bone_axes_mutable"); |
| 1338 | ADD_ARRAY_COUNT("Settings", "setting_count", "set_setting_count", "get_setting_count", "settings/"); |
| 1339 | |
| 1340 | BIND_ENUM_CONSTANT(CENTER_FROM_WORLD_ORIGIN); |
| 1341 | BIND_ENUM_CONSTANT(CENTER_FROM_NODE); |
| 1342 | BIND_ENUM_CONSTANT(CENTER_FROM_BONE); |
| 1343 | } |
| 1344 | |
| 1345 | void SpringBoneSimulator3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) { |
| 1346 | if (p_old && p_old->is_connected(SNAME("rest_updated"), callable_mp(this, &SpringBoneSimulator3D::_make_all_joints_dirty))) { |
| 1347 | p_old->disconnect(SNAME("rest_updated"), callable_mp(this, &SpringBoneSimulator3D::_make_all_joints_dirty)); |
| 1348 | } |
| 1349 | if (p_new && !p_new->is_connected(SNAME("rest_updated"), callable_mp(this, &SpringBoneSimulator3D::_make_all_joints_dirty))) { |
| 1350 | p_new->connect(SNAME("rest_updated"), callable_mp(this, &SpringBoneSimulator3D::_make_all_joints_dirty)); |
| 1351 | } |
| 1352 | _make_all_joints_dirty(); |
| 1353 | } |
| 1354 | |
| 1355 | void SpringBoneSimulator3D::_validate_bone_names() { |
| 1356 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 1357 | // Prior bone name. |
| 1358 | if (!settings[i]->root_bone_name.is_empty()) { |
| 1359 | set_root_bone_name(i, settings[i]->root_bone_name); |
| 1360 | } else if (settings[i]->root_bone != -1) { |
| 1361 | set_root_bone(i, settings[i]->root_bone); |
| 1362 | } |
| 1363 | // Prior bone name. |
| 1364 | if (!settings[i]->end_bone_name.is_empty()) { |
| 1365 | set_end_bone_name(i, settings[i]->end_bone_name); |
| 1366 | } else if (settings[i]->end_bone != -1) { |
| 1367 | set_end_bone(i, settings[i]->end_bone); |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | void SpringBoneSimulator3D::_make_joints_dirty(int p_index, bool p_reset) { |
| 1373 | ERR_FAIL_INDEX(p_index, (int)settings.size()); |
| 1374 | settings[p_index]->joints_dirty = true; |
| 1375 | if (joints_dirty) { |
| 1376 | return; |
| 1377 | } |
| 1378 | joints_dirty = true; |
| 1379 | callable_mp(this, &SpringBoneSimulator3D::_update_joints).call_deferred(p_reset); |
| 1380 | } |
| 1381 | |
| 1382 | void SpringBoneSimulator3D::_make_all_joints_dirty() { |
| 1383 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 1384 | _update_joint_array(i); |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | void SpringBoneSimulator3D::add_child_notify(Node *p_child) { |
| 1389 | if (Object::cast_to<SpringBoneCollision3D>(p_child)) { |
| 1390 | _make_collisions_dirty(); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | void SpringBoneSimulator3D::move_child_notify(Node *p_child) { |
| 1395 | if (Object::cast_to<SpringBoneCollision3D>(p_child)) { |
| 1396 | _make_collisions_dirty(); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | void SpringBoneSimulator3D::remove_child_notify(Node *p_child) { |
| 1401 | if (Object::cast_to<SpringBoneCollision3D>(p_child)) { |
| 1402 | _make_collisions_dirty(); |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | void SpringBoneSimulator3D::_validate_rotation_axes(Skeleton3D *p_skeleton) const { |
| 1407 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 1408 | for (uint32_t j = 0; j < settings[i]->joints.size(); j++) { |
| 1409 | _validate_rotation_axis(p_skeleton, i, j); |
| 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | void SpringBoneSimulator3D::_validate_rotation_axis(Skeleton3D *p_skeleton, int p_index, int p_joint) const { |
| 1415 | RotationAxis axis = settings[p_index]->joints[p_joint]->rotation_axis; |
| 1416 | if (axis == ROTATION_AXIS_ALL) { |
| 1417 | return; |
| 1418 | } |
| 1419 | Vector3 rot = get_joint_rotation_axis_vector(p_index, p_joint).normalized(); |
| 1420 | Vector3 fwd; |
| 1421 | if (p_joint < (int)settings[p_index]->joints.size() - 1) { |
| 1422 | fwd = p_skeleton->get_bone_rest(settings[p_index]->joints[p_joint + 1]->bone).origin; |
| 1423 | } else if (settings[p_index]->extend_end_bone) { |
| 1424 | fwd = get_end_bone_axis(settings[p_index]->end_bone, settings[p_index]->end_bone_direction); |
| 1425 | if (fwd.is_zero_approx()) { |
| 1426 | return; |
| 1427 | } |
| 1428 | } |
| 1429 | fwd.normalize(); |
| 1430 | if (Math::is_equal_approx(Math::abs(rot.dot(fwd)), 1)) { |
| 1431 | WARN_PRINT_ED("Setting: " + itos(p_index) + " Joint: " + itos(p_joint) + ": Rotation axis and forward vector are colinear. This is not advised as it may cause unwanted rotation."); |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | void SpringBoneSimulator3D::_find_collisions() { |
| 1436 | if (!collisions_dirty) { |
| 1437 | return; |
| 1438 | } |
| 1439 | collisions.clear(); |
| 1440 | for (int i = 0; i < get_child_count(); i++) { |
| 1441 | SpringBoneCollision3D *c = Object::cast_to<SpringBoneCollision3D>(get_child(i)); |
| 1442 | if (c) { |
| 1443 | collisions.push_back(c->get_instance_id()); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | bool setting_updated = false; |
| 1448 | |
| 1449 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 1450 | LocalVector<ObjectID> &cache = settings[i]->cached_collisions; |
| 1451 | cache.clear(); |
| 1452 | if (!settings[i]->enable_all_child_collisions) { |
| 1453 | // Allow list. |
| 1454 | LocalVector<NodePath> &setting_collisions = settings[i]->collisions; |
| 1455 | for (uint32_t j = 0; j < setting_collisions.size(); j++) { |
| 1456 | Node *n = get_node_or_null(setting_collisions[j]); |
| 1457 | if (!n) { |
| 1458 | continue; |
| 1459 | } |
| 1460 | ObjectID id = n->get_instance_id(); |
| 1461 | if (!collisions.has(id)) { |
| 1462 | setting_collisions[j] = NodePath(); // Clear path if not found. |
| 1463 | } else { |
| 1464 | cache.push_back(id); |
| 1465 | } |
| 1466 | } |
| 1467 | } else { |
| 1468 | // Deny list. |
| 1469 | LocalVector<uint32_t> masks; |
| 1470 | LocalVector<NodePath> &setting_exclude_collisions = settings[i]->exclude_collisions; |
| 1471 | for (uint32_t j = 0; j < setting_exclude_collisions.size(); j++) { |
| 1472 | Node *n = get_node_or_null(setting_exclude_collisions[j]); |
| 1473 | if (!n) { |
| 1474 | continue; |
| 1475 | } |
| 1476 | ObjectID id = n->get_instance_id(); |
| 1477 | int find = collisions.find(id); |
| 1478 | if (find < 0) { |
| 1479 | setting_exclude_collisions[j] = NodePath(); // Clear path if not found. |
| 1480 | } else { |
| 1481 | masks.push_back((uint32_t)find); |
| 1482 | } |
| 1483 | } |
| 1484 | uint32_t mask_index = 0; |
| 1485 | for (uint32_t j = 0; j < collisions.size(); j++) { |
| 1486 | if (mask_index < masks.size() && j == masks[mask_index]) { |
| 1487 | mask_index++; |
| 1488 | continue; |
| 1489 | } |
| 1490 | cache.push_back(collisions[j]); |
| 1491 | } |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | collisions_dirty = false; |
| 1496 | |
| 1497 | if (setting_updated) { |
| 1498 | notify_property_list_changed(); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | void SpringBoneSimulator3D::_process_collisions() { |
| 1503 | for (const ObjectID &oid : collisions) { |
| 1504 | Object *t_obj = ObjectDB::get_instance(oid); |
| 1505 | if (!t_obj) { |
| 1506 | continue; |
| 1507 | } |
| 1508 | SpringBoneCollision3D *col = Object::cast_to<SpringBoneCollision3D>(t_obj); |
| 1509 | if (!col) { |
| 1510 | continue; |
| 1511 | } |
| 1512 | col->sync_pose(); |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | void SpringBoneSimulator3D::_make_collisions_dirty() { |
| 1517 | collisions_dirty = true; |
| 1518 | } |
| 1519 | |
| 1520 | void SpringBoneSimulator3D::_update_joint_array(int p_index) { |
| 1521 | _make_joints_dirty(p_index, true); |
| 1522 | |
| 1523 | Skeleton3D *sk = get_skeleton(); |
| 1524 | int current_bone = settings[p_index]->end_bone; |
| 1525 | int root_bone = settings[p_index]->root_bone; |
| 1526 | if (!sk || current_bone < 0 || root_bone < 0) { |
| 1527 | set_joint_count(p_index, 0); |
| 1528 | return; |
| 1529 | } |
| 1530 | |
| 1531 | // Validation. |
| 1532 | bool valid = false; |
| 1533 | while (current_bone >= 0) { |
| 1534 | if (current_bone == root_bone) { |
| 1535 | valid = true; |
| 1536 | break; |
| 1537 | } |
| 1538 | current_bone = sk->get_bone_parent(current_bone); |
| 1539 | } |
| 1540 | |
| 1541 | if (!valid) { |
| 1542 | set_joint_count(p_index, 0); |
| 1543 | ERR_FAIL_EDMSG("End bone must be the same as or a child of the root bone."); |
| 1544 | } |
| 1545 | |
| 1546 | LocalVector<int> new_joints; |
| 1547 | current_bone = settings[p_index]->end_bone; |
| 1548 | while (current_bone != root_bone) { |
| 1549 | new_joints.push_back(current_bone); |
| 1550 | current_bone = sk->get_bone_parent(current_bone); |
| 1551 | } |
| 1552 | new_joints.push_back(current_bone); |
| 1553 | new_joints.reverse(); |
| 1554 | |
| 1555 | set_joint_count(p_index, new_joints.size()); |
| 1556 | for (uint32_t i = 0; i < new_joints.size(); i++) { |
| 1557 | _set_joint_bone(p_index, i, new_joints[i]); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | void SpringBoneSimulator3D::_update_joints(bool p_reset) { |
| 1562 | if (!joints_dirty) { |
| 1563 | return; |
| 1564 | } |
| 1565 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 1566 | if (!settings[i]->joints_dirty) { |
| 1567 | continue; |
| 1568 | } |
| 1569 | if (settings[i]->individual_config) { |
| 1570 | settings[i]->simulation_dirty = p_reset; |
| 1571 | settings[i]->joints_dirty = false; |
| 1572 | continue; // Abort. |
| 1573 | } |
| 1574 | LocalVector<SpringBone3DJointSetting *> &joints = settings[i]->joints; |
| 1575 | float unit = joints.size() > 0 ? (1.0 / float(joints.size() - 1)) : 0.0; |
| 1576 | for (uint32_t j = 0; j < joints.size(); j++) { |
| 1577 | float offset = j * unit; |
| 1578 | |
| 1579 | if (settings[i]->radius_damping_curve.is_valid()) { |
| 1580 | joints[j]->radius = settings[i]->radius * settings[i]->radius_damping_curve->sample_baked(offset); |
| 1581 | } else { |
| 1582 | joints[j]->radius = settings[i]->radius; |
| 1583 | } |
| 1584 | |
| 1585 | if (settings[i]->stiffness_damping_curve.is_valid()) { |
| 1586 | joints[j]->stiffness = settings[i]->stiffness * settings[i]->stiffness_damping_curve->sample_baked(offset); |
| 1587 | } else { |
| 1588 | joints[j]->stiffness = settings[i]->stiffness; |
| 1589 | } |
| 1590 | |
| 1591 | if (settings[i]->drag_damping_curve.is_valid()) { |
| 1592 | joints[j]->drag = settings[i]->drag * settings[i]->drag_damping_curve->sample_baked(offset); |
| 1593 | } else { |
| 1594 | joints[j]->drag = settings[i]->drag; |
| 1595 | } |
| 1596 | |
| 1597 | if (settings[i]->gravity_damping_curve.is_valid()) { |
| 1598 | joints[j]->gravity = settings[i]->gravity * settings[i]->gravity_damping_curve->sample_baked(offset); |
| 1599 | } else { |
| 1600 | joints[j]->gravity = settings[i]->gravity; |
| 1601 | } |
| 1602 | |
| 1603 | joints[j]->gravity_direction = settings[i]->gravity_direction; |
| 1604 | joints[j]->rotation_axis = settings[i]->rotation_axis; |
| 1605 | joints[j]->rotation_axis_vector = settings[i]->rotation_axis_vector; |
| 1606 | } |
| 1607 | settings[i]->simulation_dirty = p_reset; |
| 1608 | settings[i]->joints_dirty = false; |
| 1609 | } |
| 1610 | joints_dirty = false; |
| 1611 | Skeleton3D *sk = get_skeleton(); |
| 1612 | if (sk) { |
| 1613 | _validate_rotation_axes(sk); |
| 1614 | } |
| 1615 | #ifdef TOOLS_ENABLED |
| 1616 | _make_gizmo_dirty(); |
| 1617 | #endif // TOOLS_ENABLED |
| 1618 | } |
| 1619 | |
| 1620 | void SpringBoneSimulator3D::_update_bone_axis(Skeleton3D *p_skeleton, SpringBone3DSetting *p_setting) { |
| 1621 | #ifdef TOOLS_ENABLED |
| 1622 | bool changed = false; |
| 1623 | #endif // TOOLS_ENABLED |
| 1624 | const LocalVector<SpringBone3DJointSetting *> &joints = p_setting->joints; |
| 1625 | int len = (int)joints.size() - 1; |
| 1626 | for (int j = 0; j < len; j++) { |
| 1627 | if (!joints[j]->verlet) { |
| 1628 | continue; |
| 1629 | } |
| 1630 | Vector3 axis = p_skeleton->get_bone_pose(joints[j + 1]->bone).origin; |
| 1631 | if (axis.is_zero_approx()) { |
| 1632 | continue; |
| 1633 | } |
| 1634 | // Less computing. |
| 1635 | #ifdef TOOLS_ENABLED |
| 1636 | if (!changed) { |
| 1637 | Vector3 old_v = joints[j]->verlet->forward_vector; |
| 1638 | joints[j]->verlet->forward_vector = snap_vector_to_plane(joints[j]->get_rotation_axis_vector(), axis.normalized()); |
| 1639 | changed = changed || !old_v.is_equal_approx(joints[j]->verlet->forward_vector); |
| 1640 | float old_l = joints[j]->verlet->length; |
| 1641 | joints[j]->verlet->length = axis.length(); |
| 1642 | changed = changed || !Math::is_equal_approx(old_l, joints[j]->verlet->length); |
| 1643 | } else { |
| 1644 | joints[j]->verlet->forward_vector = snap_vector_to_plane(joints[j]->get_rotation_axis_vector(), axis.normalized()); |
| 1645 | joints[j]->verlet->length = axis.length(); |
| 1646 | } |
| 1647 | #else |
| 1648 | joints[j]->verlet->forward_vector = snap_vector_to_plane(joints[j]->get_rotation_axis_vector(), axis.normalized()); |
| 1649 | joints[j]->verlet->length = axis.length(); |
| 1650 | #endif // TOOLS_ENABLED |
| 1651 | } |
| 1652 | if (p_setting->extend_end_bone && len >= 0) { |
| 1653 | if (joints[len]->verlet) { |
| 1654 | Vector3 axis = get_end_bone_axis(p_setting->end_bone, p_setting->end_bone_direction); |
| 1655 | if (!axis.is_zero_approx()) { |
| 1656 | joints[len]->verlet->forward_vector = snap_vector_to_plane(joints[len]->get_rotation_axis_vector(), axis.normalized()); |
| 1657 | joints[len]->verlet->length = p_setting->end_bone_length; |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | #ifdef TOOLS_ENABLED |
| 1662 | if (changed) { |
| 1663 | _make_gizmo_dirty(); |
| 1664 | } |
| 1665 | #endif // TOOLS_ENABLED |
| 1666 | } |
| 1667 | |
| 1668 | #ifdef TOOLS_ENABLED |
| 1669 | Vector3 SpringBoneSimulator3D::get_bone_vector(int p_index, int p_joint) const { |
| 1670 | Skeleton3D *skeleton = get_skeleton(); |
| 1671 | if (!skeleton) { |
| 1672 | return Vector3(); |
| 1673 | } |
| 1674 | ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3()); |
| 1675 | SpringBone3DSetting *setting = settings[p_index]; |
| 1676 | if (!setting) { |
| 1677 | return Vector3(); |
| 1678 | } |
| 1679 | const LocalVector<SpringBone3DJointSetting *> &joints = setting->joints; |
| 1680 | ERR_FAIL_INDEX_V(p_joint, (int)joints.size(), Vector3()); |
| 1681 | if (!joints[p_joint]->verlet) { |
| 1682 | if (p_joint == (int)joints.size() - 1) { |
| 1683 | return get_end_bone_axis(setting->end_bone, setting->end_bone_direction) * setting->end_bone_length; |
| 1684 | } |
| 1685 | return mutable_bone_axes ? skeleton->get_bone_pose(joints[p_joint + 1]->bone).origin : skeleton->get_bone_rest(joints[p_joint + 1]->bone).origin; |
| 1686 | } |
| 1687 | return joints[p_joint]->verlet->forward_vector * joints[p_joint]->verlet->length; |
| 1688 | } |
| 1689 | |
| 1690 | void SpringBoneSimulator3D::_make_gizmo_dirty() { |
| 1691 | if (gizmo_dirty) { |
| 1692 | return; |
| 1693 | } |
| 1694 | gizmo_dirty = true; |
| 1695 | callable_mp(this, &SpringBoneSimulator3D::_redraw_gizmo).call_deferred(); |
| 1696 | } |
| 1697 | |
| 1698 | void SpringBoneSimulator3D::_redraw_gizmo() { |
| 1699 | update_gizmos(); |
| 1700 | gizmo_dirty = false; |
| 1701 | } |
| 1702 | #endif |
| 1703 | |
| 1704 | void SpringBoneSimulator3D::_set_active(bool p_active) { |
| 1705 | if (p_active) { |
| 1706 | reset(); |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | void SpringBoneSimulator3D::_process_modification(double p_delta) { |
| 1711 | Skeleton3D *skeleton = get_skeleton(); |
| 1712 | if (!skeleton) { |
| 1713 | return; |
| 1714 | } |
| 1715 | _find_collisions(); |
| 1716 | _process_collisions(); |
| 1717 | |
| 1718 | #ifdef TOOLS_ENABLED |
| 1719 | if (saving) { |
| 1720 | return; // Collision position has been reset but we don't want to process simulating on saving. Abort. |
| 1721 | } |
| 1722 | #endif // TOOLS_ENABLED |
| 1723 | |
| 1724 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 1725 | _init_joints(skeleton, settings[i]); |
| 1726 | _process_joints(p_delta, skeleton, settings[i]->joints, get_valid_collision_instance_ids(i), settings[i]->cached_center, settings[i]->cached_inverted_center, settings[i]->cached_inverted_center.basis.get_rotation_quaternion()); |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | void SpringBoneSimulator3D::reset() { |
| 1731 | if (!is_inside_tree()) { |
| 1732 | return; |
| 1733 | } |
| 1734 | Skeleton3D *skeleton = get_skeleton(); |
| 1735 | if (!skeleton) { |
| 1736 | return; |
| 1737 | } |
| 1738 | _find_collisions(); |
| 1739 | _process_collisions(); |
| 1740 | for (uint32_t i = 0; i < settings.size(); i++) { |
| 1741 | _make_joints_dirty(i, true); |
| 1742 | _init_joints(skeleton, settings[i]); |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | void SpringBoneSimulator3D::_init_joints(Skeleton3D *p_skeleton, SpringBone3DSetting *setting) { |
| 1747 | if (setting->center_from == CENTER_FROM_WORLD_ORIGIN) { |
| 1748 | setting->cached_center = p_skeleton->get_global_transform_interpolated(); |
| 1749 | } else if (setting->center_from == CENTER_FROM_NODE) { |
| 1750 | if (setting->center_node == NodePath()) { |
| 1751 | setting->cached_center = Transform3D(); |
| 1752 | } else { |
| 1753 | Node3D *nd = Object::cast_to<Node3D>(get_node_or_null(setting->center_node)); |
| 1754 | if (!nd) { |
| 1755 | setting->cached_center = Transform3D(); |
| 1756 | } else { |
| 1757 | setting->cached_center = nd->get_global_transform_interpolated().affine_inverse() * p_skeleton->get_global_transform_interpolated(); |
| 1758 | } |
| 1759 | } |
| 1760 | } else { |
| 1761 | if (setting->center_bone >= 0) { |
| 1762 | setting->cached_center = p_skeleton->get_bone_global_pose(setting->center_bone); |
| 1763 | } else { |
| 1764 | setting->cached_center = Transform3D(); |
| 1765 | } |
| 1766 | } |
| 1767 | setting->cached_inverted_center = setting->cached_center.affine_inverse(); |
| 1768 | |
| 1769 | if (!setting->simulation_dirty) { |
| 1770 | if (mutable_bone_axes) { |
| 1771 | _update_bone_axis(p_skeleton, setting); |
| 1772 | } |
| 1773 | return; |
| 1774 | } |
| 1775 | for (uint32_t i = 0; i < setting->joints.size(); i++) { |
| 1776 | if (setting->joints[i]->verlet) { |
| 1777 | memdelete(setting->joints[i]->verlet); |
| 1778 | setting->joints[i]->verlet = nullptr; |
| 1779 | } |
| 1780 | if (i < setting->joints.size() - 1) { |
| 1781 | Vector3 axis = p_skeleton->get_bone_rest(setting->joints[i + 1]->bone).origin; |
| 1782 | if (axis.is_zero_approx()) { |
| 1783 | continue; |
| 1784 | } |
| 1785 | setting->joints[i]->verlet = memnew(SpringBone3DVerletInfo); |
| 1786 | setting->joints[i]->verlet->current_tail = setting->cached_center.xform(p_skeleton->get_bone_global_pose(setting->joints[i]->bone).xform(axis)); |
| 1787 | setting->joints[i]->verlet->prev_tail = setting->joints[i]->verlet->current_tail; |
| 1788 | setting->joints[i]->verlet->forward_vector = snap_vector_to_plane(setting->joints[i]->get_rotation_axis_vector(), axis.normalized()); |
| 1789 | setting->joints[i]->verlet->length = axis.length(); |
| 1790 | setting->joints[i]->verlet->current_rot = Quaternion(0, 0, 0, 1); |
| 1791 | } else if (setting->extend_end_bone && setting->end_bone_length > 0) { |
| 1792 | Vector3 axis = get_end_bone_axis(setting->end_bone, setting->end_bone_direction); |
| 1793 | if (axis.is_zero_approx()) { |
| 1794 | continue; |
| 1795 | } |
| 1796 | setting->joints[i]->verlet = memnew(SpringBone3DVerletInfo); |
| 1797 | setting->joints[i]->verlet->forward_vector = snap_vector_to_plane(setting->joints[i]->get_rotation_axis_vector(), axis.normalized()); |
| 1798 | setting->joints[i]->verlet->length = setting->end_bone_length; |
| 1799 | setting->joints[i]->verlet->current_tail = setting->cached_center.xform(p_skeleton->get_bone_global_pose(setting->joints[i]->bone).xform(axis * setting->end_bone_length)); |
| 1800 | setting->joints[i]->verlet->prev_tail = setting->joints[i]->verlet->current_tail; |
| 1801 | setting->joints[i]->verlet->current_rot = Quaternion(0, 0, 0, 1); |
| 1802 | } |
| 1803 | } |
| 1804 | if (mutable_bone_axes) { |
| 1805 | _update_bone_axis(p_skeleton, setting); |
| 1806 | #ifdef TOOLS_ENABLED |
| 1807 | } else { |
| 1808 | _make_gizmo_dirty(); |
| 1809 | #endif // TOOLS_ENABLED |
| 1810 | } |
| 1811 | setting->simulation_dirty = false; |
| 1812 | } |
| 1813 | |
| 1814 | void SpringBoneSimulator3D::_process_joints(double p_delta, Skeleton3D *p_skeleton, LocalVector<SpringBone3DJointSetting *> &p_joints, const LocalVector<ObjectID> &p_collisions, const Transform3D &p_center_transform, const Transform3D &p_inverted_center_transform, const Quaternion &p_inverted_center_rotation) { |
| 1815 | for (uint32_t i = 0; i < p_joints.size(); i++) { |
| 1816 | SpringBone3DVerletInfo *verlet = p_joints[i]->verlet; |
| 1817 | if (!verlet) { |
| 1818 | continue; // Means not extended end bone. |
| 1819 | } |
| 1820 | Transform3D current_global_pose = p_skeleton->get_bone_global_pose(p_joints[i]->bone); |
| 1821 | Transform3D current_world_pose = p_center_transform * current_global_pose; |
| 1822 | Quaternion current_rot = current_global_pose.basis.get_rotation_quaternion(); |
| 1823 | Vector3 current_origin = p_center_transform.xform(current_global_pose.origin); |
| 1824 | Vector3 external = p_inverted_center_rotation.xform((external_force + p_joints[i]->gravity_direction * p_joints[i]->gravity) * p_delta); |
| 1825 | |
| 1826 | // Integration of velocity by verlet. |
| 1827 | Vector3 next_tail = verlet->current_tail + |
| 1828 | (verlet->current_tail - verlet->prev_tail) * (1.0 - p_joints[i]->drag) + |
| 1829 | p_center_transform.basis.get_rotation_quaternion().xform(current_rot.xform(verlet->forward_vector * (p_joints[i]->stiffness * p_delta)) + external); |
| 1830 | // Snap to plane if axis locked. |
| 1831 | if (p_joints[i]->rotation_axis != ROTATION_AXIS_ALL) { |
| 1832 | next_tail = current_world_pose.origin + current_world_pose.basis.get_rotation_quaternion().xform(snap_vector_to_plane(p_joints[i]->get_rotation_axis_vector(), current_world_pose.basis.get_rotation_quaternion().xform_inv(next_tail - current_world_pose.origin))); |
| 1833 | } |
| 1834 | // Limit bone length. |
| 1835 | next_tail = limit_length(current_origin, next_tail, verlet->length); |
| 1836 | |
| 1837 | // Collision movement. |
| 1838 | for (uint32_t j = 0; j < p_collisions.size(); j++) { |
| 1839 | Object *obj = ObjectDB::get_instance(p_collisions[j]); |
| 1840 | if (!obj) { |
| 1841 | continue; |
| 1842 | } |
| 1843 | SpringBoneCollision3D *col = Object::cast_to<SpringBoneCollision3D>(obj); |
| 1844 | if (col) { |
| 1845 | // Collider movement should separate from the effect of the center. |
| 1846 | next_tail = col->collide(p_center_transform, p_joints[i]->radius, verlet->length, next_tail); |
| 1847 | // Snap to plane if axis locked. |
| 1848 | if (p_joints[i]->rotation_axis != ROTATION_AXIS_ALL) { |
| 1849 | next_tail = current_world_pose.origin + current_world_pose.basis.get_rotation_quaternion().xform(snap_vector_to_plane(p_joints[i]->get_rotation_axis_vector(), current_world_pose.basis.get_rotation_quaternion().xform_inv(next_tail - current_world_pose.origin))); |
| 1850 | } |
| 1851 | // Limit bone length. |
| 1852 | next_tail = limit_length(current_origin, next_tail, verlet->length); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | // Store current tails for next process. |
| 1857 | verlet->prev_tail = verlet->current_tail; |
| 1858 | verlet->current_tail = next_tail; |
| 1859 | |
| 1860 | // Convert position to rotation. |
| 1861 | Vector3 from = current_rot.xform(verlet->forward_vector); |
| 1862 | Vector3 to = p_inverted_center_transform.basis.xform(next_tail - current_origin); |
| 1863 | from.normalize(); |
| 1864 | to.normalize(); |
| 1865 | Quaternion from_to = get_from_to_rotation(from, to, verlet->current_rot); |
| 1866 | verlet->current_rot = from_to; |
| 1867 | |
| 1868 | // Apply rotation. |
| 1869 | from_to *= current_rot; |
| 1870 | from_to = get_local_pose_rotation(p_skeleton, p_joints[i]->bone, from_to); |
| 1871 | p_skeleton->set_bone_pose_rotation(p_joints[i]->bone, from_to); |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | SpringBoneSimulator3D::~SpringBoneSimulator3D() { |
| 1876 | clear_settings(); |
| 1877 | } |