Blender V5.0
grease_pencil_vertex_average.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 VertexAverageOperation : 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 const float radius = brush_radius(paint, brush, extension_sample.pressure);
37 const float radius_squared = radius * radius;
38
39 const bool use_selection_masking = ED_grease_pencil_any_vertex_mask_selection(
40 scene.toolsettings);
41
42 const bool do_points = do_vertex_color_points(brush);
43 const bool do_fill = do_vertex_color_fill(brush);
44
45 /* Compute the average color under the brush. */
46 float3 average_color(0.0f);
47 int color_count = 0;
49 IndexMaskMemory memory;
50 const IndexMask point_selection = point_mask_for_stroke_operation(
51 params, use_selection_masking, memory);
52 if (!point_selection.is_empty() && do_points) {
53 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
54 const VArray<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors();
55
56 point_selection.foreach_index([&](const int64_t point_i) {
57 const ColorGeometry4f color = vertex_colors[point_i];
58 if (color.a > 0.0f && math::distance_squared(extension_sample.mouse_position,
59 view_positions[point_i]) < radius_squared)
60 {
61 average_color += float3(color.r, color.g, color.b);
62 color_count++;
63 }
64 });
65 }
66 const IndexMask fill_selection = fill_mask_for_stroke_operation(
67 params, use_selection_masking, memory);
68 if (!fill_selection.is_empty() && do_fill) {
69 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
70 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
71 const VArray<ColorGeometry4f> fill_colors = params.drawing.fill_colors();
72
73 fill_selection.foreach_index([&](const int64_t curve_i) {
74 const IndexRange points = points_by_curve[curve_i];
75 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
76 const ColorGeometry4f color = fill_colors[curve_i];
77 if (color.a > 0.0f && closest_distance_to_surface_2d(extension_sample.mouse_position,
78 curve_view_positions) < radius)
79 {
80 average_color += float3(color.r, color.g, color.b);
81 color_count++;
82 }
83 });
84 }
85 return true;
86 });
87
88 if (color_count <= 0) {
89 return;
90 }
91 average_color = average_color / color_count;
92 /* The average color is the color that will be mixed in. */
93 const ColorGeometry4f mix_color(average_color.x, average_color.y, average_color.z, 1.0f);
94
96 IndexMaskMemory memory;
97 const IndexMask point_selection = point_mask_for_stroke_operation(
98 params, use_selection_masking, memory);
99 if (!point_selection.is_empty() && do_points) {
100 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
101 MutableSpan<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors_for_write();
102
103 point_selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
104 const float influence = brush_point_influence(
105 paint, brush, view_positions[point_i], extension_sample, params.multi_frame_falloff);
106
107 ColorGeometry4f &color = vertex_colors[point_i];
108 color = math::interpolate(color, mix_color, influence);
109 });
110 }
111
112 const IndexMask fill_selection = fill_mask_for_stroke_operation(
113 params, use_selection_masking, memory);
114 if (!fill_selection.is_empty() && do_fill) {
115 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
116 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
117 MutableSpan<ColorGeometry4f> fill_colors = params.drawing.fill_colors_for_write();
118
119 fill_selection.foreach_index(GrainSize(1024), [&](const int64_t curve_i) {
120 const IndexRange points = points_by_curve[curve_i];
121 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
122 const float influence = brush_fill_influence(
123 paint, brush, curve_view_positions, extension_sample, params.multi_frame_falloff);
124
125 ColorGeometry4f &color = fill_colors[curve_i];
126 color = math::interpolate(color, mix_color, influence);
127 });
128 }
129 return true;
130 });
131}
132
134
135std::unique_ptr<GreasePencilStrokeOperation> new_vertex_average_operation()
136{
137 return std::make_unique<VertexAverageOperation>();
138}
139
140} // namespace blender::ed::sculpt_paint::greasepencil
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
#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_begin(const bContext &C, const InputSample &start_sample) override
void on_stroke_extended(const bContext &C, const InputSample &extension_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]
float closest_distance_to_surface_2d(const float2 pt, const Span< float2 > verts)
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)
std::unique_ptr< GreasePencilStrokeOperation > new_vertex_average_operation()
float brush_radius(const Paint &paint, const Brush &brush, float pressure)
T interpolate(const T &a, const T &b, const FactorT &t)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
struct ToolSettings * toolsettings