WIP: Fix #116458: Added decay factor for flattening brushes. #118699

Draft
Raul Fernandez Hernandez wants to merge 87 commits from farsthary/blender:Fix-#116458-Sculpt-Clay-strip-sculpts-on-back-face-when-front-face-only-is-turned-on into blender-v4.1-release

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
10 changed files with 86 additions and 80 deletions
Showing only changes of commit 3c6bfce1bd - Show all commits

View File

@ -614,6 +614,7 @@ def brush_settings(layout, context, brush, popover=False):
sub = row.row()
sub.active = brush.use_plane_trim
sub.prop(brush, "plane_trim", slider=True, text="")
sub.prop(brush, "plane_trim_decay", slider=True, text="")
layout.separator()

View File

@ -8679,6 +8679,7 @@ class VIEW3D_PT_sculpt_context_menu(Panel):
if capabilities.has_plane_offset:
layout.prop(brush, "plane_offset", slider=True)
layout.prop(brush, "plane_trim", slider=True, text="Distance")
layout.prop(brush, "plane_trim_decay", slider=True, text="Decay")
if capabilities.has_height:
layout.prop(brush, "height", slider=True, text="Height")

View File

@ -1750,6 +1750,7 @@ void BKE_brush_debug_print_state(Brush *br)
BR_TEST(crease_pinch_factor, f);
BR_TEST(plane_trim, f);
BR_TEST(plane_trim_decay, f);
BR_TEST(texture_sample_bias, f);
BR_TEST(texture_overlay_alpha, d);

View File

@ -3050,12 +3050,21 @@ void SCULPT_calc_brush_plane(
}
}
int SCULPT_plane_trim(const blender::ed::sculpt_paint::StrokeCache *cache,
const Brush *brush,
const float val[3])
float SCULPT_plane_trim(const blender::ed::sculpt_paint::StrokeCache *cache,
const Brush *brush,
const float val[3])
{
return (!(brush->flag & BRUSH_PLANE_TRIM) ||
(dot_v3v3(val, val) <= cache->radius_squared * cache->plane_trim_squared));
const float lensq = dot_v3v3(val, val);
if (!(brush->flag & BRUSH_PLANE_TRIM) ||
(lensq <= cache->radius_squared * cache->plane_trim_squared))
{
return 1;
}
/* Workaround for https://projects.blender.org/blender/blender/issues/116458
apply a strong falloff based on the distance to the brush plane */
const float decay_rate = cache->plane_trim_decay;
return std::exp(-decay_rate * sqrtf(lensq));
}
int SCULPT_plane_point_side(const float co[3], const float plane[4])
@ -4211,6 +4220,7 @@ static void sculpt_update_cache_invariants(
cache->scale[2] = max_scale / ob->scale[2];
cache->plane_trim_squared = brush->plane_trim * brush->plane_trim;
cache->plane_trim_decay = brush->plane_trim_decay;
cache->flag = 0;

View File

@ -368,23 +368,19 @@ static void do_fill_brush_task(
closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co);
sub_v3_v3v3(val, intr, vd.co);
if (!SCULPT_plane_trim(ss->cache, brush, val)) {
continue;
}
const float trim_factor = SCULPT_plane_trim(ss->cache, brush, val);
auto_mask::node_update(automask_data, vd);
const float fade = bstrength * SCULPT_brush_strength_factor(ss,
brush,
vd.co,
sqrtf(test.dist),
vd.no,
vd.fno,
vd.mask,
vd.vertex,
thread_id,
&automask_data);
float fade = bstrength * SCULPT_brush_strength_factor(ss,
brush,
vd.co,
sqrtf(test.dist),
vd.no,
vd.fno,
vd.mask,
vd.vertex,
thread_id,
&automask_data);
fade *= trim_factor;
mul_v3_v3fl(proxy[vd.i], val, fade);
}
BKE_pbvh_vertex_iter_end;
@ -456,23 +452,19 @@ static void do_scrape_brush_task(
closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co);
sub_v3_v3v3(val, intr, vd.co);
if (!SCULPT_plane_trim(ss->cache, brush, val)) {
continue;
}
const float trim_factor = SCULPT_plane_trim(ss->cache, brush, val);
auto_mask::node_update(automask_data, vd);
const float fade = bstrength * SCULPT_brush_strength_factor(ss,
brush,
vd.co,
sqrtf(test.dist),
vd.no,
vd.fno,
vd.mask,
vd.vertex,
thread_id,
&automask_data);
float fade = bstrength * SCULPT_brush_strength_factor(ss,
brush,
vd.co,
sqrtf(test.dist),
vd.no,
vd.fno,
vd.mask,
vd.vertex,
thread_id,
&automask_data);
fade *= trim_factor;
mul_v3_v3fl(proxy[vd.i], val, fade);
}
BKE_pbvh_vertex_iter_end;
@ -703,22 +695,20 @@ static void do_flatten_brush_task(
sub_v3_v3v3(val, intr, vd.co);
if (SCULPT_plane_trim(ss->cache, brush, val)) {
auto_mask::node_update(automask_data, vd);
const float fade = bstrength * SCULPT_brush_strength_factor(ss,
brush,
vd.co,
sqrtf(test.dist),
vd.no,
vd.fno,
vd.mask,
vd.vertex,
thread_id,
&automask_data);
mul_v3_v3fl(proxy[vd.i], val, fade);
}
const float trim_factor = SCULPT_plane_trim(ss->cache, brush, val);
auto_mask::node_update(automask_data, vd);
float fade = bstrength * SCULPT_brush_strength_factor(ss,
brush,
vd.co,
sqrtf(test.dist),
vd.no,
vd.fno,
vd.mask,
vd.vertex,
thread_id,
&automask_data);
fade *= trim_factor;
mul_v3_v3fl(proxy[vd.i], val, fade);
}
BKE_pbvh_vertex_iter_end;
}
@ -948,10 +938,7 @@ static void do_clay_strips_brush_task(Object *ob,
closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co);
sub_v3_v3v3(val, intr, vd.co);
if (!SCULPT_plane_trim(ss->cache, brush, val)) {
continue;
}
const float trim_factor = SCULPT_plane_trim(ss->cache, brush, val);
auto_mask::node_update(automask_data, vd);
/* The normal from the vertices is ignored, it causes glitch with planes, see: #44390. */
@ -965,10 +952,7 @@ static void do_clay_strips_brush_task(Object *ob,
vd.vertex,
thread_id,
&automask_data);
/* Workaround for https://projects.blender.org/blender/blender/issues/116458
apply a strong falloff based on the distance to the brush plane */
const float decay_rate = 34.0f;
fade *= std::exp(-decay_rate * len_v3(val));
fade *= trim_factor;
mul_v3_v3fl(proxy[vd.i], val, fade);
}
BKE_pbvh_vertex_iter_end;

