Debian Codegraph @

editor/scene/3d/path_3d_editor_plugin.cpp

raw symbols SCIP
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
31 #include "path_3d_editor_plugin.h"
32
33 #include "core/math/geometry_2d.h"
34 #include "core/math/geometry_3d.h"
35 #include "core/os/keyboard.h"
36 #include "editor/editor_node.h"
37 #include "editor/editor_string_names.h"
38 #include "editor/editor_undo_redo_manager.h"
39 #include "editor/scene/3d/node_3d_editor_plugin.h"
40 #include "editor/settings/editor_settings.h"
41 #include "scene/gui/dialogs.h"
42 #include "scene/gui/menu_button.h"
43 #include "scene/resources/curve.h"
44
45 String Path3DGizmo::get_handle_name(int p_id, bool p_secondary) const {
46 Ref<Curve3D> c = path->get_curve();
47 if (c.is_null()) {
48 return "";
49 }
50
51 // Primary handles: position.
52 if (!p_secondary) {
53 return TTR("Curve Point #") + itos(p_id);
54 }
55
56 // Secondary handles: in, out, tilt.
57 const HandleInfo info = _secondary_handles_info[p_id];
58 switch (info.type) {
59 case HandleType::HANDLE_TYPE_IN:
60 return TTR("Handle In #") + itos(info.point_idx);
61 case HandleType::HANDLE_TYPE_OUT:
62 return TTR("Handle Out #") + itos(info.point_idx);
63 case HandleType::HANDLE_TYPE_TILT:
64 return TTR("Handle Tilt #") + itos(info.point_idx);
65 }
66
67 return "";
68 }
69
70 Variant Path3DGizmo::get_handle_value(int p_id, bool p_secondary) const {
71 Ref<Curve3D> c = path->get_curve();
72 if (c.is_null()) {
73 return Variant();
74 }
75
76 // Primary handles: position.
77 if (!p_secondary) {
78 original = c->get_point_position(p_id);
79 return original;
80 }
81
82 // Secondary handles: in, out, tilt.
83 const HandleInfo info = _secondary_handles_info[p_id];
84 Vector3 ofs;
85 switch (info.type) {
86 case HandleType::HANDLE_TYPE_TILT:
87 return c->get_point_tilt(info.point_idx);
88 case HandleType::HANDLE_TYPE_IN:
89 ofs = c->get_point_in(info.point_idx);
90 break;
91 case HandleType::HANDLE_TYPE_OUT:
92 ofs = c->get_point_out(info.point_idx);
93 break;
94 }
95
96 original = ofs + c->get_point_position(info.point_idx);
97 return ofs;
98 }
99
100 void Path3DGizmo::set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
101 Ref<Curve3D> c = path->get_curve();
102 if (c.is_null()) {
103 return;
104 }
105
106 const Transform3D gt = path->get_global_transform();
107 const Transform3D gi = gt.affine_inverse();
108 const Vector3 ray_from = p_camera->project_ray_origin(p_point);
109 const Vector3 ray_dir = p_camera->project_ray_normal(p_point);
110 const Plane p = Plane(p_camera->get_transform().basis.get_column(2), gt.xform(original));
111
112 // Primary handles: position.
113 if (!p_secondary) {
114 Vector3 inters;
115 // Special case for primary handle, the handle id equals control point id.
116 const int idx = p_id;
117 if (p.intersects_ray(ray_from, ray_dir, &inters)) {
118 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
119 float snap = Node3DEditor::get_singleton()->get_translate_snap();
120 inters.snapf(snap);
121 }
122
123 Vector3 local = gi.xform(inters);
124 c->set_point_position(idx, local);
125 }
126
127 return;
128 }
129
130 // Secondary handles: in, out, tilt.
131 const HandleInfo info = _secondary_handles_info[p_id];
132 switch (info.type) {
133 case HandleType::HANDLE_TYPE_OUT:
134 case HandleType::HANDLE_TYPE_IN: {
135 const int idx = info.point_idx;
136 const Vector3 base = c->get_point_position(idx);
137
138 Vector3 inters;
139 if (p.intersects_ray(ray_from, ray_dir, &inters)) {
140 if (!Path3DEditorPlugin::singleton->is_handle_clicked()) {
141 orig_in_length = c->get_point_in(idx).length();
142 orig_out_length = c->get_point_out(idx).length();
143 Path3DEditorPlugin::singleton->set_handle_clicked(true);
144 }
145
146 Vector3 local = gi.xform(inters) - base;
147 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
148 float snap = Node3DEditor::get_singleton()->get_translate_snap();
149 local.snapf(snap);
150 }
151
152 // Determine if control points should be swapped based on delta movement.
153 // Only run on the next update after an overlap is detected, to get proper delta movement.
154 if (control_points_overlapped) {
155 control_points_overlapped = false;
156 Vector3 delta = local - (info.type == HANDLE_TYPE_IN ? c->get_point_in(idx) : c->get_point_out(idx));
157 Vector3 p0 = c->get_point_position(idx - 1) - base;
158 Vector3 p1 = c->get_point_position(idx + 1) - base;
159 HandleType new_type = Math::abs(delta.angle_to(p0)) < Math::abs(delta.angle_to(p1)) ? HANDLE_TYPE_IN : HANDLE_TYPE_OUT;
160 if (info.type != new_type) {
161 swapped_control_points_idx = idx;
162 }
163 }
164
165 // Detect control points overlap.
166 bool control_points_equal = c->get_point_in(idx).is_equal_approx(c->get_point_out(idx));
167 if (idx > 0 && idx < (c->get_point_count() - 1) && control_points_equal) {
168 control_points_overlapped = true;
169 }
170
171 HandleType control_type = info.type;
172 if (swapped_control_points_idx == idx) {
173 control_type = info.type == HANDLE_TYPE_IN ? HANDLE_TYPE_OUT : HANDLE_TYPE_IN;
174 }
175
176 if (control_type == HandleType::HANDLE_TYPE_IN) {
177 c->set_point_in(idx, local);
178 if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
179 c->set_point_out(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_out_length));
180 }
181 } else {
182 c->set_point_out(idx, local);
183 if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
184 c->set_point_in(idx, Path3DEditorPlugin::singleton->mirror_length_enabled() ? -local : (-local.normalized() * orig_in_length));
185 }
186 }
187 }
188 break;
189 }
190 case HandleType::HANDLE_TYPE_TILT: {
191 const int idx = info.point_idx;
192 const Vector3 position = c->get_point_position(idx);
193 const Basis posture = c->get_point_baked_posture(idx);
194 const Vector3 tangent = -posture.get_column(2);
195 const Vector3 up = posture.get_column(1);
196 const Plane tilt_plane_global = gt.xform(Plane(tangent, position));
197
198 Vector3 intersection;
199
200 if (tilt_plane_global.intersects_ray(ray_from, ray_dir, &intersection)) {
201 Vector3 direction = gi.xform(intersection) - position;
202 real_t tilt_angle = up.signed_angle_to(direction, tangent);
203
204 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
205 real_t snap_degrees = Node3DEditor::get_singleton()->get_rotate_snap();
206 tilt_angle = Math::deg_to_rad(Math::snapped(Math::rad_to_deg(tilt_angle), snap_degrees));
207 }
208
209 c->set_point_tilt(idx, tilt_angle);
210 }
211 break;
212 }
213 }
214 }
215
216 void Path3DGizmo::commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
217 swapped_control_points_idx = -1;
218 control_points_overlapped = false;
219
220 Ref<Curve3D> c = path->get_curve();
221 if (c.is_null()) {
222 return;
223 }
224
225 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
226
227 // Primary handles: position.
228 if (!p_secondary && !Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
229 // Special case for primary handle, the handle id equals control point id.
230 const int idx = p_id;
231 if (p_cancel) {
232 c->set_point_position(idx, p_restore);
233 return;
234 }
235 ur->create_action(TTR("Set Curve Point Position"));
236 ur->add_do_method(c.ptr(), "set_point_position", idx, c->get_point_position(idx));
237 ur->add_undo_method(c.ptr(), "set_point_position", idx, p_restore);
238 ur->commit_action();
239
240 return;
241 }
242
243 // Secondary handles: in, out, tilt.
244 const HandleInfo info = _secondary_handles_info[p_id];
245 const int idx = info.point_idx;
246 switch (info.type) {
247 case HandleType::HANDLE_TYPE_OUT: {
248 if (p_cancel) {
249 c->set_point_out(idx, p_restore);
250
251 return;
252 }
253
254 ur->create_action(TTR("Set Curve Out Position"));
255 ur->add_do_method(c.ptr(), "set_point_out", idx, c->get_point_out(idx));
256 ur->add_undo_method(c.ptr(), "set_point_out", idx, p_restore);
257
258 if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
259 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));
260 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));
261 }
262
263 ur->commit_action();
264 break;
265 }
266 case HandleType::HANDLE_TYPE_IN: {
267 if (p_cancel) {
268 c->set_point_in(idx, p_restore);
269 return;
270 }
271
272 ur->create_action(TTR("Set Curve In Position"));
273 ur->add_do_method(c.ptr(), "set_point_in", idx, c->get_point_in(idx));
274 ur->add_undo_method(c.ptr(), "set_point_in", idx, p_restore);
275
276 if (Path3DEditorPlugin::singleton->mirror_angle_enabled()) {
277 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));
278 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));
279 }
280
281 ur->commit_action();
282 break;
283 }
284 case HandleType::HANDLE_TYPE_TILT: {
285 if (p_cancel) {
286 c->set_point_tilt(idx, p_restore);
287 return;
288 }
289 ur->create_action(TTR("Set Curve Point Tilt"));
290 ur->add_do_method(c.ptr(), "set_point_tilt", idx, c->get_point_tilt(idx));
291 ur->add_undo_method(c.ptr(), "set_point_tilt", idx, p_restore);
292
293 ur->commit_action();
294 break;
295 }
296 }
297 }
298
299 void Path3DGizmo::redraw() {
300 clear();
301
302 Ref<StandardMaterial3D> path_thin_material = gizmo_plugin->get_material("path_thin_material", this);
303 Ref<StandardMaterial3D> path_tilt_material = gizmo_plugin->get_material("path_tilt_material", this);
304 Ref<StandardMaterial3D> path_tilt_muted_material = gizmo_plugin->get_material("path_tilt_muted_material", this);
305 Ref<StandardMaterial3D> handles_material = gizmo_plugin->get_material("handles");
306 Ref<StandardMaterial3D> first_pt_handle_material = gizmo_plugin->get_material("first_pt_handle");
307 Ref<StandardMaterial3D> last_pt_handle_material = gizmo_plugin->get_material("last_pt_handle");
308 Ref<StandardMaterial3D> closed_pt_handle_material = gizmo_plugin->get_material("closed_pt_handle");
309 Ref<StandardMaterial3D> sec_handles_material = gizmo_plugin->get_material("sec_handles");
310
311 first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));
312 last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));
313 closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));
314
315 Ref<Curve3D> c = path->get_curve();
316 if (c.is_null()) {
317 return;
318 }
319
320 debug_material = gizmo_plugin->get_material("path_material", this);
321
322 Color path_color = path->get_debug_custom_color();
323 if (path_color != Color(0.0, 0.0, 0.0)) {
324 debug_material.instantiate();
325 debug_material->set_albedo(path_color);
326 debug_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
327 debug_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
328 debug_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
329 debug_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
330 debug_material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
331 }
332
333 real_t interval = 0.1;
334 const real_t length = c->get_baked_length();
335
336 // 1. Draw curve and bones if it is visible (alpha > 0.0).
337 if (length > CMP_EPSILON && path_color.a > 0.0) {
338 const int sample_count = int(length / interval) + 2;
339 interval = length / (sample_count - 1); // Recalculate real interval length.
340
341 Vector<Transform3D> frames;
342 frames.resize(sample_count);
343
344 {
345 Transform3D *w = frames.ptrw();
346
347 for (int i = 0; i < sample_count; i++) {
348 w[i] = c->sample_baked_with_rotation(i * interval, true, true);
349 }
350 }
351
352 const Transform3D *r = frames.ptr();
353
354 Vector<Vector3> _collision_segments;
355 _collision_segments.resize((sample_count - 1) * 2);
356 Vector3 *_collisions_ptr = _collision_segments.ptrw();
357
358 Vector<Vector3> bones;
359 bones.resize(sample_count * 4);
360 Vector3 *bones_ptr = bones.ptrw();
361
362 Vector<Vector3> ribbon;
363 ribbon.resize(sample_count);
364 Vector3 *ribbon_ptr = ribbon.ptrw();
365
366 for (int i = 0; i < sample_count; i++) {
367 const Vector3 p1 = r[i].origin;
368 const Vector3 side = r[i].basis.get_column(0);
369 const Vector3 up = r[i].basis.get_column(1);
370 const Vector3 forward = r[i].basis.get_column(2);
371
372 // Collision segments.
373 if (i != sample_count - 1) {
374 const Vector3 p2 = r[i + 1].origin;
375 _collisions_ptr[(i * 2)] = p1;
376 _collisions_ptr[(i * 2) + 1] = p2;
377 }
378
379 // Path3D as a ribbon.
380 ribbon_ptr[i] = p1;
381
382 if (i % 4 == 0) {
383 // Draw fish bone every 4 points to reduce visual noise and performance impact
384 // (compared to drawing it for every point).
385 const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
386 const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
387
388 const int bone_idx = i * 4;
389
390 bones_ptr[bone_idx] = p1;
391 bones_ptr[bone_idx + 1] = p_left;
392 bones_ptr[bone_idx + 2] = p1;
393 bones_ptr[bone_idx + 3] = p_right;
394 }
395 }
396
397 add_collision_segments(_collision_segments);
398 add_lines(bones, debug_material);
399 add_vertices(ribbon, debug_material, Mesh::PRIMITIVE_LINE_STRIP);
400 }
401
402 // 2. Draw handles when selected.
403 if (Path3DEditorPlugin::singleton->get_edited_path() == path) {
404 PackedVector3Array handle_lines;
405 PackedVector3Array tilt_handle_lines;
406 PackedVector3Array primary_handle_points;
407 PackedVector3Array secondary_handle_points;
408 PackedInt32Array collected_secondary_handle_ids; // Avoid shadowing member on Node3DEditorGizmo.
409
410 _secondary_handles_info.resize(c->get_point_count() * 3);
411
412 const float disk_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/path3d_tilt_disk_size");
413
414 for (int idx = 0; idx < c->get_point_count(); idx++) {
415 // Collect primary-handles.
416 const Vector3 pos = c->get_point_position(idx);
417 primary_handle_points.append(pos);
418
419 HandleInfo info;
420 info.point_idx = idx;
421
422 // Collect in-handles except for the first point.
423 if (idx > (c->is_closed() ? -1 : 0) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
424 const Vector3 in = c->get_point_in(idx);
425
426 info.type = HandleType::HANDLE_TYPE_IN;
427 const int handle_idx = idx * 3 + 0;
428 collected_secondary_handle_ids.append(handle_idx);
429 _secondary_handles_info.write[handle_idx] = info;
430
431 secondary_handle_points.append(pos + in);
432 handle_lines.append(pos);
433 handle_lines.append(pos + in);
434 }
435
436 // Collect out-handles except for the last point.
437 if (idx < (c->is_closed() ? c->get_point_count() : c->get_point_count() - 1) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
438 const Vector3 out = c->get_point_out(idx);
439
440 info.type = HandleType::HANDLE_TYPE_OUT;
441 const int handle_idx = idx * 3 + 1;
442 collected_secondary_handle_ids.append(handle_idx);
443 _secondary_handles_info.write[handle_idx] = info;
444
445 secondary_handle_points.append(pos + out);
446 handle_lines.append(pos);
447 handle_lines.append(pos + out);
448 }
449
450 // Collect tilt-handles.
451 if (Path3DEditorPlugin::singleton->curve_edit_tilt->is_pressed()) {
452 // Tilt handle.
453 {
454 info.type = HandleType::HANDLE_TYPE_TILT;
455 const int handle_idx = idx * 3 + 2;
456 collected_secondary_handle_ids.append(handle_idx);
457 _secondary_handles_info.write[handle_idx] = info;
458
459 const Basis posture = c->get_point_baked_posture(idx, true);
460 const Vector3 up = posture.get_column(1);
461 secondary_handle_points.append(pos + up * disk_size);
462 tilt_handle_lines.append(pos);
463 tilt_handle_lines.append(pos + up * disk_size);
464 }
465
466 // Tilt disk.
467 {
468 const Basis posture = c->get_point_baked_posture(idx, false);
469 const Vector3 up = posture.get_column(1);
470 const Vector3 side = posture.get_column(0);
471
472 PackedVector3Array disk;
473 disk.append(pos);
474
475 const int n = 36;
476 for (int i = 0; i <= n; i++) {
477 const float a = Math::TAU * i / n;
478 const Vector3 edge = std::sin(a) * side + std::cos(a) * up;
479 disk.append(pos + edge * disk_size);
480 }
481 add_vertices(disk, debug_material, Mesh::PRIMITIVE_LINE_STRIP);
482 }
483 }
484 }
485
486 if (handle_lines.size() > 1) {
487 add_lines(handle_lines, path_thin_material);
488 }
489
490 if (tilt_handle_lines.size() > 1) {
491 add_lines(tilt_handle_lines, path_tilt_material);
492 }
493
494 if (!Path3DEditorPlugin::singleton->curve_edit->is_pressed() && primary_handle_points.size()) {
495 // Need to define indices separately.
496 // Point count.
497 const int pc = primary_handle_points.size();
498 Vector<int> idx;
499 idx.resize(pc);
500 int *idx_ptr = idx.ptrw();
501 for (int j = 0; j < pc; j++) {
502 idx_ptr[j] = j;
503 }
504
505 // Initialize arrays for first point.
506 PackedVector3Array first_pt_handle_point;
507 Vector<int> first_pt_id;
508 first_pt_handle_point.append(primary_handle_points[0]);
509 first_pt_id.append(idx[0]);
510
511 // Initialize arrays and add handle for last point if needed.
512 if (pc > 1) {
513 PackedVector3Array last_pt_handle_point;
514 Vector<int> last_pt_id;
515 last_pt_handle_point.append(primary_handle_points[pc - 1]);
516 last_pt_id.append(idx[pc - 1]);
517 primary_handle_points.remove_at(pc - 1);
518 idx.remove_at(pc - 1);
519 add_handles(last_pt_handle_point, c->is_closed() ? handles_material : last_pt_handle_material, last_pt_id);
520 }
521
522 // Add handle for first point.
523 primary_handle_points.remove_at(0);
524 idx.remove_at(0);
525 add_handles(first_pt_handle_point, c->is_closed() ? closed_pt_handle_material : first_pt_handle_material, first_pt_id);
526
527 // Add handles for remaining intermediate points.
528 if (!primary_handle_points.is_empty()) {
529 add_handles(primary_handle_points, handles_material, idx);
530 }
531 }
532 if (secondary_handle_points.size()) {
533 add_handles(secondary_handle_points, sec_handles_material, collected_secondary_handle_ids, false, true);
534 }
535 // Draw the gizmo plugin manually, because handles are registered. In which case, the caller code skips drawing the gizmo plugin.
536 gizmo_plugin->redraw(this);
537 }
538 }
539
540 void Path3DGizmo::_update_transform_gizmo() {
541 Node3DEditor::get_singleton()->update_transform_gizmo();
542 }
543
544 Path3DGizmo::Path3DGizmo(Path3D *p_path) {
545 path = p_path;
546 set_node_3d(p_path);
547 orig_in_length = 0;
548 orig_out_length = 0;
549
550 // Connecting to a signal once, rather than plaguing the implementation with calls to `Node3DEditor::update_transform_gizmo`.
551 path->connect("curve_changed", callable_mp(this, &Path3DGizmo::_update_transform_gizmo));
552 path->connect("debug_color_changed", callable_mp(this, &Path3DGizmo::redraw));
553
554 Path3DEditorPlugin::singleton->curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
555 Path3DEditorPlugin::singleton->curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
556 Path3DEditorPlugin::singleton->curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
557 Path3DEditorPlugin::singleton->curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
558 Path3DEditorPlugin::singleton->curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
559 }
560
561 EditorPlugin::AfterGUIInput Path3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
562 if (!path) {
563 return EditorPlugin::AFTER_GUI_INPUT_PASS;
564 }
565 Ref<Curve3D> c = path->get_curve();
566 if (c.is_null()) {
567 return EditorPlugin::AFTER_GUI_INPUT_PASS;
568 }
569 Transform3D gt = path->get_global_transform();
570 Transform3D it = gt.affine_inverse();
571
572 static const int click_dist = 10; //should make global
573
574 Ref<InputEventMouseButton> mb = p_event;
575
576 if (mb.is_valid()) {
577 Point2 mbpos(mb->get_position().x, mb->get_position().y);
578
579 Node3DEditorViewport *viewport = nullptr;
580 for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
581 Node3DEditorViewport *vp = Node3DEditor::get_singleton()->get_editor_viewport(i);
582 if (vp->get_camera_3d() == p_camera) {
583 viewport = vp;
584 break;
585 }
586 }
587
588 ERR_FAIL_NULL_V(viewport, EditorPlugin::AFTER_GUI_INPUT_PASS);
589
590 if (!mb->is_pressed()) {
591 set_handle_clicked(false);
592 }
593
594 if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->is_command_or_control_pressed()))) {
595 //click into curve, break it down
596 Vector<Vector3> v3a = c->tessellate();
597 int rc = v3a.size();
598 int closest_seg = -1;
599 Vector3 closest_seg_point;
600
601 if (rc >= 2) {
602 int idx = 0;
603 const Vector3 *r = v3a.ptr();
604 float closest_d = 1e20;
605
606 if (viewport->point_to_screen(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist) {
607 return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
608 }
609
610 for (int i = 0; i < c->get_point_count() - 1; i++) {
611 //find the offset and point index of the place to break up
612 int j = idx;
613 if (viewport->point_to_screen(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist) {
614 return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
615 }
616
617 while (j < rc && c->get_point_position(i + 1) != r[j]) {
618 Vector3 from = r[j];
619 Vector3 to = r[j + 1];
620 real_t cdist = from.distance_to(to);
621 from = gt.xform(from);
622 to = gt.xform(to);
623 if (cdist > 0) {
624 const Vector2 segment_a = viewport->point_to_screen(from);
625 const Vector2 segment_b = viewport->point_to_screen(to);
626 Vector2 inters = Geometry2D::get_closest_point_to_segment(mbpos, segment_a, segment_b);
627 float d = inters.distance_to(mbpos);
628
629 if (d < 10 && d < closest_d) {
630 closest_d = d;
631 closest_seg = i;
632 Vector3 ray_from = viewport->get_ray_pos(mbpos);
633 Vector3 ray_dir = viewport->get_ray(mbpos);
634
635 Vector3 ra, rb;
636 Geometry3D::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
637
638 closest_seg_point = it.xform(rb);
639 }
640 }
641 j++;
642 }
643 if (idx == j) {
644 idx++; //force next
645 } else {
646 idx = j; //swap
647 }
648
649 if (j == rc) {
650 break;
651 }
652 }
653 }
654
655 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
656 if (closest_seg != -1) {
657 //subdivide
658
659 ur->create_action(TTR("Split Path"));
660 ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
661 ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
662 ur->commit_action();
663 return EditorPlugin::AFTER_GUI_INPUT_STOP;
664
665 } else {
666 Vector3 origin;
667 if (c->get_point_count() == 0) {
668 origin = path->get_transform().get_origin();
669 } else {
670 origin = gt.xform(c->get_point_position(c->get_point_count() - 1));
671 }
672 Plane p(p_camera->get_transform().basis.get_column(2), origin);
673 Vector3 ray_from = viewport->get_ray_pos(mbpos);
674 Vector3 ray_dir = viewport->get_ray(mbpos);
675
676 Vector3 inters;
677 if (p.intersects_ray(ray_from, ray_dir, &inters)) {
678 ur->create_action(TTR("Add Point to Curve"));
679 ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
680 ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
681 ur->commit_action();
682 return EditorPlugin::AFTER_GUI_INPUT_STOP;
683 }
684
685 //add new at pos
686 }
687
688 } 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()))) {
689 const float disk_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/path3d_tilt_disk_size");
690 for (int i = 0; i < c->get_point_count(); i++) {
691 real_t dist_to_p = viewport->point_to_screen(gt.xform(c->get_point_position(i))).distance_to(mbpos);
692 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);
693 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);
694 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);
695
696 // Find the offset and point index of the place to break up.
697 // Also check for the control points.
698 if (dist_to_p < click_dist) {
699 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
700 ur->create_action(TTR("Remove Path Point"));
701 ur->add_do_method(c.ptr(), "remove_point", i);
702 ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i);
703 ur->commit_action();
704 return EditorPlugin::AFTER_GUI_INPUT_STOP;
705 } else if (dist_to_p_out < click_dist) {
706 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
707 ur->create_action(TTR("Reset Out-Control Point"));
708 ur->add_do_method(c.ptr(), "set_point_out", i, Vector3());
709 ur->add_undo_method(c.ptr(), "set_point_out", i, c->get_point_out(i));
710 ur->commit_action();
711 return EditorPlugin::AFTER_GUI_INPUT_STOP;
712 } else if (dist_to_p_in < click_dist) {
713 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
714 ur->create_action(TTR("Reset In-Control Point"));
715 ur->add_do_method(c.ptr(), "set_point_in", i, Vector3());
716 ur->add_undo_method(c.ptr(), "set_point_in", i, c->get_point_in(i));
717 ur->commit_action();
718 return EditorPlugin::AFTER_GUI_INPUT_STOP;
719 } else if (dist_to_p_up < click_dist) {
720 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
721 ur->create_action(TTR("Reset Point Tilt"));
722 ur->add_do_method(c.ptr(), "set_point_tilt", i, 0.0f);
723 ur->add_undo_method(c.ptr(), "set_point_tilt", i, c->get_point_tilt(i));
724 ur->commit_action();
725 return EditorPlugin::AFTER_GUI_INPUT_STOP;
726 }
727 }
728 }
729 }
730
731 return EditorPlugin::AFTER_GUI_INPUT_PASS;
732 }
733
734 void Path3DEditorPlugin::edit(Object *p_object) {
735 path = Object::cast_to<Path3D>(p_object);
736 _update_toolbar();
737 update_overlays();
738 }
739
740 bool Path3DEditorPlugin::handles(Object *p_object) const {
741 return p_object->is_class("Path3D");
742 }
743
744 void Path3DEditorPlugin::make_visible(bool p_visible) {
745 if (p_visible) {
746 topmenu_bar->show();
747 } else {
748 topmenu_bar->hide();
749 path = nullptr;
750 }
751 }
752
753 void Path3DEditorPlugin::_mode_changed(int p_mode) {
754 curve_create->set_pressed_no_signal(p_mode == MODE_CREATE);
755 curve_edit_curve->set_pressed_no_signal(p_mode == MODE_EDIT_CURVE);
756 curve_edit_tilt->set_pressed_no_signal(p_mode == MODE_EDIT_TILT);
757 curve_edit->set_pressed_no_signal(p_mode == MODE_EDIT);
758 curve_del->set_pressed_no_signal(p_mode == MODE_DELETE);
759
760 Node3DEditor::get_singleton()->clear_subgizmo_selection();
761 }
762
763 void Path3DEditorPlugin::_toggle_closed_curve() {
764 Ref<Curve3D> c = path->get_curve();
765 if (c.is_null()) {
766 return;
767 }
768 if (c->get_point_count() < 2) {
769 return;
770 }
771 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
772 ur->create_action(TTR("Toggle Open/Closed Curve"));
773 ur->add_do_method(c.ptr(), "set_closed", !c.ptr()->is_closed());
774 ur->add_undo_method(c.ptr(), "set_closed", c.ptr()->is_closed());
775 ur->commit_action();
776 }
777
778 void Path3DEditorPlugin::_handle_option_pressed(int p_option) {
779 PopupMenu *pm;
780 pm = handle_menu->get_popup();
781
782 switch (p_option) {
783 case HANDLE_OPTION_ANGLE: {
784 bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
785 mirror_handle_angle = !is_checked;
786 pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
787 pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
788 } break;
789 case HANDLE_OPTION_LENGTH: {
790 bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
791 mirror_handle_length = !is_checked;
792 pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
793 } break;
794 }
795 }
796
797 void Path3DEditorPlugin::_create_curve() {
798 ERR_FAIL_NULL(path);
799
800 Ref<Curve3D> new_curve;
801 new_curve.instantiate();
802
803 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
804 undo_redo->create_action(TTR("Create Curve in Path3D"));
805 undo_redo->add_do_property(path, "curve", new_curve);
806 undo_redo->add_undo_property(path, "curve", Ref<Curve3D>());
807 undo_redo->add_do_method(this, "_update_toolbar");
808 undo_redo->add_undo_method(this, "_update_toolbar");
809 undo_redo->commit_action();
810 }
811
812 void Path3DEditorPlugin::_confirm_clear_points() {
813 if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
814 return;
815 }
816 clear_points_dialog->reset_size();
817 clear_points_dialog->popup_centered();
818 }
819
820 void Path3DEditorPlugin::_clear_points() {
821 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
822 PackedVector3Array points = path->get_curve()->get_points().duplicate();
823
824 undo_redo->create_action(TTR("Clear Curve Points"));
825 undo_redo->add_do_method(this, "_clear_curve_points");
826 undo_redo->add_undo_method(this, "_restore_curve_points", points);
827 undo_redo->commit_action();
828 }
829
830 void Path3DEditorPlugin::_clear_curve_points() {
831 if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
832 return;
833 }
834 Ref<Curve3D> curve = path->get_curve();
835 curve->set_closed(false);
836 curve->clear_points();
837 }
838
839 void Path3DEditorPlugin::_restore_curve_points(const PackedVector3Array &p_points) {
840 if (!path || path->get_curve().is_null()) {
841 return;
842 }
843 Ref<Curve3D> curve = path->get_curve();
844
845 if (curve->get_point_count() > 0) {
846 curve->clear_points();
847 }
848
849 for (int i = 0; i < p_points.size(); i += 3) {
850 curve->add_point(p_points[i + 2], p_points[i], p_points[i + 1]);
851 }
852 }
853
854 void Path3DEditorPlugin::_update_theme() {
855 curve_edit->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveEdit")));
856 curve_edit_curve->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCurve")));
857 curve_edit_tilt->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveTilt")));
858 curve_create->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCreate")));
859 curve_del->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveDelete")));
860 curve_closed->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveClose")));
861 curve_clear_points->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Clear")));
862 create_curve_button->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Curve3D")));
863 }
864
865 void Path3DEditorPlugin::_update_toolbar() {
866 if (!path) {
867 return;
868 }
869 bool has_curve = path->get_curve().is_valid();
870 toolbar->set_visible(has_curve);
871 create_curve_button->set_visible(!has_curve);
872 }
873
874 void Path3DEditorPlugin::_notification(int p_what) {
875 switch (p_what) {
876 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
877 if (!path) {
878 return;
879 }
880
881 if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/3d_gizmos/gizmo_settings")) {
882 return;
883 }
884
885 path->update_gizmos();
886 } break;
887 }
888 }
889
890 void Path3DEditorPlugin::_bind_methods() {
891 ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path3DEditorPlugin::_update_toolbar);
892 ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path3DEditorPlugin::_clear_curve_points);
893 ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path3DEditorPlugin::_restore_curve_points);
894 }
895
896 Path3DEditorPlugin::Path3DEditorPlugin() {
897 singleton = this;
898 mirror_handle_angle = true;
899 mirror_handle_length = true;
900
901 Ref<Path3DGizmoPlugin> gizmo_plugin;
902 gizmo_plugin.instantiate();
903 Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
904 path_3d_gizmo_plugin = gizmo_plugin;
905
906 topmenu_bar = memnew(HBoxContainer);
907 topmenu_bar->hide();
908
909 toolbar = memnew(HBoxContainer);
910 topmenu_bar->add_child(toolbar);
911
912 curve_edit = memnew(Button);
913 curve_edit->set_theme_type_variation(SceneStringName(FlatButton));
914 curve_edit->set_toggle_mode(true);
915 curve_edit->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
916 curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Click: Select multiple Points") + "\n" + vformat(TTR("%s+Click: Add Point"), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL)) + "\n" + TTR("Right Click: Delete Point"));
917 curve_edit->set_accessibility_name(TTRC("Select Points"));
918 toolbar->add_child(curve_edit);
919 curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT));
920
921 curve_edit_curve = memnew(Button);
922 curve_edit_curve->set_theme_type_variation(SceneStringName(FlatButton));
923 curve_edit_curve->set_toggle_mode(true);
924 curve_edit_curve->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
925 curve_edit_curve->set_tooltip_text(TTR("Select Control Points") + "\n" + TTR("Shift+Click: Drag out Control Points"));
926 curve_edit_curve->set_accessibility_name(TTRC("Select Control Points"));
927 toolbar->add_child(curve_edit_curve);
928 curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_CURVE));
929
930 curve_edit_tilt = memnew(Button);
931 curve_edit_tilt->set_theme_type_variation(SceneStringName(FlatButton));
932 curve_edit_tilt->set_toggle_mode(true);
933 curve_edit_tilt->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
934 curve_edit_tilt->set_tooltip_text(TTR("Select Tilt Handles"));
935 toolbar->add_child(curve_edit_tilt);
936 curve_edit_tilt->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_TILT));
937
938 curve_create = memnew(Button);
939 curve_create->set_theme_type_variation(SceneStringName(FlatButton));
940 curve_create->set_toggle_mode(true);
941 curve_create->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
942 curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
943 curve_create->set_accessibility_name(TTRC("Add Point (in empty space)"));
944 toolbar->add_child(curve_create);
945 curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_CREATE));
946
947 curve_del = memnew(Button);
948 curve_del->set_theme_type_variation(SceneStringName(FlatButton));
949 curve_del->set_toggle_mode(true);
950 curve_del->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
951 curve_del->set_tooltip_text(TTR("Delete Point"));
952 toolbar->add_child(curve_del);
953 curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_DELETE));
954
955 curve_closed = memnew(Button);
956 curve_closed->set_theme_type_variation(SceneStringName(FlatButton));
957 curve_closed->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
958 curve_closed->set_tooltip_text(TTR("Close Curve"));
959 toolbar->add_child(curve_closed);
960 curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_toggle_closed_curve));
961
962 curve_clear_points = memnew(Button);
963 curve_clear_points->set_theme_type_variation(SceneStringName(FlatButton));
964 curve_clear_points->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
965 curve_clear_points->set_tooltip_text(TTR("Clear Points"));
966 curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_confirm_clear_points));
967 toolbar->add_child(curve_clear_points);
968
969 clear_points_dialog = memnew(ConfirmationDialog);
970 clear_points_dialog->set_title(TTR("Please Confirm..."));
971 clear_points_dialog->set_text(TTR("Remove all curve points?"));
972 clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path3DEditorPlugin::_clear_points));
973 toolbar->add_child(clear_points_dialog);
974
975 handle_menu = memnew(MenuButton);
976 handle_menu->set_flat(false);
977 handle_menu->set_theme_type_variation("FlatMenuButton");
978 handle_menu->set_text(TTR("Options"));
979 toolbar->add_child(handle_menu);
980
981 create_curve_button = memnew(Button);
982 create_curve_button->set_text(TTR("Create Curve"));
983 create_curve_button->hide();
984 topmenu_bar->add_child(create_curve_button);
985 create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_create_curve));
986
987 PopupMenu *menu = handle_menu->get_popup();
988 menu->add_check_item(TTR("Mirror Handle Angles"));
989 menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
990 menu->add_check_item(TTR("Mirror Handle Lengths"));
991 menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
992 menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path3DEditorPlugin::_handle_option_pressed));
993
994 curve_edit->set_pressed_no_signal(true);
995
996 topmenu_bar->connect(SceneStringName(theme_changed), callable_mp(this, &Path3DEditorPlugin::_update_theme));
997 Node3DEditor::get_singleton()->add_control_to_menu_panel(topmenu_bar);
998 }
999
1000 Ref<EditorNode3DGizmo> Path3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {
1001 Ref<Path3DGizmo> ref;
1002
1003 Path3D *path = Object::cast_to<Path3D>(p_spatial);
1004 if (path) {
1005 ref.instantiate(path);
1006 }
1007
1008 return ref;
1009 }
1010
1011 bool Path3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
1012 return Object::cast_to<Path3D>(p_spatial) != nullptr;
1013 }
1014
1015 String Path3DGizmoPlugin::get_gizmo_name() const {
1016 return "Path3D";
1017 }
1018
1019 void Path3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
1020 Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1021 ERR_FAIL_NULL(path);
1022
1023 Ref<Curve3D> curve = path->get_curve();
1024
1025 Ref<StandardMaterial3D> handle_material = get_material("handles", p_gizmo);
1026 Ref<StandardMaterial3D> first_pt_handle_material = get_material("first_pt_handle", p_gizmo);
1027 Ref<StandardMaterial3D> last_pt_handle_material = get_material("last_pt_handle", p_gizmo);
1028 Ref<StandardMaterial3D> closed_pt_handle_material = get_material("closed_pt_handle", p_gizmo);
1029
1030 first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));
1031 last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));
1032 closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));
1033
1034 PackedVector3Array handles;
1035
1036 if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
1037 for (int idx = 0; idx < curve->get_point_count(); ++idx) {
1038 // Collect handles.
1039 const Vector3 pos = curve->get_point_position(idx);
1040
1041 handles.append(pos);
1042 }
1043 }
1044
1045 if (handles.size()) {
1046 // Point count.
1047 const int pc = handles.size();
1048
1049 // Initialize arrays for first point.
1050 PackedVector3Array first_pt;
1051 first_pt.append(handles[0]);
1052
1053 // Initialize arrays and add handle for last point if needed.
1054 if (pc > 1) {
1055 PackedVector3Array last_pt;
1056 last_pt.append(handles[handles.size() - 1]);
1057 handles.remove_at(handles.size() - 1);
1058 if (curve->is_closed()) {
1059 p_gizmo->add_vertices(last_pt, handle_material, Mesh::PRIMITIVE_POINTS);
1060 } else {
1061 p_gizmo->add_vertices(last_pt, last_pt_handle_material, Mesh::PRIMITIVE_POINTS);
1062 }
1063 }
1064
1065 // Add handle for first point.
1066 handles.remove_at(0);
1067 if (curve->is_closed()) {
1068 p_gizmo->add_vertices(first_pt, closed_pt_handle_material, Mesh::PRIMITIVE_POINTS);
1069 } else {
1070 p_gizmo->add_vertices(first_pt, first_pt_handle_material, Mesh::PRIMITIVE_POINTS);
1071 }
1072
1073 // Add handles for remaining intermediate points.
1074 if (!handles.is_empty()) {
1075 p_gizmo->add_vertices(handles, handle_material, Mesh::PRIMITIVE_POINTS);
1076 }
1077 }
1078 }
1079
1080 int Path3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
1081 Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1082 ERR_FAIL_NULL_V(path, -1);
1083 Ref<Curve3D> curve = path->get_curve();
1084 ERR_FAIL_COND_V(curve.is_null(), -1);
1085
1086 if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
1087 for (int idx = 0; idx < curve->get_point_count(); ++idx) {
1088 Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
1089 if (p_camera->unproject_position(pos).distance_to(p_point) < 20) {
1090 return idx;
1091 }
1092 }
1093 }
1094 return -1;
1095 }
1096
1097 Vector<int> Path3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
1098 Vector<int> contained_points;
1099
1100 Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1101 ERR_FAIL_NULL_V(path, contained_points);
1102 Ref<Curve3D> curve = path->get_curve();
1103 ERR_FAIL_COND_V(curve.is_null(), contained_points);
1104
1105 if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
1106 for (int idx = 0; idx < curve->get_point_count(); ++idx) {
1107 Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
1108 bool is_contained_in_frustum = true;
1109 for (int i = 0; i < p_frustum.size(); ++i) {
1110 if (p_frustum[i].distance_to(pos) > 0) {
1111 is_contained_in_frustum = false;
1112 break;
1113 }
1114 }
1115
1116 if (is_contained_in_frustum) {
1117 contained_points.push_back(idx);
1118 }
1119 }
1120 }
1121
1122 return contained_points;
1123 }
1124
1125 Transform3D Path3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
1126 Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1127 ERR_FAIL_NULL_V(path, Transform3D());
1128 Ref<Curve3D> curve = path->get_curve();
1129 ERR_FAIL_COND_V(curve.is_null(), Transform3D());
1130 ERR_FAIL_INDEX_V(p_id, curve->get_point_count(), Transform3D());
1131
1132 Basis basis = transformation_locked_basis.has(p_id) ? transformation_locked_basis[p_id] : curve->get_point_baked_posture(p_id, true);
1133 Vector3 pos = curve->get_point_position(p_id);
1134
1135 Transform3D t = Transform3D(basis, pos);
1136 return t;
1137 }
1138
1139 void Path3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
1140 Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1141 ERR_FAIL_NULL(path);
1142 Ref<Curve3D> curve = path->get_curve();
1143 ERR_FAIL_COND(curve.is_null());
1144 ERR_FAIL_INDEX(p_id, curve->get_point_count());
1145
1146 if (!transformation_locked_basis.has(p_id)) {
1147 transformation_locked_basis[p_id] = Basis(curve->get_point_baked_posture(p_id, true));
1148 }
1149 curve->set_point_position(p_id, p_transform.origin);
1150 }
1151
1152 void Path3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
1153 Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1154 ERR_FAIL_NULL(path);
1155 Ref<Curve3D> curve = path->get_curve();
1156 ERR_FAIL_COND(curve.is_null());
1157
1158 transformation_locked_basis.clear();
1159
1160 if (p_cancel) {
1161 for (int i = 0; i < p_ids.size(); ++i) {
1162 curve->set_point_position(p_ids[i], p_restore[i].origin);
1163 }
1164 return;
1165 }
1166
1167 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1168
1169 undo_redo->create_action(TTR("Set Curve Point Position"));
1170
1171 for (int i = 0; i < p_ids.size(); ++i) {
1172 const int idx = p_ids[i];
1173 undo_redo->add_do_method(curve.ptr(), "set_point_position", idx, curve->get_point_position(idx));
1174 undo_redo->add_undo_method(curve.ptr(), "set_point_position", idx, p_restore[i].origin);
1175 }
1176 undo_redo->commit_action();
1177 }
1178
1179 int Path3DGizmoPlugin::get_priority() const {
1180 return -1;
1181 }
1182
1183 Path3DGizmoPlugin::Path3DGizmoPlugin() {
1184 Color path_color = SceneTree::get_singleton()->get_debug_paths_color();
1185 Color path_tilt_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/path_tilt");
1186
1187 create_material("path_material", path_color);
1188 create_material("path_thin_material", Color(0.6, 0.6, 0.6));
1189 create_material("path_tilt_material", path_tilt_color);
1190 create_material("path_tilt_muted_material", path_tilt_color * 0.7);
1191 create_handle_material("handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
1192 create_handle_material("first_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
1193 create_handle_material("last_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
1194 create_handle_material("closed_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
1195 create_handle_material("sec_handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorCurveHandle"), EditorStringName(EditorIcons)));
1196 }