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