Refactor: Restructure mirror modifier sculpt data
Part of #126672. * Groups related mirror modifier data in an anonymous `mirror_mod_clip` `struct`. * Stores the inverse of the related matrix to avoid having to recalculate it multiple times per brush stroke. Pull Request: https://projects.blender.org/blender/blender/pulls/126717
This commit is contained in:
@@ -3997,9 +3997,9 @@ StrokeCache::~StrokeCache()
|
||||
namespace blender::ed::sculpt_paint {
|
||||
|
||||
/* Initialize mirror modifier clipping. */
|
||||
static void sculpt_init_mirror_clipping(Object &ob, SculptSession &ss)
|
||||
static void sculpt_init_mirror_clipping(const Object &ob, const SculptSession &ss)
|
||||
{
|
||||
ss.cache->clip_mirror_mtx = float4x4::identity();
|
||||
ss.cache->mirror_modifier_clip.mat = float4x4::identity();
|
||||
|
||||
LISTBASE_FOREACH (ModifierData *, md, &ob.modifiers) {
|
||||
if (!(md->type == eModifierType_Mirror && (md->mode & eModifierMode_Realtime))) {
|
||||
@@ -4016,21 +4016,22 @@ static void sculpt_init_mirror_clipping(Object &ob, SculptSession &ss)
|
||||
continue;
|
||||
}
|
||||
/* Enable sculpt clipping. */
|
||||
ss.cache->flag |= CLIP_X << i;
|
||||
ss.cache->mirror_modifier_clip.flag |= CLIP_X << i;
|
||||
|
||||
/* Update the clip tolerance. */
|
||||
if (mmd->tolerance > ss.cache->clip_tolerance[i]) {
|
||||
ss.cache->clip_tolerance[i] = mmd->tolerance;
|
||||
}
|
||||
ss.cache->mirror_modifier_clip.tolerance[i] = std::max(
|
||||
mmd->tolerance, ss.cache->mirror_modifier_clip.tolerance[i]);
|
||||
|
||||
/* Store matrix for mirror object clipping. */
|
||||
if (mmd->mirror_ob) {
|
||||
float imtx_mirror_ob[4][4];
|
||||
invert_m4_m4(imtx_mirror_ob, mmd->mirror_ob->object_to_world().ptr());
|
||||
mul_m4_m4m4(ss.cache->clip_mirror_mtx.ptr(), imtx_mirror_ob, ob.object_to_world().ptr());
|
||||
const float4x4 mirror_ob_inv = math::invert(mmd->mirror_ob->object_to_world());
|
||||
mul_m4_m4m4(ss.cache->mirror_modifier_clip.mat.ptr(),
|
||||
mirror_ob_inv.ptr(),
|
||||
ob.object_to_world().ptr());
|
||||
}
|
||||
}
|
||||
}
|
||||
ss.cache->mirror_modifier_clip.mat_inv = math::invert(ss.cache->mirror_modifier_clip.mat);
|
||||
}
|
||||
|
||||
static void smooth_brush_toggle_on(const bContext *C, Paint *paint, StrokeCache *cache)
|
||||
@@ -4132,7 +4133,7 @@ static void sculpt_update_cache_invariants(
|
||||
|
||||
cache->plane_trim_squared = brush->plane_trim * brush->plane_trim;
|
||||
|
||||
cache->flag = 0;
|
||||
cache->mirror_modifier_clip.flag = 0;
|
||||
|
||||
sculpt_init_mirror_clipping(ob, ss);
|
||||
|
||||
@@ -7062,18 +7063,18 @@ void clip_and_lock_translations(const Sculpt &sd,
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(cache->flag & (CLIP_X << axis))) {
|
||||
if (!(cache->mirror_modifier_clip.flag & (CLIP_X << axis))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const float4x4 mirror(cache->clip_mirror_mtx);
|
||||
const float4x4 mirror_inverse = math::invert(mirror);
|
||||
const float4x4 mirror(cache->mirror_modifier_clip.mat);
|
||||
const float4x4 mirror_inverse(cache->mirror_modifier_clip.mat_inv);
|
||||
for (const int i : verts.index_range()) {
|
||||
const int vert = verts[i];
|
||||
|
||||
/* Transform into the space of the mirror plane, check translations, then transform back. */
|
||||
float3 co_mirror = math::transform_point(mirror, positions[vert]);
|
||||
if (math::abs(co_mirror[axis]) > cache->clip_tolerance[axis]) {
|
||||
if (math::abs(co_mirror[axis]) > cache->mirror_modifier_clip.tolerance[axis]) {
|
||||
continue;
|
||||
}
|
||||
/* Clear the translation in the local space of the mirror object. */
|
||||
@@ -7103,16 +7104,16 @@ void clip_and_lock_translations(const Sculpt &sd,
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(cache->flag & (CLIP_X << axis))) {
|
||||
if (!(cache->mirror_modifier_clip.flag & (CLIP_X << axis))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const float4x4 mirror(cache->clip_mirror_mtx);
|
||||
const float4x4 mirror_inverse = math::invert(mirror);
|
||||
const float4x4 mirror(cache->mirror_modifier_clip.mat);
|
||||
const float4x4 mirror_inverse(cache->mirror_modifier_clip.mat_inv);
|
||||
for (const int i : positions.index_range()) {
|
||||
/* Transform into the space of the mirror plane, check translations, then transform back. */
|
||||
float3 co_mirror = math::transform_point(mirror, positions[i]);
|
||||
if (math::abs(co_mirror[axis]) > cache->clip_tolerance[axis]) {
|
||||
if (math::abs(co_mirror[axis]) > cache->mirror_modifier_clip.tolerance[axis]) {
|
||||
continue;
|
||||
}
|
||||
/* Clear the translation in the local space of the mirror object. */
|
||||
|
||||
@@ -137,9 +137,12 @@ struct StrokeCache {
|
||||
/* Invariants */
|
||||
float initial_radius;
|
||||
float3 scale;
|
||||
int flag;
|
||||
float3 clip_tolerance;
|
||||
float4x4 clip_mirror_mtx;
|
||||
struct {
|
||||
int flag = 0;
|
||||
float3 tolerance;
|
||||
float4x4 mat;
|
||||
float4x4 mat_inv;
|
||||
} mirror_modifier_clip;
|
||||
float2 initial_mouse;
|
||||
|
||||
/* Variants */
|
||||
|
||||
Reference in New Issue
Block a user