Blender V5.3
clay_strips.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
17
19
20#include "DNA_brush_types.h"
21#include "DNA_mesh_types.h"
22#include "DNA_object_types.h"
23#include "DNA_scene_types.h"
24
25#include "BKE_brush.hh"
26#include "BKE_mesh.hh"
27#include "BKE_object_types.hh"
28#include "BKE_paint.hh"
29#include "BKE_paint_bvh.hh"
30#include "BKE_subdiv_ccg.hh"
31
33#include "BLI_math_geom_c.hh"
34#include "BLI_math_matrix.hh"
35#include "BLI_math_vector.hh"
36#include "BLI_task.hh"
37
41
42#include "bmesh.hh"
43
45
46inline namespace clay_strips_cc {
47
56
57static void apply_z_axis_factors(const Span<float> z_positions, const MutableSpan<float> factors)
58{
60 BLI_assert(factors.size() == z_positions.size());
61
62 for (const int i : factors.index_range()) {
63 const float local_z = z_positions[i];
64
65 /* Note: if `local_z > 1`, then `1 - local_z < 0` and the product is negative. */
66 factors[i] *= math::max(0.0f, local_z * (1.0f - local_z));
67 }
68}
69
75 const Span<float> z_positions,
76 const MutableSpan<float> factors)
77{
79 BLI_assert(factors.size() == z_positions.size());
80
81 const bool use_plane_trim = brush.flag & BRUSH_PLANE_TRIM;
82 if (!use_plane_trim) {
83 return;
84 }
85
86 for (const int i : factors.index_range()) {
87 if (z_positions[i] > brush.plane_trim) {
88 factors[i] = 0.0f;
89 }
90 }
91}
92
93static void calc_faces(const Depsgraph &depsgraph,
94 const Sculpt &sd,
95 const Brush &brush,
96 const float4x4 &mat,
97 const float3 &offset,
98 const Span<float3> vert_normals,
99 const MeshAttributeData &attribute_data,
101 Object &object,
102 const PositionDeformData &position_data)
103{
104 SculptSession &ss = *object.runtime->sculpt_session;
105 const StrokeCache &cache = *ss.cache;
106
107 const Span<int> verts = node.verts();
108
110 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
111 filter_region_clip_factors(ss, position_data.eval, verts, factors);
112 if (brush.flag & BRUSH_FRONTFACE) {
113 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
114 }
115
118
119 calc_local_positions(position_data.eval,
120 verts,
121 mat,
122 cache.location_symm,
123 cache.view_normal_symm,
124 eBrushFalloffShape(brush.falloff_shape),
125 xy_positions,
126 z_positions);
127 if (eBrushFalloffShape(brush.falloff_shape) == PAINT_FALLOFF_SHAPE_TUBE) {
128 z_positions.as_mutable_span().fill(brush.plane_offset);
129 }
130 apply_z_axis_factors(z_positions, factors);
131 apply_plane_trim_factors(brush, z_positions, factors);
132
134 calc_brush_cube_distances<float2>(brush, xy_positions, distances);
135 filter_distances_with_radius(1.0f, distances, factors);
136 apply_hardness_to_distances(1.0f, cache.hardness, distances);
137 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
138 brush.curve_distance_falloff,
139 distances,
140 1.0f,
141 factors);
142
143 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
144
145 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, position_data.eval, verts, factors);
146
148 translations_from_offset_and_factors(offset, factors, translations);
149
150 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
151 position_data.deform(translations, verts);
152}
153
154static void calc_grids(const Depsgraph &depsgraph,
155 const Sculpt &sd,
156 Object &object,
157 const Brush &brush,
158 const float4x4 &mat,
159 const float3 &offset,
161 LocalData &tls)
162{
163 SculptSession &ss = *object.runtime->sculpt_session;
164 const StrokeCache &cache = *ss.cache;
165 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
166
167 const Span<int> grids = node.grids();
168 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
169
170 tls.factors.resize(positions.size());
171 const MutableSpan<float> factors = tls.factors;
172 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
173 filter_region_clip_factors(ss, positions, factors);
174 if (brush.flag & BRUSH_FRONTFACE) {
175 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
176 }
177
178 tls.xy_positions.resize(positions.size());
179 tls.z_positions.resize(positions.size());
180 MutableSpan<float2> xy_positions = tls.xy_positions;
181 MutableSpan<float> z_positions = tls.z_positions;
182
183 calc_local_positions(positions,
184 mat,
185 cache.location_symm,
186 cache.view_normal_symm,
187 eBrushFalloffShape(brush.falloff_shape),
188 xy_positions,
189 z_positions);
190 if (eBrushFalloffShape(brush.falloff_shape) == PAINT_FALLOFF_SHAPE_TUBE) {
191 z_positions.fill(brush.plane_offset);
192 }
193 apply_z_axis_factors(z_positions, factors);
194 apply_plane_trim_factors(brush, z_positions, factors);
195
196 tls.distances.resize(positions.size());
197 const MutableSpan<float> distances = tls.distances;
198 calc_brush_cube_distances<float2>(brush, xy_positions, distances);
199 filter_distances_with_radius(1.0f, distances, factors);
200 apply_hardness_to_distances(1.0f, cache.hardness, distances);
201 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
202 brush.curve_distance_falloff,
203 distances,
204 1.0f,
205 factors);
206
207 auto_mask::calc_grids_factors(depsgraph, object, cache.automasking.get(), node, grids, factors);
208
209 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
210
211 tls.translations.resize(positions.size());
213
214 clip_and_lock_translations(sd, ss, positions, tls.translations);
215 apply_translations(tls.translations, grids, subdiv_ccg);
216}
217
218static void calc_bmesh(const Depsgraph &depsgraph,
219 const Sculpt &sd,
220 Object &object,
221 const Brush &brush,
222 const float4x4 &mat,
223 const float3 &offset,
225 LocalData &tls)
226{
227 SculptSession &ss = *object.runtime->sculpt_session;
228 const StrokeCache &cache = *ss.cache;
229
231 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
232
233 tls.factors.resize(verts.size());
234 const MutableSpan<float> factors = tls.factors;
235 fill_factor_from_hide_and_mask(*ss.bm, verts, factors);
236 filter_region_clip_factors(ss, positions, factors);
237 if (brush.flag & BRUSH_FRONTFACE) {
238 calc_front_face(cache.view_normal_symm, verts, factors);
239 }
240
241 tls.xy_positions.resize(positions.size());
242 tls.z_positions.resize(positions.size());
243 MutableSpan<float2> xy_positions = tls.xy_positions;
244 MutableSpan<float> z_positions = tls.z_positions;
245
246 calc_local_positions(positions,
247 mat,
248 cache.location_symm,
249 cache.view_normal_symm,
250 eBrushFalloffShape(brush.falloff_shape),
251 xy_positions,
252 z_positions);
253 if (eBrushFalloffShape(brush.falloff_shape) == PAINT_FALLOFF_SHAPE_TUBE) {
254 z_positions.fill(brush.plane_offset);
255 }
256 apply_z_axis_factors(z_positions, factors);
257 apply_plane_trim_factors(brush, z_positions, factors);
258
259 tls.distances.resize(positions.size());
260 const MutableSpan<float> distances = tls.distances;
261 calc_brush_cube_distances<float2>(brush, xy_positions, distances);
262 filter_distances_with_radius(1.0f, distances, factors);
263 apply_hardness_to_distances(1.0f, cache.hardness, distances);
264 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
265 brush.curve_distance_falloff,
266 distances,
267 1.0f,
268 factors);
269
270 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
271
272 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
273
274 tls.translations.resize(positions.size());
276
277 clip_and_lock_translations(sd, ss, positions, tls.translations);
279}
280
281} // namespace clay_strips_cc
282
283void do_clay_strips_brush(const Depsgraph &depsgraph,
284 const Sculpt &sd,
285 Object &object,
286 const IndexMask &node_mask,
287 const float3 &plane_normal,
288 const float3 &plane_center)
289{
291 SculptSession &ss = *object.runtime->sculpt_session;
294 const bool flip = (ss.cache->bstrength < 0.0f);
295
296 /* Note: This return has to happen *after* the call to calc_brush_plane for now, as
297 * the method is not idempotent and sets variables inside the stroke cache. */
299 return;
300 }
301
302 const float3 &tip_normal = eBrushFalloffShape(brush.falloff_shape) ==
304 plane_normal :
307 brush, *ss.cache, tip_normal, plane_center, flip);
308
309 const float3 offset = plane_normal * ss.cache->bstrength * ss.cache->radius;
310
312 switch (pbvh.type()) {
314 Mesh &mesh = *id_cast<Mesh *>(object.data);
316 const PositionDeformData position_data(depsgraph, object);
317 const MeshAttributeData attribute_data(mesh);
318 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
319 node_mask.foreach_index(
320 [&](const int i) {
321 calc_faces(depsgraph,
322 sd,
323 brush,
324 mat,
325 offset,
326 vert_normals,
327 attribute_data,
328 nodes[i],
329 object,
330 position_data);
331 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
332 },
334 break;
335 }
337 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
338 MutableSpan<float3> positions = subdiv_ccg.positions;
340 node_mask.foreach_index(
341 [&](const int i) {
342 LocalData &tls = all_tls.local();
343 calc_grids(depsgraph, sd, object, brush, mat, offset, nodes[i], tls);
344 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
345 },
347 break;
348 }
351 node_mask.foreach_index(
352 [&](const int i) {
353 LocalData &tls = all_tls.local();
354 calc_bmesh(depsgraph, sd, object, brush, mat, offset, nodes[i], tls);
356 },
358 break;
359 }
360 }
361 pbvh.tag_positions_changed(node_mask);
362 pbvh.flush_bounds_to_parents();
363}
364
365namespace clay_strips {
366
368 const StrokeCache &cache,
369 const float3 &tip_normal,
370 const float3 &plane_center,
371 const bool flip)
372{
373 /* TODO: the current calculations always behave as if Rake is enabled. Use similar logic to
374 * calc_brush_local_mat in sculpt.cc to fix the issue. */
375
377 mat.x_axis() = math::cross(tip_normal, cache.grab_delta_symm);
378 mat.y_axis() = math::cross(tip_normal, mat.x_axis());
379 mat.z_axis() = tip_normal;
380
381 /* Flip the z-axis so that the vertices below the plane have positive z-coordinates. When the
382 * brush is inverted, the affected z-coordinates are already positive. */
383 if (!flip) {
384 mat.z_axis() *= -1.0f;
385 }
386
387 mat.location() = plane_center;
388 mat = math::normalize(mat);
389
390 /* Scale brush local space matrix. */
392 float4x4 tmat = mat * scale;
393 tmat.y_axis() *= brush.tip_scale_x;
394 mat = math::invert(tmat);
395 return mat;
396}
397
398CursorSampleResult calc_node_mask(const Depsgraph &depsgraph,
399 Object &object,
400 const Brush &brush,
401 IndexMaskMemory &memory)
402{
404 const SculptSession &ss = *object.runtime->sculpt_session;
405
406 const eBrushFalloffShape falloff_shape = eBrushFalloffShape(brush.falloff_shape);
407
408 const bool flip = (ss.cache->bstrength < 0.0f);
409 const float displace = ss.cache->radius * brush_plane_offset_get(brush, ss) *
410 (flip ? -1.0f : 1.0f);
411
412 /* TODO: Test to see if the sqrt2 extra factor can be removed */
413 const float initial_radius_squared = math::square(ss.cache->radius * std::numbers::sqrt2);
414
415 const bool use_original = !ss.cache->accum;
416 const IndexMask initial_node_mask = gather_nodes(pbvh,
417 falloff_shape,
418 use_original,
420 initial_radius_squared,
422 memory);
423
424 float3 plane_center;
425 float3 plane_normal;
426 calc_brush_plane(depsgraph, brush, object, initial_node_mask, plane_normal, plane_center);
427 plane_normal = tilt_apply_to_normal(plane_normal, *ss.cache, brush.tilt_strength_factor);
428 plane_center += plane_normal * ss.cache->scale * displace;
429
430 if (math::is_zero(ss.cache->grab_delta_symm) || math::is_zero(plane_normal)) {
431 /* The brush local matrix is degenerate: return an empty index mask. */
432 return {IndexMask(), plane_center, plane_normal};
433 }
434
435 switch (falloff_shape) {
437 const float4x4 mat = calc_local_matrix(brush, *ss.cache, plane_normal, plane_center, flip);
438 const IndexMask plane_mask = bke::pbvh::search_nodes(
439 pbvh, memory, [&](const bke::pbvh::Node &node) {
441 return false;
442 }
443 return node_in_box_positive_z(mat, node.bounds());
444 });
445 return {plane_mask, plane_center, plane_normal};
446 }
447
449 const float4x4 mat = calc_local_matrix(
450 brush, *ss.cache, ss.cache->view_normal_symm, plane_center, flip);
451 const IndexMask plane_mask = bke::pbvh::search_nodes(
452 pbvh, memory, [&](const bke::pbvh::Node &node) {
454 return false;
455 }
456 return node_in_box(mat, node.bounds(), float3(0.0f), float3(1.0f, 1.0f, 1.0f), false);
457 });
458 return {plane_mask, plane_center, plane_normal};
459 }
460 }
461
463 return {};
464}
465} // namespace clay_strips
466
467} // 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_assert_unreachable()
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
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
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
float3x3 scale(float3x3 mat, float2 scale)
pbvh::Tree * pbvh_get(Object &object)
IndexMask search_nodes(const Tree &pbvh, IndexMaskMemory &memory, FunctionRef< bool(const Node &)> filter_fn)
Definition pbvh.cc:2556
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 void apply_z_axis_factors(const Span< float > z_positions, const MutableSpan< float > factors)
static void apply_plane_trim_factors(const Brush &brush, const Span< float > z_positions, const MutableSpan< float > factors)
float4x4 calc_local_matrix(const Brush &brush, const StrokeCache &cache, const float3 &plane_normal, const float3 &plane_center, const bool flip)
CursorSampleResult calc_node_mask(const Depsgraph &depsgraph, Object &ob, const Brush &brush, IndexMaskMemory &memory)
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)
void do_clay_strips_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask, const float3 &plane_normal, const float3 &plane_center)
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 gather_bmesh_positions(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6497
void apply_hardness_to_distances(float radius, float hardness, MutableSpan< float > distances)
Definition sculpt.cc:7473
bool node_fully_masked_or_hidden(const bke::pbvh::Node &node)
Definition sculpt.cc:2500
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7422
bool node_in_box_positive_z(const float4x4 &mat, const Bounds< float3 > &bounds)
Definition sculpt.cc:2606
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
IndexMask gather_nodes(const bke::pbvh::Tree &pbvh, const eBrushFalloffShape falloff_shape, const bool use_original, const float3 &location, const float radius_sq, const std::optional< float3 > &ray_direction, IndexMaskMemory &memory)
Definition sculpt.cc:2696
void calc_brush_plane(const Depsgraph &depsgraph, const Brush &brush, Object &ob, const IndexMask &node_mask, float3 &r_area_no, float3 &r_area_co)
Definition sculpt.cc:3062
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
bool node_in_box(const float4x4 &mat, const Bounds< float3 > &bounds, const float3 &brush_center, const float3 &brush_half_lengths, const bool test_z_axis)
Definition sculpt.cc:2536
float brush_plane_offset_get(const Brush &brush, const SculptSession &ss)
Definition sculpt.cc:3145
void calc_front_face(const float3 &view_normal, Span< float3 > normals, MutableSpan< float > factors)
Definition sculpt.cc:7223
void translations_from_offset_and_factors(const float3 &offset, Span< float > factors, MutableSpan< float3 > r_translations)
Definition sculpt.cc:7877
void calc_brush_cube_distances(const Brush &brush, const Span< T > positions, const MutableSpan< float > r_distances)
Definition sculpt.cc:7435
void calc_local_positions(Span< float3 > vert_positions, Span< int > verts, const float4x4 &mat, const float3 &plane_center, const float3 &view_normal, eBrushFalloffShape falloff_shape, MutableSpan< float3 > local_positions)
Definition sculpt.cc:6619
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
float3 tilt_apply_to_normal(const Object &object, const float4x4 &view_inverse, const float3 &normal, const float2 &tilt, const float tilt_strength)
Definition sculpt.cc:2876
constexpr ParallelGrainSize grain_size(int grain_size)
bool is_zero(const T &a)
CartesianBasis invert(const CartesianBasis &basis)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T square(const T &a)
T max(const T &a, const T &b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ PAINT_FALLOFF_SHAPE_SPHERE
@ PAINT_FALLOFF_SHAPE_TUBE
void BKE_brush_calc_curve_factors(eBrushCurvePreset preset, const CurveMapping *cumap, Span< float > distances, float brush_radius, MutableSpan< float > factors)
Definition brush.cc:1561
MatBase< float, 4, 4 > float4x4
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
Array< float3 > positions
std::unique_ptr< auto_mask::Cache > automasking
void resize(int new_size)
Definition vector.hh:94