Blender V5.3
smooth_mask.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
8
10
11#include "DNA_brush_types.h"
12
13#include "BKE_attribute.hh"
14#include "BKE_mesh.hh"
15#include "BKE_object_types.hh"
16#include "BKE_subdiv_ccg.hh"
17
19
26
27#include "bmesh.hh"
28
30
31inline namespace smooth_mask_cc {
32
42
43/* TODO: Extract this and the similarly named smooth.cc function
44 * to a common location. */
45static Vector<float> iteration_strengths(const float strength)
46{
47 constexpr int max_iterations = 4;
48
49 BLI_assert_msg(strength >= 0.0f,
50 "The smooth brush expects a non-negative strength to behave properly");
51 const float clamped_strength = std::min(strength, 1.0f);
52
53 const int count = int(clamped_strength * max_iterations);
54 const float last = max_iterations * (clamped_strength - float(count) / max_iterations);
56 result.append_n_times(1.0f, count);
57 result.append(last);
58 return result;
59}
60
61static void apply_masks_faces(const Depsgraph &depsgraph,
62 const Brush &brush,
63 const Span<float3> positions_eval,
64 const Span<float3> vert_normals,
65 const Span<bool> hide_vert,
67 const float strength,
68 Object &object,
69 LocalData &tls,
70 const Span<float> mask_averages,
72{
73 SculptSession &ss = *object.runtime->sculpt_session;
74 const StrokeCache &cache = *ss.cache;
75
76 const Span<int> verts = node.verts();
77
78 tls.factors.resize(verts.size());
79 const MutableSpan<float> factors = tls.factors;
80 fill_factor_from_hide(hide_vert, verts, factors);
81 filter_region_clip_factors(ss, positions_eval, verts, factors);
82 if (brush.flag & BRUSH_FRONTFACE) {
83 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
84 }
85
86 tls.distances.resize(verts.size());
87 const MutableSpan<float> distances = tls.distances;
89 ss, positions_eval, verts, eBrushFalloffShape(brush.falloff_shape), distances);
90 filter_distances_with_radius(cache.radius, distances, factors);
91 apply_hardness_to_distances(cache, distances);
92 calc_brush_strength_factors(cache, brush, distances, factors);
93
95 depsgraph, object, ss.cache->automasking.get(), node, verts, factors);
96
97 scale_factors(factors, strength);
98
99 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions_eval, verts, factors);
100
101 tls.new_masks.resize(verts.size());
102 const MutableSpan<float> new_masks = tls.new_masks;
103 gather_data_mesh(mask.as_span(), verts, new_masks);
104
105 mask::mix_new_masks(mask_averages, factors, new_masks);
106 mask::clamp_mask(new_masks);
107
108 scatter_data_mesh(new_masks.as_span(), verts, mask);
109}
110
111static void do_smooth_brush_mesh(const Depsgraph &depsgraph,
112 const Brush &brush,
113 Object &object,
114 const IndexMask &node_mask,
115 const float brush_strength)
116{
119 Mesh &mesh = *id_cast<Mesh *>(object.data);
120 const OffsetIndices faces = mesh.faces();
121 const Span<int> corner_verts = mesh.corner_verts();
122 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
123 const bke::AttributeAccessor attributes = mesh.attributes();
124 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
125
126 const Span<float3> positions_eval = bke::pbvh::vert_positions_eval(depsgraph, object);
127 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
128
129 Array<int> node_vert_offset_data;
130 OffsetIndices node_vert_offsets = create_node_vert_offsets(
131 nodes, node_mask, node_vert_offset_data);
132 Array<float> new_masks(node_vert_offsets.total_size());
133
134 bke::MutableAttributeAccessor write_attributes = mesh.attributes_for_write();
135
137 ".sculpt_mask");
138 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
139
141 for (const float strength : iteration_strengths(brush_strength)) {
142 /* Calculate new masks into a separate array to avoid non-threadsafe access of values from
143 * neighboring nodes. */
144 node_mask.foreach_index(
145 [&](const int i, const int pos) {
146 LocalData &tls = all_tls.local();
147 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
148 corner_verts,
149 vert_to_face_map,
150 hide_poly,
151 nodes[i].verts(),
152 tls.neighbor_offsets,
153 tls.neighbor_data);
155 mask.span.as_span(),
156 neighbors,
157 new_masks.as_mutable_span().slice(node_vert_offsets[pos]));
158 },
160
161 node_mask.foreach_index(
162 [&](const int i, const int pos) {
163 LocalData &tls = all_tls.local();
164 apply_masks_faces(depsgraph,
165 brush,
166 positions_eval,
167 vert_normals,
168 hide_vert,
169 nodes[i],
170 strength,
171 object,
172 tls,
173 new_masks.as_span().slice(node_vert_offsets[pos]),
174 mask.span);
175 },
177 }
179 pbvh.tag_masks_changed(node_mask);
180 mask.finish();
181}
182
183static void calc_grids(const Depsgraph &depsgraph,
184 Object &object,
185 const Brush &brush,
186 const float strength,
188 LocalData &tls)
189{
190 SculptSession &ss = *object.runtime->sculpt_session;
191 const StrokeCache &cache = *ss.cache;
192 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
193
194 const Span<int> grids = node.grids();
195 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
196
197 tls.factors.resize(positions.size());
198 const MutableSpan<float> factors = tls.factors;
199 fill_factor_from_hide(subdiv_ccg, grids, factors);
200 filter_region_clip_factors(ss, positions, factors);
201 if (brush.flag & BRUSH_FRONTFACE) {
202 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
203 }
204
205 tls.distances.resize(positions.size());
206 const MutableSpan<float> distances = tls.distances;
207 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
208 filter_distances_with_radius(cache.radius, distances, factors);
209 apply_hardness_to_distances(cache, distances);
210 calc_brush_strength_factors(cache, brush, distances, factors);
211
212 if (ss.cache->automasking) {
214 depsgraph, object, cache.automasking.get(), node, grids, factors);
215 }
216
217 scale_factors(factors, strength);
218
219 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
220
221 tls.masks.resize(positions.size());
222 const MutableSpan<float> masks = tls.masks;
223 mask::gather_mask_grids(subdiv_ccg, grids, masks);
224
225 tls.new_masks.resize(positions.size());
226 const MutableSpan<float> new_masks = tls.new_masks;
227 smooth::average_data_grids(subdiv_ccg, subdiv_ccg.masks.as_span(), grids, new_masks);
228
229 mask::mix_new_masks(new_masks, factors, masks);
230 mask::clamp_mask(masks);
231
232 mask::scatter_mask_grids(masks, subdiv_ccg, grids);
233}
234
235static void calc_bmesh(const Depsgraph &depsgraph,
236 Object &object,
237 const int mask_offset,
238 const Brush &brush,
239 const float strength,
241 LocalData &tls)
242{
243 SculptSession &ss = *object.runtime->sculpt_session;
244 const StrokeCache &cache = *ss.cache;
245
247
248 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
249
250 tls.factors.resize(verts.size());
251 const MutableSpan<float> factors = tls.factors;
252 fill_factor_from_hide(verts, factors);
253 filter_region_clip_factors(ss, positions, factors);
254 if (brush.flag & BRUSH_FRONTFACE) {
255 calc_front_face(cache.view_normal_symm, verts, factors);
256 }
257
258 tls.distances.resize(verts.size());
259 const MutableSpan<float> distances = tls.distances;
260 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
261 filter_distances_with_radius(cache.radius, distances, factors);
262 apply_hardness_to_distances(cache, distances);
263 calc_brush_strength_factors(cache, brush, distances, factors);
264
265 if (ss.cache->automasking) {
266 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
267 }
268
269 scale_factors(factors, strength);
270
271 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
272
273 tls.masks.resize(verts.size());
274 const MutableSpan<float> masks = tls.masks;
275 mask::gather_mask_bmesh(*ss.bm, verts, masks);
276
277 tls.new_masks.resize(verts.size());
278 const MutableSpan<float> new_masks = tls.new_masks;
279 mask::average_neighbor_mask_bmesh(mask_offset, verts, new_masks);
280
281 mask::mix_new_masks(new_masks, factors, masks);
282 mask::clamp_mask(masks);
283
284 mask::scatter_mask_bmesh(masks, *ss.bm, verts);
285}
286
287} // namespace smooth_mask_cc
288
289void do_smooth_mask_brush(const Depsgraph &depsgraph,
290 const Sculpt &sd,
291 Object &object,
292 const IndexMask &node_mask,
293 float brush_strength)
294{
296 SculptSession &ss = *object.runtime->sculpt_session;
300 switch (pbvh.type()) {
302 do_smooth_brush_mesh(depsgraph, brush, object, node_mask, brush_strength);
303 break;
304 }
307 for (const float strength : iteration_strengths(brush_strength)) {
309 node_mask.foreach_index(
310 [&](const int i) {
311 LocalData &tls = all_tls.local();
312 calc_grids(depsgraph, object, brush, strength, nodes[i], tls);
313 },
315 }
317 pbvh.tag_masks_changed(node_mask);
318 break;
319 }
323 const int mask_offset = CustomData_get_offset_named(
324 &ss.bm->vdata, CD_PROP_FLOAT, ".sculpt_mask");
325 for (const float strength : iteration_strengths(brush_strength)) {
327 node_mask.foreach_index(
328 [&](const int i) {
329 LocalData &tls = all_tls.local();
330 calc_bmesh(depsgraph, object, mask_offset, brush, strength, nodes[i], tls);
331 },
333 }
334 bke::pbvh::update_mask_bmesh(*ss.bm, node_mask, pbvh);
335 pbvh.tag_masks_changed(node_mask);
336 break;
337 }
338 }
339}
340
341} // namespace blender::ed::sculpt_paint::brushes
#define PRF_scope(category)
#define BLI_assert_msg(a, msg)
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
int64_t size() const
Definition BLI_set.hh:606
constexpr int64_t size() const
Definition BLI_span.hh:256
GAttributeReader lookup(const StringRef name) const
GSpanAttributeWriter lookup_for_write_span(StringRef name)
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
pbvh::Tree * pbvh_get(Object &object)
void update_mask_bmesh(const BMesh &bm, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1421
void update_mask_mesh(const Mesh &mesh, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1356
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
void update_mask_grids(const SubdivCCG &subdiv_ccg, const IndexMask &node_mask, Tree &pbvh)
Definition pbvh.cc:1387
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
void calc_vert_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::MeshNode &node, Span< int > verts, MutableSpan< float > factors)
void calc_grids_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::GridsNode &node, Span< int > grids, MutableSpan< float > factors)
void ensure_boundary_info(Object &object)
Definition sculpt.cc:588
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, Object &object, bke::pbvh::BMeshNode &node, LocalData &tls, MutableSpan< float > layer_displacement_factor)
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, Object &object, const bool use_persistent_base, const Span< float3 > persistent_base_positions, const Span< float3 > persistent_base_normals, bke::pbvh::GridsNode &node, LocalData &tls, MutableSpan< float > layer_displacement_factor)
static std::array< float, 4 > iteration_strengths(const float strength, const int stroke_iteration)
Definition relax.cc:95
static BLI_NOINLINE void do_smooth_brush_mesh(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, Object &object, const IndexMask &node_mask, const float brush_strength)
Definition smooth.cc:86
static void apply_masks_faces(const Depsgraph &depsgraph, const Brush &brush, const Span< float3 > positions_eval, const Span< float3 > vert_normals, const Span< bool > hide_vert, const bke::pbvh::MeshNode &node, const float strength, Object &object, LocalData &tls, const Span< float > mask_averages, MutableSpan< float > mask)
void do_smooth_mask_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask, float brush_strength)
void scatter_mask_grids(const Span< float > mask, SubdivCCG &subdiv_ccg, const Span< int > grids)
void scatter_mask_bmesh(const Span< float > mask, const BMesh &bm, const Set< BMVert *, 0 > &verts)
void gather_mask_bmesh(const BMesh &bm, const Set< BMVert *, 0 > &verts, const MutableSpan< float > r_mask)
void mix_new_masks(const Span< float > new_masks, const float factor, const MutableSpan< float > masks)
Definition paint_mask.cc:96
void gather_mask_grids(const SubdivCCG &subdiv_ccg, const Span< int > grids, const MutableSpan< float > r_mask)
void clamp_mask(const MutableSpan< float > masks)
void average_neighbor_mask_bmesh(const int mask_offset, const Set< BMVert *, 0 > &verts, const MutableSpan< float > new_masks)
void neighbor_data_average_mesh(const Span< T > src, const GroupedSpan< int > vert_neighbors, const MutableSpan< T > dst)
void average_data_grids(const SubdivCCG &subdiv_ccg, const Span< T > src, const Span< int > grids, const MutableSpan< T > dst)
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void gather_bmesh_positions(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6497
void calc_brush_strength_factors(const StrokeCache &cache, const Brush &brush, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7501
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
void apply_hardness_to_distances(float radius, float hardness, MutableSpan< float > distances)
Definition sculpt.cc:7473
GroupedSpan< int > calc_vert_neighbors(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face, Span< bool > hide_poly, Span< int > verts, Vector< int > &r_offset_data, Vector< int > &r_data)
Definition sculpt.cc:7949
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7422
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
void scatter_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
void calc_brush_distances(const SculptSession &ss, Span< float3 > vert_positions, Span< int > vert, eBrushFalloffShape falloff_shape, MutableSpan< float > r_distances)
Definition sculpt.cc:7370
OffsetIndices< int > create_node_vert_offsets(Span< bke::pbvh::MeshNode > nodes, const IndexMask &node_mask, Array< int > &node_data)
Definition sculpt.cc:7912
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
void calc_front_face(const float3 &view_normal, Span< float3 > normals, MutableSpan< float > factors)
Definition sculpt.cc:7223
void calc_brush_texture_factors(PaintMode paint_mode, const SculptSession &ss, const Brush &brush, Span< float3 > vert_positions, Span< int > vert, MutableSpan< float > factors)
Definition sculpt.cc:7513
void fill_factor_from_hide(Span< bool > hide_vert, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7077
constexpr ParallelGrainSize grain_size(int grain_size)
BMesh const char void * data
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CustomData vdata
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
Array< float > masks
std::unique_ptr< auto_mask::Cache > automasking
void resize(int new_size)
Definition vector.hh:94