Blender V5.3
crease.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#include "DNA_object_types.h"
14#include "DNA_scene_types.h"
15
16#include "BKE_brush.hh"
17#include "BKE_mesh.hh"
18#include "BKE_object_types.hh"
19#include "BKE_paint.hh"
20#include "BKE_paint_bvh.hh"
21#include "BKE_subdiv_ccg.hh"
22
24#include "BLI_task.hh"
25
26#include "PRF_profile.hh"
27
31
32#include "bmesh.hh"
33
35
36inline namespace crease_cc {
37
44
45BLI_NOINLINE static void translations_from_position(const Span<float3> positions_eval,
46 const Span<int> verts,
47 const float3 &location,
48 const MutableSpan<float3> translations)
49{
51 for (const int i : verts.index_range()) {
52 translations[i] = location - positions_eval[verts[i]];
53 }
54}
55
57 const float3 &location,
58 const MutableSpan<float3> translations)
59{
61 for (const int i : positions.index_range()) {
62 translations[i] = location - positions[i];
63 }
64}
65
67 const Span<float> factors,
68 const float3 &offset)
69{
71 for (const int i : translations.index_range()) {
72 translations[i] += offset * factors[i];
73 }
74}
75
76static void calc_faces(const Depsgraph &depsgraph,
77 const Sculpt &sd,
78 const Brush &brush,
79 const float3 &offset,
80 const float strength,
81 const MeshAttributeData &attribute_data,
82 const Span<float3> vert_normals,
84 Object &object,
85 const PositionDeformData &position_data)
86{
87 SculptSession &ss = *object.runtime->sculpt_session;
88 const StrokeCache &cache = *ss.cache;
89
90 const Span<int> verts = node.verts();
91
95 brush,
96 object,
97 attribute_data,
98 position_data.eval,
99 vert_normals,
100 node,
101 factors,
102 distances);
103
105 translations_from_position(position_data.eval, verts, cache.location_symm, translations);
106
107 if (brush.falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
108 project_translations(translations, cache.view_normal_symm);
109 }
110
111 scale_translations(translations, factors);
112 scale_translations(translations, strength);
113
114 /* The vertices are pinched towards a line instead of a single point. Without this we get a
115 * 'flat' surface surrounding the pinch. */
116 project_translations(translations, cache.sculpt_normal_symm);
117
118 add_offset_to_translations(translations, factors, offset);
119
120 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
121 position_data.deform(translations, verts);
122}
123
124static void calc_grids(const Depsgraph &depsgraph,
125 const Sculpt &sd,
126 Object &object,
127 const Brush &brush,
128 const float3 &offset,
129 const float strength,
131 LocalData &tls)
132{
133 SculptSession &ss = *object.runtime->sculpt_session;
134 const StrokeCache &cache = *ss.cache;
135 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
136
137 const Span<int> grids = node.grids();
138 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
139
140 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
141
142 tls.translations.resize(positions.size());
143 const MutableSpan<float3> translations = tls.translations;
144 translations_from_position(positions, cache.location_symm, translations);
145
146 if (brush.falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
147 project_translations(translations, cache.view_normal_symm);
148 }
149
150 scale_translations(translations, tls.factors);
151 scale_translations(translations, strength);
152
153 project_translations(translations, cache.sculpt_normal_symm);
154
155 add_offset_to_translations(translations, tls.factors, offset);
156
157 clip_and_lock_translations(sd, ss, positions, translations);
158 apply_translations(translations, grids, subdiv_ccg);
159}
160
161static void calc_bmesh(const Depsgraph &depsgraph,
162 const Sculpt &sd,
163 Object &object,
164 const Brush &brush,
165 const float3 &offset,
166 const float strength,
168 LocalData &tls)
169{
170 SculptSession &ss = *object.runtime->sculpt_session;
171 const StrokeCache &cache = *ss.cache;
172
174 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
175
176 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
177
178 tls.translations.resize(verts.size());
179 const MutableSpan<float3> translations = tls.translations;
180 translations_from_position(positions, cache.location_symm, translations);
181
182 if (brush.falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) {
183 project_translations(translations, cache.view_normal_symm);
184 }
185
186 scale_translations(translations, tls.factors);
187 scale_translations(translations, strength);
188
189 project_translations(translations, cache.sculpt_normal_symm);
190
191 add_offset_to_translations(translations, tls.factors, offset);
192
193 clip_and_lock_translations(sd, ss, positions, translations);
194 apply_translations(translations, verts);
195}
196
197static void do_crease_or_blob_brush(const Depsgraph &depsgraph,
198 const Sculpt &sd,
199 const bool invert_strength,
200 Object &object,
201 const IndexMask &node_mask)
202{
203 const SculptSession &ss = *object.runtime->sculpt_session;
205 const StrokeCache &cache = *ss.cache;
207
208 /* Offset with as much as possible factored in already. */
209 const float3 offset = cache.sculpt_normal_symm * cache.scale * cache.radius * cache.bstrength;
210
211 /* We divide out the squared alpha and multiply by the squared crease
212 * to give us the pinch strength. */
213 float crease_correction = brush.crease_pinch_factor * brush.crease_pinch_factor;
214 float brush_alpha = BKE_brush_alpha_get(&sd.paint, &brush);
215 if (brush_alpha > 0.0f) {
216 crease_correction /= brush_alpha * brush_alpha;
217 }
218
219 /* We always want crease to pinch or blob to relax even when draw is negative. */
220 const float strength = std::abs(cache.bstrength) * crease_correction *
221 (invert_strength ? -1.0f : 1.0f);
222
224 switch (pbvh.type()) {
226 const Mesh &mesh = *id_cast<Mesh *>(object.data);
227 const MeshAttributeData attribute_data(mesh);
228 const PositionDeformData position_data(depsgraph, object);
229 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
231 node_mask.foreach_index(
232 [&](const int i) {
233 calc_faces(depsgraph,
234 sd,
235 brush,
236 offset,
237 strength,
238 attribute_data,
239 vert_normals,
240 nodes[i],
241 object,
242 position_data);
243 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
244 },
246 break;
247 }
249 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
250 MutableSpan<float3> positions = subdiv_ccg.positions;
252 node_mask.foreach_index(
253 [&](const int i) {
254 LocalData &tls = all_tls.local();
255 calc_grids(depsgraph, sd, object, brush, offset, strength, nodes[i], tls);
256 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
257 },
259 break;
260 }
263 node_mask.foreach_index(
264 [&](const int i) {
265 LocalData &tls = all_tls.local();
266 calc_bmesh(depsgraph, sd, object, brush, offset, strength, nodes[i], tls);
268 },
270 break;
271 }
272 }
273 pbvh.tag_positions_changed(node_mask);
274 pbvh.flush_bounds_to_parents();
275}
276
277} // namespace crease_cc
278
279void do_crease_brush(const Depsgraph &depsgraph,
280 const Sculpt &sd,
281 Object &object,
282 const IndexMask &node_mask)
283{
285 do_crease_or_blob_brush(depsgraph, sd, false, object, node_mask);
286}
287
288void do_blob_brush(const Depsgraph &depsgraph,
289 const Sculpt &sd,
290 Object &object,
291 const IndexMask &node_mask)
292{
294 do_crease_or_blob_brush(depsgraph, sd, true, object, node_mask);
295}
296
297} // namespace blender::ed::sculpt_paint::brushes
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define PRF_scope(category)
#define BLI_NOINLINE
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
int64_t size() const
Definition BLI_set.hh:606
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
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 translations_from_position(const Span< float3 > positions_eval, const Span< int > verts, const float3 &location, const MutableSpan< float3 > translations)
Definition crease.cc:45
static void do_crease_or_blob_brush(const Depsgraph &depsgraph, const Sculpt &sd, const bool invert_strength, Object &object, const IndexMask &node_mask)
Definition crease.cc:197
static BLI_NOINLINE void add_offset_to_translations(const MutableSpan< float3 > translations, const Span< float > factors, const float3 &offset)
Definition crease.cc:66
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_blob_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
Definition crease.cc:288
void do_crease_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
Definition crease.cc:279
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 project_translations(MutableSpan< float3 > translations, const float3 &plane)
Definition sculpt.cc:7638
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
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)
@ PAINT_FALLOFF_SHAPE_TUBE
BMesh const char void * data
const Brush * BKE_paint_brush_for_read(const Paint *paint)
VecBase< float, 3 > float3
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
float BKE_brush_alpha_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1454
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
Array< float3 > positions