path_3d_editor_plugin.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /**************************************************************************/
  2. /* path_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 "path_3d_editor_plugin.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "core/math/geometry_3d.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "node_3d_editor_plugin.h"
  39. #include "scene/gui/dialogs.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/resources/curve.h"
  42. String Path3DGizmo::get_handle_name(int p_id, bool p_secondary) const {
  43. Ref<Curve3D> c = path->get_curve();
  44. if (c.is_null()) {
  45. return "";
  46. }
  47. // Primary handles: position.
  48. if (!p_secondary) {
  49. return TTR("Curve Point #") + itos(p_id);
  50. }
  51. // Secondary handles: in, out, tilt.
  52. const HandleInfo info = _secondary_handles_info[p_id];
  53. switch (info.type) {
  54. case HandleType::HANDLE_TYPE_IN:
  55. return TTR("Handle In #") + itos(info.point_idx);
  56. case HandleType::HANDLE_TYPE_OUT:
  57. return TTR("Handle Out #") + itos(info.point_idx);
  58. case HandleType::HANDLE_TYPE_TILT:
  59. return TTR("Handle Tilt #") + itos(info.point_idx);
  60. }
  61. return "";
  62. }
  63. Variant Path3DGizmo::get_handle_value(int p_id, bool p_secondary) const {
  64. Ref<Curve3D> c = path->get_curve();
  65. if (c.is_null()) {
  66. return Variant();
  67. }
  68. // Primary handles: position.
  69. if (!p_secondary) {
  70. original = c->get_point_position(p_id);
  71. return original;
  72. }
  73. // Secondary handles: in, out, tilt.
  74. const HandleInfo info = _secondary_handles_info[p_id];
  75. Vector3 ofs;
  76. switch (info.type) {
  77. case HandleType::HANDLE_TYPE_TILT:
  78. return c->get_point_tilt(info.point_idx);
  79. case HandleType::HANDLE_TYPE_IN:
  80. ofs = c->get_point_in(info.point_idx);
  81. break;
  82. case HandleType::HANDLE_TYPE_OUT:
  83. ofs = c->get_point_out(info.point_idx);
  84. break;
  85. }
  86. original = ofs + c->get_point_position(info.point_idx);
  87. return ofs;
  88. }
  89. void Path3DGizmo::set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  90. Ref<Curve3D> c = path->get_curve();
  91. if (c.is_null()) {
  92. return;
  93. }
  94. const Transform3D gt = path->get_global_transform();
  95. const Transform3D gi = gt.affine_inverse();
  96. const Vector3 ray_from = p_camera->project_ray_origin(p_point);
  97. const Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  98. const Plane p = Plane(p_camera->get_transform().basis.get_column(2), gt.xform(original));
  99. // Primary handles: position.
  100. if (!p_secondary) {
  101. Vector3 inters;
  102. // Special case for primary handle, the handle id equals control point id.
  103. const int idx = p_id;
  104. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  105. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  106. float snap = Node3DEditor::get_singleton()->get_translate_snap();
  107. inters.snapf(snap);
  108. }
  109. Vector3 local = gi.xform(inters);
  110. c->set_point_position(idx, local);
  111. }
  112. return;
  113. }
  114. // Secondary handles: in, out, tilt.
  115. const HandleInfo info = _secondary_handles_info[p_id];
  116. switch (info.type) {
  117. case HandleType::HANDLE_TYPE_OUT:
  118. case HandleType::HANDLE_TYPE_IN: {
  119. const int idx = info.point_idx;
  120. const Vector3 base = c->get_point_position(idx);
  121. Vector3 inters;
  122. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  123. if (!Path3DEditorPlugin::singleton->is_handle_clicked()) {
  124. orig_in_length = c->get_point_in(idx).length();
  125. orig_out_length = c->get_point_out(idx).length();
  126. Path3DEditorPlugin::singleton->set_handle_clicked(true);
  127. }
  128. Vector3 local = gi.xform(inters) - base;
  129. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  130. float snap = Node3DEditor::get_singleton()->get_translate_snap();
  131. local.snapf(snap);
  132. }
  133. if (info.type == HandleType::HANDLE_TYPE_IN) {
  134. c->set_point_in(idx, local);
  135. if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
  136. c->set_point_out(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_out_length));
  137. }
  138. } else {
  139. c->set_point_out(idx, local);
  140. if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
  141. c->set_point_in(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_in_length));
  142. }
  143. }
  144. }
  145. break;
  146. }
  147. case HandleType::HANDLE_TYPE_TILT: {
  148. const int idx = info.point_idx;
  149. const Vector3 position = c->get_point_position(idx);
  150. const Basis posture = c->get_point_baked_posture(idx);
  151. const Vector3 tangent = -posture.get_column(2);
  152. const Vector3 up = posture.get_column(1);
  153. const Plane tilt_plane_global = gt.xform(Plane(tangent, position));
  154. Vector3 intersection;
  155. if (tilt_plane_global.intersects_ray(ray_from, ray_dir, &intersection)) {
  156. Vector3 direction = gi.xform(intersection) - position;
  157. real_t tilt_angle = up.signed_angle_to(direction, tangent);
  158. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  159. real_t snap_degrees = Node3DEditor::get_singleton()->get_rotate_snap();
  160. tilt_angle = Math::deg_to_rad(Math::snapped(Math::rad_to_deg(tilt_angle), snap_degrees));
  161. }
  162. c->set_point_tilt(idx, tilt_angle);
  163. }
  164. break;
  165. }
  166. }
  167. }
  168. void Path3DGizmo::commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  169. Ref<Curve3D> c = path->get_curve();
  170. if (c.is_null()) {
  171. return;
  172. }
  173. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  174. // Primary handles: position.
  175. if (!p_secondary && !Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  176. // Special case for primary handle, the handle id equals control point id.
  177. const int idx = p_id;
  178. if (p_cancel) {
  179. c->set_point_position(idx, p_restore);
  180. return;
  181. }
  182. ur->create_action(TTR("Set Curve Point Position"));
  183. ur->add_do_method(c.ptr(), "set_point_position", idx, c->get_point_position(idx));
  184. ur->add_undo_method(c.ptr(), "set_point_position", idx, p_restore);
  185. ur->commit_action();
  186. return;
  187. }
  188. // Secondary handles: in, out, tilt.
  189. const HandleInfo info = _secondary_handles_info[p_id];
  190. const int idx = info.point_idx;
  191. switch (info.type) {
  192. case HandleType::HANDLE_TYPE_OUT: {
  193. if (p_cancel) {
  194. c->set_point_out(idx, p_restore);
  195. return;
  196. }
  197. ur->create_action(TTR("Set Curve Out Position"));
  198. ur->add_do_method(c.ptr(), "set_point_out", idx, c->get_point_out(idx));
  199. ur->add_undo_method(c.ptr(), "set_point_out", idx, p_restore);
  200. if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
  201. ur->add_do_method(c.ptr(), "set_point_in", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -c->get_point_out(idx) : (-c->get_point_out(idx).normalized() * orig_in_length));
  202. ur->add_undo_method(c.ptr(), "set_point_in", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_in_length));
  203. }
  204. ur->commit_action();
  205. break;
  206. }
  207. case HandleType::HANDLE_TYPE_IN: {
  208. if (p_cancel) {
  209. c->set_point_in(idx, p_restore);
  210. return;
  211. }
  212. ur->create_action(TTR("Set Curve In Position"));
  213. ur->add_do_method(c.ptr(), "set_point_in", idx, c->get_point_in(idx));
  214. ur->add_undo_method(c.ptr(), "set_point_in", idx, p_restore);
  215. if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
  216. ur->add_do_method(c.ptr(), "set_point_out", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -c->get_point_in(idx) : (-c->get_point_in(idx).normalized() * orig_out_length));
  217. ur->add_undo_method(c.ptr(), "set_point_out", idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -static_cast<Vector3>(p_restore) : (-static_cast<Vector3>(p_restore).normalized() * orig_out_length));
  218. }
  219. ur->commit_action();
  220. break;
  221. }
  222. case HandleType::HANDLE_TYPE_TILT: {
  223. if (p_cancel) {
  224. c->set_point_tilt(idx, p_restore);
  225. return;
  226. }
  227. ur->create_action(TTR("Set Curve Point Tilt"));
  228. ur->add_do_method(c.ptr(), "set_point_tilt", idx, c->get_point_tilt(idx));
  229. ur->add_undo_method(c.ptr(), "set_point_tilt", idx, p_restore);
  230. ur->commit_action();
  231. break;
  232. }
  233. }
  234. }
  235. void Path3DGizmo::redraw() {
  236. clear();
  237. Ref<StandardMaterial3D> path_material = gizmo_plugin->get_material("path_material", this);
  238. Ref<StandardMaterial3D> path_thin_material = gizmo_plugin->get_material("path_thin_material", this);
  239. Ref<StandardMaterial3D> path_tilt_material = gizmo_plugin->get_material("path_tilt_material", this);
  240. Ref<StandardMaterial3D> path_tilt_muted_material = gizmo_plugin->get_material("path_tilt_muted_material", this);
  241. Ref<StandardMaterial3D> handles_material = gizmo_plugin->get_material("handles");
  242. Ref<StandardMaterial3D> sec_handles_material = gizmo_plugin->get_material("sec_handles");
  243. Ref<Curve3D> c = path->get_curve();
  244. if (c.is_null()) {
  245. return;
  246. }
  247. real_t interval = 0.1;
  248. const real_t length = c->get_baked_length();
  249. // 1. Draw curve and bones.
  250. if (length > CMP_EPSILON) {
  251. const int sample_count = int(length / interval) + 2;
  252. interval = length / (sample_count - 1); // Recalculate real interval length.
  253. Vector<Transform3D> frames;
  254. frames.resize(sample_count);
  255. {
  256. Transform3D *w = frames.ptrw();
  257. for (int i = 0; i < sample_count; i++) {
  258. w[i] = c->sample_baked_with_rotation(i * interval, true, true);
  259. }
  260. }
  261. const Transform3D *r = frames.ptr();
  262. Vector<Vector3> _collision_segments;
  263. _collision_segments.resize((sample_count - 1) * 2);
  264. Vector3 *_collisions_ptr = _collision_segments.ptrw();
  265. Vector<Vector3> bones;
  266. bones.resize(sample_count * 4);
  267. Vector3 *bones_ptr = bones.ptrw();
  268. Vector<Vector3> ribbon;
  269. ribbon.resize(sample_count);
  270. Vector3 *ribbon_ptr = ribbon.ptrw();
  271. for (int i = 0; i < sample_count; i++) {
  272. const Vector3 p1 = r[i].origin;
  273. const Vector3 side = r[i].basis.get_column(0);
  274. const Vector3 up = r[i].basis.get_column(1);
  275. const Vector3 forward = r[i].basis.get_column(2);
  276. // Collision segments.
  277. if (i != sample_count - 1) {
  278. const Vector3 p2 = r[i + 1].origin;
  279. _collisions_ptr[(i * 2)] = p1;
  280. _collisions_ptr[(i * 2) + 1] = p2;
  281. }
  282. // Path3D as a ribbon.
  283. ribbon_ptr[i] = p1;
  284. // Fish Bone.
  285. const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
  286. const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
  287. const int bone_idx = i * 4;
  288. bones_ptr[bone_idx] = p1;
  289. bones_ptr[bone_idx + 1] = p_left;
  290. bones_ptr[bone_idx + 2] = p1;
  291. bones_ptr[bone_idx + 3] = p_right;
  292. }
  293. add_collision_segments(_collision_segments);
  294. add_lines(bones, path_material);
  295. add_vertices(ribbon, path_material, Mesh::PRIMITIVE_LINE_STRIP);
  296. }
  297. // 2. Draw handles when selected.
  298. if (Path3DEditorPlugin::singleton->get_edited_path() == path) {
  299. PackedVector3Array handle_lines;
  300. PackedVector3Array tilt_handle_lines;
  301. PackedVector3Array primary_handle_points;
  302. PackedVector3Array secondary_handle_points;
  303. PackedInt32Array collected_secondary_handle_ids; // Avoid shadowing member on Node3DEditorGizmo.
  304. _secondary_handles_info.resize(c->get_point_count() * 3);
  305. for (int idx = 0; idx < c->get_point_count(); idx++) {
  306. // Collect primary-handles.
  307. const Vector3 pos = c->get_point_position(idx);
  308. primary_handle_points.append(pos);
  309. HandleInfo info;
  310. info.point_idx = idx;
  311. // Collect in-handles except for the first point.
  312. if (idx > 0 && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
  313. const Vector3 in = c->get_point_in(idx);
  314. info.type = HandleType::HANDLE_TYPE_IN;
  315. const int handle_idx = idx * 3 + 0;
  316. collected_secondary_handle_ids.append(handle_idx);
  317. _secondary_handles_info.write[handle_idx] = info;
  318. secondary_handle_points.append(pos + in);
  319. handle_lines.append(pos);
  320. handle_lines.append(pos + in);
  321. }
  322. // Collect out-handles except for the last point.
  323. if (idx < c->get_point_count() - 1 && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
  324. const Vector3 out = c->get_point_out(idx);
  325. info.type = HandleType::HANDLE_TYPE_OUT;
  326. const int handle_idx = idx * 3 + 1;
  327. collected_secondary_handle_ids.append(handle_idx);
  328. _secondary_handles_info.write[handle_idx] = info;
  329. secondary_handle_points.append(pos + out);
  330. handle_lines.append(pos);
  331. handle_lines.append(pos + out);
  332. }
  333. // Collect tilt-handles.
  334. if (Path3DEditorPlugin::singleton->curve_edit_tilt->is_pressed()) {
  335. // Tilt handle.
  336. {
  337. info.type = HandleType::HANDLE_TYPE_TILT;
  338. const int handle_idx = idx * 3 + 2;
  339. collected_secondary_handle_ids.append(handle_idx);
  340. _secondary_handles_info.write[handle_idx] = info;
  341. const Basis posture = c->get_point_baked_posture(idx, true);
  342. const Vector3 up = posture.get_column(1);
  343. secondary_handle_points.append(pos + up * disk_size);
  344. tilt_handle_lines.append(pos);
  345. tilt_handle_lines.append(pos + up * disk_size);
  346. }
  347. // Tilt disk.
  348. {
  349. const Basis posture = c->get_point_baked_posture(idx, false);
  350. const Vector3 up = posture.get_column(1);
  351. const Vector3 side = posture.get_column(0);
  352. PackedVector3Array disk;
  353. disk.append(pos);
  354. const int n = 36;
  355. for (int i = 0; i <= n; i++) {
  356. const float a = Math_TAU * i / n;
  357. const Vector3 edge = sin(a) * side + cos(a) * up;
  358. disk.append(pos + edge * disk_size);
  359. }
  360. add_vertices(disk, path_tilt_material, Mesh::PRIMITIVE_LINE_STRIP);
  361. }
  362. }
  363. }
  364. if (handle_lines.size() > 1) {
  365. add_lines(handle_lines, path_thin_material);
  366. }
  367. if (tilt_handle_lines.size() > 1) {
  368. add_lines(tilt_handle_lines, path_tilt_material);
  369. }
  370. if (!Path3DEditorPlugin::singleton->curve_edit->is_pressed() && primary_handle_points.size()) {
  371. add_handles(primary_handle_points, handles_material);
  372. }
  373. if (secondary_handle_points.size()) {
  374. add_handles(secondary_handle_points, sec_handles_material, collected_secondary_handle_ids, false, true);
  375. }
  376. // Draw the gizmo plugin manually, because handles are registered. In which case, the caller code skips drawing the gizmo plugin.
  377. gizmo_plugin->redraw(this);
  378. }
  379. }
  380. void Path3DGizmo::_update_transform_gizmo() {
  381. Node3DEditor::get_singleton()->update_transform_gizmo();
  382. }
  383. Path3DGizmo::Path3DGizmo(Path3D *p_path, float p_disk_size) {
  384. path = p_path;
  385. disk_size = p_disk_size;
  386. set_node_3d(p_path);
  387. orig_in_length = 0;
  388. orig_out_length = 0;
  389. // Connecting to a signal once, rather than plaguing the implementation with calls to `Node3DEditor::update_transform_gizmo`.
  390. path->connect("curve_changed", callable_mp(this, &Path3DGizmo::_update_transform_gizmo));
  391. Path3DEditorPlugin::singleton->curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  392. Path3DEditorPlugin::singleton->curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  393. Path3DEditorPlugin::singleton->curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  394. Path3DEditorPlugin::singleton->curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  395. Path3DEditorPlugin::singleton->curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  396. }
  397. EditorPlugin::AfterGUIInput Path3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  398. if (!path) {
  399. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  400. }
  401. Ref<Curve3D> c = path->get_curve();
  402. if (c.is_null()) {
  403. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  404. }
  405. Transform3D gt = path->get_global_transform();
  406. Transform3D it = gt.affine_inverse();
  407. static const int click_dist = 10; //should make global
  408. Ref<InputEventMouseButton> mb = p_event;
  409. if (mb.is_valid()) {
  410. Point2 mbpos(mb->get_position().x, mb->get_position().y);
  411. Node3DEditorViewport *viewport = nullptr;
  412. for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
  413. Node3DEditorViewport *vp = Node3DEditor::get_singleton()->get_editor_viewport(i);
  414. if (vp->get_camera_3d() == p_camera) {
  415. viewport = vp;
  416. break;
  417. }
  418. }
  419. ERR_FAIL_NULL_V(viewport, EditorPlugin::AFTER_GUI_INPUT_PASS);
  420. if (!mb->is_pressed()) {
  421. set_handle_clicked(false);
  422. }
  423. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->is_command_or_control_pressed()))) {
  424. //click into curve, break it down
  425. Vector<Vector3> v3a = c->tessellate();
  426. int rc = v3a.size();
  427. int closest_seg = -1;
  428. Vector3 closest_seg_point;
  429. if (rc >= 2) {
  430. int idx = 0;
  431. const Vector3 *r = v3a.ptr();
  432. float closest_d = 1e20;
  433. if (viewport->point_to_screen(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist) {
  434. return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
  435. }
  436. for (int i = 0; i < c->get_point_count() - 1; i++) {
  437. //find the offset and point index of the place to break up
  438. int j = idx;
  439. if (viewport->point_to_screen(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist) {
  440. return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
  441. }
  442. while (j < rc && c->get_point_position(i + 1) != r[j]) {
  443. Vector3 from = r[j];
  444. Vector3 to = r[j + 1];
  445. real_t cdist = from.distance_to(to);
  446. from = gt.xform(from);
  447. to = gt.xform(to);
  448. if (cdist > 0) {
  449. Vector2 s[2];
  450. s[0] = viewport->point_to_screen(from);
  451. s[1] = viewport->point_to_screen(to);
  452. Vector2 inters = Geometry2D::get_closest_point_to_segment(mbpos, s);
  453. float d = inters.distance_to(mbpos);
  454. if (d < 10 && d < closest_d) {
  455. closest_d = d;
  456. closest_seg = i;
  457. Vector3 ray_from = viewport->get_ray_pos(mbpos);
  458. Vector3 ray_dir = viewport->get_ray(mbpos);
  459. Vector3 ra, rb;
  460. Geometry3D::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
  461. closest_seg_point = it.xform(rb);
  462. }
  463. }
  464. j++;
  465. }
  466. if (idx == j) {
  467. idx++; //force next
  468. } else {
  469. idx = j; //swap
  470. }
  471. if (j == rc) {
  472. break;
  473. }
  474. }
  475. }
  476. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  477. if (closest_seg != -1) {
  478. //subdivide
  479. ur->create_action(TTR("Split Path"));
  480. ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
  481. ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
  482. ur->commit_action();
  483. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  484. } else {
  485. Vector3 origin;
  486. if (c->get_point_count() == 0) {
  487. origin = path->get_transform().get_origin();
  488. } else {
  489. origin = gt.xform(c->get_point_position(c->get_point_count() - 1));
  490. }
  491. Plane p(p_camera->get_transform().basis.get_column(2), origin);
  492. Vector3 ray_from = viewport->get_ray_pos(mbpos);
  493. Vector3 ray_dir = viewport->get_ray(mbpos);
  494. Vector3 inters;
  495. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  496. ur->create_action(TTR("Add Point to Curve"));
  497. ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
  498. ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
  499. ur->commit_action();
  500. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  501. }
  502. //add new at pos
  503. }
  504. } else if (mb->is_pressed() && ((mb->get_button_index() == MouseButton::LEFT && curve_del->is_pressed()) || (mb->get_button_index() == MouseButton::RIGHT && curve_edit->is_pressed()))) {
  505. for (int i = 0; i < c->get_point_count(); i++) {
  506. real_t dist_to_p = viewport->point_to_screen(gt.xform(c->get_point_position(i))).distance_to(mbpos);
  507. real_t dist_to_p_out = viewport->point_to_screen(gt.xform(c->get_point_position(i) + c->get_point_out(i))).distance_to(mbpos);
  508. real_t dist_to_p_in = viewport->point_to_screen(gt.xform(c->get_point_position(i) + c->get_point_in(i))).distance_to(mbpos);
  509. real_t dist_to_p_up = viewport->point_to_screen(gt.xform(c->get_point_position(i) + c->get_point_baked_posture(i, true).get_column(1) * disk_size)).distance_to(mbpos);
  510. // Find the offset and point index of the place to break up.
  511. // Also check for the control points.
  512. if (dist_to_p < click_dist) {
  513. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  514. ur->create_action(TTR("Remove Path Point"));
  515. ur->add_do_method(c.ptr(), "remove_point", i);
  516. ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i);
  517. ur->commit_action();
  518. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  519. } else if (dist_to_p_out < click_dist) {
  520. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  521. ur->create_action(TTR("Reset Out-Control Point"));
  522. ur->add_do_method(c.ptr(), "set_point_out", i, Vector3());
  523. ur->add_undo_method(c.ptr(), "set_point_out", i, c->get_point_out(i));
  524. ur->commit_action();
  525. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  526. } else if (dist_to_p_in < click_dist) {
  527. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  528. ur->create_action(TTR("Reset In-Control Point"));
  529. ur->add_do_method(c.ptr(), "set_point_in", i, Vector3());
  530. ur->add_undo_method(c.ptr(), "set_point_in", i, c->get_point_in(i));
  531. ur->commit_action();
  532. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  533. } else if (dist_to_p_up < click_dist) {
  534. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  535. ur->create_action(TTR("Reset Point Tilt"));
  536. ur->add_do_method(c.ptr(), "set_point_tilt", i, 0.0f);
  537. ur->add_undo_method(c.ptr(), "set_point_tilt", i, c->get_point_tilt(i));
  538. ur->commit_action();
  539. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  540. }
  541. }
  542. }
  543. }
  544. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  545. }
  546. void Path3DEditorPlugin::edit(Object *p_object) {
  547. if (p_object) {
  548. path = Object::cast_to<Path3D>(p_object);
  549. if (path) {
  550. if (path->get_curve().is_valid()) {
  551. path->get_curve()->emit_signal(CoreStringName(changed));
  552. }
  553. }
  554. } else {
  555. Path3D *pre = path;
  556. path = nullptr;
  557. if (pre) {
  558. pre->get_curve()->emit_signal(CoreStringName(changed));
  559. }
  560. }
  561. update_overlays();
  562. //collision_polygon_editor->edit(Object::cast_to<Node>(p_object));
  563. }
  564. bool Path3DEditorPlugin::handles(Object *p_object) const {
  565. return p_object->is_class("Path3D");
  566. }
  567. void Path3DEditorPlugin::make_visible(bool p_visible) {
  568. if (p_visible) {
  569. topmenu_bar->show();
  570. } else {
  571. topmenu_bar->hide();
  572. {
  573. Path3D *pre = path;
  574. path = nullptr;
  575. if (pre && pre->get_curve().is_valid()) {
  576. pre->get_curve()->emit_signal(CoreStringName(changed));
  577. }
  578. }
  579. }
  580. }
  581. void Path3DEditorPlugin::_mode_changed(int p_mode) {
  582. curve_create->set_pressed(p_mode == MODE_CREATE);
  583. curve_edit_curve->set_pressed(p_mode == MODE_EDIT_CURVE);
  584. curve_edit_tilt->set_pressed(p_mode == MODE_EDIT_TILT);
  585. curve_edit->set_pressed(p_mode == MODE_EDIT);
  586. curve_del->set_pressed(p_mode == MODE_DELETE);
  587. Node3DEditor::get_singleton()->clear_subgizmo_selection();
  588. }
  589. void Path3DEditorPlugin::_close_curve() {
  590. Ref<Curve3D> c = path->get_curve();
  591. if (c.is_null()) {
  592. return;
  593. }
  594. if (c->get_point_count() < 2) {
  595. return;
  596. }
  597. if (c->get_point_position(0) == c->get_point_position(c->get_point_count() - 1)) {
  598. return;
  599. }
  600. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  601. ur->create_action(TTR("Close Curve"));
  602. ur->add_do_method(c.ptr(), "add_point", c->get_point_position(0), c->get_point_in(0), c->get_point_out(0), -1);
  603. ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
  604. ur->commit_action();
  605. }
  606. void Path3DEditorPlugin::_handle_option_pressed(int p_option) {
  607. PopupMenu *pm;
  608. pm = handle_menu->get_popup();
  609. switch (p_option) {
  610. case HANDLE_OPTION_ANGLE: {
  611. bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
  612. mirror_handle_angle = !is_checked;
  613. pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  614. pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
  615. } break;
  616. case HANDLE_OPTION_LENGTH: {
  617. bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
  618. mirror_handle_length = !is_checked;
  619. pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  620. } break;
  621. }
  622. }
  623. void Path3DEditorPlugin::_confirm_clear_points() {
  624. if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
  625. return;
  626. }
  627. clear_points_dialog->reset_size();
  628. clear_points_dialog->popup_centered();
  629. }
  630. void Path3DEditorPlugin::_clear_points() {
  631. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  632. PackedVector3Array points = path->get_curve()->get_points().duplicate();
  633. undo_redo->create_action(TTR("Clear Curve Points"));
  634. undo_redo->add_do_method(this, "_clear_curve_points");
  635. undo_redo->add_undo_method(this, "_restore_curve_points", points);
  636. undo_redo->commit_action();
  637. }
  638. void Path3DEditorPlugin::_clear_curve_points() {
  639. if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
  640. return;
  641. }
  642. Ref<Curve3D> curve = path->get_curve();
  643. curve->clear_points();
  644. }
  645. void Path3DEditorPlugin::_restore_curve_points(const PackedVector3Array &p_points) {
  646. if (!path || path->get_curve().is_null()) {
  647. return;
  648. }
  649. Ref<Curve3D> curve = path->get_curve();
  650. if (curve->get_point_count() > 0) {
  651. curve->clear_points();
  652. }
  653. for (int i = 0; i < p_points.size(); i += 3) {
  654. curve->add_point(p_points[i + 2], p_points[i], p_points[i + 1]);
  655. }
  656. }
  657. void Path3DEditorPlugin::_update_theme() {
  658. // TODO: Split the EditorPlugin instance from the UI instance and connect this properly.
  659. // See the 2D path editor for inspiration.
  660. curve_edit->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveEdit"), EditorStringName(EditorIcons)));
  661. curve_edit_curve->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveCurve"), EditorStringName(EditorIcons)));
  662. curve_edit_tilt->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveTilt"), EditorStringName(EditorIcons)));
  663. curve_create->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveCreate"), EditorStringName(EditorIcons)));
  664. curve_del->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveDelete"), EditorStringName(EditorIcons)));
  665. curve_close->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("CurveClose"), EditorStringName(EditorIcons)));
  666. curve_clear_points->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Clear"), EditorStringName(EditorIcons)));
  667. }
  668. void Path3DEditorPlugin::_notification(int p_what) {
  669. switch (p_what) {
  670. case NOTIFICATION_ENTER_TREE: {
  671. curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_CREATE));
  672. curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_CURVE));
  673. curve_edit_tilt->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_TILT));
  674. curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT));
  675. curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_DELETE));
  676. curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_close_curve));
  677. _update_theme();
  678. } break;
  679. case NOTIFICATION_READY: {
  680. // FIXME: This can trigger theme updates when the nodes that we want to update are not yet available.
  681. // The toolbar should be extracted to a dedicated control and theme updates should be handled through
  682. // the notification.
  683. Node3DEditor::get_singleton()->connect(SceneStringName(theme_changed), callable_mp(this, &Path3DEditorPlugin::_update_theme));
  684. } break;
  685. }
  686. }
  687. void Path3DEditorPlugin::_bind_methods() {
  688. ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path3DEditorPlugin::_clear_curve_points);
  689. ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path3DEditorPlugin::_restore_curve_points);
  690. }
  691. Path3DEditorPlugin *Path3DEditorPlugin::singleton = nullptr;
  692. Path3DEditorPlugin::Path3DEditorPlugin() {
  693. path = nullptr;
  694. singleton = this;
  695. mirror_handle_angle = true;
  696. mirror_handle_length = true;
  697. disk_size = EDITOR_DEF_RST("editors/3d_gizmos/gizmo_settings/path3d_tilt_disk_size", 0.8);
  698. Ref<Path3DGizmoPlugin> gizmo_plugin = memnew(Path3DGizmoPlugin(disk_size));
  699. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  700. path_3d_gizmo_plugin = gizmo_plugin;
  701. topmenu_bar = memnew(HBoxContainer);
  702. topmenu_bar->hide();
  703. Node3DEditor::get_singleton()->add_control_to_menu_panel(topmenu_bar);
  704. curve_edit = memnew(Button);
  705. curve_edit->set_theme_type_variation("FlatButton");
  706. curve_edit->set_toggle_mode(true);
  707. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  708. curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Click: Select multiple Points") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point"));
  709. topmenu_bar->add_child(curve_edit);
  710. curve_edit_curve = memnew(Button);
  711. curve_edit_curve->set_theme_type_variation("FlatButton");
  712. curve_edit_curve->set_toggle_mode(true);
  713. curve_edit_curve->set_focus_mode(Control::FOCUS_NONE);
  714. curve_edit_curve->set_tooltip_text(TTR("Select Control Points") + "\n" + TTR("Shift+Click: Drag out Control Points"));
  715. topmenu_bar->add_child(curve_edit_curve);
  716. curve_edit_tilt = memnew(Button);
  717. curve_edit_tilt->set_theme_type_variation("FlatButton");
  718. curve_edit_tilt->set_toggle_mode(true);
  719. curve_edit_tilt->set_focus_mode(Control::FOCUS_NONE);
  720. curve_edit_tilt->set_tooltip_text(TTR("Select Tilt Handles"));
  721. topmenu_bar->add_child(curve_edit_tilt);
  722. curve_create = memnew(Button);
  723. curve_create->set_theme_type_variation("FlatButton");
  724. curve_create->set_toggle_mode(true);
  725. curve_create->set_focus_mode(Control::FOCUS_NONE);
  726. curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
  727. topmenu_bar->add_child(curve_create);
  728. curve_del = memnew(Button);
  729. curve_del->set_theme_type_variation("FlatButton");
  730. curve_del->set_toggle_mode(true);
  731. curve_del->set_focus_mode(Control::FOCUS_NONE);
  732. curve_del->set_tooltip_text(TTR("Delete Point"));
  733. topmenu_bar->add_child(curve_del);
  734. curve_close = memnew(Button);
  735. curve_close->set_theme_type_variation("FlatButton");
  736. curve_close->set_focus_mode(Control::FOCUS_NONE);
  737. curve_close->set_tooltip_text(TTR("Close Curve"));
  738. topmenu_bar->add_child(curve_close);
  739. curve_clear_points = memnew(Button);
  740. curve_clear_points->set_theme_type_variation("FlatButton");
  741. curve_clear_points->set_focus_mode(Control::FOCUS_NONE);
  742. curve_clear_points->set_tooltip_text(TTR("Clear Points"));
  743. curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_confirm_clear_points));
  744. topmenu_bar->add_child(curve_clear_points);
  745. clear_points_dialog = memnew(ConfirmationDialog);
  746. clear_points_dialog->set_title(TTR("Please Confirm..."));
  747. clear_points_dialog->set_text(TTR("Remove all curve points?"));
  748. clear_points_dialog->connect("confirmed", callable_mp(this, &Path3DEditorPlugin::_clear_points));
  749. topmenu_bar->add_child(clear_points_dialog);
  750. handle_menu = memnew(MenuButton);
  751. handle_menu->set_flat(false);
  752. handle_menu->set_theme_type_variation("FlatMenuButton");
  753. handle_menu->set_text(TTR("Options"));
  754. topmenu_bar->add_child(handle_menu);
  755. PopupMenu *menu;
  756. menu = handle_menu->get_popup();
  757. menu->add_check_item(TTR("Mirror Handle Angles"));
  758. menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  759. menu->add_check_item(TTR("Mirror Handle Lengths"));
  760. menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  761. menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path3DEditorPlugin::_handle_option_pressed));
  762. curve_edit->set_pressed(true);
  763. }
  764. Path3DEditorPlugin::~Path3DEditorPlugin() {
  765. }
  766. Ref<EditorNode3DGizmo> Path3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {
  767. Ref<Path3DGizmo> ref;
  768. Path3D *path = Object::cast_to<Path3D>(p_spatial);
  769. if (path) {
  770. ref = Ref<Path3DGizmo>(memnew(Path3DGizmo(path, disk_size)));
  771. }
  772. return ref;
  773. }
  774. bool Path3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  775. return Object::cast_to<Path3D>(p_spatial) != nullptr;
  776. }
  777. String Path3DGizmoPlugin::get_gizmo_name() const {
  778. return "Path3D";
  779. }
  780. void Path3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  781. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  782. ERR_FAIL_NULL(path);
  783. Ref<Curve3D> curve = path->get_curve();
  784. Ref<StandardMaterial3D> handle_material = get_material("handles", p_gizmo);
  785. PackedVector3Array handles;
  786. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  787. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  788. // Collect handles.
  789. const Vector3 pos = curve->get_point_position(idx);
  790. handles.append(pos);
  791. }
  792. }
  793. if (handles.size()) {
  794. p_gizmo->add_vertices(handles, handle_material, Mesh::PRIMITIVE_POINTS);
  795. }
  796. }
  797. int Path3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
  798. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  799. ERR_FAIL_NULL_V(path, -1);
  800. Ref<Curve3D> curve = path->get_curve();
  801. ERR_FAIL_COND_V(curve.is_null(), -1);
  802. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  803. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  804. Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
  805. if (p_camera->unproject_position(pos).distance_to(p_point) < 20) {
  806. return idx;
  807. }
  808. }
  809. }
  810. return -1;
  811. }
  812. Vector<int> Path3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
  813. Vector<int> contained_points;
  814. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  815. ERR_FAIL_NULL_V(path, contained_points);
  816. Ref<Curve3D> curve = path->get_curve();
  817. ERR_FAIL_COND_V(curve.is_null(), contained_points);
  818. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  819. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  820. Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
  821. bool is_contained_in_frustum = true;
  822. for (int i = 0; i < p_frustum.size(); ++i) {
  823. if (p_frustum[i].distance_to(pos) > 0) {
  824. is_contained_in_frustum = false;
  825. break;
  826. }
  827. }
  828. if (is_contained_in_frustum) {
  829. contained_points.push_back(idx);
  830. }
  831. }
  832. }
  833. return contained_points;
  834. }
  835. Transform3D Path3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
  836. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  837. ERR_FAIL_NULL_V(path, Transform3D());
  838. Ref<Curve3D> curve = path->get_curve();
  839. ERR_FAIL_COND_V(curve.is_null(), Transform3D());
  840. ERR_FAIL_INDEX_V(p_id, curve->get_point_count(), Transform3D());
  841. Basis basis = transformation_locked_basis.has(p_id) ? transformation_locked_basis[p_id] : curve->get_point_baked_posture(p_id, true);
  842. Vector3 pos = curve->get_point_position(p_id);
  843. Transform3D t = Transform3D(basis, pos);
  844. return t;
  845. }
  846. void Path3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
  847. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  848. ERR_FAIL_NULL(path);
  849. Ref<Curve3D> curve = path->get_curve();
  850. ERR_FAIL_COND(curve.is_null());
  851. ERR_FAIL_INDEX(p_id, curve->get_point_count());
  852. if (!transformation_locked_basis.has(p_id)) {
  853. transformation_locked_basis[p_id] = Basis(curve->get_point_baked_posture(p_id, true));
  854. }
  855. curve->set_point_position(p_id, p_transform.origin);
  856. }
  857. void Path3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
  858. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  859. ERR_FAIL_NULL(path);
  860. Ref<Curve3D> curve = path->get_curve();
  861. ERR_FAIL_COND(curve.is_null());
  862. transformation_locked_basis.clear();
  863. if (p_cancel) {
  864. for (int i = 0; i < p_ids.size(); ++i) {
  865. curve->set_point_position(p_ids[i], p_restore[i].origin);
  866. }
  867. return;
  868. }
  869. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  870. undo_redo->create_action(TTR("Set Curve Point Position"));
  871. for (int i = 0; i < p_ids.size(); ++i) {
  872. const int idx = p_ids[i];
  873. undo_redo->add_do_method(curve.ptr(), "set_point_position", idx, curve->get_point_position(idx));
  874. undo_redo->add_undo_method(curve.ptr(), "set_point_position", idx, p_restore[i].origin);
  875. }
  876. undo_redo->commit_action();
  877. }
  878. int Path3DGizmoPlugin::get_priority() const {
  879. return -1;
  880. }
  881. Path3DGizmoPlugin::Path3DGizmoPlugin(float p_disk_size) {
  882. Color path_color = SceneTree::get_singleton()->get_debug_paths_color();
  883. Color path_tilt_color = EDITOR_DEF_RST("editors/3d_gizmos/gizmo_colors/path_tilt", Color(1.0, 1.0, 0.4, 0.9));
  884. disk_size = p_disk_size;
  885. create_material("path_material", path_color);
  886. create_material("path_thin_material", Color(0.6, 0.6, 0.6));
  887. create_material("path_tilt_material", path_tilt_color);
  888. create_material("path_tilt_muted_material", path_tilt_color * 0.7);
  889. create_handle_material("handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
  890. create_handle_material("sec_handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorCurveHandle"), EditorStringName(EditorIcons)));
  891. }