Blender V5.3
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_color.hh"
6#include "BLI_color_mix.hh"
7#include "BLI_math_color.hh"
8
9#include "DNA_brush_types.h"
10
11#include "BKE_brush.hh"
12#include "BKE_context.hh"
13#include "BKE_curves.hh"
14#include "BKE_grease_pencil.hh"
15#include "BKE_paint.hh"
16
18
20
21class VertexPaintOperation : public GreasePencilStrokeOperationCommon {
23
24 public:
25 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
26 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
27 void on_stroke_done(const bContext & /*C*/) override {}
28};
29
31{
32 this->init_stroke(C, start_sample);
33 this->on_stroke_extended(C, start_sample);
34}
35
37 const InputSample &extension_sample)
38{
39 const Scene &scene = *CTX_data_scene(&C);
41 const Brush &brush = *BKE_paint_brush(&paint);
42 const bool invert = this->is_inverted(brush);
43
44 const IMB_BlendMode blend_mode = IMB_BlendMode(brush.blend);
45
46 const bool use_selection_masking = ED_grease_pencil_any_vertex_mask_selection(
47 scene.toolsettings);
48
49 const bool do_points = do_vertex_color_points(brush);
50 const bool do_fill = do_vertex_color_fill(brush);
51
52 float color_linear[3];
53 copy_v3_v3(color_linear, BKE_brush_color_get(&paint, &brush));
54 const ColorPaint4f mix_color(color_linear[0], color_linear[1], color_linear[2], 1.0f);
55
56 this->foreach_editable_drawing(C, [&](const GreasePencilStrokeParams &params) {
57 IndexMaskMemory memory;
58 const IndexMask point_selection = point_mask_for_stroke_operation(
59 params, use_selection_masking, memory);
60 if (!point_selection.is_empty() && do_points) {
61 const Array<float2> view_positions = view_positions_from_point_mask(params, point_selection);
63
64 if (invert) {
65 /* Erase vertex colors. */
66 point_selection.foreach_index(
67 [&](const int64_t point_i) {
68 const float influence = brush_point_influence(paint,
69 brush,
70 view_positions[point_i],
71 extension_sample,
72 params.multi_frame_falloff);
73
74 ColorGeometry4f &color = vertex_colors[point_i];
75 color.a -= influence;
76 color.a = math::max(color.a, 0.0f);
77 },
79 }
80 else {
81 /* Mix brush color into vertex colors by influence. */
82 point_selection.foreach_index(
83 [&](const int64_t point_i) {
84 const float influence = brush_point_influence(paint,
85 brush,
86 view_positions[point_i],
87 extension_sample,
88 params.multi_frame_falloff);
89
90 ColorGeometry4f &color = vertex_colors[point_i];
91
92 using Color = ColorPaint4f;
94
95 const Color linearrgb_color = color::unpremultiply_alpha(color);
96
98 blend_mode, linearrgb_color, mix_color, Traits::range * influence));
99 },
101 }
102 }
103
104 const IndexMask fill_selection = fill_mask_for_stroke_operation(
105 params, use_selection_masking, memory);
106 if (!fill_selection.is_empty() && do_fill) {
107 BLI_assert(params.drawing.fills().has_value());
108 const GroupedSpan<int> fills = *params.drawing.fills();
109 const bke::CurvesGeometry &curves = params.drawing.strokes();
110 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
112 /* TODO. Only calculate needed positions. */
113 const Array<float2> view_positions = view_positions_from_curve_mask(params,
114 curves.curves_range());
115
116 if (invert) {
117 fill_selection.foreach_index(
118 [&](const int64_t fill_i) {
119 const Span<int> fill_curves = fills[fill_i];
120 const IndexMask fill_curve_mask = IndexMask::from_indices(fill_curves, memory);
121
122 float influence = 0.0f;
123 fill_curve_mask.foreach_index([&](const int64_t curve) {
124 const IndexRange points = points_by_curve[curve];
125 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
126 influence = math::max(influence,
128 brush,
129 curve_view_positions,
130 extension_sample,
131 params.multi_frame_falloff));
132 });
133
134 ColorGeometry4f color = fill_colors[fill_curves.first()];
135 color.a -= influence;
136 color.a = math::max(color.a, 0.0f);
137
138 index_mask::masked_fill(fill_colors, color, fill_curve_mask);
139 },
141 }
142 else {
143 fill_selection.foreach_index(
144 [&](const int64_t fill_i) {
145 const Span<int> fill_curves = fills[fill_i];
146 const IndexMask fill_curve_mask = IndexMask::from_indices(fill_curves, memory);
147
148 float influence = 0.0f;
149 fill_curve_mask.foreach_index([&](const int64_t curve) {
150 const IndexRange points = points_by_curve[curve];
151 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
152 influence = math::max(influence,
154 brush,
155 curve_view_positions,
156 extension_sample,
157 params.multi_frame_falloff));
158 });
159
160 ColorGeometry4f color = fill_colors[fill_curves.first()];
161
162 using Color = ColorPaint4f;
164
165 const Color linearrgb_color = color::unpremultiply_alpha(color);
166
168 blend_mode, linearrgb_color, mix_color, Traits::range * influence));
169
170 index_mask::masked_fill(fill_colors, color, fill_curve_mask);
171 },
173 }
174 }
175
176 return true;
177 });
178}
179
180std::unique_ptr<GreasePencilStrokeOperation> new_vertex_paint_operation(
181 const BrushStrokeMode stroke_mode)
182{
183 return std::make_unique<VertexPaintOperation>(stroke_mode);
184}
185
186} // namespace blender::ed::sculpt_paint::greasepencil
Low-level operations for curves.
Low-level operations for grease pencil.
#define C
Definition RandGen.cpp:29
Span< T > as_span() const
Definition BLI_array.hh:254
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &memory)
constexpr const T & first() const
Definition BLI_span.hh:319
std::optional< GroupedSpan< int > > fills() const
const bke::CurvesGeometry & strokes() const
MutableSpan< ColorGeometry4f > fill_colors_for_write()
MutableSpan< ColorGeometry4f > vertex_colors_for_write()
void init_stroke(const bContext &C, const InputSample &start_sample)
void foreach_editable_drawing(const bContext &C, FunctionRef< bool(const GreasePencilStrokeParams &params, const DeltaProjectionFunc &projection_fn)> 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, Mode mode=exec_mode::serial) const
void mix_color(float fac, float4 col1, float4 col2, float4 &outcol)
float2x2 invert(float2x2 mat)
ColorSceneLinear4f< eAlpha::Straight > unpremultiply_alpha(const ColorSceneLinear4f< Alpha > &color)
Definition BLI_color.hh:111
ColorSceneLinear4f< eAlpha::Premultiplied > premultiply_alpha(const ColorSceneLinear4f< Alpha > &color)
Definition BLI_color.hh:93
BLI_INLINE Color BLI_mix_colors(const IMB_BlendMode tool, const Color a, const Color b, const typename Traits::BlendType alpha)
typename TraitsType< T >::type Traits
IndexMask fill_mask_for_stroke_operation(const GreasePencilStrokeParams &params, bool use_selection_masking, IndexMaskMemory &memory)
bool do_vertex_color_fill(const Brush &brush)
Array< float2 > view_positions_from_curve_mask(const GreasePencilStrokeParams &params, const IndexMask &curve_mask)
std::unique_ptr< GreasePencilStrokeOperation > new_vertex_paint_operation(BrushStrokeMode stroke_mode)
IndexMask point_mask_for_stroke_operation(const GreasePencilStrokeParams &params, bool use_selection_masking, IndexMaskMemory &memory)
float brush_point_influence(const Paint &paint, const Brush &brush, const float2 &co, const InputSample &sample, float multi_frame_falloff)
Array< float2 > view_positions_from_point_mask(const GreasePencilStrokeParams &params, const IndexMask &point_mask)
float brush_fill_influence(const Paint &paint, const Brush &brush, Span< float2 > fill_positions, const InputSample &sample, float multi_frame_falloff)
bool do_vertex_color_points(const Brush &brush)
constexpr ParallelGrainSize grain_size(int grain_size)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
T max(const T &a, const T &b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
MINLINE void copy_v3_v3(float r[3], const float a[3])
bool ED_grease_pencil_any_vertex_mask_selection(const ToolSettings *tool_settings)
ColorSceneLinear4f< eAlpha::Straight > ColorPaint4f
Paint * BKE_paint_get_active_from_context(const 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
struct ToolSettings * toolsettings