Blender V5.3
clay.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "DNA_brush_types.h"
12#include "DNA_mesh_types.h"
13
14#include "BKE_object_types.hh"
15#include "BKE_paint_bvh.hh"
16#include "BKE_subdiv_ccg.hh"
17
19#include "BLI_math_geom_c.hh"
20
21#include "PRF_profile.hh"
22
26
27#include "bmesh.hh"
28
30inline namespace clay_cc {
37
38BLI_NOINLINE static void calc_closest_to_plane(const float4 &test_plane,
39 const Span<float3> positions,
40 const Span<int> verts,
41 const MutableSpan<float3> translations)
42{
44 /* Equivalent to #closest_to_plane_normalized_v3 */
45 BLI_assert(verts.size() == translations.size());
46 for (const int i : verts.index_range()) {
47 const float side = plane_point_side_v3(test_plane, positions[verts[i]]);
48 translations[i] = float3(test_plane) * -side;
49 }
50}
51
52BLI_NOINLINE static void calc_closest_to_plane(const float4 &test_plane,
53 const Span<float3> positions,
54 const MutableSpan<float3> translations)
55{
57 /* Equivalent to #closest_to_plane_normalized_v3 */
58 BLI_assert(positions.size() == translations.size());
59 for (const int i : positions.index_range()) {
60 const float side = plane_point_side_v3(test_plane, positions[i]);
61 translations[i] = float3(test_plane) * -side;
62 }
63}
64
65static void calc_faces(const Depsgraph &depsgraph,
66 const Sculpt &sd,
67 const Brush &brush,
68 const float4 &test_plane,
69 const float strength,
70 const MeshAttributeData &attribute_data,
71 const Span<float3> vert_normals,
73 Object &object,
74 const PositionDeformData &position_data)
75{
76 SculptSession &ss = *object.runtime->sculpt_session;
77
78 const Span<int> verts = node.verts();
79
83 brush,
84 object,
85 attribute_data,
86 position_data.eval,
87 vert_normals,
88 node,
89 factors,
90 distances);
91
93
94 calc_closest_to_plane(test_plane, position_data.eval, verts, translations);
95 scale_translations(translations, strength);
96 scale_translations(translations, factors);
97
98 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
99 position_data.deform(translations, verts);
100}
101
102static void calc_grids(const Depsgraph &depsgraph,
103 const Sculpt &sd,
104 Object &object,
105 const Brush &brush,
106 const float4 &test_plane,
107 const float strength,
109 LocalData &tls)
110{
111 SculptSession &ss = *object.runtime->sculpt_session;
112 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
113
114 const Span<int> grids = node.grids();
115 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
116
117 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
118
119 tls.translations.resize(positions.size());
120 const MutableSpan<float3> translations = tls.translations;
121
122 calc_closest_to_plane(test_plane, positions, translations);
123 scale_translations(translations, strength);
124 scale_translations(translations, tls.factors);
125
126 clip_and_lock_translations(sd, ss, positions, translations);
127 apply_translations(translations, grids, subdiv_ccg);
128}
129
130static void calc_bmesh(const Depsgraph &depsgraph,
131 const Sculpt &sd,
132 Object &object,
133 const Brush &brush,
134 const float4 &test_plane,
135 const float strength,
137 LocalData &tls)
138{
139 SculptSession &ss = *object.runtime->sculpt_session;
140
142 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
143
144 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
145
146 tls.translations.resize(verts.size());
147 const MutableSpan<float3> translations = tls.translations;
148
149 calc_closest_to_plane(test_plane, positions, translations);
150 scale_translations(translations, strength);
151 scale_translations(translations, tls.factors);
152
153 clip_and_lock_translations(sd, ss, positions, translations);
154 apply_translations(translations, verts);
155}
156
157} // namespace clay_cc
158
159void do_clay_brush(const Depsgraph &depsgraph,
160 const Sculpt &sd,
161 Object &object,
162 const IndexMask &node_mask)
163{
165 SculptSession &ss = *object.runtime->sculpt_session;
168
169 float3 area_no;
170 float3 area_co;
171 calc_brush_plane(depsgraph, brush, object, node_mask, area_no, area_co);
172
173 const float initial_radius = fabsf(ss.cache->initial_radius);
174 const float offset = brush_plane_offset_get(brush, ss);
175
176 /* This implementation skips a factor calculation as it currently has
177 * no user-facing impact (i.e. is effectively a constant)
178 * See: #123518 */
179 float displace = fabsf(initial_radius * offset);
180
181 const bool flip = ss.cache->bstrength < 0.0f;
182 if (flip) {
183 displace = -displace;
184 }
185
186 const float3 modified_area_co = ss.cache->location_symm + (area_no * ss.cache->scale * displace);
187
188 float4 test_plane;
189 plane_from_point_normal_v3(test_plane, modified_area_co, area_no);
190 BLI_ASSERT_UNIT_V3(test_plane);
191
192 const float bstrength = fabsf(ss.cache->bstrength);
194 switch (pbvh.type()) {
196 const Mesh &mesh = *id_cast<Mesh *>(object.data);
197 const MeshAttributeData attribute_data(mesh);
198 const PositionDeformData position_data(depsgraph, object);
199 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
201 node_mask.foreach_index(
202 [&](const int i) {
203 calc_faces(depsgraph,
204 sd,
205 brush,
206 test_plane,
207 bstrength,
208 attribute_data,
209 vert_normals,
210 nodes[i],
211 object,
212 position_data);
213 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
214 },
216 break;
217 }
219 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
220 MutableSpan<float3> positions = subdiv_ccg.positions;
222 node_mask.foreach_index(
223 [&](const int i) {
224 LocalData &tls = all_tls.local();
225 calc_grids(depsgraph, sd, object, brush, test_plane, bstrength, nodes[i], tls);
226 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
227 },
229 break;
230 }
233 node_mask.foreach_index(
234 [&](const int i) {
235 LocalData &tls = all_tls.local();
236 calc_bmesh(depsgraph, sd, object, brush, test_plane, bstrength, nodes[i], tls);
238 },
240 break;
241 }
242 }
243 pbvh.tag_positions_changed(node_mask);
244 pbvh.flush_bounds_to_parents();
245}
246
247} // namespace blender::ed::sculpt_paint::brushes
A BVH for high poly meshes.
#define BLI_ASSERT_UNIT_V3(v)
#define PRF_scope(category)
#define BLI_NOINLINE
constexpr int64_t size() const
Definition BLI_span.hh:499
int64_t size() const
Definition BLI_set.hh:606
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
pbvh::Tree * pbvh_get(Object &object)
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1216
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1196
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:952
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1205
static BLI_NOINLINE void calc_closest_to_plane(const float4 &test_plane, const Span< float3 > positions, const Span< int > verts, const MutableSpan< float3 > translations)
Definition clay.cc:38
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, Object &object, bke::pbvh::BMeshNode &node, LocalData &tls, MutableSpan< float > layer_displacement_factor)
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, Object &object, const bool use_persistent_base, const Span< float3 > persistent_base_positions, const Span< float3 > persistent_base_normals, bke::pbvh::GridsNode &node, LocalData &tls, MutableSpan< float > layer_displacement_factor)
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const MeshAttributeData &attribute_data, const Span< float3 > vert_normals, const bool use_persistent_base, const Span< float3 > persistent_base_positions, const Span< float3 > persistent_base_normals, Object &object, bke::pbvh::MeshNode &node, MutableSpan< float > layer_displacement_factor, const PositionDeformData &position_data)
void do_clay_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
Definition clay.cc:159
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
void gather_bmesh_positions(const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6497
void calc_factors_common_grids(const Depsgraph &depsgraph, const Brush &brush, const Object &object, Span< float3 > positions, const bke::pbvh::GridsNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6804
void scale_translations(MutableSpan< float3 > translations, Span< float > factors)
Definition sculpt.cc:7837
void calc_factors_common_mesh_indexed(const Depsgraph &depsgraph, const Brush &brush, const Object &object, const MeshAttributeData &attribute_data, Span< float3 > vert_positions, Span< float3 > vert_normals, const bke::pbvh::MeshNode &node, MutableSpan< float > factors, MutableSpan< float > distances)
Definition sculpt.cc:6739
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
void calc_brush_plane(const Depsgraph &depsgraph, const Brush &brush, Object &ob, const IndexMask &node_mask, float3 &r_area_no, float3 &r_area_co)
Definition sculpt.cc:3062
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
float brush_plane_offset_get(const Brush &brush, const SculptSession &ss)
Definition sculpt.cc:3145
void calc_factors_common_bmesh(const Depsgraph &depsgraph, const Brush &brush, const Object &object, Span< float3 > positions, bke::pbvh::BMeshNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6891
constexpr ParallelGrainSize grain_size(int grain_size)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
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
VecBase< float, 4 > float4
BMesh const char void * data
MINLINE float plane_point_side_v3(const float plane[4], const float co[3])
const Brush * BKE_paint_brush_for_read(const Paint *paint)
VecBase< float, 3 > float3
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
#define fabsf
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
Array< float3 > positions