diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2022-11-02 14:15:54 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2022-11-02 14:15:54 +0100 |
commit | df26d11b5d833666a3606dece41fb9cde16c197c (patch) | |
tree | 0a61bb4997f55ebb68c3f0da88b9379dde1fe913 | |
parent | a30ed00fb1c5226b20655d704d56e11ce9c606e0 (diff) | |
parent | 877cb1cbc0bfc9428fb4b9f2cd61a7b23e6425ce (diff) |
Merge pull request #68020 from timothyqiu/3d-drag-instance
Improve dragging scene into 3D viewport
-rw-r--r-- | editor/plugins/node_3d_editor_plugin.cpp | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index 5309a028a9..3923d25734 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -2664,7 +2664,7 @@ void Node3DEditorViewport::_notification(int p_what) { return; } if (preview_node->is_inside_tree()) { - preview_node_pos = _get_instance_position(preview_node_viewport_pos); + preview_node_pos = spatial_editor->snap_point(_get_instance_position(preview_node_viewport_pos)); Transform3D preview_gl_transform = Transform3D(Basis(), preview_node_pos); preview_node->set_global_transform(preview_gl_transform); if (!preview_node->is_visible()) { @@ -3748,24 +3748,45 @@ void Node3DEditorViewport::assign_pending_data_pointers(Node3D *p_preview_node, Vector3 Node3DEditorViewport::_get_instance_position(const Point2 &p_pos) const { const float MAX_DISTANCE = 50.0; + const float FALLBACK_DISTANCE = 5.0; Vector3 world_ray = _get_ray(p_pos); Vector3 world_pos = _get_ray_pos(p_pos); - Vector3 point = world_pos + world_ray * MAX_DISTANCE; - PhysicsDirectSpaceState3D *ss = get_tree()->get_root()->get_world_3d()->get_direct_space_state(); PhysicsDirectSpaceState3D::RayParameters ray_params; ray_params.from = world_pos; - ray_params.to = world_pos + world_ray * MAX_DISTANCE; + ray_params.to = world_pos + world_ray * camera->get_far(); PhysicsDirectSpaceState3D::RayResult result; if (ss->intersect_ray(ray_params, result)) { - point = result.position; + return result.position; + } + + const bool is_orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL; + + // The XZ plane. + Vector3 intersection; + Plane plane(Vector3(0, 1, 0)); + if (plane.intersects_ray(world_pos, world_ray, &intersection)) { + if (is_orthogonal || world_pos.distance_to(intersection) <= MAX_DISTANCE) { + return intersection; + } + } + + // Plane facing the camera using fallback distance. + if (is_orthogonal) { + plane = Plane(world_ray, cursor.pos - world_ray * (cursor.distance - FALLBACK_DISTANCE)); + } else { + plane = Plane(world_ray, world_pos + world_ray * FALLBACK_DISTANCE); + } + if (plane.intersects_ray(world_pos, world_ray, &intersection)) { + return intersection; } - return point; + // Not likely, but just in case... + return world_pos + world_ray * FALLBACK_DISTANCE; } AABB Node3DEditorViewport::_calculate_spatial_bounds(const Node3D *p_parent, bool p_exclude_top_level_transform) { @@ -4045,7 +4066,7 @@ bool Node3DEditorViewport::_create_instance(Node *parent, String &path, const Po gl_transform = parent_node3d->get_global_gizmo_transform(); } - gl_transform.origin = spatial_editor->snap_point(preview_node_pos); + gl_transform.origin = preview_node_pos; gl_transform.basis *= node3d->get_transform().basis; editor_data->get_undo_redo()->add_do_method(instantiated_scene, "set_global_transform", gl_transform); |