Blender V5.3
editors/sculpt_paint/mesh/brushes/layer.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#include "DNA_mesh_types.h"
13#include "DNA_object_types.h"
14#include "DNA_scene_types.h"
15
16#include "BKE_attribute.hh"
17#include "BKE_mesh.hh"
18#include "BKE_object_types.hh"
19#include "BKE_paint.hh"
20#include "BKE_paint_bvh.hh"
21#include "BKE_subdiv_ccg.hh"
22
23#include "BLI_array.hh"
25#include "BLI_task.hh"
26
31
32#include "bmesh.hh"
33
35
36inline namespace layer_cc {
37
48
49BLI_NOINLINE static void offset_displacement_factors(const MutableSpan<float> displacement_factors,
50 const Span<float> factors,
51 const float strength)
52{
54 for (const int i : displacement_factors.index_range()) {
55 displacement_factors[i] += factors[i] * strength * (1.05f - std::abs(displacement_factors[i]));
56 }
57}
58
67BLI_NOINLINE static void reset_displacement_factors(const MutableSpan<float> displacement_factors,
68 const Span<float> factors,
69 const float strength)
70{
72 for (const int i : displacement_factors.index_range()) {
73 displacement_factors[i] += std::abs(factors[i] * strength * displacement_factors[i]) *
74 (displacement_factors[i] > 0.0f ? -1.0f : 1.0f);
75 }
76}
77
78BLI_NOINLINE static void clamp_displacement_factors(const MutableSpan<float> displacement_factors,
79 const Span<float> masks)
80{
82 if (masks.is_empty()) {
83 for (const int i : displacement_factors.index_range()) {
84 displacement_factors[i] = std::clamp(displacement_factors[i], -1.0f, 1.0f);
85 }
86 }
87 else {
88 for (const int i : displacement_factors.index_range()) {
89 const float clamp_mask = 1.0f - masks[i];
90 displacement_factors[i] = std::clamp(displacement_factors[i], -clamp_mask, clamp_mask);
91 }
92 }
93}
94
95BLI_NOINLINE static void calc_translations(const Span<float3> orig_positions,
96 const Span<float3> orig_normals,
97 const Span<float3> positions,
98 const Span<float> displacement_factors,
99 const Span<float> factors,
100 const float height,
101 const MutableSpan<float3> r_translations)
102{
104 for (const int i : positions.index_range()) {
105 const float3 offset = orig_normals[i] * height * displacement_factors[i];
106 const float3 translation = orig_positions[i] + offset - positions[i];
107 r_translations[i] = translation * factors[i];
108 }
109}
110
111BLI_NOINLINE static void calc_translations(const Span<float3> base_positions,
112 const Span<float3> base_normals,
113 const Span<int> verts,
114 const Span<float3> positions,
115 const Span<float> displacement_factors,
116 const Span<float> factors,
117 const float height,
118 const MutableSpan<float3> r_translations)
119{
121 for (const int i : positions.index_range()) {
122 const float3 offset = base_normals[verts[i]] * height * displacement_factors[i];
123 const float3 translation = base_positions[verts[i]] + offset - positions[i];
124 r_translations[i] = translation * factors[i];
125 }
126}
127
128static void calc_faces(const Depsgraph &depsgraph,
129 const Sculpt &sd,
130 const Brush &brush,
131 const MeshAttributeData &attribute_data,
132 const Span<float3> vert_normals,
133 const bool use_persistent_base,
134 const Span<float3> persistent_base_positions,
135 const Span<float3> persistent_base_normals,
136 Object &object,
138 MutableSpan<float> layer_displacement_factor,
139 const PositionDeformData &position_data)
140{
141 const SculptSession &ss = *object.runtime->sculpt_session;
142 const StrokeCache &cache = *ss.cache;
143
144 const Span<int> verts = node.verts();
145 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
147 gather_data_mesh<float3>(position_data.eval, verts, positions);
148
150 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
151 filter_region_clip_factors(ss, positions, factors);
152 if (brush.flag & BRUSH_FRONTFACE) {
153 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
154 }
155
158 ss, orig_data.positions, eBrushFalloffShape(brush.falloff_shape), distances);
159 filter_distances_with_radius(cache.radius, distances, factors);
160 apply_hardness_to_distances(cache, distances);
161 calc_brush_strength_factors(cache, brush, distances, factors);
162
163 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
164
165 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
166
167 Array<float, bke::pbvh::MESH_LEAF_LIMIT> masks(attribute_data.mask.is_empty() ? 0 :
168 verts.size());
169 if (!attribute_data.mask.is_empty()) {
170 gather_data_mesh<float>(attribute_data.mask, verts, masks);
171 }
172
173 Array<float, bke::pbvh::MESH_LEAF_LIMIT> displacement_factors(verts.size());
174 gather_data_mesh<float>(layer_displacement_factor.as_span(), verts, displacement_factors);
175
176 if (use_persistent_base) {
177 if (cache.toggle_settings.invert) {
178 reset_displacement_factors(displacement_factors, factors, cache.bstrength);
179 }
180 else {
181 offset_displacement_factors(displacement_factors, factors, cache.bstrength);
182 }
183 clamp_displacement_factors(displacement_factors, masks);
184
185 scatter_data_mesh(displacement_factors.as_span(), verts, layer_displacement_factor);
186
188 calc_translations(persistent_base_positions,
189 persistent_base_normals,
190 verts,
191 positions,
192 displacement_factors,
193 factors,
194 brush.height,
195 translations);
196
197 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
198 position_data.deform(translations, verts);
199 }
200 else {
201 offset_displacement_factors(displacement_factors, factors, cache.bstrength);
202 clamp_displacement_factors(displacement_factors, masks);
203
204 scatter_data_mesh(displacement_factors.as_span(), verts, layer_displacement_factor);
205
208 orig_data.normals,
209 positions,
210 displacement_factors,
211 factors,
212 brush.height,
213 translations);
214
215 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
216 position_data.deform(translations, verts);
217 }
218}
219
220static void calc_grids(const Depsgraph &depsgraph,
221 const Sculpt &sd,
222 const Brush &brush,
223 Object &object,
224 const bool use_persistent_base,
225 const Span<float3> persistent_base_positions,
226 const Span<float3> persistent_base_normals,
228 LocalData &tls,
229 MutableSpan<float> layer_displacement_factor)
230{
231 SculptSession &ss = *object.runtime->sculpt_session;
232 const StrokeCache &cache = *ss.cache;
233 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
234
235 const Span<int> grids = node.grids();
236 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
237 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
238
239 tls.factors.resize(positions.size());
240 const MutableSpan<float> factors = tls.factors;
241 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
242 filter_region_clip_factors(ss, positions, factors);
243 if (brush.flag & BRUSH_FRONTFACE) {
244 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
245 }
246
247 tls.distances.resize(positions.size());
248 const MutableSpan<float> distances = tls.distances;
250 ss, orig_data.positions, eBrushFalloffShape(brush.falloff_shape), distances);
251 filter_distances_with_radius(cache.radius, distances, factors);
252 apply_hardness_to_distances(cache, distances);
253 calc_brush_strength_factors(cache, brush, distances, factors);
254
255 auto_mask::calc_grids_factors(depsgraph, object, cache.automasking.get(), node, grids, factors);
256
257 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
258
259 if (subdiv_ccg.masks.is_empty()) {
260 tls.masks.clear();
261 }
262 else {
263 tls.masks.resize(positions.size());
264 gather_data_grids(subdiv_ccg, subdiv_ccg.masks.as_span(), grids, tls.masks.as_mutable_span());
265 }
266 const MutableSpan<float> masks = tls.masks;
267
268 const MutableSpan<float> displacement_factors = gather_data_grids(
269 subdiv_ccg, layer_displacement_factor.as_span(), grids, tls.displacement_factors);
270
271 if (use_persistent_base) {
272 if (cache.toggle_settings.invert) {
273 reset_displacement_factors(displacement_factors, tls.factors, cache.bstrength);
274 }
275 else {
276 offset_displacement_factors(displacement_factors, tls.factors, cache.bstrength);
277 }
278 clamp_displacement_factors(displacement_factors, masks);
279
281 subdiv_ccg, displacement_factors.as_span(), grids, layer_displacement_factor);
282
283 tls.translations.resize(positions.size());
284 const MutableSpan<float3> translations = tls.translations;
286 gather_data_grids(subdiv_ccg, persistent_base_positions, grids, tls.persistent_positions),
287 gather_data_grids(subdiv_ccg, persistent_base_normals, grids, tls.persistent_normals),
288 positions,
289 displacement_factors,
290 tls.factors,
291 brush.height,
292 translations);
293
294 clip_and_lock_translations(sd, ss, positions, translations);
295 apply_translations(translations, grids, subdiv_ccg);
296 }
297 else {
298 offset_displacement_factors(displacement_factors, tls.factors, cache.bstrength);
299 clamp_displacement_factors(displacement_factors, masks);
300
302 subdiv_ccg, displacement_factors.as_span(), grids, layer_displacement_factor);
303
304 tls.translations.resize(positions.size());
305 const MutableSpan<float3> translations = tls.translations;
307 orig_data.normals,
308 positions,
309 displacement_factors,
310 tls.factors,
311 brush.height,
312 translations);
313
314 clip_and_lock_translations(sd, ss, positions, translations);
315 apply_translations(translations, grids, subdiv_ccg);
316 }
317}
318
319static void calc_bmesh(const Depsgraph &depsgraph,
320 const Sculpt &sd,
321 const Brush &brush,
322 Object &object,
324 LocalData &tls,
325 MutableSpan<float> layer_displacement_factor)
326{
327 SculptSession &ss = *object.runtime->sculpt_session;
328 const StrokeCache &cache = *ss.cache;
329
331
332 Array<float3> orig_positions(verts.size());
333 Array<float3> orig_normals(verts.size());
334 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
335
336 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
337
338 tls.factors.resize(verts.size());
339 const MutableSpan<float> factors = tls.factors;
340 fill_factor_from_hide_and_mask(*ss.bm, verts, factors);
341 filter_region_clip_factors(ss, positions, factors);
342 if (brush.flag & BRUSH_FRONTFACE) {
343 calc_front_face(cache.view_normal_symm, verts, factors);
344 }
345
346 tls.distances.resize(verts.size());
347 const MutableSpan<float> distances = tls.distances;
348 calc_brush_distances(ss, orig_positions, eBrushFalloffShape(brush.falloff_shape), distances);
349 filter_distances_with_radius(cache.radius, distances, factors);
350 apply_hardness_to_distances(cache, distances);
351 calc_brush_strength_factors(cache, brush, distances, factors);
352
353 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
354
355 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
356
357 const MutableSpan<float> displacement_factors = gather_data_bmesh(
358 layer_displacement_factor.as_span(), verts, tls.displacement_factors);
359
360 offset_displacement_factors(displacement_factors, tls.factors, cache.bstrength);
361
362 tls.masks.resize(verts.size());
363 const MutableSpan<float> masks = tls.masks;
364 mask::gather_mask_bmesh(*ss.bm, verts, masks);
365 clamp_displacement_factors(displacement_factors, masks);
366
367 scatter_data_bmesh(displacement_factors.as_span(), verts, layer_displacement_factor);
368
369 tls.translations.resize(verts.size());
370 const MutableSpan<float3> translations = tls.translations;
371 calc_translations(orig_positions,
372 orig_normals,
373 positions,
374 displacement_factors,
375 tls.factors,
376 brush.height,
377 translations);
378
379 clip_and_lock_translations(sd, ss, positions, translations);
380 apply_translations(translations, verts);
381}
382
383} // namespace layer_cc
384
385void do_layer_brush(const Depsgraph &depsgraph,
386 const Sculpt &sd,
387 Object &object,
388 const IndexMask &node_mask)
389{
391 SculptSession &ss = *object.runtime->sculpt_session;
394
396 switch (pbvh.type()) {
398 Mesh &mesh = *id_cast<Mesh *>(object.data);
399 const PositionDeformData position_data(depsgraph, object);
400 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
401
402 const MeshAttributeData attribute_data(mesh);
403 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
404 const VArraySpan persistent_position = *attributes.lookup<float3>(".sculpt_persistent_co",
406 const VArraySpan persistent_normal = *attributes.lookup<float3>(".sculpt_persistent_no",
408
409 bke::SpanAttributeWriter<float> persistent_disp_attr;
410 bool use_persistent_base = false;
411 MutableSpan<float> displacement;
412 if (brush.flag & BRUSH_PERSISTENT) {
413 if (!persistent_position.is_empty() && !persistent_normal.is_empty()) {
414 persistent_disp_attr = attributes.lookup_or_add_for_write_span<float>(
415 ".sculpt_persistent_disp", bke::AttrDomain::Point);
416 if (persistent_disp_attr) {
417 use_persistent_base = true;
418 displacement = persistent_disp_attr.span;
419 }
420 }
421 }
422
423 if (displacement.is_empty()) {
426 }
427 displacement = ss.cache->layer_displacement_factor;
428 }
429
431 node_mask.foreach_index(
432 [&](const int i) {
433 calc_faces(depsgraph,
434 sd,
435 brush,
436 attribute_data,
437 vert_normals,
438 use_persistent_base,
439 persistent_position,
440 persistent_normal,
441 object,
442 nodes[i],
443 displacement,
444 position_data);
445 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
446 },
448 persistent_disp_attr.finish();
449 break;
450 }
452 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
453 MutableSpan<float3> positions = subdiv_ccg.positions;
454
455 const std::optional<PersistentMultiresData> persistent_multires_data =
457
458 Span<float3> persistent_position;
459 Span<float3> persistent_normal;
460
461 bool use_persistent_base = false;
462 MutableSpan<float> displacement;
463 if (brush.flag & BRUSH_PERSISTENT) {
464 if (persistent_multires_data) {
465 use_persistent_base = true;
466 persistent_position = persistent_multires_data->positions;
467 persistent_normal = persistent_multires_data->normals;
468 displacement = persistent_multires_data->displacements;
469 }
470 }
471
472 if (displacement.is_empty()) {
474 ss.cache->layer_displacement_factor = Array<float>(positions.size(), 0.0f);
475 }
476 displacement = ss.cache->layer_displacement_factor;
477 }
478
480 node_mask.foreach_index(
481 [&](const int i) {
482 LocalData &tls = all_tls.local();
483 calc_grids(depsgraph,
484 sd,
485 brush,
486 object,
487 use_persistent_base,
488 persistent_position,
489 persistent_normal,
490 nodes[i],
491 tls,
492 displacement);
493 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
494 },
496 break;
497 }
501 }
502 const MutableSpan<float> displacement = ss.cache->layer_displacement_factor;
504 node_mask.foreach_index(
505 [&](const int i) {
506 LocalData &tls = all_tls.local();
507 calc_bmesh(depsgraph, sd, brush, object, nodes[i], tls, displacement);
509 },
511 break;
512 }
513 }
514 pbvh.tag_positions_changed(node_mask);
515 pbvh.flush_bounds_to_parents();
516}
517
518} // namespace blender::ed::sculpt_paint::brushes
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define PRF_scope(category)
#define BLI_NOINLINE
Span< T > as_span() const
Definition BLI_array.hh:254
bool is_empty() const
Definition BLI_array.hh:275
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
int64_t size() const
Definition BLI_set.hh:606
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
GAttributeReader lookup(const StringRef name) const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
constexpr bool is_empty() const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
pbvh::Tree * pbvh_get(Object &object)
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1216
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1196
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1205
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)
static BLI_NOINLINE void reset_displacement_factors(const MutableSpan< float > displacement_factors, const Span< float > factors, const float strength)
static BLI_NOINLINE void clamp_displacement_factors(const MutableSpan< float > displacement_factors, const Span< float > masks)
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 BLI_NOINLINE void offset_displacement_factors(const MutableSpan< float > displacement_factors, const Span< float > factors, const float strength)
static BLI_NOINLINE void calc_translations(const Span< float3 > orig_positions, const Span< float3 > orig_normals, const Span< float3 > positions, const Span< float > displacement_factors, const Span< float > factors, const float height, const MutableSpan< float3 > r_translations)
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const MeshAttributeData &attribute_data, const Span< float3 > vert_normals, const bool use_persistent_base, const Span< float3 > persistent_base_positions, const Span< float3 > persistent_base_normals, Object &object, bke::pbvh::MeshNode &node, MutableSpan< float > layer_displacement_factor, const PositionDeformData &position_data)
void do_layer_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
void gather_mask_bmesh(const BMesh &bm, const Set< BMVert *, 0 > &verts, const MutableSpan< float > r_mask)
void fill_factor_from_hide_and_mask(Span< bool > hide_vert, Span< float > mask, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7128
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void scatter_data_bmesh(Span< T > node_data, const Set< BMVert *, 0 > &verts, MutableSpan< T > dst)
Definition sculpt.cc:6577
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 gather_data_grids(const SubdivCCG &subdiv_ccg, Span< T > src, Span< int > grids, MutableSpan< T > node_data)
Definition sculpt.cc:6528
void apply_hardness_to_distances(float radius, float hardness, MutableSpan< float > distances)
Definition sculpt.cc:7473
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7422
int vertex_count_get(const Object &object)
Definition sculpt.cc:163
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
void gather_data_bmesh(Span< T > src, const Set< BMVert *, 0 > &verts, MutableSpan< T > node_data)
Definition sculpt.cc:6545
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
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
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
OrigPositionData orig_position_data_get_mesh(const Object &object, const bke::pbvh::MeshNode &node)
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
OrigPositionData orig_position_data_get_grids(const Object &object, const bke::pbvh::GridsNode &node)
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 scatter_data_grids(const SubdivCCG &subdiv_ccg, Span< T > node_data, Span< int > grids, MutableSpan< T > dst)
Definition sculpt.cc:6560
constexpr ParallelGrainSize grain_size(int grain_size)
BMesh const char void * data
const Brush * BKE_paint_brush_for_read(const Paint *paint)
VecBase< float, 3 > float3
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
std::optional< PersistentMultiresData > persistent_multires_data()
Array< float3 > positions
Array< float > masks
std::unique_ptr< auto_mask::Cache > automasking
void resize(int new_size)
Definition vector.hh:94