Blender V5.3
multiplane_scrape.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
12#include "BLI_math_geom_c.hh"
13#include "BLI_math_matrix.hh"
14#include "BLI_math_matrix_c.hh"
17#include "BLI_span.hh"
18
19#include "DNA_brush_types.h"
20#include "DNA_object_types.h"
21
22#include "BKE_brush.hh"
23#include "BKE_mesh.hh"
24#include "BKE_object_types.hh"
25#include "BKE_paint.hh"
26#include "BKE_paint_bvh.hh"
27#include "BKE_subdiv_ccg.hh"
28
29#include "GPU_immediate.hh"
30#include "GPU_matrix.hh"
31
32#include "bmesh.hh"
33
34#include <cmath>
35#include <cstdlib>
36
40
42
44 std::array<float3, 2> area_cos;
45 std::array<float3, 2> area_nos;
46 std::array<int, 2> area_count;
47
48 bool has_samples() const
49 {
50 return area_count[0] != 0 && area_count[1] != 0;
51 }
52};
53
62
64{
65 ScrapeSampleData joined = a;
66
67 joined.area_cos[0] = a.area_cos[0] + b.area_cos[0];
68 joined.area_cos[1] = a.area_cos[1] + b.area_cos[1];
69
70 joined.area_nos[0] = a.area_nos[0] + b.area_nos[0];
71 joined.area_nos[1] = a.area_nos[1] + b.area_nos[1];
72
73 joined.area_count[0] = a.area_count[0] + b.area_count[0];
74 joined.area_count[1] = a.area_count[1] + b.area_count[1];
75 return joined;
76}
77
79 const Span<float3> local_positions,
80 const std::array<float4, 2> &scrape_planes,
81 const MutableSpan<float> factors)
82{
84 BLI_assert(positions.size() == local_positions.size());
85 BLI_assert(positions.size() == factors.size());
86
87 for (const int i : local_positions.index_range()) {
88 const bool plane_index = local_positions[i][0] <= 0.0f;
89 if (plane_point_side_v3(scrape_planes[plane_index], positions[i]) <= 0.0f) {
90 factors[i] = 0.0f;
91 }
92 }
93}
94
95BLI_NOINLINE static void calc_distances(const Span<float3> local_positions,
96 const MutableSpan<float> distances)
97{
99 BLI_assert(local_positions.size() == distances.size());
100
101 for (const int i : local_positions.index_range()) {
102 /* Deform the local space along the Y axis to avoid artifacts on curved strokes. */
103 /* This produces a not round brush tip. */
104 float3 local = local_positions[i];
105 local[1] *= 2.0f;
106 distances[i] = math::length(local);
107 }
108}
109
110BLI_NOINLINE static void calc_translations(const Span<float3> positions,
111 const Span<float3> local_positions,
112 const std::array<float4, 2> &scrape_planes,
113 const MutableSpan<float3> translations)
114{
116 for (const int i : positions.index_range()) {
117 const bool plane_index = local_positions[i][0] <= 0.0f;
119 closest_to_plane_normalized_v3(closest, scrape_planes[plane_index], positions[i]);
120 translations[i] = closest - positions[i];
121 }
122}
123
124BLI_NOINLINE static void accumulate_samples(const Span<float3> positions,
125 const Span<float3> local_positions,
126 const Span<float3> normals,
127 const Span<float> factors,
129{
131 for (const int i : positions.index_range()) {
132 if (factors[i] <= 0.0f) {
133 continue;
134 }
135 const bool plane_index = local_positions[i].x <= 0.0f;
136 sample.area_nos[plane_index] += normals[i] * factors[i];
137 sample.area_cos[plane_index] += positions[i];
138 sample.area_count[plane_index]++;
139 }
140}
141
142static void sample_node_surface_mesh(const Depsgraph &depsgraph,
143 const Object &object,
144 const Brush &brush,
145 const float4x4 &mat,
146 const Span<float3> vert_positions,
147 const Span<float3> vert_normals,
148 const MeshAttributeData &attribute_data,
151{
153 const SculptSession &ss = *object.runtime->sculpt_session;
154 const StrokeCache &cache = *ss.cache;
155
156 const Span<int> verts = node.verts();
158 gather_data_mesh(vert_positions, verts, positions.as_mutable_span());
159
161 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
162 filter_region_clip_factors(ss, positions, factors);
163 if (brush.flag & BRUSH_FRONTFACE) {
164 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
165 }
166 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
167
168 const float radius = cache.radius * brush.normal_radius_factor;
169
171 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
172 filter_distances_with_radius(radius, distances, factors);
173 apply_hardness_to_distances(radius, cache.hardness, distances);
174 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
175 brush.curve_distance_falloff,
176 distances,
177 radius,
178 factors);
179
180 Array<float3, bke::pbvh::MESH_LEAF_LIMIT> local_positions(verts.size());
181 math::transform_points(positions, mat, local_positions, false);
182
184 gather_data_mesh(vert_normals, verts, normals.as_mutable_span());
185
186 accumulate_samples(positions, local_positions, normals, factors, sample);
187}
188
189static void sample_node_surface_grids(const Depsgraph &depsgraph,
190 const Object &object,
191 const Brush &brush,
192 const float4x4 &mat,
195 LocalData &tls)
196{
197 SculptSession &ss = *object.runtime->sculpt_session;
198 const StrokeCache &cache = *ss.cache;
199 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
200
201 const Span<int> grids = node.grids();
202 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
203
204 tls.factors.resize(positions.size());
205 const MutableSpan<float> factors = tls.factors;
206 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
207 filter_region_clip_factors(ss, positions, factors);
208 if (brush.flag & BRUSH_FRONTFACE) {
209 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
210 }
211 auto_mask::calc_grids_factors(depsgraph, object, cache.automasking.get(), node, grids, factors);
212
213 const float radius = cache.radius * brush.normal_radius_factor;
214
215 tls.distances.resize(positions.size());
216 const MutableSpan<float> distances = tls.distances;
217 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
218 filter_distances_with_radius(radius, distances, factors);
219 apply_hardness_to_distances(radius, cache.hardness, distances);
220 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
221 brush.curve_distance_falloff,
222 distances,
223 radius,
224 factors);
225
226 tls.local_positions.resize(positions.size());
227 MutableSpan<float3> local_positions = tls.local_positions;
228 math::transform_points(positions, mat, local_positions, false);
229
230 tls.normals.resize(positions.size());
231 MutableSpan<float3> normals = tls.normals;
232 gather_grids_normals(subdiv_ccg, grids, normals);
233
234 accumulate_samples(positions, local_positions, normals, factors, sample);
235}
236
237static void sample_node_surface_bmesh(const Depsgraph &depsgraph,
238 const Object &object,
239 const Brush &brush,
240 const float4x4 &mat,
243 LocalData &tls)
244{
245 const SculptSession &ss = *object.runtime->sculpt_session;
246 const StrokeCache &cache = *ss.cache;
247
249 const_cast<bke::pbvh::BMeshNode *>(&node));
250
251 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
252
253 tls.factors.resize(verts.size());
254 const MutableSpan<float> factors = tls.factors;
255 fill_factor_from_hide_and_mask(*ss.bm, verts, factors);
256 filter_region_clip_factors(ss, positions, factors);
257 if (brush.flag & BRUSH_FRONTFACE) {
258 calc_front_face(cache.view_normal_symm, verts, factors);
259 }
260 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
261
262 const float radius = cache.radius * brush.normal_radius_factor;
263
264 tls.distances.resize(verts.size());
265 const MutableSpan<float> distances = tls.distances;
266 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
267 filter_distances_with_radius(radius, distances, factors);
268 apply_hardness_to_distances(radius, cache.hardness, distances);
269 BKE_brush_calc_curve_factors(eBrushCurvePreset(brush.curve_distance_falloff_preset),
270 brush.curve_distance_falloff,
271 distances,
272 radius,
273 factors);
274
275 tls.local_positions.resize(verts.size());
276 MutableSpan<float3> local_positions = tls.local_positions;
277 math::transform_points(positions, mat, local_positions, false);
278
279 tls.normals.resize(verts.size());
280 MutableSpan<float3> normals = tls.normals;
281 gather_bmesh_normals(verts, normals);
282
283 accumulate_samples(positions, local_positions, normals, factors, sample);
284}
285
292static std::optional<ScrapeSampleData> sample_surface(const Depsgraph &depsgraph,
293 const Object &object,
294 const Brush &brush,
295 const float4x4 &mat,
296 const IndexMask &node_mask)
297{
302 switch (pbvh.type()) {
304 Mesh &mesh = *id_cast<Mesh *>(object.data);
305 const MeshAttributeData attribute_data(mesh);
307 const Span<float3> positions_eval = bke::pbvh::vert_positions_eval(depsgraph, object);
308 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
310 node_mask.index_range(),
311 1,
313 [&](const IndexRange range, ScrapeSampleData sample) {
314 node_mask.slice(range).foreach_index([&](const int i) {
315 sample_node_surface_mesh(depsgraph,
316 object,
317 brush,
318 mat,
319 positions_eval,
320 vert_normals,
321 attribute_data,
322 nodes[i],
323 sample);
324 });
325 return sample;
326 },
328 break;
329 }
333 node_mask.index_range(),
334 1,
336 [&](const IndexRange range, ScrapeSampleData sample) {
337 LocalData &tls = all_tls.local();
338 node_mask.slice(range).foreach_index([&](const int i) {
339 sample_node_surface_grids(depsgraph, object, brush, mat, nodes[i], sample, tls);
340 });
341 return sample;
342 },
344 break;
345 }
349 node_mask.index_range(),
350 1,
351 ScrapeSampleData{},
352 [&](const IndexRange range, ScrapeSampleData sample) {
353 LocalData &tls = all_tls.local();
354 node_mask.slice(range).foreach_index([&](const int i) {
355 sample_node_surface_bmesh(depsgraph, object, brush, mat, nodes[i], sample, tls);
356 });
357 return sample;
358 },
360 break;
361 }
362 }
363
364 return result.has_samples() ? std::make_optional(result) : std::nullopt;
365}
366
367static void calc_faces(const Depsgraph &depsgraph,
368 const Sculpt &sd,
369 const Brush &brush,
370 const float4x4 &mat,
371 const std::array<float4, 2> &scrape_planes,
372 const float angle,
373 const float strength,
374 const Span<float3> vert_normals,
375 const MeshAttributeData &attribute_data,
377 Object &object,
378 const PositionDeformData &position_data)
379{
380 SculptSession &ss = *object.runtime->sculpt_session;
381 const StrokeCache &cache = *ss.cache;
382
383 const Span<int> verts = node.verts();
385 gather_data_mesh<float3>(position_data.eval, verts, positions);
386
388 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
389 filter_region_clip_factors(ss, positions, factors);
390 if (brush.flag & BRUSH_FRONTFACE) {
391 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
392 }
393 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
394
396 /* NOTE: The distances are not used from this call, it's only used for filtering. */
397 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
398 filter_distances_with_radius(cache.radius, distances, factors);
399
400 Array<float3, bke::pbvh::MESH_LEAF_LIMIT> local_positions(verts.size());
401 math::transform_points(positions, mat, local_positions, false);
402
403 if (angle >= 0.0f) {
404 filter_plane_side_factors(positions, local_positions, scrape_planes, factors);
405 }
406
407 calc_distances(local_positions, distances);
408 /* TODO: Using the radius for the filter here is probably too high, but due to the Y-axis
409 * deformation, a simple value of 1.0 isn't correct. */
410 filter_distances_with_radius(cache.radius, distances, factors);
411
412 apply_hardness_to_distances(cache, distances);
413 calc_brush_strength_factors(cache, brush, distances, factors);
414
416 calc_translations(positions, local_positions, scrape_planes, translations);
417
418 filter_plane_trim_limit_factors(brush, cache, translations, factors);
419
420 scale_factors(factors, strength);
421 scale_translations(translations, factors);
422
423 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
424 position_data.deform(translations, verts);
425}
426
427static void calc_grids(const Depsgraph &depsgraph,
428 const Sculpt &sd,
429 const Brush &brush,
430 const float4x4 &mat,
431 const std::array<float4, 2> &scrape_planes,
432 const float angle,
433 const float strength,
435 Object &object,
436 LocalData &tls)
437{
438 SculptSession &ss = *object.runtime->sculpt_session;
439 const StrokeCache &cache = *ss.cache;
440 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
441
442 const Span<int> grids = node.grids();
443 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
444
445 tls.factors.resize(positions.size());
446 const MutableSpan<float> factors = tls.factors;
447 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
448 filter_region_clip_factors(ss, positions, factors);
449 if (brush.flag & BRUSH_FRONTFACE) {
450 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
451 }
452 auto_mask::calc_grids_factors(depsgraph, object, cache.automasking.get(), node, grids, factors);
453
454 tls.distances.resize(positions.size());
455 const MutableSpan<float> distances = tls.distances;
456 /* NOTE: The distances are not used from this call, it's only used for filtering. */
457 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
458 filter_distances_with_radius(cache.radius, distances, factors);
459
460 tls.local_positions.resize(positions.size());
461 MutableSpan<float3> local_positions = tls.local_positions;
462 math::transform_points(positions, mat, local_positions, false);
463
464 if (angle >= 0.0f) {
465 filter_plane_side_factors(positions, local_positions, scrape_planes, factors);
466 }
467
468 calc_distances(local_positions, distances);
469 /* TODO: Using the radius for the filter here is probably too high, but due to the Y-axis
470 * deformation, a simple value of 1.0 isn't correct. */
471 filter_distances_with_radius(cache.radius, distances, factors);
472
473 apply_hardness_to_distances(cache, distances);
474 calc_brush_strength_factors(cache, brush, distances, factors);
475
476 tls.translations.resize(positions.size());
477 MutableSpan<float3> translations = tls.translations;
478 calc_translations(positions, local_positions, scrape_planes, translations);
479
480 filter_plane_trim_limit_factors(brush, cache, translations, factors);
481
482 scale_factors(factors, strength);
483 scale_translations(translations, factors);
484
485 clip_and_lock_translations(sd, ss, positions, translations);
486 apply_translations(translations, grids, subdiv_ccg);
487}
488
489static void calc_bmesh(const Depsgraph &depsgraph,
490 const Sculpt &sd,
491 const Brush &brush,
492 const float4x4 &mat,
493 const std::array<float4, 2> &scrape_planes,
494 const float angle,
495 const float strength,
497 Object &object,
498 LocalData &tls)
499{
500 SculptSession &ss = *object.runtime->sculpt_session;
501 const StrokeCache &cache = *ss.cache;
502
504 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
505
506 tls.factors.resize(verts.size());
507 const MutableSpan<float> factors = tls.factors;
508 fill_factor_from_hide_and_mask(*ss.bm, verts, factors);
509 filter_region_clip_factors(ss, positions, factors);
510 if (brush.flag & BRUSH_FRONTFACE) {
511 calc_front_face(cache.view_normal_symm, verts, factors);
512 }
513 auto_mask::calc_vert_factors(depsgraph, object, cache.automasking.get(), node, verts, factors);
514
515 tls.distances.resize(verts.size());
516 const MutableSpan<float> distances = tls.distances;
517 /* NOTE: The distances are not used from this call, it's only used for filtering. */
518 calc_brush_distances(ss, positions, eBrushFalloffShape(brush.falloff_shape), distances);
519 filter_distances_with_radius(cache.radius, distances, factors);
520
521 tls.local_positions.resize(verts.size());
522 MutableSpan<float3> local_positions = tls.local_positions;
523 math::transform_points(positions, mat, local_positions, false);
524
525 if (angle >= 0.0f) {
526 filter_plane_side_factors(positions, local_positions, scrape_planes, factors);
527 }
528
529 calc_distances(local_positions, distances);
530 /* TODO: Using the radius for the filter here is probably too high, but due to the Y-axis
531 * deformation, a simple value of 1.0 isn't correct. */
532 filter_distances_with_radius(cache.radius, distances, factors);
533
534 apply_hardness_to_distances(cache, distances);
535 calc_brush_strength_factors(cache, brush, distances, factors);
536
537 tls.translations.resize(verts.size());
538 MutableSpan<float3> translations = tls.translations;
539 calc_translations(positions, local_positions, scrape_planes, translations);
540
541 filter_plane_trim_limit_factors(brush, cache, translations, factors);
542
543 scale_factors(factors, strength);
544 scale_translations(translations, factors);
545
546 clip_and_lock_translations(sd, ss, positions, translations);
547 apply_translations(translations, verts);
548}
549
550void do_multiplane_scrape_brush(const Depsgraph &depsgraph,
551 const Sculpt &sd,
552 Object &object,
553 const IndexMask &node_mask)
554{
556 SculptSession &ss = *object.runtime->sculpt_session;
559
560 const bool flip = (ss.cache->bstrength < 0.0f);
561 const float radius = flip ? -ss.cache->radius : ss.cache->radius;
562 const float offset = brush_plane_offset_get(brush, ss);
563 const float displace = -radius * offset;
564
565 float3 sculpt_plane_normal;
566 float3 area_position;
567 calc_brush_plane(depsgraph, brush, object, node_mask, sculpt_plane_normal, area_position);
568
569 float3 area_normal = sculpt_plane_normal;
570 /* Ignore brush settings and recalculate the area normal. */
571 if (brush.sculpt_plane != SCULPT_DISP_DIR_AREA || (brush.flag & BRUSH_ORIGINAL_NORMAL)) {
572 area_normal = calc_area_normal(depsgraph, brush, object, node_mask).value_or(float3(0));
573 }
574
575 /* Delay the first daub because grab delta is not setup. */
578 return;
579 }
580
582 return;
583 }
584
585 area_position += area_normal * ss.cache->scale * displace;
586
587 /* Init brush local space matrix. */
589 mat.x_axis() = math::cross(area_normal, ss.cache->grab_delta_symm);
590 mat.y_axis() = math::cross(area_normal, mat.x_axis());
591 mat.z_axis() = area_normal;
592 mat.location() = ss.cache->location_symm;
593 /* NOTE: #math::normalize behaves differently for some reason. */
594 normalize_m4(mat.ptr());
595 mat = math::invert(mat);
596
597 /* Update matrix for the cursor preview. */
598 if (ss.cache->mirror_symmetry_pass == 0 && ss.cache->radial_symmetry_pass == 0) {
599 ss.cache->stroke_local_mat = mat;
600 }
601
602 /* Dynamic mode. */
603
605 /* Sample the individual normal and area center of the areas at both sides of the cursor. */
606 const std::optional<ScrapeSampleData> sample = sample_surface(
607 depsgraph, object, brush, mat, node_mask);
608 if (!sample) {
609 return;
610 }
611
612 BLI_assert(sample->has_samples());
613
614 /* Use the plane centers to check if we are sculpting along a concave or convex edge. */
615 const std::array<float3, 2> sampled_plane_co{
616 sample->area_cos[0] * 1.0f / float(sample->area_count[0]),
617 sample->area_cos[1] * 1.0f / float(sample->area_count[1])};
618 const float3 mid_co = math::midpoint(sampled_plane_co[0], sampled_plane_co[1]);
619
620 /* Calculate the scrape planes angle based on the sampled normals. */
621 const std::array<float3, 2> sampled_plane_normals{
622 math::normalize(sample->area_nos[0] * 1.0f / float(sample->area_count[0])),
623 math::normalize(sample->area_nos[1] * 1.0f / float(sample->area_count[1]))};
624
625 float sampled_angle = angle_v3v3(sampled_plane_normals[0], sampled_plane_normals[1]);
626 const std::array<float3, 2> sampled_cv{area_normal, ss.cache->location_symm - mid_co};
627
628 sampled_angle += DEG2RADF(brush.multiplane_scrape_angle) * ss.cache->pressure;
629
630 /* Invert the angle if we are sculpting along a concave edge. */
631 if (math::dot(sampled_cv[0], sampled_cv[1]) < 0.0f) {
632 sampled_angle = -sampled_angle;
633 }
634
635 /* In dynamic mode, set the angle to 0 when inverting the brush, so you can trim plane
636 * surfaces without changing the brush. */
637 if (flip) {
638 sampled_angle = 0.0f;
639 }
640 else {
641 area_position = ss.cache->location_symm;
642 }
643
644 /* Interpolate between the previous and new sampled angles to avoid artifacts when if angle
645 * difference between two samples is too big. */
647 RAD2DEGF(sampled_angle), ss.cache->multiplane_scrape_angle, 0.2f);
648 }
649 else {
650 /* Standard mode: Scrape with the brush property fixed angle. */
651 area_position = ss.cache->location_symm;
652 ss.cache->multiplane_scrape_angle = brush.multiplane_scrape_angle;
653 if (flip) {
654 ss.cache->multiplane_scrape_angle *= -1.0f;
655 }
656 }
657
658 /* Calculate the final left and right scrape planes. */
659 float3 plane_no;
660 float3 plane_no_rot;
661 const float3 y_axis(0.0f, 1.0f, 0.0f);
662 const float4x4 mat_inv = math::invert(mat);
663
664 std::array<float4, 2> multiplane_scrape_planes;
665
666 mul_v3_mat3_m4v3(plane_no, mat.ptr(), area_normal);
668 plane_no_rot, plane_no, y_axis, DEG2RADF(-ss.cache->multiplane_scrape_angle * 0.5f));
669 mul_v3_mat3_m4v3(plane_no, mat_inv.ptr(), plane_no_rot);
670 normalize_v3(plane_no);
671 plane_from_point_normal_v3(multiplane_scrape_planes[1], area_position, plane_no);
672
673 mul_v3_mat3_m4v3(plane_no, mat.ptr(), area_normal);
675 plane_no_rot, plane_no, y_axis, DEG2RADF(ss.cache->multiplane_scrape_angle * 0.5f));
676 mul_v3_mat3_m4v3(plane_no, mat_inv.ptr(), plane_no_rot);
677 normalize_v3(plane_no);
678 plane_from_point_normal_v3(multiplane_scrape_planes[0], area_position, plane_no);
679
680 const float strength = std::abs(ss.cache->bstrength);
681
683 switch (pbvh.type()) {
685 Mesh &mesh = *id_cast<Mesh *>(object.data);
686 const MeshAttributeData attribute_data(mesh);
688 const PositionDeformData position_data(depsgraph, object);
689 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
690 node_mask.foreach_index(
691 [&](const int i) {
692 calc_faces(depsgraph,
693 sd,
694 brush,
695 mat,
696 multiplane_scrape_planes,
698 strength,
699 vert_normals,
700 attribute_data,
701 nodes[i],
702 object,
703 position_data);
704 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
705 },
707 break;
708 }
710 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
711 MutableSpan<float3> positions = subdiv_ccg.positions;
713 node_mask.foreach_index(
714 [&](const int i) {
715 LocalData &tls = all_tls.local();
716 calc_grids(depsgraph,
717 sd,
718 brush,
719 mat,
720 multiplane_scrape_planes,
722 strength,
723 nodes[i],
724 object,
725 tls);
726 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
727 },
729 break;
730 }
733 node_mask.foreach_index(
734 [&](const int i) {
735 LocalData &tls = all_tls.local();
736 calc_bmesh(depsgraph,
737 sd,
738 brush,
739 mat,
740 multiplane_scrape_planes,
742 strength,
743 nodes[i],
744 object,
745 tls);
747 },
749 break;
750 }
751 }
752 pbvh.tag_positions_changed(node_mask);
753 pbvh.flush_bounds_to_parents();
754}
755
757 const Brush &brush,
758 const SculptSession &ss,
759 const float outline_col[3],
760 const float outline_alpha)
761{
763 return;
764 }
765
766 float4x4 local_mat_inv = math::invert(ss.cache->stroke_local_mat);
767 GPU_matrix_mul(local_mat_inv.ptr());
769 if (ss.cache->toggle_settings.invert) {
770 angle = -angle;
771 }
772
773 float offset = ss.cache->radius * 0.25f;
774
775 const float3 p{0.0f, 0.0f, ss.cache->radius};
776 const float3 y_axis{0.0f, 1.0f, 0.0f};
777 float3 p_l;
778 float3 p_r;
779 const float3 area_center(0);
780 rotate_v3_v3v3fl(p_r, p, y_axis, DEG2RADF((angle + 180) * 0.5f));
781 rotate_v3_v3v3fl(p_l, p, y_axis, DEG2RADF(-(angle + 180) * 0.5f));
782
784 immVertex3f(gpuattr, area_center[0], area_center[1] + offset, area_center[2]);
785 immVertex3f(gpuattr, p_r[0], p_r[1] + offset, p_r[2]);
786 immVertex3f(gpuattr, area_center[0], area_center[1] + offset, area_center[2]);
787 immVertex3f(gpuattr, p_l[0], p_l[1] + offset, p_l[2]);
788
789 immVertex3f(gpuattr, area_center[0], area_center[1] - offset, area_center[2]);
790 immVertex3f(gpuattr, p_r[0], p_r[1] - offset, p_r[2]);
791 immVertex3f(gpuattr, area_center[0], area_center[1] - offset, area_center[2]);
792 immVertex3f(gpuattr, p_l[0], p_l[1] - offset, p_l[2]);
793
794 immVertex3f(gpuattr, area_center[0], area_center[1] - offset, area_center[2]);
795 immVertex3f(gpuattr, area_center[0], area_center[1] + offset, area_center[2]);
796
797 immVertex3f(gpuattr, p_r[0], p_r[1] - offset, p_r[2]);
798 immVertex3f(gpuattr, p_r[0], p_r[1] + offset, p_r[2]);
799
800 immVertex3f(gpuattr, p_l[0], p_l[1] - offset, p_l[2]);
801 immVertex3f(gpuattr, p_l[0], p_l[1] + offset, p_l[2]);
802
803 immEnd();
804
805 immUniformColor3fvAlpha(outline_col, outline_alpha * 0.1f);
807 immVertex3f(gpuattr, area_center[0], area_center[1] + offset, area_center[2]);
808 immVertex3f(gpuattr, p_r[0], p_r[1] + offset, p_r[2]);
809 immVertex3f(gpuattr, p_r[0], p_r[1] - offset, p_r[2]);
810 immVertex3f(gpuattr, area_center[0], area_center[1] + offset, area_center[2]);
811 immVertex3f(gpuattr, area_center[0], area_center[1] - offset, area_center[2]);
812 immVertex3f(gpuattr, p_r[0], p_r[1] - offset, p_r[2]);
813
814 immVertex3f(gpuattr, area_center[0], area_center[1] + offset, area_center[2]);
815 immVertex3f(gpuattr, p_l[0], p_l[1] + offset, p_l[2]);
816 immVertex3f(gpuattr, p_l[0], p_l[1] - offset, p_l[2]);
817 immVertex3f(gpuattr, area_center[0], area_center[1] + offset, area_center[2]);
818 immVertex3f(gpuattr, area_center[0], area_center[1] - offset, area_center[2]);
819 immVertex3f(gpuattr, p_l[0], p_l[1] - offset, p_l[2]);
820
821 immEnd();
822}
823
824} // namespace blender::ed::sculpt_paint::brushes
A BVH for high poly meshes.
#define DEG2RADF(_deg)
#define RAD2DEGF(_rad)
Object is a sort of wrapper for general info.
#define PRF_scope(category)
#define BLI_NOINLINE
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
bool closest(btVector3 &v)
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
constexpr int64_t size() const
Definition BLI_span.hh:499
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
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
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 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 sample_node_surface_bmesh(const Depsgraph &depsgraph, const Object &object, const Brush &brush, const float4x4 &mat, const bke::pbvh::BMeshNode &node, ScrapeSampleData &sample, LocalData &tls)
void multiplane_scrape_preview_draw(uint gpuattr, const Brush &brush, const SculptSession &ss, const float outline_col[3], float outline_alpha)
static BLI_NOINLINE void filter_plane_side_factors(const Span< float3 > positions, const Span< float3 > local_positions, const std::array< float4, 2 > &scrape_planes, const MutableSpan< float > factors)
static void sample_node_surface_mesh(const Depsgraph &depsgraph, const Object &object, const Brush &brush, const float4x4 &mat, const Span< float3 > vert_positions, const Span< float3 > vert_normals, const MeshAttributeData &attribute_data, const bke::pbvh::MeshNode &node, ScrapeSampleData &sample)
static BLI_NOINLINE void calc_distances(const Span< float3 > local_positions, const MutableSpan< float > distances)
static BLI_NOINLINE void accumulate_samples(const Span< float3 > positions, const Span< float3 > local_positions, const Span< float3 > normals, const Span< float > factors, ScrapeSampleData &sample)
static ScrapeSampleData join_samples(const ScrapeSampleData &a, const ScrapeSampleData &b)
void do_multiplane_scrape_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
static std::optional< ScrapeSampleData > sample_surface(const Depsgraph &depsgraph, const Object &object, const Brush &brush, const float4x4 &mat, const IndexMask &node_mask)
static void sample_node_surface_grids(const Depsgraph &depsgraph, const Object &object, const Brush &brush, const float4x4 &mat, const bke::pbvh::GridsNode &node, ScrapeSampleData &sample, LocalData &tls)
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 gather_bmesh_normals(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > normals)
Definition sculpt.cc:6517
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
bool stroke_is_first_brush_step_of_symmetry_pass(const StrokeCache &cache)
Definition sculpt.cc:667
void scale_translations(MutableSpan< float3 > translations, Span< float > factors)
Definition sculpt.cc:7837
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 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
std::optional< float3 > calc_area_normal(const Depsgraph &depsgraph, const Brush &brush, const Object &ob, const IndexMask &node_mask)
Definition sculpt.cc:1926
void filter_plane_trim_limit_factors(const Brush &brush, const StrokeCache &cache, Span< float3 > translations, MutableSpan< float > factors)
Definition sculpt.cc:8231
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)
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 gather_grids_normals(const SubdivCCG &subdiv_ccg, Span< int > grids, MutableSpan< float3 > normals)
Definition sculpt.cc:6509
constexpr ParallelGrainSize grain_size(int grain_size)
T length(const VecBase< T, Size > &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
CartesianBasis invert(const CartesianBasis &basis)
T midpoint(const T &a, const T &b)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
T interpolate(const T &a, const T &b, const FactorT &t)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
void transform_points(const float4x4 &transform, MutableSpan< float3 > points, bool use_threading=true)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void immEnd()
unsigned int uint
Definition BKE_text.h:55
float angle_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void immBegin(GPUPrimType, uint vertex_len)
void mul_v3_mat3_m4v3(float r[3], const float mat[4][4], const float vec[3])
@ BRUSH_MULTIPLANE_SCRAPE_PLANES_PREVIEW
@ BRUSH_MULTIPLANE_SCRAPE_DYNAMIC
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
void closest_to_plane_normalized_v3(float r_close[3], const float plane[4], const float pt[3])
Definition math_geom.cc:458
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void GPU_matrix_mul(const float m[4][4])
void normalize_m4(float R[4][4]) ATTR_NONNULL()
BMesh const char void * data
MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
const Brush * BKE_paint_brush_for_read(const Paint *paint)
MINLINE float normalize_v3(float n[3])
@ BRUSH_ORIGINAL_NORMAL
void immVertex3f(uint attr_id, float x, float y, float z)
VecBase< float, 3 > float3
void immUniformColor3fvAlpha(const float rgb[3], float a)
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void rotate_v3_v3v3fl(float r[3], const float p[3], const float axis[3], float angle)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const c_style_mat & ptr() const
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