Blender V4.3
grease_pencil_sculpt_strength.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 "BLI_task.hh"
6
7#include "BKE_context.hh"
9#include "BKE_paint.hh"
10
11#include "ED_grease_pencil.hh"
12#include "ED_view3d.hh"
13
14#include "WM_api.hh"
15#include "WM_types.hh"
16
18#include "paint_intern.hh"
19
21
23 public:
25
26 void on_stroke_begin(const bContext &C, const InputSample &start_sample) override;
27 void on_stroke_extended(const bContext &C, const InputSample &extension_sample) override;
28 void on_stroke_done(const bContext & /*C*/) override {}
29};
30
31void StrengthOperation::on_stroke_begin(const bContext &C, const InputSample &start_sample)
32{
33 this->init_stroke(C, start_sample);
34}
35
36void StrengthOperation::on_stroke_extended(const bContext &C, const InputSample &extension_sample)
37{
38 const Scene &scene = *CTX_data_scene(&C);
40 const Brush &brush = *BKE_paint_brush(&paint);
41 const bool invert = this->is_inverted(brush);
42
43 const bool is_masking = GPENCIL_ANY_SCULPT_MASK(
44 eGP_Sculpt_SelectMaskFlag(scene.toolsettings->gpencil_selectmode_sculpt));
45
47 IndexMaskMemory selection_memory;
48 const IndexMask selection = point_selection_mask(params, is_masking, selection_memory);
49 if (selection.is_empty()) {
50 return false;
51 }
52
53 Array<float2> view_positions = calculate_view_positions(params, selection);
54 MutableSpan<float> opacities = params.drawing.opacities_for_write();
55
56 selection.foreach_index(GrainSize(4096), [&](const int64_t point_i) {
57 float &opacity = opacities[point_i];
58 const float influence = brush_point_influence(
59 scene, brush, view_positions[point_i], extension_sample, params.multi_frame_falloff);
60 /* Brush influence mapped to opacity by a factor of 0.125. */
61 const float delta_opacity = (invert ? -influence : influence) * 0.125f;
62 opacity = std::clamp(opacity + delta_opacity, 0.0f, 1.0f);
63 });
64
65 return true;
66 });
67 this->stroke_extended(extension_sample);
68}
69
70std::unique_ptr<GreasePencilStrokeOperation> new_strength_operation(
71 const BrushStrokeMode stroke_mode)
72{
73 return std::make_unique<StrengthOperation>(stroke_mode);
74}
75
76} // namespace blender::ed::sculpt_paint::greasepencil
Scene * CTX_data_scene(const bContext *C)
Low-level operations for grease pencil.
Paint * BKE_paint_get_active_from_context(const bContext *C)
Definition paint.cc:477
Brush * BKE_paint_brush(Paint *paint)
Definition paint.cc:649
#define GPENCIL_ANY_SCULPT_MASK(flag)
eGP_Sculpt_SelectMaskFlag
void foreach_editable_drawing(const bContext &C, FunctionRef< bool(const GreasePencilStrokeParams &params)> 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
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition invert.h:9
std::unique_ptr< GreasePencilStrokeOperation > new_strength_operation(BrushStrokeMode stroke_mode)
IndexMask point_selection_mask(const GreasePencilStrokeParams &params, const bool use_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)
BrushStrokeMode
__int64 int64_t
Definition stdint.h:89