View File

@ -520,6 +520,7 @@ struct StrokeCache {
bool alt_smooth;
float plane_trim_squared;
float plane_trim_decay;
bool supports_gravity;
float3 true_gravity_direction;
@ -1048,9 +1049,9 @@ PBVHVertRef SCULPT_nearest_vertex_get(Object *ob,
bool use_original);
int SCULPT_plane_point_side(const float co[3], const float plane[4]);
int SCULPT_plane_trim(const blender::ed::sculpt_paint::StrokeCache *cache,
const Brush *brush,
const float val[3]);
float SCULPT_plane_trim(const blender::ed::sculpt_paint::StrokeCache *cache,
const Brush *brush,
const float val[3]);
/**
* Handles clipping against a mirror modifier and #SCULPT_LOCK_X/Y/Z axis flags.
*/

View File

@ -161,26 +161,23 @@ static void do_multiplane_scrape_brush_task(Object *ob,
}
sub_v3_v3v3(val, intr, vd.co);
if (!SCULPT_plane_trim(ss->cache, brush, val)) {
continue;
}
const float trim_factor = SCULPT_plane_trim(ss->cache, brush, val);
auto_mask::node_update(automask_data, vd);
/* Deform the local space along the Y axis to avoid artifacts on curved strokes. */
/* This produces a not round brush tip. */
local_co[1] *= 2.0f;
const float fade = bstrength * SCULPT_brush_strength_factor(ss,
brush,
vd.co,
len_v3(local_co),
vd.no,
vd.fno,
vd.mask,
vd.vertex,
thread_id,
&automask_data);
float fade = bstrength * SCULPT_brush_strength_factor(ss,
brush,
vd.co,
len_v3(local_co),
vd.no,
vd.fno,
vd.mask,
vd.vertex,
thread_id,
&automask_data);
fade *= trim_factor;
mul_v3_v3fl(proxy[vd.i], val, fade);
}
BKE_pbvh_vertex_iter_end;

View File

@ -44,6 +44,7 @@
/* How far above or below the plane that is found by averaging the faces. */ \
.plane_offset = 0.0f, \
.plane_trim = 0.5f, \
.plane_trim_decay = 0, \
farsthary marked this conversation as resolved Outdated

Nitpick: 0.0f instead of 0 to remain consistent with the rest of the file.

Nitpick: `0.0f` instead of `0` to remain consistent with the rest of the file.
.clone.alpha = 0.5f, \
.normal_weight = 0.0f, \
.fill_threshold = 0.2f, \

View File

@ -291,7 +291,7 @@ typedef struct Brush {
char gpencil_weight_tool;
/** Active curves sculpt tool (#eBrushCurvesSculptTool). */
char curves_sculpt_tool;
char _pad1[5];
char _pad1;
float autosmooth_factor;
@ -306,6 +306,7 @@ typedef struct Brush {
float wet_paint_radius_factor;
float plane_trim;
float plane_trim_decay;
/** Affectable height of brush (layer height for layer tool, i.e.). */
float height;

View File

@ -3011,6 +3011,15 @@ static void rna_def_brush(BlenderRNA *brna)
"If a vertex is further away from offset plane than this, then it is not affected");
RNA_def_property_update(prop, 0, "rna_Brush_update");
prop = RNA_def_property(srna, "plane_trim_decay", PROP_FLOAT, PROP_DISTANCE);
farsthary marked this conversation as resolved Outdated

Based on your comment in the chat, I assume you wanted this to be a raw value instead of distance, the subtype here should be PROP_NONE then instead of PROP_DISTANCE

Based on your comment in the chat, I assume you wanted this to be a raw value instead of distance, the subtype here should be `PROP_NONE` then instead of `PROP_DISTANCE`
RNA_def_property_float_sdna(prop, nullptr, "plane_trim_decay");
RNA_def_property_range(prop, 0, 50.0f);
RNA_def_property_ui_text(
prop,
"Plane Trim Decay",
"If a vertex is away from offset plane, its deformation exponentially attenuates");
farsthary marked this conversation as resolved Outdated

This description is a bit confusing to me from a user perspective. I don't have a good suggestion for rewording it, but I think making it clear that this value is the strength (rate?) of the falloff would be clearer.

This description is a bit confusing to me from a user perspective. I don't have a good suggestion for rewording it, but I think making it clear that this value is the strength (rate?) of the falloff would be clearer.
RNA_def_property_update(prop, 0, "rna_Brush_update");
prop = RNA_def_property(srna, "height", PROP_FLOAT, PROP_DISTANCE);
RNA_def_property_float_sdna(prop, nullptr, "height");
RNA_def_property_float_default(prop, 0.5f);