Fix #127077: Edit voxel size op unpredictable with large/small meshes

If voxel sizes were big, code could run into clamping at 1.0f, making it
look like nothing was happening, also even though the "step size" was
taking into account the initial voxels size (only in relative mode,
absolute mode was, well... absolute, no matter what object this was
happening on, also see 5946ea938a).

So previously, a reasonable voxels size could not achieved using this
operator for e.g. a terrain-size mesh.

Now code allows for bigger/smaller meshes to behave predictably.
This is done by storing a reasonable min and max voxel size (this is
taken from the bounding plane which is also displayed to the user),
making the max the length of the longer side of the bounding plane and
the min a fraction of it (based on `VOXEL_SIZE_EDIT_MAX_GRIDS_LINES` --
smaller voxels would not display anyways...)

Based on the above, we can have a reasonable range to base the change on
which is done moving the mouse.
Because this is more predictable than before, we can even remove the
`relative` mode (dont think it makes too much sense now anymore).

Pull Request: https://projects.blender.org/blender/blender/pulls/127109
This commit is contained in:
Philipp Oeser
2024-09-05 16:31:05 +02:00
committed by Philipp Oeser
parent 5e63435e74
commit 4221f827cf

View File

@@ -199,12 +199,13 @@ struct VoxelSizeEditCustomData {
float init_mval[2];
float slow_mval[2];
bool relative_mode;
bool slow_mode;
float init_voxel_size;
float slow_voxel_size;
float voxel_size;
float voxel_size_min;
float voxel_size_max;
float preview_plane[4][3];
@@ -361,7 +362,6 @@ static void voxel_size_edit_update_header(wmOperator *op, bContext *C)
status.item(IFACE_("Confirm"), ICON_EVENT_RETURN, ICON_MOUSE_LMB);
status.item(IFACE_("Cancel"), ICON_EVENT_ESC, ICON_MOUSE_RMB);
status.item(IFACE_("Change Size"), ICON_MOUSE_MOVE);
status.item_bool(IFACE_("Relative Mode"), cd->relative_mode, ICON_EVENT_CTRL);
status.item_bool(IFACE_("Precision Mode"), cd->slow_mode, ICON_EVENT_SHIFT);
}
@@ -403,18 +403,8 @@ static int voxel_size_edit_modal(bContext *C, wmOperator *op, const wmEvent *eve
d = cd->slow_mval[0] - mval[0];
}
if (event->modifier & KM_CTRL) {
/* Multiply d by the initial voxel size to prevent uncontrollable speeds when using low voxel
* sizes. */
/* When the voxel size is slower, it needs more precision. */
d = d * min_ff(pow2f(cd->init_voxel_size), 0.1f) * 0.05f;
cd->relative_mode = true;
}
else {
/* Linear mode, enables jumping to any voxel size. */
d = d * 0.0005f;
cd->relative_mode = false;
}
d *= cd->voxel_size_min * 0.25f;
if (cd->slow_mode) {
cd->voxel_size = cd->slow_voxel_size + d * 0.05f;
}
@@ -432,7 +422,8 @@ static int voxel_size_edit_modal(bContext *C, wmOperator *op, const wmEvent *eve
cd->slow_voxel_size = 0.0f;
}
cd->voxel_size = clamp_f(cd->voxel_size, 0.0001f, 1.0f);
cd->voxel_size = clamp_f(
cd->voxel_size, max_ff(cd->voxel_size_min, 0.0001f), cd->voxel_size_max);
ED_region_tag_redraw(region);
@@ -457,7 +448,6 @@ static int voxel_size_edit_invoke(bContext *C, wmOperator *op, const wmEvent *ev
cd->init_mval[1] = event->mval[1];
cd->init_voxel_size = mesh->remesh_voxel_size;
cd->voxel_size = mesh->remesh_voxel_size;
cd->relative_mode = false;
cd->slow_mode = false;
op->customdata = cd;
@@ -516,6 +506,13 @@ static int voxel_size_edit_invoke(bContext *C, wmOperator *op, const wmEvent *ev
}
}
/* Cap the max/min voxel size based on the point where we cant visually display any more info
* with grid lines. */
cd->voxel_size_max = max_ff(len_v3v3(cd->preview_plane[1], cd->preview_plane[0]),
len_v3v3(cd->preview_plane[3], cd->preview_plane[0])) *
0.5f;
cd->voxel_size_min = cd->voxel_size_max / VOXEL_SIZE_EDIT_MAX_GRIDS_LINES;
/* Matrix calculation to position the text in 3D space. */
float text_pos[3];
float scale_mat[4][4];