skeleton_3d.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /**************************************************************************/
  2. /* skeleton_3d.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_3d.h"
  31. #include "core/object/message_queue.h"
  32. #include "core/variant/type_info.h"
  33. #include "scene/3d/physics_body_3d.h"
  34. #include "scene/resources/surface_tool.h"
  35. #include "scene/scene_string_names.h"
  36. void SkinReference::_skin_changed() {
  37. if (skeleton_node) {
  38. skeleton_node->_make_dirty();
  39. }
  40. skeleton_version = 0;
  41. }
  42. void SkinReference::_bind_methods() {
  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. Ref<Skin> SkinReference::get_skin() const {
  50. return skin;
  51. }
  52. SkinReference::~SkinReference() {
  53. ERR_FAIL_NULL(RenderingServer::get_singleton());
  54. if (skeleton_node) {
  55. skeleton_node->skin_bindings.erase(this);
  56. }
  57. RS::get_singleton()->free(skeleton);
  58. }
  59. ///////////////////////////////////////
  60. bool Skeleton3D::_set(const StringName &p_path, const Variant &p_value) {
  61. String path = p_path;
  62. if (!path.begins_with("bones/")) {
  63. return false;
  64. }
  65. int which = path.get_slicec('/', 1).to_int();
  66. String what = path.get_slicec('/', 2);
  67. if (which == bones.size() && what == "name") {
  68. add_bone(p_value);
  69. return true;
  70. }
  71. ERR_FAIL_INDEX_V(which, bones.size(), false);
  72. if (what == "parent") {
  73. set_bone_parent(which, p_value);
  74. } else if (what == "rest") {
  75. set_bone_rest(which, p_value);
  76. } else if (what == "enabled") {
  77. set_bone_enabled(which, p_value);
  78. } else if (what == "position") {
  79. set_bone_pose_position(which, p_value);
  80. } else if (what == "rotation") {
  81. set_bone_pose_rotation(which, p_value);
  82. } else if (what == "scale") {
  83. set_bone_pose_scale(which, p_value);
  84. } else {
  85. return false;
  86. }
  87. return true;
  88. }
  89. bool Skeleton3D::_get(const StringName &p_path, Variant &r_ret) const {
  90. String path = p_path;
  91. if (!path.begins_with("bones/")) {
  92. return false;
  93. }
  94. int which = path.get_slicec('/', 1).to_int();
  95. String what = path.get_slicec('/', 2);
  96. ERR_FAIL_INDEX_V(which, bones.size(), false);
  97. if (what == "name") {
  98. r_ret = get_bone_name(which);
  99. } else if (what == "parent") {
  100. r_ret = get_bone_parent(which);
  101. } else if (what == "rest") {
  102. r_ret = get_bone_rest(which);
  103. } else if (what == "enabled") {
  104. r_ret = is_bone_enabled(which);
  105. } else if (what == "position") {
  106. r_ret = get_bone_pose_position(which);
  107. } else if (what == "rotation") {
  108. r_ret = get_bone_pose_rotation(which);
  109. } else if (what == "scale") {
  110. r_ret = get_bone_pose_scale(which);
  111. } else {
  112. return false;
  113. }
  114. return true;
  115. }
  116. void Skeleton3D::_get_property_list(List<PropertyInfo> *p_list) const {
  117. for (int i = 0; i < bones.size(); i++) {
  118. const String prep = vformat("%s/%d/", PNAME("bones"), i);
  119. p_list->push_back(PropertyInfo(Variant::STRING, prep + PNAME("name"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  120. p_list->push_back(PropertyInfo(Variant::INT, prep + PNAME("parent"), PROPERTY_HINT_RANGE, "-1," + itos(bones.size() - 1) + ",1", PROPERTY_USAGE_NO_EDITOR));
  121. p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prep + PNAME("rest"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  122. p_list->push_back(PropertyInfo(Variant::BOOL, prep + PNAME("enabled"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  123. p_list->push_back(PropertyInfo(Variant::VECTOR3, prep + PNAME("position"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  124. p_list->push_back(PropertyInfo(Variant::QUATERNION, prep + PNAME("rotation"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  125. p_list->push_back(PropertyInfo(Variant::VECTOR3, prep + PNAME("scale"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  126. }
  127. for (PropertyInfo &E : *p_list) {
  128. _validate_property(E);
  129. }
  130. }
  131. void Skeleton3D::_validate_property(PropertyInfo &p_property) const {
  132. PackedStringArray split = p_property.name.split("/");
  133. if (split.size() == 3 && split[0] == "bones") {
  134. if (split[2] == "rest") {
  135. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  136. }
  137. if (is_show_rest_only()) {
  138. if (split[2] == "enabled") {
  139. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  140. }
  141. if (split[2] == "position") {
  142. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  143. }
  144. if (split[2] == "rotation") {
  145. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  146. }
  147. if (split[2] == "scale") {
  148. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  149. }
  150. } else if (!is_bone_enabled(split[1].to_int())) {
  151. if (split[2] == "position") {
  152. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  153. }
  154. if (split[2] == "rotation") {
  155. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  156. }
  157. if (split[2] == "scale") {
  158. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  159. }
  160. }
  161. }
  162. }
  163. void Skeleton3D::_update_process_order() {
  164. if (!process_order_dirty) {
  165. return;
  166. }
  167. Bone *bonesptr = bones.ptrw();
  168. int len = bones.size();
  169. parentless_bones.clear();
  170. for (int i = 0; i < len; i++) {
  171. bonesptr[i].child_bones.clear();
  172. }
  173. for (int i = 0; i < len; i++) {
  174. if (bonesptr[i].parent >= len) {
  175. // Validate this just in case.
  176. ERR_PRINT("Bone " + itos(i) + " has invalid parent: " + itos(bonesptr[i].parent));
  177. bonesptr[i].parent = -1;
  178. }
  179. if (bonesptr[i].parent != -1) {
  180. int parent_bone_idx = bonesptr[i].parent;
  181. // Check to see if this node is already added to the parent.
  182. if (bonesptr[parent_bone_idx].child_bones.find(i) < 0) {
  183. // Add the child node.
  184. bonesptr[parent_bone_idx].child_bones.push_back(i);
  185. } else {
  186. ERR_PRINT("Skeleton3D parenthood graph is cyclic");
  187. }
  188. } else {
  189. parentless_bones.push_back(i);
  190. }
  191. }
  192. process_order_dirty = false;
  193. }
  194. void Skeleton3D::_notification(int p_what) {
  195. switch (p_what) {
  196. case NOTIFICATION_ENTER_TREE: {
  197. if (dirty) {
  198. notification(NOTIFICATION_UPDATE_SKELETON);
  199. }
  200. } break;
  201. case NOTIFICATION_UPDATE_SKELETON: {
  202. RenderingServer *rs = RenderingServer::get_singleton();
  203. Bone *bonesptr = bones.ptrw();
  204. int len = bones.size();
  205. dirty = false;
  206. // Update bone transforms.
  207. force_update_all_bone_transforms();
  208. // Update skins.
  209. for (SkinReference *E : skin_bindings) {
  210. const Skin *skin = E->skin.operator->();
  211. RID skeleton = E->skeleton;
  212. uint32_t bind_count = skin->get_bind_count();
  213. if (E->bind_count != bind_count) {
  214. RS::get_singleton()->skeleton_allocate_data(skeleton, bind_count);
  215. E->bind_count = bind_count;
  216. E->skin_bone_indices.resize(bind_count);
  217. E->skin_bone_indices_ptrs = E->skin_bone_indices.ptrw();
  218. }
  219. if (E->skeleton_version != version) {
  220. for (uint32_t i = 0; i < bind_count; i++) {
  221. StringName bind_name = skin->get_bind_name(i);
  222. if (bind_name != StringName()) {
  223. // Bind name used, use this.
  224. bool found = false;
  225. for (int j = 0; j < len; j++) {
  226. if (bonesptr[j].name == bind_name) {
  227. E->skin_bone_indices_ptrs[i] = j;
  228. found = true;
  229. break;
  230. }
  231. }
  232. if (!found) {
  233. ERR_PRINT("Skin bind #" + itos(i) + " contains named bind '" + String(bind_name) + "' but Skeleton3D has no bone by that name.");
  234. E->skin_bone_indices_ptrs[i] = 0;
  235. }
  236. } else if (skin->get_bind_bone(i) >= 0) {
  237. int bind_index = skin->get_bind_bone(i);
  238. if (bind_index >= len) {
  239. ERR_PRINT("Skin bind #" + itos(i) + " contains bone index bind: " + itos(bind_index) + " , which is greater than the skeleton bone count: " + itos(len) + ".");
  240. E->skin_bone_indices_ptrs[i] = 0;
  241. } else {
  242. E->skin_bone_indices_ptrs[i] = bind_index;
  243. }
  244. } else {
  245. ERR_PRINT("Skin bind #" + itos(i) + " does not contain a name nor a bone index.");
  246. E->skin_bone_indices_ptrs[i] = 0;
  247. }
  248. }
  249. E->skeleton_version = version;
  250. }
  251. for (uint32_t i = 0; i < bind_count; i++) {
  252. uint32_t bone_index = E->skin_bone_indices_ptrs[i];
  253. ERR_CONTINUE(bone_index >= (uint32_t)len);
  254. rs->skeleton_bone_set_transform(skeleton, i, bonesptr[bone_index].pose_global * skin->get_bind_pose(i));
  255. }
  256. }
  257. emit_signal(SceneStringNames::get_singleton()->pose_updated);
  258. } break;
  259. #ifndef _3D_DISABLED
  260. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  261. // This is active only if the skeleton animates the physical bones
  262. // and the state of the bone is not active.
  263. if (animate_physical_bones) {
  264. for (int i = 0; i < bones.size(); i += 1) {
  265. if (bones[i].physical_bone) {
  266. if (bones[i].physical_bone->is_simulating_physics() == false) {
  267. bones[i].physical_bone->reset_to_rest_position();
  268. }
  269. }
  270. }
  271. }
  272. } break;
  273. case NOTIFICATION_READY: {
  274. if (Engine::get_singleton()->is_editor_hint()) {
  275. set_physics_process_internal(true);
  276. }
  277. } break;
  278. #endif // _3D_DISABLED
  279. }
  280. }
  281. void Skeleton3D::clear_bones_global_pose_override() {
  282. for (int i = 0; i < bones.size(); i += 1) {
  283. bones.write[i].global_pose_override_amount = 0;
  284. bones.write[i].global_pose_override_reset = true;
  285. }
  286. _make_dirty();
  287. }
  288. void Skeleton3D::set_bone_global_pose_override(int p_bone, const Transform3D &p_pose, real_t p_amount, bool p_persistent) {
  289. const int bone_size = bones.size();
  290. ERR_FAIL_INDEX(p_bone, bone_size);
  291. bones.write[p_bone].global_pose_override_amount = p_amount;
  292. bones.write[p_bone].global_pose_override = p_pose;
  293. bones.write[p_bone].global_pose_override_reset = !p_persistent;
  294. _make_dirty();
  295. }
  296. Transform3D Skeleton3D::get_bone_global_pose_override(int p_bone) const {
  297. const int bone_size = bones.size();
  298. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  299. return bones[p_bone].global_pose_override;
  300. }
  301. Transform3D Skeleton3D::get_bone_global_pose(int p_bone) const {
  302. const int bone_size = bones.size();
  303. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  304. if (dirty) {
  305. const_cast<Skeleton3D *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  306. }
  307. return bones[p_bone].pose_global;
  308. }
  309. Transform3D Skeleton3D::get_bone_global_pose_no_override(int p_bone) const {
  310. const int bone_size = bones.size();
  311. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  312. if (dirty) {
  313. const_cast<Skeleton3D *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  314. }
  315. return bones[p_bone].pose_global_no_override;
  316. }
  317. void Skeleton3D::set_motion_scale(float p_motion_scale) {
  318. if (p_motion_scale <= 0) {
  319. motion_scale = 1;
  320. ERR_FAIL_MSG("Motion scale must be larger than 0.");
  321. }
  322. motion_scale = p_motion_scale;
  323. }
  324. float Skeleton3D::get_motion_scale() const {
  325. ERR_FAIL_COND_V(motion_scale <= 0, 1);
  326. return motion_scale;
  327. }
  328. // Skeleton creation api
  329. uint64_t Skeleton3D::get_version() const {
  330. return version;
  331. }
  332. void Skeleton3D::add_bone(const String &p_name) {
  333. ERR_FAIL_COND(p_name.is_empty() || p_name.contains(":") || p_name.contains("/") || name_to_bone_index.has(p_name));
  334. Bone b;
  335. b.name = p_name;
  336. bones.push_back(b);
  337. name_to_bone_index.insert(p_name, bones.size() - 1);
  338. process_order_dirty = true;
  339. version++;
  340. rest_dirty = true;
  341. _make_dirty();
  342. update_gizmos();
  343. }
  344. int Skeleton3D::find_bone(const String &p_name) const {
  345. const int *bone_index_ptr = name_to_bone_index.getptr(p_name);
  346. return bone_index_ptr != nullptr ? *bone_index_ptr : -1;
  347. }
  348. String Skeleton3D::get_bone_name(int p_bone) const {
  349. const int bone_size = bones.size();
  350. ERR_FAIL_INDEX_V(p_bone, bone_size, "");
  351. return bones[p_bone].name;
  352. }
  353. void Skeleton3D::set_bone_name(int p_bone, const String &p_name) {
  354. const int bone_size = bones.size();
  355. ERR_FAIL_INDEX(p_bone, bone_size);
  356. const int *bone_index_ptr = name_to_bone_index.getptr(p_name);
  357. if (bone_index_ptr != nullptr) {
  358. ERR_FAIL_COND_MSG(*bone_index_ptr != p_bone, "Skeleton3D: '" + get_name() + "', bone name: '" + p_name + "' already exists.");
  359. return; // No need to rename, the bone already has the given name.
  360. }
  361. name_to_bone_index.erase(bones[p_bone].name);
  362. bones.write[p_bone].name = p_name;
  363. name_to_bone_index.insert(p_name, p_bone);
  364. version++;
  365. }
  366. bool Skeleton3D::is_bone_parent_of(int p_bone, int p_parent_bone_id) const {
  367. int parent_of_bone = get_bone_parent(p_bone);
  368. if (-1 == parent_of_bone) {
  369. return false;
  370. }
  371. if (parent_of_bone == p_parent_bone_id) {
  372. return true;
  373. }
  374. return is_bone_parent_of(parent_of_bone, p_parent_bone_id);
  375. }
  376. int Skeleton3D::get_bone_count() const {
  377. return bones.size();
  378. }
  379. void Skeleton3D::set_bone_parent(int p_bone, int p_parent) {
  380. const int bone_size = bones.size();
  381. ERR_FAIL_INDEX(p_bone, bone_size);
  382. ERR_FAIL_COND(p_parent != -1 && (p_parent < 0));
  383. ERR_FAIL_COND(p_bone == p_parent);
  384. bones.write[p_bone].parent = p_parent;
  385. process_order_dirty = true;
  386. rest_dirty = true;
  387. _make_dirty();
  388. }
  389. void Skeleton3D::unparent_bone_and_rest(int p_bone) {
  390. const int bone_size = bones.size();
  391. ERR_FAIL_INDEX(p_bone, bone_size);
  392. _update_process_order();
  393. int parent = bones[p_bone].parent;
  394. while (parent >= 0) {
  395. bones.write[p_bone].rest = bones[parent].rest * bones[p_bone].rest;
  396. parent = bones[parent].parent;
  397. }
  398. bones.write[p_bone].parent = -1;
  399. process_order_dirty = true;
  400. rest_dirty = true;
  401. _make_dirty();
  402. }
  403. int Skeleton3D::get_bone_parent(int p_bone) const {
  404. const int bone_size = bones.size();
  405. ERR_FAIL_INDEX_V(p_bone, bone_size, -1);
  406. if (process_order_dirty) {
  407. const_cast<Skeleton3D *>(this)->_update_process_order();
  408. }
  409. return bones[p_bone].parent;
  410. }
  411. Vector<int> Skeleton3D::get_bone_children(int p_bone) const {
  412. const int bone_size = bones.size();
  413. ERR_FAIL_INDEX_V(p_bone, bone_size, Vector<int>());
  414. if (process_order_dirty) {
  415. const_cast<Skeleton3D *>(this)->_update_process_order();
  416. }
  417. return bones[p_bone].child_bones;
  418. }
  419. Vector<int> Skeleton3D::get_parentless_bones() const {
  420. if (process_order_dirty) {
  421. const_cast<Skeleton3D *>(this)->_update_process_order();
  422. }
  423. return parentless_bones;
  424. }
  425. void Skeleton3D::set_bone_rest(int p_bone, const Transform3D &p_rest) {
  426. const int bone_size = bones.size();
  427. ERR_FAIL_INDEX(p_bone, bone_size);
  428. bones.write[p_bone].rest = p_rest;
  429. rest_dirty = true;
  430. _make_dirty();
  431. }
  432. Transform3D Skeleton3D::get_bone_rest(int p_bone) const {
  433. const int bone_size = bones.size();
  434. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  435. return bones[p_bone].rest;
  436. }
  437. Transform3D Skeleton3D::get_bone_global_rest(int p_bone) const {
  438. const int bone_size = bones.size();
  439. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  440. if (rest_dirty) {
  441. const_cast<Skeleton3D *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  442. }
  443. return bones[p_bone].global_rest;
  444. }
  445. void Skeleton3D::set_bone_enabled(int p_bone, bool p_enabled) {
  446. const int bone_size = bones.size();
  447. ERR_FAIL_INDEX(p_bone, bone_size);
  448. bones.write[p_bone].enabled = p_enabled;
  449. emit_signal(SceneStringNames::get_singleton()->bone_enabled_changed, p_bone);
  450. _make_dirty();
  451. }
  452. bool Skeleton3D::is_bone_enabled(int p_bone) const {
  453. const int bone_size = bones.size();
  454. ERR_FAIL_INDEX_V(p_bone, bone_size, false);
  455. return bones[p_bone].enabled;
  456. }
  457. void Skeleton3D::set_show_rest_only(bool p_enabled) {
  458. show_rest_only = p_enabled;
  459. emit_signal(SceneStringNames::get_singleton()->show_rest_only_changed);
  460. _make_dirty();
  461. }
  462. bool Skeleton3D::is_show_rest_only() const {
  463. return show_rest_only;
  464. }
  465. void Skeleton3D::clear_bones() {
  466. bones.clear();
  467. name_to_bone_index.clear();
  468. process_order_dirty = true;
  469. version++;
  470. _make_dirty();
  471. }
  472. // Posing api
  473. void Skeleton3D::set_bone_pose_position(int p_bone, const Vector3 &p_position) {
  474. const int bone_size = bones.size();
  475. ERR_FAIL_INDEX(p_bone, bone_size);
  476. bones.write[p_bone].pose_position = p_position;
  477. bones.write[p_bone].pose_cache_dirty = true;
  478. if (is_inside_tree()) {
  479. _make_dirty();
  480. }
  481. }
  482. void Skeleton3D::set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation) {
  483. const int bone_size = bones.size();
  484. ERR_FAIL_INDEX(p_bone, bone_size);
  485. bones.write[p_bone].pose_rotation = p_rotation;
  486. bones.write[p_bone].pose_cache_dirty = true;
  487. if (is_inside_tree()) {
  488. _make_dirty();
  489. }
  490. }
  491. void Skeleton3D::set_bone_pose_scale(int p_bone, const Vector3 &p_scale) {
  492. const int bone_size = bones.size();
  493. ERR_FAIL_INDEX(p_bone, bone_size);
  494. bones.write[p_bone].pose_scale = p_scale;
  495. bones.write[p_bone].pose_cache_dirty = true;
  496. if (is_inside_tree()) {
  497. _make_dirty();
  498. }
  499. }
  500. Vector3 Skeleton3D::get_bone_pose_position(int p_bone) const {
  501. const int bone_size = bones.size();
  502. ERR_FAIL_INDEX_V(p_bone, bone_size, Vector3());
  503. return bones[p_bone].pose_position;
  504. }
  505. Quaternion Skeleton3D::get_bone_pose_rotation(int p_bone) const {
  506. const int bone_size = bones.size();
  507. ERR_FAIL_INDEX_V(p_bone, bone_size, Quaternion());
  508. return bones[p_bone].pose_rotation;
  509. }
  510. Vector3 Skeleton3D::get_bone_pose_scale(int p_bone) const {
  511. const int bone_size = bones.size();
  512. ERR_FAIL_INDEX_V(p_bone, bone_size, Vector3());
  513. return bones[p_bone].pose_scale;
  514. }
  515. void Skeleton3D::reset_bone_pose(int p_bone) {
  516. const int bone_size = bones.size();
  517. ERR_FAIL_INDEX(p_bone, bone_size);
  518. set_bone_pose_position(p_bone, bones[p_bone].rest.origin);
  519. set_bone_pose_rotation(p_bone, bones[p_bone].rest.basis.get_rotation_quaternion());
  520. set_bone_pose_scale(p_bone, bones[p_bone].rest.basis.get_scale());
  521. }
  522. void Skeleton3D::reset_bone_poses() {
  523. for (int i = 0; i < bones.size(); i++) {
  524. reset_bone_pose(i);
  525. }
  526. }
  527. Transform3D Skeleton3D::get_bone_pose(int p_bone) const {
  528. const int bone_size = bones.size();
  529. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  530. ((Skeleton3D *)this)->bones.write[p_bone].update_pose_cache();
  531. return bones[p_bone].pose_cache;
  532. }
  533. void Skeleton3D::_make_dirty() {
  534. if (dirty) {
  535. return;
  536. }
  537. if (is_inside_tree()) {
  538. notify_deferred_thread_group(NOTIFICATION_UPDATE_SKELETON);
  539. }
  540. dirty = true;
  541. }
  542. void Skeleton3D::localize_rests() {
  543. Vector<int> bones_to_process = get_parentless_bones();
  544. while (bones_to_process.size() > 0) {
  545. int current_bone_idx = bones_to_process[0];
  546. bones_to_process.erase(current_bone_idx);
  547. if (bones[current_bone_idx].parent >= 0) {
  548. set_bone_rest(current_bone_idx, bones[bones[current_bone_idx].parent].rest.affine_inverse() * bones[current_bone_idx].rest);
  549. }
  550. // Add the bone's children to the list of bones to be processed.
  551. int child_bone_size = bones[current_bone_idx].child_bones.size();
  552. for (int i = 0; i < child_bone_size; i++) {
  553. bones_to_process.push_back(bones[current_bone_idx].child_bones[i]);
  554. }
  555. }
  556. }
  557. void Skeleton3D::set_animate_physical_bones(bool p_enabled) {
  558. animate_physical_bones = p_enabled;
  559. if (Engine::get_singleton()->is_editor_hint() == false) {
  560. bool sim = false;
  561. for (int i = 0; i < bones.size(); i += 1) {
  562. if (bones[i].physical_bone) {
  563. bones[i].physical_bone->reset_physics_simulation_state();
  564. if (bones[i].physical_bone->is_simulating_physics()) {
  565. sim = true;
  566. }
  567. }
  568. }
  569. set_physics_process_internal(sim == false && p_enabled);
  570. }
  571. }
  572. bool Skeleton3D::get_animate_physical_bones() const {
  573. return animate_physical_bones;
  574. }
  575. void Skeleton3D::bind_physical_bone_to_bone(int p_bone, PhysicalBone3D *p_physical_bone) {
  576. const int bone_size = bones.size();
  577. ERR_FAIL_INDEX(p_bone, bone_size);
  578. ERR_FAIL_COND(bones[p_bone].physical_bone);
  579. ERR_FAIL_NULL(p_physical_bone);
  580. bones.write[p_bone].physical_bone = p_physical_bone;
  581. _rebuild_physical_bones_cache();
  582. }
  583. void Skeleton3D::unbind_physical_bone_from_bone(int p_bone) {
  584. const int bone_size = bones.size();
  585. ERR_FAIL_INDEX(p_bone, bone_size);
  586. bones.write[p_bone].physical_bone = nullptr;
  587. _rebuild_physical_bones_cache();
  588. }
  589. PhysicalBone3D *Skeleton3D::get_physical_bone(int p_bone) {
  590. const int bone_size = bones.size();
  591. ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr);
  592. return bones[p_bone].physical_bone;
  593. }
  594. PhysicalBone3D *Skeleton3D::get_physical_bone_parent(int p_bone) {
  595. const int bone_size = bones.size();
  596. ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr);
  597. if (bones[p_bone].cache_parent_physical_bone) {
  598. return bones[p_bone].cache_parent_physical_bone;
  599. }
  600. return _get_physical_bone_parent(p_bone);
  601. }
  602. PhysicalBone3D *Skeleton3D::_get_physical_bone_parent(int p_bone) {
  603. const int bone_size = bones.size();
  604. ERR_FAIL_INDEX_V(p_bone, bone_size, nullptr);
  605. const int parent_bone = bones[p_bone].parent;
  606. if (0 > parent_bone) {
  607. return nullptr;
  608. }
  609. PhysicalBone3D *pb = bones[parent_bone].physical_bone;
  610. if (pb) {
  611. return pb;
  612. } else {
  613. return get_physical_bone_parent(parent_bone);
  614. }
  615. }
  616. void Skeleton3D::_rebuild_physical_bones_cache() {
  617. const int b_size = bones.size();
  618. for (int i = 0; i < b_size; ++i) {
  619. PhysicalBone3D *parent_pb = _get_physical_bone_parent(i);
  620. if (parent_pb != bones[i].cache_parent_physical_bone) {
  621. bones.write[i].cache_parent_physical_bone = parent_pb;
  622. if (bones[i].physical_bone) {
  623. bones[i].physical_bone->_on_bone_parent_changed();
  624. }
  625. }
  626. }
  627. }
  628. void _pb_stop_simulation(Node *p_node) {
  629. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  630. _pb_stop_simulation(p_node->get_child(i));
  631. }
  632. PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node);
  633. if (pb) {
  634. pb->set_simulate_physics(false);
  635. }
  636. }
  637. void Skeleton3D::physical_bones_stop_simulation() {
  638. _pb_stop_simulation(this);
  639. if (Engine::get_singleton()->is_editor_hint() == false && animate_physical_bones) {
  640. set_physics_process_internal(true);
  641. }
  642. }
  643. void _pb_start_simulation(const Skeleton3D *p_skeleton, Node *p_node, const Vector<int> &p_sim_bones) {
  644. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  645. _pb_start_simulation(p_skeleton, p_node->get_child(i), p_sim_bones);
  646. }
  647. PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node);
  648. if (pb) {
  649. if (p_sim_bones.is_empty()) { // If no bones is specified, activate ragdoll on full body.
  650. pb->set_simulate_physics(true);
  651. } else {
  652. for (int i = p_sim_bones.size() - 1; 0 <= i; --i) {
  653. if (p_sim_bones[i] == pb->get_bone_id() || p_skeleton->is_bone_parent_of(pb->get_bone_id(), p_sim_bones[i])) {
  654. pb->set_simulate_physics(true);
  655. break;
  656. }
  657. }
  658. }
  659. }
  660. }
  661. void Skeleton3D::physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones) {
  662. set_physics_process_internal(false);
  663. Vector<int> sim_bones;
  664. if (p_bones.size() > 0) {
  665. sim_bones.resize(p_bones.size());
  666. int c = 0;
  667. for (int i = sim_bones.size() - 1; 0 <= i; --i) {
  668. int bone_id = find_bone(p_bones[i]);
  669. if (bone_id != -1) {
  670. sim_bones.write[c++] = bone_id;
  671. }
  672. }
  673. sim_bones.resize(c);
  674. }
  675. _pb_start_simulation(this, this, sim_bones);
  676. }
  677. void _physical_bones_add_remove_collision_exception(bool p_add, Node *p_node, RID p_exception) {
  678. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  679. _physical_bones_add_remove_collision_exception(p_add, p_node->get_child(i), p_exception);
  680. }
  681. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_node);
  682. if (co) {
  683. if (p_add) {
  684. PhysicsServer3D::get_singleton()->body_add_collision_exception(co->get_rid(), p_exception);
  685. } else {
  686. PhysicsServer3D::get_singleton()->body_remove_collision_exception(co->get_rid(), p_exception);
  687. }
  688. }
  689. }
  690. void Skeleton3D::physical_bones_add_collision_exception(RID p_exception) {
  691. _physical_bones_add_remove_collision_exception(true, this, p_exception);
  692. }
  693. void Skeleton3D::physical_bones_remove_collision_exception(RID p_exception) {
  694. _physical_bones_add_remove_collision_exception(false, this, p_exception);
  695. }
  696. void Skeleton3D::_skin_changed() {
  697. _make_dirty();
  698. }
  699. Ref<Skin> Skeleton3D::create_skin_from_rest_transforms() {
  700. Ref<Skin> skin;
  701. skin.instantiate();
  702. skin->set_bind_count(bones.size());
  703. // Pose changed, rebuild cache of inverses.
  704. const Bone *bonesptr = bones.ptr();
  705. int len = bones.size();
  706. // Calculate global rests and invert them.
  707. LocalVector<int> bones_to_process;
  708. bones_to_process = get_parentless_bones();
  709. while (bones_to_process.size() > 0) {
  710. int current_bone_idx = bones_to_process[0];
  711. const Bone &b = bonesptr[current_bone_idx];
  712. bones_to_process.erase(current_bone_idx);
  713. LocalVector<int> child_bones_vector;
  714. child_bones_vector = get_bone_children(current_bone_idx);
  715. int child_bones_size = child_bones_vector.size();
  716. if (b.parent < 0) {
  717. skin->set_bind_pose(current_bone_idx, b.rest);
  718. }
  719. for (int i = 0; i < child_bones_size; i++) {
  720. int child_bone_idx = child_bones_vector[i];
  721. const Bone &cb = bonesptr[child_bone_idx];
  722. skin->set_bind_pose(child_bone_idx, skin->get_bind_pose(current_bone_idx) * cb.rest);
  723. // Add the bone's children to the list of bones to be processed.
  724. bones_to_process.push_back(child_bones_vector[i]);
  725. }
  726. }
  727. for (int i = 0; i < len; i++) {
  728. // The inverse is what is actually required.
  729. skin->set_bind_bone(i, i);
  730. skin->set_bind_pose(i, skin->get_bind_pose(i).affine_inverse());
  731. }
  732. return skin;
  733. }
  734. Ref<SkinReference> Skeleton3D::register_skin(const Ref<Skin> &p_skin) {
  735. ERR_FAIL_COND_V(p_skin.is_null(), Ref<SkinReference>());
  736. for (const SkinReference *E : skin_bindings) {
  737. if (E->skin == p_skin) {
  738. return Ref<SkinReference>(E);
  739. }
  740. }
  741. Ref<SkinReference> skin_ref;
  742. skin_ref.instantiate();
  743. skin_ref->skeleton_node = this;
  744. skin_ref->bind_count = 0;
  745. skin_ref->skeleton = RenderingServer::get_singleton()->skeleton_create();
  746. skin_ref->skeleton_node = this;
  747. skin_ref->skin = p_skin;
  748. skin_bindings.insert(skin_ref.operator->());
  749. skin_ref->skin->connect_changed(callable_mp(skin_ref.operator->(), &SkinReference::_skin_changed));
  750. _make_dirty(); // Skin needs to be updated, so update skeleton.
  751. return skin_ref;
  752. }
  753. void Skeleton3D::force_update_all_dirty_bones() {
  754. if (dirty) {
  755. const_cast<Skeleton3D *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
  756. }
  757. }
  758. void Skeleton3D::force_update_all_bone_transforms() {
  759. _update_process_order();
  760. for (int i = 0; i < parentless_bones.size(); i++) {
  761. force_update_bone_children_transforms(parentless_bones[i]);
  762. }
  763. rest_dirty = false;
  764. }
  765. void Skeleton3D::force_update_bone_children_transforms(int p_bone_idx) {
  766. const int bone_size = bones.size();
  767. ERR_FAIL_INDEX(p_bone_idx, bone_size);
  768. Bone *bonesptr = bones.ptrw();
  769. List<int> bones_to_process = List<int>();
  770. bones_to_process.push_back(p_bone_idx);
  771. while (bones_to_process.size() > 0) {
  772. int current_bone_idx = bones_to_process[0];
  773. bones_to_process.erase(current_bone_idx);
  774. Bone &b = bonesptr[current_bone_idx];
  775. bool bone_enabled = b.enabled && !show_rest_only;
  776. if (bone_enabled) {
  777. b.update_pose_cache();
  778. Transform3D pose = b.pose_cache;
  779. if (b.parent >= 0) {
  780. b.pose_global = bonesptr[b.parent].pose_global * pose;
  781. b.pose_global_no_override = bonesptr[b.parent].pose_global_no_override * pose;
  782. } else {
  783. b.pose_global = pose;
  784. b.pose_global_no_override = pose;
  785. }
  786. } else {
  787. if (b.parent >= 0) {
  788. b.pose_global = bonesptr[b.parent].pose_global * b.rest;
  789. b.pose_global_no_override = bonesptr[b.parent].pose_global_no_override * b.rest;
  790. } else {
  791. b.pose_global = b.rest;
  792. b.pose_global_no_override = b.rest;
  793. }
  794. }
  795. if (rest_dirty) {
  796. b.global_rest = b.parent >= 0 ? bonesptr[b.parent].global_rest * b.rest : b.rest;
  797. }
  798. if (b.global_pose_override_amount >= CMP_EPSILON) {
  799. b.pose_global = b.pose_global.interpolate_with(b.global_pose_override, b.global_pose_override_amount);
  800. }
  801. if (b.global_pose_override_reset) {
  802. b.global_pose_override_amount = 0.0;
  803. }
  804. // Add the bone's children to the list of bones to be processed.
  805. int child_bone_size = b.child_bones.size();
  806. for (int i = 0; i < child_bone_size; i++) {
  807. bones_to_process.push_back(b.child_bones[i]);
  808. }
  809. emit_signal(SceneStringNames::get_singleton()->bone_pose_changed, current_bone_idx);
  810. }
  811. }
  812. void Skeleton3D::_bind_methods() {
  813. ClassDB::bind_method(D_METHOD("add_bone", "name"), &Skeleton3D::add_bone);
  814. ClassDB::bind_method(D_METHOD("find_bone", "name"), &Skeleton3D::find_bone);
  815. ClassDB::bind_method(D_METHOD("get_bone_name", "bone_idx"), &Skeleton3D::get_bone_name);
  816. ClassDB::bind_method(D_METHOD("set_bone_name", "bone_idx", "name"), &Skeleton3D::set_bone_name);
  817. ClassDB::bind_method(D_METHOD("get_bone_parent", "bone_idx"), &Skeleton3D::get_bone_parent);
  818. ClassDB::bind_method(D_METHOD("set_bone_parent", "bone_idx", "parent_idx"), &Skeleton3D::set_bone_parent);
  819. ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton3D::get_bone_count);
  820. ClassDB::bind_method(D_METHOD("get_version"), &Skeleton3D::get_version);
  821. ClassDB::bind_method(D_METHOD("unparent_bone_and_rest", "bone_idx"), &Skeleton3D::unparent_bone_and_rest);
  822. ClassDB::bind_method(D_METHOD("get_bone_children", "bone_idx"), &Skeleton3D::get_bone_children);
  823. ClassDB::bind_method(D_METHOD("get_parentless_bones"), &Skeleton3D::get_parentless_bones);
  824. ClassDB::bind_method(D_METHOD("get_bone_rest", "bone_idx"), &Skeleton3D::get_bone_rest);
  825. ClassDB::bind_method(D_METHOD("set_bone_rest", "bone_idx", "rest"), &Skeleton3D::set_bone_rest);
  826. ClassDB::bind_method(D_METHOD("get_bone_global_rest", "bone_idx"), &Skeleton3D::get_bone_global_rest);
  827. ClassDB::bind_method(D_METHOD("create_skin_from_rest_transforms"), &Skeleton3D::create_skin_from_rest_transforms);
  828. ClassDB::bind_method(D_METHOD("register_skin", "skin"), &Skeleton3D::register_skin);
  829. ClassDB::bind_method(D_METHOD("localize_rests"), &Skeleton3D::localize_rests);
  830. ClassDB::bind_method(D_METHOD("clear_bones"), &Skeleton3D::clear_bones);
  831. ClassDB::bind_method(D_METHOD("get_bone_pose", "bone_idx"), &Skeleton3D::get_bone_pose);
  832. ClassDB::bind_method(D_METHOD("set_bone_pose_position", "bone_idx", "position"), &Skeleton3D::set_bone_pose_position);
  833. ClassDB::bind_method(D_METHOD("set_bone_pose_rotation", "bone_idx", "rotation"), &Skeleton3D::set_bone_pose_rotation);
  834. ClassDB::bind_method(D_METHOD("set_bone_pose_scale", "bone_idx", "scale"), &Skeleton3D::set_bone_pose_scale);
  835. ClassDB::bind_method(D_METHOD("get_bone_pose_position", "bone_idx"), &Skeleton3D::get_bone_pose_position);
  836. ClassDB::bind_method(D_METHOD("get_bone_pose_rotation", "bone_idx"), &Skeleton3D::get_bone_pose_rotation);
  837. ClassDB::bind_method(D_METHOD("get_bone_pose_scale", "bone_idx"), &Skeleton3D::get_bone_pose_scale);
  838. ClassDB::bind_method(D_METHOD("reset_bone_pose", "bone_idx"), &Skeleton3D::reset_bone_pose);
  839. ClassDB::bind_method(D_METHOD("reset_bone_poses"), &Skeleton3D::reset_bone_poses);
  840. ClassDB::bind_method(D_METHOD("is_bone_enabled", "bone_idx"), &Skeleton3D::is_bone_enabled);
  841. ClassDB::bind_method(D_METHOD("set_bone_enabled", "bone_idx", "enabled"), &Skeleton3D::set_bone_enabled, DEFVAL(true));
  842. ClassDB::bind_method(D_METHOD("clear_bones_global_pose_override"), &Skeleton3D::clear_bones_global_pose_override);
  843. ClassDB::bind_method(D_METHOD("set_bone_global_pose_override", "bone_idx", "pose", "amount", "persistent"), &Skeleton3D::set_bone_global_pose_override, DEFVAL(false));
  844. ClassDB::bind_method(D_METHOD("get_bone_global_pose_override", "bone_idx"), &Skeleton3D::get_bone_global_pose_override);
  845. ClassDB::bind_method(D_METHOD("get_bone_global_pose", "bone_idx"), &Skeleton3D::get_bone_global_pose);
  846. ClassDB::bind_method(D_METHOD("get_bone_global_pose_no_override", "bone_idx"), &Skeleton3D::get_bone_global_pose_no_override);
  847. ClassDB::bind_method(D_METHOD("force_update_all_bone_transforms"), &Skeleton3D::force_update_all_bone_transforms);
  848. ClassDB::bind_method(D_METHOD("force_update_bone_child_transform", "bone_idx"), &Skeleton3D::force_update_bone_children_transforms);
  849. ClassDB::bind_method(D_METHOD("set_motion_scale", "motion_scale"), &Skeleton3D::set_motion_scale);
  850. ClassDB::bind_method(D_METHOD("get_motion_scale"), &Skeleton3D::get_motion_scale);
  851. ClassDB::bind_method(D_METHOD("set_show_rest_only", "enabled"), &Skeleton3D::set_show_rest_only);
  852. ClassDB::bind_method(D_METHOD("is_show_rest_only"), &Skeleton3D::is_show_rest_only);
  853. ClassDB::bind_method(D_METHOD("set_animate_physical_bones", "enabled"), &Skeleton3D::set_animate_physical_bones);
  854. ClassDB::bind_method(D_METHOD("get_animate_physical_bones"), &Skeleton3D::get_animate_physical_bones);
  855. ClassDB::bind_method(D_METHOD("physical_bones_stop_simulation"), &Skeleton3D::physical_bones_stop_simulation);
  856. ClassDB::bind_method(D_METHOD("physical_bones_start_simulation", "bones"), &Skeleton3D::physical_bones_start_simulation_on, DEFVAL(Array()));
  857. ClassDB::bind_method(D_METHOD("physical_bones_add_collision_exception", "exception"), &Skeleton3D::physical_bones_add_collision_exception);
  858. ClassDB::bind_method(D_METHOD("physical_bones_remove_collision_exception", "exception"), &Skeleton3D::physical_bones_remove_collision_exception);
  859. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "motion_scale", PROPERTY_HINT_RANGE, "0.001,10,0.001,or_greater"), "set_motion_scale", "get_motion_scale");
  860. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_rest_only"), "set_show_rest_only", "is_show_rest_only");
  861. #ifndef _3D_DISABLED
  862. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "animate_physical_bones"), "set_animate_physical_bones", "get_animate_physical_bones");
  863. #endif // _3D_DISABLED
  864. ADD_SIGNAL(MethodInfo("pose_updated"));
  865. ADD_SIGNAL(MethodInfo("bone_pose_changed", PropertyInfo(Variant::INT, "bone_idx")));
  866. ADD_SIGNAL(MethodInfo("bone_enabled_changed", PropertyInfo(Variant::INT, "bone_idx")));
  867. ADD_SIGNAL(MethodInfo("show_rest_only_changed"));
  868. BIND_CONSTANT(NOTIFICATION_UPDATE_SKELETON);
  869. }
  870. Skeleton3D::Skeleton3D() {
  871. }
  872. Skeleton3D::~Skeleton3D() {
  873. // Some skins may remain bound.
  874. for (SkinReference *E : skin_bindings) {
  875. E->skeleton_node = nullptr;
  876. }
  877. }