path_3d_editor_plugin.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. _update_toolbar();
  554. }
  555. } else {
  556. Path3D *pre = path;
  557. path = nullptr;
  558. if (pre) {
  559. pre->get_curve()->emit_signal(CoreStringName(changed));
  560. }
  561. }
  562. update_overlays();
  563. //collision_polygon_editor->edit(Object::cast_to<Node>(p_object));
  564. }
  565. bool Path3DEditorPlugin::handles(Object *p_object) const {
  566. return p_object->is_class("Path3D");
  567. }
  568. void Path3DEditorPlugin::make_visible(bool p_visible) {
  569. if (p_visible) {
  570. topmenu_bar->show();
  571. } else {
  572. topmenu_bar->hide();
  573. {
  574. Path3D *pre = path;
  575. path = nullptr;
  576. if (pre && pre->get_curve().is_valid()) {
  577. pre->get_curve()->emit_signal(CoreStringName(changed));
  578. }
  579. }
  580. }
  581. }
  582. void Path3DEditorPlugin::_mode_changed(int p_mode) {
  583. curve_create->set_pressed_no_signal(p_mode == MODE_CREATE);
  584. curve_edit_curve->set_pressed_no_signal(p_mode == MODE_EDIT_CURVE);
  585. curve_edit_tilt->set_pressed_no_signal(p_mode == MODE_EDIT_TILT);
  586. curve_edit->set_pressed_no_signal(p_mode == MODE_EDIT);
  587. curve_del->set_pressed_no_signal(p_mode == MODE_DELETE);
  588. Node3DEditor::get_singleton()->clear_subgizmo_selection();
  589. }
  590. void Path3DEditorPlugin::_close_curve() {
  591. Ref<Curve3D> c = path->get_curve();
  592. if (c.is_null()) {
  593. return;
  594. }
  595. if (c->get_point_count() < 2) {
  596. return;
  597. }
  598. if (c->get_point_position(0) == c->get_point_position(c->get_point_count() - 1)) {
  599. return;
  600. }
  601. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  602. ur->create_action(TTR("Close Curve"));
  603. ur->add_do_method(c.ptr(), "add_point", c->get_point_position(0), c->get_point_in(0), c->get_point_out(0), -1);
  604. ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
  605. ur->commit_action();
  606. }
  607. void Path3DEditorPlugin::_handle_option_pressed(int p_option) {
  608. PopupMenu *pm;
  609. pm = handle_menu->get_popup();
  610. switch (p_option) {
  611. case HANDLE_OPTION_ANGLE: {
  612. bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
  613. mirror_handle_angle = !is_checked;
  614. pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  615. pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
  616. } break;
  617. case HANDLE_OPTION_LENGTH: {
  618. bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
  619. mirror_handle_length = !is_checked;
  620. pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  621. } break;
  622. }
  623. }
  624. void Path3DEditorPlugin::_create_curve() {
  625. ERR_FAIL_NULL(path);
  626. Ref<Curve3D> new_curve;
  627. new_curve.instantiate();
  628. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  629. undo_redo->create_action(TTR("Create Curve in Path3D"));
  630. undo_redo->add_do_property(path, "curve", new_curve);
  631. undo_redo->add_undo_property(path, "curve", Ref<Curve3D>());
  632. undo_redo->add_do_method(this, "_update_toolbar");
  633. undo_redo->add_undo_method(this, "_update_toolbar");
  634. undo_redo->commit_action();
  635. }
  636. void Path3DEditorPlugin::_confirm_clear_points() {
  637. if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
  638. return;
  639. }
  640. clear_points_dialog->reset_size();
  641. clear_points_dialog->popup_centered();
  642. }
  643. void Path3DEditorPlugin::_clear_points() {
  644. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  645. PackedVector3Array points = path->get_curve()->get_points().duplicate();
  646. undo_redo->create_action(TTR("Clear Curve Points"));
  647. undo_redo->add_do_method(this, "_clear_curve_points");
  648. undo_redo->add_undo_method(this, "_restore_curve_points", points);
  649. undo_redo->commit_action();
  650. }
  651. void Path3DEditorPlugin::_clear_curve_points() {
  652. if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
  653. return;
  654. }
  655. Ref<Curve3D> curve = path->get_curve();
  656. curve->clear_points();
  657. }
  658. void Path3DEditorPlugin::_restore_curve_points(const PackedVector3Array &p_points) {
  659. if (!path || path->get_curve().is_null()) {
  660. return;
  661. }
  662. Ref<Curve3D> curve = path->get_curve();
  663. if (curve->get_point_count() > 0) {
  664. curve->clear_points();
  665. }
  666. for (int i = 0; i < p_points.size(); i += 3) {
  667. curve->add_point(p_points[i + 2], p_points[i], p_points[i + 1]);
  668. }
  669. }
  670. void Path3DEditorPlugin::_update_theme() {
  671. curve_edit->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveEdit")));
  672. curve_edit_curve->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCurve")));
  673. curve_edit_tilt->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveTilt")));
  674. curve_create->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCreate")));
  675. curve_del->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveDelete")));
  676. curve_close->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveClose")));
  677. curve_clear_points->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Clear")));
  678. create_curve_button->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Curve3D")));
  679. }
  680. void Path3DEditorPlugin::_update_toolbar() {
  681. if (!path) {
  682. return;
  683. }
  684. bool has_curve = path->get_curve().is_valid();
  685. toolbar->set_visible(has_curve);
  686. create_curve_button->set_visible(!has_curve);
  687. }
  688. void Path3DEditorPlugin::_bind_methods() {
  689. ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path3DEditorPlugin::_update_toolbar);
  690. ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path3DEditorPlugin::_clear_curve_points);
  691. ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path3DEditorPlugin::_restore_curve_points);
  692. }
  693. Path3DEditorPlugin::Path3DEditorPlugin() {
  694. singleton = this;
  695. mirror_handle_angle = true;
  696. mirror_handle_length = true;
  697. disk_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/path3d_tilt_disk_size");
  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. toolbar = memnew(HBoxContainer);
  704. topmenu_bar->add_child(toolbar);
  705. curve_edit = memnew(Button);
  706. curve_edit->set_theme_type_variation("FlatButton");
  707. curve_edit->set_toggle_mode(true);
  708. curve_edit->set_focus_mode(Control::FOCUS_NONE);
  709. 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"));
  710. toolbar->add_child(curve_edit);
  711. curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT));
  712. curve_edit_curve = memnew(Button);
  713. curve_edit_curve->set_theme_type_variation("FlatButton");
  714. curve_edit_curve->set_toggle_mode(true);
  715. curve_edit_curve->set_focus_mode(Control::FOCUS_NONE);
  716. curve_edit_curve->set_tooltip_text(TTR("Select Control Points") + "\n" + TTR("Shift+Click: Drag out Control Points"));
  717. toolbar->add_child(curve_edit_curve);
  718. curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_CURVE));
  719. curve_edit_tilt = memnew(Button);
  720. curve_edit_tilt->set_theme_type_variation("FlatButton");
  721. curve_edit_tilt->set_toggle_mode(true);
  722. curve_edit_tilt->set_focus_mode(Control::FOCUS_NONE);
  723. curve_edit_tilt->set_tooltip_text(TTR("Select Tilt Handles"));
  724. toolbar->add_child(curve_edit_tilt);
  725. curve_edit_tilt->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_TILT));
  726. curve_create = memnew(Button);
  727. curve_create->set_theme_type_variation("FlatButton");
  728. curve_create->set_toggle_mode(true);
  729. curve_create->set_focus_mode(Control::FOCUS_NONE);
  730. curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
  731. toolbar->add_child(curve_create);
  732. curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_CREATE));
  733. curve_del = memnew(Button);
  734. curve_del->set_theme_type_variation("FlatButton");
  735. curve_del->set_toggle_mode(true);
  736. curve_del->set_focus_mode(Control::FOCUS_NONE);
  737. curve_del->set_tooltip_text(TTR("Delete Point"));
  738. toolbar->add_child(curve_del);
  739. curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_DELETE));
  740. curve_close = memnew(Button);
  741. curve_close->set_theme_type_variation("FlatButton");
  742. curve_close->set_focus_mode(Control::FOCUS_NONE);
  743. curve_close->set_tooltip_text(TTR("Close Curve"));
  744. toolbar->add_child(curve_close);
  745. curve_close->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_close_curve));
  746. curve_clear_points = memnew(Button);
  747. curve_clear_points->set_theme_type_variation("FlatButton");
  748. curve_clear_points->set_focus_mode(Control::FOCUS_NONE);
  749. curve_clear_points->set_tooltip_text(TTR("Clear Points"));
  750. curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_confirm_clear_points));
  751. toolbar->add_child(curve_clear_points);
  752. clear_points_dialog = memnew(ConfirmationDialog);
  753. clear_points_dialog->set_title(TTR("Please Confirm..."));
  754. clear_points_dialog->set_text(TTR("Remove all curve points?"));
  755. clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path3DEditorPlugin::_clear_points));
  756. toolbar->add_child(clear_points_dialog);
  757. handle_menu = memnew(MenuButton);
  758. handle_menu->set_flat(false);
  759. handle_menu->set_theme_type_variation("FlatMenuButton");
  760. handle_menu->set_text(TTR("Options"));
  761. toolbar->add_child(handle_menu);
  762. create_curve_button = memnew(Button);
  763. create_curve_button->set_text(TTR("Create Curve"));
  764. create_curve_button->hide();
  765. topmenu_bar->add_child(create_curve_button);
  766. create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_create_curve));
  767. PopupMenu *menu = handle_menu->get_popup();
  768. menu->add_check_item(TTR("Mirror Handle Angles"));
  769. menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
  770. menu->add_check_item(TTR("Mirror Handle Lengths"));
  771. menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
  772. menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path3DEditorPlugin::_handle_option_pressed));
  773. curve_edit->set_pressed_no_signal(true);
  774. topmenu_bar->connect(SceneStringName(theme_changed), callable_mp(this, &Path3DEditorPlugin::_update_theme));
  775. Node3DEditor::get_singleton()->add_control_to_menu_panel(topmenu_bar);
  776. }
  777. Ref<EditorNode3DGizmo> Path3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {
  778. Ref<Path3DGizmo> ref;
  779. Path3D *path = Object::cast_to<Path3D>(p_spatial);
  780. if (path) {
  781. ref = Ref<Path3DGizmo>(memnew(Path3DGizmo(path, disk_size)));
  782. }
  783. return ref;
  784. }
  785. bool Path3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  786. return Object::cast_to<Path3D>(p_spatial) != nullptr;
  787. }
  788. String Path3DGizmoPlugin::get_gizmo_name() const {
  789. return "Path3D";
  790. }
  791. void Path3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  792. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  793. ERR_FAIL_NULL(path);
  794. Ref<Curve3D> curve = path->get_curve();
  795. Ref<StandardMaterial3D> handle_material = get_material("handles", p_gizmo);
  796. PackedVector3Array handles;
  797. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  798. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  799. // Collect handles.
  800. const Vector3 pos = curve->get_point_position(idx);
  801. handles.append(pos);
  802. }
  803. }
  804. if (handles.size()) {
  805. p_gizmo->add_vertices(handles, handle_material, Mesh::PRIMITIVE_POINTS);
  806. }
  807. }
  808. int Path3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
  809. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  810. ERR_FAIL_NULL_V(path, -1);
  811. Ref<Curve3D> curve = path->get_curve();
  812. ERR_FAIL_COND_V(curve.is_null(), -1);
  813. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  814. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  815. Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
  816. if (p_camera->unproject_position(pos).distance_to(p_point) < 20) {
  817. return idx;
  818. }
  819. }
  820. }
  821. return -1;
  822. }
  823. Vector<int> Path3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
  824. Vector<int> contained_points;
  825. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  826. ERR_FAIL_NULL_V(path, contained_points);
  827. Ref<Curve3D> curve = path->get_curve();
  828. ERR_FAIL_COND_V(curve.is_null(), contained_points);
  829. if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
  830. for (int idx = 0; idx < curve->get_point_count(); ++idx) {
  831. Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
  832. bool is_contained_in_frustum = true;
  833. for (int i = 0; i < p_frustum.size(); ++i) {
  834. if (p_frustum[i].distance_to(pos) > 0) {
  835. is_contained_in_frustum = false;
  836. break;
  837. }
  838. }
  839. if (is_contained_in_frustum) {
  840. contained_points.push_back(idx);
  841. }
  842. }
  843. }
  844. return contained_points;
  845. }
  846. Transform3D Path3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
  847. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  848. ERR_FAIL_NULL_V(path, Transform3D());
  849. Ref<Curve3D> curve = path->get_curve();
  850. ERR_FAIL_COND_V(curve.is_null(), Transform3D());
  851. ERR_FAIL_INDEX_V(p_id, curve->get_point_count(), Transform3D());
  852. Basis basis = transformation_locked_basis.has(p_id) ? transformation_locked_basis[p_id] : curve->get_point_baked_posture(p_id, true);
  853. Vector3 pos = curve->get_point_position(p_id);
  854. Transform3D t = Transform3D(basis, pos);
  855. return t;
  856. }
  857. void Path3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
  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. ERR_FAIL_INDEX(p_id, curve->get_point_count());
  863. if (!transformation_locked_basis.has(p_id)) {
  864. transformation_locked_basis[p_id] = Basis(curve->get_point_baked_posture(p_id, true));
  865. }
  866. curve->set_point_position(p_id, p_transform.origin);
  867. }
  868. void Path3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
  869. Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
  870. ERR_FAIL_NULL(path);
  871. Ref<Curve3D> curve = path->get_curve();
  872. ERR_FAIL_COND(curve.is_null());
  873. transformation_locked_basis.clear();
  874. if (p_cancel) {
  875. for (int i = 0; i < p_ids.size(); ++i) {
  876. curve->set_point_position(p_ids[i], p_restore[i].origin);
  877. }
  878. return;
  879. }
  880. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  881. undo_redo->create_action(TTR("Set Curve Point Position"));
  882. for (int i = 0; i < p_ids.size(); ++i) {
  883. const int idx = p_ids[i];
  884. undo_redo->add_do_method(curve.ptr(), "set_point_position", idx, curve->get_point_position(idx));
  885. undo_redo->add_undo_method(curve.ptr(), "set_point_position", idx, p_restore[i].origin);
  886. }
  887. undo_redo->commit_action();
  888. }
  889. int Path3DGizmoPlugin::get_priority() const {
  890. return -1;
  891. }
  892. Path3DGizmoPlugin::Path3DGizmoPlugin(float p_disk_size) {
  893. Color path_color = SceneTree::get_singleton()->get_debug_paths_color();
  894. Color path_tilt_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/path_tilt");
  895. disk_size = p_disk_size;
  896. create_material("path_material", path_color);
  897. create_material("path_thin_material", Color(0.6, 0.6, 0.6));
  898. create_material("path_tilt_material", path_tilt_color);
  899. create_material("path_tilt_muted_material", path_tilt_color * 0.7);
  900. create_handle_material("handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
  901. create_handle_material("sec_handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorCurveHandle"), EditorStringName(EditorIcons)));
  902. }