Blender V5.3
clay_thumb.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_mesh.hh"
17#include "BKE_object_types.hh"
18#include "BKE_paint.hh"
19#include "BKE_paint_bvh.hh"
20#include "BKE_subdiv_ccg.hh"
21
23#include "BLI_math_geom_c.hh"
24#include "BLI_math_matrix.hh"
25#include "BLI_math_matrix_c.hh"
28#include "BLI_math_vector.hh"
29#include "BLI_task.hh"
30
33
34#include "bmesh.hh"
35
36#include <numeric>
37
39
40inline namespace clay_thumb_cc {
41
48
49static void calc_faces(const Depsgraph &depsgraph,
50 const Sculpt &sd,
51 const Brush &brush,
52 const float4 &plane_tilt,
53 const float strength,
54 const MeshAttributeData &attribute_data,
55 const Span<float3> vert_normals,
57 Object &object,
58 const PositionDeformData &position_data)
59{
60 SculptSession &ss = *object.runtime->sculpt_session;
61
62 const Span<int> verts = node.verts();
64 gather_data_mesh<float3>(position_data.eval, verts, positions);
65
69 depsgraph, brush, object, attribute_data, positions, vert_normals, node, factors, distances);
70
71 scale_factors(factors, strength);
72
74 calc_translations_to_plane(positions, plane_tilt, translations);
75
76 scale_translations(translations, factors);
77
78 clip_and_lock_translations(sd, ss, positions, translations);
79 position_data.deform(translations, verts);
80}
81
82static void calc_grids(const Depsgraph &depsgraph,
83 const Sculpt &sd,
84 const Brush &brush,
85 const float4 &plane_tilt,
86 const float strength,
88 Object &object,
89 LocalData &tls)
90{
91 SculptSession &ss = *object.runtime->sculpt_session;
92 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
93
94 const Span<int> grids = node.grids();
95 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
96
97 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
98
99 scale_factors(tls.factors, strength);
100
101 tls.translations.resize(positions.size());
102 const MutableSpan<float3> translations = tls.translations;
103 calc_translations_to_plane(positions, plane_tilt, translations);
104
105 scale_translations(translations, tls.factors);
106
107 clip_and_lock_translations(sd, ss, positions, translations);
108 apply_translations(translations, grids, subdiv_ccg);
109}
110
111static void calc_bmesh(const Depsgraph &depsgraph,
112 const Sculpt &sd,
113 const Brush &brush,
114 const float4 &plane_tilt,
115 const float strength,
116 Object &object,
118 LocalData &tls)
119{
120 SculptSession &ss = *object.runtime->sculpt_session;
121
123 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
124
125 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
126
127 scale_factors(tls.factors, strength);
128
129 tls.translations.resize(verts.size());
130 const MutableSpan<float3> translations = tls.translations;
131 calc_translations_to_plane(positions, plane_tilt, translations);
132
133 scale_translations(translations, tls.factors);
134
135 clip_and_lock_translations(sd, ss, positions, translations);
136 apply_translations(translations, verts);
137}
138
139} // namespace clay_thumb_cc
140
141void do_clay_thumb_brush(const Depsgraph &depsgraph,
142 const Sculpt &sd,
143 Object &object,
144 const IndexMask &node_mask)
145{
147 const SculptSession &ss = *object.runtime->sculpt_session;
150 const float3 &location = ss.cache->location_symm;
151
152 float3 area_position;
153 float3 sculpt_plane_normal;
154 calc_brush_plane(depsgraph, brush, object, node_mask, area_position, sculpt_plane_normal);
155
156 float3 area_normal = area_position;
157 /* Ignore brush settings and recalculate the area normal. */
158 if (brush.sculpt_plane != SCULPT_DISP_DIR_AREA || (brush.flag & BRUSH_ORIGINAL_NORMAL)) {
159 area_normal = calc_area_normal(depsgraph, brush, object, node_mask).value_or(float3(0));
160 }
161
162 /* Delay the first daub because grab delta is not setup. */
164 ss.cache->clay_thumb_brush.front_angle = 0.0f;
165 return;
166 }
167
168 /* Simulate the clay accumulation by increasing the plane angle as more samples are added to the
169 * stroke. */
171 ss.cache->clay_thumb_brush.front_angle += 0.8f;
172 ss.cache->clay_thumb_brush.front_angle = std::clamp(
173 ss.cache->clay_thumb_brush.front_angle, 0.0f, 60.0f);
174 }
175
177 return;
178 }
179
180 /* Initialize brush local-space matrix. */
182 mat.x_axis() = math::cross(area_normal, ss.cache->grab_delta_symm);
183 mat.y_axis() = math::cross(area_normal, mat.x_axis());
184 mat.z_axis() = area_normal;
185 mat.location() = ss.cache->location_symm;
186 /* NOTE: #math::normalize behaves differently for some reason. */
187 normalize_m4(mat.ptr());
188
189 /* Scale brush local space matrix. */
191 const float4x4 tmat = mat * scale;
192
193 float3 normal_tilt;
194 rotate_v3_v3v3fl(normal_tilt,
195 area_position,
196 tmat.x_axis(),
197 DEG2RADF(-ss.cache->clay_thumb_brush.front_angle));
198
199 /* Tilted plane (front part of the brush). */
200 float4 plane_tilt;
201 plane_from_point_normal_v3(plane_tilt, location, normal_tilt);
202
203 const float clay_strength = ss.cache->bstrength * clay_thumb_get_stabilized_pressure(*ss.cache);
205 switch (pbvh.type()) {
207 const Mesh &mesh = *id_cast<Mesh *>(object.data);
208 const MeshAttributeData attribute_data(mesh);
210 const PositionDeformData position_data(depsgraph, object);
211 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
212 node_mask.foreach_index(
213 [&](const int i) {
214 calc_faces(depsgraph,
215 sd,
216 brush,
217 plane_tilt,
218 clay_strength,
219 attribute_data,
220 vert_normals,
221 nodes[i],
222 object,
223 position_data);
224 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
225 },
227 break;
228 }
230 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
231 MutableSpan<float3> positions = subdiv_ccg.positions;
233 node_mask.foreach_index(
234 [&](const int i) {
235 LocalData &tls = all_tls.local();
236 calc_grids(depsgraph, sd, brush, plane_tilt, clay_strength, nodes[i], object, tls);
237 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
238 },
240 break;
241 }
244 node_mask.foreach_index(
245 [&](const int i) {
246 LocalData &tls = all_tls.local();
247 calc_bmesh(depsgraph, sd, brush, plane_tilt, clay_strength, object, nodes[i], tls);
249 },
251 break;
252 }
253 }
254 pbvh.tag_positions_changed(node_mask);
255 pbvh.flush_bounds_to_parents();
256}
257
259{
260 const float pressure_sum = std::accumulate(cache.clay_thumb_brush.pressure_stabilizer.begin(),
261 cache.clay_thumb_brush.pressure_stabilizer.end(),
262 0.0f);
263 return pressure_sum / cache.clay_thumb_brush.pressure_stabilizer.size();
264}
265
266} // namespace blender::ed::sculpt_paint::brushes
A BVH for high poly meshes.
#define DEG2RADF(_deg)
Object is a sort of wrapper for general info.
#define PRF_scope(category)
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
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
float3x3 scale(float3x3 mat, float2 scale)
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 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_thumb_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
float clay_thumb_get_stabilized_pressure(const StrokeCache &cache)
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
bool stroke_is_first_brush_step_of_symmetry_pass(const StrokeCache &cache)
Definition sculpt.cc:667
void scale_translations(MutableSpan< float3 > translations, Span< float > factors)
Definition sculpt.cc:7837
void calc_translations_to_plane(Span< float3 > vert_positions, Span< int > verts, const float4 &plane, MutableSpan< float3 > translations)
Definition sculpt.cc:8189
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7664
void calc_factors_common_mesh(const Depsgraph &depsgraph, const Brush &brush, const Object &object, const MeshAttributeData &attribute_data, Span< float3 > positions, Span< float3 > vert_normals, const bke::pbvh::MeshNode &node, MutableSpan< float > factors, MutableSpan< float > distances)
Definition sculpt.cc:6772
void calc_brush_plane(const Depsgraph &depsgraph, const Brush &brush, Object &ob, const IndexMask &node_mask, float3 &r_area_no, float3 &r_area_co)
Definition sculpt.cc:3062
std::optional< float3 > calc_area_normal(const Depsgraph &depsgraph, const Brush &brush, const Object &ob, const IndexMask &node_mask)
Definition sculpt.cc:1926
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
void gather_data_mesh(Span< T > src, Span< int > indices, MutableSpan< T > dst)
bool stroke_is_main_symmetry_pass(const StrokeCache &cache)
Definition sculpt.cc:655
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)
bool is_zero(const T &a)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
MatBase< float, 4, 4 > float4x4
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void normalize_m4(float R[4][4]) ATTR_NONNULL()
VecBase< float, 4 > float4
BMesh const char void * data
const Brush * BKE_paint_brush_for_read(const Paint *paint)
@ BRUSH_ORIGINAL_NORMAL
VecBase< float, 3 > float3
const Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(bke::pbvh::BMeshNode *node)
void rotate_v3_v3v3fl(float r[3], const float p[3], const float axis[3], float angle)
const c_style_mat & ptr() const
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
Array< float3 > positions
struct blender::ed::sculpt_paint::StrokeCache::@211256062006232337057366306324060315203271114371 clay_thumb_brush