skeleton.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /**************************************************************************/
  2. /* skeleton.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 "skeleton.h"
  31. #include "core/message_queue.h"
  32. #include "core/project_settings.h"
  33. #include "scene/3d/physics_body.h"
  34. #include "scene/resources/surface_tool.h"
  35. void SkinReference::_skin_changed() {
  36. if (skeleton_node) {
  37. skeleton_node->_make_dirty();
  38. }
  39. skeleton_version = 0;
  40. }
  41. void SkinReference::_bind_methods() {
  42. ClassDB::bind_method(D_METHOD("_skin_changed"), &SkinReference::_skin_changed);
  43. ClassDB::bind_method(D_METHOD("get_skeleton"), &SkinReference::get_skeleton);
  44. ClassDB::bind_method(D_METHOD("get_skin"), &SkinReference::get_skin);
  45. }
  46. RID SkinReference::get_skeleton() const {
  47. return skeleton;
  48. }
  49. Skeleton *SkinReference::get_skeleton_node() const {
  50. return skeleton_node;
  51. }
  52. Ref<Skin> SkinReference::get_skin() const {
  53. return skin;
  54. }
  55. SkinReference::~SkinReference() {
  56. if (skeleton_node) {
  57. skeleton_node->skin_bindings.erase(this);
  58. }
  59. VS::get_singleton()->free(skeleton);
  60. }
  61. bool Skeleton::_set(const StringName &p_path, const Variant &p_value) {
  62. String path = p_path;
  63. if (!path.begins_with("bones/")) {
  64. return false;
  65. }
  66. int which = path.get_slicec('/', 1).to_int();
  67. String what = path.get_slicec('/', 2);
  68. if (which == bones.size() && what == "name") {
  69. add_bone(p_value);
  70. return true;
  71. }
  72. ERR_FAIL_INDEX_V(which, bones.size(), false);
  73. if (what == "parent") {
  74. set_bone_parent(which, p_value);
  75. } else if (what == "rest") {
  76. set_bone_rest(which, p_value);
  77. } else if (what == "enabled") {
  78. set_bone_enabled(which, p_value);
  79. } else if (what == "pose") {
  80. set_bone_pose(which, p_value);
  81. } else if (what == "bound_children") {
  82. Array children = p_value;
  83. if (is_inside_tree()) {
  84. bones.write[which].nodes_bound.clear();
  85. for (int i = 0; i < children.size(); i++) {
  86. NodePath npath = children[i];
  87. ERR_CONTINUE(npath.operator String() == "");
  88. Node *node = get_node(npath);
  89. ERR_CONTINUE(!node);
  90. bind_child_node_to_bone(which, node);
  91. }
  92. }
  93. } else {
  94. return false;
  95. }
  96. return true;
  97. }
  98. bool Skeleton::_get(const StringName &p_path, Variant &r_ret) const {
  99. String path = p_path;
  100. if (!path.begins_with("bones/")) {
  101. return false;
  102. }
  103. int which = path.get_slicec('/', 1).to_int();
  104. String what = path.get_slicec('/', 2);
  105. ERR_FAIL_INDEX_V(which, bones.size(), false);
  106. if (what == "name") {
  107. r_ret = get_bone_name(which);
  108. } else if (what == "parent") {
  109. r_ret = get_bone_parent(which);
  110. } else if (what == "rest") {
  111. r_ret = get_bone_rest(which);
  112. } else if (what == "enabled") {
  113. r_ret = is_bone_enabled(which);
  114. } else if (what == "pose") {
  115. r_ret = get_bone_pose(which);
  116. } else if (what == "bound_children") {
  117. Array children;
  118. const LocalVectori<uint32_t> &nodes = bones[which].nodes_bound;
  119. for (int i = 0; i < nodes.size(); i++) {
  120. Object *obj = ObjectDB::get_instance(nodes[i]);
  121. ERR_CONTINUE(!obj);
  122. Node *node = Object::cast_to<Node>(obj);
  123. ERR_CONTINUE(!node);
  124. NodePath npath = get_path_to(node);
  125. children.push_back(npath);
  126. }
  127. r_ret = children;
  128. } else {
  129. return false;
  130. }
  131. return true;
  132. }
  133. void Skeleton::_get_property_list(List<PropertyInfo> *p_list) const {
  134. for (int i = 0; i < bones.size(); i++) {
  135. const String prep = vformat("%s/%d/", PNAME("bones"), i);
  136. p_list->push_back(PropertyInfo(Variant::STRING, prep + PNAME("name")));
  137. p_list->push_back(PropertyInfo(Variant::INT, prep + PNAME("parent"), PROPERTY_HINT_RANGE, "-1," + itos(bones.size() - 1) + ",1"));
  138. p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + PNAME("rest")));
  139. p_list->push_back(PropertyInfo(Variant::BOOL, prep + PNAME("enabled")));
  140. p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + PNAME("pose"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  141. p_list->push_back(PropertyInfo(Variant::ARRAY, prep + PNAME("bound_children")));
  142. }
  143. }
  144. void Skeleton::_update_process_order() {
  145. if (!process_order_dirty) {
  146. return;
  147. }
  148. Bone *bonesptr = bones.ptrw();
  149. int len = bones.size();
  150. process_order.resize(len);
  151. int *order = process_order.ptrw();
  152. for (int i = 0; i < len; i++) {
  153. if (bonesptr[i].parent >= len) {
  154. //validate this just in case
  155. ERR_PRINT("Bone " + itos(i) + " has invalid parent: " + itos(bonesptr[i].parent));
  156. bonesptr[i].parent = -1;
  157. }
  158. order[i] = i;
  159. bonesptr[i].sort_index = i;
  160. }
  161. //now check process order
  162. int pass_count = 0;
  163. while (pass_count < len * len) {
  164. //using bubblesort because of simplicity, it won't run every frame though.
  165. //bublesort worst case is O(n^2), and this may be an infinite loop if cyclic
  166. bool swapped = false;
  167. for (int i = 0; i < len; i++) {
  168. int parent_idx = bonesptr[order[i]].parent;
  169. if (parent_idx < 0) {
  170. continue; //do nothing because it has no parent
  171. }
  172. //swap indices
  173. int parent_order = bonesptr[parent_idx].sort_index;
  174. if (parent_order > i) {
  175. bonesptr[order[i]].sort_index = parent_order;
  176. bonesptr[parent_idx].sort_index = i;
  177. //swap order
  178. SWAP(order[i], order[parent_order]);
  179. swapped = true;
  180. }
  181. }
  182. if (!swapped) {
  183. break;
  184. }
  185. pass_count++;
  186. }
  187. if (pass_count == len * len) {
  188. ERR_PRINT("Skeleton parenthood graph is cyclic");
  189. }
  190. process_order_dirty = false;
  191. }
  192. void Skeleton::_notification(int p_what) {
  193. switch (p_what) {
  194. case NOTIFICATION_UPDATE_SKELETON: {
  195. VisualServer *vs = VisualServer::get_singleton();
  196. Bone *bonesptr = bones.ptrw();
  197. int len = bones.size();
  198. _update_process_order();
  199. const int *order = process_order.ptr();
  200. for (int i = 0; i < len; i++) {
  201. Bone &b = bonesptr[order[i]];
  202. if (b.disable_rest) {
  203. if (b.enabled) {
  204. Transform pose = b.pose;
  205. if (b.custom_pose_enable) {
  206. pose = b.custom_pose * pose;
  207. }
  208. if (b.parent >= 0) {
  209. b.pose_global = bonesptr[b.parent].pose_global * pose;
  210. b.pose_global_no_override = bonesptr[b.parent].pose_global_no_override * pose;
  211. } else {
  212. b.pose_global = pose;
  213. b.pose_global_no_override = pose;
  214. }
  215. } else {
  216. if (b.parent >= 0) {
  217. b.pose_global = bonesptr[b.parent].pose_global;
  218. b.pose_global_no_override = bonesptr[b.parent].pose_global_no_override;
  219. } else {
  220. b.pose_global = Transform();
  221. b.pose_global_no_override = Transform();
  222. }
  223. }
  224. } else {
  225. if (b.enabled) {
  226. Transform pose = b.pose;
  227. if (b.custom_pose_enable) {
  228. pose = b.custom_pose * pose;
  229. }
  230. if (b.parent >= 0) {
  231. b.pose_global = bonesptr[b.parent].pose_global * (b.rest * pose);
  232. b.pose_global_no_override = bonesptr[b.parent].pose_global_no_override * (b.rest * pose);
  233. } else {
  234. b.pose_global = b.rest * pose;
  235. b.pose_global_no_override = b.rest * pose;
  236. }
  237. } else {
  238. if (b.parent >= 0) {
  239. b.pose_global = bonesptr[b.parent].pose_global * b.rest;
  240. b.pose_global_no_override = bonesptr[b.parent].pose_global_no_override * b.rest;
  241. } else {
  242. b.pose_global = b.rest;
  243. b.pose_global_no_override = b.rest;
  244. }
  245. }
  246. }
  247. if (b.global_pose_override_amount >= CMP_EPSILON) {
  248. b.pose_global = b.pose_global.interpolate_with(b.global_pose_override, b.global_pose_override_amount);
  249. }
  250. if (b.global_pose_override_reset) {
  251. b.global_pose_override_amount = 0.0;
  252. }
  253. const LocalVectori<uint32_t> &nodes = b.nodes_bound;
  254. for (int j = 0; j < nodes.size(); j++) {
  255. Object *obj = ObjectDB::get_instance(nodes[j]);
  256. ERR_CONTINUE(!obj);
  257. Spatial *sp = Object::cast_to<Spatial>(obj);
  258. ERR_CONTINUE(!sp);
  259. sp->set_transform(b.pose_global);
  260. }
  261. }
  262. //update skins
  263. for (Set<SkinReference *>::Element *E = skin_bindings.front(); E; E = E->next()) {
  264. const Skin *skin = E->get()->skin.operator->();
  265. RID skeleton = E->get()->skeleton;
  266. uint32_t bind_count = skin->get_bind_count();
  267. if (E->get()->bind_count != bind_count) {
  268. VS::get_singleton()->skeleton_allocate(skeleton, bind_count);
  269. E->get()->bind_count = bind_count;
  270. E->get()->skin_bone_indices.resize(bind_count);
  271. E->get()->skin_bone_indices_ptrs = E->get()->skin_bone_indices.ptrw();
  272. }
  273. if (E->get()->skeleton_version != version) {
  274. for (uint32_t i = 0; i < bind_count; i++) {
  275. StringName bind_name = skin->get_bind_name(i);
  276. if (bind_name != StringName()) {
  277. //bind name used, use this
  278. bool found = false;
  279. for (int j = 0; j < len; j++) {
  280. if (bonesptr[j].name == bind_name) {
  281. E->get()->skin_bone_indices_ptrs[i] = j;
  282. found = true;
  283. break;
  284. }
  285. }
  286. if (!found) {
  287. ERR_PRINT("Skin bind #" + itos(i) + " contains named bind '" + String(bind_name) + "' but Skeleton has no bone by that name.");
  288. E->get()->skin_bone_indices_ptrs[i] = 0;
  289. }
  290. } else if (skin->get_bind_bone(i) >= 0) {
  291. int bind_index = skin->get_bind_bone(i);
  292. if (bind_index >= len) {
  293. ERR_PRINT("Skin bind #" + itos(i) + " contains bone index bind: " + itos(bind_index) + " , which is greater than the skeleton bone count: " + itos(len) + ".");
  294. E->get()->skin_bone_indices_ptrs[i] = 0;
  295. } else {
  296. E->get()->skin_bone_indices_ptrs[i] = bind_index;
  297. }
  298. } else {
  299. ERR_PRINT("Skin bind #" + itos(i) + " does not contain a name nor a bone index.");
  300. E->get()->skin_bone_indices_ptrs[i] = 0;
  301. }
  302. }
  303. E->get()->skeleton_version = version;
  304. }
  305. for (uint32_t i = 0; i < bind_count; i++) {
  306. uint32_t bone_index = E->get()->skin_bone_indices_ptrs[i];
  307. ERR_CONTINUE(bone_index >= (uint32_t)len);
  308. vs->skeleton_bone_set_transform(skeleton, i, bonesptr[bone_index].pose_global * skin->get_bind_pose(i));
  309. }
  310. }
  311. dirty = false;
  312. emit_signal("skeleton_updated");
  313. } break;
  314. }
  315. }
  316. void Skeleton::clear_bones_global_pose_override() {
  317. for (int i = 0; i < bones.size(); i += 1) {
  318. bones.write[i].global_pose_override_amount = 0;
  319. bones.write[i].global_pose_override_reset = true;
  320. }
  321. _make_dirty();
  322. }
  323. void Skeleton::set_bone_global_pose_override(int p_bone, const Transform &p_pose, float p_amount, bool p_persistent) {
  324. ERR_FAIL_INDEX(p_bone, bones.size());
  325. bones.write[p_bone].global_pose_override_amount = p_amount;
  326. bones.write[p_bone].global_pose_override = p_pose;
  327. bones.write[p_bone].global_pose_override_reset = !p_persistent;
  328. _make_dirty();
  329. }
  330. Transform Skeleton::get_bone_global_pose(int p_bone) const {
  331. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  332. if (dirty) {
  333. const_cast<Skeleton *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  334. }
  335. return bones[p_bone].pose_global;
  336. }
  337. Transform Skeleton::get_bone_global_pose_no_override(int p_bone) const {
  338. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  339. if (dirty)
  340. const_cast<Skeleton *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  341. return bones[p_bone].pose_global_no_override;
  342. }
  343. // skeleton creation api
  344. void Skeleton::add_bone(const String &p_name) {
  345. ERR_FAIL_COND(p_name == "" || p_name.find(":") != -1 || p_name.find("/") != -1);
  346. for (int i = 0; i < bones.size(); i++) {
  347. ERR_FAIL_COND(bones[i].name == p_name);
  348. }
  349. Bone b;
  350. b.name = p_name;
  351. bones.push_back(b);
  352. process_order_dirty = true;
  353. version++;
  354. _make_dirty();
  355. update_gizmo();
  356. }
  357. int Skeleton::find_bone(const String &p_name) const {
  358. for (int i = 0; i < bones.size(); i++) {
  359. if (bones[i].name == p_name) {
  360. return i;
  361. }
  362. }
  363. return -1;
  364. }
  365. String Skeleton::get_bone_name(int p_bone) const {
  366. ERR_FAIL_INDEX_V(p_bone, bones.size(), "");
  367. return bones[p_bone].name;
  368. }
  369. void Skeleton::set_bone_name(int p_bone, const String &p_name) {
  370. ERR_FAIL_INDEX(p_bone, bones.size());
  371. for (int i = 0; i < bones.size(); i++) {
  372. if (i != p_bone) {
  373. ERR_FAIL_COND(bones[i].name == p_name);
  374. }
  375. }
  376. bones.write[p_bone].name = p_name;
  377. }
  378. bool Skeleton::is_bone_parent_of(int p_bone, int p_parent_bone_id) const {
  379. int parent_of_bone = get_bone_parent(p_bone);
  380. if (-1 == parent_of_bone) {
  381. return false;
  382. }
  383. if (parent_of_bone == p_parent_bone_id) {
  384. return true;
  385. }
  386. return is_bone_parent_of(parent_of_bone, p_parent_bone_id);
  387. }
  388. int Skeleton::get_bone_count() const {
  389. return bones.size();
  390. }
  391. void Skeleton::set_bone_parent(int p_bone, int p_parent) {
  392. ERR_FAIL_INDEX(p_bone, bones.size());
  393. ERR_FAIL_COND(p_parent != -1 && (p_parent < 0));
  394. ERR_FAIL_COND(p_bone == p_parent);
  395. bones.write[p_bone].parent = p_parent;
  396. process_order_dirty = true;
  397. _make_dirty();
  398. }
  399. void Skeleton::unparent_bone_and_rest(int p_bone) {
  400. ERR_FAIL_INDEX(p_bone, bones.size());
  401. _update_process_order();
  402. int parent = bones[p_bone].parent;
  403. while (parent >= 0) {
  404. bones.write[p_bone].rest = bones[parent].rest * bones[p_bone].rest;
  405. parent = bones[parent].parent;
  406. }
  407. bones.write[p_bone].parent = -1;
  408. process_order_dirty = true;
  409. _make_dirty();
  410. }
  411. void Skeleton::set_bone_disable_rest(int p_bone, bool p_disable) {
  412. ERR_FAIL_INDEX(p_bone, bones.size());
  413. bones.write[p_bone].disable_rest = p_disable;
  414. }
  415. bool Skeleton::is_bone_rest_disabled(int p_bone) const {
  416. ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
  417. return bones[p_bone].disable_rest;
  418. }
  419. int Skeleton::get_bone_parent(int p_bone) const {
  420. ERR_FAIL_INDEX_V(p_bone, bones.size(), -1);
  421. return bones[p_bone].parent;
  422. }
  423. void Skeleton::set_bone_rest(int p_bone, const Transform &p_rest) {
  424. ERR_FAIL_INDEX(p_bone, bones.size());
  425. bones.write[p_bone].rest = p_rest;
  426. _make_dirty();
  427. }
  428. Transform Skeleton::get_bone_rest(int p_bone) const {
  429. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  430. return bones[p_bone].rest;
  431. }
  432. void Skeleton::set_bone_enabled(int p_bone, bool p_enabled) {
  433. ERR_FAIL_INDEX(p_bone, bones.size());
  434. bones.write[p_bone].enabled = p_enabled;
  435. _make_dirty();
  436. }
  437. bool Skeleton::is_bone_enabled(int p_bone) const {
  438. ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
  439. return bones[p_bone].enabled;
  440. }
  441. void Skeleton::bind_child_node_to_bone(int p_bone, Node *p_node) {
  442. ERR_FAIL_NULL(p_node);
  443. ERR_FAIL_INDEX(p_bone, bones.size());
  444. uint32_t id = p_node->get_instance_id();
  445. if (bones[p_bone].nodes_bound.find(id) != -1) {
  446. return; // Already here.
  447. }
  448. bones.write[p_bone].nodes_bound.push_back(id);
  449. }
  450. void Skeleton::unbind_child_node_from_bone(int p_bone, Node *p_node) {
  451. ERR_FAIL_NULL(p_node);
  452. ERR_FAIL_INDEX(p_bone, bones.size());
  453. uint32_t id = p_node->get_instance_id();
  454. int index = bones[p_bone].nodes_bound.find(id);
  455. if (index == -1) {
  456. return; // Doesn't exist in the first place.
  457. }
  458. bones.write[p_bone].nodes_bound.remove(index);
  459. }
  460. void Skeleton::get_bound_child_nodes_to_bone(int p_bone, List<Node *> *p_bound) const {
  461. ERR_FAIL_INDEX(p_bone, bones.size());
  462. const LocalVectori<uint32_t> &nodes = bones[p_bone].nodes_bound;
  463. for (int i = 0; i < nodes.size(); i++) {
  464. Object *obj = ObjectDB::get_instance(nodes[i]);
  465. ERR_CONTINUE(!obj);
  466. p_bound->push_back(Object::cast_to<Node>(obj));
  467. }
  468. }
  469. void Skeleton::clear_bones() {
  470. bones.clear();
  471. process_order_dirty = true;
  472. version++;
  473. _make_dirty();
  474. }
  475. // posing api
  476. void Skeleton::set_bone_pose(int p_bone, const Transform &p_pose) {
  477. ERR_FAIL_INDEX(p_bone, bones.size());
  478. bones.write[p_bone].pose = p_pose;
  479. if (is_inside_tree()) {
  480. _make_dirty();
  481. }
  482. }
  483. Transform Skeleton::get_bone_pose(int p_bone) const {
  484. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  485. return bones[p_bone].pose;
  486. }
  487. void Skeleton::set_bone_custom_pose(int p_bone, const Transform &p_custom_pose) {
  488. ERR_FAIL_INDEX(p_bone, bones.size());
  489. //ERR_FAIL_COND( !is_inside_scene() );
  490. bones.write[p_bone].custom_pose_enable = (p_custom_pose != Transform());
  491. bones.write[p_bone].custom_pose = p_custom_pose;
  492. _make_dirty();
  493. }
  494. Transform Skeleton::get_bone_custom_pose(int p_bone) const {
  495. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  496. return bones[p_bone].custom_pose;
  497. }
  498. void Skeleton::_make_dirty() {
  499. if (dirty) {
  500. return;
  501. }
  502. MessageQueue::get_singleton()->push_notification(this, NOTIFICATION_UPDATE_SKELETON);
  503. dirty = true;
  504. }
  505. int Skeleton::get_process_order(int p_idx) {
  506. ERR_FAIL_INDEX_V(p_idx, bones.size(), -1);
  507. _update_process_order();
  508. return process_order[p_idx];
  509. }
  510. void Skeleton::localize_rests() {
  511. _update_process_order();
  512. for (int i = bones.size() - 1; i >= 0; i--) {
  513. int idx = process_order[i];
  514. if (bones[idx].parent >= 0) {
  515. set_bone_rest(idx, bones[bones[idx].parent].rest.affine_inverse() * bones[idx].rest);
  516. }
  517. }
  518. }
  519. #ifndef _3D_DISABLED
  520. void Skeleton::bind_physical_bone_to_bone(int p_bone, PhysicalBone *p_physical_bone) {
  521. ERR_FAIL_INDEX(p_bone, bones.size());
  522. ERR_FAIL_COND(bones[p_bone].physical_bone);
  523. ERR_FAIL_COND(!p_physical_bone);
  524. bones.write[p_bone].physical_bone = p_physical_bone;
  525. _rebuild_physical_bones_cache();
  526. }
  527. void Skeleton::unbind_physical_bone_from_bone(int p_bone) {
  528. ERR_FAIL_INDEX(p_bone, bones.size());
  529. bones.write[p_bone].physical_bone = nullptr;
  530. _rebuild_physical_bones_cache();
  531. }
  532. PhysicalBone *Skeleton::get_physical_bone(int p_bone) {
  533. ERR_FAIL_INDEX_V(p_bone, bones.size(), nullptr);
  534. return bones[p_bone].physical_bone;
  535. }
  536. PhysicalBone *Skeleton::get_physical_bone_parent(int p_bone) {
  537. ERR_FAIL_INDEX_V(p_bone, bones.size(), nullptr);
  538. if (bones[p_bone].cache_parent_physical_bone) {
  539. return bones[p_bone].cache_parent_physical_bone;
  540. }
  541. return _get_physical_bone_parent(p_bone);
  542. }
  543. PhysicalBone *Skeleton::_get_physical_bone_parent(int p_bone) {
  544. ERR_FAIL_INDEX_V(p_bone, bones.size(), nullptr);
  545. const int parent_bone = bones[p_bone].parent;
  546. if (0 > parent_bone) {
  547. return nullptr;
  548. }
  549. PhysicalBone *pb = bones[parent_bone].physical_bone;
  550. if (pb) {
  551. return pb;
  552. } else {
  553. return get_physical_bone_parent(parent_bone);
  554. }
  555. }
  556. void Skeleton::_rebuild_physical_bones_cache() {
  557. const int b_size = bones.size();
  558. for (int i = 0; i < b_size; ++i) {
  559. PhysicalBone *parent_pb = _get_physical_bone_parent(i);
  560. if (parent_pb != bones[i].cache_parent_physical_bone) {
  561. bones.write[i].cache_parent_physical_bone = parent_pb;
  562. if (bones[i].physical_bone) {
  563. bones[i].physical_bone->_on_bone_parent_changed();
  564. }
  565. }
  566. }
  567. }
  568. void _pb_stop_simulation(Node *p_node) {
  569. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  570. _pb_stop_simulation(p_node->get_child(i));
  571. }
  572. PhysicalBone *pb = Object::cast_to<PhysicalBone>(p_node);
  573. if (pb) {
  574. pb->set_simulate_physics(false);
  575. pb->set_static_body(false);
  576. }
  577. }
  578. void Skeleton::physical_bones_stop_simulation() {
  579. _pb_stop_simulation(this);
  580. }
  581. void _pb_start_simulation(const Skeleton *p_skeleton, Node *p_node, const Vector<int> &p_sim_bones) {
  582. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  583. _pb_start_simulation(p_skeleton, p_node->get_child(i), p_sim_bones);
  584. }
  585. PhysicalBone *pb = Object::cast_to<PhysicalBone>(p_node);
  586. if (pb) {
  587. bool sim = false;
  588. if (p_sim_bones.empty()) { // If no bones is specified, activate ragdoll on full body.
  589. sim = true;
  590. } else {
  591. for (int i = p_sim_bones.size() - 1; 0 <= i; --i) {
  592. if (p_sim_bones[i] == pb->get_bone_id() || p_skeleton->is_bone_parent_of(pb->get_bone_id(), p_sim_bones[i])) {
  593. sim = true;
  594. break;
  595. }
  596. }
  597. }
  598. pb->set_simulate_physics(true);
  599. if (sim) {
  600. pb->set_static_body(false);
  601. } else {
  602. pb->set_static_body(true);
  603. }
  604. }
  605. }
  606. void Skeleton::physical_bones_start_simulation_on(const Array &p_bones) {
  607. Vector<int> sim_bones;
  608. if (p_bones.size() > 0) {
  609. sim_bones.resize(p_bones.size());
  610. int c = 0;
  611. for (int i = sim_bones.size() - 1; 0 <= i; --i) {
  612. if (Variant::STRING == p_bones.get(i).get_type()) {
  613. int bone_id = find_bone(p_bones.get(i));
  614. if (bone_id != -1) {
  615. sim_bones.write[c++] = bone_id;
  616. }
  617. }
  618. }
  619. sim_bones.resize(c);
  620. }
  621. _pb_start_simulation(this, this, sim_bones);
  622. }
  623. void _physical_bones_add_remove_collision_exception(bool p_add, Node *p_node, RID p_exception) {
  624. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  625. _physical_bones_add_remove_collision_exception(p_add, p_node->get_child(i), p_exception);
  626. }
  627. CollisionObject *co = Object::cast_to<CollisionObject>(p_node);
  628. if (co) {
  629. if (p_add) {
  630. PhysicsServer::get_singleton()->body_add_collision_exception(co->get_rid(), p_exception);
  631. } else {
  632. PhysicsServer::get_singleton()->body_remove_collision_exception(co->get_rid(), p_exception);
  633. }
  634. }
  635. }
  636. void Skeleton::physical_bones_add_collision_exception(RID p_exception) {
  637. _physical_bones_add_remove_collision_exception(true, this, p_exception);
  638. }
  639. void Skeleton::physical_bones_remove_collision_exception(RID p_exception) {
  640. _physical_bones_add_remove_collision_exception(false, this, p_exception);
  641. }
  642. #endif // _3D_DISABLED
  643. void Skeleton::_skin_changed() {
  644. _make_dirty();
  645. }
  646. Ref<SkinReference> Skeleton::register_skin(const Ref<Skin> &p_skin) {
  647. for (Set<SkinReference *>::Element *E = skin_bindings.front(); E; E = E->next()) {
  648. if (E->get()->skin == p_skin) {
  649. return Ref<SkinReference>(E->get());
  650. }
  651. }
  652. Ref<Skin> skin = p_skin;
  653. if (skin.is_null()) {
  654. //need to create one from existing code, this is for compatibility only
  655. //when skeletons did not support skins. It is also used by gizmo
  656. //to display the skeleton.
  657. skin.instance();
  658. skin->set_bind_count(bones.size());
  659. _update_process_order(); //just in case
  660. // pose changed, rebuild cache of inverses
  661. const Bone *bonesptr = bones.ptr();
  662. int len = bones.size();
  663. const int *order = process_order.ptr();
  664. // calculate global rests and invert them
  665. for (int i = 0; i < len; i++) {
  666. const Bone &b = bonesptr[order[i]];
  667. if (b.parent >= 0) {
  668. skin->set_bind_pose(order[i], skin->get_bind_pose(b.parent) * b.rest);
  669. } else {
  670. skin->set_bind_pose(order[i], b.rest);
  671. }
  672. }
  673. for (int i = 0; i < len; i++) {
  674. //the inverse is what is actually required
  675. skin->set_bind_bone(i, i);
  676. skin->set_bind_pose(i, skin->get_bind_pose(i).affine_inverse());
  677. }
  678. }
  679. ERR_FAIL_COND_V(skin.is_null(), Ref<SkinReference>());
  680. Ref<SkinReference> skin_ref;
  681. skin_ref.instance();
  682. skin_ref->skeleton_node = this;
  683. skin_ref->bind_count = 0;
  684. skin_ref->skeleton = RID_PRIME(VisualServer::get_singleton()->skeleton_create());
  685. skin_ref->skeleton_node = this;
  686. skin_ref->skin = skin;
  687. skin_bindings.insert(skin_ref.operator->());
  688. skin->connect("changed", skin_ref.operator->(), "_skin_changed");
  689. _make_dirty();
  690. return skin_ref;
  691. }
  692. void Skeleton::_bind_methods() {
  693. ClassDB::bind_method(D_METHOD("add_bone", "name"), &Skeleton::add_bone);
  694. ClassDB::bind_method(D_METHOD("find_bone", "name"), &Skeleton::find_bone);
  695. ClassDB::bind_method(D_METHOD("get_bone_name", "bone_idx"), &Skeleton::get_bone_name);
  696. ClassDB::bind_method(D_METHOD("set_bone_name", "bone_idx", "name"), &Skeleton::set_bone_name);
  697. ClassDB::bind_method(D_METHOD("get_bone_parent", "bone_idx"), &Skeleton::get_bone_parent);
  698. ClassDB::bind_method(D_METHOD("set_bone_parent", "bone_idx", "parent_idx"), &Skeleton::set_bone_parent);
  699. ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton::get_bone_count);
  700. ClassDB::bind_method(D_METHOD("unparent_bone_and_rest", "bone_idx"), &Skeleton::unparent_bone_and_rest);
  701. ClassDB::bind_method(D_METHOD("get_bone_rest", "bone_idx"), &Skeleton::get_bone_rest);
  702. ClassDB::bind_method(D_METHOD("set_bone_rest", "bone_idx", "rest"), &Skeleton::set_bone_rest);
  703. ClassDB::bind_method(D_METHOD("register_skin", "skin"), &Skeleton::register_skin);
  704. ClassDB::bind_method(D_METHOD("localize_rests"), &Skeleton::localize_rests);
  705. ClassDB::bind_method(D_METHOD("set_bone_disable_rest", "bone_idx", "disable"), &Skeleton::set_bone_disable_rest);
  706. ClassDB::bind_method(D_METHOD("is_bone_rest_disabled", "bone_idx"), &Skeleton::is_bone_rest_disabled);
  707. ClassDB::bind_method(D_METHOD("bind_child_node_to_bone", "bone_idx", "node"), &Skeleton::bind_child_node_to_bone);
  708. ClassDB::bind_method(D_METHOD("unbind_child_node_from_bone", "bone_idx", "node"), &Skeleton::unbind_child_node_from_bone);
  709. ClassDB::bind_method(D_METHOD("get_bound_child_nodes_to_bone", "bone_idx"), &Skeleton::_get_bound_child_nodes_to_bone);
  710. ClassDB::bind_method(D_METHOD("clear_bones"), &Skeleton::clear_bones);
  711. ClassDB::bind_method(D_METHOD("get_bone_pose", "bone_idx"), &Skeleton::get_bone_pose);
  712. ClassDB::bind_method(D_METHOD("set_bone_pose", "bone_idx", "pose"), &Skeleton::set_bone_pose);
  713. ClassDB::bind_method(D_METHOD("clear_bones_global_pose_override"), &Skeleton::clear_bones_global_pose_override);
  714. ClassDB::bind_method(D_METHOD("set_bone_global_pose_override", "bone_idx", "pose", "amount", "persistent"), &Skeleton::set_bone_global_pose_override, DEFVAL(false));
  715. ClassDB::bind_method(D_METHOD("get_bone_global_pose", "bone_idx"), &Skeleton::get_bone_global_pose);
  716. ClassDB::bind_method(D_METHOD("get_bone_global_pose_no_override", "bone_idx"), &Skeleton::get_bone_global_pose_no_override);
  717. ClassDB::bind_method(D_METHOD("get_bone_custom_pose", "bone_idx"), &Skeleton::get_bone_custom_pose);
  718. ClassDB::bind_method(D_METHOD("set_bone_custom_pose", "bone_idx", "custom_pose"), &Skeleton::set_bone_custom_pose);
  719. #ifndef _3D_DISABLED
  720. ClassDB::bind_method(D_METHOD("physical_bones_stop_simulation"), &Skeleton::physical_bones_stop_simulation);
  721. ClassDB::bind_method(D_METHOD("physical_bones_start_simulation", "bones"), &Skeleton::physical_bones_start_simulation_on, DEFVAL(Array()));
  722. ClassDB::bind_method(D_METHOD("physical_bones_add_collision_exception", "exception"), &Skeleton::physical_bones_add_collision_exception);
  723. ClassDB::bind_method(D_METHOD("physical_bones_remove_collision_exception", "exception"), &Skeleton::physical_bones_remove_collision_exception);
  724. #endif // _3D_DISABLED
  725. ADD_SIGNAL(MethodInfo("skeleton_updated"));
  726. BIND_CONSTANT(NOTIFICATION_UPDATE_SKELETON);
  727. }
  728. Skeleton::Skeleton() {
  729. dirty = false;
  730. version = 1;
  731. process_order_dirty = true;
  732. }
  733. Skeleton::~Skeleton() {
  734. //some skins may remain bound
  735. for (Set<SkinReference *>::Element *E = skin_bindings.front(); E; E = E->next()) {
  736. E->get()->skeleton_node = nullptr;
  737. }
  738. }