Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add a new AABB-based focusing method.

JekSun979d671acdf649
diff --git a/editor/scene/3d/node_3d_editor_plugin.cpp b/editor/scene/3d/node_3d_editor_plugin.cpp
index f2a65f023e2..cd508ba3a35 100644
--- a/editor/scene/3d/node_3d_editor_plugin.cpp
+++ b/editor/scene/3d/node_3d_editor_plugin.cpp
@@ -3644,7 +3644,8 @@ Node3DEditor::Node3DEditor() {
ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal", TTRC("Switch Perspective/Orthogonal View"), Key::KP_5);
ED_SHORTCUT("spatial_editor/insert_anim_key", TTRC("Insert Animation Key"), Key::K);
ED_SHORTCUT("spatial_editor/focus_origin", TTRC("Focus Origin"), Key::O);
- ED_SHORTCUT("spatial_editor/focus_selection", TTRC("Focus Selection"), Key::F);
+ ED_SHORTCUT("spatial_editor/focus_selection", TTRC("Focus Selection"), KeyModifierMask::CMD_OR_CTRL + Key::F);
+ ED_SHORTCUT("spatial_editor/focus_aabb", TTRC("Focus and Frame Selection"), Key::F);
ED_SHORTCUT_ARRAY("spatial_editor/align_transform_with_view", TTRC("Align Transform with View"),
{ int32_t(KeyModifierMask::ALT | KeyModifierMask::CTRL | Key::KP_0),
int32_t(KeyModifierMask::ALT | KeyModifierMask::CTRL | Key::M),
diff --git a/editor/scene/3d/node_3d_editor_viewport.cpp b/editor/scene/3d/node_3d_editor_viewport.cpp
index a191fcc7ce2..f1c562d7ba2 100644
--- a/editor/scene/3d/node_3d_editor_viewport.cpp
+++ b/editor/scene/3d/node_3d_editor_viewport.cpp
@@ -2914,6 +2914,12 @@ void Node3DEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
if (ED_IS_SHORTCUT("spatial_editor/focus_selection", event_mod)) {
_menu_option(VIEW_CENTER_TO_SELECTION);
times_focused_consecutively += 1;
+ follow_mode_uses_aabb = false;
+ }
+ if (ED_IS_SHORTCUT("spatial_editor/focus_aabb", event_mod)) {
+ _menu_option(VIEW_CENTER_TO_AABB);
+ times_focused_consecutively += 1;
+ follow_mode_uses_aabb = true;
}
if (ED_IS_SHORTCUT("spatial_editor/align_transform_with_view", event_mod)) {
_menu_option(VIEW_ALIGN_TRANSFORM_WITH_VIEW);
@@ -3589,7 +3595,11 @@ void Node3DEditorViewport::_notification(int p_what) {
follow_mode->set_text(vformat(TTR("Following %s"), focused_node->get_name()));
follow_mode->set_button_icon(get_editor_theme_icon(focused_node->get_class()));
follow_mode->show();
- focus_selection();
+ if (follow_mode_uses_aabb) {
+ focus_aabb();
+ } else {
+ focus_selection();
+ }
} else {
_disable_follow_mode();
}
@@ -4400,6 +4410,10 @@ void Node3DEditorViewport::_menu_option(int p_option) {
case VIEW_CENTER_TO_SELECTION: {
focus_selection();
+ } break;
+ case VIEW_CENTER_TO_AABB: {
+ focus_aabb();
+
} break;
case VIEW_ALIGN_TRANSFORM_WITH_VIEW: {
if (!get_selected_count()) {
@@ -5421,6 +5435,66 @@ void Node3DEditorViewport::focus_selection() {
view_3d_controller->cursor.pos_z = center.z;
}
+void Node3DEditorViewport::focus_aabb() {
+ Vector3 center;
+ AABB combined_aabb;
+ bool aabb_valid = false;
+ float required_distance = 0.0f;
+
+ const List<Node *> &selection = editor_selection->get_top_selected_node_list();
+ if (selection.is_empty()) {
+ return;
+ }
+
+ focused_node_id = selection.front()->get()->get_instance_id();
+
+ LocalVector<Node *> stack;
+ stack.reserve(selection.size());
+ for (Node *node : selection) {
+ stack.push_back(node);
+ }
+
+ while (!stack.is_empty()) {
+ Node *node = stack[stack.size() - 1];
+ stack.resize(stack.size() - 1);
+
+ VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node);
+ if (vi) {
+ AABB local_aabb = vi->get_aabb();
+ if (local_aabb.has_volume()) {
+ AABB global_aabb = vi->get_global_transform().xform(local_aabb);
+ if (!aabb_valid) {
+ combined_aabb = global_aabb;
+ aabb_valid = true;
+ } else {
+ combined_aabb = combined_aabb.merge(global_aabb);
+ }
+ }
+ }
+
+ for (Node *child : node->iterate_children()) {
+ stack.push_back(child);
+ }
+ }
+
+ if (aabb_valid) {
+ center = combined_aabb.get_center();
+
+ float aabb_radius = combined_aabb.get_size().length() * 0.5f;
+ float fov_rad = Math::deg_to_rad(get_fov());
+ required_distance = aabb_radius / Math::tan(fov_rad * 0.5f);
+ required_distance *= 1.2f;
+ required_distance = CLAMP(required_distance, get_znear() * 2.0f, get_zfar() * 0.8f);
+
+ view_3d_controller->cursor.pos_x = center.x;
+ view_3d_controller->cursor.pos_y = center.y;
+ view_3d_controller->cursor.pos_z = center.z;
+ view_3d_controller->cursor.distance = required_distance;
+ } else {
+ focus_selection();
+ }
+}
+
void Node3DEditorViewport::assign_pending_data_pointers(Node3D *p_preview_node, AABB *p_preview_bounds, AcceptDialog *p_accept) {
preview_node = p_preview_node;
preview_bounds = p_preview_bounds;
@@ -6920,6 +6994,7 @@ Node3DEditorViewport::Node3DEditorViewport(Node3DEditor *p_spatial_editor, int p
view_display_menu->get_popup()->add_separator();
view_display_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_origin"), VIEW_CENTER_TO_ORIGIN);
view_display_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
+ view_display_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_aabb"), VIEW_CENTER_TO_AABB);
view_display_menu->get_popup()->set_item_tooltip(-1, TTR("Press Focus Selection twice to start following the selection as it moves. Press it yet another time to stop following the selection."));
view_display_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_transform_with_view"), VIEW_ALIGN_TRANSFORM_WITH_VIEW);
view_display_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_rotation_with_view"), VIEW_ALIGN_ROTATION_WITH_VIEW);
diff --git a/editor/scene/3d/node_3d_editor_viewport.h b/editor/scene/3d/node_3d_editor_viewport.h
index 129987e031c..2bdbc16b1db 100644
--- a/editor/scene/3d/node_3d_editor_viewport.h
+++ b/editor/scene/3d/node_3d_editor_viewport.h
@@ -157,6 +157,7 @@ class Node3DEditorViewport : public Control {
VIEW_REAR,
VIEW_CENTER_TO_ORIGIN,
VIEW_CENTER_TO_SELECTION,
+ VIEW_CENTER_TO_AABB,
VIEW_ALIGN_TRANSFORM_WITH_VIEW,
VIEW_ALIGN_ROTATION_WITH_VIEW,
VIEW_PERSPECTIVE,
@@ -471,6 +472,7 @@ private:
bool previewing_camera = false;
bool previewing_cinema = false;
int times_focused_consecutively = 0;
+ bool follow_mode_uses_aabb = false;
bool pilot_preview_enabled = false;
bool pilot_undo_session_active = false;
@@ -563,6 +565,7 @@ public:
Point2 point_to_screen(const Vector3 &p_point);
void focus_selection();
+ void focus_aabb();
void assign_pending_data_pointers(
Node3D *p_preview_node,