modular-avatar/Runtime/ArmatureAwase/TransformState.cs
bd_ ddbc3b164b
chore: restructure repository to put package at top-level (#477)
* chore: rearrange package structure to have the package at the root

* ci: update CI workflows

* ci: fixing workflow bugs

* ci: recurse building .zip package

* ci: more fixes

* ci: add back in the nadena.dev VPM repo

* ci: fix tests
2023-10-08 15:39:57 +09:00

50 lines
1.7 KiB
C#

using System.Runtime.CompilerServices;
using Unity.Burst;
using UnityEngine;
namespace nadena.dev.modular_avatar.core.armature_lock
{
internal struct TransformState
{
private const float POS_EPSILON = 0.00001f;
private const float ROT_EPSILON = 0.00001f;
private const float SCALE_EPSILON = 0.00001f;
public Vector3 localPosition;
public Quaternion localRotation;
public Vector3 localScale;
public static TransformState FromTransform(Transform mergeBone)
{
return new TransformState
{
localPosition = mergeBone.localPosition,
localRotation = mergeBone.localRotation,
localScale = mergeBone.localScale
};
}
public void ToTransform(Transform bone)
{
#if UNITY_EDITOR
UnityEditor.Undo.RecordObject(bone, UnityEditor.Undo.GetCurrentGroupName());
#endif
bone.localPosition = localPosition;
bone.localRotation = localRotation;
bone.localScale = localScale;
}
[BurstCompile]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Differs(TransformState self, TransformState other)
{
var deltaMergePos = (self.localPosition - other.localPosition).sqrMagnitude;
var deltaMergeRot = self.localRotation * Quaternion.Inverse(other.localRotation);
var deltaMergeScale = (self.localScale - other.localScale).sqrMagnitude;
return (deltaMergePos > POS_EPSILON
|| Quaternion.Angle(deltaMergeRot, Quaternion.identity) > ROT_EPSILON
|| deltaMergeScale > SCALE_EPSILON);
}
}
}