Blender V4.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
9#include "MEM_guardedalloc.h"
10
11#include "DNA_mesh_types.h"
12#include "DNA_modifier_types.h"
13
14#include "BKE_customdata.hh"
15#include "BKE_lib_id.hh"
16#include "BKE_modifier.hh"
17#include "BKE_multires.hh"
18#include "BKE_object.hh"
19#include "BKE_subsurf.hh"
20#include "BLI_math_vector.h"
21
23
24#include "multires_reshape.hh"
25
26/* -------------------------------------------------------------------- */
31 Object *object,
33 const float (*vert_coords)[3],
34 const int num_vert_coords)
35{
36 MultiresReshapeContext reshape_context;
37 if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
38 return false;
39 }
41 multires_reshape_ensure_grids(static_cast<Mesh *>(object->data), reshape_context.top.level);
43 &reshape_context, vert_coords, num_vert_coords))
44 {
45 multires_reshape_context_free(&reshape_context);
46 return false;
47 }
50 multires_reshape_context_free(&reshape_context);
51 return true;
52}
53
56 Object *dst,
57 Object *src)
58{
59 const Object *ob_eval = DEG_get_evaluated_object(depsgraph, src);
60 if (!ob_eval) {
61 return false;
62 }
63 const Mesh *src_mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
64 if (!src_mesh_eval) {
65 return false;
66 }
67
70 dst,
71 mmd,
72 reinterpret_cast<const float(*)[3]>(src_mesh_eval->vert_positions().data()),
73 src_mesh_eval->verts_num);
74}
75
78/* -------------------------------------------------------------------- */
83 Object *object,
85 ModifierData *deform_md)
86{
87 using namespace blender;
88 MultiresModifierData highest_mmd = blender::dna::shallow_copy(*mmd);
89 highest_mmd.sculptlvl = highest_mmd.totlvl;
90 highest_mmd.lvl = highest_mmd.totlvl;
91 highest_mmd.renderlvl = highest_mmd.totlvl;
92
93 /* Create mesh for the multires, ignoring any further modifiers (leading
94 * deformation modifiers will be applied though). */
95 Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
96 Array<float3> deformed_verts(multires_mesh->vert_positions());
97
98 /* Apply deformation modifier on the multires, */
99 ModifierEvalContext modifier_ctx{};
100 modifier_ctx.depsgraph = depsgraph;
101 modifier_ctx.object = object;
102 modifier_ctx.flag = MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY;
103
104 BKE_modifier_deform_verts(deform_md, &modifier_ctx, multires_mesh, deformed_verts);
105 BKE_id_free(nullptr, multires_mesh);
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
120/* -------------------------------------------------------------------- */
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
149/* -------------------------------------------------------------------- */
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,
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
236/* -------------------------------------------------------------------- */
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)
@ MOD_APPLY_USECACHE
@ MOD_APPLY_IGNORE_SIMPLIFY
void BKE_modifier_deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, blender::MutableSpan< blender::float3 > positions)
void multires_subdivide_create_tangent_displacement_linear_grids(Object *object, MultiresModifierData *mmd)
void multires_flush_sculpt_updates(Object *object)
Definition multires.cc:394
Mesh * BKE_multires_create_mesh(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
Definition multires.cc:224
void multires_ensure_external_read(Mesh *mesh, int top_level)
Definition multires.cc:1492
eMultiresSubdivideModeType
@ MULTIRES_SUBDIVIDE_LINEAR
@ MULTIRES_SUBDIVIDE_SIMPLE
void multires_force_sculpt_rebuild(Object *object)
Definition multires.cc:451
void multires_set_tot_level(Object *ob, MultiresModifierData *mmd, int lvl)
Definition multires.cc:351
General operations, lookup, etc. for blender objects.
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
#define ELEM(...)
Object * DEG_get_evaluated_object(const Depsgraph *depsgraph, Object *object)
Read Guarded memory(de)allocation.
int64_t size() const
Definition BLI_array.hh:245
const T * data() const
Definition BLI_array.hh:301
const Depsgraph * depsgraph
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 eMultiresSubdivideModeType mode)
void multiresModifier_subdivide_to_level(Object *object, MultiresModifierData *mmd, const int top_level, const eMultiresSubdivideModeType 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_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_smooth_object_grids(const MultiresReshapeContext *reshape_context, enum eMultiresSubdivideModeType mode)
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
struct MultiresReshapeContext::@92 top