Blender V4.3
sculpt_paint/grease_pencil_vertex_paint.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_color.hh"
6
7#include "BKE_brush.hh"
8#include "BKE_context.hh"
9#include "BKE_curves.hh"
10#include "BKE_grease_pencil.hh"
11#include "BKE_paint.hh"
12
14
16
19
20 public:
21 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
22 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
23 void on_stroke_done(const bContext & /*C*/) override {}
24};
25
27{
28 this->init_stroke(C, start_sample);
29 this->on_stroke_extended(C, start_sample);
30}
31
33 const InputSample &extension_sample)
34{
35 const Scene &scene = *CTX_data_scene(&C);
37 const Brush &brush = *BKE_paint_brush(&paint);
38 const bool invert = this->is_inverted(brush);
39
40 const bool is_masking = GPENCIL_ANY_VERTEX_MASK(
41 eGP_vertex_SelectMaskFlag(scene.toolsettings->gpencil_selectmode_vertex));
42
43 const bool do_points = do_vertex_color_points(brush);
44 const bool do_fill = do_vertex_color_fill(brush);
45
46 float color_linear[3];
47 srgb_to_linearrgb_v3_v3(color_linear, BKE_brush_color_get(&scene, &paint, &brush));
48 const ColorGeometry4f mix_color(color_linear[0], color_linear[1], color_linear[2], 1.0f);
49
51 IndexMaskMemory memory;
52 const IndexMask point_selection = point_selection_mask(params, is_masking, memory);
53 if (!point_selection.is_empty() && do_points) {
54 Array<float2> view_positions = calculate_view_positions(params, point_selection);
55 MutableSpan<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors_for_write();
56
57 if (invert) {
58 /* Erase vertex colors. */
59 point_selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
60 const float influence = brush_point_influence(
61 scene, brush, view_positions[point_i], extension_sample, params.multi_frame_falloff);
62
63 ColorGeometry4f &color = vertex_colors[point_i];
64 color.a -= influence;
65 color.a = math::max(color.a, 0.0f);
66 });
67 }
68 else {
69 /* Mix brush color into vertex colors by influence using alpha over. */
70 point_selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
71 const float influence = brush_point_influence(
72 scene, brush, view_positions[point_i], extension_sample, params.multi_frame_falloff);
73
74 ColorGeometry4f &color = vertex_colors[point_i];
75 color = math::interpolate(color, mix_color, influence);
76 });
77 }
78 }
79
80 const IndexMask fill_selection = fill_selection_mask(params, is_masking, memory);
81 if (!fill_selection.is_empty() && do_fill) {
82 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
83 Array<float2> view_positions = calculate_view_positions(params, point_selection);
84 MutableSpan<ColorGeometry4f> fill_colors = params.drawing.fill_colors_for_write();
85
86 if (invert) {
87 fill_selection.foreach_index(GrainSize(1024), [&](const int64_t curve_i) {
88 const IndexRange points = points_by_curve[curve_i];
89 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
90 const float influence = brush_fill_influence(
91 scene, brush, curve_view_positions, extension_sample, params.multi_frame_falloff);
92
93 ColorGeometry4f &color = fill_colors[curve_i];
94 color.a -= influence;
95 color.a = math::max(color.a, 0.0f);
96 });
97 }
98 else {
99 fill_selection.foreach_index(GrainSize(1024), [&](const int64_t curve_i) {
100 const IndexRange points = points_by_curve[curve_i];
101 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
102 const float influence = brush_fill_influence(
103 scene, brush, curve_view_positions, extension_sample, params.multi_frame_falloff);
104
105 ColorGeometry4f &color = fill_colors[curve_i];
106 color = math::interpolate(color, mix_color, influence);
107 });
108 }
109 }
110
111 return true;
112 });
113}
114
115std::unique_ptr<GreasePencilStrokeOperation> new_vertex_paint_operation(
116 const BrushStrokeMode stroke_mode)
117{
118 return std::make_unique<VertexPaintOperation>(stroke_mode);
119}
120
121} // namespace blender::ed::sculpt_paint::greasepencil
const float * BKE_brush_color_get(const Scene *scene, const Paint *paint, const Brush *brush)
Definition brush.cc:1029
Scene * CTX_data_scene(const bContext *C)
Low-level operations for curves.
Low-level operations for grease pencil.
Paint * BKE_paint_get_active_from_context(const bContext *C)
Definition paint.cc:477
Brush * BKE_paint_brush(Paint *paint)
Definition paint.cc:649
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
#define GPENCIL_ANY_VERTEX_MASK(flag)
eGP_vertex_SelectMaskFlag
Span< T > as_span() const
Definition BLI_array.hh:232
void foreach_editable_drawing(const bContext &C, FunctionRef< bool(const GreasePencilStrokeParams &params)> fn) const
void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override
void on_stroke_begin(const bContext &C, const InputSample &start_sample) override
void foreach_index(Fn &&fn) const
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition invert.h:9
std::unique_ptr< GreasePencilStrokeOperation > new_vertex_paint_operation(BrushStrokeMode stroke_mode)
IndexMask point_selection_mask(const GreasePencilStrokeParams &params, const bool use_masking, IndexMaskMemory &memory)
Array< float2 > calculate_view_positions(const GreasePencilStrokeParams &params, const IndexMask &selection)
IndexMask fill_selection_mask(const GreasePencilStrokeParams &params, const bool use_masking, IndexMaskMemory &memory)
float brush_point_influence(const Scene &scene, const Brush &brush, const float2 &co, const InputSample &sample, float multi_frame_falloff)
float brush_fill_influence(const Scene &scene, const Brush &brush, Span< float2 > fill_positions, const InputSample &sample, float multi_frame_falloff)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(const T &a, const T &b)
BrushStrokeMode
__int64 int64_t
Definition stdint.h:89