Refactor: Restructure sculpt shape key updates

Pull Request: https://projects.blender.org/blender/blender/pulls/126161
This commit is contained in:
Sean Kim
2024-08-12 18:25:45 +02:00
committed by Sean Kim
parent fd7c884aef
commit cab4d6f724
2 changed files with 26 additions and 24 deletions

View File

@@ -383,10 +383,12 @@ void clip_and_lock_translations(const Sculpt &sd,
* shape key positions must be kept in sync, and shape keys dependent on the active key must also
* be modified.
*/
void apply_translations_to_shape_keys(Object &object,
Span<int> verts,
Span<float3> translations,
MutableSpan<float3> positions_mesh);
void update_shape_keys(Object &object,
const Mesh &mesh,
const KeyBlock &active_key,
Span<int> verts,
Span<float3> translations,
Span<float3> positions_orig);
/**
* Currently the pbvh::Tree owns its own copy of deformed positions that needs to be updated to

View File

@@ -1493,9 +1493,9 @@ void restore_position_from_undo_step(Object &object)
}
}
if (BKE_keyblock_from_object(&object)) {
/* Update dependent shape keys back to their original */
apply_translations_to_shape_keys(object, verts, tls.translations, positions_orig);
if (const KeyBlock *active_key = BKE_keyblock_from_object(&object)) {
update_shape_keys(
object, mesh, *active_key, verts, tls.translations, positions_orig);
}
BKE_pbvh_node_mark_positions_update(node);
@@ -7246,19 +7246,15 @@ void clip_and_lock_translations(const Sculpt &sd,
}
}
void apply_translations_to_shape_keys(Object &object,
const Span<int> verts,
const Span<float3> translations,
const MutableSpan<float3> positions_orig)
void update_shape_keys(Object &object,
const Mesh &mesh,
const KeyBlock &active_key,
const Span<int> verts,
const Span<float3> translations,
const Span<float3> positions_orig)
{
Mesh &mesh = *static_cast<Mesh *>(object.data);
KeyBlock *active_key = BKE_keyblock_from_object(&object);
if (!active_key) {
return;
}
MutableSpan active_key_data(static_cast<float3 *>(active_key->data), active_key->totelem);
if (active_key == mesh.key->refkey) {
const MutableSpan active_key_data(static_cast<float3 *>(active_key.data), active_key.totelem);
if (&active_key == mesh.key->refkey) {
for (const int vert : verts) {
active_key_data[vert] = positions_orig[vert];
}
@@ -7267,11 +7263,10 @@ void apply_translations_to_shape_keys(Object &object,
apply_translations(translations, verts, active_key_data);
}
/* For relative keys editing of base should update other keys. */
if (bool *dependent = BKE_keyblock_get_dependent_keys(mesh.key, object.shapenr - 1)) {
int i;
LISTBASE_FOREACH_INDEX (KeyBlock *, other_key, &mesh.key->block, i) {
if ((other_key != active_key) && dependent[i]) {
if ((other_key != &active_key) && dependent[i]) {
MutableSpan<float3> data(static_cast<float3 *>(other_key->data), other_key->totelem);
apply_translations(translations, verts, data);
}
@@ -7313,12 +7308,17 @@ void write_translations(const Sculpt &sd,
const Mesh &mesh = *static_cast<Mesh *>(object.data);
const KeyBlock *active_key = BKE_keyblock_from_object(&object);
const bool relative_shapekey_active = active_key != nullptr && active_key != mesh.key->refkey;
if (!relative_shapekey_active) {
if (!active_key) {
apply_translations(translations, verts, positions_orig);
return;
}
const bool basis_shapekey_active = active_key == mesh.key->refkey;
if (basis_shapekey_active) {
apply_translations(translations, verts, positions_orig);
}
apply_translations_to_shape_keys(object, verts, translations, positions_orig);
update_shape_keys(object, mesh, *active_key, verts, translations, positions_orig);
}
void scale_translations(const MutableSpan<float3> translations, const Span<float> factors)