Blender V5.3
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 C,
49 [&](const GreasePencilStrokeParams &params) {
50 IndexMaskMemory memory;
51 const IndexMask point_selection = point_mask_for_stroke_operation(
52 params, use_selection_masking, memory);
53 if (!point_selection.is_empty() && do_points) {
54 const Array<float2> view_positions = view_positions_from_point_mask(params,
55 point_selection);
57 point_selection.foreach_index(
58 [&](const int64_t point_i) {
59 const float influence = brush_point_influence(paint,
60 brush,
61 view_positions[point_i],
62 extension_sample,
63 params.multi_frame_falloff);
64 if (influence > 0.0f && vertex_colors[point_i].a > 0.0f) {
65 vertex_colors[point_i] = replace_color;
66 }
67 },
69 }
70
71 const IndexMask fill_selection = fill_mask_for_stroke_operation(
72 params, use_selection_masking, memory);
73 if (!fill_selection.is_empty() && do_fill) {
74 BLI_assert(params.drawing.fills().has_value());
75 const GroupedSpan<int> fills = *params.drawing.fills();
76 const bke::CurvesGeometry &curves = params.drawing.strokes();
77 const OffsetIndices<int> points_by_curve = curves.points_by_curve();
79 /* TODO. Only calculate needed positions. */
80 const Array<float2> view_positions = view_positions_from_curve_mask(
81 params, curves.curves_range());
82
83 fill_selection.foreach_index(
84 [&](const int64_t fill_i) {
85 const Span<int> fill_curves = fills[fill_i];
86 const IndexMask fill_curve_mask = IndexMask::from_indices(fill_curves, memory);
87
88 float influence = 0.0f;
89 fill_curve_mask.foreach_index([&](const int64_t curve) {
90 const IndexRange points = points_by_curve[curve];
91 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
92 influence = math::max(influence,
94 brush,
95 curve_view_positions,
96 extension_sample,
97 params.multi_frame_falloff));
98 });
99
100 ColorGeometry4f color = fill_colors[fill_curves.first()];
101 color.a -= influence;
102 color.a = math::max(color.a, 0.0f);
103
104 if (influence > 0.0f && color.a > 0.0f) {
105 color = replace_color;
106 }
107
108 index_mask::masked_fill(fill_colors, color, fill_curve_mask);
109 },
110
112 }
113 return true;
114 },
116}
117
119
120std::unique_ptr<GreasePencilStrokeOperation> new_vertex_replace_operation()
121{
122 return std::make_unique<VertexReplaceOperation>();
123}
124
125} // 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
std::unique_ptr< GreasePencilStrokeOperation > new_vertex_replace_operation()
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)
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)
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
VecBase< float, 3 > float3
struct ToolSettings * toolsettings