Blender  V2.93
multires_reshape.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "DNA_mesh_types.h"
27 #include "DNA_meshdata_types.h"
28 #include "DNA_modifier_types.h"
29 #include "DNA_scene_types.h"
30 
31 #include "BKE_customdata.h"
32 #include "BKE_lib_id.h"
33 #include "BKE_mesh.h"
34 #include "BKE_mesh_runtime.h"
35 #include "BKE_modifier.h"
36 #include "BKE_multires.h"
37 #include "BKE_subdiv.h"
38 #include "BKE_subsurf.h"
39 #include "BLI_math_vector.h"
40 
41 #include "DEG_depsgraph_query.h"
42 
43 #include "multires_reshape.h"
44 
45 /* -------------------------------------------------------------------- */
50  struct Object *object,
51  struct MultiresModifierData *mmd,
52  const float (*vert_coords)[3],
53  const int num_vert_coords)
54 {
55  MultiresReshapeContext reshape_context;
56  if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
57  return false;
58  }
59  multires_reshape_store_original_grids(&reshape_context);
60  multires_reshape_ensure_grids(object->data, reshape_context.top.level);
62  &reshape_context, vert_coords, num_vert_coords)) {
63  multires_reshape_context_free(&reshape_context);
64  return false;
65  }
68  multires_reshape_context_free(&reshape_context);
69  return true;
70 }
71 
72 /* Returns truth on success, false otherwise.
73  *
74  * This function might fail in cases like source and destination not having
75  * matched amount of vertices. */
77  struct MultiresModifierData *mmd,
78  struct Object *dst,
79  struct Object *src)
80 {
81  struct Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
82  struct Object *src_eval = DEG_get_evaluated_object(depsgraph, src);
83  Mesh *src_mesh_eval = mesh_get_eval_final(depsgraph, scene_eval, src_eval, &CD_MASK_BAREMESH);
84 
85  int num_deformed_verts;
86  float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(src_mesh_eval, &num_deformed_verts);
87 
89  depsgraph, dst, mmd, deformed_verts, num_deformed_verts);
90 
91  MEM_freeN(deformed_verts);
92 
93  return result;
94 }
95 
98 /* -------------------------------------------------------------------- */
103  struct Object *object,
104  struct MultiresModifierData *mmd,
105  struct ModifierData *deform_md)
106 {
107  MultiresModifierData highest_mmd = *mmd;
108  highest_mmd.sculptlvl = highest_mmd.totlvl;
109  highest_mmd.lvl = highest_mmd.totlvl;
110  highest_mmd.renderlvl = highest_mmd.totlvl;
111 
112  /* Create mesh for the multires, ignoring any further modifiers (leading
113  * deformation modifiers will be applied though). */
114  Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
115  int num_deformed_verts;
116  float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(multires_mesh, &num_deformed_verts);
117 
118  /* Apply deformation modifier on the multires, */
119  const ModifierEvalContext modifier_ctx = {
120  .depsgraph = depsgraph,
121  .object = object,
123  };
125  deform_md, &modifier_ctx, multires_mesh, deformed_verts, multires_mesh->totvert);
126  BKE_id_free(NULL, multires_mesh);
127 
128  /* Reshaping */
130  depsgraph, object, &highest_mmd, deformed_verts, num_deformed_verts);
131 
132  /* Cleanup */
133  MEM_freeN(deformed_verts);
134 
135  return result;
136 }
137 
140 /* -------------------------------------------------------------------- */
144 bool multiresModifier_reshapeFromCCG(const int tot_level,
145  Mesh *coarse_mesh,
146  struct SubdivCCG *subdiv_ccg)
147 {
148  MultiresReshapeContext reshape_context;
150  &reshape_context, subdiv_ccg, coarse_mesh, tot_level)) {
151  return false;
152  }
153 
154  multires_ensure_external_read(coarse_mesh, reshape_context.top.level);
155 
156  multires_reshape_store_original_grids(&reshape_context);
157  multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
158  if (!multires_reshape_assign_final_coords_from_ccg(&reshape_context, subdiv_ccg)) {
159  multires_reshape_context_free(&reshape_context);
160  return false;
161  }
164  multires_reshape_context_free(&reshape_context);
165  return true;
166 }
167 
170 /* -------------------------------------------------------------------- */
177 {
178  const int top_level = mmd->totlvl + 1;
179  multiresModifier_subdivide_to_level(object, mmd, top_level, mode);
180 }
181 
183  struct MultiresModifierData *mmd,
184  const int top_level,
186 {
187  if (top_level <= mmd->totlvl) {
188  return;
189  }
190 
191  Mesh *coarse_mesh = object->data;
192  if (coarse_mesh->totloop == 0) {
193  /* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output
194  * from here as there is nothing to subdivide. */
195  return;
196  }
197 
198  MultiresReshapeContext reshape_context;
199 
200  /* There was no multires at all, all displacement is at 0. Can simply make sure all mdisps grids
201  * are allocated at a proper level and return. */
202  const bool has_mdisps = CustomData_has_layer(&coarse_mesh->ldata, CD_MDISPS);
203  if (!has_mdisps) {
204  CustomData_add_layer(&coarse_mesh->ldata, CD_MDISPS, CD_CALLOC, NULL, coarse_mesh->totloop);
205  }
206 
207  /* NOTE: Subdivision happens from the top level of the existing multires modifier. If it is set
208  * to 0 and there is mdisps layer it would mean that the modifier went out of sync with the data.
209  * This happens when, for example, linking modifiers from one object to another.
210  *
211  * In such cases simply ensure grids to be the proper level.
212  *
213  * If something smarter is needed it is up to the operators which does data synchronization, so
214  * that the mdisps layer is also synchronized. */
215  if (!has_mdisps || top_level == 1 || mmd->totlvl == 0) {
216  multires_reshape_ensure_grids(coarse_mesh, top_level);
219  }
220  else {
221  multires_set_tot_level(object, mmd, top_level);
222  }
223  return;
224  }
225 
227 
228  if (!multires_reshape_context_create_from_modifier(&reshape_context, object, mmd, top_level)) {
229  return;
230  }
231 
232  multires_reshape_store_original_grids(&reshape_context);
233  multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
235 
236  /* Free original grids which makes it so smoothing with details thinks all the details were
237  * added against base mesh's limit surface. This is similar behavior to as if we've done all
238  * displacement in sculpt mode at the old top level and then propagated to the new top level.*/
239  multires_reshape_free_original_grids(&reshape_context);
240 
242  multires_reshape_smooth_object_grids(&reshape_context, mode);
243  }
244  else {
246  }
247 
249  multires_reshape_context_free(&reshape_context);
250 
251  multires_set_tot_level(object, mmd, top_level);
252 }
253 
256 /* -------------------------------------------------------------------- */
261  Object *object,
263 {
265 
266  MultiresReshapeContext reshape_context;
267  if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
268  return;
269  }
270 
271  multires_reshape_store_original_grids(&reshape_context);
272 
273  /* At this point base_mesh is object's mesh, the subdiv is initialized to the deformed state of
274  * the base mesh.
275  * Store coordinates of top level grids in object space which will define true shape we would
276  * want to reshape to after modifying the base mesh. */
278 
279  /* For modifying base mesh we only want to consider deformation caused by multires displacement
280  * and ignore all deformation which might be caused by deformation modifiers leading the multires
281  * one.
282  * So refine the subdiv to the original mesh vertices positions, which will also need to make
283  * it so object space displacement is re-evaluated for them (as in, can not re-use any knowledge
284  * from the final coordinates in the object space ). */
286 
287  /* Modify original mesh coordinates. This happens in two steps:
288  * - Coordinates are set to their final location, where they are intended to be in the final
289  * result.
290  * - Heuristic moves them a bit, kind of canceling out the effect of subsurf (so then when
291  * multires modifier applies subsurf vertices are placed at the desired location). */
294 
295  /* Reshape to the stored final state.
296  * Not that the base changed, so the subdiv is to be refined to the new positions. Unfortunately,
297  * this can not be done foe entirely cheap: if there were deformation modifiers prior to the
298  * multires they need to be re-evaluated for the new base mesh. */
301 
302  multires_reshape_context_free(&reshape_context);
303 }
304 
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.c:1919
void * CustomData_add_layer(struct CustomData *data, int type, eCDAllocType alloctype, void *layer, int totelem)
Definition: customdata.c:2620
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, 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:131
@ MOD_APPLY_IGNORE_SIMPLIFY
Definition: BKE_modifier.h:136
void multires_set_tot_level(struct Object *ob, struct MultiresModifierData *mmd, int lvl)
Definition: multires.c:384
void multires_force_sculpt_rebuild(struct Object *object)
Definition: multires.c:456
void multires_ensure_external_read(struct Mesh *mesh, int top_level)
Definition: multires.c:1518
eMultiresSubdivideModeType
Definition: BKE_multires.h:171
@ MULTIRES_SUBDIVIDE_LINEAR
Definition: BKE_multires.h:174
@ MULTIRES_SUBDIVIDE_SIMPLE
Definition: BKE_multires.h:173
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:427
struct Mesh * BKE_multires_create_mesh(struct Depsgraph *depsgraph, struct Object *object, struct MultiresModifierData *mmd)
Definition: multires.c:242
#define ELEM(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
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
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
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)
void multires_reshape_ensure_grids(struct Mesh *mesh, const int level)
void multires_reshape_smooth_object_grids(const MultiresReshapeContext *reshape_context, const enum eMultiresSubdivideModeType mode)
void multires_reshape_context_free(MultiresReshapeContext *reshape_context)
void multires_reshape_store_original_grids(MultiresReshapeContext *reshape_context)
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_object_grids_to_tangent_displacement(const MultiresReshapeContext *reshape_context)
bool multires_reshape_assign_final_coords_from_vertcos(const MultiresReshapeContext *reshape_context, const float(*vert_coords)[3], const int num_vert_coords)
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)
struct CustomData pdata ldata
int totvert
int totloop
struct Depsgraph * depsgraph
Definition: BKE_modifier.h:153
struct MultiresReshapeContext::@100 top
void * data