From 3ab946f8ac76ba7ed4600c696ccaf11ed182a6b4 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 2 Aug 2022 19:58:27 +0900 Subject: [PATCH 1/4] =?UTF-8?q?euler=20=E8=A7=92=E3=81=AE=E6=AD=A3?= =?UTF-8?q?=E3=81=AE=E6=96=B9=E5=90=91=E3=80=82=E3=83=AD=E3=82=B8=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=82=92=E3=83=89=E3=82=AD=E3=83=A5=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=81=AB=E5=90=88=E3=82=8F=E3=81=9B=E3=82=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/EditorTool/VRM10LookAtEditorTool.cs | 6 +++++- .../Components/LookAt/Matrix4x4Extensions.cs | 15 ++++++++------- .../Components/Vrm10Instance/Vrm10Instance.cs | 4 ++++ .../Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs | 15 +++++++++------ 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs index 596bbb4f5..b7917fee5 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs @@ -46,6 +46,10 @@ namespace UniVRM10 public override void OnToolGUI(EditorWindow window) { + if(Selection.activeTransform==null) + { + return; + } var root = Selection.activeTransform.GetComponent(); if (root == null) { @@ -165,7 +169,7 @@ namespace UniVRM10 Handles.DrawSolidArc(Vector3.zero, Matrix4x4.identity.GetColumn(0), Matrix4x4.identity.GetColumn(2), - -pitch, + pitch, RADIUS); } } diff --git a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs index 0271eedf5..0a24557a6 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs @@ -64,15 +64,16 @@ namespace UniVRM10 public static void CalcYawPitch(this Matrix4x4 m, Vector3 target, out float yaw, out float pitch) { - 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 z = Vector3.Dot(target, m.GetColumn(2)); + var x = Vector3.Dot(target, m.GetColumn(0)); + yaw = Mathf.Atan2(x, z) * Mathf.Rad2Deg; - var yawPlusMinus = Vector3.Dot(xaxis, m.GetColumn(0)) > 0 ? 1.0f : -1.0f; - yaw = (float)Math.Atan2(xaxis.magnitude, zaxis.magnitude) * yawPlusMinus * Mathf.Rad2Deg; + // x+y z plane + var xz = Mathf.Sqrt(x * x + z * z); + var y = Vector3.Dot(target, m.GetColumn(1)); - var pitchPlusMinus = Vector3.Dot(yaxis, m.GetColumn(1)) > 0 ? 1.0f : -1.0f; - pitch = (float)Math.Atan2(yaxis.magnitude, (xaxis + zaxis).magnitude) * pitchPlusMinus * Mathf.Rad2Deg; + // 正の方向が Euler 角になるように調整 + pitch = -Mathf.Atan2(y, xz) * Mathf.Rad2Deg; } public static Quaternion YawPitchRotation(this Matrix4x4 m, float yaw, float pitch) diff --git a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs index 585478a26..809ea6736 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs @@ -83,6 +83,10 @@ namespace UniVRM10 void Start() { + // cause new Vrm10Runtime. + // init LookAt init rotation. + var runtime = Runtime; + if (LookAtTargetType == VRM10ObjectLookAt.LookAtTargetTypes.CalcYawPitchToGaze) { if (Gaze == null) diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs index b166efc7a..eca4d8e4b 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs @@ -20,19 +20,19 @@ namespace UniVRM10 #region LookAtTargetTypes.CalcYawPitchToGaze // 座標計算用のempty - Transform m_lookAtOrigin; + Transform m_lookAtSpace; public Transform GetLookAtOrigin(Transform head) { if (!Application.isPlaying) { return null; } - if (m_lookAtOrigin == null) + if (m_lookAtSpace == null) { - m_lookAtOrigin = new GameObject("_lookat_origin_").transform; - m_lookAtOrigin.SetParent(head); + m_lookAtSpace = new GameObject("_lookat_origin_").transform; + m_lookAtSpace.SetParent(head); } - return m_lookAtOrigin; + return m_lookAtSpace; } /// @@ -42,7 +42,7 @@ namespace UniVRM10 { GetLookAtOrigin(head).localPosition = m_lookat.OffsetFromHead; - var localPosition = m_lookAtOrigin.worldToLocalMatrix.MultiplyPoint(targetWorldPosition); + var localPosition = m_lookAtSpace.worldToLocalMatrix.MultiplyPoint(targetWorldPosition); float yaw, pitch; Matrix4x4.identity.CalcYawPitch(localPosition, out yaw, out pitch); return (yaw, pitch); @@ -87,6 +87,9 @@ namespace UniVRM10 internal Vrm10RuntimeLookAt(VRM10ObjectLookAt lookat, UniHumanoid.Humanoid humanoid, Transform head, VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gaze) { + // 初期姿勢で初期化する! + GetLookAtOrigin(head); + m_lookat = lookat; m_head = head; From 996ff6e03bac4d0b7922f0688c96b1b17078e8fe Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 2 Aug 2022 20:14:46 +0900 Subject: [PATCH 2/4] =?UTF-8?q?lookat=20bone=E6=96=B9=E5=BC=8F=E3=81=AE?= =?UTF-8?q?=E5=88=9D=E6=9C=9F=E5=A7=BF=E5=8B=A2=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LookAt/LookAtEyeDirectionApplicableToBone.cs | 10 ++++++++-- .../Runtime/Components/LookAt/Matrix4x4Extensions.cs | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/LookAt/LookAtEyeDirectionApplicableToBone.cs b/Assets/VRM10/Runtime/Components/LookAt/LookAtEyeDirectionApplicableToBone.cs index c29c7ea6b..bf4dda274 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/LookAtEyeDirectionApplicableToBone.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/LookAtEyeDirectionApplicableToBone.cs @@ -6,7 +6,11 @@ namespace UniVRM10 internal sealed class LookAtEyeDirectionApplicableToBone : ILookAtEyeDirectionApplicable { private readonly Transform _leftEye; + private readonly Matrix4x4 _leftInit; + private readonly Transform _rightEye; + private readonly Matrix4x4 _rightInit; + private readonly CurveMapper _horizontalOuter; private readonly CurveMapper _horizontalInner; private readonly CurveMapper _verticalDown; @@ -16,7 +20,9 @@ namespace UniVRM10 CurveMapper horizontalOuter, CurveMapper horizontalInner, CurveMapper verticalDown, CurveMapper verticalUp) { _leftEye = leftEye; + _leftInit= Matrix4x4.Rotate(leftEye.localRotation); _rightEye = rightEye; + _rightInit = Matrix4x4.Rotate(rightEye.localRotation); _horizontalOuter = horizontalOuter; _horizontalInner = horizontalInner; _verticalDown = verticalDown; @@ -68,8 +74,8 @@ namespace UniVRM10 { if (_leftEye != null && _rightEye != null) { - _leftEye.localRotation = Matrix4x4.identity.YawPitchRotation(actualEyeDirection.LeftYaw, actualEyeDirection.LeftPitch); - _rightEye.localRotation = Matrix4x4.identity.YawPitchRotation(actualEyeDirection.RightYaw, actualEyeDirection.RightPitch); + _leftEye.localRotation = _leftInit.rotation * Matrix4x4.identity.YawPitchRotation(actualEyeDirection.LeftYaw, actualEyeDirection.LeftPitch); + _rightEye.localRotation = _rightInit.rotation * Matrix4x4.identity.YawPitchRotation(actualEyeDirection.RightYaw, actualEyeDirection.RightPitch); } } } diff --git a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs index 0a24557a6..682c7392a 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs @@ -78,7 +78,7 @@ namespace UniVRM10 public static Quaternion YawPitchRotation(this Matrix4x4 m, float yaw, float pitch) { - return Quaternion.AngleAxis(yaw, m.GetColumn(1)) * Quaternion.AngleAxis(-pitch, m.GetColumn(0)); + return Quaternion.AngleAxis(yaw, m.GetColumn(1)) * Quaternion.AngleAxis(pitch, m.GetColumn(0)); } public static Matrix4x4 RotationToWorldAxis(this Matrix4x4 m) From a3ee9b44b88bf1995f391063f3f047524bebbc58 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 5 Aug 2022 15:52:43 +0900 Subject: [PATCH 3/4] =?UTF-8?q?pitch=20=E3=81=AE=E6=AD=A3=E6=96=B9?= =?UTF-8?q?=E5=90=91=E3=82=92=20vrm-0.x=20=E3=81=A8=E5=90=8C=E3=81=98?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/EditorTool/VRM10LookAtEditorTool.cs | 2 +- .../Components/LookAt/Matrix4x4Extensions.cs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs index b7917fee5..d0458b33a 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs @@ -169,7 +169,7 @@ namespace UniVRM10 Handles.DrawSolidArc(Vector3.zero, Matrix4x4.identity.GetColumn(0), Matrix4x4.identity.GetColumn(2), - pitch, + -pitch, RADIUS); } } diff --git a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs index 682c7392a..f88b4905c 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs @@ -62,6 +62,11 @@ namespace UniVRM10 return (yaw, pitch); } + /// + /// yaw: 右が+ + /// pitch: 上が+ + /// という仕様。vrm-0.x から据え置き + /// public static void CalcYawPitch(this Matrix4x4 m, Vector3 target, out float yaw, out float pitch) { var z = Vector3.Dot(target, m.GetColumn(2)); @@ -71,14 +76,17 @@ namespace UniVRM10 // x+y z plane var xz = Mathf.Sqrt(x * x + z * z); var y = Vector3.Dot(target, m.GetColumn(1)); - - // 正の方向が Euler 角になるように調整 - pitch = -Mathf.Atan2(y, xz) * Mathf.Rad2Deg; + pitch = Mathf.Atan2(y, xz) * Mathf.Rad2Deg; } + /// + /// yaw: 右が+ + /// pitch: 上が+ + /// という仕様。vrm-0.x から据え置き + /// public static Quaternion YawPitchRotation(this Matrix4x4 m, float yaw, float pitch) { - return Quaternion.AngleAxis(yaw, m.GetColumn(1)) * Quaternion.AngleAxis(pitch, m.GetColumn(0)); + return Quaternion.AngleAxis(yaw, m.GetColumn(1)) * Quaternion.AngleAxis(-pitch, m.GetColumn(0)); } public static Matrix4x4 RotationToWorldAxis(this Matrix4x4 m) From 95dddde1437c23fc5fa4d4d7840e9f63795902ac Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 5 Aug 2022 15:59:32 +0900 Subject: [PATCH 4/4] =?UTF-8?q?m=5FlookAtSpace=20=E3=81=B8=E3=81=AE?= =?UTF-8?q?=E3=82=A2=E3=82=AF=E3=82=BB=E3=82=B9=E3=82=92=E5=B1=80=E6=89=80?= =?UTF-8?q?=E5=8C=96=E3=80=82`OffsetFromHead`=20=E3=81=AF=E3=81=A1?= =?UTF-8?q?=E3=82=83=E3=82=93=E3=81=A8=E4=BD=BF=E3=81=A3=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=82=8B=E3=81=93=E3=81=A8=E3=82=92=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs index eca4d8e4b..7aa88d374 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs @@ -40,9 +40,9 @@ namespace UniVRM10 /// (float, float) CalcLookAtYawPitch(Vector3 targetWorldPosition, Transform head) { - GetLookAtOrigin(head).localPosition = m_lookat.OffsetFromHead; - - var localPosition = m_lookAtSpace.worldToLocalMatrix.MultiplyPoint(targetWorldPosition); + var lookAtSpace = GetLookAtOrigin(head); + lookAtSpace.localPosition = m_lookat.OffsetFromHead; + var localPosition = lookAtSpace.worldToLocalMatrix.MultiplyPoint(targetWorldPosition); float yaw, pitch; Matrix4x4.identity.CalcYawPitch(localPosition, out yaw, out pitch); return (yaw, pitch);