Blender V5.3
plane.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
20
22
23#include "DNA_brush_types.h"
24#include "DNA_mesh_types.h"
25#include "DNA_object_types.h"
26#include "DNA_scene_types.h"
27
28#include "BKE_brush.hh"
29#include "BKE_mesh.hh"
30#include "BKE_object_types.hh"
31#include "BKE_paint.hh"
32#include "BKE_paint_bvh.hh"
33#include "BKE_subdiv_ccg.hh"
34
36#include "BLI_math_matrix.hh"
37#include "BLI_task.hh"
38
42
43#include "bmesh.hh"
44
46
47inline namespace plane_cc {
48
56
57static void calc_local_positions(const float4x4 &mat,
58 const Span<int> verts,
59 const Span<float3> positions,
60 const MutableSpan<float3> local_positions)
61{
63 for (const int i : verts.index_range()) {
64 local_positions[i] = math::transform_point(mat, positions[verts[i]]);
65 }
66}
67
80static void calc_local_distances(const float height,
81 const float depth,
82 const MutableSpan<float3> local_positions,
83 const MutableSpan<float> distances)
84{
86 if (height != 0.0f) {
87 const float height_rcp = math::rcp(height);
88
89 for (const int i : local_positions.index_range()) {
90 const float3 &position = local_positions[i];
91 if (position.z >= 0.0f) {
92 distances[i] = math::length(float3(position.x, position.y, position.z * height_rcp));
93 }
94 }
95 }
96 else {
97 for (const int i : local_positions.index_range()) {
98 if (local_positions[i].z >= 0.0f) {
99 distances[i] = 1.0f;
100 }
101 }
102 }
103
104 if (depth != 0.0f) {
105 const float depth_rcp = math::rcp(depth);
106
107 for (const int i : local_positions.index_range()) {
108 const float3 &position = local_positions[i];
109 if (position.z < 0.0f) {
110 distances[i] = math::length(float3(position.x, position.y, position.z * depth_rcp));
111 }
112 }
113 }
114 else {
115 for (const int i : local_positions.index_range()) {
116 if (local_positions[i].z < 0.0f) {
117 distances[i] = 1.0f;
118 }
119 }
120 }
121}
122
123/*
124 * Scales factors by `height` (if local z > 0) or `depth` (if local z < 0).
125 * This is necessary to "normalize" the strength contribute given by the local z distance.
126 *
127 * Note that if `height = 0`, the falloff strength of the vertices
128 * above the plane is 0 (see #calc_local_distances), hence
129 * the factor for such vertices is already 0.
130 *
131 * The same is true for vertices below the plane if `depth` = 0.
132 */
133static void scale_factors_by_height_and_depth(const float height,
134 const float depth,
135 const MutableSpan<float3> local_positions,
136 const MutableSpan<float> factors)
137{
139 if (!ELEM(height, 1.0f, 0.0f)) {
140 for (const int i : factors.index_range()) {
141 if (local_positions[i].z > 0.0f) {
142 factors[i] *= height;
143 }
144 }
145 }
146
147 if (!ELEM(depth, 0.0f, 1.0f)) {
148 for (const int i : factors.index_range()) {
149 if (local_positions[i].z < 0.0f) {
150 factors[i] *= depth;
151 }
152 }
153 }
154}
155
156/*
157 * Computes the translation vectors for the Plane brush.
158 *
159 * The translation of a vertex with index `i`, position `P`, and plane projection `Q`
160 * is determined by the vector `PQ` scaled by `factors[i]` and `strength`.
161 *
162 * In local (brush) space, `PQ` is given by:
163 * `PQ = Q - P = (x, y, 0) - (x, y, z) = (0, 0, -z)`
164 *
165 * Therefore, the translation in object space is:
166 * `T = A * (0, 0, -z) * factors[i] * strength`
167 *
168 * where `A` is the 3x3 local-to-object transformation matrix.
169 *
170 * Given how the local space is defined, `A * (0, 0, -z)` simplifies to:
171 * `-z * radius * plane_normal`
172 *
173 * Substituting this back, the translation becomes:
174 * `T = -z * radius * plane_normal * factors[i] * strength`
175 * which is equal to:
176 * `(factors[i] * z) * (-plane_normal * radius * strength)`
177 */
178static void calc_translations(const float3 &plane_normal,
179 const float radius,
180 const float strength,
181 const MutableSpan<float3> local_positions,
182 const MutableSpan<float> factors,
183 const MutableSpan<float3> r_translations)
184{
186 for (const int i : local_positions.index_range()) {
187 factors[i] *= local_positions[i].z;
188 }
189
190 const float3 &offset = -plane_normal * radius * strength;
191 translations_from_offset_and_factors(offset, factors, r_translations);
192}
193
194static void calc_faces(const Depsgraph &depsgraph,
195 const Sculpt &sd,
196 const Brush &brush,
197 const float4x4 &mat,
198 const float3 &plane_normal,
199 const float strength,
200 const float height,
201 const float depth,
202 const MeshAttributeData &attribute_data,
203 const Span<float3> vert_normals,
205 Object &object,
206 LocalData &tls,
207 const PositionDeformData &position_data)
208{
209 const SculptSession &ss = *object.runtime->sculpt_session;
210 const StrokeCache &cache = *ss.cache;
211
212 const Span<int> verts = node.verts();
213
214 tls.factors.resize(verts.size());
215 const MutableSpan<float> factors = tls.factors;
216 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
217 filter_region_clip_factors(ss, position_data.eval, verts, factors);
218
219 if (brush.flag & BRUSH_FRONTFACE) {
220 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
221 }
222
223 tls.positions.resize(verts.size());
224 const MutableSpan<float3> local_positions = tls.positions;
225 calc_local_positions(mat, verts, position_data.eval, local_positions);
226
227 tls.distances.resize(verts.size());
228 const MutableSpan<float> distances = tls.distances;
229 calc_local_distances(height, depth, local_positions, distances);
230 filter_distances_with_radius(1.0f, distances, factors);
231
232 apply_hardness_to_distances(1.0f, cache.hardness, distances);
233 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
234 brush.curve_distance_falloff,
235 distances,
236 1.0f,
237 factors);
238
239 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
240
241 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, position_data.eval, verts, factors);
242
243 tls.translations.resize(verts.size());
244 const MutableSpan<float3> translations = tls.translations;
245 scale_factors_by_height_and_depth(height, depth, local_positions, factors);
246 calc_translations(plane_normal, cache.radius, strength, local_positions, factors, translations);
247
248 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
249 position_data.deform(translations, verts);
250}
251
252static void calc_grids(const Depsgraph &depsgraph,
253 const Sculpt &sd,
254 Object &object,
255 const Brush &brush,
256 const float4x4 &mat,
257 const float3 &plane_normal,
258 const float strength,
259 const float height,
260 const float depth,
262 LocalData &tls)
263{
264 SculptSession &ss = *object.runtime->sculpt_session;
265 const StrokeCache &cache = *ss.cache;
266 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
267
268 const Span<int> grids = node.grids();
269 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
270
271 tls.factors.resize(positions.size());
272 const MutableSpan<float> factors = tls.factors;
273 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
274 filter_region_clip_factors(ss, positions, factors);
275
276 if (brush.flag & BRUSH_FRONTFACE) {
277 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
278 }
279
280 tls.local_positions.resize(positions.size());
281 const MutableSpan<float3> local_positions = tls.local_positions;
282 math::transform_points(positions, mat, local_positions, false);
283
284 tls.distances.resize(positions.size());
285 const MutableSpan<float> distances = tls.distances;
286 calc_local_distances(height, depth, local_positions, distances);
287 filter_distances_with_radius(1.0f, distances, factors);
288
289 apply_hardness_to_distances(1.0f, cache.hardness, distances);
290 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
291 brush.curve_distance_falloff,
292 distances,
293 1.0f,
294 factors);
295
296 auto_mask::calc_grids_factors(depsgraph, object, cache.automasking.get(), node, grids, factors);
297
298 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
299
300 tls.translations.resize(positions.size());
301 const MutableSpan<float3> translations = tls.translations;
302 scale_factors_by_height_and_depth(height, depth, local_positions, factors);
303 calc_translations(plane_normal, cache.radius, strength, local_positions, factors, translations);
304
305 clip_and_lock_translations(sd, ss, positions, translations);
306 apply_translations(translations, grids, subdiv_ccg);
307}
308
309static void calc_bmesh(const Depsgraph &depsgraph,
310 const Sculpt &sd,
311 Object &object,
312 const Brush &brush,
313 const float4x4 &mat,
314 const float3 &plane_normal,
315 const float strength,
316 const float height,
317 const float depth,
319 LocalData &tls)
320{
321 SculptSession &ss = *object.runtime->sculpt_session;
322 const StrokeCache &cache = *ss.cache;
323
325 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
326
327 tls.factors.resize(verts.size());
328 const MutableSpan<float> factors = tls.factors;
329 fill_factor_from_hide_and_mask(*ss.bm, verts, factors);
330 filter_region_clip_factors(ss, positions, factors);
331 if (brush.flag & BRUSH_FRONTFACE) {
332 calc_front_face(cache.view_normal_symm, verts, factors);
333 }
334
335 tls.local_positions.resize(positions.size());
336 const MutableSpan<float3> local_positions = tls.local_positions;
337 math::transform_points(positions, mat, local_positions, false);
338
339 tls.distances.resize(positions.size());
340 const MutableSpan<float> distances = tls.distances;
341 calc_local_distances(height, depth, local_positions, distances);
342 filter_distances_with_radius(1.0f, distances, factors);
343
344 apply_hardness_to_distances(1.0f, cache.hardness, distances);
345 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
346 brush.curve_distance_falloff,
347 distances,
348 1.0f,
349 factors);
350
351 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
352
353 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, positions, factors);
354
355 tls.translations.resize(positions.size());
356 const MutableSpan<float3> translations = tls.translations;
357 scale_factors_by_height_and_depth(height, depth, local_positions, factors);
358 calc_translations(plane_normal, cache.radius, strength, local_positions, factors, translations);
359
360 clip_and_lock_translations(sd, ss, positions, translations);
361 apply_translations(translations, verts);
362}
363
364} // namespace plane_cc
365
366void do_plane_brush(const Depsgraph &depsgraph,
367 const Sculpt &sd,
368 Object &object,
369 const IndexMask &node_mask,
370 const float3 &plane_normal,
371 const float3 &plane_center)
372{
374 const SculptSession &ss = *object.runtime->sculpt_session;
377
379 return;
380 }
381
382 float3 normal = plane_normal;
383 float3 center = plane_center;
384
385 normal = tilt_apply_to_normal(normal, *ss.cache, brush.tilt_strength_factor);
386
387 const bool flip = ss.cache->initial_direction_flipped;
388 const float offset = brush_plane_offset_get(brush, ss);
389 const float displace = ss.cache->radius * offset * (flip ? -1.0f : 1.0f);
390 center += normal * ss.cache->scale * displace;
391
393 mat.x_axis() = math::cross(normal, ss.cache->grab_delta_symm);
394 mat.y_axis() = math::cross(normal, mat.x_axis());
395 mat.z_axis() = normal;
396 mat.location() = center;
397 mat = math::normalize(mat);
398
400 float4x4 tmat = mat * scale;
401
402 mat = math::invert(tmat);
403
404 float strength = ss.cache->bstrength;
405 float height = brush.plane_height;
406 float depth = brush.plane_depth;
407
408 if (flip) {
409 switch (brush.plane_inversion_mode) {
411 strength *= -1.0f;
412 break;
413 }
415 std::swap(height, depth);
416 break;
417 }
418 }
419 }
420
422 switch (pbvh.type()) {
424 const Mesh &mesh = *id_cast<Mesh *>(object.data);
425 const MeshAttributeData attribute_data(mesh);
426 const PositionDeformData position_data(depsgraph, object);
427 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
429 node_mask.foreach_index(
430 [&](const int i) {
431 LocalData &tls = all_tls.local();
432 calc_faces(depsgraph,
433 sd,
434 brush,
435 mat,
436 normal,
437 strength,
438 height,
439 depth,
440 attribute_data,
441 vert_normals,
442 nodes[i],
443 object,
444 tls,
445 position_data);
446 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
447 },
449 break;
450 }
452 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
453 MutableSpan<float3> positions = subdiv_ccg.positions;
455 node_mask.foreach_index(
456 [&](const int i) {
457 LocalData &tls = all_tls.local();
459 depsgraph, sd, object, brush, mat, normal, strength, height, depth, nodes[i], tls);
460 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
461 },
463 break;
464 }
467 node_mask.foreach_index(
468 [&](const int i) {
469 LocalData &tls = all_tls.local();
471 depsgraph, sd, object, brush, mat, normal, strength, height, depth, nodes[i], tls);
473 },
475 break;
476 }
477 }
478 pbvh.tag_positions_changed(node_mask);
479 pbvh.flush_bounds_to_parents();
480}
481
482namespace plane {
483CursorSampleResult calc_node_mask(const Depsgraph &depsgraph,
484 Object &ob,
485 const Brush &brush,
486 IndexMaskMemory &memory)
487{
488 const SculptSession &ss = *ob.runtime->sculpt_session;
490
491 const bool use_original = !ss.cache->accum;
492 const IndexMask initial_node_mask = gather_nodes(pbvh,
493 eBrushFalloffShape(brush.falloff_shape),
494 use_original,
498 memory);
499
500 float3 plane_center;
501 float3 plane_normal;
502 calc_brush_plane(depsgraph, brush, ob, initial_node_mask, plane_normal, plane_center);
503
504 /* Recompute the node mask using the center of the brush plane as the center.
505 *
506 * The indices of the nodes in `cursor_node_mask` have been calculated based on the cursor
507 * location. However, for the Plane brush, its effective center often deviates from the cursor
508 * location. Calculating the affected nodes using the cursor location as the center can lead to
509 * issues (see, for example, #123768). */
510 const IndexMask plane_mask = bke::pbvh::search_nodes(
511 pbvh, memory, [&](const bke::pbvh::Node &node) {
513 return false;
514 }
515 return node_in_sphere(node, plane_center, ss.cache->radius_squared, use_original);
516 });
517
518 return {plane_mask, plane_center, plane_normal};
519}
520} // namespace plane
521} // 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 ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
constexpr int64_t size() const
Definition BLI_span.hh:499
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
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define position
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 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 BLI_NOINLINE void calc_translations(const Span< float3 > orig_positions, const Span< float3 > orig_normals, const Span< float3 > positions, const Span< float > displacement_factors, const Span< float > factors, const float height, const MutableSpan< float3 > r_translations)
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 void scale_factors_by_height_and_depth(const float height, const float depth, const MutableSpan< float3 > local_positions, const MutableSpan< float > factors)
Definition plane.cc:133
static void calc_local_distances(const float height, const float depth, const MutableSpan< float3 > local_positions, const MutableSpan< float > distances)
Definition plane.cc:80
CursorSampleResult calc_node_mask(const Depsgraph &depsgraph, Object &ob, const Brush &brush, IndexMaskMemory &memory)
Definition plane.cc:483
void do_plane_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask, const float3 &plane_normal, const float3 &plane_center)
Definition plane.cc:366
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)
bool node_in_sphere(const bke::pbvh::Node &node, const float3 &location, const float radius_sq, const bool original)
Definition sculpt.cc:2511
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
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
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_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)
T length(const VecBase< T, Size > &a)
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)
T rcp(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
void transform_points(const float4x4 &transform, MutableSpan< float3 > points, bool use_threading=true)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
@ BRUSH_PLANE_INVERT_DISPLACEMENT
@ BRUSH_PLANE_SWAP_HEIGHT_AND_DEPTH
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)
bke::ObjectRuntime * runtime
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