Blender V4.3
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_key.hh"
13#include "BKE_mesh.hh"
14#include "BKE_paint.hh"
15#include "BKE_pbvh.hh"
16#include "BKE_subdiv_ccg.hh"
17
18#include "BLI_array.hh"
20#include "BLI_math_matrix.hh"
21#include "BLI_math_vector.hh"
22#include "BLI_task.h"
23#include "BLI_task.hh"
24
28
30
31inline namespace draw_sharp_cc {
32
38
39static void calc_faces(const Depsgraph &depsgraph,
40 const Sculpt &sd,
41 const Brush &brush,
42 const float3 &offset,
43 const MeshAttributeData &attribute_data,
44 const bke::pbvh::MeshNode &node,
45 Object &object,
46 LocalData &tls,
47 const PositionDeformData &position_data)
48{
49 SculptSession &ss = *object.sculpt;
50
51 const OrigPositionData orig_data = orig_position_data_get_mesh(object, node);
52 const Span<int> verts = node.verts();
53
55 brush,
56 object,
57 attribute_data,
58 orig_data.positions,
59 orig_data.normals,
60 node,
61 tls.factors,
62 tls.distances);
63
64 tls.translations.resize(verts.size());
65 const MutableSpan<float3> translations = tls.translations;
66 translations_from_offset_and_factors(offset, tls.factors, translations);
67
68 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
69 position_data.deform(translations, verts);
70}
71
72static void calc_grids(const Depsgraph &depsgraph,
73 const Sculpt &sd,
74 Object &object,
75 const Brush &brush,
76 const float3 &offset,
77 const bke::pbvh::GridsNode &node,
78 LocalData &tls)
79{
80 SculptSession &ss = *object.sculpt;
81 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
82 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
83
84 const OrigPositionData orig_data = orig_position_data_get_grids(object, node);
85 const Span<int> grids = node.grids();
86 const int grid_verts_num = grids.size() * key.grid_area;
87
89 brush,
90 object,
91 orig_data.positions,
92 orig_data.normals,
93 node,
94 tls.factors,
95 tls.distances);
96
97 tls.translations.resize(grid_verts_num);
98 const MutableSpan<float3> translations = tls.translations;
99 translations_from_offset_and_factors(offset, tls.factors, translations);
100
101 clip_and_lock_translations(sd, ss, orig_data.positions, translations);
102 apply_translations(translations, grids, subdiv_ccg);
103}
104
105static void calc_bmesh(const Depsgraph &depsgraph,
106 const Sculpt &sd,
107 Object &object,
108 const Brush &brush,
109 const float3 &offset,
111 LocalData &tls)
112{
113 SculptSession &ss = *object.sculpt;
114
116
117 Array<float3> orig_positions(verts.size());
118 Array<float3> orig_normals(verts.size());
119 orig_position_data_gather_bmesh(*ss.bm_log, verts, orig_positions, orig_normals);
120
122 depsgraph, brush, object, orig_positions, orig_normals, node, tls.factors, tls.distances);
123
124 tls.translations.resize(verts.size());
125 const MutableSpan<float3> translations = tls.translations;
126 translations_from_offset_and_factors(offset, tls.factors, translations);
127
128 clip_and_lock_translations(sd, ss, orig_positions, translations);
129 apply_translations(translations, verts);
130}
131
132} // namespace draw_sharp_cc
133
134static void offset_positions(const Depsgraph &depsgraph,
135 const Sculpt &sd,
136 Object &object,
137 const float3 &offset,
138 const IndexMask &node_mask)
139{
140 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
141 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
142
144 switch (pbvh.type()) {
146 const Mesh &mesh = *static_cast<Mesh *>(object.data);
147 const MeshAttributeData attribute_data(mesh.attributes());
148 const PositionDeformData position_data(depsgraph, object);
150 node_mask.foreach_index(GrainSize(1), [&](const int i) {
151 LocalData &tls = all_tls.local();
153 depsgraph, sd, brush, offset, attribute_data, nodes[i], object, tls, position_data);
154 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
155 });
156 break;
157 }
159 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
160 MutableSpan<float3> positions = subdiv_ccg.positions;
162 node_mask.foreach_index(GrainSize(1), [&](const int i) {
163 LocalData &tls = all_tls.local();
164 calc_grids(depsgraph, sd, object, brush, offset, nodes[i], tls);
165 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
166 });
167 break;
168 }
171 node_mask.foreach_index(GrainSize(1), [&](const int i) {
172 LocalData &tls = all_tls.local();
173 calc_bmesh(depsgraph, sd, object, brush, offset, nodes[i], tls);
175 });
176 break;
177 }
178 }
179 pbvh.tag_positions_changed(node_mask);
181}
182
183void do_draw_sharp_brush(const Depsgraph &depsgraph,
184 const Sculpt &sd,
185 Object &object,
186 const IndexMask &node_mask)
187{
188 const SculptSession &ss = *object.sculpt;
189 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
190
191 float3 effective_normal;
192 SCULPT_tilt_effective_normal_get(ss, brush, effective_normal);
193
194 const float3 offset = effective_normal * ss.cache->radius * ss.cache->scale *
195 ss.cache->bstrength;
196
197 offset_positions(depsgraph, sd, object, offset, node_mask);
198}
199
200} // namespace blender::ed::sculpt_paint
const Brush * BKE_paint_brush_for_read(const Paint *paint)
Definition paint.cc:654
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.
constexpr int64_t size() const
Definition BLI_span.hh:253
void tag_positions_changed(const IndexMask &node_mask)
Definition pbvh.cc:549
Span< NodeT > nodes() const
void deform(MutableSpan< float3 > translations, Span< int > verts) const
Definition sculpt.cc:7139
void foreach_index(Fn &&fn) const
const Depsgraph * depsgraph
static float verts[][3]
pbvh::Tree * pbvh_get(Object &object)
Definition paint.cc:2846
void update_node_bounds_bmesh(BMeshNode &node)
Definition pbvh.cc:1095
void update_node_bounds_mesh(Span< float3 > positions, MeshNode &node)
Definition pbvh.cc:1075
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1084
void flush_bounds_to_parents(Tree &pbvh)
Definition pbvh.cc:1132
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:58
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:95
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:72
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_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:39
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:6373
void orig_position_data_gather_bmesh(const BMLog &bm_log, const Set< BMVert *, 0 > &verts, MutableSpan< float3 > positions, MutableSpan< float3 > normals)
void do_draw_sharp_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
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:6337
void clip_and_lock_translations(const Sculpt &sd, const SculptSession &ss, Span< float3 > positions, Span< int > verts, MutableSpan< float3 > translations)
Definition sculpt.cc:7022
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6958
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:7246
static void offset_positions(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const float3 &offset, const IndexMask &node_mask)
Definition draw.cc:122
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:6408
void SCULPT_tilt_effective_normal_get(const SculptSession &ss, const Brush &brush, float r_no[3])
Definition sculpt.cc:2568
int grid_area
Definition BKE_ccg.hh:35
blender::ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:427
BMLog * bm_log
Definition BKE_paint.hh:402
SubdivCCG * subdiv_ccg
Definition BKE_paint.hh:405
blender::Array< blender::float3 > positions