Blender V5.3
multires_displacement_smear.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_object_types.h"
13#include "DNA_scene_types.h"
14
15#include "BKE_object_types.hh"
16#include "BKE_paint.hh"
17#include "BKE_paint_bvh.hh"
18#include "BKE_subdiv_ccg.hh"
19
21#include "BLI_math_vector.hh"
22#include "BLI_task.hh"
23
26
28
30
36
37static void calc_node(const Depsgraph &depsgraph,
38 Object &object,
39 const Brush &brush,
40 const float strength,
42 LocalData &tls)
43{
44 SculptSession &ss = *object.runtime->sculpt_session;
45 const StrokeCache &cache = *ss.cache;
46 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
47 MutableSpan<float3> ccg_positions = subdiv_ccg.positions;
48 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
49
50 const Span<int> grids = node.grids();
51 const MutableSpan positions = gather_grids_positions(subdiv_ccg, grids, tls.positions);
52
53 calc_factors_common_grids(depsgraph, brush, object, positions, node, tls.factors, tls.distances);
54
55 scale_factors(tls.factors, strength);
56
57 for (const int i : grids.index_range()) {
58 const IndexRange node_grid_range = bke::ccg::grid_range(key.grid_area, i);
60 const int grid = grids[i];
61 for (const int y : IndexRange(key.grid_size)) {
62 for (const int x : IndexRange(key.grid_size)) {
63 const int offset = CCG_grid_xy_to_index(key.grid_size, x, y);
64 const int node_vert = node_grid_range[offset];
65 const int vert = grid_range[offset];
66
67 float3 current_disp;
68 switch (brush.smear_deform_type) {
70 current_disp = cache.location_symm - cache.last_location_symm;
71 break;
73 current_disp = cache.location_symm - ccg_positions[vert];
74 break;
76 current_disp = ccg_positions[vert] - cache.location_symm;
77 break;
78 }
79
80 const float3 current_disp_norm = math::normalize(current_disp);
81
82 SubdivCCGCoord coord{};
83 coord.grid_index = grid;
84 coord.x = x;
85 coord.y = y;
86
87 float3 interp_limit_surface_disp = cache.displacement_smear.prev_displacement[vert];
88 float weights_accum = 1.0f;
89
90 SubdivCCGNeighbors neighbors;
91 BKE_subdiv_ccg_neighbor_coords_get(*ss.subdiv_ccg, coord, false, neighbors);
92
93 for (const SubdivCCGCoord neighbor : neighbors.coords) {
94 const int neighbor_index = neighbor.to_index(key);
95 const float3 vert_disp = cache.displacement_smear.limit_surface_co[neighbor_index] -
96 cache.displacement_smear.limit_surface_co[vert];
97 const float3 &neighbor_limit_surface_disp =
98 cache.displacement_smear.prev_displacement[neighbor_index];
99 const float3 vert_disp_norm = math::normalize(vert_disp);
100
101 if (math::dot(current_disp_norm, vert_disp_norm) >= 0.0f) {
102 continue;
103 }
104
105 const float disp_interp = std::clamp(
106 -math::dot(current_disp_norm, vert_disp_norm), 0.0f, 1.0f);
107 interp_limit_surface_disp += neighbor_limit_surface_disp * disp_interp;
108 weights_accum += disp_interp;
109 }
110
111 interp_limit_surface_disp *= math::rcp(weights_accum);
112
113 float3 new_co = cache.displacement_smear.limit_surface_co[vert] +
114 interp_limit_surface_disp;
115 ccg_positions[vert] = math::interpolate(
116 ccg_positions[vert], new_co, tls.factors[node_vert]);
117 }
118 }
119 }
120}
121
122BLI_NOINLINE static void eval_all_limit_positions(const SubdivCCG &subdiv_ccg,
123 const MutableSpan<float3> limit_positions)
124{
126 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
127 threading::parallel_for(IndexRange(subdiv_ccg.grids_num), 1024, [&](const IndexRange range) {
128 for (const int grid : range) {
129 const MutableSpan grid_limit_positions = limit_positions.slice(
130 bke::ccg::grid_range(key.grid_area, grid));
131 BKE_subdiv_ccg_eval_limit_positions(subdiv_ccg, key, grid, grid_limit_positions);
132 }
133 });
134}
135
137 const Span<float3> positions,
138 const CCGKey &key,
140 const MutableSpan<float3> prev_displacement)
141{
143 for (const int grid : node.grids()) {
144 for (const int i : bke::ccg::grid_range(key.grid_area, grid)) {
145 prev_displacement[i] = positions[i] - limit_positions[i];
146 }
147 }
148}
149
150} // namespace multires_displacement_smear_cc
151
152void do_displacement_smear_brush(const Depsgraph &depsgraph,
153 const Sculpt &sd,
154 Object &ob,
155 const IndexMask &node_mask)
156{
162
163 SubdivCCG &subdiv_ccg = *ss.subdiv_ccg;
164 MutableSpan<float3> positions = subdiv_ccg.positions;
165 const CCGKey key = BKE_subdiv_ccg_key_top_level(subdiv_ccg);
166
167 if (ss.cache->displacement_smear.limit_surface_co.is_empty()) {
168 ss.cache->displacement_smear.prev_displacement = Array<float3>(positions.size(), float3(0.0f));
169 ss.cache->displacement_smear.limit_surface_co = Array<float3>(positions.size());
170
171 eval_all_limit_positions(subdiv_ccg, ss.cache->displacement_smear.limit_surface_co);
172 }
173
174 node_mask.foreach_index(
175 [&](const int i) {
177 subdiv_ccg.positions,
178 key,
179 nodes[i],
180 ss.cache->displacement_smear.prev_displacement);
181 },
183
184 const float strength = std::clamp(ss.cache->bstrength, 0.0f, 1.0f);
185
187 node_mask.foreach_index(
188 [&](const int i) {
189 LocalData &tls = all_tls.local();
190 calc_node(depsgraph, ob, brush, strength, nodes[i], tls);
191 bke::pbvh::update_node_bounds_grids(subdiv_ccg.grid_area, positions, nodes[i]);
192 },
194 pbvh.tag_positions_changed(node_mask);
195 pbvh.flush_bounds_to_parents();
196}
197
198} // 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)
#define BLI_NOINLINE
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
IndexRange grid_range(const int grid_area, const int grid)
pbvh::Tree * pbvh_get(Object &object)
void update_node_bounds_grids(int grid_area, Span< float3 > positions, GridsNode &node)
Definition pbvh.cc:1205
static void calc_node(const Depsgraph &depsgraph, const Sculpt &sd, Object &object, const Brush &brush, const float strength, bke::pbvh::GridsNode &node, LocalData &tls)
static BLI_NOINLINE void eval_all_limit_positions(const SubdivCCG &subdiv_ccg, const MutableSpan< float3 > limit_positions)
static BLI_NOINLINE void store_node_prev_displacement(const Span< float3 > limit_positions, const Span< float3 > positions, const CCGKey &key, const bke::pbvh::GridsNode &node, const MutableSpan< float3 > prev_displacement)
void do_displacement_smear_brush(const Depsgraph &depsgraph, const Sculpt &sd, Object &ob, const IndexMask &node_mask)
MutableSpan< float3 > gather_grids_positions(const SubdivCCG &subdiv_ccg, const Span< int > grids, Vector< float3 > &positions)
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
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
constexpr ParallelGrainSize grain_size(int grain_size)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T rcp(const T &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
CCGKey BKE_subdiv_ccg_key_top_level(const SubdivCCG &subdiv_ccg)
void BKE_subdiv_ccg_neighbor_coords_get(const SubdivCCG &subdiv_ccg, const SubdivCCGCoord &coord, bool include_duplicates, SubdivCCGNeighbors &r_neighbors)
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:75
const Brush * BKE_paint_brush_for_read(const Paint *paint)
@ BRUSH_SMEAR_DEFORM_EXPAND
@ BRUSH_SMEAR_DEFORM_DRAG
@ BRUSH_SMEAR_DEFORM_PINCH
VecBase< float, 3 > float3
bke::ObjectRuntime * runtime
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
int to_index(const CCGKey &key) const
SubdivCCGNeighborCoords coords
Array< float3 > positions
struct blender::ed::sculpt_paint::StrokeCache::@210266372035032112040136174225236171214246130144 displacement_smear