Debian Codegraph @

editor/scene/3d/node_3d_editor_plugin.h

raw symbols SCIP
1 /**************************************************************************/
2 /* node_3d_editor_plugin.h */
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 #pragma once
32
33 #include "core/math/dynamic_bvh.h"
34 #include "editor/plugins/editor_plugin.h"
35 #include "editor/scene/3d/node_3d_editor_gizmos.h"
36 #include "editor/themes/editor_scale.h"
37 #include "scene/gui/box_container.h"
38 #include "scene/gui/button.h"
39 #include "scene/gui/spin_box.h"
40 #include "scene/resources/gradient.h"
41 #include "scene/resources/immediate_mesh.h"
42
43 class AcceptDialog;
44 class CheckBox;
45 class ColorPickerButton;
46 class ConfirmationDialog;
47 class DirectionalLight3D;
48 class EditorData;
49 class EditorSelection;
50 class EditorSpinSlider;
51 class HSplitContainer;
52 class LineEdit;
53 class MenuButton;
54 class Node3DEditor;
55 class Node3DEditorViewport;
56 class OptionButton;
57 class PanelContainer;
58 class ProceduralSkyMaterial;
59 class SubViewport;
60 class SubViewportContainer;
61 class VSeparator;
62 class VSplitContainer;
63 class ViewportNavigationControl;
64 class WorldEnvironment;
65 class MeshInstance3D;
66
67 class ViewportRotationControl : public Control {
68 GDCLASS(ViewportRotationControl, Control);
69
70 struct Axis2D {
71 Vector2 screen_point;
72 float z_axis = -99.0;
73 int axis = -1;
74 bool is_positive = true;
75 };
76
77 struct Axis2DCompare {
78 _FORCE_INLINE_ bool operator()(const Axis2D &l, const Axis2D &r) const {
79 return l.z_axis < r.z_axis;
80 }
81 };
82
83 Node3DEditorViewport *viewport = nullptr;
84 Vector<Color> axis_colors;
85 Vector<int> axis_menu_options;
86 Vector2i orbiting_mouse_start;
87 Point2 original_mouse_pos;
88 int orbiting_index = -1;
89 int focused_axis = -2;
90 bool gizmo_activated = false;
91
92 const float AXIS_CIRCLE_RADIUS = 8.0f * EDSCALE;
93
94 protected:
95 void _notification(int p_what);
96 virtual void gui_input(const Ref<InputEvent> &p_event) override;
97 void _draw();
98 void _draw_axis(const Axis2D &p_axis);
99 void _get_sorted_axis(Vector<Axis2D> &r_axis);
100 void _update_focus();
101 void _process_click(int p_index, Vector2 p_position, bool p_pressed);
102 void _process_drag(Ref<InputEventWithModifiers> p_event, int p_index, Vector2 p_position, Vector2 p_relative_position);
103
104 public:
105 void set_viewport(Node3DEditorViewport *p_viewport);
106 };
107
108 class Node3DEditorViewport : public Control {
109 GDCLASS(Node3DEditorViewport, Control);
110 friend class Node3DEditor;
111 friend class ViewportNavigationControl;
112 friend class ViewportRotationControl;
113 enum {
114 VIEW_TOP,
115 VIEW_BOTTOM,
116 VIEW_LEFT,
117 VIEW_RIGHT,
118 VIEW_FRONT,
119 VIEW_REAR,
120 VIEW_CENTER_TO_ORIGIN,
121 VIEW_CENTER_TO_SELECTION,
122 VIEW_ALIGN_TRANSFORM_WITH_VIEW,
123 VIEW_ALIGN_ROTATION_WITH_VIEW,
124 VIEW_PERSPECTIVE,
125 VIEW_ENVIRONMENT,
126 VIEW_ORTHOGONAL,
127 VIEW_SWITCH_PERSPECTIVE_ORTHOGONAL,
128 VIEW_HALF_RESOLUTION,
129 VIEW_AUDIO_LISTENER,
130 VIEW_AUDIO_DOPPLER,
131 VIEW_GIZMOS,
132 VIEW_TRANSFORM_GIZMO,
133 VIEW_GRID,
134 VIEW_INFORMATION,
135 VIEW_FRAME_TIME,
136
137 // < Keep in sync with menu.
138 VIEW_DISPLAY_NORMAL,
139 VIEW_DISPLAY_WIREFRAME,
140 VIEW_DISPLAY_OVERDRAW,
141 VIEW_DISPLAY_LIGHTING,
142 VIEW_DISPLAY_UNSHADED,
143 VIEW_DISPLAY_ADVANCED,
144 // Advanced menu:
145 VIEW_DISPLAY_DEBUG_PSSM_SPLITS,
146 VIEW_DISPLAY_NORMAL_BUFFER,
147 VIEW_DISPLAY_DEBUG_SHADOW_ATLAS,
148 VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS,
149 VIEW_DISPLAY_DEBUG_DECAL_ATLAS,
150 VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO,
151 VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING,
152 VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION,
153 VIEW_DISPLAY_DEBUG_SDFGI,
154 VIEW_DISPLAY_DEBUG_SDFGI_PROBES,
155 VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE,
156 VIEW_DISPLAY_DEBUG_SSAO,
157 VIEW_DISPLAY_DEBUG_SSIL,
158 VIEW_DISPLAY_DEBUG_GI_BUFFER,
159 VIEW_DISPLAY_DEBUG_DISABLE_LOD,
160 VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS,
161 VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS,
162 VIEW_DISPLAY_DEBUG_CLUSTER_DECALS,
163 VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES,
164 VIEW_DISPLAY_DEBUG_OCCLUDERS,
165 VIEW_DISPLAY_MOTION_VECTORS,
166 VIEW_DISPLAY_INTERNAL_BUFFER,
167 VIEW_DISPLAY_MAX,
168 // > Keep in sync with menu.
169
170 VIEW_LOCK_ROTATION,
171 VIEW_CINEMATIC_PREVIEW,
172 VIEW_AUTO_ORTHOGONAL,
173 VIEW_MAX
174 };
175
176 enum ViewType {
177 VIEW_TYPE_USER,
178 VIEW_TYPE_TOP,
179 VIEW_TYPE_BOTTOM,
180 VIEW_TYPE_LEFT,
181 VIEW_TYPE_RIGHT,
182 VIEW_TYPE_FRONT,
183 VIEW_TYPE_REAR,
184 };
185
186 public:
187 static constexpr int32_t GIZMO_BASE_LAYER = 27;
188 static constexpr int32_t GIZMO_EDIT_LAYER = 26;
189 static constexpr int32_t GIZMO_GRID_LAYER = 25;
190 static constexpr int32_t MISC_TOOL_LAYER = 24;
191
192 static constexpr int32_t FRAME_TIME_HISTORY = 20;
193
194 enum NavigationScheme {
195 NAVIGATION_GODOT = 0,
196 NAVIGATION_MAYA = 1,
197 NAVIGATION_MODO = 2,
198 NAVIGATION_CUSTOM = 3,
199 NAVIGATION_TABLET = 4,
200 };
201
202 enum FreelookNavigationScheme {
203 FREELOOK_DEFAULT,
204 FREELOOK_PARTIALLY_AXIS_LOCKED,
205 FREELOOK_FULLY_AXIS_LOCKED,
206 };
207
208 enum ViewportNavMouseButton {
209 NAVIGATION_LEFT_MOUSE,
210 NAVIGATION_MIDDLE_MOUSE,
211 NAVIGATION_RIGHT_MOUSE,
212 NAVIGATION_MOUSE_4,
213 NAVIGATION_MOUSE_5,
214 };
215
216 private:
217 double cpu_time_history[FRAME_TIME_HISTORY];
218 int cpu_time_history_index;
219 double gpu_time_history[FRAME_TIME_HISTORY];
220 int gpu_time_history_index;
221
222 Node *ruler = nullptr;
223 Node3D *ruler_start_point = nullptr;
224 Node3D *ruler_end_point = nullptr;
225 Ref<ImmediateMesh> geometry;
226 MeshInstance3D *ruler_line = nullptr;
227 MeshInstance3D *ruler_line_xray = nullptr;
228 Label *ruler_label = nullptr;
229 Ref<StandardMaterial3D> ruler_material;
230 Ref<StandardMaterial3D> ruler_material_xray;
231
232 int index;
233 ViewType view_type;
234 void _menu_option(int p_option);
235 void _set_auto_orthogonal();
236 Node3D *preview_node = nullptr;
237 bool update_preview_node = false;
238 Point2 preview_node_viewport_pos;
239 Vector3 preview_node_pos;
240 AABB *preview_bounds = nullptr;
241 Vector<String> selected_files;
242 AcceptDialog *accept = nullptr;
243
244 Node *target_node = nullptr;
245 Point2 drop_pos;
246
247 EditorSelection *editor_selection = nullptr;
248
249 Button *translation_preview_button = nullptr;
250 CheckBox *preview_camera = nullptr;
251 SubViewportContainer *subviewport_container = nullptr;
252
253 MenuButton *view_display_menu = nullptr;
254 PopupMenu *display_submenu = nullptr;
255
256 Control *surface = nullptr;
257 SubViewport *viewport = nullptr;
258 Camera3D *camera = nullptr;
259 bool transforming = false;
260 bool orthogonal;
261 bool auto_orthogonal;
262 bool lock_rotation;
263 bool transform_gizmo_visible = true;
264 bool collision_reposition = false;
265 real_t gizmo_scale;
266
267 bool freelook_active;
268 real_t freelook_speed;
269 Vector2 previous_mouse_position;
270
271 PanelContainer *info_panel = nullptr;
272 Label *info_label = nullptr;
273 Label *cinema_label = nullptr;
274 Label *locked_label = nullptr;
275 Label *zoom_limit_label = nullptr;
276
277 Label *preview_material_label = nullptr;
278 Label *preview_material_label_desc = nullptr;
279
280 VBoxContainer *top_right_vbox = nullptr;
281 VBoxContainer *bottom_center_vbox = nullptr;
282 ViewportNavigationControl *position_control = nullptr;
283 ViewportNavigationControl *look_control = nullptr;
284 ViewportRotationControl *rotation_control = nullptr;
285 Gradient *frame_time_gradient = nullptr;
286 PanelContainer *frame_time_panel = nullptr;
287 VBoxContainer *frame_time_vbox = nullptr;
288 Label *cpu_time_label = nullptr;
289 Label *gpu_time_label = nullptr;
290 Label *fps_label = nullptr;
291
292 struct _RayResult {
293 Node3D *item = nullptr;
294 real_t depth = 0;
295 _FORCE_INLINE_ bool operator<(const _RayResult &p_rr) const { return depth < p_rr.depth; }
296 };
297
298 void _update_name();
299 void _compute_edit(const Point2 &p_point);
300 void _clear_selected();
301 bool _is_rotation_arc_visible() const;
302 void _select_clicked(bool p_allow_locked);
303 ObjectID _select_ray(const Point2 &p_pos) const;
304 void _find_items_at_pos(const Point2 &p_pos, Vector<_RayResult> &r_results, bool p_include_locked);
305
306 Transform3D _get_camera_transform() const;
307 int get_selected_count() const;
308 void cancel_transform();
309 void _update_shrink();
310
311 Vector3 _get_camera_position() const;
312 Vector3 _get_camera_normal() const;
313 Vector3 _get_screen_to_space(const Vector3 &p_vector3);
314
315 void _select_region();
316 bool _transform_gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only = false);
317 void _transform_gizmo_apply(Node3D *p_node, const Transform3D &p_transform, bool p_local);
318
319 void _nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
320 void _nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
321 void _nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
322 void _nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative);
323
324 bool _is_shortcut_empty(const String &p_name);
325 bool _is_nav_modifier_pressed(const String &p_name);
326 int _get_shortcut_input_count(const String &p_name);
327
328 float get_znear() const;
329 float get_zfar() const;
330 float get_fov() const;
331
332 ObjectID clicked;
333 ObjectID material_target;
334 Vector<Node3D *> selection_results;
335 Vector<Node3D *> selection_results_menu;
336 bool clicked_wants_append = false;
337 bool selection_in_progress = false;
338 bool movement_threshold_passed = false;
339
340 PopupMenu *selection_menu = nullptr;
341
342 enum NavigationZoomStyle {
343 NAVIGATION_ZOOM_VERTICAL,
344 NAVIGATION_ZOOM_HORIZONTAL
345 };
346
347 enum NavigationMode {
348 NAVIGATION_NONE,
349 NAVIGATION_PAN,
350 NAVIGATION_ZOOM,
351 NAVIGATION_ORBIT,
352 NAVIGATION_LOOK,
353 NAVIGATION_MOVE
354 };
355 enum TransformMode {
356 TRANSFORM_NONE,
357 TRANSFORM_ROTATE,
358 TRANSFORM_TRANSLATE,
359 TRANSFORM_SCALE
360 };
361 enum TransformPlane {
362 TRANSFORM_VIEW,
363 TRANSFORM_X_AXIS,
364 TRANSFORM_Y_AXIS,
365 TRANSFORM_Z_AXIS,
366 TRANSFORM_YZ,
367 TRANSFORM_XZ,
368 TRANSFORM_XY,
369 };
370 enum TransformType {
371 POSITION,
372 ROTATION,
373 SCALE,
374 };
375
376 struct EditData {
377 TransformMode mode;
378 TransformPlane plane;
379 Transform3D original;
380 Vector3 click_ray;
381 Vector3 click_ray_pos;
382 Vector3 center;
383 Point2 mouse_pos;
384 Point2 original_mouse_pos;
385 bool snap = false;
386 bool show_rotation_line = false;
387 Ref<EditorNode3DGizmo> gizmo;
388 int gizmo_handle = 0;
389 bool gizmo_handle_secondary = false;
390 Variant gizmo_initial_value;
391 bool original_local;
392 bool instant;
393
394 // Numeric blender-style transforms (e.g. 'g5x').
395 // numeric_input tracks the current input value, e.g. 1.23.
396 // numeric_negate indicates whether '-' has been pressed to negate the value
397 // while numeric_next_decimal is 0, numbers are input before the decimal point
398 // after pressing '.', numeric next decimal changes to -1, and decrements after each press.
399 double numeric_input = 0.0;
400 bool numeric_negate = false;
401 int numeric_next_decimal = 0;
402
403 Vector3 rotation_axis;
404 Vector3 view_axis_local;
405 double accumulated_rotation_angle = 0.0;
406 double display_rotation_angle = 0.0;
407 Vector3 initial_click_vector;
408 Vector3 previous_rotation_vector;
409 bool gizmo_initiated = false;
410 } _edit;
411
412 struct Cursor {
413 Vector3 pos;
414 real_t x_rot, y_rot, distance, fov_scale;
415 real_t unsnapped_x_rot, unsnapped_y_rot;
416 Vector3 eye_pos; // Used in freelook mode
417 bool region_select;
418 Point2 region_begin, region_end;
419
420 Cursor() {
421 // These rotations place the camera in +X +Y +Z, aka south east, facing north west.
422 x_rot = 0.5;
423 y_rot = -0.5;
424 unsnapped_x_rot = x_rot;
425 unsnapped_y_rot = y_rot;
426 distance = 4;
427 fov_scale = 1.0;
428 region_select = false;
429 }
430 };
431 // Viewport camera supports movement smoothing,
432 // so one cursor is the real cursor, while the other can be an interpolated version.
433 Cursor cursor; // Immediate cursor
434 Cursor camera_cursor; // That one may be interpolated (don't modify this one except for smoothing purposes)
435 Cursor previous_cursor; // Storing previous cursor state for canceling purposes
436
437 void scale_fov(real_t p_fov_offset);
438 void reset_fov();
439 void scale_cursor_distance(real_t scale);
440
441 struct ShortcutCheckSet {
442 bool mod_pressed = false;
443 bool shortcut_not_empty = true;
444 int input_count = 0;
445 ViewportNavMouseButton mouse_preference = NAVIGATION_LEFT_MOUSE;
446 NavigationMode result_nav_mode = NAVIGATION_NONE;
447
448 ShortcutCheckSet() {}
449
450 ShortcutCheckSet(bool p_mod_pressed, bool p_shortcut_not_empty, int p_input_count, const ViewportNavMouseButton &p_mouse_preference, const NavigationMode &p_result_nav_mode) :
451 mod_pressed(p_mod_pressed), shortcut_not_empty(p_shortcut_not_empty), input_count(p_input_count), mouse_preference(p_mouse_preference), result_nav_mode(p_result_nav_mode) {
452 }
453 };
454
455 struct ShortcutCheckSetComparator {
456 _FORCE_INLINE_ bool operator()(const ShortcutCheckSet &A, const ShortcutCheckSet &B) const {
457 return A.input_count > B.input_count;
458 }
459 };
460
461 NavigationMode _get_nav_mode_from_shortcut_check(ViewportNavMouseButton p_mouse_button, Vector<ShortcutCheckSet> p_shortcut_check_sets, bool p_use_not_empty);
462
463 void set_freelook_active(bool active_now);
464 void scale_freelook_speed(real_t scale);
465
466 real_t zoom_indicator_delay;
467 int zoom_failed_attempts_count = 0;
468
469 RID move_gizmo_instance[3], move_plane_gizmo_instance[3], rotate_gizmo_instance[4], scale_gizmo_instance[3], scale_plane_gizmo_instance[3], axis_gizmo_instance[3];
470
471 String last_message;
472 String message;
473 double message_time;
474
475 void set_message(const String &p_message, float p_time = 5);
476
477 void _view_settings_confirmed(real_t p_interp_delta);
478 void _update_camera(real_t p_interp_delta);
479 void _update_navigation_controls_visibility();
480 Transform3D to_camera_transform(const Cursor &p_cursor) const;
481 void _draw();
482
483 // These allow tool scripts to set the 3D cursor location by updating the camera transform.
484 Transform3D last_camera_transform;
485 bool _camera_moved_externally();
486 void _apply_camera_transform_to_cursor();
487
488 void _surface_mouse_enter();
489 void _surface_mouse_exit();
490 void _surface_focus_enter();
491 void _surface_focus_exit();
492
493 void input(const Ref<InputEvent> &p_event) override;
494 void _sinput(const Ref<InputEvent> &p_event);
495 void _update_freelook(real_t delta);
496 Node3DEditor *spatial_editor = nullptr;
497
498 Camera3D *previewing = nullptr;
499 Camera3D *preview = nullptr;
500
501 bool previewing_camera = false;
502 bool previewing_cinema = false;
503 bool _is_node_locked(const Node *p_node) const;
504 void _preview_exited_scene();
505 void _preview_camera_property_changed();
506 void _update_centered_labels();
507 void _toggle_camera_preview(bool);
508 void _toggle_cinema_preview(bool);
509 void _init_gizmo_instance(int p_idx);
510 void _finish_gizmo_instances();
511 void _selection_result_pressed(int);
512 void _selection_menu_hide();
513 void _list_select(Ref<InputEventMouseButton> b);
514 Point2 _get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const;
515
516 Vector3 _get_instance_position(const Point2 &p_pos, Node3D *p_node) const;
517 static AABB _calculate_spatial_bounds(const Node3D *p_parent, bool p_omit_top_level = false, const Transform3D *p_bounds_orientation = nullptr);
518
519 Node *_sanitize_preview_node(Node *p_node) const;
520
521 void _create_preview_node(const Vector<String> &files) const;
522 void _remove_preview_node();
523 bool _apply_preview_material(ObjectID p_target, const Point2 &p_point) const;
524 void _reset_preview_material() const;
525 void _remove_preview_material();
526 bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) const;
527 bool _create_instance(Node *p_parent, const String &p_path, const Point2 &p_point);
528 bool _create_audio_node(Node *p_parent, const String &p_path, const Point2 &p_point);
529 void _perform_drop_data();
530
531 bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
532 void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
533
534 void _project_settings_changed();
535
536 Transform3D _compute_transform(TransformMode p_mode, const Transform3D &p_original, const Transform3D &p_original_local, Vector3 p_motion, double p_extra, bool p_local, bool p_orthogonal, bool p_view_axis = false);
537
538 void _reset_transform(TransformType p_type);
539
540 void begin_transform(TransformMode p_mode, bool instant);
541 void commit_transform();
542 void apply_transform(Vector3 p_motion, double p_snap);
543 void update_transform(bool p_shift);
544 void update_transform_numeric();
545 void finish_transform();
546
547 void _load_viewport_inputs();
548 void register_shortcut_action(const String &p_path, const String &p_name, Key p_keycode, bool p_physical = false);
549 void shortcut_changed_callback(const Ref<Shortcut> p_shortcut, const String &p_shortcut_path);
550
551 // Supported rendering methods for advanced debug draw mode items.
552 enum SupportedRenderingMethods {
553 ALL,
554 FORWARD_PLUS,
555 FORWARD_PLUS_MOBILE,
556 };
557
558 void _set_lock_view_rotation(bool p_lock_rotation);
559 void _add_advanced_debug_draw_mode_item(PopupMenu *p_popup, const String &p_name, int p_value, SupportedRenderingMethods p_rendering_methods = SupportedRenderingMethods::ALL, const String &p_tooltip = "");
560
561 protected:
562 void _notification(int p_what);
563 static void _bind_methods();
564
565 public:
566 void update_surface() { surface->queue_redraw(); }
567 void update_transform_gizmo_view();
568
569 void set_can_preview(Camera3D *p_preview);
570 void set_state(const Dictionary &p_state);
571 Dictionary get_state() const;
572 void reset();
573 bool is_freelook_active() const { return freelook_active; }
574
575 Vector3 get_ray_pos(const Vector2 &p_pos) const;
576 Vector3 get_ray(const Vector2 &p_pos) const;
577 Point2 point_to_screen(const Vector3 &p_point);
578
579 void focus_selection();
580
581 void assign_pending_data_pointers(
582 Node3D *p_preview_node,
583 AABB *p_preview_bounds,
584 AcceptDialog *p_accept);
585
586 SubViewport *get_viewport_node() { return viewport; }
587 Camera3D *get_camera_3d() { return camera; } // return the default camera object.
588 Control *get_surface() { return surface; }
589
590 Node3DEditorViewport(Node3DEditor *p_spatial_editor, int p_index);
591 ~Node3DEditorViewport();
592 };
593
594 class Node3DEditorSelectedItem : public Object {
595 GDCLASS(Node3DEditorSelectedItem, Object);
596
597 public:
598 AABB aabb;
599 Transform3D original; // original location when moving
600 Transform3D original_local;
601 Transform3D last_xform; // last transform
602 bool last_xform_dirty;
603 Node3D *sp = nullptr;
604 RID sbox_instance;
605 RID sbox_instance_offset;
606 RID sbox_instance_xray;
607 RID sbox_instance_xray_offset;
608 Ref<EditorNode3DGizmo> gizmo;
609 HashMap<int, Transform3D> subgizmos; // Key: Subgizmo ID, Value: Initial subgizmo transform.
610
611 Node3DEditorSelectedItem() {
612 sp = nullptr;
613 last_xform_dirty = true;
614 }
615 ~Node3DEditorSelectedItem();
616 };
617
618 class Node3DEditorViewportContainer : public Container {
619 GDCLASS(Node3DEditorViewportContainer, Container);
620
621 public:
622 enum View {
623 VIEW_USE_1_VIEWPORT,
624 VIEW_USE_2_VIEWPORTS,
625 VIEW_USE_2_VIEWPORTS_ALT,
626 VIEW_USE_3_VIEWPORTS,
627 VIEW_USE_3_VIEWPORTS_ALT,
628 VIEW_USE_4_VIEWPORTS,
629 };
630
631 private:
632 View view;
633 bool mouseover;
634 real_t ratio_h;
635 real_t ratio_v;
636
637 bool hovering_v;
638 bool hovering_h;
639
640 bool dragging_v;
641 bool dragging_h;
642 Vector2 drag_begin_pos;
643 Vector2 drag_begin_ratio;
644
645 virtual void gui_input(const Ref<InputEvent> &p_event) override;
646
647 protected:
648 void _notification(int p_what);
649
650 public:
651 void set_view(View p_view);
652 View get_view();
653
654 Node3DEditorViewportContainer();
655 };
656
657 class Node3DEditor : public VBoxContainer {
658 GDCLASS(Node3DEditor, VBoxContainer);
659
660 public:
661 static const unsigned int VIEWPORTS_COUNT = 4;
662
663 enum ToolMode {
664 TOOL_MODE_TRANSFORM,
665 TOOL_MODE_MOVE,
666 TOOL_MODE_ROTATE,
667 TOOL_MODE_SCALE,
668 TOOL_MODE_SELECT,
669 TOOL_MODE_LIST_SELECT,
670 TOOL_LOCK_SELECTED,
671 TOOL_UNLOCK_SELECTED,
672 TOOL_GROUP_SELECTED,
673 TOOL_UNGROUP_SELECTED,
674 TOOL_RULER,
675 TOOL_MAX
676 };
677
678 enum ToolOptions {
679 TOOL_OPT_LOCAL_COORDS,
680 TOOL_OPT_USE_SNAP,
681 TOOL_OPT_MAX
682 };
683
684 enum TransformMode {
685 TRANSFORM_MODE_GLOBAL = 1,
686 TRANSFORM_MODE_LOCAL = 2,
687 };
688
689 private:
690 EditorSelection *editor_selection = nullptr;
691
692 Node3DEditorViewportContainer *viewport_base = nullptr;
693 Node3DEditorViewport *viewports[VIEWPORTS_COUNT];
694 int last_used_viewport = 0;
695
696 VSplitContainer *shader_split = nullptr;
697 HSplitContainer *left_panel_split = nullptr;
698 HSplitContainer *right_panel_split = nullptr;
699
700 /////
701
702 ToolMode tool_mode;
703
704 RID origin_mesh;
705 RID origin_multimesh;
706 RID origin_instance;
707 bool origin_enabled = false;
708 RID grid[3];
709 RID grid_instance[3];
710 bool grid_visible[3] = { false, false, false }; //currently visible
711 bool grid_enable[3] = { false, false, false }; //should be always visible if true
712 bool grid_enabled = false;
713 bool grid_init_draw = false;
714 Camera3D::ProjectionType grid_camera_last_update_perspective = Camera3D::PROJECTION_PERSPECTIVE;
715 Vector3 grid_camera_last_update_position;
716
717 Ref<ArrayMesh> move_gizmo[3], move_plane_gizmo[3], rotate_gizmo[4], scale_gizmo[3], scale_plane_gizmo[3], axis_gizmo[3];
718 Ref<StandardMaterial3D> gizmo_color[3];
719 Ref<StandardMaterial3D> plane_gizmo_color[3];
720 Ref<ShaderMaterial> rotate_gizmo_color[4];
721 Ref<StandardMaterial3D> gizmo_color_hl[3];
722 Ref<StandardMaterial3D> plane_gizmo_color_hl[3];
723 Ref<ShaderMaterial> rotate_gizmo_color_hl[4];
724
725 Ref<Node3DGizmo> current_hover_gizmo;
726 int current_hover_gizmo_handle;
727 bool current_hover_gizmo_handle_secondary;
728
729 DynamicBVH gizmo_bvh;
730
731 real_t snap_translate_value;
732 real_t snap_rotate_value;
733 real_t snap_scale_value;
734
735 Ref<ArrayMesh> active_selection_box_xray;
736 Ref<ArrayMesh> active_selection_box;
737 Ref<ArrayMesh> selection_box_xray;
738 Ref<ArrayMesh> selection_box;
739
740 Ref<StandardMaterial3D> selection_box_mat = memnew(StandardMaterial3D);
741 Ref<StandardMaterial3D> selection_box_mat_xray = memnew(StandardMaterial3D);
742 Ref<StandardMaterial3D> active_selection_box_mat = memnew(StandardMaterial3D);
743 Ref<StandardMaterial3D> active_selection_box_mat_xray = memnew(StandardMaterial3D);
744
745 RID indicators;
746 RID indicators_instance;
747 RID cursor_mesh;
748 RID cursor_instance;
749 Ref<ShaderMaterial> origin_mat;
750 Ref<ShaderMaterial> grid_mat[3];
751 Ref<StandardMaterial3D> cursor_material;
752
753 // Scene drag and drop support
754 Node3D *preview_node = nullptr;
755 AABB preview_bounds;
756
757 Ref<Material> preview_material;
758 Ref<Material> preview_reset_material;
759 ObjectID preview_material_target;
760 int preview_material_surface = -1;
761
762 struct Gizmo {
763 bool visible = false;
764 real_t scale = 0;
765 Transform3D transform;
766 } gizmo;
767
768 enum MenuOption {
769 MENU_TOOL_TRANSFORM,
770 MENU_TOOL_MOVE,
771 MENU_TOOL_ROTATE,
772 MENU_TOOL_SCALE,
773 MENU_TOOL_SELECT,
774 MENU_TOOL_LIST_SELECT,
775 MENU_TOOL_LOCAL_COORDS,
776 MENU_TOOL_USE_SNAP,
777 MENU_TRANSFORM_CONFIGURE_SNAP,
778 MENU_TRANSFORM_DIALOG,
779 MENU_VIEW_USE_1_VIEWPORT,
780 MENU_VIEW_USE_2_VIEWPORTS,
781 MENU_VIEW_USE_2_VIEWPORTS_ALT,
782 MENU_VIEW_USE_3_VIEWPORTS,
783 MENU_VIEW_USE_3_VIEWPORTS_ALT,
784 MENU_VIEW_USE_4_VIEWPORTS,
785 MENU_VIEW_ORIGIN,
786 MENU_VIEW_GRID,
787 MENU_VIEW_GIZMOS_3D_ICONS,
788 MENU_VIEW_CAMERA_SETTINGS,
789 MENU_LOCK_SELECTED,
790 MENU_UNLOCK_SELECTED,
791 MENU_GROUP_SELECTED,
792 MENU_UNGROUP_SELECTED,
793 MENU_SNAP_TO_FLOOR,
794 MENU_RULER,
795 };
796
797 Button *tool_button[TOOL_MAX];
798 Button *tool_option_button[TOOL_OPT_MAX];
799
800 MenuButton *transform_menu = nullptr;
801 PopupMenu *gizmos_menu = nullptr;
802 MenuButton *view_layout_menu = nullptr;
803
804 AcceptDialog *accept = nullptr;
805
806 ConfirmationDialog *snap_dialog = nullptr;
807 ConfirmationDialog *xform_dialog = nullptr;
808 ConfirmationDialog *settings_dialog = nullptr;
809
810 bool snap_enabled;
811 bool snap_key_enabled;
812 EditorSpinSlider *snap_translate = nullptr;
813 EditorSpinSlider *snap_rotate = nullptr;
814 EditorSpinSlider *snap_scale = nullptr;
815
816 LineEdit *xform_translate[3];
817 LineEdit *xform_rotate[3];
818 LineEdit *xform_scale[3];
819 OptionButton *xform_type = nullptr;
820
821 VBoxContainer *settings_vbc = nullptr;
822 SpinBox *settings_fov = nullptr;
823 SpinBox *settings_znear = nullptr;
824 SpinBox *settings_zfar = nullptr;
825
826 void _snap_changed();
827 void _snap_update();
828 void _xform_dialog_action();
829 void _menu_item_pressed(int p_option);
830 void _menu_item_toggled(bool pressed, int p_option);
831 void _menu_gizmo_toggled(int p_option);
832 // Used for secondary menu items which are displayed depending on the currently selected node
833 // (such as MeshInstance's "Mesh" menu).
834 PanelContainer *context_toolbar_panel = nullptr;
835 HBoxContainer *context_toolbar_hbox = nullptr;
836 HashMap<Control *, VSeparator *> context_toolbar_separators;
837
838 void _update_context_toolbar();
839
840 void _generate_selection_boxes();
841
842 void _init_indicators();
843 void _update_gizmos_menu();
844 void _update_gizmos_menu_theme();
845 void _init_grid();
846 void _finish_indicators();
847 void _finish_grid();
848
849 void _toggle_maximize_view(Object *p_viewport);
850 void _viewport_clicked(int p_viewport_idx);
851
852 Node *custom_camera = nullptr;
853
854 Object *_get_editor_data(Object *p_what);
855
856 Ref<Environment> viewport_environment;
857
858 Node3D *selected = nullptr;
859
860 Node3DEditorViewport *freelook_viewport = nullptr;
861
862 void _request_gizmo(Object *p_obj);
863 void _request_gizmo_for_id(ObjectID p_id);
864 void _set_subgizmo_selection(Object *p_obj, Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform = Transform3D());
865 void _clear_subgizmo_selection(Object *p_obj = nullptr);
866
867 bool gizmos_dirty = false;
868
869 static Node3DEditor *singleton;
870
871 void _node_added(Node *p_node);
872 void _node_removed(Node *p_node);
873 Vector<Ref<EditorNode3DGizmoPlugin>> gizmo_plugins_by_priority;
874 Vector<Ref<EditorNode3DGizmoPlugin>> gizmo_plugins_by_name;
875
876 void _register_all_gizmos();
877
878 void _selection_changed();
879 void _refresh_menu_icons();
880
881 bool do_snap_selected_nodes_to_floor = false;
882 void _snap_selected_nodes_to_floor();
883
884 // Preview Sun and Environment
885
886 class PreviewSunEnvPopup : public PopupPanel {
887 GDCLASS(PreviewSunEnvPopup, PopupPanel);
888
889 protected:
890 virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
891 };
892
893 uint32_t world_env_count = 0;
894 uint32_t directional_light_count = 0;
895
896 Button *sun_button = nullptr;
897 Label *sun_state = nullptr;
898 Label *sun_title = nullptr;
899 VBoxContainer *sun_vb = nullptr;
900 Popup *sun_environ_popup = nullptr;
901 Control *sun_direction = nullptr;
902 EditorSpinSlider *sun_angle_altitude = nullptr;
903 EditorSpinSlider *sun_angle_azimuth = nullptr;
904 ColorPickerButton *sun_color = nullptr;
905 EditorSpinSlider *sun_energy = nullptr;
906 EditorSpinSlider *sun_shadow_max_distance = nullptr;
907 Button *sun_add_to_scene = nullptr;
908
909 Vector2 sun_rotation;
910
911 Ref<Shader> sun_direction_shader;
912 Ref<ShaderMaterial> sun_direction_material;
913
914 Button *environ_button = nullptr;
915 Label *environ_state = nullptr;
916 Label *environ_title = nullptr;
917 VBoxContainer *environ_vb = nullptr;
918 ColorPickerButton *environ_sky_color = nullptr;
919 ColorPickerButton *environ_ground_color = nullptr;
920 EditorSpinSlider *environ_energy = nullptr;
921 Button *environ_ao_button = nullptr;
922 Button *environ_glow_button = nullptr;
923 Button *environ_tonemap_button = nullptr;
924 Button *environ_gi_button = nullptr;
925 Button *environ_add_to_scene = nullptr;
926
927 Button *sun_environ_settings = nullptr;
928
929 DirectionalLight3D *preview_sun = nullptr;
930 bool preview_sun_dangling = false;
931 WorldEnvironment *preview_environment = nullptr;
932 bool preview_env_dangling = false;
933 Ref<Environment> environment;
934 Ref<CameraAttributesPractical> camera_attributes;
935 Ref<ProceduralSkyMaterial> sky_material;
936
937 bool sun_environ_updating = false;
938
939 void _sun_direction_draw();
940 void _sun_direction_input(const Ref<InputEvent> &p_event);
941 void _sun_direction_set_altitude(float p_altitude);
942 void _sun_direction_set_azimuth(float p_azimuth);
943 void _sun_set_color(const Color &p_color);
944 void _sun_set_energy(float p_energy);
945 void _sun_set_shadow_max_distance(float p_shadow_max_distance);
946
947 void _environ_set_sky_color(const Color &p_color);
948 void _environ_set_ground_color(const Color &p_color);
949 void _environ_set_sky_energy(float p_energy);
950 void _environ_set_ao();
951 void _environ_set_glow();
952 void _environ_set_tonemap();
953 void _environ_set_gi();
954
955 void _load_default_preview_settings();
956 void _update_preview_environment();
957
958 void _preview_settings_changed();
959 void _sun_environ_settings_pressed();
960
961 void _add_sun_to_scene(bool p_already_added_environment = false);
962 void _add_environment_to_scene(bool p_already_added_sun = false);
963
964 void _update_theme();
965
966 protected:
967 void _notification(int p_what);
968 //void _gui_input(InputEvent p_event);
969 virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
970
971 static void _bind_methods();
972
973 public:
974 static Node3DEditor *get_singleton() { return singleton; }
975
976 static Size2i get_camera_viewport_size(Camera3D *p_camera);
977
978 Vector3 snap_point(Vector3 p_target, Vector3 p_start = Vector3(0, 0, 0)) const;
979
980 float get_znear() const { return settings_znear->get_value(); }
981 float get_zfar() const { return settings_zfar->get_value(); }
982 float get_fov() const { return settings_fov->get_value(); }
983
984 Transform3D get_gizmo_transform() const { return gizmo.transform; }
985 bool is_gizmo_visible() const;
986
987 ToolMode get_tool_mode() const { return tool_mode; }
988 bool are_local_coords_enabled() const { return tool_option_button[Node3DEditor::TOOL_OPT_LOCAL_COORDS]->is_pressed(); }
989 void set_local_coords_enabled(bool on) const { tool_option_button[Node3DEditor::TOOL_OPT_LOCAL_COORDS]->set_pressed(on); }
990 bool is_snap_enabled() const { return snap_enabled ^ snap_key_enabled; }
991 real_t get_translate_snap() const;
992 real_t get_rotate_snap() const;
993 real_t get_scale_snap() const;
994
995 Ref<ArrayMesh> get_move_gizmo(int idx) const { return move_gizmo[idx]; }
996 Ref<ArrayMesh> get_axis_gizmo(int idx) const { return axis_gizmo[idx]; }
997 Ref<ArrayMesh> get_move_plane_gizmo(int idx) const { return move_plane_gizmo[idx]; }
998 Ref<ArrayMesh> get_rotate_gizmo(int idx) const { return rotate_gizmo[idx]; }
999 Ref<ArrayMesh> get_scale_gizmo(int idx) const { return scale_gizmo[idx]; }
1000 Ref<ArrayMesh> get_scale_plane_gizmo(int idx) const { return scale_plane_gizmo[idx]; }
1001
1002 void update_grid();
1003 void update_transform_gizmo();
1004 void update_all_gizmos(Node *p_node = nullptr);
1005 void update_gizmo_opacity();
1006 void snap_selected_nodes_to_floor();
1007 void select_gizmo_highlight_axis(int p_axis);
1008 void set_custom_camera(Node *p_camera) { custom_camera = p_camera; }
1009
1010 Dictionary get_state() const;
1011 void set_state(const Dictionary &p_state);
1012
1013 Ref<Environment> get_viewport_environment() { return viewport_environment; }
1014
1015 void add_control_to_menu_panel(Control *p_control);
1016 void remove_control_from_menu_panel(Control *p_control);
1017
1018 void add_control_to_left_panel(Control *p_control);
1019 void remove_control_from_left_panel(Control *p_control);
1020
1021 void add_control_to_right_panel(Control *p_control);
1022 void remove_control_from_right_panel(Control *p_control);
1023
1024 void move_control_to_left_panel(Control *p_control);
1025 void move_control_to_right_panel(Control *p_control);
1026
1027 VSplitContainer *get_shader_split();
1028
1029 Node3D *get_single_selected_node() { return selected; }
1030 bool is_current_selected_gizmo(const EditorNode3DGizmo *p_gizmo);
1031 bool is_subgizmo_selected(int p_id);
1032 Vector<int> get_subgizmo_selection();
1033 void clear_subgizmo_selection(Object *p_obj = nullptr);
1034 void refresh_dirty_gizmos();
1035
1036 Ref<EditorNode3DGizmo> get_current_hover_gizmo() const { return current_hover_gizmo; }
1037 void set_current_hover_gizmo(Ref<EditorNode3DGizmo> p_gizmo) { current_hover_gizmo = p_gizmo; }
1038
1039 void set_current_hover_gizmo_handle(int p_id, bool p_secondary) {
1040 current_hover_gizmo_handle = p_id;
1041 current_hover_gizmo_handle_secondary = p_secondary;
1042 }
1043
1044 int get_current_hover_gizmo_handle(bool &r_secondary) const {
1045 r_secondary = current_hover_gizmo_handle_secondary;
1046 return current_hover_gizmo_handle;
1047 }
1048
1049 void set_can_preview(Camera3D *p_preview);
1050
1051 void set_preview_material(Ref<Material> p_material) { preview_material = p_material; }
1052 Ref<Material> get_preview_material() { return preview_material; }
1053 void set_preview_reset_material(Ref<Material> p_material) { preview_reset_material = p_material; }
1054 Ref<Material> get_preview_reset_material() const { return preview_reset_material; }
1055 void set_preview_material_target(ObjectID p_object_id) { preview_material_target = p_object_id; }
1056 ObjectID get_preview_material_target() const { return preview_material_target; }
1057 void set_preview_material_surface(int p_surface) { preview_material_surface = p_surface; }
1058 int get_preview_material_surface() const { return preview_material_surface; }
1059
1060 Node3DEditorViewport *get_editor_viewport(int p_idx) {
1061 ERR_FAIL_INDEX_V(p_idx, static_cast<int>(VIEWPORTS_COUNT), nullptr);
1062 return viewports[p_idx];
1063 }
1064 Node3DEditorViewport *get_last_used_viewport();
1065
1066 void set_freelook_viewport(Node3DEditorViewport *p_viewport) { freelook_viewport = p_viewport; }
1067 Node3DEditorViewport *get_freelook_viewport() const { return freelook_viewport; }
1068
1069 void add_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin);
1070 void remove_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin);
1071
1072 DynamicBVH::ID insert_gizmo_bvh_node(Node3D *p_node, const AABB &p_aabb);
1073 void update_gizmo_bvh_node(DynamicBVH::ID p_id, const AABB &p_aabb);
1074 void remove_gizmo_bvh_node(DynamicBVH::ID p_id);
1075 Vector<Node3D *> gizmo_bvh_ray_query(const Vector3 &p_ray_start, const Vector3 &p_ray_end);
1076 Vector<Node3D *> gizmo_bvh_frustum_query(const Vector<Plane> &p_frustum);
1077
1078 void edit(Node3D *p_spatial);
1079 void clear();
1080
1081 Node3DEditor();
1082 ~Node3DEditor();
1083 };
1084
1085 class Node3DEditorPlugin : public EditorPlugin {
1086 GDCLASS(Node3DEditorPlugin, EditorPlugin);
1087
1088 Node3DEditor *spatial_editor = nullptr;
1089
1090 public:
1091 Node3DEditor *get_spatial_editor() { return spatial_editor; }
1092 virtual String get_plugin_name() const override { return TTRC("3D"); }
1093 bool has_main_screen() const override { return true; }
1094 virtual void make_visible(bool p_visible) override;
1095 virtual void edit(Object *p_object) override;
1096 virtual bool handles(Object *p_object) const override;
1097
1098 virtual Dictionary get_state() const override;
1099 virtual void set_state(const Dictionary &p_state) override;
1100 virtual void clear() override { spatial_editor->clear(); }
1101
1102 virtual void edited_scene_changed() override;
1103
1104 Node3DEditorPlugin();
1105 };
1106
1107 class ViewportNavigationControl : public Control {
1108 GDCLASS(ViewportNavigationControl, Control);
1109
1110 Node3DEditorViewport *viewport = nullptr;
1111 Vector2i focused_mouse_start;
1112 Vector2 focused_pos;
1113 bool hovered = false;
1114 int focused_index = -1;
1115 Node3DEditorViewport::NavigationMode nav_mode = Node3DEditorViewport::NavigationMode::NAVIGATION_NONE;
1116
1117 const float AXIS_CIRCLE_RADIUS = 30.0f * EDSCALE;
1118
1119 protected:
1120 void _notification(int p_what);
1121 virtual void gui_input(const Ref<InputEvent> &p_event) override;
1122 void _draw();
1123 void _process_click(int p_index, Vector2 p_position, bool p_pressed);
1124 void _process_drag(int p_index, Vector2 p_position, Vector2 p_relative_position);
1125 void _update_navigation();
1126
1127 public:
1128 void set_navigation_mode(Node3DEditorViewport::NavigationMode p_nav_mode);
1129 void set_viewport(Node3DEditorViewport *p_viewport);
1130 };