skeleton_3d_editor_plugin.cpp 54 KB

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