gltf_skin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**************************************************************************/
  2. /* gltf_skin.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 "gltf_skin.h"
  31. #include "../gltf_template_convert.h"
  32. #include "core/variant/typed_array.h"
  33. #include "scene/resources/3d/skin.h"
  34. void GLTFSkin::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("get_skin_root"), &GLTFSkin::get_skin_root);
  36. ClassDB::bind_method(D_METHOD("set_skin_root", "skin_root"), &GLTFSkin::set_skin_root);
  37. ClassDB::bind_method(D_METHOD("get_joints_original"), &GLTFSkin::get_joints_original);
  38. ClassDB::bind_method(D_METHOD("set_joints_original", "joints_original"), &GLTFSkin::set_joints_original);
  39. ClassDB::bind_method(D_METHOD("get_inverse_binds"), &GLTFSkin::get_inverse_binds);
  40. ClassDB::bind_method(D_METHOD("set_inverse_binds", "inverse_binds"), &GLTFSkin::set_inverse_binds);
  41. ClassDB::bind_method(D_METHOD("get_joints"), &GLTFSkin::get_joints);
  42. ClassDB::bind_method(D_METHOD("set_joints", "joints"), &GLTFSkin::set_joints);
  43. ClassDB::bind_method(D_METHOD("get_non_joints"), &GLTFSkin::get_non_joints);
  44. ClassDB::bind_method(D_METHOD("set_non_joints", "non_joints"), &GLTFSkin::set_non_joints);
  45. ClassDB::bind_method(D_METHOD("get_roots"), &GLTFSkin::get_roots);
  46. ClassDB::bind_method(D_METHOD("set_roots", "roots"), &GLTFSkin::set_roots);
  47. ClassDB::bind_method(D_METHOD("get_skeleton"), &GLTFSkin::get_skeleton);
  48. ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &GLTFSkin::set_skeleton);
  49. ClassDB::bind_method(D_METHOD("get_joint_i_to_bone_i"), &GLTFSkin::get_joint_i_to_bone_i);
  50. ClassDB::bind_method(D_METHOD("set_joint_i_to_bone_i", "joint_i_to_bone_i"), &GLTFSkin::set_joint_i_to_bone_i);
  51. ClassDB::bind_method(D_METHOD("get_joint_i_to_name"), &GLTFSkin::get_joint_i_to_name);
  52. ClassDB::bind_method(D_METHOD("set_joint_i_to_name", "joint_i_to_name"), &GLTFSkin::set_joint_i_to_name);
  53. ClassDB::bind_method(D_METHOD("get_godot_skin"), &GLTFSkin::get_godot_skin);
  54. ClassDB::bind_method(D_METHOD("set_godot_skin", "godot_skin"), &GLTFSkin::set_godot_skin);
  55. ADD_PROPERTY(PropertyInfo(Variant::INT, "skin_root"), "set_skin_root", "get_skin_root"); // GLTFNodeIndex
  56. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints_original"), "set_joints_original", "get_joints_original"); // Vector<GLTFNodeIndex>
  57. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "inverse_binds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_inverse_binds", "get_inverse_binds"); // Vector<Transform3D>
  58. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints"), "set_joints", "get_joints"); // Vector<GLTFNodeIndex>
  59. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "non_joints"), "set_non_joints", "get_non_joints"); // Vector<GLTFNodeIndex>
  60. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "roots"), "set_roots", "get_roots"); // Vector<GLTFNodeIndex>
  61. ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton"), "set_skeleton", "get_skeleton"); // int
  62. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "joint_i_to_bone_i", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_joint_i_to_bone_i", "get_joint_i_to_bone_i"); // RBMap<int,
  63. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "joint_i_to_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_joint_i_to_name", "get_joint_i_to_name"); // RBMap<int,
  64. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "godot_skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_godot_skin", "get_godot_skin"); // Ref<Skin>
  65. }
  66. GLTFNodeIndex GLTFSkin::get_skin_root() {
  67. return skin_root;
  68. }
  69. void GLTFSkin::set_skin_root(GLTFNodeIndex p_skin_root) {
  70. skin_root = p_skin_root;
  71. }
  72. Vector<GLTFNodeIndex> GLTFSkin::get_joints_original() {
  73. return joints_original;
  74. }
  75. void GLTFSkin::set_joints_original(Vector<GLTFNodeIndex> p_joints_original) {
  76. joints_original = p_joints_original;
  77. }
  78. TypedArray<Transform3D> GLTFSkin::get_inverse_binds() {
  79. return GLTFTemplateConvert::to_array(inverse_binds);
  80. }
  81. void GLTFSkin::set_inverse_binds(TypedArray<Transform3D> p_inverse_binds) {
  82. GLTFTemplateConvert::set_from_array(inverse_binds, p_inverse_binds);
  83. }
  84. Vector<GLTFNodeIndex> GLTFSkin::get_joints() {
  85. return joints;
  86. }
  87. void GLTFSkin::set_joints(Vector<GLTFNodeIndex> p_joints) {
  88. joints = p_joints;
  89. }
  90. Vector<GLTFNodeIndex> GLTFSkin::get_non_joints() {
  91. return non_joints;
  92. }
  93. void GLTFSkin::set_non_joints(Vector<GLTFNodeIndex> p_non_joints) {
  94. non_joints = p_non_joints;
  95. }
  96. Vector<GLTFNodeIndex> GLTFSkin::get_roots() {
  97. return roots;
  98. }
  99. void GLTFSkin::set_roots(Vector<GLTFNodeIndex> p_roots) {
  100. roots = p_roots;
  101. }
  102. int GLTFSkin::get_skeleton() {
  103. return skeleton;
  104. }
  105. void GLTFSkin::set_skeleton(int p_skeleton) {
  106. skeleton = p_skeleton;
  107. }
  108. Dictionary GLTFSkin::get_joint_i_to_bone_i() {
  109. return GLTFTemplateConvert::to_dictionary(joint_i_to_bone_i);
  110. }
  111. void GLTFSkin::set_joint_i_to_bone_i(Dictionary p_joint_i_to_bone_i) {
  112. GLTFTemplateConvert::set_from_dictionary(joint_i_to_bone_i, p_joint_i_to_bone_i);
  113. }
  114. Dictionary GLTFSkin::get_joint_i_to_name() {
  115. Dictionary ret;
  116. HashMap<int, StringName>::Iterator elem = joint_i_to_name.begin();
  117. while (elem) {
  118. ret[elem->key] = String(elem->value);
  119. ++elem;
  120. }
  121. return ret;
  122. }
  123. void GLTFSkin::set_joint_i_to_name(Dictionary p_joint_i_to_name) {
  124. joint_i_to_name = HashMap<int, StringName>();
  125. Array keys = p_joint_i_to_name.keys();
  126. for (int i = 0; i < keys.size(); i++) {
  127. joint_i_to_name[keys[i]] = p_joint_i_to_name[keys[i]];
  128. }
  129. }
  130. Ref<Skin> GLTFSkin::get_godot_skin() {
  131. return godot_skin;
  132. }
  133. void GLTFSkin::set_godot_skin(Ref<Skin> p_godot_skin) {
  134. godot_skin = p_godot_skin;
  135. }
  136. Error GLTFSkin::from_dictionary(const Dictionary &dict) {
  137. ERR_FAIL_COND_V(!dict.has("skin_root"), ERR_INVALID_DATA);
  138. skin_root = dict["skin_root"];
  139. ERR_FAIL_COND_V(!dict.has("joints_original"), ERR_INVALID_DATA);
  140. Array joints_original_array = dict["joints_original"];
  141. joints_original.clear();
  142. for (int i = 0; i < joints_original_array.size(); ++i) {
  143. joints_original.push_back(joints_original_array[i]);
  144. }
  145. ERR_FAIL_COND_V(!dict.has("inverse_binds"), ERR_INVALID_DATA);
  146. Array inverse_binds_array = dict["inverse_binds"];
  147. inverse_binds.clear();
  148. for (int i = 0; i < inverse_binds_array.size(); ++i) {
  149. ERR_FAIL_COND_V(inverse_binds_array[i].get_type() != Variant::TRANSFORM3D, ERR_INVALID_DATA);
  150. inverse_binds.push_back(inverse_binds_array[i]);
  151. }
  152. ERR_FAIL_COND_V(!dict.has("joints"), ERR_INVALID_DATA);
  153. Array joints_array = dict["joints"];
  154. joints.clear();
  155. for (int i = 0; i < joints_array.size(); ++i) {
  156. joints.push_back(joints_array[i]);
  157. }
  158. ERR_FAIL_COND_V(!dict.has("non_joints"), ERR_INVALID_DATA);
  159. Array non_joints_array = dict["non_joints"];
  160. non_joints.clear();
  161. for (int i = 0; i < non_joints_array.size(); ++i) {
  162. non_joints.push_back(non_joints_array[i]);
  163. }
  164. ERR_FAIL_COND_V(!dict.has("roots"), ERR_INVALID_DATA);
  165. Array roots_array = dict["roots"];
  166. roots.clear();
  167. for (int i = 0; i < roots_array.size(); ++i) {
  168. roots.push_back(roots_array[i]);
  169. }
  170. ERR_FAIL_COND_V(!dict.has("skeleton"), ERR_INVALID_DATA);
  171. skeleton = dict["skeleton"];
  172. ERR_FAIL_COND_V(!dict.has("joint_i_to_bone_i"), ERR_INVALID_DATA);
  173. Dictionary joint_i_to_bone_i_dict = dict["joint_i_to_bone_i"];
  174. joint_i_to_bone_i.clear();
  175. for (int i = 0; i < joint_i_to_bone_i_dict.keys().size(); ++i) {
  176. int key = joint_i_to_bone_i_dict.keys()[i];
  177. int value = joint_i_to_bone_i_dict[key];
  178. joint_i_to_bone_i[key] = value;
  179. }
  180. ERR_FAIL_COND_V(!dict.has("joint_i_to_name"), ERR_INVALID_DATA);
  181. Dictionary joint_i_to_name_dict = dict["joint_i_to_name"];
  182. joint_i_to_name.clear();
  183. for (int i = 0; i < joint_i_to_name_dict.keys().size(); ++i) {
  184. int key = joint_i_to_name_dict.keys()[i];
  185. StringName value = joint_i_to_name_dict[key];
  186. joint_i_to_name[key] = value;
  187. }
  188. if (dict.has("godot_skin")) {
  189. godot_skin = dict["godot_skin"];
  190. }
  191. return OK;
  192. }
  193. Dictionary GLTFSkin::to_dictionary() {
  194. Dictionary dict;
  195. dict["skin_root"] = skin_root;
  196. Array joints_original_array;
  197. for (int i = 0; i < joints_original.size(); ++i) {
  198. joints_original_array.push_back(joints_original[i]);
  199. }
  200. dict["joints_original"] = joints_original_array;
  201. Array inverse_binds_array;
  202. for (int i = 0; i < inverse_binds.size(); ++i) {
  203. inverse_binds_array.push_back(inverse_binds[i]);
  204. }
  205. dict["inverse_binds"] = inverse_binds_array;
  206. Array joints_array;
  207. for (int i = 0; i < joints.size(); ++i) {
  208. joints_array.push_back(joints[i]);
  209. }
  210. dict["joints"] = joints_array;
  211. Array non_joints_array;
  212. for (int i = 0; i < non_joints.size(); ++i) {
  213. non_joints_array.push_back(non_joints[i]);
  214. }
  215. dict["non_joints"] = non_joints_array;
  216. Array roots_array;
  217. for (int i = 0; i < roots.size(); ++i) {
  218. roots_array.push_back(roots[i]);
  219. }
  220. dict["roots"] = roots_array;
  221. dict["skeleton"] = skeleton;
  222. Dictionary joint_i_to_bone_i_dict;
  223. for (HashMap<int, int>::Iterator E = joint_i_to_bone_i.begin(); E; ++E) {
  224. joint_i_to_bone_i_dict[E->key] = E->value;
  225. }
  226. dict["joint_i_to_bone_i"] = joint_i_to_bone_i_dict;
  227. Dictionary joint_i_to_name_dict;
  228. for (HashMap<int, StringName>::Iterator E = joint_i_to_name.begin(); E; ++E) {
  229. joint_i_to_name_dict[E->key] = E->value;
  230. }
  231. dict["joint_i_to_name"] = joint_i_to_name_dict;
  232. dict["godot_skin"] = godot_skin;
  233. return dict;
  234. }