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