/* SPDX-FileCopyrightText: 2020 Blender Authors * * SPDX-License-Identifier: GPL-2.0-or-later */ /** \file * \ingroup bke */ #include "MEM_guardedalloc.h" #include "DNA_mesh_types.h" #include "DNA_modifier_types.h" #include "BKE_customdata.hh" #include "BKE_lib_id.hh" #include "BKE_modifier.hh" #include "BKE_multires.hh" #include "BKE_object.hh" #include "BKE_subsurf.hh" #include "BLI_math_vector.h" #include "DEG_depsgraph_query.hh" #include "multires_reshape.hh" /* -------------------------------------------------------------------- */ /** \name Reshape from object * \{ */ bool multiresModifier_reshapeFromVertcos(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, const float (*vert_coords)[3], const int num_vert_coords) { MultiresReshapeContext reshape_context; if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) { return false; } multires_reshape_store_original_grids(&reshape_context); multires_reshape_ensure_grids(static_cast(object->data), reshape_context.top.level); if (!multires_reshape_assign_final_coords_from_vertcos( &reshape_context, vert_coords, num_vert_coords)) { multires_reshape_context_free(&reshape_context); return false; } multires_reshape_smooth_object_grids_with_details(&reshape_context); multires_reshape_object_grids_to_tangent_displacement(&reshape_context); multires_reshape_context_free(&reshape_context); return true; } bool multiresModifier_reshapeFromObject(Depsgraph *depsgraph, MultiresModifierData *mmd, Object *dst, Object *src) { const Object *ob_eval = DEG_get_evaluated_object(depsgraph, src); if (!ob_eval) { return false; } const Mesh *src_mesh_eval = BKE_object_get_evaluated_mesh(ob_eval); if (!src_mesh_eval) { return false; } return multiresModifier_reshapeFromVertcos( depsgraph, dst, mmd, reinterpret_cast(src_mesh_eval->vert_positions().data()), src_mesh_eval->verts_num); } /** \} */ /* -------------------------------------------------------------------- */ /** \name Reshape from modifier * \{ */ bool multiresModifier_reshapeFromDeformModifier(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd, ModifierData *deform_md) { using namespace blender; MultiresModifierData highest_mmd = blender::dna::shallow_copy(*mmd); highest_mmd.sculptlvl = highest_mmd.totlvl; highest_mmd.lvl = highest_mmd.totlvl; highest_mmd.renderlvl = highest_mmd.totlvl; /* Create mesh for the multires, ignoring any further modifiers (leading * deformation modifiers will be applied though). */ Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd); Array deformed_verts(multires_mesh->vert_positions()); /* Apply deformation modifier on the multires, */ ModifierEvalContext modifier_ctx{}; modifier_ctx.depsgraph = depsgraph; modifier_ctx.object = object; modifier_ctx.flag = MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY; BKE_modifier_deform_verts(deform_md, &modifier_ctx, multires_mesh, deformed_verts); BKE_id_free(nullptr, multires_mesh); /* Reshaping */ bool result = multiresModifier_reshapeFromVertcos( depsgraph, object, &highest_mmd, reinterpret_cast(deformed_verts.data()), deformed_verts.size()); return result; } /** \} */ /* -------------------------------------------------------------------- */ /** \name Reshape from grids * \{ */ bool multiresModifier_reshapeFromCCG(const int tot_level, Mesh *coarse_mesh, SubdivCCG *subdiv_ccg) { MultiresReshapeContext reshape_context; if (!multires_reshape_context_create_from_ccg( &reshape_context, subdiv_ccg, coarse_mesh, tot_level)) { return false; } multires_ensure_external_read(coarse_mesh, reshape_context.top.level); multires_reshape_store_original_grids(&reshape_context); multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level); if (!multires_reshape_assign_final_coords_from_ccg(&reshape_context, subdiv_ccg)) { multires_reshape_context_free(&reshape_context); return false; } multires_reshape_smooth_object_grids_with_details(&reshape_context); multires_reshape_object_grids_to_tangent_displacement(&reshape_context); multires_reshape_context_free(&reshape_context); return true; } /** \} */ /* -------------------------------------------------------------------- */ /** \name Subdivision * \{ */ void multiresModifier_subdivide(Object *object, MultiresModifierData *mmd, const eMultiresSubdivideModeType mode) { const int top_level = mmd->totlvl + 1; multiresModifier_subdivide_to_level(object, mmd, top_level, mode); } void multiresModifier_subdivide_to_level(Object *object, MultiresModifierData *mmd, const int top_level, const eMultiresSubdivideModeType mode) { if (top_level <= mmd->totlvl) { return; } Mesh *coarse_mesh = static_cast(object->data); if (coarse_mesh->corners_num == 0) { /* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output * from here as there is nothing to subdivide. */ return; } MultiresReshapeContext reshape_context; /* There was no multires at all, all displacement is at 0. Can simply make sure all mdisps grids * are allocated at a proper level and return. */ const bool has_mdisps = CustomData_has_layer(&coarse_mesh->corner_data, CD_MDISPS); if (!has_mdisps) { CustomData_add_layer( &coarse_mesh->corner_data, CD_MDISPS, CD_SET_DEFAULT, coarse_mesh->corners_num); } /* NOTE: Subdivision happens from the top level of the existing multires modifier. If it is set * to 0 and there is mdisps layer it would mean that the modifier went out of sync with the data. * This happens when, for example, linking modifiers from one object to another. * * In such cases simply ensure grids to be the proper level. * * If something smarter is needed it is up to the operators which does data synchronization, so * that the mdisps layer is also synchronized. */ if (!has_mdisps || top_level == 1 || mmd->totlvl == 0) { multires_reshape_ensure_grids(coarse_mesh, top_level); if (ELEM(mode, MULTIRES_SUBDIVIDE_LINEAR, MULTIRES_SUBDIVIDE_SIMPLE)) { multires_subdivide_create_tangent_displacement_linear_grids(object, mmd); } else { multires_set_tot_level(object, mmd, top_level); } return; } multires_flush_sculpt_updates(object); if (!multires_reshape_context_create_from_modifier(&reshape_context, object, mmd, top_level)) { return; } multires_reshape_store_original_grids(&reshape_context); multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level); multires_reshape_assign_final_elements_from_orig_mdisps(&reshape_context); /* Free original grids which makes it so smoothing with details thinks all the details were * added against base mesh's limit surface. This is similar behavior to as if we've done all * displacement in sculpt mode at the old top level and then propagated to the new top level. */ multires_reshape_free_original_grids(&reshape_context); if (ELEM(mode, MULTIRES_SUBDIVIDE_LINEAR, MULTIRES_SUBDIVIDE_SIMPLE)) { multires_reshape_smooth_object_grids(&reshape_context, mode); } else { multires_reshape_smooth_object_grids_with_details(&reshape_context); } multires_reshape_object_grids_to_tangent_displacement(&reshape_context); multires_reshape_context_free(&reshape_context); multires_set_tot_level(object, mmd, top_level); } /** \} */ /* -------------------------------------------------------------------- */ /** \name Apply base * \{ */ void multiresModifier_base_apply(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd) { multires_force_sculpt_rebuild(object); MultiresReshapeContext reshape_context; if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) { return; } multires_reshape_store_original_grids(&reshape_context); /* At this point base_mesh is object's mesh, the subdiv is initialized to the deformed state of * the base mesh. * Store coordinates of top level grids in object space which will define true shape we would * want to reshape to after modifying the base mesh. */ multires_reshape_assign_final_coords_from_mdisps(&reshape_context); /* For modifying base mesh we only want to consider deformation caused by multires displacement * and ignore all deformation which might be caused by deformation modifiers leading the multires * one. * So refine the subdiv to the original mesh vertices positions, which will also need to make * it so object space displacement is re-evaluated for them (as in, can not re-use any knowledge * from the final coordinates in the object space ). */ multires_reshape_apply_base_refine_from_base(&reshape_context); /* Modify original mesh coordinates. This happens in two steps: * - Coordinates are set to their final location, where they are intended to be in the final * result. * - Heuristic moves them a bit, kind of canceling out the effect of subsurf (so then when * multires modifier applies subsurf vertices are placed at the desired location). */ multires_reshape_apply_base_update_mesh_coords(&reshape_context); multires_reshape_apply_base_refit_base_mesh(&reshape_context); /* Reshape to the stored final state. * Not that the base changed, so the subdiv is to be refined to the new positions. Unfortunately, * this can not be done foe entirely cheap: if there were deformation modifiers prior to the * multires they need to be re-evaluated for the new base mesh. */ multires_reshape_apply_base_refine_from_deform(&reshape_context); multires_reshape_object_grids_to_tangent_displacement(&reshape_context); multires_reshape_context_free(&reshape_context); } /** \} */