From 12e0cc0406026dd3474c55602a8ac3d3cbd93a59 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 14 May 2021 14:54:03 +0900 Subject: [PATCH] WIP aim --- .../Constraint/VRM10AimConstraintEditor.cs | 51 ++++++++++--------- .../Constraint/VRM10AimConstraint.cs | 46 ++++++++--------- .../Components/LookAt/Matrix4x4Extensions.cs | 24 +++++++++ 3 files changed, 73 insertions(+), 48 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs index 1521bda5d..cc162f34d 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs @@ -29,6 +29,7 @@ namespace UniVRM10 static void DrawAimUp(Quaternion rot, Vector3 pos, Color c) { + Handles.matrix = Matrix4x4.identity; Handles.color = c; // aim var aim = pos + rot * Vector3.forward * 0.3f; @@ -52,36 +53,36 @@ namespace UniVRM10 Handles.DrawLine(m_target.Source.position, m_target.transform.position); var pos = m_target.transform.position; - TR.FromWorld(m_target.transform).Draw(0.2f); - // EditorGUI.BeginChangeCheck(); - // var pr = m_target.ParentRotation; + var pr = m_target.ParentRotation; - // if (m_target.Logic == null) - // { - // TR.FromWorld(m_target.transform).Draw(0.2f); + if (m_target.Logic == null) + { + EditorGUI.BeginChangeCheck(); + TR.FromWorld(m_target.transform).Draw(0.2f); - // Handles.matrix = Matrix4x4.identity; - // var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); - // if (EditorGUI.EndChangeCheck()) - // { - // Undo.RecordObject(m_target, "Rotated RotateAt Point"); - // m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; - // } + Handles.matrix = Matrix4x4.identity; + var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(m_target, "Rotated RotateAt Point"); + m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; + } - // DrawAimUp(rot, pos, Color.yellow); - // } - // else - // { - // var rot = m_target.ParentRotation * m_target.Logic.InitialLocalRotation * m_target.DestinationOffset; - // DrawAimUp(rot, m_target.transform.position, Color.yellow); - // DrawAimUp(m_target.transform.rotation * m_target.Delta, m_target.transform.position, Color.magenta); + DrawAimUp(rot, pos, Color.yellow); + } + else + { + var init = m_target.ParentRotation * m_target.Logic.InitialLocalRotation; + DrawAimUp(init * m_target.DestinationOffset, m_target.transform.position, Color.yellow); + new TR(init, m_target.transform.position).Draw(0.2f); + DrawAimUp(m_target.transform.rotation, m_target.transform.position, Color.magenta); - // var sb = new StringBuilder(); - // sb.AppendLine($"yaw: {m_target.Yaw:0.}"); - // sb.AppendLine($"pitch: {m_target.Pitch:0.}"); - // Handles.Label(m_target.transform.position, sb.ToString(), Style); - // } + var sb = new StringBuilder(); + sb.AppendLine($"yaw: {m_target.Yaw:0.}"); + sb.AppendLine($"pitch: {m_target.Pitch:0.}"); + Handles.Label(m_target.transform.position, sb.ToString(), Style); + } } } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index c9d4feee8..c70beaf15 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -22,19 +22,18 @@ namespace UniVRM10 public Quaternion ParentRotation => transform.parent == null ? Quaternion.identity : transform.parent.rotation; - // [SerializeField] - // public Quaternion DestinationOffset = Quaternion.identity; - - // public class AimLogic - // { - // public readonly Quaternion InitialLocalRotation; - // public AimLogic(Quaternion initialLocalRotation) - // { - // InitialLocalRotation = initialLocalRotation; - // } - // } - // public AimLogic Logic { get; private set; } + [SerializeField] + public Quaternion DestinationOffset = Quaternion.identity; + public class AimLogic + { + public readonly Quaternion InitialLocalRotation; + public AimLogic(Quaternion initialLocalRotation) + { + InitialLocalRotation = initialLocalRotation; + } + } + public AimLogic Logic { get; private set; } void Start() { @@ -44,10 +43,10 @@ namespace UniVRM10 return; } - // if (Logic == null) - // { - // Logic = new AimLogic(transform.localRotation); - // } + if (Logic == null) + { + Logic = new AimLogic(transform.localRotation); + } } /// @@ -56,14 +55,15 @@ namespace UniVRM10 /// public override void Process() { - // if (Logic == null) - // { - // return; - // } + if (Logic == null) + { + return; + } - var m = Matrix4x4.Rotate(ParentRotation); - m.CalcYawPitch(Source.position - transform.position, out Yaw, out Pitch); - Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(-Pitch, 0, 0); + // var init = ParentRotation * Logic.InitialLocalRotation; + var m = Matrix4x4.TRS(transform.position, ParentRotation * DestinationOffset, Vector3.one); + (Yaw, Pitch) = m.CalcYawPitch(Source.position); + Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0); transform.rotation = ParentRotation * Delta; } diff --git a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs index 0cd2da454..bf1806ca7 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs @@ -6,6 +6,30 @@ namespace UniVRM10 { public static class Matrix4x4Extensions { + /// + /// from AimConstraint + /// + /// + /// + /// + /// + public static (float Yaw, float Pitch) CalcYawPitch(this Matrix4x4 m, Vector3 target) + { + var p = m.GetColumn(3); + target -= new Vector3(p.x, p.y, p.z); + var zaxis = Vector3.Project(target, m.GetColumn(2)); + var yaxis = Vector3.Project(target, m.GetColumn(1)); + var xaxis = Vector3.Project(target, m.GetColumn(0)); + + var yawPlusMinus = Vector3.Dot(xaxis, m.GetColumn(0)) > 0 ? 1.0f : -1.0f; + var yaw = (float)Math.Atan2(xaxis.magnitude, zaxis.magnitude) * yawPlusMinus * Mathf.Rad2Deg; + + var pitchPlusMinus = Vector3.Dot(yaxis, m.GetColumn(1)) < 0 ? 1.0f : -1.0f; + var pitch = (float)Math.Atan2(yaxis.magnitude, (xaxis + zaxis).magnitude) * pitchPlusMinus * Mathf.Rad2Deg; + + return (yaw, pitch); + } + public static void CalcYawPitch(this Matrix4x4 m, Vector3 target, out float yaw, out float pitch) { var zaxis = Vector3.Project(target, m.GetColumn(2));