Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tool scripts to alter transform of Node3DEditorViewport camera #93503

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
@@ -590,7 +590,8 @@ void Node3DEditorViewport::_update_camera(real_t p_interp_delta) {
}

if (!equal || p_interp_delta == 0 || is_orthogonal != orthogonal) {
camera->set_global_transform(to_camera_transform(camera_cursor));
last_camera_transform = to_camera_transform(camera_cursor);
camera->set_global_transform(last_camera_transform);

if (orthogonal) {
float half_fov = Math::deg_to_rad(get_fov()) / 2.0;
@@ -2908,6 +2909,11 @@ void Node3DEditorViewport::_notification(int p_what) {
}
}

if (_camera_moved_externally()) {
// If camera moved after this plugin last set it, presumably a tool script has moved it, accept the new camera transform as the cursor position.
_apply_camera_transform_to_cursor();
}

_update_camera(delta);

const HashMap<Node *, Object *> &selection = editor_selection->get_selection();
@@ -3376,6 +3382,19 @@ void Node3DEditorViewport::_draw() {
}
}

bool Node3DEditorViewport::_camera_moved_externally() {
Transform3D t = camera->get_global_transform();
return !t.is_equal_approx(last_camera_transform);
}

void Node3DEditorViewport::_apply_camera_transform_to_cursor() {
// Effectively the reverse of to_camera_transform, use camera transform to set cursor position and rotation.
Transform3D camera_transform = camera->get_camera_transform();
cursor.pos = camera_transform.origin;
cursor.x_rot = -camera_transform.basis.get_euler().x;
cursor.y_rot = -camera_transform.basis.get_euler().y;
}

void Node3DEditorViewport::_menu_option(int p_option) {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
switch (p_option) {
5 changes: 5 additions & 0 deletions editor/plugins/node_3d_editor_plugin.h
Original file line number Diff line number Diff line change
@@ -445,6 +445,11 @@ class Node3DEditorViewport : public Control {
Transform3D to_camera_transform(const Cursor &p_cursor) const;
void _draw();

// These allow tool scripts to set the 3D cursor location by updating the camera transform.
Transform3D last_camera_transform;
bool _camera_moved_externally();
void _apply_camera_transform_to_cursor();

void _surface_mouse_enter();
void _surface_mouse_exit();
void _surface_focus_enter();