node_3d_editor_plugin.h 33 KB

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