Blender V4.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
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_cc {
32
39
40static void calc_faces(const Depsgraph &depsgraph,
41 const Sculpt &sd,
42 const Brush &brush,
43 const float3 &offset,
44 const MeshAttributeData &attribute_data,
45 const Span<float3> vert_normals,
46 const bke::pbvh::MeshNode &node,
47 Object &object,
48 LocalData &tls,
49 const PositionDeformData &position_data)
50{
51 const SculptSession &ss = *object.sculpt;
52
53 const Span<int> verts = node.verts();
54
56 brush,
57 object,
58 attribute_data,
59 position_data.eval,
60 vert_normals,
61 node,
62 tls.factors,
63 tls.distances);
64
65 tls.translations.resize(verts.size());
66 const MutableSpan<float3> translations = tls.translations;
67 translations_from_offset_and_factors(offset, tls.factors, translations);
68
69 clip_and_lock_translations(sd, ss, position_data.eval, verts, translations);
70 position_data.deform(translations, verts);
71}
72
73static void calc_grids(const Depsgraph &depsgraph,
74 const Sculpt &sd,
75 Object &object,
76 const Brush &brush,
77 const float3 &offset,
78 const bke::pbvh::GridsNode &node,
79 LocalData &tls)
80{
81 SculptSession &ss = *object.sculpt;
82 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
83
84 const Span<int> grids = node.grids();
85 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
86
87 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
88
89 tls.translations.resize(positions.size());
90 const MutableSpan<float3> translations = tls.translations;
91 translations_from_offset_and_factors(offset, tls.factors, translations);
92
93 clip_and_lock_translations(sd, ss, positions, translations);
94 apply_translations(translations, grids, subdiv_ccg);
95}
96
97static void calc_bmesh(const Depsgraph &depsgraph,
98 const Sculpt &sd,
99 Object &object,
100 const Brush &brush,
101 const float3 &offset,
103 LocalData &tls)
104{
105 SculptSession &ss = *object.sculpt;
106
108 const MutableSpan positions = gather_bmesh_positions(verts, tls.positions);
109
110 calc_factors_common_bmesh(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
111
112 tls.translations.resize(verts.size());
113 const MutableSpan<float3> translations = tls.translations;
114 translations_from_offset_and_factors(offset, tls.factors, translations);
115
116 clip_and_lock_translations(sd, ss, positions, translations);
117 apply_translations(translations, verts);
118}
119
120} // namespace draw_cc
121
122static void offset_positions(const Depsgraph &depsgraph,
123 const Sculpt &sd,
124 Object &object,
125 const float3 &offset,
126 const IndexMask &node_mask)
127{
128 bke::pbvh::Tree &pbvh = *bke::object::pbvh_get(object);
129 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
130
132 switch (pbvh.type()) {
134 const Mesh &mesh = *static_cast<Mesh *>(object.data);
135 const MeshAttributeData attribute_data(mesh.attributes());
136 const PositionDeformData position_data(depsgraph, object);
137 const Span<float3> vert_normals = bke::pbvh::vert_normals_eval(depsgraph, object);
139 node_mask.foreach_index(GrainSize(1), [&](const int i) {
140 LocalData &tls = all_tls.local();
142 sd,
143 brush,
144 offset,
145 attribute_data,
146 vert_normals,
147 nodes[i],
148 object,
149 tls,
150 position_data);
151 bke::pbvh::update_node_bounds_mesh(position_data.eval, nodes[i]);
152 });
153 break;
154 }
156 SubdivCCG &subdiv_ccg = *object.sculpt->subdiv_ccg;
157 MutableSpan<float3> positions = subdiv_ccg.positions;
159 node_mask.foreach_index(GrainSize(1), [&](const int i) {
160 LocalData &tls = all_tls.local();
161 calc_grids(depsgraph, sd, object, brush, offset, nodes[i], tls);
162 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
163 });
164 break;
165 }
168 node_mask.foreach_index(GrainSize(1), [&](const int i) {
169 LocalData &tls = all_tls.local();
170 calc_bmesh(depsgraph, sd, object, brush, offset, nodes[i], tls);
172 });
173 break;
174 }
175 }
176 pbvh.tag_positions_changed(node_mask);
178}
179
180void do_draw_brush(const Depsgraph &depsgraph,
181 const Sculpt &sd,
182 Object &object,
183 const IndexMask &node_mask)
184{
185 const SculptSession &ss = *object.sculpt;
186 const Brush &brush = *BKE_paint_brush_for_read(&sd.paint);
187
188 float3 effective_normal;
189 SCULPT_tilt_effective_normal_get(ss, brush, effective_normal);
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
197void do_nudge_brush(const Depsgraph &depsgraph,
198 const Sculpt &sd,
199 Object &object,
200 const IndexMask &node_mask)
201{
202 const SculptSession &ss = *object.sculpt;
203
204 const float3 offset = math::cross(
207
208 offset_positions(depsgraph, sd, object, offset * ss.cache->bstrength, node_mask);
209}
210
211void do_gravity_brush(const Depsgraph &depsgraph,
212 const Sculpt &sd,
213 Object &object,
214 const IndexMask &node_mask)
215{
216 const SculptSession &ss = *object.sculpt;
217
218 const float3 offset = ss.cache->gravity_direction_symm * -ss.cache->radius_squared *
219 ss.cache->scale * sd.gravity_factor;
220
221 offset_positions(depsgraph, sd, object, offset, node_mask);
222}
223
224} // 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)
Object is a sort of wrapper for general info.
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
Span< float3 > vert_normals_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:2502
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_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:40
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:73
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:97
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:6054
void do_nudge_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition draw.cc:197
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:6270
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:6199
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 do_draw_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition draw.cc:180
void apply_translations(Span< float3 > translations, Span< int > verts, MutableSpan< float3 > positions)
Definition sculpt.cc:6958
void translations_from_offset_and_factors(const float3 &offset, Span< float > factors, MutableSpan< float3 > r_translations)
Definition sculpt.cc:7246
void do_gravity_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const IndexMask &node_mask)
Definition draw.cc:211
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:6304
static void offset_positions(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const float3 &offset, const IndexMask &node_mask)
Definition draw.cc:122
AxisSigned cross(const AxisSigned a, const AxisSigned b)
void SCULPT_tilt_effective_normal_get(const SculptSession &ss, const Brush &brush, float r_no[3])
Definition sculpt.cc:2568
blender::ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:427
SubdivCCG * subdiv_ccg
Definition BKE_paint.hh:405
float gravity_factor
blender::Array< blender::float3 > positions