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
9912 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 (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
for (int idx = 0; idx < c->get_point_count(); idx++) {
413
// Collect primary-handles.
414
const Vector3 pos = c->get_point_position(idx);
415
primary_handle_points.append(pos);
416
417
HandleInfo info;
418
info.point_idx = idx;
419
420
// Collect in-handles except for the first point.
421
if (idx > (c->is_closed() ? -1 : 0) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
422
const Vector3 in = c->get_point_in(idx);
423
424
info.type = HandleType::HANDLE_TYPE_IN;
425
const int handle_idx = idx * 3 + 0;
426
collected_secondary_handle_ids.append(handle_idx);
427
_secondary_handles_info.write[handle_idx] = info;
428
429
secondary_handle_points.append(pos + in);
430
handle_lines.append(pos);
431
handle_lines.append(pos + in);
432
}
433
434
// Collect out-handles except for the last point.
435
if (idx < (c->is_closed() ? c->get_point_count() : c->get_point_count() - 1) && Path3DEditorPlugin::singleton->curve_edit_curve->is_pressed()) {
436
const Vector3 out = c->get_point_out(idx);
437
438
info.type = HandleType::HANDLE_TYPE_OUT;
439
const int handle_idx = idx * 3 + 1;
440
collected_secondary_handle_ids.append(handle_idx);
441
_secondary_handles_info.write[handle_idx] = info;
442
443
secondary_handle_points.append(pos + out);
444
handle_lines.append(pos);
445
handle_lines.append(pos + out);
446
}
447
448
// Collect tilt-handles.
449
if (Path3DEditorPlugin::singleton->curve_edit_tilt->is_pressed()) {
450
// Tilt handle.
451
{
452
info.type = HandleType::HANDLE_TYPE_TILT;
453
const int handle_idx = idx * 3 + 2;
454
collected_secondary_handle_ids.append(handle_idx);
455
_secondary_handles_info.write[handle_idx] = info;
456
457
const Basis posture = c->get_point_baked_posture(idx, true);
458
const Vector3 up = posture.get_column(1);
459
secondary_handle_points.append(pos + up * disk_size);
460
tilt_handle_lines.append(pos);
461
tilt_handle_lines.append(pos + up * disk_size);
462
}
463
464
// Tilt disk.
465
{
466
const Basis posture = c->get_point_baked_posture(idx, false);
467
const Vector3 up = posture.get_column(1);
468
const Vector3 side = posture.get_column(0);
469
470
PackedVector3Array disk;
471
disk.append(pos);
472
473
const int n = 36;
474
for (int i = 0; i <= n; i++) {
475
const float a = Math::TAU * i / n;
476
const Vector3 edge = std::sin(a) * side + std::cos(a) * up;
477
disk.append(pos + edge * disk_size);
478
}
479
add_vertices(disk, debug_material, Mesh::PRIMITIVE_LINE_STRIP);
480
}
481
}
482
}
483
484
if (handle_lines.size() > 1) {
485
add_lines(handle_lines, path_thin_material);
486
}
487
488
if (tilt_handle_lines.size() > 1) {
489
add_lines(tilt_handle_lines, path_tilt_material);
490
}
491
492
if (!Path3DEditorPlugin::singleton->curve_edit->is_pressed() && primary_handle_points.size()) {
493
// Need to define indices separately.
494
// Point count.
495
const int pc = primary_handle_points.size();
496
Vector<int> idx;
497
idx.resize(pc);
498
int *idx_ptr = idx.ptrw();
499
for (int j = 0; j < pc; j++) {
500
idx_ptr[j] = j;
501
}
502
503
// Initialize arrays for first point.
504
PackedVector3Array first_pt_handle_point;
505
Vector<int> first_pt_id;
506
first_pt_handle_point.append(primary_handle_points[0]);
507
first_pt_id.append(idx[0]);
508
509
// Initialize arrays and add handle for last point if needed.
510
if (pc > 1) {
511
PackedVector3Array last_pt_handle_point;
512
Vector<int> last_pt_id;
513
last_pt_handle_point.append(primary_handle_points[pc - 1]);
514
last_pt_id.append(idx[pc - 1]);
515
primary_handle_points.remove_at(pc - 1);
516
idx.remove_at(pc - 1);
517
add_handles(last_pt_handle_point, c->is_closed() ? handles_material : last_pt_handle_material, last_pt_id);
518
}
519
520
// Add handle for first point.
521
primary_handle_points.remove_at(0);
522
idx.remove_at(0);
523
add_handles(first_pt_handle_point, c->is_closed() ? closed_pt_handle_material : first_pt_handle_material, first_pt_id);
524
525
// Add handles for remaining intermediate points.
526
if (!primary_handle_points.is_empty()) {
527
add_handles(primary_handle_points, handles_material, idx);
528
}
529
}
530
if (secondary_handle_points.size()) {
531
add_handles(secondary_handle_points, sec_handles_material, collected_secondary_handle_ids, false, true);
532
}
533
// Draw the gizmo plugin manually, because handles are registered. In which case, the caller code skips drawing the gizmo plugin.
534
gizmo_plugin->redraw(this);
535
}
536
}
537
538
void Path3DGizmo::_update_transform_gizmo() {
539
Node3DEditor::get_singleton()->update_transform_gizmo();
540
}
541
542
Path3DGizmo::Path3DGizmo(Path3D *p_path, float p_disk_size) {
543
path = p_path;
544
disk_size = p_disk_size;
545
set_node_3d(p_path);
546
orig_in_length = 0;
547
orig_out_length = 0;
548
549
// Connecting to a signal once, rather than plaguing the implementation with calls to `Node3DEditor::update_transform_gizmo`.
550
path->connect("curve_changed", callable_mp(this, &Path3DGizmo::_update_transform_gizmo));
551
path->connect("debug_color_changed", callable_mp(this, &Path3DGizmo::redraw));
552
553
Path3DEditorPlugin::singleton->curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
554
Path3DEditorPlugin::singleton->curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
555
Path3DEditorPlugin::singleton->curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
556
Path3DEditorPlugin::singleton->curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
557
Path3DEditorPlugin::singleton->curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DGizmo::redraw));
558
}
559
560
EditorPlugin::AfterGUIInput Path3DEditorPlugin::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
561
if (!path) {
562
return EditorPlugin::AFTER_GUI_INPUT_PASS;
563
}
564
Ref<Curve3D> c = path->get_curve();
565
if (c.is_null()) {
566
return EditorPlugin::AFTER_GUI_INPUT_PASS;
567
}
568
Transform3D gt = path->get_global_transform();
569
Transform3D it = gt.affine_inverse();
570
571
static const int click_dist = 10; //should make global
572
573
Ref<InputEventMouseButton> mb = p_event;
574
575
if (mb.is_valid()) {
576
Point2 mbpos(mb->get_position().x, mb->get_position().y);
577
578
Node3DEditorViewport *viewport = nullptr;
579
for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
580
Node3DEditorViewport *vp = Node3DEditor::get_singleton()->get_editor_viewport(i);
581
if (vp->get_camera_3d() == p_camera) {
582
viewport = vp;
583
break;
584
}
585
}
586
587
ERR_FAIL_NULL_V(viewport, EditorPlugin::AFTER_GUI_INPUT_PASS);
588
589
if (!mb->is_pressed()) {
590
set_handle_clicked(false);
591
}
592
593
if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && (curve_create->is_pressed() || (curve_edit->is_pressed() && mb->is_command_or_control_pressed()))) {
594
//click into curve, break it down
595
Vector<Vector3> v3a = c->tessellate();
596
int rc = v3a.size();
597
int closest_seg = -1;
598
Vector3 closest_seg_point;
599
600
if (rc >= 2) {
601
int idx = 0;
602
const Vector3 *r = v3a.ptr();
603
float closest_d = 1e20;
604
605
if (viewport->point_to_screen(gt.xform(c->get_point_position(0))).distance_to(mbpos) < click_dist) {
606
return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
607
}
608
609
for (int i = 0; i < c->get_point_count() - 1; i++) {
610
//find the offset and point index of the place to break up
611
int j = idx;
612
if (viewport->point_to_screen(gt.xform(c->get_point_position(i + 1))).distance_to(mbpos) < click_dist) {
613
return EditorPlugin::AFTER_GUI_INPUT_PASS; //nope, existing
614
}
615
616
while (j < rc && c->get_point_position(i + 1) != r[j]) {
617
Vector3 from = r[j];
618
Vector3 to = r[j + 1];
619
real_t cdist = from.distance_to(to);
620
from = gt.xform(from);
621
to = gt.xform(to);
622
if (cdist > 0) {
623
const Vector2 segment_a = viewport->point_to_screen(from);
624
const Vector2 segment_b = viewport->point_to_screen(to);
625
Vector2 inters = Geometry2D::get_closest_point_to_segment(mbpos, segment_a, segment_b);
626
float d = inters.distance_to(mbpos);
627
628
if (d < 10 && d < closest_d) {
629
closest_d = d;
630
closest_seg = i;
631
Vector3 ray_from = viewport->get_ray_pos(mbpos);
632
Vector3 ray_dir = viewport->get_ray(mbpos);
633
634
Vector3 ra, rb;
635
Geometry3D::get_closest_points_between_segments(ray_from, ray_from + ray_dir * 4096, from, to, ra, rb);
636
637
closest_seg_point = it.xform(rb);
638
}
639
}
640
j++;
641
}
642
if (idx == j) {
643
idx++; //force next
644
} else {
645
idx = j; //swap
646
}
647
648
if (j == rc) {
649
break;
650
}
651
}
652
}
653
654
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
655
if (closest_seg != -1) {
656
//subdivide
657
658
ur->create_action(TTR("Split Path"));
659
ur->add_do_method(c.ptr(), "add_point", closest_seg_point, Vector3(), Vector3(), closest_seg + 1);
660
ur->add_undo_method(c.ptr(), "remove_point", closest_seg + 1);
661
ur->commit_action();
662
return EditorPlugin::AFTER_GUI_INPUT_STOP;
663
664
} else {
665
Vector3 origin;
666
if (c->get_point_count() == 0) {
667
origin = path->get_transform().get_origin();
668
} else {
669
origin = gt.xform(c->get_point_position(c->get_point_count() - 1));
670
}
671
Plane p(p_camera->get_transform().basis.get_column(2), origin);
672
Vector3 ray_from = viewport->get_ray_pos(mbpos);
673
Vector3 ray_dir = viewport->get_ray(mbpos);
674
675
Vector3 inters;
676
if (p.intersects_ray(ray_from, ray_dir, &inters)) {
677
ur->create_action(TTR("Add Point to Curve"));
678
ur->add_do_method(c.ptr(), "add_point", it.xform(inters), Vector3(), Vector3(), -1);
679
ur->add_undo_method(c.ptr(), "remove_point", c->get_point_count());
680
ur->commit_action();
681
return EditorPlugin::AFTER_GUI_INPUT_STOP;
682
}
683
684
//add new at pos
685
}
686
687
} 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()))) {
688
for (int i = 0; i < c->get_point_count(); i++) {
689
real_t dist_to_p = viewport->point_to_screen(gt.xform(c->get_point_position(i))).distance_to(mbpos);
690
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);
691
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);
692
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);
693
694
// Find the offset and point index of the place to break up.
695
// Also check for the control points.
696
if (dist_to_p < click_dist) {
697
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
698
ur->create_action(TTR("Remove Path Point"));
699
ur->add_do_method(c.ptr(), "remove_point", i);
700
ur->add_undo_method(c.ptr(), "add_point", c->get_point_position(i), c->get_point_in(i), c->get_point_out(i), i);
701
ur->commit_action();
702
return EditorPlugin::AFTER_GUI_INPUT_STOP;
703
} else if (dist_to_p_out < click_dist) {
704
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
705
ur->create_action(TTR("Reset Out-Control Point"));
706
ur->add_do_method(c.ptr(), "set_point_out", i, Vector3());
707
ur->add_undo_method(c.ptr(), "set_point_out", i, c->get_point_out(i));
708
ur->commit_action();
709
return EditorPlugin::AFTER_GUI_INPUT_STOP;
710
} else if (dist_to_p_in < click_dist) {
711
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
712
ur->create_action(TTR("Reset In-Control Point"));
713
ur->add_do_method(c.ptr(), "set_point_in", i, Vector3());
714
ur->add_undo_method(c.ptr(), "set_point_in", i, c->get_point_in(i));
715
ur->commit_action();
716
return EditorPlugin::AFTER_GUI_INPUT_STOP;
717
} else if (dist_to_p_up < click_dist) {
718
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
719
ur->create_action(TTR("Reset Point Tilt"));
720
ur->add_do_method(c.ptr(), "set_point_tilt", i, 0.0f);
721
ur->add_undo_method(c.ptr(), "set_point_tilt", i, c->get_point_tilt(i));
722
ur->commit_action();
723
return EditorPlugin::AFTER_GUI_INPUT_STOP;
724
}
725
}
726
}
727
}
728
729
return EditorPlugin::AFTER_GUI_INPUT_PASS;
730
}
731
732
void Path3DEditorPlugin::edit(Object *p_object) {
733
if (p_object) {
734
path = Object::cast_to<Path3D>(p_object);
735
if (path) {
736
if (path->get_curve().is_valid()) {
737
path->get_curve()->emit_signal(CoreStringName(changed));
738
}
739
_update_toolbar();
740
}
741
} else {
742
Path3D *pre = path;
743
path = nullptr;
744
if (pre && pre->get_curve().is_valid()) {
745
pre->get_curve()->emit_signal(CoreStringName(changed));
746
}
747
}
748
749
update_overlays();
750
//collision_polygon_editor->edit(Object::cast_to<Node>(p_object));
751
}
752
753
bool Path3DEditorPlugin::handles(Object *p_object) const {
754
return p_object->is_class("Path3D");
755
}
756
757
void Path3DEditorPlugin::make_visible(bool p_visible) {
758
if (p_visible) {
759
topmenu_bar->show();
760
} else {
761
topmenu_bar->hide();
762
763
{
764
Path3D *pre = path;
765
path = nullptr;
766
if (pre && pre->get_curve().is_valid()) {
767
pre->get_curve()->emit_signal(CoreStringName(changed));
768
}
769
}
770
}
771
}
772
773
void Path3DEditorPlugin::_mode_changed(int p_mode) {
774
curve_create->set_pressed_no_signal(p_mode == MODE_CREATE);
775
curve_edit_curve->set_pressed_no_signal(p_mode == MODE_EDIT_CURVE);
776
curve_edit_tilt->set_pressed_no_signal(p_mode == MODE_EDIT_TILT);
777
curve_edit->set_pressed_no_signal(p_mode == MODE_EDIT);
778
curve_del->set_pressed_no_signal(p_mode == MODE_DELETE);
779
780
Node3DEditor::get_singleton()->clear_subgizmo_selection();
781
}
782
783
void Path3DEditorPlugin::_toggle_closed_curve() {
784
Ref<Curve3D> c = path->get_curve();
785
if (c.is_null()) {
786
return;
787
}
788
if (c->get_point_count() < 2) {
789
return;
790
}
791
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
792
ur->create_action(TTR("Toggle Open/Closed Curve"));
793
ur->add_do_method(c.ptr(), "set_closed", !c.ptr()->is_closed());
794
ur->add_undo_method(c.ptr(), "set_closed", c.ptr()->is_closed());
795
ur->commit_action();
796
}
797
798
void Path3DEditorPlugin::_handle_option_pressed(int p_option) {
799
PopupMenu *pm;
800
pm = handle_menu->get_popup();
801
802
switch (p_option) {
803
case HANDLE_OPTION_ANGLE: {
804
bool is_checked = pm->is_item_checked(HANDLE_OPTION_ANGLE);
805
mirror_handle_angle = !is_checked;
806
pm->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
807
pm->set_item_disabled(HANDLE_OPTION_LENGTH, !mirror_handle_angle);
808
} break;
809
case HANDLE_OPTION_LENGTH: {
810
bool is_checked = pm->is_item_checked(HANDLE_OPTION_LENGTH);
811
mirror_handle_length = !is_checked;
812
pm->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
813
} break;
814
}
815
}
816
817
void Path3DEditorPlugin::_create_curve() {
818
ERR_FAIL_NULL(path);
819
820
Ref<Curve3D> new_curve;
821
new_curve.instantiate();
822
823
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
824
undo_redo->create_action(TTR("Create Curve in Path3D"));
825
undo_redo->add_do_property(path, "curve", new_curve);
826
undo_redo->add_undo_property(path, "curve", Ref<Curve3D>());
827
undo_redo->add_do_method(this, "_update_toolbar");
828
undo_redo->add_undo_method(this, "_update_toolbar");
829
undo_redo->commit_action();
830
}
831
832
void Path3DEditorPlugin::_confirm_clear_points() {
833
if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
834
return;
835
}
836
clear_points_dialog->reset_size();
837
clear_points_dialog->popup_centered();
838
}
839
840
void Path3DEditorPlugin::_clear_points() {
841
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
842
PackedVector3Array points = path->get_curve()->get_points().duplicate();
843
844
undo_redo->create_action(TTR("Clear Curve Points"));
845
undo_redo->add_do_method(this, "_clear_curve_points");
846
undo_redo->add_undo_method(this, "_restore_curve_points", points);
847
undo_redo->commit_action();
848
}
849
850
void Path3DEditorPlugin::_clear_curve_points() {
851
if (!path || path->get_curve().is_null() || path->get_curve()->get_point_count() == 0) {
852
return;
853
}
854
Ref<Curve3D> curve = path->get_curve();
855
curve->set_closed(false);
856
curve->clear_points();
857
}
858
859
void Path3DEditorPlugin::_restore_curve_points(const PackedVector3Array &p_points) {
860
if (!path || path->get_curve().is_null()) {
861
return;
862
}
863
Ref<Curve3D> curve = path->get_curve();
864
865
if (curve->get_point_count() > 0) {
866
curve->clear_points();
867
}
868
869
for (int i = 0; i < p_points.size(); i += 3) {
870
curve->add_point(p_points[i + 2], p_points[i], p_points[i + 1]);
871
}
872
}
873
874
void Path3DEditorPlugin::_update_theme() {
875
curve_edit->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveEdit")));
876
curve_edit_curve->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCurve")));
877
curve_edit_tilt->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveTilt")));
878
curve_create->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveCreate")));
879
curve_del->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveDelete")));
880
curve_closed->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("CurveClose")));
881
curve_clear_points->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Clear")));
882
create_curve_button->set_button_icon(topmenu_bar->get_editor_theme_icon(SNAME("Curve3D")));
883
}
884
885
void Path3DEditorPlugin::_update_toolbar() {
886
if (!path) {
887
return;
888
}
889
bool has_curve = path->get_curve().is_valid();
890
toolbar->set_visible(has_curve);
891
create_curve_button->set_visible(!has_curve);
892
}
893
894
void Path3DEditorPlugin::_bind_methods() {
895
ClassDB::bind_method(D_METHOD("_update_toolbar"), &Path3DEditorPlugin::_update_toolbar);
896
ClassDB::bind_method(D_METHOD("_clear_curve_points"), &Path3DEditorPlugin::_clear_curve_points);
897
ClassDB::bind_method(D_METHOD("_restore_curve_points"), &Path3DEditorPlugin::_restore_curve_points);
898
}
899
900
Path3DEditorPlugin::Path3DEditorPlugin() {
901
singleton = this;
902
mirror_handle_angle = true;
903
mirror_handle_length = true;
904
905
disk_size = EDITOR_GET("editors/3d_gizmos/gizmo_settings/path3d_tilt_disk_size");
906
907
Ref<Path3DGizmoPlugin> gizmo_plugin = memnew(Path3DGizmoPlugin(disk_size));
908
Node3DEditor::get_singleton()->add_gizmo_plugin(gizmo_plugin);
909
path_3d_gizmo_plugin = gizmo_plugin;
910
911
topmenu_bar = memnew(HBoxContainer);
912
topmenu_bar->hide();
913
914
toolbar = memnew(HBoxContainer);
915
topmenu_bar->add_child(toolbar);
916
917
curve_edit = memnew(Button);
918
curve_edit->set_theme_type_variation(SceneStringName(FlatButton));
919
curve_edit->set_toggle_mode(true);
920
curve_edit->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
921
curve_edit->set_tooltip_text(TTR("Select Points") + "\n" + TTR("Shift+Click: Select multiple Points") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Click: Add Point") + "\n" + TTR("Right Click: Delete Point"));
922
curve_edit->set_accessibility_name(TTRC("Select Points"));
923
toolbar->add_child(curve_edit);
924
curve_edit->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT));
925
926
curve_edit_curve = memnew(Button);
927
curve_edit_curve->set_theme_type_variation(SceneStringName(FlatButton));
928
curve_edit_curve->set_toggle_mode(true);
929
curve_edit_curve->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
930
curve_edit_curve->set_tooltip_text(TTR("Select Control Points") + "\n" + TTR("Shift+Click: Drag out Control Points"));
931
curve_edit_curve->set_accessibility_name(TTRC("Select Control Points"));
932
toolbar->add_child(curve_edit_curve);
933
curve_edit_curve->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_CURVE));
934
935
curve_edit_tilt = memnew(Button);
936
curve_edit_tilt->set_theme_type_variation(SceneStringName(FlatButton));
937
curve_edit_tilt->set_toggle_mode(true);
938
curve_edit_tilt->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
939
curve_edit_tilt->set_tooltip_text(TTR("Select Tilt Handles"));
940
toolbar->add_child(curve_edit_tilt);
941
curve_edit_tilt->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_EDIT_TILT));
942
943
curve_create = memnew(Button);
944
curve_create->set_theme_type_variation(SceneStringName(FlatButton));
945
curve_create->set_toggle_mode(true);
946
curve_create->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
947
curve_create->set_tooltip_text(TTR("Add Point (in empty space)") + "\n" + TTR("Split Segment (in curve)"));
948
curve_create->set_accessibility_name(TTRC("Add Point (in empty space)"));
949
toolbar->add_child(curve_create);
950
curve_create->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_CREATE));
951
952
curve_del = memnew(Button);
953
curve_del->set_theme_type_variation(SceneStringName(FlatButton));
954
curve_del->set_toggle_mode(true);
955
curve_del->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
956
curve_del->set_tooltip_text(TTR("Delete Point"));
957
toolbar->add_child(curve_del);
958
curve_del->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_mode_changed).bind(MODE_DELETE));
959
960
curve_closed = memnew(Button);
961
curve_closed->set_theme_type_variation(SceneStringName(FlatButton));
962
curve_closed->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
963
curve_closed->set_tooltip_text(TTR("Close Curve"));
964
toolbar->add_child(curve_closed);
965
curve_closed->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_toggle_closed_curve));
966
967
curve_clear_points = memnew(Button);
968
curve_clear_points->set_theme_type_variation(SceneStringName(FlatButton));
969
curve_clear_points->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
970
curve_clear_points->set_tooltip_text(TTR("Clear Points"));
971
curve_clear_points->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_confirm_clear_points));
972
toolbar->add_child(curve_clear_points);
973
974
clear_points_dialog = memnew(ConfirmationDialog);
975
clear_points_dialog->set_title(TTR("Please Confirm..."));
976
clear_points_dialog->set_text(TTR("Remove all curve points?"));
977
clear_points_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Path3DEditorPlugin::_clear_points));
978
toolbar->add_child(clear_points_dialog);
979
980
handle_menu = memnew(MenuButton);
981
handle_menu->set_flat(false);
982
handle_menu->set_theme_type_variation("FlatMenuButton");
983
handle_menu->set_text(TTR("Options"));
984
toolbar->add_child(handle_menu);
985
986
create_curve_button = memnew(Button);
987
create_curve_button->set_text(TTR("Create Curve"));
988
create_curve_button->hide();
989
topmenu_bar->add_child(create_curve_button);
990
create_curve_button->connect(SceneStringName(pressed), callable_mp(this, &Path3DEditorPlugin::_create_curve));
991
992
PopupMenu *menu = handle_menu->get_popup();
993
menu->add_check_item(TTR("Mirror Handle Angles"));
994
menu->set_item_checked(HANDLE_OPTION_ANGLE, mirror_handle_angle);
995
menu->add_check_item(TTR("Mirror Handle Lengths"));
996
menu->set_item_checked(HANDLE_OPTION_LENGTH, mirror_handle_length);
997
menu->connect(SceneStringName(id_pressed), callable_mp(this, &Path3DEditorPlugin::_handle_option_pressed));
998
999
curve_edit->set_pressed_no_signal(true);
1000
1001
topmenu_bar->connect(SceneStringName(theme_changed), callable_mp(this, &Path3DEditorPlugin::_update_theme));
1002
Node3DEditor::get_singleton()->add_control_to_menu_panel(topmenu_bar);
1003
}
1004
1005
Ref<EditorNode3DGizmo> Path3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {
1006
Ref<Path3DGizmo> ref;
1007
1008
Path3D *path = Object::cast_to<Path3D>(p_spatial);
1009
if (path) {
1010
ref.instantiate(path, disk_size);
1011
}
1012
1013
return ref;
1014
}
1015
1016
bool Path3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
1017
return Object::cast_to<Path3D>(p_spatial) != nullptr;
1018
}
1019
1020
String Path3DGizmoPlugin::get_gizmo_name() const {
1021
return "Path3D";
1022
}
1023
1024
void Path3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
1025
Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1026
ERR_FAIL_NULL(path);
1027
1028
Ref<Curve3D> curve = path->get_curve();
1029
1030
Ref<StandardMaterial3D> handle_material = get_material("handles", p_gizmo);
1031
Ref<StandardMaterial3D> first_pt_handle_material = get_material("first_pt_handle", p_gizmo);
1032
Ref<StandardMaterial3D> last_pt_handle_material = get_material("last_pt_handle", p_gizmo);
1033
Ref<StandardMaterial3D> closed_pt_handle_material = get_material("closed_pt_handle", p_gizmo);
1034
1035
first_pt_handle_material->set_albedo(Color(0.2, 1.0, 0.0));
1036
last_pt_handle_material->set_albedo(Color(1.0, 0.2, 0.0));
1037
closed_pt_handle_material->set_albedo(Color(1.0, 0.8, 0.0));
1038
1039
PackedVector3Array handles;
1040
1041
if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
1042
for (int idx = 0; idx < curve->get_point_count(); ++idx) {
1043
// Collect handles.
1044
const Vector3 pos = curve->get_point_position(idx);
1045
1046
handles.append(pos);
1047
}
1048
}
1049
1050
if (handles.size()) {
1051
// Point count.
1052
const int pc = handles.size();
1053
1054
// Initialize arrays for first point.
1055
PackedVector3Array first_pt;
1056
first_pt.append(handles[0]);
1057
1058
// Initialize arrays and add handle for last point if needed.
1059
if (pc > 1) {
1060
PackedVector3Array last_pt;
1061
last_pt.append(handles[handles.size() - 1]);
1062
handles.remove_at(handles.size() - 1);
1063
if (curve->is_closed()) {
1064
p_gizmo->add_vertices(last_pt, handle_material, Mesh::PRIMITIVE_POINTS);
1065
} else {
1066
p_gizmo->add_vertices(last_pt, last_pt_handle_material, Mesh::PRIMITIVE_POINTS);
1067
}
1068
}
1069
1070
// Add handle for first point.
1071
handles.remove_at(0);
1072
if (curve->is_closed()) {
1073
p_gizmo->add_vertices(first_pt, closed_pt_handle_material, Mesh::PRIMITIVE_POINTS);
1074
} else {
1075
p_gizmo->add_vertices(first_pt, first_pt_handle_material, Mesh::PRIMITIVE_POINTS);
1076
}
1077
1078
// Add handles for remaining intermediate points.
1079
if (!handles.is_empty()) {
1080
p_gizmo->add_vertices(handles, handle_material, Mesh::PRIMITIVE_POINTS);
1081
}
1082
}
1083
}
1084
1085
int Path3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
1086
Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1087
ERR_FAIL_NULL_V(path, -1);
1088
Ref<Curve3D> curve = path->get_curve();
1089
ERR_FAIL_COND_V(curve.is_null(), -1);
1090
1091
if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
1092
for (int idx = 0; idx < curve->get_point_count(); ++idx) {
1093
Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
1094
if (p_camera->unproject_position(pos).distance_to(p_point) < 20) {
1095
return idx;
1096
}
1097
}
1098
}
1099
return -1;
1100
}
1101
1102
Vector<int> Path3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
1103
Vector<int> contained_points;
1104
1105
Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1106
ERR_FAIL_NULL_V(path, contained_points);
1107
Ref<Curve3D> curve = path->get_curve();
1108
ERR_FAIL_COND_V(curve.is_null(), contained_points);
1109
1110
if (Path3DEditorPlugin::singleton->curve_edit->is_pressed()) {
1111
for (int idx = 0; idx < curve->get_point_count(); ++idx) {
1112
Vector3 pos = path->get_global_transform().xform(curve->get_point_position(idx));
1113
bool is_contained_in_frustum = true;
1114
for (int i = 0; i < p_frustum.size(); ++i) {
1115
if (p_frustum[i].distance_to(pos) > 0) {
1116
is_contained_in_frustum = false;
1117
break;
1118
}
1119
}
1120
1121
if (is_contained_in_frustum) {
1122
contained_points.push_back(idx);
1123
}
1124
}
1125
}
1126
1127
return contained_points;
1128
}
1129
1130
Transform3D Path3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
1131
Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1132
ERR_FAIL_NULL_V(path, Transform3D());
1133
Ref<Curve3D> curve = path->get_curve();
1134
ERR_FAIL_COND_V(curve.is_null(), Transform3D());
1135
ERR_FAIL_INDEX_V(p_id, curve->get_point_count(), Transform3D());
1136
1137
Basis basis = transformation_locked_basis.has(p_id) ? transformation_locked_basis[p_id] : curve->get_point_baked_posture(p_id, true);
1138
Vector3 pos = curve->get_point_position(p_id);
1139
1140
Transform3D t = Transform3D(basis, pos);
1141
return t;
1142
}
1143
1144
void Path3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
1145
Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1146
ERR_FAIL_NULL(path);
1147
Ref<Curve3D> curve = path->get_curve();
1148
ERR_FAIL_COND(curve.is_null());
1149
ERR_FAIL_INDEX(p_id, curve->get_point_count());
1150
1151
if (!transformation_locked_basis.has(p_id)) {
1152
transformation_locked_basis[p_id] = Basis(curve->get_point_baked_posture(p_id, true));
1153
}
1154
curve->set_point_position(p_id, p_transform.origin);
1155
}
1156
1157
void Path3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
1158
Path3D *path = Object::cast_to<Path3D>(p_gizmo->get_node_3d());
1159
ERR_FAIL_NULL(path);
1160
Ref<Curve3D> curve = path->get_curve();
1161
ERR_FAIL_COND(curve.is_null());
1162
1163
transformation_locked_basis.clear();
1164
1165
if (p_cancel) {
1166
for (int i = 0; i < p_ids.size(); ++i) {
1167
curve->set_point_position(p_ids[i], p_restore[i].origin);
1168
}
1169
return;
1170
}
1171
1172
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1173
1174
undo_redo->create_action(TTR("Set Curve Point Position"));
1175
1176
for (int i = 0; i < p_ids.size(); ++i) {
1177
const int idx = p_ids[i];
1178
undo_redo->add_do_method(curve.ptr(), "set_point_position", idx, curve->get_point_position(idx));
1179
undo_redo->add_undo_method(curve.ptr(), "set_point_position", idx, p_restore[i].origin);
1180
}
1181
undo_redo->commit_action();
1182
}
1183
1184
int Path3DGizmoPlugin::get_priority() const {
1185
return -1;
1186
}
1187
1188
Path3DGizmoPlugin::Path3DGizmoPlugin(float p_disk_size) {
1189
Color path_color = SceneTree::get_singleton()->get_debug_paths_color();
1190
Color path_tilt_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/path_tilt");
1191
disk_size = p_disk_size;
1192
1193
create_material("path_material", path_color);
1194
create_material("path_thin_material", Color(0.6, 0.6, 0.6));
1195
create_material("path_tilt_material", path_tilt_color);
1196
create_material("path_tilt_muted_material", path_tilt_color * 0.7);
1197
create_handle_material("handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
1198
create_handle_material("first_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
1199
create_handle_material("last_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
1200
create_handle_material("closed_pt_handle", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorPathSmoothHandle"), EditorStringName(EditorIcons)));
1201
create_handle_material("sec_handles", false, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("EditorCurveHandle"), EditorStringName(EditorIcons)));
1202
}
1203
1204