Blender V5.3
editors/sculpt_paint/mesh/brushes/mask.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "DNA_brush_types.h"
12#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_object_types.hh"
18#include "BKE_paint_bvh.hh"
19#include "BKE_subdiv_ccg.hh"
20
22#include "BLI_span.hh"
23
28
29#include "bmesh.hh"
30
32inline namespace mask_cc {
33
41
42BLI_NOINLINE static void apply_factors(const float strength,
43 const Span<float> current_masks,
44 const Span<float> factors,
45 const MutableSpan<float> masks)
46{
48 BLI_assert(current_masks.size() == masks.size());
49 BLI_assert(factors.size() == masks.size());
50 for (const int i : masks.index_range()) {
51 masks[i] += factors[i] * current_masks[i] * strength;
52 }
53}
54
56{
58 for (float &mask : masks) {
59 mask = std::clamp(mask, 0.0f, 1.0f);
60 }
61}
62
63static void calc_faces(const Depsgraph &depsgraph,
64 const Brush &brush,
65 const float strength,
66 const Span<float3> positions,
67 const Span<float3> vert_normals,
69 Object &object,
70 const Span<bool> hide_vert,
72{
73 SculptSession &ss = *object.runtime->sculpt_session;
74 const StrokeCache &cache = *ss.cache;
75
76 const Span<int> verts = node.verts();
77
79 fill_factor_from_hide(hide_vert, verts, factors);
80 filter_region_clip_factors(ss, positions, verts, factors);
81 if (brush.flag & BRUSH_FRONTFACE) {
82 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
83 }
84
86 calc_brush_distances(ss, positions, verts, eBrushFalloffShape(brush.falloff_shape), distances);
87 filter_distances_with_radius(cache.radius, distances, factors);
88 apply_hardness_to_distances(cache, distances);
89 calc_brush_strength_factors(cache, brush, distances, factors);
90
91 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
92
93 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, verts, factors);
94
96 gather_data_mesh<float>(mask.as_span(), verts, new_masks);
97
98 Array<float, bke::pbvh::MESH_LEAF_LIMIT> current_masks = new_masks;
99 if (strength > 0.0f) {
100 mask::invert_mask(current_masks);
101 }
102 apply_factors(strength, current_masks, factors, new_masks);
103 clamp_mask(new_masks);
104
105 scatter_data_mesh(new_masks.as_span(), verts, mask);
106}
107
108static void calc_grids(const Depsgraph &depsgraph,
109 Object &object,
110 const Brush &brush,
111 const float strength,
113 LocalData &tls)
114{
115 SculptSession &ss = *object.runtime->sculpt_session;
116 const StrokeCache &cache = *ss.cache;
117 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
118
119 const Span<int> grids = node.grids();
120 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
121
122 tls.factors.resize(positions.size());
123 const MutableSpan<float> factors = tls.factors;
124 fill_factor_from_hide(subdiv_ccg, grids, factors);
125 filter_region_clip_factors(ss, positions, factors);
126 if (brush.flag & BRUSH_FRONTFACE) {
127 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
128 }
129
130 tls.distances.resize(positions.size());
131 const MutableSpan<float> distances = tls.distances;
132 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
133 filter_distances_with_radius(cache.radius, distances, factors);
134 apply_hardness_to_distances(cache, distances);
135 calc_brush_strength_factors(cache, brush, distances, factors);
136
137 auto_mask::calc_grids_factors(depsgraph, object, cache.automasking.get(), node, grids, factors);
138
139 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
140
141 tls.new_masks.resize(positions.size());
142 const MutableSpan<float> new_masks = tls.new_masks;
143 mask::gather_mask_grids(subdiv_ccg, grids, new_masks);
144
145 tls.current_masks = tls.new_masks;
146 const MutableSpan<float> current_masks = tls.current_masks;
147 if (strength > 0.0f) {
148 mask::invert_mask(current_masks);
149 }
150 apply_factors(strength, current_masks, factors, new_masks);
151 clamp_mask(new_masks);
152
153 mask::scatter_mask_grids(new_masks.as_span(), subdiv_ccg, grids);
154}
155
156static void calc_bmesh(const Depsgraph &depsgraph,
157 Object &object,
158 const Brush &brush,
159 const float strength,
161 LocalData &tls)
162{
163 SculptSession &ss = *object.runtime->sculpt_session;
164 const BMesh &bm = *ss.bm;
165 const StrokeCache &cache = *ss.cache;
166
168 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
169
170 tls.factors.resize(verts.size());
171 const MutableSpan<float> factors = tls.factors;
172 fill_factor_from_hide(verts, factors);
173 filter_region_clip_factors(ss, positions, factors);
174 if (brush.flag & BRUSH_FRONTFACE) {
175 calc_front_face(cache.view_normal_symm, verts, factors);
176 }
177
178 tls.distances.resize(verts.size());
179 const MutableSpan<float> distances = tls.distances;
180 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
181 filter_distances_with_radius(cache.radius, distances, factors);
182 apply_hardness_to_distances(cache, distances);
183 calc_brush_strength_factors(cache, brush, distances, factors);
184
185 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
186
187 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
188
189 tls.new_masks.resize(verts.size());
190 const MutableSpan<float> new_masks = tls.new_masks;
191 mask::gather_mask_bmesh(bm, verts, new_masks);
192
193 tls.current_masks = tls.new_masks;
194 const MutableSpan<float> current_masks = tls.current_masks;
195 if (strength > 0.0f) {
196 mask::invert_mask(current_masks);
197 }
198 apply_factors(strength, current_masks, factors, new_masks);
199 clamp_mask(new_masks);
200
201 mask::scatter_mask_bmesh(new_masks.as_span(), bm, verts);
202}
203
204} // namespace mask_cc
205
206void do_mask_brush(const Depsgraph &depsgraph,
207 const Sculpt &sd,
208 Object &object,
209 const IndexMask &node_mask)
210{
212 SculptSession &ss = *object.runtime->sculpt_session;
215 const float bstrength = ss.cache->bstrength;
216
218 switch (pbvh.type()) {
221 Mesh &mesh = *id_cast<Mesh *>(object.data);
222 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, object);
223 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
224
225 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
226
228 ".sculpt_mask", bke::AttrDomain::Point);
229 const VArraySpan hide_vert = *attributes.lookup<bool>(".hide_vert", bke::AttrDomain::Point);
230
231 node_mask.foreach_index(
232 [&](const int i) {
233 calc_faces(depsgraph,
234 brush,
235 bstrength,
236 positions,
237 vert_normals,
238 nodes[i],
239 object,
240 hide_vert,
241 mask.span);
243 },
245 mask.finish();
246 break;
247 }
249 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
250 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
251 MutableSpan<float> masks = subdiv_ccg.masks;
253 node_mask.foreach_index(
254 [&](const int i) {
255 LocalData &tls = all_tls.local();
256 calc_grids(depsgraph, object, brush, bstrength, nodes[i], tls);
258 },
260 break;
261 }
263 const int mask_offset = CustomData_get_offset_named(
264 &ss.bm->vdata, CD_PROP_FLOAT, ".sculpt_mask");
266 node_mask.foreach_index(
267 [&](const int i) {
268 LocalData &tls = all_tls.local();
269 calc_bmesh(depsgraph, object, brush, bstrength, nodes[i], tls);
271 },
273 break;
274 }
275 }
276 pbvh.tag_masks_changed(node_mask);
277}
278
279} // 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
constexpr int64_t size() const
Definition BLI_span.hh:499
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
GAttributeReader lookup(const StringRef name) const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
pbvh::Tree * pbvh_get(Object &object)
void node_update_mask_bmesh(int mask_offset, BMeshNode &node)
Definition pbvh.cc:1404
void node_update_mask_mesh(Span< float > mask, MeshNode &node)
Definition pbvh.cc:1345
void node_update_mask_grids(const CCGKey &key, Span< float > masks, GridsNode &node)
Definition pbvh.cc:1373
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
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 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 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)
static BLI_NOINLINE void apply_factors(const float strength, const Span< float > current_masks, const Span< float > factors, const MutableSpan< float > masks)
static BLI_NOINLINE void clamp_mask(const MutableSpan< float > masks)
void do_mask_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
void scatter_mask_grids(const Span< float > mask, SubdivCCG &subdiv_ccg, const Span< int > grids)
void scatter_mask_bmesh(const Span< float > mask, const BMesh &bm, const Set< BMVert *, 0 > &verts)
void gather_mask_bmesh(const BMesh &bm, const Set< BMVert *, 0 > &verts, const MutableSpan< float > r_mask)
void invert_mask(const MutableSpan< float > masks)
void gather_mask_grids(const SubdivCCG &subdiv_ccg, const Span< int > grids, const MutableSpan< float > r_mask)
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void gather_bmesh_positions(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6497
void calc_brush_strength_factors(const StrokeCache &cache, const Brush &brush, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7501
void 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
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
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 gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
void calc_front_face(const float3 &view_normal, Span< float3 > normals, MutableSpan< float > factors)
Definition sculpt.cc:7223
void calc_brush_texture_factors(PaintMode paint_mode, const SculptSession &ss, const Brush &brush, Span< float3 > vert_positions, Span< int > vert, MutableSpan< float > factors)
Definition sculpt.cc:7513
void fill_factor_from_hide(Span< bool > hide_vert, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7077
constexpr ParallelGrainSize grain_size(int grain_size)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
BMesh const char void * data
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
CustomData vdata
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
Array< float > masks
std::unique_ptr< auto_mask::Cache > automasking
void resize(int new_size)
Definition vector.hh:94