skin_tool.cpp 26 KB

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