Merge branch 'blender2.7'
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/sculpt_paint/paint_vertex.c
29  *  \ingroup edsculpt
30  *
31  * Used for vertex color & weight paint and mode switching.
32  *
33  * \note This file is already big,
34  * use `paint_vertex_color_ops.c` & `paint_vertex_weight_ops.c` for general purpose operators.
35  */
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_listbase.h"
40 #include "BLI_rect.h"
41 #include "BLI_math.h"
42 #include "BLI_array_utils.h"
43 #include "BLI_task.h"
44
45 #include "DNA_armature_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_particle_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_brush_types.h"
50 #include "DNA_object_types.h"
51
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54
55 #include "BKE_brush.h"
56 #include "BKE_context.h"
57 #include "BKE_deform.h"
58 #include "BKE_main.h"
59 #include "BKE_mesh.h"
60 #include "BKE_mesh_mapping.h"
61 #include "BKE_modifier.h"
62 #include "BKE_object.h"
63 #include "BKE_object_deform.h"
64 #include "BKE_paint.h"
65 #include "BKE_report.h"
66 #include "BKE_subsurf.h"
67
68 #include "DEG_depsgraph.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72 #include "WM_message.h"
73 #include "WM_toolsystem.h"
74
75 #include "ED_armature.h"
76 #include "ED_object.h"
77 #include "ED_mesh.h"
78 #include "ED_screen.h"
79 #include "ED_view3d.h"
80
81 /* For IMB_BlendMode only. */
82 #include "IMB_imbuf.h"
83
84 #include "bmesh.h"
85 #include "BKE_ccg.h"
86
87 #include "sculpt_intern.h"
88 #include "paint_intern.h"  /* own include */
89
90 /* Use for 'blur' brush, align with PBVH nodes, created and freed on each update. */
91 struct VPaintAverageAccum {
92         uint len;
93         uint value[3];
94 };
95
96 struct WPaintAverageAccum {
97         uint len;
98         double value;
99 };
100
101 struct NormalAnglePrecalc {
102         bool do_mask_normal;
103         /* what angle to mask at */
104         float angle;
105         /* cos(angle), faster to compare */
106         float angle__cos;
107         float angle_inner;
108         float angle_inner__cos;
109         /* difference between angle and angle_inner, for easy access */
110         float angle_range;
111 };
112
113
114 static void view_angle_limits_init(
115         struct NormalAnglePrecalc *a, float angle, bool do_mask_normal)
116 {
117         angle = RAD2DEGF(angle);
118         a->do_mask_normal = do_mask_normal;
119         if (do_mask_normal) {
120                 a->angle_inner = angle;
121                 a->angle = (a->angle_inner + 90.0f) * 0.5f;
122         }
123         else {
124                 a->angle_inner = a->angle = angle;
125         }
126
127         a->angle_inner *=   (float)(M_PI_2 / 90);
128         a->angle *=         (float)(M_PI_2 / 90);
129         a->angle_range = a->angle - a->angle_inner;
130
131         if (a->angle_range <= 0.0f) {
132                 a->do_mask_normal = false;  /* no need to do blending */
133         }
134
135         a->angle__cos       = cosf(a->angle);
136         a->angle_inner__cos = cosf(a->angle_inner);
137 }
138
139 static float view_angle_limits_apply_falloff(
140         const struct NormalAnglePrecalc *a, float angle_cos, float *mask_p)
141 {
142         if (angle_cos <= a->angle__cos) {
143                 /* outsize the normal limit */
144                 return false;
145         }
146         else if (angle_cos < a->angle_inner__cos) {
147                 *mask_p *= (a->angle - acosf(angle_cos)) / a->angle_range;
148                 return true;
149         }
150         else {
151                 return true;
152         }
153 }
154
155 static bool vwpaint_use_normal(const VPaint *vp)
156 {
157         return ((vp->paint.brush->flag & BRUSH_FRONTFACE) != 0) ||
158                ((vp->paint.brush->flag & BRUSH_FRONTFACE_FALLOFF) != 0);
159 }
160
161 static bool brush_use_accumulate_ex(const Brush *brush, const int ob_mode)
162 {
163         return ((brush->flag & BRUSH_ACCUMULATE) != 0 ||
164                 (ob_mode == OB_MODE_VERTEX_PAINT ?
165                  (brush->vertexpaint_tool == VPAINT_TOOL_SMEAR) :
166                  (brush->weightpaint_tool == WPAINT_TOOL_SMEAR)));
167 }
168
169 static bool brush_use_accumulate(const VPaint *vp)
170 {
171         return brush_use_accumulate_ex(vp->paint.brush, vp->paint.runtime.ob_mode);
172 }
173
174 static MDeformVert *defweight_prev_init(MDeformVert *dvert_prev, MDeformVert *dvert_curr, int index)
175 {
176         MDeformVert *dv_curr = &dvert_curr[index];
177         MDeformVert *dv_prev = &dvert_prev[index];
178         if (dv_prev->flag == 1) {
179                 dv_prev->flag = 0;
180                 defvert_copy(dv_prev, dv_curr);
181         }
182         return dv_prev;
183 }
184
185 /* check if we can do partial updates and have them draw realtime
186  * (without rebuilding the 'derivedFinal') */
187 static bool vertex_paint_use_fast_update_check(Object *ob)
188 {
189         Mesh *me_eval = ob->runtime.mesh_eval;
190
191         if (me_eval != NULL) {
192                 Mesh *me = BKE_mesh_from_object(ob);
193                 if (me && me->mloopcol) {
194                         return (me->mloopcol == CustomData_get_layer(&me_eval->ldata, CD_MLOOPCOL));
195                 }
196         }
197
198         return false;
199 }
200
201 static void paint_last_stroke_update(Scene *scene, ARegion *ar, const float mval[2])
202 {
203         const int mval_i[2] = {mval[0], mval[1]};
204         float world[3];
205
206         if (ED_view3d_autodist_simple(ar, mval_i, world, 0, NULL)) {
207                 UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
208                 ups->average_stroke_counter++;
209                 add_v3_v3(ups->average_stroke_accum, world);
210                 ups->last_stroke_valid = true;
211         }
212 }
213
214 /* polling - retrieve whether cursor should be set or operator should be done */
215
216 /* Returns true if vertex paint mode is active */
217 bool vertex_paint_mode_poll(bContext *C)
218 {
219         Object *ob = CTX_data_active_object(C);
220
221         return ob && ob->mode == OB_MODE_VERTEX_PAINT && ((Mesh *)ob->data)->totpoly;
222 }
223
224 static bool vertex_paint_poll_ex(bContext *C, bool check_tool)
225 {
226         if (vertex_paint_mode_poll(C) &&
227             BKE_paint_brush(&CTX_data_tool_settings(C)->vpaint->paint))
228         {
229                 ScrArea *sa = CTX_wm_area(C);
230                 if (sa && sa->spacetype == SPACE_VIEW3D) {
231                         ARegion *ar = CTX_wm_region(C);
232                         if (ar->regiontype == RGN_TYPE_WINDOW) {
233                                 if (!check_tool || WM_toolsystem_active_tool_is_brush(C)) {
234                                         return 1;
235                                 }
236                         }
237                 }
238         }
239         return 0;
240 }
241
242 bool vertex_paint_poll(bContext *C)
243 {
244         return vertex_paint_poll_ex(C, true);
245 }
246
247 bool vertex_paint_poll_ignore_tool(bContext *C)
248 {
249         return vertex_paint_poll_ex(C, false);
250 }
251
252 bool weight_paint_mode_poll(bContext *C)
253 {
254         Object *ob = CTX_data_active_object(C);
255
256         return ob && ob->mode == OB_MODE_WEIGHT_PAINT && ((Mesh *)ob->data)->totpoly;
257 }
258
259 static bool weight_paint_poll_ex(bContext *C, bool check_tool)
260 {
261         Object *ob = CTX_data_active_object(C);
262         ScrArea *sa;
263
264         if ((ob != NULL) &&
265             (ob->mode & OB_MODE_WEIGHT_PAINT) &&
266             (BKE_paint_brush(&CTX_data_tool_settings(C)->wpaint->paint) != NULL) &&
267             (sa = CTX_wm_area(C)) &&
268             (sa->spacetype == SPACE_VIEW3D))
269         {
270                 ARegion *ar = CTX_wm_region(C);
271                 if (ar->regiontype == RGN_TYPE_WINDOW) {
272                         if (!check_tool || WM_toolsystem_active_tool_is_brush(C)) {
273                                 return 1;
274                         }
275                 }
276         }
277         return 0;
278 }
279
280 bool weight_paint_poll(bContext *C)
281 {
282         return weight_paint_poll_ex(C, true);
283 }
284
285 bool weight_paint_poll_ignore_tool(bContext *C)
286 {
287         return weight_paint_poll_ex(C, false);
288 }
289
290 uint vpaint_get_current_col(Scene *scene, VPaint *vp, bool secondary)
291 {
292         Brush *brush = BKE_paint_brush(&vp->paint);
293         uchar col[4];
294         rgb_float_to_uchar(col, secondary ? BKE_brush_secondary_color_get(scene, brush) : BKE_brush_color_get(scene, brush));
295         col[3] = 255; /* alpha isn't used, could even be removed to speedup paint a little */
296         return *(uint *)col;
297 }
298
299 /* wpaint has 'wpaint_blend' */
300 static uint vpaint_blend(
301         const VPaint *vp, uint color_curr, uint color_orig,
302         uint color_paint, const int alpha_i,
303         /* pre scaled from [0-1] --> [0-255] */
304         const int brush_alpha_value_i)
305 {
306         const Brush *brush = vp->paint.brush;
307         const IMB_BlendMode blend = brush->blend;
308
309         uint color_blend = ED_vpaint_blend_tool(blend, color_curr, color_paint, alpha_i);
310
311         /* if no accumulate, clip color adding with colorig & orig alpha */
312         if (!brush_use_accumulate(vp)) {
313                 uint color_test, a;
314                 char *cp, *ct, *co;
315
316                 color_test = ED_vpaint_blend_tool(blend, color_orig, color_paint, brush_alpha_value_i);
317
318                 cp = (char *)&color_blend;
319                 ct = (char *)&color_test;
320                 co = (char *)&color_orig;
321
322                 for (a = 0; a < 4; a++) {
323                         if (ct[a] < co[a]) {
324                                 if (cp[a] < ct[a]) cp[a] = ct[a];
325                                 else if (cp[a] > co[a]) cp[a] = co[a];
326                         }
327                         else {
328                                 if (cp[a] < co[a]) cp[a] = co[a];
329                                 else if (cp[a] > ct[a]) cp[a] = ct[a];
330                         }
331                 }
332         }
333
334         if ((brush->flag & BRUSH_LOCK_ALPHA) &&
335             !ELEM(blend, IMB_BLEND_ERASE_ALPHA, IMB_BLEND_ADD_ALPHA))
336         {
337                 char *cp, *cc;
338                 cp = (char *)&color_blend;
339                 cc = (char *)&color_curr;
340                 cp[3] = cc[3];
341         }
342
343         return color_blend;
344 }
345
346 static void tex_color_alpha(
347         VPaint *vp, const ViewContext *vc, const float co[3],
348         float r_rgba[4])
349 {
350         const Brush *brush = BKE_paint_brush(&vp->paint);
351         BLI_assert(brush->mtex.tex != NULL);
352         if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D) {
353                 BKE_brush_sample_tex_3d(vc->scene, brush, co, r_rgba, 0, NULL);
354         }
355         else {
356                 float co_ss[2];  /* screenspace */
357                 if (ED_view3d_project_float_object(
358                         vc->ar,
359                         co, co_ss,
360                         V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
361                 {
362                         const float co_ss_3d[3] = {co_ss[0], co_ss[1], 0.0f};  /* we need a 3rd empty value */
363                         BKE_brush_sample_tex_3d(vc->scene, brush, co_ss_3d, r_rgba, 0, NULL);
364                 }
365                 else {
366                         zero_v4(r_rgba);
367                 }
368         }
369 }
370
371 /* vpaint has 'vpaint_blend' */
372 static float wpaint_blend(
373         const VPaint *wp, float weight,
374         const float alpha, float paintval,
375         const float UNUSED(brush_alpha_value),
376         const short do_flip)
377 {
378         const Brush *brush = wp->paint.brush;
379         IMB_BlendMode blend = brush->blend;
380
381         if (do_flip) {
382                 switch (blend) {
383                         case IMB_BLEND_MIX:
384                                 paintval = 1.f - paintval; break;
385                         case IMB_BLEND_ADD:
386                                 blend = IMB_BLEND_SUB; break;
387                         case IMB_BLEND_SUB:
388                                 blend = IMB_BLEND_ADD; break;
389                         case IMB_BLEND_LIGHTEN:
390                                 blend = IMB_BLEND_DARKEN; break;
391                         case IMB_BLEND_DARKEN:
392                                 blend = IMB_BLEND_LIGHTEN; break;
393                         default: break;
394                 }
395         }
396
397         weight = ED_wpaint_blend_tool(blend, weight, paintval, alpha);
398
399         CLAMP(weight, 0.0f, 1.0f);
400
401         return weight;
402 }
403
404 static float wpaint_clamp_monotonic(float oldval, float curval, float newval)
405 {
406         if (newval < oldval)
407                 return MIN2(newval, curval);
408         else if (newval > oldval)
409                 return MAX2(newval, curval);
410         else
411                 return newval;
412 }
413
414 /* ----------------------------------------------------- */
415
416 static void do_weight_paint_normalize_all(MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap)
417 {
418         float sum = 0.0f, fac;
419         uint i, tot = 0;
420         MDeformWeight *dw;
421
422         for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
423                 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
424                         tot++;
425                         sum += dw->weight;
426                 }
427         }
428
429         if ((tot == 0) || (sum == 1.0f)) {
430                 return;
431         }
432
433         if (sum != 0.0f) {
434                 fac = 1.0f / sum;
435
436                 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
437                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
438                                 dw->weight *= fac;
439                         }
440                 }
441         }
442         else {
443                 /* hrmf, not a factor in this case */
444                 fac = 1.0f / tot;
445
446                 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
447                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
448                                 dw->weight = fac;
449                         }
450                 }
451         }
452 }
453
454 /**
455  * A version of #do_weight_paint_normalize_all that includes locked weights
456  * but only changes unlocked weights.
457  */
458 static bool do_weight_paint_normalize_all_locked(
459         MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap,
460         const bool *lock_flags)
461 {
462         float sum = 0.0f, fac;
463         float sum_unlock = 0.0f;
464         float lock_weight = 0.0f;
465         uint i, tot = 0;
466         MDeformWeight *dw;
467
468         if (lock_flags == NULL) {
469                 do_weight_paint_normalize_all(dvert, defbase_tot, vgroup_validmap);
470                 return true;
471         }
472
473         for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
474                 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
475                         sum += dw->weight;
476
477                         if (lock_flags[dw->def_nr]) {
478                                 lock_weight += dw->weight;
479                         }
480                         else {
481                                 tot++;
482                                 sum_unlock += dw->weight;
483                         }
484                 }
485         }
486
487         if (sum == 1.0f) {
488                 return true;
489         }
490
491         if (tot == 0) {
492                 return false;
493         }
494
495         if (lock_weight >= 1.0f) {
496                 /* locked groups make it impossible to fully normalize,
497                  * zero out what we can and return false */
498                 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
499                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
500                                 if (lock_flags[dw->def_nr] == false) {
501                                         dw->weight = 0.0f;
502                                 }
503                         }
504                 }
505
506                 return (lock_weight == 1.0f);
507         }
508         else if (sum_unlock != 0.0f) {
509                 fac = (1.0f - lock_weight) / sum_unlock;
510
511                 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
512                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
513                                 if (lock_flags[dw->def_nr] == false) {
514                                         dw->weight *= fac;
515                                         /* paranoid but possibly with float error */
516                                         CLAMP(dw->weight, 0.0f, 1.0f);
517                                 }
518                         }
519                 }
520         }
521         else {
522                 /* hrmf, not a factor in this case */
523                 fac = (1.0f - lock_weight) / tot;
524                 /* paranoid but possibly with float error */
525                 CLAMP(fac, 0.0f, 1.0f);
526
527                 for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
528                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
529                                 if (lock_flags[dw->def_nr] == false) {
530                                         dw->weight = fac;
531                                 }
532                         }
533                 }
534         }
535
536         return true;
537 }
538
539 /**
540  * \note same as function above except it does a second pass without active group
541  * if normalize fails with it.
542  */
543 static void do_weight_paint_normalize_all_locked_try_active(
544         MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap,
545         const bool *lock_flags, const bool *lock_with_active)
546 {
547         /* first pass with both active and explicitly locked groups restricted from change */
548
549         bool success = do_weight_paint_normalize_all_locked(dvert, defbase_tot, vgroup_validmap, lock_with_active);
550
551         if (!success) {
552                 /**
553                  * Locks prevented the first pass from full completion, so remove restriction on active group; e.g:
554                  *
555                  * - With 1.0 weight painted into active:
556                  *   nonzero locked weight; first pass zeroed out unlocked weight; scale 1 down to fit.
557                  * - With 0.0 weight painted into active:
558                  *   no unlocked groups; first pass did nothing; increase 0 to fit.
559                  */
560                 do_weight_paint_normalize_all_locked(dvert, defbase_tot, vgroup_validmap, lock_flags);
561         }
562 }
563
564 #if 0 /* UNUSED */
565 static bool has_unselected_unlocked_bone_group(
566         int defbase_tot, bool *defbase_sel, int selected,
567         const bool *lock_flags, const bool *vgroup_validmap)
568 {
569         int i;
570         if (defbase_tot == selected) {
571                 return false;
572         }
573         for (i = 0; i < defbase_tot; i++) {
574                 if (vgroup_validmap[i] && !defbase_sel[i] && !lock_flags[i]) {
575                         return true;
576                 }
577         }
578         return false;
579 }
580 #endif
581
582 static void multipaint_clamp_change(
583         MDeformVert *dvert, const int defbase_tot, const bool *defbase_sel,
584         float *change_p)
585 {
586         int i;
587         MDeformWeight *dw;
588         float val;
589         float change = *change_p;
590
591         /* verify that the change does not cause values exceeding 1 and clamp it */
592         for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
593                 if (dw->def_nr < defbase_tot && defbase_sel[dw->def_nr]) {
594                         if (dw->weight) {
595                                 val = dw->weight * change;
596                                 if (val > 1) {
597                                         change = 1.0f / dw->weight;
598                                 }
599                         }
600                 }
601         }
602
603         *change_p = change;
604 }
605
606 static bool multipaint_verify_change(MDeformVert *dvert, const int defbase_tot, float change, const bool *defbase_sel)
607 {
608         int i;
609         MDeformWeight *dw;
610         float val;
611
612         /* in case the change is reduced, you need to recheck
613          * the earlier values to make sure they are not 0
614          * (precision error) */
615         for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
616                 if (dw->def_nr < defbase_tot && defbase_sel[dw->def_nr]) {
617                         if (dw->weight) {
618                                 val = dw->weight * change;
619                                 /* the value should never reach zero while multi-painting if it
620                                  * was nonzero beforehand */
621                                 if (val <= 0) {
622                                         return false;
623                                 }
624                         }
625                 }
626         }
627
628         return true;
629 }
630
631 static void multipaint_apply_change(MDeformVert *dvert, const int defbase_tot, float change, const bool *defbase_sel)
632 {
633         int i;
634         MDeformWeight *dw;
635
636         /* apply the valid change */
637         for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
638                 if (dw->def_nr < defbase_tot && defbase_sel[dw->def_nr]) {
639                         if (dw->weight) {
640                                 dw->weight = dw->weight * change;
641                                 CLAMP(dw->weight, 0.0f, 1.0f);
642                         }
643                 }
644         }
645 }
646
647 /**
648  * Variables stored both for 'active' and 'mirror' sides.
649  */
650 struct WeightPaintGroupData {
651         /** index of active group or its mirror
652          *
653          * - 'active' is always `ob->actdef`.
654          * - 'mirror' is -1 when 'ME_EDIT_MIRROR_X' flag id disabled,
655          *   otherwise this will be set to the mirror or the active group (if the group isn't mirrored).
656          */
657         int index;
658         /** lock that includes the 'index' as locked too
659          *
660          * - 'active' is set of locked or active/selected groups
661          * - 'mirror' is set of locked or mirror groups
662          */
663         const bool *lock;
664 };
665
666 /* struct to avoid passing many args each call to do_weight_paint_vertex()
667  * this _could_ be made a part of the operators 'WPaintData' struct, or at
668  * least a member, but for now keep its own struct, initialized on every
669  * paint stroke update - campbell */
670 typedef struct WeightPaintInfo {
671
672         int defbase_tot;
673
674         /* both must add up to 'defbase_tot' */
675         int defbase_tot_sel;
676         int defbase_tot_unsel;
677
678         struct WeightPaintGroupData active, mirror;
679
680         /* boolean array for locked bones,
681          * length of defbase_tot */
682         const bool *lock_flags;
683         /* boolean array for selected bones,
684          * length of defbase_tot, cant be const because of how its passed */
685         const bool *defbase_sel;
686         /* same as WeightPaintData.vgroup_validmap,
687          * only added here for convenience */
688         const bool *vgroup_validmap;
689
690         bool do_flip;
691         bool do_multipaint;
692         bool do_auto_normalize;
693
694         float brush_alpha_value;  /* result of BKE_brush_alpha_get() */
695 } WeightPaintInfo;
696
697 static void do_weight_paint_vertex_single(
698         /* vars which remain the same for every vert */
699         const VPaint *wp, Object *ob, const WeightPaintInfo *wpi,
700         /* vars which change on each stroke */
701         const uint index, float alpha, float paintweight)
702 {
703         Mesh *me = ob->data;
704         MDeformVert *dv = &me->dvert[index];
705         bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
706
707         MDeformWeight *dw;
708         float weight_prev;
709
710         /* mirror vars */
711         int index_mirr;
712         int vgroup_mirr;
713
714         MDeformVert *dv_mirr;
715         MDeformWeight *dw_mirr;
716
717         /* from now on we can check if mirrors enabled if this var is -1 and not bother with the flag */
718         if (me->editflag & ME_EDIT_MIRROR_X) {
719                 index_mirr = mesh_get_x_mirror_vert(ob, NULL, index, topology);
720                 vgroup_mirr = wpi->mirror.index;
721
722                 /* another possible error - mirror group _and_ active group are the same (which is fine),
723                  * but we also are painting onto a center vertex - this would paint the same weight twice */
724                 if (index_mirr == index && vgroup_mirr == wpi->active.index) {
725                         index_mirr = vgroup_mirr = -1;
726                 }
727         }
728         else {
729                 index_mirr = vgroup_mirr = -1;
730         }
731
732         if (wp->flag & VP_FLAG_VGROUP_RESTRICT) {
733                 dw = defvert_find_index(dv, wpi->active.index);
734         }
735         else {
736                 dw = defvert_verify_index(dv, wpi->active.index);
737         }
738
739         if (dw == NULL) {
740                 return;
741         }
742
743         /* get the mirror def vars */
744         if (index_mirr != -1) {
745                 dv_mirr = &me->dvert[index_mirr];
746                 if (wp->flag & VP_FLAG_VGROUP_RESTRICT) {
747                         dw_mirr = defvert_find_index(dv_mirr, vgroup_mirr);
748
749                         if (dw_mirr == NULL) {
750                                 index_mirr = vgroup_mirr = -1;
751                                 dv_mirr = NULL;
752                         }
753                 }
754                 else {
755                         if (index != index_mirr) {
756                                 dw_mirr = defvert_verify_index(dv_mirr, vgroup_mirr);
757                         }
758                         else {
759                                 /* dv and dv_mirr are the same */
760                                 int totweight_prev = dv_mirr->totweight;
761                                 int dw_offset = (int)(dw - dv_mirr->dw);
762                                 dw_mirr = defvert_verify_index(dv_mirr, vgroup_mirr);
763
764                                 /* if we added another, get our old one back */
765                                 if (totweight_prev != dv_mirr->totweight) {
766                                         dw = &dv_mirr->dw[dw_offset];
767                                 }
768                         }
769                 }
770         }
771         else {
772                 dv_mirr = NULL;
773                 dw_mirr = NULL;
774         }
775
776         if (!brush_use_accumulate(wp)) {
777                 MDeformVert *dvert_prev = ob->sculpt->mode.wpaint.dvert_prev;
778                 MDeformVert *dv_prev = defweight_prev_init(dvert_prev, me->dvert, index);
779                 if (index_mirr != -1) {
780                         defweight_prev_init(dvert_prev, me->dvert, index_mirr);
781                 }
782
783                 weight_prev = defvert_find_weight(dv_prev, wpi->active.index);
784         }
785         else {
786                 weight_prev = dw->weight;
787         }
788
789         /* If there are no normalize-locks or multipaint,
790          * then there is no need to run the more complicated checks */
791
792         {
793                 float new_weight = wpaint_blend(
794                         wp, weight_prev, alpha, paintweight,
795                         wpi->brush_alpha_value, wpi->do_flip);
796
797                 dw->weight = wpaint_clamp_monotonic(weight_prev, dw->weight, new_weight);
798
799                 /* WATCH IT: take care of the ordering of applying mirror -> normalize,
800                  * can give wrong results [#26193], least confusing if normalize is done last */
801
802                 /* apply mirror */
803                 if (index_mirr != -1) {
804                         /* copy, not paint again */
805                         dw_mirr->weight = dw->weight;
806                 }
807
808                 /* apply normalize */
809                 if (wpi->do_auto_normalize) {
810                         /* note on normalize - this used to be applied after painting and normalize all weights,
811                          * in some ways this is good because there is feedback where the more weights involved would
812                          * 'resist' so you couldn't instantly zero out other weights by painting 1.0 on the active.
813                          *
814                          * However this gave a problem since applying mirror, then normalize both verts
815                          * the resulting weight wont match on both sides.
816                          *
817                          * If this 'resisting', slower normalize is nicer, we could call
818                          * do_weight_paint_normalize_all() and only use...
819                          * do_weight_paint_normalize_all_active() when normalizing the mirror vertex.
820                          * - campbell
821                          */
822                         do_weight_paint_normalize_all_locked_try_active(
823                                 dv, wpi->defbase_tot, wpi->vgroup_validmap, wpi->lock_flags, wpi->active.lock);
824
825                         if (index_mirr != -1) {
826                                 /* only normalize if this is not a center vertex,
827                                  * else we get a conflict, normalizing twice */
828                                 if (index != index_mirr) {
829                                         do_weight_paint_normalize_all_locked_try_active(
830                                                 dv_mirr, wpi->defbase_tot, wpi->vgroup_validmap, wpi->lock_flags, wpi->mirror.lock);
831                                 }
832                                 else {
833                                         /* this case accounts for...
834                                          * - painting onto a center vertex of a mesh
835                                          * - x mirror is enabled
836                                          * - auto normalize is enabled
837                                          * - the group you are painting onto has a L / R version
838                                          *
839                                          * We want L/R vgroups to have the same weight but this cant be if both are over 0.5,
840                                          * We _could_ have special check for that, but this would need its own normalize function which
841                                          * holds 2 groups from changing at once.
842                                          *
843                                          * So! just balance out the 2 weights, it keeps them equal and everything normalized.
844                                          *
845                                          * While it wont hit the desired weight immediately as the user waggles their mouse,
846                                          * constant painting and re-normalizing will get there. this is also just simpler logic.
847                                          * - campbell */
848                                         dw_mirr->weight = dw->weight = (dw_mirr->weight + dw->weight) * 0.5f;
849                                 }
850                         }
851                 }
852         }
853 }
854
855 static void do_weight_paint_vertex_multi(
856         /* vars which remain the same for every vert */
857         const VPaint *wp, Object *ob, const WeightPaintInfo *wpi,
858         /* vars which change on each stroke */
859         const uint index, float alpha, float paintweight)
860 {
861         Mesh *me = ob->data;
862         MDeformVert *dv = &me->dvert[index];
863         bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
864
865         /* mirror vars */
866         int index_mirr = -1;
867         MDeformVert *dv_mirr = NULL;
868
869         /* weights */
870         float curw, oldw, neww, change, curw_mirr, change_mirr;
871
872         /* from now on we can check if mirrors enabled if this var is -1 and not bother with the flag */
873         if (me->editflag & ME_EDIT_MIRROR_X) {
874                 index_mirr = mesh_get_x_mirror_vert(ob, NULL, index, topology);
875
876                 if (index_mirr != -1 && index_mirr != index) {
877                         dv_mirr = &me->dvert[index_mirr];
878                 }
879                 else {
880                         index_mirr = -1;
881                 }
882         }
883
884         /* compute weight change by applying the brush to average or sum of group weights */
885         curw = BKE_defvert_multipaint_collective_weight(
886                 dv, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize);
887
888         if (curw == 0.0f) {
889                 /* note: no weight to assign to this vertex, could add all groups? */
890                 return;
891         }
892
893         if (!brush_use_accumulate(wp)) {
894                 MDeformVert *dvert_prev = ob->sculpt->mode.wpaint.dvert_prev;
895                 MDeformVert *dv_prev = defweight_prev_init(dvert_prev, me->dvert, index);
896                 if (index_mirr != -1) {
897                         defweight_prev_init(dvert_prev, me->dvert, index_mirr);
898                 }
899
900                 oldw = BKE_defvert_multipaint_collective_weight(
901                         dv_prev, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize);
902         }
903         else {
904                 oldw = curw;
905         }
906
907         neww = wpaint_blend(wp, oldw, alpha, paintweight, wpi->brush_alpha_value, wpi->do_flip);
908         neww = wpaint_clamp_monotonic(oldw, curw, neww);
909
910         change = neww / curw;
911
912         /* verify for all groups that 0 < result <= 1 */
913         multipaint_clamp_change(dv, wpi->defbase_tot, wpi->defbase_sel, &change);
914
915         if (dv_mirr != NULL) {
916                 curw_mirr = BKE_defvert_multipaint_collective_weight(
917                         dv_mirr, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize);
918
919                 if (curw_mirr == 0.0f) {
920                         /* can't mirror into a zero weight vertex */
921                         dv_mirr = NULL;
922                 }
923                 else {
924                         /* mirror is changed to achieve the same collective weight value */
925                         float orig = change_mirr = curw * change / curw_mirr;
926
927                         multipaint_clamp_change(dv_mirr, wpi->defbase_tot, wpi->defbase_sel, &change_mirr);
928
929                         if (!multipaint_verify_change(dv_mirr, wpi->defbase_tot, change_mirr, wpi->defbase_sel)) {
930                                 return;
931                         }
932
933                         change *= change_mirr / orig;
934                 }
935         }
936
937         if (!multipaint_verify_change(dv, wpi->defbase_tot, change, wpi->defbase_sel)) {
938                 return;
939         }
940
941         /* apply validated change to vertex and mirror */
942         multipaint_apply_change(dv, wpi->defbase_tot, change, wpi->defbase_sel);
943
944         if (dv_mirr != NULL) {
945                 multipaint_apply_change(dv_mirr, wpi->defbase_tot, change_mirr, wpi->defbase_sel);
946         }
947
948         /* normalize */
949         if (wpi->do_auto_normalize) {
950                 do_weight_paint_normalize_all_locked_try_active(
951                         dv, wpi->defbase_tot, wpi->vgroup_validmap, wpi->lock_flags, wpi->active.lock);
952
953                 if (dv_mirr != NULL) {
954                         do_weight_paint_normalize_all_locked_try_active(
955                                 dv_mirr, wpi->defbase_tot, wpi->vgroup_validmap, wpi->lock_flags, wpi->active.lock);
956                 }
957         }
958 }
959
960 static void do_weight_paint_vertex(
961         /* vars which remain the same for every vert */
962         const VPaint *wp, Object *ob, const WeightPaintInfo *wpi,
963         /* vars which change on each stroke */
964         const uint index, float alpha, float paintweight)
965 {
966         if (wpi->do_multipaint) {
967                 do_weight_paint_vertex_multi(wp, ob, wpi, index, alpha, paintweight);
968         }
969         else {
970                 do_weight_paint_vertex_single(wp, ob, wpi, index, alpha, paintweight);
971         }
972 }
973
974
975 /* Toggle operator for turning vertex paint mode on or off (copied from sculpt.c) */
976 static void vertex_paint_init_session(Depsgraph *depsgraph, Scene *scene, Object *ob, eObjectMode object_mode)
977 {
978         /* Create persistent sculpt mode data */
979         BKE_sculpt_toolsettings_data_ensure(scene);
980
981         BLI_assert(ob->sculpt == NULL);
982         ob->sculpt = MEM_callocN(sizeof(SculptSession), "sculpt session");
983         ob->sculpt->mode_type = object_mode;
984         BKE_sculpt_update_mesh_elements(depsgraph, scene, scene->toolsettings->sculpt, ob, false, false);
985 }
986
987 static void vertex_paint_init_stroke(Depsgraph *depsgraph, Scene *scene, Object *ob)
988 {
989         BKE_sculpt_update_mesh_elements(depsgraph, scene, scene->toolsettings->sculpt, ob, false, false);
990 }
991
992 static void vertex_paint_init_session_data(const ToolSettings *ts, Object *ob)
993 {
994         /* Create maps */
995         struct SculptVertexPaintGeomMap *gmap = NULL;
996         if (ob->mode == OB_MODE_VERTEX_PAINT) {
997                 gmap = &ob->sculpt->mode.vpaint.gmap;
998                 BLI_assert(ob->sculpt->mode_type == OB_MODE_VERTEX_PAINT);
999         }
1000         else if (ob->mode == OB_MODE_WEIGHT_PAINT) {
1001                 gmap = &ob->sculpt->mode.wpaint.gmap;
1002                 BLI_assert(ob->sculpt->mode_type == OB_MODE_WEIGHT_PAINT);
1003         }
1004         else {
1005                 ob->sculpt->mode_type = 0;
1006                 BLI_assert(0);
1007                 return;
1008         }
1009
1010         Mesh *me = ob->data;
1011
1012         if (gmap->vert_to_loop == NULL) {
1013                 gmap->vert_map_mem = NULL;
1014                 gmap->vert_to_loop = NULL;
1015                 gmap->poly_map_mem = NULL;
1016                 gmap->vert_to_poly = NULL;
1017                 BKE_mesh_vert_loop_map_create(
1018                         &gmap->vert_to_loop,
1019                         &gmap->vert_map_mem,
1020                         me->mpoly, me->mloop, me->totvert, me->totpoly, me->totloop);
1021                 BKE_mesh_vert_poly_map_create(
1022                         &gmap->vert_to_poly,
1023                         &gmap->poly_map_mem,
1024                         me->mpoly, me->mloop, me->totvert, me->totpoly, me->totloop);
1025         }
1026
1027         /* Create average brush arrays */
1028         if (ob->mode == OB_MODE_VERTEX_PAINT) {
1029                 if (!brush_use_accumulate(ts->vpaint)) {
1030                         if (ob->sculpt->mode.vpaint.previous_color == NULL) {
1031                                 ob->sculpt->mode.vpaint.previous_color =
1032                                         MEM_callocN(me->totloop * sizeof(uint), __func__);
1033                         }
1034                 }
1035                 else {
1036                         MEM_SAFE_FREE(ob->sculpt->mode.vpaint.previous_color);
1037                 }
1038         }
1039         else if (ob->mode == OB_MODE_WEIGHT_PAINT) {
1040                 if (!brush_use_accumulate(ts->wpaint)) {
1041                         if (ob->sculpt->mode.wpaint.alpha_weight == NULL) {
1042                                 ob->sculpt->mode.wpaint.alpha_weight =
1043                                         MEM_callocN(me->totvert * sizeof(float), __func__);
1044                         }
1045                         if (ob->sculpt->mode.wpaint.dvert_prev == NULL) {
1046                                 ob->sculpt->mode.wpaint.dvert_prev =
1047                                         MEM_callocN(me->totvert * sizeof(MDeformVert), __func__);
1048                                 MDeformVert *dv = ob->sculpt->mode.wpaint.dvert_prev;
1049                                 for (int i = 0; i < me->totvert; i++, dv++) {
1050                                         /* Use to show this isn't initialized, never apply to the mesh data. */
1051                                         dv->flag = 1;
1052                                 }
1053                         }
1054                 }
1055                 else {
1056                         MEM_SAFE_FREE(ob->sculpt->mode.wpaint.alpha_weight);
1057                         if (ob->sculpt->mode.wpaint.dvert_prev != NULL) {
1058                                 BKE_defvert_array_free_elems(ob->sculpt->mode.wpaint.dvert_prev, me->totvert);
1059                                 MEM_freeN(ob->sculpt->mode.wpaint.dvert_prev);
1060                                 ob->sculpt->mode.wpaint.dvert_prev = NULL;
1061                         }
1062                 }
1063         }
1064
1065 }
1066
1067 /* -------------------------------------------------------------------- */
1068 /** \name Enter Vertex/Weight Paint Mode
1069  * \{ */
1070
1071 static void ed_vwpaintmode_enter_generic(
1072         Main *bmain, Depsgraph *depsgraph,
1073         wmWindowManager *wm, Scene *scene,
1074         Object *ob, const eObjectMode mode_flag)
1075 {
1076         ob->mode |= mode_flag;
1077         Mesh *me = BKE_mesh_from_object(ob);
1078
1079         /* Same as sculpt mode, make sure we don't have cached derived mesh which
1080          * points to freed arrays.
1081          */
1082         BKE_object_free_derived_caches(ob);
1083
1084         if (mode_flag == OB_MODE_VERTEX_PAINT) {
1085                 const ePaintMode paint_mode = PAINT_MODE_VERTEX;
1086                 ED_mesh_color_ensure(me, NULL);
1087
1088                 BKE_paint_ensure(scene->toolsettings, (Paint **)&scene->toolsettings->vpaint);
1089                 Paint *paint = BKE_paint_get_active_from_paintmode(scene, paint_mode);
1090                 paint_cursor_start_explicit(paint, wm, vertex_paint_poll);
1091                 BKE_paint_init(bmain, scene, paint_mode, PAINT_CURSOR_VERTEX_PAINT);
1092         }
1093         else if (mode_flag == OB_MODE_WEIGHT_PAINT) {
1094                 const  ePaintMode paint_mode = PAINT_MODE_WEIGHT;
1095
1096                 BKE_paint_ensure(scene->toolsettings, (Paint **)&scene->toolsettings->wpaint);
1097                 Paint *paint = BKE_paint_get_active_from_paintmode(scene, paint_mode);
1098                 paint_cursor_start_explicit(paint, wm, weight_paint_poll);
1099                 BKE_paint_init(bmain, scene, paint_mode, PAINT_CURSOR_WEIGHT_PAINT);
1100
1101                 /* weight paint specific */
1102                 ED_mesh_mirror_spatial_table(ob, NULL, NULL, NULL, 's');
1103                 ED_vgroup_sync_from_pose(ob);
1104         }
1105         else {
1106                 BLI_assert(0);
1107         }
1108
1109         /* Create vertex/weight paint mode session data */
1110         if (ob->sculpt) {
1111                 if (ob->sculpt->cache) {
1112                         sculpt_cache_free(ob->sculpt->cache);
1113                         ob->sculpt->cache = NULL;
1114                 }
1115                 BKE_sculptsession_free(ob);
1116         }
1117
1118         vertex_paint_init_session(depsgraph, scene, ob, mode_flag);
1119
1120         /* Flush object mode. */
1121         DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
1122 }
1123
1124 void ED_object_vpaintmode_enter_ex(
1125         Main *bmain, Depsgraph *depsgraph, wmWindowManager *wm,
1126         Scene *scene, Object *ob)
1127 {
1128         ed_vwpaintmode_enter_generic(
1129                 bmain, depsgraph, wm, scene, ob, OB_MODE_VERTEX_PAINT);
1130 }
1131 void ED_object_vpaintmode_enter(struct bContext *C)
1132 {
1133         Main *bmain = CTX_data_main(C);
1134         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1135         wmWindowManager *wm = CTX_wm_manager(C);
1136         Scene *scene = CTX_data_scene(C);
1137         Object *ob = CTX_data_active_object(C);
1138         ED_object_vpaintmode_enter_ex(bmain, depsgraph, wm, scene, ob);
1139 }
1140
1141 void ED_object_wpaintmode_enter_ex(
1142         Main *bmain, Depsgraph *depsgraph, wmWindowManager *wm,
1143         Scene *scene, Object *ob)
1144 {
1145         ed_vwpaintmode_enter_generic(
1146                 bmain, depsgraph, wm, scene, ob, OB_MODE_WEIGHT_PAINT);
1147 }
1148 void ED_object_wpaintmode_enter(struct bContext *C)
1149 {
1150         Main *bmain = CTX_data_main(C);
1151         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1152         wmWindowManager *wm = CTX_wm_manager(C);
1153         Scene *scene = CTX_data_scene(C);
1154         Object *ob = CTX_data_active_object(C);
1155         ED_object_wpaintmode_enter_ex(bmain, depsgraph, wm, scene, ob);
1156 }
1157
1158 /** \} */
1159
1160 /* -------------------------------------------------------------------- */
1161 /** \name Exit Vertex/Weight Paint Mode
1162  * \{ */
1163
1164 static void ed_vwpaintmode_exit_generic(
1165         Object *ob, const eObjectMode mode_flag)
1166 {
1167         Mesh *me = BKE_mesh_from_object(ob);
1168         ob->mode &= ~mode_flag;
1169
1170         if (mode_flag == OB_MODE_VERTEX_PAINT) {
1171                 if (me->editflag & ME_EDIT_PAINT_FACE_SEL) {
1172                         BKE_mesh_flush_select_from_polys(me);
1173                 }
1174                 else if (me->editflag & ME_EDIT_PAINT_VERT_SEL) {
1175                         BKE_mesh_flush_select_from_verts(me);
1176                 }
1177         }
1178         else if (mode_flag == OB_MODE_WEIGHT_PAINT) {
1179                 if (me->editflag & ME_EDIT_PAINT_VERT_SEL) {
1180                         BKE_mesh_flush_select_from_verts(me);
1181                 }
1182                 else if (me->editflag & ME_EDIT_PAINT_FACE_SEL) {
1183                         BKE_mesh_flush_select_from_polys(me);
1184                 }
1185         }
1186         else {
1187                 BLI_assert(0);
1188         }
1189
1190         /* If the cache is not released by a cancel or a done, free it now. */
1191         if (ob->sculpt && ob->sculpt->cache) {
1192                 sculpt_cache_free(ob->sculpt->cache);
1193                 ob->sculpt->cache = NULL;
1194         }
1195
1196         BKE_sculptsession_free(ob);
1197
1198         paint_cursor_delete_textures();
1199
1200         if (mode_flag == OB_MODE_WEIGHT_PAINT) {
1201                 ED_mesh_mirror_spatial_table(NULL, NULL, NULL, NULL, 'e');
1202                 ED_mesh_mirror_topo_table(NULL, NULL, 'e');
1203         }
1204
1205         /* Never leave derived meshes behind. */
1206         BKE_object_free_derived_caches(ob);
1207
1208         /* Flush object mode. */
1209         DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
1210 }
1211
1212 void ED_object_vpaintmode_exit_ex(Object *ob)
1213 {
1214         ed_vwpaintmode_exit_generic(ob, OB_MODE_VERTEX_PAINT);
1215 }
1216 void ED_object_vpaintmode_exit(struct bContext *C)
1217 {
1218         Object *ob = CTX_data_active_object(C);
1219         ED_object_vpaintmode_exit_ex(ob);
1220 }
1221
1222 void ED_object_wpaintmode_exit_ex(Object *ob)
1223 {
1224         ed_vwpaintmode_exit_generic(ob, OB_MODE_WEIGHT_PAINT);
1225 }
1226 void ED_object_wpaintmode_exit(struct bContext *C)
1227 {
1228         Object *ob = CTX_data_active_object(C);
1229         ED_object_wpaintmode_exit_ex(ob);
1230 }
1231
1232 /** \} */
1233
1234 /* *************** set wpaint operator ****************** */
1235
1236 /**
1237  * \note Keep in sync with #vpaint_mode_toggle_exec
1238  */
1239 static int wpaint_mode_toggle_exec(bContext *C, wmOperator *op)
1240 {
1241         Main *bmain = CTX_data_main(C);
1242         struct wmMsgBus *mbus = CTX_wm_message_bus(C);
1243         Object *ob = CTX_data_active_object(C);
1244         const int mode_flag = OB_MODE_WEIGHT_PAINT;
1245         const bool is_mode_set = (ob->mode & mode_flag) != 0;
1246         Scene *scene = CTX_data_scene(C);
1247         ToolSettings *ts = scene->toolsettings;
1248
1249         if (!is_mode_set) {
1250                 if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
1251                         return OPERATOR_CANCELLED;
1252                 }
1253         }
1254
1255         Mesh *me = BKE_mesh_from_object(ob);
1256
1257         if (is_mode_set) {
1258                 ED_object_wpaintmode_exit_ex(ob);
1259         }
1260         else {
1261                 Depsgraph *depsgraph = CTX_data_depsgraph_on_load(C);
1262                 wmWindowManager *wm = CTX_wm_manager(C);
1263                 ED_object_wpaintmode_enter_ex(bmain, depsgraph, wm, scene, ob);
1264                 BKE_paint_toolslots_brush_validate(bmain, &ts->wpaint->paint);
1265         }
1266
1267         /* When locked, it's almost impossible to select the pose then the object to enter weight paint mode.
1268          * In this case move our pose object in/out of pose mode.
1269          * This is in fits with the convention of selecting multiple objects and entering a mode. */
1270         if (scene->toolsettings->object_flag & SCE_OBJECT_MODE_LOCK) {
1271                 Object *ob_arm = modifiers_isDeformedByArmature(ob);
1272                 if (ob_arm && (ob_arm->base_flag & BASE_SELECTED)) {
1273                         if (ob_arm->mode & OB_MODE_POSE) {
1274                                 ED_object_posemode_exit_ex(bmain, ob_arm);
1275                         }
1276                         else {
1277                                 ED_object_posemode_enter_ex(bmain, ob_arm);
1278                         }
1279                 }
1280         }
1281
1282         /* Weightpaint works by overriding colors in mesh,
1283          * so need to make sure we recalc on enter and
1284          * exit (exit needs doing regardless because we
1285          * should redeform).
1286          */
1287         DEG_id_tag_update(&me->id, 0);
1288
1289         WM_event_add_notifier(C, NC_SCENE | ND_MODE, scene);
1290
1291         WM_msg_publish_rna_prop(mbus, &ob->id, ob, Object, mode);
1292
1293         WM_toolsystem_update_from_context_view3d(C);
1294
1295         return OPERATOR_FINISHED;
1296 }
1297
1298 /* for switching to/from mode */
1299 static bool paint_poll_test(bContext *C)
1300 {
1301         Object *ob = CTX_data_active_object(C);
1302         if (ob == NULL || ob->type != OB_MESH)
1303                 return 0;
1304         if (!ob->data || ID_IS_LINKED(ob->data))
1305                 return 0;
1306         if (CTX_data_edit_object(C))
1307                 return 0;
1308         return 1;
1309 }
1310
1311 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1312 {
1313
1314         /* identifiers */
1315         ot->name = "Weight Paint Mode";
1316         ot->idname = "PAINT_OT_weight_paint_toggle";
1317         ot->description = "Toggle weight paint mode in 3D view";
1318
1319         /* api callbacks */
1320         ot->exec = wpaint_mode_toggle_exec;
1321         ot->poll = paint_poll_test;
1322
1323         /* flags */
1324         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_USE_EVAL_DATA;
1325 }
1326
1327 /* ************ weight paint operator ********** */
1328
1329 struct WPaintData {
1330         ViewContext vc;
1331         struct NormalAnglePrecalc normal_angle_precalc;
1332
1333         struct WeightPaintGroupData active, mirror;
1334
1335         /* variables for auto normalize */
1336         const bool *vgroup_validmap; /* stores if vgroups tie to deforming bones or not */
1337         const bool *lock_flags;
1338
1339         /* variables for multipaint */
1340         const bool *defbase_sel;      /* set of selected groups */
1341         int defbase_tot_sel;          /* number of selected groups */
1342         bool do_multipaint;           /* true if multipaint enabled and multiple groups selected */
1343
1344         int defbase_tot;
1345
1346         /* original weight values for use in blur/smear */
1347         float *precomputed_weight;
1348         bool precomputed_weight_ready;
1349 };
1350
1351 /* Initialize the stroke cache invariants from operator properties */
1352 static void vwpaint_update_cache_invariants(
1353         bContext *C, const VPaint *vp, SculptSession *ss, wmOperator *op, const float mouse[2])
1354 {
1355         StrokeCache *cache;
1356         Scene *scene = CTX_data_scene(C);
1357         UnifiedPaintSettings *ups = &CTX_data_tool_settings(C)->unified_paint_settings;
1358         const Brush *brush = vp->paint.brush;
1359         ViewContext *vc = paint_stroke_view_context(op->customdata);
1360         Object *ob = CTX_data_active_object(C);
1361         float mat[3][3];
1362         float view_dir[3] = {0.0f, 0.0f, 1.0f};
1363         int mode;
1364
1365         /* VW paint needs to allocate stroke cache before update is called. */
1366         if (!ss->cache) {
1367                 cache = MEM_callocN(sizeof(StrokeCache), "stroke cache");
1368                 ss->cache = cache;
1369         }
1370         else {
1371                 cache = ss->cache;
1372         }
1373
1374         /* Initial mouse location */
1375         if (mouse)
1376                 copy_v2_v2(cache->initial_mouse, mouse);
1377         else
1378                 zero_v2(cache->initial_mouse);
1379
1380         mode = RNA_enum_get(op->ptr, "mode");
1381         cache->invert = mode == BRUSH_STROKE_INVERT;
1382         cache->alt_smooth = mode == BRUSH_STROKE_SMOOTH;
1383         /* not very nice, but with current events system implementation
1384          * we can't handle brush appearance inversion hotkey separately (sergey) */
1385         if (cache->invert) ups->draw_inverted = true;
1386         else ups->draw_inverted = false;
1387
1388         copy_v2_v2(cache->mouse, cache->initial_mouse);
1389         /* Truly temporary data that isn't stored in properties */
1390         cache->vc = vc;
1391         cache->brush = brush;
1392         cache->first_time = 1;
1393
1394         /* cache projection matrix */
1395         ED_view3d_ob_project_mat_get(cache->vc->rv3d, ob, cache->projection_mat);
1396
1397         invert_m4_m4(ob->imat, ob->obmat);
1398         copy_m3_m4(mat, cache->vc->rv3d->viewinv);
1399         mul_m3_v3(mat, view_dir);
1400         copy_m3_m4(mat, ob->imat);
1401         mul_m3_v3(mat, view_dir);
1402         normalize_v3_v3(cache->true_view_normal, view_dir);
1403
1404         copy_v3_v3(cache->view_normal, cache->true_view_normal);
1405         cache->bstrength = BKE_brush_alpha_get(scene, brush);
1406         cache->is_last_valid = false;
1407 }
1408
1409 /* Initialize the stroke cache variants from operator properties */
1410 static void vwpaint_update_cache_variants(bContext *C, VPaint *vp, Object *ob, PointerRNA *ptr)
1411 {
1412         Scene *scene = CTX_data_scene(C);
1413         SculptSession *ss = ob->sculpt;
1414         StrokeCache *cache = ss->cache;
1415         Brush *brush = BKE_paint_brush(&vp->paint);
1416
1417         /* This effects the actual brush radius, so things farther away
1418          * are compared with a larger radius and vice versa. */
1419         if (cache->first_time) {
1420                 RNA_float_get_array(ptr, "location", cache->true_location);
1421         }
1422
1423         RNA_float_get_array(ptr, "mouse", cache->mouse);
1424
1425         /* XXX: Use pressure value from first brush step for brushes which don't
1426          * support strokes (grab, thumb). They depends on initial state and
1427          * brush coord/pressure/etc.
1428          * It's more an events design issue, which doesn't split coordinate/pressure/angle
1429          * changing events. We should avoid this after events system re-design */
1430         if (paint_supports_dynamic_size(brush, PAINT_MODE_SCULPT) || cache->first_time) {
1431                 cache->pressure = RNA_float_get(ptr, "pressure");
1432         }
1433
1434         /* Truly temporary data that isn't stored in properties */
1435         if (cache->first_time) {
1436                 if (!BKE_brush_use_locked_size(scene, brush)) {
1437                         cache->initial_radius = paint_calc_object_space_radius(
1438                                 cache->vc, cache->true_location, BKE_brush_size_get(scene, brush));
1439                         BKE_brush_unprojected_radius_set(scene, brush, cache->initial_radius);
1440                 }
1441                 else {
1442                         cache->initial_radius = BKE_brush_unprojected_radius_get(scene, brush);
1443                 }
1444         }
1445
1446         if (BKE_brush_use_size_pressure(scene, brush) && paint_supports_dynamic_size(brush, PAINT_MODE_SCULPT)) {
1447                 cache->radius = cache->initial_radius * cache->pressure;
1448         }
1449         else {
1450                 cache->radius = cache->initial_radius;
1451         }
1452
1453         cache->radius_squared = cache->radius * cache->radius;
1454
1455         if (ss->pbvh) {
1456                 BKE_pbvh_update(ss->pbvh, PBVH_UpdateRedraw, NULL);
1457                 BKE_pbvh_update(ss->pbvh, PBVH_UpdateBB, NULL);
1458         }
1459 }
1460
1461 static bool wpaint_stroke_test_start(bContext *C, wmOperator *op, const float mouse[2])
1462 {
1463         Scene *scene = CTX_data_scene(C);
1464         struct PaintStroke *stroke = op->customdata;
1465         ToolSettings *ts = scene->toolsettings;
1466         Object *ob = CTX_data_active_object(C);
1467         Mesh *me = BKE_mesh_from_object(ob);
1468         struct WPaintData *wpd;
1469         struct WPaintVGroupIndex vgroup_index;
1470         int defbase_tot, defbase_tot_sel;
1471         bool *defbase_sel;
1472         SculptSession *ss = ob->sculpt;
1473         VPaint *vp = CTX_data_tool_settings(C)->wpaint;
1474         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1475
1476         if (ED_wpaint_ensure_data(C, op->reports, WPAINT_ENSURE_MIRROR, &vgroup_index) == false) {
1477                 return false;
1478         }
1479
1480         {
1481                 /* check if we are attempting to paint onto a locked vertex group,
1482                  * and other options disallow it from doing anything useful */
1483                 bDeformGroup *dg;
1484                 dg = BLI_findlink(&ob->defbase, vgroup_index.active);
1485                 if (dg->flag & DG_LOCK_WEIGHT) {
1486                         BKE_report(op->reports, RPT_WARNING, "Active group is locked, aborting");
1487                         return false;
1488                 }
1489                 if (vgroup_index.mirror != -1) {
1490                         dg = BLI_findlink(&ob->defbase, vgroup_index.mirror);
1491                         if (dg->flag & DG_LOCK_WEIGHT) {
1492                                 BKE_report(op->reports, RPT_WARNING, "Mirror group is locked, aborting");
1493                                 return false;
1494                         }
1495                 }
1496         }
1497
1498         /* check that multipaint groups are unlocked */
1499         defbase_tot = BLI_listbase_count(&ob->defbase);
1500         defbase_sel = BKE_object_defgroup_selected_get(ob, defbase_tot, &defbase_tot_sel);
1501
1502         if (ts->multipaint && defbase_tot_sel > 1) {
1503                 int i;
1504                 bDeformGroup *dg;
1505
1506                 if (me->editflag & ME_EDIT_MIRROR_X) {
1507                         BKE_object_defgroup_mirror_selection(ob, defbase_tot, defbase_sel, defbase_sel, &defbase_tot_sel);
1508                 }
1509
1510                 for (i = 0; i < defbase_tot; i++) {
1511                         if (defbase_sel[i]) {
1512                                 dg = BLI_findlink(&ob->defbase, i);
1513                                 if (dg->flag & DG_LOCK_WEIGHT) {
1514                                         BKE_report(op->reports, RPT_WARNING, "Multipaint group is locked, aborting");
1515                                         MEM_freeN(defbase_sel);
1516                                         return false;
1517                                 }
1518                         }
1519                 }
1520         }
1521
1522         /* ALLOCATIONS! no return after this line */
1523         /* make mode data storage */
1524         wpd = MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1525         paint_stroke_set_mode_data(stroke, wpd);
1526         ED_view3d_viewcontext_init(C, &wpd->vc);
1527         view_angle_limits_init(
1528                 &wpd->normal_angle_precalc, vp->paint.brush->falloff_angle,
1529                 (vp->paint.brush->flag & BRUSH_FRONTFACE_FALLOFF) != 0);
1530
1531         wpd->active.index = vgroup_index.active;
1532         wpd->mirror.index = vgroup_index.mirror;
1533
1534         /* multipaint */
1535         wpd->defbase_tot = defbase_tot;
1536         wpd->defbase_sel = defbase_sel;
1537         wpd->defbase_tot_sel = defbase_tot_sel > 1 ? defbase_tot_sel : 1;
1538         wpd->do_multipaint = (ts->multipaint && defbase_tot_sel > 1);
1539
1540         /* set up auto-normalize, and generate map for detecting which
1541          * vgroups affect deform bones */
1542         wpd->lock_flags = BKE_object_defgroup_lock_flags_get(ob, wpd->defbase_tot);
1543         if (ts->auto_normalize || ts->multipaint || wpd->lock_flags) {
1544                 wpd->vgroup_validmap = BKE_object_defgroup_validmap_get(ob, wpd->defbase_tot);
1545         }
1546
1547         if (wpd->do_multipaint && ts->auto_normalize) {
1548                 bool *tmpflags;
1549                 tmpflags = MEM_mallocN(sizeof(bool) * defbase_tot, __func__);
1550                 if (wpd->lock_flags) {
1551                         BLI_array_binary_or(tmpflags, wpd->defbase_sel, wpd->lock_flags, wpd->defbase_tot);
1552                 }
1553                 else {
1554                         memcpy(tmpflags, wpd->defbase_sel, sizeof(*tmpflags) * wpd->defbase_tot);
1555                 }
1556                 wpd->active.lock = tmpflags;
1557         }
1558         else if (ts->auto_normalize) {
1559                 bool *tmpflags;
1560
1561                 tmpflags = wpd->lock_flags ?
1562                         MEM_dupallocN(wpd->lock_flags) :
1563                         MEM_callocN(sizeof(bool) * defbase_tot, __func__);
1564                 tmpflags[wpd->active.index] = true;
1565                 wpd->active.lock = tmpflags;
1566
1567                 tmpflags = wpd->lock_flags ?
1568                         MEM_dupallocN(wpd->lock_flags) :
1569                         MEM_callocN(sizeof(bool) * defbase_tot, __func__);
1570                 tmpflags[(wpd->mirror.index != -1) ? wpd->mirror.index : wpd->active.index] = true;
1571                 wpd->mirror.lock = tmpflags;
1572         }
1573
1574         if (ELEM(vp->paint.brush->weightpaint_tool, WPAINT_TOOL_SMEAR, WPAINT_TOOL_BLUR)) {
1575                 wpd->precomputed_weight = MEM_mallocN(sizeof(float) * me->totvert, __func__);
1576         }
1577
1578         /* If not previously created, create vertex/weight paint mode session data */
1579         vertex_paint_init_stroke(depsgraph, scene, ob);
1580         vwpaint_update_cache_invariants(C, vp, ss, op, mouse);
1581         vertex_paint_init_session_data(ts, ob);
1582
1583         if (ob->sculpt->mode.wpaint.dvert_prev != NULL) {
1584                 MDeformVert *dv = ob->sculpt->mode.wpaint.dvert_prev;
1585                 for (int i = 0; i < me->totvert; i++, dv++) {
1586                         /* Use to show this isn't initialized, never apply to the mesh data. */
1587                         dv->flag = 1;
1588                 }
1589         }
1590
1591         return true;
1592 }
1593
1594 static float dot_vf3vs3(const float brushNormal[3], const short vertexNormal[3])
1595 {
1596         float normal[3];
1597         normal_short_to_float_v3(normal, vertexNormal);
1598         return dot_v3v3(brushNormal, normal);
1599 }
1600
1601 static void get_brush_alpha_data(
1602         const Scene *scene, const SculptSession *ss, const Brush *brush,
1603         float *r_brush_size_pressure, float *r_brush_alpha_value, float *r_brush_alpha_pressure)
1604 {
1605         *r_brush_size_pressure =
1606                 BKE_brush_size_get(scene, brush) *
1607                 (BKE_brush_use_size_pressure(scene, brush) ? ss->cache->pressure : 1.0f);
1608         *r_brush_alpha_value =
1609                 BKE_brush_alpha_get(scene, brush);
1610         *r_brush_alpha_pressure =
1611                 (BKE_brush_use_alpha_pressure(scene, brush) ? ss->cache->pressure : 1.0f);
1612 }
1613
1614 static float wpaint_get_active_weight(const MDeformVert *dv, const WeightPaintInfo *wpi)
1615 {
1616         if (wpi->do_multipaint) {
1617                 float weight = BKE_defvert_multipaint_collective_weight(
1618                      dv, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize);
1619
1620                 CLAMP(weight, 0.0f, 1.0f);
1621                 return weight;
1622         }
1623         else {
1624                 return defvert_find_weight(dv, wpi->active.index);
1625         }
1626 }
1627
1628 static void do_wpaint_precompute_weight_cb_ex(
1629         void *__restrict userdata,
1630         const int n,
1631         const ParallelRangeTLS *__restrict UNUSED(tls))
1632 {
1633         SculptThreadedTaskData *data = userdata;
1634         const MDeformVert *dv = &data->me->dvert[n];
1635
1636         data->wpd->precomputed_weight[n] = wpaint_get_active_weight(dv, data->wpi);
1637 }
1638
1639 static void precompute_weight_values(
1640         bContext *C, Object *ob, Brush *brush, struct WPaintData *wpd, WeightPaintInfo *wpi, Mesh *me)
1641 {
1642         if (wpd->precomputed_weight_ready && !brush_use_accumulate_ex(brush, ob->mode))
1643                 return;
1644
1645         /* threaded loop over vertices */
1646         SculptThreadedTaskData data = {
1647                 .C = C, .ob = ob, .wpd = wpd, .wpi = wpi, .me = me,
1648         };
1649
1650         ParallelRangeSettings settings;
1651         BLI_parallel_range_settings_defaults(&settings);
1652         BLI_task_parallel_range(
1653                 0, me->totvert,
1654                 &data,
1655                 do_wpaint_precompute_weight_cb_ex,
1656                 &settings);
1657
1658         wpd->precomputed_weight_ready = true;
1659 }
1660
1661 static void do_wpaint_brush_blur_task_cb_ex(
1662         void *__restrict userdata,
1663         const int n,
1664         const ParallelRangeTLS *__restrict UNUSED(tls))
1665 {
1666         SculptThreadedTaskData *data = userdata;
1667         SculptSession *ss = data->ob->sculpt;
1668         const PBVHType pbvh_type = BKE_pbvh_type(ss->pbvh);
1669         const bool has_grids = (pbvh_type == PBVH_GRIDS);
1670         const struct SculptVertexPaintGeomMap *gmap = &ss->mode.wpaint.gmap;
1671
1672         const Brush *brush = data->brush;
1673         const StrokeCache *cache = ss->cache;
1674         Scene *scene = CTX_data_scene(data->C);
1675
1676         float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1677         get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1678         const bool use_normal = vwpaint_use_normal(data->vp);
1679         const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1680         const bool use_vert_sel = (data->me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
1681
1682         SculptBrushTest test;
1683         SculptBrushTestFn sculpt_brush_test_sq_fn =
1684                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
1685         const float *sculpt_normal_frontface =
1686                 sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape);
1687
1688         /* For each vertex */
1689         PBVHVertexIter vd;
1690         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1691         {
1692                 /* Test to see if the vertex coordinates are within the spherical brush region. */
1693                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
1694                         /* For grid based pbvh, take the vert whose loop coopresponds to the current grid.
1695                          * Otherwise, take the current vert. */
1696                         const int v_index = has_grids ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i];
1697                         const float grid_alpha = has_grids ? 1.0f / vd.gridsize : 1.0f;
1698                         const char v_flag = data->me->mvert[v_index].flag;
1699                         /* If the vertex is selected */
1700                         if (!(use_face_sel || use_vert_sel) || v_flag & SELECT) {
1701                                 /* Get the average poly weight */
1702                                 int total_hit_loops = 0;
1703                                 float weight_final = 0.0f;
1704                                 for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) {
1705                                         const int p_index = gmap->vert_to_poly[v_index].indices[j];
1706                                         const MPoly *mp = &data->me->mpoly[p_index];
1707
1708                                         total_hit_loops += mp->totloop;
1709                                         for (int k = 0; k < mp->totloop; k++) {
1710                                                 const int l_index = mp->loopstart + k;
1711                                                 const MLoop *ml = &data->me->mloop[l_index];
1712                                                 weight_final += data->wpd->precomputed_weight[ml->v];
1713                                         }
1714                                 }
1715
1716                                 /* Apply the weight to the vertex. */
1717                                 if (total_hit_loops != 0) {
1718                                         float brush_strength = cache->bstrength;
1719                                         const float angle_cos = (use_normal && vd.no) ?
1720                                                 dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f;
1721                                         if (((brush->flag & BRUSH_FRONTFACE) == 0 ||
1722                                              (angle_cos > 0.0f)) &&
1723                                             ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 ||
1724                                              view_angle_limits_apply_falloff(&data->wpd->normal_angle_precalc, angle_cos, &brush_strength)))
1725                                         {
1726                                                 const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius);
1727                                                 const float final_alpha =
1728                                                         brush_fade * brush_strength *
1729                                                         grid_alpha * brush_alpha_pressure;
1730
1731                                                 if ((brush->flag & BRUSH_ACCUMULATE) == 0) {
1732                                                         if (ss->mode.wpaint.alpha_weight[v_index] < final_alpha) {
1733                                                                 ss->mode.wpaint.alpha_weight[v_index] = final_alpha;
1734                                                         }
1735                                                         else {
1736                                                                 continue;
1737                                                         }
1738                                                 }
1739
1740                                                 weight_final /= total_hit_loops;
1741                                                 /* Only paint visible verts */
1742                                                 do_weight_paint_vertex(
1743                                                         data->vp, data->ob, data->wpi,
1744                                                         v_index, final_alpha, weight_final);
1745                                         }
1746                                 }
1747                         }
1748                 }
1749         }
1750         BKE_pbvh_vertex_iter_end;
1751 }
1752
1753 static void do_wpaint_brush_smear_task_cb_ex(
1754         void *__restrict userdata,
1755         const int n,
1756         const ParallelRangeTLS *__restrict UNUSED(tls))
1757 {
1758         SculptThreadedTaskData *data = userdata;
1759         SculptSession *ss = data->ob->sculpt;
1760         const PBVHType pbvh_type = BKE_pbvh_type(ss->pbvh);
1761         const bool has_grids = (pbvh_type == PBVH_GRIDS);
1762         const struct SculptVertexPaintGeomMap *gmap = &ss->mode.wpaint.gmap;
1763
1764         const Brush *brush = data->brush;
1765         const Scene *scene = CTX_data_scene(data->C);
1766         const StrokeCache *cache = ss->cache;
1767         float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1768         get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1769         const bool use_normal = vwpaint_use_normal(data->vp);
1770         const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1771         const bool use_vert_sel = (data->me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
1772         float brush_dir[3];
1773
1774         sub_v3_v3v3(brush_dir, cache->location, cache->last_location);
1775         project_plane_v3_v3v3(brush_dir, brush_dir, cache->view_normal);
1776
1777         if (cache->is_last_valid && (normalize_v3(brush_dir) != 0.0f)) {
1778
1779                 SculptBrushTest test;
1780                 SculptBrushTestFn sculpt_brush_test_sq_fn =
1781                         sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
1782                 const float *sculpt_normal_frontface =
1783                         sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape);
1784
1785                 /* For each vertex */
1786                 PBVHVertexIter vd;
1787                 BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1788                 {
1789                         /* Test to see if the vertex coordinates are within the spherical brush region. */
1790                         if (sculpt_brush_test_sq_fn(&test, vd.co)) {
1791                                 /* For grid based pbvh, take the vert whose loop cooresponds to the current grid.
1792                                  * Otherwise, take the current vert. */
1793                                 const int v_index = has_grids ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i];
1794                                 const float grid_alpha = has_grids ? 1.0f / vd.gridsize : 1.0f;
1795                                 const MVert *mv_curr = &data->me->mvert[v_index];
1796
1797                                 /* If the vertex is selected */
1798                                 if (!(use_face_sel || use_vert_sel) || mv_curr->flag & SELECT) {
1799                                         float brush_strength = cache->bstrength;
1800                                         const float angle_cos = (use_normal && vd.no) ?
1801                                                 dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f;
1802                                         if (((brush->flag & BRUSH_FRONTFACE) == 0 ||
1803                                              (angle_cos > 0.0f)) &&
1804                                             ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 ||
1805                                              view_angle_limits_apply_falloff(&data->wpd->normal_angle_precalc, angle_cos, &brush_strength)))
1806                                         {
1807                                                 bool do_color = false;
1808                                                 /* Minimum dot product between brush direction and current
1809                                                  * to neighbor direction is 0.0, meaning orthogonal. */
1810                                                 float stroke_dot_max = 0.0f;
1811
1812                                                 /* Get the color of the loop in the opposite direction of the brush movement
1813                                                  * (this callback is specifically for smear.) */
1814                                                 float weight_final = 0.0;
1815                                                 for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) {
1816                                                         const int p_index = gmap->vert_to_poly[v_index].indices[j];
1817                                                         const MPoly *mp = &data->me->mpoly[p_index];
1818                                                         const MLoop *ml_other = &data->me->mloop[mp->loopstart];
1819                                                         for (int k = 0; k < mp->totloop; k++, ml_other++) {
1820                                                                 const uint v_other_index = ml_other->v;
1821                                                                 if (v_other_index != v_index) {
1822                                                                         const MVert *mv_other = &data->me->mvert[v_other_index];
1823
1824                                                                         /* Get the direction from the selected vert to the neighbor. */
1825                                                                         float other_dir[3];
1826                                                                         sub_v3_v3v3(other_dir, mv_curr->co, mv_other->co);
1827                                                                         project_plane_v3_v3v3(other_dir, other_dir, cache->view_normal);
1828
1829                                                                         normalize_v3(other_dir);
1830
1831                                                                         const float stroke_dot = dot_v3v3(other_dir, brush_dir);
1832
1833                                                                         if (stroke_dot > stroke_dot_max) {
1834                                                                                 stroke_dot_max = stroke_dot;
1835                                                                                 weight_final = data->wpd->precomputed_weight[v_other_index];
1836                                                                                 do_color = true;
1837                                                                         }
1838                                                                 }
1839                                                         }
1840                                                 }
1841                                                 /* Apply weight to vertex */
1842                                                 if (do_color) {
1843                                                         const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius);
1844                                                         const float final_alpha =
1845                                                                 brush_fade * brush_strength *
1846                                                                 grid_alpha * brush_alpha_pressure;
1847
1848                                                         if (final_alpha <= 0.0f)
1849                                                                 continue;
1850
1851                                                         do_weight_paint_vertex(
1852                                                                 data->vp, data->ob, data->wpi,
1853                                                                 v_index, final_alpha, (float)weight_final);
1854                                                 }
1855                                         }
1856                                 }
1857                         }
1858                 }
1859                 BKE_pbvh_vertex_iter_end;
1860         }
1861 }
1862
1863 static void do_wpaint_brush_draw_task_cb_ex(
1864         void *__restrict userdata,
1865         const int n,
1866         const ParallelRangeTLS *__restrict UNUSED(tls))
1867 {
1868         SculptThreadedTaskData *data = userdata;
1869         SculptSession *ss = data->ob->sculpt;
1870         const PBVHType pbvh_type = BKE_pbvh_type(ss->pbvh);
1871         const bool has_grids = (pbvh_type == PBVH_GRIDS);
1872         const Scene *scene = CTX_data_scene(data->C);
1873
1874         const Brush *brush = data->brush;
1875         const StrokeCache *cache = ss->cache;
1876         /* note: normally `BKE_brush_weight_get(scene, brush)` is used,
1877          * however in this case we calculate a new weight each time. */
1878         const float paintweight = data->strength;
1879         float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1880         get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1881         const bool use_normal = vwpaint_use_normal(data->vp);
1882         const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1883         const bool use_vert_sel = (data->me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
1884
1885         SculptBrushTest test;
1886         SculptBrushTestFn sculpt_brush_test_sq_fn =
1887                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
1888         const float *sculpt_normal_frontface =
1889                 sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape);
1890
1891         /* For each vertex */
1892         PBVHVertexIter vd;
1893         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1894         {
1895                 /* Test to see if the vertex coordinates are within the spherical brush region. */
1896                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
1897                         /* Note: grids are 1:1 with corners (aka loops).
1898                          * For multires, take the vert whose loop cooresponds to the current grid.
1899                          * Otherwise, take the current vert. */
1900                         const int v_index = has_grids ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i];
1901                         const float grid_alpha = has_grids ? 1.0f / vd.gridsize : 1.0f;
1902
1903                         const char v_flag = data->me->mvert[v_index].flag;
1904                         /* If the vertex is selected */
1905                         if (!(use_face_sel || use_vert_sel) || v_flag & SELECT) {
1906                                 float brush_strength = cache->bstrength;
1907                                 const float angle_cos = (use_normal && vd.no) ?
1908                                         dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f;
1909                                 if (((brush->flag & BRUSH_FRONTFACE) == 0 ||
1910                                      (angle_cos > 0.0f)) &&
1911                                     ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 ||
1912                                      view_angle_limits_apply_falloff(&data->wpd->normal_angle_precalc, angle_cos, &brush_strength)))
1913                                 {
1914                                         const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius);
1915                                         const float final_alpha = brush_fade * brush_strength * grid_alpha * brush_alpha_pressure;
1916
1917                                         if ((brush->flag & BRUSH_ACCUMULATE) == 0) {
1918                                                 if (ss->mode.wpaint.alpha_weight[v_index] < final_alpha) {
1919                                                         ss->mode.wpaint.alpha_weight[v_index] = final_alpha;
1920                                                 }
1921                                                 else {
1922                                                         continue;
1923                                                 }
1924                                         }
1925
1926                                         do_weight_paint_vertex(
1927                                                 data->vp, data->ob, data->wpi,
1928                                                 v_index, final_alpha, paintweight);
1929                                 }
1930                         }
1931                 }
1932         }
1933         BKE_pbvh_vertex_iter_end;
1934 }
1935
1936 static void do_wpaint_brush_calc_average_weight_cb_ex(
1937         void *__restrict userdata,
1938         const int n,
1939         const ParallelRangeTLS *__restrict UNUSED(tls))
1940 {
1941         SculptThreadedTaskData *data = userdata;
1942         SculptSession *ss = data->ob->sculpt;
1943         StrokeCache *cache = ss->cache;
1944         const PBVHType pbvh_type = BKE_pbvh_type(ss->pbvh);
1945         const bool has_grids = (pbvh_type == PBVH_GRIDS);
1946
1947         const bool use_normal = vwpaint_use_normal(data->vp);
1948         const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1949         const bool use_vert_sel = (data->me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
1950
1951         struct WPaintAverageAccum *accum = (struct WPaintAverageAccum *)data->custom_data + n;
1952         accum->len = 0;
1953         accum->value = 0.0;
1954
1955         SculptBrushTest test;
1956         SculptBrushTestFn sculpt_brush_test_sq_fn =
1957                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
1958         const float *sculpt_normal_frontface =
1959                 sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape);
1960
1961         /* For each vertex */
1962         PBVHVertexIter vd;
1963         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
1964         {
1965                 /* Test to see if the vertex coordinates are within the spherical brush region. */
1966                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
1967                         const float angle_cos = (use_normal && vd.no) ?
1968                                 dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f;
1969                         if (angle_cos > 0.0 && BKE_brush_curve_strength(data->brush, sqrtf(test.dist), cache->radius) > 0.0) {
1970                                 const int v_index = has_grids ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i];
1971                                 // const float grid_alpha = has_grids ? 1.0f / vd.gridsize : 1.0f;
1972                                 const char v_flag = data->me->mvert[v_index].flag;
1973
1974                                 /* If the vertex is selected. */
1975                                 if (!(use_face_sel || use_vert_sel) || v_flag & SELECT) {
1976                                         const MDeformVert *dv = &data->me->dvert[v_index];
1977                                         accum->len += 1;
1978                                         accum->value += wpaint_get_active_weight(dv, data->wpi);
1979                                 }
1980                         }
1981                 }
1982         }
1983         BKE_pbvh_vertex_iter_end;
1984 }
1985
1986 static void calculate_average_weight(SculptThreadedTaskData *data, PBVHNode **UNUSED(nodes), int totnode)
1987 {
1988         struct WPaintAverageAccum *accum = MEM_mallocN(sizeof(*accum) * totnode, __func__);
1989         data->custom_data = accum;
1990
1991         ParallelRangeSettings settings;
1992         BLI_parallel_range_settings_defaults(&settings);
1993         settings.use_threading = ((data->sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT);
1994         BLI_task_parallel_range(
1995                 0, totnode,
1996                 data,
1997                 do_wpaint_brush_calc_average_weight_cb_ex,
1998                 &settings);
1999
2000         uint accum_len = 0;
2001         double accum_weight = 0.0;
2002         for (int i = 0; i < totnode; i++) {
2003                 accum_len += accum[i].len;
2004                 accum_weight += accum[i].value;
2005         }
2006         if (accum_len != 0) {
2007                 accum_weight /= accum_len;
2008                 data->strength = (float)accum_weight;
2009         }
2010
2011         MEM_SAFE_FREE(data->custom_data);  /* 'accum' */
2012 }
2013
2014
2015 static void wpaint_paint_leaves(
2016         bContext *C, Object *ob, Sculpt *sd, VPaint *vp, struct WPaintData *wpd, WeightPaintInfo *wpi,
2017         Mesh *me, PBVHNode **nodes, int totnode)
2018 {
2019         Scene *scene = CTX_data_scene(C);
2020         const Brush *brush = ob->sculpt->cache->brush;
2021
2022         /* threaded loop over nodes */
2023         SculptThreadedTaskData data = {
2024                  .C = C, .sd = sd, .ob = ob, .brush = brush, .nodes = nodes,
2025                  .vp = vp, .wpd = wpd, .wpi = wpi, .me = me,
2026         };
2027
2028         /* Use this so average can modify its weight without touching the brush. */
2029         data.strength = BKE_brush_weight_get(scene, brush);
2030
2031         ParallelRangeSettings settings;
2032         BLI_parallel_range_settings_defaults(&settings);
2033         /* NOTE: current mirroring code cannot be run in parallel */
2034         settings.use_threading = !(me->editflag & ME_EDIT_MIRROR_X);
2035
2036         switch ((eBrushWeightPaintTool)brush->weightpaint_tool) {
2037                 case WPAINT_TOOL_AVERAGE:
2038                         calculate_average_weight(&data, nodes, totnode);
2039                         BLI_task_parallel_range(
2040                                 0, totnode,
2041                                 &data,
2042                                 do_wpaint_brush_draw_task_cb_ex,
2043                                 &settings);
2044                         break;
2045                 case WPAINT_TOOL_SMEAR:
2046                         BLI_task_parallel_range(
2047                                 0, totnode,
2048                                 &data,
2049                                 do_wpaint_brush_smear_task_cb_ex,
2050                                 &settings);
2051                         break;
2052                 case WPAINT_TOOL_BLUR:
2053                         BLI_task_parallel_range(
2054                                 0, totnode,
2055                                 &data,
2056                                 do_wpaint_brush_blur_task_cb_ex,
2057                                 &settings);
2058                         break;
2059                 case WPAINT_TOOL_DRAW:
2060                         BLI_task_parallel_range(
2061                                 0, totnode,
2062                                 &data,
2063                                 do_wpaint_brush_draw_task_cb_ex,
2064                                 &settings);
2065                         break;
2066         }
2067 }
2068
2069 static PBVHNode **vwpaint_pbvh_gather_generic(
2070         Object *ob, VPaint *wp, Sculpt *sd, Brush *brush, int *r_totnode)
2071 {
2072         SculptSession *ss = ob->sculpt;
2073         const bool use_normal = vwpaint_use_normal(wp);
2074         PBVHNode **nodes = NULL;
2075
2076         /* Build a list of all nodes that are potentially within the brush's area of influence */
2077         if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
2078                 SculptSearchSphereData data = {
2079                         .ss = ss,
2080                         .sd = sd,
2081                         .radius_squared = ss->cache->radius_squared,
2082                         .original = true,
2083                 };
2084                 BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, r_totnode);
2085                 if (use_normal) {
2086                         sculpt_pbvh_calc_area_normal(brush, ob, nodes, *r_totnode, true, ss->cache->sculpt_normal_symm);
2087                 }
2088                 else {
2089                         zero_v3(ss->cache->sculpt_normal_symm);
2090                 }
2091         }
2092         else {
2093                 struct DistRayAABB_Precalc dist_ray_to_aabb_precalc;
2094                 dist_squared_ray_to_aabb_v3_precalc(&dist_ray_to_aabb_precalc, ss->cache->location, ss->cache->view_normal);
2095                 SculptSearchCircleData data = {
2096                         .ss = ss,
2097                         .sd = sd,
2098                         .radius_squared = ss->cache->radius_squared,
2099                         .original = true,
2100                         .dist_ray_to_aabb_precalc = &dist_ray_to_aabb_precalc,
2101                 };
2102                 BKE_pbvh_search_gather(ss->pbvh, sculpt_search_circle_cb, &data, &nodes, r_totnode);
2103                 if (use_normal) {
2104                         copy_v3_v3(ss->cache->sculpt_normal_symm, ss->cache->view_normal);
2105                 }
2106                 else {
2107                         zero_v3(ss->cache->sculpt_normal_symm);
2108                 }
2109         }
2110         return nodes;
2111 }
2112
2113 static void wpaint_do_paint(
2114         bContext *C, Object *ob, VPaint *wp, Sculpt *sd, struct WPaintData *wpd, WeightPaintInfo *wpi,
2115         Mesh *me, Brush *brush, const char symm, const int axis, const int i, const float angle)
2116 {
2117         SculptSession *ss = ob->sculpt;
2118         ss->cache->radial_symmetry_pass = i;
2119         sculpt_cache_calc_brushdata_symm(ss->cache, symm, axis, angle);
2120
2121         int totnode;
2122         PBVHNode **nodes = vwpaint_pbvh_gather_generic(ob, wp, sd, brush, &totnode);
2123
2124         wpaint_paint_leaves(C, ob, sd, wp, wpd, wpi, me, nodes, totnode);
2125
2126         if (nodes)
2127                 MEM_freeN(nodes);
2128 }
2129
2130 static void wpaint_do_radial_symmetry(
2131         bContext *C, Object *ob, VPaint *wp, Sculpt *sd, struct WPaintData *wpd, WeightPaintInfo *wpi,
2132         Mesh *me, Brush *brush, const char symm, const int axis)
2133 {
2134         for (int i = 1; i < wp->radial_symm[axis - 'X']; i++) {
2135                 const float angle = (2.0 * M_PI) * i / wp->radial_symm[axis - 'X'];
2136                 wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, symm, axis, i, angle);
2137         }
2138 }
2139
2140 /* near duplicate of: sculpt.c's,
2141  * 'do_symmetrical_brush_actions' and 'vpaint_do_symmetrical_brush_actions'. */
2142 static void wpaint_do_symmetrical_brush_actions(
2143         bContext *C, Object *ob, VPaint *wp, Sculpt *sd, struct WPaintData *wpd, WeightPaintInfo *wpi)
2144 {
2145         Brush *brush = BKE_paint_brush(&wp->paint);
2146         Mesh *me = ob->data;
2147         SculptSession *ss = ob->sculpt;
2148         StrokeCache *cache = ss->cache;
2149         const char symm = wp->paint.symmetry_flags & PAINT_SYMM_AXIS_ALL;
2150         int i = 0;
2151
2152         /* initial stroke */
2153         wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, 0, 'X', 0, 0);
2154         wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, 0, 'X');
2155         wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, 0, 'Y');
2156         wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, 0, 'Z');
2157
2158         cache->symmetry = symm;
2159
2160         /* symm is a bit combination of XYZ - 1 is mirror
2161          * X; 2 is Y; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */
2162         for (i = 1; i <= symm; i++) {
2163                 if ((symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5)))) {
2164                         cache->mirror_symmetry_pass = i;
2165                         cache->radial_symmetry_pass = 0;
2166                         sculpt_cache_calc_brushdata_symm(cache, i, 0, 0);
2167
2168                         if (i & (1 << 0)) {
2169                                 wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, i, 'X', 0, 0);
2170                                 wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, i, 'X');
2171                         }
2172                         if (i & (1 << 1)) {
2173                                 wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, i, 'Y', 0, 0);
2174                                 wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, i, 'Y');
2175                         }
2176                         if (i & (1 << 2)) {
2177                                 wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, i, 'Z', 0, 0);
2178                                 wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, i, 'Z');
2179                         }
2180                 }
2181         }
2182         copy_v3_v3(cache->true_last_location, cache->true_location);
2183         cache->is_last_valid = true;
2184 }
2185
2186 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
2187 {
2188         Scene *scene = CTX_data_scene(C);
2189         ToolSettings *ts = CTX_data_tool_settings(C);
2190         VPaint *wp = ts->wpaint;
2191         Brush *brush = BKE_paint_brush(&wp->paint);
2192         struct WPaintData *wpd = paint_stroke_mode_data(stroke);
2193         ViewContext *vc;
2194         Object *ob = CTX_data_active_object(C);
2195
2196         SculptSession *ss = ob->sculpt;
2197         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
2198
2199         vwpaint_update_cache_variants(C, wp, ob, itemptr);
2200
2201         float mat[4][4];
2202         float mval[2];
2203
2204         const float brush_alpha_value = BKE_brush_alpha_get(scene, brush);
2205
2206         /* intentionally don't initialize as NULL, make sure we initialize all members below */
2207         WeightPaintInfo wpi;
2208
2209         /* cannot paint if there is no stroke data */
2210         if (wpd == NULL) {
2211                 /* XXX: force a redraw here, since even though we can't paint,
2212                  * at least view won't freeze until stroke ends */
2213                 ED_region_tag_redraw(CTX_wm_region(C));
2214                 return;
2215         }
2216
2217         vc = &wpd->vc;
2218         ob = vc->obact;
2219
2220         view3d_operator_needs_opengl(C);
2221         ED_view3d_init_mats_rv3d(ob, vc->rv3d);
2222
2223         /* load projection matrix */
2224         mul_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat);
2225
2226
2227         /* *** setup WeightPaintInfo - pass onto do_weight_paint_vertex *** */
2228         wpi.defbase_tot =        wpd->defbase_tot;
2229         wpi.defbase_sel =        wpd->defbase_sel;
2230         wpi.defbase_tot_sel =    wpd->defbase_tot_sel;
2231
2232         wpi.defbase_tot_unsel =  wpi.defbase_tot - wpi.defbase_tot_sel;
2233         wpi.active =             wpd->active;
2234         wpi.mirror =             wpd->mirror;
2235         wpi.lock_flags =         wpd->lock_flags;
2236         wpi.vgroup_validmap =    wpd->vgroup_validmap;
2237         wpi.do_flip =            RNA_boolean_get(itemptr, "pen_flip");
2238         wpi.do_multipaint =      wpd->do_multipaint;
2239         wpi.do_auto_normalize =  ((ts->auto_normalize != 0) && (wpi.vgroup_validmap != NULL));
2240         wpi.brush_alpha_value =  brush_alpha_value;
2241         /* *** done setting up WeightPaintInfo *** */
2242
2243         if (wpd->precomputed_weight) {
2244                 precompute_weight_values(C, ob, brush, wpd, &wpi, ob->data);
2245         }
2246
2247         wpaint_do_symmetrical_brush_actions(C, ob, wp, sd, wpd, &wpi);
2248
2249         swap_m4m4(vc->rv3d->persmat, mat);
2250
2251         /* calculate pivot for rotation around seletion if needed */
2252         /* also needed for "View Selected" on last stroke */
2253         paint_last_stroke_update(scene, vc->ar, mval);
2254
2255         BKE_mesh_batch_cache_dirty_tag(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
2256
2257         DEG_id_tag_update(ob->data, 0);
2258         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2259         swap_m4m4(wpd->vc.rv3d->persmat, mat);
2260
2261         rcti r;
2262         if (sculpt_get_redraw_rect(vc->ar, CTX_wm_region_view3d(C), ob, &r)) {
2263                 if (ss->cache) {
2264                         ss->cache->current_r = r;
2265                 }
2266
2267                 /* previous is not set in the current cache else
2268                  * the partial rect will always grow */
2269                 if (ss->cache) {
2270                         if (!BLI_rcti_is_empty(&ss->cache->previous_r))
2271                                 BLI_rcti_union(&r, &ss->cache->previous_r);
2272                 }
2273
2274                 r.xmin += vc->ar->winrct.xmin - 2;
2275                 r.xmax += vc->ar->winrct.xmin + 2;
2276                 r.ymin += vc->ar->winrct.ymin - 2;
2277                 r.ymax += vc->ar->winrct.ymin + 2;
2278
2279                 ss->partial_redraw = 1;
2280         }
2281         ED_region_tag_redraw_partial(vc->ar, &r);
2282 }
2283
2284 static void wpaint_stroke_done(const bContext *C, struct PaintStroke *stroke)
2285 {
2286         Object *ob = CTX_data_active_object(C);
2287         struct WPaintData *wpd = paint_stroke_mode_data(stroke);
2288
2289         if (wpd) {
2290                 if (wpd->defbase_sel)
2291                         MEM_freeN((void *)wpd->defbase_sel);
2292                 if (wpd->vgroup_validmap)
2293                         MEM_freeN((void *)wpd->vgroup_validmap);
2294                 if (wpd->lock_flags)
2295                         MEM_freeN((void *)wpd->lock_flags);
2296                 if (wpd->active.lock)
2297                         MEM_freeN((void *)wpd->active.lock);
2298                 if (wpd->mirror.lock)
2299                         MEM_freeN((void *)wpd->mirror.lock);
2300                 if (wpd->precomputed_weight)
2301                         MEM_freeN(wpd->precomputed_weight);
2302
2303                 MEM_freeN(wpd);
2304         }
2305
2306         /* and particles too */
2307         if (ob->particlesystem.first) {
2308                 ParticleSystem *psys;
2309                 int i;
2310
2311                 for (psys = ob->particlesystem.first; psys; psys = psys->next) {
2312                         for (i = 0; i < PSYS_TOT_VG; i++) {
2313                                 if (psys->vgroup[i] == ob->actdef) {
2314                                         psys->recalc |= ID_RECALC_PSYS_RESET;
2315                                         break;
2316                                 }
2317                         }
2318                 }
2319         }
2320
2321         DEG_id_tag_update(ob->data, 0);
2322
2323         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
2324
2325         sculpt_cache_free(ob->sculpt->cache);
2326         ob->sculpt->cache = NULL;
2327 }
2328
2329
2330 static int wpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2331 {
2332         int retval;
2333
2334         op->customdata = paint_stroke_new(
2335                 C, op, sculpt_stroke_get_location, wpaint_stroke_test_start,
2336                 wpaint_stroke_update_step, NULL,
2337                 wpaint_stroke_done, event->type);
2338
2339         if ((retval = op->type->modal(C, op, event)) == OPERATOR_FINISHED) {
2340                 paint_stroke_data_free(op);
2341                 return OPERATOR_FINISHED;
2342         }
2343         /* add modal handler */
2344         WM_event_add_modal_handler(C, op);
2345
2346         OPERATOR_RETVAL_CHECK(retval);
2347         BLI_assert(retval == OPERATOR_RUNNING_MODAL);
2348
2349         return OPERATOR_RUNNING_MODAL;
2350 }
2351
2352 static int wpaint_exec(bContext *C, wmOperator *op)
2353 {
2354         op->customdata = paint_stroke_new(
2355                 C, op, sculpt_stroke_get_location, wpaint_stroke_test_start,
2356                 wpaint_stroke_update_step, NULL,
2357                 wpaint_stroke_done, 0);
2358
2359         /* frees op->customdata */
2360         paint_stroke_exec(C, op);
2361
2362         return OPERATOR_FINISHED;
2363 }
2364
2365 static void wpaint_cancel(bContext *C, wmOperator *op)
2366 {
2367         Object *ob = CTX_data_active_object(C);
2368         if (ob->sculpt->cache) {
2369                 sculpt_cache_free(ob->sculpt->cache);
2370                 ob->sculpt->cache = NULL;
2371         }
2372
2373         paint_stroke_cancel(C, op);
2374 }
2375
2376 void PAINT_OT_weight_paint(wmOperatorType *ot)
2377 {
2378         /* identifiers */
2379         ot->name = "Weight Paint";
2380         ot->idname = "PAINT_OT_weight_paint";
2381         ot->description = "Paint a stroke in the current vertex group's weights";
2382
2383         /* api callbacks */
2384         ot->invoke = wpaint_invoke;
2385         ot->modal = paint_stroke_modal;
2386         ot->exec = wpaint_exec;
2387         ot->poll = weight_paint_poll;
2388         ot->cancel = wpaint_cancel;
2389
2390         /* flags */
2391         ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING;
2392
2393         paint_stroke_operator_properties(ot);
2394 }
2395
2396 /* ************ set / clear vertex paint mode ********** */
2397
2398 /**
2399  * \note Keep in sync with #wpaint_mode_toggle_exec
2400  */
2401 static int vpaint_mode_toggle_exec(bContext *C, wmOperator *op)
2402 {
2403         Main *bmain = CTX_data_main(C);
2404         struct wmMsgBus *mbus = CTX_wm_message_bus(C);
2405         Object *ob = CTX_data_active_object(C);
2406         const int mode_flag = OB_MODE_VERTEX_PAINT;
2407         const bool is_mode_set = (ob->mode & mode_flag) != 0;
2408         Scene *scene = CTX_data_scene(C);
2409         ToolSettings *ts = scene->toolsettings;
2410
2411         if (!is_mode_set) {
2412                 if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
2413                         return OPERATOR_CANCELLED;
2414                 }
2415         }
2416
2417         Mesh *me = BKE_mesh_from_object(ob);
2418
2419         /* toggle: end vpaint */
2420         if (is_mode_set) {
2421                 ED_object_vpaintmode_exit_ex(ob);
2422         }
2423         else {
2424                 Depsgraph *depsgraph = CTX_data_depsgraph_on_load(C);
2425                 wmWindowManager *wm = CTX_wm_manager(C);
2426                 ED_object_vpaintmode_enter_ex(bmain, depsgraph, wm, scene, ob);
2427                 BKE_paint_toolslots_brush_validate(bmain, &ts->vpaint->paint);
2428         }
2429
2430         BKE_mesh_batch_cache_dirty_tag(ob->data, BKE_MESH_BATCH_DIRTY_ALL);
2431
2432         /* update modifier stack for mapping requirements */
2433         DEG_id_tag_update(&me->id, 0);
2434
2435         WM_event_add_notifier(C, NC_SCENE | ND_MODE, scene);
2436
2437         WM_msg_publish_rna_prop(mbus, &ob->id, ob, Object, mode);
2438
2439         WM_toolsystem_update_from_context_view3d(C);
2440
2441         return OPERATOR_FINISHED;
2442 }
2443
2444 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
2445 {
2446         /* identifiers */
2447         ot->name = "Vertex Paint Mode";
2448         ot->idname = "PAINT_OT_vertex_paint_toggle";
2449         ot->description = "Toggle the vertex paint mode in 3D view";
2450
2451         /* api callbacks */
2452         ot->exec = vpaint_mode_toggle_exec;
2453         ot->poll = paint_poll_test;
2454
2455         /* flags */
2456         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_USE_EVAL_DATA;
2457 }
2458
2459
2460
2461 /* ********************** vertex paint operator ******************* */
2462
2463 /* Implementation notes:
2464  *
2465  * Operator->invoke()
2466  * - validate context (add mcol)
2467  * - create customdata storage
2468  * - call paint once (mouse click)
2469  * - add modal handler
2470  *
2471  * Operator->modal()
2472  * - for every mousemove, apply vertex paint
2473  * - exit on mouse release, free customdata
2474  *   (return OPERATOR_FINISHED also removes handler and operator)
2475  *
2476  * For future:
2477  * - implement a stroke event (or mousemove with past positions)
2478  * - revise whether op->customdata should be added in object, in set_vpaint
2479  */
2480
2481 typedef struct PolyFaceMap {
2482         struct PolyFaceMap *next, *prev;
2483         int facenr;
2484 } PolyFaceMap;
2485
2486 struct VPaintData {
2487         ViewContext vc;
2488         struct NormalAnglePrecalc normal_angle_precalc;
2489
2490         uint paintcol;
2491
2492         struct VertProjHandle *vp_handle;
2493         struct CoNo *vertexcosnos;
2494
2495         /* modify 'me->mcol' directly, since the derived mesh is drawing from this
2496          * array, otherwise we need to refresh the modifier stack */
2497         bool use_fast_update;
2498
2499         /* loops tagged as having been painted, to apply shared vertex color
2500          * blending only to modified loops */
2501         bool *mlooptag;
2502
2503         bool is_texbrush;
2504
2505         /* Special storage for smear brush, avoid feedback loop - update each step. */
2506         struct {
2507                 uint *color_prev;
2508                 uint *color_curr;
2509         } smear;
2510 };
2511
2512 static bool vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const float mouse[2])
2513 {
2514         Scene *scene = CTX_data_scene(C);
2515         ToolSettings *ts = scene->toolsettings;
2516         struct PaintStroke *stroke = op->customdata;
2517         VPaint *vp = ts->vpaint;
2518         Brush *brush = BKE_paint_brush(&vp->paint);
2519         struct VPaintData *vpd;
2520         Object *ob = CTX_data_active_object(C);
2521         Mesh *me;
2522         SculptSession *ss = ob->sculpt;
2523         Depsgraph *depsgraph = CTX_data_depsgraph(C);
2524
2525         /* context checks could be a poll() */
2526         me = BKE_mesh_from_object(ob);
2527         if (me == NULL || me->totpoly == 0)
2528                 return false;
2529
2530         ED_mesh_color_ensure(me, NULL);
2531         if (me->mloopcol == NULL)
2532                 return false;
2533
2534         /* make mode data storage */
2535         vpd = MEM_callocN(sizeof(*vpd), "VPaintData");
2536         paint_stroke_set_mode_data(stroke, vpd);
2537         ED_view3d_viewcontext_init(C, &vpd->vc);
2538         view_angle_limits_init(
2539                 &vpd->normal_angle_precalc, vp->paint.brush->falloff_angle,
2540                 (vp->paint.brush->flag & BRUSH_FRONTFACE_FALLOFF) != 0);
2541
2542         vpd->paintcol = vpaint_get_current_col(scene, vp, (RNA_enum_get(op->ptr, "mode") == BRUSH_STROKE_INVERT));
2543
2544         vpd->is_texbrush = !(brush->vertexpaint_tool == VPAINT_TOOL_BLUR) && brush->mtex.tex;
2545
2546         /* are we painting onto a modified mesh?,
2547          * if not we can skip face map trickiness */
2548         if (vertex_paint_use_fast_update_check(ob)) {
2549                 vpd->use_fast_update = true;
2550 /*              printf("Fast update!\n");*/
2551         }
2552         else {
2553                 vpd->use_fast_update = false;
2554 /*              printf("No fast update!\n");*/
2555         }
2556
2557         /* to keep tracked of modified loops for shared vertex color blending */
2558         if (brush->vertexpaint_tool == VPAINT_TOOL_BLUR) {
2559                 vpd->mlooptag = MEM_mallocN(sizeof(bool) * me->totloop, "VPaintData mlooptag");
2560         }
2561
2562         if (brush->vertexpaint_tool == VPAINT_TOOL_SMEAR) {
2563                 vpd->smear.color_prev = MEM_mallocN(sizeof(uint) * me->totloop, __func__);
2564                 memcpy(vpd->smear.color_prev, me->mloopcol, sizeof(uint) * me->totloop);
2565                 vpd->smear.color_curr = MEM_dupallocN(vpd->smear.color_prev);
2566         }
2567
2568         /* Create projection handle */
2569         if (vpd->is_texbrush) {
2570                 ob->sculpt->building_vp_handle = true;
2571                 vpd->vp_handle = ED_vpaint_proj_handle_create(depsgraph, scene, ob, &vpd->vertexcosnos);
2572                 ob->sculpt->building_vp_handle = false;
2573         }
2574
2575         /* If not previously created, create vertex/weight paint mode session data */
2576         vertex_paint_init_stroke(depsgraph, scene, ob);
2577         vwpaint_update_cache_invariants(C, vp, ss, op, mouse);
2578         vertex_paint_init_session_data(ts, ob);
2579
2580         if (ob->sculpt->mode.vpaint.previous_color != NULL) {
2581                 memset(ob->sculpt->mode.vpaint.previous_color, 0, sizeof(uint) * me->totloop);
2582         }
2583
2584         return 1;
2585 }
2586
2587 static void do_vpaint_brush_calc_average_color_cb_ex(
2588         void *__restrict userdata,
2589         const int n,
2590         const ParallelRangeTLS *__restrict UNUSED(tls))
2591 {
2592         SculptThreadedTaskData *data = userdata;
2593         SculptSession *ss = data->ob->sculpt;
2594         const PBVHType pbvh_type = BKE_pbvh_type(ss->pbvh);
2595         const bool has_grids = (pbvh_type == PBVH_GRIDS);
2596         const struct SculptVertexPaintGeomMap *gmap = &ss->mode.vpaint.gmap;
2597
2598         StrokeCache *cache = ss->cache;
2599         uint *lcol = data->lcol;
2600         char *col;
2601         const bool use_vert_sel = (data->me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0;
2602
2603         struct VPaintAverageAccum *accum = (struct VPaintAverageAccum *)data->custom_data + n;
2604         accum->len = 0;
2605         memset(accum->value, 0, sizeof(accum->value));
2606
2607         SculptBrushTest test;
2608         SculptBrushTestFn sculpt_brush_test_sq_fn =
2609                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2610
2611         /* For each vertex */
2612         PBVHVertexIter vd;
2613         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2614         {
2615                 /* Test to see if the vertex coordinates are within the spherical brush region. */
2616                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2617                         const int v_index = has_grids ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i];
2618                         if (BKE_brush_curve_strength(data->brush, 0.0, cache->radius) > 0.0) {
2619                                 /* If the vertex is selected for painting. */
2620                                 const MVert *mv = &data->me->mvert[v_index];
2621                                 if (!use_vert_sel || mv->flag & SELECT) {
2622                                         accum->len += gmap->vert_to_loop[v_index].count;
2623                                         /* if a vertex is within the brush region, then add it's color to the blend. */
2624                                         for (int j = 0; j < gmap->vert_to_loop[v_index].count; j++) {
2625                                                 const int l_index = gmap->vert_to_loop[v_index].indices[j];
2626                                                 col = (char *)(&lcol[l_index]);
2627                                                 /* Color is squared to compensate the sqrt color encoding. */
2628                                                 accum->value[0] += col[0] * col[0];
2629                                                 accum->value[1] += col[1] * col[1];
2630                                                 accum->value[2] += col[2] * col[2];
2631                                         }
2632                                 }
2633                         }
2634                 }
2635         }
2636         BKE_pbvh_vertex_iter_end;
2637 }
2638
2639 static float tex_color_alpha_ubyte(
2640         SculptThreadedTaskData *data, const float v_co[3],
2641         uint *r_color)
2642 {
2643         float rgba[4];
2644         float rgba_br[3];
2645         tex_color_alpha(data->vp, &data->vpd->vc, v_co, rgba);
2646         rgb_uchar_to_float(rgba_br, (const uchar *)&data->vpd->paintcol);
2647         mul_v3_v3(rgba_br, rgba);
2648         rgb_float_to_uchar((uchar *)r_color, rgba_br);
2649         return rgba[3];
2650 }
2651
2652 static void do_vpaint_brush_draw_task_cb_ex(
2653         void *__restrict userdata,
2654         const int n,
2655         const ParallelRangeTLS *__restrict UNUSED(tls))
2656 {
2657         SculptThreadedTaskData *data = userdata;
2658         SculptSession *ss = data->ob->sculpt;
2659         const PBVHType pbvh_type = BKE_pbvh_type(ss->pbvh);
2660         const bool has_grids = (pbvh_type == PBVH_GRIDS);
2661         const struct SculptVertexPaintGeomMap *gmap = &ss->mode.vpaint.gmap;
2662
2663         const Brush *brush = data->brush;
2664         const StrokeCache *cache = ss->cache;
2665         uint *lcol = data->lcol;
2666         const Scene *scene = CTX_data_scene(data->C);
2667         float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
2668         get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
2669         const bool use_normal = vwpaint_use_normal(data->vp);
2670         const bool use_vert_sel = (data->me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0;
2671         const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
2672
2673         SculptBrushTest test;
2674         SculptBrushTestFn sculpt_brush_test_sq_fn =
2675                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2676         const float *sculpt_normal_frontface =
2677                 sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape);
2678
2679         /* For each vertex */
2680         PBVHVertexIter vd;
2681         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2682         {
2683                 /* Test to see if the vertex coordinates are within the spherical brush region. */
2684                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2685                         /* Note: Grids are 1:1 with corners (aka loops).
2686                          * For grid based pbvh, take the vert whose loop cooresponds to the current grid.
2687                          * Otherwise, take the current vert. */
2688                         const int v_index = has_grids ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i];
2689                         const float grid_alpha = has_grids ? 1.0f / vd.gridsize : 1.0f;
2690                         const MVert *mv = &data->me->mvert[v_index];
2691
2692                         /* If the vertex is selected for painting. */
2693                         if (!use_vert_sel || mv->flag & SELECT) {
2694                                 /* Calc the dot prod. between ray norm on surf and current vert
2695                                  * (ie splash prevention factor), and only paint front facing verts. */
2696                                 float brush_strength = cache->bstrength;
2697                                 const float angle_cos = (use_normal && vd.no) ?
2698                                         dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f;
2699                                 if (((brush->flag & BRUSH_FRONTFACE) == 0 ||
2700                                      (angle_cos > 0.0f)) &&
2701                                     ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 ||
2702                                      view_angle_limits_apply_falloff(&data->vpd->normal_angle_precalc, angle_cos, &brush_strength)))
2703                                 {
2704                                         const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius);
2705                                         uint color_final = data->vpd->paintcol;
2706
2707                                         /* If we're painting with a texture, sample the texture color and alpha. */
2708                                         float tex_alpha = 1.0;
2709                                         if (data->vpd->is_texbrush) {
2710                                                 /* Note: we may want to paint alpha as vertex color alpha. */
2711                                                 tex_alpha = tex_color_alpha_ubyte(
2712                                                         data, data->vpd->vertexcosnos[v_index].co,
2713                                                         &color_final);
2714                                         }
2715                                         /* For each poly owning this vert, paint each loop belonging to this vert. */
2716                                         for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) {
2717                                                 const int p_index = gmap->vert_to_poly[v_index].indices[j];
2718                                                 const int l_index = gmap->vert_to_loop[v_index].indices[j];
2719                                                 BLI_assert(data->me->mloop[l_index].v == v_index);
2720                                                 const MPoly *mp = &data->me->mpoly[p_index];
2721                                                 if (!use_face_sel || mp->flag & ME_FACE_SEL) {
2722                                                         uint color_orig = 0;  /* unused when array is NULL */
2723                                                         if (ss->mode.vpaint.previous_color != NULL) {
2724                                                                 /* Get the previous loop color */
2725                                                                 if (ss->mode.vpaint.previous_color[l_index] == 0) {
2726                                                                         ss->mode.vpaint.previous_color[l_index] = lcol[l_index];
2727                                                                 }
2728                                                                 color_orig = ss->mode.vpaint.previous_color[l_index];
2729                                                         }
2730                                                         const float final_alpha =
2731                                                                 255 * brush_fade * brush_strength *
2732                                                                 tex_alpha * brush_alpha_pressure * grid_alpha;
2733
2734                                                         /* Mix the new color with the original based on final_alpha. */
2735                                                         lcol[l_index] = vpaint_blend(
2736                                                                 data->vp, lcol[l_index], color_orig, color_final,
2737                                                                 final_alpha, 255 * brush_strength);
2738                                                 }
2739                                         }
2740                                 }
2741                         }
2742                 }
2743         }
2744         BKE_pbvh_vertex_iter_end;
2745 }
2746
2747 static void do_vpaint_brush_blur_task_cb_ex(
2748         void *__restrict userdata,
2749         const int n,
2750         const ParallelRangeTLS *__restrict UNUSED(tls))
2751 {
2752         SculptThreadedTaskData *data = userdata;
2753         SculptSession *ss = data->ob->sculpt;
2754         const PBVHType pbvh_type = BKE_pbvh_type(ss->pbvh);
2755         const bool has_grids = (pbvh_type == PBVH_GRIDS);
2756
2757         Scene *scene = CTX_data_scene(data->C);
2758         const struct SculptVertexPaintGeomMap *gmap = &ss->mode.vpaint.gmap;
2759         const Brush *brush = data->brush;
2760         const StrokeCache *cache = ss->cache;
2761         uint *lcol = data->lcol;
2762         float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
2763         get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
2764         const bool use_normal = vwpaint_use_normal(data->vp);
2765         const bool use_vert_sel = (data->me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0;
2766         const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
2767
2768         SculptBrushTest test;
2769         SculptBrushTestFn sculpt_brush_test_sq_fn =
2770                 sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2771         const float *sculpt_normal_frontface =
2772                 sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape);
2773
2774         /* For each vertex */
2775         PBVHVertexIter vd;
2776         BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2777         {
2778                 /* Test to see if the vertex coordinates are within the spherical brush region. */
2779                 if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2780                         /* For grid based pbvh, take the vert whose loop cooresponds to the current grid.
2781                          * Otherwise, take the current vert. */
2782                         const int v_index = has_grids ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i];
2783                         const float grid_alpha = has_grids ? 1.0f / vd.gridsize : 1.0f;
2784                         const MVert *mv = &data->me->mvert[v_index];
2785
2786                         /* If the vertex is selected for painting. */
2787                         if (!use_vert_sel || mv->flag & SELECT) {
2788                                 float brush_strength = cache->bstrength;
2789                                 const float angle_cos = (use_normal && vd.no) ?
2790                                         dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f;
2791                                 if (((brush->flag & BRUSH_FRONTFACE) == 0 ||
2792                                      (angle_cos > 0.0f)) &&
2793                                     ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 ||
2794                                      view_angle_limits_apply_falloff(&data->vpd->normal_angle_precalc, angle_cos, &brush_strength)))
2795                                 {
2796                                         const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius);
2797
2798                                         /* Get the average poly color */
2799                                         uint color_final = 0;
2800                                         int total_hit_loops = 0;
2801                                         uint blend[4] = {0};
2802                                         for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) {
2803                                                 int p_index = gmap->vert_to_poly[v_index].indices[j];
2804                                                 const MPoly *mp = &data->me->mpoly[p_index];
2805                                                 if (!use_face_sel || mp->flag & ME_FACE_SEL) {
2806                                                         total_hit_loops += mp->totloop;
2807                                                         for (int k = 0; k < mp->totloop; k++) {
2808                                                                 const uint l_index = mp->loopstart + k;
2809                                                                 const char *col = (const char *)(&lcol[l_index]);
2810                                                                 /* Color is squared to compensate the sqrt color encoding. */
2811                                                                 blend[0] += (uint)col[0] * (uint)col[0];
2812                                                                 blend[1] += (uint)col[1] * (uint)col[1];
2813                                                                 blend[2] += (uint)col[2] * (uint)col[2];
2814                                                                 blend[3] += (uint)col[3] * (uint)col[3];
2815                                                         }
2816                                                 }
2817                                         }
2818                                         if (total_hit_loops != 0) {
2819                                                 /* Use rgb^2 color averaging. */
2820                                                 char *col = (char *)(&color_final);
2821                                                 col[0] = round_fl_to_uchar(sqrtf(divide_round_i(blend[0], total_hit_loops)));
2822                                                 col[1] = round_fl_to_uchar(sqrtf(divide_round_i(blend[1], total_hit_loops)));
2823                                                 col[2] = round_fl_to_uchar(sqrtf(divide_round_i(blend[2], total_hit_loops)));
2824                                                 col[3] = round_fl_to_uchar(sqrtf(divide_round_i(blend[3], total_hit_loops)));
2825
2826                                                 /* For each poly owning this vert,
2827                                                  * paint each loop belonging to this vert. */
2828                                                 for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) {
2829                                                         const int p_index = gmap->vert_to_poly[v_index].indices[j];
2830                                                         const int l_index = gmap->vert_to_loop[v_index].indices[j];
2831                                                         BLI_assert(data->me->mloop[l_index].v == v_index);
2832                                                         const MPoly *mp = &data->me->mpoly[p_index];
2833                                                         if (!use_face_sel || mp->flag & ME_FACE_SEL) {
2834                                                                 uint color_orig = 0;  /* unused when array is NULL */
2835                                                                 if (ss->mode.vpaint.previous_color != NULL) {
2836                                                                         /* Get the previous loop color */
2837                                                                         if (ss->mode.vpaint.previous_color[l_index] == 0) {
2838                                                                                 ss->mode.vpaint.previous_color[l_index] = lcol[l_index];
2839                                                                         }
2840                                                                         color_orig = ss->mode.vpaint.previous_color[l_index];
2841                                                                 }
2842                                                                 const float final_alpha =
2843                                                                         255 * brush_fade * brush_strength *
2844                                                                         brush_alpha_pressure * grid_alpha;
2845                                                                 /* Mix the new color with the original
2846                                                                  * based on the brush strength and the curve. */
2847                                                                 lcol[l_index] = vpaint_blend(
2848                                                                         data->vp, lcol[l_index], color_orig, *((uint *)col),
2849                                                                         final_alpha, 255 * brush_strength);
2850                                                         }
2851                                                 }
2852                                         }
2853                                 }
2854                         }
2855                 }
2856         }
2857         BKE_pbvh_vertex_iter_end;
2858 }
2859
2860 static void do_vpaint_brush_smear_task_cb_ex(
2861         void *__restrict userdata,
2862         const int n,
2863         const ParallelRangeTLS *__restrict UNUSED(tls))
2864 {
2865         SculptThreadedTaskData *data = userdata;
2866         SculptSession *ss = data->ob->sculpt;
2867         const PBVHType pbvh_type = BKE_pbvh_type(ss->pbvh);
2868         const bool has_grids = (pbvh_type == PBVH_GRIDS);
2869
2870         Scene *scene = CTX_data_scene(data->C);
2871         const struct SculptVertexPaintGeomMap *gmap = &ss->mode.vpaint.gmap;
2872         const Brush *brush = data->brush;
2873         const StrokeCache *cache = ss->cache;
2874         uint *lcol = data->lcol;
2875         float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
2876         get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
2877         float brush_dir[3];
2878         const bool use_normal = vwpaint_use_normal(data->vp);
2879         const bool use_vert_sel = (data->me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0;
2880         const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
2881
2882         sub_v3_v3v3(brush_dir, cache->location, cache->last_location);
2883         project_plane_v3_v3v3(brush_dir, brush_dir, cache->view_normal);
2884
2885         if (cache->is_last_valid && (normalize_v3(brush_dir) != 0.0f)) {
2886
2887                 SculptBrushTest test;
2888                 SculptBrushTestFn sculpt_brush_test_sq_fn =
2889                         sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape);
2890                 const float *sculpt_normal_frontface =
2891                         sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape);
2892
2893                 /* For each vertex */
2894                 PBVHVertexIter vd;
2895                 BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE)
2896                 {
2897                         /* Test to see if the vertex coordinates are within the spherical brush region. */
2898                         if (sculpt_brush_test_sq_fn(&test, vd.co)) {
2899                                 /* For grid based pbvh, take the vert whose loop cooresponds to the current grid.
2900                                  * Otherwise, take the current vert. */
2901                                 const int v_index = has_grids ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i];
2902                                 const float grid_alpha = has_grids ? 1.0f / vd.gridsize : 1.0f;
2903                                 const MVert *mv_curr = &data->me->mvert[v_index];
2904
2905                                 /* if the vertex is selected for painting. */
2906                                 if (!use_vert_sel || mv_curr->flag & SELECT) {
2907                                         /* Calc the dot prod. between ray norm on surf and current vert
2908                                          * (ie splash prevention factor), and only paint front facing verts. */
2909                                         float brush_strength = cache->bstrength;
2910                                         const float angle_cos = (use_normal && vd.no) ?
2911                                                 dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f;
2912                                         if (((brush->flag & BRUSH_FRONTFACE) == 0 ||
2913                                              (angle_cos > 0.0f)) &&
2914                                             ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 ||
2915                                              view_angle_limits_apply_falloff(&data->vpd->normal_angle_precalc, angle_cos, &brush_strength)))
2916                                         {
2917                                                 const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius);
2918
2919                                                 bool do_color = false;
2920                                                 /* Minimum dot product between brush direction and current
2921                                                  * to neighbor direction is 0.0, meaning orthogonal. */
2922                                                 float stroke_dot_max = 0.0f;
2923
2924                                                 /* Get the color of the loop in the opposite
2925                                                  * direction of the brush movement */
2926                                                 uint color_final = 0;
2927                                                 for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) {
2928                                                         const int p_index = gmap->vert_to_poly[v_index].indices[j];
2929                                                         const int l_index = gmap->vert_to_loop[v_index].indices[j];
2930                                                         BLI_assert(data->me->mloop[l_index].v == v_index);
2931                                                         UNUSED_VARS_NDEBUG(l_index);
2932                                                         const MPoly *mp = &data->me->mpoly[p_index];
2933                                                         if (!use_face_sel || mp->flag & ME_FACE_SEL) {
2934                                                                 const MLoop *ml_other = &data->me->mloop[mp->loopstart];
2935                                                                 for (int k = 0; k < mp->totloop; k++, ml_other++) {
2936                                                                         const uint v_other_index = ml_other->v;
2937                                                                         if (v_other_index != v_index) {
2938                                                                                 const MVert *mv_other = &data->me->mvert[v_other_index];
2939
2940                                                                                 /* Get the direction from the
2941                                                                                  * selected vert to the neighbor. */
2942                                                                                 float other_dir[3];
2943                                                                                 sub_v3_v3v3(other_dir, mv_curr->co, mv_other->co);
2944                                                                                 project_plane_v3_v3v3(other_dir, other_dir, cache->view_normal);
2945
2946                                                                                 normalize_v3(other_dir);
2947
2948                                                                                 const float stroke_dot = dot_v3v3(other_dir, brush_dir);
2949
2950                                                                                 if (stroke_dot > stroke_dot_max) {
2951                                                                                         stroke_dot_max = stroke_dot;
2952                                                                                         color_final = data->vpd->smear.color_prev[mp->loopstart + k];
2953