-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
/
node_3d_editor_plugin.cpp
8964 lines (7549 loc) · 331 KB
/
node_3d_editor_plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* node_3d_editor_plugin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "node_3d_editor_plugin.h"
#include "core/config/project_settings.h"
#include "core/input/input.h"
#include "core/input/input_map.h"
#include "core/math/math_funcs.h"
#include "core/math/projection.h"
#include "core/os/keyboard.h"
#include "editor/debugger/editor_debugger_node.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/editor_undo_redo_manager.h"
#include "editor/gui/editor_run_bar.h"
#include "editor/gui/editor_spin_slider.h"
#include "editor/plugins/animation_player_editor_plugin.h"
#include "editor/plugins/gizmos/audio_listener_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/audio_stream_player_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/camera_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/collision_object_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/collision_polygon_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/collision_shape_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/cpu_particles_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/decal_gizmo_plugin.h"
#include "editor/plugins/gizmos/fog_volume_gizmo_plugin.h"
#include "editor/plugins/gizmos/gpu_particles_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/gpu_particles_collision_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/joint_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/label_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/light_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/lightmap_gi_gizmo_plugin.h"
#include "editor/plugins/gizmos/lightmap_probe_gizmo_plugin.h"
#include "editor/plugins/gizmos/marker_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/mesh_instance_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/navigation_link_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/navigation_region_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/occluder_instance_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/physics_bone_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/ray_cast_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/reflection_probe_gizmo_plugin.h"
#include "editor/plugins/gizmos/shape_cast_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/soft_body_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/spring_arm_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/sprite_base_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/vehicle_body_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/visible_on_screen_notifier_3d_gizmo_plugin.h"
#include "editor/plugins/gizmos/voxel_gi_gizmo_plugin.h"
#include "editor/plugins/node_3d_editor_gizmos.h"
#include "editor/scene_tree_dock.h"
#include "scene/3d/camera_3d.h"
#include "scene/3d/collision_shape_3d.h"
#include "scene/3d/decal.h"
#include "scene/3d/light_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/physics_body_3d.h"
#include "scene/3d/visual_instance_3d.h"
#include "scene/3d/world_environment.h"
#include "scene/gui/center_container.h"
#include "scene/gui/color_picker.h"
#include "scene/gui/flow_container.h"
#include "scene/gui/split_container.h"
#include "scene/gui/subviewport_container.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/sky_material.h"
#include "scene/resources/surface_tool.h"
constexpr real_t DISTANCE_DEFAULT = 4;
constexpr real_t GIZMO_ARROW_SIZE = 0.35;
constexpr real_t GIZMO_RING_HALF_WIDTH = 0.1;
constexpr real_t GIZMO_PLANE_SIZE = 0.2;
constexpr real_t GIZMO_PLANE_DST = 0.3;
constexpr real_t GIZMO_CIRCLE_SIZE = 1.1;
constexpr real_t GIZMO_SCALE_OFFSET = GIZMO_CIRCLE_SIZE + 0.3;
constexpr real_t GIZMO_ARROW_OFFSET = GIZMO_CIRCLE_SIZE + 0.3;
constexpr real_t ZOOM_FREELOOK_MIN = 0.01;
constexpr real_t ZOOM_FREELOOK_MULTIPLIER = 1.08;
constexpr real_t ZOOM_FREELOOK_INDICATOR_DELAY_S = 1.5;
#ifdef REAL_T_IS_DOUBLE
constexpr double ZOOM_FREELOOK_MAX = 1'000'000'000'000;
#else
constexpr float ZOOM_FREELOOK_MAX = 10'000;
#endif
constexpr real_t MIN_Z = 0.01;
constexpr real_t MAX_Z = 1000000.0;
constexpr real_t MIN_FOV = 0.01;
constexpr real_t MAX_FOV = 179;
void ViewportNavigationControl::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
if (!is_connected("mouse_exited", callable_mp(this, &ViewportNavigationControl::_on_mouse_exited))) {
connect("mouse_exited", callable_mp(this, &ViewportNavigationControl::_on_mouse_exited));
}
if (!is_connected("mouse_entered", callable_mp(this, &ViewportNavigationControl::_on_mouse_entered))) {
connect("mouse_entered", callable_mp(this, &ViewportNavigationControl::_on_mouse_entered));
}
} break;
case NOTIFICATION_DRAW: {
if (viewport != nullptr) {
_draw();
_update_navigation();
}
} break;
}
}
void ViewportNavigationControl::_draw() {
if (nav_mode == Node3DEditorViewport::NAVIGATION_NONE) {
return;
}
Vector2 center = get_size() / 2.0;
float radius = get_size().x / 2.0;
const bool focused = focused_index != -1;
draw_circle(center, radius, Color(0.5, 0.5, 0.5, focused || hovered ? 0.35 : 0.15));
const Color c = focused ? Color(0.9, 0.9, 0.9, 0.9) : Color(0.5, 0.5, 0.5, 0.25);
Vector2 circle_pos = focused ? center.move_toward(focused_pos, radius) : center;
draw_circle(circle_pos, AXIS_CIRCLE_RADIUS, c);
draw_circle(circle_pos, AXIS_CIRCLE_RADIUS * 0.8, c.darkened(0.4));
}
void ViewportNavigationControl::_process_click(int p_index, Vector2 p_position, bool p_pressed) {
hovered = false;
queue_redraw();
if (focused_index != -1 && focused_index != p_index) {
return;
}
if (p_pressed) {
if (p_position.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
focused_pos = p_position;
focused_index = p_index;
queue_redraw();
}
} else {
focused_index = -1;
if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
Input::get_singleton()->warp_mouse(focused_mouse_start);
}
}
}
void ViewportNavigationControl::_process_drag(int p_index, Vector2 p_position, Vector2 p_relative_position) {
if (focused_index == p_index) {
if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
focused_mouse_start = p_position;
}
focused_pos += p_relative_position;
queue_redraw();
}
}
void ViewportNavigationControl::gui_input(const Ref<InputEvent> &p_event) {
// Mouse events
const Ref<InputEventMouseButton> mouse_button = p_event;
if (mouse_button.is_valid() && mouse_button->get_button_index() == MouseButton::LEFT) {
_process_click(100, mouse_button->get_position(), mouse_button->is_pressed());
}
const Ref<InputEventMouseMotion> mouse_motion = p_event;
if (mouse_motion.is_valid()) {
_process_drag(100, mouse_motion->get_global_position(), viewport->_get_warped_mouse_motion(mouse_motion));
}
// Touch events
const Ref<InputEventScreenTouch> screen_touch = p_event;
if (screen_touch.is_valid()) {
_process_click(screen_touch->get_index(), screen_touch->get_position(), screen_touch->is_pressed());
}
const Ref<InputEventScreenDrag> screen_drag = p_event;
if (screen_drag.is_valid()) {
_process_drag(screen_drag->get_index(), screen_drag->get_position(), screen_drag->get_relative());
}
}
void ViewportNavigationControl::_update_navigation() {
if (focused_index == -1) {
return;
}
Vector2 delta = focused_pos - (get_size() / 2.0);
Vector2 delta_normalized = delta.normalized();
switch (nav_mode) {
case Node3DEditorViewport::NavigationMode::NAVIGATION_MOVE: {
real_t speed_multiplier = MIN(delta.length() / (get_size().x * 100.0), 3.0);
real_t speed = viewport->freelook_speed * speed_multiplier;
const Node3DEditorViewport::FreelookNavigationScheme navigation_scheme = (Node3DEditorViewport::FreelookNavigationScheme)EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_navigation_scheme").operator int();
Vector3 forward;
if (navigation_scheme == Node3DEditorViewport::FreelookNavigationScheme::FREELOOK_FULLY_AXIS_LOCKED) {
// Forward/backward keys will always go straight forward/backward, never moving on the Y axis.
forward = Vector3(0, 0, delta_normalized.y).rotated(Vector3(0, 1, 0), viewport->camera->get_rotation().y);
} else {
// Forward/backward keys will be relative to the camera pitch.
forward = viewport->camera->get_transform().basis.xform(Vector3(0, 0, delta_normalized.y));
}
const Vector3 right = viewport->camera->get_transform().basis.xform(Vector3(delta_normalized.x, 0, 0));
const Vector3 direction = forward + right;
const Vector3 motion = direction * speed;
viewport->cursor.pos += motion;
viewport->cursor.eye_pos += motion;
} break;
case Node3DEditorViewport::NavigationMode::NAVIGATION_LOOK: {
real_t speed_multiplier = MIN(delta.length() / (get_size().x * 2.5), 3.0);
real_t speed = viewport->freelook_speed * speed_multiplier;
viewport->_nav_look(nullptr, delta_normalized * speed);
} break;
case Node3DEditorViewport::NAVIGATION_PAN: {
real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0);
real_t speed = viewport->freelook_speed * speed_multiplier;
viewport->_nav_pan(nullptr, -delta_normalized * speed);
} break;
case Node3DEditorViewport::NAVIGATION_ZOOM: {
real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0);
real_t speed = viewport->freelook_speed * speed_multiplier;
viewport->_nav_zoom(nullptr, delta_normalized * speed);
} break;
case Node3DEditorViewport::NAVIGATION_ORBIT: {
real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0);
real_t speed = viewport->freelook_speed * speed_multiplier;
viewport->_nav_orbit(nullptr, delta_normalized * speed);
} break;
case Node3DEditorViewport::NAVIGATION_NONE: {
} break;
}
}
void ViewportNavigationControl::_on_mouse_entered() {
hovered = true;
queue_redraw();
}
void ViewportNavigationControl::_on_mouse_exited() {
hovered = false;
queue_redraw();
}
void ViewportNavigationControl::set_navigation_mode(Node3DEditorViewport::NavigationMode p_nav_mode) {
nav_mode = p_nav_mode;
}
void ViewportNavigationControl::set_viewport(Node3DEditorViewport *p_viewport) {
viewport = p_viewport;
}
void ViewportRotationControl::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
axis_menu_options.clear();
axis_menu_options.push_back(Node3DEditorViewport::VIEW_RIGHT);
axis_menu_options.push_back(Node3DEditorViewport::VIEW_TOP);
axis_menu_options.push_back(Node3DEditorViewport::VIEW_FRONT);
axis_menu_options.push_back(Node3DEditorViewport::VIEW_LEFT);
axis_menu_options.push_back(Node3DEditorViewport::VIEW_BOTTOM);
axis_menu_options.push_back(Node3DEditorViewport::VIEW_REAR);
axis_colors.clear();
axis_colors.push_back(get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor)));
axis_colors.push_back(get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor)));
axis_colors.push_back(get_theme_color(SNAME("axis_z_color"), EditorStringName(Editor)));
queue_redraw();
if (!is_connected("mouse_exited", callable_mp(this, &ViewportRotationControl::_on_mouse_exited))) {
connect("mouse_exited", callable_mp(this, &ViewportRotationControl::_on_mouse_exited));
}
} break;
case NOTIFICATION_DRAW: {
if (viewport != nullptr) {
_draw();
}
} break;
}
}
void ViewportRotationControl::_draw() {
const Vector2i center = get_size() / 2.0;
const real_t radius = get_size().x / 2.0;
if (focused_axis > -2 || orbiting_index != -1) {
draw_circle(center, radius, Color(0.5, 0.5, 0.5, 0.25));
}
Vector<Axis2D> axis_to_draw;
_get_sorted_axis(axis_to_draw);
for (int i = 0; i < axis_to_draw.size(); ++i) {
_draw_axis(axis_to_draw[i]);
}
}
void ViewportRotationControl::_draw_axis(const Axis2D &p_axis) {
const bool focused = focused_axis == p_axis.axis;
const bool positive = p_axis.axis < 3;
const int direction = p_axis.axis % 3;
const Color axis_color = axis_colors[direction];
const double alpha = focused ? 1.0 : ((p_axis.z_axis + 1.0) / 2.0) * 0.5 + 0.5;
const Color c = focused ? Color(0.9, 0.9, 0.9) : Color(axis_color, alpha);
if (positive) {
// Draw axis lines for the positive axes.
const Vector2i center = get_size() / 2.0;
draw_line(center, p_axis.screen_point, c, 1.5 * EDSCALE);
draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c);
// Draw the axis letter for the positive axes.
const String axis_name = direction == 0 ? "X" : (direction == 1 ? "Y" : "Z");
draw_char(get_theme_font(SNAME("rotation_control"), EditorStringName(EditorFonts)), p_axis.screen_point + Vector2i(Math::round(-4.0 * EDSCALE), Math::round(5.0 * EDSCALE)), axis_name, get_theme_font_size(SNAME("rotation_control_size"), EditorStringName(EditorFonts)), Color(0.0, 0.0, 0.0, alpha));
} else {
// Draw an outline around the negative axes.
draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c);
draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS * 0.8, c.darkened(0.4));
}
}
void ViewportRotationControl::_get_sorted_axis(Vector<Axis2D> &r_axis) {
const Vector2i center = get_size() / 2.0;
const real_t radius = get_size().x / 2.0 - AXIS_CIRCLE_RADIUS - 2.0 * EDSCALE;
const Basis camera_basis = viewport->to_camera_transform(viewport->cursor).get_basis().inverse();
for (int i = 0; i < 3; ++i) {
Vector3 axis_3d = camera_basis.get_column(i);
Vector2i axis_vector = Vector2(axis_3d.x, -axis_3d.y) * radius;
if (Math::abs(axis_3d.z) <= 1.0) {
Axis2D pos_axis;
pos_axis.axis = i;
pos_axis.screen_point = center + axis_vector;
pos_axis.z_axis = axis_3d.z;
r_axis.push_back(pos_axis);
Axis2D neg_axis;
neg_axis.axis = i + 3;
neg_axis.screen_point = center - axis_vector;
neg_axis.z_axis = -axis_3d.z;
r_axis.push_back(neg_axis);
} else {
// Special case when the camera is aligned with one axis
Axis2D axis;
axis.axis = i + (axis_3d.z <= 0 ? 0 : 3);
axis.screen_point = center;
axis.z_axis = 1.0;
r_axis.push_back(axis);
}
}
r_axis.sort_custom<Axis2DCompare>();
}
void ViewportRotationControl::_process_click(int p_index, Vector2 p_position, bool p_pressed) {
if (orbiting_index != -1 && orbiting_index != p_index) {
return;
}
if (p_pressed) {
if (p_position.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
orbiting_index = p_index;
}
} else {
if (focused_axis > -1) {
viewport->_menu_option(axis_menu_options[focused_axis]);
_update_focus();
}
orbiting_index = -1;
if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
Input::get_singleton()->warp_mouse(orbiting_mouse_start);
}
}
}
void ViewportRotationControl::_process_drag(Ref<InputEventWithModifiers> p_event, int p_index, Vector2 p_position, Vector2 p_relative_position) {
if (orbiting_index == p_index) {
if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
orbiting_mouse_start = p_position;
}
viewport->_nav_orbit(p_event, p_relative_position);
focused_axis = -1;
} else {
_update_focus();
}
}
void ViewportRotationControl::gui_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null());
// Mouse events
const Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
_process_click(100, mb->get_position(), mb->is_pressed());
}
const Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
_process_drag(mm, 100, mm->get_global_position(), viewport->_get_warped_mouse_motion(mm));
}
// Touch events
const Ref<InputEventScreenTouch> screen_touch = p_event;
if (screen_touch.is_valid()) {
_process_click(screen_touch->get_index(), screen_touch->get_position(), screen_touch->is_pressed());
}
const Ref<InputEventScreenDrag> screen_drag = p_event;
if (screen_drag.is_valid()) {
_process_drag(screen_drag, screen_drag->get_index(), screen_drag->get_position(), screen_drag->get_relative());
}
}
void ViewportRotationControl::_update_focus() {
int original_focus = focused_axis;
focused_axis = -2;
Vector2 mouse_pos = get_local_mouse_position();
if (mouse_pos.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
focused_axis = -1;
}
Vector<Axis2D> axes;
_get_sorted_axis(axes);
for (int i = 0; i < axes.size(); i++) {
const Axis2D &axis = axes[i];
if (mouse_pos.distance_to(axis.screen_point) < AXIS_CIRCLE_RADIUS) {
focused_axis = axis.axis;
}
}
if (focused_axis != original_focus) {
queue_redraw();
}
}
void ViewportRotationControl::_on_mouse_exited() {
focused_axis = -2;
queue_redraw();
}
void ViewportRotationControl::set_viewport(Node3DEditorViewport *p_viewport) {
viewport = p_viewport;
}
void Node3DEditorViewport::_view_settings_confirmed(real_t p_interp_delta) {
// Set FOV override multiplier back to the default, so that the FOV
// setting specified in the View menu is correctly applied.
cursor.fov_scale = 1.0;
_update_camera(p_interp_delta);
}
void Node3DEditorViewport::_update_navigation_controls_visibility() {
bool show_viewport_rotation_gizmo = EDITOR_GET("editors/3d/navigation/show_viewport_rotation_gizmo") && (!previewing_cinema && !previewing_camera);
rotation_control->set_visible(show_viewport_rotation_gizmo);
bool show_viewport_navigation_gizmo = EDITOR_GET("editors/3d/navigation/show_viewport_navigation_gizmo") && (!previewing_cinema && !previewing_camera);
position_control->set_visible(show_viewport_navigation_gizmo);
look_control->set_visible(show_viewport_navigation_gizmo);
}
void Node3DEditorViewport::_update_camera(real_t p_interp_delta) {
bool is_orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL;
Cursor old_camera_cursor = camera_cursor;
camera_cursor = cursor;
if (p_interp_delta > 0) {
//-------
// Perform smoothing
if (is_freelook_active()) {
// Higher inertia should increase "lag" (lerp with factor between 0 and 1)
// Inertia of zero should produce instant movement (lerp with factor of 1) in this case it returns a really high value and gets clamped to 1.
const real_t inertia = EDITOR_GET("editors/3d/freelook/freelook_inertia");
real_t factor = (1.0 / inertia) * p_interp_delta;
// We interpolate a different point here, because in freelook mode the focus point (cursor.pos) orbits around eye_pos
camera_cursor.eye_pos = old_camera_cursor.eye_pos.lerp(cursor.eye_pos, CLAMP(factor, 0, 1));
const real_t orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) {
camera_cursor.x_rot = cursor.x_rot;
}
if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) {
camera_cursor.y_rot = cursor.y_rot;
}
Vector3 forward = to_camera_transform(camera_cursor).basis.xform(Vector3(0, 0, -1));
camera_cursor.pos = camera_cursor.eye_pos + forward * camera_cursor.distance;
} else {
const real_t orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
const real_t translation_inertia = EDITOR_GET("editors/3d/navigation_feel/translation_inertia");
const real_t zoom_inertia = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia");
camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) {
camera_cursor.x_rot = cursor.x_rot;
}
if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) {
camera_cursor.y_rot = cursor.y_rot;
}
camera_cursor.pos = old_camera_cursor.pos.lerp(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia)));
camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN((real_t)1.0, p_interp_delta * (1 / zoom_inertia)));
}
}
//-------
// Apply camera transform
real_t tolerance = 0.001;
bool equal = true;
if (!Math::is_equal_approx(old_camera_cursor.x_rot, camera_cursor.x_rot, tolerance) || !Math::is_equal_approx(old_camera_cursor.y_rot, camera_cursor.y_rot, tolerance)) {
equal = false;
} else if (!old_camera_cursor.pos.is_equal_approx(camera_cursor.pos)) {
equal = false;
} else if (!Math::is_equal_approx(old_camera_cursor.distance, camera_cursor.distance, tolerance)) {
equal = false;
} else if (!Math::is_equal_approx(old_camera_cursor.fov_scale, camera_cursor.fov_scale, tolerance)) {
equal = false;
}
if (!equal || p_interp_delta == 0 || is_orthogonal != orthogonal) {
camera->set_global_transform(to_camera_transform(camera_cursor));
if (orthogonal) {
float half_fov = Math::deg_to_rad(get_fov()) / 2.0;
float height = 2.0 * cursor.distance * Math::tan(half_fov);
camera->set_orthogonal(height, get_znear(), get_zfar());
} else {
camera->set_perspective(get_fov(), get_znear(), get_zfar());
}
update_transform_gizmo_view();
rotation_control->queue_redraw();
position_control->queue_redraw();
look_control->queue_redraw();
spatial_editor->update_grid();
}
}
Transform3D Node3DEditorViewport::to_camera_transform(const Cursor &p_cursor) const {
Transform3D camera_transform;
camera_transform.translate_local(p_cursor.pos);
camera_transform.basis.rotate(Vector3(1, 0, 0), -p_cursor.x_rot);
camera_transform.basis.rotate(Vector3(0, 1, 0), -p_cursor.y_rot);
if (orthogonal) {
camera_transform.translate_local(0, 0, (get_zfar() - get_znear()) / 2.0);
} else {
camera_transform.translate_local(0, 0, p_cursor.distance);
}
return camera_transform;
}
int Node3DEditorViewport::get_selected_count() const {
const HashMap<Node *, Object *> &selection = editor_selection->get_selection();
int count = 0;
for (const KeyValue<Node *, Object *> &E : selection) {
Node3D *sp = Object::cast_to<Node3D>(E.key);
if (!sp) {
continue;
}
Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
if (!se) {
continue;
}
count++;
}
return count;
}
void Node3DEditorViewport::cancel_transform() {
List<Node *> &selection = editor_selection->get_selected_node_list();
for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
Node3D *sp = Object::cast_to<Node3D>(E->get());
if (!sp) {
continue;
}
Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
if (!se) {
continue;
}
sp->set_global_transform(se->original);
}
finish_transform();
set_message(TTR("Transform Aborted."), 3);
}
void Node3DEditorViewport::_update_shrink() {
bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION));
subviewport_container->set_stretch_shrink(shrink ? 2 : 1);
subviewport_container->set_texture_filter(shrink ? TEXTURE_FILTER_NEAREST : TEXTURE_FILTER_PARENT_NODE);
}
float Node3DEditorViewport::get_znear() const {
return CLAMP(spatial_editor->get_znear(), MIN_Z, MAX_Z);
}
float Node3DEditorViewport::get_zfar() const {
return CLAMP(spatial_editor->get_zfar(), MIN_Z, MAX_Z);
}
float Node3DEditorViewport::get_fov() const {
return CLAMP(spatial_editor->get_fov() * cursor.fov_scale, MIN_FOV, MAX_FOV);
}
Transform3D Node3DEditorViewport::_get_camera_transform() const {
return camera->get_global_transform();
}
Vector3 Node3DEditorViewport::_get_camera_position() const {
return _get_camera_transform().origin;
}
Point2 Node3DEditorViewport::_point_to_screen(const Vector3 &p_point) {
return camera->unproject_position(p_point) * subviewport_container->get_stretch_shrink();
}
Vector3 Node3DEditorViewport::_get_ray_pos(const Vector2 &p_pos) const {
return camera->project_ray_origin(p_pos / subviewport_container->get_stretch_shrink());
}
Vector3 Node3DEditorViewport::_get_camera_normal() const {
return -_get_camera_transform().basis.get_column(2);
}
Vector3 Node3DEditorViewport::_get_ray(const Vector2 &p_pos) const {
return camera->project_ray_normal(p_pos / subviewport_container->get_stretch_shrink());
}
void Node3DEditorViewport::_clear_selected() {
_edit.gizmo = Ref<EditorNode3DGizmo>();
_edit.gizmo_handle = -1;
_edit.gizmo_handle_secondary = false;
_edit.gizmo_initial_value = Variant();
Node3D *selected = spatial_editor->get_single_selected_node();
Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
if (se && se->gizmo.is_valid()) {
se->subgizmos.clear();
se->gizmo->redraw();
se->gizmo.unref();
spatial_editor->update_transform_gizmo();
} else {
editor_selection->clear();
Node3DEditor::get_singleton()->edit(nullptr);
}
}
void Node3DEditorViewport::_select_clicked(bool p_allow_locked) {
Node *node = Object::cast_to<Node3D>(ObjectDB::get_instance(clicked));
Node3D *selected = Object::cast_to<Node3D>(node);
clicked = ObjectID();
if (!selected) {
return;
}
if (!p_allow_locked) {
// Replace the node by the group if grouped
while (node && node != EditorNode::get_singleton()->get_edited_scene()->get_parent()) {
Node3D *selected_tmp = Object::cast_to<Node3D>(node);
if (selected_tmp && node->has_meta("_edit_group_")) {
selected = selected_tmp;
}
node = node->get_parent();
}
}
if (p_allow_locked || !_is_node_locked(selected)) {
if (clicked_wants_append) {
if (editor_selection->is_selected(selected)) {
editor_selection->remove_node(selected);
} else {
editor_selection->add_node(selected);
}
} else {
if (!editor_selection->is_selected(selected)) {
editor_selection->clear();
editor_selection->add_node(selected);
EditorNode::get_singleton()->edit_node(selected);
}
}
if (editor_selection->get_selected_node_list().size() == 1) {
EditorNode::get_singleton()->edit_node(editor_selection->get_selected_node_list()[0]);
}
}
}
ObjectID Node3DEditorViewport::_select_ray(const Point2 &p_pos) const {
Vector3 ray = _get_ray(p_pos);
Vector3 pos = _get_ray_pos(p_pos);
Vector2 shrinked_pos = p_pos / subviewport_container->get_stretch_shrink();
if (viewport->get_debug_draw() == Viewport::DEBUG_DRAW_SDFGI_PROBES) {
RS::get_singleton()->sdfgi_set_debug_probe_select(pos, ray);
}
Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_ray(pos, pos + ray * camera->get_far(), get_tree()->get_root()->get_world_3d()->get_scenario());
HashSet<Ref<EditorNode3DGizmo>> found_gizmos;
Node *edited_scene = get_tree()->get_edited_scene_root();
ObjectID closest;
Node *item = nullptr;
float closest_dist = 1e20;
for (int i = 0; i < instances.size(); i++) {
Node3D *spat = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i]));
if (!spat) {
continue;
}
Vector<Ref<Node3DGizmo>> gizmos = spat->get_gizmos();
for (int j = 0; j < gizmos.size(); j++) {
Ref<EditorNode3DGizmo> seg = gizmos[j];
if ((!seg.is_valid()) || found_gizmos.has(seg)) {
continue;
}
found_gizmos.insert(seg);
Vector3 point;
Vector3 normal;
bool inters = seg->intersect_ray(camera, shrinked_pos, point, normal);
if (!inters) {
continue;
}
const real_t dist = pos.distance_to(point);
if (dist < 0) {
continue;
}
if (dist < closest_dist) {
item = Object::cast_to<Node>(spat);
if (item != edited_scene) {
item = edited_scene->get_deepest_editable_node(item);
}
closest = item->get_instance_id();
closest_dist = dist;
}
}
}
if (!item) {
return ObjectID();
}
return closest;
}
void Node3DEditorViewport::_find_items_at_pos(const Point2 &p_pos, Vector<_RayResult> &r_results, bool p_include_locked_nodes) {
Vector3 ray = _get_ray(p_pos);
Vector3 pos = _get_ray_pos(p_pos);
Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_ray(pos, pos + ray * camera->get_far(), get_tree()->get_root()->get_world_3d()->get_scenario());
HashSet<Node3D *> found_nodes;
for (int i = 0; i < instances.size(); i++) {
Node3D *spat = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i]));
if (!spat) {
continue;
}
if (found_nodes.has(spat)) {
continue;
}
if (!p_include_locked_nodes && _is_node_locked(spat)) {
continue;
}
Vector<Ref<Node3DGizmo>> gizmos = spat->get_gizmos();
for (int j = 0; j < gizmos.size(); j++) {
Ref<EditorNode3DGizmo> seg = gizmos[j];
if (!seg.is_valid()) {
continue;
}
Vector3 point;
Vector3 normal;
bool inters = seg->intersect_ray(camera, p_pos, point, normal);
if (!inters) {
continue;
}
const real_t dist = pos.distance_to(point);
if (dist < 0) {
continue;
}
found_nodes.insert(spat);
_RayResult res;
res.item = spat;
res.depth = dist;
r_results.push_back(res);
break;
}
}
r_results.sort();
}
Vector3 Node3DEditorViewport::_get_screen_to_space(const Vector3 &p_vector3) {
Projection cm;
if (orthogonal) {
cm.set_orthogonal(camera->get_size(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
} else {
cm.set_perspective(get_fov(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
}
Vector2 screen_he = cm.get_viewport_half_extents();
Transform3D camera_transform;
camera_transform.translate_local(cursor.pos);
camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
camera_transform.translate_local(0, 0, cursor.distance);
return camera_transform.xform(Vector3(((p_vector3.x / get_size().width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (p_vector3.y / get_size().height)) * 2.0 - 1.0) * screen_he.y, -(get_znear() + p_vector3.z)));
}
void Node3DEditorViewport::_select_region() {
if (cursor.region_begin == cursor.region_end) {
if (!clicked_wants_append) {
_clear_selected();
}
return; //nothing really
}
const real_t z_offset = MAX(0.0, 5.0 - get_znear());
Vector3 box[4] = {
Vector3(
MIN(cursor.region_begin.x, cursor.region_end.x),
MIN(cursor.region_begin.y, cursor.region_end.y),
z_offset),
Vector3(
MAX(cursor.region_begin.x, cursor.region_end.x),
MIN(cursor.region_begin.y, cursor.region_end.y),
z_offset),
Vector3(
MAX(cursor.region_begin.x, cursor.region_end.x),
MAX(cursor.region_begin.y, cursor.region_end.y),
z_offset),
Vector3(
MIN(cursor.region_begin.x, cursor.region_end.x),
MAX(cursor.region_begin.y, cursor.region_end.y),
z_offset)
};
Vector<Plane> frustum;
Vector3 cam_pos = _get_camera_position();
for (int i = 0; i < 4; i++) {
Vector3 a = _get_screen_to_space(box[i]);
Vector3 b = _get_screen_to_space(box[(i + 1) % 4]);
if (orthogonal) {
frustum.push_back(Plane((a - b).normalized(), a));
} else {
frustum.push_back(Plane(a, b, cam_pos));
}
}
Plane near(-_get_camera_normal(), cam_pos);
near.d -= get_znear();
frustum.push_back(near);
Plane far = -near;
far.d += get_zfar();
frustum.push_back(far);
if (spatial_editor->get_single_selected_node()) {
Node3D *single_selected = spatial_editor->get_single_selected_node();
Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(single_selected);
if (se) {
Ref<EditorNode3DGizmo> old_gizmo;
if (!clicked_wants_append) {
se->subgizmos.clear();
old_gizmo = se->gizmo;
se->gizmo.unref();
}
bool found_subgizmos = false;
Vector<Ref<Node3DGizmo>> gizmos = single_selected->get_gizmos();
for (int j = 0; j < gizmos.size(); j++) {
Ref<EditorNode3DGizmo> seg = gizmos[j];
if (!seg.is_valid()) {
continue;
}
if (se->gizmo.is_valid() && se->gizmo != seg) {
continue;
}
Vector<int> subgizmos = seg->subgizmos_intersect_frustum(camera, frustum);
if (!subgizmos.is_empty()) {
se->gizmo = seg;
for (int i = 0; i < subgizmos.size(); i++) {
int subgizmo_id = subgizmos[i];
if (!se->subgizmos.has(subgizmo_id)) {
se->subgizmos.insert(subgizmo_id, se->gizmo->get_subgizmo_transform(subgizmo_id));
}
}
found_subgizmos = true;
break;
}
}
if (!clicked_wants_append || found_subgizmos) {
if (se->gizmo.is_valid()) {
se->gizmo->redraw();
}
if (old_gizmo != se->gizmo && old_gizmo.is_valid()) {
old_gizmo->redraw();
}