node_3d_editor_plugin.h 32 KB

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