Blender V5.3
paint_vertex.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include "MEM_guardedalloc.h"
15
16#include "CLG_log.h"
17
18#include "BLI_color.hh"
19#include "BLI_color_mix.hh"
21#include "BLI_math_geom_c.hh"
22#include "BLI_math_matrix.hh"
23#include "BLI_task.hh"
24#include "BLI_vector.hh"
25
26#include "DNA_brush_types.h"
27#include "DNA_mesh_types.h"
28#include "DNA_meshdata_types.h"
29#include "DNA_object_types.h"
30#include "DNA_scene_types.h"
31
32#include "RNA_access.hh"
33#include "RNA_define.hh"
34
35#include "BKE_attribute.h"
36#include "BKE_attribute.hh"
37#include "BKE_brush.hh"
38#include "BKE_colortools.hh"
39#include "BKE_context.hh"
40#include "BKE_deform.hh"
41#include "BKE_editmesh.hh"
42#include "BKE_library.hh"
43#include "BKE_mesh.hh"
44#include "BKE_object.hh"
45#include "BKE_object_types.hh"
46#include "BKE_paint.hh"
47#include "BKE_paint_types.hh"
48
49#include "DEG_depsgraph.hh"
50
51#include "WM_api.hh"
52#include "WM_message.hh"
53#include "WM_toolsystem.hh"
54#include "WM_types.hh"
55
56#include "ED_image.hh"
57#include "ED_mesh.hh"
58#include "ED_object.hh"
59#include "ED_paint.hh"
60#include "ED_screen.hh"
61#include "ED_sculpt.hh"
62#include "ED_view3d.hh"
63
64/* For IMB_BlendMode only. */
65#include "IMB_imbuf.hh"
66
67#include "bmesh.hh"
68
69#include "../paint_intern.hh" /* own include */
70#include "mesh_brush_common.hh"
71#include "mesh_paint.hh"
72#include "sculpt_automask.hh"
73#include "sculpt_intern.hh"
74#include "sculpt_pose.hh"
75#include "vw_paint_intern.hh" /* own include */
76
77namespace blender {
78
79using bke::AttrDomain;
80using namespace color;
81using namespace ed::sculpt_paint; /* For vwpaint namespace. */
82
83static CLG_LogRef LOG = {"paint.vertex"};
84
85/* -------------------------------------------------------------------- */
88
89static bool isZero(ColorPaint4f c)
90{
91 return c.r == 0.0f && c.g == 0.0f && c.b == 0.0f && c.a == 0.0f;
92}
93
94static bool isZero(ColorPaint4b c)
95{
96 return c.r == 0 && c.g == 0 && c.b == 0 && c.a == 0;
97}
98
99template<typename Color> static ColorPaint4f toFloat(const Color &c)
100{
101 if constexpr (std::is_same_v<Color, ColorPaint4b>) {
102 return blender::color::decode(c);
103 }
104 else {
105 return c;
106 }
107}
108
109template<typename Color> static Color fromFloat(const ColorPaint4f &c)
110{
111 if constexpr (std::is_same_v<Color, ColorPaint4b>) {
112 return blender::color::encode(c);
113 }
114 else {
115 return c;
116 }
117}
118
119/* Use for 'blur' brush, align with pbvh::Tree nodes, created and freed on each update. */
120template<typename BlendType> struct VPaintAverageAccum {
121 BlendType len;
122 BlendType value[3];
123};
124
126
128
129/* -------------------------------------------------------------------- */
132
133bool test_brush_angle_falloff(const Brush &brush, const float angle_cos)
134{
135 if ((brush.flag & BRUSH_FRONTFACE) == 0) {
136 return true;
137 }
138
139 return angle_cos > 0.0f;
140}
141
142bool use_normal(const VPaint &vp)
143{
145 return ((brush.flag & BRUSH_FRONTFACE) != 0);
146}
147
149{
150 return ((brush.flag & BRUSH_ACCUMULATE) != 0 ||
151 (ob_mode == OB_MODE_VERTEX_PAINT ?
152 (brush.vertex_brush_type == VPAINT_BRUSH_TYPE_SMEAR) :
153 (brush.weight_brush_type == WPAINT_BRUSH_TYPE_SMEAR)));
154}
155
161
162void init_stroke(const wmOperator &op, Main &main, Paint &paint, Depsgraph &depsgraph, Object &ob)
163{
164 BKE_sculptsession_update_for_edit(&depsgraph, &ob, true);
166
167 /* Ensure ss.cache is allocated. It will mostly be initialized in
168 * vwpaint::update_cache_invariants and vwpaint::update_cache_variants.
169 */
170 if (!ss.cache) {
171 ss.cache = MEM_new<StrokeCache>(__func__);
173 }
174}
175
176void update_sculpt_normal(const Depsgraph &depsgraph,
177 const Object &ob,
178 const VPaint &vp,
179 const Brush &brush,
180 const IndexMask node_mask)
181{
183
184 const bool use_normal = vwpaint::use_normal(vp);
185
186 if (!use_normal) {
188 return;
189 }
190
191 if (brush.falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) {
192 /* TODO: This is inefficient for symmetry */
194 calc_area_normal(depsgraph, brush, ob, node_mask).value_or(float3(0));
195 return;
196 }
197
198 /* TODO: This should be removed after the normal is fixed */
199 BLI_assert(brush.falloff_shape == PAINT_FALLOFF_SHAPE_TUBE);
201}
202
204{
206 if (ob == nullptr || ob->type != OB_MESH) {
207 return false;
208 }
209 if (!ob->data || !ID_IS_EDITABLE(ob->data)) {
210 return false;
211 }
212 return true;
213}
214
216{
218 /* The current brush should match with what we have stored in the cache. */
219 BLI_assert(brush == cache->brush);
220
221 /* If original_active_brush is not set, brush was not switched/affected in
222 * smooth_brush_toggle_on(). */
226 cache->toggle_settings.original_active_brush = nullptr;
227 }
228}
229
231{
232 const BrushStrokeMode stroke_mode = BrushStrokeMode(RNA_enum_get(op.ptr, "mode"));
233 const BrushSwitchMode brush_switch_mode = BrushSwitchMode(RNA_enum_get(op.ptr, "brush_toggle"));
234 const bool pen_flip = RNA_boolean_get(op.ptr, "pen_flip");
235
236 StrokeToggleSettings toggle_settings;
237
238 toggle_settings.invert = stroke_mode == BrushStrokeMode::Invert || pen_flip;
239 toggle_settings.alt_smooth = brush_switch_mode == BrushSwitchMode::Smooth;
240 /* not very nice, but with current events system implementation
241 * we can't handle brush appearance inversion hotkey separately (sergey) */
242
243 if (toggle_settings.alt_smooth) {
244 vwpaint::smooth_brush_toggle_on(&bmain, &paint, toggle_settings);
245 }
246
247 return toggle_settings;
248}
249
250void update_cache_invariants(VPaint &vp, SculptSession &ss, wmOperator *op, const float mval[2])
251{
252 PaintStroke *stroke = static_cast<PaintStroke *>(op->customdata);
253 StrokeCache *cache = ss.cache;
254 ViewContext *vc = &stroke->vc;
255
256 stroke_cache_common_init(*vc, vp.paint, *stroke->brush, *stroke->object, mval);
257
258 cache->base_brush_strength = BKE_brush_alpha_get(&vp.paint, stroke->brush);
259 cache->bstrength = cache->base_brush_strength;
260
261 cache->accum = true;
262
265 }
266}
267
269void update_cache_variants(Depsgraph &depsgraph,
270 ViewContext &vc,
271 VPaint &vp,
272 const PaintMode paint_mode,
273 Object &ob,
274 Base &base,
275 const PaintStroke::StrokeStep &stroke_step)
276{
278 StrokeCache *cache = ss.cache;
280
281 /* TODO: When anchored strokes get supported, this needs to match the implementation in
282 * #stroke_cache_update */
283 cache->location = stroke_step.location;
284 cache->mouse = stroke_step.mouse;
285 cache->mouse_event = stroke_step.mouse_event;
286
289 depsgraph, vp.paint, nullptr, vc, &base, cache->mouse_event, false);
290 }
291
292 /* XXX: Use pressure value from first brush step for brushes which don't
293 * support strokes (grab, thumb). They depends on initial state and
294 * brush coord/pressure/etc.
295 * It's more an events design issue, which doesn't split coordinate/pressure/angle
296 * changing events. We should avoid this after events system re-design */
297 if (paint_supports_dynamic_size(brush, paint_mode) || cache->first_time) {
298 cache->pressure = stroke_step.pressure;
299 }
300
301 /* Truly temporary data that isn't stored in properties */
304 *cache->vc, cache->location, BKE_brush_radius_get(&vp.paint, &brush));
306 }
307
309 cache->radius = cache->initial_radius *
310 BKE_curvemapping_evaluateF(brush.curve_size, 0, cache->pressure);
311 }
312 else {
313 cache->radius = cache->initial_radius;
314 }
315
316 cache->radius_squared = cache->radius * cache->radius;
317
319 pbvh->update_bounds(depsgraph, ob);
320 }
321}
322
324 const Paint &paint,
325 const Brush &brush,
326 float *r_brush_size_pressure,
327 float *r_brush_alpha_value,
328 float *r_brush_alpha_pressure)
329{
330 *r_brush_size_pressure = BKE_brush_radius_get(&paint, &brush) *
333 brush.curve_size, 0, ss.cache->pressure) :
334 1.0f);
335 *r_brush_alpha_value = BKE_brush_alpha_get(&paint, &brush);
336 *r_brush_alpha_pressure = BKE_brush_use_alpha_pressure(&brush) ?
338 brush.curve_strength, 0, ss.cache->pressure) :
339 1.0f;
340}
341
342/* -------------------------------------------------------------------- */
343
345{
346 Brush *cur_brush = BKE_paint_brush(paint);
347
348 /* Switch to the blur (smooth) brush if possible. */
349 if (!BKE_paint_brush_set_essentials(bmain, paint, "Blur")) {
350 BKE_paint_brush_set(paint, cur_brush);
351 CLOG_WARN(&LOG, "Unable to switch to the 'Blur' essentials brush asset");
352 toggle_settings.original_active_brush = nullptr;
353 return;
354 }
355
356 Brush *smooth_brush = BKE_paint_brush(paint);
357 int cur_brush_size = BKE_brush_size_get(paint, cur_brush);
358
359 toggle_settings.original_active_brush = cur_brush;
360 toggle_settings.original_brush_size = BKE_brush_size_get(paint, smooth_brush);
361 BKE_brush_size_set(paint, smooth_brush, cur_brush_size);
363}
364
365} // namespace ed::sculpt_paint::vwpaint
366
367/* -------------------------------------------------------------------- */
370
372{
373 const Object *ob = CTX_data_active_object(C);
374 if (!ob || ob->type != OB_MESH) {
375 return false;
376 }
377 const Mesh *mesh = id_cast<const Mesh *>(ob->data);
378
379 if (!(ob->mode == OB_MODE_VERTEX_PAINT && mesh->faces_num)) {
380 return false;
381 }
382
383 return true;
384}
385
386static bool vertex_paint_poll_ex(bContext *C, bool check_tool)
387{
389 ScrArea *area = CTX_wm_area(C);
390 if (area && area->spacetype == SPACE_VIEW3D) {
391 if (ARegion *region = CTX_wm_region(C)) {
392 if (region->regiontype == RGN_TYPE_WINDOW) {
393 if (!check_tool || WM_toolsystem_active_tool_is_brush(C)) {
394 return true;
395 }
396 }
397 }
398 }
399 }
400 return false;
401}
402
404{
405 return vertex_paint_poll_ex(C, true);
406}
407
409{
410 return vertex_paint_poll_ex(C, false);
411}
412
413static ColorPaint4f vpaint_get_current_col(VPaint &vp, bool secondary)
414{
416 const float3 brush_color = secondary ? BKE_brush_secondary_color_get(&vp.paint, brush) :
418
419 /* alpha isn't used, could even be removed to speedup paint a little */
420 return ColorPaint4f(brush_color.x, brush_color.y, brush_color.z, 1.0f);
421}
422
423/* Weight-paint has `wpaint_blend`. */
424template<typename Color, typename Traits>
425static Color vpaint_blend(const VPaint &vp,
426 Color color_curr,
427 Color color_orig,
428 Color color_paint,
429 const typename Traits::ValueType alpha,
430 const typename Traits::BlendType brush_alpha_value)
431{
432 using Value = typename Traits::ValueType;
433
435 const IMB_BlendMode blend = IMB_BlendMode(brush.blend);
436
437 const Color color_blend = BLI_mix_colors<Color, Traits>(blend, color_curr, color_paint, alpha);
438
439 /* If no accumulate, clip color adding with `color_orig` & `color_test`. */
441 uint a;
442 Color color_test;
443 Value *cp, *ct, *co;
444
445 color_test = BLI_mix_colors<Color, Traits>(blend, color_orig, color_paint, brush_alpha_value);
446
447 cp = (Value *)&color_blend;
448 ct = (Value *)&color_test;
449 co = (Value *)&color_orig;
450
451 for (a = 0; a < 4; a++) {
452 if (ct[a] < co[a]) {
453 if (cp[a] < ct[a]) {
454 cp[a] = ct[a];
455 }
456 else if (cp[a] > co[a]) {
457 cp[a] = co[a];
458 }
459 }
460 else {
461 if (cp[a] < co[a]) {
462 cp[a] = co[a];
463 }
464 else if (cp[a] > ct[a]) {
465 cp[a] = ct[a];
466 }
467 }
468 }
469 }
470
472 {
473 Value *cp, *cc;
474 cp = (Value *)&color_blend;
475 cc = (Value *)&color_curr;
476 cp[3] = cc[3];
477 }
478
479 return color_blend;
480}
481
489template<typename Color, typename Traits>
490static Color vpaint_blend_stroke(const VPaint &vp,
491 MutableSpan<Color> prev_vertex_colors,
492 MutableSpan<Color> vertex_colors,
493 MutableSpan<Color> stroke_buffer,
494 Color brush_mark_color,
495 float brush_mark_alpha,
496 float brush_strength,
497 int index)
498{
499 Color result;
501 BLI_assert(!stroke_buffer.is_empty());
502 BLI_assert(!prev_vertex_colors.is_empty());
503
504 if (isZero(prev_vertex_colors[index])) {
505 prev_vertex_colors[index] = vertex_colors[index];
506 }
507
508 /* Mix with mesh color under the stroke (a bit easier than trying to premultiply
509 * byte Color types) */
510 if (isZero(stroke_buffer[index])) {
511 stroke_buffer[index] = vertex_colors[index];
512 stroke_buffer[index].a = 0;
513 }
514
516 brush_mark_color,
517 stroke_buffer[index],
518 stroke_buffer[index].a);
519
521 vertex_colors[index],
522 prev_vertex_colors[index],
523 stroke_buffer[index],
524 brush_mark_alpha,
525 Traits::range * brush_strength);
526 }
527 else {
529 vertex_colors[index],
530 Color() /* unused in accumulate mode */,
531 brush_mark_color,
532 brush_mark_alpha,
533 Traits::range * brush_strength);
534 }
535 return result;
536}
537
539 const ViewContext *vc,
540 const float3 &co,
541 float4 &r_rgba)
542{
545 BLI_assert(mtex->tex != nullptr);
546 if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
547 BKE_brush_sample_tex_3d(&vp.paint, brush, mtex, co, r_rgba, 0, nullptr);
548 }
549 else {
550 float co_ss[2]; /* screenspace */
554 {
555 const float co_ss_3d[3] = {co_ss[0], co_ss[1], 0.0f}; /* we need a 3rd empty value */
556 BKE_brush_sample_tex_3d(&vp.paint, brush, mtex, co_ss_3d, r_rgba, 0, nullptr);
557 }
558 else {
559 zero_v4(r_rgba);
560 }
561 }
562}
563
565
566/* -------------------------------------------------------------------- */
569
570void ED_object_vpaintmode_enter_ex(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob)
571{
574 ED_mesh_color_ensure(mesh, nullptr);
575
576 /* Flush object mode. */
578}
579void ED_object_vpaintmode_enter(bContext *C, Depsgraph &depsgraph)
580{
581 Main &bmain = *CTX_data_main(C);
582 Scene &scene = *CTX_data_scene(C);
584 ED_object_vpaintmode_enter_ex(bmain, depsgraph, scene, ob);
585}
586
588
589/* -------------------------------------------------------------------- */
592
602
604
605/* -------------------------------------------------------------------- */
608
613{
614 Main &bmain = *CTX_data_main(C);
617 const int mode_flag = OB_MODE_VERTEX_PAINT;
618 const bool is_mode_set = (ob.mode & mode_flag) != 0;
619 Scene &scene = *CTX_data_scene(C);
620
621 if (!is_mode_set) {
622 if (!ed::object::mode_compat_set(C, &ob, eObjectMode(mode_flag), op->reports)) {
623 return OPERATOR_CANCELLED;
624 }
625 }
626
628
629 if (is_mode_set) {
631 }
632 else {
633 Depsgraph *depsgraph = CTX_data_depsgraph_on_load(C);
634 if (depsgraph) {
636 }
637 ED_object_vpaintmode_enter_ex(bmain, *depsgraph, scene, ob);
638 }
639
641
642 /* update modifier stack for mapping requirements */
644
646 WM_msg_publish_rna_prop(mbus, &ob.id, &ob, Object, mode);
647
649
650 return OPERATOR_FINISHED;
651}
652
654{
655 ot->name = "Vertex Paint Mode";
656 ot->idname = "PAINT_OT_vertex_paint_toggle";
657 ot->description = "Toggle the vertex paint mode in 3D view";
658
661
663}
664
666
667/* -------------------------------------------------------------------- */
670
671/* Implementation notes:
672 *
673 * Operator->invoke()
674 * - Validate context (add #Mesh.mloopcol).
675 * - Create custom-data storage.
676 * - Call paint once (mouse click).
677 * - Add modal handler.
678 *
679 * Operator->modal()
680 * - For every mouse-move, apply vertex paint.
681 * - Exit on mouse release, free custom-data.
682 * (return OPERATOR_FINISHED also removes handler and operator)
683 *
684 * For future:
685 * - implement a stroke event (or mouse-move with past positions).
686 * - revise whether op->customdata should be added in object, in set_vpaint.
687 */
688
689template<typename Func>
690static void to_static_color_type(const bke::AttrType type, const Func &func)
691{
692 switch (type) {
694 func(ColorGeometry4f());
695 break;
697 func(ColorGeometry4b());
698 break;
699 default:
701 break;
702 }
703}
704
705struct VPaintData : public PaintModeData {
709
711
713
714 /* Special storage for smear brush, avoid feedback loop - update each step. */
719
720 /* For brushes that don't use accumulation, a temporary holding array */
723};
724
725static std::unique_ptr<VPaintData> vpaint_init_vpaint(wmOperator *op,
726 const ViewContext &vc,
727 VPaint &vp,
728 Mesh &mesh,
729 const AttrDomain domain,
730 const bke::AttrType type,
731 const Brush &brush)
732{
733 std::unique_ptr<VPaintData> vpd = std::make_unique<VPaintData>();
734
735 vpd->type = type;
736 vpd->domain = domain;
737
738 vpd->vc = vc;
739
740 vpd->paintcol = vpaint_get_current_col(
742
743 vpd->is_texbrush = !(brush.vertex_brush_type == VPAINT_BRUSH_TYPE_BLUR) && brush.mtex.tex;
744
745 if (brush.vertex_brush_type == VPAINT_BRUSH_TYPE_SMEAR) {
746 const GVArray attribute = *mesh.attributes().lookup(mesh.active_color_attribute, domain);
747 vpd->smear.color_prev = GArray(attribute.type(), attribute.size());
748 attribute.materialize(vpd->smear.color_prev.data());
749
750 vpd->smear.color_curr = vpd->smear.color_prev;
751 }
752
754 if (vpd->prev_colors.is_empty()) {
755 const GVArray attribute = *mesh.attributes().lookup(mesh.active_color_attribute);
756 vpd->prev_colors = GArray(attribute.type(), attribute.size());
757 attribute.type().value_initialize_n(vpd->prev_colors.data(), vpd->prev_colors.size());
758 }
759
760 if (vpd->stroke_buffer.is_empty()) {
761 const GVArray attribute = *mesh.attributes().lookup(mesh.active_color_attribute);
762 vpd->stroke_buffer = GArray(attribute.type(), attribute.size());
763 attribute.type().value_initialize_n(vpd->stroke_buffer.data(), vpd->stroke_buffer.size());
764 }
765 }
766 else {
767 vpd->prev_colors = {};
768 vpd->stroke_buffer = {};
769 }
770
771 return vpd;
772}
773
778
787
788 std::optional<float3> get_location(float2 mouse, bool force_original) override;
789 bool test_start(wmOperator *op, float2 mouse) override;
790 void redraw(bool final) override;
791 bool test_cancel() override;
792 void update_step(wmOperator *op, const StrokeStep &stroke_step) override;
793 void done(bool is_cancel, bool stroke_started) override;
794};
795
796std::optional<float3> VertexPaintStroke::get_location(const float2 mouse, bool force_original)
797{
799 *this->depsgraph, this->vc, *this->paint, this->brush, mouse, force_original);
800}
801
808
810{
811 Scene &scene = *this->scene;
813 VPaint &vp = *ts.vpaint;
815 Object &ob = *this->object;
817
818 /* context checks could be a poll() */
820 if (mesh == nullptr || mesh->faces_num == 0) {
821 return false;
822 }
823
824 if (!ED_mesh_color_ensure(mesh, nullptr)) {
825 return false;
826 }
827
828 const std::optional<bke::AttributeMetaData> meta_data = mesh->attributes().lookup_meta_data(
829 mesh->active_color_attribute);
830 if (!meta_data) {
831 return false;
832 }
833
834 std::unique_ptr<VPaintData> vpd = vpaint_init_vpaint(
835 op, this->vc, vp, *mesh, meta_data->domain, meta_data->data_type, brush);
836
837 mode_data_ = std::move(vpd);
838
842
843 vwpaint::update_cache_invariants(vp, ss, op, mouse);
845
846 return true;
847}
848
849void VertexPaintStroke::redraw(bool /*final*/) {}
851{
852 return false;
853}
854
855static void filter_factors_with_selection(const Span<bool> select_vert,
856 const Span<int> verts,
857 const MutableSpan<float> factors)
858{
859 BLI_assert(verts.size() == factors.size());
860
861 for (const int i : verts.index_range()) {
862 if (!select_vert[verts[i]]) {
863 factors[i] = 0.0f;
864 }
865 }
866}
867
868static void do_vpaint_brush_blur_loops(const Depsgraph &depsgraph,
869 const VPaint &vp,
870 VPaintData &vpd,
871 Object &ob,
872 Mesh &mesh,
874 const IndexMask &node_mask,
876{
878 const StrokeCache &cache = *ss.cache;
879
881
882 float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
884 ss, vp.paint, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
885 const bool use_normal = vwpaint::use_normal(vp);
886 const bool use_vert_sel = (mesh.editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) !=
887 0;
888 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
889
890 const float *sculpt_normal_frontface = brush_frontface_normal_from_falloff_shape(
891 ss, brush.falloff_shape);
892
893 GMutableSpan g_previous_color = vpd.prev_colors;
894
895 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
896 const OffsetIndices faces = mesh.faces();
897 const Span<int> corner_verts = mesh.corner_verts();
898 const GroupedSpan<int> vert_to_face = mesh.vert_to_face_map();
899 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
900 const bke::AttributeAccessor attributes = mesh.attributes();
901 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
902 VArraySpan<bool> select_vert;
903 if (use_vert_sel) {
904 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
905 }
906 VArraySpan<bool> select_poly;
907 if (use_face_sel) {
908 select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
909 }
910
911 node_mask.foreach_index(
912 [&](const int i) {
913 const Span<int> verts = nodes[i].verts();
914
916 fill_factor_from_hide(hide_vert, verts, factors);
917 filter_region_clip_factors(ss, vert_positions, verts, factors);
918 if (!select_vert.is_empty()) {
919 filter_factors_with_selection(select_vert, verts, factors);
920 }
921
924 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
925 filter_distances_with_radius(cache.radius, distances, factors);
926 calc_brush_strength_factors(cache, brush, distances, factors);
927
929 depsgraph, ob, cache.automasking.get(), nodes[i], verts, factors);
930
931 for (const int i : verts.index_range()) {
932 const int vert = verts[i];
933 if (factors[i] == 0.0f) {
934 continue;
935 }
936
937 float brush_strength = cache.bstrength;
938 const float angle_cos = use_normal ?
939 dot_v3v3(sculpt_normal_frontface, vert_normals[vert]) :
940 1.0f;
941 if (!vwpaint::test_brush_angle_falloff(brush, angle_cos)) {
942 continue;
943 }
944
945 const float brush_fade = factors[i];
946
947 to_static_color_type(vpd.type, [&](auto dummy) {
948 using T = decltype(dummy);
949 using Color =
950 std::conditional_t<std::is_same_v<T, ColorGeometry4f>, ColorPaint4f, ColorPaint4b>;
951 using Traits = blender::color::Traits<Color>;
952 using Blend = typename Traits::BlendType;
953 MutableSpan<Color> previous_color = g_previous_color.typed<T>().template cast<Color>();
954 MutableSpan<Color> colors = attribute.typed<T>().template cast<Color>();
955 /* Get the average face color */
956 Color color_final(0, 0, 0, 0);
957
958 int total_hit_loops = 0;
959 Blend blend[4] = {0};
960
961 for (const int face : vert_to_face[vert]) {
962 if (!select_poly.is_empty() && !select_poly[face]) {
963 return;
964 }
965 total_hit_loops += faces[face].size();
966 for (const int corner : faces[face]) {
967 const Color &col = colors[corner];
968
969 /* Color is squared to compensate the `sqrt` color encoding. */
970 blend[0] += static_cast<Blend>(col.r) * static_cast<Blend>(col.r);
971 blend[1] += static_cast<Blend>(col.g) * static_cast<Blend>(col.g);
972 blend[2] += static_cast<Blend>(col.b) * static_cast<Blend>(col.b);
973 blend[3] += static_cast<Blend>(col.a) * static_cast<Blend>(col.a);
974 }
975 }
976
977 if (total_hit_loops == 0) {
978 return;
979 }
980
981 /* Use rgb^2 color averaging. */
982 Color *col = &color_final;
983
984 color_final.r = Traits::round(sqrtf(Traits::divide_round(blend[0], total_hit_loops)));
985 color_final.g = Traits::round(sqrtf(Traits::divide_round(blend[1], total_hit_loops)));
986 color_final.b = Traits::round(sqrtf(Traits::divide_round(blend[2], total_hit_loops)));
987 color_final.a = Traits::round(sqrtf(Traits::divide_round(blend[3], total_hit_loops)));
988
989 /* For each face owning this vert,
990 * paint each loop belonging to this vert. */
991 for (const int face : vert_to_face[vert]) {
992 const int corner = bke::mesh::face_find_corner_from_vert(
993 faces[face], corner_verts, vert);
994 if (!select_poly.is_empty() && !select_poly[face]) {
995 continue;
996 }
997 Color color_orig(0, 0, 0, 0); /* unused when array is nullptr */
998
999 if (!previous_color.is_empty()) {
1000 /* Get the previous loop color */
1001 if (isZero(previous_color[corner])) {
1002 previous_color[corner] = colors[corner];
1003 }
1004 color_orig = previous_color[corner];
1005 }
1006 const float final_alpha = Traits::range * brush_fade * brush_strength *
1007 brush_alpha_pressure;
1008 /* Mix the new color with the original
1009 * based on the brush strength and the curve. */
1010 colors[corner] = vpaint_blend<Color, Traits>(vp,
1011 colors[corner],
1012 color_orig,
1013 *col,
1014 final_alpha,
1015 Traits::range * brush_strength);
1016 }
1017 });
1018 }
1019 },
1021}
1022
1023static void do_vpaint_brush_blur_verts(const Depsgraph &depsgraph,
1024 const VPaint &vp,
1025 VPaintData &vpd,
1026 Object &ob,
1027 Mesh &mesh,
1029 const IndexMask &node_mask,
1031{
1033 const StrokeCache &cache = *ss.cache;
1034
1035 const Brush &brush = *ss.cache->brush;
1036
1037 float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1039 ss, vp.paint, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1040 const bool use_normal = vwpaint::use_normal(vp);
1041 const bool use_vert_sel = (mesh.editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) !=
1042 0;
1043 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1044
1045 const float *sculpt_normal_frontface = brush_frontface_normal_from_falloff_shape(
1046 ss, brush.falloff_shape);
1047
1048 GMutableSpan g_previous_color = vpd.prev_colors;
1049
1050 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1051 const OffsetIndices faces = mesh.faces();
1052 const Span<int> corner_verts = mesh.corner_verts();
1053 const GroupedSpan<int> vert_to_face = mesh.vert_to_face_map();
1054 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1055 const bke::AttributeAccessor attributes = mesh.attributes();
1056 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
1057 VArraySpan<bool> select_vert;
1058 if (use_vert_sel) {
1059 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1060 }
1061 VArraySpan<bool> select_poly;
1062 if (use_face_sel) {
1063 select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
1064 }
1065
1066 node_mask.foreach_index(
1067 [&](const int i) {
1068 const Span<int> verts = nodes[i].verts();
1070 fill_factor_from_hide(hide_vert, verts, factors);
1071 filter_region_clip_factors(ss, vert_positions, verts, factors);
1072 if (!select_vert.is_empty()) {
1073 filter_factors_with_selection(select_vert, verts, factors);
1074 }
1075
1078 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
1079 filter_distances_with_radius(cache.radius, distances, factors);
1080 calc_brush_strength_factors(cache, brush, distances, factors);
1081
1083 depsgraph, ob, cache.automasking.get(), nodes[i], verts, factors);
1084
1085 for (const int i : verts.index_range()) {
1086 const int vert = verts[i];
1087 if (factors[i] == 0.0f) {
1088 continue;
1089 }
1090
1091 float brush_strength = cache.bstrength;
1092 const float angle_cos = use_normal ?
1093 dot_v3v3(sculpt_normal_frontface, vert_normals[vert]) :
1094 1.0f;
1095 if (!vwpaint::test_brush_angle_falloff(brush, angle_cos)) {
1096 continue;
1097 }
1098 const float brush_fade = factors[i];
1099
1100 /* Get the average face color */
1101 to_static_color_type(vpd.type, [&](auto dummy) {
1102 using T = decltype(dummy);
1103 using Color =
1104 std::conditional_t<std::is_same_v<T, ColorGeometry4f>, ColorPaint4f, ColorPaint4b>;
1105 using Traits = blender::color::Traits<Color>;
1106 using Blend = typename Traits::BlendType;
1107 MutableSpan<Color> previous_color = g_previous_color.typed<T>().template cast<Color>();
1108 MutableSpan<Color> colors = attribute.typed<T>().template cast<Color>();
1109 Color color_final(0, 0, 0, 0);
1110
1111 int total_hit_loops = 0;
1112 Blend blend[4] = {0};
1113
1114 for (const int face : vert_to_face[vert]) {
1115 if (!select_poly.is_empty() && !select_poly[face]) {
1116 continue;
1117 }
1118 total_hit_loops += faces[face].size();
1119 for (const int vert : corner_verts.slice(faces[face])) {
1120 const Color &col = colors[vert];
1121
1122 /* Color is squared to compensate the `sqrt` color encoding. */
1123 blend[0] += static_cast<Blend>(col.r) * static_cast<Blend>(col.r);
1124 blend[1] += static_cast<Blend>(col.g) * static_cast<Blend>(col.g);
1125 blend[2] += static_cast<Blend>(col.b) * static_cast<Blend>(col.b);
1126 blend[3] += static_cast<Blend>(col.a) * static_cast<Blend>(col.a);
1127 }
1128 }
1129
1130 if (total_hit_loops == 0) {
1131 return;
1132 }
1133 /* Use rgb^2 color averaging. */
1134 color_final.r = Traits::round(sqrtf(Traits::divide_round(blend[0], total_hit_loops)));
1135 color_final.g = Traits::round(sqrtf(Traits::divide_round(blend[1], total_hit_loops)));
1136 color_final.b = Traits::round(sqrtf(Traits::divide_round(blend[2], total_hit_loops)));
1137 color_final.a = Traits::round(sqrtf(Traits::divide_round(blend[3], total_hit_loops)));
1138
1139 Color color_orig(0, 0, 0, 0); /* unused when array is nullptr */
1140
1141 if (!previous_color.is_empty()) {
1142 /* Get the previous loop color */
1143 if (isZero(previous_color[vert])) {
1144 previous_color[vert] = colors[vert];
1145 }
1146 color_orig = previous_color[vert];
1147 }
1148 const float final_alpha = Traits::range * brush_fade * brush_strength *
1149 brush_alpha_pressure;
1150 /* Mix the new color with the original
1151 * based on the brush strength and the curve. */
1152 colors[vert] = vpaint_blend<Color, Traits>(vp,
1153 colors[vert],
1154 color_orig,
1155 color_final,
1156 final_alpha,
1157 Traits::range * brush_strength);
1158 });
1159 }
1160 },
1161 exec_mode::grain_size(1));
1162}
1163
1164static void do_vpaint_brush_smear(const Depsgraph &depsgraph,
1165 const VPaint &vp,
1166 VPaintData &vpd,
1167 Object &ob,
1168 Mesh &mesh,
1170 const IndexMask &node_mask,
1172{
1174 StrokeCache &cache = *ss.cache;
1175
1177 /* We need a directional component to calculate the effect of this brush */
1178 return;
1179 }
1180
1181 const Brush &brush = *cache.brush;
1182 GMutableSpan g_color_curr = vpd.smear.color_curr;
1183 GMutableSpan g_color_prev_smear = vpd.smear.color_prev;
1184 GMutableSpan g_color_prev = vpd.prev_colors;
1185
1186 float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1187
1189 ss, vp.paint, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1190 const bool use_normal = vwpaint::use_normal(vp);
1191 const bool use_vert_sel = (mesh.editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) !=
1192 0;
1193 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1194
1195 float brush_dir[3];
1196 sub_v3_v3v3(brush_dir, cache.location_symm, cache.last_location_symm);
1197 project_plane_v3_v3v3(brush_dir, brush_dir, cache.view_normal_symm);
1198 if (normalize_v3(brush_dir) == 0.0f) {
1199 return;
1200 }
1201
1202 const float *sculpt_normal_frontface = brush_frontface_normal_from_falloff_shape(
1203 ss, brush.falloff_shape);
1204
1205 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1206 const OffsetIndices faces = mesh.faces();
1207 const Span<int> corner_verts = mesh.corner_verts();
1208 const GroupedSpan<int> vert_to_face = mesh.vert_to_face_map();
1209 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1210 const bke::AttributeAccessor attributes = mesh.attributes();
1211 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
1212 VArraySpan<bool> select_vert;
1213 if (use_vert_sel) {
1214 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1215 }
1216 VArraySpan<bool> select_poly;
1217 if (use_face_sel) {
1218 select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
1219 }
1220
1221 node_mask.foreach_index(
1222 [&](const int i) {
1223 const Span<int> verts = nodes[i].verts();
1225 fill_factor_from_hide(hide_vert, verts, factors);
1226 filter_region_clip_factors(ss, vert_positions, verts, factors);
1227 if (!select_vert.is_empty()) {
1228 filter_factors_with_selection(select_vert, verts, factors);
1229 }
1230
1233 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
1234 filter_distances_with_radius(cache.radius, distances, factors);
1235 calc_brush_strength_factors(cache, brush, distances, factors);
1236
1238 depsgraph, ob, cache.automasking.get(), nodes[i], verts, factors);
1239
1240 for (const int i : verts.index_range()) {
1241 const int vert = verts[i];
1242 if (factors[i] == 0.0f) {
1243 continue;
1244 }
1245
1246 /* Calculate the dot prod. between ray norm on surf and current vert
1247 * (ie splash prevention factor), and only paint front facing verts. */
1248 float brush_strength = cache.bstrength;
1249 const float angle_cos = use_normal ?
1250 dot_v3v3(sculpt_normal_frontface, vert_normals[vert]) :
1251 1.0f;
1252 if (!vwpaint::test_brush_angle_falloff(brush, angle_cos)) {
1253 continue;
1254 }
1255 const float brush_fade = factors[i];
1256
1257 bool do_color = false;
1258 /* Minimum dot product between brush direction and current
1259 * to neighbor direction is 0.0, meaning orthogonal. */
1260 float stroke_dot_max = 0.0f;
1261
1262 /* Get the color of the loop in the opposite
1263 * direction of the brush movement */
1264 to_static_color_type(vpd.type, [&](auto dummy) {
1265 using T = decltype(dummy);
1266 using Color =
1267 std::conditional_t<std::is_same_v<T, ColorGeometry4f>, ColorPaint4f, ColorPaint4b>;
1268 using Traits = blender::color::Traits<Color>;
1269 MutableSpan<Color> color_curr = g_color_curr.typed<T>().template cast<Color>();
1270 MutableSpan<Color> color_prev_smear =
1271 g_color_prev_smear.typed<T>().template cast<Color>();
1272 MutableSpan<Color> color_prev = g_color_prev.typed<T>().template cast<Color>();
1273 MutableSpan<Color> colors = attribute.typed<T>().template cast<Color>();
1274
1275 Color color_final(0, 0, 0, 0);
1276
1277 for (const int face : vert_to_face[vert]) {
1278 if (!select_poly.is_empty() && !select_poly[face]) {
1279 continue;
1280 }
1281 for (const int corner : faces[face]) {
1282 const int v_other_index = corner_verts[corner];
1283 if (v_other_index == vert) {
1284 continue;
1285 }
1286
1287 /* Get the direction from the
1288 * selected vert to the neighbor. */
1289 float other_dir[3];
1290 sub_v3_v3v3(other_dir, vert_positions[vert], vert_positions[v_other_index]);
1291 project_plane_v3_v3v3(other_dir, other_dir, cache.view_normal_symm);
1292
1293 normalize_v3(other_dir);
1294
1295 const float stroke_dot = dot_v3v3(other_dir, brush_dir);
1296 int elem_index;
1297
1298 if (vpd.domain == AttrDomain::Point) {
1299 elem_index = v_other_index;
1300 }
1301 else {
1302 elem_index = corner;
1303 }
1304
1305 if (stroke_dot > stroke_dot_max) {
1306 stroke_dot_max = stroke_dot;
1307 color_final = color_prev_smear[elem_index];
1308 do_color = true;
1309 }
1310 }
1311 }
1312
1313 if (!do_color) {
1314 return;
1315 }
1316
1317 const float final_alpha = Traits::range * brush_fade * brush_strength *
1318 brush_alpha_pressure;
1319
1320 /* For each face owning this vert,
1321 * paint each loop belonging to this vert. */
1322 for (const int face : vert_to_face[vert]) {
1323
1324 int elem_index;
1325 if (vpd.domain == AttrDomain::Point) {
1326 elem_index = vert;
1327 }
1328 else {
1330 faces[face], corner_verts, vert);
1331 }
1332 if (!select_poly.is_empty() && !select_poly[face]) {
1333 continue;
1334 }
1335
1336 /* Get the previous element color */
1337 Color color_orig(0, 0, 0, 0); /* unused when array is nullptr */
1338
1339 if (!color_prev.is_empty()) {
1340 /* Get the previous element color */
1341 if (isZero(color_prev[elem_index])) {
1342 color_prev[elem_index] = colors[elem_index];
1343 }
1344 color_orig = color_prev[elem_index];
1345 }
1346 /* Mix the new color with the original
1347 * based on the brush strength and the curve. */
1348 colors[elem_index] = vpaint_blend<Color, Traits>(vp,
1349 colors[elem_index],
1350 color_orig,
1351 color_final,
1352 final_alpha,
1353 Traits::range * brush_strength);
1354
1355 color_curr[elem_index] = colors[elem_index];
1356 }
1357 });
1358 }
1359 },
1360 exec_mode::grain_size(1));
1361}
1362
1364 Object &ob,
1365 Mesh &mesh,
1366 const Brush &brush,
1367 const GSpan attribute,
1369 const IndexMask &node_mask)
1370{
1372 const StrokeCache &cache = *ss.cache;
1373 const Depsgraph &depsgraph = *vpd.vc.depsgraph;
1374
1375 const bool use_vert_sel = (mesh.editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) !=
1376 0;
1377
1378 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1379 const OffsetIndices faces = mesh.faces();
1380 const Span<int> corner_verts = mesh.corner_verts();
1381 const GroupedSpan<int> vert_to_face = mesh.vert_to_face_map();
1382 const bke::AttributeAccessor attributes = mesh.attributes();
1383 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
1384 VArraySpan<bool> select_vert;
1385 if (use_vert_sel) {
1386 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1387 }
1388
1389 to_static_color_type(vpd.type, [&](auto dummy) {
1390 using T = decltype(dummy);
1391 using Color =
1392 std::conditional_t<std::is_same_v<T, ColorGeometry4f>, ColorPaint4f, ColorPaint4b>;
1393 using Traits = blender::color::Traits<Color>;
1394 using Blend = typename Traits::BlendType;
1395 const Span<Color> colors = attribute.typed<T>().template cast<Color>();
1396
1397 Array<VPaintAverageAccum<Blend>> accum(nodes.size(), {0, {0, 0, 0}});
1398 node_mask.foreach_index(
1399 [&](const int i) {
1400 VPaintAverageAccum<Blend> &accum2 = accum[i];
1401
1402 const Span<int> verts = nodes[i].verts();
1403
1405 fill_factor_from_hide(hide_vert, verts, factors);
1406 filter_region_clip_factors(ss, vert_positions, verts, factors);
1407 if (!select_vert.is_empty()) {
1408 filter_factors_with_selection(select_vert, verts, factors);
1409 }
1410
1413 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
1414 filter_distances_with_radius(cache.radius, distances, factors);
1415 calc_brush_strength_factors(cache, brush, distances, factors);
1416
1417 for (const int i : verts.index_range()) {
1418 const int vert = verts[i];
1419 if (factors[i] == 0.0f) {
1420 continue;
1421 }
1422
1423 accum2.len += vert_to_face[vert].size();
1424 /* if a vertex is within the brush region, then add its color to the blend. */
1425 for (const int face : vert_to_face[vert]) {
1426 int elem_index;
1427 if (vpd.domain == AttrDomain::Corner) {
1429 faces[face], corner_verts, vert);
1430 }
1431 else {
1432 elem_index = vert;
1433 }
1434
1435 /* Color is squared to compensate the `sqrt` color encoding. */
1436 const Color &col = colors[elem_index];
1437 accum2.value[0] += col.r * col.r;
1438 accum2.value[1] += col.g * col.g;
1439 accum2.value[2] += col.b * col.b;
1440 }
1441 }
1442 },
1444
1445 Blend accum_len = 0;
1446 Blend accum_value[3] = {0};
1447 Color blend(0, 0, 0, 0);
1448
1449 node_mask.foreach_index([&](const int i) {
1450 accum_len += accum[i].len;
1451 accum_value[0] += accum[i].value[0];
1452 accum_value[1] += accum[i].value[1];
1453 accum_value[2] += accum[i].value[2];
1454 });
1455 if (accum_len != 0) {
1456 blend.r = Traits::round(sqrtf(Traits::divide_round(accum_value[0], accum_len)));
1457 blend.g = Traits::round(sqrtf(Traits::divide_round(accum_value[1], accum_len)));
1458 blend.b = Traits::round(sqrtf(Traits::divide_round(accum_value[2], accum_len)));
1459 blend.a = Traits::range;
1460
1461 vpd.paintcol = toFloat(blend);
1462 }
1463 });
1464}
1465
1466template<typename Color>
1467static float paint_and_tex_color_alpha(const VPaint &vp,
1468 VPaintData &vpd,
1469 const float v_co[3],
1470 Color *r_color)
1471{
1472 float4 rgba;
1473 paint_and_tex_color_alpha_intern(vp, &vpd.vc, v_co, rgba);
1474
1475 ColorPaint4f rgba_br = toFloat(vpd.paintcol);
1476 mul_v3_v3(rgba_br, rgba);
1477
1478 *r_color = fromFloat<Color>(rgba_br);
1479 return rgba[3];
1480}
1481
1482/* Compute brush color, using jitter if it's enabled */
1484 const Brush *brush,
1485 const StrokeCache &cache,
1486 const ColorPaint4f &paint_color)
1487{
1488 float3 brush_color = float3(paint_color.r, paint_color.g, paint_color.b);
1489 const std::optional<BrushColorJitterSettings> color_jitter_settings =
1491 if (color_jitter_settings) {
1492 brush_color = BKE_paint_randomize_color(*color_jitter_settings,
1493 *cache.initial_hsv_jitter,
1494 cache.stroke_distance,
1495 cache.pressure,
1496 brush_color);
1497 }
1498 return brush_color;
1499}
1500
1501static void vpaint_do_draw(const Depsgraph &depsgraph,
1502 const VPaint &vp,
1503 VPaintData &vpd,
1504 Object &ob,
1505 Mesh &mesh,
1507 const IndexMask &node_mask,
1509{
1511 const StrokeCache &cache = *ss.cache;
1512 const Brush &brush = *ob.runtime->sculpt_session->cache->brush;
1513
1514 float brush_size_pressure, brush_alpha_value, brush_alpha_pressure;
1516 ss, vp.paint, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure);
1517 const bool use_normal = vwpaint::use_normal(vp);
1518 const bool use_vert_sel = (mesh.editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) !=
1519 0;
1520 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
1521
1522 const float *sculpt_normal_frontface = brush_frontface_normal_from_falloff_shape(
1523 ss, brush.falloff_shape);
1524
1525 GMutableSpan g_previous_color = vpd.prev_colors;
1526 GMutableSpan g_stroke_buffer = vpd.stroke_buffer;
1527
1528 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1529 const OffsetIndices faces = mesh.faces();
1530 const Span<int> corner_verts = mesh.corner_verts();
1531 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1532 const GroupedSpan<int> vert_to_face = mesh.vert_to_face_map();
1533 const bke::AttributeAccessor attributes = mesh.attributes();
1534 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
1535 VArraySpan<bool> select_vert;
1536 if (use_vert_sel) {
1537 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1538 }
1539 VArraySpan<bool> select_poly;
1540 if (use_face_sel) {
1541 select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
1542 }
1543
1544 const float3 brush_color = get_brush_color(&vp.paint, &brush, cache, vpd.paintcol);
1545
1546 node_mask.foreach_index(
1547 [&](const int i) {
1548 const Span<int> verts = nodes[i].verts();
1550 fill_factor_from_hide(hide_vert, verts, factors);
1551 filter_region_clip_factors(ss, vert_positions, verts, factors);
1552 if (!select_vert.is_empty()) {
1553 filter_factors_with_selection(select_vert, verts, factors);
1554 }
1555
1558 ss, vert_positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
1559 filter_distances_with_radius(cache.radius, distances, factors);
1560 calc_brush_strength_factors(cache, brush, distances, factors);
1561
1563 if (cache.automasking) {
1564 automask_factors.reinitialize(verts.size());
1565 automask_factors.fill(1.0f);
1567 depsgraph, ob, *cache.automasking, nodes[i], verts, automask_factors);
1568 scale_factors(factors, automask_factors);
1569 }
1570
1571 for (const int i : verts.index_range()) {
1572 const int vert = verts[i];
1573 if (factors[i] == 0.0f) {
1574 continue;
1575 }
1576
1577 /* Calculate the dot product between ray normal on surface and current vertex
1578 * (ie splash prevention factor), and only paint front facing verts. */
1579 float brush_strength = cache.bstrength;
1580 const float angle_cos = use_normal ?
1581 dot_v3v3(sculpt_normal_frontface, vert_normals[vert]) :
1582 1.0f;
1583 if (!vwpaint::test_brush_angle_falloff(brush, angle_cos)) {
1584 continue;
1585 }
1586 const float brush_fade = factors[i];
1587
1588 to_static_color_type(vpd.type, [&](auto dummy) {
1589 using T = decltype(dummy);
1590 using Color =
1591 std::conditional_t<std::is_same_v<T, ColorGeometry4f>, ColorPaint4f, ColorPaint4b>;
1592 using Traits = blender::color::Traits<Color>;
1593 MutableSpan<Color> colors = attribute.typed<T>().template cast<Color>();
1594 MutableSpan<Color> previous_color = g_previous_color.typed<T>().template cast<Color>();
1595 MutableSpan<Color> stroke_buffer = g_stroke_buffer.typed<T>().template cast<Color>();
1596
1597 Color color_final = fromFloat<Color>(
1598 ColorPaint4f(brush_color[0], brush_color[1], brush_color[2], 1.0));
1599
1600 /* If we're painting with a texture, sample the texture color and alpha. */
1601 float tex_alpha = 1.0;
1602 if (vpd.is_texbrush) {
1603 /* NOTE: we may want to paint alpha as vertex color alpha. */
1604
1605 /* If the active area is being applied for symmetry, flip it
1606 * across the symmetry axis and rotate it back to the original
1607 * position in order to project it. This ensures that the
1608 * brush texture will be oriented correctly.
1609 * This is the method also used in #sculpt_apply_texture(). */
1610 float3 position = vert_positions[vert];
1611 if (cache.radial_symmetry_pass) {
1612 position = math::transform_point(cache.symm_rot_mat_inv, position);
1613 }
1614 const float3 symm_point = ed::sculpt_paint::symmetry_flip(
1615 position, cache.mirror_symmetry_pass);
1616
1617 tex_alpha = paint_and_tex_color_alpha<Color>(vp, vpd, symm_point, &color_final);
1618 }
1619
1620 /* Use the automasking factor again when calculating the final brush strength to avoid
1621 * washing out non-binary values. Note that this only has an effect for non-accumulate
1622 * brushes. */
1623 const float automask_factor = automask_factors.is_empty() ? 1.0f : automask_factors[i];
1624 const float final_alpha = Traits::frange * brush_fade * brush_strength * tex_alpha *
1625 brush_alpha_pressure;
1626
1627 if (vpd.domain == AttrDomain::Point) {
1628 colors[vert] = vpaint_blend_stroke<Color, Traits>(vp,
1629 previous_color,
1630 colors,
1631 stroke_buffer,
1632 color_final,
1633 final_alpha,
1634 brush_strength * automask_factor,
1635 vert);
1636 }
1637 else {
1638 /* For each face owning this vert, paint each loop belonging to this vert. */
1639 for (const int face : vert_to_face[vert]) {
1640 const int corner = bke::mesh::face_find_corner_from_vert(
1641 faces[face], corner_verts, vert);
1642 if (!select_poly.is_empty() && !select_poly[face]) {
1643 continue;
1644 }
1645 colors[corner] = vpaint_blend_stroke<Color, Traits>(vp,
1646 previous_color,
1647 colors,
1648 stroke_buffer,
1649 color_final,
1650 final_alpha,
1651 brush_strength *
1652 automask_factor,
1653 corner);
1654 }
1655 }
1656 });
1657 }
1658 },
1659 exec_mode::grain_size(1));
1660}
1661
1662static void vpaint_do_blur(const Depsgraph &depsgraph,
1663 const VPaint &vp,
1664 VPaintData &vpd,
1665 Object &ob,
1666 Mesh &mesh,
1668 const IndexMask &node_mask,
1670{
1671 if (vpd.domain == AttrDomain::Point) {
1672 do_vpaint_brush_blur_verts(depsgraph, vp, vpd, ob, mesh, nodes, node_mask, attribute);
1673 }
1674 else {
1675 do_vpaint_brush_blur_loops(depsgraph, vp, vpd, ob, mesh, nodes, node_mask, attribute);
1676 }
1677}
1678
1679static void vpaint_paint_leaves(const Depsgraph &depsgraph,
1680 const VPaint &vp,
1681 VPaintData &vpd,
1682 Object &ob,
1683 Mesh &mesh,
1686 const IndexMask &node_mask)
1687{
1688 const Brush &brush = *ob.runtime->sculpt_session->cache->brush;
1689
1690 switch (eBrushVertexPaintType(brush.vertex_brush_type)) {
1692 calculate_average_color(vpd, ob, mesh, brush, attribute, nodes, node_mask);
1693 vpaint_do_draw(depsgraph, vp, vpd, ob, mesh, nodes, node_mask, attribute);
1694 break;
1696 vpaint_do_draw(depsgraph, vp, vpd, ob, mesh, nodes, node_mask, attribute);
1697 break;
1699 vpaint_do_blur(depsgraph, vp, vpd, ob, mesh, nodes, node_mask, attribute);
1700 break;
1702 do_vpaint_brush_smear(depsgraph, vp, vpd, ob, mesh, nodes, node_mask, attribute);
1703 break;
1704 }
1705}
1706
1707static void vpaint_do_paint(const Depsgraph &depsgraph,
1708 const Scene &scene,
1709 const Brush &brush,
1710 Object &ob,
1711 PaintModeData *paint_mode_data)
1712{
1713 VPaintData &vpd = *static_cast<VPaintData *>(paint_mode_data);
1714 const VPaint &vp = *scene.toolsettings->vpaint;
1715 Mesh &mesh = *id_cast<Mesh *>(ob.data);
1716 IndexMaskMemory memory;
1717 const IndexMask node_mask = gather_brush_nodes(
1719 vwpaint::update_sculpt_normal(depsgraph, ob, vp, brush, node_mask);
1720
1721 if (auto_mask::is_enabled(vp.paint, ob, &brush)) {
1722 auto_mask::Cache &cache = auto_mask::stroke_cache_ensure(depsgraph, vp.paint, &brush, ob);
1724 cache.calc_cavity_factor(depsgraph, ob, node_mask);
1725 }
1726 }
1727
1728 bke::GSpanAttributeWriter attribute = mesh.attributes_for_write().lookup_for_write_span(
1729 mesh.active_color_attribute);
1730 BLI_assert(attribute.domain == vpd.domain);
1731
1732 /* Paint those leaves. */
1733 vpaint_paint_leaves(depsgraph,
1734 vp,
1735 vpd,
1736 ob,
1737 mesh,
1738 attribute.span,
1740 node_mask);
1741
1742 attribute.finish();
1743}
1744
1745void VertexPaintStroke::update_step(wmOperator * /*op*/, const StrokeStep &stroke_step)
1746{
1747 VPaintData &vpd = *static_cast<VPaintData *>(mode_data_.get());
1748 ViewContext &vc = vpd.vc;
1749 Object &ob = *vc.obact;
1751 StrokeCache &cache = *ss.cache;
1752
1754
1756 *this->depsgraph, vc, *vertex_paint_, this->paint_mode, ob, *base_, stroke_step);
1757
1759 *this->depsgraph, *this->scene, vertex_paint_->paint, ob, vpaint_do_paint, &vpd);
1760
1761 ss.cache->first_time = false;
1762 copy_v3_v3(cache.last_location, cache.location);
1763
1765
1766 Brush &brush = *BKE_paint_brush(this->paint);
1768 vpd.smear.color_prev = vpd.smear.color_curr;
1769 }
1770
1771 /* Calculate pivot for rotation around selection if needed.
1772 * also needed for "Frame Selected" on last stroke. */
1773 float loc_world[3];
1774 mul_v3_m4v3(loc_world, ob.object_to_world().ptr(), ss.cache->location);
1775 bke::paint::stroke_track_location(*this->paint, loc_world);
1776
1778
1780}
1781
1782void VertexPaintStroke::done(bool /*is_cancel*/, bool /*stroke_started*/)
1783{
1784 VPaintData *vpd = static_cast<VPaintData *>(mode_data_.get());
1785 Object &ob = *vpd->vc.obact;
1786
1788
1789 if (ss.cache && ss.cache->toggle_settings.alt_smooth) {
1791 }
1792
1794
1795 MEM_delete(ob.runtime->sculpt_session->cache);
1796 ob.runtime->sculpt_session->cache = nullptr;
1797}
1798
1800{
1801 VertexPaintStroke *stroke = MEM_new<VertexPaintStroke>(__func__, C, op, event);
1802 op->customdata = stroke;
1803
1804 vwpaint::init_stroke(*op, *stroke->bmain_, *stroke->paint, *stroke->depsgraph, *stroke->object);
1805
1806 const wmOperatorStatus retval = op->type->modal(C, op, event);
1807 OPERATOR_RETVAL_CHECK(retval);
1808
1809 if (retval == OPERATOR_FINISHED) {
1810 VertexPaintStroke *stroke = static_cast<VertexPaintStroke *>(op->customdata);
1811 if (stroke) {
1812 stroke->finish(C);
1813 MEM_delete(stroke);
1814 }
1815 return OPERATOR_FINISHED;
1816 }
1817
1819
1821
1823}
1824
1826{
1827 VertexPaintStroke *stroke = MEM_new<VertexPaintStroke>(__func__, C, op, nullptr);
1828 op->customdata = stroke;
1829
1830 vwpaint::init_stroke(*op, *stroke->bmain_, *stroke->paint, *stroke->depsgraph, *stroke->object);
1831
1832 stroke->exec(C, op);
1833
1834 MEM_delete(stroke);
1835 return OPERATOR_FINISHED;
1836}
1837
1839{
1840 VertexPaintStroke *stroke = static_cast<VertexPaintStroke *>(op->customdata);
1841 const wmOperatorStatus retval = stroke->modal(C, op, event);
1842
1844 MEM_delete(stroke);
1845 op->customdata = nullptr;
1846 }
1847
1848 return retval;
1849}
1850
1852{
1853 ot->name = "Vertex Paint";
1854 ot->idname = "PAINT_OT_vertex_paint";
1855 ot->description = "Paint a stroke in the active color attribute layer";
1856
1857 ot->invoke = vpaint_invoke;
1858 ot->modal = vpaint_modal;
1859 ot->exec = vpaint_exec;
1860 ot->poll = vertex_paint_poll;
1861
1863
1866 ot->srna,
1867 "override_location",
1868 false,
1869 "Override Location",
1870 "Override the given \"location\" array by recalculating object space positions from the "
1871 "provided \"mouse_event\" positions");
1873}
1874
1876
1877/* -------------------------------------------------------------------- */
1880
1881namespace ed::sculpt_paint {
1882
1883template<typename T>
1885 const T &value,
1886 const AttrDomain domain,
1887 const int cd_offset,
1888 const bool use_vert_sel,
1889 const bool only_visible = false)
1890{
1891 BMFace *f;
1892 BMIter iter;
1893 BM_ITER_MESH (f, &iter, &bm, BM_FACES_OF_MESH) {
1894 if (only_visible && BKE_paint_is_bmesh_face_hidden(f)) {
1895 continue;
1896 }
1897 BMLoop *l = f->l_first;
1898 do {
1899 if (!(use_vert_sel && !BM_elem_flag_test(l->v, BM_ELEM_SELECT))) {
1900 if (domain == AttrDomain::Corner) {
1901 *static_cast<T *>(BM_ELEM_CD_GET_VOID_P(l, cd_offset)) = value;
1902 }
1903 else if (domain == AttrDomain::Point) {
1904 *static_cast<T *>(BM_ELEM_CD_GET_VOID_P(l->v, cd_offset)) = value;
1905 }
1906 }
1907 } while ((l = l->next) != f->l_first);
1908 }
1909}
1910
1911template<typename T>
1913 const T &value,
1914 const AttrDomain domain,
1915 const MutableSpan<T> data,
1916 const bool use_vert_sel,
1917 const bool use_face_sel,
1918 const bool affect_alpha,
1919 const bool only_visible = false)
1920{
1921 const bke::AttributeAccessor attributes = mesh.attributes();
1922 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1923 VArraySpan<bool> select_vert;
1924 if (use_vert_sel) {
1925 select_vert = *attributes.lookup<bool>(".select_vert", bke::AttrDomain::Point);
1926 }
1927 VArraySpan<bool> select_poly;
1928 if (use_face_sel) {
1929 select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
1930 }
1931
1932 const OffsetIndices faces = mesh.faces();
1933 const Span<int> corner_verts = mesh.corner_verts();
1934
1935 for (const int i : faces.index_range()) {
1936 if (only_visible && !hide_poly.is_empty() && hide_poly[i]) {
1937 continue;
1938 }
1939 if (!select_poly.is_empty() && !select_poly[i]) {
1940 continue;
1941 }
1942 for (const int corner : faces[i]) {
1943 const int vert = corner_verts[corner];
1944 if (!select_vert.is_empty() && !select_vert[vert]) {
1945 continue;
1946 }
1947 const int data_index = domain == AttrDomain::Corner ? corner : vert;
1948 data[data_index].r = value.r;
1949 data[data_index].g = value.g;
1950 data[data_index].b = value.b;
1951 if (affect_alpha) {
1952 data[data_index].a = value.a;
1953 }
1954 }
1955 }
1956
1958}
1959
1961 const ColorPaint4f &color,
1962 const StringRef name,
1963 const bool use_vert_sel,
1964 const bool use_face_sel,
1965 const bool affect_alpha,
1966 const bool only_visible = false)
1967{
1969 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, name);
1970 if (attr.type == bke::AttrType::ColorFloat) {
1972 *bm, color, attr.domain, attr.offset, use_vert_sel, only_visible);
1973 }
1974 else if (attr.type == bke::AttrType::ColorByte) {
1977 attr.domain,
1978 attr.offset,
1979 use_vert_sel,
1980 only_visible);
1981 }
1982 }
1983 else {
1984 bke::GSpanAttributeWriter attribute = mesh.attributes_for_write().lookup_for_write_span(name);
1985 if (attribute.span.type().is<ColorGeometry4f>()) {
1987 mesh,
1988 color,
1989 attribute.domain,
1990 attribute.span.typed<ColorGeometry4f>().cast<ColorPaint4f>(),
1991 use_vert_sel,
1992 use_face_sel,
1993 affect_alpha,
1994 only_visible);
1995 }
1996 else if (attribute.span.type().is<ColorGeometry4b>()) {
1998 mesh,
2000 attribute.domain,
2001 attribute.span.typed<ColorGeometry4b>().cast<ColorPaint4b>(),
2002 use_vert_sel,
2003 use_face_sel,
2004 affect_alpha,
2005 only_visible);
2006 }
2007 attribute.finish();
2008 }
2009}
2010
2012 ColorPaint4f fill_color,
2013 bool only_selected = true,
2014 bool affect_alpha = true,
2015 const bool only_visible = false)
2016{
2018 if (!mesh) {
2019 return false;
2020 }
2021
2022 const bool use_face_sel = only_selected ? (mesh->editflag & ME_EDIT_PAINT_FACE_SEL) != 0 : false;
2023 const bool use_vert_sel = only_selected ? (mesh->editflag & ME_EDIT_PAINT_VERT_SEL) != 0 : false;
2025 fill_color,
2026 mesh->active_color_attribute,
2027 use_vert_sel,
2028 use_face_sel,
2029 affect_alpha,
2030 only_visible);
2031
2033
2034 /* NOTE: Original mesh is used for display, so tag it directly here. */
2036
2037 return true;
2038}
2039
2040bool object_active_color_init(Object &ob, const float fill_color[4])
2041{
2042 return fill_active_color(ob, ColorPaint4f(fill_color), false, true, false);
2043}
2044
2045} // namespace ed::sculpt_paint
2046
2048{
2049 using namespace blender::ed::sculpt_paint;
2050 Scene &scene = *CTX_data_scene(C);
2051 Object &obact = *CTX_data_active_object(C);
2052 Mesh *mesh = BKE_mesh_from_object(&obact);
2053 if (!mesh) {
2054 return OPERATOR_CANCELLED;
2055 }
2056 if (!ED_mesh_color_ensure(mesh, nullptr)) {
2057 return OPERATOR_CANCELLED;
2058 }
2059
2060 ColorPaint4f paintcol = vpaint_get_current_col(*scene.toolsettings->vpaint, false);
2061 const bool affect_alpha = RNA_boolean_get(op->ptr, "use_alpha");
2062
2063 /* Ensure valid sculpt state. */
2065
2067
2068 IndexMaskMemory memory;
2069 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
2070
2071 fill_active_color(obact, paintcol, true, affect_alpha, true);
2072
2073 pbvh.tag_attribute_changed(node_mask, mesh->active_color_attribute);
2074
2076 return OPERATOR_FINISHED;
2077}
2078
2080{
2081 ot->name = "Set Vertex Colors";
2082 ot->idname = "PAINT_OT_vertex_color_set";
2083 ot->description = "Fill the active vertex color layer with the current paint color";
2084
2087
2089
2091 "use_alpha",
2092 true,
2093 "Affect Alpha",
2094 "Set color completely opaque instead of reusing existing alpha");
2095}
2096
2098
2099} // namespace blender
Generic geometry attributes built on CustomData.
support for deformation groups and hooks.
General operations, lookup, etc. for blender objects.
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Object is a sort of wrapper for general info.
#define OPERATOR_RETVAL_CHECK(ret)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:502
#define ND_MODE
Definition WM_types.hh:464
#define NC_SCENE
Definition WM_types.hh:391
#define NC_OBJECT
Definition WM_types.hh:392
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_MESH(ele, iter, bm, itype)
void fill(const T &value) const
Definition BLI_array.hh:283
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
bool is_empty() const
Definition BLI_array.hh:275
ChannelStorageType r
ChannelStorageType g
ChannelStorageType b
ChannelStorageType a
GArray(Allocator allocator={}) noexcept
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define main()
#define attribute(slot)
#define T
void common_pressure_curves_init(Brush &brush)
Definition brush.cc:1549
int face_find_corner_from_vert(const IndexRange face, const Span< int > corner_verts, const int vert)
Definition BKE_mesh.hh:327
pbvh::Tree * pbvh_get(Object &object)
void stroke_track_location(Paint &paint, float3 location)
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
BLI_INLINE ColorSceneLinear4f< Alpha > decode(const ColorSceneLinearByteEncoded4b< Alpha > &color)
Definition BLI_color.hh:77
BLI_INLINE Color BLI_mix_colors(const IMB_BlendMode tool, const Color a, const Color b, const typename Traits::BlendType alpha)
BLI_INLINE ColorSceneLinearByteEncoded4b< Alpha > encode(const ColorSceneLinear4f< Alpha > &color)
Definition BLI_color.hh:50
bool mode_compat_set(bContext *C, Object *ob, eObjectMode mode, ReportList *reports)
Cache & stroke_cache_ensure(const Depsgraph &depsgraph, const Paint &paint, const Brush *brush, Object &ob)
bool is_enabled(const Paint &paint, const Object &object, const Brush *br)
void calc_vert_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::MeshNode &node, Span< int > verts, MutableSpan< float > factors)
StrokeToggleSettings create_toggle_settings(const wmOperator &op, Main &bmain, Paint &paint)
void update_cache_variants(Depsgraph &depsgraph, ViewContext &vc, VPaint &vp, const PaintMode paint_mode, Object &ob, Base &base, const PaintStroke::StrokeStep &stroke_step)
bool use_normal(const VPaint &vp)
void update_cache_invariants(VPaint &vp, SculptSession &ss, wmOperator *op, const float mval[2])
void update_sculpt_normal(const Depsgraph &depsgraph, const Object &ob, const VPaint &vp, const Brush &brush, const IndexMask node_mask)
bool brush_use_accumulate_ex(const Brush &brush, const eObjectMode ob_mode)
void get_brush_alpha_data(const SculptSession &ss, const Paint &paint, const Brush &brush, float *r_brush_size_pressure, float *r_brush_alpha_value, float *r_brush_alpha_pressure)
void smooth_brush_toggle_on(Main *bmain, Paint *paint, StrokeToggleSettings &toggle_settings)
bool brush_use_accumulate(const VPaint &vp)
void smooth_brush_toggle_off(Paint *paint, StrokeCache *cache)
bool test_brush_angle_falloff(const Brush &brush, const float angle_cos)
void init_stroke(const wmOperator &op, Main &main, Paint &paint, Depsgraph &depsgraph, Object &ob)
void mode_exit_generic(Scene &scene, Object &ob, const eObjectMode mode_flag)
IndexMask gather_brush_nodes(const Object &ob, const Brush &brush, IndexMaskMemory &memory, FunctionRef< bool(const bke::pbvh::Node &)> node_ignore_fn)
void mode_enter_generic(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob, const eObjectMode mode_flag)
Definition mesh_paint.cc:76
void calc_brush_strength_factors(const StrokeCache &cache, const Brush &brush, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7501
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7422
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
bool stroke_is_first_brush_step_of_symmetry_pass(const StrokeCache &cache)
Definition sculpt.cc:667
void do_symmetrical_brush_actions(const Depsgraph &depsgraph, const Scene &scene, const Paint &paint, Object &object, const BrushActionFn action_fn, PaintModeData *paint_mode_data)
bool paint_supports_dynamic_size(const Brush &br, PaintMode mode)
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
static void fill_mesh_face_or_corner_attribute(Mesh &mesh, const T &value, const AttrDomain domain, const MutableSpan< T > data, const bool use_vert_sel, const bool use_face_sel, const bool affect_alpha, const bool only_visible=false)
std::optional< float3 > stroke_get_location_bvh(Depsgraph &depsgraph, ViewContext &vc, const Sculpt &sd, const Brush *brush, const float mval[2], const bool force_original)
Definition sculpt.cc:4855
void calc_brush_distances(const SculptSession &ss, Span< float3 > vert_positions, Span< int > vert, eBrushFalloffShape falloff_shape, MutableSpan< float > r_distances)
Definition sculpt.cc:7370
void stroke_cache_common_init(ViewContext &vc, const Paint &paint, const Brush &brush, Object &object, const float2 mval)
std::optional< float3 > calc_area_normal(const Depsgraph &depsgraph, const Brush &brush, const Object &ob, const IndexMask &node_mask)
Definition sculpt.cc:1926
const float * brush_frontface_normal_from_falloff_shape(const SculptSession &ss, char falloff_shape)
Definition sculpt.cc:1341
static bool fill_active_color(Object &ob, ColorPaint4f fill_color, bool only_selected=true, bool affect_alpha=true, const bool only_visible=false)
bool object_active_color_init(Object &ob, const float fill_color[4])
std::optional< CursorGeometryInfo > cursor_geometry_info_update(bContext *C, const float2 &mval, const bool use_sampled_normal)
Definition sculpt.cc:4584
void fill_factor_from_hide(Span< bool > hide_vert, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7077
static void fill_mesh_color(Mesh &mesh, const ColorPaint4f &color, const StringRef name, const bool use_vert_sel, const bool use_face_sel, const bool affect_alpha, const bool only_visible=false)
static void fill_bm_face_or_corner_attribute(BMesh &bm, const T &value, const AttrDomain domain, const int cd_offset, const bool use_vert_sel, const bool only_visible=false)
constexpr ParallelGrainSize grain_size(int grain_size)
BMesh Iterators.
static std::unique_ptr< VPaintData > vpaint_init_vpaint(wmOperator *op, const ViewContext &vc, VPaint &vp, Mesh &mesh, const AttrDomain domain, const bke::AttrType type, const Brush &brush)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void vpaint_do_draw(const Depsgraph &depsgraph, const VPaint &vp, VPaintData &vpd, Object &ob, Mesh &mesh, const Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask, GMutableSpan attribute)
@ V3D_PROJ_RET_OK
Definition ED_view3d.hh:273
ScrArea * CTX_wm_area(const bContext *C)
static Color vpaint_blend_stroke(const VPaint &vp, MutableSpan< Color > prev_vertex_colors, MutableSpan< Color > vertex_colors, MutableSpan< Color > stroke_buffer, Color brush_mark_color, float brush_mark_alpha, float brush_strength, int index)
int RNA_enum_get(PointerRNA *ptr, const char *name)
static void filter_factors_with_selection(const Span< bool > select_vert, const Span< int > verts, const MutableSpan< float > factors)
static bool isZero(ColorPaint4f c)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
float BKE_brush_sample_tex_3d(const Paint *paint, const Brush *br, const MTex *mtex, const float3 &point, float4 &rgba, int thread, ImagePool *pool)
Definition brush.cc:985
ATTR_WARN_UNUSED_RESULT const BMLoop * l
bool vertex_paint_poll_ignore_tool(bContext *C)
eV3DProjStatus ED_view3d_project_float_object(const ARegion *region, const float co[3], float r_co[2], eV3DProjTest flag)
void PAINT_OT_vertex_color_set(wmOperatorType *ot)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
float BKE_brush_radius_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1366
@ PAINT_FALLOFF_SHAPE_SPHERE
@ PAINT_FALLOFF_SHAPE_TUBE
@ IMB_BLEND_ADD_ALPHA
Definition IMB_imbuf.hh:211
@ IMB_BLEND_MIX
Definition IMB_imbuf.hh:204
@ IMB_BLEND_ERASE_ALPHA
Definition IMB_imbuf.hh:210
void PAINT_OT_vertex_paint(wmOperatorType *ot)
@ WPAINT_BRUSH_TYPE_SMEAR
static void to_static_color_type(const bke::AttrType type, const Func &func)
static wmOperatorStatus vpaint_mode_toggle_exec(bContext *C, wmOperator *op)
void BKE_mesh_tessface_clear(Mesh *mesh)
static bool vertex_paint_poll_ex(bContext *C, bool check_tool)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
Base * CTX_data_active_base(const bContext *C)
bool BKE_brush_use_alpha_pressure(const Brush *brush)
Definition brush.cc:1385
static void paint_and_tex_color_alpha_intern(const VPaint &vp, const ViewContext *vc, const float3 &co, float4 &r_rgba)
void BKE_brush_unprojected_size_set(Paint *paint, Brush *brush, float unprojected_size)
Definition brush.cc:1390
VecBase< float, 4 > float4
MINLINE void zero_v4(float r[4])
static Color vpaint_blend(const VPaint &vp, Color color_curr, Color color_orig, Color color_paint, const typename Traits::ValueType alpha, const typename Traits::BlendType brush_alpha_value)
ColorSceneLinearByteEncoded4b< eAlpha::Straight > ColorPaint4b
static wmOperatorStatus vpaint_modal(bContext *C, wmOperator *op, const wmEvent *event)
void BKE_curvemapping_init(CurveMapping *cumap)
static void vpaint_do_blur(const Depsgraph &depsgraph, const VPaint &vp, VPaintData &vpd, Object &ob, Mesh &mesh, const Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask, GMutableSpan attribute)
VecBase< float, 2 > float2
MINLINE void mul_v3_v3(float r[3], const float a[3])
@ ME_EDIT_PAINT_VERT_SEL
@ ME_EDIT_PAINT_FACE_SEL
void DEG_id_tag_update(ID *id, unsigned int flags)
static Color fromFloat(const ColorPaint4f &c)
BMesh const char void * data
static wmOperatorStatus vpaint_exec(bContext *C, wmOperator *op)
static void do_vpaint_brush_smear(const Depsgraph &depsgraph, const VPaint &vp, VPaintData &vpd, Object &ob, Mesh &mesh, const Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask, GMutableSpan attribute)
Mesh * BKE_mesh_from_object(Object *ob)
void BKE_mesh_batch_cache_dirty_tag(Mesh *mesh, eMeshBatchDirtyMode mode)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static void vpaint_paint_leaves(const Depsgraph &depsgraph, const VPaint &vp, VPaintData &vpd, Object &ob, Mesh &mesh, GMutableSpan attribute, const Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask)
static wmOperatorStatus vpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event)
float3 BKE_paint_seed_hsv_jitter()
static void do_vpaint_brush_blur_verts(const Depsgraph &depsgraph, const VPaint &vp, VPaintData &vpd, Object &ob, Mesh &mesh, const Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask, GMutableSpan attribute)
bool vertex_paint_poll(bContext *C)
static float paint_and_tex_color_alpha(const VPaint &vp, VPaintData &vpd, const float v_co[3], Color *r_color)
void WM_toolsystem_update_from_context_view3d(bContext *C)
static ColorPaint4f vpaint_get_current_col(VPaint &vp, bool secondary)
void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
void ED_object_vpaintmode_exit_ex(Scene &scene, Object &ob)
int BKE_brush_size_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1356
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
ToolSettings * CTX_data_tool_settings(const bContext *C)
bool BKE_paint_brush_set(Paint *paint, Brush *brush)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void BKE_brush_size_set(Paint *paint, Brush *brush, int size)
Definition brush.cc:1340
bool ED_mesh_color_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:331
static void do_vpaint_brush_blur_loops(const Depsgraph &depsgraph, const VPaint &vp, VPaintData &vpd, Object &ob, Mesh &mesh, const Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask, GMutableSpan attribute)
const MTex * BKE_brush_mask_texture_get(const Brush *brush, PaintMode paint_mode)
Definition brush.cc:969
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmMsgBus * CTX_wm_message_bus(const bContext *C)
ColorSceneLinear4f< eAlpha::Straight > ColorPaint4f
void project_plane_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
@ BRUSH_AUTOMASKING_CAVITY_ALL
void paint_stroke_operator_properties(wmOperatorType *ot)
static ColorPaint4f toFloat(const Color &c)
bool BKE_brush_use_size_pressure(const Brush *brush)
Definition brush.cc:1380
const Brush * BKE_paint_brush_for_read(const Paint *paint)
MINLINE float normalize_v3(float n[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
Depsgraph * CTX_data_depsgraph_on_load(const bContext *C)
Main * CTX_data_main(const bContext *C)
bool vertex_paint_mode_poll(bContext *C)
bool WM_toolsystem_active_tool_is_brush(const bContext *C)
float paint_calc_object_space_radius(const ViewContext &vc, const float3 &center, float pixel_radius)
bool BKE_paint_is_bmesh_face_hidden(const BMFace *f)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
void ED_object_vpaintmode_enter(bContext *C, Depsgraph &depsgraph)
bool BKE_pbvh_node_fully_hidden_get(const bke::pbvh::Node &node)
Definition pbvh.cc:1625
@ V3D_PROJ_TEST_CLIP_NEAR
Definition ED_view3d.hh:298
@ V3D_PROJ_TEST_CLIP_BB
Definition ED_view3d.hh:296
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
static void calculate_average_color(VPaintData &vpd, Object &ob, Mesh &mesh, const Brush &brush, const GSpan attribute, const Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask)
std::optional< BrushColorJitterSettings > BKE_brush_color_jitter_get_settings(const Paint *paint, const Brush *brush)
Definition brush.cc:1262
static CLG_LogRef LOG
Definition logging.cpp:12
void ED_object_vpaintmode_exit(bContext *C)
Brush * BKE_paint_brush(Paint *paint)
Scene * CTX_data_scene(const bContext *C)
float3 BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1253
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void ED_object_vpaintmode_enter_ex(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static float3 get_brush_color(const Paint *paint, const Brush *brush, const StrokeCache &cache, const ColorPaint4f &paint_color)
@ BKE_MESH_BATCH_DIRTY_ALL
Definition BKE_mesh.h:48
bool BKE_paint_brush_set_essentials(Main *bmain, Paint *paint, const char *name)
float3 BKE_paint_randomize_color(const BrushColorJitterSettings &color_jitter, const float3 &initial_hsv_jitter, const float distance, const float pressure, const float3 &color)
Object * CTX_data_active_object(const bContext *C)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
ARegion * CTX_wm_region(const bContext *C)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
float BKE_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1454
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
static wmOperatorStatus vertex_color_set_exec(bContext *C, wmOperator *op)
static void vpaint_do_paint(const Depsgraph &depsgraph, const Scene &scene, const Brush &brush, Object &ob, PaintModeData *paint_mode_data)
static void init_session_data(Object &ob)
@ VPAINT_BRUSH_TYPE_DRAW
@ VPAINT_BRUSH_TYPE_SMEAR
@ VPAINT_BRUSH_TYPE_AVERAGE
@ VPAINT_BRUSH_TYPE_BLUR
float3 BKE_brush_secondary_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1297
#define sqrtf
struct BMLoop * next
struct BMVert * v
struct CurveMapping * curve_rand_value
struct CurveMapping * curve_rand_saturation
eBrushVertexPaintType vertex_brush_type
struct CurveMapping * curve_rand_hue
eMTex_BrushMapMode brush_map_mode
bke::ObjectRuntime * runtime
bke::PaintRuntime * runtime
struct ToolSettings * toolsettings
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
ColorPaint4f paintcol
struct blender::VPaintData::@061176217167105244271012330276020027300325355275 smear
bool test_start(wmOperator *op, float2 mouse) override
VertexPaintStroke(bContext *C, wmOperator *op, const wmEvent *event)
void update_step(wmOperator *op, const StrokeStep &stroke_step) override
std::optional< float3 > get_location(float2 mouse, bool force_original) override
void redraw(bool final) override
void done(bool is_cancel, bool stroke_started) override
Depsgraph * depsgraph
Definition ED_view3d.hh:78
wmOperatorStatus modal(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus exec(bContext *C, wmOperator *op)
std::unique_ptr< PaintModeData > mode_data_
std::unique_ptr< auto_mask::Cache > automasking
void calc_cavity_factor(const Depsgraph &depsgraph, const Object &object, const IndexMask &node_mask)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type
#define WM_msg_publish_rna_prop(mbus, id_, data_, type_, prop_)