Browse Source

Merge pull request #93503 from Geekotron/viewport_accept_cam_change_from_tool

Allow tool scripts to alter transform of Node3DEditorViewport camera
Rémi Verschelde 8 months ago
parent
commit
274fc9858c
2 changed files with 25 additions and 1 deletions
  1. 20 1
      editor/plugins/node_3d_editor_plugin.cpp
  2. 5 0
      editor/plugins/node_3d_editor_plugin.h

+ 20 - 1
editor/plugins/node_3d_editor_plugin.cpp

@@ -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 - 0
editor/plugins/node_3d_editor_plugin.h

@@ -445,6 +445,11 @@ private:
 	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();