Blender V5.0
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
6
7#include "DNA_brush_types.h"
8#include "DNA_mesh_types.h"
9#include "DNA_object_types.h"
10#include "DNA_scene_types.h"
11
12#include "BKE_kelvinlet.h"
13#include "BKE_mesh.hh"
14#include "BKE_paint.hh"
15#include "BKE_paint_bvh.hh"
16#include "BKE_subdiv_ccg.hh"
17
20#include "BLI_math_vector.h"
21#include "BLI_math_vector.hh"
22#include "BLI_task.hh"
23
27
28#include "bmesh.hh"
29
31
32inline namespace snake_hook_cc {
33
40
47
52{
53 spvc->plane = plane;
54 spvc->len_sq = math::length_squared(spvc->plane);
55 spvc->is_valid = (spvc->len_sq > FLT_EPSILON);
56 spvc->len_sq_inv_neg = (spvc->is_valid) ? -1.0f / spvc->len_sq : 0.0f;
57}
58
62static void sculpt_project_v3(const SculptProjectVector *spvc, const float3 &vec, float3 &r_vec)
63{
64#if 0
65 project_plane_v3_v3v3(r_vec, vec, spvc->plane);
66#else
67 /* inline the projection, cache `-1.0 / dot_v3_v3(v_proj, v_proj)` */
68 r_vec += spvc->plane * math::dot(vec, spvc->plane) * spvc->len_sq_inv_neg;
69#endif
70}
71
73 const float3 &sculpt_co,
74 const float3 &v_co,
75 float factor)
76{
77 float3 vec_rot = v_co - sculpt_co;
78 const math::Quaternion rotation = math::pow(*cache.rake_rotation_symm, factor);
79 vec_rot = math::transform_point(rotation, vec_rot);
80
81 vec_rot += sculpt_co;
82 return vec_rot - v_co;
83}
84
85BLI_NOINLINE static void calc_pinch_influence(const Brush &brush,
86 const StrokeCache &cache,
87 const float3 &grab_delta,
88 const SculptProjectVector *spvc,
89 const Span<float3> positions,
90 const Span<float> factors,
91 const MutableSpan<float3> translations)
92{
93 if (brush.crease_pinch_factor == 0.5f) {
94 return;
95 }
96
97 const float pinch = 2.0f * (0.5f - brush.crease_pinch_factor) * math::length(grab_delta) /
98 cache.radius;
99
100 for (const int i : positions.index_range()) {
101 /* Negative pinch will inflate, helps maintain volume. */
102 float3 delta_pinch = positions[i] - cache.location_symm;
103
105 project_plane_v3_v3v3(delta_pinch, delta_pinch, cache.view_normal);
106 }
107
108 /* Important to calculate based on the grabbed location
109 * (intentionally ignore fade here). */
110 delta_pinch += grab_delta;
111
112 sculpt_project_v3(spvc, delta_pinch, delta_pinch);
113
114 float3 delta_pinch_init = delta_pinch;
115
116 float pinch_fade = pinch * factors[i];
117 /* When reducing, scale reduction back by how close to the center we are,
118 * so we don't pinch into nothingness. */
119 if (pinch > 0.0f) {
120 /* Square to have even less impact for close vertices. */
121 pinch_fade *= pow2f(std::min(1.0f, math::length(delta_pinch) / cache.radius));
122 }
123 delta_pinch *= (1.0f + pinch_fade);
124 delta_pinch = delta_pinch_init - delta_pinch;
125 translations[i] += delta_pinch;
126 }
127}
128
130 const Span<float3> positions,
131 const Span<float> factors,
132 const MutableSpan<float3> translations)
133{
134 if (!cache.rake_rotation_symm) {
135 return;
136 }
137 for (const int i : positions.index_range()) {
138 translations[i] += sculpt_rake_rotate(cache, cache.location_symm, positions[i], factors[i]);
139 }
140}
141
143 const Span<float3> positions,
144 const Span<float> factors,
145 const MutableSpan<float3> translations)
146{
148 BKE_kelvinlet_init_params(&params, cache.radius, cache.bstrength, 1.0f, 0.4f);
149 for (const int i : positions.index_range()) {
150 float3 disp;
151 BKE_kelvinlet_grab_triscale(disp, &params, positions[i], cache.location_symm, translations[i]);
152 translations[i] = disp * factors[i];
153 }
154}
155
156static void calc_faces(const Depsgraph &depsgraph,
157 const Sculpt &sd,
158 Object &object,
159 const Brush &brush,
160 const SculptProjectVector *spvc,
161 const float3 &grab_delta,
162 const Span<float3> vert_normals,
163 const MeshAttributeData &attribute_data,
165 LocalData &tls,
166 const PositionDeformData &position_data)
167{
168 SculptSession &ss = *object.sculpt;
169 const StrokeCache &cache = *ss.cache;
170 const bool do_elastic = brush.snake_hook_deform_type == BRUSH_SNAKE_HOOK_DEFORM_ELASTIC;
171
172 const Span<int> verts = node.verts();
173 const MutableSpan positions = gather_data_mesh(position_data.eval, verts, tls.positions);
174
175 tls.factors.resize(verts.size());
176 const MutableSpan<float> factors = tls.factors;
177
178 if (do_elastic) {
179 factors.fill(1.0f);
180 }
181 else {
182 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
183 filter_region_clip_factors(ss, positions, factors);
184 if (brush.flag & BRUSH_FRONTFACE) {
185 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
186 }
187
188 tls.distances.resize(verts.size());
189 const MutableSpan<float> distances = tls.distances;
190 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
191 filter_distances_with_radius(cache.radius, distances, factors);
192 apply_hardness_to_distances(cache, distances);
193 calc_brush_strength_factors(cache, brush, distances, factors);
194
195 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
196 calc_brush_texture_factors(ss, brush, positions, factors);
197 scale_factors(factors, cache.bstrength);
198 }
199
200 tls.translations.resize(verts.size());
201 const MutableSpan<float3> translations = tls.translations;
202
203 translations_from_offset_and_factors(grab_delta, factors, translations);
204
205 calc_pinch_influence(brush, cache, grab_delta, spvc, positions, factors, translations);
206
207 calc_rake_rotation_influence(cache, positions, factors, translations);
208
209 if (do_elastic) {
210 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
211 scale_factors(factors, cache.bstrength * 20.0f);
212 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
213
214 calc_kelvinet_translation(cache, positions, factors, translations);
215 }
216
217 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
218 position_data.deform(translations, verts);
219}
220
221static void calc_grids(const Depsgraph &depsgraph,
222 const Sculpt &sd,
223 Object &object,
224 const Brush &brush,
226 const float3 &grab_delta,
228 LocalData &tls)
229{
230 SculptSession &ss = *object.sculpt;
231 const StrokeCache &cache = *ss.cache;
232 const bool do_elastic = brush.snake_hook_deform_type == BRUSH_SNAKE_HOOK_DEFORM_ELASTIC;
233 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
234
235 const Span<int> grids = node.grids();
236 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
237
238 tls.factors.resize(positions.size());
239 const MutableSpan<float> factors = tls.factors;
240
241 if (do_elastic) {
242 factors.fill(1.0f);
243 }
244 else {
245 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
246 filter_region_clip_factors(ss, positions, factors);
247 if (brush.flag & BRUSH_FRONTFACE) {
248 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
249 }
250
251 tls.distances.resize(positions.size());
252 const MutableSpan<float> distances = tls.distances;
253 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
254 filter_distances_with_radius(cache.radius, distances, factors);
255 apply_hardness_to_distances(cache, distances);
256 calc_brush_strength_factors(cache, brush, distances, factors);
257
259 depsgraph, object, cache.automasking.get(), node, grids, factors);
260 calc_brush_texture_factors(ss, brush, positions, factors);
261 scale_factors(factors, cache.bstrength);
262 }
263
264 tls.translations.resize(positions.size());
265 const MutableSpan<float3> translations = tls.translations;
266
267 translations_from_offset_and_factors(grab_delta, factors, translations);
268
269 calc_pinch_influence(brush, cache, grab_delta, spvc, positions, factors, translations);
270
271 calc_rake_rotation_influence(cache, positions, factors, translations);
272
273 if (do_elastic) {
274 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
275 scale_factors(factors, cache.bstrength * 20.0f);
277 depsgraph, object, cache.automasking.get(), node, grids, factors);
278
279 calc_kelvinet_translation(cache, positions, factors, translations);
280 }
281
282 clip_and_lock_translations(sd, ss, positions, translations);
283 apply_translations(translations, grids, subdiv_ccg);
284}
285
286static void calc_bmesh(const Depsgraph &depsgraph,
287 const Sculpt &sd,
288 Object &object,
289 const Brush &brush,
291 const float3 &grab_delta,
293 LocalData &tls)
294{
295 SculptSession &ss = *object.sculpt;
296 const StrokeCache &cache = *ss.cache;
297 const bool do_elastic = brush.snake_hook_deform_type == BRUSH_SNAKE_HOOK_DEFORM_ELASTIC;
298
300 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
301
302 tls.factors.resize(verts.size());
303 const MutableSpan<float> factors = tls.factors;
304
305 if (do_elastic) {
306 factors.fill(1.0f);
307 }
308 else {
310 filter_region_clip_factors(ss, positions, factors);
311 if (brush.flag & BRUSH_FRONTFACE) {
312 calc_front_face(cache.view_normal_symm, verts, factors);
313 }
314
315 tls.distances.resize(verts.size());
316 const MutableSpan<float> distances = tls.distances;
317 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
318 filter_distances_with_radius(cache.radius, distances, factors);
319 apply_hardness_to_distances(cache, distances);
320 calc_brush_strength_factors(cache, brush, distances, factors);
321
322 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
323 calc_brush_texture_factors(ss, brush, positions, factors);
324 scale_factors(factors, cache.bstrength);
325 }
326
327 tls.translations.resize(verts.size());
328 const MutableSpan<float3> translations = tls.translations;
329
330 translations_from_offset_and_factors(grab_delta, factors, translations);
331
332 calc_pinch_influence(brush, cache, grab_delta, spvc, positions, factors, translations);
333
334 calc_rake_rotation_influence(cache, positions, factors, translations);
335
336 if (do_elastic) {
338 scale_factors(factors, cache.bstrength * 20.0f);
339 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
340
341 calc_kelvinet_translation(cache, positions, factors, translations);
342 }
343
344 clip_and_lock_translations(sd, ss, positions, translations);
345 apply_translations(translations, verts);
346}
347
348} // namespace snake_hook_cc
349
350void do_snake_hook_brush(const Depsgraph &depsgraph,
351 const Sculpt &sd,
352 Object &object,
353 const IndexMask &node_mask)
354{
355 SculptSession &ss = *object.sculpt;
356 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
357 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
358 const float bstrength = ss.cache->bstrength;
359
361
362 float3 grab_delta = ss.cache->grab_delta_symm;
363
364 if (bstrength < 0.0f) {
365 grab_delta *= -1.0f;
366 }
367
368 if (ss.cache->normal_weight > 0.0f) {
370 }
371
372 /* Optionally pinch while painting. */
373 if (brush.crease_pinch_factor != 0.5f) {
374 sculpt_project_v3_cache_init(&spvc, grab_delta);
375 }
376
378 switch (pbvh.type()) {
380 Mesh &mesh = *static_cast<Mesh *>(object.data);
381 const MeshAttributeData attribute_data(mesh);
382 const PositionDeformData position_data(depsgraph, object);
383 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
385 node_mask.foreach_index(GrainSize(1), [&](const int i) {
386 LocalData &tls = all_tls.local();
388 sd,
389 object,
390 brush,
391 &spvc,
392 grab_delta,
393 vert_normals,
394 attribute_data,
395 nodes[i],
396 tls,
397 position_data);
398 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
399 });
400 break;
401 }
403 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
404 MutableSpan<float3> positions = subdiv_ccg.positions;
406 node_mask.foreach_index(GrainSize(1), [&](const int i) {
407 LocalData &tls = all_tls.local();
408 calc_grids(depsgraph, sd, object, brush, &spvc, grab_delta, nodes[i], tls);
409 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
410 });
411 break;
412 }
415 node_mask.foreach_index(GrainSize(1), [&](const int i) {
416 LocalData &tls = all_tls.local();
417 calc_bmesh(depsgraph, sd, object, brush, &spvc, grab_delta, nodes[i], tls);
419 });
420 break;
421 }
422 }
423 pbvh.tag_positions_changed(node_mask);
425}
426
427} // namespace blender::ed::sculpt_paint::brushes
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:90
void BKE_kelvinlet_init_params(KelvinletParams *params, float radius, float force, float shear_modulus, float poisson_ratio)
Definition kelvinlet.cc:16
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:650
A BVH for high poly meshes.
const blender::Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(blender::bke::pbvh::BMeshNode *node)
#define BLI_NOINLINE
MINLINE float pow2f(float x)
void project_plane_v3_v3v3(float out[3], const float p[3], const float v_plane[3])
@ BRUSH_FRONTFACE
@ BRUSH_SNAKE_HOOK_DEFORM_ELASTIC
eBrushFalloffShape
@ PAINT_FALLOFF_SHAPE_TUBE
Object is a sort of wrapper for general info.
BPy_StructRNA * depsgraph
void resize(const int64_t new_size)
constexpr int64_t size() const
Definition BLI_span.hh:493
constexpr void fill(const T &value) const
Definition BLI_span.hh:517
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
void tag_positions_changed(const IndexMask &node_mask)
Definition pbvh.cc:635
Span< NodeT > nodes() const
void flush_bounds_to_parents()
Definition pbvh.cc:1306
void foreach_index(Fn &&fn) const
static float verts[][3]
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
pbvh::Tree * pbvh_get(Object &object)
Definition paint.cc:3052
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1294
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1274
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:1059
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1283
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, Object &object, const Brush &brush, const float3 &direction, const float strength, bke::pbvh::BMeshNode &node, LocalData &tls)
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const float4 &test_plane, const float strength, const MeshAttributeData &attribute_data, const Span< float3 > vert_normals, const bke::pbvh::MeshNode &node, Object &object, LocalData &tls, const PositionDeformData &position_data)
Definition clay.cc:56
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float4 &test_plane, const float strength, bke::pbvh::GridsNode &node, LocalData &tls)
Definition clay.cc:93
static BLI_NOINLINE void fill_factor_from_hide_and_mask(const Mesh &mesh, const Span< int > face_indices, const MutableSpan< float > r_factors)
static float3 sculpt_rake_rotate(const StrokeCache &cache, const float3 &sculpt_co, const float3 &v_co, float factor)
Definition snake_hook.cc:72
static void sculpt_project_v3(const SculptProjectVector *spvc, const float3 &vec, float3 &r_vec)
Definition snake_hook.cc:62
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, SculptProjectVector *spvc, const float3 &grab_delta, bke::pbvh::BMeshNode &node, LocalData &tls)
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:85
static void sculpt_project_v3_cache_init(SculptProjectVector *spvc, const float3 &plane)
Definition snake_hook.cc:51
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const SculptProjectVector *spvc, const float3 &grab_delta, const Span< float3 > vert_normals, const MeshAttributeData &attribute_data, bke::pbvh::MeshNode &node, LocalData &tls, const PositionDeformData &position_data)
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)
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, SculptProjectVector *spvc, const float3 &grab_delta, bke::pbvh::GridsNode &node, LocalData &tls)
void do_snake_hook_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_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:6367
void calc_brush_strength_factors(const StrokeCache &cache, const Brush &brush, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7183
void apply_hardness_to_distances(float radius, float hardness, MutableSpan< float > distances)
Definition sculpt.cc:7156
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7105
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:6972
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7512
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7335
void calc_brush_distances(const SculptSession &ss, Span< float3 > vert_positions, Span< int > vert, eBrushFalloffShape falloff_shape, MutableSpan< float > r_distances)
Definition sculpt.cc:7055
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7268
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
Definition sculpt.cc:6395
void calc_brush_texture_factors(const SculptSession &ss, const Brush &brush, Span< float3 > vert_positions, Span< int > vert, MutableSpan< float > factors)
Definition sculpt.cc:7195
void calc_front_face(const float3 &view_normal, Span< float3 > normals, MutableSpan< float > factors)
Definition sculpt.cc:6914
void translations_from_offset_and_factors(const float3 &offset, Span< float > factors, MutableSpan< float3 > r_translations)
Definition sculpt.cc:7531
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)
VecBase< float, 3 > float3
void sculpt_project_v3_normal_align(const SculptSession &ss, const float normal_weight, float grab_delta[3])
Definition sculpt.cc:567
int snake_hook_deform_type
char falloff_shape
float crease_pinch_factor
blender::ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:417
SubdivCCG * subdiv_ccg
Definition BKE_paint.hh:395
blender::Array< blender::float3 > positions
std::unique_ptr< auto_mask::Cache > automasking
std::optional< math::Quaternion > rake_rotation_symm
i
Definition text_draw.cc:230