mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-14 06:50:19 -05:00
aim constraint Yaw, Pitch freeze
This commit is contained in:
@@ -14,6 +14,13 @@ namespace UniVRM10
|
||||
Z = 4,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum YawPitchMask
|
||||
{
|
||||
Yaw = 1,
|
||||
Pitch = 2,
|
||||
}
|
||||
|
||||
public static class AxesMaskExtensions
|
||||
{
|
||||
public static Vector3 Freeze(this AxisMask mask, Vector3 src)
|
||||
|
||||
@@ -8,6 +8,18 @@ namespace UniVRM10
|
||||
[DisallowMultipleComponent]
|
||||
public class VRM10AimConstraint : VRM10Constraint
|
||||
{
|
||||
/// <summary>
|
||||
/// Yaw(Y), Pitch(X) の2軸だけ
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
[EnumFlags]
|
||||
YawPitchMask m_freezeAxes = default;
|
||||
public YawPitchMask FreezeAxes
|
||||
{
|
||||
get => m_freezeAxes;
|
||||
set => m_freezeAxes = value;
|
||||
}
|
||||
|
||||
[Header("Source")]
|
||||
[SerializeField]
|
||||
Transform m_source = default;
|
||||
@@ -21,6 +33,10 @@ namespace UniVRM10
|
||||
[SerializeField]
|
||||
ObjectSpace m_destinationCoordinate = default;
|
||||
|
||||
/// <summary>
|
||||
/// シリアライズは、Aim と Up で記録。
|
||||
/// UniVRM の Editor では Aim と Up が直交しないことを許可しない。
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
public Quaternion DestinationOffset = Quaternion.identity;
|
||||
|
||||
@@ -59,6 +75,13 @@ namespace UniVRM10
|
||||
var m = new Matrix4x4(xAxis, yAxis, zAxis, new Vector4(0, 0, 0, 1));
|
||||
var parent = TR.FromParent(transform);
|
||||
m_delta = Quaternion.Inverse(parent.Rotation * m_src.LocalInitial.Rotation * DestinationOffset) * m.rotation;
|
||||
|
||||
var (yaw, pitch) = Matrix4x4.Rotate(Quaternion.Inverse(m_delta)).CalcYawPitch(Vector3.forward);
|
||||
if (m_freezeAxes.HasFlag(YawPitchMask.Yaw)) yaw = 0;
|
||||
if (m_freezeAxes.HasFlag(YawPitchMask.Pitch)) pitch = 0;
|
||||
|
||||
m_delta = Quaternion.Euler(pitch, yaw, 0);
|
||||
|
||||
transform.rotation = parent.Rotation * m_src.LocalInitial.Rotation * Delta;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user