From 673aac40e3558cd9e5f9657ea54dc7e8b96fbe26 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 13 May 2021 17:44:35 +0900 Subject: [PATCH] DrawAimUp --- .../Constraint/VRM10AimConstraintEditor.cs | 109 ++++++------------ .../Constraint/VRM10AimConstraint.cs | 20 +++- 2 files changed, 51 insertions(+), 78 deletions(-) diff --git a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs index fb9cee2f2..1c7f4fd2f 100644 --- a/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs +++ b/Assets/VRM10/Editor/Components/Constraint/VRM10AimConstraintEditor.cs @@ -27,6 +27,19 @@ namespace UniVRM10 } } + static void DrawAimUp(Quaternion rot, Vector3 pos, Color c) + { + Handles.color = c; + // aim + var aim = pos + rot * Vector3.forward * 0.3f; + Handles.DrawLine(pos, aim); + Handles.Label(aim, "aim"); + // up + var up = pos + rot * Vector3.up * 0.3f; + Handles.DrawLine(pos, up); + Handles.Label(up, "up"); + } + public void OnSceneGUI() { if (m_target.Source == null) @@ -38,84 +51,36 @@ namespace UniVRM10 Handles.color = Color.yellow; Handles.DrawLine(m_target.Source.position, m_target.transform.position); - TR.FromWorld(m_target.transform).Draw(0.2f); + var pos = m_target.transform.position; - Handles.matrix = Matrix4x4.identity; EditorGUI.BeginChangeCheck(); var pr = m_target.ParentRotation; - var pos = m_target.transform.position; - var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos); - if (EditorGUI.EndChangeCheck()) + + if (m_target.Logic == null) { - Undo.RecordObject(m_target, "Rotated RotateAt Point"); - m_target.DestinationOffset = Quaternion.Inverse(pr) * rot; + 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; + } + + 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); - // aim - Handles.color = Color.yellow; - var aim = pos + rot * Vector3.forward * 0.3f; - Handles.DrawLine(pos, aim); - Handles.Label(aim, "aim"); - // up - var up = pos + rot * Vector3.up * 0.3f; - Handles.DrawLine(pos, up); - Handles.Label(up, "up"); - - // var delta = Clamp180(m_target.Delta.eulerAngles); - - // // show source - // { - // var sb = new StringBuilder(); - // sb.AppendLine(); - // sb.AppendLine(); - // sb.AppendLine($"source: {m_target.SourceCoordinate}"); - // sb.AppendLine($"{delta.x:0.}"); - // sb.AppendLine($"{delta.y:0.}"); - // sb.Append($"{delta.z:0.}"); - // Handles.Label(m_target.Source.position, sb.ToString(), Style); - // } - - // // show dst - // { - // var sb = new StringBuilder(); - // sb.AppendLine($"constraint: {m_target.DestinationCoordinate}"); - // sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.}"); - // sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.}"); - // sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z: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); + } } - // void OnDrawGizmos() - // { - // if (Source == null) - // { - // return; - // } - - // var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position); - // var (yaw, yaw_, pitch, pitch_) = CalcYawPitch(m_coords, localPosition); - // switch (yaw_) - // { - // case 0: break; - // case 1: yaw = 180 - yaw; break; - // case 2: yaw = 180 + yaw; break; - // case 3: yaw = 360 - yaw; break; - // } - // switch (pitch_) - // { - // case 0: pitch = -pitch; break; - // case 1: pitch = -pitch; break; - // case 2: break; - // case 3: break; - // } - // // Debug.Log($"{yaw}({yaw_}), {pitch}({pitch_})"); - // // var rot = Quaternion.Euler(pitch, yaw, 0); - // var rot = Quaternion.AngleAxis(yaw, Vector3.up) * Quaternion.AngleAxis(pitch, Vector3.right); - // var p = rot * Vector3.forward; - - // Gizmos.matrix = transform.localToWorldMatrix; - // Gizmos.DrawLine(Vector3.zero, p * 5); - // } - } } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index 489a3ecf3..3dc9f53d2 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -33,7 +33,8 @@ namespace UniVRM10 InitialLocalRotation = initialLocalRotation; } } - AimLogic m_runtime; + public AimLogic Logic { get; private set; } + void Start() { @@ -43,9 +44,9 @@ namespace UniVRM10 return; } - if (m_runtime == null) + if (Logic == null) { - m_runtime = new AimLogic(DestinationOffset); + Logic = new AimLogic(transform.localRotation); } } @@ -120,13 +121,20 @@ namespace UniVRM10 /// public override void Process() { - if (Source == null) + if (Logic == null) { return; } - // var localPosition = transform.worldToLocalMatrix.MultiplyPoint(Source.position); - // var (yaw, pitch) = CalcYawPitch(m_coords, localPosition); + var m = Matrix4x4.TRS(transform.position, ParentRotation * Logic.InitialLocalRotation, Vector3.one); + m.CalcYawPitch(Source.position, out Yaw, out Pitch); + // Delta = Quaternion.Euler(0, Yaw, 0) * Quaternion.Euler(Pitch, 0, 0); + Delta = m.YawPitchRotation(Yaw, Pitch); + transform.rotation = ParentRotation * Logic.InitialLocalRotation * Delta; } + + public float Yaw; + public float Pitch; + public Quaternion Delta; } }