mirror of
https://github.com/bdunderscore/modular-avatar.git
synced 2025-04-11 23:19:00 +08:00
fix: Forget to keep the inverse root bone.
This commit is contained in:
parent
9f4a7a6304
commit
5e12d1d6f6
@ -1,4 +1,5 @@
|
|||||||
using System.Linq;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace nadena.dev.modular_avatar.core.editor
|
namespace nadena.dev.modular_avatar.core.editor
|
||||||
@ -144,9 +145,57 @@ namespace nadena.dev.modular_avatar.core.editor
|
|||||||
|
|
||||||
if (newMesh) context.SaveAsset(newMesh);
|
if (newMesh) context.SaveAsset(newMesh);
|
||||||
}
|
}
|
||||||
smr.rootBone = settings.RootBone;
|
|
||||||
smr.localBounds = settings.Bounds;
|
var settingsRootBone = settings.RootBone;
|
||||||
|
settingsRootBone = settingsRootBone == null ? smr.transform : settingsRootBone;
|
||||||
|
var smrRootBone = smr.rootBone;
|
||||||
|
smrRootBone = smrRootBone == null ? smr.transform : smrRootBone;
|
||||||
|
|
||||||
|
if (IsInverted(smrRootBone) != IsInverted(settingsRootBone))
|
||||||
|
{
|
||||||
|
smr.rootBone = GetInvertedRootBone(settingsRootBone);
|
||||||
|
|
||||||
|
var bounds = settings.Bounds;
|
||||||
|
var center = bounds.center;
|
||||||
|
center.x *= -1;
|
||||||
|
bounds.center = center;
|
||||||
|
smr.localBounds = bounds;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
smr.rootBone = settings.RootBone;
|
||||||
|
smr.localBounds = settings.Bounds;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool IsInverted(Transform bone)
|
||||||
|
{
|
||||||
|
var inverseCount = 0;
|
||||||
|
|
||||||
|
var scale = bone.lossyScale;
|
||||||
|
if (scale.x < 0) inverseCount += 1;
|
||||||
|
if (scale.y < 0) inverseCount += 1;
|
||||||
|
if (scale.z < 0) inverseCount += 1;
|
||||||
|
|
||||||
|
return (inverseCount % 2) != 0;
|
||||||
|
}
|
||||||
|
private Dictionary<Transform, Transform> invertedRootBoneCache = new();
|
||||||
|
private Transform GetInvertedRootBone(Transform rootBone)
|
||||||
|
{
|
||||||
|
if (invertedRootBoneCache.TryGetValue(rootBone, out var cache)) { return cache; }
|
||||||
|
|
||||||
|
var cloned = Object.Instantiate(rootBone.gameObject);
|
||||||
|
cloned.name = rootBone.gameObject.name + "-InvertedRootBone";
|
||||||
|
cloned.transform.SetParent(rootBone, false);
|
||||||
|
|
||||||
|
var invertedRootBone = cloned.transform;
|
||||||
|
var scale = invertedRootBone.localScale;
|
||||||
|
scale.x *= -1;
|
||||||
|
invertedRootBone.localScale = scale;
|
||||||
|
|
||||||
|
invertedRootBoneCache[rootBone] = invertedRootBone;
|
||||||
|
return invertedRootBone;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user