diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs index 596bbb4f5..d0458b33a 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) { 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 0271eedf5..f88b4905c 100644 --- a/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs +++ b/Assets/VRM10/Runtime/Components/LookAt/Matrix4x4Extensions.cs @@ -62,19 +62,28 @@ 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 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; - - 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; + // x+y z plane + var xz = Mathf.Sqrt(x * x + z * z); + var y = Vector3.Dot(target, m.GetColumn(1)); + 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)); 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..7aa88d374 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; } /// @@ -40,9 +40,9 @@ namespace UniVRM10 /// (float, float) CalcLookAtYawPitch(Vector3 targetWorldPosition, Transform head) { - GetLookAtOrigin(head).localPosition = m_lookat.OffsetFromHead; - - var localPosition = m_lookAtOrigin.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); @@ -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;