skeleton.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*************************************************************************/
  2. /* skeleton.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. bool Skeleton::_set(const StringName &p_path, const Variant &p_value) {
  36. String path = p_path;
  37. if (!path.begins_with("bones/"))
  38. return false;
  39. int which = path.get_slicec('/', 1).to_int();
  40. String what = path.get_slicec('/', 2);
  41. if (which == bones.size() && what == "name") {
  42. add_bone(p_value);
  43. return true;
  44. }
  45. ERR_FAIL_INDEX_V(which, bones.size(), false);
  46. if (what == "parent")
  47. set_bone_parent(which, p_value);
  48. else if (what == "rest")
  49. set_bone_rest(which, p_value);
  50. else if (what == "enabled")
  51. set_bone_enabled(which, p_value);
  52. else if (what == "pose")
  53. set_bone_pose(which, p_value);
  54. else if (what == "bound_children") {
  55. Array children = p_value;
  56. if (is_inside_tree()) {
  57. bones.write[which].nodes_bound.clear();
  58. for (int i = 0; i < children.size(); i++) {
  59. NodePath path = children[i];
  60. ERR_CONTINUE(path.operator String() == "");
  61. Node *node = get_node(path);
  62. ERR_CONTINUE(!node);
  63. bind_child_node_to_bone(which, node);
  64. }
  65. }
  66. } else {
  67. return false;
  68. }
  69. return true;
  70. }
  71. bool Skeleton::_get(const StringName &p_path, Variant &r_ret) const {
  72. String path = p_path;
  73. if (!path.begins_with("bones/"))
  74. return false;
  75. int which = path.get_slicec('/', 1).to_int();
  76. String what = path.get_slicec('/', 2);
  77. ERR_FAIL_INDEX_V(which, bones.size(), false);
  78. if (what == "name")
  79. r_ret = get_bone_name(which);
  80. else if (what == "parent")
  81. r_ret = get_bone_parent(which);
  82. else if (what == "rest")
  83. r_ret = get_bone_rest(which);
  84. else if (what == "enabled")
  85. r_ret = is_bone_enabled(which);
  86. else if (what == "pose")
  87. r_ret = get_bone_pose(which);
  88. else if (what == "bound_children") {
  89. Array children;
  90. for (const List<uint32_t>::Element *E = bones[which].nodes_bound.front(); E; E = E->next()) {
  91. Object *obj = ObjectDB::get_instance(E->get());
  92. ERR_CONTINUE(!obj);
  93. Node *node = Object::cast_to<Node>(obj);
  94. ERR_CONTINUE(!node);
  95. NodePath path = get_path_to(node);
  96. children.push_back(path);
  97. }
  98. r_ret = children;
  99. } else
  100. return false;
  101. return true;
  102. }
  103. void Skeleton::_get_property_list(List<PropertyInfo> *p_list) const {
  104. for (int i = 0; i < bones.size(); i++) {
  105. String prep = "bones/" + itos(i) + "/";
  106. p_list->push_back(PropertyInfo(Variant::STRING, prep + "name"));
  107. p_list->push_back(PropertyInfo(Variant::INT, prep + "parent", PROPERTY_HINT_RANGE, "-1," + itos(bones.size() - 1) + ",1"));
  108. p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + "rest"));
  109. p_list->push_back(PropertyInfo(Variant::BOOL, prep + "enabled"));
  110. p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + "pose", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  111. p_list->push_back(PropertyInfo(Variant::ARRAY, prep + "bound_children"));
  112. }
  113. }
  114. void Skeleton::_update_process_order() {
  115. if (!process_order_dirty)
  116. return;
  117. Bone *bonesptr = bones.ptrw();
  118. int len = bones.size();
  119. process_order.resize(len);
  120. int *order = process_order.ptrw();
  121. for (int i = 0; i < len; i++) {
  122. if (bonesptr[i].parent >= len) {
  123. //validate this just in case
  124. ERR_PRINTS("Bone " + itos(i) + " has invalid parent: " + itos(bonesptr[i].parent));
  125. bonesptr[i].parent = -1;
  126. }
  127. order[i] = i;
  128. bonesptr[i].sort_index = i;
  129. }
  130. //now check process order
  131. int pass_count = 0;
  132. while (pass_count < len * len) {
  133. //using bubblesort because of simplicity, it wont run every frame though.
  134. //bublesort worst case is O(n^2), and this may be an infinite loop if cyclic
  135. bool swapped = false;
  136. for (int i = 0; i < len; i++) {
  137. int parent_idx = bonesptr[order[i]].parent;
  138. if (parent_idx < 0)
  139. continue; //do nothing because it has no parent
  140. //swap indices
  141. int parent_order = bonesptr[parent_idx].sort_index;
  142. if (parent_order > i) {
  143. bonesptr[order[i]].sort_index = parent_order;
  144. bonesptr[parent_idx].sort_index = i;
  145. //swap order
  146. SWAP(order[i], order[parent_order]);
  147. swapped = true;
  148. }
  149. }
  150. if (!swapped)
  151. break;
  152. pass_count++;
  153. }
  154. if (pass_count == len * len) {
  155. ERR_PRINT("Skeleton parenthood graph is cyclic");
  156. }
  157. process_order_dirty = false;
  158. }
  159. void Skeleton::_notification(int p_what) {
  160. switch (p_what) {
  161. case NOTIFICATION_ENTER_WORLD: {
  162. if (dirty) {
  163. dirty = false;
  164. _make_dirty(); // property make it dirty
  165. }
  166. } break;
  167. case NOTIFICATION_EXIT_WORLD: {
  168. } break;
  169. case NOTIFICATION_TRANSFORM_CHANGED: {
  170. if (dirty)
  171. break; //will be eventually updated
  172. //if moved, just update transforms
  173. VisualServer *vs = VisualServer::get_singleton();
  174. const Bone *bonesptr = bones.ptr();
  175. int len = bones.size();
  176. Transform global_transform = get_global_transform();
  177. Transform global_transform_inverse = global_transform.affine_inverse();
  178. for (int i = 0; i < len; i++) {
  179. const Bone &b = bonesptr[i];
  180. vs->skeleton_bone_set_transform(skeleton, i, global_transform * (b.transform_final * global_transform_inverse));
  181. }
  182. } break;
  183. case NOTIFICATION_UPDATE_SKELETON: {
  184. VisualServer *vs = VisualServer::get_singleton();
  185. Bone *bonesptr = bones.ptrw();
  186. int len = bones.size();
  187. vs->skeleton_allocate(skeleton, len); // if same size, nothin really happens
  188. _update_process_order();
  189. const int *order = process_order.ptr();
  190. // pose changed, rebuild cache of inverses
  191. if (rest_global_inverse_dirty) {
  192. // calculate global rests and invert them
  193. for (int i = 0; i < len; i++) {
  194. Bone &b = bonesptr[order[i]];
  195. if (b.parent >= 0)
  196. b.rest_global_inverse = bonesptr[b.parent].rest_global_inverse * b.rest;
  197. else
  198. b.rest_global_inverse = b.rest;
  199. }
  200. for (int i = 0; i < len; i++) {
  201. Bone &b = bonesptr[order[i]];
  202. b.rest_global_inverse.affine_invert();
  203. }
  204. rest_global_inverse_dirty = false;
  205. }
  206. Transform global_transform = get_global_transform();
  207. Transform global_transform_inverse = global_transform.affine_inverse();
  208. for (int i = 0; i < len; i++) {
  209. Bone &b = bonesptr[order[i]];
  210. if (b.disable_rest) {
  211. if (b.enabled) {
  212. Transform pose = b.pose;
  213. if (b.custom_pose_enable) {
  214. pose = b.custom_pose * pose;
  215. }
  216. if (b.parent >= 0) {
  217. b.pose_global = bonesptr[b.parent].pose_global * pose;
  218. } else {
  219. b.pose_global = pose;
  220. }
  221. } else {
  222. if (b.parent >= 0) {
  223. b.pose_global = bonesptr[b.parent].pose_global;
  224. } else {
  225. b.pose_global = Transform();
  226. }
  227. }
  228. } else {
  229. if (b.enabled) {
  230. Transform pose = b.pose;
  231. if (b.custom_pose_enable) {
  232. pose = b.custom_pose * pose;
  233. }
  234. if (b.parent >= 0) {
  235. b.pose_global = bonesptr[b.parent].pose_global * (b.rest * pose);
  236. } else {
  237. b.pose_global = b.rest * pose;
  238. }
  239. } else {
  240. if (b.parent >= 0) {
  241. b.pose_global = bonesptr[b.parent].pose_global * b.rest;
  242. } else {
  243. b.pose_global = b.rest;
  244. }
  245. }
  246. }
  247. b.transform_final = b.pose_global * b.rest_global_inverse;
  248. vs->skeleton_bone_set_transform(skeleton, i, global_transform * (b.transform_final * global_transform_inverse));
  249. for (List<uint32_t>::Element *E = b.nodes_bound.front(); E; E = E->next()) {
  250. Object *obj = ObjectDB::get_instance(E->get());
  251. ERR_CONTINUE(!obj);
  252. Spatial *sp = Object::cast_to<Spatial>(obj);
  253. ERR_CONTINUE(!sp);
  254. sp->set_transform(b.pose_global);
  255. }
  256. }
  257. dirty = false;
  258. } break;
  259. }
  260. }
  261. Transform Skeleton::get_bone_transform(int p_bone) const {
  262. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  263. if (dirty)
  264. const_cast<Skeleton *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  265. return bones[p_bone].pose_global * bones[p_bone].rest_global_inverse;
  266. }
  267. void Skeleton::set_bone_global_pose(int p_bone, const Transform &p_pose) {
  268. ERR_FAIL_INDEX(p_bone, bones.size());
  269. if (bones[p_bone].parent == -1) {
  270. set_bone_pose(p_bone, bones[p_bone].rest_global_inverse * p_pose); //fast
  271. } else {
  272. set_bone_pose(p_bone, bones[p_bone].rest.affine_inverse() * (get_bone_global_pose(bones[p_bone].parent).affine_inverse() * p_pose)); //slow
  273. }
  274. }
  275. Transform Skeleton::get_bone_global_pose(int p_bone) const {
  276. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  277. if (dirty)
  278. const_cast<Skeleton *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  279. return bones[p_bone].pose_global;
  280. }
  281. RID Skeleton::get_skeleton() const {
  282. return skeleton;
  283. }
  284. // skeleton creation api
  285. void Skeleton::add_bone(const String &p_name) {
  286. ERR_FAIL_COND(p_name == "" || p_name.find(":") != -1 || p_name.find("/") != -1);
  287. for (int i = 0; i < bones.size(); i++) {
  288. ERR_FAIL_COND(bones[i].name == p_name);
  289. }
  290. Bone b;
  291. b.name = p_name;
  292. bones.push_back(b);
  293. process_order_dirty = true;
  294. rest_global_inverse_dirty = true;
  295. _make_dirty();
  296. update_gizmo();
  297. }
  298. int Skeleton::find_bone(const String &p_name) const {
  299. for (int i = 0; i < bones.size(); i++) {
  300. if (bones[i].name == p_name)
  301. return i;
  302. }
  303. return -1;
  304. }
  305. String Skeleton::get_bone_name(int p_bone) const {
  306. ERR_FAIL_INDEX_V(p_bone, bones.size(), "");
  307. return bones[p_bone].name;
  308. }
  309. bool Skeleton::is_bone_parent_of(int p_bone, int p_parent_bone_id) const {
  310. int parent_of_bone = get_bone_parent(p_bone);
  311. if (-1 == parent_of_bone)
  312. return false;
  313. if (parent_of_bone == p_parent_bone_id)
  314. return true;
  315. return is_bone_parent_of(parent_of_bone, p_parent_bone_id);
  316. }
  317. int Skeleton::get_bone_count() const {
  318. return bones.size();
  319. }
  320. void Skeleton::set_bone_parent(int p_bone, int p_parent) {
  321. ERR_FAIL_INDEX(p_bone, bones.size());
  322. ERR_FAIL_COND(p_parent != -1 && (p_parent < 0));
  323. bones.write[p_bone].parent = p_parent;
  324. rest_global_inverse_dirty = true;
  325. process_order_dirty = true;
  326. _make_dirty();
  327. }
  328. void Skeleton::unparent_bone_and_rest(int p_bone) {
  329. ERR_FAIL_INDEX(p_bone, bones.size());
  330. _update_process_order();
  331. int parent = bones[p_bone].parent;
  332. while (parent >= 0) {
  333. bones.write[p_bone].rest = bones[parent].rest * bones[p_bone].rest;
  334. parent = bones[parent].parent;
  335. }
  336. bones.write[p_bone].parent = -1;
  337. bones.write[p_bone].rest_global_inverse = bones[p_bone].rest.affine_inverse(); //same thing
  338. process_order_dirty = true;
  339. _make_dirty();
  340. }
  341. void Skeleton::set_bone_ignore_animation(int p_bone, bool p_ignore) {
  342. ERR_FAIL_INDEX(p_bone, bones.size());
  343. bones.write[p_bone].ignore_animation = p_ignore;
  344. }
  345. bool Skeleton::is_bone_ignore_animation(int p_bone) const {
  346. ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
  347. return bones[p_bone].ignore_animation;
  348. }
  349. void Skeleton::set_bone_disable_rest(int p_bone, bool p_disable) {
  350. ERR_FAIL_INDEX(p_bone, bones.size());
  351. bones.write[p_bone].disable_rest = p_disable;
  352. }
  353. bool Skeleton::is_bone_rest_disabled(int p_bone) const {
  354. ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
  355. return bones[p_bone].disable_rest;
  356. }
  357. int Skeleton::get_bone_parent(int p_bone) const {
  358. ERR_FAIL_INDEX_V(p_bone, bones.size(), -1);
  359. return bones[p_bone].parent;
  360. }
  361. void Skeleton::set_bone_rest(int p_bone, const Transform &p_rest) {
  362. ERR_FAIL_INDEX(p_bone, bones.size());
  363. bones.write[p_bone].rest = p_rest;
  364. rest_global_inverse_dirty = true;
  365. _make_dirty();
  366. }
  367. Transform Skeleton::get_bone_rest(int p_bone) const {
  368. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  369. return bones[p_bone].rest;
  370. }
  371. void Skeleton::set_bone_enabled(int p_bone, bool p_enabled) {
  372. ERR_FAIL_INDEX(p_bone, bones.size());
  373. bones.write[p_bone].enabled = p_enabled;
  374. rest_global_inverse_dirty = true;
  375. _make_dirty();
  376. }
  377. bool Skeleton::is_bone_enabled(int p_bone) const {
  378. ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
  379. return bones[p_bone].enabled;
  380. }
  381. void Skeleton::bind_child_node_to_bone(int p_bone, Node *p_node) {
  382. ERR_FAIL_NULL(p_node);
  383. ERR_FAIL_INDEX(p_bone, bones.size());
  384. uint32_t id = p_node->get_instance_id();
  385. for (const List<uint32_t>::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) {
  386. if (E->get() == id)
  387. return; // already here
  388. }
  389. bones.write[p_bone].nodes_bound.push_back(id);
  390. }
  391. void Skeleton::unbind_child_node_from_bone(int p_bone, Node *p_node) {
  392. ERR_FAIL_NULL(p_node);
  393. ERR_FAIL_INDEX(p_bone, bones.size());
  394. uint32_t id = p_node->get_instance_id();
  395. bones.write[p_bone].nodes_bound.erase(id);
  396. }
  397. void Skeleton::get_bound_child_nodes_to_bone(int p_bone, List<Node *> *p_bound) const {
  398. ERR_FAIL_INDEX(p_bone, bones.size());
  399. for (const List<uint32_t>::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) {
  400. Object *obj = ObjectDB::get_instance(E->get());
  401. ERR_CONTINUE(!obj);
  402. p_bound->push_back(Object::cast_to<Node>(obj));
  403. }
  404. }
  405. void Skeleton::clear_bones() {
  406. bones.clear();
  407. rest_global_inverse_dirty = true;
  408. process_order_dirty = true;
  409. _make_dirty();
  410. }
  411. // posing api
  412. void Skeleton::set_bone_pose(int p_bone, const Transform &p_pose) {
  413. ERR_FAIL_INDEX(p_bone, bones.size());
  414. ERR_FAIL_COND(!is_inside_tree());
  415. bones.write[p_bone].pose = p_pose;
  416. _make_dirty();
  417. }
  418. Transform Skeleton::get_bone_pose(int p_bone) const {
  419. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  420. return bones[p_bone].pose;
  421. }
  422. void Skeleton::set_bone_custom_pose(int p_bone, const Transform &p_custom_pose) {
  423. ERR_FAIL_INDEX(p_bone, bones.size());
  424. //ERR_FAIL_COND( !is_inside_scene() );
  425. bones.write[p_bone].custom_pose_enable = (p_custom_pose != Transform());
  426. bones.write[p_bone].custom_pose = p_custom_pose;
  427. _make_dirty();
  428. }
  429. Transform Skeleton::get_bone_custom_pose(int p_bone) const {
  430. ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
  431. return bones[p_bone].custom_pose;
  432. }
  433. void Skeleton::_make_dirty() {
  434. if (dirty)
  435. return;
  436. if (!is_inside_tree()) {
  437. dirty = true;
  438. return;
  439. }
  440. MessageQueue::get_singleton()->push_notification(this, NOTIFICATION_UPDATE_SKELETON);
  441. dirty = true;
  442. }
  443. int Skeleton::get_process_order(int p_idx) {
  444. ERR_FAIL_INDEX_V(p_idx, bones.size(), -1);
  445. _update_process_order();
  446. return process_order[p_idx];
  447. }
  448. void Skeleton::localize_rests() {
  449. _update_process_order();
  450. for (int i = bones.size() - 1; i >= 0; i--) {
  451. int idx = process_order[i];
  452. if (bones[idx].parent >= 0) {
  453. set_bone_rest(idx, bones[bones[idx].parent].rest.affine_inverse() * bones[idx].rest);
  454. }
  455. }
  456. }
  457. #ifndef _3D_DISABLED
  458. void Skeleton::bind_physical_bone_to_bone(int p_bone, PhysicalBone *p_physical_bone) {
  459. ERR_FAIL_INDEX(p_bone, bones.size());
  460. ERR_FAIL_COND(bones[p_bone].physical_bone);
  461. ERR_FAIL_COND(!p_physical_bone);
  462. bones.write[p_bone].physical_bone = p_physical_bone;
  463. _rebuild_physical_bones_cache();
  464. }
  465. void Skeleton::unbind_physical_bone_from_bone(int p_bone) {
  466. ERR_FAIL_INDEX(p_bone, bones.size());
  467. bones.write[p_bone].physical_bone = NULL;
  468. _rebuild_physical_bones_cache();
  469. }
  470. PhysicalBone *Skeleton::get_physical_bone(int p_bone) {
  471. ERR_FAIL_INDEX_V(p_bone, bones.size(), NULL);
  472. return bones[p_bone].physical_bone;
  473. }
  474. PhysicalBone *Skeleton::get_physical_bone_parent(int p_bone) {
  475. ERR_FAIL_INDEX_V(p_bone, bones.size(), NULL);
  476. if (bones[p_bone].cache_parent_physical_bone) {
  477. return bones[p_bone].cache_parent_physical_bone;
  478. }
  479. return _get_physical_bone_parent(p_bone);
  480. }
  481. PhysicalBone *Skeleton::_get_physical_bone_parent(int p_bone) {
  482. ERR_FAIL_INDEX_V(p_bone, bones.size(), NULL);
  483. const int parent_bone = bones[p_bone].parent;
  484. if (0 > parent_bone) {
  485. return NULL;
  486. }
  487. PhysicalBone *pb = bones[parent_bone].physical_bone;
  488. if (pb) {
  489. return pb;
  490. } else {
  491. return get_physical_bone_parent(parent_bone);
  492. }
  493. }
  494. void Skeleton::_rebuild_physical_bones_cache() {
  495. const int b_size = bones.size();
  496. for (int i = 0; i < b_size; ++i) {
  497. PhysicalBone *parent_pb = _get_physical_bone_parent(i);
  498. if (parent_pb != bones[i].physical_bone) {
  499. bones.write[i].cache_parent_physical_bone = parent_pb;
  500. if (bones[i].physical_bone)
  501. bones[i].physical_bone->_on_bone_parent_changed();
  502. }
  503. }
  504. }
  505. void _pb_stop_simulation(Node *p_node) {
  506. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  507. _pb_stop_simulation(p_node->get_child(i));
  508. }
  509. PhysicalBone *pb = Object::cast_to<PhysicalBone>(p_node);
  510. if (pb) {
  511. pb->set_simulate_physics(false);
  512. pb->set_static_body(false);
  513. }
  514. }
  515. void Skeleton::physical_bones_stop_simulation() {
  516. _pb_stop_simulation(this);
  517. }
  518. void _pb_start_simulation(const Skeleton *p_skeleton, Node *p_node, const Vector<int> &p_sim_bones) {
  519. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  520. _pb_start_simulation(p_skeleton, p_node->get_child(i), p_sim_bones);
  521. }
  522. PhysicalBone *pb = Object::cast_to<PhysicalBone>(p_node);
  523. if (pb) {
  524. bool sim = false;
  525. for (int i = p_sim_bones.size() - 1; 0 <= i; --i) {
  526. if (p_sim_bones[i] == pb->get_bone_id() || p_skeleton->is_bone_parent_of(pb->get_bone_id(), p_sim_bones[i])) {
  527. sim = true;
  528. break;
  529. }
  530. }
  531. pb->set_simulate_physics(true);
  532. if (sim) {
  533. pb->set_static_body(false);
  534. } else {
  535. pb->set_static_body(true);
  536. }
  537. }
  538. }
  539. void Skeleton::physical_bones_start_simulation_on(const Array &p_bones) {
  540. Vector<int> sim_bones;
  541. if (p_bones.size() <= 0) {
  542. sim_bones.push_back(0); // if no bones is specified, activate ragdoll on full body
  543. } else {
  544. sim_bones.resize(p_bones.size());
  545. int c = 0;
  546. for (int i = sim_bones.size() - 1; 0 <= i; --i) {
  547. if (Variant::STRING == p_bones.get(i).get_type()) {
  548. int bone_id = find_bone(p_bones.get(i));
  549. if (bone_id != -1)
  550. sim_bones.write[c++] = bone_id;
  551. }
  552. }
  553. sim_bones.resize(c);
  554. }
  555. _pb_start_simulation(this, this, sim_bones);
  556. }
  557. void _physical_bones_add_remove_collision_exception(bool p_add, Node *p_node, RID p_exception) {
  558. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  559. _physical_bones_add_remove_collision_exception(p_add, p_node->get_child(i), p_exception);
  560. }
  561. CollisionObject *co = Object::cast_to<CollisionObject>(p_node);
  562. if (co) {
  563. if (p_add) {
  564. PhysicsServer::get_singleton()->body_add_collision_exception(co->get_rid(), p_exception);
  565. } else {
  566. PhysicsServer::get_singleton()->body_remove_collision_exception(co->get_rid(), p_exception);
  567. }
  568. }
  569. }
  570. void Skeleton::physical_bones_add_collision_exception(RID p_exception) {
  571. _physical_bones_add_remove_collision_exception(true, this, p_exception);
  572. }
  573. void Skeleton::physical_bones_remove_collision_exception(RID p_exception) {
  574. _physical_bones_add_remove_collision_exception(false, this, p_exception);
  575. }
  576. #endif // _3D_DISABLED
  577. void Skeleton::_bind_methods() {
  578. ClassDB::bind_method(D_METHOD("add_bone", "name"), &Skeleton::add_bone);
  579. ClassDB::bind_method(D_METHOD("find_bone", "name"), &Skeleton::find_bone);
  580. ClassDB::bind_method(D_METHOD("get_bone_name", "bone_idx"), &Skeleton::get_bone_name);
  581. ClassDB::bind_method(D_METHOD("get_bone_parent", "bone_idx"), &Skeleton::get_bone_parent);
  582. ClassDB::bind_method(D_METHOD("set_bone_parent", "bone_idx", "parent_idx"), &Skeleton::set_bone_parent);
  583. ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton::get_bone_count);
  584. ClassDB::bind_method(D_METHOD("unparent_bone_and_rest", "bone_idx"), &Skeleton::unparent_bone_and_rest);
  585. ClassDB::bind_method(D_METHOD("get_bone_rest", "bone_idx"), &Skeleton::get_bone_rest);
  586. ClassDB::bind_method(D_METHOD("set_bone_rest", "bone_idx", "rest"), &Skeleton::set_bone_rest);
  587. ClassDB::bind_method(D_METHOD("set_bone_disable_rest", "bone_idx", "disable"), &Skeleton::set_bone_disable_rest);
  588. ClassDB::bind_method(D_METHOD("is_bone_rest_disabled", "bone_idx"), &Skeleton::is_bone_rest_disabled);
  589. ClassDB::bind_method(D_METHOD("bind_child_node_to_bone", "bone_idx", "node"), &Skeleton::bind_child_node_to_bone);
  590. ClassDB::bind_method(D_METHOD("unbind_child_node_from_bone", "bone_idx", "node"), &Skeleton::unbind_child_node_from_bone);
  591. ClassDB::bind_method(D_METHOD("get_bound_child_nodes_to_bone", "bone_idx"), &Skeleton::_get_bound_child_nodes_to_bone);
  592. ClassDB::bind_method(D_METHOD("clear_bones"), &Skeleton::clear_bones);
  593. ClassDB::bind_method(D_METHOD("get_bone_pose", "bone_idx"), &Skeleton::get_bone_pose);
  594. ClassDB::bind_method(D_METHOD("set_bone_pose", "bone_idx", "pose"), &Skeleton::set_bone_pose);
  595. ClassDB::bind_method(D_METHOD("set_bone_global_pose", "bone_idx", "pose"), &Skeleton::set_bone_global_pose);
  596. ClassDB::bind_method(D_METHOD("get_bone_global_pose", "bone_idx"), &Skeleton::get_bone_global_pose);
  597. ClassDB::bind_method(D_METHOD("get_bone_custom_pose", "bone_idx"), &Skeleton::get_bone_custom_pose);
  598. ClassDB::bind_method(D_METHOD("set_bone_custom_pose", "bone_idx", "custom_pose"), &Skeleton::set_bone_custom_pose);
  599. ClassDB::bind_method(D_METHOD("get_bone_transform", "bone_idx"), &Skeleton::get_bone_transform);
  600. #ifndef _3D_DISABLED
  601. ClassDB::bind_method(D_METHOD("physical_bones_stop_simulation"), &Skeleton::physical_bones_stop_simulation);
  602. ClassDB::bind_method(D_METHOD("physical_bones_start_simulation", "bones"), &Skeleton::physical_bones_start_simulation_on, DEFVAL(Array()));
  603. ClassDB::bind_method(D_METHOD("physical_bones_add_collision_exception", "exception"), &Skeleton::physical_bones_add_collision_exception);
  604. ClassDB::bind_method(D_METHOD("physical_bones_remove_collision_exception", "exception"), &Skeleton::physical_bones_remove_collision_exception);
  605. #endif // _3D_DISABLED
  606. ClassDB::bind_method(D_METHOD("set_bone_ignore_animation", "bone", "ignore"), &Skeleton::set_bone_ignore_animation);
  607. BIND_CONSTANT(NOTIFICATION_UPDATE_SKELETON);
  608. }
  609. Skeleton::Skeleton() {
  610. rest_global_inverse_dirty = true;
  611. dirty = false;
  612. process_order_dirty = true;
  613. skeleton = VisualServer::get_singleton()->skeleton_create();
  614. set_notify_transform(true);
  615. }
  616. Skeleton::~Skeleton() {
  617. VisualServer::get_singleton()->free(skeleton);
  618. }