path_3d_editor_plugin.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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> first_pt_handle_material = gizmo_plugin->get_material("first_pt_handle");
  243. Ref<StandardMaterial3D> last_pt_handle_material = gizmo_plugin->get_material("last_pt_handle");
  244. Ref<StandardMaterial3D> closed_pt_handle_material = gizmo_plugin->get_material("closed_pt_handle");
  245. Ref<StandardMaterial3D> sec_handles_material = gizmo_plugin->get_material("sec_handles");
  246. first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));
  247. last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));
  248. closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));
  249. Ref<Curve3D> c = path->get_curve();
  250. if (c.is_null()) {
  251. return;
  252. }
  253. real_t interval = 0.1;
  254. const real_t length = c->get_baked_length();
  255. // 1. Draw curve and bones.
  256. if (length > CMP_EPSILON) {
  257. const int sample_count = int(length / interval) + 2;
  258. interval = length / (sample_count - 1); // Recalculate real interval length.
  259. Vector<Transform3D> frames;
  260. frames.resize(sample_count);
  261. {
  262. Transform3D *w = frames.ptrw();
  263. for (int i = 0; i < sample_count; i++) {
  264. w[i] = c->sample_baked_with_rotation(i * interval, true, true);
  265. }
  266. }
  267. const Transform3D *r = frames.ptr();
  268. Vector<Vector3> _collision_segments;
  269. _collision_segments.resize((sample_count - 1) * 2);
  270. Vector3 *_collisions_ptr = _collision_segments.ptrw();
  271. Vector<Vector3> bones;
  272. bones.resize(sample_count * 4);
  273. Vector3 *bones_ptr = bones.ptrw();
  274. Vector<Vector3> ribbon;
  275. ribbon.resize(sample_count);
  276. Vector3 *ribbon_ptr = ribbon.ptrw();
  277. for (int i = 0; i < sample_count; i++) {
  278. const Vector3 p1 = r[i].origin;
  279. const Vector3 side = r[i].basis.get_column(0);
  280. const Vector3 up = r[i].basis.get_column(1);
  281. const Vector3 forward = r[i].basis.get_column(2);
  282. // Collision segments.
  283. if (i != sample_count - 1) {
  284. const Vector3 p2 = r[i + 1].origin;
  285. _collisions_ptr[(i * 2)] = p1;
  286. _collisions_ptr[(i * 2) + 1] = p2;
  287. }
  288. // Path3D as a ribbon.
  289. ribbon_ptr[i] = p1;
  290. if (i % 4 == 0) {
  291. // Draw fish bone every 4 points to reduce visual noise and performance impact
  292. // (compared to drawing it for every point).
  293. const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
  294. const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
  295. const int bone_idx = i * 4;
  296. bones_ptr[bone_idx] = p1;
  297. bones_ptr[bone_idx + 1] = p_left;
  298. bones_ptr[bone_idx + 2] = p1;
  299. bones_ptr[bone_idx + 3] = p_right;
  300. }
  301. }
  302. add_collision_segments(_collision_segments);
  303. add_lines(bones, path_material);
  304. add_vertices(ribbon, path_material, Mesh::PRIMITIVE_LINE_STRIP);
  305. }
  306. // 2. Draw handles when selected.
  307. if (Path3DEditorPlugin::singleton->get_edited_path() == path) {
  308. PackedVector3Array handle_lines;
  309. PackedVector3Array tilt_handle_lines;
  310. PackedVector3Array primary_handle_points;
  311. PackedVector3Array secondary_handle_points;
  312. PackedInt32Array collected_secondary_handle_ids; // Avoid shadowing member on Node3DEditorGizmo.
  313. _secondary_handles_info.resize(c->get_point_count() * 3);
  314. for (int idx = 0; idx < c->get_point_count(); idx++) {
  315. // Collect primary-handles.
  316. const Vector3 pos = c->get_point_position(idx);
  317. primary_handle_points.append(pos);
  318. HandleInfo info;
  319. info.point_idx = idx;
  320. // Collect in-handles except for the first point.
  321. if (idx > (c->is_closed() ? -1 : 0) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
  322. const Vector3 in = c->get_point_in(idx);
  323. info.type = HandleType::HANDLE_TYPE_IN;
  324. const int handle_idx = idx * 3 + 0;
  325. collected_secondary_handle_ids.append(handle_idx);
  326. _secondary_handles_info.write[handle_idx] = info;
  327. secondary_handle_points.append(pos + in);
  328. handle_lines.append(pos);
  329. handle_lines.append(pos + in);
  330. }
  331. // Collect out-handles except for the last point.
  332. if (idx < (c->is_closed() ? c->get_point_count() : c->get_point_count() - 1) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
  333. const Vector3 out = c->get_point_out(idx);
  334. info.type = HandleType::HANDLE_TYPE_OUT;
  335. const int handle_idx = idx * 3 + 1;
  336. collected_secondary_handle_ids.append(handle_idx);
  337. _secondary_handles_info.write[handle_idx] = info;
  338. secondary_handle_points.append(pos + out);
  339. handle_lines.append(pos);
  340. handle_lines.append(pos + out);
  341. }
  342. // Collect tilt-handles.
  343. if (Path3DEditorPlugin::singleton->curve_edit_tilt->is_pressed()) {
  344. // Tilt handle.
  345. {
  346. info.type = HandleType::HANDLE_TYPE_TILT;
  347. const int handle_idx = idx * 3 + 2;
  348. collected_secondary_handle_ids.append(handle_idx);
  349. _secondary_handles_info.write[handle_idx] = info;
  350. const Basis posture = c->get_point_baked_posture(idx, true);
  351. const Vector3 up = posture.get_column(1);
  352. secondary_handle_points.append(pos + up * disk_size);
  353. tilt_handle_lines.append(pos);
  354. tilt_handle_lines.append(pos + up * disk_size);
  355. }
  356. // Tilt disk.
  357. {
  358. const Basis posture = c->get_point_baked_posture(idx, false);
  359. const Vector3 up = posture.get_column(1);
  360. const Vector3 side = posture.get_column(0);
  361. PackedVector3Array disk;
  362. disk.append(pos);
  363. const int n = 36;
  364. for (int i = 0; i <= n; i++) {
  365. const float a = Math_TAU * i / n;
  366. const Vector3 edge = sin(a) * side + cos(a) * up;
  367. disk.append(pos + edge * disk_size);
  368. }
  369. add_vertices(disk, path_tilt_material, Mesh::PRIMITIVE_LINE_STRIP);
  370. }
  371. }
  372. }
  373. if (handle_lines.size() > 1) {
  374. add_lines(handle_lines, path_thin_material);
  375. }
  376. if (tilt_handle_lines.size() > 1) {
  377. add_lines(tilt_handle_lines, path_tilt_material);
  378. }
  379. if (!Path3DEditorPlugin::singleton->curve_edit->is_pressed() && primary_handle_points.size()) {
  380. // Need to define indices separately.
  381. // Point count.
  382. const int pc = primary_handle_points.size();
  383. Vector<int> idx;
  384. idx.resize(pc);
  385. int *idx_ptr = idx.ptrw();
  386. for (int j = 0; j < pc; j++) {
  387. idx_ptr[j] = j;
  388. }
  389. // Initialize arrays for first point.
  390. PackedVector3Array first_pt_handle_point;
  391. Vector<int> first_pt_id;
  392. first_pt_handle_point.append(primary_handle_points[0]);
  393. first_pt_id.append(idx[0]);
  394. // Initialize arrays and add handle for last point if needed.
  395. if (pc > 1) {
  396. PackedVector3Array last_pt_handle_point;
  397. Vector<int> last_pt_id;
  398. last_pt_handle_point.append(primary_handle_points[pc - 1]);
  399. last_pt_id.append(idx[pc - 1]);
  400. primary_handle_points.remove_at(pc - 1);
  401. idx.remove_at(pc - 1);
  402. add_handles(last_pt_handle_point, c->is_closed() ? handles_material : last_pt_handle_material, last_pt_id);
  403. }
  404. // Add handle for first point.
  405. primary_handle_points.remove_at(0);
  406. idx.remove_at(0);
  407. add_handles(first_pt_handle_point, c->is_closed() ? closed_pt_handle_material : first_pt_handle_material, first_pt_id);
  408. // Add handles for remaining intermediate points.
  409. if (!primary_handle_points.is_empty()) {
  410. add_handles(primary_handle_points, handles_material, idx);
  411. }
  412. }
  413. if (secondary_handle_points.size()) {
  414. add_handles(secondary_handle_points, sec_handles_material, collected_secondary_handle_ids, false, true);
  415. }
  416. // Draw the gizmo plugin manually, because handles are registered. In which case, the caller code skips drawing the gizmo plugin.
  417. gizmo_plugin->redraw(this);
  418. }
  419. }
  420. void Path3DGizmo::_update_transform_gizmo() {
  421. Node3DEditor::get_singleton()->update_transform_gizmo();
  422. }
  423. Path3DGizmo::Path3DGizmo(Path3D *p_path, float p_disk_size) {
  424. path = p_path;
  425. disk_size = p_disk_size;
  426. set_node_3d(p_path);
  427. orig_in_length = 0;
  428. orig_out_length = 0;
  429. // Connecting to a signal once, rather than plaguing the implementation with calls to `Node3DEditor::update_transform_gizmo`.
  430. path->connect("curve_changed", callable_mp(this, &Path3DGizmo::_update_transform_gizmo));
  431. Path3DEditorPlugin::singleton->curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  432. Path3DEditorPlugin::singleton->curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  433. Path3DEditorPlugin::singleton->curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  434. Path3DEditorPlugin::singleton->curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  435. Path3DEditorPlugin::singleton->curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
  436. }
  437. EditorPlugin::AfterGUIInput Path3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  438. if (!path) {
  439. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  440. }
  441. Ref<Curve3D> c = path->get_curve();
  442. if (c.is_null()) {
  443. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  444. }
  445. Transform3D gt = path->get_global_transform();
  446. Transform3D it = gt.affine_inverse();
  447. static const int click_dist = 10; //should make global
  448. Ref<InputEventMouseButton> mb = p_event;
  449. if (mb.is_valid()) {
  450. Point2 mbpos(mb->get_position().x, mb->get_position().y);
  451. Node3DEditorViewport *viewport = nullptr;
  452. for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
  453. Node3DEditorViewport *vp = Node3DEditor::get_singleton()->get_editor_viewport(i);
  454. if (vp->get_camera_3d() == p_camera) {
  455. viewport = vp;
  456. break;
  457. }
  458. }
  459. ERR_FAIL_NULL_V(viewport, EditorPlugin::AFTER_GUI_INPUT_PASS);
  460. if (!mb->is_pressed()) {
  461. set_handle_clicked(false);
  462. }
  463. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->is_command_or_control_pressed()))) {
  464. //click into curve, break it down
  465. Vector<Vector3> v3a = c->tessellate();
  466. int rc = v3a.size();
  467. int closest_seg = -1;
  468. Vector3 closest_seg_point;
  469. if (rc >= 2) {
  470. int idx = 0;
  471. const Vector3 *r = v3a.ptr();
  472. float closest_d = 1e20;
  473. if (viewport->point_to_screen(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist) {
  474. return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
  475. }
  476. for (int i = 0; i < c->get_point_count() - 1; i++) {
  477. //find the offset and point index of the place to break up
  478. int j = idx;
  479. if (viewport->point_to_screen(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist) {
  480. return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
  481. }
  482. while (j < rc && c->get_point_position(i + 1) != r[j]) {
  483. Vector3 from = r[j];
  484. Vector3 to = r[j + 1];
  485. real_t cdist = from.distance_to(to);
  486. from = gt.xform(from);
  487. to = gt.xform(to);
  488. if (cdist > 0) {
  489. Vector2 s[2];
  490. s[0] = viewport->point_to_screen(from);
  491. s[1] = viewport->point_to_screen(to);
  492. Vector2 inters = Geometry2D::get_closest_point_to_segment(mbpos, s);
  493. float d = inters.distance_to(mbpos);
  494. if (d < 10 && d < closest_d) {
  495. closest_d = d;
  496. closest_seg = i;
  497. Vector3 ray_from = viewport->get_ray_pos(mbpos);
  498. Vector3 ray_dir = viewport->get_ray(mbpos);
  499. Vector3 ra, rb;
  500. Geometry3D::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
  501. closest_seg_point = it.xform(rb);
  502. }
  503. }
  504. j++;
  505. }
  506. if (idx == j) {
  507. idx++; //force next
  508. } else {
  509. idx = j; //swap
  510. }
  511. if (j == rc) {
  512. break;
  513. }
  514. }
  515. }
  516. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  517. if (closest_seg != -1) {
  518. //subdivide
  519. ur->create_action(TTR("Split Path"));
  520. ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
  521. ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
  522. ur->commit_action();
  523. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  524. } else {
  525. Vector3 origin;
  526. if (c->get_point_count() == 0) {
  527. origin = path->get_transform().get_origin();
  528. } else {
  529. origin = gt.xform(c->get_point_position(c->get_point_count() - 1));
  530. }
  531. Plane p(p_camera->get_transform().basis.get_column(2), origin);
  532. Vector3 ray_from = viewport->get_ray_pos(mbpos);
  533. Vector3 ray_dir = viewport->get_ray(mbpos);
  534. Vector3 inters;
  535. if (p.intersects_ray(ray_from, ray_dir, &inters)) {
  536. ur->create_action(TTR("Add Point to Curve"));
  537. ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
  538. ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
  539. ur->commit_action();
  540. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  541. }
  542. //add new at pos
  543. }
  544. } 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()))) {
  545. for (int i = 0; i < c->get_point_count(); i++) {
  546. real_t dist_to_p = viewport->point_to_screen(gt.xform(c->get_point_position(i))).distance_to(mbpos);
  547. 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);
  548. 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);
  549. 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);
  550. // Find the offset and point index of the place to break up.
  551. // Also check for the control points.
  552. if (dist_to_p < click_dist) {
  553. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  554. ur->create_action(TTR("Remove Path Point"));
  555. ur->add_do_method(c.ptr(), "remove_point", i);
  556. ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i);
  557. ur->commit_action();
  558. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  559. } else if (dist_to_p_out < click_dist) {
  560. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  561. ur->create_action(TTR("Reset Out-Control Point"));
  562. ur->add_do_method(c.ptr(), "set_point_out", i, Vector3());
  563. ur->add_undo_method(c.ptr(), "set_point_out", i, c->get_point_out(i));
  564. ur->commit_action();
  565. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  566. } else if (dist_to_p_in < click_dist) {
  567. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  568. ur->create_action(TTR("Reset In-Control Point"));
  569. ur->add_do_method(c.ptr(), "set_point_in", i, Vector3());
  570. ur->add_undo_method(c.ptr(), "set_point_in", i, c->get_point_in(i));
  571. ur->commit_action();
  572. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  573. } else if (dist_to_p_up < click_dist) {
  574. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  575. ur->create_action(TTR("Reset Point Tilt"));
  576. ur->add_do_method(c.ptr(), "set_point_tilt", i, 0.0f);
  577. ur->add_undo_method(c.ptr(), "set_point_tilt", i, c->get_point_tilt(i));
  578. ur->commit_action();
  579. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  580. }
  581. }
  582. }
  583. }
  584. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  585. }
  586. void Path3DEditorPlugin::edit(Object *p_object) {
  587. if (p_object) {
  588. path = Object::cast_to<Path3D>(p_object);
  589. if (path) {
  590. if (path->get_curve().is_valid()) {
  591. path->get_curve()->emit_signal(CoreStringName(changed));
  592. }
  593. _update_toolbar();
  594. }
  595. } else {
  596. Path3D *pre = path;
  597. path = nullptr;
  598. if (pre) {
  599. pre->get_curve()->emit_signal(CoreStringName(changed));
  600. }
  601. }
  602. update_overlays();
  603. //collision_polygon_editor->edit(Object::cast_to<Node>(p_object));
  604. }
  605. bool Path3DEditorPlugin::handles(Object *p_object) const {
  606. return p_object->is_class("Path3D");
  607. }
  608. void Path3DEditorPlugin::make_visible(bool p_visible) {
  609. if (p_visible) {
  610. topmenu_bar->show();
  611. } else {
  612. topmenu_bar->hide();
  613. {
  614. Path3D *pre = path;
  615. path = nullptr;
  616. if (pre && pre->get_curve().is_valid()) {
  617. pre->get_curve()->emit_signal(CoreStringName(changed));
  618. }
  619. }
  620. }
  621. }
  622. void Path3DEditorPlugin::_mode_changed(int p_mode) {
  623. curve_create->set_pressed_no_signal(p_mode == MODE_CREATE);
  624. curve_edit_curve->set_pressed_no_signal(p_mode == MODE_EDIT_CURVE);
  625. curve_edit_tilt->set_pressed_no_signal(p_mode == MODE_EDIT_TILT);
  626. curve_edit->set_pressed_no_signal(p_mode == MODE_EDIT);
  627. curve_del->set_pressed_no_signal(p_mode == MODE_DELETE);
  628. Node3DEditor::get_singleton()->clear_subgizmo_selection();
  629. }
  630. void Path3DEditorPlugin::_toggle_closed_curve() {
  631. Ref<Curve3D> c = path->get_curve();
  632. if (c.is_null()) {
  633. return;
  634. }
  635. if (c->get_point_count() < 2) {
  636. return;
  637. }
  638. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  639. ur->create_action(TTR("Toggle Open/Closed Curve"));
  640. ur->add_do_method(c.ptr(), "set_closed", !c.ptr()->is_closed());
  641. ur->add_undo_method(c.ptr(), "set_closed", c.ptr()->is_closed());
  642. ur->commit_action();
  643. }
  644. void Path3DEditorPlugin::_handle_option_pressed(int p_option) {
  645. PopupMenu *pm;
  646. pm = handle_menu->get_popup();
  647. switch (p_option) {
  648. case HANDLE_OPTION_ANGLE: {
  649. bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
  650. mirror_handle_angle = !is_checked;
  651. pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  652. pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
  653. } break;
  654. case HANDLE_OPTION_LENGTH: {
  655. bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
  656. mirror_handle_length = !is_checked;
  657. pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  658. } break;
  659. }
  660. }
  661. void Path3DEditorPlugin::_create_curve() {
  662. ERR_FAIL_NULL(path);
  663. Ref<Curve3D> new_curve;
  664. new_curve.instantiate();
  665. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  666. undo_redo->create_action(TTR("Create Curve in Path3D"));
  667. undo_redo->add_do_property(path, "curve", new_curve);
  668. undo_redo->add_undo_property(path, "curve", Ref<Curve3D>());
  669. undo_redo->add_do_method(this, "_update_toolbar");
  670. undo_redo->add_undo_method(this, "_update_toolbar");
  671. undo_redo->commit_action();
  672. }
  673. void Path3DEditorPlugin::_confirm_clear_points() {
  674. if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
  675. return;
  676. }
  677. clear_points_dialog->reset_size();
  678. clear_points_dialog->popup_centered();
  679. }
  680. void Path3DEditorPlugin::_clear_points() {
  681. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  682. PackedVector3Array points = path->get_curve()->get_points().duplicate();
  683. undo_redo->create_action(TTR("Clear Curve Points"));
  684. undo_redo->add_do_method(this, "_clear_curve_points");
  685. undo_redo->add_undo_method(this, "_restore_curve_points", points);
  686. undo_redo->commit_action();
  687. }
  688. void Path3DEditorPlugin::_clear_curve_points() {
  689. if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
  690. return;
  691. }
  692. Ref<Curve3D> curve = path->get_curve();
  693. curve->set_closed(false);
  694. curve->clear_points();
  695. }
  696. void Path3DEditorPlugin::_restore_curve_points(const PackedVector3Array &p_points) {
  697. if (!path || path->get_curve().is_null()) {
  698. return;
  699. }
  700. Ref<Curve3D> curve = path->get_curve();
  701. if (curve->get_point_count() > 0) {
  702. curve->clear_points();
  703. }
  704. for (int i = 0; i < p_points.size(); i += 3) {
  705. curve->add_point(p_points[i + 2], p_points[i], p_points[i + 1]);
  706. }
  707. }
  708. void Path3DEditorPlugin::_update_theme() {
  709. curve_edit->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveEdit")));
  710. curve_edit_curve->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCurve")));
  711. curve_edit_tilt->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveTilt")));
  712. curve_create->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCreate")));
  713. curve_del->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveDelete")));
  714. curve_closed->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveClose")));
  715. curve_clear_points->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Clear")));
  716. create_curve_button->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Curve3D")));
  717. }
  718. void Path3DEditorPlugin::_update_toolbar() {
  719. if (!path) {
  720. return;
  721. }
  722. bool has_curve = path->get_curve().is_valid();
  723. toolbar->set_visible(has_curve);
  724. create_curve_button->set_visible(!has_curve);
  725. }
  726. void Path3DEditorPlugin::_bind_methods() {
  727. ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path3DEditorPlugin::_update_toolbar);
  728. ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path3DEditorPlugin::_clear_curve_points);
  729. ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path3DEditorPlugin::_restore_curve_points);
  730. }
  731. Path3DEditorPlugin::Path3DEditorPlugin() {
  732. singleton = this;
  733. mirror_handle_angle = true;
  734. mirror_handle_length = true;
  735. disk_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/path3d_tilt_disk_size");
  736. Ref<Path3DGizmoPlugin> gizmo_plugin = memnew(Path3DGizmoPlugin(disk_size));
  737. Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
  738. path_3d_gizmo_plugin = gizmo_plugin;
  739. topmenu_bar = memnew(HBoxContainer);
  740. topmenu_bar->hide();
  741. toolbar = memnew(HBoxContainer);
  742. topmenu_bar->add_child(toolbar);
  743. curve_edit = memnew(Button);
  744. curve_edit->set_theme_type_variation(SceneStringName(FlatButton));
  745. curve_edit->set_toggle_mode(true);
  746. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  747. 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"));
  748. toolbar->add_child(curve_edit);
  749. curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT));
  750. curve_edit_curve = memnew(Button);
  751. curve_edit_curve->set_theme_type_variation(SceneStringName(FlatButton));
  752. curve_edit_curve->set_toggle_mode(true);
  753. curve_edit_curve->set_focus_mode(Control::FOCUS_NONE);
  754. curve_edit_curve->set_tooltip_text(TTR("Select Control Points") + "\n" + TTR("Shift+Click: Drag out Control Points"));
  755. toolbar->add_child(curve_edit_curve);
  756. curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_CURVE));
  757. curve_edit_tilt = memnew(Button);
  758. curve_edit_tilt->set_theme_type_variation(SceneStringName(FlatButton));
  759. curve_edit_tilt->set_toggle_mode(true);
  760. curve_edit_tilt->set_focus_mode(Control::FOCUS_NONE);
  761. curve_edit_tilt->set_tooltip_text(TTR("Select Tilt Handles"));
  762. toolbar->add_child(curve_edit_tilt);
  763. curve_edit_tilt->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_TILT));
  764. curve_create = memnew(Button);
  765. curve_create->set_theme_type_variation(SceneStringName(FlatButton));
  766. curve_create->set_toggle_mode(true);
  767. curve_create->set_focus_mode(Control::FOCUS_NONE);
  768. curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
  769. toolbar->add_child(curve_create);
  770. curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_CREATE));
  771. curve_del = memnew(Button);
  772. curve_del->set_theme_type_variation(SceneStringName(FlatButton));
  773. curve_del->set_toggle_mode(true);
  774. curve_del->set_focus_mode(Control::FOCUS_NONE);
  775. curve_del->set_tooltip_text(TTR("Delete Point"));
  776. toolbar->add_child(curve_del);
  777. curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_DELETE));
  778. curve_closed = memnew(Button);
  779. curve_closed->set_theme_type_variation(SceneStringName(FlatButton));
  780. curve_closed->set_focus_mode(Control::FOCUS_NONE);
  781. curve_closed->set_tooltip_text(TTR("Close Curve"));
  782. toolbar->add_child(curve_closed);
  783. curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_toggle_closed_curve));
  784. curve_clear_points = memnew(Button);
  785. curve_clear_points->set_theme_type_variation(SceneStringName(FlatButton));
  786. curve_clear_points->set_focus_mode(Control::FOCUS_NONE);
  787. curve_clear_points->set_tooltip_text(TTR("Clear Points"));
  788. curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_confirm_clear_points));
  789. toolbar->add_child(curve_clear_points);
  790. clear_points_dialog = memnew(ConfirmationDialog);
  791. clear_points_dialog->set_title(TTR("Please Confirm..."));
  792. clear_points_dialog->set_text(TTR("Remove all curve points?"));
  793. clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path3DEditorPlugin::_clear_points));
  794. toolbar->add_child(clear_points_dialog);
  795. handle_menu = memnew(MenuButton);
  796. handle_menu->set_flat(false);
  797. handle_menu->set_theme_type_variation("FlatMenuButton");
  798. handle_menu->set_text(TTR("Options"));
  799. toolbar->add_child(handle_menu);
  800. create_curve_button = memnew(Button);
  801. create_curve_button->set_text(TTR("Create Curve"));
  802. create_curve_button->hide();
  803. topmenu_bar->add_child(create_curve_button);
  804. create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_create_curve));
  805. PopupMenu *menu = handle_menu->get_popup();
  806. menu->add_check_item(TTR("Mirror Handle Angles"));
  807. menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  808. menu->add_check_item(TTR("Mirror Handle Lengths"));
  809. menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  810. menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path3DEditorPlugin::_handle_option_pressed));
  811. curve_edit->set_pressed_no_signal(true);
  812. topmenu_bar->connect(SceneStringName(theme_changed), callable_mp(this, &Path3DEditorPlugin::_update_theme));
  813. Node3DEditor::get_singleton()->add_control_to_menu_panel(topmenu_bar);
  814. }
  815. Ref<EditorNode3DGizmo> Path3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {
  816. Ref<Path3DGizmo> ref;
  817. Path3D *path = Object::cast_to<Path3D>(p_spatial);
  818. if (path) {
  819. ref.instantiate(path, disk_size);
  820. }
  821. return ref;
  822. }
  823. bool Path3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  824. return Object::cast_to<Path3D>(p_spatial) != nullptr;
  825. }
  826. String Path3DGizmoPlugin::get_gizmo_name() const {
  827. return "Path3D";
  828. }
  829. void Path3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  830. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  831. ERR_FAIL_NULL(path);
  832. Ref<Curve3D> curve = path->get_curve();
  833. Ref<StandardMaterial3D> handle_material = get_material("handles", p_gizmo);
  834. Ref<StandardMaterial3D> first_pt_handle_material = get_material("first_pt_handle", p_gizmo);
  835. Ref<StandardMaterial3D> last_pt_handle_material = get_material("last_pt_handle", p_gizmo);
  836. Ref<StandardMaterial3D> closed_pt_handle_material = get_material("closed_pt_handle", p_gizmo);
  837. first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));
  838. last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));
  839. closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));
  840. PackedVector3Array handles;
  841. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  842. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  843. // Collect handles.
  844. const Vector3 pos = curve->get_point_position(idx);
  845. handles.append(pos);
  846. }
  847. }
  848. if (handles.size()) {
  849. // Point count.
  850. const int pc = handles.size();
  851. // Initialize arrays for first point.
  852. PackedVector3Array first_pt;
  853. first_pt.append(handles[0]);
  854. // Initialize arrays and add handle for last point if needed.
  855. if (pc > 1) {
  856. PackedVector3Array last_pt;
  857. last_pt.append(handles[handles.size() - 1]);
  858. handles.remove_at(handles.size() - 1);
  859. if (curve->is_closed()) {
  860. p_gizmo->add_vertices(last_pt, handle_material, Mesh::PRIMITIVE_POINTS);
  861. } else {
  862. p_gizmo->add_vertices(last_pt, last_pt_handle_material, Mesh::PRIMITIVE_POINTS);
  863. }
  864. }
  865. // Add handle for first point.
  866. handles.remove_at(0);
  867. if (curve->is_closed()) {
  868. p_gizmo->add_vertices(first_pt, closed_pt_handle_material, Mesh::PRIMITIVE_POINTS);
  869. } else {
  870. p_gizmo->add_vertices(first_pt, first_pt_handle_material, Mesh::PRIMITIVE_POINTS);
  871. }
  872. // Add handles for remaining intermediate points.
  873. if (!handles.is_empty()) {
  874. p_gizmo->add_vertices(handles, handle_material, Mesh::PRIMITIVE_POINTS);
  875. }
  876. }
  877. }
  878. int Path3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
  879. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  880. ERR_FAIL_NULL_V(path, -1);
  881. Ref<Curve3D> curve = path->get_curve();
  882. ERR_FAIL_COND_V(curve.is_null(), -1);
  883. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  884. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  885. Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
  886. if (p_camera->unproject_position(pos).distance_to(p_point) < 20) {
  887. return idx;
  888. }
  889. }
  890. }
  891. return -1;
  892. }
  893. Vector<int> Path3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
  894. Vector<int> contained_points;
  895. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  896. ERR_FAIL_NULL_V(path, contained_points);
  897. Ref<Curve3D> curve = path->get_curve();
  898. ERR_FAIL_COND_V(curve.is_null(), contained_points);
  899. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  900. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  901. Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
  902. bool is_contained_in_frustum = true;
  903. for (int i = 0; i < p_frustum.size(); ++i) {
  904. if (p_frustum[i].distance_to(pos) > 0) {
  905. is_contained_in_frustum = false;
  906. break;
  907. }
  908. }
  909. if (is_contained_in_frustum) {
  910. contained_points.push_back(idx);
  911. }
  912. }
  913. }
  914. return contained_points;
  915. }
  916. Transform3D Path3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
  917. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  918. ERR_FAIL_NULL_V(path, Transform3D());
  919. Ref<Curve3D> curve = path->get_curve();
  920. ERR_FAIL_COND_V(curve.is_null(), Transform3D());
  921. ERR_FAIL_INDEX_V(p_id, curve->get_point_count(), Transform3D());
  922. Basis basis = transformation_locked_basis.has(p_id) ? transformation_locked_basis[p_id] : curve->get_point_baked_posture(p_id, true);
  923. Vector3 pos = curve->get_point_position(p_id);
  924. Transform3D t = Transform3D(basis, pos);
  925. return t;
  926. }
  927. void Path3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
  928. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  929. ERR_FAIL_NULL(path);
  930. Ref<Curve3D> curve = path->get_curve();
  931. ERR_FAIL_COND(curve.is_null());
  932. ERR_FAIL_INDEX(p_id, curve->get_point_count());
  933. if (!transformation_locked_basis.has(p_id)) {
  934. transformation_locked_basis[p_id] = Basis(curve->get_point_baked_posture(p_id, true));
  935. }
  936. curve->set_point_position(p_id, p_transform.origin);
  937. }
  938. void Path3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
  939. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  940. ERR_FAIL_NULL(path);
  941. Ref<Curve3D> curve = path->get_curve();
  942. ERR_FAIL_COND(curve.is_null());
  943. transformation_locked_basis.clear();
  944. if (p_cancel) {
  945. for (int i = 0; i < p_ids.size(); ++i) {
  946. curve->set_point_position(p_ids[i], p_restore[i].origin);
  947. }
  948. return;
  949. }
  950. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  951. undo_redo->create_action(TTR("Set Curve Point Position"));
  952. for (int i = 0; i < p_ids.size(); ++i) {
  953. const int idx = p_ids[i];
  954. undo_redo->add_do_method(curve.ptr(), "set_point_position", idx, curve->get_point_position(idx));
  955. undo_redo->add_undo_method(curve.ptr(), "set_point_position", idx, p_restore[i].origin);
  956. }
  957. undo_redo->commit_action();
  958. }
  959. int Path3DGizmoPlugin::get_priority() const {
  960. return -1;
  961. }
  962. Path3DGizmoPlugin::Path3DGizmoPlugin(float p_disk_size) {
  963. Color path_color = SceneTree::get_singleton()->get_debug_paths_color();
  964. Color path_tilt_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/path_tilt");
  965. disk_size = p_disk_size;
  966. create_material("path_material", path_color);
  967. create_material("path_thin_material", Color(0.6, 0.6, 0.6));
  968. create_material("path_tilt_material", path_tilt_color);
  969. create_material("path_tilt_muted_material", path_tilt_color * 0.7);
  970. create_handle_material("handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
  971. create_handle_material("first_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
  972. create_handle_material("last_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
  973. create_handle_material("closed_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
  974. create_handle_material("sec_handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorCurveHandle"), EditorStringName(EditorIcons)));
  975. }