Blender  V3.3
multires_reshape.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2020 Blender Foundation. All rights reserved. */
3 
8 #include "MEM_guardedalloc.h"
9 
10 #include "DNA_mesh_types.h"
11 #include "DNA_meshdata_types.h"
12 #include "DNA_modifier_types.h"
13 #include "DNA_scene_types.h"
14 
15 #include "BKE_customdata.h"
16 #include "BKE_lib_id.h"
17 #include "BKE_mesh.h"
18 #include "BKE_mesh_runtime.h"
19 #include "BKE_modifier.h"
20 #include "BKE_multires.h"
21 #include "BKE_subdiv.h"
22 #include "BKE_subsurf.h"
23 #include "BLI_math_vector.h"
24 
25 #include "DEG_depsgraph_query.h"
26 
27 #include "multires_reshape.h"
28 
29 /* -------------------------------------------------------------------- */
34  struct Object *object,
35  struct MultiresModifierData *mmd,
36  const float (*vert_coords)[3],
37  const int num_vert_coords)
38 {
39  MultiresReshapeContext reshape_context;
40  if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
41  return false;
42  }
43  multires_reshape_store_original_grids(&reshape_context);
44  multires_reshape_ensure_grids(object->data, reshape_context.top.level);
46  &reshape_context, vert_coords, num_vert_coords)) {
47  multires_reshape_context_free(&reshape_context);
48  return false;
49  }
52  multires_reshape_context_free(&reshape_context);
53  return true;
54 }
55 
57  struct MultiresModifierData *mmd,
58  struct Object *dst,
59  struct Object *src)
60 {
61  struct Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
62  struct Object *src_eval = DEG_get_evaluated_object(depsgraph, src);
63  Mesh *src_mesh_eval = mesh_get_eval_final(depsgraph, scene_eval, src_eval, &CD_MASK_BAREMESH);
64 
65  int num_deformed_verts;
66  float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(src_mesh_eval, &num_deformed_verts);
67 
69  depsgraph, dst, mmd, deformed_verts, num_deformed_verts);
70 
71  MEM_freeN(deformed_verts);
72 
73  return result;
74 }
75 
78 /* -------------------------------------------------------------------- */
83  struct Object *object,
84  struct MultiresModifierData *mmd,
85  struct ModifierData *deform_md)
86 {
87  MultiresModifierData highest_mmd = *mmd;
88  highest_mmd.sculptlvl = highest_mmd.totlvl;
89  highest_mmd.lvl = highest_mmd.totlvl;
90  highest_mmd.renderlvl = highest_mmd.totlvl;
91 
92  /* Create mesh for the multires, ignoring any further modifiers (leading
93  * deformation modifiers will be applied though). */
94  Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
95  int num_deformed_verts;
96  float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(multires_mesh, &num_deformed_verts);
97 
98  /* Apply deformation modifier on the multires, */
99  const ModifierEvalContext modifier_ctx = {
100  .depsgraph = depsgraph,
101  .object = object,
103  };
105  deform_md, &modifier_ctx, multires_mesh, deformed_verts, multires_mesh->totvert);
106  BKE_id_free(NULL, multires_mesh);
107 
108  /* Reshaping */
110  depsgraph, object, &highest_mmd, deformed_verts, num_deformed_verts);
111 
112  /* Cleanup */
113  MEM_freeN(deformed_verts);
114 
115  return result;
116 }
117 
120 /* -------------------------------------------------------------------- */
124 bool multiresModifier_reshapeFromCCG(const int tot_level,
125  Mesh *coarse_mesh,
126  struct SubdivCCG *subdiv_ccg)
127 {
128  MultiresReshapeContext reshape_context;
130  &reshape_context, subdiv_ccg, coarse_mesh, tot_level)) {
131  return false;
132  }
133 
134  multires_ensure_external_read(coarse_mesh, reshape_context.top.level);
135 
136  multires_reshape_store_original_grids(&reshape_context);
137  multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
138  if (!multires_reshape_assign_final_coords_from_ccg(&reshape_context, subdiv_ccg)) {
139  multires_reshape_context_free(&reshape_context);
140  return false;
141  }
144  multires_reshape_context_free(&reshape_context);
145  return true;
146 }
147 
150 /* -------------------------------------------------------------------- */
157 {
158  const int top_level = mmd->totlvl + 1;
159  multiresModifier_subdivide_to_level(object, mmd, top_level, mode);
160 }
161 
163  struct MultiresModifierData *mmd,
164  const int top_level,
166 {
167  if (top_level <= mmd->totlvl) {
168  return;
169  }
170 
171  Mesh *coarse_mesh = object->data;
172  if (coarse_mesh->totloop == 0) {
173  /* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output
174  * from here as there is nothing to subdivide. */
175  return;
176  }
177 
178  MultiresReshapeContext reshape_context;
179 
180  /* There was no multires at all, all displacement is at 0. Can simply make sure all mdisps grids
181  * are allocated at a proper level and return. */
182  const bool has_mdisps = CustomData_has_layer(&coarse_mesh->ldata, CD_MDISPS);
183  if (!has_mdisps) {
184  CustomData_add_layer(&coarse_mesh->ldata, CD_MDISPS, CD_CALLOC, NULL, coarse_mesh->totloop);
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 
212  multires_reshape_store_original_grids(&reshape_context);
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  Object *object,
243 {
245 
246  MultiresReshapeContext reshape_context;
247  if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
248  return;
249  }
250 
251  multires_reshape_store_original_grids(&reshape_context);
252 
253  /* At this point base_mesh is object's mesh, the subdiv is initialized to the deformed state of
254  * the base mesh.
255  * Store coordinates of top level grids in object space which will define true shape we would
256  * want to reshape to after modifying the base mesh. */
258 
259  /* For modifying base mesh we only want to consider deformation caused by multires displacement
260  * and ignore all deformation which might be caused by deformation modifiers leading the multires
261  * one.
262  * So refine the subdiv to the original mesh vertices positions, which will also need to make
263  * it so object space displacement is re-evaluated for them (as in, can not re-use any knowledge
264  * from the final coordinates in the object space ). */
266 
267  /* Modify original mesh coordinates. This happens in two steps:
268  * - Coordinates are set to their final location, where they are intended to be in the final
269  * result.
270  * - Heuristic moves them a bit, kind of canceling out the effect of subsurf (so then when
271  * multires modifier applies subsurf vertices are placed at the desired location). */
274 
275  /* Reshape to the stored final state.
276  * Not that the base changed, so the subdiv is to be refined to the new positions. Unfortunately,
277  * this can not be done foe entirely cheap: if there were deformation modifiers prior to the
278  * multires they need to be re-evaluated for the new base mesh. */
281 
282  multires_reshape_context_free(&reshape_context);
283 }
284 
typedef float(TangentPoint)[2]
CustomData interface, see also DNA_customdata_types.h.
@ CD_CALLOC
bool CustomData_has_layer(const struct CustomData *data, int type)
const CustomData_MeshMasks CD_MASK_BAREMESH
Definition: customdata.cc:2051
void * CustomData_add_layer(struct CustomData *data, int type, eCDAllocType alloctype, void *layer, int totelem)
Definition: customdata.cc:2776
void BKE_id_free(struct Main *bmain, void *idv)
float(* BKE_mesh_vert_coords_alloc(const struct Mesh *mesh, int *r_vert_len))[3]
struct Mesh * mesh_get_eval_final(struct Depsgraph *depsgraph, const struct Scene *scene, struct Object *ob, const struct CustomData_MeshMasks *dataMask)
void BKE_modifier_deform_verts(ModifierData *md, const struct ModifierEvalContext *ctx, struct Mesh *me, float(*vertexCos)[3], int numVerts)
@ MOD_APPLY_USECACHE
Definition: BKE_modifier.h:118
@ MOD_APPLY_IGNORE_SIMPLIFY
Definition: BKE_modifier.h:123
void multires_set_tot_level(struct Object *ob, struct MultiresModifierData *mmd, int lvl)
Definition: multires.c:365
void multires_force_sculpt_rebuild(struct Object *object)
Definition: multires.c:437
void multires_ensure_external_read(struct Mesh *mesh, int top_level)
Definition: multires.c:1482
eMultiresSubdivideModeType
Definition: BKE_multires.h:195
@ MULTIRES_SUBDIVIDE_LINEAR
Definition: BKE_multires.h:198
@ MULTIRES_SUBDIVIDE_SIMPLE
Definition: BKE_multires.h:197
void multires_subdivide_create_tangent_displacement_linear_grids(struct Object *object, struct MultiresModifierData *mmd)
void multires_flush_sculpt_updates(struct Object *object)
Definition: multires.c:408
struct Mesh * BKE_multires_create_mesh(struct Depsgraph *depsgraph, struct Object *object, struct MultiresModifierData *mmd)
Definition: multires.c:226
#define ELEM(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
Read Guarded memory(de)allocation.
const Depsgraph * depsgraph
SyclQueue void void * src
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void multiresModifier_subdivide_to_level(struct Object *object, struct MultiresModifierData *mmd, const int top_level, const eMultiresSubdivideModeType mode)
bool multiresModifier_reshapeFromCCG(const int tot_level, Mesh *coarse_mesh, struct SubdivCCG *subdiv_ccg)
void multiresModifier_base_apply(struct Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
bool multiresModifier_reshapeFromDeformModifier(struct Depsgraph *depsgraph, struct Object *object, struct MultiresModifierData *mmd, struct ModifierData *deform_md)
void multiresModifier_subdivide(Object *object, MultiresModifierData *mmd, const eMultiresSubdivideModeType mode)
bool multiresModifier_reshapeFromObject(struct Depsgraph *depsgraph, struct MultiresModifierData *mmd, struct Object *dst, struct Object *src)
bool multiresModifier_reshapeFromVertcos(struct Depsgraph *depsgraph, struct Object *object, struct MultiresModifierData *mmd, const float(*vert_coords)[3], const int num_vert_coords)
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)
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(struct Mesh *mesh, int level)
void multires_reshape_apply_base_refine_from_base(MultiresReshapeContext *reshape_context)
bool multires_reshape_context_create_from_modifier(MultiresReshapeContext *reshape_context, struct Object *object, struct MultiresModifierData *mmd, int top_level)
bool multires_reshape_context_create_from_ccg(MultiresReshapeContext *reshape_context, struct SubdivCCG *subdiv_ccg, struct Mesh *base_mesh, int top_level)
void multires_reshape_free_original_grids(MultiresReshapeContext *reshape_context)
bool multires_reshape_context_create_from_object(MultiresReshapeContext *reshape_context, struct Depsgraph *depsgraph, struct Object *object, struct MultiresModifierData *mmd)
void multires_reshape_apply_base_update_mesh_coords(MultiresReshapeContext *reshape_context)
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)
void multires_reshape_apply_base_refine_from_deform(MultiresReshapeContext *reshape_context)
bool multires_reshape_assign_final_coords_from_ccg(const MultiresReshapeContext *reshape_context, struct SubdivCCG *subdiv_ccg)
int totvert
int totloop
CustomData ldata
struct Depsgraph * depsgraph
Definition: BKE_modifier.h:140
struct MultiresReshapeContext::@100 top
void * data