skeleton_3d_editor_plugin.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  1. /**************************************************************************/
  2. /* skeleton_3d_editor_plugin.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_editor_plugin.h"
  31. #include "core/io/resource_saver.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_properties.h"
  34. #include "editor/editor_properties_vector.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/plugins/animation_player_editor_plugin.h"
  40. #include "node_3d_editor_plugin.h"
  41. #include "scene/3d/collision_shape_3d.h"
  42. #include "scene/3d/joint_3d.h"
  43. #include "scene/3d/mesh_instance_3d.h"
  44. #include "scene/3d/physics_body_3d.h"
  45. #include "scene/gui/separator.h"
  46. #include "scene/gui/texture_rect.h"
  47. #include "scene/resources/capsule_shape_3d.h"
  48. #include "scene/resources/skeleton_profile.h"
  49. #include "scene/resources/sphere_shape_3d.h"
  50. #include "scene/resources/surface_tool.h"
  51. #include "scene/scene_string_names.h"
  52. void BoneTransformEditor::create_editors() {
  53. section = memnew(EditorInspectorSection);
  54. section->setup("trf_properties", label, this, Color(0.0f, 0.0f, 0.0f), true);
  55. section->unfold();
  56. add_child(section);
  57. enabled_checkbox = memnew(EditorPropertyCheck());
  58. enabled_checkbox->set_label("Pose Enabled");
  59. enabled_checkbox->set_selectable(false);
  60. enabled_checkbox->connect("property_changed", callable_mp(this, &BoneTransformEditor::_value_changed));
  61. section->get_vbox()->add_child(enabled_checkbox);
  62. // Position property.
  63. position_property = memnew(EditorPropertyVector3());
  64. position_property->setup(-10000, 10000, 0.001f, true);
  65. position_property->set_label("Position");
  66. position_property->set_selectable(false);
  67. position_property->connect("property_changed", callable_mp(this, &BoneTransformEditor::_value_changed));
  68. position_property->connect("property_keyed", callable_mp(this, &BoneTransformEditor::_property_keyed));
  69. section->get_vbox()->add_child(position_property);
  70. // Rotation property.
  71. rotation_property = memnew(EditorPropertyQuaternion());
  72. rotation_property->setup(-10000, 10000, 0.001f, true);
  73. rotation_property->set_label("Rotation");
  74. rotation_property->set_selectable(false);
  75. rotation_property->connect("property_changed", callable_mp(this, &BoneTransformEditor::_value_changed));
  76. rotation_property->connect("property_keyed", callable_mp(this, &BoneTransformEditor::_property_keyed));
  77. section->get_vbox()->add_child(rotation_property);
  78. // Scale property.
  79. scale_property = memnew(EditorPropertyVector3());
  80. scale_property->setup(-10000, 10000, 0.001f, true);
  81. scale_property->set_label("Scale");
  82. scale_property->set_selectable(false);
  83. scale_property->connect("property_changed", callable_mp(this, &BoneTransformEditor::_value_changed));
  84. scale_property->connect("property_keyed", callable_mp(this, &BoneTransformEditor::_property_keyed));
  85. section->get_vbox()->add_child(scale_property);
  86. // Transform/Matrix section.
  87. rest_section = memnew(EditorInspectorSection);
  88. rest_section->setup("trf_properties_transform", "Rest", this, Color(0.0f, 0.0f, 0.0f), true);
  89. section->get_vbox()->add_child(rest_section);
  90. // Transform/Matrix property.
  91. rest_matrix = memnew(EditorPropertyTransform3D());
  92. rest_matrix->setup(-10000, 10000, 0.001f, true);
  93. rest_matrix->set_label("Transform");
  94. rest_matrix->set_selectable(false);
  95. rest_section->get_vbox()->add_child(rest_matrix);
  96. }
  97. void BoneTransformEditor::_notification(int p_what) {
  98. switch (p_what) {
  99. case NOTIFICATION_THEME_CHANGED: {
  100. const Color section_color = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
  101. section->set_bg_color(section_color);
  102. rest_section->set_bg_color(section_color);
  103. } break;
  104. }
  105. }
  106. void BoneTransformEditor::_value_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  107. if (updating) {
  108. return;
  109. }
  110. if (skeleton) {
  111. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  112. undo_redo->create_action(TTR("Set Bone Transform"), UndoRedo::MERGE_ENDS);
  113. undo_redo->add_undo_property(skeleton, p_property, skeleton->get(p_property));
  114. undo_redo->add_do_property(skeleton, p_property, p_value);
  115. undo_redo->commit_action();
  116. }
  117. }
  118. BoneTransformEditor::BoneTransformEditor(Skeleton3D *p_skeleton) :
  119. skeleton(p_skeleton) {
  120. create_editors();
  121. }
  122. void BoneTransformEditor::set_keyable(const bool p_keyable) {
  123. position_property->set_keying(p_keyable);
  124. rotation_property->set_keying(p_keyable);
  125. scale_property->set_keying(p_keyable);
  126. }
  127. void BoneTransformEditor::set_target(const String &p_prop) {
  128. enabled_checkbox->set_object_and_property(skeleton, p_prop + "enabled");
  129. enabled_checkbox->update_property();
  130. position_property->set_object_and_property(skeleton, p_prop + "position");
  131. position_property->update_property();
  132. rotation_property->set_object_and_property(skeleton, p_prop + "rotation");
  133. rotation_property->update_property();
  134. scale_property->set_object_and_property(skeleton, p_prop + "scale");
  135. scale_property->update_property();
  136. rest_matrix->set_object_and_property(skeleton, p_prop + "rest");
  137. rest_matrix->update_property();
  138. }
  139. void BoneTransformEditor::_property_keyed(const String &p_path, bool p_advance) {
  140. AnimationTrackEditor *te = AnimationPlayerEditor::get_singleton()->get_track_editor();
  141. if (!te || !te->has_keying()) {
  142. return;
  143. }
  144. te->_clear_selection();
  145. PackedStringArray split = p_path.split("/");
  146. if (split.size() == 3 && split[0] == "bones") {
  147. int bone_idx = split[1].to_int();
  148. if (split[2] == "position") {
  149. te->insert_transform_key(skeleton, skeleton->get_bone_name(bone_idx), Animation::TYPE_POSITION_3D, (Vector3)skeleton->get(p_path) / skeleton->get_motion_scale());
  150. }
  151. if (split[2] == "rotation") {
  152. te->insert_transform_key(skeleton, skeleton->get_bone_name(bone_idx), Animation::TYPE_ROTATION_3D, skeleton->get(p_path));
  153. }
  154. if (split[2] == "scale") {
  155. te->insert_transform_key(skeleton, skeleton->get_bone_name(bone_idx), Animation::TYPE_SCALE_3D, skeleton->get(p_path));
  156. }
  157. }
  158. }
  159. void BoneTransformEditor::_update_properties() {
  160. if (!skeleton) {
  161. return;
  162. }
  163. int selected = Skeleton3DEditor::get_singleton()->get_selected_bone();
  164. List<PropertyInfo> props;
  165. skeleton->get_property_list(&props);
  166. for (const PropertyInfo &E : props) {
  167. PackedStringArray split = E.name.split("/");
  168. if (split.size() == 3 && split[0] == "bones") {
  169. if (split[1].to_int() == selected) {
  170. if (split[2] == "enabled") {
  171. enabled_checkbox->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY);
  172. enabled_checkbox->update_property();
  173. enabled_checkbox->queue_redraw();
  174. }
  175. if (split[2] == "position") {
  176. position_property->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY);
  177. position_property->update_property();
  178. position_property->queue_redraw();
  179. }
  180. if (split[2] == "rotation") {
  181. rotation_property->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY);
  182. rotation_property->update_property();
  183. rotation_property->queue_redraw();
  184. }
  185. if (split[2] == "scale") {
  186. scale_property->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY);
  187. scale_property->update_property();
  188. scale_property->queue_redraw();
  189. }
  190. if (split[2] == "rest") {
  191. rest_matrix->set_read_only(E.usage & PROPERTY_USAGE_READ_ONLY);
  192. rest_matrix->update_property();
  193. rest_matrix->queue_redraw();
  194. }
  195. }
  196. }
  197. }
  198. }
  199. Skeleton3DEditor *Skeleton3DEditor::singleton = nullptr;
  200. void Skeleton3DEditor::set_keyable(const bool p_keyable) {
  201. keyable = p_keyable;
  202. if (p_keyable) {
  203. animation_hb->show();
  204. } else {
  205. animation_hb->hide();
  206. }
  207. };
  208. void Skeleton3DEditor::set_bone_options_enabled(const bool p_bone_options_enabled) {
  209. skeleton_options->get_popup()->set_item_disabled(SKELETON_OPTION_RESET_SELECTED_POSES, !p_bone_options_enabled);
  210. skeleton_options->get_popup()->set_item_disabled(SKELETON_OPTION_SELECTED_POSES_TO_RESTS, !p_bone_options_enabled);
  211. };
  212. void Skeleton3DEditor::_on_click_skeleton_option(int p_skeleton_option) {
  213. if (!skeleton) {
  214. return;
  215. }
  216. switch (p_skeleton_option) {
  217. case SKELETON_OPTION_RESET_ALL_POSES: {
  218. reset_pose(true);
  219. break;
  220. }
  221. case SKELETON_OPTION_RESET_SELECTED_POSES: {
  222. reset_pose(false);
  223. break;
  224. }
  225. case SKELETON_OPTION_ALL_POSES_TO_RESTS: {
  226. pose_to_rest(true);
  227. break;
  228. }
  229. case SKELETON_OPTION_SELECTED_POSES_TO_RESTS: {
  230. pose_to_rest(false);
  231. break;
  232. }
  233. case SKELETON_OPTION_CREATE_PHYSICAL_SKELETON: {
  234. create_physical_skeleton();
  235. break;
  236. }
  237. case SKELETON_OPTION_EXPORT_SKELETON_PROFILE: {
  238. export_skeleton_profile();
  239. break;
  240. }
  241. }
  242. }
  243. void Skeleton3DEditor::reset_pose(const bool p_all_bones) {
  244. if (!skeleton) {
  245. return;
  246. }
  247. const int bone_count = skeleton->get_bone_count();
  248. if (!bone_count) {
  249. return;
  250. }
  251. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  252. ur->create_action(TTR("Set Bone Transform"), UndoRedo::MERGE_ENDS);
  253. if (p_all_bones) {
  254. for (int i = 0; i < bone_count; i++) {
  255. ur->add_undo_method(skeleton, "set_bone_pose_position", i, skeleton->get_bone_pose_position(i));
  256. ur->add_undo_method(skeleton, "set_bone_pose_rotation", i, skeleton->get_bone_pose_rotation(i));
  257. ur->add_undo_method(skeleton, "set_bone_pose_scale", i, skeleton->get_bone_pose_scale(i));
  258. }
  259. ur->add_do_method(skeleton, "reset_bone_poses");
  260. } else {
  261. // Todo: Do method with multiple bone selection.
  262. if (selected_bone == -1) {
  263. ur->commit_action();
  264. return;
  265. }
  266. ur->add_undo_method(skeleton, "set_bone_pose_position", selected_bone, skeleton->get_bone_pose_position(selected_bone));
  267. ur->add_undo_method(skeleton, "set_bone_pose_rotation", selected_bone, skeleton->get_bone_pose_rotation(selected_bone));
  268. ur->add_undo_method(skeleton, "set_bone_pose_scale", selected_bone, skeleton->get_bone_pose_scale(selected_bone));
  269. ur->add_do_method(skeleton, "reset_bone_pose", selected_bone);
  270. }
  271. ur->commit_action();
  272. }
  273. void Skeleton3DEditor::insert_keys(const bool p_all_bones) {
  274. if (!skeleton) {
  275. return;
  276. }
  277. bool pos_enabled = key_loc_button->is_pressed();
  278. bool rot_enabled = key_rot_button->is_pressed();
  279. bool scl_enabled = key_scale_button->is_pressed();
  280. int bone_len = skeleton->get_bone_count();
  281. Node *root = EditorNode::get_singleton()->get_tree()->get_root();
  282. String path = root->get_path_to(skeleton);
  283. AnimationTrackEditor *te = AnimationPlayerEditor::get_singleton()->get_track_editor();
  284. te->make_insert_queue();
  285. for (int i = 0; i < bone_len; i++) {
  286. const String name = skeleton->get_bone_name(i);
  287. if (name.is_empty()) {
  288. continue;
  289. }
  290. if (pos_enabled && (p_all_bones || te->has_track(skeleton, name, Animation::TYPE_POSITION_3D))) {
  291. te->insert_transform_key(skeleton, name, Animation::TYPE_POSITION_3D, skeleton->get_bone_pose_position(i) / skeleton->get_motion_scale());
  292. }
  293. if (rot_enabled && (p_all_bones || te->has_track(skeleton, name, Animation::TYPE_ROTATION_3D))) {
  294. te->insert_transform_key(skeleton, name, Animation::TYPE_ROTATION_3D, skeleton->get_bone_pose_rotation(i));
  295. }
  296. if (scl_enabled && (p_all_bones || te->has_track(skeleton, name, Animation::TYPE_SCALE_3D))) {
  297. te->insert_transform_key(skeleton, name, Animation::TYPE_SCALE_3D, skeleton->get_bone_pose_scale(i));
  298. }
  299. }
  300. te->commit_insert_queue();
  301. }
  302. void Skeleton3DEditor::pose_to_rest(const bool p_all_bones) {
  303. if (!skeleton) {
  304. return;
  305. }
  306. const int bone_count = skeleton->get_bone_count();
  307. if (!bone_count) {
  308. return;
  309. }
  310. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  311. ur->create_action(TTR("Set Bone Rest"), UndoRedo::MERGE_ENDS);
  312. if (p_all_bones) {
  313. for (int i = 0; i < bone_count; i++) {
  314. ur->add_do_method(skeleton, "set_bone_rest", i, skeleton->get_bone_pose(i));
  315. ur->add_undo_method(skeleton, "set_bone_rest", i, skeleton->get_bone_rest(i));
  316. }
  317. } else {
  318. // Todo: Do method with multiple bone selection.
  319. if (selected_bone == -1) {
  320. ur->commit_action();
  321. return;
  322. }
  323. ur->add_do_method(skeleton, "set_bone_rest", selected_bone, skeleton->get_bone_pose(selected_bone));
  324. ur->add_undo_method(skeleton, "set_bone_rest", selected_bone, skeleton->get_bone_rest(selected_bone));
  325. }
  326. ur->commit_action();
  327. }
  328. void Skeleton3DEditor::create_physical_skeleton() {
  329. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  330. ERR_FAIL_NULL(get_tree());
  331. Node *owner = get_tree()->get_edited_scene_root();
  332. const int bone_count = skeleton->get_bone_count();
  333. if (!bone_count) {
  334. EditorNode::get_singleton()->show_warning(vformat(TTR("Cannot create a physical skeleton for a Skeleton3D node with no bones.")));
  335. return;
  336. }
  337. Vector<BoneInfo> bones_infos;
  338. bones_infos.resize(bone_count);
  339. ur->create_action(TTR("Create physical bones"), UndoRedo::MERGE_ALL);
  340. for (int bone_id = 0; bone_count > bone_id; ++bone_id) {
  341. const int parent = skeleton->get_bone_parent(bone_id);
  342. if (parent < 0) {
  343. bones_infos.write[bone_id].relative_rest = skeleton->get_bone_rest(bone_id);
  344. } else {
  345. const int parent_parent = skeleton->get_bone_parent(parent);
  346. bones_infos.write[bone_id].relative_rest = bones_infos[parent].relative_rest * skeleton->get_bone_rest(bone_id);
  347. // Create physical bone on parent.
  348. if (!bones_infos[parent].physical_bone) {
  349. PhysicalBone3D *physical_bone = create_physical_bone(parent, bone_id, bones_infos);
  350. if (physical_bone && physical_bone->get_child(0)) {
  351. CollisionShape3D *collision_shape = Object::cast_to<CollisionShape3D>(physical_bone->get_child(0));
  352. if (collision_shape) {
  353. bones_infos.write[parent].physical_bone = physical_bone;
  354. ur->add_do_method(skeleton, "add_child", physical_bone);
  355. ur->add_do_method(physical_bone, "set_owner", owner);
  356. ur->add_do_method(collision_shape, "set_owner", owner);
  357. ur->add_do_property(physical_bone, "bone_name", skeleton->get_bone_name(parent));
  358. // Create joint between parent of parent.
  359. if (parent_parent != -1) {
  360. ur->add_do_method(physical_bone, "set_joint_type", PhysicalBone3D::JOINT_TYPE_PIN);
  361. }
  362. ur->add_do_method(Node3DEditor::get_singleton(), SceneStringNames::get_singleton()->_request_gizmo, physical_bone);
  363. ur->add_do_method(Node3DEditor::get_singleton(), SceneStringNames::get_singleton()->_request_gizmo, collision_shape);
  364. ur->add_do_reference(physical_bone);
  365. ur->add_undo_method(skeleton, "remove_child", physical_bone);
  366. }
  367. }
  368. }
  369. }
  370. }
  371. ur->commit_action();
  372. }
  373. PhysicalBone3D *Skeleton3DEditor::create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos) {
  374. const Transform3D child_rest = skeleton->get_bone_rest(bone_child_id);
  375. const real_t half_height(child_rest.origin.length() * 0.5);
  376. const real_t radius(half_height * 0.2);
  377. CapsuleShape3D *bone_shape_capsule = memnew(CapsuleShape3D);
  378. bone_shape_capsule->set_height(half_height * 2);
  379. bone_shape_capsule->set_radius(radius);
  380. CollisionShape3D *bone_shape = memnew(CollisionShape3D);
  381. bone_shape->set_shape(bone_shape_capsule);
  382. bone_shape->set_name("CollisionShape3D");
  383. Transform3D capsule_transform;
  384. capsule_transform.basis.rows[0] = Vector3(1, 0, 0);
  385. capsule_transform.basis.rows[1] = Vector3(0, 0, 1);
  386. capsule_transform.basis.rows[2] = Vector3(0, -1, 0);
  387. bone_shape->set_transform(capsule_transform);
  388. /// Get an up vector not collinear with child rest origin
  389. Vector3 up = Vector3(0, 1, 0);
  390. if (up.cross(child_rest.origin).is_zero_approx()) {
  391. up = Vector3(0, 0, 1);
  392. }
  393. Transform3D body_transform;
  394. body_transform.basis = Basis::looking_at(child_rest.origin, up);
  395. body_transform.origin = body_transform.basis.xform(Vector3(0, 0, -half_height));
  396. Transform3D joint_transform;
  397. joint_transform.origin = Vector3(0, 0, half_height);
  398. PhysicalBone3D *physical_bone = memnew(PhysicalBone3D);
  399. physical_bone->add_child(bone_shape);
  400. physical_bone->set_name("Physical Bone " + skeleton->get_bone_name(bone_id));
  401. physical_bone->set_body_offset(body_transform);
  402. physical_bone->set_joint_offset(joint_transform);
  403. return physical_bone;
  404. }
  405. void Skeleton3DEditor::export_skeleton_profile() {
  406. if (!skeleton->get_bone_count()) {
  407. EditorNode::get_singleton()->show_warning(vformat(TTR("Cannot export a SkeletonProfile for a Skeleton3D node with no bones.")));
  408. return;
  409. }
  410. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  411. file_dialog->set_title(TTR("Export Skeleton Profile As..."));
  412. List<String> exts;
  413. ResourceLoader::get_recognized_extensions_for_type("SkeletonProfile", &exts);
  414. file_dialog->clear_filters();
  415. for (const String &K : exts) {
  416. file_dialog->add_filter("*." + K);
  417. }
  418. file_dialog->popup_file_dialog();
  419. }
  420. void Skeleton3DEditor::_file_selected(const String &p_file) {
  421. // Export SkeletonProfile.
  422. Ref<SkeletonProfile> sp(memnew(SkeletonProfile));
  423. // Build SkeletonProfile.
  424. sp->set_group_size(1);
  425. Vector<Vector2> handle_positions;
  426. Vector2 position_max;
  427. Vector2 position_min;
  428. const int bone_count = skeleton->get_bone_count();
  429. sp->set_bone_size(bone_count);
  430. for (int i = 0; i < bone_count; i++) {
  431. sp->set_bone_name(i, skeleton->get_bone_name(i));
  432. int parent = skeleton->get_bone_parent(i);
  433. if (parent >= 0) {
  434. sp->set_bone_parent(i, skeleton->get_bone_name(parent));
  435. }
  436. sp->set_reference_pose(i, skeleton->get_bone_rest(i));
  437. Transform3D grest = skeleton->get_bone_global_rest(i);
  438. handle_positions.append(Vector2(grest.origin.x, grest.origin.y));
  439. if (i == 0) {
  440. position_max = Vector2(grest.origin.x, grest.origin.y);
  441. position_min = Vector2(grest.origin.x, grest.origin.y);
  442. } else {
  443. position_max.x = MAX(grest.origin.x, position_max.x);
  444. position_max.y = MAX(grest.origin.y, position_max.y);
  445. position_min.x = MIN(grest.origin.x, position_min.x);
  446. position_min.y = MIN(grest.origin.y, position_min.y);
  447. }
  448. }
  449. // Layout handles provisionaly.
  450. Vector2 bound = Vector2(position_max.x - position_min.x, position_max.y - position_min.y);
  451. Vector2 center = Vector2((position_max.x + position_min.x) * 0.5, (position_max.y + position_min.y) * 0.5);
  452. float nrm = MAX(bound.x, bound.y);
  453. if (nrm > 0) {
  454. for (int i = 0; i < bone_count; i++) {
  455. handle_positions.write[i] = (handle_positions[i] - center) / nrm * 0.9;
  456. sp->set_handle_offset(i, Vector2(0.5 + handle_positions[i].x, 0.5 - handle_positions[i].y));
  457. }
  458. }
  459. Error err = ResourceSaver::save(sp, p_file);
  460. if (err != OK) {
  461. EditorNode::get_singleton()->show_warning(vformat(TTR("Error saving file: %s"), p_file));
  462. return;
  463. }
  464. }
  465. Variant Skeleton3DEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  466. TreeItem *selected = joint_tree->get_selected();
  467. if (!selected) {
  468. return Variant();
  469. }
  470. Ref<Texture> icon = selected->get_icon(0);
  471. VBoxContainer *vb = memnew(VBoxContainer);
  472. HBoxContainer *hb = memnew(HBoxContainer);
  473. TextureRect *tf = memnew(TextureRect);
  474. tf->set_texture(icon);
  475. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  476. hb->add_child(tf);
  477. Label *label = memnew(Label(selected->get_text(0)));
  478. hb->add_child(label);
  479. vb->add_child(hb);
  480. hb->set_modulate(Color(1, 1, 1, 1));
  481. set_drag_preview(vb);
  482. Dictionary drag_data;
  483. drag_data["type"] = "nodes";
  484. drag_data["node"] = selected;
  485. return drag_data;
  486. }
  487. bool Skeleton3DEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  488. TreeItem *target = joint_tree->get_item_at_position(p_point);
  489. if (!target) {
  490. return false;
  491. }
  492. const String path = target->get_metadata(0);
  493. if (!path.begins_with("bones/")) {
  494. return false;
  495. }
  496. TreeItem *selected = Object::cast_to<TreeItem>(Dictionary(p_data)["node"]);
  497. if (target == selected) {
  498. return false;
  499. }
  500. const String path2 = target->get_metadata(0);
  501. if (!path2.begins_with("bones/")) {
  502. return false;
  503. }
  504. return true;
  505. }
  506. void Skeleton3DEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  507. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  508. return;
  509. }
  510. TreeItem *target = joint_tree->get_item_at_position(p_point);
  511. TreeItem *selected = Object::cast_to<TreeItem>(Dictionary(p_data)["node"]);
  512. const BoneId target_boneidx = String(target->get_metadata(0)).get_slicec('/', 1).to_int();
  513. const BoneId selected_boneidx = String(selected->get_metadata(0)).get_slicec('/', 1).to_int();
  514. move_skeleton_bone(skeleton->get_path(), selected_boneidx, target_boneidx);
  515. }
  516. void Skeleton3DEditor::move_skeleton_bone(NodePath p_skeleton_path, int32_t p_selected_boneidx, int32_t p_target_boneidx) {
  517. Node *node = get_node_or_null(p_skeleton_path);
  518. Skeleton3D *skeleton_node = Object::cast_to<Skeleton3D>(node);
  519. ERR_FAIL_NULL(skeleton_node);
  520. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  521. ur->create_action(TTR("Set Bone Parentage"));
  522. // If the target is a child of ourselves, we move only *us* and not our children.
  523. if (skeleton_node->is_bone_parent_of(p_target_boneidx, p_selected_boneidx)) {
  524. const BoneId parent_idx = skeleton_node->get_bone_parent(p_selected_boneidx);
  525. const int bone_count = skeleton_node->get_bone_count();
  526. for (BoneId i = 0; i < bone_count; ++i) {
  527. if (skeleton_node->get_bone_parent(i) == p_selected_boneidx) {
  528. ur->add_undo_method(skeleton_node, "set_bone_parent", i, skeleton_node->get_bone_parent(i));
  529. ur->add_do_method(skeleton_node, "set_bone_parent", i, parent_idx);
  530. skeleton_node->set_bone_parent(i, parent_idx);
  531. }
  532. }
  533. }
  534. ur->add_undo_method(skeleton_node, "set_bone_parent", p_selected_boneidx, skeleton_node->get_bone_parent(p_selected_boneidx));
  535. ur->add_do_method(skeleton_node, "set_bone_parent", p_selected_boneidx, p_target_boneidx);
  536. skeleton_node->set_bone_parent(p_selected_boneidx, p_target_boneidx);
  537. update_joint_tree();
  538. ur->commit_action();
  539. }
  540. void Skeleton3DEditor::_joint_tree_selection_changed() {
  541. TreeItem *selected = joint_tree->get_selected();
  542. if (selected) {
  543. const String path = selected->get_metadata(0);
  544. if (!path.begins_with("bones/")) {
  545. return;
  546. }
  547. const int b_idx = path.get_slicec('/', 1).to_int();
  548. selected_bone = b_idx;
  549. if (pose_editor) {
  550. const String bone_path = "bones/" + itos(b_idx) + "/";
  551. pose_editor->set_target(bone_path);
  552. pose_editor->set_keyable(keyable);
  553. }
  554. }
  555. if (pose_editor && pose_editor->is_inside_tree()) {
  556. pose_editor->set_visible(selected);
  557. }
  558. set_bone_options_enabled(selected);
  559. _update_properties();
  560. _update_gizmo_visible();
  561. }
  562. // May be not used with single select mode.
  563. void Skeleton3DEditor::_joint_tree_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
  564. }
  565. void Skeleton3DEditor::_update_properties() {
  566. if (pose_editor) {
  567. pose_editor->_update_properties();
  568. }
  569. Node3DEditor::get_singleton()->update_transform_gizmo();
  570. }
  571. void Skeleton3DEditor::update_joint_tree() {
  572. joint_tree->clear();
  573. if (!skeleton) {
  574. return;
  575. }
  576. TreeItem *root = joint_tree->create_item();
  577. HashMap<int, TreeItem *> items;
  578. items.insert(-1, root);
  579. Ref<Texture> bone_icon = get_editor_theme_icon(SNAME("BoneAttachment3D"));
  580. Vector<int> bones_to_process = skeleton->get_parentless_bones();
  581. while (bones_to_process.size() > 0) {
  582. int current_bone_idx = bones_to_process[0];
  583. bones_to_process.erase(current_bone_idx);
  584. const int parent_idx = skeleton->get_bone_parent(current_bone_idx);
  585. TreeItem *parent_item = items.find(parent_idx)->value;
  586. TreeItem *joint_item = joint_tree->create_item(parent_item);
  587. items.insert(current_bone_idx, joint_item);
  588. joint_item->set_text(0, skeleton->get_bone_name(current_bone_idx));
  589. joint_item->set_icon(0, bone_icon);
  590. joint_item->set_selectable(0, true);
  591. joint_item->set_metadata(0, "bones/" + itos(current_bone_idx));
  592. // Add the bone's children to the list of bones to be processed.
  593. Vector<int> current_bone_child_bones = skeleton->get_bone_children(current_bone_idx);
  594. int child_bone_size = current_bone_child_bones.size();
  595. for (int i = 0; i < child_bone_size; i++) {
  596. bones_to_process.push_back(current_bone_child_bones[i]);
  597. }
  598. }
  599. }
  600. void Skeleton3DEditor::create_editors() {
  601. set_h_size_flags(SIZE_EXPAND_FILL);
  602. set_focus_mode(FOCUS_ALL);
  603. Node3DEditor *ne = Node3DEditor::get_singleton();
  604. AnimationTrackEditor *te = AnimationPlayerEditor::get_singleton()->get_track_editor();
  605. // Create File dialog.
  606. file_dialog = memnew(EditorFileDialog);
  607. file_dialog->connect("file_selected", callable_mp(this, &Skeleton3DEditor::_file_selected));
  608. add_child(file_dialog);
  609. // Create Top Menu Bar.
  610. topmenu_bar = memnew(HBoxContainer);
  611. ne->add_control_to_menu_panel(topmenu_bar);
  612. // Create Skeleton Option in Top Menu Bar.
  613. skeleton_options = memnew(MenuButton);
  614. skeleton_options->set_flat(false);
  615. skeleton_options->set_theme_type_variation("FlatMenuButton");
  616. topmenu_bar->add_child(skeleton_options);
  617. skeleton_options->set_text(TTR("Skeleton3D"));
  618. // Skeleton options.
  619. PopupMenu *p = skeleton_options->get_popup();
  620. p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/reset_all_poses", TTR("Reset All Bone Poses")), SKELETON_OPTION_RESET_ALL_POSES);
  621. p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/reset_selected_poses", TTR("Reset Selected Poses")), SKELETON_OPTION_RESET_SELECTED_POSES);
  622. p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/all_poses_to_rests", TTR("Apply All Poses to Rests")), SKELETON_OPTION_ALL_POSES_TO_RESTS);
  623. p->add_shortcut(ED_SHORTCUT("skeleton_3d_editor/selected_poses_to_rests", TTR("Apply Selected Poses to Rests")), SKELETON_OPTION_SELECTED_POSES_TO_RESTS);
  624. p->add_item(TTR("Create Physical Skeleton"), SKELETON_OPTION_CREATE_PHYSICAL_SKELETON);
  625. p->add_item(TTR("Export Skeleton Profile"), SKELETON_OPTION_EXPORT_SKELETON_PROFILE);
  626. p->connect("id_pressed", callable_mp(this, &Skeleton3DEditor::_on_click_skeleton_option));
  627. set_bone_options_enabled(false);
  628. Vector<Variant> button_binds;
  629. button_binds.resize(1);
  630. edit_mode_button = memnew(Button);
  631. topmenu_bar->add_child(edit_mode_button);
  632. edit_mode_button->set_theme_type_variation("FlatButton");
  633. edit_mode_button->set_toggle_mode(true);
  634. edit_mode_button->set_focus_mode(FOCUS_NONE);
  635. edit_mode_button->set_tooltip_text(TTR("Edit Mode\nShow buttons on joints."));
  636. edit_mode_button->connect("toggled", callable_mp(this, &Skeleton3DEditor::edit_mode_toggled));
  637. edit_mode = false;
  638. if (skeleton) {
  639. skeleton->add_child(handles_mesh_instance);
  640. handles_mesh_instance->set_skeleton_path(NodePath(""));
  641. }
  642. // Keying buttons.
  643. animation_hb = memnew(HBoxContainer);
  644. topmenu_bar->add_child(animation_hb);
  645. animation_hb->add_child(memnew(VSeparator));
  646. animation_hb->hide();
  647. key_loc_button = memnew(Button);
  648. key_loc_button->set_theme_type_variation("FlatButton");
  649. key_loc_button->set_toggle_mode(true);
  650. key_loc_button->set_pressed(false);
  651. key_loc_button->set_focus_mode(FOCUS_NONE);
  652. key_loc_button->set_tooltip_text(TTR("Translation mask for inserting keys."));
  653. animation_hb->add_child(key_loc_button);
  654. key_rot_button = memnew(Button);
  655. key_rot_button->set_theme_type_variation("FlatButton");
  656. key_rot_button->set_toggle_mode(true);
  657. key_rot_button->set_pressed(true);
  658. key_rot_button->set_focus_mode(FOCUS_NONE);
  659. key_rot_button->set_tooltip_text(TTR("Rotation mask for inserting keys."));
  660. animation_hb->add_child(key_rot_button);
  661. key_scale_button = memnew(Button);
  662. key_scale_button->set_theme_type_variation("FlatButton");
  663. key_scale_button->set_toggle_mode(true);
  664. key_scale_button->set_pressed(false);
  665. key_scale_button->set_focus_mode(FOCUS_NONE);
  666. key_scale_button->set_tooltip_text(TTR("Scale mask for inserting keys."));
  667. animation_hb->add_child(key_scale_button);
  668. key_insert_button = memnew(Button);
  669. key_insert_button->set_theme_type_variation("FlatButton");
  670. key_insert_button->set_focus_mode(FOCUS_NONE);
  671. key_insert_button->connect("pressed", callable_mp(this, &Skeleton3DEditor::insert_keys).bind(false));
  672. key_insert_button->set_tooltip_text(TTR("Insert key of bone poses already exist track."));
  673. key_insert_button->set_shortcut(ED_SHORTCUT("skeleton_3d_editor/insert_key_to_existing_tracks", TTR("Insert Key (Existing Tracks)"), Key::INSERT));
  674. animation_hb->add_child(key_insert_button);
  675. key_insert_all_button = memnew(Button);
  676. key_insert_all_button->set_theme_type_variation("FlatButton");
  677. key_insert_all_button->set_focus_mode(FOCUS_NONE);
  678. key_insert_all_button->connect("pressed", callable_mp(this, &Skeleton3DEditor::insert_keys).bind(true));
  679. key_insert_all_button->set_tooltip_text(TTR("Insert key of all bone poses."));
  680. key_insert_all_button->set_shortcut(ED_SHORTCUT("skeleton_3d_editor/insert_key_of_all_bones", TTR("Insert Key (All Bones)"), KeyModifierMask::CMD_OR_CTRL + Key::INSERT));
  681. animation_hb->add_child(key_insert_all_button);
  682. // Bone tree.
  683. bones_section = memnew(EditorInspectorSection);
  684. bones_section->setup("bones", "Bones", skeleton, Color(0.0f, 0.0, 0.0f), true);
  685. add_child(bones_section);
  686. bones_section->unfold();
  687. ScrollContainer *s_con = memnew(ScrollContainer);
  688. s_con->set_h_size_flags(SIZE_EXPAND_FILL);
  689. s_con->set_custom_minimum_size(Size2(1, 350) * EDSCALE);
  690. bones_section->get_vbox()->add_child(s_con);
  691. joint_tree = memnew(Tree);
  692. joint_tree->set_columns(1);
  693. joint_tree->set_focus_mode(Control::FOCUS_NONE);
  694. joint_tree->set_select_mode(Tree::SELECT_SINGLE);
  695. joint_tree->set_hide_root(true);
  696. joint_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  697. joint_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  698. joint_tree->set_allow_rmb_select(true);
  699. SET_DRAG_FORWARDING_GCD(joint_tree, Skeleton3DEditor);
  700. s_con->add_child(joint_tree);
  701. pose_editor = memnew(BoneTransformEditor(skeleton));
  702. pose_editor->set_label(TTR("Bone Transform"));
  703. pose_editor->set_visible(false);
  704. add_child(pose_editor);
  705. set_keyable(te->has_keying());
  706. }
  707. void Skeleton3DEditor::_notification(int p_what) {
  708. switch (p_what) {
  709. case NOTIFICATION_ENTER_TREE: {
  710. update_joint_tree();
  711. joint_tree->connect("item_selected", callable_mp(this, &Skeleton3DEditor::_joint_tree_selection_changed));
  712. joint_tree->connect("item_mouse_selected", callable_mp(this, &Skeleton3DEditor::_joint_tree_rmb_select));
  713. #ifdef TOOLS_ENABLED
  714. skeleton->connect("pose_updated", callable_mp(this, &Skeleton3DEditor::_draw_gizmo));
  715. skeleton->connect("pose_updated", callable_mp(this, &Skeleton3DEditor::_update_properties));
  716. skeleton->connect("bone_enabled_changed", callable_mp(this, &Skeleton3DEditor::_bone_enabled_changed));
  717. skeleton->connect("show_rest_only_changed", callable_mp(this, &Skeleton3DEditor::_update_gizmo_visible));
  718. #endif
  719. get_tree()->connect("node_removed", callable_mp(this, &Skeleton3DEditor::_node_removed), Object::CONNECT_ONE_SHOT);
  720. } break;
  721. case NOTIFICATION_READY: {
  722. // Will trigger NOTIFICATION_THEME_CHANGED, but won't cause any loops if called here.
  723. add_theme_constant_override("separation", 0);
  724. } break;
  725. case NOTIFICATION_THEME_CHANGED: {
  726. skeleton_options->set_icon(get_editor_theme_icon(SNAME("Skeleton3D")));
  727. edit_mode_button->set_icon(get_editor_theme_icon(SNAME("ToolBoneSelect")));
  728. key_loc_button->set_icon(get_editor_theme_icon(SNAME("KeyPosition")));
  729. key_rot_button->set_icon(get_editor_theme_icon(SNAME("KeyRotation")));
  730. key_scale_button->set_icon(get_editor_theme_icon(SNAME("KeyScale")));
  731. key_insert_button->set_icon(get_editor_theme_icon(SNAME("Key")));
  732. key_insert_all_button->set_icon(get_editor_theme_icon(SNAME("NewKey")));
  733. bones_section->set_bg_color(get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  734. update_joint_tree();
  735. } break;
  736. case NOTIFICATION_PREDELETE: {
  737. if (skeleton) {
  738. select_bone(-1); // Requires that the joint_tree has not been deleted.
  739. #ifdef TOOLS_ENABLED
  740. skeleton->disconnect("show_rest_only_changed", callable_mp(this, &Skeleton3DEditor::_update_gizmo_visible));
  741. skeleton->disconnect("bone_enabled_changed", callable_mp(this, &Skeleton3DEditor::_bone_enabled_changed));
  742. skeleton->disconnect("pose_updated", callable_mp(this, &Skeleton3DEditor::_draw_gizmo));
  743. skeleton->disconnect("pose_updated", callable_mp(this, &Skeleton3DEditor::_update_properties));
  744. skeleton->set_transform_gizmo_visible(true);
  745. #endif
  746. handles_mesh_instance->get_parent()->remove_child(handles_mesh_instance);
  747. }
  748. edit_mode_toggled(false);
  749. } break;
  750. }
  751. }
  752. void Skeleton3DEditor::_node_removed(Node *p_node) {
  753. if (skeleton && p_node == skeleton) {
  754. skeleton = nullptr;
  755. skeleton_options->hide();
  756. }
  757. _update_properties();
  758. }
  759. void Skeleton3DEditor::_bind_methods() {
  760. ClassDB::bind_method(D_METHOD("_node_removed"), &Skeleton3DEditor::_node_removed);
  761. ClassDB::bind_method(D_METHOD("_joint_tree_selection_changed"), &Skeleton3DEditor::_joint_tree_selection_changed);
  762. ClassDB::bind_method(D_METHOD("_joint_tree_rmb_select"), &Skeleton3DEditor::_joint_tree_rmb_select);
  763. ClassDB::bind_method(D_METHOD("_update_properties"), &Skeleton3DEditor::_update_properties);
  764. ClassDB::bind_method(D_METHOD("_on_click_skeleton_option"), &Skeleton3DEditor::_on_click_skeleton_option);
  765. ClassDB::bind_method(D_METHOD("move_skeleton_bone"), &Skeleton3DEditor::move_skeleton_bone);
  766. ClassDB::bind_method(D_METHOD("_draw_gizmo"), &Skeleton3DEditor::_draw_gizmo);
  767. }
  768. void Skeleton3DEditor::edit_mode_toggled(const bool pressed) {
  769. edit_mode = pressed;
  770. _update_gizmo_visible();
  771. }
  772. Skeleton3DEditor::Skeleton3DEditor(EditorInspectorPluginSkeleton *e_plugin, Skeleton3D *p_skeleton) :
  773. editor_plugin(e_plugin),
  774. skeleton(p_skeleton) {
  775. singleton = this;
  776. // Handle.
  777. handle_material = Ref<ShaderMaterial>(memnew(ShaderMaterial));
  778. handle_shader = Ref<Shader>(memnew(Shader));
  779. handle_shader->set_code(R"(
  780. // Skeleton 3D gizmo handle shader.
  781. shader_type spatial;
  782. render_mode unshaded, shadows_disabled, depth_draw_always, fog_disabled;
  783. uniform sampler2D texture_albedo : source_color;
  784. uniform float point_size : hint_range(0, 128) = 32;
  785. void vertex() {
  786. if (!OUTPUT_IS_SRGB) {
  787. COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045)));
  788. }
  789. VERTEX = VERTEX;
  790. POSITION = PROJECTION_MATRIX * VIEW_MATRIX * MODEL_MATRIX * vec4(VERTEX.xyz, 1.0);
  791. POSITION.z = mix(POSITION.z, 0.0, 0.999);
  792. POINT_SIZE = point_size;
  793. }
  794. void fragment() {
  795. vec4 albedo_tex = texture(texture_albedo, POINT_COORD);
  796. vec3 col = albedo_tex.rgb + COLOR.rgb;
  797. col = vec3(min(col.r, 1.0), min(col.g, 1.0), min(col.b, 1.0));
  798. ALBEDO = col;
  799. if (albedo_tex.a < 0.5) {
  800. discard;
  801. }
  802. ALPHA = albedo_tex.a;
  803. }
  804. )");
  805. handle_material->set_shader(handle_shader);
  806. Ref<Texture2D> handle = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorBoneHandle"), EditorStringName(EditorIcons));
  807. handle_material->set_shader_parameter("point_size", handle->get_width());
  808. handle_material->set_shader_parameter("texture_albedo", handle);
  809. handles_mesh_instance = memnew(MeshInstance3D);
  810. handles_mesh_instance->set_cast_shadows_setting(GeometryInstance3D::SHADOW_CASTING_SETTING_OFF);
  811. handles_mesh.instantiate();
  812. handles_mesh_instance->set_mesh(handles_mesh);
  813. create_editors();
  814. }
  815. void Skeleton3DEditor::update_bone_original() {
  816. if (!skeleton) {
  817. return;
  818. }
  819. if (skeleton->get_bone_count() == 0 || selected_bone == -1) {
  820. return;
  821. }
  822. bone_original_position = skeleton->get_bone_pose_position(selected_bone);
  823. bone_original_rotation = skeleton->get_bone_pose_rotation(selected_bone);
  824. bone_original_scale = skeleton->get_bone_pose_scale(selected_bone);
  825. }
  826. void Skeleton3DEditor::_hide_handles() {
  827. handles_mesh_instance->hide();
  828. }
  829. void Skeleton3DEditor::_draw_gizmo() {
  830. if (!skeleton) {
  831. return;
  832. }
  833. // If you call get_bone_global_pose() while drawing the surface, such as toggle rest mode,
  834. // the skeleton update will be done first and
  835. // the drawing surface will be interrupted once and an error will occur.
  836. skeleton->force_update_all_dirty_bones();
  837. // Handles.
  838. if (edit_mode) {
  839. _draw_handles();
  840. } else {
  841. _hide_handles();
  842. }
  843. }
  844. void Skeleton3DEditor::_draw_handles() {
  845. const int bone_count = skeleton->get_bone_count();
  846. handles_mesh->clear_surfaces();
  847. if (bone_count) {
  848. handles_mesh_instance->show();
  849. handles_mesh->surface_begin(Mesh::PRIMITIVE_POINTS);
  850. for (int i = 0; i < bone_count; i++) {
  851. Color c;
  852. if (i == selected_bone) {
  853. c = Color(1, 1, 0);
  854. } else {
  855. c = Color(0.1, 0.25, 0.8);
  856. }
  857. Vector3 point = skeleton->get_bone_global_pose(i).origin;
  858. handles_mesh->surface_set_color(c);
  859. handles_mesh->surface_add_vertex(point);
  860. }
  861. handles_mesh->surface_end();
  862. handles_mesh->surface_set_material(0, handle_material);
  863. } else {
  864. handles_mesh_instance->hide();
  865. }
  866. }
  867. TreeItem *Skeleton3DEditor::_find(TreeItem *p_node, const NodePath &p_path) {
  868. if (!p_node) {
  869. return nullptr;
  870. }
  871. NodePath np = p_node->get_metadata(0);
  872. if (np == p_path) {
  873. return p_node;
  874. }
  875. TreeItem *children = p_node->get_first_child();
  876. while (children) {
  877. TreeItem *n = _find(children, p_path);
  878. if (n) {
  879. return n;
  880. }
  881. children = children->get_next();
  882. }
  883. return nullptr;
  884. }
  885. void Skeleton3DEditor::_subgizmo_selection_change() {
  886. if (!skeleton) {
  887. return;
  888. }
  889. // Once validated by subgizmos_intersect_ray, but required if through inspector's bones tree.
  890. if (!edit_mode) {
  891. skeleton->clear_subgizmo_selection();
  892. return;
  893. }
  894. int selected = -1;
  895. Skeleton3DEditor *se = Skeleton3DEditor::get_singleton();
  896. if (se) {
  897. selected = se->get_selected_bone();
  898. }
  899. if (selected >= 0) {
  900. Vector<Ref<Node3DGizmo>> gizmos = skeleton->get_gizmos();
  901. for (int i = 0; i < gizmos.size(); i++) {
  902. Ref<EditorNode3DGizmo> gizmo = gizmos[i];
  903. if (!gizmo.is_valid()) {
  904. continue;
  905. }
  906. Ref<Skeleton3DGizmoPlugin> plugin = gizmo->get_plugin();
  907. if (!plugin.is_valid()) {
  908. continue;
  909. }
  910. skeleton->set_subgizmo_selection(gizmo, selected, skeleton->get_bone_global_pose(selected));
  911. break;
  912. }
  913. } else {
  914. skeleton->clear_subgizmo_selection();
  915. }
  916. }
  917. void Skeleton3DEditor::select_bone(int p_idx) {
  918. if (p_idx >= 0) {
  919. TreeItem *ti = _find(joint_tree->get_root(), "bones/" + itos(p_idx));
  920. if (ti) {
  921. // Make visible when it's collapsed.
  922. TreeItem *node = ti->get_parent();
  923. while (node && node != joint_tree->get_root()) {
  924. node->set_collapsed(false);
  925. node = node->get_parent();
  926. }
  927. ti->select(0);
  928. joint_tree->scroll_to_item(ti);
  929. }
  930. } else {
  931. selected_bone = -1;
  932. joint_tree->deselect_all();
  933. _joint_tree_selection_changed();
  934. }
  935. }
  936. Skeleton3DEditor::~Skeleton3DEditor() {
  937. singleton = nullptr;
  938. handles_mesh_instance->queue_free();
  939. Node3DEditor *ne = Node3DEditor::get_singleton();
  940. ne->remove_control_from_menu_panel(topmenu_bar);
  941. memdelete(topmenu_bar);
  942. }
  943. bool EditorInspectorPluginSkeleton::can_handle(Object *p_object) {
  944. return Object::cast_to<Skeleton3D>(p_object) != nullptr;
  945. }
  946. void EditorInspectorPluginSkeleton::parse_begin(Object *p_object) {
  947. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_object);
  948. ERR_FAIL_NULL(skeleton);
  949. skel_editor = memnew(Skeleton3DEditor(this, skeleton));
  950. add_custom_control(skel_editor);
  951. }
  952. Skeleton3DEditorPlugin::Skeleton3DEditorPlugin() {
  953. skeleton_plugin = memnew(EditorInspectorPluginSkeleton);
  954. EditorInspector::add_inspector_plugin(skeleton_plugin);
  955. Ref<Skeleton3DGizmoPlugin> gizmo_plugin = Ref<Skeleton3DGizmoPlugin>(memnew(Skeleton3DGizmoPlugin));
  956. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  957. }
  958. EditorPlugin::AfterGUIInput Skeleton3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  959. Skeleton3DEditor *se = Skeleton3DEditor::get_singleton();
  960. Node3DEditor *ne = Node3DEditor::get_singleton();
  961. if (se && se->is_edit_mode()) {
  962. const Ref<InputEventMouseButton> mb = p_event;
  963. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  964. if (ne->get_tool_mode() != Node3DEditor::TOOL_MODE_SELECT) {
  965. if (!ne->is_gizmo_visible()) {
  966. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  967. }
  968. }
  969. if (mb->is_pressed()) {
  970. se->update_bone_original();
  971. }
  972. }
  973. return EditorPlugin::AFTER_GUI_INPUT_CUSTOM;
  974. }
  975. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  976. }
  977. bool Skeleton3DEditorPlugin::handles(Object *p_object) const {
  978. return p_object->is_class("Skeleton3D");
  979. }
  980. void Skeleton3DEditor::_bone_enabled_changed(const int p_bone_id) {
  981. _update_gizmo_visible();
  982. }
  983. void Skeleton3DEditor::_update_gizmo_visible() {
  984. _subgizmo_selection_change();
  985. if (edit_mode) {
  986. if (selected_bone == -1) {
  987. #ifdef TOOLS_ENABLED
  988. skeleton->set_transform_gizmo_visible(false);
  989. #endif
  990. } else {
  991. #ifdef TOOLS_ENABLED
  992. if (skeleton->is_bone_enabled(selected_bone) && !skeleton->is_show_rest_only()) {
  993. skeleton->set_transform_gizmo_visible(true);
  994. } else {
  995. skeleton->set_transform_gizmo_visible(false);
  996. }
  997. #endif
  998. }
  999. } else {
  1000. #ifdef TOOLS_ENABLED
  1001. skeleton->set_transform_gizmo_visible(true);
  1002. #endif
  1003. }
  1004. _draw_gizmo();
  1005. }
  1006. int Skeleton3DEditor::get_selected_bone() const {
  1007. return selected_bone;
  1008. }
  1009. Skeleton3DGizmoPlugin::Skeleton3DGizmoPlugin() {
  1010. unselected_mat = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  1011. unselected_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1012. unselected_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1013. unselected_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1014. unselected_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  1015. unselected_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1016. selected_mat = Ref<ShaderMaterial>(memnew(ShaderMaterial));
  1017. selected_sh = Ref<Shader>(memnew(Shader));
  1018. selected_sh->set_code(R"(
  1019. // Skeleton 3D gizmo bones shader.
  1020. shader_type spatial;
  1021. render_mode unshaded, shadows_disabled;
  1022. void vertex() {
  1023. if (!OUTPUT_IS_SRGB) {
  1024. COLOR.rgb = mix( pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb* (1.0 / 12.92), lessThan(COLOR.rgb,vec3(0.04045)) );
  1025. }
  1026. VERTEX = VERTEX;
  1027. POSITION = PROJECTION_MATRIX * VIEW_MATRIX * MODEL_MATRIX * vec4(VERTEX.xyz, 1.0);
  1028. POSITION.z = mix(POSITION.z, 0, 0.998);
  1029. }
  1030. void fragment() {
  1031. ALBEDO = COLOR.rgb;
  1032. ALPHA = COLOR.a;
  1033. }
  1034. )");
  1035. selected_mat->set_shader(selected_sh);
  1036. // Register properties in editor settings.
  1037. EDITOR_DEF("editors/3d_gizmos/gizmo_colors/skeleton", Color(1, 0.8, 0.4));
  1038. EDITOR_DEF("editors/3d_gizmos/gizmo_colors/selected_bone", Color(0.8, 0.3, 0.0));
  1039. EDITOR_DEF("editors/3d_gizmos/gizmo_settings/bone_axis_length", (float)0.1);
  1040. EDITOR_DEF("editors/3d_gizmos/gizmo_settings/bone_shape", 1);
  1041. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/3d_gizmos/gizmo_settings/bone_shape", PROPERTY_HINT_ENUM, "Wire,Octahedron"));
  1042. }
  1043. bool Skeleton3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1044. return Object::cast_to<Skeleton3D>(p_spatial) != nullptr;
  1045. }
  1046. String Skeleton3DGizmoPlugin::get_gizmo_name() const {
  1047. return "Skeleton3D";
  1048. }
  1049. int Skeleton3DGizmoPlugin::get_priority() const {
  1050. return -1;
  1051. }
  1052. int Skeleton3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
  1053. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_gizmo->get_node_3d());
  1054. ERR_FAIL_NULL_V(skeleton, -1);
  1055. Skeleton3DEditor *se = Skeleton3DEditor::get_singleton();
  1056. if (!se || !se->is_edit_mode()) {
  1057. return -1;
  1058. }
  1059. if (Node3DEditor::get_singleton()->get_tool_mode() != Node3DEditor::TOOL_MODE_SELECT) {
  1060. return -1;
  1061. }
  1062. // Select bone.
  1063. real_t grab_threshold = 4 * EDSCALE;
  1064. Vector3 ray_from = p_camera->get_global_transform().origin;
  1065. Transform3D gt = skeleton->get_global_transform();
  1066. int closest_idx = -1;
  1067. real_t closest_dist = 1e10;
  1068. const int bone_count = skeleton->get_bone_count();
  1069. for (int i = 0; i < bone_count; i++) {
  1070. Vector3 joint_pos_3d = gt.xform(skeleton->get_bone_global_pose(i).origin);
  1071. Vector2 joint_pos_2d = p_camera->unproject_position(joint_pos_3d);
  1072. real_t dist_3d = ray_from.distance_to(joint_pos_3d);
  1073. real_t dist_2d = p_point.distance_to(joint_pos_2d);
  1074. if (dist_2d < grab_threshold && dist_3d < closest_dist) {
  1075. closest_dist = dist_3d;
  1076. closest_idx = i;
  1077. }
  1078. }
  1079. if (closest_idx >= 0) {
  1080. se->select_bone(closest_idx);
  1081. return closest_idx;
  1082. }
  1083. se->select_bone(-1);
  1084. return -1;
  1085. }
  1086. Transform3D Skeleton3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
  1087. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_gizmo->get_node_3d());
  1088. ERR_FAIL_NULL_V(skeleton, Transform3D());
  1089. return skeleton->get_bone_global_pose(p_id);
  1090. }
  1091. void Skeleton3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
  1092. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_gizmo->get_node_3d());
  1093. ERR_FAIL_NULL(skeleton);
  1094. // Prepare for global to local.
  1095. Transform3D original_to_local;
  1096. int parent_idx = skeleton->get_bone_parent(p_id);
  1097. if (parent_idx >= 0) {
  1098. original_to_local = skeleton->get_bone_global_pose(parent_idx);
  1099. }
  1100. Basis to_local = original_to_local.get_basis().inverse();
  1101. // Prepare transform.
  1102. Transform3D t;
  1103. // Basis.
  1104. t.basis = to_local * p_transform.get_basis();
  1105. // Origin.
  1106. Vector3 orig = skeleton->get_bone_pose(p_id).origin;
  1107. Vector3 sub = p_transform.origin - skeleton->get_bone_global_pose(p_id).origin;
  1108. t.origin = orig + to_local.xform(sub);
  1109. // Apply transform.
  1110. skeleton->set_bone_pose_position(p_id, t.origin);
  1111. skeleton->set_bone_pose_rotation(p_id, t.basis.get_rotation_quaternion());
  1112. skeleton->set_bone_pose_scale(p_id, t.basis.get_scale());
  1113. }
  1114. void Skeleton3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
  1115. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_gizmo->get_node_3d());
  1116. ERR_FAIL_NULL(skeleton);
  1117. Skeleton3DEditor *se = Skeleton3DEditor::get_singleton();
  1118. Node3DEditor *ne = Node3DEditor::get_singleton();
  1119. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1120. ur->create_action(TTR("Set Bone Transform"));
  1121. if (ne->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || ne->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE) {
  1122. for (int i = 0; i < p_ids.size(); i++) {
  1123. ur->add_do_method(skeleton, "set_bone_pose_position", p_ids[i], skeleton->get_bone_pose_position(p_ids[i]));
  1124. ur->add_undo_method(skeleton, "set_bone_pose_position", p_ids[i], se->get_bone_original_position());
  1125. }
  1126. }
  1127. if (ne->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || ne->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE) {
  1128. for (int i = 0; i < p_ids.size(); i++) {
  1129. ur->add_do_method(skeleton, "set_bone_pose_rotation", p_ids[i], skeleton->get_bone_pose_rotation(p_ids[i]));
  1130. ur->add_undo_method(skeleton, "set_bone_pose_rotation", p_ids[i], se->get_bone_original_rotation());
  1131. }
  1132. }
  1133. if (ne->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE) {
  1134. for (int i = 0; i < p_ids.size(); i++) {
  1135. // If the axis is swapped by scaling, the rotation can be changed.
  1136. ur->add_do_method(skeleton, "set_bone_pose_rotation", p_ids[i], skeleton->get_bone_pose_rotation(p_ids[i]));
  1137. ur->add_undo_method(skeleton, "set_bone_pose_rotation", p_ids[i], se->get_bone_original_rotation());
  1138. ur->add_do_method(skeleton, "set_bone_pose_scale", p_ids[i], skeleton->get_bone_pose_scale(p_ids[i]));
  1139. ur->add_undo_method(skeleton, "set_bone_pose_scale", p_ids[i], se->get_bone_original_scale());
  1140. }
  1141. }
  1142. ur->commit_action();
  1143. }
  1144. void Skeleton3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1145. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_gizmo->get_node_3d());
  1146. p_gizmo->clear();
  1147. if (!skeleton->get_bone_count()) {
  1148. return;
  1149. }
  1150. int selected = -1;
  1151. Skeleton3DEditor *se = Skeleton3DEditor::get_singleton();
  1152. if (se) {
  1153. selected = se->get_selected_bone();
  1154. }
  1155. Color bone_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/skeleton");
  1156. Color selected_bone_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/selected_bone");
  1157. real_t bone_axis_length = EDITOR_GET("editors/3d_gizmos/gizmo_settings/bone_axis_length");
  1158. int bone_shape = EDITOR_GET("editors/3d_gizmos/gizmo_settings/bone_shape");
  1159. LocalVector<Color> axis_colors;
  1160. axis_colors.push_back(Node3DEditor::get_singleton()->get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor)));
  1161. axis_colors.push_back(Node3DEditor::get_singleton()->get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor)));
  1162. axis_colors.push_back(Node3DEditor::get_singleton()->get_theme_color(SNAME("axis_z_color"), EditorStringName(Editor)));
  1163. Ref<SurfaceTool> surface_tool(memnew(SurfaceTool));
  1164. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  1165. if (p_gizmo->is_selected()) {
  1166. surface_tool->set_material(selected_mat);
  1167. } else {
  1168. unselected_mat->set_albedo(bone_color);
  1169. surface_tool->set_material(unselected_mat);
  1170. }
  1171. LocalVector<int> bones;
  1172. LocalVector<float> weights;
  1173. bones.resize(4);
  1174. weights.resize(4);
  1175. for (int i = 0; i < 4; i++) {
  1176. bones[i] = 0;
  1177. weights[i] = 0;
  1178. }
  1179. weights[0] = 1;
  1180. int current_bone_index = 0;
  1181. Vector<int> bones_to_process = skeleton->get_parentless_bones();
  1182. while (bones_to_process.size() > current_bone_index) {
  1183. int current_bone_idx = bones_to_process[current_bone_index];
  1184. current_bone_index++;
  1185. Color current_bone_color = (current_bone_idx == selected) ? selected_bone_color : bone_color;
  1186. Vector<int> child_bones_vector;
  1187. child_bones_vector = skeleton->get_bone_children(current_bone_idx);
  1188. int child_bones_size = child_bones_vector.size();
  1189. for (int i = 0; i < child_bones_size; i++) {
  1190. // Something wrong.
  1191. if (child_bones_vector[i] < 0) {
  1192. continue;
  1193. }
  1194. int child_bone_idx = child_bones_vector[i];
  1195. Vector3 v0 = skeleton->get_bone_global_rest(current_bone_idx).origin;
  1196. Vector3 v1 = skeleton->get_bone_global_rest(child_bone_idx).origin;
  1197. Vector3 d = (v1 - v0).normalized();
  1198. real_t dist = v0.distance_to(v1);
  1199. // Find closest axis.
  1200. int closest = -1;
  1201. real_t closest_d = 0.0;
  1202. for (int j = 0; j < 3; j++) {
  1203. real_t dp = Math::abs(skeleton->get_bone_global_rest(current_bone_idx).basis[j].normalized().dot(d));
  1204. if (j == 0 || dp > closest_d) {
  1205. closest = j;
  1206. }
  1207. }
  1208. // Draw bone.
  1209. switch (bone_shape) {
  1210. case 0: { // Wire shape.
  1211. surface_tool->set_color(current_bone_color);
  1212. bones[0] = current_bone_idx;
  1213. surface_tool->set_bones(bones);
  1214. surface_tool->set_weights(weights);
  1215. surface_tool->add_vertex(v0);
  1216. bones[0] = child_bone_idx;
  1217. surface_tool->set_bones(bones);
  1218. surface_tool->set_weights(weights);
  1219. surface_tool->add_vertex(v1);
  1220. } break;
  1221. case 1: { // Octahedron shape.
  1222. Vector3 first;
  1223. Vector3 points[6];
  1224. int point_idx = 0;
  1225. for (int j = 0; j < 3; j++) {
  1226. Vector3 axis;
  1227. if (first == Vector3()) {
  1228. axis = d.cross(d.cross(skeleton->get_bone_global_rest(current_bone_idx).basis[j])).normalized();
  1229. first = axis;
  1230. } else {
  1231. axis = d.cross(first).normalized();
  1232. }
  1233. surface_tool->set_color(current_bone_color);
  1234. for (int k = 0; k < 2; k++) {
  1235. if (k == 1) {
  1236. axis = -axis;
  1237. }
  1238. Vector3 point = v0 + d * dist * 0.2;
  1239. point += axis * dist * 0.1;
  1240. bones[0] = current_bone_idx;
  1241. surface_tool->set_bones(bones);
  1242. surface_tool->set_weights(weights);
  1243. surface_tool->add_vertex(v0);
  1244. surface_tool->set_bones(bones);
  1245. surface_tool->set_weights(weights);
  1246. surface_tool->add_vertex(point);
  1247. surface_tool->set_bones(bones);
  1248. surface_tool->set_weights(weights);
  1249. surface_tool->add_vertex(point);
  1250. bones[0] = child_bone_idx;
  1251. surface_tool->set_bones(bones);
  1252. surface_tool->set_weights(weights);
  1253. surface_tool->add_vertex(v1);
  1254. points[point_idx++] = point;
  1255. }
  1256. }
  1257. surface_tool->set_color(current_bone_color);
  1258. SWAP(points[1], points[2]);
  1259. bones[0] = current_bone_idx;
  1260. for (int j = 0; j < 6; j++) {
  1261. surface_tool->set_bones(bones);
  1262. surface_tool->set_weights(weights);
  1263. surface_tool->add_vertex(points[j]);
  1264. surface_tool->set_bones(bones);
  1265. surface_tool->set_weights(weights);
  1266. surface_tool->add_vertex(points[(j + 1) % 6]);
  1267. }
  1268. } break;
  1269. }
  1270. // Axis as root of the bone.
  1271. for (int j = 0; j < 3; j++) {
  1272. bones[0] = current_bone_idx;
  1273. surface_tool->set_color(axis_colors[j]);
  1274. surface_tool->set_bones(bones);
  1275. surface_tool->set_weights(weights);
  1276. surface_tool->add_vertex(v0);
  1277. surface_tool->set_bones(bones);
  1278. surface_tool->set_weights(weights);
  1279. surface_tool->add_vertex(v0 + (skeleton->get_bone_global_rest(current_bone_idx).basis.inverse())[j].normalized() * dist * bone_axis_length);
  1280. if (j == closest) {
  1281. continue;
  1282. }
  1283. }
  1284. // Axis at the end of the bone children.
  1285. if (i == child_bones_size - 1) {
  1286. for (int j = 0; j < 3; j++) {
  1287. bones[0] = child_bone_idx;
  1288. surface_tool->set_color(axis_colors[j]);
  1289. surface_tool->set_bones(bones);
  1290. surface_tool->set_weights(weights);
  1291. surface_tool->add_vertex(v1);
  1292. surface_tool->set_bones(bones);
  1293. surface_tool->set_weights(weights);
  1294. surface_tool->add_vertex(v1 + (skeleton->get_bone_global_rest(child_bone_idx).basis.inverse())[j].normalized() * dist * bone_axis_length);
  1295. if (j == closest) {
  1296. continue;
  1297. }
  1298. }
  1299. }
  1300. // Add the bone's children to the list of bones to be processed.
  1301. bones_to_process.push_back(child_bones_vector[i]);
  1302. }
  1303. }
  1304. Ref<ArrayMesh> m = surface_tool->commit();
  1305. p_gizmo->add_mesh(m, Ref<Material>(), Transform3D(), skeleton->register_skin(skeleton->create_skin_from_rest_transforms()));
  1306. }