Blender V5.3
draw.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_vector.hh"
24#include "BLI_task.hh"
25
29
30#include "bmesh.hh"
31
33
34inline namespace draw_cc {
35
42
43static void calc_faces(const Depsgraph &depsgraph,
44 const Sculpt &sd,
45 const Brush &brush,
46 const float3 &offset,
47 const MeshAttributeData &attribute_data,
48 const Span<float3> vert_normals,
50 Object &object,
51 const PositionDeformData &position_data)
52{
53 const SculptSession &ss = *object.runtime->sculpt_session;
54
55 const Span<int> verts = node.verts();
56
60 brush,
61 object,
62 attribute_data,
63 position_data.eval,
64 vert_normals,
65 node,
66 factors,
67 distances);
68
70 translations_from_offset_and_factors(offset, factors, translations);
71
72 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
73 position_data.deform(translations, verts);
74}
75
76static void calc_grids(const Depsgraph &depsgraph,
77 const Sculpt &sd,
78 Object &object,
79 const Brush &brush,
80 const float3 &offset,
82 LocalData &tls)
83{
84 SculptSession &ss = *object.runtime->sculpt_session;
85 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
86
87 const Span<int> grids = node.grids();
88 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
89
90 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
91
92 tls.translations.resize(positions.size());
93 const MutableSpan<float3> translations = tls.translations;
94 translations_from_offset_and_factors(offset, tls.factors, translations);
95
96 clip_and_lock_translations(sd, ss, positions, translations);
97 apply_translations(translations, grids, subdiv_ccg);
98}
99
100static void calc_bmesh(const Depsgraph &depsgraph,
101 const Sculpt &sd,
102 Object &object,
103 const Brush &brush,
104 const float3 &offset,
106 LocalData &tls)
107{
108 SculptSession &ss = *object.runtime->sculpt_session;
109
111 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
112
113 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
114
115 tls.translations.resize(verts.size());
116 const MutableSpan<float3> translations = tls.translations;
117 translations_from_offset_and_factors(offset, tls.factors, translations);
118
119 clip_and_lock_translations(sd, ss, positions, translations);
120 apply_translations(translations, verts);
121}
122
123} // namespace draw_cc
124
125static void offset_positions(const Depsgraph &depsgraph,
126 const Sculpt &sd,
127 Object &object,
128 const float3 &offset,
129 const IndexMask &node_mask)
130{
133
135 switch (pbvh.type()) {
137 const Mesh &mesh = *id_cast<Mesh *>(object.data);
138 const MeshAttributeData attribute_data(mesh);
139 const PositionDeformData position_data(depsgraph, object);
140 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
142 node_mask.foreach_index(
143 [&](const int i) {
144 calc_faces(depsgraph,
145 sd,
146 brush,
147 offset,
148 attribute_data,
149 vert_normals,
150 nodes[i],
151 object,
152 position_data);
153 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
154 },
156 break;
157 }
159 SubdivCCG &subdiv_ccg = *object.runtime->sculpt_session->subdiv_ccg;
160 MutableSpan<float3> positions = subdiv_ccg.positions;
162 node_mask.foreach_index(
163 [&](const int i) {
164 LocalData &tls = all_tls.local();
165 calc_grids(depsgraph, sd, object, brush, offset, nodes[i], tls);
166 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
167 },
169 break;
170 }
173 node_mask.foreach_index(
174 [&](const int i) {
175 LocalData &tls = all_tls.local();
176 calc_bmesh(depsgraph, sd, object, brush, offset, nodes[i], tls);
178 },
180 break;
181 }
182 }
183 pbvh.tag_positions_changed(node_mask);
184 pbvh.flush_bounds_to_parents();
185}
186
187void do_draw_brush(const Depsgraph &depsgraph,
188 const Sculpt &sd,
189 Object &object,
190 const IndexMask &node_mask)
191{
193 const SculptSession &ss = *object.runtime->sculpt_session;
195
196 const float3 effective_normal = tilt_effective_normal_get(ss, brush);
197
198 const float3 offset = effective_normal * ss.cache->radius * ss.cache->scale *
199 ss.cache->bstrength;
200
201 offset_positions(depsgraph, sd, object, offset, node_mask);
202}
203
204void do_nudge_brush(const Depsgraph &depsgraph,
205 const Sculpt &sd,
206 Object &object,
207 const IndexMask &node_mask)
208{
210 const SculptSession &ss = *object.runtime->sculpt_session;
211
212 const float3 offset = math::cross(
215
216 offset_positions(depsgraph, sd, object, offset * ss.cache->bstrength, node_mask);
217}
218
219void do_gravity_brush(const Depsgraph &depsgraph,
220 const Sculpt &sd,
221 Object &object,
222 const IndexMask &node_mask)
223{
225 const SculptSession &ss = *object.runtime->sculpt_session;
226
227 const float3 offset = ss.cache->gravity_direction_symm * -ss.cache->radius_squared *
228 ss.cache->scale * sd.gravity_factor;
229
230 offset_positions(depsgraph, sd, object, offset, node_mask);
231}
232
233} // 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)
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
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_draw_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition draw.cc:187
void do_nudge_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition draw.cc:204
void do_gravity_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
Definition draw.cc:219
static void offset_positions(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const float3 &offset, const IndexMask &node_mask)
Definition draw.cc:125
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
float3 tilt_effective_normal_get(const SculptSession &ss, const Brush &brush)
Definition sculpt.cc:2904
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 apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7592
void translations_from_offset_and_factors(const float3 &offset, Span< float > factors, MutableSpan< float3 > r_translations)
Definition sculpt.cc:7877
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)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
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)
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
Array< float3 > positions