Blender V4.5
multires_reshape.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 "DNA_mesh_types.h"
10#include "DNA_modifier_types.h"
11
12#include "BKE_customdata.hh"
13#include "BKE_lib_id.hh"
14#include "BKE_modifier.hh"
15#include "BKE_multires.hh"
16#include "BKE_object.hh"
17
19
20#include "multires_reshape.hh"
21
22/* -------------------------------------------------------------------- */
25
27 Object *object,
29 const float (*vert_coords)[3],
30 const int num_vert_coords)
31{
32 MultiresReshapeContext reshape_context;
33 if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
34 return false;
35 }
37 multires_reshape_ensure_grids(static_cast<Mesh *>(object->data), reshape_context.top.level);
39 &reshape_context, vert_coords, num_vert_coords))
40 {
41 multires_reshape_context_free(&reshape_context);
42 return false;
43 }
46 multires_reshape_context_free(&reshape_context);
47 return true;
48}
49
52 Object *dst,
53 Object *src)
54{
55 const Object *ob_eval = DEG_get_evaluated(depsgraph, src);
56 if (!ob_eval) {
57 return false;
58 }
59 const Mesh *src_mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
60 if (!src_mesh_eval) {
61 return false;
62 }
63
66 dst,
67 mmd,
68 reinterpret_cast<const float(*)[3]>(src_mesh_eval->vert_positions().data()),
69 src_mesh_eval->verts_num);
70}
71
73
74/* -------------------------------------------------------------------- */
77
79 Object *object,
81 ModifierData *deform_md)
82{
83 using namespace blender;
84 MultiresModifierData highest_mmd = blender::dna::shallow_copy(*mmd);
85 highest_mmd.sculptlvl = highest_mmd.totlvl;
86 highest_mmd.lvl = highest_mmd.totlvl;
87 highest_mmd.renderlvl = highest_mmd.totlvl;
88
89 /* Create mesh for the multires, ignoring any further modifiers (leading
90 * deformation modifiers will be applied though). */
91 Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
92 Array<float3> deformed_verts(multires_mesh->vert_positions());
93
94 /* Apply deformation modifier on the multires, */
95 ModifierEvalContext modifier_ctx{};
96 modifier_ctx.depsgraph = depsgraph;
97 modifier_ctx.object = object;
99
100 const bool deform_success = BKE_modifier_deform_verts(
101 deform_md, &modifier_ctx, multires_mesh, deformed_verts);
102 BKE_id_free(nullptr, multires_mesh);
103 if (!deform_success) {
104 return false;
105 }
106
107 /* Reshaping */
109 depsgraph,
110 object,
111 &highest_mmd,
112 reinterpret_cast<float(*)[3]>(deformed_verts.data()),
113 deformed_verts.size());
114
115 return result;
116}
117
119
120/* -------------------------------------------------------------------- */
123
124bool multiresModifier_reshapeFromCCG(const int tot_level, Mesh *coarse_mesh, SubdivCCG *subdiv_ccg)
125{
126 MultiresReshapeContext reshape_context;
128 &reshape_context, subdiv_ccg, coarse_mesh, tot_level))
129 {
130 return false;
131 }
132
133 multires_ensure_external_read(coarse_mesh, reshape_context.top.level);
134
136 multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
137 if (!multires_reshape_assign_final_coords_from_ccg(&reshape_context, subdiv_ccg)) {
138 multires_reshape_context_free(&reshape_context);
139 return false;
140 }
143 multires_reshape_context_free(&reshape_context);
144 return true;
145}
146
148
149/* -------------------------------------------------------------------- */
152
155 const MultiresSubdivideModeType mode)
156{
157 const int top_level = mmd->totlvl + 1;
158 multiresModifier_subdivide_to_level(object, mmd, top_level, mode);
159}
160
163 const int top_level,
164 const MultiresSubdivideModeType mode)
165{
166 if (top_level <= mmd->totlvl) {
167 return;
168 }
169
170 Mesh *coarse_mesh = static_cast<Mesh *>(object->data);
171 if (coarse_mesh->corners_num == 0) {
172 /* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output
173 * from here as there is nothing to subdivide. */
174 return;
175 }
176
177 MultiresReshapeContext reshape_context;
178
179 /* There was no multires at all, all displacement is at 0. Can simply make sure all mdisps grids
180 * are allocated at a proper level and return. */
181 const bool has_mdisps = CustomData_has_layer(&coarse_mesh->corner_data, CD_MDISPS);
182 if (!has_mdisps) {
184 &coarse_mesh->corner_data, CD_MDISPS, CD_SET_DEFAULT, coarse_mesh->corners_num);
185 }
186
187 /* NOTE: Subdivision happens from the top level of the existing multires modifier. If it is set
188 * to 0 and there is mdisps layer it would mean that the modifier went out of sync with the data.
189 * This happens when, for example, linking modifiers from one object to another.
190 *
191 * In such cases simply ensure grids to be the proper level.
192 *
193 * If something smarter is needed it is up to the operators which does data synchronization, so
194 * that the mdisps layer is also synchronized. */
195 if (!has_mdisps || top_level == 1 || mmd->totlvl == 0) {
196 multires_reshape_ensure_grids(coarse_mesh, top_level);
199 }
200 else {
201 multires_set_tot_level(object, mmd, top_level);
202 }
203 return;
204 }
205
207
208 if (!multires_reshape_context_create_from_modifier(&reshape_context, object, mmd, top_level)) {
209 return;
210 }
211
213 multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
215
216 /* Free original grids which makes it so smoothing with details thinks all the details were
217 * added against base mesh's limit surface. This is similar behavior to as if we've done all
218 * displacement in sculpt mode at the old top level and then propagated to the new top level. */
219 multires_reshape_free_original_grids(&reshape_context);
220
222 multires_reshape_smooth_object_grids(&reshape_context, mode);
223 }
224 else {
226 }
227
229 multires_reshape_context_free(&reshape_context);
230
231 multires_set_tot_level(object, mmd, top_level);
232}
233
235
236/* -------------------------------------------------------------------- */
239
241{
243
244 MultiresReshapeContext reshape_context;
245 if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
246 return;
247 }
248
250
251 /* At this point base_mesh is object's mesh, the subdiv is initialized to the deformed state of
252 * the base mesh.
253 * Store coordinates of top level grids in object space which will define true shape we would
254 * want to reshape to after modifying the base mesh. */
256
257 /* For modifying base mesh we only want to consider deformation caused by multires displacement
258 * and ignore all deformation which might be caused by deformation modifiers leading the multires
259 * one.
260 * So refine the subdiv to the original mesh vertices positions, which will also need to make
261 * it so object space displacement is re-evaluated for them (as in, can not re-use any knowledge
262 * from the final coordinates in the object space ). */
264
265 /* Modify original mesh coordinates. This happens in two steps:
266 * - Coordinates are set to their final location, where they are intended to be in the final
267 * result.
268 * - Heuristic moves them a bit, kind of canceling out the effect of subsurf (so then when
269 * multires modifier applies subsurf vertices are placed at the desired location). */
272
273 /* Reshape to the stored final state.
274 * Not that the base changed, so the subdiv is to be refined to the new positions. Unfortunately,
275 * this can not be done foe entirely cheap: if there were deformation modifiers prior to the
276 * multires they need to be re-evaluated for the new base mesh. */
279
280 multires_reshape_context_free(&reshape_context);
281}
282
CustomData interface, see also DNA_customdata_types.h.
@ CD_SET_DEFAULT
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
void BKE_id_free(Main *bmain, void *idv)
bool BKE_modifier_deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, blender::MutableSpan< blender::float3 > positions)
@ MOD_APPLY_USECACHE
@ MOD_APPLY_IGNORE_SIMPLIFY
void multires_subdivide_create_tangent_displacement_linear_grids(Object *object, MultiresModifierData *mmd)
void multires_flush_sculpt_updates(Object *object)
Definition multires.cc:388
Mesh * BKE_multires_create_mesh(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
Definition multires.cc:217
void multires_ensure_external_read(Mesh *mesh, int top_level)
Definition multires.cc:1464
MultiresSubdivideModeType
void multires_force_sculpt_rebuild(Object *object)
Definition multires.cc:445
void multires_set_tot_level(Object *ob, MultiresModifierData *mmd, int lvl)
Definition multires.cc:343
General operations, lookup, etc. for blender objects.
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
#define ELEM(...)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
BPy_StructRNA * depsgraph
int64_t size() const
Definition BLI_array.hh:245
const T * data() const
Definition BLI_array.hh:301
bool multiresModifier_reshapeFromVertcos(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, const float(*vert_coords)[3], const int num_vert_coords)
bool multiresModifier_reshapeFromDeformModifier(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, ModifierData *deform_md)
bool multiresModifier_reshapeFromObject(Depsgraph *depsgraph, MultiresModifierData *mmd, Object *dst, Object *src)
bool multiresModifier_reshapeFromCCG(const int tot_level, Mesh *coarse_mesh, SubdivCCG *subdiv_ccg)
void multiresModifier_subdivide(Object *object, MultiresModifierData *mmd, const MultiresSubdivideModeType mode)
void multiresModifier_subdivide_to_level(Object *object, MultiresModifierData *mmd, const int top_level, const MultiresSubdivideModeType mode)
void multiresModifier_base_apply(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
void multires_reshape_assign_final_elements_from_orig_mdisps(const MultiresReshapeContext *reshape_context)
void multires_reshape_smooth_object_grids(const MultiresReshapeContext *reshape_context, enum MultiresSubdivideModeType mode)
void multires_reshape_smooth_object_grids_with_details(const MultiresReshapeContext *reshape_context)
void multires_reshape_assign_final_coords_from_mdisps(const MultiresReshapeContext *reshape_context)
bool multires_reshape_context_create_from_object(MultiresReshapeContext *reshape_context, Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape_context)
bool multires_reshape_assign_final_coords_from_vertcos(const MultiresReshapeContext *reshape_context, const float(*vert_coords)[3], int num_vert_coords)
void multires_reshape_context_free(MultiresReshapeContext *reshape_context)
void multires_reshape_store_original_grids(MultiresReshapeContext *reshape_context)
void multires_reshape_ensure_grids(Mesh *mesh, int level)
void multires_reshape_apply_base_refine_from_base(MultiresReshapeContext *reshape_context)
void multires_reshape_free_original_grids(MultiresReshapeContext *reshape_context)
void multires_reshape_apply_base_update_mesh_coords(MultiresReshapeContext *reshape_context)
bool multires_reshape_context_create_from_modifier(MultiresReshapeContext *reshape_context, Object *object, MultiresModifierData *mmd, int top_level)
void multires_reshape_object_grids_to_tangent_displacement(const MultiresReshapeContext *reshape_context)
bool multires_reshape_assign_final_coords_from_ccg(const MultiresReshapeContext *reshape_context, SubdivCCG *subdiv_ccg)
void multires_reshape_apply_base_refine_from_deform(MultiresReshapeContext *reshape_context)
bool multires_reshape_context_create_from_ccg(MultiresReshapeContext *reshape_context, SubdivCCG *subdiv_ccg, Mesh *base_mesh, int top_level)
int corners_num
CustomData corner_data
int verts_num
ModifierApplyFlag flag
struct MultiresReshapeContext::@020251072072313275303356043045365232052345353114 top