Blender V5.3
sculpt_cloth.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#include "sculpt_cloth.hh"
9
10#include "MEM_guardedalloc.h"
11
12#include "BLI_array_utils.hh"
14#include "BLI_math_geom_c.hh"
15#include "BLI_math_matrix.hh"
16#include "BLI_math_matrix_c.hh"
18#include "BLI_math_vector.hh"
19#include "BLI_ordered_edge.hh"
20#include "BLI_utildefines.hh"
21#include "BLI_vector.hh"
22
23#include "BLT_translation.hh"
24
25#include "DNA_brush_types.h"
27#include "DNA_object_types.h"
28#include "DNA_scene_types.h"
29
30#include "BKE_brush.hh"
31#include "BKE_ccg.hh"
32#include "BKE_collision.h"
33#include "BKE_context.hh"
34#include "BKE_layer.hh"
35#include "BKE_mesh.hh"
36#include "BKE_modifier.hh"
37#include "BKE_paint.hh"
38#include "BKE_paint_bvh.hh"
39#include "BKE_subdiv_ccg.hh"
40
42
43#include "WM_api.hh"
44#include "WM_types.hh"
45
46#include "ED_sculpt.hh"
47
48#include "mesh_brush_common.hh"
49#include "sculpt_automask.hh"
50#include "sculpt_face_set.hh"
51#include "sculpt_filter.hh"
52#include "sculpt_hide.hh"
53#include "sculpt_intern.hh"
54#include "sculpt_undo.hh"
55
56#include "RNA_access.hh"
57#include "RNA_define.hh"
58
59#include "GPU_immediate.hh"
60#include "GPU_immediate_util.hh"
61#include "GPU_matrix.hh"
62#include "GPU_state.hh"
63
64#include "bmesh.hh"
65
66#include <cmath>
67#include <cstring>
68
70
72 const Span<int> grids,
73 Vector<int> &indices)
74{
76 const int grid_verts_num = grids.size() * key.grid_area;
77 indices.resize(grid_verts_num);
78 for (const int i : grids.index_range()) {
80 indices.as_mutable_span().slice(i * key.grid_area, key.grid_area),
81 grids[i] * key.grid_area);
82 }
83 return indices;
84}
85
87 Vector<int> &indices)
88{
90 indices.resize(verts.size());
91 int i = 0;
92 for (const BMVert *vert : verts) {
93 indices[i] = BM_elem_index_get(vert);
94 i++;
95 }
96 return indices;
97}
98
100 const BitGroupVector<> &grid_hidden,
101 const Span<int> grids,
102 Vector<int> &indices)
103{
105 if (grid_hidden.is_empty()) {
106 return calc_vert_indices_grids(key, grids, indices);
107 }
108 const int grid_verts_num = grids.size() * key.grid_area;
109 indices.reserve(grid_verts_num);
110 for (const int i : grids.index_range()) {
111 const int start = grids[i] * key.grid_area;
112 bits::foreach_0_index(grid_hidden[grids[i]],
113 [&](const int offset) { indices.append(start + offset); });
114 }
115 return indices;
116}
117
119 Vector<int> &indices)
120{
122 indices.reserve(verts.size());
123 for (const BMVert *vert : verts) {
124 if (!BM_elem_flag_test(vert, BM_ELEM_HIDDEN)) {
125 indices.append(BM_elem_index_get(vert));
126 }
127 }
128 return indices;
129}
130
132 const Span<int> verts,
133 Vector<int> &r_offset_data,
134 Vector<int> &r_data)
135{
137 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
138
139 r_offset_data.resize(verts.size() + 1);
140 r_data.clear();
141
142 for (const int i : verts.index_range()) {
143 r_offset_data[i] = r_data.size();
144 SubdivCCGNeighbors neighbors;
146 subdiv_ccg, SubdivCCGCoord::from_index(key, verts[i]), false, neighbors);
147
148 for (const SubdivCCGCoord coord : neighbors.coords) {
149 r_data.append(coord.to_index(key));
150 }
151 }
152
153 r_offset_data.last() = r_data.size();
154 return GroupedSpan<int>(r_offset_data.as_span(), r_data.as_span());
155}
156
158 const Span<int> verts,
159 Vector<int> &r_offset_data,
160 Vector<int> &r_data)
161{
163 BMeshNeighborVerts neighbors;
164
165 r_offset_data.resize(verts.size() + 1);
166 r_data.clear();
167
168 for (const int i : verts.index_range()) {
169 r_offset_data[i] = r_data.size();
170 BMVert *vert = BM_vert_at_index(&const_cast<BMesh &>(bm), verts[i]);
171 for (const BMVert *neighbor : vert_neighbors_get_bmesh(*vert, neighbors)) {
172 r_data.append(BM_elem_index_get(neighbor));
173 }
174 }
175 r_offset_data.last() = r_data.size();
176 return GroupedSpan<int>(r_offset_data.as_span(), r_data.as_span());
177}
178
180{
181 if (!ss.cache || !brush) {
182 return float3(0);
183 }
184 if (brush->cloth_simulation_area_type == BRUSH_CLOTH_SIMULATION_AREA_LOCAL) {
185 return ss.cache->initial_location_symm;
186 }
187 return ss.cache->location_symm;
188}
189
191 const Brush &brush,
192 IndexMaskMemory &memory)
193{
194 const SculptSession &ss = *object.runtime->sculpt_session;
195 BLI_assert(ss.cache);
196 BLI_assert(brush.sculpt_brush_type == SCULPT_BRUSH_TYPE_CLOTH);
198
199 switch (brush.cloth_simulation_area_type) {
201 const float radius_squared = math::square(ss.cache->initial_radius *
202 (1.0 + brush.cloth_sim_limit));
203 return bke::pbvh::search_nodes(pbvh, memory, [&](const bke::pbvh::Node &node) {
204 return node_in_sphere(node, ss.cache->initial_location_symm, radius_squared, false);
205 });
206 }
208 return bke::pbvh::all_leaf_nodes(pbvh, memory);
210 const float radius_squared = math::square(ss.cache->radius * (1.0 + brush.cloth_sim_limit));
211 return bke::pbvh::search_nodes(pbvh, memory, [&](const bke::pbvh::Node &node) {
212 return node_in_sphere(node, ss.cache->location_symm, radius_squared, false);
213 });
214 }
215 }
216
218 return {};
219}
220
222{
223 return (brush.sculpt_brush_type == SCULPT_BRUSH_TYPE_CLOTH &&
225 /* All brushes that are not the cloth brush deform the simulation using softbody
226 * constraints instead of applying forces. */
227 (brush.sculpt_brush_type != SCULPT_BRUSH_TYPE_CLOTH &&
228 brush.deform_target == BRUSH_DEFORM_TARGET_CLOTH_SIM);
229}
230
232 const float radius,
233 const float3 &location,
234 const float3 &co)
235{
236 if (brush.sculpt_brush_type != SCULPT_BRUSH_TYPE_CLOTH) {
237 /* All brushes that are not the cloth brush do not use simulation areas. */
238 return 1.0f;
239 }
240
241 /* Global simulation does not have any falloff as the entire mesh is being simulated. */
242 if (brush.cloth_simulation_area_type == BRUSH_CLOTH_SIMULATION_AREA_GLOBAL) {
243 return 1.0f;
244 }
245
246 const float distance = math::distance(location, co);
247 const float limit = radius + (radius * brush.cloth_sim_limit);
248 const float falloff = radius + (radius * brush.cloth_sim_limit * brush.cloth_sim_falloff);
249
250 if (distance > limit) {
251 /* Outside the limits. */
252 return 0.0f;
253 }
254 if (distance < falloff) {
255 /* Before the falloff area. */
256 return 1.0f;
257 }
258 /* Do a smooth-step transition inside the falloff area. */
259 float p = 1.0f - ((distance - falloff) / (limit - falloff));
260 return 3.0f * p * p - 2.0f * p * p * p;
261}
262
264 const float radius,
265 const float3 &location,
266 const Span<float3> positions,
267 const MutableSpan<float> factors)
268{
269 BLI_assert(positions.size() == factors.size());
270
271 for (const int i : positions.index_range()) {
272 factors[i] *= cloth_brush_simulation_falloff_get(brush, radius, location, positions[i]);
273 }
274}
275
276#define CLOTH_LENGTH_CONSTRAINTS_BLOCK 100000
277#define CLOTH_SIMULATION_ITERATIONS 5
278
279#define CLOTH_SOLVER_DISPLACEMENT_FACTOR 0.6f
280#define CLOTH_MAX_CONSTRAINTS_PER_VERTEX 1024
281#define CLOTH_SIMULATION_TIME_STEP 0.01f
282#define CLOTH_DEFORMATION_SNAKEHOOK_STRENGTH 0.35f
283#define CLOTH_DEFORMATION_TARGET_STRENGTH 0.01f
284#define CLOTH_DEFORMATION_GRAB_STRENGTH 0.1f
285
287 const int node_index,
288 const int v1,
289 const int v2,
290 const Span<float3> init_positions)
291{
292 LengthConstraint length_constraint{};
293
294 length_constraint.elem_index_a = v1;
295 length_constraint.elem_index_b = v2;
296
297 length_constraint.node = node_index;
298
299 length_constraint.elem_position_a = cloth_sim.pos[v1];
300 length_constraint.elem_position_b = cloth_sim.pos[v2];
301
302 length_constraint.type = SCULPT_CLOTH_CONSTRAINT_STRUCTURAL;
303
304 length_constraint.length = math::distance(init_positions[v1], init_positions[v2]);
305 length_constraint.strength = 1.0f;
306
307 cloth_sim.length_constraints.append(length_constraint);
308}
309
311 const int node_index,
312 const int v,
313 const float strength)
314{
315 LengthConstraint length_constraint{};
316
317 length_constraint.elem_index_a = v;
318 length_constraint.elem_index_b = v;
319
320 length_constraint.node = node_index;
321
322 length_constraint.elem_position_a = cloth_sim.pos[v];
323 length_constraint.elem_position_b = cloth_sim.softbody_pos[v];
324
325 length_constraint.type = SCULPT_CLOTH_CONSTRAINT_SOFTBODY;
326
327 length_constraint.length = 0.0f;
328 length_constraint.strength = strength;
329
330 cloth_sim.length_constraints.append(length_constraint);
331}
332
334 const int node_index,
335 const int v,
336 const float strength)
337{
338 LengthConstraint length_constraint{};
339
340 length_constraint.elem_index_a = v;
341 length_constraint.elem_index_b = v;
342
343 length_constraint.node = node_index;
344
345 length_constraint.elem_position_a = cloth_sim.pos[v];
346 length_constraint.elem_position_b = cloth_sim.init_pos[v];
347
348 length_constraint.type = SCULPT_CLOTH_CONSTRAINT_PIN;
349
350 length_constraint.length = 0.0f;
351 length_constraint.strength = strength;
352
353 cloth_sim.length_constraints.append(length_constraint);
354}
355
357 const int node_index,
358 const int v,
359 const float strength)
360{
361 LengthConstraint length_constraint{};
362
363 length_constraint.elem_index_a = v;
364 length_constraint.elem_index_b = v;
365
366 length_constraint.node = node_index;
367
368 length_constraint.type = SCULPT_CLOTH_CONSTRAINT_DEFORMATION;
369
370 length_constraint.elem_position_a = cloth_sim.pos[v];
371 length_constraint.elem_position_b = cloth_sim.deformation_pos[v];
372
373 length_constraint.length = 0.0f;
374 length_constraint.strength = strength;
375
376 cloth_sim.length_constraints.append(length_constraint);
377}
378
379static void add_constraints_for_verts(const Object &object,
380 const Brush *brush,
381 const float3 &cloth_sim_initial_location,
382 const float cloth_sim_radius,
383 const Span<float3> init_positions,
384 const int node_index,
385 const Span<int> verts,
386 const GroupedSpan<int> vert_neighbors,
387 SimulationData &cloth_sim,
388 Set<OrderedEdge> &created_length_constraints)
389{
390 const SculptSession &ss = *object.runtime->sculpt_session;
391
392 const bool is_brush_has_stroke_cache = ss.cache != nullptr && brush != nullptr;
393 const bool pin_simulation_boundary = is_brush_has_stroke_cache &&
395 brush->cloth_simulation_area_type !=
397
398 /* Brush can be nullptr in tools that use the solver without relying of constraints with
399 * deformation positions. */
400 const bool cloth_is_deform_brush = is_brush_has_stroke_cache && is_cloth_deform_brush(*brush);
401
402 const bool use_falloff_plane = brush->cloth_force_falloff_type ==
404 float radius_squared = 0.0f;
405 if (cloth_is_deform_brush) {
406 radius_squared = ss.cache->initial_radius * ss.cache->initial_radius;
407 }
408
409 /* Only limit the constraint creation to a radius when the simulation is local. */
410 const float cloth_sim_radius_squared = brush->cloth_simulation_area_type ==
412 cloth_sim_radius * cloth_sim_radius :
413 FLT_MAX;
414
415 for (const int i : verts.index_range()) {
416 const int vert = verts[i];
417 const float len_squared = math::distance_squared(init_positions[vert],
418 cloth_sim_initial_location);
419 if (len_squared < cloth_sim_radius_squared) {
420 if (cloth_sim.softbody_strength > 0.0f) {
421 cloth_brush_add_softbody_constraint(cloth_sim, node_index, vert, 1.0f);
422 }
423
424 const Span<int> neighbors = vert_neighbors[i];
425
426 /* As we don't know the order of the neighbor vertices, we create all possible combinations
427 * between the neighbor and the original vertex as length constraints. */
428 /* This results on a pattern that contains structural, shear and bending constraints for all
429 * vertices, but constraints are repeated taking more memory than necessary. */
430 for (const int neighbor : neighbors) {
431 if (created_length_constraints.add({vert, neighbor})) {
432 cloth_brush_add_length_constraint(cloth_sim, node_index, vert, neighbor, init_positions);
433 }
434 }
435 for (const int a : neighbors) {
436 for (const int b : neighbors) {
437 if (a != b) {
438 if (created_length_constraints.add({a, b})) {
439 cloth_brush_add_length_constraint(cloth_sim, node_index, a, b, init_positions);
440 }
441 }
442 }
443 }
444 }
445
446 if (is_brush_has_stroke_cache && brush->sculpt_brush_type == SCULPT_BRUSH_TYPE_CLOTH) {
447 /* The cloth brush works by applying forces in most of its modes, but some of them require
448 * deformation coordinates to make the simulation stable. */
449 if (brush->cloth_deform_type == BRUSH_CLOTH_DEFORM_GRAB) {
450 if (use_falloff_plane) {
451 /* With plane falloff the strength of the constraints is set when applying the
452 * deformation forces. */
454 cloth_sim, node_index, vert, CLOTH_DEFORMATION_GRAB_STRENGTH);
455 }
456 else if (len_squared < radius_squared) {
457 /* With radial falloff deformation constraints are created with different strengths and
458 * only inside the radius of the brush. */
459 const float fade = BKE_brush_curve_strength(
460 brush, std::sqrt(len_squared), ss.cache->radius);
462 cloth_sim, node_index, vert, fade * CLOTH_DEFORMATION_GRAB_STRENGTH);
463 }
464 }
465 else if (brush->cloth_deform_type == BRUSH_CLOTH_DEFORM_SNAKE_HOOK) {
466 /* Cloth Snake Hook creates deformation constraint with fixed strength because the strength
467 * is controlled per iteration using cloth_sim.deformation_strength. */
469 cloth_sim, node_index, vert, CLOTH_DEFORMATION_SNAKEHOOK_STRENGTH);
470 }
471 }
472 else if (!cloth_sim.deformation_pos.is_empty()) {
473 /* Any other tool that target the cloth simulation handle the falloff in
474 * their own code when modifying the deformation coordinates of the simulation, so
475 * deformation constraints are created with a fixed strength for all vertices. */
477 cloth_sim, node_index, vert, CLOTH_DEFORMATION_TARGET_STRENGTH);
478 }
479
480 if (pin_simulation_boundary) {
481 const float sim_falloff = cloth_brush_simulation_falloff_get(
482 *brush, ss.cache->initial_radius, ss.cache->location_symm, init_positions[vert]);
483 /* Vertex is inside the area of the simulation without any falloff applied. */
484 if (sim_falloff < 1.0f) {
485 /* Create constraints with more strength the closer the vertex is to the simulation
486 * boundary. */
487 cloth_brush_add_pin_constraint(cloth_sim, node_index, vert, 1.0f - sim_falloff);
488 }
489 }
490 }
491}
492
494 Object &object,
495 const IndexMask &node_mask,
496 SimulationData &cloth_sim,
497 const float3 &initial_location,
498 const float radius)
499{
501 SculptSession &ss = *object.runtime->sculpt_session;
504
505 /* TODO: Multi-threaded needs to be disabled for this task until implementing the optimization of
506 * storing the constraints per node. */
507 /* Currently all constrains are added to the same global array which can't be accessed from
508 * different threads. */
509
510 IndexMaskMemory memory;
511 Set<OrderedEdge> created_length_constraints;
512 Vector<int> neighbor_offsets;
513 Vector<int> neighbor_data;
514 switch (pbvh.type()) {
517 const IndexMask uninitialized_nodes = IndexMask::from_predicate(
518 node_mask, memory, [&](const int i) {
519 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
520 return cloth_sim.node_state[node_index] == SCULPT_CLOTH_NODE_UNINITIALIZED;
521 });
522 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
523 const OffsetIndices faces = mesh.faces();
524 const Span<int> corner_verts = mesh.corner_verts();
525 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
526 const bke::AttributeAccessor attributes = mesh.attributes();
527 const VArraySpan<bool> hide_vert = *attributes.lookup<bool>(".hide_vert",
529 const VArraySpan<bool> hide_poly = *attributes.lookup<bool>(".hide_poly",
531
532 Span<float3> init_positions;
533 VArraySpan<float3> persistent_position;
534 if (brush != nullptr && brush->flag & BRUSH_PERSISTENT) {
535 persistent_position = *attributes.lookup<float3>(".sculpt_persistent_co",
537 }
538 if (persistent_position.is_empty()) {
539 init_positions = cloth_sim.init_pos;
540 }
541 else {
542 init_positions = persistent_position;
543 }
544 uninitialized_nodes.foreach_index([&](const int i) {
546 const Span<int> verts = hide::node_visible_verts(nodes[i], hide_vert, vert_indices);
547 const GroupedSpan<int> neighbors = calc_vert_neighbors(faces,
548 corner_verts,
549 vert_to_face_map,
550 hide_poly,
551 verts,
552 neighbor_offsets,
553 neighbor_data);
555 brush,
556 initial_location,
557 radius,
558 init_positions,
559 cloth_sim.node_state_index.lookup(&nodes[i]),
560 verts,
561 neighbors,
562 cloth_sim,
563 created_length_constraints);
564 });
565 break;
566 }
569 const IndexMask uninitialized_nodes = IndexMask::from_predicate(
570 node_mask, memory, [&](const int i) {
571 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
572 return cloth_sim.node_state[node_index] == SCULPT_CLOTH_NODE_UNINITIALIZED;
573 });
574 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
575 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
576 const BitGroupVector<> &grid_hidden = subdiv_ccg.grid_hidden;
577
578 Span<float3> init_positions;
579 Span<float3> persistent_position;
580 const std::optional<PersistentMultiresData> persistent_multires_data =
582 if (brush != nullptr && brush->flag & BRUSH_PERSISTENT && persistent_multires_data) {
583 persistent_position = persistent_multires_data->positions;
584 }
585 if (persistent_position.is_empty()) {
586 init_positions = cloth_sim.init_pos;
587 }
588 else {
589 init_positions = persistent_position;
590 }
591 Vector<int> vert_indices;
592 uninitialized_nodes.foreach_index([&](const int i) {
594 key, grid_hidden, nodes[i].grids(), vert_indices);
596 subdiv_ccg, verts, neighbor_offsets, neighbor_data);
598 brush,
599 initial_location,
600 radius,
601 init_positions,
602 cloth_sim.node_state_index.lookup(&nodes[i]),
603 verts,
604 neighbors,
605 cloth_sim,
606 created_length_constraints);
607 });
608 break;
609 }
612 const IndexMask uninitialized_nodes = IndexMask::from_predicate(
613 node_mask, memory, [&](const int i) {
614 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
615 return cloth_sim.node_state[node_index] == SCULPT_CLOTH_NODE_UNINITIALIZED;
616 });
617 BMesh &bm = *ss.bm;
619 Vector<int> vert_indices;
620 uninitialized_nodes.foreach_index([&](const int i) {
622 const Span<int> verts = calc_visible_vert_indices_bmesh(bm_verts, vert_indices);
624 bm, verts, neighbor_offsets, neighbor_data);
626 brush,
627 initial_location,
628 radius,
629 cloth_sim.init_pos,
630 cloth_sim.node_state_index.lookup(&nodes[i]),
631 verts,
632 neighbors,
633 cloth_sim,
634 created_length_constraints);
635 });
636 break;
637 }
638 }
639}
640
642 const Span<float3> forces,
643 const Span<int> verts)
644{
646 const float mass_inv = math::rcp(cloth_sim.mass);
647 for (const int i : verts.index_range()) {
648 cloth_sim.acceleration[verts[i]] += forces[i] * mass_inv;
649 }
650}
651
653 const Span<int> verts,
654 const Span<float> factors)
655{
657 MutableSpan<float> length_constraint_tweak = cloth_sim.length_constraint_tweak;
658 for (const int i : verts.index_range()) {
659 length_constraint_tweak[verts[i]] += factors[i] * 0.01f;
660 }
661}
662
664 const float4 &plane,
665 const MutableSpan<float> distances)
666{
668 for (const int i : positions.index_range()) {
669 distances[i] = dist_to_plane_v3(positions[i], plane);
670 }
671}
672
674 const float min,
675 const float max)
676{
678 for (float &factor : factors) {
679 factor = std::clamp(factor, min, max);
680 }
681}
682
684 const Span<int> verts,
685 const MutableSpan<float> factors,
686 const bool use_falloff_plane,
687 const float3 &grab_delta_symmetry)
688{
690 for (const int i : verts.index_range()) {
691 cloth_sim.deformation_pos[verts[i]] = cloth_sim.init_pos[verts[i]] +
692 grab_delta_symmetry * factors[i];
693 }
694 if (use_falloff_plane) {
695 clamp_factors(factors, 0.0f, 1.0f);
696 scatter_data_mesh(factors.as_span(), verts, cloth_sim.deformation_strength.as_mutable_span());
697 }
698 else {
699 cloth_sim.deformation_strength.as_mutable_span().fill_indices(verts, 1.0f);
700 }
701}
702
704 const Span<int> verts,
705 const MutableSpan<float> factors,
706 const float3 &grab_delta_symmetry)
707{
709 for (const int i : verts.index_range()) {
710 const int vert = verts[i];
711 cloth_sim.deformation_pos[vert] = cloth_sim.pos[vert] + grab_delta_symmetry * factors[i];
712 }
713 scatter_data_mesh(factors.as_span(), verts, cloth_sim.deformation_strength.as_mutable_span());
714}
715
716BLI_NOINLINE static void calc_pinch_forces(const Span<float3> positions,
717 const float3 &location,
718 const MutableSpan<float3> forces)
719{
721 for (const int i : forces.index_range()) {
722 forces[i] = math::normalize(location - positions[i]);
723 }
724}
725
727 const float4 &plane,
728 const float3 &plane_normal,
729 const MutableSpan<float3> forces)
730{
732 for (const int i : positions.index_range()) {
733 const float distance = dist_signed_to_plane_v3(positions[i], plane);
734 forces[i] = math::normalize(plane_normal * -distance);
735 }
736}
737
739 const float4x4 &imat,
740 const float3 &location,
741 const MutableSpan<float3> forces)
742{
744 const float3 x_object_space = math::normalize(imat.x_axis());
745 const float3 z_object_space = math::normalize(imat.z_axis());
746 for (const int i : positions.index_range()) {
747 const float3 disp_center = math::normalize(location - positions[i]);
748 const float3 x_disp = x_object_space * math::dot(disp_center, x_object_space);
749 const float3 z_disp = z_object_space * math::dot(disp_center, z_object_space);
750 forces[i] = x_disp + z_disp;
751 }
752}
753
764
769
770static void calc_forces_mesh(const Depsgraph &depsgraph,
771 Object &ob,
772 const Brush &brush,
773 const float3 &offset,
774 const float4x4 &imat,
775 const float3 &sim_location,
776 const float3 &gravity,
777 const std::optional<FalloffPlane> &falloff_plane,
778 const MeshAttributeData &attribute_data,
779 const Span<float3> positions_eval,
780 const Span<float3> vert_normals,
782{
784 SimulationData &cloth_sim = *ss.cache->cloth_sim;
785 const StrokeCache &cache = *ss.cache;
786
787 const Span<int> verts = node.verts();
789 gather_data_mesh(positions_eval, verts, positions.as_mutable_span());
790
791 Array<float3, bke::pbvh::MESH_LEAF_LIMIT> init_positions(verts.size());
792 gather_data_mesh(cloth_sim.init_pos.as_span(), verts, init_positions.as_mutable_span());
793 const Span<float3> current_positions = brush.cloth_deform_type == BRUSH_CLOTH_DEFORM_GRAB ?
794 init_positions :
795 positions;
796
798 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
799 filter_region_clip_factors(ss, current_positions, factors);
800
801 calc_brush_simulation_falloff(brush, cache.radius, sim_location, positions, factors);
802
804
805 /* Apply gravity in the entire simulation area before brush distances are taken into account. */
806 if (!math::is_zero(gravity)) {
807 translations_from_offset_and_factors(gravity, factors, forces);
808 apply_forces(cloth_sim, forces, verts);
809 }
810
811 if (brush.flag & BRUSH_FRONTFACE) {
812 calc_front_face(cache.view_normal_symm, vert_normals, verts, factors);
813 }
814
816 if (falloff_plane) {
817 calc_distances_to_plane(current_positions, falloff_plane->plane, distances);
818 }
819 else {
821 ss, current_positions, eBrushFalloffShape(brush.falloff_shape), distances);
822 }
823 filter_distances_with_radius(cache.radius, distances, factors);
824 apply_hardness_to_distances(cache, distances);
825 calc_brush_strength_factors(cache, brush, distances, factors);
826
827 const auto_mask::Cache *automask = auto_mask::active_cache_get(ss);
828 auto_mask::calc_vert_factors(depsgraph, ob, automask, node, verts, factors);
829
830 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, current_positions, factors);
831
832 scale_factors(factors, cache.bstrength);
833
834 switch (brush.cloth_deform_type) {
837 math::normalize(cache.location_symm - cache.last_location_symm), factors, forces);
838 apply_forces(cloth_sim, forces, verts);
839 break;
841 translations_from_offset_and_factors(-offset, factors, forces);
842 apply_forces(cloth_sim, forces, verts);
843 break;
846 cloth_sim, verts, factors, falloff_plane.has_value(), cache.grab_delta_symm);
847 break;
849 apply_snake_hook_brush(cloth_sim, verts, factors, cache.grab_delta_symm);
850 break;
852 if (falloff_plane) {
853 calc_plane_pinch_forces(positions, falloff_plane->plane, falloff_plane->normal, forces);
854 }
855 else {
856 calc_pinch_forces(positions, cache.location_symm, forces);
857 }
858 scale_translations(forces, factors);
859 apply_forces(cloth_sim, forces, verts);
860 break;
862 calc_perpendicular_pinch_forces(positions, imat, cache.location_symm, forces);
863 scale_translations(forces, factors);
864 apply_forces(cloth_sim, forces, verts);
865 break;
866 }
868 gather_data_mesh(vert_normals, verts, forces.as_mutable_span());
869 scale_translations(forces, factors);
870 apply_forces(cloth_sim, forces, verts);
871 break;
873 expand_length_constraints(cloth_sim, verts, factors);
874 break;
875 }
876}
877
878static void calc_forces_grids(const Depsgraph &depsgraph,
879 Object &ob,
880 const Brush &brush,
881 const float3 &offset,
882 const float4x4 &imat,
883 const float3 &sim_location,
884 const float3 &gravity,
885 const std::optional<FalloffPlane> &falloff_plane,
887 LocalData &tls)
888{
890 SimulationData &cloth_sim = *ss.cache->cloth_sim;
891 const StrokeCache &cache = *ss.cache;
892 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
893 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
894
895 const Span<int> grids = node.grids();
896 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
897 const MutableSpan init_positions = gather_data_grids(
898 subdiv_ccg, cloth_sim.init_pos.as_span(), grids, tls.init_positions);
899 const Span<float3> current_positions = brush.cloth_deform_type == BRUSH_CLOTH_DEFORM_GRAB ?
900 init_positions :
901 positions;
902
903 tls.factors.resize(positions.size());
904 const MutableSpan<float> factors = tls.factors;
905 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
906 filter_region_clip_factors(ss, current_positions, factors);
907
908 calc_brush_simulation_falloff(brush, cache.radius, sim_location, positions, factors);
909
910 const Span<int> verts = calc_vert_indices_grids(key, grids, tls.vert_indices);
911
912 tls.translations.resize(verts.size());
913 const MutableSpan<float3> forces = tls.translations;
914
915 /* Apply gravity in the entire simulation area before brush distances are taken into account. */
916 if (!math::is_zero(gravity)) {
917 translations_from_offset_and_factors(gravity, factors, forces);
918 apply_forces(cloth_sim, forces, verts);
919 }
920
921 if (brush.flag & BRUSH_FRONTFACE) {
922 calc_front_face(cache.view_normal_symm, subdiv_ccg, grids, factors);
923 }
924
925 tls.distances.resize(verts.size());
926 const MutableSpan<float> distances = tls.distances;
927 if (falloff_plane) {
928 calc_distances_to_plane(current_positions, falloff_plane->plane, distances);
929 }
930 else {
932 ss, current_positions, eBrushFalloffShape(brush.falloff_shape), distances);
933 }
934 filter_distances_with_radius(cache.radius, distances, factors);
935 apply_hardness_to_distances(cache, distances);
936 calc_brush_strength_factors(cache, brush, distances, factors);
937
938 const auto_mask::Cache *automask = auto_mask::active_cache_get(ss);
939 auto_mask::calc_grids_factors(depsgraph, ob, automask, node, grids, factors);
940
941 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, current_positions, factors);
942
943 scale_factors(factors, cache.bstrength);
944
945 switch (brush.cloth_deform_type) {
948 math::normalize(cache.location_symm - cache.last_location_symm), factors, forces);
949 apply_forces(cloth_sim, forces, verts);
950 break;
952 translations_from_offset_and_factors(-offset, factors, forces);
953 apply_forces(cloth_sim, forces, verts);
954 break;
957 cloth_sim, verts, factors, falloff_plane.has_value(), cache.grab_delta_symm);
958 break;
960 apply_snake_hook_brush(cloth_sim, verts, factors, cache.grab_delta_symm);
961 break;
963 if (falloff_plane) {
964 calc_plane_pinch_forces(positions, falloff_plane->plane, falloff_plane->normal, forces);
965 }
966 else {
967 calc_pinch_forces(positions, cache.location_symm, forces);
968 }
969 scale_translations(forces, factors);
970 apply_forces(cloth_sim, forces, verts);
971 break;
973 calc_perpendicular_pinch_forces(positions, imat, cache.location_symm, forces);
974 scale_translations(forces, factors);
975 apply_forces(cloth_sim, forces, verts);
976 break;
977 }
979 gather_grids_normals(subdiv_ccg, grids, forces);
980 scale_translations(forces, factors);
981 apply_forces(cloth_sim, forces, verts);
982 break;
984 expand_length_constraints(cloth_sim, verts, factors);
985 break;
986 }
987}
988
989static void calc_forces_bmesh(const Depsgraph &depsgraph,
990 Object &ob,
991 const Brush &brush,
992 const float3 &offset,
993 const float4x4 &imat,
994 const float3 &sim_location,
995 const float3 &gravity,
996 const std::optional<FalloffPlane> &falloff_plane,
998 LocalData &tls)
999{
1001 SimulationData &cloth_sim = *ss.cache->cloth_sim;
1002 const StrokeCache &cache = *ss.cache;
1003
1005 const Span<int> verts = calc_vert_indices_bmesh(bm_verts, tls.vert_indices);
1006
1007 const MutableSpan positions = gather_bmesh_positions(bm_verts, tls.positions);
1008 const MutableSpan init_positions = gather_data_mesh(
1009 cloth_sim.init_pos.as_span(), verts, tls.init_positions);
1010 const Span<float3> current_positions = brush.cloth_deform_type == BRUSH_CLOTH_DEFORM_GRAB ?
1011 init_positions :
1012 positions;
1013
1014 tls.factors.resize(verts.size());
1015 const MutableSpan<float> factors = tls.factors;
1016 fill_factor_from_hide_and_mask(*ss.bm, bm_verts, factors);
1017 filter_region_clip_factors(ss, current_positions, factors);
1018
1019 calc_brush_simulation_falloff(brush, cache.radius, sim_location, positions, factors);
1020
1021 tls.translations.resize(verts.size());
1022 const MutableSpan<float3> forces = tls.translations;
1023
1024 /* Apply gravity in the entire simulation area before brush distances are taken into account. */
1025 if (!math::is_zero(gravity)) {
1026 translations_from_offset_and_factors(gravity, factors, forces);
1027 apply_forces(cloth_sim, forces, verts);
1028 }
1029
1030 if (brush.flag & BRUSH_FRONTFACE) {
1031 calc_front_face(cache.view_normal_symm, bm_verts, factors);
1032 }
1033
1034 tls.distances.resize(verts.size());
1035 const MutableSpan<float> distances = tls.distances;
1036 if (falloff_plane) {
1037 calc_distances_to_plane(current_positions, falloff_plane->plane, distances);
1038 }
1039 else {
1041 ss, current_positions, eBrushFalloffShape(brush.falloff_shape), distances);
1042 }
1043 filter_distances_with_radius(cache.radius, distances, factors);
1044 apply_hardness_to_distances(cache, distances);
1045 calc_brush_strength_factors(cache, brush, distances, factors);
1046
1047 const auto_mask::Cache *automask = auto_mask::active_cache_get(ss);
1048 auto_mask::calc_vert_factors(depsgraph, ob, automask, node, bm_verts, factors);
1049
1050 calc_brush_texture_factors(PaintMode::Sculpt, ss, brush, current_positions, factors);
1051
1052 scale_factors(factors, cache.bstrength);
1053
1054 switch (brush.cloth_deform_type) {
1057 math::normalize(cache.location_symm - cache.last_location_symm), factors, forces);
1058 apply_forces(cloth_sim, forces, verts);
1059 break;
1061 translations_from_offset_and_factors(-offset, factors, forces);
1062 apply_forces(cloth_sim, forces, verts);
1063 break;
1066 cloth_sim, verts, factors, falloff_plane.has_value(), cache.grab_delta_symm);
1067 break;
1069 apply_snake_hook_brush(cloth_sim, verts, factors, cache.grab_delta_symm);
1070 break;
1072 if (falloff_plane) {
1073 calc_plane_pinch_forces(positions, falloff_plane->plane, falloff_plane->normal, forces);
1074 }
1075 else {
1076 calc_pinch_forces(positions, cache.location_symm, forces);
1077 }
1078 scale_translations(forces, factors);
1079 apply_forces(cloth_sim, forces, verts);
1080 break;
1082 calc_perpendicular_pinch_forces(positions, imat, cache.location_symm, forces);
1083 scale_translations(forces, factors);
1084 apply_forces(cloth_sim, forces, verts);
1085 break;
1086 }
1088 gather_bmesh_normals(bm_verts, forces);
1089 scale_translations(forces, factors);
1090 apply_forces(cloth_sim, forces, verts);
1091 break;
1093 expand_length_constraints(cloth_sim, verts, factors);
1094 break;
1095 }
1096}
1097
1099 const Depsgraph &depsgraph)
1100{
1102 DEGObjectIterSettings deg_iter_settings = {nullptr};
1103 deg_iter_settings.depsgraph = &const_cast<Depsgraph &>(depsgraph);
1106 DEG_OBJECT_ITER_BEGIN (&deg_iter_settings, ob) {
1107 if (STREQ(object.id.name, ob->id.name)) {
1108 continue;
1109 }
1110
1111 CollisionModifierData *cmd = reinterpret_cast<CollisionModifierData *>(
1113 if (!cmd) {
1114 continue;
1115 }
1116
1117 if (!cmd->bvhtree) {
1118 continue;
1119 }
1120
1121 ColliderCache col{};
1122 col.ob = ob;
1123 col.collmd = cmd;
1124 collision_move_object(cmd, 1.0, 0.0, true);
1125 cache.append(col);
1126 }
1128 return cache;
1129}
1130
1135
1136static void cloth_brush_collision_cb(void *userdata,
1137 int index,
1138 const BVHTreeRay *ray,
1139 BVHTreeRayHit *hit)
1140{
1141 ClothBrushCollision *col = static_cast<ClothBrushCollision *>(userdata);
1142 CollisionModifierData *col_data = col->col_data;
1143 const int3 vert_tri = col_data->vert_tris[index];
1144 float (*positions)[3] = col_data->x;
1145 float *tri[3], no[3], co[3];
1146
1147 tri[0] = positions[vert_tri[0]];
1148 tri[1] = positions[vert_tri[1]];
1149 tri[2] = positions[vert_tri[2]];
1150 float dist = 0.0f;
1151
1152 bool tri_hit = isect_ray_tri_watertight_v3(
1153 ray->origin, &col->isect_precalc, UNPACK3(tri), &dist, nullptr);
1154 normal_tri_v3(no, UNPACK3(tri));
1155 madd_v3_v3v3fl(co, ray->origin, ray->direction, dist);
1156
1157 if (tri_hit && dist < hit->dist) {
1158 hit->index = index;
1159 hit->dist = dist;
1160
1161 copy_v3_v3(hit->co, co);
1162 copy_v3_v3(hit->no, no);
1163 }
1164}
1165
1166static void cloth_brush_solve_collision(const Object &object,
1167 SimulationData &cloth_sim,
1168 const int i)
1169{
1170 const int raycast_flag = BVH_RAYCAST_DEFAULT & ~BVH_RAYCAST_WATERTIGHT;
1171
1172 const float4x4 &object_to_world = object.object_to_world();
1173 const float4x4 &world_to_object = object.world_to_object();
1174
1175 for (const ColliderCache &collider_cache : cloth_sim.collider_list) {
1176 const float3 pos_world_space = math::transform_point(object_to_world, cloth_sim.pos[i]);
1177 const float3 prev_pos_world_space = math::transform_point(object_to_world,
1178 cloth_sim.last_iteration_pos[i]);
1179
1180 BVHTreeRayHit hit{};
1181 hit.index = -1;
1182
1183 const float3 ray_normal = math::normalize_and_get_length(
1184 pos_world_space - prev_pos_world_space, hit.dist);
1185
1187 CollisionModifierData *collmd = collider_cache.collmd;
1188 col.col_data = collmd;
1190
1192 prev_pos_world_space,
1193 ray_normal,
1194 0.3f,
1195 &hit,
1197 &col,
1198 raycast_flag);
1199
1200 if (hit.index == -1) {
1201 continue;
1202 }
1203
1204 const float3 collision_disp = float3(hit.no) * 0.005f;
1205
1206 float4 friction_plane;
1207 plane_from_point_normal_v3(friction_plane, hit.co, hit.no);
1208 float3 pos_on_friction_plane;
1209 closest_to_plane_v3(pos_on_friction_plane, friction_plane, pos_world_space);
1210 constexpr float friction_factor = 0.35f;
1211 const float3 movement_disp = (pos_on_friction_plane - float3(hit.co)) * friction_factor;
1212
1213 cloth_sim.pos[i] = math::transform_point(world_to_object,
1214 float3(hit.co) + movement_disp + collision_disp);
1215 }
1216}
1217
1219 const Brush *brush,
1220 const float3 &sim_location,
1221 const Span<int> verts,
1222 const MutableSpan<float> factors,
1223 LocalData &tls,
1224 SimulationData &cloth_sim)
1225{
1227 const SculptSession &ss = *object.runtime->sculpt_session;
1228
1229 tls.diffs.resize(verts.size());
1230 const MutableSpan<float3> pos_diff = tls.diffs;
1231 for (const int i : verts.index_range()) {
1232 pos_diff[i] = cloth_sim.pos[verts[i]] - cloth_sim.prev_pos[verts[i]];
1233 }
1234
1235 for (const int vert : verts) {
1236 cloth_sim.prev_pos[vert] = cloth_sim.pos[vert];
1237 }
1238
1239 for (const int i : verts.index_range()) {
1240 const int vert = verts[i];
1241 cloth_sim.pos[vert] += cloth_sim.acceleration[vert] * factors[i] * CLOTH_SIMULATION_TIME_STEP;
1242 }
1243
1244 scale_factors(factors, 1.0f - cloth_sim.damping);
1245 if (ss.cache) {
1246 const MutableSpan positions = gather_data_mesh(
1247 cloth_sim.init_pos.as_span(), verts, tls.positions);
1248 calc_brush_simulation_falloff(*brush, ss.cache->radius, sim_location, positions, factors);
1249 }
1250 scale_translations(pos_diff, factors);
1251
1252 for (const int i : verts.index_range()) {
1253 const int vert = verts[i];
1254 cloth_sim.pos[vert] += pos_diff[i];
1255 }
1256
1257 for (const int vert : verts) {
1258 cloth_brush_solve_collision(object, cloth_sim, vert);
1259 }
1260
1261 for (const int vert : verts) {
1262 cloth_sim.last_iteration_pos[vert] = cloth_sim.pos[vert];
1263 }
1264
1265 cloth_sim.acceleration.as_mutable_span().fill_indices(verts, float3(0));
1266}
1267
1268static void calc_constraint_factors(const Depsgraph &depsgraph,
1269 const Object &object,
1270 const Brush *brush,
1271 const float3 &sim_location,
1272 const Span<float3> init_positions,
1273 const MutableSpan<float> cloth_factors)
1274{
1275 const SculptSession &ss = *object.runtime->sculpt_session;
1276 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1277 IndexMaskMemory memory;
1278 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1279
1280 const auto_mask::Cache *automasking = auto_mask::active_cache_get(ss);
1281
1282 struct LocalData {
1285 };
1287 switch (pbvh.type()) {
1288 case bke::pbvh::Type::Mesh: {
1289 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
1290 const MeshAttributeData attribute_data(mesh);
1292 node_mask.foreach_index(
1293 [&](const int i) {
1294 LocalData &tls = all_tls.local();
1295 const Span<int> verts = nodes[i].verts();
1296 tls.factors.resize(verts.size());
1297 const MutableSpan<float> factors = tls.factors;
1299 attribute_data.hide_vert, attribute_data.mask, verts, factors);
1300 auto_mask::calc_vert_factors(depsgraph, object, automasking, nodes[i], verts, factors);
1301 if (ss.cache) {
1302 const MutableSpan positions = gather_data_mesh(init_positions, verts, tls.positions);
1304 *brush, ss.cache->radius, sim_location, positions, factors);
1305 }
1306 scatter_data_mesh(factors.as_span(), verts, cloth_factors);
1307 },
1309 break;
1310 }
1312 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1313 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1315 node_mask.foreach_index(
1316 [&](const int i) {
1317 LocalData &tls = all_tls.local();
1318 const Span<int> grids = nodes[i].grids();
1319 const int grid_verts_num = grids.size() * key.grid_area;
1320 tls.factors.resize(grid_verts_num);
1321 const MutableSpan<float> factors = tls.factors;
1322 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
1324 depsgraph, object, automasking, nodes[i], grids, factors);
1325 if (ss.cache) {
1326 const Span<float3> positions = gather_data_grids(
1327 subdiv_ccg, init_positions, grids, tls.positions);
1329 *brush, ss.cache->radius, sim_location, positions, factors);
1330 }
1331 scatter_data_grids(subdiv_ccg, factors.as_span(), grids, cloth_factors);
1332 },
1334 break;
1335 }
1337 const BMesh &bm = *ss.bm;
1339 node_mask.foreach_index(
1340 [&](const int i) {
1341 LocalData &tls = all_tls.local();
1343 const_cast<bke::pbvh::BMeshNode *>(&nodes[i]));
1344 tls.factors.resize(verts.size());
1345 const MutableSpan<float> factors = tls.factors;
1346 fill_factor_from_hide_and_mask(bm, verts, factors);
1347 auto_mask::calc_vert_factors(depsgraph, object, automasking, nodes[i], verts, factors);
1348 if (ss.cache) {
1349 const MutableSpan positions = gather_data_bmesh(
1350 init_positions, verts, tls.positions);
1352 *brush, ss.cache->radius, sim_location, positions, factors);
1353 }
1354 scatter_data_bmesh(factors.as_span(), verts, cloth_factors);
1355 },
1357 break;
1358 }
1359 }
1360}
1361
1362static void cloth_brush_satisfy_constraints(const Depsgraph &depsgraph,
1363 const Object &object,
1364 const Brush *brush,
1365 SimulationData &cloth_sim)
1366{
1368 const SculptSession &ss = *object.runtime->sculpt_session;
1369
1370 const float3 sim_location = cloth_brush_simulation_location_get(ss, brush);
1371
1372 /* Precalculate factors into an array since we need random access to specific vertex values. */
1373 Array<float> factors(vertex_count_get(object));
1374 calc_constraint_factors(depsgraph, object, brush, sim_location, cloth_sim.init_pos, factors);
1375
1376 for (int constraint_it = 0; constraint_it < CLOTH_SIMULATION_ITERATIONS; constraint_it++) {
1377 for (const LengthConstraint &constraint : cloth_sim.length_constraints) {
1378 if (cloth_sim.node_state[constraint.node] != SCULPT_CLOTH_NODE_ACTIVE) {
1379 /* Skip all constraints that were created for inactive nodes. */
1380 continue;
1381 }
1382
1383 const int v1 = constraint.elem_index_a;
1384 const int v2 = constraint.elem_index_b;
1385
1386 const float3 v1_to_v2 = float3(constraint.elem_position_b) -
1387 float3(constraint.elem_position_a);
1388 const float current_distance = math::length(v1_to_v2);
1389 float3 correction_vector;
1390
1391 const float constraint_distance = constraint.length +
1392 (cloth_sim.length_constraint_tweak[v1] * 0.5f) +
1393 (cloth_sim.length_constraint_tweak[v2] * 0.5f);
1394
1395 if (current_distance > 0.0f) {
1396 correction_vector = v1_to_v2 * CLOTH_SOLVER_DISPLACEMENT_FACTOR *
1397 (1.0f - (constraint_distance / current_distance));
1398 }
1399 else {
1400 correction_vector = v1_to_v2 * CLOTH_SOLVER_DISPLACEMENT_FACTOR;
1401 }
1402
1403 const float3 correction_vector_half = correction_vector * 0.5f;
1404
1405 const float factor_v1 = factors[v1];
1406 const float factor_v2 = factors[v2];
1407
1408 float deformation_strength = 1.0f;
1409 if (constraint.type == SCULPT_CLOTH_CONSTRAINT_DEFORMATION) {
1410 deformation_strength = (cloth_sim.deformation_strength[v1] +
1411 cloth_sim.deformation_strength[v2]) *
1412 0.5f;
1413 }
1414
1415 if (constraint.type == SCULPT_CLOTH_CONSTRAINT_SOFTBODY) {
1416 const float softbody_plasticity = brush ? brush->cloth_constraint_softbody_strength : 0.0f;
1417 cloth_sim.pos[v1] += correction_vector_half *
1418 (1.0f * factor_v1 * constraint.strength * softbody_plasticity);
1419 cloth_sim.softbody_pos[v1] += correction_vector_half * -1.0f * factor_v1 *
1420 constraint.strength * (1.0f - softbody_plasticity);
1421 }
1422 else {
1423 cloth_sim.pos[v1] += correction_vector_half * 1.0f * factor_v1 * constraint.strength *
1424 deformation_strength;
1425 if (v1 != v2) {
1426 cloth_sim.pos[v2] += correction_vector_half * -1.0f * factor_v2 * constraint.strength *
1427 deformation_strength;
1428 }
1429 }
1430 }
1431 }
1432}
1433
1434void do_simulation_step(const Depsgraph &depsgraph,
1435 const Sculpt &sd,
1436 Object &object,
1437 SimulationData &cloth_sim,
1438 const IndexMask &node_mask)
1439{
1441 SculptSession &ss = *object.runtime->sculpt_session;
1444
1445 /* Update the constraints. */
1446 cloth_brush_satisfy_constraints(depsgraph, object, brush, cloth_sim);
1447
1448 const float3 sim_location = cloth_brush_simulation_location_get(ss, brush);
1449
1450 IndexMaskMemory memory;
1452 switch (pbvh.type()) {
1453 case bke::pbvh::Type::Mesh: {
1455 const IndexMask active_nodes = IndexMask::from_predicate(
1456 node_mask, memory, [&](const int i) {
1457 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
1458 return cloth_sim.node_state[node_index] == SCULPT_CLOTH_NODE_ACTIVE;
1459 });
1460 Mesh &mesh = *id_cast<Mesh *>(object.data);
1461 const MeshAttributeData attribute_data(mesh);
1462 const PositionDeformData position_data(depsgraph, object);
1463 active_nodes.foreach_index(
1464 [&](const int i) {
1465 LocalData &tls = all_tls.local();
1466 const Span<int> verts = nodes[i].verts();
1467
1468 tls.factors.resize(verts.size());
1469 const MutableSpan<float> factors = tls.factors;
1471 attribute_data.hide_vert, attribute_data.mask, verts, factors);
1472 const auto_mask::Cache *automasking = auto_mask::active_cache_get(ss);
1473 auto_mask::calc_vert_factors(depsgraph, object, automasking, nodes[i], verts, factors);
1474
1475 solve_verts_simulation(object, brush, sim_location, verts, factors, tls, cloth_sim);
1476
1477 tls.translations.resize(verts.size());
1478 const MutableSpan<float3> translations = tls.translations;
1479 for (const int i : verts.index_range()) {
1480 translations[i] = cloth_sim.pos[verts[i]] - position_data.eval[verts[i]];
1481 }
1482
1483 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
1484 position_data.deform(translations, verts);
1485
1486 cloth_sim.node_state[cloth_sim.node_state_index.lookup(&nodes[i])] =
1488 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
1489 },
1491 break;
1492 }
1495 const IndexMask active_nodes = IndexMask::from_predicate(
1496 node_mask, memory, [&](const int i) {
1497 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
1498 return cloth_sim.node_state[node_index] == SCULPT_CLOTH_NODE_ACTIVE;
1499 });
1500 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1501 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
1502 const Span<float3> cloth_positions = cloth_sim.pos;
1503 MutableSpan<float3> positions = subdiv_ccg.positions;
1504 active_nodes.foreach_index(
1505 [&](const int i) {
1506 LocalData &tls = all_tls.local();
1507 const Span<int> grids = nodes[i].grids();
1508 const int grid_verts_num = grids.size() * key.grid_area;
1509
1510 tls.factors.resize(grid_verts_num);
1511 const MutableSpan<float> factors = tls.factors;
1512 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
1513 const auto_mask::Cache *automasking = auto_mask::active_cache_get(ss);
1515 depsgraph, object, automasking, nodes[i], grids, factors);
1516
1517 const Span<int> verts = calc_vert_indices_grids(key, grids, tls.vert_indices);
1518 solve_verts_simulation(object, brush, sim_location, verts, factors, tls, cloth_sim);
1519
1520 for (const int grid : grids) {
1521 const IndexRange grid_range = bke::ccg::grid_range(key, grid);
1522 positions.slice(grid_range).copy_from(cloth_positions.slice(grid_range));
1523 }
1524
1525 cloth_sim.node_state[cloth_sim.node_state_index.lookup(&nodes[i])] =
1527 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
1528 },
1530 break;
1531 }
1534 const IndexMask active_nodes = IndexMask::from_predicate(
1535 node_mask, memory, [&](const int i) {
1536 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
1537 return cloth_sim.node_state[node_index] == SCULPT_CLOTH_NODE_ACTIVE;
1538 });
1539 BMesh &bm = *ss.bm;
1540 active_nodes.foreach_index(
1541 [&](const int i) {
1542 LocalData &tls = all_tls.local();
1544
1545 tls.factors.resize(verts.size());
1546 const MutableSpan<float> factors = tls.factors;
1547 fill_factor_from_hide_and_mask(bm, verts, factors);
1548 const auto_mask::Cache *automasking = auto_mask::active_cache_get(ss);
1549 auto_mask::calc_vert_factors(depsgraph, object, automasking, nodes[i], verts, factors);
1550
1551 const Span<int> vert_indices = calc_vert_indices_bmesh(verts, tls.vert_indices);
1553 object, brush, sim_location, vert_indices, factors, tls, cloth_sim);
1554
1555 for (BMVert *vert : verts) {
1556 copy_v3_v3(vert->co, cloth_sim.pos[BM_elem_index_get(vert)]);
1557 }
1558
1559 cloth_sim.node_state[cloth_sim.node_state_index.lookup(&nodes[i])] =
1562 },
1564 break;
1565 }
1566 }
1567 pbvh.tag_positions_changed(node_mask);
1568 pbvh.flush_bounds_to_parents();
1569}
1570
1571static void cloth_brush_apply_brush_forces(const Depsgraph &depsgraph,
1572 const Sculpt &sd,
1573 Object &ob,
1574 const IndexMask &node_mask)
1575{
1577 StrokeCache &cache = *ss.cache;
1579
1580 float3 area_no;
1581 float3 area_co;
1582 float3 offset;
1583
1584 if (math::is_zero(cache.grab_delta_symm)) {
1585 return;
1586 }
1587
1588 float3 grab_delta = math::normalize(cache.grab_delta_symm);
1589
1590 /* Calculate push offset. */
1591 if (brush.cloth_deform_type == BRUSH_CLOTH_DEFORM_PUSH) {
1592 offset = cache.sculpt_normal_symm * cache.radius * cache.scale * 2.0f;
1593 }
1594
1596 if (brush.cloth_deform_type == BRUSH_CLOTH_DEFORM_PINCH_PERPENDICULAR ||
1597 brush.cloth_force_falloff_type == BRUSH_CLOTH_FORCE_FALLOFF_PLANE)
1598 {
1599 calc_brush_plane(depsgraph, brush, ob, node_mask, area_no, area_co);
1600
1601 /* Initialize stroke local space matrix. */
1602 mat.x_axis() = math::cross(area_no, cache.grab_delta_symm);
1603 mat.y_axis() = math::cross(area_no, mat.x_axis());
1604 mat.z_axis() = area_no;
1605 mat.location() = cache.location_symm;
1606 normalize_m4(mat.ptr());
1607
1608 /* Update matrix for the cursor preview. */
1609 if (cache.mirror_symmetry_pass == 0) {
1610 cache.stroke_local_mat = mat;
1611 }
1612 }
1613
1615 /* Set the deformation strength to 0. Brushes will initialize the strength in the required
1616 * area. */
1617 cache.cloth_sim->deformation_strength.fill(0.0f);
1618 }
1619
1620 const float3 sim_location = cloth_brush_simulation_location_get(ss, &brush);
1621
1622 /* Gravity */
1623 float3 gravity(0);
1624 if (cache.supports_gravity) {
1625 gravity += cache.gravity_direction_symm * -sd.gravity_factor;
1626 }
1627
1628 std::optional<FalloffPlane> falloff_plane;
1629 if (brush.cloth_force_falloff_type == BRUSH_CLOTH_FORCE_FALLOFF_PLANE) {
1630 falloff_plane.emplace();
1631 falloff_plane->normal = math::normalize(grab_delta);
1632 plane_from_point_normal_v3(falloff_plane->plane, area_co, falloff_plane->normal);
1633 }
1634
1636
1638 switch (pbvh.type()) {
1639 case bke::pbvh::Type::Mesh: {
1640 const Mesh &mesh = *id_cast<Mesh *>(ob.data);
1641 const MeshAttributeData attribute_data(mesh);
1642 const Span<float3> positions_eval = bke::pbvh::vert_positions_eval(depsgraph, ob);
1643 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, ob);
1645 node_mask.foreach_index(
1646 [&](const int i) {
1647 calc_forces_mesh(depsgraph,
1648 ob,
1649 brush,
1650 offset,
1651 mat,
1652 sim_location,
1653 gravity,
1654 falloff_plane,
1655 attribute_data,
1656 positions_eval,
1657 vert_normals,
1658 nodes[i]);
1659 },
1661 break;
1662 }
1665 node_mask.foreach_index(
1666 [&](const int i) {
1667 LocalData &tls = all_tls.local();
1668 calc_forces_grids(depsgraph,
1669 ob,
1670 brush,
1671 offset,
1672 mat,
1673 sim_location,
1674 gravity,
1675 falloff_plane,
1676 nodes[i],
1677 tls);
1678 },
1680 break;
1681 }
1684 node_mask.foreach_index(
1685 [&](const int i) {
1686 LocalData &tls = all_tls.local();
1687 calc_forces_bmesh(depsgraph,
1688 ob,
1689 brush,
1690 offset,
1691 mat,
1692 sim_location,
1693 gravity,
1694 falloff_plane,
1695 nodes[i],
1696 tls);
1697 },
1699 break;
1700 }
1701 }
1702}
1703
1704/* Allocates nodes state and initializes them to Uninitialized, so constraints can be created for
1705 * them. */
1707{
1709 IndexMaskMemory memory;
1710 const IndexMask node_mask = bke::pbvh::all_leaf_nodes(pbvh, memory);
1712
1713 switch (pbvh.type()) {
1714 case bke::pbvh::Type::Mesh: {
1716 node_mask.foreach_index([&](const int i) { cloth_sim.node_state_index.add(&nodes[i], i); });
1717 break;
1718 }
1721 node_mask.foreach_index([&](const int i) { cloth_sim.node_state_index.add(&nodes[i], i); });
1722 break;
1723 }
1726 node_mask.foreach_index([&](const int i) { cloth_sim.node_state_index.add(&nodes[i], i); });
1727 break;
1728 }
1729 }
1730}
1731
1732static void copy_positions_to_array(const Depsgraph &depsgraph,
1733 const Object &object,
1734 MutableSpan<float3> positions)
1735{
1736 const SculptSession &ss = *object.runtime->sculpt_session;
1737 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1738 switch (pbvh.type()) {
1740 positions.copy_from(bke::pbvh::vert_positions_eval(depsgraph, object));
1741 break;
1743 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1744 positions.copy_from(subdiv_ccg.positions);
1745 break;
1746 }
1748 BM_mesh_vert_coords_get(ss.bm, positions);
1749 break;
1750 }
1751}
1752
1753static void copy_normals_to_array(const Depsgraph &depsgraph,
1754 const Object &object,
1755 MutableSpan<float3> normals)
1756{
1757 const SculptSession &ss = *object.runtime->sculpt_session;
1758 const bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
1759 switch (pbvh.type()) {
1761 normals.copy_from(bke::pbvh::vert_normals_eval(depsgraph, object));
1762 break;
1764 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
1765 normals.copy_from(subdiv_ccg.normals);
1766 break;
1767 }
1769 BM_mesh_vert_normals_get(ss.bm, normals);
1770 break;
1771 }
1772}
1773
1774std::unique_ptr<SimulationData> brush_simulation_create(const Depsgraph &depsgraph,
1775 Object &ob,
1776 const float cloth_mass,
1777 const float cloth_damping,
1778 const float cloth_softbody_strength,
1779 const bool use_collisions,
1780 const bool needs_deform_coords)
1781{
1783 const int totverts = vertex_count_get(ob);
1784 std::unique_ptr<SimulationData> cloth_sim = std::make_unique<SimulationData>();
1785
1786 cloth_sim->length_constraints.reserve(CLOTH_LENGTH_CONSTRAINTS_BLOCK);
1787
1788 cloth_sim->acceleration = Array<float3>(totverts, float3(0));
1789 cloth_sim->pos = Array<float3>(totverts, float3(0));
1790 cloth_sim->length_constraint_tweak = Array<float>(totverts, 0.0f);
1791
1792 cloth_sim->init_pos.reinitialize(totverts);
1793 copy_positions_to_array(depsgraph, ob, cloth_sim->init_pos);
1794
1795 cloth_sim->last_iteration_pos = cloth_sim->init_pos;
1796 cloth_sim->prev_pos = cloth_sim->init_pos;
1797
1798 cloth_sim->init_no.reinitialize(totverts);
1799 copy_normals_to_array(depsgraph, ob, cloth_sim->init_no);
1800
1801 if (needs_deform_coords) {
1802 cloth_sim->deformation_pos = cloth_sim->init_pos;
1803 cloth_sim->deformation_strength = Array<float>(totverts, 1.0f);
1804 }
1805
1806 if (cloth_softbody_strength > 0.0f) {
1807 cloth_sim->softbody_pos = cloth_sim->init_pos;
1808 }
1809
1810 cloth_sim->mass = cloth_mass;
1811 cloth_sim->damping = cloth_damping;
1812 cloth_sim->softbody_strength = cloth_softbody_strength;
1813
1814 if (use_collisions) {
1815 cloth_sim->collider_list = cloth_brush_collider_cache_create(ob, depsgraph);
1816 }
1817
1819
1820 return cloth_sim;
1821}
1822
1823void brush_store_simulation_state(const Depsgraph &depsgraph,
1824 const Object &object,
1825 SimulationData &cloth_sim)
1826{
1828 copy_positions_to_array(depsgraph, object, cloth_sim.pos);
1829}
1830
1831void sim_activate_nodes(Object &object, SimulationData &cloth_sim, const IndexMask &node_mask)
1832{
1835
1836 /* Activate the nodes inside the simulation area. */
1837 switch (pbvh.type()) {
1838 case bke::pbvh::Type::Mesh: {
1840 node_mask.foreach_index([&](const int i) {
1841 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
1842 cloth_sim.node_state[node_index] = SCULPT_CLOTH_NODE_ACTIVE;
1843 });
1844 break;
1845 }
1848 node_mask.foreach_index([&](const int i) {
1849 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
1850 cloth_sim.node_state[node_index] = SCULPT_CLOTH_NODE_ACTIVE;
1851 });
1852 break;
1853 }
1856 node_mask.foreach_index([&](const int i) {
1857 const int node_index = cloth_sim.node_state_index.lookup(&nodes[i]);
1858 cloth_sim.node_state[node_index] = SCULPT_CLOTH_NODE_ACTIVE;
1859 });
1860 break;
1861 }
1862 }
1863}
1864
1866 Object &ob,
1867 const IndexMask &node_mask)
1868{
1871 const float radius = ss.cache->initial_radius;
1872 const float limit = radius + (radius * brush->cloth_sim_limit);
1873 const float3 sim_location = cloth_brush_simulation_location_get(ss, brush);
1874 ensure_nodes_constraints(sd, ob, node_mask, *ss.cache->cloth_sim, sim_location, limit);
1875}
1876
1877void do_cloth_brush(const Depsgraph &depsgraph,
1878 const Sculpt &sd,
1879 Object &ob,
1880 const IndexMask &node_mask)
1881{
1885
1886 if (!ss.cache->cloth_sim) {
1887 ss.cache->cloth_sim = brush_simulation_create(depsgraph,
1888 ob,
1889 brush->cloth_mass,
1890 brush->cloth_damping,
1891 brush->cloth_constraint_softbody_strength,
1894 }
1895
1897 if (brush->cloth_simulation_area_type == BRUSH_CLOTH_SIMULATION_AREA_LOCAL) {
1898 /* When using simulation a fixed local simulation area, constraints are created only using
1899 * the initial stroke position and initial radius (per symmetry pass) instead of per node.
1900 * This allows to skip unnecessary constraints that will never be simulated, making the
1901 * solver faster. When the simulation starts for a node, the node gets activated and all its
1902 * constraints are considered final. As the same node can be included inside the brush radius
1903 * from multiple symmetry passes, the cloth brush can't activate the node for simulation yet
1904 * as this will cause the ensure constraints function to skip the node in the next symmetry
1905 * passes. It needs to build the constraints here and skip simulating the first step, so all
1906 * passes can add their constraints to all affected nodes. */
1908 }
1909 /* The first step of a symmetry pass is never simulated as deformation modes need valid delta
1910 * for brush tip alignment. */
1911 return;
1912 }
1913
1914 /* Ensure the constraints for the nodes. */
1916
1917 /* Store the initial state in the simulation. */
1918 brush_store_simulation_state(depsgraph, ob, *ss.cache->cloth_sim);
1919
1920 /* Enable the nodes that should be simulated. */
1921 sim_activate_nodes(ob, *ss.cache->cloth_sim, node_mask);
1922
1923 /* Apply forces to the vertices. */
1924 cloth_brush_apply_brush_forces(depsgraph, sd, ob, node_mask);
1925
1926 /* Update and write the simulation to the nodes. */
1927 do_simulation_step(depsgraph, sd, ob, *ss.cache->cloth_sim, node_mask);
1928}
1929
1931
1932void simulation_limits_draw(const uint gpuattr,
1933 const Brush &brush,
1934 const float location[3],
1935 const float normal[3],
1936 const float rds,
1937 const float line_width,
1938 const float outline_col[3],
1939 const float alpha)
1940{
1941 float cursor_trans[4][4], cursor_rot[4][4];
1942 const float z_axis[4] = {0.0f, 0.0f, 1.0f, 0.0f};
1943 float quat[4];
1944 unit_m4(cursor_trans);
1945 translate_m4(cursor_trans, location[0], location[1], location[2]);
1946 rotation_between_vecs_to_quat(quat, z_axis, normal);
1947 quat_to_mat4(cursor_rot, quat);
1949 GPU_matrix_mul(cursor_trans);
1950 GPU_matrix_mul(cursor_rot);
1951
1952 GPU_line_width(line_width);
1953 immUniformColor3fvAlpha(outline_col, alpha * 0.5f);
1955 gpuattr, 0, 0, rds + (rds * brush.cloth_sim_limit * brush.cloth_sim_falloff), 320);
1956 immUniformColor3fvAlpha(outline_col, alpha * 0.7f);
1957 imm_draw_circle_wire_3d(gpuattr, 0, 0, rds + rds * brush.cloth_sim_limit, 80);
1959}
1960
1962 Brush &brush,
1963 SculptSession &ss,
1964 const float outline_col[3],
1965 float outline_alpha)
1966{
1967 float4x4 local_mat = ss.cache->stroke_local_mat;
1968
1969 if (brush.cloth_deform_type == BRUSH_CLOTH_DEFORM_GRAB) {
1970 add_v3_v3v3(local_mat[3], ss.cache->location, ss.cache->grab_delta);
1971 }
1972
1973 GPU_matrix_mul(local_mat.ptr());
1974
1975 const float dist = ss.cache->radius;
1976 const float arrow_x = ss.cache->radius * 0.2f;
1977 const float arrow_y = ss.cache->radius * 0.1f;
1978
1979 immUniformColor3fvAlpha(outline_col, outline_alpha);
1980 GPU_line_width(2.0f);
1982 immVertex3f(gpuattr, dist, 0.0f, 0.0f);
1983 immVertex3f(gpuattr, -dist, 0.0f, 0.0f);
1984 immEnd();
1985
1987 immVertex3f(gpuattr, dist, 0.0f, 0.0f);
1988 immVertex3f(gpuattr, dist - arrow_x, arrow_y, 0.0f);
1989 immVertex3f(gpuattr, dist - arrow_x, -arrow_y, 0.0f);
1990
1991 immVertex3f(gpuattr, -dist, 0.0f, 0.0f);
1992 immVertex3f(gpuattr, -dist + arrow_x, arrow_y, 0.0f);
1993 immVertex3f(gpuattr, -dist + arrow_x, -arrow_y, 0.0f);
1994
1995 immEnd();
1996}
1997
1998/* Cloth Filter. */
1999
2007
2009 {int(ClothFilterType::Gravity), "GRAVITY", 0, "Gravity", "Applies gravity to the simulation"},
2010 {int(ClothFilterType::Inflate), "INFLATE", 0, "Inflate", "Inflates the cloth"},
2011 {int(ClothFilterType::Expand), "EXPAND", 0, "Expand", "Expands the cloth's dimensions"},
2013 "PINCH",
2014 0,
2015 "Pinch",
2016 "Pulls the cloth to the cursor's start position"},
2018 "SCALE",
2019 0,
2020 "Scale",
2021 "Scales the mesh as a soft body using the origin of the object as scale"},
2022 {0, nullptr, 0, nullptr, nullptr},
2023};
2024
2027 "LOCAL",
2028 0,
2029 "Local",
2030 "Use the local axis to limit the force and set the gravity direction"},
2032 "WORLD",
2033 0,
2034 "World",
2035 "Use the global axis to limit the force and set the gravity direction"},
2037 "VIEW",
2038 0,
2039 "View",
2040 "Use the view axis to limit the force and set the gravity direction"},
2041 {0, nullptr, 0, nullptr, nullptr},
2042};
2043
2049
2051 {CLOTH_FILTER_FORCE_X, "X", 0, "X", "Apply force in the X axis"},
2052 {CLOTH_FILTER_FORCE_Y, "Y", 0, "Y", "Apply force in the Y axis"},
2053 {CLOTH_FILTER_FORCE_Z, "Z", 0, "Z", "Apply force in the Z axis"},
2054 {0, nullptr, 0, nullptr, nullptr},
2055};
2056
2058{
2059 return ELEM(filter_type, ClothFilterType::Scale);
2060}
2061
2063 const filter::Cache &filter_cache,
2064 const MutableSpan<float3> forces)
2065{
2066 const float3x3 to_object_space = filter::to_object_space(filter_cache);
2067 for (const int i : forces.index_range()) {
2068 float3 force(0.0f);
2069 if (filter_cache.orientation == filter::FilterOrientation::View) {
2070 /* When using the view orientation apply gravity in the -Y axis, this way objects will
2071 * fall down instead of backwards. */
2072 force[1] = -factors[i];
2073 }
2074 else {
2075 force[2] = -factors[i];
2076 }
2077 forces[i] = to_object_space * force;
2078 }
2079}
2080
2088
2090 const Span<int> verts,
2091 const Span<float> factors,
2092 FilterLocalData &tls)
2093{
2094 const MutableSpan translations = gather_data_mesh(
2095 filter_cache.cloth_sim->init_pos.as_span(), verts, tls.forces);
2096 scale_translations(translations, factors);
2097 filter::zero_disabled_axis_components(filter_cache, translations);
2098 for (const int i : verts.index_range()) {
2099 filter_cache.cloth_sim->deformation_pos[verts[i]] =
2100 filter_cache.cloth_sim->init_pos[verts[i]] + translations[i];
2101 }
2102}
2103
2104static void apply_filter_forces_mesh(const Depsgraph &depsgraph,
2105 const ClothFilterType filter_type,
2106 const float filter_strength,
2107 const float3 &gravity,
2108 const Span<float3> positions_eval,
2109 const Span<float3> vert_normals,
2110 const GroupedSpan<int> vert_to_face_map,
2111 const MeshAttributeData &attribute_data,
2113 Object &object,
2114 FilterLocalData &tls)
2115{
2116 const SculptSession &ss = *object.runtime->sculpt_session;
2117 SimulationData &cloth_sim = *ss.filter_cache->cloth_sim;
2118
2119 const Span<int> verts = node.verts();
2120
2121 tls.factors.resize(verts.size());
2122 const MutableSpan<float> factors = tls.factors;
2123 fill_factor_from_hide_and_mask(attribute_data.hide_vert, attribute_data.mask, verts, factors);
2124 const auto_mask::Cache *automasking = auto_mask::active_cache_get(ss);
2125 auto_mask::calc_vert_factors(depsgraph, object, automasking, node, verts, factors);
2126
2127 if (ss.filter_cache->active_face_set != face_set_none_id) {
2128 for (const int i : verts.index_range()) {
2129 const int vert = verts[i];
2131 vert_to_face_map, attribute_data.face_sets, vert, ss.filter_cache->active_face_set))
2132 {
2133 factors[i] = 0.0f;
2134 }
2135 }
2136 }
2137
2138 scale_factors(factors, filter_strength);
2139
2140 tls.forces.resize(verts.size());
2141 const MutableSpan<float3> forces = tls.forces;
2142 if (!math::is_zero(gravity)) {
2143 forces.fill(gravity);
2144 apply_forces(cloth_sim, forces, verts);
2145 }
2146
2147 switch (filter_type) {
2149 calc_gravity_forces(factors, *ss.filter_cache, forces);
2150 apply_forces(cloth_sim, tls.forces, verts);
2151 break;
2153 gather_data_mesh(vert_normals, verts, forces);
2154 scale_translations(forces, factors);
2155 apply_forces(cloth_sim, tls.forces, verts);
2156 break;
2158 expand_length_constraints(cloth_sim, verts, factors);
2159 break;
2162
2163 gather_data_mesh(positions_eval, verts, tls.positions),
2165 forces);
2166 scale_translations(forces, factors);
2167 apply_forces(cloth_sim, tls.forces, verts);
2168 break;
2170 apply_scale_filter(*ss.filter_cache, verts, factors, tls);
2171 break;
2172 }
2173}
2174
2175static void apply_filter_forces_grids(const Depsgraph &depsgraph,
2176 const Span<int> face_sets,
2177 const ClothFilterType filter_type,
2178 const float filter_strength,
2179 const float3 &gravity,
2181 Object &object,
2182 FilterLocalData &tls)
2183{
2184 const SculptSession &ss = *object.runtime->sculpt_session;
2185 SimulationData &cloth_sim = *ss.filter_cache->cloth_sim;
2186 const SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
2187 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
2188
2189 const Span<int> grids = node.grids();
2190 const int grid_verts_num = grids.size() * key.grid_area;
2191
2192 tls.factors.resize(grid_verts_num);
2193 const MutableSpan<float> factors = tls.factors;
2194 fill_factor_from_hide_and_mask(subdiv_ccg, grids, factors);
2195 const auto_mask::Cache *automasking = auto_mask::active_cache_get(ss);
2196 auto_mask::calc_grids_factors(depsgraph, object, automasking, node, grids, factors);
2197
2198 if (ss.filter_cache->active_face_set != face_set_none_id) {
2199 for (const int i : grids.index_range()) {
2201 subdiv_ccg, face_sets, grids[i], ss.filter_cache->active_face_set))
2202 {
2203 factors.slice(i * key.grid_area, key.grid_area).fill(0.0f);
2204 }
2205 }
2206 }
2207
2208 scale_factors(factors, filter_strength);
2209
2210 const Span<int> verts = calc_vert_indices_grids(key, grids, tls.vert_indices);
2211
2212 tls.forces.resize(verts.size());
2213 const MutableSpan<float3> forces = tls.forces;
2214 if (!math::is_zero(gravity)) {
2215 forces.fill(gravity);
2216 apply_forces(cloth_sim, forces, verts);
2217 }
2218
2219 switch (filter_type) {
2221 calc_gravity_forces(factors, *ss.filter_cache, forces);
2222 apply_forces(cloth_sim, tls.forces, verts);
2223 break;
2225 gather_grids_normals(subdiv_ccg, grids, forces);
2226 scale_translations(forces, factors);
2227 apply_forces(cloth_sim, tls.forces, verts);
2228 break;
2230 expand_length_constraints(cloth_sim, verts, factors);
2231 break;
2234
2235 gather_grids_positions(subdiv_ccg, grids, tls.positions),
2237 forces);
2238 scale_translations(forces, factors);
2239 apply_forces(cloth_sim, tls.forces, verts);
2240 break;
2242 apply_scale_filter(*ss.filter_cache, verts, factors, tls);
2243 break;
2244 }
2245}
2246
2247static void apply_filter_forces_bmesh(const Depsgraph &depsgraph,
2248 const ClothFilterType filter_type,
2249 const float filter_strength,
2250 const float3 &gravity,
2252 Object &object,
2253 FilterLocalData &tls)
2254{
2255 const SculptSession &ss = *object.runtime->sculpt_session;
2256 SimulationData &cloth_sim = *ss.filter_cache->cloth_sim;
2257 const BMesh &bm = *ss.bm;
2258
2260
2261 tls.factors.resize(verts.size());
2262 const MutableSpan<float> factors = tls.factors;
2263 fill_factor_from_hide_and_mask(bm, verts, factors);
2264 const auto_mask::Cache *automasking = auto_mask::active_cache_get(ss);
2265 auto_mask::calc_vert_factors(depsgraph, object, automasking, node, verts, factors);
2266
2267 if (ss.filter_cache->active_face_set != face_set_none_id) {
2268 const int face_set_offset = CustomData_get_offset_named(
2269 &bm.pdata, CD_PROP_INT32, ".sculpt_face_set");
2270 int i = 0;
2271 for (const BMVert *vert : verts) {
2272 if (!face_set::vert_has_face_set(face_set_offset, *vert, ss.filter_cache->active_face_set)) {
2273 factors[i] = 0.0f;
2274 }
2275 i++;
2276 }
2277 }
2278
2279 scale_factors(factors, filter_strength);
2280
2281 const Span<int> vert_indices = calc_vert_indices_bmesh(verts, tls.vert_indices);
2282
2283 tls.forces.resize(verts.size());
2284 const MutableSpan<float3> forces = tls.forces;
2285 if (!math::is_zero(gravity)) {
2286 forces.fill(gravity);
2287 apply_forces(cloth_sim, forces, vert_indices);
2288 }
2289
2290 switch (filter_type) {
2292 calc_gravity_forces(factors, *ss.filter_cache, forces);
2293 apply_forces(cloth_sim, tls.forces, vert_indices);
2294 break;
2296 gather_bmesh_normals(verts, forces);
2297 scale_translations(forces, factors);
2298 apply_forces(cloth_sim, tls.forces, vert_indices);
2299 break;
2301 expand_length_constraints(cloth_sim, vert_indices, factors);
2302 break;
2305
2308 forces);
2309 scale_translations(forces, factors);
2310 apply_forces(cloth_sim, tls.forces, vert_indices);
2311 break;
2313 apply_scale_filter(*ss.filter_cache, vert_indices, factors, tls);
2314 break;
2315 }
2316}
2317
2319 wmOperator *op,
2320 const wmEvent *event)
2321{
2322 Object &object = *CTX_data_active_object(C);
2323 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
2324 SculptSession &ss = *object.runtime->sculpt_session;
2325 const Sculpt &sd = *CTX_data_tool_settings(C)->sculpt;
2326 const ClothFilterType filter_type = ClothFilterType(RNA_enum_get(op->ptr, "type"));
2327 float filter_strength = RNA_float_get(op->ptr, "strength");
2328
2329 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
2330 MEM_delete(ss.filter_cache);
2331 ss.filter_cache = nullptr;
2332 undo::push_end(object);
2334 return OPERATOR_FINISHED;
2335 }
2336
2337 if (event->type != MOUSEMOVE) {
2339 }
2340
2341 const float len = event->prev_press_xy[0] - event->xy[0];
2342 filter_strength = filter_strength * -len * 0.001f * UI_SCALE_FAC;
2343
2345
2346 BKE_sculptsession_update_for_edit(depsgraph, &object, false);
2347
2348 brush_store_simulation_state(*depsgraph, object, *ss.filter_cache->cloth_sim);
2349
2350 const IndexMask &node_mask = ss.filter_cache->node_mask;
2351
2352 if (auto_mask::is_enabled(sd.paint, object, nullptr) && ss.filter_cache->automasking &&
2354 {
2355 ss.filter_cache->automasking->calc_cavity_factor(*depsgraph, object, node_mask);
2356 }
2357
2358 float3 gravity(0.0f);
2359 if (sd.gravity_object) {
2360 gravity = sd.gravity_object->object_to_world().ptr()[2];
2361 }
2362 else {
2363 gravity[2] = -1.0f;
2364 }
2365 gravity *= sd.gravity_factor * filter_strength;
2366
2368
2370 switch (pbvh.type()) {
2371 case bke::pbvh::Type::Mesh: {
2372 const Span<float3> positions_eval = bke::pbvh::vert_positions_eval(*depsgraph, object);
2373 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(*depsgraph, object);
2374 const Mesh &mesh = *id_cast<const Mesh *>(object.data);
2375 const GroupedSpan<int> vert_to_face_map = mesh.vert_to_face_map();
2376 const MeshAttributeData attribute_data(mesh);
2378 node_mask.foreach_index(
2379 [&](const int i) {
2380 FilterLocalData &tls = all_tls.local();
2381 apply_filter_forces_mesh(*depsgraph,
2382 filter_type,
2383 filter_strength,
2384 gravity,
2385 positions_eval,
2386 vert_normals,
2387 vert_to_face_map,
2388 attribute_data,
2389 nodes[i],
2390 object,
2391 tls);
2392 bke::pbvh::update_node_bounds_mesh(positions_eval, nodes[i]);
2393 },
2395 break;
2396 }
2398 const Mesh &base_mesh = *id_cast<const Mesh *>(object.data);
2399 const bke::AttributeAccessor attributes = base_mesh.attributes();
2400 const VArraySpan face_sets = *attributes.lookup<int>(".sculpt_face_set",
2402 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
2403 MutableSpan<float3> positions = subdiv_ccg.positions;
2405 node_mask.foreach_index(
2406 [&](const int i) {
2407 FilterLocalData &tls = all_tls.local();
2408 apply_filter_forces_grids(*depsgraph,
2409 face_sets,
2410 filter_type,
2411 filter_strength,
2412 gravity,
2413 nodes[i],
2414 object,
2415 tls);
2416 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
2417 },
2419 break;
2420 }
2423 node_mask.foreach_index(
2424 [&](const int i) {
2425 FilterLocalData &tls = all_tls.local();
2427 *depsgraph, filter_type, filter_strength, gravity, nodes[i], object, tls);
2429 },
2431 break;
2432 }
2433 }
2434 pbvh.tag_positions_changed(node_mask);
2435 pbvh.flush_bounds_to_parents();
2436
2437 /* Activate all nodes. */
2438 sim_activate_nodes(object, *ss.filter_cache->cloth_sim, node_mask);
2439
2440 /* Update and write the simulation to the nodes. */
2441 do_simulation_step(*depsgraph, sd, object, *ss.filter_cache->cloth_sim, node_mask);
2442
2445}
2446
2448 wmOperator *op,
2449 const wmEvent *event)
2450{
2451 const Scene &scene = *CTX_data_scene(C);
2453 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2456
2457 const View3D *v3d = CTX_wm_view3d(C);
2458 const Base *base = CTX_data_active_base(C);
2459 if (!BKE_base_is_visible(v3d, base)) {
2460 return OPERATOR_CANCELLED;
2461 }
2462
2463 const ClothFilterType filter_type = ClothFilterType(RNA_enum_get(op->ptr, "type"));
2464
2465 /* Update the active vertex */
2466 float2 mval_fl{float(event->mval[0]), float(event->mval[1])};
2467 cursor_geometry_info_update(C, mval_fl, false);
2468
2469 /* Needs mask data to be available as it is used when solving the constraints. */
2470 BKE_sculptsession_update_for_edit(depsgraph, &ob, false);
2471
2472 if (!shape_key_check(ob, op->reports)) {
2473 return OPERATOR_CANCELLED;
2474 }
2475
2476 undo::push_begin(scene, ob, op);
2478 ob,
2479 sd,
2481 mval_fl,
2482 RNA_float_get(op->ptr, "area_normal_radius"),
2483 RNA_float_get(op->ptr, "strength"));
2484
2485 if (auto_mask::is_enabled(sd.paint, ob, nullptr)) {
2486 auto_mask::filter_cache_ensure(*depsgraph, sd.paint, ob);
2487 }
2488
2489 const float cloth_mass = RNA_float_get(op->ptr, "cloth_mass");
2490 const float cloth_damping = RNA_float_get(op->ptr, "cloth_damping");
2491 const bool use_collisions = RNA_boolean_get(op->ptr, "use_collisions");
2493 *depsgraph,
2494 ob,
2495 cloth_mass,
2496 cloth_damping,
2497 0.0f,
2498 use_collisions,
2500
2502
2503 float3 origin(0);
2505 sd, ob, ss.filter_cache->node_mask, *ss.filter_cache->cloth_sim, origin, FLT_MAX);
2506
2507 const bool use_face_sets = RNA_boolean_get(op->ptr, "use_face_sets");
2508 if (use_face_sets) {
2510 }
2511 else {
2512 ss.filter_cache->active_face_set = face_set_none_id;
2513 }
2514
2515 const int force_axis = RNA_enum_get(op->ptr, "force_axis");
2516 ss.filter_cache->enabled_axis[0] = force_axis & CLOTH_FILTER_FORCE_X;
2517 ss.filter_cache->enabled_axis[1] = force_axis & CLOTH_FILTER_FORCE_Y;
2518 ss.filter_cache->enabled_axis[2] = force_axis & CLOTH_FILTER_FORCE_Z;
2519
2521
2524}
2525
2527{
2528 ot->name = "Filter Cloth";
2529 ot->idname = "SCULPT_OT_cloth_filter";
2530 ot->description = "Applies a cloth simulation deformation to the entire mesh";
2531
2534 ot->poll = sculpt_mode_poll;
2535
2537
2539
2540 ot->prop = RNA_def_enum(ot->srna,
2541 "type",
2544 "Filter Type",
2545 "Operation that is going to be applied to the mesh");
2548 "force_axis",
2551 "Force Axis",
2552 "Apply the force in the selected axis");
2553 RNA_def_enum(ot->srna,
2554 "orientation",
2557 "Orientation",
2558 "Orientation of the axis to limit the filter force");
2559 RNA_def_float(ot->srna,
2560 "cloth_mass",
2561 1.0f,
2562 0.0f,
2563 2.0f,
2564 "Cloth Mass",
2565 "Mass of each simulation particle",
2566 0.0f,
2567 1.0f);
2568 RNA_def_float(ot->srna,
2569 "cloth_damping",
2570 0.0f,
2571 0.0f,
2572 1.0f,
2573 "Cloth Damping",
2574 "How much the applied forces are propagated through the cloth",
2575 0.0f,
2576 1.0f);
2577 ot->prop = RNA_def_boolean(ot->srna,
2578 "use_face_sets",
2579 false,
2580 "Use Face Sets",
2581 "Apply the filter only to the face set under the cursor");
2582 ot->prop = RNA_def_boolean(ot->srna,
2583 "use_collisions",
2584 false,
2585 "Use Collisions",
2586 "Collide with other collider objects in the scene");
2587}
2588
2589} // namespace blender::ed::sculpt_paint::cloth
A BVH for high poly meshes.
#define BVH_RAYCAST_DEFAULT
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define DEG_OBJECT_ITER_BEGIN(settings_, instance_)
#define DEG_OBJECT_ITER_END
Object is a sort of wrapper for general info.
#define UI_SCALE_FAC
Read Blender memory allocation and freeing.
#define PRF_scope(category)
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define UNPACK3(a)
#define BLI_NOINLINE
#define ELEM(...)
#define STREQ(a, b)
#define BM_elem_index_get(ele)
#define BM_elem_flag_test(ele, hflag)
btScalar max
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
bool is_empty() const
Definition BLI_array.hh:275
static IndexMask from_predicate(const IndexMask &universe, LinearAllocator<> &memory, Fn &&predicate, exec_mode::Mode mode=exec_mode::parallel)
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr Span< T > as_span() const
Definition BLI_span.hh:667
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
int64_t size() const
Definition BLI_set.hh:606
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void append(const T &value)
const T & last(const int64_t n=0) const
void resize(const int64_t new_size)
MutableSpan< T > as_mutable_span()
void reserve(const int64_t min_capacity)
Span< T > as_span() const
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define FLT_MAX
float distance(float, float) RET
float fade(float t)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
void fill_index_range(MutableSpan< T > span, const T start=0)
void foreach_0_index(const BitSpanT &data, Fn &&fn)
IndexRange grid_range(const int grid_area, const int grid)
pbvh::Tree * pbvh_get(Object &object)
IndexMask search_nodes(const Tree &pbvh, IndexMaskMemory &memory, FunctionRef< bool(const Node &)> filter_fn)
Definition pbvh.cc:2556
IndexMask all_leaf_nodes(const Tree &pbvh, IndexMaskMemory &memory)
Definition pbvh.cc:2522
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
const Cache * active_cache_get(const SculptSession &ss)
bool is_enabled(const Paint &paint, const Object &object, const Brush *br)
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)
Cache & filter_cache_ensure(const Depsgraph &depsgraph, const Paint &paint, Object &ob)
static BLI_NOINLINE void solve_verts_simulation(const Object &object, const Brush *brush, const float3 &sim_location, const Span< int > verts, const MutableSpan< float > factors, LocalData &tls, SimulationData &cloth_sim)
static void sculpt_cloth_ensure_constraints_in_simulation_area(const Sculpt &sd, Object &ob, const IndexMask &node_mask)
static void calc_forces_grids(const Depsgraph &depsgraph, Object &ob, const Brush &brush, const float3 &offset, const float4x4 &imat, const float3 &sim_location, const float3 &gravity, const std::optional< FalloffPlane > &falloff_plane, const bke::pbvh::GridsNode &node, LocalData &tls)
static BLI_NOINLINE void apply_grab_brush(SimulationData &cloth_sim, const Span< int > verts, const MutableSpan< float > factors, const bool use_falloff_plane, const float3 &grab_delta_symmetry)
static BLI_NOINLINE void calc_perpendicular_pinch_forces(const Span< float3 > positions, const float4x4 &imat, const float3 &location, const MutableSpan< float3 > forces)
static void cloth_sim_initialize_default_node_state(Object &object, SimulationData &cloth_sim)
static BLI_NOINLINE void calc_distances_to_plane(const Span< float3 > positions, const float4 &plane, const MutableSpan< float > distances)
static float3 cloth_brush_simulation_location_get(const SculptSession &ss, const Brush *brush)
static BLI_NOINLINE void apply_scale_filter(filter::Cache &filter_cache, const Span< int > verts, const Span< float > factors, FilterLocalData &tls)
static void cloth_brush_add_deformation_constraint(SimulationData &cloth_sim, const int node_index, const int v, const float strength)
static MutableSpan< int > calc_visible_vert_indices_bmesh(const Set< BMVert *, 0 > &verts, Vector< int > &indices)
static void copy_positions_to_array(const Depsgraph &depsgraph, const Object &object, MutableSpan< float3 > positions)
static bool cloth_filter_is_deformation_filter(ClothFilterType filter_type)
bool is_cloth_deform_brush(const Brush &brush)
static void calc_forces_mesh(const Depsgraph &depsgraph, Object &ob, const Brush &brush, const float3 &offset, const float4x4 &imat, const float3 &sim_location, const float3 &gravity, const std::optional< FalloffPlane > &falloff_plane, const MeshAttributeData &attribute_data, const Span< float3 > positions_eval, const Span< float3 > vert_normals, const bke::pbvh::MeshNode &node)
static void add_constraints_for_verts(const Object &object, const Brush *brush, const float3 &cloth_sim_initial_location, const float cloth_sim_radius, const Span< float3 > init_positions, const int node_index, const Span< int > verts, const GroupedSpan< int > vert_neighbors, SimulationData &cloth_sim, Set< OrderedEdge > &created_length_constraints)
static void cloth_brush_apply_brush_forces(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
static void calc_constraint_factors(const Depsgraph &depsgraph, const Object &object, const Brush *brush, const float3 &sim_location, const Span< float3 > init_positions, const MutableSpan< float > cloth_factors)
static BLI_NOINLINE void calc_plane_pinch_forces(const Span< float3 > positions, const float4 &plane, const float3 &plane_normal, const MutableSpan< float3 > forces)
void SCULPT_OT_cloth_filter(wmOperatorType *ot)
static void apply_filter_forces_grids(const Depsgraph &depsgraph, const Span< int > face_sets, const ClothFilterType filter_type, const float filter_strength, const float3 &gravity, const bke::pbvh::GridsNode &node, Object &object, FilterLocalData &tls)
static GroupedSpan< int > calc_vert_neighbor_indices_bmesh(const BMesh &bm, const Span< int > verts, Vector< int > &r_offset_data, Vector< int > &r_data)
static void copy_normals_to_array(const Depsgraph &depsgraph, const Object &object, MutableSpan< float3 > normals)
static EnumPropertyItem prop_cloth_filter_type[]
void brush_store_simulation_state(const Depsgraph &depsgraph, const Object &object, SimulationData &cloth_sim)
static BLI_NOINLINE void calc_gravity_forces(const Span< float > factors, const filter::Cache &filter_cache, const MutableSpan< float3 > forces)
void ensure_nodes_constraints(const Sculpt &sd, Object &object, const IndexMask &node_mask, SimulationData &cloth_sim, const float3 &initial_location, const float radius)
void sim_activate_nodes(Object &object, SimulationData &cloth_sim, const IndexMask &node_mask)
static BLI_NOINLINE void calc_brush_simulation_falloff(const Brush &brush, const float radius, const float3 &location, const Span< float3 > positions, const MutableSpan< float > factors)
static void cloth_brush_collision_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
static void calc_forces_bmesh(const Depsgraph &depsgraph, Object &ob, const Brush &brush, const float3 &offset, const float4x4 &imat, const float3 &sim_location, const float3 &gravity, const std::optional< FalloffPlane > &falloff_plane, bke::pbvh::BMeshNode &node, LocalData &tls)
static BLI_NOINLINE void expand_length_constraints(SimulationData &cloth_sim, const Span< int > verts, const Span< float > factors)
static float cloth_brush_simulation_falloff_get(const Brush &brush, const float radius, const float3 &location, const float3 &co)
static void cloth_brush_add_pin_constraint(SimulationData &cloth_sim, const int node_index, const int v, const float strength)
IndexMask brush_affected_nodes_gather(const Object &object, const Brush &brush, IndexMaskMemory &memory)
static void cloth_brush_satisfy_constraints(const Depsgraph &depsgraph, const Object &object, const Brush *brush, SimulationData &cloth_sim)
void plane_falloff_preview_draw(const uint gpuattr, Brush &brush, SculptSession &ss, const float outline_col[3], float outline_alpha)
void do_simulation_step(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, SimulationData &cloth_sim, const IndexMask &node_mask)
static void apply_filter_forces_bmesh(const Depsgraph &depsgraph, const ClothFilterType filter_type, const float filter_strength, const float3 &gravity, bke::pbvh::BMeshNode &node, Object &object, FilterLocalData &tls)
static MutableSpan< int > calc_vert_indices_grids(const CCGKey &key, const Span< int > grids, Vector< int > &indices)
static BLI_NOINLINE void clamp_factors(const MutableSpan< float > factors, const float min, const float max)
static void cloth_brush_add_length_constraint(SimulationData &cloth_sim, const int node_index, const int v1, const int v2, const Span< float3 > init_positions)
static BLI_NOINLINE void apply_forces(SimulationData &cloth_sim, const Span< float3 > forces, const Span< int > verts)
static EnumPropertyItem prop_cloth_filter_force_axis_items[]
static wmOperatorStatus sculpt_cloth_filter_modal(bContext *C, wmOperator *op, const wmEvent *event)
static BLI_NOINLINE void apply_snake_hook_brush(SimulationData &cloth_sim, const Span< int > verts, const MutableSpan< float > factors, const float3 &grab_delta_symmetry)
static wmOperatorStatus sculpt_cloth_filter_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static MutableSpan< int > calc_vert_indices_bmesh(const Set< BMVert *, 0 > &verts, Vector< int > &indices)
static void cloth_brush_add_softbody_constraint(SimulationData &cloth_sim, const int node_index, const int v, const float strength)
static void apply_filter_forces_mesh(const Depsgraph &depsgraph, const ClothFilterType filter_type, const float filter_strength, const float3 &gravity, const Span< float3 > positions_eval, const Span< float3 > vert_normals, const GroupedSpan< int > vert_to_face_map, const MeshAttributeData &attribute_data, const bke::pbvh::MeshNode &node, Object &object, FilterLocalData &tls)
static void cloth_brush_solve_collision(const Object &object, SimulationData &cloth_sim, const int i)
void simulation_limits_draw(const uint gpuattr, const Brush &brush, const float location[3], const float normal[3], const float rds, const float line_width, const float outline_col[3], const float alpha)
std::unique_ptr< SimulationData > brush_simulation_create(const Depsgraph &depsgraph, Object &ob, const float cloth_mass, const float cloth_damping, const float cloth_softbody_strength, const bool use_collisions, const bool needs_deform_coords)
static MutableSpan< int > calc_visible_vert_indices_grids(const CCGKey &key, const BitGroupVector<> &grid_hidden, const Span< int > grids, Vector< int > &indices)
static BLI_NOINLINE void calc_pinch_forces(const Span< float3 > positions, const float3 &location, const MutableSpan< float3 > forces)
static Vector< ColliderCache > cloth_brush_collider_cache_create(Object &object, const Depsgraph &depsgraph)
void do_cloth_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
static GroupedSpan< int > calc_vert_neighbor_indices_grids(const SubdivCCG &subdiv_ccg, const Span< int > verts, Vector< int > &r_offset_data, Vector< int > &r_data)
static EnumPropertyItem prop_cloth_filter_orientation_items[]
bool vert_has_face_set(const GroupedSpan< int > vert_to_face_map, const Span< int > face_sets, const int vert, const int face_set)
Definition sculpt.cc:268
int active_face_set_get(const Object &object)
Definition sculpt.cc:202
void zero_disabled_axis_components(const filter::Cache &filter_cache, MutableSpan< float3 > vectors)
void cache_init(bContext *C, Object &ob, Sculpt &sd, undo::Type undo_type, const float mval_fl[2], float area_normal_radius, float start_strength)
float3x3 to_object_space(const filter::Cache &filter_cache)
void register_operator_props(wmOperatorType *ot)
Span< int > node_visible_verts(const bke::pbvh::MeshNode &node, const Span< bool > hide_vert, Vector< int, bke::pbvh::MESH_LEAF_LIMIT > &indices)
void push_begin(const Scene &scene, Object &ob, const wmOperator *op)
bool shape_key_check(const Object &ob, ReportList *reports)
Definition sculpt.cc:134
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 scatter_data_bmesh(Span< T > node_data, const Set< BMVert *, 0 > &verts, MutableSpan< T > dst)
Definition sculpt.cc:6577
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 gather_data_grids(const SubdivCCG &subdiv_ccg, Span< T > src, Span< int > grids, MutableSpan< T > node_data)
Definition sculpt.cc:6528
void vert_random_access_ensure(Object &object)
Definition sculpt.cc:154
void apply_hardness_to_distances(float radius, float hardness, MutableSpan< float > distances)
Definition sculpt.cc:7473
GroupedSpan< int > calc_vert_neighbors(OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face, Span< bool > hide_poly, Span< int > verts, Vector< int > &r_offset_data, Vector< int > &r_data)
Definition sculpt.cc:7949
void gather_bmesh_normals(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > normals)
Definition sculpt.cc:6517
Vector< BMVert *, 64 > BMeshNeighborVerts
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7422
int vertex_count_get(const Object &object)
Definition sculpt.cc:163
void filter_region_clip_factors(const SculptSession &ss, Span< float3 > vert_positions, Span< int > verts, MutableSpan< float > factors)
Definition sculpt.cc:7285
void gather_data_bmesh(Span< T > src, const Set< BMVert *, 0 > &verts, MutableSpan< T > node_data)
Definition sculpt.cc:6545
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
bool sculpt_mode_poll(bContext *C)
Definition sculpt.cc:3800
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 scatter_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
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
Span< BMVert * > vert_neighbors_get_bmesh(BMVert &vert, BMeshNeighborVerts &r_neighbors)
Definition sculpt.cc:490
void flush_update_step(bContext *C, const UpdateType update_type)
Definition sculpt.cc:5090
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
void flush_update_done(bContext *C, Object &ob, const UpdateType update_type)
Definition sculpt.cc:5131
std::optional< CursorGeometryInfo > cursor_geometry_info_update(bContext *C, const float2 &mval, const bool use_sampled_normal)
Definition sculpt.cc:4584
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
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
void scatter_data_grids(const SubdivCCG &subdiv_ccg, Span< T > node_data, Span< int > grids, MutableSpan< T > dst)
Definition sculpt.cc:6560
constexpr ParallelGrainSize grain_size(int grain_size)
T distance(const T &a, const T &b)
T length(const VecBase< T, Size > &a)
QuaternionBase< T > normalize_and_get_length(const QuaternionBase< T > &q, T &out_length)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
bool is_zero(const T &a)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
T rcp(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T square(const T &a)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void closest_to_plane_v3(float r_close[3], const float plane[4], const float pt[3])
Definition math_geom.cc:451
void BM_mesh_vert_normals_get(BMesh *bm, MutableSpan< float3 > normals)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void immEnd()
void imm_draw_circle_dashed_3d(uint pos, float x, float y, float radius, int nsegments)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
bool isect_ray_tri_watertight_v3(const float ray_origin[3], const struct IsectRayPrecalc *isect_precalc, const float v0[3], const float v1[3], const float v2[3], float *r_lambda, float r_uv[2])
bool BKE_base_is_visible(const View3D *v3d, const Base *base)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
float dist_signed_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:511
void collision_move_object(struct CollisionModifierData *collmd, float step, float prevstep, bool moving_bvh)
Definition collision.cc:62
int BLI_bvhtree_ray_cast_ex(const BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata, int flag)
void immBegin(GPUPrimType, uint vertex_len)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
@ BRUSH_CLOTH_USE_COLLISION
@ BRUSH_CLOTH_PIN_SIMULATION_BOUNDARY
ATTR_WARN_UNUSED_RESULT const BMVert * v
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:45
MatBase< float, 4, 4 > float4x4
Base * CTX_data_active_base(const bContext *C)
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
@ BRUSH_CLOTH_DEFORM_PUSH
@ BRUSH_CLOTH_DEFORM_EXPAND
@ BRUSH_CLOTH_DEFORM_INFLATE
@ BRUSH_CLOTH_DEFORM_SNAKE_HOOK
@ BRUSH_CLOTH_DEFORM_DRAG
@ BRUSH_CLOTH_DEFORM_GRAB
@ BRUSH_CLOTH_DEFORM_PINCH_POINT
@ BRUSH_CLOTH_DEFORM_PINCH_PERPENDICULAR
void BM_mesh_vert_coords_get(const BMesh *bm, MutableSpan< float3 > positions)
void GPU_matrix_mul(const float m[4][4])
void GPU_line_width(float width)
Definition gpu_state.cc:151
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void normalize_m4(float R[4][4]) ATTR_NONNULL()
@ SCULPT_BRUSH_TYPE_CLOTH
VecBase< float, 4 > float4
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
BMesh const char void * data
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
@ DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY
float BKE_brush_curve_strength(eBrushCurvePreset preset, const CurveMapping *cumap, float distance, float brush_radius)
Definition brush.cc:1691
void unit_m4(float m[4][4])
void isect_ray_tri_watertight_v3_precalc(struct IsectRayPrecalc *isect_precalc, const float ray_direction[3])
ToolSettings * CTX_data_tool_settings(const bContext *C)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
MatBase< float, 3, 3 > float3x3
BLI_INLINE BMVert * BM_vert_at_index(BMesh *bm, const int index)
@ BVH_RAYCAST_WATERTIGHT
@ BRUSH_AUTOMASKING_CAVITY_ALL
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
const Brush * BKE_paint_brush_for_read(const Paint *paint)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
float dist_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:518
ATTR_WARN_UNUSED_RESULT const BMVert * v2
@ BRUSH_DEFORM_TARGET_CLOTH_SIM
void BKE_sculptsession_update_for_edit(Depsgraph *depsgraph, Object *ob_orig, bool is_paint_tool)
PropertyRNA * RNA_def_enum_flag(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
@ KM_RELEASE
Definition WM_types.hh:325
Scene * CTX_data_scene(const bContext *C)
void immVertex3f(uint attr_id, float x, float y, float z)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
VecBase< float, 3 > float3
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void GPU_matrix_pop()
void imm_draw_circle_wire_3d(uint pos, float x, float y, float radius, int nsegments)
void immUniformColor3fvAlpha(const float rgb[3], float a)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
Object * CTX_data_active_object(const bContext *C)
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
@ BRUSH_CLOTH_SIMULATION_AREA_GLOBAL
@ BRUSH_CLOTH_SIMULATION_AREA_LOCAL
@ BRUSH_CLOTH_SIMULATION_AREA_DYNAMIC
float RNA_float_get(PointerRNA *ptr, const char *name)
View3D * CTX_wm_view3d(const bContext *C)
void GPU_matrix_push()
void quat_to_mat4(float m[4][4], const float q[4])
@ BRUSH_CLOTH_FORCE_FALLOFF_PLANE
#define CLOTH_SOLVER_DISPLACEMENT_FACTOR
#define CLOTH_LENGTH_CONSTRAINTS_BLOCK
#define CLOTH_DEFORMATION_SNAKEHOOK_STRENGTH
#define CLOTH_DEFORMATION_GRAB_STRENGTH
#define CLOTH_DEFORMATION_TARGET_STRENGTH
#define CLOTH_SIMULATION_ITERATIONS
#define CLOTH_SIMULATION_TIME_STEP
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
CustomData pdata
struct CollisionModifierData * collmd
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
float3 active_vert_position(const Depsgraph &depsgraph, const Object &object) const
std::optional< PersistentMultiresData > persistent_multires_data()
ed::sculpt_paint::filter::Cache * filter_cache
Definition BKE_paint.hh:410
struct Object * gravity_object
static SubdivCCGCoord from_index(const CCGKey &key, int index)
int to_index(const CCGKey &key) const
SubdivCCGNeighborCoords coords
Array< float3 > positions
Array< float3 > normals
std::unique_ptr< cloth::SimulationData > cloth_sim
Map< const bke::pbvh::Node *, int > node_state_index
std::unique_ptr< cloth::SimulationData > cloth_sim
std::unique_ptr< auto_mask::Cache > automasking
wmEventType type
Definition WM_types.hh:817
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
void resize(int new_size)
Definition vector.hh:94