Blender V4.5
grease_pencil_vertex_blur.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 VertexBlurOperation : 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
45 IndexMaskMemory memory;
46 const IndexMask stroke_selection = curve_mask_for_stroke_operation(
47 params, use_selection_masking, memory);
48 if (stroke_selection.is_empty()) {
49 return false;
50 }
51 const IndexMask point_selection = point_mask_for_stroke_operation(
52 params, use_selection_masking, memory);
53 const Array<float2> view_positions = calculate_view_positions(params, point_selection);
54 const OffsetIndices<int> points_by_curve = params.drawing.strokes().points_by_curve();
55 MutableSpan<ColorGeometry4f> vertex_colors = params.drawing.vertex_colors_for_write();
56 stroke_selection.foreach_index(GrainSize(1024), [&](const int64_t curve) {
57 const IndexRange points = points_by_curve[curve];
58
59 float3 average_color(0.0f);
60 int color_count = 0;
61 for (const int point : points) {
62 const ColorGeometry4f color = vertex_colors[point];
63 const float distance = math::distance_squared(extension_sample.mouse_position,
64 view_positions[point]);
65 if (color.a > 0.0f && distance < radius_squared) {
66 average_color += float3(color.r, color.g, color.b);
67 color_count++;
68 }
69 }
70
71 if (color_count == 0) {
72 return;
73 }
74 average_color = average_color / color_count;
75 const ColorGeometry4f mix_color(average_color.x, average_color.y, average_color.z, 1.0f);
76
77 for (const int point : points) {
78 const float influence = brush_point_influence(
79 scene, brush, view_positions[point], extension_sample, params.multi_frame_falloff);
80 ColorGeometry4f &color = vertex_colors[point];
81 if (color.a > 0.0f && influence > 0.0f) {
82 color = math::interpolate(color, mix_color, influence);
83 }
84 }
85 });
86 return true;
87 });
88}
89
91
92std::unique_ptr<GreasePencilStrokeOperation> new_vertex_blur_operation()
93{
94 return std::make_unique<VertexBlurOperation>();
95}
96
97} // 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
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
float distance(VecOp< float, D >, VecOp< float, D >) RET
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
IndexMask curve_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)
std::unique_ptr< GreasePencilStrokeOperation > new_vertex_blur_operation()
float brush_point_influence(const Scene &scene, const Brush &brush, const float2 &co, const InputSample &sample, float multi_frame_falloff)
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