modular-avatar/Packages/net.fushizen.modular-avatar/Runtime/MAInternalOffsetMarker.cs
bd_ b154b01e45 Support editing original bone animations in modular components
This adds support for animation mapping for MergeArmature components, as well as supporting locking the position of
MergeArmature-controlled transforms to their original counterparts, to ease animation editing.

With this change it might be best to eliminate the bone proxy component, as it's a bit redundant now.
2022-08-29 15:13:26 -07:00

58 lines
2.0 KiB
C#

using Codice.CM.WorkspaceServer.Tree;
using UnityEngine;
namespace net.fushizen.modular_avatar.core
{
[ExecuteInEditMode]
public class MAInternalOffsetMarker : AvatarTagComponent
{
void OnValidate()
{
#if MODULAR_AVATAR_DEBUG
hideFlags = HideFlags.None;
#else
hideFlags = HideFlags.HideInInspector;
#endif
}
private const float POS_EPSILON = 0.01f;
private const float ROT_EPSILON = 0.01f;
private Vector3 lastLocalPos;
private Vector3 lastLocalScale;
private Quaternion lastLocalRot;
public Transform correspondingObject;
public bool lockBasePosition;
public void Update()
{
if (correspondingObject == null) return;
// ReSharper disable once LocalVariableHidesMember
var transform = this.transform;
if ((transform.localPosition - lastLocalPos).sqrMagnitude > POS_EPSILON
|| (transform.localScale - lastLocalScale).sqrMagnitude > POS_EPSILON
|| Quaternion.Angle(lastLocalRot, transform.localRotation) > ROT_EPSILON)
{
if (lockBasePosition) transform.position = correspondingObject.position;
else correspondingObject.localPosition = transform.localPosition;
correspondingObject.localScale = transform.localScale;
correspondingObject.localRotation = transform.localRotation;
}
else
{
if (lockBasePosition) transform.position = correspondingObject.position;
else transform.localPosition = correspondingObject.localPosition;
transform.localScale = correspondingObject.localScale;
transform.localRotation = correspondingObject.localRotation;
}
lastLocalPos = transform.localPosition;
lastLocalScale = transform.localScale;
lastLocalRot = transform.localRotation;
}
}
}