Blender V5.0
draw_sharp.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
17#include "BLI_array.hh"
19#include "BLI_task.hh"
20
24
25#include "bmesh.hh"
26
28
29inline namespace draw_sharp_cc {
30
36
37static void calc_faces(const Depsgraph &depsgraph,
38 const Sculpt &sd,
39 const Brush &brush,
40 const float3 &offset,
41 const MeshAttributeData &attribute_data,
42 const bke::pbvh::MeshNode &node,
43 Object &object,
44 LocalData &tls,
45 const PositionDeformData &position_data)
46{
47 SculptSession &ss = *object.sculpt;
48
49 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
50 const Span<int> verts = node.verts();
51
53 brush,
54 object,
55 attribute_data,
56 orig_data.positions,
57 orig_data.normals,
58 node,
59 tls.factors,
60 tls.distances);
61
62 tls.translations.resize(verts.size());
63 const MutableSpan<float3> translations = tls.translations;
64 translations_from_offset_and_factors(offset, tls.factors, translations);
65
66 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
67 position_data.deform(translations, verts);
68}
69
70static void calc_grids(const Depsgraph &depsgraph,
71 const Sculpt &sd,
72 Object &object,
73 const Brush &brush,
74 const float3 &offset,
75 const bke::pbvh::GridsNode &node,
76 LocalData &tls)
77{
78 SculptSession &ss = *object.sculpt;
79 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
80 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
81
82 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
83 const Span<int> grids = node.grids();
84 const int grid_verts_num = grids.size() * key.grid_area;
85
87 brush,
88 object,
89 orig_data.positions,
90 orig_data.normals,
91 node,
92 tls.factors,
93 tls.distances);
94
95 tls.translations.resize(grid_verts_num);
96 const MutableSpan<float3> translations = tls.translations;
97 translations_from_offset_and_factors(offset, tls.factors, translations);
98
99 clip_and_lock_translations(sd, ss, orig_data.positions, translations);
100 apply_translations(translations, grids, subdiv_ccg);
101}
102
103static void calc_bmesh(const Depsgraph &depsgraph,
104 const Sculpt &sd,
105 Object &object,
106 const Brush &brush,
107 const float3 &offset,
109 LocalData &tls)
110{
111 SculptSession &ss = *object.sculpt;
112
114
115 Array<float3> orig_positions(verts.size());
116 Array<float3> orig_normals(verts.size());
117 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
118
120 depsgraph, brush, object, orig_positions, orig_normals, node, tls.factors, tls.distances);
121
122 tls.translations.resize(verts.size());
123 const MutableSpan<float3> translations = tls.translations;
124 translations_from_offset_and_factors(offset, tls.factors, translations);
125
126 clip_and_lock_translations(sd, ss, orig_positions, translations);
127 apply_translations(translations, verts);
128}
129
130} // namespace draw_sharp_cc
131
132static void offset_positions(const Depsgraph &depsgraph,
133 const Sculpt &sd,
134 Object &object,
135 const float3 &offset,
136 const IndexMask &node_mask)
137{
138 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
139 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
140
142 switch (pbvh.type()) {
144 const Mesh &mesh = *static_cast<Mesh *>(object.data);
145 const MeshAttributeData attribute_data(mesh);
146 const PositionDeformData position_data(depsgraph, object);
148 node_mask.foreach_index(GrainSize(1), [&](const int i) {
149 LocalData &tls = all_tls.local();
151 depsgraph, sd, brush, offset, attribute_data, nodes[i], object, tls, position_data);
152 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
153 });
154 break;
155 }
157 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
158 MutableSpan<float3> positions = subdiv_ccg.positions;
160 node_mask.foreach_index(GrainSize(1), [&](const int i) {
161 LocalData &tls = all_tls.local();
162 calc_grids(depsgraph, sd, object, brush, offset, nodes[i], tls);
163 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
164 });
165 break;
166 }
169 node_mask.foreach_index(GrainSize(1), [&](const int i) {
170 LocalData &tls = all_tls.local();
171 calc_bmesh(depsgraph, sd, object, brush, offset, nodes[i], tls);
173 });
174 break;
175 }
176 }
177 pbvh.tag_positions_changed(node_mask);
179}
180
181void do_draw_sharp_brush(const Depsgraph &depsgraph,
182 const Sculpt &sd,
183 Object &object,
184 const IndexMask &node_mask)
185{
186 const SculptSession &ss = *object.sculpt;
187 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
188
189 const float3 effective_normal = tilt_effective_normal_get(ss, brush);
190
191 const float3 offset = effective_normal * ss.cache->radius * ss.cache->scale *
192 ss.cache->bstrength;
193
194 offset_positions(depsgraph, sd, object, offset, node_mask);
195}
196
197} // 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)
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
Object is a sort of wrapper for general info.
BPy_StructRNA * depsgraph
constexpr int64_t size() const
Definition BLI_span.hh:252
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
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_faces(const Depsgraph &depsgraph, const Sculpt &sd, const Brush &brush, const float3 &offset, const MeshAttributeData &attribute_data, const bke::pbvh::MeshNode &node, Object &object, LocalData &tls, const PositionDeformData &position_data)
Definition draw_sharp.cc:37
static void calc_bmesh(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float3 &offset, bke::pbvh::BMeshNode &node, LocalData &tls)
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_sharp.cc:70
void do_draw_sharp_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
static void offset_positions(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const float3 &offset, const IndexMask &node_mask)
Definition draw.cc:120
void calc_factors_common_from_orig_data_grids(const Depsgraph &depsgraph, const Brush &brush, const Object &object, Span< float3 > positions, Span< float3 > normals, const bke::pbvh::GridsNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6706
float3 tilt_effective_normal_get(const SculptSession &ss, const Brush &brush)
Definition sculpt.cc:2718
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
void calc_factors_common_from_orig_data_mesh(const Depsgraph &depsgraph, const Brush &brush, const Object &object, const MeshAttributeData &attribute_data, Span< float3 > positions, Span< float3 > normals, const bke::pbvh::MeshNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6670
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
OrigPositionData orig_position_data_get_mesh(const Object &object, const bke::pbvh::MeshNode &node)
OrigPositionData orig_position_data_get_grids(const Object &object, const bke::pbvh::GridsNode &node)
void translations_from_offset_and_factors(const float3 &offset, Span< float > factors, MutableSpan< float3 > r_translations)
Definition sculpt.cc:7531
void calc_factors_common_from_orig_data_bmesh(const Depsgraph &depsgraph, const Brush &brush, const Object &object, Span< float3 > positions, Span< float3 > normals, bke::pbvh::BMeshNode &node, Vector< float > &r_factors, Vector< float > &r_distances)
Definition sculpt.cc:6741
VecBase< float, 3 > float3
int grid_area
Definition BKE_ccg.hh:35
blender::ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:417
BMLog * bm_log
Definition BKE_paint.hh:392
SubdivCCG * subdiv_ccg
Definition BKE_paint.hh:395
blender::Array< blender::float3 > positions
i
Definition text_draw.cc:230