Blender V5.3
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
22namespace blender {
23
32 MultiresReshapeContext *reshape_context)
33{
34 if (reshape_context->top.level == reshape_context->reshape.level) {
35 return;
36 }
38}
39
40/* -------------------------------------------------------------------- */
43
44static bool multiresModifier_reshapeFromVertcos(Depsgraph *depsgraph,
45 Object *object,
47 Span<float3> positions)
48{
49 MultiresReshapeContext reshape_context;
50 if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
51 return false;
52 }
54 multires_reshape_ensure_grids(id_cast<Mesh *>(object->data), reshape_context.top.level);
55 if (!multires_reshape_assign_final_coords_from_vertcos(&reshape_context, positions)) {
56 multires_reshape_context_free(&reshape_context);
57 return false;
58 }
61 multires_reshape_context_free(&reshape_context);
62 return true;
63}
64
65bool multiresModifier_reshapeFromObject(Depsgraph *depsgraph,
67 Object *dst,
68 Object *src)
69{
70 const Object *ob_eval = DEG_get_evaluated(depsgraph, src);
71 if (!ob_eval) {
72 return false;
73 }
74 const Mesh *src_mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
75 if (!src_mesh_eval) {
76 return false;
77 }
78
79 return multiresModifier_reshapeFromVertcos(depsgraph, dst, mmd, src_mesh_eval->vert_positions());
80}
81
83
84/* -------------------------------------------------------------------- */
87
89 Object *object,
91 ModifierData *deform_md)
92{
93 MultiresModifierData highest_mmd = dna::shallow_copy(*mmd);
94 highest_mmd.sculptlvl = highest_mmd.totlvl;
95 highest_mmd.lvl = highest_mmd.totlvl;
96 highest_mmd.renderlvl = highest_mmd.totlvl;
97
98 /* Create mesh for the multires, ignoring any further modifiers (leading
99 * deformation modifiers will be applied though). */
100 Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
101 Array<float3> deformed_verts(multires_mesh->vert_positions());
102
103 /* Apply deformation modifier on the multires, */
104 ModifierEvalContext modifier_ctx{};
105 modifier_ctx.depsgraph = depsgraph;
106 modifier_ctx.object = object;
108
109 const bool deform_success = BKE_modifier_deform_verts(
110 deform_md, &modifier_ctx, multires_mesh, deformed_verts);
111 BKE_id_free(nullptr, multires_mesh);
112 if (!deform_success) {
113 return false;
114 }
115
116 /* Reshaping */
118 depsgraph, object, &highest_mmd, deformed_verts);
119
120 return result;
121}
122
124
125/* -------------------------------------------------------------------- */
128
129bool multiresModifier_reshapeFromCCG(const int tot_level, Mesh *coarse_mesh, SubdivCCG *subdiv_ccg)
130{
131 MultiresReshapeContext reshape_context;
133 &reshape_context, subdiv_ccg, coarse_mesh, tot_level))
134 {
135 return false;
136 }
137
138 multires_ensure_external_read(coarse_mesh, reshape_context.top.level);
139
141 multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
142 if (!multires_reshape_assign_final_coords_from_ccg(&reshape_context, subdiv_ccg)) {
143 multires_reshape_context_free(&reshape_context);
144 return false;
145 }
148 multires_reshape_context_free(&reshape_context);
149 return true;
150}
151
153
154/* -------------------------------------------------------------------- */
157
160 const MultiresSubdivideModeType mode)
161{
162 const int top_level = mmd->totlvl + 1;
163 multiresModifier_subdivide_to_level(object, mmd, top_level, mode);
164}
165
168 const int top_level,
169 const MultiresSubdivideModeType mode)
170{
171 if (top_level <= mmd->totlvl) {
172 return;
173 }
174
175 Mesh *coarse_mesh = id_cast<Mesh *>(object->data);
176 if (coarse_mesh->corners_num == 0) {
177 /* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output
178 * from here as there is nothing to subdivide. */
179 return;
180 }
181
182 MultiresReshapeContext reshape_context;
183
184 /* There was no multires at all, all displacement is at 0. Can simply make sure all mdisps grids
185 * are allocated at a proper level and return. */
186 const bool has_mdisps = CustomData_has_layer(&coarse_mesh->corner_data, CD_MDISPS);
187 if (!has_mdisps) {
189 &coarse_mesh->corner_data, CD_MDISPS, CD_SET_DEFAULT, coarse_mesh->corners_num);
190 }
191
192 /* NOTE: Subdivision happens from the top level of the existing multires modifier. If it is set
193 * to 0 and there is mdisps layer it would mean that the modifier went out of sync with the data.
194 * This happens when, for example, linking modifiers from one object to another.
195 *
196 * In such cases simply ensure grids to be the proper level.
197 *
198 * If something smarter is needed it is up to the operators which does data synchronization, so
199 * that the mdisps layer is also synchronized. */
200 if (!has_mdisps || top_level == 1 || mmd->totlvl == 0) {
201 multires_reshape_ensure_grids(coarse_mesh, top_level);
204 }
205 else {
206 multires_set_tot_level(object, mmd, top_level);
207 }
208 return;
209 }
210
212
213 if (!multires_reshape_context_create_from_modifier(&reshape_context, object, mmd, top_level)) {
214 return;
215 }
216
218 multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
220
221 /* Free original grids which makes it so smoothing with details thinks all the details were
222 * added against base mesh's limit surface. This is similar behavior to as if we've done all
223 * displacement in sculpt mode at the old top level and then propagated to the new top level. */
224 multires_reshape_free_original_grids(&reshape_context);
225
227 multires_reshape_smooth_object_grids(&reshape_context, mode);
228 }
229 else {
231 }
232
234 multires_reshape_context_free(&reshape_context);
235
236 multires_set_tot_level(object, mmd, top_level);
237}
238
240
241/* -------------------------------------------------------------------- */
244
245void multiresModifier_base_apply(Depsgraph *depsgraph,
246 Object *object,
248 const ApplyBaseMode mode)
249{
251
252 MultiresReshapeContext reshape_context;
253 if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
254 return;
255 }
256
258
259 /* At this point base_mesh is object's mesh, the subdiv is initialized to the deformed state of
260 * the base mesh.
261 * Store coordinates of top level grids in object space which will define true shape we would
262 * want to reshape to after modifying the base mesh. */
264
265 /* For modifying base mesh we only want to consider deformation caused by multires displacement
266 * and ignore all deformation which might be caused by deformation modifiers leading the multires
267 * one.
268 * So refine the subdiv to the original mesh vertices positions, which will also need to make
269 * it so object space displacement is re-evaluated for them (as in, can not re-use any knowledge
270 * from the final coordinates in the object space ). */
272
273 /* Modify original mesh coordinates. This happens in two steps:
274 * - Coordinates are set to their final location, where they are intended to be in the final
275 * result.
276 * - Heuristic moves them a bit, kind of canceling out the effect of subsurf (so then when
277 * multires modifier applies subsurf vertices are placed at the desired location). */
279 if (mode == ApplyBaseMode::ForSubdivision) {
281 }
283
284 /* Reshape to the stored final state.
285 * Not that the base changed, so the subdiv is to be refined to the new positions. Unfortunately,
286 * this can not be done foe entirely cheap: if there were deformation modifiers prior to the
287 * multires they need to be re-evaluated for the new base mesh. */
290
291 multires_reshape_context_free(&reshape_context);
292}
293
295
296} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
General operations, lookup, etc. for blender objects.
#define ELEM(...)
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
BMesh Iterators.
void multires_reshape_assign_final_coords_from_mdisps(const MultiresReshapeContext *reshape_context)
void multires_reshape_store_original_grids(MultiresReshapeContext *reshape_context)
void multires_reshape_smooth_object_grids(const MultiresReshapeContext *reshape_context, MultiresSubdivideModeType mode)
void multires_reshape_apply_base_update_mesh_coords(MultiresReshapeContext *reshape_context)
static bool multiresModifier_reshapeFromVertcos(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, Span< float3 > positions)
Mesh * BKE_multires_create_mesh(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
Definition multires.cc:103
void multires_reshape_smooth_object_grids_with_details(const MultiresReshapeContext *reshape_context)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void multires_reshape_ensure_grids(Mesh *mesh, int level)
void multiresModifier_subdivide(Object *object, MultiresModifierData *mmd, MultiresSubdivideModeType mode)
void multiresModifier_subdivide_to_level(Object *object, MultiresModifierData *mmd, int top_level, MultiresSubdivideModeType mode)
void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape_context)
void multires_reshape_assign_final_elements_from_orig_mdisps(const MultiresReshapeContext *reshape_context)
static void multiresModifier_reshape_store_original_grids_for_smoothing(MultiresReshapeContext *reshape_context)
void multires_flush_sculpt_updates(Object *object)
Definition multires.cc:277
bool multires_reshape_assign_final_coords_from_vertcos(const MultiresReshapeContext *reshape_context, Span< float3 > positions)
void multiresModifier_base_apply(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, ApplyBaseMode mode)
bool BKE_modifier_deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, MutableSpan< float3 > positions)
bool multiresModifier_reshapeFromObject(Depsgraph *depsgraph, MultiresModifierData *mmd, Object *dst, Object *src)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void multires_reshape_apply_base_refine_from_base(MultiresReshapeContext *reshape_context)
@ MOD_APPLY_IGNORE_SIMPLIFY
@ MOD_APPLY_USECACHE
void multires_reshape_free_original_grids(MultiresReshapeContext *reshape_context)
MultiresSubdivideModeType
void multires_reshape_apply_base_update_shape_key(MultiresReshapeContext *reshape_context)
void multires_force_sculpt_rebuild(Object *object)
Definition multires.cc:332
void multires_set_tot_level(Object *ob, MultiresModifierData *mmd, int lvl)
Definition multires.cc:232
void BKE_id_free(Main *bmain, void *idv)
void multires_ensure_external_read(Mesh *mesh, int top_level)
Definition multires.cc:766
bool multires_reshape_context_create_from_object(MultiresReshapeContext *reshape_context, Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
void multires_reshape_context_free(MultiresReshapeContext *reshape_context)
void multires_reshape_apply_base_refine_from_deform(MultiresReshapeContext *reshape_context)
bool multiresModifier_reshapeFromCCG(int tot_level, Mesh *coarse_mesh, SubdivCCG *subdiv_ccg)
void multires_reshape_object_grids_to_tangent_displacement(const MultiresReshapeContext *reshape_context)
bool multiresModifier_reshapeFromDeformModifier(Depsgraph *depsgraph, Object *ob, MultiresModifierData *mmd, ModifierData *deform_md)
bool multires_reshape_assign_final_coords_from_ccg(const MultiresReshapeContext *reshape_context, SubdivCCG *subdiv_ccg)
bool multires_reshape_context_create_from_modifier(MultiresReshapeContext *reshape_context, Object *object, MultiresModifierData *mmd, int top_level)
void multires_subdivide_create_tangent_displacement_linear_grids(Object *object, MultiresModifierData *mmd)
bool multires_reshape_context_create_from_ccg(MultiresReshapeContext *reshape_context, SubdivCCG *subdiv_ccg, Mesh *base_mesh, int top_level)
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
CustomData corner_data
struct blender::MultiresReshapeContext::@021252173372013124221256162374334156074326245244 top
struct blender::MultiresReshapeContext::@034344347246210140240253130013212240027344072112 reshape