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