Avoid using ParentConstraints for bone proxy position tracking

This commit is contained in:
bd_ 2022-09-17 09:37:01 -07:00 committed by bd_
parent cba767b0fa
commit 5455e0da87
2 changed files with 32 additions and 87 deletions

View File

@ -29,24 +29,28 @@ using UnityEngine.Animations;
namespace net.fushizen.modular_avatar.core
{
[ExecuteInEditMode]
public class ModularAvatarBoneProxy : AvatarTagComponent
{
public Transform target;
public HumanBodyBones boneReference = HumanBodyBones.LastBone;
public string subPath;
[SerializeField] [HideInInspector] public ParentConstraint constraint;
#if UNITY_EDITOR
void OnValidate()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += CheckReferences;
#endif
}
void CheckReferences() {
void CheckReferences()
{
if (this == null) return; // post-destroy
if (target == null && (boneReference != HumanBodyBones.LastBone || !string.IsNullOrWhiteSpace(subPath)))
{
UpdateDynamicMapping();
@ -54,7 +58,8 @@ namespace net.fushizen.modular_avatar.core
{
RuntimeUtil.MarkDirty(this);
}
} else if (target != null)
}
else if (target != null)
{
var origBoneReference = boneReference;
var origSubpath = subPath;
@ -64,42 +69,37 @@ namespace net.fushizen.modular_avatar.core
RuntimeUtil.MarkDirty(this);
}
}
CheckConstraint();
}
private void CheckConstraint()
{
if (target != null)
if (constraint != null)
{
if (constraint == null)
{
constraint = gameObject.AddComponent<ParentConstraint>();
constraint.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
constraint.AddSource(new ConstraintSource()
{
weight = 1,
sourceTransform = target
});
constraint.translationOffsets = new Vector3[] {Vector3.zero};
constraint.rotationOffsets = new Vector3[] {Vector3.zero};
constraint.locked = true;
constraint.constraintActive = true;
}
else
{
constraint.SetSource(0, new ConstraintSource()
{
weight = 1,
sourceTransform = target
});
}
DestroyImmediate(constraint, true);
}
}
private void Update()
{
if (!RuntimeUtil.isPlaying && target != null)
{
var targetTransform = target.transform;
var myTransform = transform;
myTransform.position = targetTransform.position;
myTransform.rotation = targetTransform.rotation;
}
}
private void OnDestroy()
{
UnityEditor.EditorApplication.delayCall += () => { if (constraint != null) DestroyImmediate(constraint); };
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += () =>
{
if (constraint != null) DestroyImmediate(constraint);
};
#endif
}
private void UpdateDynamicMapping()
@ -171,6 +171,5 @@ namespace net.fushizen.modular_avatar.core
subPath = RuntimeUtil.RelativePath(iter.gameObject, target.gameObject);
}
#endif
}
}

View File

@ -4988,7 +4988,6 @@ GameObject:
m_Component:
- component: {fileID: 8484779175115796484}
- component: {fileID: 8484779175115796486}
- component: {fileID: 8484779175115796485}
m_Layer: 0
m_Name: Proxy_Hand_Left
m_TagString: Untagged
@ -5026,33 +5025,7 @@ MonoBehaviour:
target: {fileID: 0}
boneReference: 17
subPath:
constraint: {fileID: 8484779175115796485}
--- !u!1773428102 &8484779175115796485
ParentConstraint:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8484779175115796483}
m_Enabled: 1
m_Weight: 1
m_TranslationAtRest: {x: -0.45912412, y: 1.04774, z: -0.0050687576}
m_RotationAtRest: {x: 0, y: 0, z: 90.00001}
m_TranslationOffsets:
- {x: 0, y: 0, z: 0}
m_RotationOffsets:
- {x: 0, y: 0, z: 0}
m_AffectTranslationX: 1
m_AffectTranslationY: 1
m_AffectTranslationZ: 1
m_AffectRotationX: 1
m_AffectRotationY: 1
m_AffectRotationZ: 1
m_IsContraintActive: 1
m_IsLocked: 1
m_Sources:
- sourceTransform: {fileID: 0}
weight: 1
constraint: {fileID: 0}
--- !u!1 &8484779175182551488
GameObject:
m_ObjectHideFlags: 0
@ -10048,7 +10021,6 @@ GameObject:
m_Component:
- component: {fileID: 8484779175388350923}
- component: {fileID: 8484779175388350925}
- component: {fileID: 8484779175388350924}
m_Layer: 0
m_Name: Proxy_Hand_Right
m_TagString: Untagged
@ -10086,33 +10058,7 @@ MonoBehaviour:
target: {fileID: 0}
boneReference: 18
subPath:
constraint: {fileID: 8484779175388350924}
--- !u!1773428102 &8484779175388350924
ParentConstraint:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8484779175388350922}
m_Enabled: 1
m_Weight: 1
m_TranslationAtRest: {x: 0.45912412, y: 1.04774, z: -0.0050687576}
m_RotationAtRest: {x: 0, y: 0, z: -90.00001}
m_TranslationOffsets:
- {x: 0, y: 0, z: 0}
m_RotationOffsets:
- {x: 0, y: 0, z: 0}
m_AffectTranslationX: 1
m_AffectTranslationY: 1
m_AffectTranslationZ: 1
m_AffectRotationX: 1
m_AffectRotationY: 1
m_AffectRotationZ: 1
m_IsContraintActive: 1
m_IsLocked: 1
m_Sources:
- sourceTransform: {fileID: 0}
weight: 1
constraint: {fileID: 0}
--- !u!1 &8484779175587995429
GameObject:
m_ObjectHideFlags: 0