skeleton_3d.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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 "skeleton_3d.compat.inc"
  32. #include "core/variant/type_info.h"
  33. #include "scene/3d/skeleton_modifier_3d.h"
  34. #include "scene/resources/surface_tool.h"
  35. #ifndef DISABLE_DEPRECATED
  36. #include "scene/3d/physical_bone_simulator_3d.h"
  37. #endif // _DISABLE_DEPRECATED
  38. void SkinReference::_skin_changed() {
  39. if (skeleton_node) {
  40. skeleton_node->_make_dirty();
  41. }
  42. skeleton_version = 0;
  43. }
  44. void SkinReference::_bind_methods() {
  45. ClassDB::bind_method(D_METHOD("get_skeleton"), &SkinReference::get_skeleton);
  46. ClassDB::bind_method(D_METHOD("get_skin"), &SkinReference::get_skin);
  47. }
  48. RID SkinReference::get_skeleton() const {
  49. return skeleton;
  50. }
  51. Ref<Skin> SkinReference::get_skin() const {
  52. return skin;
  53. }
  54. SkinReference::~SkinReference() {
  55. ERR_FAIL_NULL(RenderingServer::get_singleton());
  56. if (skeleton_node) {
  57. skeleton_node->skin_bindings.erase(this);
  58. }
  59. RS::get_singleton()->free(skeleton);
  60. }
  61. ///////////////////////////////////////
  62. bool Skeleton3D::_set(const StringName &p_path, const Variant &p_value) {
  63. #ifndef DISABLE_DEPRECATED
  64. if (p_path == SNAME("animate_physical_bones")) {
  65. set_animate_physical_bones(p_value);
  66. return true;
  67. }
  68. #endif
  69. String path = p_path;
  70. if (!path.begins_with("bones/")) {
  71. return false;
  72. }
  73. uint32_t which = path.get_slicec('/', 1).to_int();
  74. String what = path.get_slicec('/', 2);
  75. if (which == bones.size() && what == "name") {
  76. add_bone(p_value);
  77. return true;
  78. }
  79. ERR_FAIL_UNSIGNED_INDEX_V(which, bones.size(), false);
  80. if (what == "parent") {
  81. set_bone_parent(which, p_value);
  82. } else if (what == "rest") {
  83. set_bone_rest(which, p_value);
  84. } else if (what == "enabled") {
  85. set_bone_enabled(which, p_value);
  86. } else if (what == "position") {
  87. set_bone_pose_position(which, p_value);
  88. } else if (what == "rotation") {
  89. set_bone_pose_rotation(which, p_value);
  90. } else if (what == "scale") {
  91. set_bone_pose_scale(which, p_value);
  92. } else if (what == "bone_meta") {
  93. set_bone_meta(which, path.get_slicec('/', 3), p_value);
  94. #ifndef DISABLE_DEPRECATED
  95. } else if (what == "pose" || what == "bound_children") {
  96. // Kept for compatibility from 3.x to 4.x.
  97. WARN_DEPRECATED_MSG("Skeleton uses old pose format, which is deprecated (and loads slower). Consider re-importing or re-saving the scene." +
  98. (is_inside_tree() ? vformat(" Path: \"%s\"", get_path()) : String()));
  99. if (what == "pose") {
  100. // Old Skeleton poses were relative to rest, new ones are absolute, so we need to recompute the pose.
  101. // Skeleton3D nodes were always written with rest before pose, so this *SHOULD* work...
  102. Transform3D rest = get_bone_rest(which);
  103. Transform3D pose = rest * (Transform3D)p_value;
  104. set_bone_pose_position(which, pose.origin);
  105. set_bone_pose_rotation(which, pose.basis.get_rotation_quaternion());
  106. set_bone_pose_scale(which, pose.basis.get_scale());
  107. } else { // bound_children
  108. // This handles the case where the pose was set to the rest position; the pose property would == Transform() and would not be saved to the scene by default.
  109. // However, the bound_children property was always saved regardless of value, and it was always saved after both pose and rest.
  110. // We don't do anything else with bound_children, as it's not present on Skeleton3D.
  111. Vector3 pos = get_bone_pose_position(which);
  112. Quaternion rot = get_bone_pose_rotation(which);
  113. Vector3 scale = get_bone_pose_scale(which);
  114. Transform3D rest = get_bone_rest(which);
  115. if (rest != Transform3D() && pos == Vector3() && rot == Quaternion() && scale == Vector3(1, 1, 1)) {
  116. set_bone_pose_position(which, rest.origin);
  117. set_bone_pose_rotation(which, rest.basis.get_rotation_quaternion());
  118. set_bone_pose_scale(which, rest.basis.get_scale());
  119. }
  120. }
  121. #endif
  122. } else {
  123. return false;
  124. }
  125. return true;
  126. }
  127. bool Skeleton3D::_get(const StringName &p_path, Variant &r_ret) const {
  128. #ifndef DISABLE_DEPRECATED
  129. if (p_path == SNAME("animate_physical_bones")) {
  130. r_ret = get_animate_physical_bones();
  131. return true;
  132. }
  133. #endif
  134. String path = p_path;
  135. if (!path.begins_with("bones/")) {
  136. return false;
  137. }
  138. uint32_t which = path.get_slicec('/', 1).to_int();
  139. String what = path.get_slicec('/', 2);
  140. ERR_FAIL_UNSIGNED_INDEX_V(which, bones.size(), false);
  141. if (what == "name") {
  142. r_ret = get_bone_name(which);
  143. } else if (what == "parent") {
  144. r_ret = get_bone_parent(which);
  145. } else if (what == "rest") {
  146. r_ret = get_bone_rest(which);
  147. } else if (what == "enabled") {
  148. r_ret = is_bone_enabled(which);
  149. } else if (what == "position") {
  150. r_ret = get_bone_pose_position(which);
  151. } else if (what == "rotation") {
  152. r_ret = get_bone_pose_rotation(which);
  153. } else if (what == "scale") {
  154. r_ret = get_bone_pose_scale(which);
  155. } else if (what == "bone_meta") {
  156. r_ret = get_bone_meta(which, path.get_slicec('/', 3));
  157. } else {
  158. return false;
  159. }
  160. return true;
  161. }
  162. void Skeleton3D::_get_property_list(List<PropertyInfo> *p_list) const {
  163. for (uint32_t i = 0; i < bones.size(); i++) {
  164. const String prep = vformat("%s/%d/", "bones", i);
  165. p_list->push_back(PropertyInfo(Variant::STRING, prep + "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  166. p_list->push_back(PropertyInfo(Variant::INT, prep + "parent", PROPERTY_HINT_RANGE, "-1," + itos(bones.size() - 1) + ",1", PROPERTY_USAGE_NO_EDITOR));
  167. p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prep + "rest", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  168. p_list->push_back(PropertyInfo(Variant::BOOL, prep + "enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  169. p_list->push_back(PropertyInfo(Variant::VECTOR3, prep + "position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  170. p_list->push_back(PropertyInfo(Variant::QUATERNION, prep + "rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  171. p_list->push_back(PropertyInfo(Variant::VECTOR3, prep + "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  172. for (const KeyValue<StringName, Variant> &K : bones[i].metadata) {
  173. PropertyInfo pi = PropertyInfo(bones[i].metadata[K.key].get_type(), prep + "bone_meta/" + K.key, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR);
  174. p_list->push_back(pi);
  175. }
  176. }
  177. for (PropertyInfo &E : *p_list) {
  178. _validate_property(E);
  179. }
  180. }
  181. void Skeleton3D::_validate_property(PropertyInfo &p_property) const {
  182. PackedStringArray split = p_property.name.split("/");
  183. if (split.size() == 3 && split[0] == "bones") {
  184. if (split[2] == "rest") {
  185. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  186. }
  187. if (is_show_rest_only()) {
  188. if (split[2] == "enabled") {
  189. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  190. }
  191. if (split[2] == "position") {
  192. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  193. }
  194. if (split[2] == "rotation") {
  195. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  196. }
  197. if (split[2] == "scale") {
  198. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  199. }
  200. } else if (!is_bone_enabled(split[1].to_int())) {
  201. if (split[2] == "position") {
  202. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  203. }
  204. if (split[2] == "rotation") {
  205. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  206. }
  207. if (split[2] == "scale") {
  208. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  209. }
  210. }
  211. }
  212. }
  213. void Skeleton3D::_update_process_order() {
  214. if (!process_order_dirty) {
  215. return;
  216. }
  217. Bone *bonesptr = bones.ptr();
  218. int len = bones.size();
  219. parentless_bones.clear();
  220. for (int i = 0; i < len; i++) {
  221. bonesptr[i].child_bones.clear();
  222. }
  223. for (int i = 0; i < len; i++) {
  224. if (bonesptr[i].parent >= len) {
  225. // Validate this just in case.
  226. ERR_PRINT("Bone " + itos(i) + " has invalid parent: " + itos(bonesptr[i].parent));
  227. bonesptr[i].parent = -1;
  228. }
  229. if (bonesptr[i].parent != -1) {
  230. int parent_bone_idx = bonesptr[i].parent;
  231. // Check to see if this node is already added to the parent.
  232. if (!bonesptr[parent_bone_idx].child_bones.has(i)) {
  233. // Add the child node.
  234. bonesptr[parent_bone_idx].child_bones.push_back(i);
  235. } else {
  236. ERR_PRINT("Skeleton3D parenthood graph is cyclic");
  237. }
  238. } else {
  239. parentless_bones.push_back(i);
  240. }
  241. }
  242. bones_backup.resize(bones.size());
  243. concatenated_bone_names = StringName();
  244. _update_bones_nested_set();
  245. process_order_dirty = false;
  246. emit_signal("bone_list_changed");
  247. }
  248. void Skeleton3D::_update_bone_names() const {
  249. String names;
  250. for (uint32_t i = 0; i < bones.size(); i++) {
  251. if (i > 0) {
  252. names += ",";
  253. }
  254. names += bones[i].name;
  255. }
  256. concatenated_bone_names = StringName(names);
  257. }
  258. StringName Skeleton3D::get_concatenated_bone_names() const {
  259. if (concatenated_bone_names == StringName()) {
  260. _update_bone_names();
  261. }
  262. return concatenated_bone_names;
  263. }
  264. #ifndef DISABLE_DEPRECATED
  265. void Skeleton3D::setup_simulator() {
  266. if (simulator && simulator->get_parent() == this) {
  267. remove_child(simulator);
  268. simulator->queue_free();
  269. }
  270. PhysicalBoneSimulator3D *sim = memnew(PhysicalBoneSimulator3D);
  271. simulator = sim;
  272. sim->is_compat = true;
  273. sim->set_active(false); // Don't run unneeded process.
  274. add_child(simulator, false, INTERNAL_MODE_BACK);
  275. set_animate_physical_bones(animate_physical_bones);
  276. }
  277. #endif // _DISABLE_DEPRECATED
  278. void Skeleton3D::_notification(int p_what) {
  279. switch (p_what) {
  280. case NOTIFICATION_ENTER_TREE: {
  281. _process_changed();
  282. _make_dirty();
  283. _make_modifiers_dirty();
  284. force_update_all_dirty_bones();
  285. #ifndef DISABLE_DEPRECATED
  286. setup_simulator();
  287. #endif // _DISABLE_DEPRECATED
  288. update_flags = UPDATE_FLAG_POSE;
  289. _notification(NOTIFICATION_UPDATE_SKELETON);
  290. } break;
  291. case NOTIFICATION_UPDATE_SKELETON: {
  292. // Update bone transforms to apply unprocessed poses.
  293. force_update_all_dirty_bones();
  294. updating = true;
  295. Bone *bonesptr = bones.ptr();
  296. int len = bones.size();
  297. thread_local LocalVector<bool> bone_global_pose_dirty_backup;
  298. // Process modifiers.
  299. _find_modifiers();
  300. if (!modifiers.is_empty()) {
  301. // Store unmodified bone poses.
  302. for (uint32_t i = 0; i < bones.size(); i++) {
  303. bones_backup[i].save(bones[i]);
  304. }
  305. // Store dirty flags for global bone poses.
  306. bone_global_pose_dirty_backup = bone_global_pose_dirty;
  307. _process_modifiers();
  308. }
  309. // Abort if pose is not changed.
  310. if (!(update_flags & UPDATE_FLAG_POSE)) {
  311. updating = false;
  312. update_flags = UPDATE_FLAG_NONE;
  313. return;
  314. }
  315. emit_signal(SceneStringName(skeleton_updated));
  316. // Update skins.
  317. RenderingServer *rs = RenderingServer::get_singleton();
  318. for (SkinReference *E : skin_bindings) {
  319. const Skin *skin = E->skin.operator->();
  320. RID skeleton = E->skeleton;
  321. uint32_t bind_count = skin->get_bind_count();
  322. if (E->bind_count != bind_count) {
  323. RS::get_singleton()->skeleton_allocate_data(skeleton, bind_count);
  324. E->bind_count = bind_count;
  325. E->skin_bone_indices.resize(bind_count);
  326. E->skin_bone_indices_ptrs = E->skin_bone_indices.ptrw();
  327. }
  328. if (E->skeleton_version != version) {
  329. for (uint32_t i = 0; i < bind_count; i++) {
  330. StringName bind_name = skin->get_bind_name(i);
  331. if (bind_name != StringName()) {
  332. // Bind name used, use this.
  333. bool found = false;
  334. for (int j = 0; j < len; j++) {
  335. if (bonesptr[j].name == bind_name) {
  336. E->skin_bone_indices_ptrs[i] = j;
  337. found = true;
  338. break;
  339. }
  340. }
  341. if (!found) {
  342. ERR_PRINT("Skin bind #" + itos(i) + " contains named bind '" + String(bind_name) + "' but Skeleton3D has no bone by that name.");
  343. E->skin_bone_indices_ptrs[i] = 0;
  344. }
  345. } else if (skin->get_bind_bone(i) >= 0) {
  346. int bind_index = skin->get_bind_bone(i);
  347. if (bind_index >= len) {
  348. ERR_PRINT("Skin bind #" + itos(i) + " contains bone index bind: " + itos(bind_index) + " , which is greater than the skeleton bone count: " + itos(len) + ".");
  349. E->skin_bone_indices_ptrs[i] = 0;
  350. } else {
  351. E->skin_bone_indices_ptrs[i] = bind_index;
  352. }
  353. } else {
  354. ERR_PRINT("Skin bind #" + itos(i) + " does not contain a name nor a bone index.");
  355. E->skin_bone_indices_ptrs[i] = 0;
  356. }
  357. }
  358. E->skeleton_version = version;
  359. }
  360. for (uint32_t i = 0; i < bind_count; i++) {
  361. uint32_t bone_index = E->skin_bone_indices_ptrs[i];
  362. ERR_CONTINUE(bone_index >= (uint32_t)len);
  363. rs->skeleton_bone_set_transform(skeleton, i, bonesptr[bone_index].global_pose * skin->get_bind_pose(i));
  364. }
  365. }
  366. if (!modifiers.is_empty()) {
  367. // Restore unmodified bone poses.
  368. for (uint32_t i = 0; i < bones.size(); i++) {
  369. bones_backup[i].restore(bones[i]);
  370. }
  371. // Restore dirty flags for global bone poses.
  372. bone_global_pose_dirty = bone_global_pose_dirty_backup;
  373. }
  374. updating = false;
  375. update_flags = UPDATE_FLAG_NONE;
  376. } break;
  377. case NOTIFICATION_INTERNAL_PROCESS:
  378. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  379. _find_modifiers();
  380. if (!modifiers.is_empty()) {
  381. _update_deferred(UPDATE_FLAG_MODIFIER);
  382. }
  383. } break;
  384. }
  385. }
  386. void Skeleton3D::set_modifier_callback_mode_process(Skeleton3D::ModifierCallbackModeProcess p_mode) {
  387. if (modifier_callback_mode_process == p_mode) {
  388. return;
  389. }
  390. modifier_callback_mode_process = p_mode;
  391. _process_changed();
  392. }
  393. Skeleton3D::ModifierCallbackModeProcess Skeleton3D::get_modifier_callback_mode_process() const {
  394. return modifier_callback_mode_process;
  395. }
  396. void Skeleton3D::_process_changed() {
  397. if (modifier_callback_mode_process == MODIFIER_CALLBACK_MODE_PROCESS_IDLE) {
  398. set_process_internal(true);
  399. set_physics_process_internal(false);
  400. } else if (modifier_callback_mode_process == MODIFIER_CALLBACK_MODE_PROCESS_PHYSICS) {
  401. set_process_internal(false);
  402. set_physics_process_internal(true);
  403. }
  404. }
  405. void Skeleton3D::_make_modifiers_dirty() {
  406. modifiers_dirty = true;
  407. _update_deferred(UPDATE_FLAG_MODIFIER);
  408. }
  409. void Skeleton3D::_update_bones_nested_set() {
  410. nested_set_offset_to_bone_index.resize(bones.size());
  411. bone_global_pose_dirty.resize(bones.size());
  412. _make_bone_global_poses_dirty();
  413. int offset = 0;
  414. for (int bone : parentless_bones) {
  415. offset += _update_bone_nested_set(bone, offset);
  416. }
  417. }
  418. int Skeleton3D::_update_bone_nested_set(int p_bone, int p_offset) {
  419. Bone &bone = bones[p_bone];
  420. int offset = p_offset + 1;
  421. int span = 1;
  422. for (int child_bone : bone.child_bones) {
  423. int subspan = _update_bone_nested_set(child_bone, offset);
  424. offset += subspan;
  425. span += subspan;
  426. }
  427. nested_set_offset_to_bone_index[p_offset] = p_bone;
  428. bone.nested_set_offset = p_offset;
  429. bone.nested_set_span = span;
  430. return span;
  431. }
  432. void Skeleton3D::_make_bone_global_poses_dirty() {
  433. for (uint32_t i = 0; i < bone_global_pose_dirty.size(); i++) {
  434. bone_global_pose_dirty[i] = true;
  435. }
  436. }
  437. void Skeleton3D::_make_bone_global_pose_subtree_dirty(int p_bone) {
  438. if (process_order_dirty) {
  439. return;
  440. }
  441. const Bone &bone = bones[p_bone];
  442. int span_offset = bone.nested_set_offset;
  443. // No need to make subtree dirty when bone is already dirty.
  444. if (bone_global_pose_dirty[span_offset]) {
  445. return;
  446. }
  447. // Make global poses of subtree dirty.
  448. int span_end = span_offset + bone.nested_set_span;
  449. for (int i = span_offset; i < span_end; i++) {
  450. bone_global_pose_dirty[i] = true;
  451. }
  452. }
  453. void Skeleton3D::_update_bone_global_pose(int p_bone) {
  454. const int bone_size = bones.size();
  455. ERR_FAIL_INDEX(p_bone, bone_size);
  456. _update_process_order();
  457. // Global pose is already calculated.
  458. int nested_set_offset = bones[p_bone].nested_set_offset;
  459. if (!bone_global_pose_dirty[nested_set_offset]) {
  460. return;
  461. }
  462. thread_local LocalVector<int> bone_list;
  463. bone_list.clear();
  464. Transform3D global_pose;
  465. // Create list of parent bones for which the global pose needs to be recalculated.
  466. for (int bone = p_bone; bone >= 0; bone = bones[bone].parent) {
  467. int offset = bones[bone].nested_set_offset;
  468. // Stop searching when global pose is not dirty.
  469. if (!bone_global_pose_dirty[offset]) {
  470. global_pose = bones[bone].global_pose;
  471. break;
  472. }
  473. bone_list.push_back(bone);
  474. }
  475. // Calculate global poses for all parent bones and the current bone.
  476. for (int i = bone_list.size() - 1; i >= 0; i--) {
  477. int bone_idx = bone_list[i];
  478. Bone &bone = bones[bone_idx];
  479. bool bone_enabled = bone.enabled && !show_rest_only;
  480. Transform3D bone_pose = bone_enabled ? get_bone_pose(bone_idx) : get_bone_rest(bone_idx);
  481. global_pose *= bone_pose;
  482. #ifndef DISABLE_DEPRECATED
  483. if (bone.global_pose_override_amount >= CMP_EPSILON) {
  484. global_pose = global_pose.interpolate_with(bone.global_pose_override, bone.global_pose_override_amount);
  485. }
  486. #endif // _DISABLE_DEPRECATED
  487. bone.global_pose = global_pose;
  488. bone_global_pose_dirty[bone.nested_set_offset] = false;
  489. }
  490. }
  491. Transform3D Skeleton3D::get_bone_global_pose(int p_bone) const {
  492. const int bone_size = bones.size();
  493. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  494. const_cast<Skeleton3D *>(this)->_update_bone_global_pose(p_bone);
  495. return bones[p_bone].global_pose;
  496. }
  497. void Skeleton3D::set_bone_global_pose(int p_bone, const Transform3D &p_pose) {
  498. const int bone_size = bones.size();
  499. ERR_FAIL_INDEX(p_bone, bone_size);
  500. Transform3D pt;
  501. if (bones[p_bone].parent >= 0) {
  502. pt = get_bone_global_pose(bones[p_bone].parent);
  503. }
  504. Transform3D t = pt.affine_inverse() * p_pose;
  505. set_bone_pose(p_bone, t);
  506. }
  507. void Skeleton3D::set_motion_scale(float p_motion_scale) {
  508. if (p_motion_scale <= 0) {
  509. motion_scale = 1;
  510. ERR_FAIL_MSG("Motion scale must be larger than 0.");
  511. }
  512. motion_scale = p_motion_scale;
  513. }
  514. float Skeleton3D::get_motion_scale() const {
  515. ERR_FAIL_COND_V(motion_scale <= 0, 1);
  516. return motion_scale;
  517. }
  518. // Skeleton creation api
  519. uint64_t Skeleton3D::get_version() const {
  520. return version;
  521. }
  522. int Skeleton3D::add_bone(const String &p_name) {
  523. ERR_FAIL_COND_V_MSG(p_name.is_empty() || p_name.contains(":") || p_name.contains("/"), -1, vformat("Bone name cannot be empty or contain ':' or '/'.", p_name));
  524. ERR_FAIL_COND_V_MSG(name_to_bone_index.has(p_name), -1, vformat("Skeleton3D \"%s\" already has a bone with name \"%s\".", to_string(), p_name));
  525. Bone b;
  526. b.name = p_name;
  527. bones.push_back(b);
  528. int new_idx = bones.size() - 1;
  529. name_to_bone_index.insert(p_name, new_idx);
  530. process_order_dirty = true;
  531. version++;
  532. rest_dirty = true;
  533. _make_dirty();
  534. update_gizmos();
  535. return new_idx;
  536. }
  537. int Skeleton3D::find_bone(const String &p_name) const {
  538. const int *bone_index_ptr = name_to_bone_index.getptr(p_name);
  539. return bone_index_ptr != nullptr ? *bone_index_ptr : -1;
  540. }
  541. String Skeleton3D::get_bone_name(int p_bone) const {
  542. const int bone_size = bones.size();
  543. ERR_FAIL_INDEX_V(p_bone, bone_size, "");
  544. return bones[p_bone].name;
  545. }
  546. void Skeleton3D::set_bone_name(int p_bone, const String &p_name) {
  547. const int bone_size = bones.size();
  548. ERR_FAIL_INDEX(p_bone, bone_size);
  549. const int *bone_index_ptr = name_to_bone_index.getptr(p_name);
  550. if (bone_index_ptr != nullptr) {
  551. ERR_FAIL_COND_MSG(*bone_index_ptr != p_bone, "Skeleton3D: '" + get_name() + "', bone name: '" + p_name + "' already exists.");
  552. return; // No need to rename, the bone already has the given name.
  553. }
  554. name_to_bone_index.erase(bones[p_bone].name);
  555. bones[p_bone].name = p_name;
  556. name_to_bone_index.insert(p_name, p_bone);
  557. version++;
  558. }
  559. Variant Skeleton3D::get_bone_meta(int p_bone, const StringName &p_key) const {
  560. const int bone_size = bones.size();
  561. ERR_FAIL_INDEX_V(p_bone, bone_size, Variant());
  562. if (!bones[p_bone].metadata.has(p_key)) {
  563. return Variant();
  564. }
  565. return bones[p_bone].metadata[p_key];
  566. }
  567. TypedArray<StringName> Skeleton3D::_get_bone_meta_list_bind(int p_bone) const {
  568. const int bone_size = bones.size();
  569. ERR_FAIL_INDEX_V(p_bone, bone_size, TypedArray<StringName>());
  570. TypedArray<StringName> _metaret;
  571. for (const KeyValue<StringName, Variant> &K : bones[p_bone].metadata) {
  572. _metaret.push_back(K.key);
  573. }
  574. return _metaret;
  575. }
  576. void Skeleton3D::get_bone_meta_list(int p_bone, List<StringName> *p_list) const {
  577. const int bone_size = bones.size();
  578. ERR_FAIL_INDEX(p_bone, bone_size);
  579. for (const KeyValue<StringName, Variant> &K : bones[p_bone].metadata) {
  580. p_list->push_back(K.key);
  581. }
  582. }
  583. bool Skeleton3D::has_bone_meta(int p_bone, const StringName &p_key) const {
  584. const int bone_size = bones.size();
  585. ERR_FAIL_INDEX_V(p_bone, bone_size, false);
  586. return bones[p_bone].metadata.has(p_key);
  587. }
  588. void Skeleton3D::set_bone_meta(int p_bone, const StringName &p_key, const Variant &p_value) {
  589. const int bone_size = bones.size();
  590. ERR_FAIL_INDEX(p_bone, bone_size);
  591. if (p_value.get_type() == Variant::NIL) {
  592. if (bones[p_bone].metadata.has(p_key)) {
  593. bones[p_bone].metadata.erase(p_key);
  594. }
  595. return;
  596. }
  597. bones[p_bone].metadata.insert(p_key, p_value, false);
  598. }
  599. bool Skeleton3D::is_bone_parent_of(int p_bone, int p_parent_bone_id) const {
  600. int parent_of_bone = get_bone_parent(p_bone);
  601. if (-1 == parent_of_bone) {
  602. return false;
  603. }
  604. if (parent_of_bone == p_parent_bone_id) {
  605. return true;
  606. }
  607. return is_bone_parent_of(parent_of_bone, p_parent_bone_id);
  608. }
  609. int Skeleton3D::get_bone_count() const {
  610. return bones.size();
  611. }
  612. void Skeleton3D::set_bone_parent(int p_bone, int p_parent) {
  613. const int bone_size = bones.size();
  614. ERR_FAIL_INDEX(p_bone, bone_size);
  615. ERR_FAIL_COND(p_parent != -1 && (p_parent < 0));
  616. ERR_FAIL_COND(p_bone == p_parent);
  617. bones[p_bone].parent = p_parent;
  618. process_order_dirty = true;
  619. rest_dirty = true;
  620. _make_dirty();
  621. }
  622. void Skeleton3D::unparent_bone_and_rest(int p_bone) {
  623. const int bone_size = bones.size();
  624. ERR_FAIL_INDEX(p_bone, bone_size);
  625. _update_process_order();
  626. int parent = bones[p_bone].parent;
  627. while (parent >= 0) {
  628. bones[p_bone].rest = bones[parent].rest * bones[p_bone].rest;
  629. parent = bones[parent].parent;
  630. }
  631. bones[p_bone].parent = -1;
  632. process_order_dirty = true;
  633. rest_dirty = true;
  634. _make_dirty();
  635. }
  636. int Skeleton3D::get_bone_parent(int p_bone) const {
  637. const int bone_size = bones.size();
  638. ERR_FAIL_INDEX_V(p_bone, bone_size, -1);
  639. if (process_order_dirty) {
  640. const_cast<Skeleton3D *>(this)->_update_process_order();
  641. }
  642. return bones[p_bone].parent;
  643. }
  644. Vector<int> Skeleton3D::get_bone_children(int p_bone) const {
  645. const int bone_size = bones.size();
  646. ERR_FAIL_INDEX_V(p_bone, bone_size, Vector<int>());
  647. if (process_order_dirty) {
  648. const_cast<Skeleton3D *>(this)->_update_process_order();
  649. }
  650. return bones[p_bone].child_bones;
  651. }
  652. Vector<int> Skeleton3D::get_parentless_bones() const {
  653. if (process_order_dirty) {
  654. const_cast<Skeleton3D *>(this)->_update_process_order();
  655. }
  656. return parentless_bones;
  657. }
  658. void Skeleton3D::set_bone_rest(int p_bone, const Transform3D &p_rest) {
  659. const int bone_size = bones.size();
  660. ERR_FAIL_INDEX(p_bone, bone_size);
  661. bones[p_bone].rest = p_rest;
  662. rest_dirty = true;
  663. _make_dirty();
  664. _make_bone_global_pose_subtree_dirty(p_bone);
  665. }
  666. Transform3D Skeleton3D::get_bone_rest(int p_bone) const {
  667. const int bone_size = bones.size();
  668. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  669. return bones[p_bone].rest;
  670. }
  671. Transform3D Skeleton3D::get_bone_global_rest(int p_bone) const {
  672. const int bone_size = bones.size();
  673. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  674. if (rest_dirty) {
  675. const_cast<Skeleton3D *>(this)->force_update_all_bone_transforms();
  676. }
  677. return bones[p_bone].global_rest;
  678. }
  679. void Skeleton3D::set_bone_enabled(int p_bone, bool p_enabled) {
  680. const int bone_size = bones.size();
  681. ERR_FAIL_INDEX(p_bone, bone_size);
  682. bones[p_bone].enabled = p_enabled;
  683. emit_signal(SceneStringName(bone_enabled_changed), p_bone);
  684. _make_dirty();
  685. _make_bone_global_pose_subtree_dirty(p_bone);
  686. }
  687. bool Skeleton3D::is_bone_enabled(int p_bone) const {
  688. const int bone_size = bones.size();
  689. ERR_FAIL_INDEX_V(p_bone, bone_size, false);
  690. return bones[p_bone].enabled;
  691. }
  692. void Skeleton3D::set_show_rest_only(bool p_enabled) {
  693. show_rest_only = p_enabled;
  694. emit_signal(SceneStringName(show_rest_only_changed));
  695. _make_dirty();
  696. _make_bone_global_poses_dirty();
  697. }
  698. bool Skeleton3D::is_show_rest_only() const {
  699. return show_rest_only;
  700. }
  701. void Skeleton3D::clear_bones() {
  702. bones.clear();
  703. name_to_bone_index.clear();
  704. process_order_dirty = true;
  705. version++;
  706. _make_dirty();
  707. }
  708. // Posing api
  709. void Skeleton3D::set_bone_pose(int p_bone, const Transform3D &p_pose) {
  710. const int bone_size = bones.size();
  711. ERR_FAIL_INDEX(p_bone, bone_size);
  712. bones[p_bone].pose_position = p_pose.origin;
  713. bones[p_bone].pose_rotation = p_pose.basis.get_rotation_quaternion();
  714. bones[p_bone].pose_scale = p_pose.basis.get_scale();
  715. bones[p_bone].pose_cache_dirty = true;
  716. if (is_inside_tree()) {
  717. _make_dirty();
  718. _make_bone_global_pose_subtree_dirty(p_bone);
  719. }
  720. }
  721. void Skeleton3D::set_bone_pose_position(int p_bone, const Vector3 &p_position) {
  722. const int bone_size = bones.size();
  723. ERR_FAIL_INDEX(p_bone, bone_size);
  724. bones[p_bone].pose_position = p_position;
  725. bones[p_bone].pose_cache_dirty = true;
  726. if (is_inside_tree()) {
  727. _make_dirty();
  728. _make_bone_global_pose_subtree_dirty(p_bone);
  729. }
  730. }
  731. void Skeleton3D::set_bone_pose_rotation(int p_bone, const Quaternion &p_rotation) {
  732. const int bone_size = bones.size();
  733. ERR_FAIL_INDEX(p_bone, bone_size);
  734. bones[p_bone].pose_rotation = p_rotation;
  735. bones[p_bone].pose_cache_dirty = true;
  736. if (is_inside_tree()) {
  737. _make_dirty();
  738. _make_bone_global_pose_subtree_dirty(p_bone);
  739. }
  740. }
  741. void Skeleton3D::set_bone_pose_scale(int p_bone, const Vector3 &p_scale) {
  742. const int bone_size = bones.size();
  743. ERR_FAIL_INDEX(p_bone, bone_size);
  744. bones[p_bone].pose_scale = p_scale;
  745. bones[p_bone].pose_cache_dirty = true;
  746. if (is_inside_tree()) {
  747. _make_dirty();
  748. _make_bone_global_pose_subtree_dirty(p_bone);
  749. }
  750. }
  751. Vector3 Skeleton3D::get_bone_pose_position(int p_bone) const {
  752. const int bone_size = bones.size();
  753. ERR_FAIL_INDEX_V(p_bone, bone_size, Vector3());
  754. return bones[p_bone].pose_position;
  755. }
  756. Quaternion Skeleton3D::get_bone_pose_rotation(int p_bone) const {
  757. const int bone_size = bones.size();
  758. ERR_FAIL_INDEX_V(p_bone, bone_size, Quaternion());
  759. return bones[p_bone].pose_rotation;
  760. }
  761. Vector3 Skeleton3D::get_bone_pose_scale(int p_bone) const {
  762. const int bone_size = bones.size();
  763. ERR_FAIL_INDEX_V(p_bone, bone_size, Vector3());
  764. return bones[p_bone].pose_scale;
  765. }
  766. void Skeleton3D::reset_bone_pose(int p_bone) {
  767. const int bone_size = bones.size();
  768. ERR_FAIL_INDEX(p_bone, bone_size);
  769. set_bone_pose_position(p_bone, bones[p_bone].rest.origin);
  770. set_bone_pose_rotation(p_bone, bones[p_bone].rest.basis.get_rotation_quaternion());
  771. set_bone_pose_scale(p_bone, bones[p_bone].rest.basis.get_scale());
  772. }
  773. void Skeleton3D::reset_bone_poses() {
  774. for (uint32_t i = 0; i < bones.size(); i++) {
  775. reset_bone_pose(i);
  776. }
  777. }
  778. Transform3D Skeleton3D::get_bone_pose(int p_bone) const {
  779. const int bone_size = bones.size();
  780. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  781. const_cast<Skeleton3D *>(this)->bones[p_bone].update_pose_cache();
  782. return bones[p_bone].pose_cache;
  783. }
  784. void Skeleton3D::_make_dirty() {
  785. if (dirty) {
  786. return;
  787. }
  788. dirty = true;
  789. _update_deferred();
  790. }
  791. void Skeleton3D::_update_deferred(UpdateFlag p_update_flag) {
  792. if (is_inside_tree()) {
  793. if (update_flags == UPDATE_FLAG_NONE && !updating) {
  794. notify_deferred_thread_group(NOTIFICATION_UPDATE_SKELETON); // It must never be called more than once in a single frame.
  795. }
  796. update_flags |= p_update_flag;
  797. }
  798. }
  799. void Skeleton3D::localize_rests() {
  800. Vector<int> bones_to_process = get_parentless_bones();
  801. while (bones_to_process.size() > 0) {
  802. int current_bone_idx = bones_to_process[0];
  803. bones_to_process.erase(current_bone_idx);
  804. if (bones[current_bone_idx].parent >= 0) {
  805. set_bone_rest(current_bone_idx, bones[bones[current_bone_idx].parent].rest.affine_inverse() * bones[current_bone_idx].rest);
  806. }
  807. // Add the bone's children to the list of bones to be processed.
  808. int child_bone_size = bones[current_bone_idx].child_bones.size();
  809. for (int i = 0; i < child_bone_size; i++) {
  810. bones_to_process.push_back(bones[current_bone_idx].child_bones[i]);
  811. }
  812. }
  813. }
  814. void Skeleton3D::_skin_changed() {
  815. _make_dirty();
  816. }
  817. Ref<Skin> Skeleton3D::create_skin_from_rest_transforms() {
  818. Ref<Skin> skin;
  819. skin.instantiate();
  820. skin->set_bind_count(bones.size());
  821. // Pose changed, rebuild cache of inverses.
  822. const Bone *bonesptr = bones.ptr();
  823. uint32_t len = bones.size();
  824. // Calculate global rests and invert them.
  825. LocalVector<int> bones_to_process;
  826. bones_to_process = get_parentless_bones();
  827. while (bones_to_process.size() > 0) {
  828. int current_bone_idx = bones_to_process[0];
  829. const Bone &b = bonesptr[current_bone_idx];
  830. bones_to_process.erase(current_bone_idx);
  831. LocalVector<int> child_bones_vector;
  832. child_bones_vector = get_bone_children(current_bone_idx);
  833. int child_bones_size = child_bones_vector.size();
  834. if (b.parent < 0) {
  835. skin->set_bind_pose(current_bone_idx, b.rest);
  836. }
  837. for (int i = 0; i < child_bones_size; i++) {
  838. int child_bone_idx = child_bones_vector[i];
  839. const Bone &cb = bonesptr[child_bone_idx];
  840. skin->set_bind_pose(child_bone_idx, skin->get_bind_pose(current_bone_idx) * cb.rest);
  841. // Add the bone's children to the list of bones to be processed.
  842. bones_to_process.push_back(child_bones_vector[i]);
  843. }
  844. }
  845. for (uint32_t i = 0; i < len; i++) {
  846. // The inverse is what is actually required.
  847. skin->set_bind_bone(i, i);
  848. skin->set_bind_pose(i, skin->get_bind_pose(i).affine_inverse());
  849. }
  850. return skin;
  851. }
  852. Ref<SkinReference> Skeleton3D::register_skin(const Ref<Skin> &p_skin) {
  853. ERR_FAIL_COND_V(p_skin.is_null(), Ref<SkinReference>());
  854. for (const SkinReference *E : skin_bindings) {
  855. if (E->skin == p_skin) {
  856. return Ref<SkinReference>(E);
  857. }
  858. }
  859. Ref<SkinReference> skin_ref;
  860. skin_ref.instantiate();
  861. skin_ref->skeleton_node = this;
  862. skin_ref->bind_count = 0;
  863. skin_ref->skeleton = RenderingServer::get_singleton()->skeleton_create();
  864. skin_ref->skeleton_node = this;
  865. skin_ref->skin = p_skin;
  866. skin_bindings.insert(skin_ref.operator->());
  867. skin_ref->skin->connect_changed(callable_mp(skin_ref.operator->(), &SkinReference::_skin_changed));
  868. _make_dirty(); // Skin needs to be updated, so update skeleton.
  869. return skin_ref;
  870. }
  871. void Skeleton3D::force_update_deferred() {
  872. _make_dirty();
  873. }
  874. void Skeleton3D::force_update_all_dirty_bones() {
  875. if (!dirty) {
  876. return;
  877. }
  878. force_update_all_bone_transforms();
  879. }
  880. void Skeleton3D::force_update_all_bone_transforms() {
  881. _update_process_order();
  882. for (int i = 0; i < parentless_bones.size(); i++) {
  883. force_update_bone_children_transforms(parentless_bones[i]);
  884. }
  885. rest_dirty = false;
  886. dirty = false;
  887. if (updating) {
  888. return;
  889. }
  890. emit_signal(SceneStringName(pose_updated));
  891. }
  892. void Skeleton3D::force_update_bone_children_transforms(int p_bone_idx) {
  893. const int bone_size = bones.size();
  894. ERR_FAIL_INDEX(p_bone_idx, bone_size);
  895. Bone *bonesptr = bones.ptr();
  896. // Loop through nested set.
  897. for (int offset = 0; offset < bone_size; offset++) {
  898. if (!bone_global_pose_dirty[offset]) {
  899. continue;
  900. }
  901. int current_bone_idx = nested_set_offset_to_bone_index[offset];
  902. Bone &b = bonesptr[current_bone_idx];
  903. bool bone_enabled = b.enabled && !show_rest_only;
  904. if (bone_enabled) {
  905. b.update_pose_cache();
  906. Transform3D pose = b.pose_cache;
  907. if (b.parent >= 0) {
  908. b.global_pose = bonesptr[b.parent].global_pose * pose;
  909. } else {
  910. b.global_pose = pose;
  911. }
  912. } else {
  913. if (b.parent >= 0) {
  914. b.global_pose = bonesptr[b.parent].global_pose * b.rest;
  915. } else {
  916. b.global_pose = b.rest;
  917. }
  918. }
  919. if (rest_dirty) {
  920. b.global_rest = b.parent >= 0 ? bonesptr[b.parent].global_rest * b.rest : b.rest;
  921. }
  922. #ifndef DISABLE_DEPRECATED
  923. if (bone_enabled) {
  924. Transform3D pose = b.pose_cache;
  925. if (b.parent >= 0) {
  926. b.pose_global_no_override = bonesptr[b.parent].pose_global_no_override * pose;
  927. } else {
  928. b.pose_global_no_override = pose;
  929. }
  930. } else {
  931. if (b.parent >= 0) {
  932. b.pose_global_no_override = bonesptr[b.parent].pose_global_no_override * b.rest;
  933. } else {
  934. b.pose_global_no_override = b.rest;
  935. }
  936. }
  937. if (b.global_pose_override_amount >= CMP_EPSILON) {
  938. b.global_pose = b.global_pose.interpolate_with(b.global_pose_override, b.global_pose_override_amount);
  939. }
  940. if (b.global_pose_override_reset) {
  941. b.global_pose_override_amount = 0.0;
  942. }
  943. #endif // _DISABLE_DEPRECATED
  944. bone_global_pose_dirty[offset] = false;
  945. }
  946. }
  947. void Skeleton3D::_find_modifiers() {
  948. if (!modifiers_dirty) {
  949. return;
  950. }
  951. modifiers.clear();
  952. for (int i = 0; i < get_child_count(); i++) {
  953. SkeletonModifier3D *c = Object::cast_to<SkeletonModifier3D>(get_child(i));
  954. if (c) {
  955. modifiers.push_back(c->get_instance_id());
  956. }
  957. }
  958. modifiers_dirty = false;
  959. }
  960. void Skeleton3D::_process_modifiers() {
  961. for (const ObjectID &oid : modifiers) {
  962. Object *t_obj = ObjectDB::get_instance(oid);
  963. if (!t_obj) {
  964. continue;
  965. }
  966. SkeletonModifier3D *mod = cast_to<SkeletonModifier3D>(t_obj);
  967. if (!mod) {
  968. continue;
  969. }
  970. real_t influence = mod->get_influence();
  971. if (influence < 1.0) {
  972. LocalVector<Transform3D> old_poses;
  973. for (int i = 0; i < get_bone_count(); i++) {
  974. old_poses.push_back(get_bone_pose(i));
  975. }
  976. mod->process_modification();
  977. LocalVector<Transform3D> new_poses;
  978. for (int i = 0; i < get_bone_count(); i++) {
  979. new_poses.push_back(get_bone_pose(i));
  980. }
  981. for (int i = 0; i < get_bone_count(); i++) {
  982. if (old_poses[i] == new_poses[i]) {
  983. continue; // Avoid unneeded calculation.
  984. }
  985. set_bone_pose(i, old_poses[i].interpolate_with(new_poses[i], influence));
  986. }
  987. } else {
  988. mod->process_modification();
  989. }
  990. force_update_all_dirty_bones();
  991. }
  992. }
  993. void Skeleton3D::add_child_notify(Node *p_child) {
  994. if (Object::cast_to<SkeletonModifier3D>(p_child)) {
  995. _make_modifiers_dirty();
  996. }
  997. }
  998. void Skeleton3D::move_child_notify(Node *p_child) {
  999. if (Object::cast_to<SkeletonModifier3D>(p_child)) {
  1000. _make_modifiers_dirty();
  1001. }
  1002. }
  1003. void Skeleton3D::remove_child_notify(Node *p_child) {
  1004. if (Object::cast_to<SkeletonModifier3D>(p_child)) {
  1005. _make_modifiers_dirty();
  1006. }
  1007. }
  1008. void Skeleton3D::_bind_methods() {
  1009. ClassDB::bind_method(D_METHOD("add_bone", "name"), &Skeleton3D::add_bone);
  1010. ClassDB::bind_method(D_METHOD("find_bone", "name"), &Skeleton3D::find_bone);
  1011. ClassDB::bind_method(D_METHOD("get_bone_name", "bone_idx"), &Skeleton3D::get_bone_name);
  1012. ClassDB::bind_method(D_METHOD("set_bone_name", "bone_idx", "name"), &Skeleton3D::set_bone_name);
  1013. ClassDB::bind_method(D_METHOD("get_bone_meta", "bone_idx", "key"), &Skeleton3D::get_bone_meta);
  1014. ClassDB::bind_method(D_METHOD("get_bone_meta_list", "bone_idx"), &Skeleton3D::_get_bone_meta_list_bind);
  1015. ClassDB::bind_method(D_METHOD("has_bone_meta", "bone_idx", "key"), &Skeleton3D::has_bone_meta);
  1016. ClassDB::bind_method(D_METHOD("set_bone_meta", "bone_idx", "key", "value"), &Skeleton3D::set_bone_meta);
  1017. ClassDB::bind_method(D_METHOD("get_concatenated_bone_names"), &Skeleton3D::get_concatenated_bone_names);
  1018. ClassDB::bind_method(D_METHOD("get_bone_parent", "bone_idx"), &Skeleton3D::get_bone_parent);
  1019. ClassDB::bind_method(D_METHOD("set_bone_parent", "bone_idx", "parent_idx"), &Skeleton3D::set_bone_parent);
  1020. ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton3D::get_bone_count);
  1021. ClassDB::bind_method(D_METHOD("get_version"), &Skeleton3D::get_version);
  1022. ClassDB::bind_method(D_METHOD("unparent_bone_and_rest", "bone_idx"), &Skeleton3D::unparent_bone_and_rest);
  1023. ClassDB::bind_method(D_METHOD("get_bone_children", "bone_idx"), &Skeleton3D::get_bone_children);
  1024. ClassDB::bind_method(D_METHOD("get_parentless_bones"), &Skeleton3D::get_parentless_bones);
  1025. ClassDB::bind_method(D_METHOD("get_bone_rest", "bone_idx"), &Skeleton3D::get_bone_rest);
  1026. ClassDB::bind_method(D_METHOD("set_bone_rest", "bone_idx", "rest"), &Skeleton3D::set_bone_rest);
  1027. ClassDB::bind_method(D_METHOD("get_bone_global_rest", "bone_idx"), &Skeleton3D::get_bone_global_rest);
  1028. ClassDB::bind_method(D_METHOD("create_skin_from_rest_transforms"), &Skeleton3D::create_skin_from_rest_transforms);
  1029. ClassDB::bind_method(D_METHOD("register_skin", "skin"), &Skeleton3D::register_skin);
  1030. ClassDB::bind_method(D_METHOD("localize_rests"), &Skeleton3D::localize_rests);
  1031. ClassDB::bind_method(D_METHOD("clear_bones"), &Skeleton3D::clear_bones);
  1032. ClassDB::bind_method(D_METHOD("get_bone_pose", "bone_idx"), &Skeleton3D::get_bone_pose);
  1033. ClassDB::bind_method(D_METHOD("set_bone_pose", "bone_idx", "pose"), &Skeleton3D::set_bone_pose);
  1034. ClassDB::bind_method(D_METHOD("set_bone_pose_position", "bone_idx", "position"), &Skeleton3D::set_bone_pose_position);
  1035. ClassDB::bind_method(D_METHOD("set_bone_pose_rotation", "bone_idx", "rotation"), &Skeleton3D::set_bone_pose_rotation);
  1036. ClassDB::bind_method(D_METHOD("set_bone_pose_scale", "bone_idx", "scale"), &Skeleton3D::set_bone_pose_scale);
  1037. ClassDB::bind_method(D_METHOD("get_bone_pose_position", "bone_idx"), &Skeleton3D::get_bone_pose_position);
  1038. ClassDB::bind_method(D_METHOD("get_bone_pose_rotation", "bone_idx"), &Skeleton3D::get_bone_pose_rotation);
  1039. ClassDB::bind_method(D_METHOD("get_bone_pose_scale", "bone_idx"), &Skeleton3D::get_bone_pose_scale);
  1040. ClassDB::bind_method(D_METHOD("reset_bone_pose", "bone_idx"), &Skeleton3D::reset_bone_pose);
  1041. ClassDB::bind_method(D_METHOD("reset_bone_poses"), &Skeleton3D::reset_bone_poses);
  1042. ClassDB::bind_method(D_METHOD("is_bone_enabled", "bone_idx"), &Skeleton3D::is_bone_enabled);
  1043. ClassDB::bind_method(D_METHOD("set_bone_enabled", "bone_idx", "enabled"), &Skeleton3D::set_bone_enabled, DEFVAL(true));
  1044. ClassDB::bind_method(D_METHOD("get_bone_global_pose", "bone_idx"), &Skeleton3D::get_bone_global_pose);
  1045. ClassDB::bind_method(D_METHOD("set_bone_global_pose", "bone_idx", "pose"), &Skeleton3D::set_bone_global_pose);
  1046. ClassDB::bind_method(D_METHOD("force_update_all_bone_transforms"), &Skeleton3D::force_update_all_bone_transforms);
  1047. ClassDB::bind_method(D_METHOD("force_update_bone_child_transform", "bone_idx"), &Skeleton3D::force_update_bone_children_transforms);
  1048. ClassDB::bind_method(D_METHOD("set_motion_scale", "motion_scale"), &Skeleton3D::set_motion_scale);
  1049. ClassDB::bind_method(D_METHOD("get_motion_scale"), &Skeleton3D::get_motion_scale);
  1050. ClassDB::bind_method(D_METHOD("set_show_rest_only", "enabled"), &Skeleton3D::set_show_rest_only);
  1051. ClassDB::bind_method(D_METHOD("is_show_rest_only"), &Skeleton3D::is_show_rest_only);
  1052. ClassDB::bind_method(D_METHOD("set_modifier_callback_mode_process", "mode"), &Skeleton3D::set_modifier_callback_mode_process);
  1053. ClassDB::bind_method(D_METHOD("get_modifier_callback_mode_process"), &Skeleton3D::get_modifier_callback_mode_process);
  1054. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "motion_scale", PROPERTY_HINT_RANGE, "0.001,10,0.001,or_greater"), "set_motion_scale", "get_motion_scale");
  1055. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_rest_only"), "set_show_rest_only", "is_show_rest_only");
  1056. ADD_GROUP("Modifier", "modifier_");
  1057. ADD_PROPERTY(PropertyInfo(Variant::INT, "modifier_callback_mode_process", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_modifier_callback_mode_process", "get_modifier_callback_mode_process");
  1058. ADD_SIGNAL(MethodInfo("pose_updated"));
  1059. ADD_SIGNAL(MethodInfo("skeleton_updated"));
  1060. ADD_SIGNAL(MethodInfo("bone_enabled_changed", PropertyInfo(Variant::INT, "bone_idx")));
  1061. ADD_SIGNAL(MethodInfo("bone_list_changed"));
  1062. ADD_SIGNAL(MethodInfo("show_rest_only_changed"));
  1063. BIND_CONSTANT(NOTIFICATION_UPDATE_SKELETON);
  1064. BIND_ENUM_CONSTANT(MODIFIER_CALLBACK_MODE_PROCESS_PHYSICS);
  1065. BIND_ENUM_CONSTANT(MODIFIER_CALLBACK_MODE_PROCESS_IDLE);
  1066. #ifndef DISABLE_DEPRECATED
  1067. ClassDB::bind_method(D_METHOD("clear_bones_global_pose_override"), &Skeleton3D::clear_bones_global_pose_override);
  1068. ClassDB::bind_method(D_METHOD("set_bone_global_pose_override", "bone_idx", "pose", "amount", "persistent"), &Skeleton3D::set_bone_global_pose_override, DEFVAL(false));
  1069. ClassDB::bind_method(D_METHOD("get_bone_global_pose_override", "bone_idx"), &Skeleton3D::get_bone_global_pose_override);
  1070. ClassDB::bind_method(D_METHOD("get_bone_global_pose_no_override", "bone_idx"), &Skeleton3D::get_bone_global_pose_no_override);
  1071. ClassDB::bind_method(D_METHOD("set_animate_physical_bones", "enabled"), &Skeleton3D::set_animate_physical_bones);
  1072. ClassDB::bind_method(D_METHOD("get_animate_physical_bones"), &Skeleton3D::get_animate_physical_bones);
  1073. ClassDB::bind_method(D_METHOD("physical_bones_stop_simulation"), &Skeleton3D::physical_bones_stop_simulation);
  1074. ClassDB::bind_method(D_METHOD("physical_bones_start_simulation", "bones"), &Skeleton3D::physical_bones_start_simulation_on, DEFVAL(Array()));
  1075. ClassDB::bind_method(D_METHOD("physical_bones_add_collision_exception", "exception"), &Skeleton3D::physical_bones_add_collision_exception);
  1076. ClassDB::bind_method(D_METHOD("physical_bones_remove_collision_exception", "exception"), &Skeleton3D::physical_bones_remove_collision_exception);
  1077. ADD_GROUP("Deprecated", "");
  1078. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "animate_physical_bones"), "set_animate_physical_bones", "get_animate_physical_bones");
  1079. #endif // _DISABLE_DEPRECATED
  1080. }
  1081. #ifndef DISABLE_DEPRECATED
  1082. void Skeleton3D::clear_bones_global_pose_override() {
  1083. for (uint32_t i = 0; i < bones.size(); i += 1) {
  1084. bones[i].global_pose_override_amount = 0;
  1085. bones[i].global_pose_override_reset = true;
  1086. }
  1087. _make_dirty();
  1088. _make_bone_global_poses_dirty();
  1089. }
  1090. void Skeleton3D::set_bone_global_pose_override(int p_bone, const Transform3D &p_pose, real_t p_amount, bool p_persistent) {
  1091. const int bone_size = bones.size();
  1092. ERR_FAIL_INDEX(p_bone, bone_size);
  1093. bones[p_bone].global_pose_override_amount = p_amount;
  1094. bones[p_bone].global_pose_override = p_pose;
  1095. bones[p_bone].global_pose_override_reset = !p_persistent;
  1096. _make_dirty();
  1097. _make_bone_global_pose_subtree_dirty(p_bone);
  1098. }
  1099. Transform3D Skeleton3D::get_bone_global_pose_override(int p_bone) const {
  1100. const int bone_size = bones.size();
  1101. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  1102. return bones[p_bone].global_pose_override;
  1103. }
  1104. Transform3D Skeleton3D::get_bone_global_pose_no_override(int p_bone) const {
  1105. const int bone_size = bones.size();
  1106. ERR_FAIL_INDEX_V(p_bone, bone_size, Transform3D());
  1107. const_cast<Skeleton3D *>(this)->force_update_all_dirty_bones();
  1108. return bones[p_bone].pose_global_no_override;
  1109. }
  1110. Node *Skeleton3D::get_simulator() {
  1111. return simulator;
  1112. }
  1113. void Skeleton3D::set_animate_physical_bones(bool p_enabled) {
  1114. animate_physical_bones = p_enabled;
  1115. PhysicalBoneSimulator3D *sim = cast_to<PhysicalBoneSimulator3D>(simulator);
  1116. if (!sim) {
  1117. return;
  1118. }
  1119. sim->set_active(animate_physical_bones || sim->is_simulating_physics());
  1120. }
  1121. bool Skeleton3D::get_animate_physical_bones() const {
  1122. return animate_physical_bones;
  1123. }
  1124. void Skeleton3D::physical_bones_stop_simulation() {
  1125. PhysicalBoneSimulator3D *sim = cast_to<PhysicalBoneSimulator3D>(simulator);
  1126. if (!sim) {
  1127. return;
  1128. }
  1129. sim->physical_bones_stop_simulation();
  1130. sim->set_active(animate_physical_bones || sim->is_simulating_physics());
  1131. }
  1132. void Skeleton3D::physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones) {
  1133. PhysicalBoneSimulator3D *sim = cast_to<PhysicalBoneSimulator3D>(simulator);
  1134. if (!sim) {
  1135. return;
  1136. }
  1137. sim->set_active(true);
  1138. sim->physical_bones_start_simulation_on(p_bones);
  1139. }
  1140. void Skeleton3D::physical_bones_add_collision_exception(RID p_exception) {
  1141. PhysicalBoneSimulator3D *sim = cast_to<PhysicalBoneSimulator3D>(simulator);
  1142. if (!sim) {
  1143. return;
  1144. }
  1145. sim->physical_bones_add_collision_exception(p_exception);
  1146. }
  1147. void Skeleton3D::physical_bones_remove_collision_exception(RID p_exception) {
  1148. PhysicalBoneSimulator3D *sim = cast_to<PhysicalBoneSimulator3D>(simulator);
  1149. if (!sim) {
  1150. return;
  1151. }
  1152. sim->physical_bones_remove_collision_exception(p_exception);
  1153. }
  1154. #endif // _DISABLE_DEPRECATED
  1155. Skeleton3D::Skeleton3D() {
  1156. }
  1157. Skeleton3D::~Skeleton3D() {
  1158. // Some skins may remain bound.
  1159. for (SkinReference *E : skin_bindings) {
  1160. E->skeleton_node = nullptr;
  1161. }
  1162. }