Blender V4.5
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
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 VertexAverageOperation : 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 float radius = brush_radius(scene, brush, extension_sample.pressure);
39 const float radius_squared = radius * radius;
40
41 const bool use_selection_masking = GPENCIL_ANY_VERTEX_MASK(
43
44 const bool do_points = do_vertex_color_points(brush);
45 const bool do_fill = do_vertex_color_fill(brush);
46
47 /* Compute the average color under the brush. */
48 float3 average_color(0.0f);
49 int color_count = 0;
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 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
56 const VArray<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors();
57
58 point_selection.foreach_index([&](const int64_t point_i) {
59 const ColorGeometry4f color = vertex_colors[point_i];
60 if (color.a > 0.0f && math::distance_squared(extension_sample.mouse_position,
61 view_positions[point_i]) < radius_squared)
62 {
63 average_color += float3(color.r, color.g, color.b);
64 color_count++;
65 }
66 });
67 }
68 const IndexMask fill_selection = fill_mask_for_stroke_operation(
69 params, use_selection_masking, memory);
70 if (!fill_selection.is_empty() && do_fill) {
71 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
72 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
73 const VArray<ColorGeometry4f> fill_colors = params.drawing.fill_colors();
74
75 fill_selection.foreach_index([&](const int64_t curve_i) {
76 const IndexRange points = points_by_curve[curve_i];
77 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
78 const ColorGeometry4f color = fill_colors[curve_i];
79 if (color.a > 0.0f && closest_distance_to_surface_2d(extension_sample.mouse_position,
80 curve_view_positions) < radius)
81 {
82 average_color += float3(color.r, color.g, color.b);
83 color_count++;
84 }
85 });
86 }
87 return true;
88 });
89
90 if (color_count <= 0) {
91 return;
92 }
93 average_color = average_color / color_count;
94 /* The average color is the color that will be mixed in. */
95 const ColorGeometry4f mix_color(average_color.x, average_color.y, average_color.z, 1.0f);
96
98 IndexMaskMemory memory;
99 const IndexMask point_selection = point_mask_for_stroke_operation(
100 params, use_selection_masking, memory);
101 if (!point_selection.is_empty() && do_points) {
102 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
103 MutableSpan<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors_for_write();
104
105 point_selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
106 const float influence = brush_point_influence(
107 scene, brush, view_positions[point_i], extension_sample, params.multi_frame_falloff);
108
109 ColorGeometry4f &color = vertex_colors[point_i];
110 color = math::interpolate(color, mix_color, influence);
111 });
112 }
113
114 const IndexMask fill_selection = fill_mask_for_stroke_operation(
115 params, use_selection_masking, memory);
116 if (!fill_selection.is_empty() && do_fill) {
117 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
118 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
119 MutableSpan<ColorGeometry4f> fill_colors = params.drawing.fill_colors_for_write();
120
121 fill_selection.foreach_index(GrainSize(1024), [&](const int64_t curve_i) {
122 const IndexRange points = points_by_curve[curve_i];
123 const Span<float2> curve_view_positions = view_positions.as_span().slice(points);
124 const float influence = brush_fill_influence(
125 scene, brush, curve_view_positions, extension_sample, params.multi_frame_falloff);
126
127 ColorGeometry4f &color = fill_colors[curve_i];
128 color = math::interpolate(color, mix_color, influence);
129 });
130 }
131 return true;
132 });
133}
134
136
137std::unique_ptr<GreasePencilStrokeOperation> new_vertex_average_operation()
138{
139 return std::make_unique<VertexAverageOperation>();
140}
141
142} // 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:467
Brush * BKE_paint_brush(Paint *paint)
Definition paint.cc:636
#define GPENCIL_ANY_VERTEX_MASK(flag)
eGP_vertex_SelectMaskFlag
#define C
Definition RandGen.cpp:29
long long int int64_t
Span< T > as_span() const
Definition BLI_array.hh:232
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
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)
Array< float2 > calculate_view_positions(const GreasePencilStrokeParams &params, const IndexMask &selection)
float brush_point_influence(const Scene &scene, const Brush &brush, const float2 &co, const InputSample &sample, float multi_frame_falloff)
float brush_fill_influence(const Scene &scene, 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 Scene &scene, 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
Definition BLI_color.hh:342
VecBase< float, 3 > float3
struct ToolSettings * toolsettings
char gpencil_selectmode_vertex