From a8f4affb2ead7f49996f54a8e6a14be5a48db785 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 20 Nov 2020 16:45:06 +1100 Subject: Add Object Tool: use the closest view-aligned axis as a fallback When using surface orientation and nothing is under the mouse-cursor, pick the axis that's closest to the view's Z axis. Now by default default, drawing into empty spaces wont use a plane orthogonal to the view. --- .../editors/space_view3d/view3d_placement.c | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/source/blender/editors/space_view3d/view3d_placement.c b/source/blender/editors/space_view3d/view3d_placement.c index 85388819f29..bdf795a33a1 100644 --- a/source/blender/editors/space_view3d/view3d_placement.c +++ b/source/blender/editors/space_view3d/view3d_placement.c @@ -152,6 +152,33 @@ struct InteractivePlaceData { /** \name Internal Utilities * \{ */ +/** + * Re-order \a mat so \a axis_align uses it's own axis which is closest to \a v. + */ +static bool mat3_align_axis_to_v3(float mat[3][3], const int axis_align, const float v[3]) +{ + float dot_best = -1.0f; + int axis_found = axis_align; + for (int i = 0; i < 3; i++) { + const float dot_test = fabsf(dot_v3v3(mat[i], v)); + if (dot_test > dot_best) { + dot_best = dot_test; + axis_found = i; + } + } + + if (axis_align != axis_found) { + float tmat[3][3]; + copy_m3_m3(tmat, mat); + const int offset = mod_i(axis_found - axis_align, 3); + for (int i = 0; i < 3; i++) { + copy_v3_v3(mat[i], tmat[(i + offset) % 3]); + } + return true; + } + return false; +} + /* On-screen snap distance. */ #define MVAL_MAX_PX_DIST 12.0f @@ -656,6 +683,7 @@ static void view3d_interactive_add_calc_plane(bContext *C, } if (plane_orient == PLACE_ORIENT_SURFACE) { + bool found_surface_or_normal = false; float matrix_orient_surface[3][3]; /* Use the snap normal as a fallback in case the cursor isn't over a surface @@ -673,6 +701,12 @@ static void view3d_interactive_add_calc_plane(bContext *C, use_normal_fallback ? normal_fallback : NULL, matrix_orient_surface)) { copy_m3_m3(r_matrix_orient, matrix_orient_surface); + found_surface_or_normal = true; + } + + if (!found_surface_or_normal) { + /* Drawing into empty mspace, draw onto the plane most aligned to the view direction. */ + mat3_align_axis_to_v3(r_matrix_orient, plane_axis, rv3d->viewinv[2]); } } -- cgit v1.2.3