skeleton_3d.cpp 34 KB

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