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