2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2020 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-03-03 12:35:51 +01:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_customdata.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2023-11-14 09:30:40 +01:00
|
|
|
#include "BKE_modifier.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_multires.hh"
|
2023-10-09 23:41:53 +02:00
|
|
|
#include "BKE_object.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_subsurf.hh"
|
2020-04-30 15:15:19 +02:00
|
|
|
#include "BLI_math_vector.h"
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph_query.hh"
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2023-02-03 08:48:00 -05:00
|
|
|
#include "multires_reshape.hh"
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Reshape from object
|
|
|
|
|
* \{ */
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2023-01-20 07:59:38 -06:00
|
|
|
bool multiresModifier_reshapeFromVertcos(Depsgraph *depsgraph,
|
|
|
|
|
Object *object,
|
|
|
|
|
MultiresModifierData *mmd,
|
2020-03-03 12:35:51 +01:00
|
|
|
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);
|
2023-01-20 07:59:38 -06:00
|
|
|
multires_reshape_ensure_grids(static_cast<Mesh *>(object->data), reshape_context.top.level);
|
2020-03-03 12:35:51 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 07:59:38 -06:00
|
|
|
bool multiresModifier_reshapeFromObject(Depsgraph *depsgraph,
|
|
|
|
|
MultiresModifierData *mmd,
|
|
|
|
|
Object *dst,
|
|
|
|
|
Object *src)
|
2020-03-03 12:35:51 +01:00
|
|
|
{
|
2023-05-30 22:25:06 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2023-06-09 18:38:35 +02:00
|
|
|
return multiresModifier_reshapeFromVertcos(
|
2023-06-14 11:59:32 -04:00
|
|
|
depsgraph,
|
|
|
|
|
dst,
|
|
|
|
|
mmd,
|
|
|
|
|
reinterpret_cast<const float(*)[3]>(src_mesh_eval->vert_positions().data()),
|
2023-12-20 02:21:48 +01:00
|
|
|
src_mesh_eval->verts_num);
|
2020-03-03 12:35:51 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Reshape from modifier
|
|
|
|
|
* \{ */
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2023-01-20 07:59:38 -06:00
|
|
|
bool multiresModifier_reshapeFromDeformModifier(Depsgraph *depsgraph,
|
|
|
|
|
Object *object,
|
|
|
|
|
MultiresModifierData *mmd,
|
|
|
|
|
ModifierData *deform_md)
|
2020-03-03 12:35:51 +01:00
|
|
|
{
|
2023-11-28 12:14:09 -05:00
|
|
|
using namespace blender;
|
2023-01-22 02:03:44 +01:00
|
|
|
MultiresModifierData highest_mmd = blender::dna::shallow_copy(*mmd);
|
2020-03-03 12:35:51 +01:00
|
|
|
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). */
|
2020-03-17 14:20:14 +01:00
|
|
|
Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
|
2023-11-28 12:14:09 -05:00
|
|
|
Array<float3> deformed_verts(multires_mesh->vert_positions());
|
2020-03-03 12:35:51 +01:00
|
|
|
|
|
|
|
|
/* Apply deformation modifier on the multires, */
|
2023-01-20 07:59:38 -06:00
|
|
|
ModifierEvalContext modifier_ctx{};
|
|
|
|
|
modifier_ctx.depsgraph = depsgraph;
|
|
|
|
|
modifier_ctx.object = object;
|
|
|
|
|
modifier_ctx.flag = MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY;
|
|
|
|
|
|
2023-11-28 12:14:09 -05:00
|
|
|
BKE_modifier_deform_verts(deform_md, &modifier_ctx, multires_mesh, deformed_verts);
|
2023-01-20 07:59:38 -06:00
|
|
|
BKE_id_free(nullptr, multires_mesh);
|
2020-03-03 12:35:51 +01:00
|
|
|
|
|
|
|
|
/* Reshaping */
|
|
|
|
|
bool result = multiresModifier_reshapeFromVertcos(
|
2023-11-28 12:14:09 -05:00
|
|
|
depsgraph,
|
|
|
|
|
object,
|
|
|
|
|
&highest_mmd,
|
|
|
|
|
reinterpret_cast<float(*)[3]>(deformed_verts.data()),
|
|
|
|
|
deformed_verts.size());
|
2020-03-03 12:35:51 +01:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Reshape from grids
|
|
|
|
|
* \{ */
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2023-01-20 07:59:38 -06:00
|
|
|
bool multiresModifier_reshapeFromCCG(const int tot_level, Mesh *coarse_mesh, SubdivCCG *subdiv_ccg)
|
2020-03-03 12:35:51 +01:00
|
|
|
{
|
|
|
|
|
MultiresReshapeContext reshape_context;
|
|
|
|
|
if (!multires_reshape_context_create_from_ccg(
|
|
|
|
|
&reshape_context, subdiv_ccg, coarse_mesh, tot_level))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-03-13 16:09:33 +01:00
|
|
|
|
2020-03-13 16:13:32 +01:00
|
|
|
multires_ensure_external_read(coarse_mesh, reshape_context.top.level);
|
2020-03-13 16:09:33 +01:00
|
|
|
|
2020-03-03 12:35:51 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Subdivision
|
|
|
|
|
* \{ */
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2020-04-30 15:15:19 +02:00
|
|
|
void multiresModifier_subdivide(Object *object,
|
|
|
|
|
MultiresModifierData *mmd,
|
|
|
|
|
const eMultiresSubdivideModeType mode)
|
2020-03-03 12:35:51 +01:00
|
|
|
{
|
|
|
|
|
const int top_level = mmd->totlvl + 1;
|
2020-04-30 15:15:19 +02:00
|
|
|
multiresModifier_subdivide_to_level(object, mmd, top_level, mode);
|
2020-03-03 12:35:51 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-20 07:59:38 -06:00
|
|
|
void multiresModifier_subdivide_to_level(Object *object,
|
|
|
|
|
MultiresModifierData *mmd,
|
2020-04-30 15:15:19 +02:00
|
|
|
const int top_level,
|
|
|
|
|
const eMultiresSubdivideModeType mode)
|
2020-03-03 12:35:51 +01:00
|
|
|
{
|
|
|
|
|
if (top_level <= mmd->totlvl) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 07:59:38 -06:00
|
|
|
Mesh *coarse_mesh = static_cast<Mesh *>(object->data);
|
2023-12-20 02:21:48 +01:00
|
|
|
if (coarse_mesh->corners_num == 0) {
|
2020-08-26 09:41:30 +10:00
|
|
|
/* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output
|
2020-08-24 18:26:28 +02:00
|
|
|
* from here as there is nothing to subdivide. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 12:35:51 +01:00
|
|
|
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. */
|
2023-12-19 20:38:59 -05:00
|
|
|
const bool has_mdisps = CustomData_has_layer(&coarse_mesh->corner_data, CD_MDISPS);
|
2020-03-03 12:35:51 +01:00
|
|
|
if (!has_mdisps) {
|
2023-12-20 02:21:48 +01:00
|
|
|
CustomData_add_layer(
|
2023-12-19 20:38:59 -05:00
|
|
|
&coarse_mesh->corner_data, CD_MDISPS, CD_SET_DEFAULT, coarse_mesh->corners_num);
|
2020-03-03 12:35:51 +01:00
|
|
|
}
|
2020-05-18 10:48:45 +02:00
|
|
|
|
|
|
|
|
/* 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) {
|
2020-03-03 12:35:51 +01:00
|
|
|
multires_reshape_ensure_grids(coarse_mesh, top_level);
|
2020-04-30 15:15:19 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2020-03-03 12:35:51 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
multires_flush_sculpt_updates(object);
|
|
|
|
|
|
2020-10-22 17:25:41 +02:00
|
|
|
if (!multires_reshape_context_create_from_modifier(&reshape_context, object, mmd, top_level)) {
|
2020-03-03 12:35:51 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2020-04-30 15:15:19 +02:00
|
|
|
|
2020-03-03 12:35:51 +01:00
|
|
|
multires_reshape_store_original_grids(&reshape_context);
|
|
|
|
|
multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
|
2020-05-13 10:42:13 +02:00
|
|
|
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
|
2021-06-26 21:35:18 +10:00
|
|
|
* displacement in sculpt mode at the old top level and then propagated to the new top level. */
|
2020-05-13 10:42:13 +02:00
|
|
|
multires_reshape_free_original_grids(&reshape_context);
|
2020-04-23 18:20:18 +02:00
|
|
|
|
2020-04-30 15:15:19 +02:00
|
|
|
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);
|
|
|
|
|
}
|
2020-05-13 10:42:13 +02:00
|
|
|
|
2020-03-03 12:35:51 +01:00
|
|
|
multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
|
|
|
|
|
multires_reshape_context_free(&reshape_context);
|
|
|
|
|
|
|
|
|
|
multires_set_tot_level(object, mmd, top_level);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Apply base
|
|
|
|
|
* \{ */
|
2020-03-03 12:35:51 +01:00
|
|
|
|
2023-01-20 07:59:38 -06:00
|
|
|
void multiresModifier_base_apply(Depsgraph *depsgraph, Object *object, MultiresModifierData *mmd)
|
2020-03-03 12:35:51 +01:00
|
|
|
{
|
|
|
|
|
multires_force_sculpt_rebuild(object);
|
|
|
|
|
|
|
|
|
|
MultiresReshapeContext reshape_context;
|
|
|
|
|
if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 16:08:31 +01:00
|
|
|
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. */
|
2020-03-03 12:35:51 +01:00
|
|
|
multires_reshape_assign_final_coords_from_mdisps(&reshape_context);
|
2020-03-17 16:08:31 +01:00
|
|
|
|
|
|
|
|
/* 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.
|
2020-03-19 12:05:34 +11:00
|
|
|
* So refine the subdiv to the original mesh vertices positions, which will also need to make
|
2020-03-17 16:08:31 +01:00
|
|
|
* 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). */
|
2020-03-03 12:35:51 +01:00
|
|
|
multires_reshape_apply_base_update_mesh_coords(&reshape_context);
|
|
|
|
|
multires_reshape_apply_base_refit_base_mesh(&reshape_context);
|
2020-03-17 16:08:31 +01:00
|
|
|
|
|
|
|
|
/* 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);
|
2020-03-03 12:35:51 +01:00
|
|
|
multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
|
|
|
|
|
|
|
|
|
|
multires_reshape_context_free(&reshape_context);
|
|
|
|
|
}
|
2020-04-29 11:51:21 +10:00
|
|
|
|
|
|
|
|
/** \} */
|