diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2017-10-04 20:18:05 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2017-10-04 20:18:05 +0300 |
commit | 63c472b4f9f7eae787bf4ce36160a28a1777bc76 (patch) | |
tree | 0e3144ddeacb83b7ff2a769af178824b1dea5563 /source/blender/editors/sculpt_paint | |
parent | 9422db7796e17bc23a9544d44f2a2e00a9b7e775 (diff) | |
parent | 296757804cc2e97a3d95b4e44a6cc5d508e1c345 (diff) |
Merge branch 'master' into blender2.8
Diffstat (limited to 'source/blender/editors/sculpt_paint')
6 files changed, 189 insertions, 144 deletions
diff --git a/source/blender/editors/sculpt_paint/paint_intern.h b/source/blender/editors/sculpt_paint/paint_intern.h index 83246f7cc7f..b3b7d40391a 100644 --- a/source/blender/editors/sculpt_paint/paint_intern.h +++ b/source/blender/editors/sculpt_paint/paint_intern.h @@ -241,8 +241,8 @@ void paint_calc_redraw_planes(float planes[4][4], const struct rcti *screen_rect); float paint_calc_object_space_radius(struct ViewContext *vc, const float center[3], float pixel_radius); -float paint_get_tex_pixel(struct MTex *mtex, float u, float v, struct ImagePool *pool, int thread); -void paint_get_tex_pixel_col(struct MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert, struct ColorSpace *colorspace); +float paint_get_tex_pixel(const struct MTex *mtex, float u, float v, struct ImagePool *pool, int thread); +void paint_get_tex_pixel_col(const struct MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert, struct ColorSpace *colorspace); void paint_sample_color(struct bContext *C, struct ARegion *ar, int x, int y, bool texpaint_proj, bool palette); diff --git a/source/blender/editors/sculpt_paint/paint_utils.c b/source/blender/editors/sculpt_paint/paint_utils.c index c432990d4c3..bb69a5d3db9 100644 --- a/source/blender/editors/sculpt_paint/paint_utils.c +++ b/source/blender/editors/sculpt_paint/paint_utils.c @@ -168,7 +168,7 @@ float paint_calc_object_space_radius(ViewContext *vc, const float center[3], return len_v3(delta) / scale; } -float paint_get_tex_pixel(MTex *mtex, float u, float v, struct ImagePool *pool, int thread) +float paint_get_tex_pixel(const MTex *mtex, float u, float v, struct ImagePool *pool, int thread) { float intensity, rgba[4]; float co[3] = {u, v, 0.0f}; @@ -179,7 +179,7 @@ float paint_get_tex_pixel(MTex *mtex, float u, float v, struct ImagePool *pool, return intensity; } -void paint_get_tex_pixel_col(MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert_to_linear, struct ColorSpace *colorspace) +void paint_get_tex_pixel_col(const MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert_to_linear, struct ColorSpace *colorspace) { float co[3] = {u, v, 0.0f}; int hasrgb; diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c b/source/blender/editors/sculpt_paint/paint_vertex.c index 1df6030bb10..5d87030bc67 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.c +++ b/source/blender/editors/sculpt_paint/paint_vertex.c @@ -149,7 +149,7 @@ static bool vwpaint_use_normal(const VPaint *vp) } -static void defweight_prev_restore_or_init(MDeformVert *dvert_prev, MDeformVert *dvert_curr, int index) +static MDeformVert *defweight_prev_init(MDeformVert *dvert_prev, MDeformVert *dvert_curr, int index) { MDeformVert *dv_curr = &dvert_curr[index]; MDeformVert *dv_prev = &dvert_prev[index]; @@ -157,9 +157,7 @@ static void defweight_prev_restore_or_init(MDeformVert *dvert_prev, MDeformVert dv_prev->flag = 0; defvert_copy(dv_prev, dv_curr); } - else { - defvert_copy(dv_curr, dv_prev); - } + return dv_prev; } /* check if we can do partial updates and have them draw realtime @@ -242,11 +240,10 @@ int weight_paint_poll(bContext *C) return 0; } -static VPaint *new_vpaint(int wpaint) +static VPaint *new_vpaint(void) { VPaint *vp = MEM_callocN(sizeof(VPaint), "VPaint"); - vp->flag = (wpaint) ? 0 : VP_FLAG_SPRAY; vp->paint.flags |= PAINT_SHOW_BRUSH; return vp; @@ -263,18 +260,18 @@ uint vpaint_get_current_col(Scene *scene, VPaint *vp) /* wpaint has 'wpaint_blend' */ static uint vpaint_blend( - VPaint *vp, uint color_curr, uint color_orig, + const VPaint *vp, uint color_curr, uint color_orig, uint color_paint, const int alpha_i, /* pre scaled from [0-1] --> [0-255] */ const int brush_alpha_value_i) { - Brush *brush = BKE_paint_brush(&vp->paint); + const Brush *brush = vp->paint.brush; const int tool = brush->vertexpaint_tool; uint color_blend = ED_vpaint_blend_tool(tool, color_curr, color_paint, alpha_i); - /* if no spray, clip color adding with colorig & orig alpha */ - if ((vp->flag & VP_FLAG_SPRAY) == 0) { + /* if no accumulate, clip color adding with colorig & orig alpha */ + if ((brush->flag & BRUSH_ACCUMULATE) == 0) { uint color_test, a; char *cp, *ct, *co; @@ -384,12 +381,12 @@ static float calc_vp_alpha_col_dl( /* vpaint has 'vpaint_blend' */ static float wpaint_blend( - VPaint *wp, float weight, + const VPaint *wp, float weight, const float alpha, float paintval, const float UNUSED(brush_alpha_value), const short do_flip) { - Brush *brush = BKE_paint_brush(&wp->paint); + const Brush *brush = wp->paint.brush; int tool = brush->vertexpaint_tool; if (do_flip) { @@ -414,6 +411,16 @@ static float wpaint_blend( return weight; } +static float wpaint_clamp_monotonic(float oldval, float curval, float newval) +{ + if (newval < oldval) + return MIN2(newval, curval); + else if (newval > oldval) + return MAX2(newval, curval); + else + return newval; +} + /* ----------------------------------------------------- */ static void do_weight_paint_normalize_all(MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap) @@ -699,7 +706,7 @@ typedef struct WeightPaintInfo { static void do_weight_paint_vertex_single( /* vars which remain the same for every vert */ - VPaint *wp, Object *ob, const WeightPaintInfo *wpi, + const VPaint *wp, Object *ob, const WeightPaintInfo *wpi, /* vars which change on each stroke */ const uint index, float alpha, float paintweight) { @@ -708,6 +715,7 @@ static void do_weight_paint_vertex_single( bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0; MDeformWeight *dw; + float weight_prev; /* mirror vars */ int index_mirr; @@ -731,14 +739,6 @@ static void do_weight_paint_vertex_single( index_mirr = vgroup_mirr = -1; } - if ((wp->flag & VP_FLAG_SPRAY) == 0) { - struct MDeformVert *dvert_prev = ob->sculpt->mode.wpaint.dvert_prev; - defweight_prev_restore_or_init(dvert_prev, me->dvert, index); - if (index_mirr != -1) { - defweight_prev_restore_or_init(dvert_prev, me->dvert, index_mirr); - } - } - if (wp->flag & VP_FLAG_VGROUP_RESTRICT) { dw = defvert_find_index(dv, wpi->active.index); } @@ -783,14 +783,29 @@ static void do_weight_paint_vertex_single( dw_mirr = NULL; } + if ((wp->paint.brush->flag & BRUSH_ACCUMULATE) == 0) { + MDeformVert *dvert_prev = ob->sculpt->mode.wpaint.dvert_prev; + MDeformVert *dv_prev = defweight_prev_init(dvert_prev, me->dvert, index); + if (index_mirr != -1) { + defweight_prev_init(dvert_prev, me->dvert, index_mirr); + } + + weight_prev = defvert_find_weight(dv_prev, wpi->active.index); + } + else { + weight_prev = dw->weight; + } + /* If there are no normalize-locks or multipaint, * then there is no need to run the more complicated checks */ { - dw->weight = wpaint_blend( - wp, dw->weight, alpha, paintweight, + float new_weight = wpaint_blend( + wp, weight_prev, alpha, paintweight, wpi->brush_alpha_value, wpi->do_flip); + dw->weight = wpaint_clamp_monotonic(weight_prev, dw->weight, new_weight); + /* WATCH IT: take care of the ordering of applying mirror -> normalize, * can give wrong results [#26193], least confusing if normalize is done last */ @@ -848,7 +863,7 @@ static void do_weight_paint_vertex_single( static void do_weight_paint_vertex_multi( /* vars which remain the same for every vert */ - VPaint *wp, Object *ob, const WeightPaintInfo *wpi, + const VPaint *wp, Object *ob, const WeightPaintInfo *wpi, /* vars which change on each stroke */ const uint index, float alpha, float paintweight) { @@ -861,7 +876,7 @@ static void do_weight_paint_vertex_multi( MDeformVert *dv_mirr = NULL; /* weights */ - float curw, neww, change, curw_mirr, change_mirr; + float curw, oldw, neww, change, curw_mirr, change_mirr; /* from now on we can check if mirrors enabled if this var is -1 and not bother with the flag */ if (me->editflag & ME_EDIT_MIRROR_X) { @@ -875,14 +890,6 @@ static void do_weight_paint_vertex_multi( } } - if ((wp->flag & VP_FLAG_SPRAY) == 0) { - struct MDeformVert *dvert_prev = ob->sculpt->mode.wpaint.dvert_prev; - defweight_prev_restore_or_init(dvert_prev, me->dvert, index); - if (index_mirr != -1) { - defweight_prev_restore_or_init(dvert_prev, me->dvert, index_mirr); - } - } - /* compute weight change by applying the brush to average or sum of group weights */ curw = BKE_defvert_multipaint_collective_weight( dv, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize); @@ -892,7 +899,22 @@ static void do_weight_paint_vertex_multi( return; } - neww = wpaint_blend(wp, curw, alpha, paintweight, wpi->brush_alpha_value, wpi->do_flip); + if ((wp->paint.brush->flag & BRUSH_ACCUMULATE) == 0) { + MDeformVert *dvert_prev = ob->sculpt->mode.wpaint.dvert_prev; + MDeformVert *dv_prev = defweight_prev_init(dvert_prev, me->dvert, index); + if (index_mirr != -1) { + defweight_prev_init(dvert_prev, me->dvert, index_mirr); + } + + oldw = BKE_defvert_multipaint_collective_weight( + dv_prev, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize); + } + else { + oldw = curw; + } + + neww = wpaint_blend(wp, oldw, alpha, paintweight, wpi->brush_alpha_value, wpi->do_flip); + neww = wpaint_clamp_monotonic(oldw, curw, neww); change = neww / curw; @@ -946,7 +968,7 @@ static void do_weight_paint_vertex_multi( static void do_weight_paint_vertex( /* vars which remain the same for every vert */ - VPaint *wp, Object *ob, const WeightPaintInfo *wpi, + const VPaint *wp, Object *ob, const WeightPaintInfo *wpi, /* vars which change on each stroke */ const uint index, float alpha, float paintweight) { @@ -1008,7 +1030,7 @@ static void vertex_paint_init_session_data(const ToolSettings *ts, Object *ob) /* Create average brush arrays */ if (ob->mode == OB_MODE_VERTEX_PAINT) { - if ((ts->vpaint->flag & VP_FLAG_SPRAY) == 0) { + if ((brush->flag & BRUSH_ACCUMULATE) == 0) { if (ob->sculpt->mode.vpaint.previous_color == NULL) { ob->sculpt->mode.vpaint.previous_color = MEM_callocN(me->totloop * sizeof(uint), __func__); @@ -1017,19 +1039,9 @@ static void vertex_paint_init_session_data(const ToolSettings *ts, Object *ob) else { MEM_SAFE_FREE(ob->sculpt->mode.vpaint.previous_color); } - - if (brush && brush->flag & BRUSH_ACCUMULATE) { - if (ob->sculpt->mode.vpaint.previous_accum == NULL) { - ob->sculpt->mode.vpaint.previous_accum = - MEM_callocN(me->totloop * sizeof(float), __func__); - } - } - else { - MEM_SAFE_FREE(ob->sculpt->mode.vpaint.previous_accum); - } } else if (ob->mode == OB_MODE_WEIGHT_PAINT) { - if ((ts->wpaint->flag & VP_FLAG_SPRAY) == 0) { + if ((brush->flag & BRUSH_ACCUMULATE) == 0) { if (ob->sculpt->mode.wpaint.alpha_weight == NULL) { ob->sculpt->mode.wpaint.alpha_weight = MEM_callocN(me->totvert * sizeof(float), __func__); @@ -1052,15 +1064,6 @@ static void vertex_paint_init_session_data(const ToolSettings *ts, Object *ob) ob->sculpt->mode.wpaint.dvert_prev = NULL; } } - if (brush && brush->flag & BRUSH_ACCUMULATE) { - if (ob->sculpt->mode.wpaint.previous_accum == NULL) { - ob->sculpt->mode.wpaint.previous_accum = - MEM_callocN(me->totvert * sizeof(float), __func__); - } - } - else { - MEM_SAFE_FREE(ob->sculpt->mode.wpaint.previous_accum); - } } } @@ -1119,7 +1122,7 @@ static int wpaint_mode_toggle_exec(bContext *C, wmOperator *op) ob->mode |= mode_flag; if (wp == NULL) - wp = scene->toolsettings->wpaint = new_vpaint(1); + wp = scene->toolsettings->wpaint = new_vpaint(); paint_cursor_start(C, weight_paint_poll); @@ -1187,9 +1190,6 @@ struct WPaintData { struct WeightPaintGroupData active, mirror; - void *vp_handle; - DMCoNo *vertexcosnos; - float wpimat[3][3]; /* variables for auto normalize */ @@ -1202,16 +1202,22 @@ struct WPaintData { bool do_multipaint; /* true if multipaint enabled and multiple groups selected */ int defbase_tot; + + /* Special storage for smear brush, avoid feedback loop - update each step and swap. */ + struct { + float *weight_prev; + float *weight_curr; + } smear; }; /* Initialize the stroke cache invariants from operator properties */ static void vwpaint_update_cache_invariants( - bContext *C, VPaint *vp, SculptSession *ss, wmOperator *op, const float mouse[2]) + bContext *C, const VPaint *vp, SculptSession *ss, wmOperator *op, const float mouse[2]) { StrokeCache *cache; Scene *scene = CTX_data_scene(C); UnifiedPaintSettings *ups = &CTX_data_tool_settings(C)->unified_paint_settings; - Brush *brush = BKE_paint_brush(&vp->paint); + const Brush *brush = vp->paint.brush; ViewContext *vc = paint_stroke_view_context(op->customdata); Object *ob = CTX_data_active_object(C); float mat[3][3]; @@ -1429,10 +1435,25 @@ static bool wpaint_stroke_test_start(bContext *C, wmOperator *op, const float mo wpd->mirror.lock = tmpflags; } - /* painting on subsurfs should give correct points too, this returns me->totvert amount */ - ob->sculpt->building_vp_handle = true; - wpd->vp_handle = ED_vpaint_proj_handle_create(&eval_ctx, scene, ob, &wpd->vertexcosnos); - ob->sculpt->building_vp_handle = false; + if (vp->paint.brush->vertexpaint_tool == PAINT_BLEND_SMEAR) { + wpd->smear.weight_prev = MEM_mallocN(sizeof(float) * me->totvert, __func__); + const MDeformVert *dv = me->dvert; + if (wpd->do_multipaint) { + const bool do_auto_normalize = ((ts->auto_normalize != 0) && (wpd->vgroup_validmap != NULL)); + for (int i = 0; i < me->totvert; i++, dv++) { + float weight = BKE_defvert_multipaint_collective_weight( + dv, wpd->defbase_tot, wpd->defbase_sel, wpd->defbase_tot_sel, do_auto_normalize); + CLAMP(weight, 0.0f, 1.0f); + wpd->smear.weight_prev[i] = weight; + } + } + else { + for (int i = 0; i < me->totvert; i++, dv++) { + wpd->smear.weight_prev[i] = defvert_find_weight(dv, wpd->active.index); + } + } + wpd->smear.weight_curr = MEM_dupallocN(wpd->smear.weight_prev); + } /* imat for normals */ mul_m4_m4m4(mat, wpd->vc.rv3d->viewmat, ob->obmat); @@ -1567,14 +1588,17 @@ static void do_wpaint_brush_blur_task_cb_ex( view_angle_limits_apply_falloff(&data->wpd->normal_angle_precalc, angle_cos, &brush_strength))) { const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); - float final_alpha = + const float final_alpha = brush_fade * brush_strength * grid_alpha * brush_alpha_pressure; - if (brush->flag & BRUSH_ACCUMULATE) { - float mask_accum = ss->mode.wpaint.previous_accum[v_index]; - final_alpha = min_ff(final_alpha + mask_accum, brush_strength); - ss->mode.wpaint.previous_accum[v_index] = final_alpha; + if ((brush->flag & BRUSH_ACCUMULATE) == 0) { + if (ss->mode.wpaint.alpha_weight[v_index] < final_alpha) { + ss->mode.wpaint.alpha_weight[v_index] = final_alpha; + } + else { + continue; + } } weight_final /= total_hit_loops; @@ -1611,7 +1635,7 @@ static void do_wpaint_brush_smear_task_cb_ex( sub_v3_v3v3(brush_dir, cache->location, cache->last_location); project_plane_v3_v3v3(brush_dir, brush_dir, cache->view_normal); - if (normalize_v3(brush_dir) != 0.0f) { + if (cache->is_last_valid && (normalize_v3(brush_dir) != 0.0f)) { SculptBrushTest test; SculptBrushTestFn sculpt_brush_test_sq_fn = @@ -1667,8 +1691,7 @@ static void do_wpaint_brush_smear_task_cb_ex( if (stroke_dot > stroke_dot_max) { stroke_dot_max = stroke_dot; - MDeformVert *dv = &data->me->dvert[v_other_index]; - weight_final = wpaint_get_active_weight(dv, data->wpi); + weight_final = data->wpd->smear.weight_prev[v_other_index]; do_color = true; } } @@ -1676,13 +1699,16 @@ static void do_wpaint_brush_smear_task_cb_ex( } /* Apply weight to vertex */ if (do_color) { - const float brush_fade = BKE_brush_curve_strength(brush, 0.0f, cache->radius); - float final_alpha = + const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); + const float final_alpha = brush_fade * brush_strength * grid_alpha * brush_alpha_pressure; do_weight_paint_vertex( data->vp, data->ob, data->wpi, v_index, final_alpha, (float)weight_final); + /* Access the weight again because it might not have been applied completely. */ + data->wpd->smear.weight_curr[v_index] = + wpaint_get_active_weight(&data->me->dvert[v_index], data->wpi); } } } @@ -1702,7 +1728,9 @@ static void do_wpaint_brush_draw_task_cb_ex( const Brush *brush = data->brush; const StrokeCache *cache = ss->cache; - const float paintweight = BKE_brush_weight_get(scene, brush); + /* note: normally `BKE_brush_weight_get(scene, brush)` is used, + * however in this case we calculate a new weight each time. */ + const float paintweight = data->strength; float brush_size_pressure, brush_alpha_value, brush_alpha_pressure; get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure); const bool use_normal = vwpaint_use_normal(data->vp); @@ -1737,16 +1765,9 @@ static void do_wpaint_brush_draw_task_cb_ex( view_angle_limits_apply_falloff(&data->wpd->normal_angle_precalc, angle_cos, &brush_strength))) { const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); - float final_alpha = brush_fade * brush_strength * grid_alpha * brush_alpha_pressure; - if (brush->flag & BRUSH_ACCUMULATE) { - float mask_accum = ss->mode.wpaint.previous_accum[v_index]; - final_alpha = min_ff(final_alpha + mask_accum, brush_strength); - ss->mode.wpaint.previous_accum[v_index] = final_alpha; - } + const float final_alpha = brush_fade * brush_strength * grid_alpha * brush_alpha_pressure; - /* Non-spray logic. */ - if ((data->vp->flag & VP_FLAG_SPRAY) == 0) { - /* Only paint if we have greater alpha. */ + if ((brush->flag & BRUSH_ACCUMULATE) == 0) { if (ss->mode.wpaint.alpha_weight[v_index] < final_alpha) { ss->mode.wpaint.alpha_weight[v_index] = final_alpha; } @@ -1812,9 +1833,6 @@ static void do_wpaint_brush_calc_average_weight_cb_ex( static void calculate_average_weight(SculptThreadedTaskData *data, PBVHNode **UNUSED(nodes), int totnode) { - Scene *scene = CTX_data_scene(data->C); - UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings; - struct WPaintAverageAccum *accum = MEM_mallocN(sizeof(*accum) * totnode, __func__); data->custom_data = accum; @@ -1830,10 +1848,7 @@ static void calculate_average_weight(SculptThreadedTaskData *data, PBVHNode **UN } if (accum_len != 0) { accum_weight /= accum_len; - if (ups->flag & UNIFIED_PAINT_WEIGHT) - ups->weight = (float)accum_weight; - else - data->brush->weight = (float)accum_weight; + data->strength = (float)accum_weight; } MEM_SAFE_FREE(data->custom_data); /* 'accum' */ @@ -1844,13 +1859,17 @@ static void wpaint_paint_leaves( bContext *C, Object *ob, Sculpt *sd, VPaint *vp, struct WPaintData *wpd, WeightPaintInfo *wpi, Mesh *me, PBVHNode **nodes, int totnode) { - Brush *brush = ob->sculpt->cache->brush; + Scene *scene = CTX_data_scene(C); + const Brush *brush = ob->sculpt->cache->brush; /* threaded loop over nodes */ SculptThreadedTaskData data = { .sd = sd, .ob = ob, .brush = brush, .nodes = nodes, .vp = vp, .wpd = wpd, .wpi = wpi, .me = me, .C = C, }; + /* Use this so average can modify its weight without touching the brush. */ + data.strength = BKE_brush_weight_get(scene, brush); + /* current mirroring code cannot be run in parallel */ bool use_threading = !(me->editflag & ME_EDIT_MIRROR_X); @@ -2055,6 +2074,10 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P swap_m4m4(vc->rv3d->persmat, mat); + if (wp->paint.brush->vertexpaint_tool == PAINT_BLEND_SMEAR) { + SWAP(float *, wpd->smear.weight_curr, wpd->smear.weight_prev); + } + /* calculate pivot for rotation around seletion if needed */ /* also needed for "View Selected" on last stroke */ paint_last_stroke_update(scene, vc->ar, mval); @@ -2094,8 +2117,6 @@ static void wpaint_stroke_done(const bContext *C, struct PaintStroke *stroke) struct WPaintData *wpd = paint_stroke_mode_data(stroke); if (wpd) { - ED_vpaint_proj_handle_free(wpd->vp_handle); - if (wpd->defbase_sel) MEM_freeN((void *)wpd->defbase_sel); if (wpd->vgroup_validmap) @@ -2106,6 +2127,10 @@ static void wpaint_stroke_done(const bContext *C, struct PaintStroke *stroke) MEM_freeN((void *)wpd->active.lock); if (wpd->mirror.lock) MEM_freeN((void *)wpd->mirror.lock); + if (wpd->smear.weight_prev) + MEM_freeN(wpd->smear.weight_prev); + if (wpd->smear.weight_curr) + MEM_freeN(wpd->smear.weight_curr); MEM_freeN(wpd); } @@ -2253,7 +2278,7 @@ static int vpaint_mode_toggle_exec(bContext *C, wmOperator *op) ED_mesh_color_ensure(me, NULL); if (vp == NULL) - vp = scene->toolsettings->vpaint = new_vpaint(0); + vp = scene->toolsettings->vpaint = new_vpaint(); paint_cursor_start(C, vertex_paint_poll); @@ -2342,6 +2367,12 @@ struct VPaintData { bool *mlooptag; bool is_texbrush; + + /* Special storage for smear brush, avoid feedback loop - update each step and swap. */ + struct { + uint *color_prev; + uint *color_curr; + } smear; }; static bool vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const float mouse[2]) @@ -2396,6 +2427,12 @@ static bool vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const f vpd->mlooptag = MEM_mallocN(sizeof(bool) * me->totloop, "VPaintData mlooptag"); } + if (brush->vertexpaint_tool == PAINT_BLEND_SMEAR) { + vpd->smear.color_prev = MEM_mallocN(sizeof(uint) * me->totloop, __func__); + memcpy(vpd->smear.color_prev, me->mloopcol, sizeof(uint) * me->totloop); + vpd->smear.color_curr = MEM_dupallocN(vpd->smear.color_prev); + } + /* Create projection handle */ if (vpd->is_texbrush) { ob->sculpt->building_vp_handle = true; @@ -2560,14 +2597,9 @@ static void do_vpaint_brush_draw_task_cb_ex( } color_orig = ss->mode.vpaint.previous_color[l_index]; } - float final_alpha = + const float final_alpha = 255 * brush_fade * brush_strength * tex_alpha * brush_alpha_pressure * grid_alpha; - if (brush->flag & BRUSH_ACCUMULATE) { - float mask_accum = ss->mode.vpaint.previous_accum[l_index]; - final_alpha = min_ff(final_alpha + mask_accum, 255.0f * brush_strength); - ss->mode.vpaint.previous_accum[l_index] = final_alpha; - } /* Mix the new color with the original based on final_alpha. */ lcol[l_index] = vpaint_blend( @@ -2671,7 +2703,7 @@ static void do_vpaint_brush_blur_task_cb_ex( } color_orig = ss->mode.vpaint.previous_color[l_index]; } - float final_alpha = + const float final_alpha = 255 * brush_fade * brush_strength * brush_alpha_pressure * grid_alpha; /* Mix the new color with the original @@ -2711,7 +2743,7 @@ static void do_vpaint_brush_smear_task_cb_ex( sub_v3_v3v3(brush_dir, cache->location, cache->last_location); project_plane_v3_v3v3(brush_dir, brush_dir, cache->view_normal); - if (normalize_v3(brush_dir) != 0.0f) { + if (cache->is_last_valid && (normalize_v3(brush_dir) != 0.0f)) { SculptBrushTest test; SculptBrushTestFn sculpt_brush_test_sq_fn = @@ -2754,6 +2786,7 @@ static void do_vpaint_brush_smear_task_cb_ex( const int p_index = gmap->vert_to_poly[v_index].indices[j]; const int l_index = gmap->vert_to_loop[v_index].indices[j]; BLI_assert(data->me->mloop[l_index].v == v_index); + UNUSED_VARS_NDEBUG(l_index); const MPoly *mp = &data->me->mpoly[p_index]; if (!use_face_sel || mp->flag & ME_FACE_SEL) { const MLoop *ml_other = &data->me->mloop[mp->loopstart]; @@ -2773,7 +2806,7 @@ static void do_vpaint_brush_smear_task_cb_ex( if (stroke_dot > stroke_dot_max) { stroke_dot_max = stroke_dot; - color_final = lcol[mp->loopstart + k]; + color_final = data->vpd->smear.color_prev[mp->loopstart + k]; do_color = true; } } @@ -2782,6 +2815,10 @@ static void do_vpaint_brush_smear_task_cb_ex( } if (do_color) { + const float final_alpha = + 255 * brush_fade * brush_strength * + brush_alpha_pressure * grid_alpha; + /* For each poly owning this vert, paint each loop belonging to this vert. */ for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) { const int p_index = gmap->vert_to_poly[v_index].indices[j]; @@ -2798,14 +2835,13 @@ static void do_vpaint_brush_smear_task_cb_ex( } color_orig = ss->mode.vpaint.previous_color[l_index]; } - float final_alpha = - 255 * brush_fade * brush_strength * - brush_alpha_pressure * grid_alpha; /* Mix the new color with the original * based on the brush strength and the curve. */ lcol[l_index] = vpaint_blend( data->vp, lcol[l_index], color_orig, color_final, final_alpha, 255 * brush_strength); + + data->vpd->smear.color_curr[l_index] = lcol[l_index]; } } } @@ -2850,7 +2886,7 @@ static void vpaint_paint_leaves( bContext *C, Sculpt *sd, VPaint *vp, struct VPaintData *vpd, Object *ob, Mesh *me, PBVHNode **nodes, int totnode) { - Brush *brush = ob->sculpt->cache->brush; + const Brush *brush = ob->sculpt->cache->brush; SculptThreadedTaskData data = { .sd = sd, .ob = ob, .brush = brush, .nodes = nodes, .vp = vp, .vpd = vpd, @@ -2983,6 +3019,10 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P BKE_mesh_batch_cache_dirty(ob->data, BKE_MESH_BATCH_DIRTY_ALL); + if (vp->paint.brush->vertexpaint_tool == PAINT_BLEND_SMEAR) { + SWAP(uint *, vpd->smear.color_curr, vpd->smear.color_prev); + } + /* calculate pivot for rotation around seletion if needed */ /* also needed for "View Selected" on last stroke */ paint_last_stroke_update(scene, vc->ar, mval); @@ -3008,6 +3048,10 @@ static void vpaint_stroke_done(const bContext *C, struct PaintStroke *stroke) if (vpd->mlooptag) MEM_freeN(vpd->mlooptag); + if (vpd->smear.color_prev) + MEM_freeN(vpd->smear.color_prev); + if (vpd->smear.color_curr) + MEM_freeN(vpd->smear.color_curr); WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob); diff --git a/source/blender/editors/sculpt_paint/paint_vertex_color_ops.c b/source/blender/editors/sculpt_paint/paint_vertex_color_ops.c index 32515fa81cb..d7668a48139 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex_color_ops.c +++ b/source/blender/editors/sculpt_paint/paint_vertex_color_ops.c @@ -191,7 +191,7 @@ void PAINT_OT_vertex_color_from_weight(wmOperatorType *ot) /* identifiers */ ot->name = "Vertex Color from Weight"; ot->idname = "PAINT_OT_vertex_color_from_weight"; - ot->description = "Converts active weight into greyscale vertex colors"; + ot->description = "Convert active weight into gray scale vertex colors"; /* api callback */ ot->exec = vertex_paint_from_weight_exec; diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c index ac5138deec7..aa8e536a430 100644 --- a/source/blender/editors/sculpt_paint/sculpt.c +++ b/source/blender/editors/sculpt_paint/sculpt.c @@ -634,7 +634,7 @@ bool sculpt_brush_test_cube(SculptBrushTest *test, const float co[3], float loca } } -static float frontface(Brush *br, const float sculpt_normal[3], +static float frontface(const Brush *br, const float sculpt_normal[3], const short no[3], const float fno[3]) { if (br->flag & BRUSH_FRONTFACE) { @@ -1128,7 +1128,7 @@ static float brush_strength( } /* Return a multiplier for brush strength on a particular vertex. */ -float tex_strength(SculptSession *ss, Brush *br, +float tex_strength(SculptSession *ss, const Brush *br, const float brush_point[3], const float len, const short vno[3], @@ -1138,7 +1138,7 @@ float tex_strength(SculptSession *ss, Brush *br, { StrokeCache *cache = ss->cache; const Scene *scene = cache->vc->scene; - MTex *mtex = &br->mtex; + const MTex *mtex = &br->mtex; float avg = 1; float rgba[4]; float point[3]; @@ -1556,7 +1556,7 @@ static void do_smooth_brush_mesh_task_cb_ex( SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; Sculpt *sd = data->sd; - Brush *brush = data->brush; + const Brush *brush = data->brush; const bool smooth_mask = data->smooth_mask; float bstrength = data->strength; @@ -1604,7 +1604,7 @@ static void do_smooth_brush_bmesh_task_cb_ex( SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; Sculpt *sd = data->sd; - Brush *brush = data->brush; + const Brush *brush = data->brush; const bool smooth_mask = data->smooth_mask; float bstrength = data->strength; @@ -1652,7 +1652,7 @@ static void do_smooth_brush_multires_task_cb_ex( SculptDoBrushSmoothGridDataChunk *data_chunk = userdata_chunk; SculptSession *ss = data->ob->sculpt; Sculpt *sd = data->sd; - Brush *brush = data->brush; + const Brush *brush = data->brush; const bool smooth_mask = data->smooth_mask; float bstrength = data->strength; @@ -1866,7 +1866,7 @@ static void do_mask_brush_draw_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float bstrength = ss->cache->bstrength; PBVHVertexIter vd; @@ -1923,7 +1923,7 @@ static void do_draw_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *offset = data->offset; PBVHVertexIter vd; @@ -1982,7 +1982,7 @@ static void do_crease_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; SculptProjectVector *spvc = data->spvc; const float flippedbstrength = data->flippedbstrength; const float *offset = data->offset; @@ -2070,7 +2070,7 @@ static void do_pinch_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; PBVHVertexIter vd; SculptBrushTest test; @@ -2116,7 +2116,7 @@ static void do_grab_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *grab_delta = data->grab_delta; PBVHVertexIter vd; @@ -2176,7 +2176,7 @@ static void do_nudge_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *cono = data->cono; PBVHVertexIter vd; @@ -2230,7 +2230,7 @@ static void do_snake_hook_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; SculptProjectVector *spvc = data->spvc; const float *grab_delta = data->grab_delta; @@ -2331,7 +2331,7 @@ static void do_thumb_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *cono = data->cono; PBVHVertexIter vd; @@ -2391,7 +2391,7 @@ static void do_rotate_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float angle = data->angle; PBVHVertexIter vd; @@ -2453,7 +2453,7 @@ static void do_layer_brush_task_cb_ex( SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; Sculpt *sd = data->sd; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *offset = data->offset; PBVHVertexIter vd; @@ -2537,7 +2537,7 @@ static void do_inflate_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; PBVHVertexIter vd; SculptBrushTest test; @@ -2695,7 +2695,7 @@ static void do_flatten_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *area_no = data->area_no; const float *area_co = data->area_co; @@ -2771,7 +2771,7 @@ static void do_clay_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *area_no = data->area_no; const float *area_co = data->area_co; @@ -2855,7 +2855,7 @@ static void do_clay_strips_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; float (*mat)[4] = data->mat; const float *area_no_sp = data->area_no_sp; const float *area_co = data->area_co; @@ -2965,7 +2965,7 @@ static void do_fill_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *area_no = data->area_no; const float *area_co = data->area_co; @@ -3044,7 +3044,7 @@ static void do_scrape_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *area_no = data->area_no; const float *area_co = data->area_co; @@ -3123,7 +3123,7 @@ static void do_gravity_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; float *offset = data->offset; PBVHVertexIter vd; @@ -3602,6 +3602,7 @@ void sculpt_cache_calc_brushdata_symm( const char axis, const float angle) { flip_v3_v3(cache->location, cache->true_location, symm); + flip_v3_v3(cache->last_location, cache->true_last_location, symm); flip_v3_v3(cache->grab_delta_symmetry, cache->grab_delta, symm); flip_v3_v3(cache->view_normal, cache->true_view_normal, symm); @@ -4667,6 +4668,7 @@ static void sculpt_stroke_done(const bContext *C, struct PaintStroke *UNUSED(str if (ss->cache) { UnifiedPaintSettings *ups = &CTX_data_tool_settings(C)->unified_paint_settings; Brush *brush = BKE_paint_brush(&sd->paint); + BLI_assert(brush == ss->cache->brush); /* const, so we shouldn't change. */ ups->draw_inverted = false; sculpt_stroke_modifiers_check(C, ob); @@ -4677,11 +4679,10 @@ static void sculpt_stroke_done(const bContext *C, struct PaintStroke *UNUSED(str brush->mask_tool = ss->cache->saved_mask_brush_tool; } else { - Paint *p = &sd->paint; - BKE_brush_size_set(scene, ss->cache->brush, ss->cache->saved_smooth_size); + BKE_brush_size_set(scene, brush, ss->cache->saved_smooth_size); brush = (Brush *)BKE_libblock_find_name(ID_BR, ss->cache->saved_active_brush_name); if (brush) { - BKE_paint_brush_set(p, brush); + BKE_paint_brush_set(&sd->paint, brush); } } } diff --git a/source/blender/editors/sculpt_paint/sculpt_intern.h b/source/blender/editors/sculpt_paint/sculpt_intern.h index 425cecb0010..b9b662f4157 100644 --- a/source/blender/editors/sculpt_paint/sculpt_intern.h +++ b/source/blender/editors/sculpt_paint/sculpt_intern.h @@ -132,7 +132,7 @@ typedef struct SculptThreadedTaskData { struct bContext *C; struct Sculpt *sd; struct Object *ob; - struct Brush *brush; + const struct Brush *brush; struct PBVHNode **nodes; int totnode; @@ -214,7 +214,7 @@ bool sculpt_brush_test_circle_sq(SculptBrushTest *test, const float co[3]); bool sculpt_search_sphere_cb(PBVHNode *node, void *data_v); bool sculpt_search_circle_cb(PBVHNode *node, void *data_v); float tex_strength( - struct SculptSession *ss, struct Brush *br, + struct SculptSession *ss, const struct Brush *br, const float point[3], const float len, const short vno[3], @@ -268,7 +268,7 @@ typedef struct StrokeCache { /* Clean this up! */ struct ViewContext *vc; - struct Brush *brush; + const struct Brush *brush; float special_rotation; float grab_delta[3], grab_delta_symmetry[3]; |