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

View File

@ -4988,7 +4988,6 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 8484779175115796484} - component: {fileID: 8484779175115796484}
- component: {fileID: 8484779175115796486} - component: {fileID: 8484779175115796486}
- component: {fileID: 8484779175115796485}
m_Layer: 0 m_Layer: 0
m_Name: Proxy_Hand_Left m_Name: Proxy_Hand_Left
m_TagString: Untagged m_TagString: Untagged
@ -5026,33 +5025,7 @@ MonoBehaviour:
target: {fileID: 0} target: {fileID: 0}
boneReference: 17 boneReference: 17
subPath: subPath:
constraint: {fileID: 8484779175115796485} constraint: {fileID: 0}
--- !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
--- !u!1 &8484779175182551488 --- !u!1 &8484779175182551488
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -10048,7 +10021,6 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 8484779175388350923} - component: {fileID: 8484779175388350923}
- component: {fileID: 8484779175388350925} - component: {fileID: 8484779175388350925}
- component: {fileID: 8484779175388350924}
m_Layer: 0 m_Layer: 0
m_Name: Proxy_Hand_Right m_Name: Proxy_Hand_Right
m_TagString: Untagged m_TagString: Untagged
@ -10086,33 +10058,7 @@ MonoBehaviour:
target: {fileID: 0} target: {fileID: 0}
boneReference: 18 boneReference: 18
subPath: subPath:
constraint: {fileID: 8484779175388350924} constraint: {fileID: 0}
--- !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
--- !u!1 &8484779175587995429 --- !u!1 &8484779175587995429
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0