Blender V5.0
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,
30{
31 MultiresReshapeContext reshape_context;
32 if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
33 return false;
34 }
36 multires_reshape_ensure_grids(static_cast<Mesh *>(object->data), reshape_context.top.level);
37 if (!multires_reshape_assign_final_coords_from_vertcos(&reshape_context, positions)) {
38 multires_reshape_context_free(&reshape_context);
39 return false;
40 }
43 multires_reshape_context_free(&reshape_context);
44 return true;
45}
46
49 Object *dst,
50 Object *src)
51{
52 const Object *ob_eval = DEG_get_evaluated(depsgraph, src);
53 if (!ob_eval) {
54 return false;
55 }
56 const Mesh *src_mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
57 if (!src_mesh_eval) {
58 return false;
59 }
60
61 return multiresModifier_reshapeFromVertcos(depsgraph, dst, mmd, src_mesh_eval->vert_positions());
62}
63
65
66/* -------------------------------------------------------------------- */
69
71 Object *object,
73 ModifierData *deform_md)
74{
75 using namespace blender;
76 MultiresModifierData highest_mmd = blender::dna::shallow_copy(*mmd);
77 highest_mmd.sculptlvl = highest_mmd.totlvl;
78 highest_mmd.lvl = highest_mmd.totlvl;
79 highest_mmd.renderlvl = highest_mmd.totlvl;
80
81 /* Create mesh for the multires, ignoring any further modifiers (leading
82 * deformation modifiers will be applied though). */
83 Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
84 Array<float3> deformed_verts(multires_mesh->vert_positions());
85
86 /* Apply deformation modifier on the multires, */
87 ModifierEvalContext modifier_ctx{};
88 modifier_ctx.depsgraph = depsgraph;
89 modifier_ctx.object = object;
91
92 const bool deform_success = BKE_modifier_deform_verts(
93 deform_md, &modifier_ctx, multires_mesh, deformed_verts);
94 BKE_id_free(nullptr, multires_mesh);
95 if (!deform_success) {
96 return false;
97 }
98
99 /* Reshaping */
101 depsgraph, object, &highest_mmd, deformed_verts);
102
103 return result;
104}
105
107
108/* -------------------------------------------------------------------- */
111
112bool multiresModifier_reshapeFromCCG(const int tot_level, Mesh *coarse_mesh, SubdivCCG *subdiv_ccg)
113{
114 MultiresReshapeContext reshape_context;
116 &reshape_context, subdiv_ccg, coarse_mesh, tot_level))
117 {
118 return false;
119 }
120
121 multires_ensure_external_read(coarse_mesh, reshape_context.top.level);
122
124 multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
125 if (!multires_reshape_assign_final_coords_from_ccg(&reshape_context, subdiv_ccg)) {
126 multires_reshape_context_free(&reshape_context);
127 return false;
128 }
131 multires_reshape_context_free(&reshape_context);
132 return true;
133}
134
136
137/* -------------------------------------------------------------------- */
140
143 const MultiresSubdivideModeType mode)
144{
145 const int top_level = mmd->totlvl + 1;
146 multiresModifier_subdivide_to_level(object, mmd, top_level, mode);
147}
148
151 const int top_level,
152 const MultiresSubdivideModeType mode)
153{
154 if (top_level <= mmd->totlvl) {
155 return;
156 }
157
158 Mesh *coarse_mesh = static_cast<Mesh *>(object->data);
159 if (coarse_mesh->corners_num == 0) {
160 /* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output
161 * from here as there is nothing to subdivide. */
162 return;
163 }
164
165 MultiresReshapeContext reshape_context;
166
167 /* There was no multires at all, all displacement is at 0. Can simply make sure all mdisps grids
168 * are allocated at a proper level and return. */
169 const bool has_mdisps = CustomData_has_layer(&coarse_mesh->corner_data, CD_MDISPS);
170 if (!has_mdisps) {
172 &coarse_mesh->corner_data, CD_MDISPS, CD_SET_DEFAULT, coarse_mesh->corners_num);
173 }
174
175 /* NOTE: Subdivision happens from the top level of the existing multires modifier. If it is set
176 * to 0 and there is mdisps layer it would mean that the modifier went out of sync with the data.
177 * This happens when, for example, linking modifiers from one object to another.
178 *
179 * In such cases simply ensure grids to be the proper level.
180 *
181 * If something smarter is needed it is up to the operators which does data synchronization, so
182 * that the mdisps layer is also synchronized. */
183 if (!has_mdisps || top_level == 1 || mmd->totlvl == 0) {
184 multires_reshape_ensure_grids(coarse_mesh, top_level);
187 }
188 else {
189 multires_set_tot_level(object, mmd, top_level);
190 }
191 return;
192 }
193
195
196 if (!multires_reshape_context_create_from_modifier(&reshape_context, object, mmd, top_level)) {
197 return;
198 }
199
201 multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
203
204 /* Free original grids which makes it so smoothing with details thinks all the details were
205 * added against base mesh's limit surface. This is similar behavior to as if we've done all
206 * displacement in sculpt mode at the old top level and then propagated to the new top level. */
207 multires_reshape_free_original_grids(&reshape_context);
208
210 multires_reshape_smooth_object_grids(&reshape_context, mode);
211 }
212 else {
214 }
215
217 multires_reshape_context_free(&reshape_context);
218
219 multires_set_tot_level(object, mmd, top_level);
220}
221
223
224/* -------------------------------------------------------------------- */
227
229 Object *object,
231 const ApplyBaseMode mode)
232{
234
235 MultiresReshapeContext reshape_context;
236 if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
237 return;
238 }
239
241
242 /* At this point base_mesh is object's mesh, the subdiv is initialized to the deformed state of
243 * the base mesh.
244 * Store coordinates of top level grids in object space which will define true shape we would
245 * want to reshape to after modifying the base mesh. */
247
248 /* For modifying base mesh we only want to consider deformation caused by multires displacement
249 * and ignore all deformation which might be caused by deformation modifiers leading the multires
250 * one.
251 * So refine the subdiv to the original mesh vertices positions, which will also need to make
252 * it so object space displacement is re-evaluated for them (as in, can not re-use any knowledge
253 * from the final coordinates in the object space ). */
255
256 /* Modify original mesh coordinates. This happens in two steps:
257 * - Coordinates are set to their final location, where they are intended to be in the final
258 * result.
259 * - Heuristic moves them a bit, kind of canceling out the effect of subsurf (so then when
260 * multires modifier applies subsurf vertices are placed at the desired location). */
262 if (mode == ApplyBaseMode::ForSubdivision) {
264 }
265
266 /* Reshape to the stored final state.
267 * Not that the base changed, so the subdiv is to be refined to the new positions. Unfortunately,
268 * this can not be done foe entirely cheap: if there were deformation modifiers prior to the
269 * multires they need to be re-evaluated for the new base mesh. */
272
273 multires_reshape_context_free(&reshape_context);
274}
275
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:270
Mesh * BKE_multires_create_mesh(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
Definition multires.cc:99
void multires_ensure_external_read(Mesh *mesh, int top_level)
Definition multires.cc:766
MultiresSubdivideModeType
ApplyBaseMode
void multires_force_sculpt_rebuild(Object *object)
Definition multires.cc:327
void multires_set_tot_level(Object *ob, MultiresModifierData *mmd, int lvl)
Definition multires.cc:225
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
void multiresModifier_base_apply(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, const ApplyBaseMode mode)
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)
static bool multiresModifier_reshapeFromVertcos(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, blender::Span< blender::float3 > positions)
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)
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_assign_final_coords_from_vertcos(const MultiresReshapeContext *reshape_context, blender::Span< blender::float3 > positions)
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
ModifierApplyFlag flag
struct MultiresReshapeContext::@204160262355230072153262361266331214205042063262 top