Blender V5.0
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
17class VertexPaintOperation : public GreasePencilStrokeOperationCommon {
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 use_selection_masking = ED_grease_pencil_any_vertex_mask_selection(
41 scene.toolsettings);
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 copy_v3_v3(color_linear, BKE_brush_color_get(&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_mask_for_stroke_operation(
53 params, use_selection_masking, memory);
54 if (!point_selection.is_empty() && do_points) {
55 Array<float2> view_positions = calculate_view_positions(params, point_selection);
56 MutableSpan<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors_for_write();
57
58 if (invert) {
59 /* Erase vertex colors. */
60 point_selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
61 const float influence = brush_point_influence(
62 paint, brush, view_positions[point_i], extension_sample, params.multi_frame_falloff);
63
64 ColorGeometry4f &color = vertex_colors[point_i];
65 color.a -= influence;
66 color.a = math::max(color.a, 0.0f);
67 });
68 }
69 else {
70 /* Mix brush color into vertex colors by influence using alpha over. */
71 point_selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
72 const float influence = brush_point_influence(
73 paint, brush, view_positions[point_i], extension_sample, params.multi_frame_falloff);
74
75 ColorGeometry4f &color = vertex_colors[point_i];
76 color = math::interpolate(color, mix_color, influence);
77 });
78 }
79 }
80
81 const IndexMask fill_selection = fill_mask_for_stroke_operation(
82 params, use_selection_masking, memory);
83 if (!fill_selection.is_empty() && do_fill) {
84 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
85 Array<float2> view_positions = calculate_view_positions(params, point_selection);
86 MutableSpan<ColorGeometry4f> fill_colors = params.drawing.fill_colors_for_write();
87
88 if (invert) {
89 fill_selection.foreach_index(GrainSize(1024), [&](const int64_t curve_i) {
90 const IndexRange points = points_by_curve[curve_i];
91 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
92 const float influence = brush_fill_influence(
93 paint, brush, curve_view_positions, extension_sample, params.multi_frame_falloff);
94
95 ColorGeometry4f &color = fill_colors[curve_i];
96 color.a -= influence;
97 color.a = math::max(color.a, 0.0f);
98 });
99 }
100 else {
101 fill_selection.foreach_index(GrainSize(1024), [&](const int64_t curve_i) {
102 const IndexRange points = points_by_curve[curve_i];
103 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
104 const float influence = brush_fill_influence(
105 paint, brush, curve_view_positions, extension_sample, params.multi_frame_falloff);
106
107 ColorGeometry4f &color = fill_colors[curve_i];
108 color = math::interpolate(color, mix_color, influence);
109 });
110 }
111 }
112
113 return true;
114 });
115}
116
117std::unique_ptr<GreasePencilStrokeOperation> new_vertex_paint_operation(
118 const BrushStrokeMode stroke_mode)
119{
120 return std::make_unique<VertexPaintOperation>(stroke_mode);
121}
122
123} // namespace blender::ed::sculpt_paint::greasepencil
const float * BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1161
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:476
Brush * BKE_paint_brush(Paint *paint)
Definition paint.cc:645
MINLINE void copy_v3_v3(float r[3], const float a[3])
#define C
Definition RandGen.cpp:29
long long int int64_t
Span< T > as_span() const
Definition BLI_array.hh:243
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) const
bool ED_grease_pencil_any_vertex_mask_selection(const ToolSettings *tool_settings)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
CCL_NAMESPACE_BEGIN ccl_device float invert(const float color, const float factor)
Definition invert.h:11
IndexMask fill_mask_for_stroke_operation(const GreasePencilStrokeParams &params, bool use_selection_masking, IndexMaskMemory &memory)
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 > calculate_view_positions(const GreasePencilStrokeParams &params, const IndexMask &selection)
float brush_fill_influence(const Paint &paint, 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)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
BrushStrokeMode
struct ToolSettings * toolsettings