Blender V5.3
snake_hook.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_kelvinlet.h"
17#include "BKE_mesh.hh"
18#include "BKE_object_types.hh"
19#include "BKE_paint.hh"
20#include "BKE_paint_bvh.hh"
21#include "BKE_subdiv_ccg.hh"
22
25#include "BLI_math_vector.hh"
26#include "BLI_math_vector_c.hh"
27#include "BLI_task.hh"
28
32
33#include "bmesh.hh"
34
36
37inline namespace snake_hook_cc {
38
45
52
57{
58 spvc->plane = plane;
59 spvc->len_sq = math::length_squared(spvc->plane);
60 spvc->is_valid = (spvc->len_sq > FLT_EPSILON);
61 spvc->len_sq_inv_neg = (spvc->is_valid) ? -1.0f / spvc->len_sq : 0.0f;
62}
63
67static void sculpt_project_v3(const SculptProjectVector *spvc, const float3 &vec, float3 &r_vec)
68{
69#if 0
70 project_plane_v3_v3v3(r_vec, vec, spvc->plane);
71#else
72 /* inline the projection, cache `-1.0 / dot_v3_v3(v_proj, v_proj)` */
73 r_vec += spvc->plane * math::dot(vec, spvc->plane) * spvc->len_sq_inv_neg;
74#endif
75}
76
78 const float3 &sculpt_co,
79 const float3 &v_co,
80 float factor)
81{
82 float3 vec_rot = v_co - sculpt_co;
83 const math::Quaternion rotation = math::pow(*cache.rake_rotation_symm, factor);
84 vec_rot = math::transform_point(rotation, vec_rot);
85
86 vec_rot += sculpt_co;
87 return vec_rot - v_co;
88}
89
91 const StrokeCache &cache,
92 const float3 &grab_delta,
93 const SculptProjectVector *spvc,
94 const Span<float3> positions,
95 const Span<float> factors,
96 const MutableSpan<float3> translations)
97{
99 if (brush.crease_pinch_factor == 0.5f) {
100 return;
101 }
102
103 const float pinch = 2.0f * (0.5f - brush.crease_pinch_factor) * math::length(grab_delta) /
104 cache.radius;
105
106 for (const int i : positions.index_range()) {
107 /* Negative pinch will inflate, helps maintain volume. */
108 float3 delta_pinch = positions[i] - cache.location_symm;
109
110 if (brush.falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
111 project_plane_v3_v3v3(delta_pinch, delta_pinch, cache.view_normal);
112 }
113
114 /* Important to calculate based on the grabbed location
115 * (intentionally ignore fade here). */
116 delta_pinch += grab_delta;
117
118 sculpt_project_v3(spvc, delta_pinch, delta_pinch);
119
120 float3 delta_pinch_init = delta_pinch;
121
122 float pinch_fade = pinch * factors[i];
123 /* When reducing, scale reduction back by how close to the center we are,
124 * so we don't pinch into nothingness. */
125 if (pinch > 0.0f) {
126 /* Square to have even less impact for close vertices. */
127 pinch_fade *= pow2f(std::min(1.0f, math::length(delta_pinch) / cache.radius));
128 }
129 delta_pinch *= (1.0f + pinch_fade);
130 delta_pinch = delta_pinch_init - delta_pinch;
131 translations[i] += delta_pinch;
132 }
133}
134
136 const Span<float3> positions,
137 const Span<float> factors,
138 const MutableSpan<float3> translations)
139{
141 if (!cache.rake_rotation_symm) {
142 return;
143 }
144 for (const int i : positions.index_range()) {
145 translations[i] += sculpt_rake_rotate(cache, cache.location_symm, positions[i], factors[i]);
146 }
147}
148
150 const Span<float3> positions,
151 const Span<float> factors,
152 const MutableSpan<float3> translations)
153{
155 KelvinletParams params;
156 BKE_kelvinlet_init_params(&params, cache.radius, cache.bstrength, 1.0f, 0.4f);
157 for (const int i : positions.index_range()) {
158 float3 disp;
159 BKE_kelvinlet_grab_triscale(disp, &params, positions[i], cache.location_symm, translations[i]);
160 translations[i] = disp * factors[i];
161 }
162}
163
164static void calc_faces(const Depsgraph &depsgraph,
165 const Sculpt &sd,
166 Object &object,
167 const Brush &brush,
168 const SculptProjectVector *spvc,
169 const float3 &grab_delta,
170 const Span<float3> vert_normals,
171 const MeshAttributeData &attribute_data,
173 const PositionDeformData &position_data)
174{
175 SculptSession &ss = *object.runtime->sculpt_session;
176 const StrokeCache &cache = *ss.cache;
177 const bool do_elastic = brush.snake_hook_deform_type == BRUSH_SNAKE_HOOK_DEFORM_ELASTIC;
178
179 const Span<int> verts = node.verts();
181 gather_data_mesh(position_data.eval, verts, positions.as_mutable_span());
182
184
185 if (do_elastic) {
186 factors.fill(1.0f);
187 }
188 else {
189 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
190 filter_region_clip_factors(ss, positions, factors);
191 if (brush.flag & BRUSH_FRONTFACE) {
192 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
193 }
194
196 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
197 filter_distances_with_radius(cache.radius, distances, factors);
198 apply_hardness_to_distances(cache, distances);
199 calc_brush_strength_factors(cache, brush, distances, factors);
200
201 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
202 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
203 scale_factors(factors, cache.bstrength);
204 }
205
207
208 translations_from_offset_and_factors(grab_delta, factors, translations);
209
210 calc_pinch_influence(brush, cache, grab_delta, spvc, positions, factors, translations);
211
212 calc_rake_rotation_influence(cache, positions, factors, translations);
213
214 if (do_elastic) {
215 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
216 scale_factors(factors, cache.bstrength * 20.0f);
217 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
218
219 calc_kelvinet_translation(cache, positions, factors, translations);
220 }
221
222 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
223 position_data.deform(translations, verts);
224}
225
226static void calc_grids(const Depsgraph &depsgraph,
227 const Sculpt &sd,
228 Object &object,
229 const Brush &brush,
231 const float3 &grab_delta,
233 LocalData &tls)
234{
235 SculptSession &ss = *object.runtime->sculpt_session;
236 const StrokeCache &cache = *ss.cache;
237 const bool do_elastic = brush.snake_hook_deform_type == BRUSH_SNAKE_HOOK_DEFORM_ELASTIC;
238 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
239
240 const Span<int> grids = node.grids();
241 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
242
243 tls.factors.resize(positions.size());
244 const MutableSpan<float> factors = tls.factors;
245
246 if (do_elastic) {
247 factors.fill(1.0f);
248 }
249 else {
250 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
251 filter_region_clip_factors(ss, positions, factors);
252 if (brush.flag & BRUSH_FRONTFACE) {
253 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
254 }
255
256 tls.distances.resize(positions.size());
257 const MutableSpan<float> distances = tls.distances;
258 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
259 filter_distances_with_radius(cache.radius, distances, factors);
260 apply_hardness_to_distances(cache, distances);
261 calc_brush_strength_factors(cache, brush, distances, factors);
262
264 depsgraph, object, cache.automasking.get(), node, grids, factors);
265 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
266 scale_factors(factors, cache.bstrength);
267 }
268
269 tls.translations.resize(positions.size());
270 const MutableSpan<float3> translations = tls.translations;
271
272 translations_from_offset_and_factors(grab_delta, factors, translations);
273
274 calc_pinch_influence(brush, cache, grab_delta, spvc, positions, factors, translations);
275
276 calc_rake_rotation_influence(cache, positions, factors, translations);
277
278 if (do_elastic) {
279 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
280 scale_factors(factors, cache.bstrength * 20.0f);
282 depsgraph, object, cache.automasking.get(), node, grids, factors);
283
284 calc_kelvinet_translation(cache, positions, factors, translations);
285 }
286
287 clip_and_lock_translations(sd, ss, positions, translations);
288 apply_translations(translations, grids, subdiv_ccg);
289}
290
291static void calc_bmesh(const Depsgraph &depsgraph,
292 const Sculpt &sd,
293 Object &object,
294 const Brush &brush,
296 const float3 &grab_delta,
298 LocalData &tls)
299{
300 SculptSession &ss = *object.runtime->sculpt_session;
301 const StrokeCache &cache = *ss.cache;
302 const bool do_elastic = brush.snake_hook_deform_type == BRUSH_SNAKE_HOOK_DEFORM_ELASTIC;
303
305 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
306
307 tls.factors.resize(verts.size());
308 const MutableSpan<float> factors = tls.factors;
309
310 if (do_elastic) {
311 factors.fill(1.0f);
312 }
313 else {
314 fill_factor_from_hide_and_mask(*ss.bm, verts, factors);
315 filter_region_clip_factors(ss, positions, factors);
316 if (brush.flag & BRUSH_FRONTFACE) {
317 calc_front_face(cache.view_normal_symm, verts, factors);
318 }
319
320 tls.distances.resize(verts.size());
321 const MutableSpan<float> distances = tls.distances;
322 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
323 filter_distances_with_radius(cache.radius, distances, factors);
324 apply_hardness_to_distances(cache, distances);
325 calc_brush_strength_factors(cache, brush, distances, factors);
326
327 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
328 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
329 scale_factors(factors, cache.bstrength);
330 }
331
332 tls.translations.resize(verts.size());
333 const MutableSpan<float3> translations = tls.translations;
334
335 translations_from_offset_and_factors(grab_delta, factors, translations);
336
337 calc_pinch_influence(brush, cache, grab_delta, spvc, positions, factors, translations);
338
339 calc_rake_rotation_influence(cache, positions, factors, translations);
340
341 if (do_elastic) {
342 fill_factor_from_hide_and_mask(*ss.bm, verts, factors);
343 scale_factors(factors, cache.bstrength * 20.0f);
344 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
345
346 calc_kelvinet_translation(cache, positions, factors, translations);
347 }
348
349 clip_and_lock_translations(sd, ss, positions, translations);
350 apply_translations(translations, verts);
351}
352
353} // namespace snake_hook_cc
354
355void do_snake_hook_brush(const Depsgraph &depsgraph,
356 const Sculpt &sd,
357 Object &object,
358 const IndexMask &node_mask)
359{
361 SculptSession &ss = *object.runtime->sculpt_session;
364 BLI_assert(ss.cache->bstrength >= 0.0f);
365
366 const float3 grab_delta = grab_delta_get(brush, *ss.cache);
367
369 /* Optionally pinch while painting. */
370 if (brush.crease_pinch_factor != 0.5f) {
371 sculpt_project_v3_cache_init(&spvc, grab_delta);
372 }
373
375 switch (pbvh.type()) {
377 Mesh &mesh = *id_cast<Mesh *>(object.data);
378 const MeshAttributeData attribute_data(mesh);
379 const PositionDeformData position_data(depsgraph, object);
380 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
382 node_mask.foreach_index(
383 [&](const int i) {
384 calc_faces(depsgraph,
385 sd,
386 object,
387 brush,
388 &spvc,
389 grab_delta,
390 vert_normals,
391 attribute_data,
392 nodes[i],
393 position_data);
394 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
395 },
397 break;
398 }
400 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
401 MutableSpan<float3> positions = subdiv_ccg.positions;
403 node_mask.foreach_index(
404 [&](const int i) {
405 LocalData &tls = all_tls.local();
406 calc_grids(depsgraph, sd, object, brush, &spvc, grab_delta, nodes[i], tls);
407 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
408 },
410 break;
411 }
414 node_mask.foreach_index(
415 [&](const int i) {
416 LocalData &tls = all_tls.local();
417 calc_bmesh(depsgraph, sd, object, brush, &spvc, grab_delta, nodes[i], tls);
419 },
421 break;
422 }
423 }
424 pbvh.tag_positions_changed(node_mask);
425 pbvh.flush_bounds_to_parents();
426}
427
428} // 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
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
void fill(const T &value) const
Definition BLI_array.hh:283
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
int64_t size() const
Definition BLI_set.hh:606
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define FLT_EPSILON
pbvh::Tree * pbvh_get(Object &object)
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1216
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1196
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1205
void calc_vert_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::MeshNode &node, Span< int > verts, MutableSpan< float > factors)
void calc_grids_factors(const Depsgraph &depsgraph, const Object &object, const Cache &automasking, const bke::pbvh::GridsNode &node, Span< int > grids, MutableSpan< float > factors)
static 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 float3 sculpt_rake_rotate(const StrokeCache &cache, const float3 &sculpt_co, const float3 &v_co, float factor)
Definition snake_hook.cc:77
static void sculpt_project_v3(const SculptProjectVector *spvc, const float3 &vec, float3 &r_vec)
Definition snake_hook.cc:67
static BLI_NOINLINE void calc_pinch_influence(const Brush &brush, const StrokeCache &cache, const float3 &grab_delta, const SculptProjectVector *spvc, const Span< float3 > positions, const Span< float > factors, const MutableSpan< float3 > translations)
Definition snake_hook.cc:90
static void sculpt_project_v3_cache_init(SculptProjectVector *spvc, const float3 &plane)
Definition snake_hook.cc:56
static BLI_NOINLINE void calc_rake_rotation_influence(const StrokeCache &cache, const Span< float3 > positions, const Span< float > factors, const MutableSpan< float3 > translations)
static BLI_NOINLINE void calc_kelvinet_translation(const StrokeCache &cache, const Span< float3 > positions, const Span< float > factors, const MutableSpan< float3 > translations)
void do_snake_hook_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
void fill_factor_from_hide_and_mask(Span< bool > hide_vert, Span< float > mask, Span< int > verts, MutableSpan< float > r_factors)
Definition sculpt.cc:7128
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void 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 scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
void calc_brush_distances(const SculptSession &ss, Span< float3 > vert_positions, Span< int > vert, eBrushFalloffShape falloff_shape, MutableSpan< float > r_distances)
Definition sculpt.cc:7370
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
float3 grab_delta_get(const Brush &brush, const StrokeCache &cache)
Definition sculpt.cc:718
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_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
constexpr ParallelGrainSize grain_size(int grain_size)
QuaternionBase< float > Quaternion
T length_squared(const VecBase< T, Size > &a)
T pow(const T &x, const T &power)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_kelvinlet_init_params(KelvinletParams *params, float radius, float force, float shear_modulus, float poisson_ratio)
Definition kelvinlet.cc:18
@ BRUSH_SNAKE_HOOK_DEFORM_ELASTIC
@ PAINT_FALLOFF_SHAPE_TUBE
MINLINE float pow2f(float x)
BMesh const char void * data
void BKE_kelvinlet_grab_triscale(float radius_elem_disp[3], const KelvinletParams *params, const float elem_orig_co[3], const float brush_location[3], const float brush_delta[3])
Definition kelvinlet.cc:92
void project_plane_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
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
std::optional< math::Quaternion > rake_rotation_symm
void resize(int new_size)
Definition vector.hh:94