1 | /**************************************************************************/ |
2 | /* node_3d_editor_plugin.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "node_3d_editor_plugin.h" |
32 | |
33 | #include "core/config/project_settings.h" |
34 | #include "core/input/input.h" |
35 | #include "core/input/input_map.h" |
36 | #include "core/math/math_funcs.h" |
37 | #include "core/math/projection.h" |
38 | #include "core/os/keyboard.h" |
39 | #include "editor/debugger/editor_debugger_node.h" |
40 | #include "editor/editor_node.h" |
41 | #include "editor/editor_settings.h" |
42 | #include "editor/editor_string_names.h" |
43 | #include "editor/editor_undo_redo_manager.h" |
44 | #include "editor/gui/editor_run_bar.h" |
45 | #include "editor/gui/editor_spin_slider.h" |
46 | #include "editor/plugins/animation_player_editor_plugin.h" |
47 | #include "editor/plugins/gizmos/audio_listener_3d_gizmo_plugin.h" |
48 | #include "editor/plugins/gizmos/audio_stream_player_3d_gizmo_plugin.h" |
49 | #include "editor/plugins/gizmos/camera_3d_gizmo_plugin.h" |
50 | #include "editor/plugins/gizmos/collision_object_3d_gizmo_plugin.h" |
51 | #include "editor/plugins/gizmos/collision_polygon_3d_gizmo_plugin.h" |
52 | #include "editor/plugins/gizmos/collision_shape_3d_gizmo_plugin.h" |
53 | #include "editor/plugins/gizmos/cpu_particles_3d_gizmo_plugin.h" |
54 | #include "editor/plugins/gizmos/decal_gizmo_plugin.h" |
55 | #include "editor/plugins/gizmos/fog_volume_gizmo_plugin.h" |
56 | #include "editor/plugins/gizmos/gpu_particles_3d_gizmo_plugin.h" |
57 | #include "editor/plugins/gizmos/gpu_particles_collision_3d_gizmo_plugin.h" |
58 | #include "editor/plugins/gizmos/joint_3d_gizmo_plugin.h" |
59 | #include "editor/plugins/gizmos/label_3d_gizmo_plugin.h" |
60 | #include "editor/plugins/gizmos/light_3d_gizmo_plugin.h" |
61 | #include "editor/plugins/gizmos/lightmap_gi_gizmo_plugin.h" |
62 | #include "editor/plugins/gizmos/lightmap_probe_gizmo_plugin.h" |
63 | #include "editor/plugins/gizmos/marker_3d_gizmo_plugin.h" |
64 | #include "editor/plugins/gizmos/mesh_instance_3d_gizmo_plugin.h" |
65 | #include "editor/plugins/gizmos/navigation_link_3d_gizmo_plugin.h" |
66 | #include "editor/plugins/gizmos/navigation_region_3d_gizmo_plugin.h" |
67 | #include "editor/plugins/gizmos/occluder_instance_3d_gizmo_plugin.h" |
68 | #include "editor/plugins/gizmos/physics_bone_3d_gizmo_plugin.h" |
69 | #include "editor/plugins/gizmos/ray_cast_3d_gizmo_plugin.h" |
70 | #include "editor/plugins/gizmos/reflection_probe_gizmo_plugin.h" |
71 | #include "editor/plugins/gizmos/shape_cast_3d_gizmo_plugin.h" |
72 | #include "editor/plugins/gizmos/soft_body_3d_gizmo_plugin.h" |
73 | #include "editor/plugins/gizmos/spring_arm_3d_gizmo_plugin.h" |
74 | #include "editor/plugins/gizmos/sprite_3d_gizmo_plugin.h" |
75 | #include "editor/plugins/gizmos/vehicle_body_3d_gizmo_plugin.h" |
76 | #include "editor/plugins/gizmos/visible_on_screen_notifier_3d_gizmo_plugin.h" |
77 | #include "editor/plugins/gizmos/voxel_gi_gizmo_plugin.h" |
78 | #include "editor/plugins/node_3d_editor_gizmos.h" |
79 | #include "editor/scene_tree_dock.h" |
80 | #include "scene/3d/camera_3d.h" |
81 | #include "scene/3d/collision_shape_3d.h" |
82 | #include "scene/3d/decal.h" |
83 | #include "scene/3d/light_3d.h" |
84 | #include "scene/3d/mesh_instance_3d.h" |
85 | #include "scene/3d/physics_body_3d.h" |
86 | #include "scene/3d/visual_instance_3d.h" |
87 | #include "scene/3d/world_environment.h" |
88 | #include "scene/gui/center_container.h" |
89 | #include "scene/gui/color_picker.h" |
90 | #include "scene/gui/flow_container.h" |
91 | #include "scene/gui/split_container.h" |
92 | #include "scene/gui/subviewport_container.h" |
93 | #include "scene/resources/packed_scene.h" |
94 | #include "scene/resources/sky_material.h" |
95 | #include "scene/resources/surface_tool.h" |
96 | |
97 | constexpr real_t DISTANCE_DEFAULT = 4; |
98 | |
99 | constexpr real_t GIZMO_ARROW_SIZE = 0.35; |
100 | constexpr real_t GIZMO_RING_HALF_WIDTH = 0.1; |
101 | constexpr real_t GIZMO_PLANE_SIZE = 0.2; |
102 | constexpr real_t GIZMO_PLANE_DST = 0.3; |
103 | constexpr real_t GIZMO_CIRCLE_SIZE = 1.1; |
104 | constexpr real_t GIZMO_SCALE_OFFSET = GIZMO_CIRCLE_SIZE + 0.3; |
105 | constexpr real_t GIZMO_ARROW_OFFSET = GIZMO_CIRCLE_SIZE + 0.3; |
106 | |
107 | constexpr real_t ZOOM_FREELOOK_MIN = 0.01; |
108 | constexpr real_t ZOOM_FREELOOK_MULTIPLIER = 1.08; |
109 | constexpr real_t ZOOM_FREELOOK_INDICATOR_DELAY_S = 1.5; |
110 | |
111 | #ifdef REAL_T_IS_DOUBLE |
112 | constexpr double ZOOM_FREELOOK_MAX = 1'000'000'000'000; |
113 | #else |
114 | constexpr float ZOOM_FREELOOK_MAX = 10'000; |
115 | #endif |
116 | |
117 | constexpr real_t MIN_Z = 0.01; |
118 | constexpr real_t MAX_Z = 1000000.0; |
119 | |
120 | constexpr real_t MIN_FOV = 0.01; |
121 | constexpr real_t MAX_FOV = 179; |
122 | |
123 | void ViewportNavigationControl::_notification(int p_what) { |
124 | switch (p_what) { |
125 | case NOTIFICATION_ENTER_TREE: { |
126 | if (!is_connected("mouse_exited" , callable_mp(this, &ViewportNavigationControl::_on_mouse_exited))) { |
127 | connect("mouse_exited" , callable_mp(this, &ViewportNavigationControl::_on_mouse_exited)); |
128 | } |
129 | if (!is_connected("mouse_entered" , callable_mp(this, &ViewportNavigationControl::_on_mouse_entered))) { |
130 | connect("mouse_entered" , callable_mp(this, &ViewportNavigationControl::_on_mouse_entered)); |
131 | } |
132 | } break; |
133 | |
134 | case NOTIFICATION_DRAW: { |
135 | if (viewport != nullptr) { |
136 | _draw(); |
137 | _update_navigation(); |
138 | } |
139 | } break; |
140 | } |
141 | } |
142 | |
143 | void ViewportNavigationControl::_draw() { |
144 | if (nav_mode == Node3DEditorViewport::NAVIGATION_NONE) { |
145 | return; |
146 | } |
147 | |
148 | Vector2 center = get_size() / 2.0; |
149 | float radius = get_size().x / 2.0; |
150 | |
151 | const bool focused = focused_index != -1; |
152 | draw_circle(center, radius, Color(0.5, 0.5, 0.5, focused || hovered ? 0.35 : 0.15)); |
153 | |
154 | const Color c = focused ? Color(0.9, 0.9, 0.9, 0.9) : Color(0.5, 0.5, 0.5, 0.25); |
155 | |
156 | Vector2 circle_pos = focused ? center.move_toward(focused_pos, radius) : center; |
157 | |
158 | draw_circle(circle_pos, AXIS_CIRCLE_RADIUS, c); |
159 | draw_circle(circle_pos, AXIS_CIRCLE_RADIUS * 0.8, c.darkened(0.4)); |
160 | } |
161 | |
162 | void ViewportNavigationControl::_process_click(int p_index, Vector2 p_position, bool p_pressed) { |
163 | hovered = false; |
164 | queue_redraw(); |
165 | |
166 | if (focused_index != -1 && focused_index != p_index) { |
167 | return; |
168 | } |
169 | if (p_pressed) { |
170 | if (p_position.distance_to(get_size() / 2.0) < get_size().x / 2.0) { |
171 | focused_pos = p_position; |
172 | focused_index = p_index; |
173 | queue_redraw(); |
174 | } |
175 | } else { |
176 | focused_index = -1; |
177 | if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) { |
178 | Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE); |
179 | Input::get_singleton()->warp_mouse(focused_mouse_start); |
180 | } |
181 | } |
182 | } |
183 | |
184 | void ViewportNavigationControl::_process_drag(int p_index, Vector2 p_position, Vector2 p_relative_position) { |
185 | if (focused_index == p_index) { |
186 | if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) { |
187 | Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); |
188 | focused_mouse_start = p_position; |
189 | } |
190 | focused_pos += p_relative_position; |
191 | queue_redraw(); |
192 | } |
193 | } |
194 | |
195 | void ViewportNavigationControl::gui_input(const Ref<InputEvent> &p_event) { |
196 | // Mouse events |
197 | const Ref<InputEventMouseButton> mouse_button = p_event; |
198 | if (mouse_button.is_valid() && mouse_button->get_button_index() == MouseButton::LEFT) { |
199 | _process_click(100, mouse_button->get_position(), mouse_button->is_pressed()); |
200 | } |
201 | |
202 | const Ref<InputEventMouseMotion> mouse_motion = p_event; |
203 | if (mouse_motion.is_valid()) { |
204 | _process_drag(100, mouse_motion->get_global_position(), viewport->_get_warped_mouse_motion(mouse_motion)); |
205 | } |
206 | |
207 | // Touch events |
208 | const Ref<InputEventScreenTouch> screen_touch = p_event; |
209 | if (screen_touch.is_valid()) { |
210 | _process_click(screen_touch->get_index(), screen_touch->get_position(), screen_touch->is_pressed()); |
211 | } |
212 | |
213 | const Ref<InputEventScreenDrag> screen_drag = p_event; |
214 | if (screen_drag.is_valid()) { |
215 | _process_drag(screen_drag->get_index(), screen_drag->get_position(), screen_drag->get_relative()); |
216 | } |
217 | } |
218 | |
219 | void ViewportNavigationControl::_update_navigation() { |
220 | if (focused_index == -1) { |
221 | return; |
222 | } |
223 | |
224 | Vector2 delta = focused_pos - (get_size() / 2.0); |
225 | Vector2 delta_normalized = delta.normalized(); |
226 | switch (nav_mode) { |
227 | case Node3DEditorViewport::NavigationMode::NAVIGATION_MOVE: { |
228 | real_t speed_multiplier = MIN(delta.length() / (get_size().x * 100.0), 3.0); |
229 | real_t speed = viewport->freelook_speed * speed_multiplier; |
230 | |
231 | const Node3DEditorViewport::FreelookNavigationScheme navigation_scheme = (Node3DEditorViewport::FreelookNavigationScheme)EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_navigation_scheme" ).operator int(); |
232 | |
233 | Vector3 forward; |
234 | if (navigation_scheme == Node3DEditorViewport::FreelookNavigationScheme::FREELOOK_FULLY_AXIS_LOCKED) { |
235 | // Forward/backward keys will always go straight forward/backward, never moving on the Y axis. |
236 | forward = Vector3(0, 0, delta_normalized.y).rotated(Vector3(0, 1, 0), viewport->camera->get_rotation().y); |
237 | } else { |
238 | // Forward/backward keys will be relative to the camera pitch. |
239 | forward = viewport->camera->get_transform().basis.xform(Vector3(0, 0, delta_normalized.y)); |
240 | } |
241 | |
242 | const Vector3 right = viewport->camera->get_transform().basis.xform(Vector3(delta_normalized.x, 0, 0)); |
243 | |
244 | const Vector3 direction = forward + right; |
245 | const Vector3 motion = direction * speed; |
246 | viewport->cursor.pos += motion; |
247 | viewport->cursor.eye_pos += motion; |
248 | } break; |
249 | |
250 | case Node3DEditorViewport::NavigationMode::NAVIGATION_LOOK: { |
251 | real_t speed_multiplier = MIN(delta.length() / (get_size().x * 2.5), 3.0); |
252 | real_t speed = viewport->freelook_speed * speed_multiplier; |
253 | viewport->_nav_look(nullptr, delta_normalized * speed); |
254 | } break; |
255 | |
256 | case Node3DEditorViewport::NAVIGATION_PAN: { |
257 | real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0); |
258 | real_t speed = viewport->freelook_speed * speed_multiplier; |
259 | viewport->_nav_pan(nullptr, -delta_normalized * speed); |
260 | } break; |
261 | case Node3DEditorViewport::NAVIGATION_ZOOM: { |
262 | real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0); |
263 | real_t speed = viewport->freelook_speed * speed_multiplier; |
264 | viewport->_nav_zoom(nullptr, delta_normalized * speed); |
265 | } break; |
266 | case Node3DEditorViewport::NAVIGATION_ORBIT: { |
267 | real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0); |
268 | real_t speed = viewport->freelook_speed * speed_multiplier; |
269 | viewport->_nav_orbit(nullptr, delta_normalized * speed); |
270 | } break; |
271 | case Node3DEditorViewport::NAVIGATION_NONE: { |
272 | } break; |
273 | } |
274 | } |
275 | |
276 | void ViewportNavigationControl::_on_mouse_entered() { |
277 | hovered = true; |
278 | queue_redraw(); |
279 | } |
280 | |
281 | void ViewportNavigationControl::_on_mouse_exited() { |
282 | hovered = false; |
283 | queue_redraw(); |
284 | } |
285 | |
286 | void ViewportNavigationControl::set_navigation_mode(Node3DEditorViewport::NavigationMode p_nav_mode) { |
287 | nav_mode = p_nav_mode; |
288 | } |
289 | |
290 | void ViewportNavigationControl::set_viewport(Node3DEditorViewport *p_viewport) { |
291 | viewport = p_viewport; |
292 | } |
293 | |
294 | void ViewportRotationControl::_notification(int p_what) { |
295 | switch (p_what) { |
296 | case NOTIFICATION_ENTER_TREE: { |
297 | axis_menu_options.clear(); |
298 | axis_menu_options.push_back(Node3DEditorViewport::VIEW_RIGHT); |
299 | axis_menu_options.push_back(Node3DEditorViewport::VIEW_TOP); |
300 | axis_menu_options.push_back(Node3DEditorViewport::VIEW_FRONT); |
301 | axis_menu_options.push_back(Node3DEditorViewport::VIEW_LEFT); |
302 | axis_menu_options.push_back(Node3DEditorViewport::VIEW_BOTTOM); |
303 | axis_menu_options.push_back(Node3DEditorViewport::VIEW_REAR); |
304 | |
305 | axis_colors.clear(); |
306 | axis_colors.push_back(get_theme_color(SNAME("axis_x_color" ), EditorStringName(Editor))); |
307 | axis_colors.push_back(get_theme_color(SNAME("axis_y_color" ), EditorStringName(Editor))); |
308 | axis_colors.push_back(get_theme_color(SNAME("axis_z_color" ), EditorStringName(Editor))); |
309 | queue_redraw(); |
310 | |
311 | if (!is_connected("mouse_exited" , callable_mp(this, &ViewportRotationControl::_on_mouse_exited))) { |
312 | connect("mouse_exited" , callable_mp(this, &ViewportRotationControl::_on_mouse_exited)); |
313 | } |
314 | } break; |
315 | |
316 | case NOTIFICATION_DRAW: { |
317 | if (viewport != nullptr) { |
318 | _draw(); |
319 | } |
320 | } break; |
321 | } |
322 | } |
323 | |
324 | void ViewportRotationControl::_draw() { |
325 | const Vector2i center = get_size() / 2.0; |
326 | const real_t radius = get_size().x / 2.0; |
327 | |
328 | if (focused_axis > -2 || orbiting_index != -1) { |
329 | draw_circle(center, radius, Color(0.5, 0.5, 0.5, 0.25)); |
330 | } |
331 | |
332 | Vector<Axis2D> axis_to_draw; |
333 | _get_sorted_axis(axis_to_draw); |
334 | for (int i = 0; i < axis_to_draw.size(); ++i) { |
335 | _draw_axis(axis_to_draw[i]); |
336 | } |
337 | } |
338 | |
339 | void ViewportRotationControl::_draw_axis(const Axis2D &p_axis) { |
340 | const bool focused = focused_axis == p_axis.axis; |
341 | const bool positive = p_axis.axis < 3; |
342 | const int direction = p_axis.axis % 3; |
343 | |
344 | const Color axis_color = axis_colors[direction]; |
345 | const double alpha = focused ? 1.0 : ((p_axis.z_axis + 1.0) / 2.0) * 0.5 + 0.5; |
346 | const Color c = focused ? Color(0.9, 0.9, 0.9) : Color(axis_color, alpha); |
347 | |
348 | if (positive) { |
349 | // Draw axis lines for the positive axes. |
350 | const Vector2i center = get_size() / 2.0; |
351 | draw_line(center, p_axis.screen_point, c, 1.5 * EDSCALE); |
352 | |
353 | draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c); |
354 | |
355 | // Draw the axis letter for the positive axes. |
356 | const String axis_name = direction == 0 ? "X" : (direction == 1 ? "Y" : "Z" ); |
357 | draw_char(get_theme_font(SNAME("rotation_control" ), EditorStringName(EditorFonts)), p_axis.screen_point + Vector2i(Math::round(-4.0 * EDSCALE), Math::round(5.0 * EDSCALE)), axis_name, get_theme_font_size(SNAME("rotation_control_size" ), EditorStringName(EditorFonts)), Color(0.0, 0.0, 0.0, alpha)); |
358 | } else { |
359 | // Draw an outline around the negative axes. |
360 | draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c); |
361 | draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS * 0.8, c.darkened(0.4)); |
362 | } |
363 | } |
364 | |
365 | void ViewportRotationControl::_get_sorted_axis(Vector<Axis2D> &r_axis) { |
366 | const Vector2i center = get_size() / 2.0; |
367 | const real_t radius = get_size().x / 2.0 - AXIS_CIRCLE_RADIUS - 2.0 * EDSCALE; |
368 | const Basis camera_basis = viewport->to_camera_transform(viewport->cursor).get_basis().inverse(); |
369 | |
370 | for (int i = 0; i < 3; ++i) { |
371 | Vector3 axis_3d = camera_basis.get_column(i); |
372 | Vector2i axis_vector = Vector2(axis_3d.x, -axis_3d.y) * radius; |
373 | |
374 | if (Math::abs(axis_3d.z) <= 1.0) { |
375 | Axis2D pos_axis; |
376 | pos_axis.axis = i; |
377 | pos_axis.screen_point = center + axis_vector; |
378 | pos_axis.z_axis = axis_3d.z; |
379 | r_axis.push_back(pos_axis); |
380 | |
381 | Axis2D neg_axis; |
382 | neg_axis.axis = i + 3; |
383 | neg_axis.screen_point = center - axis_vector; |
384 | neg_axis.z_axis = -axis_3d.z; |
385 | r_axis.push_back(neg_axis); |
386 | } else { |
387 | // Special case when the camera is aligned with one axis |
388 | Axis2D axis; |
389 | axis.axis = i + (axis_3d.z <= 0 ? 0 : 3); |
390 | axis.screen_point = center; |
391 | axis.z_axis = 1.0; |
392 | r_axis.push_back(axis); |
393 | } |
394 | } |
395 | |
396 | r_axis.sort_custom<Axis2DCompare>(); |
397 | } |
398 | |
399 | void ViewportRotationControl::_process_click(int p_index, Vector2 p_position, bool p_pressed) { |
400 | if (orbiting_index != -1 && orbiting_index != p_index) { |
401 | return; |
402 | } |
403 | if (p_pressed) { |
404 | if (p_position.distance_to(get_size() / 2.0) < get_size().x / 2.0) { |
405 | orbiting_index = p_index; |
406 | } |
407 | } else { |
408 | if (focused_axis > -1) { |
409 | viewport->_menu_option(axis_menu_options[focused_axis]); |
410 | _update_focus(); |
411 | } |
412 | orbiting_index = -1; |
413 | if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) { |
414 | Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE); |
415 | Input::get_singleton()->warp_mouse(orbiting_mouse_start); |
416 | } |
417 | } |
418 | } |
419 | |
420 | void ViewportRotationControl::_process_drag(Ref<InputEventWithModifiers> p_event, int p_index, Vector2 p_position, Vector2 p_relative_position) { |
421 | if (orbiting_index == p_index) { |
422 | if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) { |
423 | Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); |
424 | orbiting_mouse_start = p_position; |
425 | } |
426 | viewport->_nav_orbit(p_event, p_relative_position); |
427 | focused_axis = -1; |
428 | } else { |
429 | _update_focus(); |
430 | } |
431 | } |
432 | |
433 | void ViewportRotationControl::gui_input(const Ref<InputEvent> &p_event) { |
434 | ERR_FAIL_COND(p_event.is_null()); |
435 | |
436 | // Mouse events |
437 | const Ref<InputEventMouseButton> mb = p_event; |
438 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) { |
439 | _process_click(100, mb->get_position(), mb->is_pressed()); |
440 | } |
441 | |
442 | const Ref<InputEventMouseMotion> mm = p_event; |
443 | if (mm.is_valid()) { |
444 | _process_drag(mm, 100, mm->get_global_position(), viewport->_get_warped_mouse_motion(mm)); |
445 | } |
446 | |
447 | // Touch events |
448 | const Ref<InputEventScreenTouch> screen_touch = p_event; |
449 | if (screen_touch.is_valid()) { |
450 | _process_click(screen_touch->get_index(), screen_touch->get_position(), screen_touch->is_pressed()); |
451 | } |
452 | |
453 | const Ref<InputEventScreenDrag> screen_drag = p_event; |
454 | if (screen_drag.is_valid()) { |
455 | _process_drag(screen_drag, screen_drag->get_index(), screen_drag->get_position(), screen_drag->get_relative()); |
456 | } |
457 | } |
458 | |
459 | void ViewportRotationControl::_update_focus() { |
460 | int original_focus = focused_axis; |
461 | focused_axis = -2; |
462 | Vector2 mouse_pos = get_local_mouse_position(); |
463 | |
464 | if (mouse_pos.distance_to(get_size() / 2.0) < get_size().x / 2.0) { |
465 | focused_axis = -1; |
466 | } |
467 | |
468 | Vector<Axis2D> axes; |
469 | _get_sorted_axis(axes); |
470 | |
471 | for (int i = 0; i < axes.size(); i++) { |
472 | const Axis2D &axis = axes[i]; |
473 | if (mouse_pos.distance_to(axis.screen_point) < AXIS_CIRCLE_RADIUS) { |
474 | focused_axis = axis.axis; |
475 | } |
476 | } |
477 | |
478 | if (focused_axis != original_focus) { |
479 | queue_redraw(); |
480 | } |
481 | } |
482 | |
483 | void ViewportRotationControl::_on_mouse_exited() { |
484 | focused_axis = -2; |
485 | queue_redraw(); |
486 | } |
487 | |
488 | void ViewportRotationControl::set_viewport(Node3DEditorViewport *p_viewport) { |
489 | viewport = p_viewport; |
490 | } |
491 | |
492 | void Node3DEditorViewport::_view_settings_confirmed(real_t p_interp_delta) { |
493 | // Set FOV override multiplier back to the default, so that the FOV |
494 | // setting specified in the View menu is correctly applied. |
495 | cursor.fov_scale = 1.0; |
496 | |
497 | _update_camera(p_interp_delta); |
498 | } |
499 | |
500 | void Node3DEditorViewport::_update_navigation_controls_visibility() { |
501 | bool show_viewport_rotation_gizmo = EDITOR_GET("editors/3d/navigation/show_viewport_rotation_gizmo" ) && (!previewing_cinema && !previewing_camera); |
502 | rotation_control->set_visible(show_viewport_rotation_gizmo); |
503 | |
504 | bool show_viewport_navigation_gizmo = EDITOR_GET("editors/3d/navigation/show_viewport_navigation_gizmo" ) && (!previewing_cinema && !previewing_camera); |
505 | position_control->set_visible(show_viewport_navigation_gizmo); |
506 | look_control->set_visible(show_viewport_navigation_gizmo); |
507 | } |
508 | |
509 | void Node3DEditorViewport::_update_camera(real_t p_interp_delta) { |
510 | bool is_orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL; |
511 | |
512 | Cursor old_camera_cursor = camera_cursor; |
513 | camera_cursor = cursor; |
514 | |
515 | if (p_interp_delta > 0) { |
516 | //------- |
517 | // Perform smoothing |
518 | |
519 | if (is_freelook_active()) { |
520 | // Higher inertia should increase "lag" (lerp with factor between 0 and 1) |
521 | // Inertia of zero should produce instant movement (lerp with factor of 1) in this case it returns a really high value and gets clamped to 1. |
522 | const real_t inertia = EDITOR_GET("editors/3d/freelook/freelook_inertia" ); |
523 | real_t factor = (1.0 / inertia) * p_interp_delta; |
524 | |
525 | // We interpolate a different point here, because in freelook mode the focus point (cursor.pos) orbits around eye_pos |
526 | camera_cursor.eye_pos = old_camera_cursor.eye_pos.lerp(cursor.eye_pos, CLAMP(factor, 0, 1)); |
527 | |
528 | const real_t orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia" ); |
529 | camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia))); |
530 | camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia))); |
531 | |
532 | if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) { |
533 | camera_cursor.x_rot = cursor.x_rot; |
534 | } |
535 | |
536 | if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) { |
537 | camera_cursor.y_rot = cursor.y_rot; |
538 | } |
539 | |
540 | Vector3 forward = to_camera_transform(camera_cursor).basis.xform(Vector3(0, 0, -1)); |
541 | camera_cursor.pos = camera_cursor.eye_pos + forward * camera_cursor.distance; |
542 | |
543 | } else { |
544 | const real_t orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia" ); |
545 | const real_t translation_inertia = EDITOR_GET("editors/3d/navigation_feel/translation_inertia" ); |
546 | const real_t zoom_inertia = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia" ); |
547 | |
548 | camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia))); |
549 | camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia))); |
550 | |
551 | if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) { |
552 | camera_cursor.x_rot = cursor.x_rot; |
553 | } |
554 | |
555 | if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) { |
556 | camera_cursor.y_rot = cursor.y_rot; |
557 | } |
558 | |
559 | camera_cursor.pos = old_camera_cursor.pos.lerp(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia))); |
560 | camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN((real_t)1.0, p_interp_delta * (1 / zoom_inertia))); |
561 | } |
562 | } |
563 | |
564 | //------- |
565 | // Apply camera transform |
566 | |
567 | real_t tolerance = 0.001; |
568 | bool equal = true; |
569 | if (!Math::is_equal_approx(old_camera_cursor.x_rot, camera_cursor.x_rot, tolerance) || !Math::is_equal_approx(old_camera_cursor.y_rot, camera_cursor.y_rot, tolerance)) { |
570 | equal = false; |
571 | } else if (!old_camera_cursor.pos.is_equal_approx(camera_cursor.pos)) { |
572 | equal = false; |
573 | } else if (!Math::is_equal_approx(old_camera_cursor.distance, camera_cursor.distance, tolerance)) { |
574 | equal = false; |
575 | } else if (!Math::is_equal_approx(old_camera_cursor.fov_scale, camera_cursor.fov_scale, tolerance)) { |
576 | equal = false; |
577 | } |
578 | |
579 | if (!equal || p_interp_delta == 0 || is_orthogonal != orthogonal) { |
580 | camera->set_global_transform(to_camera_transform(camera_cursor)); |
581 | |
582 | if (orthogonal) { |
583 | float half_fov = Math::deg_to_rad(get_fov()) / 2.0; |
584 | float height = 2.0 * cursor.distance * Math::tan(half_fov); |
585 | camera->set_orthogonal(height, get_znear(), get_zfar()); |
586 | } else { |
587 | camera->set_perspective(get_fov(), get_znear(), get_zfar()); |
588 | } |
589 | |
590 | update_transform_gizmo_view(); |
591 | rotation_control->queue_redraw(); |
592 | position_control->queue_redraw(); |
593 | look_control->queue_redraw(); |
594 | spatial_editor->update_grid(); |
595 | } |
596 | } |
597 | |
598 | Transform3D Node3DEditorViewport::to_camera_transform(const Cursor &p_cursor) const { |
599 | Transform3D camera_transform; |
600 | camera_transform.translate_local(p_cursor.pos); |
601 | camera_transform.basis.rotate(Vector3(1, 0, 0), -p_cursor.x_rot); |
602 | camera_transform.basis.rotate(Vector3(0, 1, 0), -p_cursor.y_rot); |
603 | |
604 | if (orthogonal) { |
605 | camera_transform.translate_local(0, 0, (get_zfar() - get_znear()) / 2.0); |
606 | } else { |
607 | camera_transform.translate_local(0, 0, p_cursor.distance); |
608 | } |
609 | |
610 | return camera_transform; |
611 | } |
612 | |
613 | int Node3DEditorViewport::get_selected_count() const { |
614 | const HashMap<Node *, Object *> &selection = editor_selection->get_selection(); |
615 | |
616 | int count = 0; |
617 | |
618 | for (const KeyValue<Node *, Object *> &E : selection) { |
619 | Node3D *sp = Object::cast_to<Node3D>(E.key); |
620 | if (!sp) { |
621 | continue; |
622 | } |
623 | |
624 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
625 | if (!se) { |
626 | continue; |
627 | } |
628 | |
629 | count++; |
630 | } |
631 | |
632 | return count; |
633 | } |
634 | |
635 | void Node3DEditorViewport::cancel_transform() { |
636 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
637 | |
638 | for (List<Node *>::Element *E = selection.front(); E; E = E->next()) { |
639 | Node3D *sp = Object::cast_to<Node3D>(E->get()); |
640 | if (!sp) { |
641 | continue; |
642 | } |
643 | |
644 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
645 | if (!se) { |
646 | continue; |
647 | } |
648 | |
649 | sp->set_global_transform(se->original); |
650 | } |
651 | |
652 | finish_transform(); |
653 | set_message(TTR("Transform Aborted." ), 3); |
654 | } |
655 | |
656 | void Node3DEditorViewport::_update_shrink() { |
657 | bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION)); |
658 | subviewport_container->set_stretch_shrink(shrink ? 2 : 1); |
659 | subviewport_container->set_texture_filter(shrink ? TEXTURE_FILTER_NEAREST : TEXTURE_FILTER_PARENT_NODE); |
660 | } |
661 | |
662 | float Node3DEditorViewport::get_znear() const { |
663 | return CLAMP(spatial_editor->get_znear(), MIN_Z, MAX_Z); |
664 | } |
665 | |
666 | float Node3DEditorViewport::get_zfar() const { |
667 | return CLAMP(spatial_editor->get_zfar(), MIN_Z, MAX_Z); |
668 | } |
669 | |
670 | float Node3DEditorViewport::get_fov() const { |
671 | return CLAMP(spatial_editor->get_fov() * cursor.fov_scale, MIN_FOV, MAX_FOV); |
672 | } |
673 | |
674 | Transform3D Node3DEditorViewport::_get_camera_transform() const { |
675 | return camera->get_global_transform(); |
676 | } |
677 | |
678 | Vector3 Node3DEditorViewport::_get_camera_position() const { |
679 | return _get_camera_transform().origin; |
680 | } |
681 | |
682 | Point2 Node3DEditorViewport::_point_to_screen(const Vector3 &p_point) { |
683 | return camera->unproject_position(p_point) * subviewport_container->get_stretch_shrink(); |
684 | } |
685 | |
686 | Vector3 Node3DEditorViewport::_get_ray_pos(const Vector2 &p_pos) const { |
687 | return camera->project_ray_origin(p_pos / subviewport_container->get_stretch_shrink()); |
688 | } |
689 | |
690 | Vector3 Node3DEditorViewport::_get_camera_normal() const { |
691 | return -_get_camera_transform().basis.get_column(2); |
692 | } |
693 | |
694 | Vector3 Node3DEditorViewport::_get_ray(const Vector2 &p_pos) const { |
695 | return camera->project_ray_normal(p_pos / subviewport_container->get_stretch_shrink()); |
696 | } |
697 | |
698 | void Node3DEditorViewport::_clear_selected() { |
699 | _edit.gizmo = Ref<EditorNode3DGizmo>(); |
700 | _edit.gizmo_handle = -1; |
701 | _edit.gizmo_handle_secondary = false; |
702 | _edit.gizmo_initial_value = Variant(); |
703 | |
704 | Node3D *selected = spatial_editor->get_single_selected_node(); |
705 | Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr; |
706 | |
707 | if (se && se->gizmo.is_valid()) { |
708 | se->subgizmos.clear(); |
709 | se->gizmo->redraw(); |
710 | se->gizmo.unref(); |
711 | spatial_editor->update_transform_gizmo(); |
712 | } else { |
713 | editor_selection->clear(); |
714 | Node3DEditor::get_singleton()->edit(nullptr); |
715 | } |
716 | } |
717 | |
718 | void Node3DEditorViewport::_select_clicked(bool p_allow_locked) { |
719 | Node *node = Object::cast_to<Node3D>(ObjectDB::get_instance(clicked)); |
720 | Node3D *selected = Object::cast_to<Node3D>(node); |
721 | clicked = ObjectID(); |
722 | |
723 | if (!selected) { |
724 | return; |
725 | } |
726 | |
727 | if (!p_allow_locked) { |
728 | // Replace the node by the group if grouped |
729 | while (node && node != EditorNode::get_singleton()->get_edited_scene()->get_parent()) { |
730 | Node3D *selected_tmp = Object::cast_to<Node3D>(node); |
731 | if (selected_tmp && node->has_meta("_edit_group_" )) { |
732 | selected = selected_tmp; |
733 | } |
734 | node = node->get_parent(); |
735 | } |
736 | } |
737 | |
738 | if (p_allow_locked || !_is_node_locked(selected)) { |
739 | if (clicked_wants_append) { |
740 | if (editor_selection->is_selected(selected)) { |
741 | editor_selection->remove_node(selected); |
742 | } else { |
743 | editor_selection->add_node(selected); |
744 | } |
745 | } else { |
746 | if (!editor_selection->is_selected(selected)) { |
747 | editor_selection->clear(); |
748 | editor_selection->add_node(selected); |
749 | EditorNode::get_singleton()->edit_node(selected); |
750 | } |
751 | } |
752 | |
753 | if (editor_selection->get_selected_node_list().size() == 1) { |
754 | EditorNode::get_singleton()->edit_node(editor_selection->get_selected_node_list()[0]); |
755 | } |
756 | } |
757 | } |
758 | |
759 | ObjectID Node3DEditorViewport::_select_ray(const Point2 &p_pos) const { |
760 | Vector3 ray = _get_ray(p_pos); |
761 | Vector3 pos = _get_ray_pos(p_pos); |
762 | Vector2 shrinked_pos = p_pos / subviewport_container->get_stretch_shrink(); |
763 | |
764 | if (viewport->get_debug_draw() == Viewport::DEBUG_DRAW_SDFGI_PROBES) { |
765 | RS::get_singleton()->sdfgi_set_debug_probe_select(pos, ray); |
766 | } |
767 | |
768 | Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_ray(pos, pos + ray * camera->get_far(), get_tree()->get_root()->get_world_3d()->get_scenario()); |
769 | HashSet<Ref<EditorNode3DGizmo>> found_gizmos; |
770 | |
771 | Node *edited_scene = get_tree()->get_edited_scene_root(); |
772 | ObjectID closest; |
773 | Node *item = nullptr; |
774 | float closest_dist = 1e20; |
775 | |
776 | for (int i = 0; i < instances.size(); i++) { |
777 | Node3D *spat = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i])); |
778 | |
779 | if (!spat) { |
780 | continue; |
781 | } |
782 | |
783 | Vector<Ref<Node3DGizmo>> gizmos = spat->get_gizmos(); |
784 | |
785 | for (int j = 0; j < gizmos.size(); j++) { |
786 | Ref<EditorNode3DGizmo> seg = gizmos[j]; |
787 | |
788 | if ((!seg.is_valid()) || found_gizmos.has(seg)) { |
789 | continue; |
790 | } |
791 | |
792 | found_gizmos.insert(seg); |
793 | Vector3 point; |
794 | Vector3 normal; |
795 | |
796 | bool inters = seg->intersect_ray(camera, shrinked_pos, point, normal); |
797 | |
798 | if (!inters) { |
799 | continue; |
800 | } |
801 | |
802 | const real_t dist = pos.distance_to(point); |
803 | |
804 | if (dist < 0) { |
805 | continue; |
806 | } |
807 | |
808 | if (dist < closest_dist) { |
809 | item = Object::cast_to<Node>(spat); |
810 | if (item != edited_scene) { |
811 | item = edited_scene->get_deepest_editable_node(item); |
812 | } |
813 | |
814 | closest = item->get_instance_id(); |
815 | closest_dist = dist; |
816 | } |
817 | } |
818 | } |
819 | |
820 | if (!item) { |
821 | return ObjectID(); |
822 | } |
823 | |
824 | return closest; |
825 | } |
826 | |
827 | void Node3DEditorViewport::_find_items_at_pos(const Point2 &p_pos, Vector<_RayResult> &r_results, bool p_include_locked_nodes) { |
828 | Vector3 ray = _get_ray(p_pos); |
829 | Vector3 pos = _get_ray_pos(p_pos); |
830 | |
831 | Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_ray(pos, pos + ray * camera->get_far(), get_tree()->get_root()->get_world_3d()->get_scenario()); |
832 | HashSet<Node3D *> found_nodes; |
833 | |
834 | for (int i = 0; i < instances.size(); i++) { |
835 | Node3D *spat = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i])); |
836 | |
837 | if (!spat) { |
838 | continue; |
839 | } |
840 | |
841 | if (found_nodes.has(spat)) { |
842 | continue; |
843 | } |
844 | |
845 | if (!p_include_locked_nodes && _is_node_locked(spat)) { |
846 | continue; |
847 | } |
848 | |
849 | Vector<Ref<Node3DGizmo>> gizmos = spat->get_gizmos(); |
850 | for (int j = 0; j < gizmos.size(); j++) { |
851 | Ref<EditorNode3DGizmo> seg = gizmos[j]; |
852 | |
853 | if (!seg.is_valid()) { |
854 | continue; |
855 | } |
856 | |
857 | Vector3 point; |
858 | Vector3 normal; |
859 | |
860 | bool inters = seg->intersect_ray(camera, p_pos, point, normal); |
861 | |
862 | if (!inters) { |
863 | continue; |
864 | } |
865 | |
866 | const real_t dist = pos.distance_to(point); |
867 | |
868 | if (dist < 0) { |
869 | continue; |
870 | } |
871 | |
872 | found_nodes.insert(spat); |
873 | |
874 | _RayResult res; |
875 | res.item = spat; |
876 | res.depth = dist; |
877 | r_results.push_back(res); |
878 | break; |
879 | } |
880 | } |
881 | |
882 | r_results.sort(); |
883 | } |
884 | |
885 | Vector3 Node3DEditorViewport::_get_screen_to_space(const Vector3 &p_vector3) { |
886 | Projection cm; |
887 | if (orthogonal) { |
888 | cm.set_orthogonal(camera->get_size(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar()); |
889 | } else { |
890 | cm.set_perspective(get_fov(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar()); |
891 | } |
892 | Vector2 screen_he = cm.get_viewport_half_extents(); |
893 | |
894 | Transform3D camera_transform; |
895 | camera_transform.translate_local(cursor.pos); |
896 | camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot); |
897 | camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot); |
898 | camera_transform.translate_local(0, 0, cursor.distance); |
899 | |
900 | return camera_transform.xform(Vector3(((p_vector3.x / get_size().width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (p_vector3.y / get_size().height)) * 2.0 - 1.0) * screen_he.y, -(get_znear() + p_vector3.z))); |
901 | } |
902 | |
903 | void Node3DEditorViewport::_select_region() { |
904 | if (cursor.region_begin == cursor.region_end) { |
905 | if (!clicked_wants_append) { |
906 | _clear_selected(); |
907 | } |
908 | return; //nothing really |
909 | } |
910 | |
911 | const real_t z_offset = MAX(0.0, 5.0 - get_znear()); |
912 | |
913 | Vector3 box[4] = { |
914 | Vector3( |
915 | MIN(cursor.region_begin.x, cursor.region_end.x), |
916 | MIN(cursor.region_begin.y, cursor.region_end.y), |
917 | z_offset), |
918 | Vector3( |
919 | MAX(cursor.region_begin.x, cursor.region_end.x), |
920 | MIN(cursor.region_begin.y, cursor.region_end.y), |
921 | z_offset), |
922 | Vector3( |
923 | MAX(cursor.region_begin.x, cursor.region_end.x), |
924 | MAX(cursor.region_begin.y, cursor.region_end.y), |
925 | z_offset), |
926 | Vector3( |
927 | MIN(cursor.region_begin.x, cursor.region_end.x), |
928 | MAX(cursor.region_begin.y, cursor.region_end.y), |
929 | z_offset) |
930 | }; |
931 | |
932 | Vector<Plane> frustum; |
933 | |
934 | Vector3 cam_pos = _get_camera_position(); |
935 | |
936 | for (int i = 0; i < 4; i++) { |
937 | Vector3 a = _get_screen_to_space(box[i]); |
938 | Vector3 b = _get_screen_to_space(box[(i + 1) % 4]); |
939 | if (orthogonal) { |
940 | frustum.push_back(Plane((a - b).normalized(), a)); |
941 | } else { |
942 | frustum.push_back(Plane(a, b, cam_pos)); |
943 | } |
944 | } |
945 | |
946 | Plane near(-_get_camera_normal(), cam_pos); |
947 | near.d -= get_znear(); |
948 | frustum.push_back(near); |
949 | |
950 | Plane far = -near; |
951 | far.d += get_zfar(); |
952 | frustum.push_back(far); |
953 | |
954 | if (spatial_editor->get_single_selected_node()) { |
955 | Node3D *single_selected = spatial_editor->get_single_selected_node(); |
956 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(single_selected); |
957 | |
958 | if (se) { |
959 | Ref<EditorNode3DGizmo> old_gizmo; |
960 | if (!clicked_wants_append) { |
961 | se->subgizmos.clear(); |
962 | old_gizmo = se->gizmo; |
963 | se->gizmo.unref(); |
964 | } |
965 | |
966 | bool found_subgizmos = false; |
967 | Vector<Ref<Node3DGizmo>> gizmos = single_selected->get_gizmos(); |
968 | for (int j = 0; j < gizmos.size(); j++) { |
969 | Ref<EditorNode3DGizmo> seg = gizmos[j]; |
970 | if (!seg.is_valid()) { |
971 | continue; |
972 | } |
973 | |
974 | if (se->gizmo.is_valid() && se->gizmo != seg) { |
975 | continue; |
976 | } |
977 | |
978 | Vector<int> subgizmos = seg->subgizmos_intersect_frustum(camera, frustum); |
979 | if (!subgizmos.is_empty()) { |
980 | se->gizmo = seg; |
981 | for (int i = 0; i < subgizmos.size(); i++) { |
982 | int subgizmo_id = subgizmos[i]; |
983 | if (!se->subgizmos.has(subgizmo_id)) { |
984 | se->subgizmos.insert(subgizmo_id, se->gizmo->get_subgizmo_transform(subgizmo_id)); |
985 | } |
986 | } |
987 | found_subgizmos = true; |
988 | break; |
989 | } |
990 | } |
991 | |
992 | if (!clicked_wants_append || found_subgizmos) { |
993 | if (se->gizmo.is_valid()) { |
994 | se->gizmo->redraw(); |
995 | } |
996 | |
997 | if (old_gizmo != se->gizmo && old_gizmo.is_valid()) { |
998 | old_gizmo->redraw(); |
999 | } |
1000 | |
1001 | spatial_editor->update_transform_gizmo(); |
1002 | } |
1003 | |
1004 | if (found_subgizmos) { |
1005 | return; |
1006 | } |
1007 | } |
1008 | } |
1009 | |
1010 | if (!clicked_wants_append) { |
1011 | _clear_selected(); |
1012 | } |
1013 | |
1014 | Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_convex(frustum, get_tree()->get_root()->get_world_3d()->get_scenario()); |
1015 | HashSet<Node3D *> found_nodes; |
1016 | Vector<Node *> selected; |
1017 | |
1018 | Node *edited_scene = get_tree()->get_edited_scene_root(); |
1019 | |
1020 | for (int i = 0; i < instances.size(); i++) { |
1021 | Node3D *sp = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i])); |
1022 | if (!sp || _is_node_locked(sp)) { |
1023 | continue; |
1024 | } |
1025 | |
1026 | if (found_nodes.has(sp)) { |
1027 | continue; |
1028 | } |
1029 | |
1030 | found_nodes.insert(sp); |
1031 | |
1032 | Node *item = Object::cast_to<Node>(sp); |
1033 | if (item != edited_scene) { |
1034 | item = edited_scene->get_deepest_editable_node(item); |
1035 | } |
1036 | |
1037 | // Replace the node by the group if grouped |
1038 | if (item->is_class("Node3D" )) { |
1039 | Node3D *sel = Object::cast_to<Node3D>(item); |
1040 | while (item && item != EditorNode::get_singleton()->get_edited_scene()->get_parent()) { |
1041 | Node3D *selected_tmp = Object::cast_to<Node3D>(item); |
1042 | if (selected_tmp && item->has_meta("_edit_group_" )) { |
1043 | sel = selected_tmp; |
1044 | } |
1045 | item = item->get_parent(); |
1046 | } |
1047 | item = sel; |
1048 | } |
1049 | |
1050 | if (_is_node_locked(item)) { |
1051 | continue; |
1052 | } |
1053 | |
1054 | Vector<Ref<Node3DGizmo>> gizmos = sp->get_gizmos(); |
1055 | for (int j = 0; j < gizmos.size(); j++) { |
1056 | Ref<EditorNode3DGizmo> seg = gizmos[j]; |
1057 | if (!seg.is_valid()) { |
1058 | continue; |
1059 | } |
1060 | |
1061 | if (seg->intersect_frustum(camera, frustum)) { |
1062 | selected.push_back(item); |
1063 | } |
1064 | } |
1065 | } |
1066 | |
1067 | for (int i = 0; i < selected.size(); i++) { |
1068 | if (!editor_selection->is_selected(selected[i])) { |
1069 | editor_selection->add_node(selected[i]); |
1070 | } |
1071 | } |
1072 | |
1073 | if (editor_selection->get_selected_node_list().size() == 1) { |
1074 | EditorNode::get_singleton()->edit_node(editor_selection->get_selected_node_list()[0]); |
1075 | } |
1076 | } |
1077 | |
1078 | void Node3DEditorViewport::_update_name() { |
1079 | String name; |
1080 | |
1081 | switch (view_type) { |
1082 | case VIEW_TYPE_USER: { |
1083 | if (orthogonal) { |
1084 | name = TTR("Orthogonal" ); |
1085 | } else { |
1086 | name = TTR("Perspective" ); |
1087 | } |
1088 | } break; |
1089 | case VIEW_TYPE_TOP: { |
1090 | if (orthogonal) { |
1091 | name = TTR("Top Orthogonal" ); |
1092 | } else { |
1093 | name = TTR("Top Perspective" ); |
1094 | } |
1095 | } break; |
1096 | case VIEW_TYPE_BOTTOM: { |
1097 | if (orthogonal) { |
1098 | name = TTR("Bottom Orthogonal" ); |
1099 | } else { |
1100 | name = TTR("Bottom Perspective" ); |
1101 | } |
1102 | } break; |
1103 | case VIEW_TYPE_LEFT: { |
1104 | if (orthogonal) { |
1105 | name = TTR("Left Orthogonal" ); |
1106 | } else { |
1107 | name = TTR("Left Perspective" ); |
1108 | } |
1109 | } break; |
1110 | case VIEW_TYPE_RIGHT: { |
1111 | if (orthogonal) { |
1112 | name = TTR("Right Orthogonal" ); |
1113 | } else { |
1114 | name = TTR("Right Perspective" ); |
1115 | } |
1116 | } break; |
1117 | case VIEW_TYPE_FRONT: { |
1118 | if (orthogonal) { |
1119 | name = TTR("Front Orthogonal" ); |
1120 | } else { |
1121 | name = TTR("Front Perspective" ); |
1122 | } |
1123 | } break; |
1124 | case VIEW_TYPE_REAR: { |
1125 | if (orthogonal) { |
1126 | name = TTR("Rear Orthogonal" ); |
1127 | } else { |
1128 | name = TTR("Rear Perspective" ); |
1129 | } |
1130 | } break; |
1131 | } |
1132 | |
1133 | if (auto_orthogonal) { |
1134 | // TRANSLATORS: This will be appended to the view name when Auto Orthogonal is enabled. |
1135 | name += TTR(" [auto]" ); |
1136 | } |
1137 | |
1138 | view_menu->set_text(name); |
1139 | view_menu->reset_size(); |
1140 | } |
1141 | |
1142 | void Node3DEditorViewport::_compute_edit(const Point2 &p_point) { |
1143 | _edit.original_local = spatial_editor->are_local_coords_enabled(); |
1144 | _edit.click_ray = _get_ray(p_point); |
1145 | _edit.click_ray_pos = _get_ray_pos(p_point); |
1146 | _edit.plane = TRANSFORM_VIEW; |
1147 | spatial_editor->update_transform_gizmo(); |
1148 | _edit.center = spatial_editor->get_gizmo_transform().origin; |
1149 | |
1150 | Node3D *selected = spatial_editor->get_single_selected_node(); |
1151 | Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr; |
1152 | |
1153 | if (se && se->gizmo.is_valid()) { |
1154 | for (const KeyValue<int, Transform3D> &E : se->subgizmos) { |
1155 | int subgizmo_id = E.key; |
1156 | se->subgizmos[subgizmo_id] = se->gizmo->get_subgizmo_transform(subgizmo_id); |
1157 | } |
1158 | se->original_local = selected->get_transform(); |
1159 | se->original = selected->get_global_transform(); |
1160 | } else { |
1161 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
1162 | |
1163 | for (List<Node *>::Element *E = selection.front(); E; E = E->next()) { |
1164 | Node3D *sp = Object::cast_to<Node3D>(E->get()); |
1165 | if (!sp) { |
1166 | continue; |
1167 | } |
1168 | |
1169 | Node3DEditorSelectedItem *sel_item = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
1170 | |
1171 | if (!sel_item) { |
1172 | continue; |
1173 | } |
1174 | |
1175 | sel_item->original_local = sel_item->sp->get_local_gizmo_transform(); |
1176 | sel_item->original = sel_item->sp->get_global_gizmo_transform(); |
1177 | } |
1178 | } |
1179 | } |
1180 | |
1181 | static Key _get_key_modifier_setting(const String &p_property) { |
1182 | switch (EDITOR_GET(p_property).operator int()) { |
1183 | case 0: |
1184 | return Key::NONE; |
1185 | case 1: |
1186 | return Key::SHIFT; |
1187 | case 2: |
1188 | return Key::ALT; |
1189 | case 3: |
1190 | return Key::META; |
1191 | case 4: |
1192 | return Key::CTRL; |
1193 | } |
1194 | return Key::NONE; |
1195 | } |
1196 | |
1197 | static Key _get_key_modifier(Ref<InputEventWithModifiers> e) { |
1198 | if (e->is_shift_pressed()) { |
1199 | return Key::SHIFT; |
1200 | } |
1201 | if (e->is_alt_pressed()) { |
1202 | return Key::ALT; |
1203 | } |
1204 | if (e->is_ctrl_pressed()) { |
1205 | return Key::CTRL; |
1206 | } |
1207 | if (e->is_meta_pressed()) { |
1208 | return Key::META; |
1209 | } |
1210 | return Key::NONE; |
1211 | } |
1212 | |
1213 | bool Node3DEditorViewport::_transform_gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only) { |
1214 | if (!spatial_editor->is_gizmo_visible()) { |
1215 | return false; |
1216 | } |
1217 | if (get_selected_count() == 0) { |
1218 | if (p_highlight_only) { |
1219 | spatial_editor->select_gizmo_highlight_axis(-1); |
1220 | } |
1221 | return false; |
1222 | } |
1223 | |
1224 | Vector3 ray_pos = _get_ray_pos(p_screenpos); |
1225 | Vector3 ray = _get_ray(p_screenpos); |
1226 | |
1227 | Transform3D gt = spatial_editor->get_gizmo_transform(); |
1228 | |
1229 | if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE) { |
1230 | int col_axis = -1; |
1231 | real_t col_d = 1e20; |
1232 | |
1233 | for (int i = 0; i < 3; i++) { |
1234 | const Vector3 grabber_pos = gt.origin + gt.basis.get_column(i).normalized() * gizmo_scale * (GIZMO_ARROW_OFFSET + (GIZMO_ARROW_SIZE * 0.5)); |
1235 | const real_t grabber_radius = gizmo_scale * GIZMO_ARROW_SIZE; |
1236 | |
1237 | Vector3 r; |
1238 | |
1239 | if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) { |
1240 | const real_t d = r.distance_to(ray_pos); |
1241 | if (d < col_d) { |
1242 | col_d = d; |
1243 | col_axis = i; |
1244 | } |
1245 | } |
1246 | } |
1247 | |
1248 | bool is_plane_translate = false; |
1249 | // plane select |
1250 | if (col_axis == -1) { |
1251 | col_d = 1e20; |
1252 | |
1253 | for (int i = 0; i < 3; i++) { |
1254 | Vector3 ivec2 = gt.basis.get_column((i + 1) % 3).normalized(); |
1255 | Vector3 ivec3 = gt.basis.get_column((i + 2) % 3).normalized(); |
1256 | |
1257 | // Allow some tolerance to make the plane easier to click, |
1258 | // even if the click is actually slightly outside the plane. |
1259 | const Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gizmo_scale * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST * 0.6667); |
1260 | |
1261 | Vector3 r; |
1262 | Plane plane(gt.basis.get_column(i).normalized(), gt.origin); |
1263 | |
1264 | if (plane.intersects_ray(ray_pos, ray, &r)) { |
1265 | const real_t dist = r.distance_to(grabber_pos); |
1266 | // Allow some tolerance to make the plane easier to click, |
1267 | // even if the click is actually slightly outside the plane. |
1268 | if (dist < (gizmo_scale * GIZMO_PLANE_SIZE * 1.5)) { |
1269 | const real_t d = ray_pos.distance_to(r); |
1270 | if (d < col_d) { |
1271 | col_d = d; |
1272 | col_axis = i; |
1273 | |
1274 | is_plane_translate = true; |
1275 | } |
1276 | } |
1277 | } |
1278 | } |
1279 | } |
1280 | |
1281 | if (col_axis != -1) { |
1282 | if (p_highlight_only) { |
1283 | spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_translate ? 6 : 0)); |
1284 | |
1285 | } else { |
1286 | //handle plane translate |
1287 | _edit.mode = TRANSFORM_TRANSLATE; |
1288 | _compute_edit(p_screenpos); |
1289 | _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_translate ? 3 : 0)); |
1290 | } |
1291 | return true; |
1292 | } |
1293 | } |
1294 | |
1295 | if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE) { |
1296 | int col_axis = -1; |
1297 | |
1298 | Vector3 hit_position; |
1299 | Vector3 hit_normal; |
1300 | real_t ray_length = gt.origin.distance_to(ray_pos) + (GIZMO_CIRCLE_SIZE * gizmo_scale) * 4.0f; |
1301 | if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * ray_length, gt.origin, gizmo_scale * (GIZMO_CIRCLE_SIZE), &hit_position, &hit_normal)) { |
1302 | if (hit_normal.dot(_get_camera_normal()) < 0.05) { |
1303 | hit_position = gt.xform_inv(hit_position).abs(); |
1304 | int min_axis = hit_position.min_axis_index(); |
1305 | if (hit_position[min_axis] < gizmo_scale * GIZMO_RING_HALF_WIDTH) { |
1306 | col_axis = min_axis; |
1307 | } |
1308 | } |
1309 | } |
1310 | |
1311 | if (col_axis == -1) { |
1312 | float col_d = 1e20; |
1313 | |
1314 | for (int i = 0; i < 3; i++) { |
1315 | Plane plane(gt.basis.get_column(i).normalized(), gt.origin); |
1316 | Vector3 r; |
1317 | if (!plane.intersects_ray(ray_pos, ray, &r)) { |
1318 | continue; |
1319 | } |
1320 | |
1321 | const real_t dist = r.distance_to(gt.origin); |
1322 | const Vector3 r_dir = (r - gt.origin).normalized(); |
1323 | |
1324 | if (_get_camera_normal().dot(r_dir) <= 0.005) { |
1325 | if (dist > gizmo_scale * (GIZMO_CIRCLE_SIZE - GIZMO_RING_HALF_WIDTH) && dist < gizmo_scale * (GIZMO_CIRCLE_SIZE + GIZMO_RING_HALF_WIDTH)) { |
1326 | const real_t d = ray_pos.distance_to(r); |
1327 | if (d < col_d) { |
1328 | col_d = d; |
1329 | col_axis = i; |
1330 | } |
1331 | } |
1332 | } |
1333 | } |
1334 | } |
1335 | |
1336 | if (col_axis != -1) { |
1337 | if (p_highlight_only) { |
1338 | spatial_editor->select_gizmo_highlight_axis(col_axis + 3); |
1339 | } else { |
1340 | //handle rotate |
1341 | _edit.mode = TRANSFORM_ROTATE; |
1342 | _compute_edit(p_screenpos); |
1343 | _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis); |
1344 | } |
1345 | return true; |
1346 | } |
1347 | } |
1348 | |
1349 | if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE) { |
1350 | int col_axis = -1; |
1351 | float col_d = 1e20; |
1352 | |
1353 | for (int i = 0; i < 3; i++) { |
1354 | const Vector3 grabber_pos = gt.origin + gt.basis.get_column(i).normalized() * gizmo_scale * GIZMO_SCALE_OFFSET; |
1355 | const real_t grabber_radius = gizmo_scale * GIZMO_ARROW_SIZE; |
1356 | |
1357 | Vector3 r; |
1358 | |
1359 | if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) { |
1360 | const real_t d = r.distance_to(ray_pos); |
1361 | if (d < col_d) { |
1362 | col_d = d; |
1363 | col_axis = i; |
1364 | } |
1365 | } |
1366 | } |
1367 | |
1368 | bool is_plane_scale = false; |
1369 | // plane select |
1370 | if (col_axis == -1) { |
1371 | col_d = 1e20; |
1372 | |
1373 | for (int i = 0; i < 3; i++) { |
1374 | const Vector3 ivec2 = gt.basis.get_column((i + 1) % 3).normalized(); |
1375 | const Vector3 ivec3 = gt.basis.get_column((i + 2) % 3).normalized(); |
1376 | |
1377 | // Allow some tolerance to make the plane easier to click, |
1378 | // even if the click is actually slightly outside the plane. |
1379 | const Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gizmo_scale * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST * 0.6667); |
1380 | |
1381 | Vector3 r; |
1382 | Plane plane(gt.basis.get_column(i).normalized(), gt.origin); |
1383 | |
1384 | if (plane.intersects_ray(ray_pos, ray, &r)) { |
1385 | const real_t dist = r.distance_to(grabber_pos); |
1386 | // Allow some tolerance to make the plane easier to click, |
1387 | // even if the click is actually slightly outside the plane. |
1388 | if (dist < (gizmo_scale * GIZMO_PLANE_SIZE * 1.5)) { |
1389 | const real_t d = ray_pos.distance_to(r); |
1390 | if (d < col_d) { |
1391 | col_d = d; |
1392 | col_axis = i; |
1393 | |
1394 | is_plane_scale = true; |
1395 | } |
1396 | } |
1397 | } |
1398 | } |
1399 | } |
1400 | |
1401 | if (col_axis != -1) { |
1402 | if (p_highlight_only) { |
1403 | spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_scale ? 12 : 9)); |
1404 | |
1405 | } else { |
1406 | //handle scale |
1407 | _edit.mode = TRANSFORM_SCALE; |
1408 | _compute_edit(p_screenpos); |
1409 | _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_scale ? 3 : 0)); |
1410 | } |
1411 | return true; |
1412 | } |
1413 | } |
1414 | |
1415 | if (p_highlight_only) { |
1416 | spatial_editor->select_gizmo_highlight_axis(-1); |
1417 | } |
1418 | |
1419 | return false; |
1420 | } |
1421 | |
1422 | void Node3DEditorViewport::_transform_gizmo_apply(Node3D *p_node, const Transform3D &p_transform, bool p_local) { |
1423 | if (p_transform.basis.determinant() == 0) { |
1424 | return; |
1425 | } |
1426 | |
1427 | if (p_local) { |
1428 | p_node->set_transform(p_transform); |
1429 | } else { |
1430 | p_node->set_global_transform(p_transform); |
1431 | } |
1432 | } |
1433 | |
1434 | Transform3D Node3DEditorViewport::_compute_transform(TransformMode p_mode, const Transform3D &p_original, const Transform3D &p_original_local, Vector3 p_motion, double , bool p_local, bool p_orthogonal) { |
1435 | switch (p_mode) { |
1436 | case TRANSFORM_SCALE: { |
1437 | if (_edit.snap || spatial_editor->is_snap_enabled()) { |
1438 | p_motion.snap(Vector3(p_extra, p_extra, p_extra)); |
1439 | } |
1440 | Transform3D s; |
1441 | if (p_local) { |
1442 | s.basis = p_original_local.basis.scaled_local(p_motion + Vector3(1, 1, 1)); |
1443 | s.origin = p_original_local.origin; |
1444 | } else { |
1445 | s.basis.scale(p_motion + Vector3(1, 1, 1)); |
1446 | Transform3D base = Transform3D(Basis(), _edit.center); |
1447 | s = base * (s * (base.inverse() * p_original)); |
1448 | |
1449 | // Recalculate orthogonalized scale without moving origin. |
1450 | if (p_orthogonal) { |
1451 | s.basis = p_original.basis.scaled_orthogonal(p_motion + Vector3(1, 1, 1)); |
1452 | } |
1453 | } |
1454 | |
1455 | return s; |
1456 | } |
1457 | case TRANSFORM_TRANSLATE: { |
1458 | if (_edit.snap || spatial_editor->is_snap_enabled()) { |
1459 | p_motion.snap(Vector3(p_extra, p_extra, p_extra)); |
1460 | } |
1461 | |
1462 | if (p_local) { |
1463 | return p_original_local.translated_local(p_motion); |
1464 | } |
1465 | |
1466 | return p_original.translated(p_motion); |
1467 | } |
1468 | case TRANSFORM_ROTATE: { |
1469 | Transform3D r; |
1470 | |
1471 | if (p_local) { |
1472 | Vector3 axis = p_original_local.basis.xform(p_motion); |
1473 | r.basis = Basis(axis.normalized(), p_extra) * p_original_local.basis; |
1474 | r.origin = p_original_local.origin; |
1475 | } else { |
1476 | Basis local = p_original.basis * p_original_local.basis.inverse(); |
1477 | Vector3 axis = local.xform_inv(p_motion); |
1478 | r.basis = local * Basis(axis.normalized(), p_extra) * p_original_local.basis; |
1479 | r.origin = Basis(p_motion, p_extra).xform(p_original.origin - _edit.center) + _edit.center; |
1480 | } |
1481 | |
1482 | return r; |
1483 | } |
1484 | default: { |
1485 | ERR_FAIL_V_MSG(Transform3D(), "Invalid mode in '_compute_transform'" ); |
1486 | } |
1487 | } |
1488 | } |
1489 | |
1490 | void Node3DEditorViewport::_surface_mouse_enter() { |
1491 | if (!surface->has_focus() && (!get_viewport()->gui_get_focus_owner() || !get_viewport()->gui_get_focus_owner()->is_text_field())) { |
1492 | surface->grab_focus(); |
1493 | } |
1494 | } |
1495 | |
1496 | void Node3DEditorViewport::_surface_mouse_exit() { |
1497 | _remove_preview_node(); |
1498 | _reset_preview_material(); |
1499 | _remove_preview_material(); |
1500 | } |
1501 | |
1502 | void Node3DEditorViewport::_surface_focus_enter() { |
1503 | view_menu->set_disable_shortcuts(false); |
1504 | } |
1505 | |
1506 | void Node3DEditorViewport::_surface_focus_exit() { |
1507 | view_menu->set_disable_shortcuts(true); |
1508 | } |
1509 | |
1510 | bool Node3DEditorViewport ::_is_node_locked(const Node *p_node) { |
1511 | return p_node->get_meta("_edit_lock_" , false); |
1512 | } |
1513 | |
1514 | void Node3DEditorViewport::_list_select(Ref<InputEventMouseButton> b) { |
1515 | _find_items_at_pos(b->get_position(), selection_results, spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT); |
1516 | |
1517 | Node *scene = EditorNode::get_singleton()->get_edited_scene(); |
1518 | |
1519 | for (int i = 0; i < selection_results.size(); i++) { |
1520 | Node3D *item = selection_results[i].item; |
1521 | if (item != scene && item->get_owner() != scene && item != scene->get_deepest_editable_node(item)) { |
1522 | //invalid result |
1523 | selection_results.remove_at(i); |
1524 | i--; |
1525 | } |
1526 | } |
1527 | |
1528 | clicked_wants_append = b->is_shift_pressed(); |
1529 | |
1530 | if (selection_results.size() == 1) { |
1531 | clicked = selection_results[0].item->get_instance_id(); |
1532 | selection_results.clear(); |
1533 | |
1534 | if (clicked.is_valid()) { |
1535 | _select_clicked(spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT); |
1536 | } |
1537 | } else if (!selection_results.is_empty()) { |
1538 | NodePath root_path = get_tree()->get_edited_scene_root()->get_path(); |
1539 | StringName root_name = root_path.get_name(root_path.get_name_count() - 1); |
1540 | |
1541 | for (int i = 0; i < selection_results.size(); i++) { |
1542 | Node3D *spat = selection_results[i].item; |
1543 | |
1544 | Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(spat, "Node" ); |
1545 | |
1546 | String node_path = "/" + root_name + "/" + root_path.rel_path_to(spat->get_path()); |
1547 | |
1548 | int locked = 0; |
1549 | if (_is_node_locked(spat)) { |
1550 | locked = 1; |
1551 | } else { |
1552 | Node *ed_scene = EditorNode::get_singleton()->get_edited_scene(); |
1553 | Node *node = spat; |
1554 | |
1555 | while (node && node != ed_scene->get_parent()) { |
1556 | Node3D *selected_tmp = Object::cast_to<Node3D>(node); |
1557 | if (selected_tmp && node->has_meta("_edit_group_" )) { |
1558 | locked = 2; |
1559 | } |
1560 | node = node->get_parent(); |
1561 | } |
1562 | } |
1563 | |
1564 | String suffix; |
1565 | if (locked == 1) { |
1566 | suffix = " (" + TTR("Locked" ) + ")" ; |
1567 | } else if (locked == 2) { |
1568 | suffix = " (" + TTR("Grouped" ) + ")" ; |
1569 | } |
1570 | selection_menu->add_item((String)spat->get_name() + suffix); |
1571 | selection_menu->set_item_icon(i, icon); |
1572 | selection_menu->set_item_metadata(i, node_path); |
1573 | selection_menu->set_item_tooltip(i, String(spat->get_name()) + "\nType: " + spat->get_class() + "\nPath: " + node_path); |
1574 | } |
1575 | |
1576 | selection_results_menu = selection_results; |
1577 | selection_menu->set_position(get_screen_position() + b->get_position()); |
1578 | selection_menu->reset_size(); |
1579 | selection_menu->popup(); |
1580 | } |
1581 | } |
1582 | |
1583 | // This is only active during instant transforms, |
1584 | // to capture and wrap mouse events outside the control. |
1585 | void Node3DEditorViewport::input(const Ref<InputEvent> &p_event) { |
1586 | ERR_FAIL_COND(!_edit.instant); |
1587 | Ref<InputEventMouseMotion> m = p_event; |
1588 | |
1589 | if (m.is_valid()) { |
1590 | if (_edit.mode == TRANSFORM_ROTATE) { |
1591 | _edit.mouse_pos = m->get_position(); // rotate should not wrap |
1592 | } else { |
1593 | _edit.mouse_pos += _get_warped_mouse_motion(p_event); |
1594 | } |
1595 | update_transform(_get_key_modifier(m) == Key::SHIFT); |
1596 | } |
1597 | } |
1598 | |
1599 | void Node3DEditorViewport::_sinput(const Ref<InputEvent> &p_event) { |
1600 | if (previewing) { |
1601 | return; //do NONE |
1602 | } |
1603 | |
1604 | EditorPlugin::AfterGUIInput after = EditorPlugin::AFTER_GUI_INPUT_PASS; |
1605 | { |
1606 | EditorNode *en = EditorNode::get_singleton(); |
1607 | EditorPluginList *force_input_forwarding_list = en->get_editor_plugins_force_input_forwarding(); |
1608 | if (!force_input_forwarding_list->is_empty()) { |
1609 | EditorPlugin::AfterGUIInput discard = force_input_forwarding_list->forward_3d_gui_input(camera, p_event, true); |
1610 | if (discard == EditorPlugin::AFTER_GUI_INPUT_STOP) { |
1611 | return; |
1612 | } |
1613 | if (discard == EditorPlugin::AFTER_GUI_INPUT_CUSTOM) { |
1614 | after = EditorPlugin::AFTER_GUI_INPUT_CUSTOM; |
1615 | } |
1616 | } |
1617 | } |
1618 | { |
1619 | EditorNode *en = EditorNode::get_singleton(); |
1620 | EditorPluginList *over_plugin_list = en->get_editor_plugins_over(); |
1621 | if (!over_plugin_list->is_empty()) { |
1622 | EditorPlugin::AfterGUIInput discard = over_plugin_list->forward_3d_gui_input(camera, p_event, false); |
1623 | if (discard == EditorPlugin::AFTER_GUI_INPUT_STOP) { |
1624 | return; |
1625 | } |
1626 | if (discard == EditorPlugin::AFTER_GUI_INPUT_CUSTOM) { |
1627 | after = EditorPlugin::AFTER_GUI_INPUT_CUSTOM; |
1628 | } |
1629 | } |
1630 | } |
1631 | |
1632 | Ref<InputEventMouseButton> b = p_event; |
1633 | |
1634 | if (b.is_valid()) { |
1635 | emit_signal(SNAME("clicked" ), this); |
1636 | |
1637 | const real_t zoom_factor = 1 + (ZOOM_FREELOOK_MULTIPLIER - 1) * b->get_factor(); |
1638 | switch (b->get_button_index()) { |
1639 | case MouseButton::WHEEL_UP: { |
1640 | if (is_freelook_active()) { |
1641 | scale_freelook_speed(zoom_factor); |
1642 | } else { |
1643 | scale_cursor_distance(1.0 / zoom_factor); |
1644 | } |
1645 | } break; |
1646 | case MouseButton::WHEEL_DOWN: { |
1647 | if (is_freelook_active()) { |
1648 | scale_freelook_speed(1.0 / zoom_factor); |
1649 | } else { |
1650 | scale_cursor_distance(zoom_factor); |
1651 | } |
1652 | } break; |
1653 | case MouseButton::RIGHT: { |
1654 | NavigationScheme nav_scheme = (NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme" ).operator int(); |
1655 | |
1656 | if (b->is_pressed() && _edit.gizmo.is_valid()) { |
1657 | //restore |
1658 | _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_initial_value, true); |
1659 | _edit.gizmo = Ref<EditorNode3DGizmo>(); |
1660 | } |
1661 | |
1662 | if (_edit.mode == TRANSFORM_NONE && b->is_pressed()) { |
1663 | if (b->is_alt_pressed()) { |
1664 | if (nav_scheme == NAVIGATION_MAYA) { |
1665 | break; |
1666 | } |
1667 | |
1668 | _list_select(b); |
1669 | return; |
1670 | } |
1671 | } |
1672 | |
1673 | if (_edit.mode != TRANSFORM_NONE && b->is_pressed()) { |
1674 | cancel_transform(); |
1675 | break; |
1676 | } |
1677 | |
1678 | if (b->is_pressed()) { |
1679 | const Key mod = _get_key_modifier(b); |
1680 | if (!orthogonal) { |
1681 | if (mod == _get_key_modifier_setting("editors/3d/freelook/freelook_activation_modifier" )) { |
1682 | set_freelook_active(true); |
1683 | } |
1684 | } |
1685 | } else { |
1686 | set_freelook_active(false); |
1687 | } |
1688 | |
1689 | if (freelook_active && !surface->has_focus()) { |
1690 | // Focus usually doesn't trigger on right-click, but in case of freelook it should, |
1691 | // otherwise using keyboard navigation would misbehave |
1692 | surface->grab_focus(); |
1693 | } |
1694 | |
1695 | } break; |
1696 | case MouseButton::MIDDLE: { |
1697 | if (b->is_pressed() && _edit.mode != TRANSFORM_NONE) { |
1698 | switch (_edit.plane) { |
1699 | case TRANSFORM_VIEW: { |
1700 | _edit.plane = TRANSFORM_X_AXIS; |
1701 | set_message(TTR("X-Axis Transform." ), 2); |
1702 | view_type = VIEW_TYPE_USER; |
1703 | _update_name(); |
1704 | } break; |
1705 | case TRANSFORM_X_AXIS: { |
1706 | _edit.plane = TRANSFORM_Y_AXIS; |
1707 | set_message(TTR("Y-Axis Transform." ), 2); |
1708 | |
1709 | } break; |
1710 | case TRANSFORM_Y_AXIS: { |
1711 | _edit.plane = TRANSFORM_Z_AXIS; |
1712 | set_message(TTR("Z-Axis Transform." ), 2); |
1713 | |
1714 | } break; |
1715 | case TRANSFORM_Z_AXIS: { |
1716 | _edit.plane = TRANSFORM_VIEW; |
1717 | // TRANSLATORS: This refers to the transform of the view plane. |
1718 | set_message(TTR("View Plane Transform." ), 2); |
1719 | |
1720 | } break; |
1721 | case TRANSFORM_YZ: |
1722 | case TRANSFORM_XZ: |
1723 | case TRANSFORM_XY: { |
1724 | } break; |
1725 | } |
1726 | } |
1727 | } break; |
1728 | case MouseButton::LEFT: { |
1729 | if (b->is_pressed()) { |
1730 | clicked_wants_append = b->is_shift_pressed(); |
1731 | |
1732 | if (_edit.mode != TRANSFORM_NONE && _edit.instant) { |
1733 | commit_transform(); |
1734 | break; // just commit the edit, stop processing the event so we don't deselect the object |
1735 | } |
1736 | NavigationScheme nav_scheme = (NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme" ).operator int(); |
1737 | if ((nav_scheme == NAVIGATION_MAYA || nav_scheme == NAVIGATION_MODO) && b->is_alt_pressed()) { |
1738 | break; |
1739 | } |
1740 | |
1741 | if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_LIST_SELECT) { |
1742 | _list_select(b); |
1743 | break; |
1744 | } |
1745 | |
1746 | _edit.mouse_pos = b->get_position(); |
1747 | _edit.original_mouse_pos = b->get_position(); |
1748 | _edit.snap = spatial_editor->is_snap_enabled(); |
1749 | _edit.mode = TRANSFORM_NONE; |
1750 | _edit.original = spatial_editor->get_gizmo_transform(); // To prevent to break when flipping with scale. |
1751 | |
1752 | bool can_select_gizmos = spatial_editor->get_single_selected_node(); |
1753 | |
1754 | { |
1755 | int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS); |
1756 | can_select_gizmos = can_select_gizmos && view_menu->get_popup()->is_item_checked(idx); |
1757 | } |
1758 | |
1759 | // Gizmo handles |
1760 | if (can_select_gizmos) { |
1761 | Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos(); |
1762 | |
1763 | bool intersected_handle = false; |
1764 | for (int i = 0; i < gizmos.size(); i++) { |
1765 | Ref<EditorNode3DGizmo> seg = gizmos[i]; |
1766 | |
1767 | if ((!seg.is_valid())) { |
1768 | continue; |
1769 | } |
1770 | |
1771 | int gizmo_handle = -1; |
1772 | bool gizmo_secondary = false; |
1773 | seg->handles_intersect_ray(camera, _edit.mouse_pos, b->is_shift_pressed(), gizmo_handle, gizmo_secondary); |
1774 | if (gizmo_handle != -1) { |
1775 | _edit.gizmo = seg; |
1776 | seg->begin_handle_action(gizmo_handle, gizmo_secondary); |
1777 | _edit.gizmo_handle = gizmo_handle; |
1778 | _edit.gizmo_handle_secondary = gizmo_secondary; |
1779 | _edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle, gizmo_secondary); |
1780 | intersected_handle = true; |
1781 | break; |
1782 | } |
1783 | } |
1784 | |
1785 | if (intersected_handle) { |
1786 | break; |
1787 | } |
1788 | } |
1789 | |
1790 | // Transform gizmo |
1791 | if (_transform_gizmo_select(_edit.mouse_pos)) { |
1792 | break; |
1793 | } |
1794 | |
1795 | // Subgizmos |
1796 | if (can_select_gizmos) { |
1797 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(spatial_editor->get_single_selected_node()); |
1798 | Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos(); |
1799 | |
1800 | bool intersected_subgizmo = false; |
1801 | for (int i = 0; i < gizmos.size(); i++) { |
1802 | Ref<EditorNode3DGizmo> seg = gizmos[i]; |
1803 | |
1804 | if ((!seg.is_valid())) { |
1805 | continue; |
1806 | } |
1807 | |
1808 | int subgizmo_id = seg->subgizmos_intersect_ray(camera, _edit.mouse_pos); |
1809 | if (subgizmo_id != -1) { |
1810 | ERR_CONTINUE(!se); |
1811 | if (b->is_shift_pressed()) { |
1812 | if (se->subgizmos.has(subgizmo_id)) { |
1813 | se->subgizmos.erase(subgizmo_id); |
1814 | } else { |
1815 | se->subgizmos.insert(subgizmo_id, seg->get_subgizmo_transform(subgizmo_id)); |
1816 | } |
1817 | } else { |
1818 | se->subgizmos.clear(); |
1819 | se->subgizmos.insert(subgizmo_id, seg->get_subgizmo_transform(subgizmo_id)); |
1820 | } |
1821 | |
1822 | if (se->subgizmos.is_empty()) { |
1823 | se->gizmo = Ref<EditorNode3DGizmo>(); |
1824 | } else { |
1825 | se->gizmo = seg; |
1826 | } |
1827 | |
1828 | seg->redraw(); |
1829 | spatial_editor->update_transform_gizmo(); |
1830 | intersected_subgizmo = true; |
1831 | break; |
1832 | } |
1833 | } |
1834 | |
1835 | if (intersected_subgizmo) { |
1836 | break; |
1837 | } |
1838 | } |
1839 | |
1840 | clicked = ObjectID(); |
1841 | |
1842 | if ((spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT && b->is_command_or_control_pressed()) || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE) { |
1843 | begin_transform(TRANSFORM_ROTATE, false); |
1844 | break; |
1845 | } |
1846 | |
1847 | if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE) { |
1848 | begin_transform(TRANSFORM_TRANSLATE, false); |
1849 | break; |
1850 | } |
1851 | |
1852 | if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE) { |
1853 | begin_transform(TRANSFORM_SCALE, false); |
1854 | break; |
1855 | } |
1856 | |
1857 | if (after != EditorPlugin::AFTER_GUI_INPUT_CUSTOM) { |
1858 | //clicking is always deferred to either move or release |
1859 | clicked = _select_ray(b->get_position()); |
1860 | selection_in_progress = true; |
1861 | |
1862 | if (clicked.is_null()) { |
1863 | //default to regionselect |
1864 | cursor.region_select = true; |
1865 | cursor.region_begin = b->get_position(); |
1866 | cursor.region_end = b->get_position(); |
1867 | } |
1868 | } |
1869 | |
1870 | surface->queue_redraw(); |
1871 | } else { |
1872 | if (_edit.gizmo.is_valid()) { |
1873 | _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_initial_value, false); |
1874 | _edit.gizmo = Ref<EditorNode3DGizmo>(); |
1875 | break; |
1876 | } |
1877 | |
1878 | if (after != EditorPlugin::AFTER_GUI_INPUT_CUSTOM) { |
1879 | selection_in_progress = false; |
1880 | |
1881 | if (clicked.is_valid()) { |
1882 | _select_clicked(false); |
1883 | } |
1884 | |
1885 | if (cursor.region_select) { |
1886 | _select_region(); |
1887 | cursor.region_select = false; |
1888 | surface->queue_redraw(); |
1889 | } |
1890 | } |
1891 | |
1892 | if (_edit.mode != TRANSFORM_NONE) { |
1893 | Node3D *selected = spatial_editor->get_single_selected_node(); |
1894 | Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr; |
1895 | |
1896 | if (se && se->gizmo.is_valid()) { |
1897 | Vector<int> ids; |
1898 | Vector<Transform3D> restore; |
1899 | |
1900 | for (const KeyValue<int, Transform3D> &GE : se->subgizmos) { |
1901 | ids.push_back(GE.key); |
1902 | restore.push_back(GE.value); |
1903 | } |
1904 | |
1905 | se->gizmo->commit_subgizmos(ids, restore, false); |
1906 | } else { |
1907 | commit_transform(); |
1908 | } |
1909 | _edit.mode = TRANSFORM_NONE; |
1910 | set_message("" ); |
1911 | spatial_editor->update_transform_gizmo(); |
1912 | } |
1913 | surface->queue_redraw(); |
1914 | } |
1915 | |
1916 | } break; |
1917 | default: |
1918 | break; |
1919 | } |
1920 | } |
1921 | |
1922 | Ref<InputEventMouseMotion> m = p_event; |
1923 | |
1924 | // Instant transforms process mouse motion in input() to handle wrapping. |
1925 | if (m.is_valid() && !_edit.instant) { |
1926 | _edit.mouse_pos = m->get_position(); |
1927 | |
1928 | if (spatial_editor->get_single_selected_node()) { |
1929 | Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos(); |
1930 | |
1931 | Ref<EditorNode3DGizmo> found_gizmo; |
1932 | int found_handle = -1; |
1933 | bool found_handle_secondary = false; |
1934 | |
1935 | for (int i = 0; i < gizmos.size(); i++) { |
1936 | Ref<EditorNode3DGizmo> seg = gizmos[i]; |
1937 | if (!seg.is_valid()) { |
1938 | continue; |
1939 | } |
1940 | |
1941 | seg->handles_intersect_ray(camera, _edit.mouse_pos, false, found_handle, found_handle_secondary); |
1942 | |
1943 | if (found_handle != -1) { |
1944 | found_gizmo = seg; |
1945 | break; |
1946 | } |
1947 | } |
1948 | |
1949 | if (found_gizmo.is_valid()) { |
1950 | spatial_editor->select_gizmo_highlight_axis(-1); |
1951 | } |
1952 | |
1953 | bool current_hover_handle_secondary = false; |
1954 | int curreny_hover_handle = spatial_editor->get_current_hover_gizmo_handle(current_hover_handle_secondary); |
1955 | if (found_gizmo != spatial_editor->get_current_hover_gizmo() || found_handle != curreny_hover_handle || found_handle_secondary != current_hover_handle_secondary) { |
1956 | spatial_editor->set_current_hover_gizmo(found_gizmo); |
1957 | spatial_editor->set_current_hover_gizmo_handle(found_handle, found_handle_secondary); |
1958 | spatial_editor->get_single_selected_node()->update_gizmos(); |
1959 | } |
1960 | } |
1961 | |
1962 | if (spatial_editor->get_current_hover_gizmo().is_null() && !m->get_button_mask().has_flag(MouseButtonMask::LEFT) && !_edit.gizmo.is_valid()) { |
1963 | _transform_gizmo_select(_edit.mouse_pos, true); |
1964 | } |
1965 | |
1966 | NavigationScheme nav_scheme = (NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme" ).operator int(); |
1967 | NavigationMode nav_mode = NAVIGATION_NONE; |
1968 | |
1969 | if (_edit.gizmo.is_valid()) { |
1970 | _edit.gizmo->set_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, camera, m->get_position()); |
1971 | Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle, _edit.gizmo_handle_secondary); |
1972 | String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle, _edit.gizmo_handle_secondary); |
1973 | set_message(n + ": " + String(v)); |
1974 | |
1975 | } else if (m->get_button_mask().has_flag(MouseButtonMask::LEFT)) { |
1976 | if (nav_scheme == NAVIGATION_MAYA && m->is_alt_pressed()) { |
1977 | nav_mode = NAVIGATION_ORBIT; |
1978 | } else if (nav_scheme == NAVIGATION_MODO && m->is_alt_pressed() && m->is_shift_pressed()) { |
1979 | nav_mode = NAVIGATION_PAN; |
1980 | } else if (nav_scheme == NAVIGATION_MODO && m->is_alt_pressed() && m->is_ctrl_pressed()) { |
1981 | nav_mode = NAVIGATION_ZOOM; |
1982 | } else if (nav_scheme == NAVIGATION_MODO && m->is_alt_pressed()) { |
1983 | nav_mode = NAVIGATION_ORBIT; |
1984 | } else { |
1985 | const bool movement_threshold_passed = _edit.original_mouse_pos.distance_to(_edit.mouse_pos) > 8 * EDSCALE; |
1986 | |
1987 | // enable region-select if nothing has been selected yet or multi-select (shift key) is active |
1988 | if (selection_in_progress && movement_threshold_passed) { |
1989 | if (get_selected_count() == 0 || clicked_wants_append) { |
1990 | cursor.region_select = true; |
1991 | cursor.region_begin = _edit.original_mouse_pos; |
1992 | clicked = ObjectID(); |
1993 | } |
1994 | } |
1995 | |
1996 | if (cursor.region_select) { |
1997 | cursor.region_end = m->get_position(); |
1998 | surface->queue_redraw(); |
1999 | return; |
2000 | } |
2001 | |
2002 | if (clicked.is_valid() && movement_threshold_passed) { |
2003 | _compute_edit(_edit.original_mouse_pos); |
2004 | clicked = ObjectID(); |
2005 | _edit.mode = TRANSFORM_TRANSLATE; |
2006 | } |
2007 | |
2008 | if (_edit.mode == TRANSFORM_NONE || _edit.numeric_input != 0 || _edit.numeric_next_decimal != 0) { |
2009 | return; |
2010 | } |
2011 | |
2012 | update_transform(_get_key_modifier(m) == Key::SHIFT); |
2013 | } |
2014 | } else if (m->get_button_mask().has_flag(MouseButtonMask::RIGHT) || freelook_active) { |
2015 | if (nav_scheme == NAVIGATION_MAYA && m->is_alt_pressed()) { |
2016 | nav_mode = NAVIGATION_ZOOM; |
2017 | } else if (freelook_active) { |
2018 | nav_mode = NAVIGATION_LOOK; |
2019 | } else if (orthogonal) { |
2020 | nav_mode = NAVIGATION_PAN; |
2021 | } |
2022 | |
2023 | } else if (m->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) { |
2024 | const Key mod = _get_key_modifier(m); |
2025 | if (nav_scheme == NAVIGATION_GODOT) { |
2026 | if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier" )) { |
2027 | nav_mode = NAVIGATION_PAN; |
2028 | } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier" )) { |
2029 | nav_mode = NAVIGATION_ZOOM; |
2030 | } else if (mod == Key::ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier" )) { |
2031 | // Always allow Alt as a modifier to better support graphic tablets. |
2032 | nav_mode = NAVIGATION_ORBIT; |
2033 | } |
2034 | } else if (nav_scheme == NAVIGATION_MAYA) { |
2035 | if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier" )) { |
2036 | nav_mode = NAVIGATION_PAN; |
2037 | } |
2038 | } |
2039 | } else if (EDITOR_GET("editors/3d/navigation/emulate_3_button_mouse" )) { |
2040 | // Handle trackpad (no external mouse) use case |
2041 | const Key mod = _get_key_modifier(m); |
2042 | |
2043 | if (mod != Key::NONE) { |
2044 | if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier" )) { |
2045 | nav_mode = NAVIGATION_PAN; |
2046 | } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier" )) { |
2047 | nav_mode = NAVIGATION_ZOOM; |
2048 | } else if (mod == Key::ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier" )) { |
2049 | // Always allow Alt as a modifier to better support graphic tablets. |
2050 | nav_mode = NAVIGATION_ORBIT; |
2051 | } |
2052 | } |
2053 | } |
2054 | |
2055 | switch (nav_mode) { |
2056 | case NAVIGATION_PAN: { |
2057 | _nav_pan(m, _get_warped_mouse_motion(m)); |
2058 | |
2059 | } break; |
2060 | |
2061 | case NAVIGATION_ZOOM: { |
2062 | _nav_zoom(m, m->get_relative()); |
2063 | |
2064 | } break; |
2065 | |
2066 | case NAVIGATION_ORBIT: { |
2067 | _nav_orbit(m, _get_warped_mouse_motion(m)); |
2068 | |
2069 | } break; |
2070 | |
2071 | case NAVIGATION_LOOK: { |
2072 | _nav_look(m, _get_warped_mouse_motion(m)); |
2073 | |
2074 | } break; |
2075 | |
2076 | default: { |
2077 | } |
2078 | } |
2079 | } |
2080 | |
2081 | Ref<InputEventMagnifyGesture> magnify_gesture = p_event; |
2082 | if (magnify_gesture.is_valid()) { |
2083 | if (is_freelook_active()) { |
2084 | scale_freelook_speed(magnify_gesture->get_factor()); |
2085 | } else { |
2086 | scale_cursor_distance(1.0 / magnify_gesture->get_factor()); |
2087 | } |
2088 | } |
2089 | |
2090 | Ref<InputEventPanGesture> pan_gesture = p_event; |
2091 | if (pan_gesture.is_valid()) { |
2092 | NavigationScheme nav_scheme = (NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme" ).operator int(); |
2093 | NavigationMode nav_mode = NAVIGATION_NONE; |
2094 | |
2095 | if (nav_scheme == NAVIGATION_GODOT) { |
2096 | const Key mod = _get_key_modifier(pan_gesture); |
2097 | |
2098 | if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier" )) { |
2099 | nav_mode = NAVIGATION_PAN; |
2100 | } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier" )) { |
2101 | nav_mode = NAVIGATION_ZOOM; |
2102 | } else if (mod == Key::ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier" )) { |
2103 | // Always allow Alt as a modifier to better support graphic tablets. |
2104 | nav_mode = NAVIGATION_ORBIT; |
2105 | } |
2106 | |
2107 | } else if (nav_scheme == NAVIGATION_MAYA) { |
2108 | if (pan_gesture->is_alt_pressed()) { |
2109 | nav_mode = NAVIGATION_PAN; |
2110 | } |
2111 | } |
2112 | |
2113 | switch (nav_mode) { |
2114 | case NAVIGATION_PAN: { |
2115 | _nav_pan(pan_gesture, -pan_gesture->get_delta()); |
2116 | |
2117 | } break; |
2118 | |
2119 | case NAVIGATION_ZOOM: { |
2120 | _nav_zoom(pan_gesture, pan_gesture->get_delta()); |
2121 | |
2122 | } break; |
2123 | |
2124 | case NAVIGATION_ORBIT: { |
2125 | _nav_orbit(pan_gesture, -pan_gesture->get_delta()); |
2126 | |
2127 | } break; |
2128 | |
2129 | case NAVIGATION_LOOK: { |
2130 | _nav_look(pan_gesture, pan_gesture->get_delta()); |
2131 | |
2132 | } break; |
2133 | |
2134 | default: { |
2135 | } |
2136 | } |
2137 | } |
2138 | |
2139 | Ref<InputEventKey> k = p_event; |
2140 | |
2141 | if (k.is_valid()) { |
2142 | if (!k->is_pressed()) { |
2143 | return; |
2144 | } |
2145 | |
2146 | if (_edit.instant) { |
2147 | // In a Blender-style transform, numbers set the magnitude of the transform. |
2148 | // E.g. pressing g4.5x means "translate 4.5 units along the X axis". |
2149 | // Use the Unicode value because we care about the text, not the actual keycode. |
2150 | // This ensures numbers work consistently across different keyboard language layouts. |
2151 | bool processed = true; |
2152 | Key key = k->get_physical_keycode(); |
2153 | char32_t unicode = k->get_unicode(); |
2154 | if (unicode >= '0' && unicode <= '9') { |
2155 | uint32_t value = uint32_t(unicode - Key::KEY_0); |
2156 | if (_edit.numeric_next_decimal < 0) { |
2157 | _edit.numeric_input = _edit.numeric_input + value * Math::pow(10.0, _edit.numeric_next_decimal--); |
2158 | } else { |
2159 | _edit.numeric_input = _edit.numeric_input * 10 + value; |
2160 | } |
2161 | update_transform_numeric(); |
2162 | } else if (unicode == '-') { |
2163 | _edit.numeric_negate = !_edit.numeric_negate; |
2164 | update_transform_numeric(); |
2165 | } else if (unicode == '.') { |
2166 | if (_edit.numeric_next_decimal == 0) { |
2167 | _edit.numeric_next_decimal = -1; |
2168 | } |
2169 | } else if (key == Key::ENTER || key == Key::KP_ENTER || key == Key::SPACE) { |
2170 | commit_transform(); |
2171 | } else { |
2172 | processed = false; |
2173 | } |
2174 | |
2175 | if (processed) { |
2176 | // Ignore mouse inputs once we receive a numeric input. |
2177 | set_process_input(false); |
2178 | accept_event(); |
2179 | return; |
2180 | } |
2181 | } |
2182 | |
2183 | if (EDITOR_GET("editors/3d/navigation/emulate_numpad" )) { |
2184 | const Key code = k->get_physical_keycode(); |
2185 | if (code >= Key::KEY_0 && code <= Key::KEY_9) { |
2186 | k->set_keycode(code - Key::KEY_0 + Key::KP_0); |
2187 | } |
2188 | } |
2189 | |
2190 | if (_edit.mode == TRANSFORM_NONE) { |
2191 | if (_edit.gizmo.is_valid() && (k->get_keycode() == Key::ESCAPE || k->get_keycode() == Key::BACKSPACE)) { |
2192 | // Restore. |
2193 | _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_initial_value, true); |
2194 | _edit.gizmo = Ref<EditorNode3DGizmo>(); |
2195 | } |
2196 | if (k->get_keycode() == Key::ESCAPE && !cursor.region_select) { |
2197 | _clear_selected(); |
2198 | return; |
2199 | } |
2200 | } else { |
2201 | // We're actively transforming, handle keys specially |
2202 | TransformPlane new_plane = TRANSFORM_VIEW; |
2203 | if (ED_IS_SHORTCUT("spatial_editor/lock_transform_x" , p_event)) { |
2204 | new_plane = TRANSFORM_X_AXIS; |
2205 | } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_y" , p_event)) { |
2206 | new_plane = TRANSFORM_Y_AXIS; |
2207 | } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_z" , p_event)) { |
2208 | new_plane = TRANSFORM_Z_AXIS; |
2209 | } else if (_edit.mode != TRANSFORM_ROTATE) { // rotating on a plane doesn't make sense |
2210 | if (ED_IS_SHORTCUT("spatial_editor/lock_transform_yz" , p_event)) { |
2211 | new_plane = TRANSFORM_YZ; |
2212 | } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xz" , p_event)) { |
2213 | new_plane = TRANSFORM_XZ; |
2214 | } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xy" , p_event)) { |
2215 | new_plane = TRANSFORM_XY; |
2216 | } |
2217 | } |
2218 | |
2219 | if (new_plane != TRANSFORM_VIEW) { |
2220 | if (new_plane != _edit.plane) { |
2221 | // lock me once and get a global constraint |
2222 | _edit.plane = new_plane; |
2223 | spatial_editor->set_local_coords_enabled(false); |
2224 | } else if (!spatial_editor->are_local_coords_enabled()) { |
2225 | // lock me twice and get a local constraint |
2226 | spatial_editor->set_local_coords_enabled(true); |
2227 | } else { |
2228 | // lock me thrice and we're back where we started |
2229 | _edit.plane = TRANSFORM_VIEW; |
2230 | spatial_editor->set_local_coords_enabled(false); |
2231 | } |
2232 | if (_edit.numeric_input != 0 || _edit.numeric_next_decimal != 0) { |
2233 | update_transform_numeric(); |
2234 | } else { |
2235 | update_transform(Input::get_singleton()->is_key_pressed(Key::SHIFT)); |
2236 | } |
2237 | accept_event(); |
2238 | return; |
2239 | } |
2240 | } |
2241 | if (ED_IS_SHORTCUT("spatial_editor/snap" , p_event)) { |
2242 | if (_edit.mode != TRANSFORM_NONE) { |
2243 | _edit.snap = !_edit.snap; |
2244 | } |
2245 | } |
2246 | if (ED_IS_SHORTCUT("spatial_editor/bottom_view" , p_event)) { |
2247 | _menu_option(VIEW_BOTTOM); |
2248 | } |
2249 | if (ED_IS_SHORTCUT("spatial_editor/top_view" , p_event)) { |
2250 | _menu_option(VIEW_TOP); |
2251 | } |
2252 | if (ED_IS_SHORTCUT("spatial_editor/rear_view" , p_event)) { |
2253 | _menu_option(VIEW_REAR); |
2254 | } |
2255 | if (ED_IS_SHORTCUT("spatial_editor/front_view" , p_event)) { |
2256 | _menu_option(VIEW_FRONT); |
2257 | } |
2258 | if (ED_IS_SHORTCUT("spatial_editor/left_view" , p_event)) { |
2259 | _menu_option(VIEW_LEFT); |
2260 | } |
2261 | if (ED_IS_SHORTCUT("spatial_editor/right_view" , p_event)) { |
2262 | _menu_option(VIEW_RIGHT); |
2263 | } |
2264 | if (ED_IS_SHORTCUT("spatial_editor/orbit_view_down" , p_event)) { |
2265 | // Clamp rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented. |
2266 | cursor.x_rot = CLAMP(cursor.x_rot - Math_PI / 12.0, -1.57, 1.57); |
2267 | view_type = VIEW_TYPE_USER; |
2268 | _update_name(); |
2269 | } |
2270 | if (ED_IS_SHORTCUT("spatial_editor/orbit_view_up" , p_event)) { |
2271 | // Clamp rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented. |
2272 | cursor.x_rot = CLAMP(cursor.x_rot + Math_PI / 12.0, -1.57, 1.57); |
2273 | view_type = VIEW_TYPE_USER; |
2274 | _update_name(); |
2275 | } |
2276 | if (ED_IS_SHORTCUT("spatial_editor/orbit_view_right" , p_event)) { |
2277 | cursor.y_rot -= Math_PI / 12.0; |
2278 | view_type = VIEW_TYPE_USER; |
2279 | _update_name(); |
2280 | } |
2281 | if (ED_IS_SHORTCUT("spatial_editor/orbit_view_left" , p_event)) { |
2282 | cursor.y_rot += Math_PI / 12.0; |
2283 | view_type = VIEW_TYPE_USER; |
2284 | _update_name(); |
2285 | } |
2286 | if (ED_IS_SHORTCUT("spatial_editor/orbit_view_180" , p_event)) { |
2287 | cursor.y_rot += Math_PI; |
2288 | view_type = VIEW_TYPE_USER; |
2289 | _update_name(); |
2290 | } |
2291 | if (ED_IS_SHORTCUT("spatial_editor/focus_origin" , p_event)) { |
2292 | _menu_option(VIEW_CENTER_TO_ORIGIN); |
2293 | } |
2294 | if (ED_IS_SHORTCUT("spatial_editor/focus_selection" , p_event)) { |
2295 | _menu_option(VIEW_CENTER_TO_SELECTION); |
2296 | } |
2297 | if (ED_IS_SHORTCUT("spatial_editor/align_transform_with_view" , p_event)) { |
2298 | _menu_option(VIEW_ALIGN_TRANSFORM_WITH_VIEW); |
2299 | } |
2300 | if (ED_IS_SHORTCUT("spatial_editor/align_rotation_with_view" , p_event)) { |
2301 | _menu_option(VIEW_ALIGN_ROTATION_WITH_VIEW); |
2302 | } |
2303 | if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key" , p_event)) { |
2304 | if (!get_selected_count() || _edit.mode != TRANSFORM_NONE) { |
2305 | return; |
2306 | } |
2307 | |
2308 | if (!AnimationPlayerEditor::get_singleton()->get_track_editor()->has_keying()) { |
2309 | set_message(TTR("Keying is disabled (no key inserted)." )); |
2310 | return; |
2311 | } |
2312 | |
2313 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
2314 | |
2315 | for (Node *E : selection) { |
2316 | Node3D *sp = Object::cast_to<Node3D>(E); |
2317 | if (!sp) { |
2318 | continue; |
2319 | } |
2320 | |
2321 | spatial_editor->emit_signal(SNAME("transform_key_request" ), sp, "" , sp->get_transform()); |
2322 | } |
2323 | |
2324 | set_message(TTR("Animation Key Inserted." )); |
2325 | } |
2326 | if (ED_IS_SHORTCUT("spatial_editor/cancel_transform" , p_event) && _edit.mode != TRANSFORM_NONE) { |
2327 | cancel_transform(); |
2328 | } |
2329 | if (!is_freelook_active()) { |
2330 | if (ED_IS_SHORTCUT("spatial_editor/instant_translate" , p_event)) { |
2331 | begin_transform(TRANSFORM_TRANSLATE, true); |
2332 | } |
2333 | if (ED_IS_SHORTCUT("spatial_editor/instant_rotate" , p_event)) { |
2334 | begin_transform(TRANSFORM_ROTATE, true); |
2335 | } |
2336 | if (ED_IS_SHORTCUT("spatial_editor/instant_scale" , p_event)) { |
2337 | begin_transform(TRANSFORM_SCALE, true); |
2338 | } |
2339 | } |
2340 | |
2341 | // Freelook doesn't work in orthogonal mode. |
2342 | if (!orthogonal && ED_IS_SHORTCUT("spatial_editor/freelook_toggle" , p_event)) { |
2343 | set_freelook_active(!is_freelook_active()); |
2344 | |
2345 | } else if (k->get_keycode() == Key::ESCAPE) { |
2346 | set_freelook_active(false); |
2347 | } |
2348 | |
2349 | if (k->get_keycode() == Key::SPACE) { |
2350 | if (!k->is_pressed()) { |
2351 | emit_signal(SNAME("toggle_maximize_view" ), this); |
2352 | } |
2353 | } |
2354 | |
2355 | if (ED_IS_SHORTCUT("spatial_editor/decrease_fov" , p_event)) { |
2356 | scale_fov(-0.05); |
2357 | } |
2358 | |
2359 | if (ED_IS_SHORTCUT("spatial_editor/increase_fov" , p_event)) { |
2360 | scale_fov(0.05); |
2361 | } |
2362 | |
2363 | if (ED_IS_SHORTCUT("spatial_editor/reset_fov" , p_event)) { |
2364 | reset_fov(); |
2365 | } |
2366 | } |
2367 | |
2368 | // freelook uses most of the useful shortcuts, like save, so its ok |
2369 | // to consider freelook active as end of the line for future events. |
2370 | if (freelook_active) { |
2371 | accept_event(); |
2372 | } |
2373 | } |
2374 | |
2375 | void Node3DEditorViewport::_nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) { |
2376 | const NavigationScheme nav_scheme = (NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme" ).operator int(); |
2377 | |
2378 | real_t pan_speed = 1 / 150.0; |
2379 | if (p_event.is_valid() && nav_scheme == NAVIGATION_MAYA && p_event->is_shift_pressed()) { |
2380 | pan_speed *= 10; |
2381 | } |
2382 | |
2383 | Transform3D camera_transform; |
2384 | |
2385 | camera_transform.translate_local(cursor.pos); |
2386 | camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot); |
2387 | camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot); |
2388 | const bool invert_x_axis = EDITOR_GET("editors/3d/navigation/invert_x_axis" ); |
2389 | const bool invert_y_axis = EDITOR_GET("editors/3d/navigation/invert_y_axis" ); |
2390 | Vector3 translation( |
2391 | (invert_x_axis ? -1 : 1) * -p_relative.x * pan_speed, |
2392 | (invert_y_axis ? -1 : 1) * p_relative.y * pan_speed, |
2393 | 0); |
2394 | translation *= cursor.distance / DISTANCE_DEFAULT; |
2395 | camera_transform.translate_local(translation); |
2396 | cursor.pos = camera_transform.origin; |
2397 | } |
2398 | |
2399 | void Node3DEditorViewport::_nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) { |
2400 | const NavigationScheme nav_scheme = (NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme" ).operator int(); |
2401 | |
2402 | real_t zoom_speed = 1 / 80.0; |
2403 | if (p_event.is_valid() && nav_scheme == NAVIGATION_MAYA && p_event->is_shift_pressed()) { |
2404 | zoom_speed *= 10; |
2405 | } |
2406 | |
2407 | NavigationZoomStyle zoom_style = (NavigationZoomStyle)EDITOR_GET("editors/3d/navigation/zoom_style" ).operator int(); |
2408 | if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) { |
2409 | if (p_relative.x > 0) { |
2410 | scale_cursor_distance(1 - p_relative.x * zoom_speed); |
2411 | } else if (p_relative.x < 0) { |
2412 | scale_cursor_distance(1.0 / (1 + p_relative.x * zoom_speed)); |
2413 | } |
2414 | } else { |
2415 | if (p_relative.y > 0) { |
2416 | scale_cursor_distance(1 + p_relative.y * zoom_speed); |
2417 | } else if (p_relative.y < 0) { |
2418 | scale_cursor_distance(1.0 / (1 - p_relative.y * zoom_speed)); |
2419 | } |
2420 | } |
2421 | } |
2422 | |
2423 | void Node3DEditorViewport::_nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) { |
2424 | if (lock_rotation) { |
2425 | _nav_pan(p_event, p_relative); |
2426 | return; |
2427 | } |
2428 | |
2429 | if (orthogonal && auto_orthogonal) { |
2430 | _menu_option(VIEW_PERSPECTIVE); |
2431 | } |
2432 | |
2433 | const real_t degrees_per_pixel = EDITOR_GET("editors/3d/navigation_feel/orbit_sensitivity" ); |
2434 | const real_t radians_per_pixel = Math::deg_to_rad(degrees_per_pixel); |
2435 | const bool invert_y_axis = EDITOR_GET("editors/3d/navigation/invert_y_axis" ); |
2436 | const bool invert_x_axis = EDITOR_GET("editors/3d/navigation/invert_x_axis" ); |
2437 | |
2438 | if (invert_y_axis) { |
2439 | cursor.x_rot -= p_relative.y * radians_per_pixel; |
2440 | } else { |
2441 | cursor.x_rot += p_relative.y * radians_per_pixel; |
2442 | } |
2443 | // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented. |
2444 | cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57); |
2445 | |
2446 | if (invert_x_axis) { |
2447 | cursor.y_rot -= p_relative.x * radians_per_pixel; |
2448 | } else { |
2449 | cursor.y_rot += p_relative.x * radians_per_pixel; |
2450 | } |
2451 | view_type = VIEW_TYPE_USER; |
2452 | _update_name(); |
2453 | } |
2454 | |
2455 | void Node3DEditorViewport::_nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) { |
2456 | if (orthogonal) { |
2457 | _nav_pan(p_event, p_relative); |
2458 | return; |
2459 | } |
2460 | |
2461 | if (orthogonal && auto_orthogonal) { |
2462 | _menu_option(VIEW_PERSPECTIVE); |
2463 | } |
2464 | |
2465 | // Scale mouse sensitivity with camera FOV scale when zoomed in to make it easier to point at things. |
2466 | const real_t degrees_per_pixel = real_t(EDITOR_GET("editors/3d/freelook/freelook_sensitivity" )) * MIN(1.0, cursor.fov_scale); |
2467 | const real_t radians_per_pixel = Math::deg_to_rad(degrees_per_pixel); |
2468 | const bool invert_y_axis = EDITOR_GET("editors/3d/navigation/invert_y_axis" ); |
2469 | |
2470 | // Note: do NOT assume the camera has the "current" transform, because it is interpolated and may have "lag". |
2471 | const Transform3D prev_camera_transform = to_camera_transform(cursor); |
2472 | |
2473 | if (invert_y_axis) { |
2474 | cursor.x_rot -= p_relative.y * radians_per_pixel; |
2475 | } else { |
2476 | cursor.x_rot += p_relative.y * radians_per_pixel; |
2477 | } |
2478 | // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented. |
2479 | cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57); |
2480 | |
2481 | cursor.y_rot += p_relative.x * radians_per_pixel; |
2482 | |
2483 | // Look is like the opposite of Orbit: the focus point rotates around the camera |
2484 | Transform3D camera_transform = to_camera_transform(cursor); |
2485 | Vector3 pos = camera_transform.xform(Vector3(0, 0, 0)); |
2486 | Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0)); |
2487 | Vector3 diff = prev_pos - pos; |
2488 | cursor.pos += diff; |
2489 | |
2490 | view_type = VIEW_TYPE_USER; |
2491 | _update_name(); |
2492 | } |
2493 | |
2494 | void Node3DEditorViewport::set_freelook_active(bool active_now) { |
2495 | if (!freelook_active && active_now) { |
2496 | // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential |
2497 | cursor = camera_cursor; |
2498 | |
2499 | // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos |
2500 | Vector3 forward = to_camera_transform(cursor).basis.xform(Vector3(0, 0, -1)); |
2501 | cursor.eye_pos = cursor.pos - cursor.distance * forward; |
2502 | // Also sync the camera cursor, otherwise switching to freelook will be trippy if inertia is active |
2503 | camera_cursor.eye_pos = cursor.eye_pos; |
2504 | |
2505 | if (EDITOR_GET("editors/3d/freelook/freelook_speed_zoom_link" )) { |
2506 | // Re-adjust freelook speed from the current zoom level |
2507 | real_t base_speed = EDITOR_GET("editors/3d/freelook/freelook_base_speed" ); |
2508 | freelook_speed = base_speed * cursor.distance; |
2509 | } |
2510 | |
2511 | previous_mouse_position = get_local_mouse_position(); |
2512 | |
2513 | // Hide mouse like in an FPS (warping doesn't work) |
2514 | Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); |
2515 | |
2516 | } else if (freelook_active && !active_now) { |
2517 | // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential |
2518 | cursor = camera_cursor; |
2519 | |
2520 | // Restore mouse |
2521 | Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE); |
2522 | |
2523 | // Restore the previous mouse position when leaving freelook mode. |
2524 | // This is done because leaving `Input.MOUSE_MODE_CAPTURED` will center the cursor |
2525 | // due to OS limitations. |
2526 | warp_mouse(previous_mouse_position); |
2527 | } |
2528 | |
2529 | freelook_active = active_now; |
2530 | } |
2531 | |
2532 | void Node3DEditorViewport::scale_fov(real_t p_fov_offset) { |
2533 | cursor.fov_scale = CLAMP(cursor.fov_scale + p_fov_offset, 0.1, 2.5); |
2534 | surface->queue_redraw(); |
2535 | } |
2536 | |
2537 | void Node3DEditorViewport::reset_fov() { |
2538 | cursor.fov_scale = 1.0; |
2539 | surface->queue_redraw(); |
2540 | } |
2541 | |
2542 | void Node3DEditorViewport::scale_cursor_distance(real_t scale) { |
2543 | real_t min_distance = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN); |
2544 | real_t max_distance = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX); |
2545 | if (unlikely(min_distance > max_distance)) { |
2546 | cursor.distance = (min_distance + max_distance) / 2; |
2547 | } else { |
2548 | cursor.distance = CLAMP(cursor.distance * scale, min_distance, max_distance); |
2549 | } |
2550 | |
2551 | if (cursor.distance == max_distance || cursor.distance == min_distance) { |
2552 | zoom_failed_attempts_count++; |
2553 | } else { |
2554 | zoom_failed_attempts_count = 0; |
2555 | } |
2556 | |
2557 | zoom_indicator_delay = ZOOM_FREELOOK_INDICATOR_DELAY_S; |
2558 | surface->queue_redraw(); |
2559 | } |
2560 | |
2561 | void Node3DEditorViewport::scale_freelook_speed(real_t scale) { |
2562 | real_t min_speed = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN); |
2563 | real_t max_speed = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX); |
2564 | if (unlikely(min_speed > max_speed)) { |
2565 | freelook_speed = (min_speed + max_speed) / 2; |
2566 | } else { |
2567 | freelook_speed = CLAMP(freelook_speed * scale, min_speed, max_speed); |
2568 | } |
2569 | |
2570 | zoom_indicator_delay = ZOOM_FREELOOK_INDICATOR_DELAY_S; |
2571 | surface->queue_redraw(); |
2572 | } |
2573 | |
2574 | Point2i Node3DEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const { |
2575 | Point2i relative; |
2576 | if (bool(EDITOR_GET("editors/3d/navigation/warped_mouse_panning" ))) { |
2577 | relative = Input::get_singleton()->warp_mouse_motion(p_ev_mouse_motion, surface->get_global_rect()); |
2578 | } else { |
2579 | relative = p_ev_mouse_motion->get_relative(); |
2580 | } |
2581 | return relative; |
2582 | } |
2583 | |
2584 | void Node3DEditorViewport::_update_freelook(real_t delta) { |
2585 | if (!is_freelook_active()) { |
2586 | return; |
2587 | } |
2588 | |
2589 | const FreelookNavigationScheme navigation_scheme = (FreelookNavigationScheme)EDITOR_GET("editors/3d/freelook/freelook_navigation_scheme" ).operator int(); |
2590 | |
2591 | Vector3 forward; |
2592 | if (navigation_scheme == FREELOOK_FULLY_AXIS_LOCKED) { |
2593 | // Forward/backward keys will always go straight forward/backward, never moving on the Y axis. |
2594 | forward = Vector3(0, 0, -1).rotated(Vector3(0, 1, 0), camera->get_rotation().y); |
2595 | } else { |
2596 | // Forward/backward keys will be relative to the camera pitch. |
2597 | forward = camera->get_transform().basis.xform(Vector3(0, 0, -1)); |
2598 | } |
2599 | |
2600 | const Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0)); |
2601 | |
2602 | Vector3 up; |
2603 | if (navigation_scheme == FREELOOK_PARTIALLY_AXIS_LOCKED || navigation_scheme == FREELOOK_FULLY_AXIS_LOCKED) { |
2604 | // Up/down keys will always go up/down regardless of camera pitch. |
2605 | up = Vector3(0, 1, 0); |
2606 | } else { |
2607 | // Up/down keys will be relative to the camera pitch. |
2608 | up = camera->get_transform().basis.xform(Vector3(0, 1, 0)); |
2609 | } |
2610 | |
2611 | Vector3 direction; |
2612 | |
2613 | // Use actions from the inputmap, as this is the only way to reliably detect input in this method. |
2614 | // See #54469 for more discussion and explanation. |
2615 | Input *inp = Input::get_singleton(); |
2616 | if (inp->is_action_pressed("spatial_editor/freelook_left" )) { |
2617 | direction -= right; |
2618 | } |
2619 | if (inp->is_action_pressed("spatial_editor/freelook_right" )) { |
2620 | direction += right; |
2621 | } |
2622 | if (inp->is_action_pressed("spatial_editor/freelook_forward" )) { |
2623 | direction += forward; |
2624 | } |
2625 | if (inp->is_action_pressed("spatial_editor/freelook_backwards" )) { |
2626 | direction -= forward; |
2627 | } |
2628 | if (inp->is_action_pressed("spatial_editor/freelook_up" )) { |
2629 | direction += up; |
2630 | } |
2631 | if (inp->is_action_pressed("spatial_editor/freelook_down" )) { |
2632 | direction -= up; |
2633 | } |
2634 | |
2635 | real_t speed = freelook_speed; |
2636 | |
2637 | if (inp->is_action_pressed("spatial_editor/freelook_speed_modifier" )) { |
2638 | speed *= 3.0; |
2639 | } |
2640 | if (inp->is_action_pressed("spatial_editor/freelook_slow_modifier" )) { |
2641 | speed *= 0.333333; |
2642 | } |
2643 | |
2644 | const Vector3 motion = direction * speed * delta; |
2645 | cursor.pos += motion; |
2646 | cursor.eye_pos += motion; |
2647 | } |
2648 | |
2649 | void Node3DEditorViewport::set_message(String p_message, float p_time) { |
2650 | message = p_message; |
2651 | message_time = p_time; |
2652 | } |
2653 | |
2654 | void Node3DEditorPlugin::edited_scene_changed() { |
2655 | for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) { |
2656 | Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(i); |
2657 | if (viewport->is_visible()) { |
2658 | viewport->notification(Control::NOTIFICATION_VISIBILITY_CHANGED); |
2659 | } |
2660 | } |
2661 | } |
2662 | |
2663 | void Node3DEditorViewport::_project_settings_changed() { |
2664 | //update shadow atlas if changed |
2665 | int shadowmap_size = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_size" ); |
2666 | bool shadowmap_16_bits = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_16_bits" ); |
2667 | int atlas_q0 = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_0_subdiv" ); |
2668 | int atlas_q1 = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_1_subdiv" ); |
2669 | int atlas_q2 = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_2_subdiv" ); |
2670 | int atlas_q3 = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_3_subdiv" ); |
2671 | |
2672 | viewport->set_positional_shadow_atlas_size(shadowmap_size); |
2673 | viewport->set_positional_shadow_atlas_16_bits(shadowmap_16_bits); |
2674 | viewport->set_positional_shadow_atlas_quadrant_subdiv(0, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q0)); |
2675 | viewport->set_positional_shadow_atlas_quadrant_subdiv(1, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q1)); |
2676 | viewport->set_positional_shadow_atlas_quadrant_subdiv(2, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q2)); |
2677 | viewport->set_positional_shadow_atlas_quadrant_subdiv(3, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q3)); |
2678 | |
2679 | _update_shrink(); |
2680 | |
2681 | // Update MSAA, screen-space AA and debanding if changed |
2682 | |
2683 | const int msaa_mode = GLOBAL_GET("rendering/anti_aliasing/quality/msaa_3d" ); |
2684 | viewport->set_msaa_3d(Viewport::MSAA(msaa_mode)); |
2685 | const int ssaa_mode = GLOBAL_GET("rendering/anti_aliasing/quality/screen_space_aa" ); |
2686 | viewport->set_screen_space_aa(Viewport::ScreenSpaceAA(ssaa_mode)); |
2687 | const bool use_taa = GLOBAL_GET("rendering/anti_aliasing/quality/use_taa" ); |
2688 | viewport->set_use_taa(use_taa); |
2689 | |
2690 | const bool transparent_background = GLOBAL_GET("rendering/viewport/transparent_background" ); |
2691 | viewport->set_transparent_background(transparent_background); |
2692 | |
2693 | const bool use_hdr_2d = GLOBAL_GET("rendering/viewport/hdr_2d" ); |
2694 | viewport->set_use_hdr_2d(use_hdr_2d); |
2695 | |
2696 | const bool use_debanding = GLOBAL_GET("rendering/anti_aliasing/quality/use_debanding" ); |
2697 | viewport->set_use_debanding(use_debanding); |
2698 | |
2699 | const bool use_occlusion_culling = GLOBAL_GET("rendering/occlusion_culling/use_occlusion_culling" ); |
2700 | viewport->set_use_occlusion_culling(use_occlusion_culling); |
2701 | |
2702 | const float mesh_lod_threshold = GLOBAL_GET("rendering/mesh_lod/lod_change/threshold_pixels" ); |
2703 | viewport->set_mesh_lod_threshold(mesh_lod_threshold); |
2704 | |
2705 | const Viewport::Scaling3DMode scaling_3d_mode = Viewport::Scaling3DMode(int(GLOBAL_GET("rendering/scaling_3d/mode" ))); |
2706 | viewport->set_scaling_3d_mode(scaling_3d_mode); |
2707 | |
2708 | const float scaling_3d_scale = GLOBAL_GET("rendering/scaling_3d/scale" ); |
2709 | viewport->set_scaling_3d_scale(scaling_3d_scale); |
2710 | |
2711 | const float fsr_sharpness = GLOBAL_GET("rendering/scaling_3d/fsr_sharpness" ); |
2712 | viewport->set_fsr_sharpness(fsr_sharpness); |
2713 | |
2714 | const float texture_mipmap_bias = GLOBAL_GET("rendering/textures/default_filters/texture_mipmap_bias" ); |
2715 | viewport->set_texture_mipmap_bias(texture_mipmap_bias); |
2716 | } |
2717 | |
2718 | void Node3DEditorViewport::_notification(int p_what) { |
2719 | switch (p_what) { |
2720 | case NOTIFICATION_READY: { |
2721 | ProjectSettings::get_singleton()->connect("settings_changed" , callable_mp(this, &Node3DEditorViewport::_project_settings_changed)); |
2722 | } break; |
2723 | |
2724 | case NOTIFICATION_VISIBILITY_CHANGED: { |
2725 | bool vp_visible = is_visible_in_tree(); |
2726 | |
2727 | set_process(vp_visible); |
2728 | set_physics_process(vp_visible); |
2729 | |
2730 | if (vp_visible) { |
2731 | orthogonal = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL)); |
2732 | _update_name(); |
2733 | _update_camera(0); |
2734 | } else { |
2735 | set_freelook_active(false); |
2736 | } |
2737 | call_deferred(SNAME("update_transform_gizmo_view" )); |
2738 | } break; |
2739 | |
2740 | case NOTIFICATION_RESIZED: { |
2741 | call_deferred(SNAME("update_transform_gizmo_view" )); |
2742 | } break; |
2743 | |
2744 | case NOTIFICATION_PROCESS: { |
2745 | real_t delta = get_process_delta_time(); |
2746 | |
2747 | if (zoom_indicator_delay > 0) { |
2748 | zoom_indicator_delay -= delta; |
2749 | if (zoom_indicator_delay <= 0) { |
2750 | surface->queue_redraw(); |
2751 | zoom_limit_label->hide(); |
2752 | } |
2753 | } |
2754 | |
2755 | _update_navigation_controls_visibility(); |
2756 | _update_freelook(delta); |
2757 | |
2758 | Node *scene_root = SceneTreeDock::get_singleton()->get_editor_data()->get_edited_scene_root(); |
2759 | if (previewing_cinema && scene_root != nullptr) { |
2760 | Camera3D *cam = scene_root->get_viewport()->get_camera_3d(); |
2761 | if (cam != nullptr && cam != previewing) { |
2762 | //then switch the viewport's camera to the scene's viewport camera |
2763 | if (previewing != nullptr) { |
2764 | previewing->disconnect("tree_exited" , callable_mp(this, &Node3DEditorViewport::_preview_exited_scene)); |
2765 | } |
2766 | previewing = cam; |
2767 | previewing->connect("tree_exited" , callable_mp(this, &Node3DEditorViewport::_preview_exited_scene)); |
2768 | RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), cam->get_camera()); |
2769 | surface->queue_redraw(); |
2770 | } |
2771 | } |
2772 | |
2773 | _update_camera(delta); |
2774 | |
2775 | const HashMap<Node *, Object *> &selection = editor_selection->get_selection(); |
2776 | |
2777 | bool changed = false; |
2778 | bool exist = false; |
2779 | |
2780 | for (const KeyValue<Node *, Object *> &E : selection) { |
2781 | Node3D *sp = Object::cast_to<Node3D>(E.key); |
2782 | if (!sp) { |
2783 | continue; |
2784 | } |
2785 | |
2786 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
2787 | if (!se) { |
2788 | continue; |
2789 | } |
2790 | |
2791 | Transform3D t = sp->get_global_gizmo_transform(); |
2792 | VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(sp); |
2793 | AABB new_aabb = vi ? vi->get_aabb() : _calculate_spatial_bounds(sp); |
2794 | |
2795 | exist = true; |
2796 | if (se->last_xform == t && se->aabb == new_aabb && !se->last_xform_dirty) { |
2797 | continue; |
2798 | } |
2799 | changed = true; |
2800 | se->last_xform_dirty = false; |
2801 | se->last_xform = t; |
2802 | |
2803 | se->aabb = new_aabb; |
2804 | |
2805 | Transform3D t_offset = t; |
2806 | |
2807 | // apply AABB scaling before item's global transform |
2808 | { |
2809 | const Vector3 offset(0.005, 0.005, 0.005); |
2810 | Basis aabb_s; |
2811 | aabb_s.scale(se->aabb.size + offset); |
2812 | t.translate_local(se->aabb.position - offset / 2); |
2813 | t.basis = t.basis * aabb_s; |
2814 | } |
2815 | { |
2816 | const Vector3 offset(0.01, 0.01, 0.01); |
2817 | Basis aabb_s; |
2818 | aabb_s.scale(se->aabb.size + offset); |
2819 | t_offset.translate_local(se->aabb.position - offset / 2); |
2820 | t_offset.basis = t_offset.basis * aabb_s; |
2821 | } |
2822 | |
2823 | RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance, t); |
2824 | RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_offset, t_offset); |
2825 | RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_xray, t); |
2826 | RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_xray_offset, t_offset); |
2827 | } |
2828 | |
2829 | if (changed || (spatial_editor->is_gizmo_visible() && !exist)) { |
2830 | spatial_editor->update_transform_gizmo(); |
2831 | } |
2832 | |
2833 | if (message_time > 0) { |
2834 | if (message != last_message) { |
2835 | surface->queue_redraw(); |
2836 | last_message = message; |
2837 | } |
2838 | |
2839 | message_time -= get_physics_process_delta_time(); |
2840 | if (message_time < 0) { |
2841 | surface->queue_redraw(); |
2842 | } |
2843 | } |
2844 | |
2845 | bool show_info = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION)); |
2846 | if (show_info != info_label->is_visible()) { |
2847 | info_label->set_visible(show_info); |
2848 | } |
2849 | |
2850 | Camera3D *current_camera; |
2851 | |
2852 | if (previewing) { |
2853 | current_camera = previewing; |
2854 | } else { |
2855 | current_camera = camera; |
2856 | } |
2857 | |
2858 | if (show_info) { |
2859 | const String viewport_size = vformat(U"%d × %d" , viewport->get_size().x, viewport->get_size().y); |
2860 | String text; |
2861 | text += vformat(TTR("X: %s\n" ), rtos(current_camera->get_position().x).pad_decimals(1)); |
2862 | text += vformat(TTR("Y: %s\n" ), rtos(current_camera->get_position().y).pad_decimals(1)); |
2863 | text += vformat(TTR("Z: %s\n" ), rtos(current_camera->get_position().z).pad_decimals(1)); |
2864 | text += "\n" ; |
2865 | text += vformat( |
2866 | TTR("Size: %s (%.1fMP)\n" ), |
2867 | viewport_size, |
2868 | viewport->get_size().x * viewport->get_size().y * 0.000001); |
2869 | |
2870 | text += "\n" ; |
2871 | text += vformat(TTR("Objects: %d\n" ), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_OBJECTS_IN_FRAME)); |
2872 | text += vformat(TTR("Primitives: %d\n" ), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_PRIMITIVES_IN_FRAME)); |
2873 | text += vformat(TTR("Draw Calls: %d" ), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_DRAW_CALLS_IN_FRAME)); |
2874 | |
2875 | info_label->set_text(text); |
2876 | } |
2877 | |
2878 | // FPS Counter. |
2879 | bool show_fps = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME)); |
2880 | |
2881 | if (show_fps != fps_label->is_visible()) { |
2882 | cpu_time_label->set_visible(show_fps); |
2883 | gpu_time_label->set_visible(show_fps); |
2884 | fps_label->set_visible(show_fps); |
2885 | RS::get_singleton()->viewport_set_measure_render_time(viewport->get_viewport_rid(), show_fps); |
2886 | for (int i = 0; i < FRAME_TIME_HISTORY; i++) { |
2887 | // Initialize to 120 FPS, so that the initial estimation until we get enough data is always reasonable. |
2888 | cpu_time_history[i] = 8.333333; |
2889 | gpu_time_history[i] = 8.333333; |
2890 | } |
2891 | cpu_time_history_index = 0; |
2892 | gpu_time_history_index = 0; |
2893 | } |
2894 | if (show_fps) { |
2895 | cpu_time_history[cpu_time_history_index] = RS::get_singleton()->viewport_get_measured_render_time_cpu(viewport->get_viewport_rid()); |
2896 | cpu_time_history_index = (cpu_time_history_index + 1) % FRAME_TIME_HISTORY; |
2897 | double cpu_time = 0.0; |
2898 | for (int i = 0; i < FRAME_TIME_HISTORY; i++) { |
2899 | cpu_time += cpu_time_history[i]; |
2900 | } |
2901 | cpu_time /= FRAME_TIME_HISTORY; |
2902 | // Prevent unrealistically low values. |
2903 | cpu_time = MAX(0.01, cpu_time); |
2904 | |
2905 | gpu_time_history[gpu_time_history_index] = RS::get_singleton()->viewport_get_measured_render_time_gpu(viewport->get_viewport_rid()); |
2906 | gpu_time_history_index = (gpu_time_history_index + 1) % FRAME_TIME_HISTORY; |
2907 | double gpu_time = 0.0; |
2908 | for (int i = 0; i < FRAME_TIME_HISTORY; i++) { |
2909 | gpu_time += gpu_time_history[i]; |
2910 | } |
2911 | gpu_time /= FRAME_TIME_HISTORY; |
2912 | // Prevent division by zero for the FPS counter (and unrealistically low values). |
2913 | // This limits the reported FPS to 100000. |
2914 | gpu_time = MAX(0.01, gpu_time); |
2915 | |
2916 | // Color labels depending on performance level ("good" = green, "OK" = yellow, "bad" = red). |
2917 | // Middle point is at 15 ms. |
2918 | cpu_time_label->set_text(vformat(TTR("CPU Time: %s ms" ), rtos(cpu_time).pad_decimals(2))); |
2919 | cpu_time_label->add_theme_color_override( |
2920 | "font_color" , |
2921 | frame_time_gradient->get_color_at_offset( |
2922 | Math::remap(cpu_time, 0, 30, 0, 1))); |
2923 | |
2924 | gpu_time_label->set_text(vformat(TTR("GPU Time: %s ms" ), rtos(gpu_time).pad_decimals(2))); |
2925 | // Middle point is at 15 ms. |
2926 | gpu_time_label->add_theme_color_override( |
2927 | "font_color" , |
2928 | frame_time_gradient->get_color_at_offset( |
2929 | Math::remap(gpu_time, 0, 30, 0, 1))); |
2930 | |
2931 | const double fps = 1000.0 / gpu_time; |
2932 | fps_label->set_text(vformat(TTR("FPS: %d" ), fps)); |
2933 | // Middle point is at 60 FPS. |
2934 | fps_label->add_theme_color_override( |
2935 | "font_color" , |
2936 | frame_time_gradient->get_color_at_offset( |
2937 | Math::remap(fps, 110, 10, 0, 1))); |
2938 | } |
2939 | |
2940 | bool show_cinema = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW)); |
2941 | cinema_label->set_visible(show_cinema); |
2942 | if (show_cinema) { |
2943 | float cinema_half_width = cinema_label->get_size().width / 2.0f; |
2944 | cinema_label->set_anchor_and_offset(SIDE_LEFT, 0.5f, -cinema_half_width); |
2945 | } |
2946 | |
2947 | if (lock_rotation) { |
2948 | float locked_half_width = locked_label->get_size().width / 2.0f; |
2949 | locked_label->set_anchor_and_offset(SIDE_LEFT, 0.5f, -locked_half_width); |
2950 | } |
2951 | } break; |
2952 | |
2953 | case NOTIFICATION_PHYSICS_PROCESS: { |
2954 | if (!update_preview_node) { |
2955 | return; |
2956 | } |
2957 | if (preview_node->is_inside_tree()) { |
2958 | preview_node_pos = spatial_editor->snap_point(_get_instance_position(preview_node_viewport_pos)); |
2959 | Transform3D preview_gl_transform = Transform3D(Basis(), preview_node_pos); |
2960 | preview_node->set_global_transform(preview_gl_transform); |
2961 | if (!preview_node->is_visible()) { |
2962 | preview_node->show(); |
2963 | } |
2964 | } |
2965 | update_preview_node = false; |
2966 | } break; |
2967 | |
2968 | case NOTIFICATION_ENTER_TREE: { |
2969 | surface->connect("draw" , callable_mp(this, &Node3DEditorViewport::_draw)); |
2970 | surface->connect("gui_input" , callable_mp(this, &Node3DEditorViewport::_sinput)); |
2971 | surface->connect("mouse_entered" , callable_mp(this, &Node3DEditorViewport::_surface_mouse_enter)); |
2972 | surface->connect("mouse_exited" , callable_mp(this, &Node3DEditorViewport::_surface_mouse_exit)); |
2973 | surface->connect("focus_entered" , callable_mp(this, &Node3DEditorViewport::_surface_focus_enter)); |
2974 | surface->connect("focus_exited" , callable_mp(this, &Node3DEditorViewport::_surface_focus_exit)); |
2975 | |
2976 | _init_gizmo_instance(index); |
2977 | } break; |
2978 | |
2979 | case NOTIFICATION_EXIT_TREE: { |
2980 | _finish_gizmo_instances(); |
2981 | } break; |
2982 | |
2983 | case NOTIFICATION_THEME_CHANGED: { |
2984 | view_menu->set_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl" ))); |
2985 | preview_camera->set_icon(get_editor_theme_icon(SNAME("Camera3D" ))); |
2986 | Control *gui_base = EditorNode::get_singleton()->get_gui_base(); |
2987 | |
2988 | view_menu->add_theme_style_override("normal" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2989 | view_menu->add_theme_style_override("hover" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2990 | view_menu->add_theme_style_override("pressed" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2991 | view_menu->add_theme_style_override("focus" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2992 | view_menu->add_theme_style_override("disabled" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2993 | |
2994 | preview_camera->add_theme_style_override("normal" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2995 | preview_camera->add_theme_style_override("hover" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2996 | preview_camera->add_theme_style_override("pressed" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2997 | preview_camera->add_theme_style_override("focus" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2998 | preview_camera->add_theme_style_override("disabled" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
2999 | |
3000 | frame_time_gradient->set_color(0, get_theme_color(SNAME("success_color" ), EditorStringName(Editor))); |
3001 | frame_time_gradient->set_color(1, get_theme_color(SNAME("warning_color" ), EditorStringName(Editor))); |
3002 | frame_time_gradient->set_color(2, get_theme_color(SNAME("error_color" ), EditorStringName(Editor))); |
3003 | |
3004 | info_label->add_theme_style_override("normal" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
3005 | cpu_time_label->add_theme_style_override("normal" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
3006 | gpu_time_label->add_theme_style_override("normal" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
3007 | fps_label->add_theme_style_override("normal" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
3008 | cinema_label->add_theme_style_override("normal" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
3009 | locked_label->add_theme_style_override("normal" , gui_base->get_theme_stylebox(SNAME("Information3dViewport" ), EditorStringName(EditorStyles))); |
3010 | } break; |
3011 | |
3012 | case NOTIFICATION_DRAG_END: { |
3013 | // Clear preview material when dropped outside applicable object. |
3014 | if (spatial_editor->get_preview_material().is_valid() && !is_drag_successful()) { |
3015 | _remove_preview_material(); |
3016 | } |
3017 | } break; |
3018 | } |
3019 | } |
3020 | |
3021 | static void draw_indicator_bar(Control &p_surface, real_t p_fill, const Ref<Texture2D> p_icon, const Ref<Font> p_font, int p_font_size, const String &p_text, const Color &p_color) { |
3022 | // Adjust bar size from control height |
3023 | const Vector2 surface_size = p_surface.get_size(); |
3024 | const real_t h = surface_size.y / 2.0; |
3025 | const real_t y = (surface_size.y - h) / 2.0; |
3026 | |
3027 | const Rect2 r(10 * EDSCALE, y, 6 * EDSCALE, h); |
3028 | const real_t sy = r.size.y * p_fill; |
3029 | |
3030 | // Note: because this bar appears over the viewport, it has to stay readable for any background color |
3031 | // Draw both neutral dark and bright colors to account this |
3032 | p_surface.draw_rect(r, p_color * Color(1, 1, 1, 0.2)); |
3033 | p_surface.draw_rect(Rect2(r.position.x, r.position.y + r.size.y - sy, r.size.x, sy), p_color * Color(1, 1, 1, 0.6)); |
3034 | p_surface.draw_rect(r.grow(1), Color(0, 0, 0, 0.7), false, Math::round(EDSCALE)); |
3035 | |
3036 | const Vector2 icon_size = p_icon->get_size(); |
3037 | const Vector2 icon_pos = Vector2(r.position.x - (icon_size.x - r.size.x) / 2, r.position.y + r.size.y + 2 * EDSCALE); |
3038 | p_surface.draw_texture(p_icon, icon_pos, p_color); |
3039 | |
3040 | // Draw text below the bar (for speed/zoom information). |
3041 | p_surface.draw_string_outline(p_font, Vector2(icon_pos.x, icon_pos.y + icon_size.y + 16 * EDSCALE), p_text, HORIZONTAL_ALIGNMENT_LEFT, -1.f, p_font_size, Math::round(2 * EDSCALE), Color(0, 0, 0)); |
3042 | p_surface.draw_string(p_font, Vector2(icon_pos.x, icon_pos.y + icon_size.y + 16 * EDSCALE), p_text, HORIZONTAL_ALIGNMENT_LEFT, -1.f, p_font_size, p_color); |
3043 | } |
3044 | |
3045 | void Node3DEditorViewport::_draw() { |
3046 | EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over(); |
3047 | if (!over_plugin_list->is_empty()) { |
3048 | over_plugin_list->forward_3d_draw_over_viewport(surface); |
3049 | } |
3050 | |
3051 | EditorPluginList *force_over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_force_over(); |
3052 | if (!force_over_plugin_list->is_empty()) { |
3053 | force_over_plugin_list->forward_3d_force_draw_over_viewport(surface); |
3054 | } |
3055 | |
3056 | if (surface->has_focus()) { |
3057 | Size2 size = surface->get_size(); |
3058 | Rect2 r = Rect2(Point2(), size); |
3059 | get_theme_stylebox(SNAME("FocusViewport" ), EditorStringName(EditorStyles))->draw(surface->get_canvas_item(), r); |
3060 | } |
3061 | |
3062 | if (cursor.region_select) { |
3063 | const Rect2 selection_rect = Rect2(cursor.region_begin, cursor.region_end - cursor.region_begin); |
3064 | |
3065 | surface->draw_rect( |
3066 | selection_rect, |
3067 | get_theme_color(SNAME("box_selection_fill_color" ), EditorStringName(Editor))); |
3068 | |
3069 | surface->draw_rect( |
3070 | selection_rect, |
3071 | get_theme_color(SNAME("box_selection_stroke_color" ), EditorStringName(Editor)), |
3072 | false, |
3073 | Math::round(EDSCALE)); |
3074 | } |
3075 | |
3076 | RID ci = surface->get_canvas_item(); |
3077 | |
3078 | if (message_time > 0) { |
3079 | Ref<Font> font = get_theme_font(SNAME("font" ), SNAME("Label" )); |
3080 | int font_size = get_theme_font_size(SNAME("font_size" ), SNAME("Label" )); |
3081 | Point2 msgpos = Point2(5, get_size().y - 20); |
3082 | font->draw_string(ci, msgpos + Point2(1, 1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); |
3083 | font->draw_string(ci, msgpos + Point2(-1, -1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8)); |
3084 | font->draw_string(ci, msgpos, message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1)); |
3085 | } |
3086 | |
3087 | if (_edit.mode == TRANSFORM_ROTATE && _edit.show_rotation_line) { |
3088 | Point2 center = _point_to_screen(_edit.center); |
3089 | |
3090 | Color handle_color; |
3091 | switch (_edit.plane) { |
3092 | case TRANSFORM_X_AXIS: |
3093 | handle_color = get_theme_color(SNAME("axis_x_color" ), EditorStringName(Editor)); |
3094 | break; |
3095 | case TRANSFORM_Y_AXIS: |
3096 | handle_color = get_theme_color(SNAME("axis_y_color" ), EditorStringName(Editor)); |
3097 | break; |
3098 | case TRANSFORM_Z_AXIS: |
3099 | handle_color = get_theme_color(SNAME("axis_z_color" ), EditorStringName(Editor)); |
3100 | break; |
3101 | default: |
3102 | handle_color = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
3103 | break; |
3104 | } |
3105 | handle_color = handle_color.from_hsv(handle_color.get_h(), 0.25, 1.0, 1); |
3106 | |
3107 | RenderingServer::get_singleton()->canvas_item_add_line( |
3108 | ci, |
3109 | _edit.mouse_pos, |
3110 | center, |
3111 | handle_color, |
3112 | Math::round(2 * EDSCALE)); |
3113 | } |
3114 | if (previewing) { |
3115 | Size2 ss = Size2(GLOBAL_GET("display/window/size/viewport_width" ), GLOBAL_GET("display/window/size/viewport_height" )); |
3116 | float aspect = ss.aspect(); |
3117 | Size2 s = get_size(); |
3118 | |
3119 | Rect2 draw_rect; |
3120 | |
3121 | switch (previewing->get_keep_aspect_mode()) { |
3122 | case Camera3D::KEEP_WIDTH: { |
3123 | draw_rect.size = Size2(s.width, s.width / aspect); |
3124 | draw_rect.position.x = 0; |
3125 | draw_rect.position.y = (s.height - draw_rect.size.y) * 0.5; |
3126 | |
3127 | } break; |
3128 | case Camera3D::KEEP_HEIGHT: { |
3129 | draw_rect.size = Size2(s.height * aspect, s.height); |
3130 | draw_rect.position.y = 0; |
3131 | draw_rect.position.x = (s.width - draw_rect.size.x) * 0.5; |
3132 | |
3133 | } break; |
3134 | } |
3135 | |
3136 | draw_rect = Rect2(Vector2(), s).intersection(draw_rect); |
3137 | |
3138 | surface->draw_rect(draw_rect, Color(0.6, 0.6, 0.1, 0.5), false, Math::round(2 * EDSCALE)); |
3139 | |
3140 | } else { |
3141 | if (zoom_indicator_delay > 0.0) { |
3142 | if (is_freelook_active()) { |
3143 | // Show speed |
3144 | |
3145 | real_t min_speed = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN); |
3146 | real_t max_speed = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX); |
3147 | real_t scale_length = (max_speed - min_speed); |
3148 | |
3149 | if (!Math::is_zero_approx(scale_length)) { |
3150 | real_t logscale_t = 1.0 - Math::log1p(freelook_speed - min_speed) / Math::log1p(scale_length); |
3151 | |
3152 | // Display the freelook speed to help the user get a better sense of scale. |
3153 | const int precision = freelook_speed < 1.0 ? 2 : 1; |
3154 | draw_indicator_bar( |
3155 | *surface, |
3156 | 1.0 - logscale_t, |
3157 | get_editor_theme_icon(SNAME("ViewportSpeed" )), |
3158 | get_theme_font(SNAME("font" ), SNAME("Label" )), |
3159 | get_theme_font_size(SNAME("font_size" ), SNAME("Label" )), |
3160 | vformat("%s u/s" , String::num(freelook_speed).pad_decimals(precision)), |
3161 | Color(1.0, 0.95, 0.7)); |
3162 | } |
3163 | |
3164 | } else { |
3165 | // Show zoom |
3166 | zoom_limit_label->set_visible(zoom_failed_attempts_count > 15); |
3167 | |
3168 | real_t min_distance = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN); |
3169 | real_t max_distance = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX); |
3170 | real_t scale_length = (max_distance - min_distance); |
3171 | |
3172 | if (!Math::is_zero_approx(scale_length)) { |
3173 | real_t logscale_t = 1.0 - Math::log1p(cursor.distance - min_distance) / Math::log1p(scale_length); |
3174 | |
3175 | // Display the zoom center distance to help the user get a better sense of scale. |
3176 | const int precision = cursor.distance < 1.0 ? 2 : 1; |
3177 | draw_indicator_bar( |
3178 | *surface, |
3179 | logscale_t, |
3180 | get_editor_theme_icon(SNAME("ViewportZoom" )), |
3181 | get_theme_font(SNAME("font" ), SNAME("Label" )), |
3182 | get_theme_font_size(SNAME("font_size" ), SNAME("Label" )), |
3183 | vformat("%s u" , String::num(cursor.distance).pad_decimals(precision)), |
3184 | Color(0.7, 0.95, 1.0)); |
3185 | } |
3186 | } |
3187 | } |
3188 | } |
3189 | } |
3190 | |
3191 | void Node3DEditorViewport::(int p_option) { |
3192 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
3193 | switch (p_option) { |
3194 | case VIEW_TOP: { |
3195 | cursor.y_rot = 0; |
3196 | cursor.x_rot = Math_PI / 2.0; |
3197 | set_message(TTR("Top View." ), 2); |
3198 | view_type = VIEW_TYPE_TOP; |
3199 | _set_auto_orthogonal(); |
3200 | _update_name(); |
3201 | |
3202 | } break; |
3203 | case VIEW_BOTTOM: { |
3204 | cursor.y_rot = 0; |
3205 | cursor.x_rot = -Math_PI / 2.0; |
3206 | set_message(TTR("Bottom View." ), 2); |
3207 | view_type = VIEW_TYPE_BOTTOM; |
3208 | _set_auto_orthogonal(); |
3209 | _update_name(); |
3210 | |
3211 | } break; |
3212 | case VIEW_LEFT: { |
3213 | cursor.x_rot = 0; |
3214 | cursor.y_rot = Math_PI / 2.0; |
3215 | set_message(TTR("Left View." ), 2); |
3216 | view_type = VIEW_TYPE_LEFT; |
3217 | _set_auto_orthogonal(); |
3218 | _update_name(); |
3219 | |
3220 | } break; |
3221 | case VIEW_RIGHT: { |
3222 | cursor.x_rot = 0; |
3223 | cursor.y_rot = -Math_PI / 2.0; |
3224 | set_message(TTR("Right View." ), 2); |
3225 | view_type = VIEW_TYPE_RIGHT; |
3226 | _set_auto_orthogonal(); |
3227 | _update_name(); |
3228 | |
3229 | } break; |
3230 | case VIEW_FRONT: { |
3231 | cursor.x_rot = 0; |
3232 | cursor.y_rot = 0; |
3233 | set_message(TTR("Front View." ), 2); |
3234 | view_type = VIEW_TYPE_FRONT; |
3235 | _set_auto_orthogonal(); |
3236 | _update_name(); |
3237 | |
3238 | } break; |
3239 | case VIEW_REAR: { |
3240 | cursor.x_rot = 0; |
3241 | cursor.y_rot = Math_PI; |
3242 | set_message(TTR("Rear View." ), 2); |
3243 | view_type = VIEW_TYPE_REAR; |
3244 | _set_auto_orthogonal(); |
3245 | _update_name(); |
3246 | |
3247 | } break; |
3248 | case VIEW_CENTER_TO_ORIGIN: { |
3249 | cursor.pos = Vector3(0, 0, 0); |
3250 | |
3251 | } break; |
3252 | case VIEW_CENTER_TO_SELECTION: { |
3253 | focus_selection(); |
3254 | |
3255 | } break; |
3256 | case VIEW_ALIGN_TRANSFORM_WITH_VIEW: { |
3257 | if (!get_selected_count()) { |
3258 | break; |
3259 | } |
3260 | |
3261 | Transform3D camera_transform = camera->get_global_transform(); |
3262 | |
3263 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
3264 | |
3265 | undo_redo->create_action(TTR("Align Transform with View" )); |
3266 | |
3267 | for (Node *E : selection) { |
3268 | Node3D *sp = Object::cast_to<Node3D>(E); |
3269 | if (!sp) { |
3270 | continue; |
3271 | } |
3272 | |
3273 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
3274 | if (!se) { |
3275 | continue; |
3276 | } |
3277 | |
3278 | Transform3D xform; |
3279 | if (orthogonal) { |
3280 | xform = sp->get_global_transform(); |
3281 | xform.basis = Basis::from_euler(camera_transform.basis.get_euler()); |
3282 | } else { |
3283 | xform = camera_transform; |
3284 | xform.scale_basis(sp->get_scale()); |
3285 | } |
3286 | |
3287 | if (Object::cast_to<Decal>(E)) { |
3288 | // Adjust rotation to match Decal's default orientation. |
3289 | // This makes the decal "look" in the same direction as the camera, |
3290 | // rather than pointing down relative to the camera orientation. |
3291 | xform.basis.rotate_local(Vector3(1, 0, 0), Math_TAU * 0.25); |
3292 | } |
3293 | |
3294 | undo_redo->add_do_method(sp, "set_global_transform" , xform); |
3295 | undo_redo->add_undo_method(sp, "set_global_transform" , sp->get_global_gizmo_transform()); |
3296 | } |
3297 | undo_redo->commit_action(); |
3298 | |
3299 | } break; |
3300 | case VIEW_ALIGN_ROTATION_WITH_VIEW: { |
3301 | if (!get_selected_count()) { |
3302 | break; |
3303 | } |
3304 | |
3305 | Transform3D camera_transform = camera->get_global_transform(); |
3306 | |
3307 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
3308 | |
3309 | undo_redo->create_action(TTR("Align Rotation with View" )); |
3310 | for (Node *E : selection) { |
3311 | Node3D *sp = Object::cast_to<Node3D>(E); |
3312 | if (!sp) { |
3313 | continue; |
3314 | } |
3315 | |
3316 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
3317 | if (!se) { |
3318 | continue; |
3319 | } |
3320 | |
3321 | Basis basis = camera_transform.basis; |
3322 | |
3323 | if (Object::cast_to<Decal>(E)) { |
3324 | // Adjust rotation to match Decal's default orientation. |
3325 | // This makes the decal "look" in the same direction as the camera, |
3326 | // rather than pointing down relative to the camera orientation. |
3327 | basis.rotate_local(Vector3(1, 0, 0), Math_TAU * 0.25); |
3328 | } |
3329 | |
3330 | undo_redo->add_do_method(sp, "set_rotation" , basis.get_euler_normalized()); |
3331 | undo_redo->add_undo_method(sp, "set_rotation" , sp->get_rotation()); |
3332 | } |
3333 | undo_redo->commit_action(); |
3334 | |
3335 | } break; |
3336 | case VIEW_ENVIRONMENT: { |
3337 | int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT); |
3338 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3339 | current = !current; |
3340 | if (current) { |
3341 | camera->set_environment(Ref<Resource>()); |
3342 | } else { |
3343 | camera->set_environment(Node3DEditor::get_singleton()->get_viewport_environment()); |
3344 | } |
3345 | |
3346 | view_menu->get_popup()->set_item_checked(idx, current); |
3347 | |
3348 | } break; |
3349 | case VIEW_PERSPECTIVE: { |
3350 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true); |
3351 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false); |
3352 | orthogonal = false; |
3353 | auto_orthogonal = false; |
3354 | call_deferred(SNAME("update_transform_gizmo_view" )); |
3355 | _update_camera(0); |
3356 | _update_name(); |
3357 | |
3358 | } break; |
3359 | case VIEW_ORTHOGONAL: { |
3360 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false); |
3361 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true); |
3362 | orthogonal = true; |
3363 | auto_orthogonal = false; |
3364 | call_deferred(SNAME("update_transform_gizmo_view" )); |
3365 | _update_camera(0); |
3366 | _update_name(); |
3367 | } break; |
3368 | case VIEW_SWITCH_PERSPECTIVE_ORTHOGONAL: { |
3369 | _menu_option(orthogonal ? VIEW_PERSPECTIVE : VIEW_ORTHOGONAL); |
3370 | |
3371 | } break; |
3372 | case VIEW_AUTO_ORTHOGONAL: { |
3373 | int idx = view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL); |
3374 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3375 | current = !current; |
3376 | view_menu->get_popup()->set_item_checked(idx, current); |
3377 | if (auto_orthogonal) { |
3378 | auto_orthogonal = false; |
3379 | _update_name(); |
3380 | } |
3381 | } break; |
3382 | case VIEW_LOCK_ROTATION: { |
3383 | int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION); |
3384 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3385 | _set_lock_view_rotation(!current); |
3386 | |
3387 | } break; |
3388 | case VIEW_AUDIO_LISTENER: { |
3389 | int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER); |
3390 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3391 | current = !current; |
3392 | viewport->set_as_audio_listener_3d(current); |
3393 | view_menu->get_popup()->set_item_checked(idx, current); |
3394 | |
3395 | } break; |
3396 | case VIEW_AUDIO_DOPPLER: { |
3397 | int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER); |
3398 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3399 | current = !current; |
3400 | camera->set_doppler_tracking(current ? Camera3D::DOPPLER_TRACKING_IDLE_STEP : Camera3D::DOPPLER_TRACKING_DISABLED); |
3401 | view_menu->get_popup()->set_item_checked(idx, current); |
3402 | |
3403 | } break; |
3404 | case VIEW_CINEMATIC_PREVIEW: { |
3405 | int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW); |
3406 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3407 | current = !current; |
3408 | view_menu->get_popup()->set_item_checked(idx, current); |
3409 | previewing_cinema = true; |
3410 | _toggle_cinema_preview(current); |
3411 | |
3412 | if (current) { |
3413 | preview_camera->hide(); |
3414 | } else { |
3415 | if (previewing != nullptr) { |
3416 | preview_camera->show(); |
3417 | } |
3418 | } |
3419 | } break; |
3420 | case VIEW_GIZMOS: { |
3421 | int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS); |
3422 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3423 | current = !current; |
3424 | uint32_t layers = ((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_GRID_LAYER) | (1 << MISC_TOOL_LAYER); |
3425 | if (current) { |
3426 | layers |= (1 << GIZMO_EDIT_LAYER); |
3427 | } |
3428 | camera->set_cull_mask(layers); |
3429 | view_menu->get_popup()->set_item_checked(idx, current); |
3430 | |
3431 | } break; |
3432 | case VIEW_HALF_RESOLUTION: { |
3433 | int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION); |
3434 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3435 | view_menu->get_popup()->set_item_checked(idx, !current); |
3436 | _update_shrink(); |
3437 | } break; |
3438 | case VIEW_INFORMATION: { |
3439 | int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION); |
3440 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3441 | view_menu->get_popup()->set_item_checked(idx, !current); |
3442 | |
3443 | } break; |
3444 | case VIEW_FRAME_TIME: { |
3445 | int idx = view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME); |
3446 | bool current = view_menu->get_popup()->is_item_checked(idx); |
3447 | view_menu->get_popup()->set_item_checked(idx, !current); |
3448 | |
3449 | } break; |
3450 | case VIEW_DISPLAY_NORMAL: |
3451 | case VIEW_DISPLAY_WIREFRAME: |
3452 | case VIEW_DISPLAY_OVERDRAW: |
3453 | case VIEW_DISPLAY_UNSHADED: |
3454 | case VIEW_DISPLAY_LIGHTING: |
3455 | case VIEW_DISPLAY_NORMAL_BUFFER: |
3456 | case VIEW_DISPLAY_DEBUG_SHADOW_ATLAS: |
3457 | case VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS: |
3458 | case VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO: |
3459 | case VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING: |
3460 | case VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION: |
3461 | case VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE: |
3462 | case VIEW_DISPLAY_DEBUG_SSAO: |
3463 | case VIEW_DISPLAY_DEBUG_SSIL: |
3464 | case VIEW_DISPLAY_DEBUG_PSSM_SPLITS: |
3465 | case VIEW_DISPLAY_DEBUG_DECAL_ATLAS: |
3466 | case VIEW_DISPLAY_DEBUG_SDFGI: |
3467 | case VIEW_DISPLAY_DEBUG_SDFGI_PROBES: |
3468 | case VIEW_DISPLAY_DEBUG_GI_BUFFER: |
3469 | case VIEW_DISPLAY_DEBUG_DISABLE_LOD: |
3470 | case VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS: |
3471 | case VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS: |
3472 | case VIEW_DISPLAY_DEBUG_CLUSTER_DECALS: |
3473 | case VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES: |
3474 | case VIEW_DISPLAY_DEBUG_OCCLUDERS: |
3475 | case VIEW_DISPLAY_MOTION_VECTORS: { |
3476 | static const int display_options[] = { |
3477 | VIEW_DISPLAY_NORMAL, |
3478 | VIEW_DISPLAY_WIREFRAME, |
3479 | VIEW_DISPLAY_OVERDRAW, |
3480 | VIEW_DISPLAY_UNSHADED, |
3481 | VIEW_DISPLAY_LIGHTING, |
3482 | VIEW_DISPLAY_NORMAL_BUFFER, |
3483 | VIEW_DISPLAY_DEBUG_SHADOW_ATLAS, |
3484 | VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS, |
3485 | VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO, |
3486 | VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING, |
3487 | VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION, |
3488 | VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE, |
3489 | VIEW_DISPLAY_DEBUG_SSAO, |
3490 | VIEW_DISPLAY_DEBUG_SSIL, |
3491 | VIEW_DISPLAY_DEBUG_GI_BUFFER, |
3492 | VIEW_DISPLAY_DEBUG_DISABLE_LOD, |
3493 | VIEW_DISPLAY_DEBUG_PSSM_SPLITS, |
3494 | VIEW_DISPLAY_DEBUG_DECAL_ATLAS, |
3495 | VIEW_DISPLAY_DEBUG_SDFGI, |
3496 | VIEW_DISPLAY_DEBUG_SDFGI_PROBES, |
3497 | VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS, |
3498 | VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS, |
3499 | VIEW_DISPLAY_DEBUG_CLUSTER_DECALS, |
3500 | VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES, |
3501 | VIEW_DISPLAY_DEBUG_OCCLUDERS, |
3502 | VIEW_DISPLAY_MOTION_VECTORS, |
3503 | VIEW_MAX |
3504 | }; |
3505 | static const Viewport::DebugDraw debug_draw_modes[] = { |
3506 | Viewport::DEBUG_DRAW_DISABLED, |
3507 | Viewport::DEBUG_DRAW_WIREFRAME, |
3508 | Viewport::DEBUG_DRAW_OVERDRAW, |
3509 | Viewport::DEBUG_DRAW_UNSHADED, |
3510 | Viewport::DEBUG_DRAW_LIGHTING, |
3511 | Viewport::DEBUG_DRAW_NORMAL_BUFFER, |
3512 | Viewport::DEBUG_DRAW_SHADOW_ATLAS, |
3513 | Viewport::DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS, |
3514 | Viewport::DEBUG_DRAW_VOXEL_GI_ALBEDO, |
3515 | Viewport::DEBUG_DRAW_VOXEL_GI_LIGHTING, |
3516 | Viewport::DEBUG_DRAW_VOXEL_GI_EMISSION, |
3517 | Viewport::DEBUG_DRAW_SCENE_LUMINANCE, |
3518 | Viewport::DEBUG_DRAW_SSAO, |
3519 | Viewport::DEBUG_DRAW_SSIL, |
3520 | Viewport::DEBUG_DRAW_GI_BUFFER, |
3521 | Viewport::DEBUG_DRAW_DISABLE_LOD, |
3522 | Viewport::DEBUG_DRAW_PSSM_SPLITS, |
3523 | Viewport::DEBUG_DRAW_DECAL_ATLAS, |
3524 | Viewport::DEBUG_DRAW_SDFGI, |
3525 | Viewport::DEBUG_DRAW_SDFGI_PROBES, |
3526 | Viewport::DEBUG_DRAW_CLUSTER_OMNI_LIGHTS, |
3527 | Viewport::DEBUG_DRAW_CLUSTER_SPOT_LIGHTS, |
3528 | Viewport::DEBUG_DRAW_CLUSTER_DECALS, |
3529 | Viewport::DEBUG_DRAW_CLUSTER_REFLECTION_PROBES, |
3530 | Viewport::DEBUG_DRAW_OCCLUDERS, |
3531 | Viewport::DEBUG_DRAW_MOTION_VECTORS, |
3532 | }; |
3533 | |
3534 | for (int idx = 0; display_options[idx] != VIEW_MAX; idx++) { |
3535 | int id = display_options[idx]; |
3536 | int item_idx = view_menu->get_popup()->get_item_index(id); |
3537 | if (item_idx != -1) { |
3538 | view_menu->get_popup()->set_item_checked(item_idx, id == p_option); |
3539 | } |
3540 | item_idx = display_submenu->get_item_index(id); |
3541 | if (item_idx != -1) { |
3542 | display_submenu->set_item_checked(item_idx, id == p_option); |
3543 | } |
3544 | |
3545 | if (id == p_option) { |
3546 | viewport->set_debug_draw(debug_draw_modes[idx]); |
3547 | } |
3548 | } |
3549 | } break; |
3550 | } |
3551 | } |
3552 | |
3553 | void Node3DEditorViewport::_set_auto_orthogonal() { |
3554 | if (!orthogonal && view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL))) { |
3555 | _menu_option(VIEW_ORTHOGONAL); |
3556 | auto_orthogonal = true; |
3557 | } |
3558 | } |
3559 | |
3560 | void Node3DEditorViewport::_preview_exited_scene() { |
3561 | preview_camera->disconnect("toggled" , callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview)); |
3562 | preview_camera->set_pressed(false); |
3563 | _toggle_camera_preview(false); |
3564 | preview_camera->connect("toggled" , callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview)); |
3565 | view_menu->show(); |
3566 | } |
3567 | |
3568 | void Node3DEditorViewport::_init_gizmo_instance(int p_idx) { |
3569 | uint32_t layer = 1 << (GIZMO_BASE_LAYER + p_idx); |
3570 | |
3571 | for (int i = 0; i < 3; i++) { |
3572 | move_gizmo_instance[i] = RS::get_singleton()->instance_create(); |
3573 | RS::get_singleton()->instance_set_base(move_gizmo_instance[i], spatial_editor->get_move_gizmo(i)->get_rid()); |
3574 | RS::get_singleton()->instance_set_scenario(move_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario()); |
3575 | RS::get_singleton()->instance_set_visible(move_gizmo_instance[i], false); |
3576 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF); |
3577 | RS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i], layer); |
3578 | RS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
3579 | RS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
3580 | |
3581 | move_plane_gizmo_instance[i] = RS::get_singleton()->instance_create(); |
3582 | RS::get_singleton()->instance_set_base(move_plane_gizmo_instance[i], spatial_editor->get_move_plane_gizmo(i)->get_rid()); |
3583 | RS::get_singleton()->instance_set_scenario(move_plane_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario()); |
3584 | RS::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false); |
3585 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_plane_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF); |
3586 | RS::get_singleton()->instance_set_layer_mask(move_plane_gizmo_instance[i], layer); |
3587 | RS::get_singleton()->instance_geometry_set_flag(move_plane_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
3588 | RS::get_singleton()->instance_geometry_set_flag(move_plane_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
3589 | |
3590 | rotate_gizmo_instance[i] = RS::get_singleton()->instance_create(); |
3591 | RS::get_singleton()->instance_set_base(rotate_gizmo_instance[i], spatial_editor->get_rotate_gizmo(i)->get_rid()); |
3592 | RS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario()); |
3593 | RS::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false); |
3594 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF); |
3595 | RS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i], layer); |
3596 | RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
3597 | RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
3598 | |
3599 | scale_gizmo_instance[i] = RS::get_singleton()->instance_create(); |
3600 | RS::get_singleton()->instance_set_base(scale_gizmo_instance[i], spatial_editor->get_scale_gizmo(i)->get_rid()); |
3601 | RS::get_singleton()->instance_set_scenario(scale_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario()); |
3602 | RS::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false); |
3603 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF); |
3604 | RS::get_singleton()->instance_set_layer_mask(scale_gizmo_instance[i], layer); |
3605 | RS::get_singleton()->instance_geometry_set_flag(scale_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
3606 | RS::get_singleton()->instance_geometry_set_flag(scale_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
3607 | |
3608 | scale_plane_gizmo_instance[i] = RS::get_singleton()->instance_create(); |
3609 | RS::get_singleton()->instance_set_base(scale_plane_gizmo_instance[i], spatial_editor->get_scale_plane_gizmo(i)->get_rid()); |
3610 | RS::get_singleton()->instance_set_scenario(scale_plane_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario()); |
3611 | RS::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false); |
3612 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_plane_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF); |
3613 | RS::get_singleton()->instance_set_layer_mask(scale_plane_gizmo_instance[i], layer); |
3614 | RS::get_singleton()->instance_geometry_set_flag(scale_plane_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
3615 | RS::get_singleton()->instance_geometry_set_flag(scale_plane_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
3616 | |
3617 | axis_gizmo_instance[i] = RS::get_singleton()->instance_create(); |
3618 | RS::get_singleton()->instance_set_base(axis_gizmo_instance[i], spatial_editor->get_axis_gizmo(i)->get_rid()); |
3619 | RS::get_singleton()->instance_set_scenario(axis_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario()); |
3620 | RS::get_singleton()->instance_set_visible(axis_gizmo_instance[i], true); |
3621 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(axis_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF); |
3622 | RS::get_singleton()->instance_set_layer_mask(axis_gizmo_instance[i], layer); |
3623 | RS::get_singleton()->instance_geometry_set_flag(axis_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
3624 | RS::get_singleton()->instance_geometry_set_flag(axis_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
3625 | } |
3626 | |
3627 | // Rotation white outline |
3628 | rotate_gizmo_instance[3] = RS::get_singleton()->instance_create(); |
3629 | RS::get_singleton()->instance_set_base(rotate_gizmo_instance[3], spatial_editor->get_rotate_gizmo(3)->get_rid()); |
3630 | RS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[3], get_tree()->get_root()->get_world_3d()->get_scenario()); |
3631 | RS::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false); |
3632 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[3], RS::SHADOW_CASTING_SETTING_OFF); |
3633 | RS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[3], layer); |
3634 | RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[3], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
3635 | RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[3], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
3636 | } |
3637 | |
3638 | void Node3DEditorViewport::_finish_gizmo_instances() { |
3639 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
3640 | for (int i = 0; i < 3; i++) { |
3641 | RS::get_singleton()->free(move_gizmo_instance[i]); |
3642 | RS::get_singleton()->free(move_plane_gizmo_instance[i]); |
3643 | RS::get_singleton()->free(rotate_gizmo_instance[i]); |
3644 | RS::get_singleton()->free(scale_gizmo_instance[i]); |
3645 | RS::get_singleton()->free(scale_plane_gizmo_instance[i]); |
3646 | RS::get_singleton()->free(axis_gizmo_instance[i]); |
3647 | } |
3648 | // Rotation white outline |
3649 | RS::get_singleton()->free(rotate_gizmo_instance[3]); |
3650 | } |
3651 | |
3652 | void Node3DEditorViewport::_toggle_camera_preview(bool p_activate) { |
3653 | ERR_FAIL_COND(p_activate && !preview); |
3654 | ERR_FAIL_COND(!p_activate && !previewing); |
3655 | |
3656 | previewing_camera = p_activate; |
3657 | _update_navigation_controls_visibility(); |
3658 | |
3659 | if (!p_activate) { |
3660 | previewing->disconnect("tree_exiting" , callable_mp(this, &Node3DEditorViewport::_preview_exited_scene)); |
3661 | previewing = nullptr; |
3662 | RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore |
3663 | if (!preview) { |
3664 | preview_camera->hide(); |
3665 | } |
3666 | surface->queue_redraw(); |
3667 | |
3668 | } else { |
3669 | previewing = preview; |
3670 | previewing->connect("tree_exiting" , callable_mp(this, &Node3DEditorViewport::_preview_exited_scene)); |
3671 | RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), preview->get_camera()); //replace |
3672 | surface->queue_redraw(); |
3673 | } |
3674 | } |
3675 | |
3676 | void Node3DEditorViewport::_toggle_cinema_preview(bool p_activate) { |
3677 | previewing_cinema = p_activate; |
3678 | _update_navigation_controls_visibility(); |
3679 | |
3680 | if (!previewing_cinema) { |
3681 | if (previewing != nullptr) { |
3682 | previewing->disconnect("tree_exited" , callable_mp(this, &Node3DEditorViewport::_preview_exited_scene)); |
3683 | } |
3684 | |
3685 | previewing = nullptr; |
3686 | RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore |
3687 | preview_camera->set_pressed(false); |
3688 | if (!preview) { |
3689 | preview_camera->hide(); |
3690 | } else { |
3691 | preview_camera->show(); |
3692 | } |
3693 | view_menu->show(); |
3694 | surface->queue_redraw(); |
3695 | } |
3696 | } |
3697 | |
3698 | void Node3DEditorViewport::_selection_result_pressed(int p_result) { |
3699 | if (selection_results_menu.size() <= p_result) { |
3700 | return; |
3701 | } |
3702 | |
3703 | clicked = selection_results_menu[p_result].item->get_instance_id(); |
3704 | |
3705 | if (clicked.is_valid()) { |
3706 | _select_clicked(spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT); |
3707 | } |
3708 | |
3709 | selection_results_menu.clear(); |
3710 | } |
3711 | |
3712 | void Node3DEditorViewport::() { |
3713 | selection_results.clear(); |
3714 | selection_menu->clear(); |
3715 | selection_menu->reset_size(); |
3716 | } |
3717 | |
3718 | void Node3DEditorViewport::set_can_preview(Camera3D *p_preview) { |
3719 | preview = p_preview; |
3720 | |
3721 | if (!preview_camera->is_pressed() && !previewing_cinema) { |
3722 | preview_camera->set_visible(p_preview); |
3723 | } |
3724 | } |
3725 | |
3726 | void Node3DEditorViewport::update_transform_gizmo_view() { |
3727 | if (!is_visible_in_tree()) { |
3728 | return; |
3729 | } |
3730 | |
3731 | Transform3D xform = spatial_editor->get_gizmo_transform(); |
3732 | |
3733 | Transform3D camera_xform = camera->get_transform(); |
3734 | |
3735 | if (xform.origin.is_equal_approx(camera_xform.origin)) { |
3736 | for (int i = 0; i < 3; i++) { |
3737 | RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false); |
3738 | RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false); |
3739 | RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false); |
3740 | RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false); |
3741 | RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false); |
3742 | RenderingServer::get_singleton()->instance_set_visible(axis_gizmo_instance[i], false); |
3743 | } |
3744 | // Rotation white outline |
3745 | RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false); |
3746 | return; |
3747 | } |
3748 | |
3749 | const Vector3 camz = -camera_xform.get_basis().get_column(2).normalized(); |
3750 | const Vector3 camy = -camera_xform.get_basis().get_column(1).normalized(); |
3751 | const Plane p = Plane(camz, camera_xform.origin); |
3752 | const real_t gizmo_d = MAX(Math::abs(p.distance_to(xform.origin)), CMP_EPSILON); |
3753 | const real_t d0 = camera->unproject_position(camera_xform.origin + camz * gizmo_d).y; |
3754 | const real_t d1 = camera->unproject_position(camera_xform.origin + camz * gizmo_d + camy).y; |
3755 | const real_t dd = MAX(Math::abs(d0 - d1), CMP_EPSILON); |
3756 | |
3757 | const real_t gizmo_size = EDITOR_GET("editors/3d/manipulator_gizmo_size" ); |
3758 | // At low viewport heights, multiply the gizmo scale based on the viewport height. |
3759 | // This prevents the gizmo from growing very large and going outside the viewport. |
3760 | const int viewport_base_height = 400 * MAX(1, EDSCALE); |
3761 | gizmo_scale = |
3762 | (gizmo_size / Math::abs(dd)) * MAX(1, EDSCALE) * |
3763 | MIN(viewport_base_height, subviewport_container->get_size().height) / viewport_base_height / |
3764 | subviewport_container->get_stretch_shrink(); |
3765 | Vector3 scale = Vector3(1, 1, 1) * gizmo_scale; |
3766 | |
3767 | // if the determinant is zero, we should disable the gizmo from being rendered |
3768 | // this prevents supplying bad values to the renderer and then having to filter it out again |
3769 | if (xform.basis.determinant() == 0) { |
3770 | for (int i = 0; i < 3; i++) { |
3771 | RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false); |
3772 | RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false); |
3773 | RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false); |
3774 | RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false); |
3775 | RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false); |
3776 | } |
3777 | // Rotation white outline |
3778 | RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false); |
3779 | return; |
3780 | } |
3781 | |
3782 | bool show_gizmo = spatial_editor->is_gizmo_visible() && !_edit.instant; |
3783 | for (int i = 0; i < 3; i++) { |
3784 | Transform3D axis_angle; |
3785 | if (xform.basis.get_column(i).normalized().dot(xform.basis.get_column((i + 1) % 3).normalized()) < 1.0) { |
3786 | axis_angle = axis_angle.looking_at(xform.basis.get_column(i).normalized(), xform.basis.get_column((i + 1) % 3).normalized()); |
3787 | } |
3788 | axis_angle.basis.scale(scale); |
3789 | axis_angle.origin = xform.origin; |
3790 | RenderingServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], axis_angle); |
3791 | RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE)); |
3792 | RenderingServer::get_singleton()->instance_set_transform(move_plane_gizmo_instance[i], axis_angle); |
3793 | RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE)); |
3794 | RenderingServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], axis_angle); |
3795 | RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE)); |
3796 | RenderingServer::get_singleton()->instance_set_transform(scale_gizmo_instance[i], axis_angle); |
3797 | RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE)); |
3798 | RenderingServer::get_singleton()->instance_set_transform(scale_plane_gizmo_instance[i], axis_angle); |
3799 | RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE)); |
3800 | RenderingServer::get_singleton()->instance_set_transform(axis_gizmo_instance[i], xform); |
3801 | } |
3802 | |
3803 | bool show_axes = spatial_editor->is_gizmo_visible() && _edit.mode != TRANSFORM_NONE; |
3804 | RenderingServer *rs = RenderingServer::get_singleton(); |
3805 | rs->instance_set_visible(axis_gizmo_instance[0], show_axes && (_edit.plane == TRANSFORM_X_AXIS || _edit.plane == TRANSFORM_XY || _edit.plane == TRANSFORM_XZ)); |
3806 | rs->instance_set_visible(axis_gizmo_instance[1], show_axes && (_edit.plane == TRANSFORM_Y_AXIS || _edit.plane == TRANSFORM_XY || _edit.plane == TRANSFORM_YZ)); |
3807 | rs->instance_set_visible(axis_gizmo_instance[2], show_axes && (_edit.plane == TRANSFORM_Z_AXIS || _edit.plane == TRANSFORM_XZ || _edit.plane == TRANSFORM_YZ)); |
3808 | |
3809 | // Rotation white outline |
3810 | xform.orthonormalize(); |
3811 | xform.basis.scale(scale); |
3812 | RenderingServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[3], xform); |
3813 | RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE)); |
3814 | } |
3815 | |
3816 | void Node3DEditorViewport::set_state(const Dictionary &p_state) { |
3817 | if (p_state.has("position" )) { |
3818 | cursor.pos = p_state["position" ]; |
3819 | } |
3820 | if (p_state.has("x_rotation" )) { |
3821 | cursor.x_rot = p_state["x_rotation" ]; |
3822 | } |
3823 | if (p_state.has("y_rotation" )) { |
3824 | cursor.y_rot = p_state["y_rotation" ]; |
3825 | } |
3826 | if (p_state.has("distance" )) { |
3827 | cursor.distance = p_state["distance" ]; |
3828 | } |
3829 | if (p_state.has("orthogonal" )) { |
3830 | bool orth = p_state["orthogonal" ]; |
3831 | _menu_option(orth ? VIEW_ORTHOGONAL : VIEW_PERSPECTIVE); |
3832 | } |
3833 | if (p_state.has("view_type" )) { |
3834 | view_type = ViewType(p_state["view_type" ].operator int()); |
3835 | _update_name(); |
3836 | } |
3837 | if (p_state.has("auto_orthogonal" )) { |
3838 | auto_orthogonal = p_state["auto_orthogonal" ]; |
3839 | _update_name(); |
3840 | } |
3841 | if (p_state.has("auto_orthogonal_enabled" )) { |
3842 | bool enabled = p_state["auto_orthogonal_enabled" ]; |
3843 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL), enabled); |
3844 | } |
3845 | if (p_state.has("display_mode" )) { |
3846 | int display = p_state["display_mode" ]; |
3847 | |
3848 | int idx = view_menu->get_popup()->get_item_index(display); |
3849 | if (idx != -1 && !view_menu->get_popup()->is_item_checked(idx)) { |
3850 | _menu_option(display); |
3851 | } else { |
3852 | idx = display_submenu->get_item_index(display); |
3853 | if (idx != -1 && !display_submenu->is_item_checked(idx)) { |
3854 | _menu_option(display); |
3855 | } |
3856 | } |
3857 | } |
3858 | if (p_state.has("lock_rotation" )) { |
3859 | _set_lock_view_rotation(p_state["lock_rotation" ]); |
3860 | } |
3861 | if (p_state.has("use_environment" )) { |
3862 | bool env = p_state["use_environment" ]; |
3863 | |
3864 | if (env != camera->get_environment().is_valid()) { |
3865 | _menu_option(VIEW_ENVIRONMENT); |
3866 | } |
3867 | } |
3868 | if (p_state.has("listener" )) { |
3869 | bool listener = p_state["listener" ]; |
3870 | |
3871 | int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER); |
3872 | viewport->set_as_audio_listener_3d(listener); |
3873 | view_menu->get_popup()->set_item_checked(idx, listener); |
3874 | } |
3875 | if (p_state.has("doppler" )) { |
3876 | bool doppler = p_state["doppler" ]; |
3877 | |
3878 | int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER); |
3879 | camera->set_doppler_tracking(doppler ? Camera3D::DOPPLER_TRACKING_IDLE_STEP : Camera3D::DOPPLER_TRACKING_DISABLED); |
3880 | view_menu->get_popup()->set_item_checked(idx, doppler); |
3881 | } |
3882 | if (p_state.has("gizmos" )) { |
3883 | bool gizmos = p_state["gizmos" ]; |
3884 | |
3885 | int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS); |
3886 | if (view_menu->get_popup()->is_item_checked(idx) != gizmos) { |
3887 | _menu_option(VIEW_GIZMOS); |
3888 | } |
3889 | } |
3890 | if (p_state.has("information" )) { |
3891 | bool information = p_state["information" ]; |
3892 | |
3893 | int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION); |
3894 | if (view_menu->get_popup()->is_item_checked(idx) != information) { |
3895 | _menu_option(VIEW_INFORMATION); |
3896 | } |
3897 | } |
3898 | if (p_state.has("frame_time" )) { |
3899 | bool fps = p_state["frame_time" ]; |
3900 | |
3901 | int idx = view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME); |
3902 | if (view_menu->get_popup()->is_item_checked(idx) != fps) { |
3903 | _menu_option(VIEW_FRAME_TIME); |
3904 | } |
3905 | } |
3906 | if (p_state.has("half_res" )) { |
3907 | bool half_res = p_state["half_res" ]; |
3908 | |
3909 | int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION); |
3910 | view_menu->get_popup()->set_item_checked(idx, half_res); |
3911 | _update_shrink(); |
3912 | } |
3913 | if (p_state.has("cinematic_preview" )) { |
3914 | previewing_cinema = p_state["cinematic_preview" ]; |
3915 | |
3916 | int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW); |
3917 | view_menu->get_popup()->set_item_checked(idx, previewing_cinema); |
3918 | } |
3919 | |
3920 | if (preview_camera->is_connected("toggled" , callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview))) { |
3921 | preview_camera->disconnect("toggled" , callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview)); |
3922 | } |
3923 | if (p_state.has("previewing" )) { |
3924 | Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing" ]); |
3925 | if (Object::cast_to<Camera3D>(pv)) { |
3926 | previewing = Object::cast_to<Camera3D>(pv); |
3927 | previewing->connect("tree_exiting" , callable_mp(this, &Node3DEditorViewport::_preview_exited_scene)); |
3928 | RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), previewing->get_camera()); //replace |
3929 | surface->queue_redraw(); |
3930 | preview_camera->set_pressed(true); |
3931 | preview_camera->show(); |
3932 | } |
3933 | } |
3934 | preview_camera->connect("toggled" , callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview)); |
3935 | } |
3936 | |
3937 | Dictionary Node3DEditorViewport::get_state() const { |
3938 | Dictionary d; |
3939 | d["position" ] = cursor.pos; |
3940 | d["x_rotation" ] = cursor.x_rot; |
3941 | d["y_rotation" ] = cursor.y_rot; |
3942 | d["distance" ] = cursor.distance; |
3943 | d["use_environment" ] = camera->get_environment().is_valid(); |
3944 | d["orthogonal" ] = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL; |
3945 | d["view_type" ] = view_type; |
3946 | d["auto_orthogonal" ] = auto_orthogonal; |
3947 | d["auto_orthogonal_enabled" ] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL)); |
3948 | |
3949 | // Find selected display mode. |
3950 | int display_mode = VIEW_DISPLAY_NORMAL; |
3951 | for (int i = VIEW_DISPLAY_NORMAL; i < VIEW_DISPLAY_ADVANCED; i++) { |
3952 | if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(i))) { |
3953 | display_mode = i; |
3954 | break; |
3955 | } |
3956 | } |
3957 | for (int i = VIEW_DISPLAY_ADVANCED + 1; i < VIEW_DISPLAY_MAX; i++) { |
3958 | if (display_submenu->is_item_checked(display_submenu->get_item_index(i))) { |
3959 | display_mode = i; |
3960 | break; |
3961 | } |
3962 | } |
3963 | d["display_mode" ] = display_mode; |
3964 | |
3965 | d["listener" ] = viewport->is_audio_listener_3d(); |
3966 | d["doppler" ] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER)); |
3967 | d["gizmos" ] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS)); |
3968 | d["information" ] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION)); |
3969 | d["frame_time" ] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME)); |
3970 | d["half_res" ] = subviewport_container->get_stretch_shrink() > 1; |
3971 | d["cinematic_preview" ] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW)); |
3972 | if (previewing) { |
3973 | d["previewing" ] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing); |
3974 | } |
3975 | d["lock_rotation" ] = lock_rotation; |
3976 | |
3977 | return d; |
3978 | } |
3979 | |
3980 | void Node3DEditorViewport::_bind_methods() { |
3981 | ClassDB::bind_method(D_METHOD("update_transform_gizmo_view" ), &Node3DEditorViewport::update_transform_gizmo_view); // Used by call_deferred. |
3982 | |
3983 | ADD_SIGNAL(MethodInfo("toggle_maximize_view" , PropertyInfo(Variant::OBJECT, "viewport" ))); |
3984 | ADD_SIGNAL(MethodInfo("clicked" , PropertyInfo(Variant::OBJECT, "viewport" ))); |
3985 | } |
3986 | |
3987 | void Node3DEditorViewport::reset() { |
3988 | orthogonal = false; |
3989 | auto_orthogonal = false; |
3990 | lock_rotation = false; |
3991 | message_time = 0; |
3992 | message = "" ; |
3993 | last_message = "" ; |
3994 | view_type = VIEW_TYPE_USER; |
3995 | |
3996 | cursor = Cursor(); |
3997 | _update_name(); |
3998 | } |
3999 | |
4000 | void Node3DEditorViewport::focus_selection() { |
4001 | Vector3 center; |
4002 | int count = 0; |
4003 | |
4004 | const List<Node *> &selection = editor_selection->get_selected_node_list(); |
4005 | |
4006 | for (Node *E : selection) { |
4007 | Node3D *sp = Object::cast_to<Node3D>(E); |
4008 | if (!sp) { |
4009 | continue; |
4010 | } |
4011 | |
4012 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
4013 | if (!se) { |
4014 | continue; |
4015 | } |
4016 | |
4017 | if (se->gizmo.is_valid()) { |
4018 | for (const KeyValue<int, Transform3D> &GE : se->subgizmos) { |
4019 | center += se->gizmo->get_subgizmo_transform(GE.key).origin; |
4020 | count++; |
4021 | } |
4022 | } |
4023 | |
4024 | center += sp->get_global_gizmo_transform().origin; |
4025 | count++; |
4026 | } |
4027 | |
4028 | if (count != 0) { |
4029 | center /= count; |
4030 | } |
4031 | |
4032 | cursor.pos = center; |
4033 | } |
4034 | |
4035 | void Node3DEditorViewport::assign_pending_data_pointers(Node3D *p_preview_node, AABB *p_preview_bounds, AcceptDialog *p_accept) { |
4036 | preview_node = p_preview_node; |
4037 | preview_bounds = p_preview_bounds; |
4038 | accept = p_accept; |
4039 | } |
4040 | |
4041 | Vector3 Node3DEditorViewport::_get_instance_position(const Point2 &p_pos) const { |
4042 | const float MAX_DISTANCE = 50.0; |
4043 | const float FALLBACK_DISTANCE = 5.0; |
4044 | |
4045 | Vector3 world_ray = _get_ray(p_pos); |
4046 | Vector3 world_pos = _get_ray_pos(p_pos); |
4047 | |
4048 | PhysicsDirectSpaceState3D *ss = get_tree()->get_root()->get_world_3d()->get_direct_space_state(); |
4049 | |
4050 | PhysicsDirectSpaceState3D::RayParameters ray_params; |
4051 | ray_params.from = world_pos; |
4052 | ray_params.to = world_pos + world_ray * camera->get_far(); |
4053 | |
4054 | PhysicsDirectSpaceState3D::RayResult result; |
4055 | if (ss->intersect_ray(ray_params, result)) { |
4056 | return result.position; |
4057 | } |
4058 | |
4059 | const bool is_orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL; |
4060 | |
4061 | // The XZ plane. |
4062 | Vector3 intersection; |
4063 | Plane plane(Vector3(0, 1, 0)); |
4064 | if (plane.intersects_ray(world_pos, world_ray, &intersection)) { |
4065 | if (is_orthogonal || world_pos.distance_to(intersection) <= MAX_DISTANCE) { |
4066 | return intersection; |
4067 | } |
4068 | } |
4069 | |
4070 | // Plane facing the camera using fallback distance. |
4071 | if (is_orthogonal) { |
4072 | plane = Plane(world_ray, cursor.pos - world_ray * (cursor.distance - FALLBACK_DISTANCE)); |
4073 | } else { |
4074 | plane = Plane(world_ray, world_pos + world_ray * FALLBACK_DISTANCE); |
4075 | } |
4076 | if (plane.intersects_ray(world_pos, world_ray, &intersection)) { |
4077 | return intersection; |
4078 | } |
4079 | |
4080 | // Not likely, but just in case... |
4081 | return world_pos + world_ray * FALLBACK_DISTANCE; |
4082 | } |
4083 | |
4084 | AABB Node3DEditorViewport::_calculate_spatial_bounds(const Node3D *p_parent, bool p_exclude_top_level_transform) { |
4085 | AABB bounds; |
4086 | |
4087 | const VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(p_parent); |
4088 | if (visual_instance) { |
4089 | bounds = visual_instance->get_aabb(); |
4090 | } |
4091 | |
4092 | for (int i = 0; i < p_parent->get_child_count(); i++) { |
4093 | Node3D *child = Object::cast_to<Node3D>(p_parent->get_child(i)); |
4094 | if (child) { |
4095 | AABB child_bounds = _calculate_spatial_bounds(child, false); |
4096 | |
4097 | if (bounds.size == Vector3() && p_parent) { |
4098 | bounds = child_bounds; |
4099 | } else { |
4100 | bounds.merge_with(child_bounds); |
4101 | } |
4102 | } |
4103 | } |
4104 | |
4105 | if (bounds.size == Vector3() && !p_parent) { |
4106 | bounds = AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4)); |
4107 | } |
4108 | |
4109 | if (!p_exclude_top_level_transform) { |
4110 | bounds = p_parent->get_transform().xform(bounds); |
4111 | } |
4112 | |
4113 | return bounds; |
4114 | } |
4115 | |
4116 | Node *Node3DEditorViewport::_sanitize_preview_node(Node *p_node) const { |
4117 | Node3D *node_3d = Object::cast_to<Node3D>(p_node); |
4118 | if (node_3d == nullptr) { |
4119 | Node3D *replacement_node = memnew(Node3D); |
4120 | replacement_node->set_name(p_node->get_name()); |
4121 | p_node->replace_by(replacement_node); |
4122 | memdelete(p_node); |
4123 | p_node = replacement_node; |
4124 | } else { |
4125 | VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(node_3d); |
4126 | if (visual_instance == nullptr) { |
4127 | Node3D *replacement_node = memnew(Node3D); |
4128 | replacement_node->set_name(node_3d->get_name()); |
4129 | replacement_node->set_visible(node_3d->is_visible()); |
4130 | replacement_node->set_transform(node_3d->get_transform()); |
4131 | replacement_node->set_rotation_edit_mode(node_3d->get_rotation_edit_mode()); |
4132 | replacement_node->set_rotation_order(node_3d->get_rotation_order()); |
4133 | replacement_node->set_as_top_level(node_3d->is_set_as_top_level()); |
4134 | p_node->replace_by(replacement_node); |
4135 | memdelete(p_node); |
4136 | p_node = replacement_node; |
4137 | } |
4138 | } |
4139 | |
4140 | for (int i = 0; i < p_node->get_child_count(); i++) { |
4141 | _sanitize_preview_node(p_node->get_child(i)); |
4142 | } |
4143 | |
4144 | return p_node; |
4145 | } |
4146 | |
4147 | void Node3DEditorViewport::_create_preview_node(const Vector<String> &files) const { |
4148 | for (int i = 0; i < files.size(); i++) { |
4149 | String path = files[i]; |
4150 | Ref<Resource> res = ResourceLoader::load(path); |
4151 | ERR_CONTINUE(res.is_null()); |
4152 | Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res)); |
4153 | Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res)); |
4154 | if (mesh != nullptr || scene != nullptr) { |
4155 | if (mesh != nullptr) { |
4156 | MeshInstance3D *mesh_instance = memnew(MeshInstance3D); |
4157 | mesh_instance->set_mesh(mesh); |
4158 | preview_node->add_child(mesh_instance); |
4159 | } else { |
4160 | if (scene.is_valid()) { |
4161 | Node *instance = scene->instantiate(); |
4162 | if (instance) { |
4163 | instance = _sanitize_preview_node(instance); |
4164 | preview_node->add_child(instance); |
4165 | } |
4166 | } |
4167 | } |
4168 | EditorNode::get_singleton()->get_scene_root()->add_child(preview_node); |
4169 | } |
4170 | } |
4171 | *preview_bounds = _calculate_spatial_bounds(preview_node); |
4172 | } |
4173 | |
4174 | void Node3DEditorViewport::_remove_preview_node() { |
4175 | if (preview_node->get_parent()) { |
4176 | for (int i = preview_node->get_child_count() - 1; i >= 0; i--) { |
4177 | Node *node = preview_node->get_child(i); |
4178 | node->queue_free(); |
4179 | preview_node->remove_child(node); |
4180 | } |
4181 | EditorNode::get_singleton()->get_scene_root()->remove_child(preview_node); |
4182 | } |
4183 | } |
4184 | |
4185 | bool Node3DEditorViewport::_apply_preview_material(ObjectID p_target, const Point2 &p_point) const { |
4186 | _reset_preview_material(); |
4187 | |
4188 | if (p_target.is_null()) { |
4189 | return false; |
4190 | } |
4191 | |
4192 | spatial_editor->set_preview_material_target(p_target); |
4193 | |
4194 | Object *target_inst = ObjectDB::get_instance(p_target); |
4195 | |
4196 | bool is_ctrl = Input::get_singleton()->is_key_pressed(Key::CTRL); |
4197 | |
4198 | MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(target_inst); |
4199 | if (is_ctrl && mesh_instance) { |
4200 | Ref<Mesh> mesh = mesh_instance->get_mesh(); |
4201 | int surface_count = mesh->get_surface_count(); |
4202 | |
4203 | Vector3 world_ray = _get_ray(p_point); |
4204 | Vector3 world_pos = _get_ray_pos(p_point); |
4205 | |
4206 | int closest_surface = -1; |
4207 | float closest_dist = 1e20; |
4208 | |
4209 | Transform3D gt = mesh_instance->get_global_transform(); |
4210 | |
4211 | Transform3D ai = gt.affine_inverse(); |
4212 | Vector3 xform_ray = ai.basis.xform(world_ray).normalized(); |
4213 | Vector3 xform_pos = ai.xform(world_pos); |
4214 | |
4215 | for (int surface_idx = 0; surface_idx < surface_count; surface_idx++) { |
4216 | Ref<TriangleMesh> surface_mesh = mesh->generate_surface_triangle_mesh(surface_idx); |
4217 | |
4218 | Vector3 rpos, rnorm; |
4219 | if (surface_mesh->intersect_ray(xform_pos, xform_ray, rpos, rnorm)) { |
4220 | Vector3 hitpos = gt.xform(rpos); |
4221 | |
4222 | const real_t dist = world_pos.distance_to(hitpos); |
4223 | |
4224 | if (dist < 0) { |
4225 | continue; |
4226 | } |
4227 | |
4228 | if (dist < closest_dist) { |
4229 | closest_surface = surface_idx; |
4230 | closest_dist = dist; |
4231 | } |
4232 | } |
4233 | } |
4234 | |
4235 | if (closest_surface == -1) { |
4236 | return false; |
4237 | } |
4238 | |
4239 | if (spatial_editor->get_preview_material() != mesh_instance->get_surface_override_material(closest_surface)) { |
4240 | spatial_editor->set_preview_material_surface(closest_surface); |
4241 | spatial_editor->set_preview_reset_material(mesh_instance->get_surface_override_material(closest_surface)); |
4242 | mesh_instance->set_surface_override_material(closest_surface, spatial_editor->get_preview_material()); |
4243 | } |
4244 | |
4245 | return true; |
4246 | } |
4247 | |
4248 | GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(target_inst); |
4249 | if (geometry_instance && spatial_editor->get_preview_material() != geometry_instance->get_material_override()) { |
4250 | spatial_editor->set_preview_reset_material(geometry_instance->get_material_override()); |
4251 | geometry_instance->set_material_override(spatial_editor->get_preview_material()); |
4252 | return true; |
4253 | } |
4254 | |
4255 | return false; |
4256 | } |
4257 | |
4258 | void Node3DEditorViewport::_reset_preview_material() const { |
4259 | ObjectID last_target = spatial_editor->get_preview_material_target(); |
4260 | if (last_target.is_null()) { |
4261 | return; |
4262 | } |
4263 | Object *last_target_inst = ObjectDB::get_instance(last_target); |
4264 | |
4265 | MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(last_target_inst); |
4266 | GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(last_target_inst); |
4267 | if (mesh_instance && spatial_editor->get_preview_material_surface() != -1) { |
4268 | mesh_instance->set_surface_override_material(spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_reset_material()); |
4269 | spatial_editor->set_preview_material_surface(-1); |
4270 | } else if (geometry_instance) { |
4271 | geometry_instance->set_material_override(spatial_editor->get_preview_reset_material()); |
4272 | } |
4273 | } |
4274 | |
4275 | void Node3DEditorViewport::_remove_preview_material() { |
4276 | preview_material_label->hide(); |
4277 | preview_material_label_desc->hide(); |
4278 | |
4279 | spatial_editor->set_preview_material(Ref<Material>()); |
4280 | spatial_editor->set_preview_reset_material(Ref<Material>()); |
4281 | spatial_editor->set_preview_material_target(ObjectID()); |
4282 | spatial_editor->set_preview_material_surface(-1); |
4283 | } |
4284 | |
4285 | bool Node3DEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) { |
4286 | if (p_desired_node->get_scene_file_path() == p_target_scene_path) { |
4287 | return true; |
4288 | } |
4289 | |
4290 | int childCount = p_desired_node->get_child_count(); |
4291 | for (int i = 0; i < childCount; i++) { |
4292 | Node *child = p_desired_node->get_child(i); |
4293 | if (_cyclical_dependency_exists(p_target_scene_path, child)) { |
4294 | return true; |
4295 | } |
4296 | } |
4297 | return false; |
4298 | } |
4299 | |
4300 | bool Node3DEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) { |
4301 | Ref<Resource> res = ResourceLoader::load(path); |
4302 | ERR_FAIL_COND_V(res.is_null(), false); |
4303 | |
4304 | Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res)); |
4305 | Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res)); |
4306 | |
4307 | Node *instantiated_scene = nullptr; |
4308 | |
4309 | if (mesh != nullptr || scene != nullptr) { |
4310 | if (mesh != nullptr) { |
4311 | MeshInstance3D *mesh_instance = memnew(MeshInstance3D); |
4312 | mesh_instance->set_mesh(mesh); |
4313 | |
4314 | // Adjust casing according to project setting. The file name is expected to be in snake_case, but will work for others. |
4315 | String name = path.get_file().get_basename(); |
4316 | mesh_instance->set_name(Node::adjust_name_casing(name)); |
4317 | |
4318 | instantiated_scene = mesh_instance; |
4319 | } else { |
4320 | if (!scene.is_valid()) { // invalid scene |
4321 | return false; |
4322 | } else { |
4323 | instantiated_scene = scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE); |
4324 | } |
4325 | } |
4326 | } |
4327 | |
4328 | if (instantiated_scene == nullptr) { |
4329 | return false; |
4330 | } |
4331 | |
4332 | if (!EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path().is_empty()) { // Cyclic instantiation. |
4333 | if (_cyclical_dependency_exists(EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path(), instantiated_scene)) { |
4334 | memdelete(instantiated_scene); |
4335 | return false; |
4336 | } |
4337 | } |
4338 | |
4339 | if (scene != nullptr) { |
4340 | instantiated_scene->set_scene_file_path(ProjectSettings::get_singleton()->localize_path(path)); |
4341 | } |
4342 | |
4343 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
4344 | undo_redo->add_do_method(parent, "add_child" , instantiated_scene, true); |
4345 | undo_redo->add_do_method(instantiated_scene, "set_owner" , EditorNode::get_singleton()->get_edited_scene()); |
4346 | undo_redo->add_do_reference(instantiated_scene); |
4347 | undo_redo->add_undo_method(parent, "remove_child" , instantiated_scene); |
4348 | |
4349 | String new_name = parent->validate_child_name(instantiated_scene); |
4350 | EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton(); |
4351 | undo_redo->add_do_method(ed, "live_debug_instantiate_node" , EditorNode::get_singleton()->get_edited_scene()->get_path_to(parent), path, new_name); |
4352 | undo_redo->add_undo_method(ed, "live_debug_remove_node" , NodePath(String(EditorNode::get_singleton()->get_edited_scene()->get_path_to(parent)) + "/" + new_name)); |
4353 | |
4354 | Node3D *node3d = Object::cast_to<Node3D>(instantiated_scene); |
4355 | if (node3d) { |
4356 | Transform3D gl_transform; |
4357 | Node3D *parent_node3d = Object::cast_to<Node3D>(parent); |
4358 | if (parent_node3d) { |
4359 | gl_transform = parent_node3d->get_global_gizmo_transform(); |
4360 | } |
4361 | |
4362 | gl_transform.origin = preview_node_pos; |
4363 | gl_transform.basis *= node3d->get_transform().basis; |
4364 | |
4365 | undo_redo->add_do_method(instantiated_scene, "set_global_transform" , gl_transform); |
4366 | } |
4367 | |
4368 | return true; |
4369 | } |
4370 | |
4371 | void Node3DEditorViewport::_perform_drop_data() { |
4372 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
4373 | if (spatial_editor->get_preview_material_target().is_valid()) { |
4374 | GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(ObjectDB::get_instance(spatial_editor->get_preview_material_target())); |
4375 | MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(ObjectDB::get_instance(spatial_editor->get_preview_material_target())); |
4376 | if (mesh_instance && spatial_editor->get_preview_material_surface() != -1) { |
4377 | undo_redo->create_action(vformat(TTR("Set Surface %d Override Material" ), spatial_editor->get_preview_material_surface())); |
4378 | undo_redo->add_do_method(geometry_instance, "set_surface_override_material" , spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_material()); |
4379 | undo_redo->add_undo_method(geometry_instance, "set_surface_override_material" , spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_reset_material()); |
4380 | undo_redo->commit_action(); |
4381 | } else if (geometry_instance) { |
4382 | undo_redo->create_action(TTR("Set Material Override" )); |
4383 | undo_redo->add_do_method(geometry_instance, "set_material_override" , spatial_editor->get_preview_material()); |
4384 | undo_redo->add_undo_method(geometry_instance, "set_material_override" , spatial_editor->get_preview_reset_material()); |
4385 | undo_redo->commit_action(); |
4386 | } |
4387 | |
4388 | _remove_preview_material(); |
4389 | return; |
4390 | } |
4391 | |
4392 | _remove_preview_node(); |
4393 | |
4394 | Vector<String> error_files; |
4395 | |
4396 | undo_redo->create_action(TTR("Create Node" )); |
4397 | |
4398 | for (int i = 0; i < selected_files.size(); i++) { |
4399 | String path = selected_files[i]; |
4400 | Ref<Resource> res = ResourceLoader::load(path); |
4401 | if (res.is_null()) { |
4402 | continue; |
4403 | } |
4404 | Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res)); |
4405 | Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res)); |
4406 | if (mesh != nullptr || scene != nullptr) { |
4407 | bool success = _create_instance(target_node, path, drop_pos); |
4408 | if (!success) { |
4409 | error_files.push_back(path); |
4410 | } |
4411 | } |
4412 | } |
4413 | |
4414 | undo_redo->commit_action(); |
4415 | |
4416 | if (error_files.size() > 0) { |
4417 | String files_str; |
4418 | for (int i = 0; i < error_files.size(); i++) { |
4419 | files_str += error_files[i].get_file().get_basename() + "," ; |
4420 | } |
4421 | files_str = files_str.substr(0, files_str.length() - 1); |
4422 | accept->set_text(vformat(TTR("Error instantiating scene from %s" ), files_str.get_data())); |
4423 | accept->popup_centered(); |
4424 | } |
4425 | } |
4426 | |
4427 | bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
4428 | preview_node_viewport_pos = p_point; |
4429 | |
4430 | bool can_instantiate = false; |
4431 | |
4432 | if (!preview_node->is_inside_tree() && spatial_editor->get_preview_material().is_null()) { |
4433 | Dictionary d = p_data; |
4434 | if (d.has("type" ) && (String(d["type" ]) == "files" )) { |
4435 | Vector<String> files = d["files" ]; |
4436 | |
4437 | List<String> scene_extensions; |
4438 | ResourceLoader::get_recognized_extensions_for_type("PackedScene" , &scene_extensions); |
4439 | List<String> mesh_extensions; |
4440 | ResourceLoader::get_recognized_extensions_for_type("Mesh" , &mesh_extensions); |
4441 | List<String> material_extensions; |
4442 | ResourceLoader::get_recognized_extensions_for_type("Material" , &material_extensions); |
4443 | List<String> texture_extensions; |
4444 | ResourceLoader::get_recognized_extensions_for_type("Texture" , &texture_extensions); |
4445 | |
4446 | for (int i = 0; i < files.size(); i++) { |
4447 | String extension = files[i].get_extension().to_lower(); |
4448 | |
4449 | // Check if dragged files with mesh or scene extension can be created at least once. |
4450 | if (mesh_extensions.find(extension) || |
4451 | scene_extensions.find(extension) || |
4452 | material_extensions.find(extension) || |
4453 | texture_extensions.find(extension)) { |
4454 | Ref<Resource> res = ResourceLoader::load(files[i]); |
4455 | if (res.is_null()) { |
4456 | continue; |
4457 | } |
4458 | Ref<PackedScene> scn = res; |
4459 | Ref<Mesh> mesh = res; |
4460 | Ref<Material> mat = res; |
4461 | Ref<Texture2D> tex = res; |
4462 | if (scn.is_valid()) { |
4463 | Node *instantiated_scene = scn->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE); |
4464 | if (!instantiated_scene) { |
4465 | continue; |
4466 | } |
4467 | memdelete(instantiated_scene); |
4468 | } else if (mat.is_valid()) { |
4469 | Ref<BaseMaterial3D> base_mat = res; |
4470 | Ref<ShaderMaterial> shader_mat = res; |
4471 | |
4472 | if (base_mat.is_null() && !shader_mat.is_null()) { |
4473 | break; |
4474 | } |
4475 | |
4476 | spatial_editor->set_preview_material(mat); |
4477 | break; |
4478 | } else if (mesh.is_valid()) { |
4479 | // Let the mesh pass. |
4480 | } else if (tex.is_valid()) { |
4481 | Ref<StandardMaterial3D> new_mat = memnew(StandardMaterial3D); |
4482 | new_mat->set_texture(BaseMaterial3D::TEXTURE_ALBEDO, tex); |
4483 | |
4484 | spatial_editor->set_preview_material(new_mat); |
4485 | break; |
4486 | } else { |
4487 | continue; |
4488 | } |
4489 | can_instantiate = true; |
4490 | break; |
4491 | } |
4492 | } |
4493 | if (can_instantiate) { |
4494 | _create_preview_node(files); |
4495 | preview_node->hide(); |
4496 | } |
4497 | } |
4498 | } else { |
4499 | if (preview_node->is_inside_tree()) { |
4500 | can_instantiate = true; |
4501 | } |
4502 | } |
4503 | |
4504 | if (can_instantiate) { |
4505 | update_preview_node = true; |
4506 | return true; |
4507 | } |
4508 | |
4509 | if (spatial_editor->get_preview_material().is_valid()) { |
4510 | preview_material_label->show(); |
4511 | preview_material_label_desc->show(); |
4512 | |
4513 | ObjectID new_preview_material_target = _select_ray(p_point); |
4514 | return _apply_preview_material(new_preview_material_target, p_point); |
4515 | } |
4516 | |
4517 | return false; |
4518 | } |
4519 | |
4520 | void Node3DEditorViewport::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
4521 | if (!can_drop_data_fw(p_point, p_data, p_from)) { |
4522 | return; |
4523 | } |
4524 | |
4525 | bool is_shift = Input::get_singleton()->is_key_pressed(Key::SHIFT); |
4526 | bool is_ctrl = Input::get_singleton()->is_key_pressed(Key::CTRL); |
4527 | |
4528 | selected_files.clear(); |
4529 | Dictionary d = p_data; |
4530 | if (d.has("type" ) && String(d["type" ]) == "files" ) { |
4531 | selected_files = d["files" ]; |
4532 | } |
4533 | |
4534 | List<Node *> selected_nodes = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list(); |
4535 | Node *root_node = EditorNode::get_singleton()->get_edited_scene(); |
4536 | if (selected_nodes.size() == 1) { |
4537 | Node *selected_node = selected_nodes[0]; |
4538 | target_node = root_node; |
4539 | if (is_ctrl) { |
4540 | target_node = selected_node; |
4541 | } else if (is_shift && selected_node != root_node) { |
4542 | target_node = selected_node->get_parent(); |
4543 | } |
4544 | } else if (selected_nodes.size() == 0) { |
4545 | if (root_node) { |
4546 | target_node = root_node; |
4547 | } else { |
4548 | // Create a root node so we can add child nodes to it. |
4549 | SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D)); |
4550 | target_node = get_tree()->get_edited_scene_root(); |
4551 | } |
4552 | } else { |
4553 | accept->set_text(TTR("Cannot drag and drop into multiple selected nodes." )); |
4554 | accept->popup_centered(); |
4555 | _remove_preview_node(); |
4556 | return; |
4557 | } |
4558 | |
4559 | drop_pos = p_point; |
4560 | |
4561 | _perform_drop_data(); |
4562 | } |
4563 | |
4564 | void Node3DEditorViewport::begin_transform(TransformMode p_mode, bool instant) { |
4565 | if (get_selected_count() > 0) { |
4566 | _edit.mode = p_mode; |
4567 | _compute_edit(_edit.mouse_pos); |
4568 | _edit.instant = instant; |
4569 | _edit.snap = spatial_editor->is_snap_enabled(); |
4570 | update_transform_gizmo_view(); |
4571 | set_process_input(instant); |
4572 | } |
4573 | } |
4574 | |
4575 | // Apply the current transform operation. |
4576 | void Node3DEditorViewport::commit_transform() { |
4577 | ERR_FAIL_COND(_edit.mode == TRANSFORM_NONE); |
4578 | static const char *_transform_name[4] = { |
4579 | TTRC("None" ), |
4580 | TTRC("Rotate" ), |
4581 | // TRANSLATORS: This refers to the movement that changes the position of an object. |
4582 | TTRC("Translate" ), |
4583 | TTRC("Scale" ), |
4584 | }; |
4585 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
4586 | undo_redo->create_action(_transform_name[_edit.mode]); |
4587 | |
4588 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
4589 | |
4590 | for (List<Node *>::Element *E = selection.front(); E; E = E->next()) { |
4591 | Node3D *sp = Object::cast_to<Node3D>(E->get()); |
4592 | if (!sp) { |
4593 | continue; |
4594 | } |
4595 | |
4596 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
4597 | if (!se) { |
4598 | continue; |
4599 | } |
4600 | |
4601 | undo_redo->add_do_method(sp, "set_global_transform" , sp->get_global_gizmo_transform()); |
4602 | undo_redo->add_undo_method(sp, "set_global_transform" , se->original); |
4603 | } |
4604 | undo_redo->commit_action(); |
4605 | |
4606 | finish_transform(); |
4607 | set_message("" ); |
4608 | } |
4609 | |
4610 | void Node3DEditorViewport::apply_transform(Vector3 p_motion, double p_snap) { |
4611 | bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); |
4612 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
4613 | for (Node *E : selection) { |
4614 | Node3D *sp = Object::cast_to<Node3D>(E); |
4615 | if (!sp) { |
4616 | continue; |
4617 | } |
4618 | |
4619 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
4620 | if (!se) { |
4621 | continue; |
4622 | } |
4623 | |
4624 | if (sp->has_meta("_edit_lock_" )) { |
4625 | continue; |
4626 | } |
4627 | |
4628 | if (se->gizmo.is_valid()) { |
4629 | for (KeyValue<int, Transform3D> &GE : se->subgizmos) { |
4630 | Transform3D xform = GE.value; |
4631 | Transform3D new_xform = _compute_transform(_edit.mode, se->original * xform, xform, p_motion, p_snap, local_coords, _edit.plane != TRANSFORM_VIEW); // Force orthogonal with subgizmo. |
4632 | if (!local_coords) { |
4633 | new_xform = se->original.affine_inverse() * new_xform; |
4634 | } |
4635 | se->gizmo->set_subgizmo_transform(GE.key, new_xform); |
4636 | } |
4637 | } else { |
4638 | Transform3D new_xform = _compute_transform(_edit.mode, se->original, se->original_local, p_motion, p_snap, local_coords, sp->get_rotation_edit_mode() != Node3D::ROTATION_EDIT_MODE_BASIS && _edit.plane != TRANSFORM_VIEW); |
4639 | _transform_gizmo_apply(se->sp, new_xform, local_coords); |
4640 | } |
4641 | } |
4642 | |
4643 | spatial_editor->update_transform_gizmo(); |
4644 | surface->queue_redraw(); |
4645 | } |
4646 | |
4647 | // Update the current transform operation in response to an input. |
4648 | void Node3DEditorViewport::update_transform(bool p_shift) { |
4649 | Vector3 ray_pos = _get_ray_pos(_edit.mouse_pos); |
4650 | Vector3 ray = _get_ray(_edit.mouse_pos); |
4651 | double snap = EDITOR_GET("interface/inspector/default_float_step" ); |
4652 | int snap_step_decimals = Math::range_step_decimals(snap); |
4653 | |
4654 | switch (_edit.mode) { |
4655 | case TRANSFORM_SCALE: { |
4656 | Vector3 motion_mask; |
4657 | Plane plane; |
4658 | bool plane_mv = false; |
4659 | |
4660 | switch (_edit.plane) { |
4661 | case TRANSFORM_VIEW: |
4662 | motion_mask = Vector3(0, 0, 0); |
4663 | plane = Plane(_get_camera_normal(), _edit.center); |
4664 | break; |
4665 | case TRANSFORM_X_AXIS: |
4666 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(); |
4667 | plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center); |
4668 | break; |
4669 | case TRANSFORM_Y_AXIS: |
4670 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(); |
4671 | plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center); |
4672 | break; |
4673 | case TRANSFORM_Z_AXIS: |
4674 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized(); |
4675 | plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center); |
4676 | break; |
4677 | case TRANSFORM_YZ: |
4678 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(); |
4679 | plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(), _edit.center); |
4680 | plane_mv = true; |
4681 | break; |
4682 | case TRANSFORM_XZ: |
4683 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(); |
4684 | plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(), _edit.center); |
4685 | plane_mv = true; |
4686 | break; |
4687 | case TRANSFORM_XY: |
4688 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(); |
4689 | plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(2).normalized(), _edit.center); |
4690 | plane_mv = true; |
4691 | break; |
4692 | } |
4693 | |
4694 | Vector3 intersection; |
4695 | if (!plane.intersects_ray(ray_pos, ray, &intersection)) { |
4696 | break; |
4697 | } |
4698 | |
4699 | Vector3 click; |
4700 | if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) { |
4701 | break; |
4702 | } |
4703 | |
4704 | Vector3 motion = intersection - click; |
4705 | if (_edit.plane != TRANSFORM_VIEW) { |
4706 | if (!plane_mv) { |
4707 | motion = motion_mask.dot(motion) * motion_mask; |
4708 | |
4709 | } else { |
4710 | // Alternative planar scaling mode |
4711 | if (p_shift) { |
4712 | motion = motion_mask.dot(motion) * motion_mask; |
4713 | } |
4714 | } |
4715 | |
4716 | } else { |
4717 | const real_t center_click_dist = click.distance_to(_edit.center); |
4718 | const real_t center_inters_dist = intersection.distance_to(_edit.center); |
4719 | if (center_click_dist == 0) { |
4720 | break; |
4721 | } |
4722 | |
4723 | const real_t scale = center_inters_dist - center_click_dist; |
4724 | motion = Vector3(scale, scale, scale); |
4725 | } |
4726 | |
4727 | motion /= click.distance_to(_edit.center); |
4728 | |
4729 | // Disable local transformation for TRANSFORM_VIEW |
4730 | bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); |
4731 | |
4732 | if (_edit.snap || spatial_editor->is_snap_enabled()) { |
4733 | snap = spatial_editor->get_scale_snap() / 100; |
4734 | } |
4735 | Vector3 motion_snapped = motion; |
4736 | motion_snapped.snap(Vector3(snap, snap, snap)); |
4737 | // This might not be necessary anymore after issue #288 is solved (in 4.0?). |
4738 | // TRANSLATORS: Refers to changing the scale of a node in the 3D editor. |
4739 | set_message(TTR("Scaling:" ) + " (" + String::num(motion_snapped.x, snap_step_decimals) + ", " + |
4740 | String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")" ); |
4741 | if (local_coords) { |
4742 | // TODO: needed? |
4743 | motion = _edit.original.basis.inverse().xform(motion); |
4744 | } |
4745 | |
4746 | apply_transform(motion, snap); |
4747 | } break; |
4748 | |
4749 | case TRANSFORM_TRANSLATE: { |
4750 | Vector3 motion_mask; |
4751 | Plane plane; |
4752 | bool plane_mv = false; |
4753 | |
4754 | switch (_edit.plane) { |
4755 | case TRANSFORM_VIEW: |
4756 | plane = Plane(_get_camera_normal(), _edit.center); |
4757 | break; |
4758 | case TRANSFORM_X_AXIS: |
4759 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(); |
4760 | plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center); |
4761 | break; |
4762 | case TRANSFORM_Y_AXIS: |
4763 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(); |
4764 | plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center); |
4765 | break; |
4766 | case TRANSFORM_Z_AXIS: |
4767 | motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized(); |
4768 | plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center); |
4769 | break; |
4770 | case TRANSFORM_YZ: |
4771 | plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(), _edit.center); |
4772 | plane_mv = true; |
4773 | break; |
4774 | case TRANSFORM_XZ: |
4775 | plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(), _edit.center); |
4776 | plane_mv = true; |
4777 | break; |
4778 | case TRANSFORM_XY: |
4779 | plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(2).normalized(), _edit.center); |
4780 | plane_mv = true; |
4781 | break; |
4782 | } |
4783 | |
4784 | Vector3 intersection; |
4785 | if (!plane.intersects_ray(ray_pos, ray, &intersection)) { |
4786 | break; |
4787 | } |
4788 | |
4789 | Vector3 click; |
4790 | if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) { |
4791 | break; |
4792 | } |
4793 | |
4794 | Vector3 motion = intersection - click; |
4795 | if (_edit.plane != TRANSFORM_VIEW) { |
4796 | if (!plane_mv) { |
4797 | motion = motion_mask.dot(motion) * motion_mask; |
4798 | } |
4799 | } |
4800 | |
4801 | // Disable local transformation for TRANSFORM_VIEW |
4802 | bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); |
4803 | |
4804 | if (_edit.snap || spatial_editor->is_snap_enabled()) { |
4805 | snap = spatial_editor->get_translate_snap(); |
4806 | } |
4807 | Vector3 motion_snapped = motion; |
4808 | motion_snapped.snap(Vector3(snap, snap, snap)); |
4809 | // TRANSLATORS: Refers to changing the position of a node in the 3D editor. |
4810 | set_message(TTR("Translating:" ) + " (" + String::num(motion_snapped.x, snap_step_decimals) + ", " + |
4811 | String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")" ); |
4812 | if (local_coords) { |
4813 | motion = spatial_editor->get_gizmo_transform().basis.inverse().xform(motion); |
4814 | } |
4815 | |
4816 | apply_transform(motion, snap); |
4817 | } break; |
4818 | |
4819 | case TRANSFORM_ROTATE: { |
4820 | Plane plane = Plane(_get_camera_normal(), _edit.center); |
4821 | |
4822 | Vector3 local_axis; |
4823 | Vector3 global_axis; |
4824 | switch (_edit.plane) { |
4825 | case TRANSFORM_VIEW: |
4826 | // local_axis unused |
4827 | global_axis = _get_camera_normal(); |
4828 | break; |
4829 | case TRANSFORM_X_AXIS: |
4830 | local_axis = Vector3(1, 0, 0); |
4831 | break; |
4832 | case TRANSFORM_Y_AXIS: |
4833 | local_axis = Vector3(0, 1, 0); |
4834 | break; |
4835 | case TRANSFORM_Z_AXIS: |
4836 | local_axis = Vector3(0, 0, 1); |
4837 | break; |
4838 | case TRANSFORM_YZ: |
4839 | case TRANSFORM_XZ: |
4840 | case TRANSFORM_XY: |
4841 | break; |
4842 | } |
4843 | |
4844 | if (_edit.plane != TRANSFORM_VIEW) { |
4845 | global_axis = spatial_editor->get_gizmo_transform().basis.xform(local_axis).normalized(); |
4846 | } |
4847 | |
4848 | Vector3 intersection; |
4849 | if (!plane.intersects_ray(ray_pos, ray, &intersection)) { |
4850 | break; |
4851 | } |
4852 | |
4853 | Vector3 click; |
4854 | if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) { |
4855 | break; |
4856 | } |
4857 | |
4858 | static const float orthogonal_threshold = Math::cos(Math::deg_to_rad(87.0f)); |
4859 | bool axis_is_orthogonal = ABS(plane.normal.dot(global_axis)) < orthogonal_threshold; |
4860 | |
4861 | double angle = 0.0f; |
4862 | if (axis_is_orthogonal) { |
4863 | _edit.show_rotation_line = false; |
4864 | Vector3 projection_axis = plane.normal.cross(global_axis); |
4865 | Vector3 delta = intersection - click; |
4866 | float projection = delta.dot(projection_axis); |
4867 | angle = (projection * (Math_PI / 2.0f)) / (gizmo_scale * GIZMO_CIRCLE_SIZE); |
4868 | } else { |
4869 | _edit.show_rotation_line = true; |
4870 | Vector3 click_axis = (click - _edit.center).normalized(); |
4871 | Vector3 current_axis = (intersection - _edit.center).normalized(); |
4872 | angle = click_axis.signed_angle_to(current_axis, global_axis); |
4873 | } |
4874 | |
4875 | if (_edit.snap || spatial_editor->is_snap_enabled()) { |
4876 | snap = spatial_editor->get_rotate_snap(); |
4877 | } |
4878 | angle = Math::rad_to_deg(angle) + snap * 0.5; //else it won't reach +180 |
4879 | angle -= Math::fmod(angle, snap); |
4880 | set_message(vformat(TTR("Rotating %s degrees." ), String::num(angle, snap_step_decimals))); |
4881 | angle = Math::deg_to_rad(angle); |
4882 | |
4883 | bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW |
4884 | |
4885 | Vector3 compute_axis = local_coords ? local_axis : global_axis; |
4886 | apply_transform(compute_axis, angle); |
4887 | } break; |
4888 | default: { |
4889 | } |
4890 | } |
4891 | } |
4892 | |
4893 | void Node3DEditorViewport::update_transform_numeric() { |
4894 | Vector3 motion; |
4895 | switch (_edit.plane) { |
4896 | case TRANSFORM_VIEW: { |
4897 | switch (_edit.mode) { |
4898 | case TRANSFORM_TRANSLATE: |
4899 | motion = Vector3(1, 0, 0); |
4900 | break; |
4901 | case TRANSFORM_ROTATE: |
4902 | motion = spatial_editor->get_gizmo_transform().basis.xform_inv(_get_camera_normal()).normalized(); |
4903 | break; |
4904 | case TRANSFORM_SCALE: |
4905 | motion = Vector3(1, 1, 1); |
4906 | break; |
4907 | case TRANSFORM_NONE: |
4908 | ERR_FAIL_MSG("_edit.mode cannot be TRANSFORM_NONE in update_transform_numeric." ); |
4909 | } |
4910 | break; |
4911 | } |
4912 | case TRANSFORM_X_AXIS: |
4913 | motion = Vector3(1, 0, 0); |
4914 | break; |
4915 | case TRANSFORM_Y_AXIS: |
4916 | motion = Vector3(0, 1, 0); |
4917 | break; |
4918 | case TRANSFORM_Z_AXIS: |
4919 | motion = Vector3(0, 0, 1); |
4920 | break; |
4921 | case TRANSFORM_XY: |
4922 | motion = Vector3(1, 1, 0); |
4923 | break; |
4924 | case TRANSFORM_XZ: |
4925 | motion = Vector3(1, 0, 1); |
4926 | break; |
4927 | case TRANSFORM_YZ: |
4928 | motion = Vector3(0, 1, 1); |
4929 | break; |
4930 | } |
4931 | |
4932 | double value = _edit.numeric_input * (_edit.numeric_negate ? -1 : 1); |
4933 | double = 0.0; |
4934 | switch (_edit.mode) { |
4935 | case TRANSFORM_TRANSLATE: |
4936 | motion *= value; |
4937 | set_message(vformat(TTR("Translating %s." ), motion)); |
4938 | break; |
4939 | case TRANSFORM_ROTATE: |
4940 | extra = Math::deg_to_rad(value); |
4941 | set_message(vformat(TTR("Rotating %f degrees." ), value)); |
4942 | break; |
4943 | case TRANSFORM_SCALE: |
4944 | // To halve the size of an object in Blender, you scale it by 0.5. |
4945 | // Doing the same in Godot is considered scaling it by -0.5. |
4946 | motion *= (value - 1.0); |
4947 | set_message(vformat(TTR("Scaling %s." ), motion)); |
4948 | break; |
4949 | case TRANSFORM_NONE: |
4950 | ERR_FAIL_MSG("_edit.mode cannot be TRANSFORM_NONE in update_transform_numeric." ); |
4951 | } |
4952 | |
4953 | apply_transform(motion, extra); |
4954 | } |
4955 | |
4956 | // Perform cleanup after a transform operation is committed or canceled. |
4957 | void Node3DEditorViewport::finish_transform() { |
4958 | _edit.mode = TRANSFORM_NONE; |
4959 | _edit.instant = false; |
4960 | _edit.numeric_input = 0; |
4961 | _edit.numeric_next_decimal = 0; |
4962 | _edit.numeric_negate = false; |
4963 | spatial_editor->set_local_coords_enabled(_edit.original_local); |
4964 | spatial_editor->update_transform_gizmo(); |
4965 | surface->queue_redraw(); |
4966 | set_process_input(false); |
4967 | } |
4968 | |
4969 | // Register a shortcut and also add it as an input action with the same events. |
4970 | void Node3DEditorViewport::register_shortcut_action(const String &p_path, const String &p_name, Key p_keycode, bool p_physical) { |
4971 | Ref<Shortcut> sc = ED_SHORTCUT(p_path, p_name, p_keycode, p_physical); |
4972 | shortcut_changed_callback(sc, p_path); |
4973 | // Connect to the change event on the shortcut so the input binding can be updated. |
4974 | sc->connect_changed(callable_mp(this, &Node3DEditorViewport::shortcut_changed_callback).bind(sc, p_path)); |
4975 | } |
4976 | |
4977 | // Update the action in the InputMap to the provided shortcut events. |
4978 | void Node3DEditorViewport::shortcut_changed_callback(const Ref<Shortcut> p_shortcut, const String &p_shortcut_path) { |
4979 | InputMap *im = InputMap::get_singleton(); |
4980 | if (im->has_action(p_shortcut_path)) { |
4981 | im->action_erase_events(p_shortcut_path); |
4982 | } else { |
4983 | im->add_action(p_shortcut_path); |
4984 | } |
4985 | |
4986 | for (int i = 0; i < p_shortcut->get_events().size(); i++) { |
4987 | im->action_add_event(p_shortcut_path, p_shortcut->get_events()[i]); |
4988 | } |
4989 | } |
4990 | |
4991 | void Node3DEditorViewport::_set_lock_view_rotation(bool p_lock_rotation) { |
4992 | lock_rotation = p_lock_rotation; |
4993 | int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION); |
4994 | view_menu->get_popup()->set_item_checked(idx, p_lock_rotation); |
4995 | if (p_lock_rotation) { |
4996 | locked_label->show(); |
4997 | } else { |
4998 | locked_label->hide(); |
4999 | } |
5000 | } |
5001 | |
5002 | Node3DEditorViewport::Node3DEditorViewport(Node3DEditor *p_spatial_editor, int p_index) { |
5003 | cpu_time_history_index = 0; |
5004 | gpu_time_history_index = 0; |
5005 | |
5006 | _edit.mode = TRANSFORM_NONE; |
5007 | _edit.plane = TRANSFORM_VIEW; |
5008 | _edit.snap = true; |
5009 | _edit.show_rotation_line = true; |
5010 | _edit.instant = false; |
5011 | _edit.gizmo_handle = -1; |
5012 | _edit.gizmo_handle_secondary = false; |
5013 | |
5014 | index = p_index; |
5015 | editor_selection = EditorNode::get_singleton()->get_editor_selection(); |
5016 | |
5017 | orthogonal = false; |
5018 | auto_orthogonal = false; |
5019 | lock_rotation = false; |
5020 | message_time = 0; |
5021 | zoom_indicator_delay = 0.0; |
5022 | |
5023 | spatial_editor = p_spatial_editor; |
5024 | SubViewportContainer *c = memnew(SubViewportContainer); |
5025 | subviewport_container = c; |
5026 | c->set_stretch(true); |
5027 | add_child(c); |
5028 | c->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
5029 | viewport = memnew(SubViewport); |
5030 | viewport->set_disable_input(true); |
5031 | |
5032 | c->add_child(viewport); |
5033 | surface = memnew(Control); |
5034 | SET_DRAG_FORWARDING_CD(surface, Node3DEditorViewport); |
5035 | add_child(surface); |
5036 | surface->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
5037 | surface->set_clip_contents(true); |
5038 | camera = memnew(Camera3D); |
5039 | camera->set_disable_gizmos(true); |
5040 | camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + p_index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER) | (1 << MISC_TOOL_LAYER)); |
5041 | viewport->add_child(camera); |
5042 | camera->make_current(); |
5043 | surface->set_focus_mode(FOCUS_ALL); |
5044 | |
5045 | VBoxContainer *vbox = memnew(VBoxContainer); |
5046 | surface->add_child(vbox); |
5047 | vbox->set_offset(SIDE_LEFT, 10 * EDSCALE); |
5048 | vbox->set_offset(SIDE_TOP, 10 * EDSCALE); |
5049 | |
5050 | view_menu = memnew(MenuButton); |
5051 | view_menu->set_flat(false); |
5052 | view_menu->set_h_size_flags(0); |
5053 | view_menu->set_shortcut_context(this); |
5054 | vbox->add_child(view_menu); |
5055 | |
5056 | display_submenu = memnew(PopupMenu); |
5057 | view_menu->get_popup()->set_hide_on_checkable_item_selection(false); |
5058 | view_menu->get_popup()->add_child(display_submenu); |
5059 | |
5060 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view" ), VIEW_TOP); |
5061 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view" ), VIEW_BOTTOM); |
5062 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view" ), VIEW_LEFT); |
5063 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view" ), VIEW_RIGHT); |
5064 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view" ), VIEW_FRONT); |
5065 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view" ), VIEW_REAR); |
5066 | view_menu->get_popup()->add_separator(); |
5067 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal" ), VIEW_SWITCH_PERSPECTIVE_ORTHOGONAL); |
5068 | view_menu->get_popup()->add_radio_check_item(TTR("Perspective" ), VIEW_PERSPECTIVE); |
5069 | view_menu->get_popup()->add_radio_check_item(TTR("Orthogonal" ), VIEW_ORTHOGONAL); |
5070 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true); |
5071 | view_menu->get_popup()->add_check_item(TTR("Auto Orthogonal Enabled" ), VIEW_AUTO_ORTHOGONAL); |
5072 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL), true); |
5073 | view_menu->get_popup()->add_separator(); |
5074 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_lock_rotation" , TTR("Lock View Rotation" )), VIEW_LOCK_ROTATION); |
5075 | view_menu->get_popup()->add_separator(); |
5076 | // TRANSLATORS: "Normal" as in "normal life", not "normal vector". |
5077 | view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_normal" , TTR("Display Normal" )), VIEW_DISPLAY_NORMAL); |
5078 | view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_wireframe" , TTR("Display Wireframe" )), VIEW_DISPLAY_WIREFRAME); |
5079 | view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_overdraw" , TTR("Display Overdraw" )), VIEW_DISPLAY_OVERDRAW); |
5080 | view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_lighting" , TTR("Display Lighting" )), VIEW_DISPLAY_LIGHTING); |
5081 | view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_unshaded" , TTR("Display Unshaded" )), VIEW_DISPLAY_UNSHADED); |
5082 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true); |
5083 | display_submenu->set_hide_on_checkable_item_selection(false); |
5084 | display_submenu->add_radio_check_item(TTR("Directional Shadow Splits" ), VIEW_DISPLAY_DEBUG_PSSM_SPLITS); |
5085 | display_submenu->add_separator(); |
5086 | display_submenu->add_radio_check_item(TTR("Normal Buffer" ), VIEW_DISPLAY_NORMAL_BUFFER); |
5087 | display_submenu->add_separator(); |
5088 | display_submenu->add_radio_check_item(TTR("Shadow Atlas" ), VIEW_DISPLAY_DEBUG_SHADOW_ATLAS); |
5089 | display_submenu->add_radio_check_item(TTR("Directional Shadow Map" ), VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS); |
5090 | display_submenu->add_separator(); |
5091 | display_submenu->add_radio_check_item(TTR("Decal Atlas" ), VIEW_DISPLAY_DEBUG_DECAL_ATLAS); |
5092 | display_submenu->add_separator(); |
5093 | display_submenu->add_radio_check_item(TTR("VoxelGI Lighting" ), VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING); |
5094 | display_submenu->add_radio_check_item(TTR("VoxelGI Albedo" ), VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO); |
5095 | display_submenu->add_radio_check_item(TTR("VoxelGI Emission" ), VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION); |
5096 | display_submenu->add_separator(); |
5097 | display_submenu->add_radio_check_item(TTR("SDFGI Cascades" ), VIEW_DISPLAY_DEBUG_SDFGI); |
5098 | display_submenu->add_radio_check_item(TTR("SDFGI Probes" ), VIEW_DISPLAY_DEBUG_SDFGI_PROBES); |
5099 | display_submenu->add_separator(); |
5100 | display_submenu->add_radio_check_item(TTR("Scene Luminance" ), VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE); |
5101 | display_submenu->add_separator(); |
5102 | display_submenu->add_radio_check_item(TTR("SSAO" ), VIEW_DISPLAY_DEBUG_SSAO); |
5103 | display_submenu->add_radio_check_item(TTR("SSIL" ), VIEW_DISPLAY_DEBUG_SSIL); |
5104 | display_submenu->add_separator(); |
5105 | display_submenu->add_radio_check_item(TTR("VoxelGI/SDFGI Buffer" ), VIEW_DISPLAY_DEBUG_GI_BUFFER); |
5106 | display_submenu->add_separator(); |
5107 | display_submenu->add_radio_check_item(TTR("Disable Mesh LOD" ), VIEW_DISPLAY_DEBUG_DISABLE_LOD); |
5108 | display_submenu->add_separator(); |
5109 | display_submenu->add_radio_check_item(TTR("OmniLight3D Cluster" ), VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS); |
5110 | display_submenu->add_radio_check_item(TTR("SpotLight3D Cluster" ), VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS); |
5111 | display_submenu->add_radio_check_item(TTR("Decal Cluster" ), VIEW_DISPLAY_DEBUG_CLUSTER_DECALS); |
5112 | display_submenu->add_radio_check_item(TTR("ReflectionProbe Cluster" ), VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES); |
5113 | display_submenu->add_radio_check_item(TTR("Occlusion Culling Buffer" ), VIEW_DISPLAY_DEBUG_OCCLUDERS); |
5114 | display_submenu->add_radio_check_item(TTR("Motion Vectors" ), VIEW_DISPLAY_MOTION_VECTORS); |
5115 | |
5116 | display_submenu->set_name("display_advanced" ); |
5117 | view_menu->get_popup()->add_submenu_item(TTR("Display Advanced..." ), "display_advanced" , VIEW_DISPLAY_ADVANCED); |
5118 | view_menu->get_popup()->add_separator(); |
5119 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment" , TTR("View Environment" )), VIEW_ENVIRONMENT); |
5120 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos" , TTR("View Gizmos" )), VIEW_GIZMOS); |
5121 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_information" , TTR("View Information" )), VIEW_INFORMATION); |
5122 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_fps" , TTR("View Frame Time" )), VIEW_FRAME_TIME); |
5123 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT), true); |
5124 | view_menu->get_popup()->add_separator(); |
5125 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_half_resolution" , TTR("Half Resolution" )), VIEW_HALF_RESOLUTION); |
5126 | view_menu->get_popup()->add_separator(); |
5127 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener" , TTR("Audio Listener" )), VIEW_AUDIO_LISTENER); |
5128 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_doppler" , TTR("Enable Doppler" )), VIEW_AUDIO_DOPPLER); |
5129 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS), true); |
5130 | |
5131 | view_menu->get_popup()->add_separator(); |
5132 | view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_cinematic_preview" , TTR("Cinematic Preview" )), VIEW_CINEMATIC_PREVIEW); |
5133 | |
5134 | view_menu->get_popup()->add_separator(); |
5135 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_origin" ), VIEW_CENTER_TO_ORIGIN); |
5136 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection" ), VIEW_CENTER_TO_SELECTION); |
5137 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_transform_with_view" ), VIEW_ALIGN_TRANSFORM_WITH_VIEW); |
5138 | view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_rotation_with_view" ), VIEW_ALIGN_ROTATION_WITH_VIEW); |
5139 | view_menu->get_popup()->connect("id_pressed" , callable_mp(this, &Node3DEditorViewport::_menu_option)); |
5140 | display_submenu->connect("id_pressed" , callable_mp(this, &Node3DEditorViewport::_menu_option)); |
5141 | view_menu->set_disable_shortcuts(true); |
5142 | |
5143 | // TODO: Re-evaluate with new OpenGL3 renderer, and implement. |
5144 | //if (OS::get_singleton()->get_current_video_driver() == OS::RENDERING_DRIVER_OPENGL3) { |
5145 | if (false) { |
5146 | // Alternate display modes only work when using the Vulkan renderer; make this explicit. |
5147 | const int normal_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL); |
5148 | const int wireframe_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME); |
5149 | const int overdraw_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW); |
5150 | const int shadeless_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_UNSHADED); |
5151 | const String unsupported_tooltip = TTR("Not available when using the OpenGL renderer." ); |
5152 | |
5153 | view_menu->get_popup()->set_item_disabled(normal_idx, true); |
5154 | view_menu->get_popup()->set_item_tooltip(normal_idx, unsupported_tooltip); |
5155 | view_menu->get_popup()->set_item_disabled(wireframe_idx, true); |
5156 | view_menu->get_popup()->set_item_tooltip(wireframe_idx, unsupported_tooltip); |
5157 | view_menu->get_popup()->set_item_disabled(overdraw_idx, true); |
5158 | view_menu->get_popup()->set_item_tooltip(overdraw_idx, unsupported_tooltip); |
5159 | view_menu->get_popup()->set_item_disabled(shadeless_idx, true); |
5160 | view_menu->get_popup()->set_item_tooltip(shadeless_idx, unsupported_tooltip); |
5161 | } |
5162 | |
5163 | register_shortcut_action("spatial_editor/freelook_left" , TTR("Freelook Left" ), Key::A, true); |
5164 | register_shortcut_action("spatial_editor/freelook_right" , TTR("Freelook Right" ), Key::D, true); |
5165 | register_shortcut_action("spatial_editor/freelook_forward" , TTR("Freelook Forward" ), Key::W, true); |
5166 | register_shortcut_action("spatial_editor/freelook_backwards" , TTR("Freelook Backwards" ), Key::S, true); |
5167 | register_shortcut_action("spatial_editor/freelook_up" , TTR("Freelook Up" ), Key::E, true); |
5168 | register_shortcut_action("spatial_editor/freelook_down" , TTR("Freelook Down" ), Key::Q, true); |
5169 | register_shortcut_action("spatial_editor/freelook_speed_modifier" , TTR("Freelook Speed Modifier" ), Key::SHIFT); |
5170 | register_shortcut_action("spatial_editor/freelook_slow_modifier" , TTR("Freelook Slow Modifier" ), Key::ALT); |
5171 | |
5172 | ED_SHORTCUT("spatial_editor/lock_transform_x" , TTR("Lock Transformation to X axis" ), Key::X); |
5173 | ED_SHORTCUT("spatial_editor/lock_transform_y" , TTR("Lock Transformation to Y axis" ), Key::Y); |
5174 | ED_SHORTCUT("spatial_editor/lock_transform_z" , TTR("Lock Transformation to Z axis" ), Key::Z); |
5175 | ED_SHORTCUT("spatial_editor/lock_transform_yz" , TTR("Lock Transformation to YZ plane" ), KeyModifierMask::SHIFT | Key::X); |
5176 | ED_SHORTCUT("spatial_editor/lock_transform_xz" , TTR("Lock Transformation to XZ plane" ), KeyModifierMask::SHIFT | Key::Y); |
5177 | ED_SHORTCUT("spatial_editor/lock_transform_xy" , TTR("Lock Transformation to XY plane" ), KeyModifierMask::SHIFT | Key::Z); |
5178 | ED_SHORTCUT("spatial_editor/cancel_transform" , TTR("Cancel Transformation" ), Key::ESCAPE); |
5179 | ED_SHORTCUT("spatial_editor/instant_translate" , TTR("Begin Translate Transformation" )); |
5180 | ED_SHORTCUT("spatial_editor/instant_rotate" , TTR("Begin Rotate Transformation" )); |
5181 | ED_SHORTCUT("spatial_editor/instant_scale" , TTR("Begin Scale Transformation" )); |
5182 | |
5183 | preview_camera = memnew(CheckBox); |
5184 | preview_camera->set_text(TTR("Preview" )); |
5185 | preview_camera->set_shortcut(ED_SHORTCUT("spatial_editor/toggle_camera_preview" , TTR("Toggle Camera Preview" ), KeyModifierMask::CMD_OR_CTRL | Key::P)); |
5186 | vbox->add_child(preview_camera); |
5187 | preview_camera->set_h_size_flags(0); |
5188 | preview_camera->hide(); |
5189 | preview_camera->connect("toggled" , callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview)); |
5190 | previewing = nullptr; |
5191 | gizmo_scale = 1.0; |
5192 | |
5193 | preview_node = nullptr; |
5194 | |
5195 | bottom_center_vbox = memnew(VBoxContainer); |
5196 | bottom_center_vbox->set_anchors_preset(LayoutPreset::PRESET_CENTER); |
5197 | bottom_center_vbox->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -20 * EDSCALE); |
5198 | bottom_center_vbox->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -10 * EDSCALE); |
5199 | bottom_center_vbox->set_h_grow_direction(GROW_DIRECTION_BOTH); |
5200 | bottom_center_vbox->set_v_grow_direction(GROW_DIRECTION_BEGIN); |
5201 | surface->add_child(bottom_center_vbox); |
5202 | |
5203 | info_label = memnew(Label); |
5204 | info_label->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -90 * EDSCALE); |
5205 | info_label->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -90 * EDSCALE); |
5206 | info_label->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -10 * EDSCALE); |
5207 | info_label->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -10 * EDSCALE); |
5208 | info_label->set_h_grow_direction(GROW_DIRECTION_BEGIN); |
5209 | info_label->set_v_grow_direction(GROW_DIRECTION_BEGIN); |
5210 | surface->add_child(info_label); |
5211 | info_label->hide(); |
5212 | |
5213 | cinema_label = memnew(Label); |
5214 | cinema_label->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 10 * EDSCALE); |
5215 | cinema_label->set_h_grow_direction(GROW_DIRECTION_END); |
5216 | cinema_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
5217 | surface->add_child(cinema_label); |
5218 | cinema_label->set_text(TTR("Cinematic Preview" )); |
5219 | cinema_label->hide(); |
5220 | previewing_cinema = false; |
5221 | |
5222 | locked_label = memnew(Label); |
5223 | locked_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
5224 | locked_label->set_h_size_flags(SIZE_SHRINK_CENTER); |
5225 | bottom_center_vbox->add_child(locked_label); |
5226 | locked_label->set_text(TTR("View Rotation Locked" )); |
5227 | locked_label->hide(); |
5228 | |
5229 | zoom_limit_label = memnew(Label); |
5230 | zoom_limit_label->set_text(TTR("To zoom further, change the camera's clipping planes (View -> Settings...)" )); |
5231 | zoom_limit_label->set_name("ZoomLimitMessageLabel" ); |
5232 | zoom_limit_label->add_theme_color_override("font_color" , Color(1, 1, 1, 1)); |
5233 | zoom_limit_label->hide(); |
5234 | bottom_center_vbox->add_child(zoom_limit_label); |
5235 | |
5236 | preview_material_label = memnew(Label); |
5237 | preview_material_label->set_anchors_and_offsets_preset(LayoutPreset::PRESET_BOTTOM_LEFT); |
5238 | preview_material_label->set_offset(Side::SIDE_TOP, -70 * EDSCALE); |
5239 | preview_material_label->set_text(TTR("Overriding material..." )); |
5240 | preview_material_label->add_theme_color_override("font_color" , Color(1, 1, 1, 1)); |
5241 | preview_material_label->hide(); |
5242 | surface->add_child(preview_material_label); |
5243 | |
5244 | preview_material_label_desc = memnew(Label); |
5245 | preview_material_label_desc->set_anchors_and_offsets_preset(LayoutPreset::PRESET_BOTTOM_LEFT); |
5246 | preview_material_label_desc->set_offset(Side::SIDE_TOP, -50 * EDSCALE); |
5247 | preview_material_label_desc->set_text(TTR("Drag and drop to override the material of any geometry node.\nHold Ctrl when dropping to override a specific surface." )); |
5248 | preview_material_label_desc->add_theme_color_override("font_color" , Color(0.8, 0.8, 0.8, 1)); |
5249 | preview_material_label_desc->add_theme_constant_override("line_spacing" , 0); |
5250 | preview_material_label_desc->hide(); |
5251 | surface->add_child(preview_material_label_desc); |
5252 | |
5253 | frame_time_gradient = memnew(Gradient); |
5254 | // The color is set when the theme changes. |
5255 | frame_time_gradient->add_point(0.5, Color()); |
5256 | |
5257 | top_right_vbox = memnew(VBoxContainer); |
5258 | top_right_vbox->set_anchors_and_offsets_preset(PRESET_TOP_RIGHT, PRESET_MODE_MINSIZE, 10.0 * EDSCALE); |
5259 | top_right_vbox->set_h_grow_direction(GROW_DIRECTION_BEGIN); |
5260 | // Make sure frame time labels don't touch the viewport's edge. |
5261 | top_right_vbox->set_custom_minimum_size(Size2(100, 0) * EDSCALE); |
5262 | // Prevent visible spacing between frame time labels. |
5263 | top_right_vbox->add_theme_constant_override("separation" , 0); |
5264 | |
5265 | const int navigation_control_size = 150; |
5266 | |
5267 | position_control = memnew(ViewportNavigationControl); |
5268 | position_control->set_navigation_mode(Node3DEditorViewport::NAVIGATION_MOVE); |
5269 | position_control->set_custom_minimum_size(Size2(navigation_control_size, navigation_control_size) * EDSCALE); |
5270 | position_control->set_h_size_flags(SIZE_SHRINK_END); |
5271 | position_control->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0); |
5272 | position_control->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -navigation_control_size * EDSCALE); |
5273 | position_control->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_BEGIN, navigation_control_size * EDSCALE); |
5274 | position_control->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0); |
5275 | position_control->set_viewport(this); |
5276 | surface->add_child(position_control); |
5277 | |
5278 | look_control = memnew(ViewportNavigationControl); |
5279 | look_control->set_navigation_mode(Node3DEditorViewport::NAVIGATION_LOOK); |
5280 | look_control->set_custom_minimum_size(Size2(navigation_control_size, navigation_control_size) * EDSCALE); |
5281 | look_control->set_h_size_flags(SIZE_SHRINK_END); |
5282 | look_control->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -navigation_control_size * EDSCALE); |
5283 | look_control->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -navigation_control_size * EDSCALE); |
5284 | look_control->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0); |
5285 | look_control->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0); |
5286 | look_control->set_viewport(this); |
5287 | surface->add_child(look_control); |
5288 | |
5289 | rotation_control = memnew(ViewportRotationControl); |
5290 | rotation_control->set_custom_minimum_size(Size2(80, 80) * EDSCALE); |
5291 | rotation_control->set_h_size_flags(SIZE_SHRINK_END); |
5292 | rotation_control->set_viewport(this); |
5293 | top_right_vbox->add_child(rotation_control); |
5294 | |
5295 | // Individual Labels are used to allow coloring each label with its own color. |
5296 | cpu_time_label = memnew(Label); |
5297 | top_right_vbox->add_child(cpu_time_label); |
5298 | cpu_time_label->hide(); |
5299 | |
5300 | gpu_time_label = memnew(Label); |
5301 | top_right_vbox->add_child(gpu_time_label); |
5302 | gpu_time_label->hide(); |
5303 | |
5304 | fps_label = memnew(Label); |
5305 | top_right_vbox->add_child(fps_label); |
5306 | fps_label->hide(); |
5307 | |
5308 | surface->add_child(top_right_vbox); |
5309 | |
5310 | accept = nullptr; |
5311 | |
5312 | freelook_active = false; |
5313 | freelook_speed = EDITOR_GET("editors/3d/freelook/freelook_base_speed" ); |
5314 | |
5315 | selection_menu = memnew(PopupMenu); |
5316 | add_child(selection_menu); |
5317 | selection_menu->set_min_size(Size2(100, 0) * EDSCALE); |
5318 | selection_menu->connect("id_pressed" , callable_mp(this, &Node3DEditorViewport::_selection_result_pressed)); |
5319 | selection_menu->connect("popup_hide" , callable_mp(this, &Node3DEditorViewport::_selection_menu_hide)); |
5320 | |
5321 | if (p_index == 0) { |
5322 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER), true); |
5323 | viewport->set_as_audio_listener_3d(true); |
5324 | } |
5325 | |
5326 | view_type = VIEW_TYPE_USER; |
5327 | _update_name(); |
5328 | |
5329 | EditorSettings::get_singleton()->connect("settings_changed" , callable_mp(this, &Node3DEditorViewport::update_transform_gizmo_view)); |
5330 | } |
5331 | |
5332 | Node3DEditorViewport::~Node3DEditorViewport() { |
5333 | memdelete(frame_time_gradient); |
5334 | } |
5335 | |
5336 | ////////////////////////////////////////////////////////////// |
5337 | |
5338 | void Node3DEditorViewportContainer::gui_input(const Ref<InputEvent> &p_event) { |
5339 | ERR_FAIL_COND(p_event.is_null()); |
5340 | |
5341 | Ref<InputEventMouseButton> mb = p_event; |
5342 | |
5343 | if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) { |
5344 | if (mb->is_pressed()) { |
5345 | Vector2 size = get_size(); |
5346 | |
5347 | int h_sep = get_theme_constant(SNAME("separation" ), SNAME("HSplitContainer" )); |
5348 | int v_sep = get_theme_constant(SNAME("separation" ), SNAME("VSplitContainer" )); |
5349 | |
5350 | int mid_w = size.width * ratio_h; |
5351 | int mid_h = size.height * ratio_v; |
5352 | |
5353 | dragging_h = mb->get_position().x > (mid_w - h_sep / 2) && mb->get_position().x < (mid_w + h_sep / 2); |
5354 | dragging_v = mb->get_position().y > (mid_h - v_sep / 2) && mb->get_position().y < (mid_h + v_sep / 2); |
5355 | |
5356 | drag_begin_pos = mb->get_position(); |
5357 | drag_begin_ratio.x = ratio_h; |
5358 | drag_begin_ratio.y = ratio_v; |
5359 | |
5360 | switch (view) { |
5361 | case VIEW_USE_1_VIEWPORT: { |
5362 | dragging_h = false; |
5363 | dragging_v = false; |
5364 | |
5365 | } break; |
5366 | case VIEW_USE_2_VIEWPORTS: { |
5367 | dragging_h = false; |
5368 | |
5369 | } break; |
5370 | case VIEW_USE_2_VIEWPORTS_ALT: { |
5371 | dragging_v = false; |
5372 | |
5373 | } break; |
5374 | case VIEW_USE_3_VIEWPORTS: |
5375 | case VIEW_USE_3_VIEWPORTS_ALT: |
5376 | case VIEW_USE_4_VIEWPORTS: { |
5377 | // Do nothing. |
5378 | |
5379 | } break; |
5380 | } |
5381 | } else { |
5382 | dragging_h = false; |
5383 | dragging_v = false; |
5384 | } |
5385 | } |
5386 | |
5387 | Ref<InputEventMouseMotion> mm = p_event; |
5388 | |
5389 | if (mm.is_valid()) { |
5390 | if (view == VIEW_USE_3_VIEWPORTS || view == VIEW_USE_3_VIEWPORTS_ALT || view == VIEW_USE_4_VIEWPORTS) { |
5391 | Vector2 size = get_size(); |
5392 | |
5393 | int h_sep = get_theme_constant(SNAME("separation" ), SNAME("HSplitContainer" )); |
5394 | int v_sep = get_theme_constant(SNAME("separation" ), SNAME("VSplitContainer" )); |
5395 | |
5396 | int mid_w = size.width * ratio_h; |
5397 | int mid_h = size.height * ratio_v; |
5398 | |
5399 | bool was_hovering_h = hovering_h; |
5400 | bool was_hovering_v = hovering_v; |
5401 | hovering_h = mm->get_position().x > (mid_w - h_sep / 2) && mm->get_position().x < (mid_w + h_sep / 2); |
5402 | hovering_v = mm->get_position().y > (mid_h - v_sep / 2) && mm->get_position().y < (mid_h + v_sep / 2); |
5403 | |
5404 | if (was_hovering_h != hovering_h || was_hovering_v != hovering_v) { |
5405 | queue_redraw(); |
5406 | } |
5407 | } |
5408 | |
5409 | if (dragging_h) { |
5410 | real_t new_ratio = drag_begin_ratio.x + (mm->get_position().x - drag_begin_pos.x) / get_size().width; |
5411 | new_ratio = CLAMP(new_ratio, 40 / get_size().width, (get_size().width - 40) / get_size().width); |
5412 | ratio_h = new_ratio; |
5413 | queue_sort(); |
5414 | queue_redraw(); |
5415 | } |
5416 | if (dragging_v) { |
5417 | real_t new_ratio = drag_begin_ratio.y + (mm->get_position().y - drag_begin_pos.y) / get_size().height; |
5418 | new_ratio = CLAMP(new_ratio, 40 / get_size().height, (get_size().height - 40) / get_size().height); |
5419 | ratio_v = new_ratio; |
5420 | queue_sort(); |
5421 | queue_redraw(); |
5422 | } |
5423 | } |
5424 | } |
5425 | |
5426 | void Node3DEditorViewportContainer::_notification(int p_what) { |
5427 | switch (p_what) { |
5428 | case NOTIFICATION_MOUSE_ENTER: |
5429 | case NOTIFICATION_MOUSE_EXIT: { |
5430 | mouseover = (p_what == NOTIFICATION_MOUSE_ENTER); |
5431 | queue_redraw(); |
5432 | } break; |
5433 | |
5434 | case NOTIFICATION_DRAW: { |
5435 | if (mouseover) { |
5436 | Ref<Texture2D> h_grabber = get_theme_icon(SNAME("grabber" ), SNAME("HSplitContainer" )); |
5437 | Ref<Texture2D> v_grabber = get_theme_icon(SNAME("grabber" ), SNAME("VSplitContainer" )); |
5438 | |
5439 | Ref<Texture2D> hdiag_grabber = get_editor_theme_icon(SNAME("GuiViewportHdiagsplitter" )); |
5440 | Ref<Texture2D> vdiag_grabber = get_editor_theme_icon(SNAME("GuiViewportVdiagsplitter" )); |
5441 | Ref<Texture2D> vh_grabber = get_editor_theme_icon(SNAME("GuiViewportVhsplitter" )); |
5442 | |
5443 | Vector2 size = get_size(); |
5444 | |
5445 | int h_sep = get_theme_constant(SNAME("separation" ), SNAME("HSplitContainer" )); |
5446 | |
5447 | int v_sep = get_theme_constant(SNAME("separation" ), SNAME("VSplitContainer" )); |
5448 | |
5449 | int mid_w = size.width * ratio_h; |
5450 | int mid_h = size.height * ratio_v; |
5451 | |
5452 | int size_left = mid_w - h_sep / 2; |
5453 | int size_bottom = size.height - mid_h - v_sep / 2; |
5454 | |
5455 | switch (view) { |
5456 | case VIEW_USE_1_VIEWPORT: { |
5457 | // Nothing to show. |
5458 | |
5459 | } break; |
5460 | case VIEW_USE_2_VIEWPORTS: { |
5461 | draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2)); |
5462 | set_default_cursor_shape(CURSOR_VSPLIT); |
5463 | |
5464 | } break; |
5465 | case VIEW_USE_2_VIEWPORTS_ALT: { |
5466 | draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2)); |
5467 | set_default_cursor_shape(CURSOR_HSPLIT); |
5468 | |
5469 | } break; |
5470 | case VIEW_USE_3_VIEWPORTS: { |
5471 | if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) { |
5472 | draw_texture(hdiag_grabber, Vector2(mid_w - hdiag_grabber->get_width() / 2, mid_h - v_grabber->get_height() / 4)); |
5473 | set_default_cursor_shape(CURSOR_DRAG); |
5474 | } else if ((hovering_v && !dragging_h) || dragging_v) { |
5475 | draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2)); |
5476 | set_default_cursor_shape(CURSOR_VSPLIT); |
5477 | } else if (hovering_h || dragging_h) { |
5478 | draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, mid_h + v_grabber->get_height() / 2 + (size_bottom - h_grabber->get_height()) / 2)); |
5479 | set_default_cursor_shape(CURSOR_HSPLIT); |
5480 | } |
5481 | |
5482 | } break; |
5483 | case VIEW_USE_3_VIEWPORTS_ALT: { |
5484 | if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) { |
5485 | draw_texture(vdiag_grabber, Vector2(mid_w - vdiag_grabber->get_width() + v_grabber->get_height() / 4, mid_h - vdiag_grabber->get_height() / 2)); |
5486 | set_default_cursor_shape(CURSOR_DRAG); |
5487 | } else if ((hovering_v && !dragging_h) || dragging_v) { |
5488 | draw_texture(v_grabber, Vector2((size_left - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2)); |
5489 | set_default_cursor_shape(CURSOR_VSPLIT); |
5490 | } else if (hovering_h || dragging_h) { |
5491 | draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2)); |
5492 | set_default_cursor_shape(CURSOR_HSPLIT); |
5493 | } |
5494 | |
5495 | } break; |
5496 | case VIEW_USE_4_VIEWPORTS: { |
5497 | Vector2 half(mid_w, mid_h); |
5498 | if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) { |
5499 | draw_texture(vh_grabber, half - vh_grabber->get_size() / 2.0); |
5500 | set_default_cursor_shape(CURSOR_DRAG); |
5501 | } else if ((hovering_v && !dragging_h) || dragging_v) { |
5502 | draw_texture(v_grabber, half - v_grabber->get_size() / 2.0); |
5503 | set_default_cursor_shape(CURSOR_VSPLIT); |
5504 | } else if (hovering_h || dragging_h) { |
5505 | draw_texture(h_grabber, half - h_grabber->get_size() / 2.0); |
5506 | set_default_cursor_shape(CURSOR_HSPLIT); |
5507 | } |
5508 | |
5509 | } break; |
5510 | } |
5511 | } |
5512 | } break; |
5513 | |
5514 | case NOTIFICATION_SORT_CHILDREN: { |
5515 | Node3DEditorViewport *viewports[4]; |
5516 | int vc = 0; |
5517 | for (int i = 0; i < get_child_count(); i++) { |
5518 | viewports[vc] = Object::cast_to<Node3DEditorViewport>(get_child(i)); |
5519 | if (viewports[vc]) { |
5520 | vc++; |
5521 | } |
5522 | } |
5523 | |
5524 | ERR_FAIL_COND(vc != 4); |
5525 | |
5526 | Size2 size = get_size(); |
5527 | |
5528 | if (size.x < 10 || size.y < 10) { |
5529 | for (int i = 0; i < 4; i++) { |
5530 | viewports[i]->hide(); |
5531 | } |
5532 | return; |
5533 | } |
5534 | int h_sep = get_theme_constant(SNAME("separation" ), SNAME("HSplitContainer" )); |
5535 | |
5536 | int v_sep = get_theme_constant(SNAME("separation" ), SNAME("VSplitContainer" )); |
5537 | |
5538 | int mid_w = size.width * ratio_h; |
5539 | int mid_h = size.height * ratio_v; |
5540 | |
5541 | int size_left = mid_w - h_sep / 2; |
5542 | int size_right = size.width - mid_w - h_sep / 2; |
5543 | |
5544 | int size_top = mid_h - v_sep / 2; |
5545 | int size_bottom = size.height - mid_h - v_sep / 2; |
5546 | |
5547 | switch (view) { |
5548 | case VIEW_USE_1_VIEWPORT: { |
5549 | viewports[0]->show(); |
5550 | for (int i = 1; i < 4; i++) { |
5551 | viewports[i]->hide(); |
5552 | } |
5553 | |
5554 | fit_child_in_rect(viewports[0], Rect2(Vector2(), size)); |
5555 | |
5556 | } break; |
5557 | case VIEW_USE_2_VIEWPORTS: { |
5558 | for (int i = 0; i < 4; i++) { |
5559 | if (i == 1 || i == 3) { |
5560 | viewports[i]->hide(); |
5561 | } else { |
5562 | viewports[i]->show(); |
5563 | } |
5564 | } |
5565 | |
5566 | fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top))); |
5567 | fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size.width, size_bottom))); |
5568 | |
5569 | } break; |
5570 | case VIEW_USE_2_VIEWPORTS_ALT: { |
5571 | for (int i = 0; i < 4; i++) { |
5572 | if (i == 1 || i == 3) { |
5573 | viewports[i]->hide(); |
5574 | } else { |
5575 | viewports[i]->show(); |
5576 | } |
5577 | } |
5578 | fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size.height))); |
5579 | fit_child_in_rect(viewports[2], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height))); |
5580 | |
5581 | } break; |
5582 | case VIEW_USE_3_VIEWPORTS: { |
5583 | for (int i = 0; i < 4; i++) { |
5584 | if (i == 1) { |
5585 | viewports[i]->hide(); |
5586 | } else { |
5587 | viewports[i]->show(); |
5588 | } |
5589 | } |
5590 | |
5591 | fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top))); |
5592 | fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom))); |
5593 | fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom))); |
5594 | |
5595 | } break; |
5596 | case VIEW_USE_3_VIEWPORTS_ALT: { |
5597 | for (int i = 0; i < 4; i++) { |
5598 | if (i == 1) { |
5599 | viewports[i]->hide(); |
5600 | } else { |
5601 | viewports[i]->show(); |
5602 | } |
5603 | } |
5604 | |
5605 | fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top))); |
5606 | fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom))); |
5607 | fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height))); |
5608 | |
5609 | } break; |
5610 | case VIEW_USE_4_VIEWPORTS: { |
5611 | for (int i = 0; i < 4; i++) { |
5612 | viewports[i]->show(); |
5613 | } |
5614 | |
5615 | fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top))); |
5616 | fit_child_in_rect(viewports[1], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size_top))); |
5617 | fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom))); |
5618 | fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom))); |
5619 | |
5620 | } break; |
5621 | } |
5622 | } break; |
5623 | } |
5624 | } |
5625 | |
5626 | void Node3DEditorViewportContainer::set_view(View p_view) { |
5627 | view = p_view; |
5628 | queue_sort(); |
5629 | } |
5630 | |
5631 | Node3DEditorViewportContainer::View Node3DEditorViewportContainer::get_view() { |
5632 | return view; |
5633 | } |
5634 | |
5635 | Node3DEditorViewportContainer::Node3DEditorViewportContainer() { |
5636 | set_clip_contents(true); |
5637 | view = VIEW_USE_1_VIEWPORT; |
5638 | mouseover = false; |
5639 | ratio_h = 0.5; |
5640 | ratio_v = 0.5; |
5641 | hovering_v = false; |
5642 | hovering_h = false; |
5643 | dragging_v = false; |
5644 | dragging_h = false; |
5645 | } |
5646 | |
5647 | /////////////////////////////////////////////////////////////////// |
5648 | |
5649 | Node3DEditor *Node3DEditor::singleton = nullptr; |
5650 | |
5651 | Node3DEditorSelectedItem::~Node3DEditorSelectedItem() { |
5652 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
5653 | if (sbox_instance.is_valid()) { |
5654 | RenderingServer::get_singleton()->free(sbox_instance); |
5655 | } |
5656 | if (sbox_instance_offset.is_valid()) { |
5657 | RenderingServer::get_singleton()->free(sbox_instance_offset); |
5658 | } |
5659 | if (sbox_instance_xray.is_valid()) { |
5660 | RenderingServer::get_singleton()->free(sbox_instance_xray); |
5661 | } |
5662 | if (sbox_instance_xray_offset.is_valid()) { |
5663 | RenderingServer::get_singleton()->free(sbox_instance_xray_offset); |
5664 | } |
5665 | } |
5666 | |
5667 | void Node3DEditor::select_gizmo_highlight_axis(int p_axis) { |
5668 | for (int i = 0; i < 3; i++) { |
5669 | move_gizmo[i]->surface_set_material(0, i == p_axis ? gizmo_color_hl[i] : gizmo_color[i]); |
5670 | move_plane_gizmo[i]->surface_set_material(0, (i + 6) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]); |
5671 | rotate_gizmo[i]->surface_set_material(0, (i + 3) == p_axis ? rotate_gizmo_color_hl[i] : rotate_gizmo_color[i]); |
5672 | scale_gizmo[i]->surface_set_material(0, (i + 9) == p_axis ? gizmo_color_hl[i] : gizmo_color[i]); |
5673 | scale_plane_gizmo[i]->surface_set_material(0, (i + 12) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]); |
5674 | } |
5675 | } |
5676 | |
5677 | void Node3DEditor::update_transform_gizmo() { |
5678 | int count = 0; |
5679 | bool local_gizmo_coords = are_local_coords_enabled(); |
5680 | |
5681 | Vector3 gizmo_center; |
5682 | Basis gizmo_basis; |
5683 | |
5684 | Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr; |
5685 | |
5686 | if (se && se->gizmo.is_valid()) { |
5687 | for (const KeyValue<int, Transform3D> &E : se->subgizmos) { |
5688 | Transform3D xf = se->sp->get_global_transform() * se->gizmo->get_subgizmo_transform(E.key); |
5689 | gizmo_center += xf.origin; |
5690 | if (count == 0 && local_gizmo_coords) { |
5691 | gizmo_basis = xf.basis; |
5692 | } |
5693 | count++; |
5694 | } |
5695 | } else { |
5696 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
5697 | for (List<Node *>::Element *E = selection.front(); E; E = E->next()) { |
5698 | Node3D *sp = Object::cast_to<Node3D>(E->get()); |
5699 | if (!sp) { |
5700 | continue; |
5701 | } |
5702 | |
5703 | if (sp->has_meta("_edit_lock_" )) { |
5704 | continue; |
5705 | } |
5706 | |
5707 | Node3DEditorSelectedItem *sel_item = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
5708 | if (!sel_item) { |
5709 | continue; |
5710 | } |
5711 | |
5712 | Transform3D xf = sel_item->sp->get_global_transform(); |
5713 | gizmo_center += xf.origin; |
5714 | if (count == 0 && local_gizmo_coords) { |
5715 | gizmo_basis = xf.basis; |
5716 | } |
5717 | count++; |
5718 | } |
5719 | } |
5720 | |
5721 | gizmo.visible = count > 0; |
5722 | gizmo.transform.origin = (count > 0) ? gizmo_center / count : Vector3(); |
5723 | gizmo.transform.basis = (count == 1) ? gizmo_basis : Basis(); |
5724 | |
5725 | for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) { |
5726 | viewports[i]->update_transform_gizmo_view(); |
5727 | } |
5728 | } |
5729 | |
5730 | void _update_all_gizmos(Node *p_node) { |
5731 | for (int i = p_node->get_child_count() - 1; 0 <= i; --i) { |
5732 | Node3D *spatial_node = Object::cast_to<Node3D>(p_node->get_child(i)); |
5733 | if (spatial_node) { |
5734 | spatial_node->update_gizmos(); |
5735 | } |
5736 | |
5737 | _update_all_gizmos(p_node->get_child(i)); |
5738 | } |
5739 | } |
5740 | |
5741 | void Node3DEditor::update_all_gizmos(Node *p_node) { |
5742 | if (!p_node && is_inside_tree()) { |
5743 | p_node = get_tree()->get_edited_scene_root(); |
5744 | } |
5745 | |
5746 | if (!p_node) { |
5747 | // No edited scene, so nothing to update. |
5748 | return; |
5749 | } |
5750 | _update_all_gizmos(p_node); |
5751 | } |
5752 | |
5753 | Object *Node3DEditor::_get_editor_data(Object *p_what) { |
5754 | Node3D *sp = Object::cast_to<Node3D>(p_what); |
5755 | if (!sp) { |
5756 | return nullptr; |
5757 | } |
5758 | |
5759 | Node3DEditorSelectedItem *si = memnew(Node3DEditorSelectedItem); |
5760 | |
5761 | si->sp = sp; |
5762 | si->sbox_instance = RenderingServer::get_singleton()->instance_create2( |
5763 | selection_box->get_rid(), |
5764 | sp->get_world_3d()->get_scenario()); |
5765 | si->sbox_instance_offset = RenderingServer::get_singleton()->instance_create2( |
5766 | selection_box->get_rid(), |
5767 | sp->get_world_3d()->get_scenario()); |
5768 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting( |
5769 | si->sbox_instance, |
5770 | RS::SHADOW_CASTING_SETTING_OFF); |
5771 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting( |
5772 | si->sbox_instance_offset, |
5773 | RS::SHADOW_CASTING_SETTING_OFF); |
5774 | // Use the Edit layer to hide the selection box when View Gizmos is disabled, since it is a bit distracting. |
5775 | // It's still possible to approximately guess what is selected by looking at the manipulation gizmo position. |
5776 | RS::get_singleton()->instance_set_layer_mask(si->sbox_instance, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER); |
5777 | RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_offset, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER); |
5778 | RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
5779 | RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
5780 | RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_offset, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
5781 | RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_offset, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
5782 | si->sbox_instance_xray = RenderingServer::get_singleton()->instance_create2( |
5783 | selection_box_xray->get_rid(), |
5784 | sp->get_world_3d()->get_scenario()); |
5785 | si->sbox_instance_xray_offset = RenderingServer::get_singleton()->instance_create2( |
5786 | selection_box_xray->get_rid(), |
5787 | sp->get_world_3d()->get_scenario()); |
5788 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting( |
5789 | si->sbox_instance_xray, |
5790 | RS::SHADOW_CASTING_SETTING_OFF); |
5791 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting( |
5792 | si->sbox_instance_xray_offset, |
5793 | RS::SHADOW_CASTING_SETTING_OFF); |
5794 | // Use the Edit layer to hide the selection box when View Gizmos is disabled, since it is a bit distracting. |
5795 | // It's still possible to approximately guess what is selected by looking at the manipulation gizmo position. |
5796 | RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_xray, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER); |
5797 | RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_xray_offset, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER); |
5798 | RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
5799 | RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
5800 | RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray_offset, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
5801 | RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray_offset, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
5802 | |
5803 | return si; |
5804 | } |
5805 | |
5806 | void Node3DEditor::_generate_selection_boxes() { |
5807 | // Use two AABBs to create the illusion of a slightly thicker line. |
5808 | AABB aabb(Vector3(), Vector3(1, 1, 1)); |
5809 | |
5810 | // Create a x-ray (visible through solid surfaces) and standard version of the selection box. |
5811 | // Both will be drawn at the same position, but with different opacity. |
5812 | // This lets the user see where the selection is while still having a sense of depth. |
5813 | Ref<SurfaceTool> st = memnew(SurfaceTool); |
5814 | Ref<SurfaceTool> st_xray = memnew(SurfaceTool); |
5815 | |
5816 | st->begin(Mesh::PRIMITIVE_LINES); |
5817 | st_xray->begin(Mesh::PRIMITIVE_LINES); |
5818 | for (int i = 0; i < 12; i++) { |
5819 | Vector3 a, b; |
5820 | aabb.get_edge(i, a, b); |
5821 | |
5822 | st->add_vertex(a); |
5823 | st->add_vertex(b); |
5824 | st_xray->add_vertex(a); |
5825 | st_xray->add_vertex(b); |
5826 | } |
5827 | |
5828 | Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D); |
5829 | mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
5830 | const Color selection_box_color = EDITOR_GET("editors/3d/selection_box_color" ); |
5831 | mat->set_albedo(selection_box_color); |
5832 | mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
5833 | st->set_material(mat); |
5834 | selection_box = st->commit(); |
5835 | |
5836 | Ref<StandardMaterial3D> mat_xray = memnew(StandardMaterial3D); |
5837 | mat_xray->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
5838 | mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true); |
5839 | mat_xray->set_albedo(selection_box_color * Color(1, 1, 1, 0.15)); |
5840 | mat_xray->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
5841 | st_xray->set_material(mat_xray); |
5842 | selection_box_xray = st_xray->commit(); |
5843 | } |
5844 | |
5845 | Dictionary Node3DEditor::get_state() const { |
5846 | Dictionary d; |
5847 | |
5848 | d["snap_enabled" ] = snap_enabled; |
5849 | d["translate_snap" ] = get_translate_snap(); |
5850 | d["rotate_snap" ] = get_rotate_snap(); |
5851 | d["scale_snap" ] = get_scale_snap(); |
5852 | |
5853 | d["local_coords" ] = tool_option_button[TOOL_OPT_LOCAL_COORDS]->is_pressed(); |
5854 | |
5855 | int vc = 0; |
5856 | if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT))) { |
5857 | vc = 1; |
5858 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS))) { |
5859 | vc = 2; |
5860 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS))) { |
5861 | vc = 3; |
5862 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS))) { |
5863 | vc = 4; |
5864 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT))) { |
5865 | vc = 5; |
5866 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT))) { |
5867 | vc = 6; |
5868 | } |
5869 | |
5870 | d["viewport_mode" ] = vc; |
5871 | Array vpdata; |
5872 | for (int i = 0; i < 4; i++) { |
5873 | vpdata.push_back(viewports[i]->get_state()); |
5874 | } |
5875 | |
5876 | d["viewports" ] = vpdata; |
5877 | |
5878 | d["show_grid" ] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID)); |
5879 | d["show_origin" ] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN)); |
5880 | d["fov" ] = get_fov(); |
5881 | d["znear" ] = get_znear(); |
5882 | d["zfar" ] = get_zfar(); |
5883 | |
5884 | Dictionary gizmos_status; |
5885 | for (int i = 0; i < gizmo_plugins_by_name.size(); i++) { |
5886 | if (!gizmo_plugins_by_name[i]->can_be_hidden()) { |
5887 | continue; |
5888 | } |
5889 | int state = gizmos_menu->get_item_state(gizmos_menu->get_item_index(i)); |
5890 | String name = gizmo_plugins_by_name[i]->get_gizmo_name(); |
5891 | gizmos_status[name] = state; |
5892 | } |
5893 | |
5894 | d["gizmos_status" ] = gizmos_status; |
5895 | { |
5896 | Dictionary pd; |
5897 | |
5898 | pd["sun_rotation" ] = sun_rotation; |
5899 | |
5900 | pd["environ_sky_color" ] = environ_sky_color->get_pick_color(); |
5901 | pd["environ_ground_color" ] = environ_ground_color->get_pick_color(); |
5902 | pd["environ_energy" ] = environ_energy->get_value(); |
5903 | pd["environ_glow_enabled" ] = environ_glow_button->is_pressed(); |
5904 | pd["environ_tonemap_enabled" ] = environ_tonemap_button->is_pressed(); |
5905 | pd["environ_ao_enabled" ] = environ_ao_button->is_pressed(); |
5906 | pd["environ_gi_enabled" ] = environ_gi_button->is_pressed(); |
5907 | pd["sun_max_distance" ] = sun_max_distance->get_value(); |
5908 | |
5909 | pd["sun_color" ] = sun_color->get_pick_color(); |
5910 | pd["sun_energy" ] = sun_energy->get_value(); |
5911 | |
5912 | pd["sun_enabled" ] = sun_button->is_pressed(); |
5913 | pd["environ_enabled" ] = environ_button->is_pressed(); |
5914 | |
5915 | d["preview_sun_env" ] = pd; |
5916 | } |
5917 | |
5918 | return d; |
5919 | } |
5920 | |
5921 | void Node3DEditor::set_state(const Dictionary &p_state) { |
5922 | Dictionary d = p_state; |
5923 | |
5924 | if (d.has("snap_enabled" )) { |
5925 | snap_enabled = d["snap_enabled" ]; |
5926 | tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(d["snap_enabled" ]); |
5927 | } |
5928 | |
5929 | if (d.has("translate_snap" )) { |
5930 | snap_translate_value = d["translate_snap" ]; |
5931 | } |
5932 | |
5933 | if (d.has("rotate_snap" )) { |
5934 | snap_rotate_value = d["rotate_snap" ]; |
5935 | } |
5936 | |
5937 | if (d.has("scale_snap" )) { |
5938 | snap_scale_value = d["scale_snap" ]; |
5939 | } |
5940 | |
5941 | _snap_update(); |
5942 | |
5943 | if (d.has("local_coords" )) { |
5944 | tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(d["local_coords" ]); |
5945 | update_transform_gizmo(); |
5946 | } |
5947 | |
5948 | if (d.has("viewport_mode" )) { |
5949 | int vc = d["viewport_mode" ]; |
5950 | |
5951 | if (vc == 1) { |
5952 | _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT); |
5953 | } else if (vc == 2) { |
5954 | _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS); |
5955 | } else if (vc == 3) { |
5956 | _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS); |
5957 | } else if (vc == 4) { |
5958 | _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS); |
5959 | } else if (vc == 5) { |
5960 | _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT); |
5961 | } else if (vc == 6) { |
5962 | _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT); |
5963 | } |
5964 | } |
5965 | |
5966 | if (d.has("viewports" )) { |
5967 | Array vp = d["viewports" ]; |
5968 | uint32_t vp_size = static_cast<uint32_t>(vp.size()); |
5969 | if (vp_size > VIEWPORTS_COUNT) { |
5970 | WARN_PRINT("Ignoring superfluous viewport settings from spatial editor state." ); |
5971 | vp_size = VIEWPORTS_COUNT; |
5972 | } |
5973 | |
5974 | for (uint32_t i = 0; i < vp_size; i++) { |
5975 | viewports[i]->set_state(vp[i]); |
5976 | } |
5977 | } |
5978 | |
5979 | if (d.has("zfar" )) { |
5980 | settings_zfar->set_value(double(d["zfar" ])); |
5981 | } |
5982 | if (d.has("znear" )) { |
5983 | settings_znear->set_value(double(d["znear" ])); |
5984 | } |
5985 | if (d.has("fov" )) { |
5986 | settings_fov->set_value(double(d["fov" ])); |
5987 | } |
5988 | if (d.has("show_grid" )) { |
5989 | bool use = d["show_grid" ]; |
5990 | |
5991 | if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) { |
5992 | _menu_item_pressed(MENU_VIEW_GRID); |
5993 | } |
5994 | } |
5995 | |
5996 | if (d.has("show_origin" )) { |
5997 | bool use = d["show_origin" ]; |
5998 | |
5999 | if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) { |
6000 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use); |
6001 | RenderingServer::get_singleton()->instance_set_visible(origin_instance, use); |
6002 | } |
6003 | } |
6004 | |
6005 | if (d.has("gizmos_status" )) { |
6006 | Dictionary gizmos_status = d["gizmos_status" ]; |
6007 | List<Variant> keys; |
6008 | gizmos_status.get_key_list(&keys); |
6009 | |
6010 | for (int j = 0; j < gizmo_plugins_by_name.size(); ++j) { |
6011 | if (!gizmo_plugins_by_name[j]->can_be_hidden()) { |
6012 | continue; |
6013 | } |
6014 | int state = EditorNode3DGizmoPlugin::VISIBLE; |
6015 | for (int i = 0; i < keys.size(); i++) { |
6016 | if (gizmo_plugins_by_name.write[j]->get_gizmo_name() == String(keys[i])) { |
6017 | state = gizmos_status[keys[i]]; |
6018 | break; |
6019 | } |
6020 | } |
6021 | |
6022 | gizmo_plugins_by_name.write[j]->set_state(state); |
6023 | } |
6024 | _update_gizmos_menu(); |
6025 | } |
6026 | |
6027 | if (d.has("preview_sun_env" )) { |
6028 | sun_environ_updating = true; |
6029 | Dictionary pd = d["preview_sun_env" ]; |
6030 | sun_rotation = pd["sun_rotation" ]; |
6031 | |
6032 | environ_sky_color->set_pick_color(pd["environ_sky_color" ]); |
6033 | environ_ground_color->set_pick_color(pd["environ_ground_color" ]); |
6034 | environ_energy->set_value(pd["environ_energy" ]); |
6035 | environ_glow_button->set_pressed(pd["environ_glow_enabled" ]); |
6036 | environ_tonemap_button->set_pressed(pd["environ_tonemap_enabled" ]); |
6037 | environ_ao_button->set_pressed(pd["environ_ao_enabled" ]); |
6038 | environ_gi_button->set_pressed(pd["environ_gi_enabled" ]); |
6039 | sun_max_distance->set_value(pd["sun_max_distance" ]); |
6040 | |
6041 | sun_color->set_pick_color(pd["sun_color" ]); |
6042 | sun_energy->set_value(pd["sun_energy" ]); |
6043 | |
6044 | sun_button->set_pressed(pd["sun_enabled" ]); |
6045 | environ_button->set_pressed(pd["environ_enabled" ]); |
6046 | |
6047 | sun_environ_updating = false; |
6048 | |
6049 | _preview_settings_changed(); |
6050 | _update_preview_environment(); |
6051 | } else { |
6052 | _load_default_preview_settings(); |
6053 | sun_button->set_pressed(true); |
6054 | environ_button->set_pressed(true); |
6055 | _preview_settings_changed(); |
6056 | _update_preview_environment(); |
6057 | } |
6058 | } |
6059 | |
6060 | void Node3DEditor::edit(Node3D *p_spatial) { |
6061 | if (p_spatial != selected) { |
6062 | if (selected) { |
6063 | Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos(); |
6064 | for (int i = 0; i < gizmos.size(); i++) { |
6065 | Ref<EditorNode3DGizmo> seg = gizmos[i]; |
6066 | if (!seg.is_valid()) { |
6067 | continue; |
6068 | } |
6069 | seg->set_selected(false); |
6070 | } |
6071 | |
6072 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected); |
6073 | if (se) { |
6074 | se->gizmo.unref(); |
6075 | se->subgizmos.clear(); |
6076 | } |
6077 | |
6078 | selected->update_gizmos(); |
6079 | } |
6080 | |
6081 | selected = p_spatial; |
6082 | current_hover_gizmo = Ref<EditorNode3DGizmo>(); |
6083 | current_hover_gizmo_handle = -1; |
6084 | current_hover_gizmo_handle_secondary = false; |
6085 | |
6086 | if (selected) { |
6087 | Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos(); |
6088 | for (int i = 0; i < gizmos.size(); i++) { |
6089 | Ref<EditorNode3DGizmo> seg = gizmos[i]; |
6090 | if (!seg.is_valid()) { |
6091 | continue; |
6092 | } |
6093 | seg->set_selected(true); |
6094 | } |
6095 | selected->update_gizmos(); |
6096 | } |
6097 | } |
6098 | } |
6099 | |
6100 | void Node3DEditor::_snap_changed() { |
6101 | snap_translate_value = snap_translate->get_text().to_float(); |
6102 | snap_rotate_value = snap_rotate->get_text().to_float(); |
6103 | snap_scale_value = snap_scale->get_text().to_float(); |
6104 | |
6105 | EditorSettings::get_singleton()->set_project_metadata("3d_editor" , "snap_translate_value" , snap_translate_value); |
6106 | EditorSettings::get_singleton()->set_project_metadata("3d_editor" , "snap_rotate_value" , snap_rotate_value); |
6107 | EditorSettings::get_singleton()->set_project_metadata("3d_editor" , "snap_scale_value" , snap_scale_value); |
6108 | } |
6109 | |
6110 | void Node3DEditor::_snap_update() { |
6111 | snap_translate->set_text(String::num(snap_translate_value)); |
6112 | snap_rotate->set_text(String::num(snap_rotate_value)); |
6113 | snap_scale->set_text(String::num(snap_scale_value)); |
6114 | } |
6115 | |
6116 | void Node3DEditor::_xform_dialog_action() { |
6117 | Transform3D t; |
6118 | //translation |
6119 | Vector3 scale; |
6120 | Vector3 rotate; |
6121 | Vector3 translate; |
6122 | |
6123 | for (int i = 0; i < 3; i++) { |
6124 | translate[i] = xform_translate[i]->get_text().to_float(); |
6125 | rotate[i] = Math::deg_to_rad(xform_rotate[i]->get_text().to_float()); |
6126 | scale[i] = xform_scale[i]->get_text().to_float(); |
6127 | } |
6128 | |
6129 | t.basis.scale(scale); |
6130 | t.basis.rotate(rotate); |
6131 | t.origin = translate; |
6132 | |
6133 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
6134 | undo_redo->create_action(TTR("XForm Dialog" )); |
6135 | |
6136 | const List<Node *> &selection = editor_selection->get_selected_node_list(); |
6137 | |
6138 | for (Node *E : selection) { |
6139 | Node3D *sp = Object::cast_to<Node3D>(E); |
6140 | if (!sp) { |
6141 | continue; |
6142 | } |
6143 | |
6144 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
6145 | if (!se) { |
6146 | continue; |
6147 | } |
6148 | |
6149 | bool post = xform_type->get_selected() > 0; |
6150 | |
6151 | Transform3D tr = sp->get_global_gizmo_transform(); |
6152 | if (post) { |
6153 | tr = tr * t; |
6154 | } else { |
6155 | tr.basis = t.basis * tr.basis; |
6156 | tr.origin += t.origin; |
6157 | } |
6158 | |
6159 | undo_redo->add_do_method(sp, "set_global_transform" , tr); |
6160 | undo_redo->add_undo_method(sp, "set_global_transform" , sp->get_global_gizmo_transform()); |
6161 | } |
6162 | undo_redo->commit_action(); |
6163 | } |
6164 | |
6165 | void Node3DEditor::(bool pressed, int p_option) { |
6166 | switch (p_option) { |
6167 | case MENU_TOOL_LOCAL_COORDS: { |
6168 | tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(pressed); |
6169 | update_transform_gizmo(); |
6170 | } break; |
6171 | |
6172 | case MENU_TOOL_USE_SNAP: { |
6173 | tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(pressed); |
6174 | snap_enabled = pressed; |
6175 | } break; |
6176 | |
6177 | case MENU_TOOL_OVERRIDE_CAMERA: { |
6178 | EditorDebuggerNode *const debugger = EditorDebuggerNode::get_singleton(); |
6179 | |
6180 | using Override = EditorDebuggerNode::CameraOverride; |
6181 | if (pressed) { |
6182 | debugger->set_camera_override((Override)(Override::OVERRIDE_3D_1 + camera_override_viewport_id)); |
6183 | } else { |
6184 | debugger->set_camera_override(Override::OVERRIDE_NONE); |
6185 | } |
6186 | |
6187 | } break; |
6188 | } |
6189 | } |
6190 | |
6191 | void Node3DEditor::(int p_option) { |
6192 | const int idx = gizmos_menu->get_item_index(p_option); |
6193 | gizmos_menu->toggle_item_multistate(idx); |
6194 | |
6195 | // Change icon |
6196 | const int state = gizmos_menu->get_item_state(idx); |
6197 | switch (state) { |
6198 | case EditorNode3DGizmoPlugin::VISIBLE: |
6199 | gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_visible" ))); |
6200 | break; |
6201 | case EditorNode3DGizmoPlugin::ON_TOP: |
6202 | gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_xray" ))); |
6203 | break; |
6204 | case EditorNode3DGizmoPlugin::HIDDEN: |
6205 | gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_hidden" ))); |
6206 | break; |
6207 | } |
6208 | |
6209 | gizmo_plugins_by_name.write[p_option]->set_state(state); |
6210 | |
6211 | update_all_gizmos(); |
6212 | } |
6213 | |
6214 | void Node3DEditor::_update_camera_override_button(bool p_game_running) { |
6215 | Button *const button = tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]; |
6216 | |
6217 | if (p_game_running) { |
6218 | button->set_disabled(false); |
6219 | button->set_tooltip_text(TTR("Project Camera Override\nOverrides the running project's camera with the editor viewport camera." )); |
6220 | } else { |
6221 | button->set_disabled(true); |
6222 | button->set_pressed(false); |
6223 | button->set_tooltip_text(TTR("Project Camera Override\nNo project instance running. Run the project from the editor to use this feature." )); |
6224 | } |
6225 | } |
6226 | |
6227 | void Node3DEditor::_update_camera_override_viewport(Object *p_viewport) { |
6228 | Node3DEditorViewport *current_viewport = Object::cast_to<Node3DEditorViewport>(p_viewport); |
6229 | |
6230 | if (!current_viewport) { |
6231 | return; |
6232 | } |
6233 | |
6234 | EditorDebuggerNode *const debugger = EditorDebuggerNode::get_singleton(); |
6235 | |
6236 | camera_override_viewport_id = current_viewport->index; |
6237 | if (debugger->get_camera_override() >= EditorDebuggerNode::OVERRIDE_3D_1) { |
6238 | using Override = EditorDebuggerNode::CameraOverride; |
6239 | |
6240 | debugger->set_camera_override((Override)(Override::OVERRIDE_3D_1 + camera_override_viewport_id)); |
6241 | } |
6242 | } |
6243 | |
6244 | void Node3DEditor::(int p_option) { |
6245 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
6246 | switch (p_option) { |
6247 | case MENU_TOOL_SELECT: |
6248 | case MENU_TOOL_MOVE: |
6249 | case MENU_TOOL_ROTATE: |
6250 | case MENU_TOOL_SCALE: |
6251 | case MENU_TOOL_LIST_SELECT: { |
6252 | for (int i = 0; i < TOOL_MAX; i++) { |
6253 | tool_button[i]->set_pressed(i == p_option); |
6254 | } |
6255 | tool_mode = (ToolMode)p_option; |
6256 | update_transform_gizmo(); |
6257 | |
6258 | } break; |
6259 | case MENU_TRANSFORM_CONFIGURE_SNAP: { |
6260 | snap_dialog->popup_centered(Size2(200, 180)); |
6261 | } break; |
6262 | case MENU_TRANSFORM_DIALOG: { |
6263 | for (int i = 0; i < 3; i++) { |
6264 | xform_translate[i]->set_text("0" ); |
6265 | xform_rotate[i]->set_text("0" ); |
6266 | xform_scale[i]->set_text("1" ); |
6267 | } |
6268 | |
6269 | xform_dialog->popup_centered(Size2(320, 240) * EDSCALE); |
6270 | |
6271 | } break; |
6272 | case MENU_VIEW_USE_1_VIEWPORT: { |
6273 | viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_1_VIEWPORT); |
6274 | |
6275 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true); |
6276 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); |
6277 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false); |
6278 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false); |
6279 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false); |
6280 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false); |
6281 | |
6282 | } break; |
6283 | case MENU_VIEW_USE_2_VIEWPORTS: { |
6284 | viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_2_VIEWPORTS); |
6285 | |
6286 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); |
6287 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true); |
6288 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false); |
6289 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false); |
6290 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false); |
6291 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false); |
6292 | |
6293 | } break; |
6294 | case MENU_VIEW_USE_2_VIEWPORTS_ALT: { |
6295 | viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_2_VIEWPORTS_ALT); |
6296 | |
6297 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); |
6298 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); |
6299 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false); |
6300 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false); |
6301 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true); |
6302 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false); |
6303 | |
6304 | } break; |
6305 | case MENU_VIEW_USE_3_VIEWPORTS: { |
6306 | viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_3_VIEWPORTS); |
6307 | |
6308 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); |
6309 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); |
6310 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true); |
6311 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false); |
6312 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false); |
6313 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false); |
6314 | |
6315 | } break; |
6316 | case MENU_VIEW_USE_3_VIEWPORTS_ALT: { |
6317 | viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_3_VIEWPORTS_ALT); |
6318 | |
6319 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); |
6320 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); |
6321 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false); |
6322 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false); |
6323 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false); |
6324 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true); |
6325 | |
6326 | } break; |
6327 | case MENU_VIEW_USE_4_VIEWPORTS: { |
6328 | viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_4_VIEWPORTS); |
6329 | |
6330 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false); |
6331 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false); |
6332 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false); |
6333 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true); |
6334 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false); |
6335 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false); |
6336 | |
6337 | } break; |
6338 | case MENU_VIEW_ORIGIN: { |
6339 | bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option)); |
6340 | |
6341 | origin_enabled = !is_checked; |
6342 | RenderingServer::get_singleton()->instance_set_visible(origin_instance, origin_enabled); |
6343 | // Update the grid since its appearance depends on whether the origin is enabled |
6344 | _finish_grid(); |
6345 | _init_grid(); |
6346 | |
6347 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), origin_enabled); |
6348 | } break; |
6349 | case MENU_VIEW_GRID: { |
6350 | bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option)); |
6351 | |
6352 | grid_enabled = !is_checked; |
6353 | |
6354 | for (int i = 0; i < 3; ++i) { |
6355 | if (grid_enable[i]) { |
6356 | grid_visible[i] = grid_enabled; |
6357 | } |
6358 | } |
6359 | _finish_grid(); |
6360 | _init_grid(); |
6361 | |
6362 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), grid_enabled); |
6363 | |
6364 | } break; |
6365 | case MENU_VIEW_CAMERA_SETTINGS: { |
6366 | settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50)); |
6367 | } break; |
6368 | case MENU_SNAP_TO_FLOOR: { |
6369 | snap_selected_nodes_to_floor(); |
6370 | } break; |
6371 | case MENU_LOCK_SELECTED: { |
6372 | undo_redo->create_action(TTR("Lock Selected" )); |
6373 | |
6374 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
6375 | |
6376 | for (Node *E : selection) { |
6377 | Node3D *spatial = Object::cast_to<Node3D>(E); |
6378 | if (!spatial || !spatial->is_inside_tree()) { |
6379 | continue; |
6380 | } |
6381 | |
6382 | if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) { |
6383 | continue; |
6384 | } |
6385 | |
6386 | undo_redo->add_do_method(spatial, "set_meta" , "_edit_lock_" , true); |
6387 | undo_redo->add_undo_method(spatial, "remove_meta" , "_edit_lock_" ); |
6388 | undo_redo->add_do_method(this, "emit_signal" , "item_lock_status_changed" ); |
6389 | undo_redo->add_undo_method(this, "emit_signal" , "item_lock_status_changed" ); |
6390 | } |
6391 | |
6392 | undo_redo->add_do_method(this, "_refresh_menu_icons" ); |
6393 | undo_redo->add_undo_method(this, "_refresh_menu_icons" ); |
6394 | undo_redo->commit_action(); |
6395 | } break; |
6396 | case MENU_UNLOCK_SELECTED: { |
6397 | undo_redo->create_action(TTR("Unlock Selected" )); |
6398 | |
6399 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
6400 | |
6401 | for (Node *E : selection) { |
6402 | Node3D *spatial = Object::cast_to<Node3D>(E); |
6403 | if (!spatial || !spatial->is_inside_tree()) { |
6404 | continue; |
6405 | } |
6406 | |
6407 | if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) { |
6408 | continue; |
6409 | } |
6410 | |
6411 | undo_redo->add_do_method(spatial, "remove_meta" , "_edit_lock_" ); |
6412 | undo_redo->add_undo_method(spatial, "set_meta" , "_edit_lock_" , true); |
6413 | undo_redo->add_do_method(this, "emit_signal" , "item_lock_status_changed" ); |
6414 | undo_redo->add_undo_method(this, "emit_signal" , "item_lock_status_changed" ); |
6415 | } |
6416 | |
6417 | undo_redo->add_do_method(this, "_refresh_menu_icons" ); |
6418 | undo_redo->add_undo_method(this, "_refresh_menu_icons" ); |
6419 | undo_redo->commit_action(); |
6420 | } break; |
6421 | case MENU_GROUP_SELECTED: { |
6422 | undo_redo->create_action(TTR("Group Selected" )); |
6423 | |
6424 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
6425 | |
6426 | for (Node *E : selection) { |
6427 | Node3D *spatial = Object::cast_to<Node3D>(E); |
6428 | if (!spatial || !spatial->is_inside_tree()) { |
6429 | continue; |
6430 | } |
6431 | |
6432 | if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) { |
6433 | continue; |
6434 | } |
6435 | |
6436 | undo_redo->add_do_method(spatial, "set_meta" , "_edit_group_" , true); |
6437 | undo_redo->add_undo_method(spatial, "remove_meta" , "_edit_group_" ); |
6438 | undo_redo->add_do_method(this, "emit_signal" , "item_group_status_changed" ); |
6439 | undo_redo->add_undo_method(this, "emit_signal" , "item_group_status_changed" ); |
6440 | } |
6441 | |
6442 | undo_redo->add_do_method(this, "_refresh_menu_icons" ); |
6443 | undo_redo->add_undo_method(this, "_refresh_menu_icons" ); |
6444 | undo_redo->commit_action(); |
6445 | } break; |
6446 | case MENU_UNGROUP_SELECTED: { |
6447 | undo_redo->create_action(TTR("Ungroup Selected" )); |
6448 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
6449 | |
6450 | for (Node *E : selection) { |
6451 | Node3D *spatial = Object::cast_to<Node3D>(E); |
6452 | if (!spatial || !spatial->is_inside_tree()) { |
6453 | continue; |
6454 | } |
6455 | |
6456 | if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) { |
6457 | continue; |
6458 | } |
6459 | |
6460 | undo_redo->add_do_method(spatial, "remove_meta" , "_edit_group_" ); |
6461 | undo_redo->add_undo_method(spatial, "set_meta" , "_edit_group_" , true); |
6462 | undo_redo->add_do_method(this, "emit_signal" , "item_group_status_changed" ); |
6463 | undo_redo->add_undo_method(this, "emit_signal" , "item_group_status_changed" ); |
6464 | } |
6465 | |
6466 | undo_redo->add_do_method(this, "_refresh_menu_icons" ); |
6467 | undo_redo->add_undo_method(this, "_refresh_menu_icons" ); |
6468 | undo_redo->commit_action(); |
6469 | } break; |
6470 | } |
6471 | } |
6472 | |
6473 | void Node3DEditor::_init_indicators() { |
6474 | { |
6475 | origin_enabled = true; |
6476 | grid_enabled = true; |
6477 | |
6478 | indicator_mat.instantiate(); |
6479 | indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
6480 | indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); |
6481 | indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); |
6482 | indicator_mat->set_transparency(StandardMaterial3D::Transparency::TRANSPARENCY_ALPHA_DEPTH_PRE_PASS); |
6483 | |
6484 | Vector<Color> origin_colors; |
6485 | Vector<Vector3> origin_points; |
6486 | |
6487 | const int count_of_elements = 3 * 6; |
6488 | origin_colors.resize(count_of_elements); |
6489 | origin_points.resize(count_of_elements); |
6490 | |
6491 | int x = 0; |
6492 | |
6493 | for (int i = 0; i < 3; i++) { |
6494 | Vector3 axis; |
6495 | axis[i] = 1; |
6496 | Color origin_color; |
6497 | switch (i) { |
6498 | case 0: |
6499 | origin_color = get_theme_color(SNAME("axis_x_color" ), EditorStringName(Editor)); |
6500 | break; |
6501 | case 1: |
6502 | origin_color = get_theme_color(SNAME("axis_y_color" ), EditorStringName(Editor)); |
6503 | break; |
6504 | case 2: |
6505 | origin_color = get_theme_color(SNAME("axis_z_color" ), EditorStringName(Editor)); |
6506 | break; |
6507 | default: |
6508 | origin_color = Color(); |
6509 | break; |
6510 | } |
6511 | |
6512 | grid_enable[i] = false; |
6513 | grid_visible[i] = false; |
6514 | |
6515 | origin_colors.set(x, origin_color); |
6516 | origin_colors.set(x + 1, origin_color); |
6517 | origin_colors.set(x + 2, origin_color); |
6518 | origin_colors.set(x + 3, origin_color); |
6519 | origin_colors.set(x + 4, origin_color); |
6520 | origin_colors.set(x + 5, origin_color); |
6521 | // To both allow having a large origin size and avoid jitter |
6522 | // at small scales, we should segment the line into pieces. |
6523 | // 3 pieces seems to do the trick, and let's use powers of 2. |
6524 | origin_points.set(x, axis * 1048576); |
6525 | origin_points.set(x + 1, axis * 1024); |
6526 | origin_points.set(x + 2, axis * 1024); |
6527 | origin_points.set(x + 3, axis * -1024); |
6528 | origin_points.set(x + 4, axis * -1024); |
6529 | origin_points.set(x + 5, axis * -1048576); |
6530 | x += 6; |
6531 | } |
6532 | |
6533 | Ref<Shader> grid_shader = memnew(Shader); |
6534 | grid_shader->set_code(R"( |
6535 | // 3D editor grid shader. |
6536 | |
6537 | shader_type spatial; |
6538 | |
6539 | render_mode unshaded; |
6540 | |
6541 | uniform bool orthogonal; |
6542 | uniform float grid_size; |
6543 | |
6544 | void vertex() { |
6545 | // From FLAG_SRGB_VERTEX_COLOR. |
6546 | if (!OUTPUT_IS_SRGB) { |
6547 | COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045))); |
6548 | } |
6549 | } |
6550 | |
6551 | void fragment() { |
6552 | ALBEDO = COLOR.rgb; |
6553 | vec3 dir = orthogonal ? -vec3(0, 0, 1) : VIEW; |
6554 | float angle_fade = abs(dot(dir, NORMAL)); |
6555 | angle_fade = smoothstep(0.05, 0.2, angle_fade); |
6556 | |
6557 | vec3 world_pos = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz; |
6558 | vec3 world_normal = (INV_VIEW_MATRIX * vec4(NORMAL, 0.0)).xyz; |
6559 | vec3 camera_world_pos = INV_VIEW_MATRIX[3].xyz; |
6560 | vec3 camera_world_pos_on_plane = camera_world_pos * (1.0 - world_normal); |
6561 | float dist_fade = 1.0 - (distance(world_pos, camera_world_pos_on_plane) / grid_size); |
6562 | dist_fade = smoothstep(0.02, 0.3, dist_fade); |
6563 | |
6564 | ALPHA = COLOR.a * dist_fade * angle_fade; |
6565 | } |
6566 | )" ); |
6567 | |
6568 | for (int i = 0; i < 3; i++) { |
6569 | grid_mat[i].instantiate(); |
6570 | grid_mat[i]->set_shader(grid_shader); |
6571 | } |
6572 | |
6573 | grid_enable[0] = EDITOR_GET("editors/3d/grid_xy_plane" ); |
6574 | grid_enable[1] = EDITOR_GET("editors/3d/grid_yz_plane" ); |
6575 | grid_enable[2] = EDITOR_GET("editors/3d/grid_xz_plane" ); |
6576 | grid_visible[0] = grid_enable[0]; |
6577 | grid_visible[1] = grid_enable[1]; |
6578 | grid_visible[2] = grid_enable[2]; |
6579 | |
6580 | _init_grid(); |
6581 | |
6582 | origin = RenderingServer::get_singleton()->mesh_create(); |
6583 | Array d; |
6584 | d.resize(RS::ARRAY_MAX); |
6585 | d[RenderingServer::ARRAY_VERTEX] = origin_points; |
6586 | d[RenderingServer::ARRAY_COLOR] = origin_colors; |
6587 | |
6588 | RenderingServer::get_singleton()->mesh_add_surface_from_arrays(origin, RenderingServer::PRIMITIVE_LINES, d); |
6589 | RenderingServer::get_singleton()->mesh_surface_set_material(origin, 0, indicator_mat->get_rid()); |
6590 | |
6591 | origin_instance = RenderingServer::get_singleton()->instance_create2(origin, get_tree()->get_root()->get_world_3d()->get_scenario()); |
6592 | RS::get_singleton()->instance_set_layer_mask(origin_instance, 1 << Node3DEditorViewport::GIZMO_GRID_LAYER); |
6593 | RS::get_singleton()->instance_geometry_set_flag(origin_instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
6594 | RS::get_singleton()->instance_geometry_set_flag(origin_instance, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
6595 | |
6596 | RenderingServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, RS::SHADOW_CASTING_SETTING_OFF); |
6597 | } |
6598 | |
6599 | { |
6600 | //move gizmo |
6601 | |
6602 | // Inverted zxy. |
6603 | Vector3 ivec = Vector3(0, 0, -1); |
6604 | Vector3 nivec = Vector3(-1, -1, 0); |
6605 | Vector3 ivec2 = Vector3(-1, 0, 0); |
6606 | Vector3 ivec3 = Vector3(0, -1, 0); |
6607 | |
6608 | for (int i = 0; i < 3; i++) { |
6609 | Color col; |
6610 | switch (i) { |
6611 | case 0: |
6612 | col = get_theme_color(SNAME("axis_x_color" ), EditorStringName(Editor)); |
6613 | break; |
6614 | case 1: |
6615 | col = get_theme_color(SNAME("axis_y_color" ), EditorStringName(Editor)); |
6616 | break; |
6617 | case 2: |
6618 | col = get_theme_color(SNAME("axis_z_color" ), EditorStringName(Editor)); |
6619 | break; |
6620 | default: |
6621 | col = Color(); |
6622 | break; |
6623 | } |
6624 | |
6625 | col.a = EDITOR_GET("editors/3d/manipulator_gizmo_opacity" ); |
6626 | |
6627 | move_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh)); |
6628 | move_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh)); |
6629 | rotate_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh)); |
6630 | scale_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh)); |
6631 | scale_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh)); |
6632 | axis_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh)); |
6633 | |
6634 | Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D); |
6635 | mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
6636 | mat->set_on_top_of_alpha(); |
6637 | mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
6638 | mat->set_albedo(col); |
6639 | gizmo_color[i] = mat; |
6640 | |
6641 | Ref<StandardMaterial3D> mat_hl = mat->duplicate(); |
6642 | const Color albedo = col.from_hsv(col.get_h(), 0.25, 1.0, 1); |
6643 | mat_hl->set_albedo(albedo); |
6644 | gizmo_color_hl[i] = mat_hl; |
6645 | |
6646 | //translate |
6647 | { |
6648 | Ref<SurfaceTool> surftool = memnew(SurfaceTool); |
6649 | surftool->begin(Mesh::PRIMITIVE_TRIANGLES); |
6650 | |
6651 | // Arrow profile |
6652 | const int arrow_points = 5; |
6653 | Vector3 arrow[5] = { |
6654 | nivec * 0.0 + ivec * 0.0, |
6655 | nivec * 0.01 + ivec * 0.0, |
6656 | nivec * 0.01 + ivec * GIZMO_ARROW_OFFSET, |
6657 | nivec * 0.065 + ivec * GIZMO_ARROW_OFFSET, |
6658 | nivec * 0.0 + ivec * (GIZMO_ARROW_OFFSET + GIZMO_ARROW_SIZE), |
6659 | }; |
6660 | |
6661 | int arrow_sides = 16; |
6662 | |
6663 | const real_t arrow_sides_step = Math_TAU / arrow_sides; |
6664 | for (int k = 0; k < arrow_sides; k++) { |
6665 | Basis ma(ivec, k * arrow_sides_step); |
6666 | Basis mb(ivec, (k + 1) * arrow_sides_step); |
6667 | |
6668 | for (int j = 0; j < arrow_points - 1; j++) { |
6669 | Vector3 points[4] = { |
6670 | ma.xform(arrow[j]), |
6671 | mb.xform(arrow[j]), |
6672 | mb.xform(arrow[j + 1]), |
6673 | ma.xform(arrow[j + 1]), |
6674 | }; |
6675 | surftool->add_vertex(points[0]); |
6676 | surftool->add_vertex(points[1]); |
6677 | surftool->add_vertex(points[2]); |
6678 | |
6679 | surftool->add_vertex(points[0]); |
6680 | surftool->add_vertex(points[2]); |
6681 | surftool->add_vertex(points[3]); |
6682 | } |
6683 | } |
6684 | |
6685 | surftool->set_material(mat); |
6686 | surftool->commit(move_gizmo[i]); |
6687 | } |
6688 | |
6689 | // Plane Translation |
6690 | { |
6691 | Ref<SurfaceTool> surftool = memnew(SurfaceTool); |
6692 | surftool->begin(Mesh::PRIMITIVE_TRIANGLES); |
6693 | |
6694 | Vector3 vec = ivec2 - ivec3; |
6695 | Vector3 plane[4] = { |
6696 | vec * GIZMO_PLANE_DST, |
6697 | vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE, |
6698 | vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE), |
6699 | vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE |
6700 | }; |
6701 | |
6702 | Basis ma(ivec, Math_PI / 2); |
6703 | |
6704 | Vector3 points[4] = { |
6705 | ma.xform(plane[0]), |
6706 | ma.xform(plane[1]), |
6707 | ma.xform(plane[2]), |
6708 | ma.xform(plane[3]), |
6709 | }; |
6710 | surftool->add_vertex(points[0]); |
6711 | surftool->add_vertex(points[1]); |
6712 | surftool->add_vertex(points[2]); |
6713 | |
6714 | surftool->add_vertex(points[0]); |
6715 | surftool->add_vertex(points[2]); |
6716 | surftool->add_vertex(points[3]); |
6717 | |
6718 | Ref<StandardMaterial3D> plane_mat = memnew(StandardMaterial3D); |
6719 | plane_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
6720 | plane_mat->set_on_top_of_alpha(); |
6721 | plane_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
6722 | plane_mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED); |
6723 | plane_mat->set_albedo(col); |
6724 | plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides |
6725 | surftool->set_material(plane_mat); |
6726 | surftool->commit(move_plane_gizmo[i]); |
6727 | |
6728 | Ref<StandardMaterial3D> plane_mat_hl = plane_mat->duplicate(); |
6729 | plane_mat_hl->set_albedo(albedo); |
6730 | plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides |
6731 | } |
6732 | |
6733 | // Rotate |
6734 | { |
6735 | Ref<SurfaceTool> surftool = memnew(SurfaceTool); |
6736 | surftool->begin(Mesh::PRIMITIVE_TRIANGLES); |
6737 | |
6738 | int n = 128; // number of circle segments |
6739 | int m = 3; // number of thickness segments |
6740 | |
6741 | real_t step = Math_TAU / n; |
6742 | for (int j = 0; j < n; ++j) { |
6743 | Basis basis = Basis(ivec, j * step); |
6744 | |
6745 | Vector3 vertex = basis.xform(ivec2 * GIZMO_CIRCLE_SIZE); |
6746 | |
6747 | for (int k = 0; k < m; ++k) { |
6748 | Vector2 ofs = Vector2(Math::cos((Math_TAU * k) / m), Math::sin((Math_TAU * k) / m)); |
6749 | Vector3 normal = ivec * ofs.x + ivec2 * ofs.y; |
6750 | |
6751 | surftool->set_normal(basis.xform(normal)); |
6752 | surftool->add_vertex(vertex); |
6753 | } |
6754 | } |
6755 | |
6756 | for (int j = 0; j < n; ++j) { |
6757 | for (int k = 0; k < m; ++k) { |
6758 | int current_ring = j * m; |
6759 | int next_ring = ((j + 1) % n) * m; |
6760 | int current_segment = k; |
6761 | int next_segment = (k + 1) % m; |
6762 | |
6763 | surftool->add_index(current_ring + next_segment); |
6764 | surftool->add_index(current_ring + current_segment); |
6765 | surftool->add_index(next_ring + current_segment); |
6766 | |
6767 | surftool->add_index(next_ring + current_segment); |
6768 | surftool->add_index(next_ring + next_segment); |
6769 | surftool->add_index(current_ring + next_segment); |
6770 | } |
6771 | } |
6772 | |
6773 | Ref<Shader> rotate_shader = memnew(Shader); |
6774 | |
6775 | rotate_shader->set_code(R"( |
6776 | // 3D editor rotation manipulator gizmo shader. |
6777 | |
6778 | shader_type spatial; |
6779 | |
6780 | render_mode unshaded, depth_test_disabled; |
6781 | |
6782 | uniform vec4 albedo; |
6783 | |
6784 | mat3 orthonormalize(mat3 m) { |
6785 | vec3 x = normalize(m[0]); |
6786 | vec3 y = normalize(m[1] - x * dot(x, m[1])); |
6787 | vec3 z = m[2] - x * dot(x, m[2]); |
6788 | z = normalize(z - y * (dot(y,m[2]))); |
6789 | return mat3(x,y,z); |
6790 | } |
6791 | |
6792 | void vertex() { |
6793 | mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX)); |
6794 | vec3 n = mv * VERTEX; |
6795 | float orientation = dot(vec3(0, 0, -1), n); |
6796 | if (orientation <= 0.005) { |
6797 | VERTEX += NORMAL * 0.02; |
6798 | } |
6799 | } |
6800 | |
6801 | void fragment() { |
6802 | ALBEDO = albedo.rgb; |
6803 | ALPHA = albedo.a; |
6804 | } |
6805 | )" ); |
6806 | |
6807 | Ref<ShaderMaterial> rotate_mat = memnew(ShaderMaterial); |
6808 | rotate_mat->set_render_priority(Material::RENDER_PRIORITY_MAX); |
6809 | rotate_mat->set_shader(rotate_shader); |
6810 | rotate_mat->set_shader_parameter("albedo" , col); |
6811 | rotate_gizmo_color[i] = rotate_mat; |
6812 | |
6813 | Array arrays = surftool->commit_to_arrays(); |
6814 | rotate_gizmo[i]->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays); |
6815 | rotate_gizmo[i]->surface_set_material(0, rotate_mat); |
6816 | |
6817 | Ref<ShaderMaterial> rotate_mat_hl = rotate_mat->duplicate(); |
6818 | rotate_mat_hl->set_shader_parameter("albedo" , albedo); |
6819 | rotate_gizmo_color_hl[i] = rotate_mat_hl; |
6820 | |
6821 | if (i == 2) { // Rotation white outline |
6822 | Ref<ShaderMaterial> border_mat = rotate_mat->duplicate(); |
6823 | |
6824 | Ref<Shader> border_shader = memnew(Shader); |
6825 | border_shader->set_code(R"( |
6826 | // 3D editor rotation manipulator gizmo shader (white outline). |
6827 | |
6828 | shader_type spatial; |
6829 | |
6830 | render_mode unshaded, depth_test_disabled; |
6831 | |
6832 | uniform vec4 albedo; |
6833 | |
6834 | mat3 orthonormalize(mat3 m) { |
6835 | vec3 x = normalize(m[0]); |
6836 | vec3 y = normalize(m[1] - x * dot(x, m[1])); |
6837 | vec3 z = m[2] - x * dot(x, m[2]); |
6838 | z = normalize(z - y * (dot(y,m[2]))); |
6839 | return mat3(x,y,z); |
6840 | } |
6841 | |
6842 | void vertex() { |
6843 | mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX)); |
6844 | mv = inverse(mv); |
6845 | VERTEX += NORMAL*0.008; |
6846 | vec3 camera_dir_local = mv * vec3(0,0,1); |
6847 | vec3 camera_up_local = mv * vec3(0,1,0); |
6848 | mat3 rotation_matrix = mat3(cross(camera_dir_local, camera_up_local), camera_up_local, camera_dir_local); |
6849 | VERTEX = rotation_matrix * VERTEX; |
6850 | } |
6851 | |
6852 | void fragment() { |
6853 | ALBEDO = albedo.rgb; |
6854 | ALPHA = albedo.a; |
6855 | } |
6856 | )" ); |
6857 | |
6858 | border_mat->set_shader(border_shader); |
6859 | border_mat->set_shader_parameter("albedo" , Color(0.75, 0.75, 0.75, col.a / 3.0)); |
6860 | |
6861 | rotate_gizmo[3] = Ref<ArrayMesh>(memnew(ArrayMesh)); |
6862 | rotate_gizmo[3]->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays); |
6863 | rotate_gizmo[3]->surface_set_material(0, border_mat); |
6864 | } |
6865 | } |
6866 | |
6867 | // Scale |
6868 | { |
6869 | Ref<SurfaceTool> surftool = memnew(SurfaceTool); |
6870 | surftool->begin(Mesh::PRIMITIVE_TRIANGLES); |
6871 | |
6872 | // Cube arrow profile |
6873 | const int arrow_points = 6; |
6874 | Vector3 arrow[6] = { |
6875 | nivec * 0.0 + ivec * 0.0, |
6876 | nivec * 0.01 + ivec * 0.0, |
6877 | nivec * 0.01 + ivec * 1.0 * GIZMO_SCALE_OFFSET, |
6878 | nivec * 0.07 + ivec * 1.0 * GIZMO_SCALE_OFFSET, |
6879 | nivec * 0.07 + ivec * 1.11 * GIZMO_SCALE_OFFSET, |
6880 | nivec * 0.0 + ivec * 1.11 * GIZMO_SCALE_OFFSET, |
6881 | }; |
6882 | |
6883 | int arrow_sides = 4; |
6884 | |
6885 | const real_t arrow_sides_step = Math_TAU / arrow_sides; |
6886 | for (int k = 0; k < 4; k++) { |
6887 | Basis ma(ivec, k * arrow_sides_step); |
6888 | Basis mb(ivec, (k + 1) * arrow_sides_step); |
6889 | |
6890 | for (int j = 0; j < arrow_points - 1; j++) { |
6891 | Vector3 points[4] = { |
6892 | ma.xform(arrow[j]), |
6893 | mb.xform(arrow[j]), |
6894 | mb.xform(arrow[j + 1]), |
6895 | ma.xform(arrow[j + 1]), |
6896 | }; |
6897 | surftool->add_vertex(points[0]); |
6898 | surftool->add_vertex(points[1]); |
6899 | surftool->add_vertex(points[2]); |
6900 | |
6901 | surftool->add_vertex(points[0]); |
6902 | surftool->add_vertex(points[2]); |
6903 | surftool->add_vertex(points[3]); |
6904 | } |
6905 | } |
6906 | |
6907 | surftool->set_material(mat); |
6908 | surftool->commit(scale_gizmo[i]); |
6909 | } |
6910 | |
6911 | // Plane Scale |
6912 | { |
6913 | Ref<SurfaceTool> surftool = memnew(SurfaceTool); |
6914 | surftool->begin(Mesh::PRIMITIVE_TRIANGLES); |
6915 | |
6916 | Vector3 vec = ivec2 - ivec3; |
6917 | Vector3 plane[4] = { |
6918 | vec * GIZMO_PLANE_DST, |
6919 | vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE, |
6920 | vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE), |
6921 | vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE |
6922 | }; |
6923 | |
6924 | Basis ma(ivec, Math_PI / 2); |
6925 | |
6926 | Vector3 points[4] = { |
6927 | ma.xform(plane[0]), |
6928 | ma.xform(plane[1]), |
6929 | ma.xform(plane[2]), |
6930 | ma.xform(plane[3]), |
6931 | }; |
6932 | surftool->add_vertex(points[0]); |
6933 | surftool->add_vertex(points[1]); |
6934 | surftool->add_vertex(points[2]); |
6935 | |
6936 | surftool->add_vertex(points[0]); |
6937 | surftool->add_vertex(points[2]); |
6938 | surftool->add_vertex(points[3]); |
6939 | |
6940 | Ref<StandardMaterial3D> plane_mat = memnew(StandardMaterial3D); |
6941 | plane_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
6942 | plane_mat->set_on_top_of_alpha(); |
6943 | plane_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
6944 | plane_mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED); |
6945 | plane_mat->set_albedo(col); |
6946 | plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides |
6947 | surftool->set_material(plane_mat); |
6948 | surftool->commit(scale_plane_gizmo[i]); |
6949 | |
6950 | Ref<StandardMaterial3D> plane_mat_hl = plane_mat->duplicate(); |
6951 | plane_mat_hl->set_albedo(col.from_hsv(col.get_h(), 0.25, 1.0, 1)); |
6952 | plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides |
6953 | } |
6954 | |
6955 | // Lines to visualize transforms locked to an axis/plane |
6956 | { |
6957 | Ref<SurfaceTool> surftool = memnew(SurfaceTool); |
6958 | surftool->begin(Mesh::PRIMITIVE_LINE_STRIP); |
6959 | |
6960 | Vector3 vec; |
6961 | vec[i] = 1; |
6962 | |
6963 | // line extending through infinity(ish) |
6964 | surftool->add_vertex(vec * -1048576); |
6965 | surftool->add_vertex(Vector3()); |
6966 | surftool->add_vertex(vec * 1048576); |
6967 | surftool->set_material(mat_hl); |
6968 | surftool->commit(axis_gizmo[i]); |
6969 | } |
6970 | } |
6971 | } |
6972 | |
6973 | _generate_selection_boxes(); |
6974 | } |
6975 | |
6976 | void Node3DEditor::() { |
6977 | gizmos_menu->clear(); |
6978 | |
6979 | for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) { |
6980 | if (!gizmo_plugins_by_name[i]->can_be_hidden()) { |
6981 | continue; |
6982 | } |
6983 | String plugin_name = gizmo_plugins_by_name[i]->get_gizmo_name(); |
6984 | const int plugin_state = gizmo_plugins_by_name[i]->get_state(); |
6985 | gizmos_menu->add_multistate_item(plugin_name, 3, plugin_state, i); |
6986 | const int idx = gizmos_menu->get_item_index(i); |
6987 | gizmos_menu->set_item_tooltip( |
6988 | idx, |
6989 | TTR("Click to toggle between visibility states.\n\nOpen eye: Gizmo is visible.\nClosed eye: Gizmo is hidden.\nHalf-open eye: Gizmo is also visible through opaque surfaces (\"x-ray\")." )); |
6990 | switch (plugin_state) { |
6991 | case EditorNode3DGizmoPlugin::VISIBLE: |
6992 | gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_visible" ))); |
6993 | break; |
6994 | case EditorNode3DGizmoPlugin::ON_TOP: |
6995 | gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_xray" ))); |
6996 | break; |
6997 | case EditorNode3DGizmoPlugin::HIDDEN: |
6998 | gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_hidden" ))); |
6999 | break; |
7000 | } |
7001 | } |
7002 | } |
7003 | |
7004 | void Node3DEditor::() { |
7005 | for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) { |
7006 | if (!gizmo_plugins_by_name[i]->can_be_hidden()) { |
7007 | continue; |
7008 | } |
7009 | const int plugin_state = gizmo_plugins_by_name[i]->get_state(); |
7010 | const int idx = gizmos_menu->get_item_index(i); |
7011 | switch (plugin_state) { |
7012 | case EditorNode3DGizmoPlugin::VISIBLE: |
7013 | gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_visible" ))); |
7014 | break; |
7015 | case EditorNode3DGizmoPlugin::ON_TOP: |
7016 | gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_xray" ))); |
7017 | break; |
7018 | case EditorNode3DGizmoPlugin::HIDDEN: |
7019 | gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_hidden" ))); |
7020 | break; |
7021 | } |
7022 | } |
7023 | } |
7024 | |
7025 | void Node3DEditor::_init_grid() { |
7026 | if (!grid_enabled) { |
7027 | return; |
7028 | } |
7029 | Camera3D *camera = get_editor_viewport(0)->camera; |
7030 | Vector3 camera_position = camera->get_position(); |
7031 | if (camera_position == Vector3()) { |
7032 | return; // Camera3D is invalid, don't draw the grid. |
7033 | } |
7034 | |
7035 | bool orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL; |
7036 | |
7037 | Vector<Color> grid_colors[3]; |
7038 | Vector<Vector3> grid_points[3]; |
7039 | Vector<Vector3> grid_normals[3]; |
7040 | |
7041 | Color primary_grid_color = EDITOR_GET("editors/3d/primary_grid_color" ); |
7042 | Color secondary_grid_color = EDITOR_GET("editors/3d/secondary_grid_color" ); |
7043 | int grid_size = EDITOR_GET("editors/3d/grid_size" ); |
7044 | int primary_grid_steps = EDITOR_GET("editors/3d/primary_grid_steps" ); |
7045 | |
7046 | // Which grid planes are enabled? Which should we generate? |
7047 | grid_enable[0] = grid_visible[0] = EDITOR_GET("editors/3d/grid_xy_plane" ); |
7048 | grid_enable[1] = grid_visible[1] = EDITOR_GET("editors/3d/grid_yz_plane" ); |
7049 | grid_enable[2] = grid_visible[2] = EDITOR_GET("editors/3d/grid_xz_plane" ); |
7050 | |
7051 | // Offsets division_level for bigger or smaller grids. |
7052 | // Default value is -0.2. -1.0 gives Blender-like behavior, 0.5 gives huge grids. |
7053 | real_t division_level_bias = EDITOR_GET("editors/3d/grid_division_level_bias" ); |
7054 | // Default largest grid size is 8^2 (default value is 2) when primary_grid_steps is 8 (64m apart, so primary grid lines are 512m apart). |
7055 | int division_level_max = EDITOR_GET("editors/3d/grid_division_level_max" ); |
7056 | // Default smallest grid size is 8^0 (default value is 0) when primary_grid_steps is 8. |
7057 | int division_level_min = EDITOR_GET("editors/3d/grid_division_level_min" ); |
7058 | ERR_FAIL_COND_MSG(division_level_max < division_level_min, "The 3D grid's maximum division level cannot be lower than its minimum division level." ); |
7059 | |
7060 | if (primary_grid_steps != 10) { // Log10 of 10 is 1. |
7061 | // Change of base rule, divide by ln(10). |
7062 | real_t div = Math::log((real_t)primary_grid_steps) / (real_t)2.302585092994045901094; |
7063 | // Trucation (towards zero) is intentional. |
7064 | division_level_max = (int)(division_level_max / div); |
7065 | division_level_min = (int)(division_level_min / div); |
7066 | } |
7067 | |
7068 | for (int a = 0; a < 3; a++) { |
7069 | if (!grid_enable[a]) { |
7070 | continue; // If this grid plane is disabled, skip generation. |
7071 | } |
7072 | int b = (a + 1) % 3; |
7073 | int c = (a + 2) % 3; |
7074 | |
7075 | Vector3 normal; |
7076 | normal[c] = 1.0; |
7077 | |
7078 | real_t camera_distance = Math::abs(camera_position[c]); |
7079 | |
7080 | if (orthogonal) { |
7081 | camera_distance = camera->get_size() / 2.0; |
7082 | Vector3 camera_direction = -camera->get_global_transform().get_basis().get_column(2); |
7083 | Plane grid_plane = Plane(normal); |
7084 | Vector3 intersection; |
7085 | if (grid_plane.intersects_ray(camera_position, camera_direction, &intersection)) { |
7086 | camera_position = intersection; |
7087 | } |
7088 | } |
7089 | |
7090 | real_t division_level = Math::log(Math::abs(camera_distance)) / Math::log((double)primary_grid_steps) + division_level_bias; |
7091 | |
7092 | real_t clamped_division_level = CLAMP(division_level, division_level_min, division_level_max); |
7093 | real_t division_level_floored = Math::floor(clamped_division_level); |
7094 | real_t division_level_decimals = clamped_division_level - division_level_floored; |
7095 | |
7096 | real_t small_step_size = Math::pow(primary_grid_steps, division_level_floored); |
7097 | real_t large_step_size = small_step_size * primary_grid_steps; |
7098 | real_t center_a = large_step_size * (int)(camera_position[a] / large_step_size); |
7099 | real_t center_b = large_step_size * (int)(camera_position[b] / large_step_size); |
7100 | |
7101 | real_t bgn_a = center_a - grid_size * small_step_size; |
7102 | real_t end_a = center_a + grid_size * small_step_size; |
7103 | real_t bgn_b = center_b - grid_size * small_step_size; |
7104 | real_t end_b = center_b + grid_size * small_step_size; |
7105 | |
7106 | real_t fade_size = Math::pow(primary_grid_steps, division_level - 1.0); |
7107 | real_t min_fade_size = Math::pow(primary_grid_steps, float(division_level_min)); |
7108 | real_t max_fade_size = Math::pow(primary_grid_steps, float(division_level_max)); |
7109 | fade_size = CLAMP(fade_size, min_fade_size, max_fade_size); |
7110 | |
7111 | real_t grid_fade_size = (grid_size - primary_grid_steps) * fade_size; |
7112 | grid_mat[c]->set_shader_parameter("grid_size" , grid_fade_size); |
7113 | grid_mat[c]->set_shader_parameter("orthogonal" , orthogonal); |
7114 | |
7115 | // Cache these so we don't have to re-access memory. |
7116 | Vector<Vector3> &ref_grid = grid_points[c]; |
7117 | Vector<Vector3> &ref_grid_normals = grid_normals[c]; |
7118 | Vector<Color> &ref_grid_colors = grid_colors[c]; |
7119 | |
7120 | // Count our elements same as code below it. |
7121 | int expected_size = 0; |
7122 | for (int i = -grid_size; i <= grid_size; i++) { |
7123 | const real_t position_a = center_a + i * small_step_size; |
7124 | const real_t position_b = center_b + i * small_step_size; |
7125 | |
7126 | // Don't draw lines over the origin if it's enabled. |
7127 | if (!(origin_enabled && Math::is_zero_approx(position_a))) { |
7128 | expected_size += 2; |
7129 | } |
7130 | |
7131 | if (!(origin_enabled && Math::is_zero_approx(position_b))) { |
7132 | expected_size += 2; |
7133 | } |
7134 | } |
7135 | |
7136 | int idx = 0; |
7137 | ref_grid.resize(expected_size); |
7138 | ref_grid_normals.resize(expected_size); |
7139 | ref_grid_colors.resize(expected_size); |
7140 | |
7141 | // In each iteration of this loop, draw one line in each direction (so two lines per loop, in each if statement). |
7142 | for (int i = -grid_size; i <= grid_size; i++) { |
7143 | Color line_color; |
7144 | // Is this a primary line? Set the appropriate color. |
7145 | if (i % primary_grid_steps == 0) { |
7146 | line_color = primary_grid_color.lerp(secondary_grid_color, division_level_decimals); |
7147 | } else { |
7148 | line_color = secondary_grid_color; |
7149 | line_color.a = line_color.a * (1 - division_level_decimals); |
7150 | } |
7151 | |
7152 | real_t position_a = center_a + i * small_step_size; |
7153 | real_t position_b = center_b + i * small_step_size; |
7154 | |
7155 | // Don't draw lines over the origin if it's enabled. |
7156 | if (!(origin_enabled && Math::is_zero_approx(position_a))) { |
7157 | Vector3 line_bgn; |
7158 | Vector3 line_end; |
7159 | line_bgn[a] = position_a; |
7160 | line_end[a] = position_a; |
7161 | line_bgn[b] = bgn_b; |
7162 | line_end[b] = end_b; |
7163 | ref_grid.set(idx, line_bgn); |
7164 | ref_grid.set(idx + 1, line_end); |
7165 | ref_grid_colors.set(idx, line_color); |
7166 | ref_grid_colors.set(idx + 1, line_color); |
7167 | ref_grid_normals.set(idx, normal); |
7168 | ref_grid_normals.set(idx + 1, normal); |
7169 | idx += 2; |
7170 | } |
7171 | |
7172 | if (!(origin_enabled && Math::is_zero_approx(position_b))) { |
7173 | Vector3 line_bgn; |
7174 | Vector3 line_end; |
7175 | line_bgn[b] = position_b; |
7176 | line_end[b] = position_b; |
7177 | line_bgn[a] = bgn_a; |
7178 | line_end[a] = end_a; |
7179 | ref_grid.set(idx, line_bgn); |
7180 | ref_grid.set(idx + 1, line_end); |
7181 | ref_grid_colors.set(idx, line_color); |
7182 | ref_grid_colors.set(idx + 1, line_color); |
7183 | ref_grid_normals.set(idx, normal); |
7184 | ref_grid_normals.set(idx + 1, normal); |
7185 | idx += 2; |
7186 | } |
7187 | } |
7188 | |
7189 | // Create a mesh from the pushed vector points and colors. |
7190 | grid[c] = RenderingServer::get_singleton()->mesh_create(); |
7191 | Array d; |
7192 | d.resize(RS::ARRAY_MAX); |
7193 | d[RenderingServer::ARRAY_VERTEX] = grid_points[c]; |
7194 | d[RenderingServer::ARRAY_COLOR] = grid_colors[c]; |
7195 | d[RenderingServer::ARRAY_NORMAL] = grid_normals[c]; |
7196 | RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[c], RenderingServer::PRIMITIVE_LINES, d); |
7197 | RenderingServer::get_singleton()->mesh_surface_set_material(grid[c], 0, grid_mat[c]->get_rid()); |
7198 | grid_instance[c] = RenderingServer::get_singleton()->instance_create2(grid[c], get_tree()->get_root()->get_world_3d()->get_scenario()); |
7199 | |
7200 | // Yes, the end of this line is supposed to be a. |
7201 | RenderingServer::get_singleton()->instance_set_visible(grid_instance[c], grid_visible[a]); |
7202 | RenderingServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[c], RS::SHADOW_CASTING_SETTING_OFF); |
7203 | RS::get_singleton()->instance_set_layer_mask(grid_instance[c], 1 << Node3DEditorViewport::GIZMO_GRID_LAYER); |
7204 | RS::get_singleton()->instance_geometry_set_flag(grid_instance[c], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
7205 | RS::get_singleton()->instance_geometry_set_flag(grid_instance[c], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
7206 | } |
7207 | } |
7208 | |
7209 | void Node3DEditor::_finish_indicators() { |
7210 | RenderingServer::get_singleton()->free(origin_instance); |
7211 | RenderingServer::get_singleton()->free(origin); |
7212 | |
7213 | _finish_grid(); |
7214 | } |
7215 | |
7216 | void Node3DEditor::_finish_grid() { |
7217 | for (int i = 0; i < 3; i++) { |
7218 | RenderingServer::get_singleton()->free(grid_instance[i]); |
7219 | RenderingServer::get_singleton()->free(grid[i]); |
7220 | } |
7221 | } |
7222 | |
7223 | void Node3DEditor::update_grid() { |
7224 | const Camera3D::ProjectionType current_projection = viewports[0]->camera->get_projection(); |
7225 | |
7226 | if (current_projection != grid_camera_last_update_perspective) { |
7227 | grid_init_draw = false; // redraw |
7228 | grid_camera_last_update_perspective = current_projection; |
7229 | } |
7230 | |
7231 | // Gets a orthogonal or perspective position correctly (for the grid comparison) |
7232 | const Vector3 camera_position = get_editor_viewport(0)->camera->get_position(); |
7233 | |
7234 | if (!grid_init_draw || grid_camera_last_update_position.distance_squared_to(camera_position) >= 100.0f) { |
7235 | _finish_grid(); |
7236 | _init_grid(); |
7237 | grid_init_draw = true; |
7238 | grid_camera_last_update_position = camera_position; |
7239 | } |
7240 | } |
7241 | |
7242 | void Node3DEditor::_selection_changed() { |
7243 | _refresh_menu_icons(); |
7244 | if (selected && editor_selection->get_selected_node_list().size() != 1) { |
7245 | Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos(); |
7246 | for (int i = 0; i < gizmos.size(); i++) { |
7247 | Ref<EditorNode3DGizmo> seg = gizmos[i]; |
7248 | if (!seg.is_valid()) { |
7249 | continue; |
7250 | } |
7251 | seg->set_selected(false); |
7252 | } |
7253 | |
7254 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected); |
7255 | if (se) { |
7256 | se->gizmo.unref(); |
7257 | se->subgizmos.clear(); |
7258 | } |
7259 | selected->update_gizmos(); |
7260 | selected = nullptr; |
7261 | } |
7262 | update_transform_gizmo(); |
7263 | } |
7264 | |
7265 | void Node3DEditor::() { |
7266 | bool all_locked = true; |
7267 | bool all_grouped = true; |
7268 | |
7269 | List<Node *> &selection = editor_selection->get_selected_node_list(); |
7270 | |
7271 | if (selection.is_empty()) { |
7272 | all_locked = false; |
7273 | all_grouped = false; |
7274 | } else { |
7275 | for (Node *E : selection) { |
7276 | if (Object::cast_to<Node3D>(E) && !Object::cast_to<Node3D>(E)->has_meta("_edit_lock_" )) { |
7277 | all_locked = false; |
7278 | break; |
7279 | } |
7280 | } |
7281 | for (Node *E : selection) { |
7282 | if (Object::cast_to<Node3D>(E) && !Object::cast_to<Node3D>(E)->has_meta("_edit_group_" )) { |
7283 | all_grouped = false; |
7284 | break; |
7285 | } |
7286 | } |
7287 | } |
7288 | |
7289 | tool_button[TOOL_LOCK_SELECTED]->set_visible(!all_locked); |
7290 | tool_button[TOOL_LOCK_SELECTED]->set_disabled(selection.is_empty()); |
7291 | tool_button[TOOL_UNLOCK_SELECTED]->set_visible(all_locked); |
7292 | |
7293 | tool_button[TOOL_GROUP_SELECTED]->set_visible(!all_grouped); |
7294 | tool_button[TOOL_GROUP_SELECTED]->set_disabled(selection.is_empty()); |
7295 | tool_button[TOOL_UNGROUP_SELECTED]->set_visible(all_grouped); |
7296 | } |
7297 | |
7298 | template <typename T> |
7299 | HashSet<T *> _get_child_nodes(Node *parent_node) { |
7300 | HashSet<T *> nodes = HashSet<T *>(); |
7301 | T *node = Node::cast_to<T>(parent_node); |
7302 | if (node) { |
7303 | nodes.insert(node); |
7304 | } |
7305 | |
7306 | for (int i = 0; i < parent_node->get_child_count(); i++) { |
7307 | Node *child_node = parent_node->get_child(i); |
7308 | HashSet<T *> child_nodes = _get_child_nodes<T>(child_node); |
7309 | for (T *I : child_nodes) { |
7310 | nodes.insert(I); |
7311 | } |
7312 | } |
7313 | |
7314 | return nodes; |
7315 | } |
7316 | |
7317 | HashSet<RID> _get_physics_bodies_rid(Node *node) { |
7318 | HashSet<RID> rids = HashSet<RID>(); |
7319 | PhysicsBody3D *pb = Node::cast_to<PhysicsBody3D>(node); |
7320 | if (pb) { |
7321 | rids.insert(pb->get_rid()); |
7322 | } |
7323 | HashSet<PhysicsBody3D *> child_nodes = _get_child_nodes<PhysicsBody3D>(node); |
7324 | for (const PhysicsBody3D *I : child_nodes) { |
7325 | rids.insert(I->get_rid()); |
7326 | } |
7327 | |
7328 | return rids; |
7329 | } |
7330 | |
7331 | void Node3DEditor::snap_selected_nodes_to_floor() { |
7332 | do_snap_selected_nodes_to_floor = true; |
7333 | } |
7334 | |
7335 | void Node3DEditor::_snap_selected_nodes_to_floor() { |
7336 | const List<Node *> &selection = editor_selection->get_selected_node_list(); |
7337 | Dictionary snap_data; |
7338 | |
7339 | for (Node *E : selection) { |
7340 | Node3D *sp = Object::cast_to<Node3D>(E); |
7341 | if (sp) { |
7342 | Vector3 from; |
7343 | Vector3 position_offset; |
7344 | |
7345 | // Priorities for snapping to floor are CollisionShapes, VisualInstances and then origin |
7346 | HashSet<VisualInstance3D *> vi = _get_child_nodes<VisualInstance3D>(sp); |
7347 | HashSet<CollisionShape3D *> cs = _get_child_nodes<CollisionShape3D>(sp); |
7348 | bool found_valid_shape = false; |
7349 | |
7350 | if (cs.size()) { |
7351 | AABB aabb; |
7352 | HashSet<CollisionShape3D *>::Iterator I = cs.begin(); |
7353 | if ((*I)->get_shape().is_valid()) { |
7354 | CollisionShape3D *collision_shape = *cs.begin(); |
7355 | aabb = collision_shape->get_global_transform().xform(collision_shape->get_shape()->get_debug_mesh()->get_aabb()); |
7356 | found_valid_shape = true; |
7357 | } |
7358 | |
7359 | for (++I; I; ++I) { |
7360 | CollisionShape3D *col_shape = *I; |
7361 | if (col_shape->get_shape().is_valid()) { |
7362 | aabb.merge_with(col_shape->get_global_transform().xform(col_shape->get_shape()->get_debug_mesh()->get_aabb())); |
7363 | found_valid_shape = true; |
7364 | } |
7365 | } |
7366 | if (found_valid_shape) { |
7367 | Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5); |
7368 | from = aabb.position + size; |
7369 | position_offset.y = from.y - sp->get_global_transform().origin.y; |
7370 | } |
7371 | } |
7372 | if (!found_valid_shape && vi.size()) { |
7373 | VisualInstance3D *begin = *vi.begin(); |
7374 | AABB aabb = begin->get_global_transform().xform(begin->get_aabb()); |
7375 | for (const VisualInstance3D *I : vi) { |
7376 | aabb.merge_with(I->get_global_transform().xform(I->get_aabb())); |
7377 | } |
7378 | Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5); |
7379 | from = aabb.position + size; |
7380 | position_offset.y = from.y - sp->get_global_transform().origin.y; |
7381 | } else if (!found_valid_shape) { |
7382 | from = sp->get_global_transform().origin; |
7383 | } |
7384 | |
7385 | // We add a bit of margin to the from position to avoid it from snapping |
7386 | // when the spatial is already on a floor and there's another floor under |
7387 | // it |
7388 | from = from + Vector3(0.0, 1, 0.0); |
7389 | |
7390 | Dictionary d; |
7391 | |
7392 | d["from" ] = from; |
7393 | d["position_offset" ] = position_offset; |
7394 | snap_data[sp] = d; |
7395 | } |
7396 | } |
7397 | |
7398 | PhysicsDirectSpaceState3D *ss = get_tree()->get_root()->get_world_3d()->get_direct_space_state(); |
7399 | PhysicsDirectSpaceState3D::RayResult result; |
7400 | |
7401 | Array keys = snap_data.keys(); |
7402 | |
7403 | // The maximum height an object can travel to be snapped |
7404 | const float max_snap_height = 500.0; |
7405 | |
7406 | // Will be set to `true` if at least one node from the selection was successfully snapped |
7407 | bool snapped_to_floor = false; |
7408 | |
7409 | if (keys.size()) { |
7410 | // For snapping to be performed, there must be solid geometry under at least one of the selected nodes. |
7411 | // We need to check this before snapping to register the undo/redo action only if needed. |
7412 | for (int i = 0; i < keys.size(); i++) { |
7413 | Node *node = Object::cast_to<Node>(keys[i]); |
7414 | Node3D *sp = Object::cast_to<Node3D>(node); |
7415 | Dictionary d = snap_data[node]; |
7416 | Vector3 from = d["from" ]; |
7417 | Vector3 to = from - Vector3(0.0, max_snap_height, 0.0); |
7418 | HashSet<RID> excluded = _get_physics_bodies_rid(sp); |
7419 | |
7420 | PhysicsDirectSpaceState3D::RayParameters ray_params; |
7421 | ray_params.from = from; |
7422 | ray_params.to = to; |
7423 | ray_params.exclude = excluded; |
7424 | |
7425 | if (ss->intersect_ray(ray_params, result)) { |
7426 | snapped_to_floor = true; |
7427 | } |
7428 | } |
7429 | |
7430 | if (snapped_to_floor) { |
7431 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
7432 | undo_redo->create_action(TTR("Snap Nodes to Floor" )); |
7433 | |
7434 | // Perform snapping if at least one node can be snapped |
7435 | for (int i = 0; i < keys.size(); i++) { |
7436 | Node *node = Object::cast_to<Node>(keys[i]); |
7437 | Node3D *sp = Object::cast_to<Node3D>(node); |
7438 | Dictionary d = snap_data[node]; |
7439 | Vector3 from = d["from" ]; |
7440 | Vector3 to = from - Vector3(0.0, max_snap_height, 0.0); |
7441 | HashSet<RID> excluded = _get_physics_bodies_rid(sp); |
7442 | |
7443 | PhysicsDirectSpaceState3D::RayParameters ray_params; |
7444 | ray_params.from = from; |
7445 | ray_params.to = to; |
7446 | ray_params.exclude = excluded; |
7447 | |
7448 | if (ss->intersect_ray(ray_params, result)) { |
7449 | Vector3 position_offset = d["position_offset" ]; |
7450 | Transform3D new_transform = sp->get_global_transform(); |
7451 | |
7452 | new_transform.origin.y = result.position.y; |
7453 | new_transform.origin = new_transform.origin - position_offset; |
7454 | |
7455 | undo_redo->add_do_method(sp, "set_global_transform" , new_transform); |
7456 | undo_redo->add_undo_method(sp, "set_global_transform" , sp->get_global_transform()); |
7457 | } |
7458 | } |
7459 | |
7460 | undo_redo->commit_action(); |
7461 | } else { |
7462 | EditorNode::get_singleton()->show_warning(TTR("Couldn't find a solid floor to snap the selection to." )); |
7463 | } |
7464 | } |
7465 | } |
7466 | |
7467 | void Node3DEditor::shortcut_input(const Ref<InputEvent> &p_event) { |
7468 | ERR_FAIL_COND(p_event.is_null()); |
7469 | |
7470 | if (!is_visible_in_tree()) { |
7471 | return; |
7472 | } |
7473 | |
7474 | snap_key_enabled = Input::get_singleton()->is_key_pressed(Key::CTRL); |
7475 | } |
7476 | |
7477 | void Node3DEditor::_sun_environ_settings_pressed() { |
7478 | Vector2 pos = sun_environ_settings->get_screen_position() + sun_environ_settings->get_size(); |
7479 | sun_environ_popup->set_position(pos - Vector2(sun_environ_popup->get_contents_minimum_size().width / 2, 0)); |
7480 | sun_environ_popup->reset_size(); |
7481 | sun_environ_popup->popup(); |
7482 | } |
7483 | |
7484 | void Node3DEditor::_add_sun_to_scene(bool p_already_added_environment) { |
7485 | sun_environ_popup->hide(); |
7486 | |
7487 | if (!p_already_added_environment && world_env_count == 0 && Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
7488 | // Prevent infinite feedback loop between the sun and environment methods. |
7489 | _add_environment_to_scene(true); |
7490 | } |
7491 | |
7492 | Node *base = get_tree()->get_edited_scene_root(); |
7493 | if (!base) { |
7494 | // Create a root node so we can add child nodes to it. |
7495 | SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D)); |
7496 | base = get_tree()->get_edited_scene_root(); |
7497 | } |
7498 | ERR_FAIL_NULL(base); |
7499 | Node *new_sun = preview_sun->duplicate(); |
7500 | |
7501 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
7502 | undo_redo->create_action(TTR("Add Preview Sun to Scene" )); |
7503 | undo_redo->add_do_method(base, "add_child" , new_sun, true); |
7504 | // Move to the beginning of the scene tree since more "global" nodes |
7505 | // generally look better when placed at the top. |
7506 | undo_redo->add_do_method(base, "move_child" , new_sun, 0); |
7507 | undo_redo->add_do_method(new_sun, "set_owner" , base); |
7508 | undo_redo->add_undo_method(base, "remove_child" , new_sun); |
7509 | undo_redo->add_do_reference(new_sun); |
7510 | undo_redo->commit_action(); |
7511 | } |
7512 | |
7513 | void Node3DEditor::_add_environment_to_scene(bool p_already_added_sun) { |
7514 | sun_environ_popup->hide(); |
7515 | |
7516 | if (!p_already_added_sun && directional_light_count == 0 && Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
7517 | // Prevent infinite feedback loop between the sun and environment methods. |
7518 | _add_sun_to_scene(true); |
7519 | } |
7520 | |
7521 | Node *base = get_tree()->get_edited_scene_root(); |
7522 | if (!base) { |
7523 | // Create a root node so we can add child nodes to it. |
7524 | SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D)); |
7525 | base = get_tree()->get_edited_scene_root(); |
7526 | } |
7527 | ERR_FAIL_NULL(base); |
7528 | |
7529 | WorldEnvironment *new_env = memnew(WorldEnvironment); |
7530 | new_env->set_environment(preview_environment->get_environment()->duplicate(true)); |
7531 | if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units" )) { |
7532 | new_env->set_camera_attributes(preview_environment->get_camera_attributes()->duplicate(true)); |
7533 | } |
7534 | |
7535 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
7536 | undo_redo->create_action(TTR("Add Preview Environment to Scene" )); |
7537 | undo_redo->add_do_method(base, "add_child" , new_env, true); |
7538 | // Move to the beginning of the scene tree since more "global" nodes |
7539 | // generally look better when placed at the top. |
7540 | undo_redo->add_do_method(base, "move_child" , new_env, 0); |
7541 | undo_redo->add_do_method(new_env, "set_owner" , base); |
7542 | undo_redo->add_undo_method(base, "remove_child" , new_env); |
7543 | undo_redo->add_do_reference(new_env); |
7544 | undo_redo->commit_action(); |
7545 | } |
7546 | |
7547 | void Node3DEditor::_update_theme() { |
7548 | tool_button[TOOL_MODE_SELECT]->set_icon(get_editor_theme_icon(SNAME("ToolSelect" ))); |
7549 | tool_button[TOOL_MODE_MOVE]->set_icon(get_editor_theme_icon(SNAME("ToolMove" ))); |
7550 | tool_button[TOOL_MODE_ROTATE]->set_icon(get_editor_theme_icon(SNAME("ToolRotate" ))); |
7551 | tool_button[TOOL_MODE_SCALE]->set_icon(get_editor_theme_icon(SNAME("ToolScale" ))); |
7552 | tool_button[TOOL_MODE_LIST_SELECT]->set_icon(get_editor_theme_icon(SNAME("ListSelect" ))); |
7553 | tool_button[TOOL_LOCK_SELECTED]->set_icon(get_editor_theme_icon(SNAME("Lock" ))); |
7554 | tool_button[TOOL_UNLOCK_SELECTED]->set_icon(get_editor_theme_icon(SNAME("Unlock" ))); |
7555 | tool_button[TOOL_GROUP_SELECTED]->set_icon(get_editor_theme_icon(SNAME("Group" ))); |
7556 | tool_button[TOOL_UNGROUP_SELECTED]->set_icon(get_editor_theme_icon(SNAME("Ungroup" ))); |
7557 | |
7558 | tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_icon(get_editor_theme_icon(SNAME("Object" ))); |
7559 | tool_option_button[TOOL_OPT_USE_SNAP]->set_icon(get_editor_theme_icon(SNAME("Snap" ))); |
7560 | tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_icon(get_editor_theme_icon(SNAME("Camera3D" ))); |
7561 | |
7562 | view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_editor_theme_icon(SNAME("Panels1" ))); |
7563 | view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_editor_theme_icon(SNAME("Panels2" ))); |
7564 | view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_editor_theme_icon(SNAME("Panels2Alt" ))); |
7565 | view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_editor_theme_icon(SNAME("Panels3" ))); |
7566 | view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_editor_theme_icon(SNAME("Panels3Alt" ))); |
7567 | view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_editor_theme_icon(SNAME("Panels4" ))); |
7568 | |
7569 | sun_button->set_icon(get_editor_theme_icon(SNAME("PreviewSun" ))); |
7570 | environ_button->set_icon(get_editor_theme_icon(SNAME("PreviewEnvironment" ))); |
7571 | sun_environ_settings->set_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl" ))); |
7572 | |
7573 | sun_title->add_theme_font_override("font" , get_theme_font(SNAME("title_font" ), SNAME("Window" ))); |
7574 | environ_title->add_theme_font_override("font" , get_theme_font(SNAME("title_font" ), SNAME("Window" ))); |
7575 | |
7576 | sun_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height" ), EditorStringName(Editor)))); |
7577 | environ_sky_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height" ), EditorStringName(Editor)))); |
7578 | environ_ground_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height" ), EditorStringName(Editor)))); |
7579 | |
7580 | context_toolbar_panel->add_theme_style_override("panel" , get_theme_stylebox(SNAME("ContextualToolbar" ), EditorStringName(EditorStyles))); |
7581 | } |
7582 | |
7583 | void Node3DEditor::_notification(int p_what) { |
7584 | switch (p_what) { |
7585 | case NOTIFICATION_READY: { |
7586 | _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT); |
7587 | |
7588 | _refresh_menu_icons(); |
7589 | |
7590 | get_tree()->connect("node_removed" , callable_mp(this, &Node3DEditor::_node_removed)); |
7591 | get_tree()->connect("node_added" , callable_mp(this, &Node3DEditor::_node_added)); |
7592 | SceneTreeDock::get_singleton()->get_tree_editor()->connect("node_changed" , callable_mp(this, &Node3DEditor::_refresh_menu_icons)); |
7593 | editor_selection->connect("selection_changed" , callable_mp(this, &Node3DEditor::_selection_changed)); |
7594 | |
7595 | EditorRunBar::get_singleton()->connect("stop_pressed" , callable_mp(this, &Node3DEditor::_update_camera_override_button).bind(false)); |
7596 | EditorRunBar::get_singleton()->connect("play_pressed" , callable_mp(this, &Node3DEditor::_update_camera_override_button).bind(true)); |
7597 | |
7598 | _update_preview_environment(); |
7599 | |
7600 | sun_state->set_custom_minimum_size(sun_vb->get_combined_minimum_size()); |
7601 | environ_state->set_custom_minimum_size(environ_vb->get_combined_minimum_size()); |
7602 | |
7603 | ProjectSettings::get_singleton()->connect("settings_changed" , callable_mp(this, &Node3DEditor::update_all_gizmos).bind(Variant())); |
7604 | } break; |
7605 | |
7606 | case NOTIFICATION_ENTER_TREE: { |
7607 | _update_theme(); |
7608 | _register_all_gizmos(); |
7609 | _update_gizmos_menu(); |
7610 | _init_indicators(); |
7611 | update_all_gizmos(); |
7612 | } break; |
7613 | |
7614 | case NOTIFICATION_EXIT_TREE: { |
7615 | _finish_indicators(); |
7616 | } break; |
7617 | |
7618 | case NOTIFICATION_THEME_CHANGED: { |
7619 | _update_theme(); |
7620 | _update_gizmos_menu_theme(); |
7621 | sun_title->add_theme_font_override("font" , get_theme_font(SNAME("title_font" ), SNAME("Window" ))); |
7622 | environ_title->add_theme_font_override("font" , get_theme_font(SNAME("title_font" ), SNAME("Window" ))); |
7623 | } break; |
7624 | |
7625 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
7626 | // Update grid color by rebuilding grid. |
7627 | _finish_grid(); |
7628 | _init_grid(); |
7629 | } break; |
7630 | |
7631 | case NOTIFICATION_VISIBILITY_CHANGED: { |
7632 | if (!is_visible() && tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->is_pressed()) { |
7633 | EditorDebuggerNode *debugger = EditorDebuggerNode::get_singleton(); |
7634 | |
7635 | debugger->set_camera_override(EditorDebuggerNode::OVERRIDE_NONE); |
7636 | tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_pressed(false); |
7637 | } |
7638 | } break; |
7639 | |
7640 | case NOTIFICATION_PHYSICS_PROCESS: { |
7641 | if (do_snap_selected_nodes_to_floor) { |
7642 | _snap_selected_nodes_to_floor(); |
7643 | do_snap_selected_nodes_to_floor = false; |
7644 | } |
7645 | } |
7646 | } |
7647 | } |
7648 | |
7649 | bool Node3DEditor::is_subgizmo_selected(int p_id) { |
7650 | Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr; |
7651 | if (se) { |
7652 | return se->subgizmos.has(p_id); |
7653 | } |
7654 | return false; |
7655 | } |
7656 | |
7657 | bool Node3DEditor::is_current_selected_gizmo(const EditorNode3DGizmo *p_gizmo) { |
7658 | Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr; |
7659 | if (se) { |
7660 | return se->gizmo == p_gizmo; |
7661 | } |
7662 | return false; |
7663 | } |
7664 | |
7665 | Vector<int> Node3DEditor::get_subgizmo_selection() { |
7666 | Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr; |
7667 | |
7668 | Vector<int> ret; |
7669 | if (se) { |
7670 | for (const KeyValue<int, Transform3D> &E : se->subgizmos) { |
7671 | ret.push_back(E.key); |
7672 | } |
7673 | } |
7674 | return ret; |
7675 | } |
7676 | |
7677 | void Node3DEditor::(Control *p_control) { |
7678 | ERR_FAIL_NULL(p_control); |
7679 | ERR_FAIL_COND(p_control->get_parent()); |
7680 | |
7681 | VSeparator *sep = memnew(VSeparator); |
7682 | context_toolbar_hbox->add_child(sep); |
7683 | context_toolbar_hbox->add_child(p_control); |
7684 | context_toolbar_separators[p_control] = sep; |
7685 | |
7686 | p_control->connect("visibility_changed" , callable_mp(this, &Node3DEditor::_update_context_toolbar)); |
7687 | |
7688 | _update_context_toolbar(); |
7689 | } |
7690 | |
7691 | void Node3DEditor::(Control *p_control) { |
7692 | ERR_FAIL_NULL(p_control); |
7693 | ERR_FAIL_COND(p_control->get_parent() != context_toolbar_hbox); |
7694 | |
7695 | p_control->disconnect("visibility_changed" , callable_mp(this, &Node3DEditor::_update_context_toolbar)); |
7696 | |
7697 | context_toolbar_hbox->remove_child(context_toolbar_separators[p_control]); |
7698 | context_toolbar_hbox->remove_child(p_control); |
7699 | context_toolbar_separators.erase(p_control); |
7700 | |
7701 | _update_context_toolbar(); |
7702 | } |
7703 | |
7704 | void Node3DEditor::_update_context_toolbar() { |
7705 | bool has_visible = false; |
7706 | bool first_visible = false; |
7707 | |
7708 | for (int i = 0; i < context_toolbar_hbox->get_child_count(); i++) { |
7709 | Control *child = Object::cast_to<Control>(context_toolbar_hbox->get_child(i)); |
7710 | if (!child || !context_toolbar_separators.has(child)) { |
7711 | continue; |
7712 | } |
7713 | if (child->is_visible()) { |
7714 | first_visible = !has_visible; |
7715 | has_visible = true; |
7716 | } |
7717 | |
7718 | VSeparator *sep = context_toolbar_separators[child]; |
7719 | sep->set_visible(!first_visible && child->is_visible()); |
7720 | } |
7721 | |
7722 | context_toolbar_panel->set_visible(has_visible); |
7723 | } |
7724 | |
7725 | void Node3DEditor::set_can_preview(Camera3D *p_preview) { |
7726 | for (int i = 0; i < 4; i++) { |
7727 | viewports[i]->set_can_preview(p_preview); |
7728 | } |
7729 | } |
7730 | |
7731 | VSplitContainer *Node3DEditor::get_shader_split() { |
7732 | return shader_split; |
7733 | } |
7734 | |
7735 | void Node3DEditor::add_control_to_left_panel(Control *p_control) { |
7736 | left_panel_split->add_child(p_control); |
7737 | left_panel_split->move_child(p_control, 0); |
7738 | } |
7739 | |
7740 | void Node3DEditor::add_control_to_right_panel(Control *p_control) { |
7741 | right_panel_split->add_child(p_control); |
7742 | right_panel_split->move_child(p_control, 1); |
7743 | } |
7744 | |
7745 | void Node3DEditor::remove_control_from_left_panel(Control *p_control) { |
7746 | left_panel_split->remove_child(p_control); |
7747 | } |
7748 | |
7749 | void Node3DEditor::remove_control_from_right_panel(Control *p_control) { |
7750 | right_panel_split->remove_child(p_control); |
7751 | } |
7752 | |
7753 | void Node3DEditor::move_control_to_left_panel(Control *p_control) { |
7754 | ERR_FAIL_NULL(p_control); |
7755 | if (p_control->get_parent() == left_panel_split) { |
7756 | return; |
7757 | } |
7758 | |
7759 | ERR_FAIL_COND(p_control->get_parent() != right_panel_split); |
7760 | right_panel_split->remove_child(p_control); |
7761 | |
7762 | add_control_to_left_panel(p_control); |
7763 | } |
7764 | |
7765 | void Node3DEditor::move_control_to_right_panel(Control *p_control) { |
7766 | ERR_FAIL_NULL(p_control); |
7767 | if (p_control->get_parent() == right_panel_split) { |
7768 | return; |
7769 | } |
7770 | |
7771 | ERR_FAIL_COND(p_control->get_parent() != left_panel_split); |
7772 | left_panel_split->remove_child(p_control); |
7773 | |
7774 | add_control_to_right_panel(p_control); |
7775 | } |
7776 | |
7777 | void Node3DEditor::_request_gizmo(Object *p_obj) { |
7778 | Node3D *sp = Object::cast_to<Node3D>(p_obj); |
7779 | if (!sp) { |
7780 | return; |
7781 | } |
7782 | |
7783 | bool is_selected = (sp == selected); |
7784 | |
7785 | Node *edited_scene = EditorNode::get_singleton()->get_edited_scene(); |
7786 | if (edited_scene && (sp == edited_scene || (sp->get_owner() && edited_scene->is_ancestor_of(sp)))) { |
7787 | for (int i = 0; i < gizmo_plugins_by_priority.size(); ++i) { |
7788 | Ref<EditorNode3DGizmo> seg = gizmo_plugins_by_priority.write[i]->get_gizmo(sp); |
7789 | |
7790 | if (seg.is_valid()) { |
7791 | sp->add_gizmo(seg); |
7792 | |
7793 | if (is_selected != seg->is_selected()) { |
7794 | seg->set_selected(is_selected); |
7795 | } |
7796 | } |
7797 | } |
7798 | if (!sp->get_gizmos().is_empty()) { |
7799 | sp->update_gizmos(); |
7800 | } |
7801 | } |
7802 | } |
7803 | |
7804 | void Node3DEditor::_request_gizmo_for_id(ObjectID p_id) { |
7805 | Node3D *node = Object::cast_to<Node3D>(ObjectDB::get_instance(p_id)); |
7806 | if (node) { |
7807 | _request_gizmo(node); |
7808 | } |
7809 | } |
7810 | |
7811 | void Node3DEditor::_set_subgizmo_selection(Object *p_obj, Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform) { |
7812 | if (p_id == -1) { |
7813 | _clear_subgizmo_selection(p_obj); |
7814 | return; |
7815 | } |
7816 | |
7817 | Node3D *sp = nullptr; |
7818 | if (p_obj) { |
7819 | sp = Object::cast_to<Node3D>(p_obj); |
7820 | } else { |
7821 | sp = selected; |
7822 | } |
7823 | |
7824 | if (!sp) { |
7825 | return; |
7826 | } |
7827 | |
7828 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
7829 | if (se) { |
7830 | se->subgizmos.clear(); |
7831 | se->subgizmos.insert(p_id, p_transform); |
7832 | se->gizmo = p_gizmo; |
7833 | sp->update_gizmos(); |
7834 | update_transform_gizmo(); |
7835 | } |
7836 | } |
7837 | |
7838 | void Node3DEditor::_clear_subgizmo_selection(Object *p_obj) { |
7839 | Node3D *sp = nullptr; |
7840 | if (p_obj) { |
7841 | sp = Object::cast_to<Node3D>(p_obj); |
7842 | } else { |
7843 | sp = selected; |
7844 | } |
7845 | |
7846 | if (!sp) { |
7847 | return; |
7848 | } |
7849 | |
7850 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp); |
7851 | if (se) { |
7852 | se->subgizmos.clear(); |
7853 | se->gizmo.unref(); |
7854 | sp->update_gizmos(); |
7855 | update_transform_gizmo(); |
7856 | } |
7857 | } |
7858 | |
7859 | void Node3DEditor::_toggle_maximize_view(Object *p_viewport) { |
7860 | if (!p_viewport) { |
7861 | return; |
7862 | } |
7863 | Node3DEditorViewport *current_viewport = Object::cast_to<Node3DEditorViewport>(p_viewport); |
7864 | if (!current_viewport) { |
7865 | return; |
7866 | } |
7867 | |
7868 | int index = -1; |
7869 | bool maximized = false; |
7870 | for (int i = 0; i < 4; i++) { |
7871 | if (viewports[i] == current_viewport) { |
7872 | index = i; |
7873 | if (current_viewport->get_global_rect() == viewport_base->get_global_rect()) { |
7874 | maximized = true; |
7875 | } |
7876 | break; |
7877 | } |
7878 | } |
7879 | if (index == -1) { |
7880 | return; |
7881 | } |
7882 | |
7883 | if (!maximized) { |
7884 | for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) { |
7885 | if (i == (uint32_t)index) { |
7886 | viewports[i]->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
7887 | } else { |
7888 | viewports[i]->hide(); |
7889 | } |
7890 | } |
7891 | } else { |
7892 | for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) { |
7893 | viewports[i]->show(); |
7894 | } |
7895 | |
7896 | if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT))) { |
7897 | _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT); |
7898 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS))) { |
7899 | _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS); |
7900 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT))) { |
7901 | _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT); |
7902 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS))) { |
7903 | _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS); |
7904 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT))) { |
7905 | _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT); |
7906 | } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS))) { |
7907 | _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS); |
7908 | } |
7909 | } |
7910 | } |
7911 | |
7912 | void Node3DEditor::_node_added(Node *p_node) { |
7913 | if (EditorNode::get_singleton()->get_scene_root()->is_ancestor_of(p_node)) { |
7914 | if (Object::cast_to<WorldEnvironment>(p_node)) { |
7915 | world_env_count++; |
7916 | if (world_env_count == 1) { |
7917 | _update_preview_environment(); |
7918 | } |
7919 | } else if (Object::cast_to<DirectionalLight3D>(p_node)) { |
7920 | directional_light_count++; |
7921 | if (directional_light_count == 1) { |
7922 | _update_preview_environment(); |
7923 | } |
7924 | } |
7925 | } |
7926 | } |
7927 | |
7928 | void Node3DEditor::_node_removed(Node *p_node) { |
7929 | if (EditorNode::get_singleton()->get_scene_root()->is_ancestor_of(p_node)) { |
7930 | if (Object::cast_to<WorldEnvironment>(p_node)) { |
7931 | world_env_count--; |
7932 | if (world_env_count == 0) { |
7933 | _update_preview_environment(); |
7934 | } |
7935 | } else if (Object::cast_to<DirectionalLight3D>(p_node)) { |
7936 | directional_light_count--; |
7937 | if (directional_light_count == 0) { |
7938 | _update_preview_environment(); |
7939 | } |
7940 | } |
7941 | } |
7942 | |
7943 | if (p_node == selected) { |
7944 | Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected); |
7945 | if (se) { |
7946 | se->gizmo.unref(); |
7947 | se->subgizmos.clear(); |
7948 | } |
7949 | selected = nullptr; |
7950 | update_transform_gizmo(); |
7951 | } |
7952 | } |
7953 | |
7954 | void Node3DEditor::_register_all_gizmos() { |
7955 | add_gizmo_plugin(Ref<Camera3DGizmoPlugin>(memnew(Camera3DGizmoPlugin))); |
7956 | add_gizmo_plugin(Ref<Light3DGizmoPlugin>(memnew(Light3DGizmoPlugin))); |
7957 | add_gizmo_plugin(Ref<AudioStreamPlayer3DGizmoPlugin>(memnew(AudioStreamPlayer3DGizmoPlugin))); |
7958 | add_gizmo_plugin(Ref<AudioListener3DGizmoPlugin>(memnew(AudioListener3DGizmoPlugin))); |
7959 | add_gizmo_plugin(Ref<MeshInstance3DGizmoPlugin>(memnew(MeshInstance3DGizmoPlugin))); |
7960 | add_gizmo_plugin(Ref<OccluderInstance3DGizmoPlugin>(memnew(OccluderInstance3DGizmoPlugin))); |
7961 | add_gizmo_plugin(Ref<SoftBody3DGizmoPlugin>(memnew(SoftBody3DGizmoPlugin))); |
7962 | add_gizmo_plugin(Ref<Sprite3DGizmoPlugin>(memnew(Sprite3DGizmoPlugin))); |
7963 | add_gizmo_plugin(Ref<Label3DGizmoPlugin>(memnew(Label3DGizmoPlugin))); |
7964 | add_gizmo_plugin(Ref<Marker3DGizmoPlugin>(memnew(Marker3DGizmoPlugin))); |
7965 | add_gizmo_plugin(Ref<RayCast3DGizmoPlugin>(memnew(RayCast3DGizmoPlugin))); |
7966 | add_gizmo_plugin(Ref<ShapeCast3DGizmoPlugin>(memnew(ShapeCast3DGizmoPlugin))); |
7967 | add_gizmo_plugin(Ref<SpringArm3DGizmoPlugin>(memnew(SpringArm3DGizmoPlugin))); |
7968 | add_gizmo_plugin(Ref<VehicleWheel3DGizmoPlugin>(memnew(VehicleWheel3DGizmoPlugin))); |
7969 | add_gizmo_plugin(Ref<VisibleOnScreenNotifier3DGizmoPlugin>(memnew(VisibleOnScreenNotifier3DGizmoPlugin))); |
7970 | add_gizmo_plugin(Ref<GPUParticles3DGizmoPlugin>(memnew(GPUParticles3DGizmoPlugin))); |
7971 | add_gizmo_plugin(Ref<GPUParticlesCollision3DGizmoPlugin>(memnew(GPUParticlesCollision3DGizmoPlugin))); |
7972 | add_gizmo_plugin(Ref<CPUParticles3DGizmoPlugin>(memnew(CPUParticles3DGizmoPlugin))); |
7973 | add_gizmo_plugin(Ref<ReflectionProbeGizmoPlugin>(memnew(ReflectionProbeGizmoPlugin))); |
7974 | add_gizmo_plugin(Ref<DecalGizmoPlugin>(memnew(DecalGizmoPlugin))); |
7975 | add_gizmo_plugin(Ref<VoxelGIGizmoPlugin>(memnew(VoxelGIGizmoPlugin))); |
7976 | add_gizmo_plugin(Ref<LightmapGIGizmoPlugin>(memnew(LightmapGIGizmoPlugin))); |
7977 | add_gizmo_plugin(Ref<LightmapProbeGizmoPlugin>(memnew(LightmapProbeGizmoPlugin))); |
7978 | add_gizmo_plugin(Ref<CollisionObject3DGizmoPlugin>(memnew(CollisionObject3DGizmoPlugin))); |
7979 | add_gizmo_plugin(Ref<CollisionShape3DGizmoPlugin>(memnew(CollisionShape3DGizmoPlugin))); |
7980 | add_gizmo_plugin(Ref<CollisionPolygon3DGizmoPlugin>(memnew(CollisionPolygon3DGizmoPlugin))); |
7981 | add_gizmo_plugin(Ref<NavigationLink3DGizmoPlugin>(memnew(NavigationLink3DGizmoPlugin))); |
7982 | add_gizmo_plugin(Ref<NavigationRegion3DGizmoPlugin>(memnew(NavigationRegion3DGizmoPlugin))); |
7983 | add_gizmo_plugin(Ref<Joint3DGizmoPlugin>(memnew(Joint3DGizmoPlugin))); |
7984 | add_gizmo_plugin(Ref<PhysicalBone3DGizmoPlugin>(memnew(PhysicalBone3DGizmoPlugin))); |
7985 | add_gizmo_plugin(Ref<FogVolumeGizmoPlugin>(memnew(FogVolumeGizmoPlugin))); |
7986 | } |
7987 | |
7988 | void Node3DEditor::_bind_methods() { |
7989 | ClassDB::bind_method("_get_editor_data" , &Node3DEditor::_get_editor_data); |
7990 | ClassDB::bind_method("_request_gizmo" , &Node3DEditor::_request_gizmo); |
7991 | ClassDB::bind_method("_request_gizmo_for_id" , &Node3DEditor::_request_gizmo_for_id); |
7992 | ClassDB::bind_method("_set_subgizmo_selection" , &Node3DEditor::_set_subgizmo_selection); |
7993 | ClassDB::bind_method("_clear_subgizmo_selection" , &Node3DEditor::_clear_subgizmo_selection); |
7994 | ClassDB::bind_method("_refresh_menu_icons" , &Node3DEditor::_refresh_menu_icons); |
7995 | |
7996 | ADD_SIGNAL(MethodInfo("transform_key_request" )); |
7997 | ADD_SIGNAL(MethodInfo("item_lock_status_changed" )); |
7998 | ADD_SIGNAL(MethodInfo("item_group_status_changed" )); |
7999 | } |
8000 | |
8001 | void Node3DEditor::clear() { |
8002 | settings_fov->set_value(EDITOR_GET("editors/3d/default_fov" )); |
8003 | settings_znear->set_value(EDITOR_GET("editors/3d/default_z_near" )); |
8004 | settings_zfar->set_value(EDITOR_GET("editors/3d/default_z_far" )); |
8005 | |
8006 | snap_translate_value = EditorSettings::get_singleton()->get_project_metadata("3d_editor" , "snap_translate_value" , 1); |
8007 | snap_rotate_value = EditorSettings::get_singleton()->get_project_metadata("3d_editor" , "snap_rotate_value" , 15); |
8008 | snap_scale_value = EditorSettings::get_singleton()->get_project_metadata("3d_editor" , "snap_scale_value" , 10); |
8009 | _snap_update(); |
8010 | |
8011 | for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) { |
8012 | viewports[i]->reset(); |
8013 | } |
8014 | |
8015 | if (origin_instance.is_valid()) { |
8016 | RenderingServer::get_singleton()->instance_set_visible(origin_instance, true); |
8017 | } |
8018 | |
8019 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true); |
8020 | for (int i = 0; i < 3; ++i) { |
8021 | if (grid_enable[i]) { |
8022 | grid_visible[i] = true; |
8023 | } |
8024 | } |
8025 | |
8026 | for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) { |
8027 | viewports[i]->view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(Node3DEditorViewport::VIEW_AUDIO_LISTENER), i == 0); |
8028 | viewports[i]->viewport->set_as_audio_listener_3d(i == 0); |
8029 | } |
8030 | |
8031 | view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true); |
8032 | grid_enabled = true; |
8033 | grid_init_draw = false; |
8034 | } |
8035 | |
8036 | void Node3DEditor::_sun_direction_draw() { |
8037 | sun_direction->draw_rect(Rect2(Vector2(), sun_direction->get_size()), Color(1, 1, 1, 1)); |
8038 | Vector3 z_axis = preview_sun->get_transform().basis.get_column(Vector3::AXIS_Z); |
8039 | z_axis = get_editor_viewport(0)->camera->get_camera_transform().basis.xform_inv(z_axis); |
8040 | sun_direction_material->set_shader_parameter("sun_direction" , Vector3(z_axis.x, -z_axis.y, z_axis.z)); |
8041 | Color color = sun_color->get_pick_color() * sun_energy->get_value(); |
8042 | sun_direction_material->set_shader_parameter("sun_color" , Vector3(color.r, color.g, color.b)); |
8043 | } |
8044 | |
8045 | void Node3DEditor::_preview_settings_changed() { |
8046 | if (sun_environ_updating) { |
8047 | return; |
8048 | } |
8049 | |
8050 | { // preview sun |
8051 | Transform3D t; |
8052 | t.basis = Basis::from_euler(Vector3(sun_rotation.x, sun_rotation.y, 0)); |
8053 | preview_sun->set_transform(t); |
8054 | sun_direction->queue_redraw(); |
8055 | preview_sun->set_param(Light3D::PARAM_ENERGY, sun_energy->get_value()); |
8056 | preview_sun->set_param(Light3D::PARAM_SHADOW_MAX_DISTANCE, sun_max_distance->get_value()); |
8057 | preview_sun->set_color(sun_color->get_pick_color()); |
8058 | } |
8059 | |
8060 | { //preview env |
8061 | sky_material->set_sky_energy_multiplier(environ_energy->get_value()); |
8062 | Color hz_color = environ_sky_color->get_pick_color().lerp(environ_ground_color->get_pick_color(), 0.5).lerp(Color(1, 1, 1), 0.5); |
8063 | sky_material->set_sky_top_color(environ_sky_color->get_pick_color()); |
8064 | sky_material->set_sky_horizon_color(hz_color); |
8065 | sky_material->set_ground_bottom_color(environ_ground_color->get_pick_color()); |
8066 | sky_material->set_ground_horizon_color(hz_color); |
8067 | |
8068 | environment->set_ssao_enabled(environ_ao_button->is_pressed()); |
8069 | environment->set_glow_enabled(environ_glow_button->is_pressed()); |
8070 | environment->set_sdfgi_enabled(environ_gi_button->is_pressed()); |
8071 | environment->set_tonemapper(environ_tonemap_button->is_pressed() ? Environment::TONE_MAPPER_FILMIC : Environment::TONE_MAPPER_LINEAR); |
8072 | } |
8073 | } |
8074 | |
8075 | void Node3DEditor::_load_default_preview_settings() { |
8076 | sun_environ_updating = true; |
8077 | |
8078 | // These default rotations place the preview sun at an angular altitude |
8079 | // of 60 degrees (must be negative) and an azimuth of 30 degrees clockwise |
8080 | // from north (or 150 CCW from south), from north east, facing south west. |
8081 | // On any not-tidally-locked planet, a sun would have an angular altitude |
8082 | // of 60 degrees as the average of all points on the sphere at noon. |
8083 | // The azimuth choice is arbitrary, but ideally shouldn't be on an axis. |
8084 | sun_rotation = Vector2(-Math::deg_to_rad(60.0), Math::deg_to_rad(150.0)); |
8085 | |
8086 | sun_angle_altitude->set_value(-Math::rad_to_deg(sun_rotation.x)); |
8087 | sun_angle_azimuth->set_value(180.0 - Math::rad_to_deg(sun_rotation.y)); |
8088 | sun_direction->queue_redraw(); |
8089 | environ_sky_color->set_pick_color(Color(0.385, 0.454, 0.55)); |
8090 | environ_ground_color->set_pick_color(Color(0.2, 0.169, 0.133)); |
8091 | environ_energy->set_value(1.0); |
8092 | if (OS::get_singleton()->get_current_rendering_method() != "gl_compatibility" ) { |
8093 | environ_glow_button->set_pressed(true); |
8094 | } |
8095 | environ_tonemap_button->set_pressed(true); |
8096 | environ_ao_button->set_pressed(false); |
8097 | environ_gi_button->set_pressed(false); |
8098 | sun_max_distance->set_value(100); |
8099 | |
8100 | sun_color->set_pick_color(Color(1, 1, 1)); |
8101 | sun_energy->set_value(1.0); |
8102 | |
8103 | sun_environ_updating = false; |
8104 | } |
8105 | |
8106 | void Node3DEditor::_update_preview_environment() { |
8107 | bool disable_light = directional_light_count > 0 || !sun_button->is_pressed(); |
8108 | |
8109 | sun_button->set_disabled(directional_light_count > 0); |
8110 | |
8111 | if (disable_light) { |
8112 | if (preview_sun->get_parent()) { |
8113 | preview_sun->get_parent()->remove_child(preview_sun); |
8114 | sun_state->show(); |
8115 | sun_vb->hide(); |
8116 | preview_sun_dangling = true; |
8117 | } |
8118 | |
8119 | if (directional_light_count > 0) { |
8120 | sun_state->set_text(TTR("Scene contains\nDirectionalLight3D.\nPreview disabled." )); |
8121 | } else { |
8122 | sun_state->set_text(TTR("Preview disabled." )); |
8123 | } |
8124 | |
8125 | } else { |
8126 | if (!preview_sun->get_parent()) { |
8127 | add_child(preview_sun, true); |
8128 | sun_state->hide(); |
8129 | sun_vb->show(); |
8130 | preview_sun_dangling = false; |
8131 | } |
8132 | } |
8133 | |
8134 | sun_angle_altitude->set_value(-Math::rad_to_deg(sun_rotation.x)); |
8135 | sun_angle_azimuth->set_value(180.0 - Math::rad_to_deg(sun_rotation.y)); |
8136 | |
8137 | bool disable_env = world_env_count > 0 || !environ_button->is_pressed(); |
8138 | |
8139 | environ_button->set_disabled(world_env_count > 0); |
8140 | |
8141 | if (disable_env) { |
8142 | if (preview_environment->get_parent()) { |
8143 | preview_environment->get_parent()->remove_child(preview_environment); |
8144 | environ_state->show(); |
8145 | environ_vb->hide(); |
8146 | preview_env_dangling = true; |
8147 | } |
8148 | if (world_env_count > 0) { |
8149 | environ_state->set_text(TTR("Scene contains\nWorldEnvironment.\nPreview disabled." )); |
8150 | } else { |
8151 | environ_state->set_text(TTR("Preview disabled." )); |
8152 | } |
8153 | |
8154 | } else { |
8155 | if (!preview_environment->get_parent()) { |
8156 | add_child(preview_environment); |
8157 | environ_state->hide(); |
8158 | environ_vb->show(); |
8159 | preview_env_dangling = false; |
8160 | } |
8161 | } |
8162 | } |
8163 | |
8164 | void Node3DEditor::_sun_direction_input(const Ref<InputEvent> &p_event) { |
8165 | Ref<InputEventMouseMotion> mm = p_event; |
8166 | if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) { |
8167 | sun_rotation.x += mm->get_relative().y * (0.02 * EDSCALE); |
8168 | sun_rotation.y -= mm->get_relative().x * (0.02 * EDSCALE); |
8169 | sun_rotation.x = CLAMP(sun_rotation.x, -Math_TAU / 4, Math_TAU / 4); |
8170 | sun_angle_altitude->set_value(-Math::rad_to_deg(sun_rotation.x)); |
8171 | sun_angle_azimuth->set_value(180.0 - Math::rad_to_deg(sun_rotation.y)); |
8172 | _preview_settings_changed(); |
8173 | } |
8174 | } |
8175 | |
8176 | void Node3DEditor::_sun_direction_angle_set() { |
8177 | sun_rotation.x = Math::deg_to_rad(-sun_angle_altitude->get_value()); |
8178 | sun_rotation.y = Math::deg_to_rad(180.0 - sun_angle_azimuth->get_value()); |
8179 | _preview_settings_changed(); |
8180 | } |
8181 | |
8182 | Node3DEditor::Node3DEditor() { |
8183 | gizmo.visible = true; |
8184 | gizmo.scale = 1.0; |
8185 | |
8186 | viewport_environment = Ref<Environment>(memnew(Environment)); |
8187 | VBoxContainer *vbc = this; |
8188 | |
8189 | custom_camera = nullptr; |
8190 | singleton = this; |
8191 | editor_selection = EditorNode::get_singleton()->get_editor_selection(); |
8192 | editor_selection->add_editor_plugin(this); |
8193 | |
8194 | snap_enabled = false; |
8195 | snap_key_enabled = false; |
8196 | tool_mode = TOOL_MODE_SELECT; |
8197 | |
8198 | camera_override_viewport_id = 0; |
8199 | |
8200 | // Add some margin to the sides for better aesthetics. |
8201 | // This prevents the first button's hover/pressed effect from "touching" the panel's border, |
8202 | // which looks ugly. |
8203 | MarginContainer *toolbar_margin = memnew(MarginContainer); |
8204 | toolbar_margin->add_theme_constant_override("margin_left" , 4 * EDSCALE); |
8205 | toolbar_margin->add_theme_constant_override("margin_right" , 4 * EDSCALE); |
8206 | vbc->add_child(toolbar_margin); |
8207 | |
8208 | // A fluid container for all toolbars. |
8209 | HFlowContainer *main_flow = memnew(HFlowContainer); |
8210 | toolbar_margin->add_child(main_flow); |
8211 | |
8212 | // Main toolbars. |
8213 | HBoxContainer *main_menu_hbox = memnew(HBoxContainer); |
8214 | main_flow->add_child(main_menu_hbox); |
8215 | |
8216 | String sct; |
8217 | |
8218 | tool_button[TOOL_MODE_SELECT] = memnew(Button); |
8219 | main_menu_hbox->add_child(tool_button[TOOL_MODE_SELECT]); |
8220 | tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true); |
8221 | tool_button[TOOL_MODE_SELECT]->set_flat(true); |
8222 | tool_button[TOOL_MODE_SELECT]->set_pressed(true); |
8223 | tool_button[TOOL_MODE_SELECT]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_SELECT)); |
8224 | tool_button[TOOL_MODE_SELECT]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_select" , TTR("Select Mode" ), Key::Q)); |
8225 | tool_button[TOOL_MODE_SELECT]->set_shortcut_context(this); |
8226 | tool_button[TOOL_MODE_SELECT]->set_tooltip_text(keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Drag: Rotate selected node around pivot." ) + "\n" + TTR("Alt+RMB: Show list of all nodes at position clicked, including locked." )); |
8227 | main_menu_hbox->add_child(memnew(VSeparator)); |
8228 | |
8229 | tool_button[TOOL_MODE_MOVE] = memnew(Button); |
8230 | main_menu_hbox->add_child(tool_button[TOOL_MODE_MOVE]); |
8231 | tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true); |
8232 | tool_button[TOOL_MODE_MOVE]->set_flat(true); |
8233 | |
8234 | tool_button[TOOL_MODE_MOVE]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_MOVE)); |
8235 | tool_button[TOOL_MODE_MOVE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_move" , TTR("Move Mode" ), Key::W)); |
8236 | tool_button[TOOL_MODE_MOVE]->set_shortcut_context(this); |
8237 | |
8238 | tool_button[TOOL_MODE_ROTATE] = memnew(Button); |
8239 | main_menu_hbox->add_child(tool_button[TOOL_MODE_ROTATE]); |
8240 | tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true); |
8241 | tool_button[TOOL_MODE_ROTATE]->set_flat(true); |
8242 | tool_button[TOOL_MODE_ROTATE]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_ROTATE)); |
8243 | tool_button[TOOL_MODE_ROTATE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_rotate" , TTR("Rotate Mode" ), Key::E)); |
8244 | tool_button[TOOL_MODE_ROTATE]->set_shortcut_context(this); |
8245 | |
8246 | tool_button[TOOL_MODE_SCALE] = memnew(Button); |
8247 | main_menu_hbox->add_child(tool_button[TOOL_MODE_SCALE]); |
8248 | tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true); |
8249 | tool_button[TOOL_MODE_SCALE]->set_flat(true); |
8250 | tool_button[TOOL_MODE_SCALE]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_SCALE)); |
8251 | tool_button[TOOL_MODE_SCALE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_scale" , TTR("Scale Mode" ), Key::R)); |
8252 | tool_button[TOOL_MODE_SCALE]->set_shortcut_context(this); |
8253 | |
8254 | main_menu_hbox->add_child(memnew(VSeparator)); |
8255 | |
8256 | tool_button[TOOL_MODE_LIST_SELECT] = memnew(Button); |
8257 | main_menu_hbox->add_child(tool_button[TOOL_MODE_LIST_SELECT]); |
8258 | tool_button[TOOL_MODE_LIST_SELECT]->set_toggle_mode(true); |
8259 | tool_button[TOOL_MODE_LIST_SELECT]->set_flat(true); |
8260 | tool_button[TOOL_MODE_LIST_SELECT]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_LIST_SELECT)); |
8261 | tool_button[TOOL_MODE_LIST_SELECT]->set_tooltip_text(TTR("Show list of selectable nodes at position clicked." )); |
8262 | |
8263 | tool_button[TOOL_LOCK_SELECTED] = memnew(Button); |
8264 | main_menu_hbox->add_child(tool_button[TOOL_LOCK_SELECTED]); |
8265 | tool_button[TOOL_LOCK_SELECTED]->set_flat(true); |
8266 | tool_button[TOOL_LOCK_SELECTED]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_LOCK_SELECTED)); |
8267 | tool_button[TOOL_LOCK_SELECTED]->set_tooltip_text(TTR("Lock selected node, preventing selection and movement." )); |
8268 | // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused. |
8269 | tool_button[TOOL_LOCK_SELECTED]->set_shortcut(ED_SHORTCUT("editor/lock_selected_nodes" , TTR("Lock Selected Node(s)" ), KeyModifierMask::CMD_OR_CTRL | Key::L)); |
8270 | |
8271 | tool_button[TOOL_UNLOCK_SELECTED] = memnew(Button); |
8272 | main_menu_hbox->add_child(tool_button[TOOL_UNLOCK_SELECTED]); |
8273 | tool_button[TOOL_UNLOCK_SELECTED]->set_flat(true); |
8274 | tool_button[TOOL_UNLOCK_SELECTED]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_UNLOCK_SELECTED)); |
8275 | tool_button[TOOL_UNLOCK_SELECTED]->set_tooltip_text(TTR("Unlock selected node, allowing selection and movement." )); |
8276 | // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused. |
8277 | tool_button[TOOL_UNLOCK_SELECTED]->set_shortcut(ED_SHORTCUT("editor/unlock_selected_nodes" , TTR("Unlock Selected Node(s)" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::L)); |
8278 | |
8279 | tool_button[TOOL_GROUP_SELECTED] = memnew(Button); |
8280 | main_menu_hbox->add_child(tool_button[TOOL_GROUP_SELECTED]); |
8281 | tool_button[TOOL_GROUP_SELECTED]->set_flat(true); |
8282 | tool_button[TOOL_GROUP_SELECTED]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_GROUP_SELECTED)); |
8283 | tool_button[TOOL_GROUP_SELECTED]->set_tooltip_text(TTR("Make selected node's children not selectable." )); |
8284 | // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused. |
8285 | tool_button[TOOL_GROUP_SELECTED]->set_shortcut(ED_SHORTCUT("editor/group_selected_nodes" , TTR("Group Selected Node(s)" ), KeyModifierMask::CMD_OR_CTRL | Key::G)); |
8286 | |
8287 | tool_button[TOOL_UNGROUP_SELECTED] = memnew(Button); |
8288 | main_menu_hbox->add_child(tool_button[TOOL_UNGROUP_SELECTED]); |
8289 | tool_button[TOOL_UNGROUP_SELECTED]->set_flat(true); |
8290 | tool_button[TOOL_UNGROUP_SELECTED]->connect("pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_UNGROUP_SELECTED)); |
8291 | tool_button[TOOL_UNGROUP_SELECTED]->set_tooltip_text(TTR("Make selected node's children selectable." )); |
8292 | // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused. |
8293 | tool_button[TOOL_UNGROUP_SELECTED]->set_shortcut(ED_SHORTCUT("editor/ungroup_selected_nodes" , TTR("Ungroup Selected Node(s)" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::G)); |
8294 | |
8295 | main_menu_hbox->add_child(memnew(VSeparator)); |
8296 | |
8297 | tool_option_button[TOOL_OPT_LOCAL_COORDS] = memnew(Button); |
8298 | main_menu_hbox->add_child(tool_option_button[TOOL_OPT_LOCAL_COORDS]); |
8299 | tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_toggle_mode(true); |
8300 | tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_flat(true); |
8301 | tool_option_button[TOOL_OPT_LOCAL_COORDS]->connect("toggled" , callable_mp(this, &Node3DEditor::_menu_item_toggled).bind(MENU_TOOL_LOCAL_COORDS)); |
8302 | tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_shortcut(ED_SHORTCUT("spatial_editor/local_coords" , TTR("Use Local Space" ), Key::T)); |
8303 | tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_shortcut_context(this); |
8304 | |
8305 | tool_option_button[TOOL_OPT_USE_SNAP] = memnew(Button); |
8306 | main_menu_hbox->add_child(tool_option_button[TOOL_OPT_USE_SNAP]); |
8307 | tool_option_button[TOOL_OPT_USE_SNAP]->set_toggle_mode(true); |
8308 | tool_option_button[TOOL_OPT_USE_SNAP]->set_flat(true); |
8309 | tool_option_button[TOOL_OPT_USE_SNAP]->connect("toggled" , callable_mp(this, &Node3DEditor::_menu_item_toggled).bind(MENU_TOOL_USE_SNAP)); |
8310 | tool_option_button[TOOL_OPT_USE_SNAP]->set_shortcut(ED_SHORTCUT("spatial_editor/snap" , TTR("Use Snap" ), Key::Y)); |
8311 | tool_option_button[TOOL_OPT_USE_SNAP]->set_shortcut_context(this); |
8312 | |
8313 | main_menu_hbox->add_child(memnew(VSeparator)); |
8314 | |
8315 | tool_option_button[TOOL_OPT_OVERRIDE_CAMERA] = memnew(Button); |
8316 | main_menu_hbox->add_child(tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]); |
8317 | tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_toggle_mode(true); |
8318 | tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_flat(true); |
8319 | tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_disabled(true); |
8320 | tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->connect("toggled" , callable_mp(this, &Node3DEditor::_menu_item_toggled).bind(MENU_TOOL_OVERRIDE_CAMERA)); |
8321 | _update_camera_override_button(false); |
8322 | |
8323 | main_menu_hbox->add_child(memnew(VSeparator)); |
8324 | sun_button = memnew(Button); |
8325 | sun_button->set_tooltip_text(TTR("Toggle preview sunlight.\nIf a DirectionalLight3D node is added to the scene, preview sunlight is disabled." )); |
8326 | sun_button->set_toggle_mode(true); |
8327 | sun_button->set_flat(true); |
8328 | sun_button->connect("pressed" , callable_mp(this, &Node3DEditor::_update_preview_environment), CONNECT_DEFERRED); |
8329 | // Preview is enabled by default - ensure this applies on editor startup when there is no state yet. |
8330 | sun_button->set_pressed(true); |
8331 | |
8332 | main_menu_hbox->add_child(sun_button); |
8333 | |
8334 | environ_button = memnew(Button); |
8335 | environ_button->set_tooltip_text(TTR("Toggle preview environment.\nIf a WorldEnvironment node is added to the scene, preview environment is disabled." )); |
8336 | environ_button->set_toggle_mode(true); |
8337 | environ_button->set_flat(true); |
8338 | environ_button->connect("pressed" , callable_mp(this, &Node3DEditor::_update_preview_environment), CONNECT_DEFERRED); |
8339 | // Preview is enabled by default - ensure this applies on editor startup when there is no state yet. |
8340 | environ_button->set_pressed(true); |
8341 | |
8342 | main_menu_hbox->add_child(environ_button); |
8343 | |
8344 | sun_environ_settings = memnew(Button); |
8345 | sun_environ_settings->set_tooltip_text(TTR("Edit Sun and Environment settings." )); |
8346 | sun_environ_settings->set_flat(true); |
8347 | sun_environ_settings->connect("pressed" , callable_mp(this, &Node3DEditor::_sun_environ_settings_pressed)); |
8348 | |
8349 | main_menu_hbox->add_child(sun_environ_settings); |
8350 | |
8351 | main_menu_hbox->add_child(memnew(VSeparator)); |
8352 | |
8353 | // Drag and drop support; |
8354 | preview_node = memnew(Node3D); |
8355 | preview_bounds = AABB(); |
8356 | |
8357 | ED_SHORTCUT("spatial_editor/bottom_view" , TTR("Bottom View" ), KeyModifierMask::ALT + Key::KP_7); |
8358 | ED_SHORTCUT("spatial_editor/top_view" , TTR("Top View" ), Key::KP_7); |
8359 | ED_SHORTCUT("spatial_editor/rear_view" , TTR("Rear View" ), KeyModifierMask::ALT + Key::KP_1); |
8360 | ED_SHORTCUT("spatial_editor/front_view" , TTR("Front View" ), Key::KP_1); |
8361 | ED_SHORTCUT("spatial_editor/left_view" , TTR("Left View" ), KeyModifierMask::ALT + Key::KP_3); |
8362 | ED_SHORTCUT("spatial_editor/right_view" , TTR("Right View" ), Key::KP_3); |
8363 | ED_SHORTCUT("spatial_editor/orbit_view_down" , TTR("Orbit View Down" ), Key::KP_2); |
8364 | ED_SHORTCUT("spatial_editor/orbit_view_left" , TTR("Orbit View Left" ), Key::KP_4); |
8365 | ED_SHORTCUT("spatial_editor/orbit_view_right" , TTR("Orbit View Right" ), Key::KP_6); |
8366 | ED_SHORTCUT("spatial_editor/orbit_view_up" , TTR("Orbit View Up" ), Key::KP_8); |
8367 | ED_SHORTCUT("spatial_editor/orbit_view_180" , TTR("Orbit View 180" ), Key::KP_9); |
8368 | ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal" , TTR("Switch Perspective/Orthogonal View" ), Key::KP_5); |
8369 | ED_SHORTCUT("spatial_editor/insert_anim_key" , TTR("Insert Animation Key" ), Key::K); |
8370 | ED_SHORTCUT("spatial_editor/focus_origin" , TTR("Focus Origin" ), Key::O); |
8371 | ED_SHORTCUT("spatial_editor/focus_selection" , TTR("Focus Selection" ), Key::F); |
8372 | ED_SHORTCUT("spatial_editor/align_transform_with_view" , TTR("Align Transform with View" ), KeyModifierMask::ALT + KeyModifierMask::CMD_OR_CTRL + Key::M); |
8373 | ED_SHORTCUT("spatial_editor/align_rotation_with_view" , TTR("Align Rotation with View" ), KeyModifierMask::ALT + KeyModifierMask::CMD_OR_CTRL + Key::F); |
8374 | ED_SHORTCUT("spatial_editor/freelook_toggle" , TTR("Toggle Freelook" ), KeyModifierMask::SHIFT + Key::F); |
8375 | ED_SHORTCUT("spatial_editor/decrease_fov" , TTR("Decrease Field of View" ), KeyModifierMask::CMD_OR_CTRL + Key::EQUAL); // Usually direct access key for `KEY_PLUS`. |
8376 | ED_SHORTCUT("spatial_editor/increase_fov" , TTR("Increase Field of View" ), KeyModifierMask::CMD_OR_CTRL + Key::MINUS); |
8377 | ED_SHORTCUT("spatial_editor/reset_fov" , TTR("Reset Field of View to Default" ), KeyModifierMask::CMD_OR_CTRL + Key::KEY_0); |
8378 | |
8379 | PopupMenu *p; |
8380 | |
8381 | transform_menu = memnew(MenuButton); |
8382 | transform_menu->set_text(TTR("Transform" )); |
8383 | transform_menu->set_switch_on_hover(true); |
8384 | transform_menu->set_shortcut_context(this); |
8385 | main_menu_hbox->add_child(transform_menu); |
8386 | |
8387 | p = transform_menu->get_popup(); |
8388 | p->add_shortcut(ED_SHORTCUT("spatial_editor/snap_to_floor" , TTR("Snap Object to Floor" ), Key::PAGEDOWN), MENU_SNAP_TO_FLOOR); |
8389 | p->add_shortcut(ED_SHORTCUT("spatial_editor/transform_dialog" , TTR("Transform Dialog..." )), MENU_TRANSFORM_DIALOG); |
8390 | |
8391 | p->add_separator(); |
8392 | p->add_shortcut(ED_SHORTCUT("spatial_editor/configure_snap" , TTR("Configure Snap..." )), MENU_TRANSFORM_CONFIGURE_SNAP); |
8393 | |
8394 | p->connect("id_pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed)); |
8395 | |
8396 | view_menu = memnew(MenuButton); |
8397 | // TRANSLATORS: Noun, name of the 2D/3D View menus. |
8398 | view_menu->set_text(TTR("View" )); |
8399 | view_menu->set_switch_on_hover(true); |
8400 | view_menu->set_shortcut_context(this); |
8401 | main_menu_hbox->add_child(view_menu); |
8402 | |
8403 | main_menu_hbox->add_child(memnew(VSeparator)); |
8404 | |
8405 | context_toolbar_panel = memnew(PanelContainer); |
8406 | context_toolbar_hbox = memnew(HBoxContainer); |
8407 | context_toolbar_panel->add_child(context_toolbar_hbox); |
8408 | main_flow->add_child(context_toolbar_panel); |
8409 | |
8410 | // Get the view menu popup and have it stay open when a checkable item is selected |
8411 | p = view_menu->get_popup(); |
8412 | p->set_hide_on_checkable_item_selection(false); |
8413 | |
8414 | accept = memnew(AcceptDialog); |
8415 | EditorNode::get_singleton()->get_gui_base()->add_child(accept); |
8416 | |
8417 | p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/1_viewport" , TTR("1 Viewport" ), KeyModifierMask::CMD_OR_CTRL + Key::KEY_1), MENU_VIEW_USE_1_VIEWPORT); |
8418 | p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports" , TTR("2 Viewports" ), KeyModifierMask::CMD_OR_CTRL + Key::KEY_2), MENU_VIEW_USE_2_VIEWPORTS); |
8419 | p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports_alt" , TTR("2 Viewports (Alt)" ), KeyModifierMask::ALT + KeyModifierMask::CMD_OR_CTRL + Key::KEY_2), MENU_VIEW_USE_2_VIEWPORTS_ALT); |
8420 | p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports" , TTR("3 Viewports" ), KeyModifierMask::CMD_OR_CTRL + Key::KEY_3), MENU_VIEW_USE_3_VIEWPORTS); |
8421 | p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports_alt" , TTR("3 Viewports (Alt)" ), KeyModifierMask::ALT + KeyModifierMask::CMD_OR_CTRL + Key::KEY_3), MENU_VIEW_USE_3_VIEWPORTS_ALT); |
8422 | p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports" , TTR("4 Viewports" ), KeyModifierMask::CMD_OR_CTRL + Key::KEY_4), MENU_VIEW_USE_4_VIEWPORTS); |
8423 | p->add_separator(); |
8424 | |
8425 | p->add_submenu_item(TTR("Gizmos" ), "GizmosMenu" ); |
8426 | |
8427 | p->add_separator(); |
8428 | p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin" , TTR("View Origin" )), MENU_VIEW_ORIGIN); |
8429 | p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid" , TTR("View Grid" ), Key::NUMBERSIGN), MENU_VIEW_GRID); |
8430 | |
8431 | p->add_separator(); |
8432 | p->add_shortcut(ED_SHORTCUT("spatial_editor/settings" , TTR("Settings..." )), MENU_VIEW_CAMERA_SETTINGS); |
8433 | |
8434 | p->set_item_checked(p->get_item_index(MENU_VIEW_ORIGIN), true); |
8435 | p->set_item_checked(p->get_item_index(MENU_VIEW_GRID), true); |
8436 | |
8437 | p->connect("id_pressed" , callable_mp(this, &Node3DEditor::_menu_item_pressed)); |
8438 | |
8439 | gizmos_menu = memnew(PopupMenu); |
8440 | p->add_child(gizmos_menu); |
8441 | gizmos_menu->set_name("GizmosMenu" ); |
8442 | gizmos_menu->set_hide_on_checkable_item_selection(false); |
8443 | gizmos_menu->connect("id_pressed" , callable_mp(this, &Node3DEditor::_menu_gizmo_toggled)); |
8444 | |
8445 | /* REST OF MENU */ |
8446 | |
8447 | left_panel_split = memnew(HSplitContainer); |
8448 | left_panel_split->set_v_size_flags(SIZE_EXPAND_FILL); |
8449 | vbc->add_child(left_panel_split); |
8450 | |
8451 | right_panel_split = memnew(HSplitContainer); |
8452 | right_panel_split->set_v_size_flags(SIZE_EXPAND_FILL); |
8453 | left_panel_split->add_child(right_panel_split); |
8454 | |
8455 | shader_split = memnew(VSplitContainer); |
8456 | shader_split->set_h_size_flags(SIZE_EXPAND_FILL); |
8457 | right_panel_split->add_child(shader_split); |
8458 | viewport_base = memnew(Node3DEditorViewportContainer); |
8459 | shader_split->add_child(viewport_base); |
8460 | viewport_base->set_v_size_flags(SIZE_EXPAND_FILL); |
8461 | for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) { |
8462 | viewports[i] = memnew(Node3DEditorViewport(this, i)); |
8463 | viewports[i]->connect("toggle_maximize_view" , callable_mp(this, &Node3DEditor::_toggle_maximize_view)); |
8464 | viewports[i]->connect("clicked" , callable_mp(this, &Node3DEditor::_update_camera_override_viewport)); |
8465 | viewports[i]->assign_pending_data_pointers(preview_node, &preview_bounds, accept); |
8466 | viewport_base->add_child(viewports[i]); |
8467 | } |
8468 | |
8469 | /* SNAP DIALOG */ |
8470 | |
8471 | snap_dialog = memnew(ConfirmationDialog); |
8472 | snap_dialog->set_title(TTR("Snap Settings" )); |
8473 | add_child(snap_dialog); |
8474 | snap_dialog->connect("confirmed" , callable_mp(this, &Node3DEditor::_snap_changed)); |
8475 | snap_dialog->get_cancel_button()->connect("pressed" , callable_mp(this, &Node3DEditor::_snap_update)); |
8476 | |
8477 | VBoxContainer *snap_dialog_vbc = memnew(VBoxContainer); |
8478 | snap_dialog->add_child(snap_dialog_vbc); |
8479 | |
8480 | snap_translate = memnew(LineEdit); |
8481 | snap_translate->set_select_all_on_focus(true); |
8482 | snap_dialog_vbc->add_margin_child(TTR("Translate Snap:" ), snap_translate); |
8483 | |
8484 | snap_rotate = memnew(LineEdit); |
8485 | snap_rotate->set_select_all_on_focus(true); |
8486 | snap_dialog_vbc->add_margin_child(TTR("Rotate Snap (deg.):" ), snap_rotate); |
8487 | |
8488 | snap_scale = memnew(LineEdit); |
8489 | snap_scale->set_select_all_on_focus(true); |
8490 | snap_dialog_vbc->add_margin_child(TTR("Scale Snap (%):" ), snap_scale); |
8491 | |
8492 | /* SETTINGS DIALOG */ |
8493 | |
8494 | settings_dialog = memnew(ConfirmationDialog); |
8495 | settings_dialog->set_title(TTR("Viewport Settings" )); |
8496 | add_child(settings_dialog); |
8497 | settings_vbc = memnew(VBoxContainer); |
8498 | settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE); |
8499 | settings_dialog->add_child(settings_vbc); |
8500 | |
8501 | settings_fov = memnew(SpinBox); |
8502 | settings_fov->set_max(MAX_FOV); |
8503 | settings_fov->set_min(MIN_FOV); |
8504 | settings_fov->set_step(0.1); |
8505 | settings_fov->set_value(EDITOR_GET("editors/3d/default_fov" )); |
8506 | settings_fov->set_select_all_on_focus(true); |
8507 | settings_vbc->add_margin_child(TTR("Perspective FOV (deg.):" ), settings_fov); |
8508 | |
8509 | settings_znear = memnew(SpinBox); |
8510 | settings_znear->set_max(MAX_Z); |
8511 | settings_znear->set_min(MIN_Z); |
8512 | settings_znear->set_step(0.01); |
8513 | settings_znear->set_value(EDITOR_GET("editors/3d/default_z_near" )); |
8514 | settings_znear->set_select_all_on_focus(true); |
8515 | settings_vbc->add_margin_child(TTR("View Z-Near:" ), settings_znear); |
8516 | |
8517 | settings_zfar = memnew(SpinBox); |
8518 | settings_zfar->set_max(MAX_Z); |
8519 | settings_zfar->set_min(MIN_Z); |
8520 | settings_zfar->set_step(0.1); |
8521 | settings_zfar->set_value(EDITOR_GET("editors/3d/default_z_far" )); |
8522 | settings_zfar->set_select_all_on_focus(true); |
8523 | settings_vbc->add_margin_child(TTR("View Z-Far:" ), settings_zfar); |
8524 | |
8525 | for (uint32_t i = 0; i < VIEWPORTS_COUNT; ++i) { |
8526 | settings_dialog->connect("confirmed" , callable_mp(viewports[i], &Node3DEditorViewport::_view_settings_confirmed).bind(0.0)); |
8527 | } |
8528 | |
8529 | /* XFORM DIALOG */ |
8530 | |
8531 | xform_dialog = memnew(ConfirmationDialog); |
8532 | xform_dialog->set_title(TTR("Transform Change" )); |
8533 | add_child(xform_dialog); |
8534 | |
8535 | VBoxContainer *xform_vbc = memnew(VBoxContainer); |
8536 | xform_dialog->add_child(xform_vbc); |
8537 | |
8538 | Label *l = memnew(Label); |
8539 | l->set_text(TTR("Translate:" )); |
8540 | xform_vbc->add_child(l); |
8541 | |
8542 | HBoxContainer *xform_hbc = memnew(HBoxContainer); |
8543 | xform_vbc->add_child(xform_hbc); |
8544 | |
8545 | for (int i = 0; i < 3; i++) { |
8546 | xform_translate[i] = memnew(LineEdit); |
8547 | xform_translate[i]->set_h_size_flags(SIZE_EXPAND_FILL); |
8548 | xform_translate[i]->set_select_all_on_focus(true); |
8549 | xform_hbc->add_child(xform_translate[i]); |
8550 | } |
8551 | |
8552 | l = memnew(Label); |
8553 | l->set_text(TTR("Rotate (deg.):" )); |
8554 | xform_vbc->add_child(l); |
8555 | |
8556 | xform_hbc = memnew(HBoxContainer); |
8557 | xform_vbc->add_child(xform_hbc); |
8558 | |
8559 | for (int i = 0; i < 3; i++) { |
8560 | xform_rotate[i] = memnew(LineEdit); |
8561 | xform_rotate[i]->set_h_size_flags(SIZE_EXPAND_FILL); |
8562 | xform_rotate[i]->set_select_all_on_focus(true); |
8563 | xform_hbc->add_child(xform_rotate[i]); |
8564 | } |
8565 | |
8566 | l = memnew(Label); |
8567 | l->set_text(TTR("Scale (ratio):" )); |
8568 | xform_vbc->add_child(l); |
8569 | |
8570 | xform_hbc = memnew(HBoxContainer); |
8571 | xform_vbc->add_child(xform_hbc); |
8572 | |
8573 | for (int i = 0; i < 3; i++) { |
8574 | xform_scale[i] = memnew(LineEdit); |
8575 | xform_scale[i]->set_h_size_flags(SIZE_EXPAND_FILL); |
8576 | xform_scale[i]->set_select_all_on_focus(true); |
8577 | xform_hbc->add_child(xform_scale[i]); |
8578 | } |
8579 | |
8580 | l = memnew(Label); |
8581 | l->set_text(TTR("Transform Type" )); |
8582 | xform_vbc->add_child(l); |
8583 | |
8584 | xform_type = memnew(OptionButton); |
8585 | xform_type->set_h_size_flags(SIZE_EXPAND_FILL); |
8586 | xform_type->add_item(TTR("Pre" )); |
8587 | xform_type->add_item(TTR("Post" )); |
8588 | xform_vbc->add_child(xform_type); |
8589 | |
8590 | xform_dialog->connect("confirmed" , callable_mp(this, &Node3DEditor::_xform_dialog_action)); |
8591 | |
8592 | selected = nullptr; |
8593 | |
8594 | set_process_shortcut_input(true); |
8595 | add_to_group("_spatial_editor_group" ); |
8596 | |
8597 | EDITOR_DEF("editors/3d/manipulator_gizmo_size" , 80); |
8598 | EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/3d/manipulator_gizmo_size" , PROPERTY_HINT_RANGE, "16,160,1" )); |
8599 | EDITOR_DEF("editors/3d/manipulator_gizmo_opacity" , 0.9); |
8600 | EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::FLOAT, "editors/3d/manipulator_gizmo_opacity" , PROPERTY_HINT_RANGE, "0,1,0.01" )); |
8601 | EDITOR_DEF("editors/3d/navigation/show_viewport_rotation_gizmo" , true); |
8602 | EDITOR_DEF("editors/3d/navigation/show_viewport_navigation_gizmo" , DisplayServer::get_singleton()->is_touchscreen_available()); |
8603 | |
8604 | current_hover_gizmo_handle = -1; |
8605 | current_hover_gizmo_handle_secondary = false; |
8606 | { |
8607 | //sun popup |
8608 | |
8609 | sun_environ_popup = memnew(PopupPanel); |
8610 | add_child(sun_environ_popup); |
8611 | |
8612 | HBoxContainer *sun_environ_hb = memnew(HBoxContainer); |
8613 | |
8614 | sun_environ_popup->add_child(sun_environ_hb); |
8615 | |
8616 | sun_vb = memnew(VBoxContainer); |
8617 | sun_environ_hb->add_child(sun_vb); |
8618 | sun_vb->set_custom_minimum_size(Size2(200 * EDSCALE, 0)); |
8619 | sun_vb->hide(); |
8620 | |
8621 | sun_title = memnew(Label); |
8622 | sun_title->set_theme_type_variation("HeaderSmall" ); |
8623 | sun_vb->add_child(sun_title); |
8624 | sun_title->set_text(TTR("Preview Sun" )); |
8625 | sun_title->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
8626 | |
8627 | CenterContainer *sun_direction_center = memnew(CenterContainer); |
8628 | sun_direction = memnew(Control); |
8629 | sun_direction->set_custom_minimum_size(Size2(128, 128) * EDSCALE); |
8630 | sun_direction_center->add_child(sun_direction); |
8631 | sun_vb->add_margin_child(TTR("Sun Direction" ), sun_direction_center); |
8632 | sun_direction->connect("gui_input" , callable_mp(this, &Node3DEditor::_sun_direction_input)); |
8633 | sun_direction->connect("draw" , callable_mp(this, &Node3DEditor::_sun_direction_draw)); |
8634 | sun_direction->set_default_cursor_shape(CURSOR_MOVE); |
8635 | |
8636 | sun_direction_shader.instantiate(); |
8637 | sun_direction_shader->set_code(R"( |
8638 | // 3D editor Preview Sun direction shader. |
8639 | |
8640 | shader_type canvas_item; |
8641 | |
8642 | uniform vec3 sun_direction; |
8643 | uniform vec3 sun_color; |
8644 | |
8645 | void fragment() { |
8646 | vec3 n; |
8647 | n.xy = UV * 2.0 - 1.0; |
8648 | n.z = sqrt(max(0.0, 1.0 - dot(n.xy, n.xy))); |
8649 | COLOR.rgb = dot(n, sun_direction) * sun_color; |
8650 | COLOR.a = 1.0 - smoothstep(0.99, 1.0, length(n.xy)); |
8651 | } |
8652 | )" ); |
8653 | sun_direction_material.instantiate(); |
8654 | sun_direction_material->set_shader(sun_direction_shader); |
8655 | sun_direction_material->set_shader_parameter("sun_direction" , Vector3(0, 0, 1)); |
8656 | sun_direction_material->set_shader_parameter("sun_color" , Vector3(1, 1, 1)); |
8657 | sun_direction->set_material(sun_direction_material); |
8658 | |
8659 | HBoxContainer *sun_angle_hbox = memnew(HBoxContainer); |
8660 | VBoxContainer *sun_angle_altitude_vbox = memnew(VBoxContainer); |
8661 | Label *sun_angle_altitude_label = memnew(Label); |
8662 | sun_angle_altitude_label->set_text(TTR("Angular Altitude" )); |
8663 | sun_angle_altitude_vbox->add_child(sun_angle_altitude_label); |
8664 | sun_angle_altitude = memnew(EditorSpinSlider); |
8665 | sun_angle_altitude->set_max(90); |
8666 | sun_angle_altitude->set_min(-90); |
8667 | sun_angle_altitude->set_step(0.1); |
8668 | sun_angle_altitude->connect("value_changed" , callable_mp(this, &Node3DEditor::_sun_direction_angle_set).unbind(1)); |
8669 | sun_angle_altitude_vbox->add_child(sun_angle_altitude); |
8670 | sun_angle_hbox->add_child(sun_angle_altitude_vbox); |
8671 | VBoxContainer *sun_angle_azimuth_vbox = memnew(VBoxContainer); |
8672 | sun_angle_azimuth_vbox->set_custom_minimum_size(Vector2(100, 0)); |
8673 | Label *sun_angle_azimuth_label = memnew(Label); |
8674 | sun_angle_azimuth_label->set_text(TTR("Azimuth" )); |
8675 | sun_angle_azimuth_vbox->add_child(sun_angle_azimuth_label); |
8676 | sun_angle_azimuth = memnew(EditorSpinSlider); |
8677 | sun_angle_azimuth->set_max(180); |
8678 | sun_angle_azimuth->set_min(-180); |
8679 | sun_angle_azimuth->set_step(0.1); |
8680 | sun_angle_azimuth->set_allow_greater(true); |
8681 | sun_angle_azimuth->set_allow_lesser(true); |
8682 | sun_angle_azimuth->connect("value_changed" , callable_mp(this, &Node3DEditor::_sun_direction_angle_set).unbind(1)); |
8683 | sun_angle_azimuth_vbox->add_child(sun_angle_azimuth); |
8684 | sun_angle_hbox->add_child(sun_angle_azimuth_vbox); |
8685 | sun_angle_hbox->add_theme_constant_override("separation" , 10); |
8686 | sun_vb->add_child(sun_angle_hbox); |
8687 | |
8688 | sun_color = memnew(ColorPickerButton); |
8689 | sun_color->set_edit_alpha(false); |
8690 | sun_vb->add_margin_child(TTR("Sun Color" ), sun_color); |
8691 | sun_color->connect("color_changed" , callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1)); |
8692 | sun_color->get_popup()->connect("about_to_popup" , callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(sun_color->get_picker())); |
8693 | |
8694 | sun_energy = memnew(EditorSpinSlider); |
8695 | sun_energy->set_max(64.0); |
8696 | sun_energy->set_min(0); |
8697 | sun_energy->set_step(0.05); |
8698 | sun_vb->add_margin_child(TTR("Sun Energy" ), sun_energy); |
8699 | sun_energy->connect("value_changed" , callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1)); |
8700 | |
8701 | sun_max_distance = memnew(EditorSpinSlider); |
8702 | sun_vb->add_margin_child(TTR("Shadow Max Distance" ), sun_max_distance); |
8703 | sun_max_distance->connect("value_changed" , callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1)); |
8704 | sun_max_distance->set_min(1); |
8705 | sun_max_distance->set_max(4096); |
8706 | |
8707 | sun_add_to_scene = memnew(Button); |
8708 | sun_add_to_scene->set_text(TTR("Add Sun to Scene" )); |
8709 | sun_add_to_scene->set_tooltip_text(TTR("Adds a DirectionalLight3D node matching the preview sun settings to the current scene.\nHold Shift while clicking to also add the preview environment to the current scene." )); |
8710 | sun_add_to_scene->connect("pressed" , callable_mp(this, &Node3DEditor::_add_sun_to_scene).bind(false)); |
8711 | sun_vb->add_spacer(); |
8712 | sun_vb->add_child(sun_add_to_scene); |
8713 | |
8714 | sun_state = memnew(Label); |
8715 | sun_environ_hb->add_child(sun_state); |
8716 | sun_state->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
8717 | sun_state->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
8718 | sun_state->set_h_size_flags(SIZE_EXPAND_FILL); |
8719 | |
8720 | VSeparator *sc = memnew(VSeparator); |
8721 | sc->set_custom_minimum_size(Size2(50 * EDSCALE, 0)); |
8722 | sc->set_v_size_flags(SIZE_EXPAND_FILL); |
8723 | sun_environ_hb->add_child(sc); |
8724 | |
8725 | environ_vb = memnew(VBoxContainer); |
8726 | sun_environ_hb->add_child(environ_vb); |
8727 | environ_vb->set_custom_minimum_size(Size2(200 * EDSCALE, 0)); |
8728 | environ_vb->hide(); |
8729 | |
8730 | environ_title = memnew(Label); |
8731 | environ_title->set_theme_type_variation("HeaderSmall" ); |
8732 | |
8733 | environ_vb->add_child(environ_title); |
8734 | environ_title->set_text(TTR("Preview Environment" )); |
8735 | environ_title->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
8736 | |
8737 | environ_sky_color = memnew(ColorPickerButton); |
8738 | environ_sky_color->set_edit_alpha(false); |
8739 | environ_sky_color->connect("color_changed" , callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1)); |
8740 | environ_sky_color->get_popup()->connect("about_to_popup" , callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(environ_sky_color->get_picker())); |
8741 | environ_vb->add_margin_child(TTR("Sky Color" ), environ_sky_color); |
8742 | environ_ground_color = memnew(ColorPickerButton); |
8743 | environ_ground_color->connect("color_changed" , callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1)); |
8744 | environ_ground_color->set_edit_alpha(false); |
8745 | environ_ground_color->get_popup()->connect("about_to_popup" , callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(environ_ground_color->get_picker())); |
8746 | environ_vb->add_margin_child(TTR("Ground Color" ), environ_ground_color); |
8747 | environ_energy = memnew(EditorSpinSlider); |
8748 | environ_energy->set_max(8.0); |
8749 | environ_energy->set_min(0); |
8750 | environ_energy->set_step(0.05); |
8751 | environ_energy->connect("value_changed" , callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1)); |
8752 | environ_vb->add_margin_child(TTR("Sky Energy" ), environ_energy); |
8753 | HBoxContainer *fx_vb = memnew(HBoxContainer); |
8754 | fx_vb->set_h_size_flags(SIZE_EXPAND_FILL); |
8755 | |
8756 | environ_ao_button = memnew(Button); |
8757 | environ_ao_button->set_text(TTR("AO" )); |
8758 | environ_ao_button->set_toggle_mode(true); |
8759 | environ_ao_button->connect("pressed" , callable_mp(this, &Node3DEditor::_preview_settings_changed), CONNECT_DEFERRED); |
8760 | fx_vb->add_child(environ_ao_button); |
8761 | environ_glow_button = memnew(Button); |
8762 | environ_glow_button->set_text(TTR("Glow" )); |
8763 | environ_glow_button->set_toggle_mode(true); |
8764 | environ_glow_button->connect("pressed" , callable_mp(this, &Node3DEditor::_preview_settings_changed), CONNECT_DEFERRED); |
8765 | fx_vb->add_child(environ_glow_button); |
8766 | environ_tonemap_button = memnew(Button); |
8767 | environ_tonemap_button->set_text(TTR("Tonemap" )); |
8768 | environ_tonemap_button->set_toggle_mode(true); |
8769 | environ_tonemap_button->connect("pressed" , callable_mp(this, &Node3DEditor::_preview_settings_changed), CONNECT_DEFERRED); |
8770 | fx_vb->add_child(environ_tonemap_button); |
8771 | environ_gi_button = memnew(Button); |
8772 | environ_gi_button->set_text(TTR("GI" )); |
8773 | environ_gi_button->set_toggle_mode(true); |
8774 | environ_gi_button->connect("pressed" , callable_mp(this, &Node3DEditor::_preview_settings_changed), CONNECT_DEFERRED); |
8775 | fx_vb->add_child(environ_gi_button); |
8776 | environ_vb->add_margin_child(TTR("Post Process" ), fx_vb); |
8777 | |
8778 | environ_add_to_scene = memnew(Button); |
8779 | environ_add_to_scene->set_text(TTR("Add Environment to Scene" )); |
8780 | environ_add_to_scene->set_tooltip_text(TTR("Adds a WorldEnvironment node matching the preview environment settings to the current scene.\nHold Shift while clicking to also add the preview sun to the current scene." )); |
8781 | environ_add_to_scene->connect("pressed" , callable_mp(this, &Node3DEditor::_add_environment_to_scene).bind(false)); |
8782 | environ_vb->add_spacer(); |
8783 | environ_vb->add_child(environ_add_to_scene); |
8784 | |
8785 | environ_state = memnew(Label); |
8786 | sun_environ_hb->add_child(environ_state); |
8787 | environ_state->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
8788 | environ_state->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
8789 | environ_state->set_h_size_flags(SIZE_EXPAND_FILL); |
8790 | |
8791 | preview_sun = memnew(DirectionalLight3D); |
8792 | preview_sun->set_shadow(true); |
8793 | preview_sun->set_shadow_mode(DirectionalLight3D::SHADOW_PARALLEL_4_SPLITS); |
8794 | preview_environment = memnew(WorldEnvironment); |
8795 | environment.instantiate(); |
8796 | preview_environment->set_environment(environment); |
8797 | if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units" )) { |
8798 | camera_attributes.instantiate(); |
8799 | preview_environment->set_camera_attributes(camera_attributes); |
8800 | } |
8801 | Ref<Sky> sky; |
8802 | sky.instantiate(); |
8803 | sky_material.instantiate(); |
8804 | sky->set_material(sky_material); |
8805 | environment->set_sky(sky); |
8806 | environment->set_background(Environment::BG_SKY); |
8807 | |
8808 | _load_default_preview_settings(); |
8809 | _preview_settings_changed(); |
8810 | } |
8811 | clear(); // Make sure values are initialized. Will call _snap_update() for us. |
8812 | } |
8813 | Node3DEditor::~Node3DEditor() { |
8814 | memdelete(preview_node); |
8815 | if (preview_sun_dangling && preview_sun) { |
8816 | memdelete(preview_sun); |
8817 | } |
8818 | if (preview_env_dangling && preview_environment) { |
8819 | memdelete(preview_environment); |
8820 | } |
8821 | } |
8822 | |
8823 | void Node3DEditorPlugin::make_visible(bool p_visible) { |
8824 | if (p_visible) { |
8825 | spatial_editor->show(); |
8826 | spatial_editor->set_process(true); |
8827 | spatial_editor->set_physics_process(true); |
8828 | |
8829 | } else { |
8830 | spatial_editor->hide(); |
8831 | spatial_editor->set_process(false); |
8832 | spatial_editor->set_physics_process(false); |
8833 | } |
8834 | } |
8835 | |
8836 | void Node3DEditorPlugin::edit(Object *p_object) { |
8837 | spatial_editor->edit(Object::cast_to<Node3D>(p_object)); |
8838 | } |
8839 | |
8840 | bool Node3DEditorPlugin::handles(Object *p_object) const { |
8841 | return p_object->is_class("Node3D" ); |
8842 | } |
8843 | |
8844 | Dictionary Node3DEditorPlugin::get_state() const { |
8845 | return spatial_editor->get_state(); |
8846 | } |
8847 | |
8848 | void Node3DEditorPlugin::set_state(const Dictionary &p_state) { |
8849 | spatial_editor->set_state(p_state); |
8850 | } |
8851 | |
8852 | Vector3 Node3DEditor::snap_point(Vector3 p_target, Vector3 p_start) const { |
8853 | if (is_snap_enabled()) { |
8854 | p_target.x = Math::snap_scalar(0.0, get_translate_snap(), p_target.x); |
8855 | p_target.y = Math::snap_scalar(0.0, get_translate_snap(), p_target.y); |
8856 | p_target.z = Math::snap_scalar(0.0, get_translate_snap(), p_target.z); |
8857 | } |
8858 | return p_target; |
8859 | } |
8860 | |
8861 | bool Node3DEditor::is_gizmo_visible() const { |
8862 | if (selected) { |
8863 | return gizmo.visible && selected->is_transform_gizmo_visible(); |
8864 | } |
8865 | return gizmo.visible; |
8866 | } |
8867 | |
8868 | double Node3DEditor::get_translate_snap() const { |
8869 | double snap_value; |
8870 | if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
8871 | snap_value = snap_translate->get_text().to_float() / 10.0; |
8872 | } else { |
8873 | snap_value = snap_translate->get_text().to_float(); |
8874 | } |
8875 | |
8876 | return snap_value; |
8877 | } |
8878 | |
8879 | double Node3DEditor::get_rotate_snap() const { |
8880 | double snap_value; |
8881 | if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
8882 | snap_value = snap_rotate->get_text().to_float() / 3.0; |
8883 | } else { |
8884 | snap_value = snap_rotate->get_text().to_float(); |
8885 | } |
8886 | |
8887 | return snap_value; |
8888 | } |
8889 | |
8890 | double Node3DEditor::get_scale_snap() const { |
8891 | double snap_value; |
8892 | if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) { |
8893 | snap_value = snap_scale->get_text().to_float() / 2.0; |
8894 | } else { |
8895 | snap_value = snap_scale->get_text().to_float(); |
8896 | } |
8897 | |
8898 | return snap_value; |
8899 | } |
8900 | |
8901 | struct _GizmoPluginPriorityComparator { |
8902 | bool operator()(const Ref<EditorNode3DGizmoPlugin> &p_a, const Ref<EditorNode3DGizmoPlugin> &p_b) const { |
8903 | if (p_a->get_priority() == p_b->get_priority()) { |
8904 | return p_a->get_gizmo_name() < p_b->get_gizmo_name(); |
8905 | } |
8906 | return p_a->get_priority() > p_b->get_priority(); |
8907 | } |
8908 | }; |
8909 | |
8910 | struct _GizmoPluginNameComparator { |
8911 | bool operator()(const Ref<EditorNode3DGizmoPlugin> &p_a, const Ref<EditorNode3DGizmoPlugin> &p_b) const { |
8912 | return p_a->get_gizmo_name() < p_b->get_gizmo_name(); |
8913 | } |
8914 | }; |
8915 | |
8916 | void Node3DEditor::add_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin) { |
8917 | ERR_FAIL_NULL(p_plugin.ptr()); |
8918 | |
8919 | gizmo_plugins_by_priority.push_back(p_plugin); |
8920 | gizmo_plugins_by_priority.sort_custom<_GizmoPluginPriorityComparator>(); |
8921 | |
8922 | gizmo_plugins_by_name.push_back(p_plugin); |
8923 | gizmo_plugins_by_name.sort_custom<_GizmoPluginNameComparator>(); |
8924 | |
8925 | _update_gizmos_menu(); |
8926 | } |
8927 | |
8928 | void Node3DEditor::remove_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin) { |
8929 | gizmo_plugins_by_priority.erase(p_plugin); |
8930 | gizmo_plugins_by_name.erase(p_plugin); |
8931 | _update_gizmos_menu(); |
8932 | } |
8933 | |
8934 | Node3DEditorPlugin::Node3DEditorPlugin() { |
8935 | spatial_editor = memnew(Node3DEditor); |
8936 | spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
8937 | EditorNode::get_singleton()->get_main_screen_control()->add_child(spatial_editor); |
8938 | |
8939 | spatial_editor->hide(); |
8940 | } |
8941 | |
8942 | Node3DEditorPlugin::~Node3DEditorPlugin() { |
8943 | } |
8944 | |