2
0

skeleton_3d.cpp 34 KB

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