Blender V5.0
multires_reshape_ccg.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "multires_reshape.hh"
10
11#include <cstring>
12
13#include "BKE_ccg.hh"
14#include "BKE_subdiv_ccg.hh"
15
17 SubdivCCG *subdiv_ccg)
18{
19 using namespace blender;
20 const CCGKey reshape_level_key = BKE_subdiv_ccg_key(*subdiv_ccg, reshape_context->reshape.level);
21
22 const int reshape_grid_size = reshape_context->reshape.grid_size;
23 const float reshape_grid_size_1_inv = 1.0f / (float(reshape_grid_size) - 1.0f);
24
25 const Span<float3> positions = subdiv_ccg->positions;
26 const Span<float> masks = subdiv_ccg->masks;
27
28 int num_grids = subdiv_ccg->grids_num;
29 for (int grid_index = 0; grid_index < num_grids; ++grid_index) {
30 for (int y = 0; y < reshape_grid_size; ++y) {
31 const float v = float(y) * reshape_grid_size_1_inv;
32 for (int x = 0; x < reshape_grid_size; ++x) {
33 const float u = float(x) * reshape_grid_size_1_inv;
34 const int vert = bke::ccg::grid_xy_to_vert(reshape_level_key, grid_index, x, y);
35
36 GridCoord grid_coord;
37 grid_coord.grid_index = grid_index;
38 grid_coord.u = u;
39 grid_coord.v = v;
40
42 reshape_context, &grid_coord);
43
44 BLI_assert(grid_element.displacement != nullptr);
45 *grid_element.displacement = positions[vert];
46
47 /* NOTE: The sculpt mode might have SubdivCCG's data out of sync from what is stored in
48 * the original object. This happens in the following scenario:
49 *
50 * - User enters sculpt mode of the default cube object.
51 * - Sculpt mode creates new `layer`
52 * - User does some strokes.
53 * - User used undo until sculpt mode is exited.
54 *
55 * In an ideal world the sculpt mode will take care of keeping CustomData and CCG layers in
56 * sync by doing proper pushes to a local sculpt undo stack.
57 *
58 * Since the proper solution needs time to be implemented, consider the target object
59 * the source of truth of which data layers are to be updated during reshape. This means,
60 * for example, that if the undo system says object does not have paint mask layer, it is
61 * not to be updated.
62 *
63 * This is fragile logic, and is only working correctly because the code path is only
64 * used by sculpt changes. In other use cases the code might not catch inconsistency and
65 * silently make the wrong decision. */
66 /* NOTE: There is a known bug in Undo code that results in first Sculpt step
67 * after a Memfile one to never be undone (see #83806). This might be the root cause of
68 * this inconsistency. */
69 if (!subdiv_ccg->masks.is_empty() && grid_element.mask != nullptr) {
70 *grid_element.mask = masks[vert];
71 }
72 }
73 }
74 }
75
76 return true;
77}
CCGKey BKE_subdiv_ccg_key(const SubdivCCG &subdiv_ccg, int level)
#define BLI_assert(a)
Definition BLI_assert.h:46
ATTR_WARN_UNUSED_RESULT const BMVert * v
bool is_empty() const
Definition BLI_array.hh:264
nullptr float
ReshapeGridElement multires_reshape_grid_element_for_grid_coord(const MultiresReshapeContext *reshape_context, const GridCoord *grid_coord)
bool multires_reshape_assign_final_coords_from_ccg(const MultiresReshapeContext *reshape_context, SubdivCCG *subdiv_ccg)
int grid_xy_to_vert(const CCGKey &key, const int grid, const int x, const int y)
struct MultiresReshapeContext::@114377305000353127144042327223234020156043120202 reshape
blender::float3 * displacement
blender::Array< float > masks
blender::Array< blender::float3 > positions