| 1 | /**************************************************************************/ |
| 2 | /* skin_tool.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 "skin_tool.h" |
| 32 | |
| 33 | SkinNodeIndex SkinTool::_find_highest_node(Vector<Ref<GLTFNode>> &r_nodes, const Vector<GLTFNodeIndex> &p_subset) { |
| 34 | int highest = -1; |
| 35 | SkinNodeIndex best_node = -1; |
| 36 | |
| 37 | for (int i = 0; i < p_subset.size(); ++i) { |
| 38 | const SkinNodeIndex node_i = p_subset[i]; |
| 39 | const Ref<GLTFNode> node = r_nodes[node_i]; |
| 40 | |
| 41 | if (highest == -1 || node->height < highest) { |
| 42 | highest = node->height; |
| 43 | best_node = node_i; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return best_node; |
| 48 | } |
| 49 | |
| 50 | bool SkinTool::_capture_nodes_in_skin(const Vector<Ref<GLTFNode>> &nodes, Ref<GLTFSkin> p_skin, const SkinNodeIndex p_node_index) { |
| 51 | bool found_joint = false; |
| 52 | Ref<GLTFNode> current_node = nodes[p_node_index]; |
| 53 | |
| 54 | for (int i = 0; i < current_node->children.size(); ++i) { |
| 55 | found_joint |= _capture_nodes_in_skin(nodes, p_skin, current_node->children[i]); |
| 56 | } |
| 57 | |
| 58 | if (found_joint) { |
| 59 | // Mark it if we happen to find another skins joint... |
| 60 | if (current_node->joint && !p_skin->joints.has(p_node_index)) { |
| 61 | p_skin->joints.push_back(p_node_index); |
| 62 | } else if (!p_skin->non_joints.has(p_node_index)) { |
| 63 | p_skin->non_joints.push_back(p_node_index); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (p_skin->joints.find(p_node_index) > 0) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | void SkinTool::_capture_nodes_for_multirooted_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin) { |
| 75 | DisjointSet<SkinNodeIndex> disjoint_set; |
| 76 | |
| 77 | for (int i = 0; i < p_skin->joints.size(); ++i) { |
| 78 | const SkinNodeIndex node_index = p_skin->joints[i]; |
| 79 | const SkinNodeIndex parent = r_nodes[node_index]->parent; |
| 80 | disjoint_set.insert(node_index); |
| 81 | |
| 82 | if (p_skin->joints.has(parent)) { |
| 83 | disjoint_set.create_union(parent, node_index); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | Vector<SkinNodeIndex> roots; |
| 88 | disjoint_set.get_representatives(roots); |
| 89 | |
| 90 | if (roots.size() <= 1) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | int maxHeight = -1; |
| 95 | |
| 96 | // Determine the max height rooted tree |
| 97 | for (int i = 0; i < roots.size(); ++i) { |
| 98 | const SkinNodeIndex root = roots[i]; |
| 99 | |
| 100 | if (maxHeight == -1 || r_nodes[root]->height < maxHeight) { |
| 101 | maxHeight = r_nodes[root]->height; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Go up the tree till all of the multiple roots of the skin are at the same hierarchy level. |
| 106 | // This sucks, but 99% of all game engines (not just Godot) would have this same issue. |
| 107 | for (int i = 0; i < roots.size(); ++i) { |
| 108 | SkinNodeIndex current_node = roots[i]; |
| 109 | while (r_nodes[current_node]->height > maxHeight) { |
| 110 | SkinNodeIndex parent = r_nodes[current_node]->parent; |
| 111 | |
| 112 | if (r_nodes[parent]->joint && !p_skin->joints.has(parent)) { |
| 113 | p_skin->joints.push_back(parent); |
| 114 | } else if (!p_skin->non_joints.has(parent)) { |
| 115 | p_skin->non_joints.push_back(parent); |
| 116 | } |
| 117 | |
| 118 | current_node = parent; |
| 119 | } |
| 120 | |
| 121 | // replace the roots |
| 122 | roots.write[i] = current_node; |
| 123 | } |
| 124 | |
| 125 | // Climb up the tree until they all have the same parent |
| 126 | bool all_same; |
| 127 | |
| 128 | do { |
| 129 | all_same = true; |
| 130 | const SkinNodeIndex first_parent = r_nodes[roots[0]]->parent; |
| 131 | |
| 132 | for (int i = 1; i < roots.size(); ++i) { |
| 133 | all_same &= (first_parent == r_nodes[roots[i]]->parent); |
| 134 | } |
| 135 | |
| 136 | if (!all_same) { |
| 137 | for (int i = 0; i < roots.size(); ++i) { |
| 138 | const SkinNodeIndex current_node = roots[i]; |
| 139 | const SkinNodeIndex parent = r_nodes[current_node]->parent; |
| 140 | |
| 141 | if (r_nodes[parent]->joint && !p_skin->joints.has(parent)) { |
| 142 | p_skin->joints.push_back(parent); |
| 143 | } else if (!p_skin->non_joints.has(parent)) { |
| 144 | p_skin->non_joints.push_back(parent); |
| 145 | } |
| 146 | |
| 147 | roots.write[i] = parent; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | } while (!all_same); |
| 152 | } |
| 153 | |
| 154 | Error SkinTool::_expand_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin) { |
| 155 | _capture_nodes_for_multirooted_skin(r_nodes, p_skin); |
| 156 | |
| 157 | // Grab all nodes that lay in between skin joints/nodes |
| 158 | DisjointSet<GLTFNodeIndex> disjoint_set; |
| 159 | |
| 160 | Vector<SkinNodeIndex> all_skin_nodes; |
| 161 | all_skin_nodes.append_array(p_skin->joints); |
| 162 | all_skin_nodes.append_array(p_skin->non_joints); |
| 163 | |
| 164 | for (int i = 0; i < all_skin_nodes.size(); ++i) { |
| 165 | const SkinNodeIndex node_index = all_skin_nodes[i]; |
| 166 | const SkinNodeIndex parent = r_nodes[node_index]->parent; |
| 167 | disjoint_set.insert(node_index); |
| 168 | |
| 169 | if (all_skin_nodes.has(parent)) { |
| 170 | disjoint_set.create_union(parent, node_index); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | Vector<SkinNodeIndex> out_owners; |
| 175 | disjoint_set.get_representatives(out_owners); |
| 176 | |
| 177 | Vector<SkinNodeIndex> out_roots; |
| 178 | |
| 179 | for (int i = 0; i < out_owners.size(); ++i) { |
| 180 | Vector<SkinNodeIndex> set; |
| 181 | disjoint_set.get_members(set, out_owners[i]); |
| 182 | |
| 183 | const SkinNodeIndex root = _find_highest_node(r_nodes, set); |
| 184 | ERR_FAIL_COND_V(root < 0, FAILED); |
| 185 | out_roots.push_back(root); |
| 186 | } |
| 187 | |
| 188 | out_roots.sort(); |
| 189 | |
| 190 | for (int i = 0; i < out_roots.size(); ++i) { |
| 191 | _capture_nodes_in_skin(r_nodes, p_skin, out_roots[i]); |
| 192 | } |
| 193 | |
| 194 | p_skin->roots = out_roots; |
| 195 | |
| 196 | return OK; |
| 197 | } |
| 198 | |
| 199 | Error SkinTool::_verify_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin) { |
| 200 | // This may seem duplicated from expand_skins, but this is really a sanity check! (so it kinda is) |
| 201 | // In case additional interpolating logic is added to the skins, this will help ensure that you |
| 202 | // do not cause it to self implode into a fiery blaze |
| 203 | |
| 204 | // We are going to re-calculate the root nodes and compare them to the ones saved in the skin, |
| 205 | // then ensure the multiple trees (if they exist) are on the same sublevel |
| 206 | |
| 207 | // Grab all nodes that lay in between skin joints/nodes |
| 208 | DisjointSet<GLTFNodeIndex> disjoint_set; |
| 209 | |
| 210 | Vector<SkinNodeIndex> all_skin_nodes; |
| 211 | all_skin_nodes.append_array(p_skin->joints); |
| 212 | all_skin_nodes.append_array(p_skin->non_joints); |
| 213 | |
| 214 | for (int i = 0; i < all_skin_nodes.size(); ++i) { |
| 215 | const SkinNodeIndex node_index = all_skin_nodes[i]; |
| 216 | const SkinNodeIndex parent = r_nodes[node_index]->parent; |
| 217 | disjoint_set.insert(node_index); |
| 218 | |
| 219 | if (all_skin_nodes.has(parent)) { |
| 220 | disjoint_set.create_union(parent, node_index); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | Vector<SkinNodeIndex> out_owners; |
| 225 | disjoint_set.get_representatives(out_owners); |
| 226 | |
| 227 | Vector<SkinNodeIndex> out_roots; |
| 228 | |
| 229 | for (int i = 0; i < out_owners.size(); ++i) { |
| 230 | Vector<SkinNodeIndex> set; |
| 231 | disjoint_set.get_members(set, out_owners[i]); |
| 232 | |
| 233 | const SkinNodeIndex root = _find_highest_node(r_nodes, set); |
| 234 | ERR_FAIL_COND_V(root < 0, FAILED); |
| 235 | out_roots.push_back(root); |
| 236 | } |
| 237 | |
| 238 | out_roots.sort(); |
| 239 | |
| 240 | ERR_FAIL_COND_V(out_roots.is_empty(), FAILED); |
| 241 | |
| 242 | // Make sure the roots are the exact same (they better be) |
| 243 | ERR_FAIL_COND_V(out_roots.size() != p_skin->roots.size(), FAILED); |
| 244 | for (int i = 0; i < out_roots.size(); ++i) { |
| 245 | ERR_FAIL_COND_V(out_roots[i] != p_skin->roots[i], FAILED); |
| 246 | } |
| 247 | |
| 248 | // Single rooted skin? Perfectly ok! |
| 249 | if (out_roots.size() == 1) { |
| 250 | return OK; |
| 251 | } |
| 252 | |
| 253 | // Make sure all parents of a multi-rooted skin are the SAME |
| 254 | const SkinNodeIndex parent = r_nodes[out_roots[0]]->parent; |
| 255 | for (int i = 1; i < out_roots.size(); ++i) { |
| 256 | if (r_nodes[out_roots[i]]->parent != parent) { |
| 257 | return FAILED; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return OK; |
| 262 | } |
| 263 | |
| 264 | void SkinTool::_recurse_children( |
| 265 | Vector<Ref<GLTFNode>> &nodes, |
| 266 | const SkinNodeIndex p_node_index, |
| 267 | RBSet<GLTFNodeIndex> &p_all_skin_nodes, |
| 268 | HashSet<GLTFNodeIndex> &p_child_visited_set) { |
| 269 | if (p_child_visited_set.has(p_node_index)) { |
| 270 | return; |
| 271 | } |
| 272 | p_child_visited_set.insert(p_node_index); |
| 273 | |
| 274 | Ref<GLTFNode> current_node = nodes[p_node_index]; |
| 275 | for (int i = 0; i < current_node->children.size(); ++i) { |
| 276 | _recurse_children(nodes, current_node->children[i], p_all_skin_nodes, p_child_visited_set); |
| 277 | } |
| 278 | |
| 279 | // Continue to use 'current_node' for clarity and direct access. |
| 280 | if (current_node->skin < 0 || current_node->mesh < 0 || !current_node->children.is_empty()) { |
| 281 | p_all_skin_nodes.insert(p_node_index); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void SkinTool::_check_if_parent_needs_to_become_joint(const Vector<Ref<GLTFNode>> &p_all_nodes, const Vector<GLTFNodeIndex> &p_skeleton_node_indices, const Ref<GLTFNode> &p_gltf_node, Vector<GLTFNodeIndex> &r_non_joint_indices) { |
| 286 | const GLTFNodeIndex parent_index = p_gltf_node->parent; |
| 287 | if (parent_index >= 0) { |
| 288 | const Ref<GLTFNode> &parent = p_all_nodes[parent_index]; |
| 289 | if (!parent->joint && p_skeleton_node_indices.has(parent_index) && !r_non_joint_indices.has(parent_index)) { |
| 290 | _check_if_parent_needs_to_become_joint(p_all_nodes, p_skeleton_node_indices, parent, r_non_joint_indices); |
| 291 | r_non_joint_indices.push_back(parent_index); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | Error SkinTool::_determine_skeletons( |
| 297 | Vector<Ref<GLTFSkin>> &skins, |
| 298 | Vector<Ref<GLTFNode>> &nodes, |
| 299 | Vector<Ref<GLTFSkeleton>> &skeletons, |
| 300 | const Vector<GLTFNodeIndex> &p_single_skeleton_roots, |
| 301 | bool p_turn_non_joint_descendants_into_bones) { |
| 302 | if (!p_single_skeleton_roots.is_empty()) { |
| 303 | Ref<GLTFSkin> skin; |
| 304 | skin.instantiate(); |
| 305 | skin->set_name("godot_single_skeleton_root"); |
| 306 | for (GLTFNodeIndex i = 0; i < p_single_skeleton_roots.size(); i++) { |
| 307 | skin->joints.push_back(p_single_skeleton_roots[i]); |
| 308 | } |
| 309 | skins.push_back(skin); |
| 310 | } |
| 311 | |
| 312 | // Using a disjoint set, we are going to potentially combine all skins that are actually branches |
| 313 | // of a main skeleton, or treat skins defining the same set of nodes as ONE skeleton. |
| 314 | // This is another unclear issue caused by the current glTF specification. |
| 315 | |
| 316 | DisjointSet<GLTFNodeIndex> skeleton_sets; |
| 317 | |
| 318 | for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) { |
| 319 | const Ref<GLTFSkin> skin = skins[skin_i]; |
| 320 | ERR_CONTINUE(skin.is_null()); |
| 321 | |
| 322 | HashSet<GLTFNodeIndex> child_visited_set; |
| 323 | RBSet<GLTFNodeIndex> all_skin_nodes; |
| 324 | for (int i = 0; i < skin->joints.size(); ++i) { |
| 325 | all_skin_nodes.insert(skin->joints[i]); |
| 326 | SkinTool::_recurse_children(nodes, skin->joints[i], all_skin_nodes, child_visited_set); |
| 327 | } |
| 328 | for (int i = 0; i < skin->non_joints.size(); ++i) { |
| 329 | all_skin_nodes.insert(skin->non_joints[i]); |
| 330 | SkinTool::_recurse_children(nodes, skin->non_joints[i], all_skin_nodes, child_visited_set); |
| 331 | } |
| 332 | for (GLTFNodeIndex node_index : all_skin_nodes) { |
| 333 | const GLTFNodeIndex parent = nodes[node_index]->parent; |
| 334 | skeleton_sets.insert(node_index); |
| 335 | |
| 336 | if (all_skin_nodes.has(parent)) { |
| 337 | skeleton_sets.create_union(parent, node_index); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // We are going to connect the separate skin subtrees in each skin together |
| 342 | // so that the final roots are entire sets of valid skin trees |
| 343 | for (int i = 1; i < skin->roots.size(); ++i) { |
| 344 | skeleton_sets.create_union(skin->roots[0], skin->roots[i]); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | { // attempt to joint all touching subsets (siblings/parent are part of another skin) |
| 349 | Vector<SkinNodeIndex> groups_representatives; |
| 350 | skeleton_sets.get_representatives(groups_representatives); |
| 351 | |
| 352 | Vector<SkinNodeIndex> highest_group_members; |
| 353 | Vector<Vector<SkinNodeIndex>> groups; |
| 354 | for (int i = 0; i < groups_representatives.size(); ++i) { |
| 355 | Vector<SkinNodeIndex> group; |
| 356 | skeleton_sets.get_members(group, groups_representatives[i]); |
| 357 | highest_group_members.push_back(SkinTool::_find_highest_node(nodes, group)); |
| 358 | groups.push_back(group); |
| 359 | } |
| 360 | |
| 361 | for (int i = 0; i < highest_group_members.size(); ++i) { |
| 362 | const SkinNodeIndex node_i = highest_group_members[i]; |
| 363 | |
| 364 | // Attach any siblings together (this needs to be done n^2/2 times) |
| 365 | for (int j = i + 1; j < highest_group_members.size(); ++j) { |
| 366 | const SkinNodeIndex node_j = highest_group_members[j]; |
| 367 | |
| 368 | // Even if they are siblings under the root! :) |
| 369 | if (nodes[node_i]->parent == nodes[node_j]->parent) { |
| 370 | skeleton_sets.create_union(node_i, node_j); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | // Attach any parenting going on together (we need to do this n^2 times) |
| 375 | const SkinNodeIndex node_i_parent = nodes[node_i]->parent; |
| 376 | if (node_i_parent >= 0) { |
| 377 | for (int j = 0; j < groups.size() && i != j; ++j) { |
| 378 | const Vector<SkinNodeIndex> &group = groups[j]; |
| 379 | |
| 380 | if (group.has(node_i_parent)) { |
| 381 | const SkinNodeIndex node_j = highest_group_members[j]; |
| 382 | skeleton_sets.create_union(node_i, node_j); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // At this point, the skeleton groups should be finalized |
| 390 | Vector<SkinNodeIndex> skeleton_owners; |
| 391 | skeleton_sets.get_representatives(skeleton_owners); |
| 392 | |
| 393 | // Mark all the skins actual skeletons, after we have merged them |
| 394 | for (SkinSkeletonIndex skel_i = 0; skel_i < skeleton_owners.size(); ++skel_i) { |
| 395 | const SkinNodeIndex skeleton_owner = skeleton_owners[skel_i]; |
| 396 | Ref<GLTFSkeleton> skeleton; |
| 397 | skeleton.instantiate(); |
| 398 | |
| 399 | Vector<SkinNodeIndex> skeleton_nodes; |
| 400 | skeleton_sets.get_members(skeleton_nodes, skeleton_owner); |
| 401 | |
| 402 | for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) { |
| 403 | Ref<GLTFSkin> skin = skins.write[skin_i]; |
| 404 | |
| 405 | // If any of the skeletons nodes exist in a skin, that skin now maps to the skeleton |
| 406 | for (int i = 0; i < skeleton_nodes.size(); ++i) { |
| 407 | SkinNodeIndex skel_node_i = skeleton_nodes[i]; |
| 408 | if (skin->joints.has(skel_node_i) || skin->non_joints.has(skel_node_i)) { |
| 409 | skin->skeleton = skel_i; |
| 410 | continue; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | // The nodes placed into `non_joints` will be passed to `_reparent_non_joint_skeleton_subtrees` |
| 415 | // which will add them to the skeleton and set `node->joint` to true. |
| 416 | Vector<SkinNodeIndex> non_joints; |
| 417 | for (int i = 0; i < skeleton_nodes.size(); ++i) { |
| 418 | const SkinNodeIndex node_i = skeleton_nodes[i]; |
| 419 | Ref<GLTFNode> node = nodes[node_i]; |
| 420 | if (node->joint) { |
| 421 | if (!p_turn_non_joint_descendants_into_bones) { |
| 422 | // If a joint node has non-joint parents, we need to make them joints as well. |
| 423 | // For example, if A/B/C/D, and A/B and D are joints, then we need to make C a joint as well. |
| 424 | // This is required to handle the "skinD" example in `Animation_Skin_09.gltf` from the glTF-Asset-Generator: |
| 425 | // https://github.com/KhronosGroup/glTF-Asset-Generator/blob/master/Output/Positive/Animation_Skin |
| 426 | _check_if_parent_needs_to_become_joint(nodes, skeleton_nodes, node, non_joints); |
| 427 | } |
| 428 | skeleton->joints.push_back(node_i); |
| 429 | } else if (p_turn_non_joint_descendants_into_bones) { |
| 430 | non_joints.push_back(node_i); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | skeletons.push_back(skeleton); |
| 435 | |
| 436 | SkinTool::_reparent_non_joint_skeleton_subtrees(nodes, skeletons.write[skel_i], non_joints); |
| 437 | } |
| 438 | |
| 439 | for (SkinSkeletonIndex skel_i = 0; skel_i < skeletons.size(); ++skel_i) { |
| 440 | Ref<GLTFSkeleton> skeleton = skeletons.write[skel_i]; |
| 441 | |
| 442 | for (int i = 0; i < skeleton->joints.size(); ++i) { |
| 443 | const SkinNodeIndex node_i = skeleton->joints[i]; |
| 444 | Ref<GLTFNode> node = nodes[node_i]; |
| 445 | |
| 446 | ERR_FAIL_COND_V(!node->joint, ERR_PARSE_ERROR); |
| 447 | ERR_FAIL_COND_V(node->skeleton >= 0, ERR_PARSE_ERROR); |
| 448 | node->skeleton = skel_i; |
| 449 | } |
| 450 | |
| 451 | ERR_FAIL_COND_V(SkinTool::_determine_skeleton_roots(nodes, skeletons, skel_i), ERR_PARSE_ERROR); |
| 452 | } |
| 453 | |
| 454 | return OK; |
| 455 | } |
| 456 | |
| 457 | Error SkinTool::_reparent_non_joint_skeleton_subtrees( |
| 458 | Vector<Ref<GLTFNode>> &nodes, |
| 459 | Ref<GLTFSkeleton> p_skeleton, |
| 460 | const Vector<SkinNodeIndex> &p_non_joints) { |
| 461 | DisjointSet<GLTFNodeIndex> subtree_set; |
| 462 | |
| 463 | // Populate the disjoint set with ONLY non joints that are in the skeleton hierarchy (non_joints vector) |
| 464 | // This way we can find any joints that lie in between joints, as the current glTF specification |
| 465 | // mentions nothing about non-joints being in between joints of the same skin. Hopefully one day we |
| 466 | // can remove this code. |
| 467 | |
| 468 | // skinD depicted here explains this issue: |
| 469 | // https://github.com/KhronosGroup/glTF-Asset-Generator/blob/master/Output/Positive/Animation_Skin |
| 470 | |
| 471 | for (int i = 0; i < p_non_joints.size(); ++i) { |
| 472 | const SkinNodeIndex node_i = p_non_joints[i]; |
| 473 | |
| 474 | subtree_set.insert(node_i); |
| 475 | |
| 476 | const SkinNodeIndex parent_i = nodes[node_i]->parent; |
| 477 | if (parent_i >= 0 && p_non_joints.has(parent_i) && !nodes[parent_i]->joint) { |
| 478 | subtree_set.create_union(parent_i, node_i); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // Find all the non joint subtrees and re-parent them to a new "fake" joint |
| 483 | |
| 484 | Vector<SkinNodeIndex> non_joint_subtree_roots; |
| 485 | subtree_set.get_representatives(non_joint_subtree_roots); |
| 486 | |
| 487 | for (int root_i = 0; root_i < non_joint_subtree_roots.size(); ++root_i) { |
| 488 | const SkinNodeIndex subtree_root = non_joint_subtree_roots[root_i]; |
| 489 | |
| 490 | Vector<SkinNodeIndex> subtree_nodes; |
| 491 | subtree_set.get_members(subtree_nodes, subtree_root); |
| 492 | |
| 493 | for (int subtree_i = 0; subtree_i < subtree_nodes.size(); ++subtree_i) { |
| 494 | Ref<GLTFNode> node = nodes[subtree_nodes[subtree_i]]; |
| 495 | node->joint = true; |
| 496 | // Add the joint to the skeletons joints |
| 497 | p_skeleton->joints.push_back(subtree_nodes[subtree_i]); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | return OK; |
| 502 | } |
| 503 | |
| 504 | Error SkinTool::_determine_skeleton_roots( |
| 505 | Vector<Ref<GLTFNode>> &nodes, |
| 506 | Vector<Ref<GLTFSkeleton>> &skeletons, |
| 507 | const SkinSkeletonIndex p_skel_i) { |
| 508 | DisjointSet<GLTFNodeIndex> disjoint_set; |
| 509 | |
| 510 | for (SkinNodeIndex i = 0; i < nodes.size(); ++i) { |
| 511 | const Ref<GLTFNode> node = nodes[i]; |
| 512 | |
| 513 | if (node->skeleton != p_skel_i) { |
| 514 | continue; |
| 515 | } |
| 516 | |
| 517 | disjoint_set.insert(i); |
| 518 | |
| 519 | if (node->parent >= 0 && nodes[node->parent]->skeleton == p_skel_i) { |
| 520 | disjoint_set.create_union(node->parent, i); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | Ref<GLTFSkeleton> skeleton = skeletons.write[p_skel_i]; |
| 525 | |
| 526 | Vector<SkinNodeIndex> representatives; |
| 527 | disjoint_set.get_representatives(representatives); |
| 528 | |
| 529 | Vector<SkinNodeIndex> roots; |
| 530 | |
| 531 | for (int i = 0; i < representatives.size(); ++i) { |
| 532 | Vector<SkinNodeIndex> set; |
| 533 | disjoint_set.get_members(set, representatives[i]); |
| 534 | const SkinNodeIndex root = _find_highest_node(nodes, set); |
| 535 | ERR_FAIL_COND_V(root < 0, FAILED); |
| 536 | roots.push_back(root); |
| 537 | } |
| 538 | |
| 539 | roots.sort(); |
| 540 | |
| 541 | skeleton->roots = roots; |
| 542 | |
| 543 | if (roots.is_empty()) { |
| 544 | return FAILED; |
| 545 | } else if (roots.size() == 1) { |
| 546 | return OK; |
| 547 | } |
| 548 | |
| 549 | // Check that the subtrees have the same parent root |
| 550 | const SkinNodeIndex parent = nodes[roots[0]]->parent; |
| 551 | for (int i = 1; i < roots.size(); ++i) { |
| 552 | if (nodes[roots[i]]->parent != parent) { |
| 553 | return FAILED; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return OK; |
| 558 | } |
| 559 | |
| 560 | Error SkinTool::_create_skeletons( |
| 561 | HashSet<String> &unique_names, |
| 562 | Vector<Ref<GLTFSkin>> &skins, |
| 563 | Vector<Ref<GLTFNode>> &nodes, |
| 564 | HashMap<ObjectID, GLTFSkeletonIndex> &skeleton3d_to_gltf_skeleton, |
| 565 | Vector<Ref<GLTFSkeleton>> &skeletons, |
| 566 | HashMap<GLTFNodeIndex, Node *> &scene_nodes, |
| 567 | int p_naming_version) { |
| 568 | // This is the syntax to duplicate a Godot HashSet. |
| 569 | HashSet<String> unique_node_names(unique_names); |
| 570 | for (SkinSkeletonIndex skel_i = 0; skel_i < skeletons.size(); ++skel_i) { |
| 571 | Ref<GLTFSkeleton> gltf_skeleton = skeletons.write[skel_i]; |
| 572 | HashSet<String> skel_unique_names(unique_node_names); |
| 573 | |
| 574 | Skeleton3D *skeleton = memnew(Skeleton3D); |
| 575 | gltf_skeleton->godot_skeleton = skeleton; |
| 576 | skeleton3d_to_gltf_skeleton[skeleton->get_instance_id()] = skel_i; |
| 577 | |
| 578 | // Make a unique name, no gltf node represents this skeleton |
| 579 | skeleton->set_name("Skeleton3D"); |
| 580 | |
| 581 | List<GLTFNodeIndex> bones; |
| 582 | |
| 583 | for (int i = 0; i < gltf_skeleton->roots.size(); ++i) { |
| 584 | bones.push_back(gltf_skeleton->roots[i]); |
| 585 | } |
| 586 | |
| 587 | // Make the skeleton creation deterministic by going through the roots in |
| 588 | // a sorted order, and DEPTH FIRST |
| 589 | bones.sort(); |
| 590 | |
| 591 | while (!bones.is_empty()) { |
| 592 | const SkinNodeIndex node_i = bones.front()->get(); |
| 593 | bones.pop_front(); |
| 594 | |
| 595 | Ref<GLTFNode> node = nodes[node_i]; |
| 596 | ERR_FAIL_COND_V(node->skeleton != skel_i, FAILED); |
| 597 | |
| 598 | { // Add all child nodes to the stack (deterministically) |
| 599 | Vector<SkinNodeIndex> child_nodes; |
| 600 | for (int i = 0; i < node->children.size(); ++i) { |
| 601 | const SkinNodeIndex child_i = node->children[i]; |
| 602 | if (nodes[child_i]->skeleton == skel_i) { |
| 603 | child_nodes.push_back(child_i); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // Depth first insertion |
| 608 | child_nodes.sort(); |
| 609 | for (int i = child_nodes.size() - 1; i >= 0; --i) { |
| 610 | bones.push_front(child_nodes[i]); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | const int bone_index = skeleton->get_bone_count(); |
| 615 | |
| 616 | if (node->get_name().is_empty()) { |
| 617 | node->set_name("bone"); |
| 618 | } |
| 619 | |
| 620 | if (p_naming_version < 2) { |
| 621 | node->set_name(_gen_unique_bone_name(unique_names, node->get_name())); |
| 622 | } else { |
| 623 | // Make sure the bone name is unique in the skeleton and unique compared |
| 624 | // to scene nodes, but bone names may be duplicated between skeletons. |
| 625 | // Example: Two skeletons with a "Head" bone should not have one become "Head_2". |
| 626 | const String unique_bone_name = _gen_unique_bone_name(skel_unique_names, node->get_name()); |
| 627 | unique_names.insert(unique_bone_name); |
| 628 | node->set_name(unique_bone_name); |
| 629 | } |
| 630 | |
| 631 | skeleton->add_bone(node->get_name()); |
| 632 | Transform3D rest_transform = node->get_additional_data("GODOT_rest_transform"); |
| 633 | skeleton->set_bone_rest(bone_index, rest_transform); |
| 634 | skeleton->set_bone_pose_position(bone_index, node->transform.origin); |
| 635 | skeleton->set_bone_pose_rotation(bone_index, node->transform.basis.get_rotation_quaternion()); |
| 636 | skeleton->set_bone_pose_scale(bone_index, node->transform.basis.get_scale()); |
| 637 | |
| 638 | // Store bone-level GLTF extras in skeleton per bone meta. |
| 639 | if (node->has_meta("extras")) { |
| 640 | skeleton->set_bone_meta(bone_index, "extras", node->get_meta("extras")); |
| 641 | } |
| 642 | |
| 643 | if (node->parent >= 0 && nodes[node->parent]->skeleton == skel_i) { |
| 644 | const int bone_parent = skeleton->find_bone(nodes[node->parent]->get_name()); |
| 645 | ERR_FAIL_COND_V(bone_parent < 0, FAILED); |
| 646 | skeleton->set_bone_parent(bone_index, skeleton->find_bone(nodes[node->parent]->get_name())); |
| 647 | } |
| 648 | |
| 649 | scene_nodes.insert(node_i, skeleton); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | ERR_FAIL_COND_V(_map_skin_joints_indices_to_skeleton_bone_indices(skins, skeletons, nodes), ERR_PARSE_ERROR); |
| 654 | |
| 655 | return OK; |
| 656 | } |
| 657 | |
| 658 | Error SkinTool::_map_skin_joints_indices_to_skeleton_bone_indices( |
| 659 | Vector<Ref<GLTFSkin>> &skins, |
| 660 | Vector<Ref<GLTFSkeleton>> &skeletons, |
| 661 | Vector<Ref<GLTFNode>> &nodes) { |
| 662 | for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) { |
| 663 | Ref<GLTFSkin> skin = skins.write[skin_i]; |
| 664 | ERR_CONTINUE(skin.is_null()); |
| 665 | |
| 666 | Ref<GLTFSkeleton> skeleton = skeletons[skin->skeleton]; |
| 667 | |
| 668 | for (int joint_index = 0; joint_index < skin->joints_original.size(); ++joint_index) { |
| 669 | const SkinNodeIndex node_i = skin->joints_original[joint_index]; |
| 670 | const Ref<GLTFNode> node = nodes[node_i]; |
| 671 | |
| 672 | const int bone_index = skeleton->godot_skeleton->find_bone(node->get_name()); |
| 673 | ERR_FAIL_COND_V(bone_index < 0, FAILED); |
| 674 | |
| 675 | skin->joint_i_to_bone_i.insert(joint_index, bone_index); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | return OK; |
| 680 | } |
| 681 | |
| 682 | Error SkinTool::_create_skins(Vector<Ref<GLTFSkin>> &skins, Vector<Ref<GLTFNode>> &nodes, bool use_named_skin_binds, HashSet<String> &unique_names) { |
| 683 | for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) { |
| 684 | Ref<GLTFSkin> gltf_skin = skins.write[skin_i]; |
| 685 | ERR_CONTINUE(gltf_skin.is_null()); |
| 686 | |
| 687 | Ref<Skin> skin; |
| 688 | skin.instantiate(); |
| 689 | |
| 690 | // Some skins don't have IBM's! What absolute monsters! |
| 691 | const bool has_ibms = !gltf_skin->inverse_binds.is_empty(); |
| 692 | |
| 693 | for (int joint_i = 0; joint_i < gltf_skin->joints_original.size(); ++joint_i) { |
| 694 | SkinNodeIndex node = gltf_skin->joints_original[joint_i]; |
| 695 | String bone_name = nodes[node]->get_name(); |
| 696 | |
| 697 | Transform3D xform; |
| 698 | if (has_ibms) { |
| 699 | xform = gltf_skin->inverse_binds[joint_i]; |
| 700 | } |
| 701 | |
| 702 | if (use_named_skin_binds) { |
| 703 | skin->add_named_bind(bone_name, xform); |
| 704 | } else { |
| 705 | int32_t bone_i = gltf_skin->joint_i_to_bone_i[joint_i]; |
| 706 | skin->add_bind(bone_i, xform); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | gltf_skin->godot_skin = skin; |
| 711 | } |
| 712 | |
| 713 | // Purge the duplicates! |
| 714 | _remove_duplicate_skins(skins); |
| 715 | |
| 716 | // Create unique names now, after removing duplicates |
| 717 | for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) { |
| 718 | ERR_CONTINUE(skins.get(skin_i).is_null()); |
| 719 | Ref<Skin> skin = skins.write[skin_i]->godot_skin; |
| 720 | ERR_CONTINUE(skin.is_null()); |
| 721 | if (skin->get_name().is_empty()) { |
| 722 | // Make a unique name, no node represents this skin |
| 723 | skin->set_name(_gen_unique_name(unique_names, "Skin")); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | return OK; |
| 728 | } |
| 729 | |
| 730 | // FIXME: Duplicated from FBXDocument, very similar code in GLTFDocument too, |
| 731 | // and even below in this class for bone names. |
| 732 | String SkinTool::_gen_unique_name(HashSet<String> &unique_names, const String &p_name) { |
| 733 | const String s_name = p_name.validate_node_name(); |
| 734 | |
| 735 | String u_name; |
| 736 | int index = 1; |
| 737 | while (true) { |
| 738 | u_name = s_name; |
| 739 | |
| 740 | if (index > 1) { |
| 741 | u_name += itos(index); |
| 742 | } |
| 743 | if (!unique_names.has(u_name)) { |
| 744 | break; |
| 745 | } |
| 746 | index++; |
| 747 | } |
| 748 | |
| 749 | unique_names.insert(u_name); |
| 750 | |
| 751 | return u_name; |
| 752 | } |
| 753 | |
| 754 | bool SkinTool::_skins_are_same(const Ref<Skin> p_skin_a, const Ref<Skin> p_skin_b) { |
| 755 | if (p_skin_a->get_bind_count() != p_skin_b->get_bind_count()) { |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | for (int i = 0; i < p_skin_a->get_bind_count(); ++i) { |
| 760 | if (p_skin_a->get_bind_bone(i) != p_skin_b->get_bind_bone(i)) { |
| 761 | return false; |
| 762 | } |
| 763 | if (p_skin_a->get_bind_name(i) != p_skin_b->get_bind_name(i)) { |
| 764 | return false; |
| 765 | } |
| 766 | |
| 767 | Transform3D a_xform = p_skin_a->get_bind_pose(i); |
| 768 | Transform3D b_xform = p_skin_b->get_bind_pose(i); |
| 769 | |
| 770 | if (a_xform != b_xform) { |
| 771 | return false; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | void SkinTool::_remove_duplicate_skins(Vector<Ref<GLTFSkin>> &r_skins) { |
| 779 | for (int i = 0; i < r_skins.size(); ++i) { |
| 780 | for (int j = i + 1; j < r_skins.size(); ++j) { |
| 781 | const Ref<Skin> skin_i = r_skins[i]->godot_skin; |
| 782 | const Ref<Skin> skin_j = r_skins[j]->godot_skin; |
| 783 | |
| 784 | if (_skins_are_same(skin_i, skin_j)) { |
| 785 | // replace it and delete the old |
| 786 | r_skins.write[j]->godot_skin = skin_i; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | String SkinTool::_gen_unique_bone_name(HashSet<String> &r_unique_names, const String &p_name) { |
| 793 | String s_name = _sanitize_bone_name(p_name); |
| 794 | if (s_name.is_empty()) { |
| 795 | s_name = "bone"; |
| 796 | } |
| 797 | String u_name; |
| 798 | int index = 1; |
| 799 | while (true) { |
| 800 | u_name = s_name; |
| 801 | |
| 802 | if (index > 1) { |
| 803 | u_name += "_" + itos(index); |
| 804 | } |
| 805 | if (!r_unique_names.has(u_name)) { |
| 806 | break; |
| 807 | } |
| 808 | index++; |
| 809 | } |
| 810 | |
| 811 | r_unique_names.insert(u_name); |
| 812 | |
| 813 | return u_name; |
| 814 | } |
| 815 | |
| 816 | Error SkinTool::_asset_parse_skins( |
| 817 | const Vector<SkinNodeIndex> &input_skin_indices, |
| 818 | const Vector<Ref<GLTFSkin>> &input_skins, |
| 819 | const Vector<Ref<GLTFNode>> &input_nodes, |
| 820 | Vector<SkinNodeIndex> &output_skin_indices, |
| 821 | Vector<Ref<GLTFSkin>> &output_skins, |
| 822 | HashMap<GLTFNodeIndex, bool> &joint_mapping) { |
| 823 | output_skin_indices.clear(); |
| 824 | output_skins.clear(); |
| 825 | joint_mapping.clear(); |
| 826 | |
| 827 | for (int i = 0; i < input_skin_indices.size(); ++i) { |
| 828 | SkinNodeIndex skin_index = input_skin_indices[i]; |
| 829 | if (skin_index >= 0 && skin_index < input_skins.size()) { |
| 830 | output_skin_indices.push_back(skin_index); |
| 831 | output_skins.push_back(input_skins[skin_index]); |
| 832 | Ref<GLTFSkin> skin = input_skins[skin_index]; |
| 833 | Vector<SkinNodeIndex> skin_joints = skin->get_joints(); |
| 834 | for (int j = 0; j < skin_joints.size(); ++j) { |
| 835 | SkinNodeIndex joint_index = skin_joints[j]; |
| 836 | joint_mapping[joint_index] = true; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | return OK; |
| 842 | } |
| 843 | |
| 844 | String SkinTool::_sanitize_bone_name(const String &p_name) { |
| 845 | String bone_name = p_name; |
| 846 | bone_name = bone_name.replace_chars(":/", '_'); |
| 847 | return bone_name; |
| 848 | } |