Blender V5.0
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
6
7#include "DNA_brush_types.h"
8#include "DNA_mesh_types.h"
9#include "DNA_object_types.h"
10#include "DNA_scene_types.h"
11
12#include "BKE_mesh.hh"
13#include "BKE_paint.hh"
14#include "BKE_paint_bvh.hh"
15#include "BKE_subdiv_ccg.hh"
16
18#include "BLI_math_vector.hh"
19#include "BLI_task.hh"
20
24
25#include "bmesh.hh"
26
28
29inline namespace draw_cc {
30
37
38static void calc_faces(const Depsgraph &depsgraph,
39 const Sculpt &sd,
40 const Brush &brush,
41 const float3 &offset,
42 const MeshAttributeData &attribute_data,
43 const Span<float3> vert_normals,
44 const bke::pbvh::MeshNode &node,
45 Object &object,
46 LocalData &tls,
47 const PositionDeformData &position_data)
48{
49 const SculptSession &ss = *object.sculpt;
50
51 const Span<int> verts = node.verts();
52
54 brush,
55 object,
56 attribute_data,
57 position_data.eval,
58 vert_normals,
59 node,
60 tls.factors,
61 tls.distances);
62
63 tls.translations.resize(verts.size());
64 const MutableSpan<float3> translations = tls.translations;
65 translations_from_offset_and_factors(offset, tls.factors, translations);
66
67 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
68 position_data.deform(translations, verts);
69}
70
71static void calc_grids(const Depsgraph &depsgraph,
72 const Sculpt &sd,
73 Object &object,
74 const Brush &brush,
75 const float3 &offset,
76 const bke::pbvh::GridsNode &node,
77 LocalData &tls)
78{
79 SculptSession &ss = *object.sculpt;
80 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
81
82 const Span<int> grids = node.grids();
83 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
84
85 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
86
87 tls.translations.resize(positions.size());
88 const MutableSpan<float3> translations = tls.translations;
89 translations_from_offset_and_factors(offset, tls.factors, translations);
90
91 clip_and_lock_translations(sd, ss, positions, translations);
92 apply_translations(translations, grids, subdiv_ccg);
93}
94
95static void calc_bmesh(const Depsgraph &depsgraph,
96 const Sculpt &sd,
97 Object &object,
98 const Brush &brush,
99 const float3 &offset,
101 LocalData &tls)
102{
103 SculptSession &ss = *object.sculpt;
104
106 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
107
108 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
109
110 tls.translations.resize(verts.size());
111 const MutableSpan<float3> translations = tls.translations;
112 translations_from_offset_and_factors(offset, tls.factors, translations);
113
114 clip_and_lock_translations(sd, ss, positions, translations);
115 apply_translations(translations, verts);
116}
117
118} // namespace draw_cc
119
120static void offset_positions(const Depsgraph &depsgraph,
121 const Sculpt &sd,
122 Object &object,
123 const float3 &offset,
124 const IndexMask &node_mask)
125{
126 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
127 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
128
130 switch (pbvh.type()) {
132 const Mesh &mesh = *static_cast<Mesh *>(object.data);
133 const MeshAttributeData attribute_data(mesh);
134 const PositionDeformData position_data(depsgraph, object);
135 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
137 node_mask.foreach_index(GrainSize(1), [&](const int i) {
138 LocalData &tls = all_tls.local();
140 sd,
141 brush,
142 offset,
143 attribute_data,
144 vert_normals,
145 nodes[i],
146 object,
147 tls,
148 position_data);
149 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
150 });
151 break;
152 }
154 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
155 MutableSpan<float3> positions = subdiv_ccg.positions;
157 node_mask.foreach_index(GrainSize(1), [&](const int i) {
158 LocalData &tls = all_tls.local();
159 calc_grids(depsgraph, sd, object, brush, offset, nodes[i], tls);
160 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
161 });
162 break;
163 }
166 node_mask.foreach_index(GrainSize(1), [&](const int i) {
167 LocalData &tls = all_tls.local();
168 calc_bmesh(depsgraph, sd, object, brush, offset, nodes[i], tls);
170 });
171 break;
172 }
173 }
174 pbvh.tag_positions_changed(node_mask);
176}
177
178void do_draw_brush(const Depsgraph &depsgraph,
179 const Sculpt &sd,
180 Object &object,
181 const IndexMask &node_mask)
182{
183 const SculptSession &ss = *object.sculpt;
184 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
185
186 const float3 effective_normal = tilt_effective_normal_get(ss, brush);
187
188 const float3 offset = effective_normal * ss.cache->radius * ss.cache->scale *
189 ss.cache->bstrength;
190
191 offset_positions(depsgraph, sd, object, offset, node_mask);
192}
193
194void do_nudge_brush(const Depsgraph &depsgraph,
195 const Sculpt &sd,
196 Object &object,
197 const IndexMask &node_mask)
198{
199 const SculptSession &ss = *object.sculpt;
200
201 const float3 offset = math::cross(
204
205 offset_positions(depsgraph, sd, object, offset * ss.cache->bstrength, node_mask);
206}
207
208void do_gravity_brush(const Depsgraph &depsgraph,
209 const Sculpt &sd,
210 Object &object,
211 const IndexMask &node_mask)
212{
213 const SculptSession &ss = *object.sculpt;
214
215 const float3 offset = ss.cache->gravity_direction_symm * -ss.cache->radius_squared *
216 ss.cache->scale * sd.gravity_factor;
217
218 offset_positions(depsgraph, sd, object, offset, node_mask);
219}
220
221} // namespace blender::ed::sculpt_paint::brushes
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:650
A BVH for high poly meshes.
const blender::Set< BMVert *, 0 > & BKE_pbvh_bmesh_node_unique_verts(blender::bke::pbvh::BMeshNode *node)
Object is a sort of wrapper for general info.
BPy_StructRNA * depsgraph
constexpr int64_t size() const
Definition BLI_span.hh:493
void tag_positions_changed(const IndexMask &node_mask)
Definition pbvh.cc:635
Span< NodeT > nodes() const
void flush_bounds_to_parents()
Definition pbvh.cc:1306
void foreach_index(Fn &&fn) const
static float verts[][3]
pbvh::Tree * pbvh_get(Object &object)
Definition paint.cc:3052
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1294
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1274
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:1059
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1283
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float3 &direction, const float strength, bke::pbvh::BMeshNode &node, LocalData &tls)
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const float4 &test_plane, const float strength, const MeshAttributeData &attribute_data, const Span< float3 > vert_normals, const bke::pbvh::MeshNode &node, Object &object, LocalData &tls, const PositionDeformData &position_data)
Definition clay.cc:56
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float4 &test_plane, const float strength, bke::pbvh::GridsNode &node, LocalData &tls)
Definition clay.cc:93
static void calc_grids(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float3 &offset, const bke::pbvh::GridsNode &node, LocalData &tls)
Definition draw.cc:71
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float3 &offset, bke::pbvh::BMeshNode &node, LocalData &tls)
Definition draw.cc:95
static void calc_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const float3 &offset, const MeshAttributeData &attribute_data, const Span< float3 > vert_normals, const bke::pbvh::MeshNode &node, Object &object, LocalData &tls, const PositionDeformData &position_data)
Definition draw.cc:38
void do_draw_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition draw.cc:178
void do_nudge_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition draw.cc:194
void do_gravity_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
Definition draw.cc:208
static void offset_positions(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const float3 &offset, const IndexMask &node_mask)
Definition draw.cc:120
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:6367
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:6603
float3 tilt_effective_normal_get(const SculptSession &ss, const Brush &brush)
Definition sculpt.cc:2718
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, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6512
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7335
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:7268
void translations_from_offset_and_factors(const float3 &offset, Span< float > factors, MutableSpan< float3 > r_translations)
Definition sculpt.cc:7531
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:6637
AxisSigned cross(const AxisSigned a, const AxisSigned b)
VecBase< float, 3 > float3
blender::ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:417
SubdivCCG * subdiv_ccg
Definition BKE_paint.hh:395
float gravity_factor
blender::Array< blender::float3 > positions
i
Definition text_draw.cc:230