Blender V5.3
sculpt_automasking.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array.hh"
10#include "BLI_index_range.hh"
11#include "BLI_math_base.hh"
12#include "BLI_math_base_c.hh"
13#include "BLI_math_vector.hh"
15#include "BLI_set.hh"
16#include "BLI_vector.hh"
17
18#include "DNA_brush_types.h"
19#include "DNA_mesh_types.h"
20
21#include "BKE_colortools.hh"
22#include "BKE_object_types.hh"
23#include "BKE_paint.hh"
24#include "BKE_paint_bvh.hh"
25#include "BKE_subdiv_ccg.hh"
26
27#include "../paint_intern.hh"
28#include "mesh_brush_common.hh"
29#include "sculpt_automask.hh"
30#include "sculpt_boundary.hh"
31#include "sculpt_dyntopo.hh"
32#include "sculpt_face_set.hh"
33#include "sculpt_filter.hh"
34#include "sculpt_flood_fill.hh"
35#include "sculpt_intern.hh"
36#include "sculpt_islands.hh"
37#include "sculpt_undo.hh"
38
39#include "bmesh.hh"
40
41#include <cmath>
42
45{
46 if (ss.cache) {
47 return ss.cache->automasking.get();
48 }
49 if (ss.filter_cache) {
50 return ss.filter_cache->automasking.get();
51 }
52 return nullptr;
53}
54
55static bool mode_enabled(const Paint &paint, const Brush *br, const eAutomasking_flag mode)
56{
57 eAutomasking_flag automasking = paint.mesh_automasking_settings->flags;
58
59 if (br) {
60 automasking |= br->mesh_automasking_settings->flags;
61 }
62
63 return automasking & mode;
64}
65
66bool is_enabled(const Paint &paint, const Object &object, const Brush *br)
67{
68 if (object.runtime->sculpt_session && br && dyntopo::stroke_is_dyntopo(object, *br)) {
69 return false;
70 }
72 return true;
73 }
75 return true;
76 }
78 return true;
79 }
81 return true;
82 }
84 return true;
85 }
87 return true;
88 }
90 return true;
91 }
92
93 return false;
94}
95
97{
98 if (brush) {
99 eAutomasking_flag flags = paint.mesh_automasking_settings->flags |
100 brush->mesh_automasking_settings->flags;
101
102 /* Check if we are using brush cavity settings. */
103 if (brush->mesh_automasking_settings->flags & BRUSH_AUTOMASKING_CAVITY_ALL) {
106 flags |= brush->mesh_automasking_settings->flags;
107 }
108 else if (paint.mesh_automasking_settings->flags & BRUSH_AUTOMASKING_CAVITY_ALL) {
111 flags |= paint.mesh_automasking_settings->flags;
112 }
113
114 return flags;
115 }
116 return paint.mesh_automasking_settings->flags;
117}
118
119static float normal_calc(const float3 &compare_normal,
120 const float3 &normal,
121 float limit_lower,
122 float limit_upper)
123{
124 float angle = math::safe_acos(math::dot(compare_normal, normal));
125
126 /* note that limit is pre-divided by M_PI */
127
128 if (angle > limit_lower && angle < limit_upper) {
129 float t = 1.0f - (angle - limit_lower) / (limit_upper - limit_lower);
130
131 /* smoothstep */
132 t = t * t * (3.0 - 2.0 * t);
133
134 return t;
135 }
136 if (angle > limit_upper) {
137 return 0.0f;
138 }
139
140 return 1.0f;
141}
142
143static bool is_constrained_by_radius(const Brush *br)
144{
145 if (br == nullptr) {
146 return false;
147 }
148
149 /* 2D falloff is not constrained by radius. */
151 return false;
152 }
153
154 if (ELEM(br->sculpt_brush_type,
158 {
159 return true;
160 }
161 return false;
162}
163
164/* Fetch the propagation_steps value, preferring the brush level value over the global sculpt tool
165 * value. */
167{
168 return brush && (flag_is_set(brush->mesh_automasking_settings->flags,
170 flag_is_set(brush->mesh_automasking_settings->flags,
172 brush->mesh_automasking_settings->boundary_edges_propagation_steps :
173 paint.mesh_automasking_settings->boundary_edges_propagation_steps;
174}
175
176/* Determine if the given automasking settings require values to be precomputed and cached. */
177static bool needs_factors_cache(const Paint &paint, const Brush *brush)
178{
179 const eAutomasking_flag automasking_flags = calc_effective_bits(paint, brush);
180
181 if (flag_is_set(automasking_flags, BRUSH_AUTOMASKING_TOPOLOGY) && brush &&
183 {
184 return true;
185 }
186
187 if (flag_is_set(automasking_flags, BRUSH_AUTOMASKING_BOUNDARY_EDGES) ||
188 flag_is_set(automasking_flags, BRUSH_AUTOMASKING_BOUNDARY_FACE_SETS))
189 {
191 }
192 return false;
193}
194
195static float calc_brush_normal_factor(const Cache &automasking,
196 const Object &object,
197 const float3 &normal)
198{
199 const SculptSession &ss = *object.runtime->sculpt_session;
200 float falloff = automasking.settings.start_normal_falloff * M_PI;
201 float3 initial_normal;
202
203 if (ss.cache) {
204 initial_normal = ss.cache->initial_normal_symm;
205 }
206 else {
207 initial_normal = ss.filter_cache->initial_normal;
208 }
209
210 return normal_calc(normal,
211 initial_normal,
212 automasking.settings.start_normal_limit - falloff * 0.5f,
213 automasking.settings.start_normal_limit + falloff * 0.5f);
214}
215
216static float calc_view_normal_factor(const Cache &automasking,
217 const Object &object,
218 const float3 &normal)
219{
220 const SculptSession &ss = *object.runtime->sculpt_session;
221 float falloff = automasking.settings.view_normal_falloff * M_PI;
222
223 float3 view_normal;
224
225 if (ss.cache) {
226 view_normal = ss.cache->view_normal_symm;
227 }
228 else {
229 view_normal = ss.filter_cache->view_normal;
230 }
231
232 return normal_calc(normal,
233 view_normal,
234 automasking.settings.view_normal_limit,
235 automasking.settings.view_normal_limit + falloff);
236}
237
238static bool calc_view_occlusion_factor(const Depsgraph &depsgraph,
239 Cache &automasking,
240 const Object &object,
241 const int vert,
242 const float3 &vert_position)
243{
244 if (automasking.occlusion[vert] == Cache::OcclusionValue::Unknown) {
245 const bool occluded = vertex_is_occluded(depsgraph, object, vert_position, true);
246 automasking.occlusion[vert] = occluded ? Cache::OcclusionValue::Occluded :
248 }
249 return automasking.occlusion[vert] == Cache::OcclusionValue::Occluded;
250}
251
252static float calc_cavity_factor(const Cache &automasking, float factor)
253{
254 float sign = signf(factor);
255
256 factor = fabsf(factor) * automasking.settings.cavity_factor * 50.0f;
257
258 factor = factor * sign * 0.5f + 0.5f;
259 CLAMP(factor, 0.0f, 1.0f);
260
261 return flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_INVERTED) ?
262 1.0f - factor :
263 factor;
264}
265
269 float distance = 0.0f;
270 int count = 0;
271};
272
273static void calc_blurred_cavity_mesh(const Depsgraph &depsgraph,
274 const Object &object,
275 const Cache &automasking,
276 const int steps,
277 const int vert,
278 MutableSpan<float> cavity_factors)
279{
280 struct CavityBlurVert {
281 int vertex;
282 int depth;
283 };
284
285 const Mesh &mesh = *id_cast<Mesh *>(object.data);
286
287 const OffsetIndices faces = mesh.faces();
288 const Span<int> corner_verts = mesh.corner_verts();
289 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
290
291 const bke::AttributeAccessor attributes = mesh.attributes();
292 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
293
294 const Span<float3> positions_eval = bke::pbvh::vert_positions_eval(depsgraph, object);
295 const Span<float3> normals_eval = bke::pbvh::vert_normals_eval(depsgraph, object);
296
297 AccumulatedVert all_verts;
298 AccumulatedVert verts_in_range;
299 /* Steps starts at 1, but API and user interface
300 * are zero-based.
301 */
302 const int num_steps = steps + 1;
303
304 std::queue<CavityBlurVert> queue;
305 Set<int, 64> visited_verts;
306
307 const CavityBlurVert initial{vert, 0};
308 visited_verts.add_new(vert);
309 queue.push(initial);
310
311 const float3 starting_position = positions_eval[vert];
312
313 Vector<int> neighbors;
314 while (!queue.empty()) {
315 const CavityBlurVert blurvert = queue.front();
316 queue.pop();
317
318 const int current_vert = blurvert.vertex;
319
320 const float3 &blur_vert_position = positions_eval[current_vert];
321 const float3 &blur_vert_normal = normals_eval[current_vert];
322
323 const float dist_to_start = math::distance(blur_vert_position, starting_position);
324
325 all_verts.position += blur_vert_position;
326 all_verts.distance += dist_to_start;
327 all_verts.count++;
328
329 if (blurvert.depth < num_steps) {
330 verts_in_range.position += blur_vert_position;
331 verts_in_range.normal += blur_vert_normal;
332 verts_in_range.count++;
333 }
334
335 /* Use the total number of steps used to get to this particular vert to determine if we should
336 * keep processing */
337 if (blurvert.depth >= num_steps) {
338 continue;
339 }
340
341 for (const int neighbor : vert_neighbors_get_mesh(
342 faces, corner_verts, vert_to_face_map, hide_poly, current_vert, neighbors))
343 {
344 if (visited_verts.contains(neighbor)) {
345 continue;
346 }
347
348 visited_verts.add_new(neighbor);
349 queue.push({neighbor, blurvert.depth + 1});
350 }
351 }
352
353 BLI_assert(all_verts.count != verts_in_range.count);
354
355 if (all_verts.count == 0) {
356 all_verts.position = positions_eval[vert];
357 }
358 else {
359 all_verts.position /= float(all_verts.count);
360 all_verts.distance /= all_verts.count;
361 }
362
363 if (verts_in_range.count == 0) {
364 verts_in_range.position = positions_eval[vert];
365 }
366 else {
367 verts_in_range.position /= float(verts_in_range.count);
368 }
369
370 verts_in_range.normal = math::normalize(verts_in_range.normal);
371 if (math::dot(verts_in_range.normal, verts_in_range.normal) == 0.0f) {
372 verts_in_range.normal = normals_eval[vert];
373 }
374
375 const float3 vec = all_verts.position - verts_in_range.position;
376 float factor_sum = math::safe_divide(math::dot(vec, verts_in_range.normal), all_verts.distance);
377 cavity_factors[vert] = calc_cavity_factor(automasking, factor_sum);
378}
379
380static void calc_blurred_cavity_grids(const Object &object,
381 const Cache &automasking,
382 const int steps,
383 const SubdivCCGCoord vert,
384 MutableSpan<float> cavity_factors)
385{
386 struct CavityBlurVert {
387 int vert;
388 int depth;
389 };
390
391 const SculptSession &ss = *object.runtime->sculpt_session;
392 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
393 const Span<float3> positions = subdiv_ccg.positions;
394 const Span<float3> normals = subdiv_ccg.normals;
395 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
396
397 AccumulatedVert all_verts;
398 AccumulatedVert verts_in_range;
399 /* Steps starts at 1, but API and user interface
400 * are zero-based.
401 */
402 const int num_steps = steps + 1;
403
404 std::queue<CavityBlurVert> queue;
405 Set<int, 64> visited_verts;
406
407 const CavityBlurVert initial{vert.to_index(key), 0};
408 visited_verts.add_new(initial.vert);
409 queue.push(initial);
410
411 const float3 starting_position = positions[vert.to_index(key)];
412
413 SubdivCCGNeighbors neighbors;
414 while (!queue.empty()) {
415 const CavityBlurVert blurvert = queue.front();
416 queue.pop();
417
418 const int current_vert = blurvert.vert;
419
420 const float3 &blur_vert_position = positions[current_vert];
421 const float3 &blur_vert_normal = normals[current_vert];
422
423 const float dist_to_start = math::distance(blur_vert_position, starting_position);
424
425 all_verts.position += blur_vert_position;
426 all_verts.distance += dist_to_start;
427 all_verts.count++;
428
429 if (blurvert.depth < num_steps) {
430 verts_in_range.position += blur_vert_position;
431 verts_in_range.normal += blur_vert_normal;
432 verts_in_range.count++;
433 }
434
435 /* Use the total number of steps used to get to this particular vert to determine if we should
436 * keep processing */
437 if (blurvert.depth >= num_steps) {
438 continue;
439 }
440
442 subdiv_ccg, SubdivCCGCoord::from_index(key, current_vert), false, neighbors);
443 for (const SubdivCCGCoord neighbor : neighbors.coords) {
444 const int neighbor_idx = neighbor.to_index(key);
445 if (visited_verts.contains(neighbor_idx)) {
446 continue;
447 }
448
449 visited_verts.add_new(neighbor_idx);
450 queue.push({neighbor_idx, blurvert.depth + 1});
451 }
452 }
453
454 BLI_assert(all_verts.count != verts_in_range.count);
455
456 if (all_verts.count == 0) {
457 all_verts.position = positions[vert.to_index(key)];
458 }
459 else {
460 all_verts.position /= float(all_verts.count);
461 all_verts.distance /= all_verts.count;
462 }
463
464 if (verts_in_range.count == 0) {
465 verts_in_range.position = positions[vert.to_index(key)];
466 }
467 else {
468 verts_in_range.position /= float(verts_in_range.count);
469 }
470
471 verts_in_range.normal = math::normalize(verts_in_range.normal);
472 if (math::dot(verts_in_range.normal, verts_in_range.normal) == 0.0f) {
473 verts_in_range.normal = normals[vert.to_index(key)];
474 }
475
476 const float3 vec = all_verts.position - verts_in_range.position;
477 float factor_sum = math::dot(vec, verts_in_range.normal) / all_verts.distance;
478 cavity_factors[vert.to_index(key)] = calc_cavity_factor(automasking, factor_sum);
479}
480
481static void calc_blurred_cavity_bmesh(const Cache &automasking,
482 const int steps,
483 BMVert *vert,
484 MutableSpan<float> cavity_factors)
485{
486 struct CavityBlurVert {
487 BMVert *vertex;
488 int index;
489 int depth;
490 };
491
492 AccumulatedVert all_verts;
493 AccumulatedVert verts_in_range;
494 /* Steps starts at 1, but API and user interface
495 * are zero-based.
496 */
497 const int num_steps = steps + 1;
498
499 std::queue<CavityBlurVert> queue;
500 Set<int, 64> visited_verts;
501
502 const CavityBlurVert initial{vert, BM_elem_index_get(vert), 0};
503 visited_verts.add_new(initial.index);
504 queue.push(initial);
505
506 const float3 starting_position = vert->co;
507
508 BMeshNeighborVerts neighbors;
509 while (!queue.empty()) {
510 const CavityBlurVert blurvert = queue.front();
511 queue.pop();
512
513 BMVert *current_vert = blurvert.vertex;
514
515 const float3 blur_vert_position = current_vert->co;
516 const float3 blur_vert_normal = current_vert->no;
517
518 const float dist_to_start = math::distance(blur_vert_position, starting_position);
519
520 all_verts.position += blur_vert_position;
521 all_verts.distance += dist_to_start;
522 all_verts.count++;
523
524 if (blurvert.depth < num_steps) {
525 verts_in_range.position += blur_vert_position;
526 verts_in_range.normal += blur_vert_normal;
527 verts_in_range.count++;
528 }
529
530 /* Use the total number of steps used to get to this particular vert to determine if we should
531 * keep processing */
532 if (blurvert.depth >= num_steps) {
533 continue;
534 }
535
536 for (BMVert *neighbor : vert_neighbors_get_bmesh(*current_vert, neighbors)) {
537 const int neighbor_idx = BM_elem_index_get(neighbor);
538 if (visited_verts.contains(neighbor_idx)) {
539 continue;
540 }
541
542 visited_verts.add_new(neighbor_idx);
543 queue.push({neighbor, neighbor_idx, blurvert.depth + 1});
544 }
545 }
546
547 BLI_assert(all_verts.count != verts_in_range.count);
548
549 if (all_verts.count == 0) {
550 all_verts.position = vert->co;
551 }
552 else {
553 all_verts.position /= float(all_verts.count);
554 all_verts.distance /= all_verts.count;
555 }
556
557 if (verts_in_range.count == 0) {
558 verts_in_range.position = vert->co;
559 }
560 else {
561 verts_in_range.position /= float(verts_in_range.count);
562 }
563
564 verts_in_range.normal = math::normalize(verts_in_range.normal);
565 if (math::dot(verts_in_range.normal, verts_in_range.normal) == 0.0f) {
566 verts_in_range.normal = vert->no;
567 }
568
569 const float3 vec = all_verts.position - verts_in_range.position;
570 float factor_sum = math::dot(vec, verts_in_range.normal) / all_verts.distance;
571 cavity_factors[BM_elem_index_get(vert)] = calc_cavity_factor(automasking, factor_sum);
572}
573
574static float process_cavity_factor(const Cache &automasking, float factor)
575{
576 const bool inverted = flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_INVERTED);
577
578 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_ALL) &&
579 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_USE_CURVE))
580 {
581 factor = inverted ? 1.0f - factor : factor;
582 factor = BKE_curvemapping_evaluateF(automasking.settings.cavity_curve, 0, factor);
583 factor = inverted ? 1.0f - factor : factor;
584 }
585
586 return factor;
587}
588
589static void calc_cavity_factor_mesh(const Depsgraph &depsgraph,
590 const Cache &automasking,
591 const Object &object,
592 const int vert)
593{
594 if (automasking.cavity_factor[vert] == -1.0f) {
595 calc_blurred_cavity_mesh(depsgraph,
596 object,
597 automasking,
598 automasking.settings.cavity_blur_steps,
599 vert,
600 const_cast<Cache &>(automasking).cavity_factor);
601 }
602}
603
604static void calc_cavity_factor_grids(const CCGKey &key,
605 const Cache &automasking,
606 const Object &object,
607 const int vert)
608{
609 if (automasking.cavity_factor[vert] == -1.0f) {
611 automasking,
612 automasking.settings.cavity_blur_steps,
614 const_cast<Cache &>(automasking).cavity_factor);
615 }
616}
617
618static void calc_cavity_factor_bmesh(const Cache &automasking, BMVert *vert, const int vert_i)
619{
620 if (automasking.cavity_factor[vert_i] == -1.0f) {
621 calc_blurred_cavity_bmesh(automasking,
622 automasking.settings.cavity_blur_steps,
623 vert,
624 const_cast<Cache &>(automasking).cavity_factor);
625 }
626}
627
628void calc_vert_factors(const Depsgraph &depsgraph,
629 const Object &object,
630 const Cache &automasking,
632 const Span<int> verts,
633 const MutableSpan<float> factors)
634{
636 const SculptSession &ss = *object.runtime->sculpt_session;
637 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
638 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, object);
639 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
640 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
641 const BitSpan boundary_verts = ss.boundary_info_cache->verts;
642 const bke::AttributeAccessor attributes = mesh.attributes();
643 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
644 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
645 Span<float3> orig_normals;
646 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BRUSH_NORMAL) ||
647 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL))
648 {
649 /* Weight & Vertex paint do not get "original" normals */
650 if (object.mode == OB_MODE_SCULPT) {
651 if (std::optional<OrigPositionData> orig_data = orig_position_data_lookup_mesh(object, node))
652 {
653 orig_normals = orig_data->normals;
654 }
655 }
656 }
657
658 for (const int i : verts.index_range()) {
659 const int vert = verts[i];
660 const float3 &normal = orig_normals.is_empty() ? vert_normals[vert] : orig_normals[i];
661
662 /* Since brush normal mode depends on the current mirror symmetry pass
663 * it is not folded into the factor cache (when it exists). */
664 if ((ss.cache || ss.filter_cache) &&
665 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BRUSH_NORMAL))
666 {
667 factors[i] *= calc_brush_normal_factor(automasking, object, normal);
668 }
669
670 /* If the cache is initialized with valid info, use the cache. This is used when the
671 * automasking information can't be computed in real time per vertex and needs to be
672 * initialized for the whole mesh when the stroke starts. */
673 if (!automasking.factor.is_empty()) {
674 float cached_factor = automasking.factor[vert];
675
676 if (automasking.settings.flags & BRUSH_AUTOMASKING_CAVITY_ALL) {
677 BLI_assert(automasking.cavity_factor[vert] != -1.0f);
678 cached_factor *= process_cavity_factor(automasking, automasking.cavity_factor[vert]);
679 }
680
681 factors[i] *= cached_factor;
682 continue;
683 }
684
685 bool do_occlusion = flag_is_set(automasking.settings.flags,
687 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL);
688 if (do_occlusion) {
689 const bool occluded = calc_view_occlusion_factor(
690 depsgraph, const_cast<Cache &>(automasking), object, vert, vert_positions[vert]);
691 if (occluded) {
692 factors[i] = 0.0f;
693 continue;
694 }
695 }
696
697 /* The filter tools do not take symmetry into consideration, use the "generic" (i.e. 0)
698 * symmetry pass instead */
699 const int current_symmetry_pass = ss.cache ? ss.cache->mirror_symmetry_pass : 0;
700 if (!automasking.settings.topology_use_brush_limit &&
701 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_TOPOLOGY) &&
702 islands::vert_id_get(ss, vert) !=
703 automasking.settings.initial_island_nr[current_symmetry_pass])
704 {
705 factors[i] = 0.0f;
706 continue;
707 }
708
709 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_FACE_SETS)) {
710 if (automasking.settings.initial_face_set != face_set_none_id &&
712 vert_to_face_map, face_sets, vert, automasking.settings.initial_face_set))
713 {
714 factors[i] = 0.0f;
715 continue;
716 }
717 }
718
719 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BOUNDARY_EDGES)) {
720 if (boundary::vert_is_boundary(vert_to_face_map, hide_poly, boundary_verts, vert)) {
721 factors[i] = 0.0f;
722 continue;
723 }
724 }
725
726 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BOUNDARY_FACE_SETS)) {
727 bool ignore = ss.cache && ss.cache->brush &&
729 (automasking.settings.initial_face_set == face_set_none_id ||
730 face_set::vert_face_set_max_get(vert_to_face_map, face_sets, vert) ==
732
733 if (!ignore && !face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, vert)) {
734 factors[i] = 0.0f;
735 continue;
736 }
737 }
738
739 if ((ss.cache || ss.filter_cache) &&
740 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL))
741 {
742 factors[i] *= calc_view_normal_factor(automasking, object, normal);
743 }
744
745 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_ALL)) {
746 BLI_assert(automasking.cavity_factor[vert] != -1.0f);
747 factors[i] *= process_cavity_factor(automasking, automasking.cavity_factor[vert]);
748 }
749 }
750}
751
752void calc_interpolated_face_factors(const Depsgraph &depsgraph,
753 const Object &object,
754 const OffsetIndices<int> faces,
755 const Span<int> corner_verts,
756 const Cache &automasking,
757 const bke::pbvh::MeshNode & /*node*/,
758 const Span<int> face_indices,
759 const MutableSpan<float> factors)
760{
762 const SculptSession &ss = *object.runtime->sculpt_session;
763 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
764 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, object);
765 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
766 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
767 const BitSpan boundary_verts = ss.boundary_info_cache->verts;
768 const bke::AttributeAccessor attributes = mesh.attributes();
769 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
770 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
771 for (const int i : face_indices.index_range()) {
772 const Span<int> face_verts = corner_verts.slice(faces[face_indices[i]]);
773 float sum = 0.0f;
774 for (const int vert : face_verts) {
775 float factor = 1.0f;
776 BLI_SCOPED_DEFER([&]() { sum += factor; });
777
778 /* Since brush normal mode depends on the current mirror symmetry pass
779 * it is not folded into the factor cache (when it exists). */
780 if ((ss.cache || ss.filter_cache) &&
781 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BRUSH_NORMAL))
782 {
783 factor *= calc_brush_normal_factor(automasking, object, vert_normals[vert]);
784 }
785
786 /* If the cache is initialized with valid info, use the cache. This is used when the
787 * automasking information can't be computed in real time per vertex and needs to be
788 * initialized for the whole mesh when the stroke starts. */
789 if (!automasking.factor.is_empty()) {
790 float cached_factor = automasking.factor[vert];
791
792 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_ALL)) {
793 BLI_assert(automasking.cavity_factor[vert] != -1.0f);
794 cached_factor *= process_cavity_factor(automasking, automasking.cavity_factor[vert]);
795 }
796
797 factor *= cached_factor;
798 continue;
799 }
800
801 bool do_occlusion = flag_is_set(automasking.settings.flags,
803 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL);
804 if (do_occlusion) {
805 const bool occluded = calc_view_occlusion_factor(
806 depsgraph, const_cast<Cache &>(automasking), object, vert, vert_positions[vert]);
807 if (occluded) {
808 factor = 0.0f;
809 continue;
810 }
811 }
812
813 /* The filter tools do not take symmetry into consideration, use the "generic" (i.e. 0)
814 * symmetry pass instead */
815 const int current_symmetry_pass = ss.cache ? ss.cache->mirror_symmetry_pass : 0;
816 if (!automasking.settings.topology_use_brush_limit &&
817 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_TOPOLOGY) &&
818 islands::vert_id_get(ss, vert) !=
819 automasking.settings.initial_island_nr[current_symmetry_pass])
820 {
821 factor = 0.0f;
822 continue;
823 }
824
825 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_FACE_SETS)) {
826 if (automasking.settings.initial_face_set != face_set_none_id &&
828 vert_to_face_map, face_sets, vert, automasking.settings.initial_face_set))
829 {
830 factor = 0.0f;
831 continue;
832 }
833 }
834
835 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BOUNDARY_EDGES)) {
836 if (boundary::vert_is_boundary(vert_to_face_map, hide_poly, boundary_verts, vert)) {
837 factor = 0.0f;
838 continue;
839 }
840 }
841
842 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BOUNDARY_FACE_SETS)) {
843 bool ignore = ss.cache && ss.cache->brush &&
845 (automasking.settings.initial_face_set == face_set_none_id ||
846 face_set::vert_face_set_max_get(vert_to_face_map, face_sets, vert) ==
848
849 if (!ignore && !face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, vert)) {
850 factor = 0.0f;
851 continue;
852 }
853 }
854
855 if ((ss.cache || ss.filter_cache) &&
856 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL))
857 {
858 factor *= calc_view_normal_factor(automasking, object, vert_normals[vert]);
859 }
860
861 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_ALL)) {
862 BLI_assert(automasking.cavity_factor[vert] != -1.0f);
863 factor *= process_cavity_factor(automasking, automasking.cavity_factor[vert]);
864 }
865 }
866 factors[i] *= sum * math::rcp(float(face_verts.size()));
867 }
868}
869
870void calc_grids_factors(const Depsgraph &depsgraph,
871 const Object &object,
872 const Cache &automasking,
874 const Span<int> grids,
875 const MutableSpan<float> factors)
876{
878 const SculptSession &ss = *object.runtime->sculpt_session;
879 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
880 const OffsetIndices<int> faces = base_mesh.faces();
881 const Span<int> corner_verts = base_mesh.corner_verts();
882 const GroupedSpan<int> vert_to_face_map = base_mesh.vert_to_face_map();
883 const BitSpan boundary_verts = ss.boundary_info_cache->verts;
884 const bke::AttributeAccessor attributes = base_mesh.attributes();
885 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
886 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
887 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
888
889 Span<float3> orig_normals;
890 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BRUSH_NORMAL) ||
891 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL))
892 {
893 if (std::optional<OrigPositionData> orig_data = orig_position_data_lookup_grids(object, node))
894 {
895 orig_normals = orig_data->normals;
896 }
897 }
898
899 for (const int i : grids.index_range()) {
900 const int grid_face_set = face_sets.is_empty() ?
901 face_set_none_id :
902 face_sets[subdiv_ccg.grid_to_face_map[grids[i]]];
903 const int node_start = i * key.grid_area;
904 const int grids_start = grids[i] * key.grid_area;
905 for (const int offset : IndexRange(key.grid_area)) {
906 const int node_vert = node_start + offset;
907 const int vert = grids_start + offset;
908 const float3 &normal = orig_normals.is_empty() ? subdiv_ccg.normals[vert] :
909 orig_normals[node_vert];
910
911 /* Since brush normal mode depends on the current mirror symmetry pass
912 * it is not folded into the factor cache (when it exists). */
913 if ((ss.cache || ss.filter_cache) &&
914 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BRUSH_NORMAL))
915 {
916 factors[node_vert] *= calc_brush_normal_factor(automasking, object, normal);
917 }
918
919 /* If the cache is initialized with valid info, use the cache. This is used when the
920 * automasking information can't be computed in real time per vertex and needs to be
921 * initialized for the whole mesh when the stroke starts. */
922 if (!automasking.factor.is_empty()) {
923 float cached_factor = automasking.factor[vert];
924
925 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_ALL)) {
926 BLI_assert(automasking.cavity_factor[vert] != -1.0f);
927 cached_factor *= process_cavity_factor(automasking, automasking.cavity_factor[vert]);
928 }
929
930 factors[node_vert] *= cached_factor;
931 continue;
932 }
933
934 bool do_occlusion = flag_is_set(automasking.settings.flags,
936 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL);
937 if (do_occlusion) {
938 const bool occluded = calc_view_occlusion_factor(
939 depsgraph, const_cast<Cache &>(automasking), object, vert, subdiv_ccg.positions[vert]);
940 if (occluded) {
941 factors[node_vert] = 0.0f;
942 continue;
943 }
944 }
945
946 /* The filter tools do not take symmetry into consideration, use the "generic" (i.e. 0)
947 * symmetry pass instead */
948 const int current_symmetry_pass = ss.cache ? ss.cache->mirror_symmetry_pass : 0;
949 if (!automasking.settings.topology_use_brush_limit &&
950 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_TOPOLOGY) &&
951 islands::vert_id_get(ss, vert) !=
952 automasking.settings.initial_island_nr[current_symmetry_pass])
953 {
954 factors[node_vert] = 0.0f;
955 continue;
956 }
957
958 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_FACE_SETS)) {
959 if (!ELEM(automasking.settings.initial_face_set, face_set_none_id, grid_face_set)) {
960 factors[node_vert] = 0.0f;
961 continue;
962 }
963 }
964
965 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BOUNDARY_EDGES)) {
967 corner_verts,
968 boundary_verts,
969 ss.boundary_info_cache->edges,
970 subdiv_ccg,
971 SubdivCCGCoord::from_index(key, vert)))
972 {
973 factors[node_vert] = 0.0f;
974 continue;
975 }
976 }
977
978 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BOUNDARY_FACE_SETS)) {
979 bool ignore = ss.cache && ss.cache->brush &&
981 (automasking.settings.initial_face_set == face_set_none_id ||
982 grid_face_set == ss.cache->paint_face_set);
983
984 if (!ignore && !face_set::vert_has_unique_face_set(faces,
985 corner_verts,
986 vert_to_face_map,
987 face_sets,
988 subdiv_ccg,
989 SubdivCCGCoord::from_index(key, vert)))
990 {
991 factors[node_vert] = 0.0f;
992 continue;
993 }
994 }
995
996 if ((ss.cache || ss.filter_cache) &&
997 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL))
998 {
999 factors[node_vert] *= calc_view_normal_factor(automasking, object, normal);
1000 }
1001
1002 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_ALL)) {
1003 BLI_assert(automasking.cavity_factor[vert] != -1.0f);
1004 factors[node_vert] *= process_cavity_factor(automasking, automasking.cavity_factor[vert]);
1005 }
1006 }
1007 }
1008}
1009
1010void calc_vert_factors(const Depsgraph &depsgraph,
1011 const Object &object,
1012 const Cache &automasking,
1013 const bke::pbvh::BMeshNode & /*node*/,
1014 const Set<BMVert *, 0> &verts,
1015 const MutableSpan<float> factors)
1016{
1018 SculptSession &ss = *object.runtime->sculpt_session;
1019 BMesh &bm = *ss.bm;
1020 const int face_set_offset = CustomData_get_offset_named(
1021 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1022
1023 Array<float3> orig_normals;
1024 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BRUSH_NORMAL) ||
1025 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL))
1026 {
1027 orig_position_data_gather_bmesh(*ss.bm_log, verts, {}, orig_normals);
1028 }
1029
1030 int i = 0;
1031 for (BMVert *vert : verts) {
1032 BLI_SCOPED_DEFER([&]() { i++; });
1033 const int vert_i = BM_elem_index_get(vert);
1034 const float3 normal = orig_normals.is_empty() ? float3(vert->no) : orig_normals[i];
1035
1036 /* Since brush normal mode depends on the current mirror symmetry pass
1037 * it is not folded into the factor cache (when it exists). */
1038 if ((ss.cache || ss.filter_cache) &&
1039 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BRUSH_NORMAL))
1040 {
1041 factors[i] *= calc_brush_normal_factor(automasking, object, normal);
1042 }
1043
1044 /* If the cache is initialized with valid info, use the cache. This is used when the
1045 * automasking information can't be computed in real time per vertex and needs to be
1046 * initialized for the whole mesh when the stroke starts. */
1047 if (!automasking.factor.is_empty()) {
1048 float cached_factor = automasking.factor[vert_i];
1049
1050 if (automasking.settings.flags & BRUSH_AUTOMASKING_CAVITY_ALL) {
1051 BLI_assert(automasking.cavity_factor[vert_i] != -1.0f);
1052 cached_factor *= process_cavity_factor(automasking, automasking.cavity_factor[vert_i]);
1053 }
1054
1055 factors[i] *= cached_factor;
1056 continue;
1057 }
1058
1059 bool do_occlusion = flag_is_set(automasking.settings.flags,
1061 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL);
1062 if (do_occlusion) {
1063 const bool occluded = calc_view_occlusion_factor(
1064 depsgraph, const_cast<Cache &>(automasking), object, vert_i, vert->co);
1065 if (occluded) {
1066 factors[i] = 0.0f;
1067 continue;
1068 }
1069 }
1070
1071 /* The filter tools do not take symmetry into consideration, use the "generic" (i.e. 0)
1072 * symmetry pass instead */
1073 const int current_symmetry_pass = ss.cache ? ss.cache->mirror_symmetry_pass : 0;
1074 if (!automasking.settings.topology_use_brush_limit &&
1075 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_TOPOLOGY) &&
1076 islands::vert_id_get(ss, vert_i) !=
1077 automasking.settings.initial_island_nr[current_symmetry_pass])
1078 {
1079 factors[i] = 0.0f;
1080 continue;
1081 }
1082
1083 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_FACE_SETS)) {
1084 if (automasking.settings.initial_face_set != face_set_none_id &&
1086 face_set_offset, *vert, automasking.settings.initial_face_set))
1087 {
1088 factors[i] = 0.0f;
1089 continue;
1090 }
1091 }
1092
1093 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BOUNDARY_EDGES)) {
1094 if (boundary::vert_is_boundary(vert)) {
1095 factors[i] = 0.0f;
1096 continue;
1097 }
1098 }
1099
1100 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_BOUNDARY_FACE_SETS)) {
1101 bool ignore = ss.cache && ss.cache->brush &&
1103 (automasking.settings.initial_face_set == face_set_none_id ||
1104 face_set::vert_face_set_max_get(face_set_offset, *vert) ==
1105 ss.cache->paint_face_set);
1106
1107 if (!ignore && !face_set::vert_has_unique_face_set(face_set_offset, *vert)) {
1108 factors[i] = 0.0f;
1109 continue;
1110 }
1111 }
1112
1113 if ((ss.cache || ss.filter_cache) &&
1114 flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_VIEW_NORMAL))
1115 {
1116 factors[i] *= calc_view_normal_factor(automasking, object, normal);
1117 }
1118
1119 if (flag_is_set(automasking.settings.flags, BRUSH_AUTOMASKING_CAVITY_ALL)) {
1120 BLI_assert(automasking.cavity_factor[vert_i] != -1.0f);
1121 factors[i] *= process_cavity_factor(automasking, automasking.cavity_factor[vert_i]);
1122 }
1123 }
1124}
1125
1126static void fill_topology_automasking_factors_mesh(const Depsgraph &depsgraph,
1127 const Paint &paint,
1128 Object &ob,
1129 const Span<float3> vert_positions,
1130 MutableSpan<float> factors)
1131{
1134 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
1135 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1136
1137 const float radius = ss.cache ? ss.cache->radius : std::numeric_limits<float>::max();
1138 const int active_vert = std::get<int>(ss.active_vert());
1139 flood_fill::FillDataMesh flood = flood_fill::FillDataMesh(vert_positions.size());
1140
1141 flood.add_initial(find_symm_verts_mesh(depsgraph, ob, active_vert, radius));
1142
1143 const bool use_radius = ss.cache && is_constrained_by_radius(brush);
1145
1146 float3 location = vert_positions[active_vert];
1147
1148 if (use_radius) {
1149 flood.execute(ob, vert_to_face_map, [&](int from_v, int to_v) {
1150 factors[from_v] = 1.0f;
1151 factors[to_v] = 1.0f;
1152 return is_vertex_inside_brush_radius_symm(vert_positions[to_v], location, radius, symm);
1153 });
1154 }
1155 else {
1156 flood.execute(ob, vert_to_face_map, [&](int from_v, int to_v) {
1157 factors[from_v] = 1.0f;
1158 factors[to_v] = 1.0f;
1159 return true;
1160 });
1161 }
1162}
1163
1165 Object &ob,
1166 const SubdivCCG &subdiv_ccg,
1167 MutableSpan<float> factors)
1168
1169{
1172
1173 const float radius = ss.cache ? ss.cache->radius : std::numeric_limits<float>::max();
1174 const int active_vert = ss.active_vert_index();
1175
1176 const Span<float3> positions = subdiv_ccg.positions;
1177 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1178
1180
1181 flood.add_initial(key, find_symm_verts_grids(ob, active_vert, radius));
1182
1183 const bool use_radius = ss.cache && is_constrained_by_radius(brush);
1185
1186 float3 location = positions[active_vert];
1187
1188 if (use_radius) {
1189 flood.execute(
1190 ob, subdiv_ccg, [&](SubdivCCGCoord from_v, SubdivCCGCoord to_v, bool /*is_duplicate*/) {
1191 factors[from_v.to_index(key)] = 1.0f;
1192 factors[to_v.to_index(key)] = 1.0f;
1194 positions[to_v.to_index(key)], location, radius, symm);
1195 });
1196 }
1197 else {
1198 flood.execute(
1199 ob, subdiv_ccg, [&](SubdivCCGCoord from_v, SubdivCCGCoord to_v, bool /*is_duplicate*/) {
1200 factors[from_v.to_index(key)] = 1.0f;
1201 factors[to_v.to_index(key)] = 1.0f;
1202 return true;
1203 });
1204 }
1205}
1206
1208 Object &ob,
1209 BMesh &bm,
1210 MutableSpan<float> factors)
1211{
1214
1215 const float radius = ss.cache ? ss.cache->radius : std::numeric_limits<float>::max();
1216 BMVert *active_vert = std::get<BMVert *>(ss.active_vert());
1217 const int num_verts = BM_mesh_elem_count(&bm, BM_VERT);
1219
1220 flood.add_initial(*ss.bm, find_symm_verts_bmesh(ob, BM_elem_index_get(active_vert), radius));
1221
1222 const bool use_radius = ss.cache && is_constrained_by_radius(brush);
1224
1225 float3 location = active_vert->co;
1226
1227 if (use_radius) {
1228 flood.execute(ob, [&](BMVert *from_v, BMVert *to_v) {
1229 factors[BM_elem_index_get(from_v)] = 1.0f;
1230 factors[BM_elem_index_get(to_v)] = 1.0f;
1231 return is_vertex_inside_brush_radius_symm(to_v->co, location, radius, symm);
1232 });
1233 }
1234 else {
1235 flood.execute(ob, [&](BMVert *from_v, BMVert *to_v) {
1236 factors[BM_elem_index_get(from_v)] = 1.0f;
1237 factors[BM_elem_index_get(to_v)] = 1.0f;
1238 return true;
1239 });
1240 }
1241}
1242
1243static void fill_topology_automasking_factors(const Depsgraph &depsgraph,
1244 const Paint &paint,
1245 Object &ob,
1246 MutableSpan<float> factors)
1247{
1249 /* TODO: This method is to be removed when more of the automasking code handles the different
1250 * pbvh types. */
1252 if (std::holds_alternative<std::monostate>(ss.active_vert())) {
1253 /* If we don't have an active vertex (i.e. the cursor is not over the mesh), we cannot
1254 * accurately calculate the topology automasking factor as it may be ambiguous which island the
1255 * user is intending to affect. */
1256 return;
1257 }
1258
1259 switch (bke::object::pbvh_get(ob)->type()) {
1262 depsgraph, paint, ob, bke::pbvh::vert_positions_eval(depsgraph, ob), factors);
1263 break;
1266 break;
1269 break;
1270 }
1271}
1272
1274{
1277
1278 if (!is_enabled(paint, ob, brush)) {
1279 return;
1280 }
1281
1282 const int active_face_set = face_set::active_face_set_get(ob);
1283 switch (bke::object::pbvh_get(ob)->type()) {
1284 case bke::pbvh::Type::Mesh: {
1285 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
1286 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1287 const bke::AttributeAccessor attributes = mesh.attributes();
1288 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set",
1290 if (face_sets.is_empty()) {
1291 return;
1292 }
1293 threading::parallel_for(IndexRange(mesh.verts_num), 1024, [&](const IndexRange range) {
1294 for (const int vert : range) {
1295 if (!face_set::vert_has_face_set(vert_to_face_map, face_sets, vert, active_face_set)) {
1296 factors[vert] = 0.0f;
1297 }
1298 }
1299 });
1300 break;
1301 }
1303 const Mesh &base_mesh = *id_cast<const Mesh *>(ob.data);
1304 const OffsetIndices<int> faces = base_mesh.faces();
1305 const bke::AttributeAccessor attributes = base_mesh.attributes();
1306 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set",
1308 if (face_sets.is_empty()) {
1309 return;
1310 }
1311 const SculptSession &ss = *ob.runtime->sculpt_session;
1312 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1313 const int grid_area = subdiv_ccg.grid_area;
1314 threading::parallel_for(faces.index_range(), 128, [&](const IndexRange range) {
1315 for (const int face : range) {
1316 if (face_sets[face] != active_face_set) {
1317 factors.slice(bke::ccg::face_range(faces, grid_area, face)).fill(0.0f);
1318 }
1319 }
1320 });
1321
1322 break;
1323 }
1325 const SculptSession &ss = *ob.runtime->sculpt_session;
1326 const BMesh &bm = *ss.bm;
1327 const int face_set_offset = CustomData_get_offset_named(
1328 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1329 if (face_set_offset == -1) {
1330 return;
1331 }
1332 threading::parallel_for(IndexRange(bm.totvert), 1024, [&](const IndexRange range) {
1333 for (const int i : range) {
1334 const BMVert *vert = BM_vert_at_index(&const_cast<BMesh &>(bm), i);
1335 if (!face_set::vert_has_face_set(face_set_offset, *vert, active_face_set)) {
1336 factors[i] = 0.0f;
1337 }
1338 }
1339 });
1340 break;
1341 }
1342 }
1343}
1344
1345static constexpr int EDGE_DISTANCE_INF = -1;
1346
1350};
1351
1353 const Depsgraph &depsgraph,
1354 const BoundaryAutomaskMode mode,
1355 const int propagation_steps,
1356 MutableSpan<float> factors)
1357{
1358 SculptSession &ss = *object.runtime->sculpt_session;
1359 Mesh &mesh = *id_cast<Mesh *>(object.data);
1360
1361 const OffsetIndices faces = mesh.faces();
1362 const Span<int> corner_verts = mesh.corner_verts();
1363 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1364 const bke::AttributeAccessor attributes = mesh.attributes();
1365 const VArraySpan hide_poly = *attributes.lookup<bool>(".hide_poly", bke::AttrDomain::Face);
1366 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
1367
1368 const int num_verts = bke::pbvh::vert_positions_eval(depsgraph, object).size();
1369 Array<int> edge_distance(num_verts, EDGE_DISTANCE_INF);
1370
1371 for (const int i : IndexRange(num_verts)) {
1372 switch (mode) {
1375 vert_to_face_map, hide_poly, ss.boundary_info_cache->verts, i))
1376 {
1377 edge_distance[i] = 0;
1378 }
1379 break;
1381 if (!face_set::vert_has_unique_face_set(vert_to_face_map, face_sets, i)) {
1382 edge_distance[i] = 0;
1383 }
1384 break;
1385 }
1386 }
1387
1388 Vector<int> neighbors;
1389 for (const int propagation_it : IndexRange(propagation_steps)) {
1390 for (const int i : IndexRange(num_verts)) {
1391 if (edge_distance[i] != EDGE_DISTANCE_INF) {
1392 continue;
1393 }
1394
1395 for (const int neighbor :
1396 vert_neighbors_get_mesh(faces, corner_verts, vert_to_face_map, hide_poly, i, neighbors))
1397 {
1398 if (edge_distance[neighbor] == propagation_it) {
1399 edge_distance[i] = propagation_it + 1;
1400 }
1401 }
1402 }
1403 }
1404
1405 for (const int i : IndexRange(num_verts)) {
1406 if (edge_distance[i] == EDGE_DISTANCE_INF) {
1407 continue;
1408 }
1409
1410 const float p = 1.0f - (float(edge_distance[i]) / float(propagation_steps));
1411 const float edge_boundary_automask = pow2f(p);
1412
1413 factors[i] *= (1.0f - edge_boundary_automask);
1414 }
1415}
1416
1418 const BoundaryAutomaskMode mode,
1419 const int propagation_steps,
1420 MutableSpan<float> factors)
1421{
1422 SculptSession &ss = *object.runtime->sculpt_session;
1423 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1424 Mesh &mesh = *id_cast<Mesh *>(object.data);
1425
1426 const Span<float3> positions = subdiv_ccg.positions;
1427 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1428
1429 const OffsetIndices faces = mesh.faces();
1430 const Span<int> corner_verts = mesh.corner_verts();
1431 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
1432 const bke::AttributeAccessor attributes = mesh.attributes();
1433 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set", bke::AttrDomain::Face);
1434
1435 Array<int> edge_distance(positions.size(), EDGE_DISTANCE_INF);
1436 for (const int i : positions.index_range()) {
1437 const SubdivCCGCoord coord = SubdivCCGCoord::from_index(key, i);
1438 switch (mode) {
1441 corner_verts,
1442 ss.boundary_info_cache->verts,
1443 ss.boundary_info_cache->edges,
1444 subdiv_ccg,
1445 coord))
1446 {
1447 edge_distance[i] = 0;
1448 }
1449 break;
1452 faces, corner_verts, vert_to_face_map, face_sets, subdiv_ccg, coord))
1453 {
1454 edge_distance[i] = 0;
1455 }
1456 break;
1457 }
1458 }
1459
1460 SubdivCCGNeighbors neighbors;
1461 for (const int propagation_it : IndexRange(propagation_steps)) {
1462 for (const int i : positions.index_range()) {
1463 if (edge_distance[i] != EDGE_DISTANCE_INF) {
1464 continue;
1465 }
1466
1467 const SubdivCCGCoord coord = SubdivCCGCoord::from_index(key, i);
1468
1469 BKE_subdiv_ccg_neighbor_coords_get(subdiv_ccg, coord, false, neighbors);
1470 for (const SubdivCCGCoord neighbor : neighbors.coords) {
1471 const int neighbor_idx = neighbor.to_index(key);
1472 if (edge_distance[neighbor_idx] == propagation_it) {
1473 edge_distance[i] = propagation_it + 1;
1474 }
1475 }
1476 }
1477 }
1478
1479 for (const int i : positions.index_range()) {
1480 if (edge_distance[i] == EDGE_DISTANCE_INF) {
1481 continue;
1482 }
1483
1484 const float p = 1.0f - (float(edge_distance[i]) / float(propagation_steps));
1485 const float edge_boundary_automask = pow2f(p);
1486
1487 factors[i] *= (1.0f - edge_boundary_automask);
1488 }
1489}
1490
1492 const BoundaryAutomaskMode mode,
1493 const int propagation_steps,
1494 MutableSpan<float> factors)
1495{
1496 SculptSession &ss = *object.runtime->sculpt_session;
1497 BMesh &bm = *ss.bm;
1498 const int face_set_offset = CustomData_get_offset_named(
1499 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
1500 const int num_verts = BM_mesh_elem_count(&bm, BM_VERT);
1501
1502 Array<int> edge_distance(num_verts, EDGE_DISTANCE_INF);
1503
1504 for (const int i : IndexRange(num_verts)) {
1505 BMVert *vert = BM_vert_at_index(&bm, i);
1506 switch (mode) {
1508 if (boundary::vert_is_boundary(vert)) {
1509 edge_distance[i] = 0;
1510 }
1511 break;
1513 if (!face_set::vert_has_unique_face_set(face_set_offset, *vert)) {
1514 edge_distance[i] = 0;
1515 }
1516 }
1517 }
1518
1519 BMeshNeighborVerts neighbors;
1520 for (const int propagation_it : IndexRange(propagation_steps)) {
1521 for (const int i : IndexRange(num_verts)) {
1522 if (edge_distance[i] != EDGE_DISTANCE_INF) {
1523 continue;
1524 }
1525
1526 BMVert *vert = BM_vert_at_index(&bm, i);
1527 for (BMVert *neighbor : vert_neighbors_get_bmesh(*vert, neighbors)) {
1528 const int neighbor_idx = BM_elem_index_get(neighbor);
1529
1530 if (edge_distance[neighbor_idx] == propagation_it) {
1531 edge_distance[i] = propagation_it + 1;
1532 }
1533 }
1534 }
1535 }
1536
1537 for (const int i : IndexRange(num_verts)) {
1538 if (edge_distance[i] == EDGE_DISTANCE_INF) {
1539 continue;
1540 }
1541
1542 const float p = 1.0f - (float(edge_distance[i]) / float(propagation_steps));
1543 const float edge_boundary_automask = pow2f(p);
1544
1545 factors[i] *= (1.0f - edge_boundary_automask);
1546 }
1547}
1548
1549static void init_boundary_masking(Object &object,
1550 const Depsgraph &depsgraph,
1551 const BoundaryAutomaskMode mode,
1552 const int propagation_steps,
1553 MutableSpan<float> factors)
1554{
1556 switch (bke::object::pbvh_get(object)->type()) {
1558 init_boundary_masking_mesh(object, depsgraph, mode, propagation_steps, factors);
1559 break;
1561 init_boundary_masking_grids(object, mode, propagation_steps, factors);
1562 break;
1564 init_boundary_masking_bmesh(object, mode, propagation_steps, factors);
1565 break;
1566 }
1567}
1568
1569/* Updates the cached values, preferring brush settings over tool-level settings. */
1570static void cache_settings_update(Cache &automasking,
1571 Object &object,
1572 const Paint &paint,
1573 const Brush *brush)
1574{
1577
1578 if (brush && flag_is_set(brush->mesh_automasking_settings->flags, BRUSH_AUTOMASKING_VIEW_NORMAL))
1579 {
1580 automasking.settings.view_normal_limit = brush->mesh_automasking_settings->view_normal_limit;
1581 automasking.settings.view_normal_falloff =
1582 brush->mesh_automasking_settings->view_normal_falloff;
1583 }
1584 else {
1585 automasking.settings.view_normal_limit = paint.mesh_automasking_settings->view_normal_limit;
1586 automasking.settings.view_normal_falloff =
1587 paint.mesh_automasking_settings->view_normal_falloff;
1588 }
1589
1590 if (brush &&
1591 flag_is_set(brush->mesh_automasking_settings->flags, BRUSH_AUTOMASKING_BRUSH_NORMAL))
1592 {
1593 automasking.settings.start_normal_limit = brush->mesh_automasking_settings->start_normal_limit;
1594 automasking.settings.start_normal_falloff =
1595 brush->mesh_automasking_settings->start_normal_falloff;
1596 }
1597 else {
1598 automasking.settings.start_normal_limit = paint.mesh_automasking_settings->start_normal_limit;
1599 automasking.settings.start_normal_falloff =
1600 paint.mesh_automasking_settings->start_normal_falloff;
1601 }
1602
1603 if (brush && flag_is_set(brush->mesh_automasking_settings->flags, BRUSH_AUTOMASKING_CAVITY_ALL))
1604 {
1605 automasking.settings.cavity_curve = brush->mesh_automasking_settings->cavity_curve;
1606 automasking.settings.cavity_factor = brush->mesh_automasking_settings->cavity_factor;
1607 automasking.settings.cavity_blur_steps = brush->mesh_automasking_settings->cavity_blur_steps;
1608 }
1609 else {
1610 automasking.settings.cavity_curve = paint.mesh_automasking_settings->cavity_curve;
1611 automasking.settings.cavity_factor = paint.mesh_automasking_settings->cavity_factor;
1612 automasking.settings.cavity_blur_steps = paint.mesh_automasking_settings->cavity_blur_steps;
1613 }
1614}
1615
1616static void normal_occlusion_automasking_fill(const Depsgraph &depsgraph,
1617 Cache &automasking,
1618 Object &ob,
1619 eAutomasking_flag mode,
1620 MutableSpan<float> factors)
1621{
1623 const int totvert = vertex_count_get(ob);
1624 /* No need to build original data since this is only called at the beginning of strokes. */
1625 switch (bke::object::pbvh_get(ob)->type()) {
1626 case bke::pbvh::Type::Mesh: {
1627 const Span<float3> vert_positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
1628 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1629 threading::parallel_for(IndexRange(totvert), 1024, [&](const IndexRange range) {
1630 for (const int vert : range) {
1631 float f = factors[vert];
1632
1633 if (flag_is_set(mode, BRUSH_AUTOMASKING_VIEW_NORMAL)) {
1634 if (flag_is_set(mode, BRUSH_AUTOMASKING_VIEW_OCCLUSION)) {
1636 depsgraph, automasking, ob, vert, vert_positions[vert]);
1637 }
1638
1639 f *= calc_view_normal_factor(automasking, ob, vert_normals[vert]);
1640 }
1641
1642 factors[vert] = f;
1643 }
1644 });
1645 break;
1646 }
1648 const SculptSession &ss = *ob.runtime->sculpt_session;
1649 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1650 threading::parallel_for(IndexRange(totvert), 1024, [&](const IndexRange range) {
1651 for (const int vert : range) {
1652 float f = factors[vert];
1653
1654 if (flag_is_set(mode, BRUSH_AUTOMASKING_VIEW_NORMAL)) {
1655 if (flag_is_set(mode, BRUSH_AUTOMASKING_VIEW_OCCLUSION)) {
1657 depsgraph, automasking, ob, vert, subdiv_ccg.positions[vert]);
1658 }
1659
1660 f *= calc_view_normal_factor(automasking, ob, subdiv_ccg.normals[vert]);
1661 }
1662
1663 factors[vert] = f;
1664 }
1665 });
1666 break;
1667 }
1669 const SculptSession &ss = *ob.runtime->sculpt_session;
1670 BMesh &bm = *ss.bm;
1671 threading::parallel_for(IndexRange(bm.totvert), 1024, [&](const IndexRange range) {
1672 for (const int i : range) {
1673 const BMVert *vert = BM_vert_at_index(&bm, i);
1674 float f = factors[i];
1675
1676 if (flag_is_set(mode, BRUSH_AUTOMASKING_VIEW_NORMAL)) {
1677 if (flag_is_set(mode, BRUSH_AUTOMASKING_VIEW_OCCLUSION)) {
1678 f *= calc_view_occlusion_factor(depsgraph, automasking, ob, i, vert->co);
1679 }
1680
1681 f *= calc_view_normal_factor(automasking, ob, vert->no);
1682 }
1683
1684 factors[i] = f;
1685 }
1686 });
1687 break;
1688 }
1689 }
1690}
1691
1692std::unique_ptr<Cache> cache_init(const Depsgraph &depsgraph,
1693 const Paint &paint,
1694 const Brush *brush,
1695 Object &ob)
1696{
1698
1699 if (!is_enabled(paint, ob, brush)) {
1700 return nullptr;
1701 }
1702
1703 std::unique_ptr<Cache> automasking = std::make_unique<Cache>();
1704 cache_settings_update(*automasking, ob, paint, brush);
1706
1708
1710 if (flag_is_set(mode, BRUSH_AUTOMASKING_TOPOLOGY) && ss.active_vert_index() != -1) {
1712
1713 std::array<int, PAINT_SYMM_AREAS> symm_verts = find_all_symm_verts(
1714 depsgraph, ob, ss.active_vert_index());
1716
1717 for (int symm_it = 0; symm_it < PAINT_SYMM_AREAS; symm_it++) {
1718 if (!is_symmetry_iteration_valid(symm_it, symm)) {
1719 continue;
1720 }
1721 BLI_assert(symm_verts[symm_it] != -1);
1722 automasking->settings.initial_island_nr[symm_it] = islands::vert_id_get(ss,
1723 symm_verts[symm_it]);
1724 }
1725 }
1726
1727 const int verts_num = vertex_count_get(ob);
1728
1729 if (flag_is_set(mode, BRUSH_AUTOMASKING_VIEW_OCCLUSION) &&
1730 flag_is_set(mode, BRUSH_AUTOMASKING_VIEW_NORMAL))
1731 {
1732 automasking->occlusion = Array<Cache::OcclusionValue>(verts_num,
1734 }
1735
1736 if (flag_is_set(mode, BRUSH_AUTOMASKING_CAVITY_ALL)) {
1738 if (brush) {
1739 BKE_curvemapping_init(brush->mesh_automasking_settings->cavity_curve);
1740 }
1741
1742 BKE_curvemapping_init(paint.mesh_automasking_settings->cavity_curve);
1743 }
1744 automasking->cavity_factor = Array<float>(verts_num, -1.0f);
1745 }
1746
1747 /* Avoid precomputing data on the vertex level if the current auto-masking modes do not require
1748 * it to function. */
1750 return automasking;
1751 }
1752
1753 /* Topology builds up the mask from zero which other modes can subtract from.
1754 * If it isn't enabled, initialize to 1. */
1755 const float initial_value = !flag_is_set(mode, BRUSH_AUTOMASKING_TOPOLOGY) ? 1.0f : 0.0f;
1756 automasking->factor = Array<float>(verts_num, initial_value);
1757 MutableSpan<float> factors = automasking->factor;
1758
1759 /* Additive modes. */
1762
1763 automasking->settings.topology_use_brush_limit = is_constrained_by_radius(brush);
1764 fill_topology_automasking_factors(depsgraph, paint, ob, factors);
1765 }
1766
1769 init_face_sets_masking(paint, ob, factors);
1770 }
1771
1772 const int steps = boundary_propagation_steps(paint, brush);
1775 init_boundary_masking(ob, depsgraph, BoundaryAutomaskMode::Edges, steps, factors);
1776 }
1779 init_boundary_masking(ob, depsgraph, BoundaryAutomaskMode::FaceSets, steps, factors);
1780 }
1781
1782 /* Subtractive modes. */
1784
1785 if (flag_is_set(normal_bits, BRUSH_AUTOMASKING_VIEW_NORMAL) ||
1786 flag_is_set(normal_bits, BRUSH_AUTOMASKING_VIEW_OCCLUSION))
1787 {
1789 depsgraph, *automasking, ob, eAutomasking_flag(normal_bits), factors);
1790 }
1791
1792 return automasking;
1793}
1794
1795Cache &filter_cache_ensure(const Depsgraph &depsgraph, const Paint &paint, Object &ob)
1796{
1797 BLI_assert(is_enabled(paint, ob, nullptr));
1800 }
1801
1803 depsgraph, paint, nullptr, ob);
1805}
1806
1807Cache &stroke_cache_ensure(const Depsgraph &depsgraph,
1808 const Paint &paint,
1809 const Brush *brush,
1810 Object &ob)
1811{
1815 }
1816
1817 ob.runtime->sculpt_session->cache->automasking = cache_init(depsgraph, paint, brush, ob);
1819}
1820
1821void Cache::calc_cavity_factor(const Depsgraph &depsgraph,
1822 const Object &object,
1823 const IndexMask &node_mask)
1824{
1825 if (!flag_is_set(this->settings.flags, BRUSH_AUTOMASKING_CAVITY_ALL)) {
1826 return;
1827 }
1828
1829 BLI_assert(!this->cavity_factor.is_empty());
1830
1831 const SculptSession &ss = *object.runtime->sculpt_session;
1832 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1833 switch (pbvh.type()) {
1834 case bke::pbvh::Type::Mesh: {
1836 node_mask.foreach_index(
1837 [&](const int i) {
1838 const Span<int> verts = nodes[i].verts();
1839 for (const int vert : verts) {
1840 calc_cavity_factor_mesh(depsgraph, *this, object, vert);
1841 }
1842 },
1844 break;
1845 }
1847 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1848 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1850 node_mask.foreach_index(
1851 [&](const int i) {
1852 const Span<int> grids = nodes[i].grids();
1853 for (const int grid : grids) {
1854 for (const int vert : bke::ccg::grid_range(subdiv_ccg.grid_area, grid)) {
1855 calc_cavity_factor_grids(key, *this, object, vert);
1856 }
1857 }
1858 },
1860 break;
1861 }
1864 node_mask.foreach_index(
1865 [&](const int i) {
1866 const Set<BMVert *, 0> verts = nodes[i].bm_unique_verts_;
1867 for (BMVert *vert : verts) {
1868 calc_cavity_factor_bmesh(*this, vert, BM_elem_index_get(vert));
1869 }
1870 },
1872 }
1873 }
1874}
1875
1876} // namespace blender::ed::sculpt_paint::auto_mask
#define PAINT_SYMM_AREAS
Definition BKE_paint.hh:109
A BVH for high poly meshes.
#define M_PI
#define BLI_SCOPED_DEFER(function_to_defer)
#define PRF_scope(category)
#define CLAMP(a, b, c)
#define ELEM(...)
#define BM_elem_index_get(ele)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
bool is_empty() const
Definition BLI_array.hh:275
constexpr IndexRange()=default
bool contains(const Key &key) const
Definition BLI_set.hh:310
void add_new(const Key &key)
Definition BLI_set.hh:233
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
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
constexpr bool is_empty() const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define vertex
constexpr T sign(T) RET
ccl_device_inline float3x3 inverted(const float3x3 m)
IndexRange grid_range(const int grid_area, const int grid)
pbvh::Tree * pbvh_get(Object &object)
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
std::unique_ptr< Cache > cache_init(const Depsgraph &depsgraph, const Paint &paint, const Brush *brush, Object &ob)
static void init_face_sets_masking(const Paint &paint, Object &ob, MutableSpan< float > factors)
static void fill_topology_automasking_factors_mesh(const Depsgraph &depsgraph, const Paint &paint, Object &ob, const Span< float3 > vert_positions, MutableSpan< float > factors)
static void calc_blurred_cavity_mesh(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const int steps, const int vert, MutableSpan< float > cavity_factors)
static float calc_brush_normal_factor(const Cache &automasking, const Object &object, const float3 &normal)
static float normal_calc(const float3 &compare_normal, const float3 &normal, float limit_lower, float limit_upper)
static void calc_cavity_factor_bmesh(const Cache &automasking, BMVert *vert, const int vert_i)
static bool calc_view_occlusion_factor(const Depsgraph &depsgraph, Cache &automasking, const Object &object, const int vert, const float3 &vert_position)
static void calc_cavity_factor_mesh(const Depsgraph &depsgraph, const Cache &automasking, const Object &object, const int vert)
static void fill_topology_automasking_factors(const Depsgraph &depsgraph, const Paint &paint, Object &ob, MutableSpan< float > factors)
static void init_boundary_masking(Object &object, const Depsgraph &depsgraph, const BoundaryAutomaskMode mode, const int propagation_steps, MutableSpan< float > factors)
const Cache * active_cache_get(const SculptSession &ss)
static void fill_topology_automasking_factors_grids(const Paint &paint, Object &ob, const SubdivCCG &subdiv_ccg, MutableSpan< float > factors)
static float calc_view_normal_factor(const Cache &automasking, const Object &object, const float3 &normal)
static void cache_settings_update(Cache &automasking, Object &object, const Paint &paint, const Brush *brush)
static eAutomasking_flag calc_effective_bits(const Paint &paint, const Brush *brush)
static void init_boundary_masking_bmesh(Object &object, const BoundaryAutomaskMode mode, const int propagation_steps, MutableSpan< float > factors)
Cache & stroke_cache_ensure(const Depsgraph &depsgraph, const Paint &paint, const Brush *brush, Object &ob)
static void normal_occlusion_automasking_fill(const Depsgraph &depsgraph, Cache &automasking, Object &ob, eAutomasking_flag mode, MutableSpan< float > factors)
static void calc_blurred_cavity_grids(const Object &object, const Cache &automasking, const int steps, const SubdivCCGCoord vert, MutableSpan< float > cavity_factors)
static void init_boundary_masking_mesh(Object &object, const Depsgraph &depsgraph, const BoundaryAutomaskMode mode, const int propagation_steps, MutableSpan< float > factors)
static int boundary_propagation_steps(const Paint &paint, const Brush *brush)
static void init_boundary_masking_grids(Object &object, const BoundaryAutomaskMode mode, const int propagation_steps, MutableSpan< float > factors)
bool is_enabled(const Paint &paint, const Object &object, const Brush *br)
static void calc_blurred_cavity_bmesh(const Cache &automasking, const int steps, BMVert *vert, MutableSpan< float > cavity_factors)
static void fill_topology_automasking_factors_bmesh(const Paint &paint, Object &ob, BMesh &bm, MutableSpan< float > factors)
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 calc_interpolated_face_factors(const Depsgraph &depsgraph, const Object &object, OffsetIndices< int > faces, Span< int > corner_verts, const Cache &automasking, const bke::pbvh::MeshNode &node, Span< int > face_indices, MutableSpan< float > factors)
static float calc_cavity_factor(const Cache &automasking, float factor)
static bool is_constrained_by_radius(const Brush *br)
bool mode_enabled(const Sculpt &sd, const Brush *br, eAutomasking_flag mode)
Cache & filter_cache_ensure(const Depsgraph &depsgraph, const Paint &paint, Object &ob)
static float process_cavity_factor(const Cache &automasking, float factor)
static void calc_cavity_factor_grids(const CCGKey &key, const Cache &automasking, const Object &object, const int vert)
static bool needs_factors_cache(const Paint &paint, const Brush *brush)
void ensure_boundary_info(Object &object)
Definition sculpt.cc:588
bool vert_is_boundary(const GroupedSpan< int > vert_to_face_map, const Span< bool > hide_poly, const BitSpan boundary_verts, const int vert)
Definition sculpt.cc:620
bool stroke_is_dyntopo(const Object &object, const Brush &brush)
Definition sculpt.cc:986
bool vert_has_unique_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, int vert)
Definition sculpt.cc:324
bool vert_has_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const int vert, const int face_set)
Definition sculpt.cc:268
int vert_face_set_max_get(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const int vert)
Definition sculpt.cc:232
int active_face_set_get(const Object &object)
Definition sculpt.cc:202
void ensure_cache(Object &object)
Definition sculpt.cc:6448
int vert_id_get(const SculptSession &ss, const int vert)
Definition sculpt.cc:6310
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
Vector< int > find_symm_verts_grids(const Object &object, const int original_vert, const float max_distance)
Vector< BMVert *, 64 > BMeshNeighborVerts
int vertex_count_get(const Object &object)
Definition sculpt.cc:163
std::optional< OrigPositionData > orig_position_data_lookup_grids(const Object &object, const bke::pbvh::GridsNode &node)
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
ePaintSymmetryFlags mesh_symmetry_xyz_get(const Object &object)
Definition sculpt.cc:192
Vector< int > find_symm_verts_mesh(const Depsgraph &depsgraph, const Object &object, const int original_vert, const float max_distance)
Span< BMVert * > vert_neighbors_get_bmesh(BMVert &vert, BMeshNeighborVerts &r_neighbors)
Definition sculpt.cc:490
bool vertex_is_occluded(const Depsgraph &depsgraph, const Object &object, const float3 &position, bool original)
Definition sculpt.cc:6255
bool is_vertex_inside_brush_radius_symm(const float vertex[3], const float br_co[3], float radius, char symm)
Definition sculpt.cc:878
std::optional< OrigPositionData > orig_position_data_lookup_mesh(const Object &object, const bke::pbvh::MeshNode &node)
Span< int > vert_neighbors_get_mesh(const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face, const Span< bool > hide_poly, const int vert, Vector< int > &r_neighbors)
Definition sculpt.cc:534
std::array< int, PAINT_SYMM_AREAS > find_all_symm_verts(const Depsgraph &depsgraph, const Object &object, const int original_vert, const float max_distance)
Vector< int > find_symm_verts_bmesh(const Object &object, const int original_vert, const float max_distance)
bool is_symmetry_iteration_valid(const char i, const char symm)
constexpr ParallelGrainSize grain_size(int grain_size)
T safe_divide(const T &a, const T &b)
T distance(const T &a, const T &b)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T rcp(const T &a)
T safe_acos(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ PAINT_FALLOFF_SHAPE_TUBE
MINLINE float pow2f(float x)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
@ SCULPT_BRUSH_TYPE_DRAW_FACE_SETS
@ SCULPT_BRUSH_TYPE_GRAB
@ SCULPT_BRUSH_TYPE_ROTATE
@ SCULPT_BRUSH_TYPE_THUMB
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
void BKE_curvemapping_init(CurveMapping *cumap)
BMesh const char void * data
MINLINE float signf(float f)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
int BM_mesh_elem_count(BMesh *bm, const char htype)
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
@ BRUSH_AUTOMASKING_VIEW_OCCLUSION
@ BRUSH_AUTOMASKING_TOPOLOGY
@ BRUSH_AUTOMASKING_BRUSH_NORMAL
@ BRUSH_AUTOMASKING_FACE_SETS
@ BRUSH_AUTOMASKING_CAVITY_USE_CURVE
@ BRUSH_AUTOMASKING_BOUNDARY_FACE_SETS
@ BRUSH_AUTOMASKING_BOUNDARY_EDGES
@ BRUSH_AUTOMASKING_CAVITY_ALL
@ BRUSH_AUTOMASKING_VIEW_NORMAL
@ BRUSH_AUTOMASKING_CAVITY_NORMAL
@ BRUSH_AUTOMASKING_CAVITY_INVERTED
const Brush * BKE_paint_brush_for_read(const Paint *paint)
VecBase< float, 3 > float3
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
CustomData pdata
eBrushSculptType sculpt_brush_type
struct MeshAutomaskingSettings * mesh_automasking_settings
eBrushFalloffShape falloff_shape
bke::ObjectRuntime * runtime
std::unique_ptr< SculptBoundaryInfoCache > boundary_info_cache
Definition BKE_paint.hh:458
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
ed::sculpt_paint::filter::Cache * filter_cache
Definition BKE_paint.hh:410
static SubdivCCGCoord from_index(const CCGKey &key, int index)
int to_index(const CCGKey &key) const
SubdivCCGNeighborCoords coords
Array< float3 > positions
Array< float3 > normals
std::unique_ptr< auto_mask::Cache > automasking
void calc_cavity_factor(const Depsgraph &depsgraph, const Object &object, const IndexMask &node_mask)
std::array< int, PAINT_SYMM_AREAS > initial_island_nr
std::unique_ptr< auto_mask::Cache > automasking
void execute(Object &object, FunctionRef< bool(BMVert *from_v, BMVert *to_v)> func)
void execute(Object &object, const SubdivCCG &subdiv_ccg, FunctionRef< bool(SubdivCCGCoord from_v, SubdivCCGCoord to_v, bool is_duplicate)> func)
void execute(Object &object, GroupedSpan< int > vert_to_face_map, FunctionRef< bool(int from_v, int to_v)> func)