Blender V5.0
grease_pencil_vertex_replace.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 "BKE_brush.hh"
6#include "BKE_context.hh"
7#include "BKE_curves.hh"
9#include "BKE_paint.hh"
10
12
14
15class VertexReplaceOperation : public GreasePencilStrokeOperationCommon {
17
18 public:
19 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
20 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
21 void on_stroke_done(const bContext &C) override;
22};
23
25{
26 this->init_stroke(C, start_sample);
27 this->on_stroke_extended(C, start_sample);
28}
29
31 const InputSample &extension_sample)
32{
33 const Scene &scene = *CTX_data_scene(&C);
35 const Brush &brush = *BKE_paint_brush(&paint);
36
37 const bool use_selection_masking = ED_grease_pencil_any_vertex_mask_selection(
38 scene.toolsettings);
39
40 const bool do_points = do_vertex_color_points(brush);
41 const bool do_fill = do_vertex_color_fill(brush);
42
43 float3 color_linear;
44 copy_v3_v3(color_linear, BKE_brush_color_get(&paint, &brush));
45 const ColorGeometry4f replace_color(color_linear.x, color_linear.y, color_linear.z, 1.0f);
46
48 IndexMaskMemory memory;
49 const IndexMask point_selection = point_mask_for_stroke_operation(
50 params, use_selection_masking, memory);
51 if (!point_selection.is_empty() && do_points) {
52 Array<float2> view_positions = calculate_view_positions(params, point_selection);
53 MutableSpan<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors_for_write();
54 point_selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
55 const float influence = brush_point_influence(
56 paint, brush, view_positions[point_i], extension_sample, params.multi_frame_falloff);
57 if (influence > 0.0f && vertex_colors[point_i].a > 0.0f) {
58 vertex_colors[point_i] = replace_color;
59 }
60 });
61 }
62
63 const IndexMask fill_selection = fill_mask_for_stroke_operation(
64 params, use_selection_masking, memory);
65 if (!fill_selection.is_empty() && do_fill) {
66 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
67 Array<float2> view_positions = calculate_view_positions(params, point_selection);
68 MutableSpan<ColorGeometry4f> fill_colors = params.drawing.fill_colors_for_write();
69
70 fill_selection.foreach_index(GrainSize(1024), [&](const int64_t curve_i) {
71 const IndexRange points = points_by_curve[curve_i];
72 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
73 const float influence = brush_fill_influence(
74 paint, brush, curve_view_positions, extension_sample, params.multi_frame_falloff);
75 if (influence > 0.0f && fill_colors[curve_i].a > 0.0f) {
76 fill_colors[curve_i] = replace_color;
77 }
78 });
79 }
80 return true;
81 });
82}
83
85
86std::unique_ptr<GreasePencilStrokeOperation> new_vertex_replace_operation()
87{
88 return std::make_unique<VertexReplaceOperation>();
89}
90
91} // 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]
std::unique_ptr< GreasePencilStrokeOperation > new_vertex_replace_operation()
IndexMask fill_mask_for_stroke_operation(const GreasePencilStrokeParams &params, bool use_selection_masking, IndexMaskMemory &memory)
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)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
struct ToolSettings * toolsettings