From a344a8d12b1ce8fe53fb6f5e54acbac310268b03 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 14 Feb 2023 22:08:18 +0900 Subject: [PATCH 1/6] Refactoring about Vrm10 LookAtRuntime --- .../EditorTool/VRM10LookAtEditorTool.cs | 21 +- Assets/VRM10/Runtime/AssemblyInfo.cs | 5 + Assets/VRM10/Runtime/AssemblyInfo.cs.meta | 11 + .../Expression/LookAtEyeDirection.cs | 2 + .../VRM10Object/VRM10ObjectLookAt.cs | 16 +- .../Components/Vrm10Instance/Vrm10Instance.cs | 10 +- .../Components/Vrm10Runtime/Vrm10Runtime.cs | 2 +- .../Vrm10Runtime/Vrm10RuntimeLookAt.cs | 206 +++++++++--------- .../VRM10_Samples/VRM10Viewer/VRM10Loaded.cs | 2 +- 9 files changed, 145 insertions(+), 130 deletions(-) create mode 100644 Assets/VRM10/Runtime/AssemblyInfo.cs create mode 100644 Assets/VRM10/Runtime/AssemblyInfo.cs.meta diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs index d0458b33a..63ce8ace9 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs @@ -86,7 +86,7 @@ namespace UniVRM10 if (Application.isPlaying) { - OnSceneGUILookAt(root.Vrm.LookAt, root.Runtime.LookAt, head, root.LookAtTargetType, root.Gaze); + OnSceneGUILookAt(root.Vrm.LookAt, root.Runtime.LookAt, root.LookAtTargetType, root.Gaze); } else { @@ -125,28 +125,27 @@ namespace UniVRM10 const float RADIUS = 0.5f; - static void OnSceneGUILookAt(VRM10ObjectLookAt lookAt, Vrm10RuntimeLookAt runtime, Transform head, VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gaze) + static void OnSceneGUILookAt(VRM10ObjectLookAt lookAt, Vrm10RuntimeLookAt runtime, VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gazeTarget) { - if (head == null) return; - - if (gaze != null) + if (lookAtTargetType == VRM10ObjectLookAt.LookAtTargetTypes.Auto && gazeTarget != null) { { EditorGUI.BeginChangeCheck(); - var newTargetPosition = Handles.PositionHandle(gaze.position, Quaternion.identity); + var newTargetPosition = Handles.PositionHandle(gazeTarget.position, Quaternion.identity); if (EditorGUI.EndChangeCheck()) { - Undo.RecordObject(gaze, "Change Look At Target Position"); - gaze.position = newTargetPosition; + Undo.RecordObject(gazeTarget, "Change Look At Target Position"); + gazeTarget.position = newTargetPosition; } } Handles.color = new Color(1, 1, 1, 0.6f); - Handles.DrawDottedLine(runtime.GetLookAtOrigin(head).position, gaze.position, 4.0f); + Handles.DrawDottedLine(runtime.EyeTransform.position, gazeTarget.position, 4.0f); } - var (yaw, pitch) = runtime.GetLookAtYawPitch(head, lookAtTargetType, gaze); - var lookAtOriginMatrix = runtime.GetLookAtOrigin(head).localToWorldMatrix; + var yaw = runtime.Yaw; + var pitch = runtime.Pitch; + var lookAtOriginMatrix = runtime.EyeTransform.localToWorldMatrix; Handles.matrix = lookAtOriginMatrix; var p = lookAt.OffsetFromHead; Handles.Label(Vector3.zero, diff --git a/Assets/VRM10/Runtime/AssemblyInfo.cs b/Assets/VRM10/Runtime/AssemblyInfo.cs new file mode 100644 index 000000000..cdd50e031 --- /dev/null +++ b/Assets/VRM10/Runtime/AssemblyInfo.cs @@ -0,0 +1,5 @@ +#if UNITY_EDITOR +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("VRM10.Tests")] +#endif \ No newline at end of file diff --git a/Assets/VRM10/Runtime/AssemblyInfo.cs.meta b/Assets/VRM10/Runtime/AssemblyInfo.cs.meta new file mode 100644 index 000000000..e78efb4ef --- /dev/null +++ b/Assets/VRM10/Runtime/AssemblyInfo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c0bb9ff90c68a3f4bb86b5fed930f30e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Components/Expression/LookAtEyeDirection.cs b/Assets/VRM10/Runtime/Components/Expression/LookAtEyeDirection.cs index e3d486f41..f4d9db503 100644 --- a/Assets/VRM10/Runtime/Components/Expression/LookAtEyeDirection.cs +++ b/Assets/VRM10/Runtime/Components/Expression/LookAtEyeDirection.cs @@ -13,11 +13,13 @@ public float LeftPitch { get; } /// + /// NOTE: 何故か使われていない /// Yaw of RightEye /// public float RightYaw { get; } /// + /// NOTE: 何故か使われていない /// Pitch of RightEye /// public float RightPitch { get; } diff --git a/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectLookAt.cs b/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectLookAt.cs index 1077211b1..158529fb8 100644 --- a/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectLookAt.cs +++ b/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectLookAt.cs @@ -1,9 +1,6 @@ using System; using UnityEngine; using UniGLTF.Extensions.VRMC_vrm; -#if UNITY_EDITOR -using UnityEditor; -#endif namespace UniVRM10 { @@ -12,8 +9,17 @@ namespace UniVRM10 { public enum LookAtTargetTypes { - CalcYawPitchToGaze, - SetYawPitch, + /// + /// Vrm10Instance に設定した Gaze Transform を見ます. + /// + Auto = 0, + /// + /// Vrm10RuntimeLookAt に設定した Yaw/Pitch 値に従います. + /// + Manual = 1, + + [Obsolete] CalcYawPitchToGaze = 0, + [Obsolete] SetYawPitch = 1, } [SerializeField] diff --git a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs index e3a888704..2a8e7eeba 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs @@ -110,15 +110,7 @@ namespace UniVRM10 // cause new Vrm10Runtime. // init LookAt init rotation. - var runtime = Runtime; - - if (LookAtTargetType == VRM10ObjectLookAt.LookAtTargetTypes.CalcYawPitchToGaze) - { - if (Gaze == null) - { - LookAtTargetType = VRM10ObjectLookAt.LookAtTargetTypes.SetYawPitch; - } - } + var _ = Runtime; } private void Update() diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs index 0265e65b7..261ff44a8 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs @@ -63,7 +63,7 @@ namespace UniVRM10 ControlRig = new Vrm10RuntimeControlRig(target.Humanoid, m_target.transform, controlRigInitialRotations); } Constraints = target.GetComponentsInChildren(); - LookAt = new Vrm10RuntimeLookAt(target.Vrm.LookAt, target.Humanoid, m_head, target.LookAtTargetType, target.Gaze); + LookAt = new Vrm10RuntimeLookAt(target.Vrm.LookAt, target.Humanoid, m_head); Expression = new Vrm10RuntimeExpression(target, LookAt, LookAt.EyeDirectionApplicable); var instance = target.GetComponent(); diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs index f01dd1e3b..18b17a309 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs @@ -1,128 +1,128 @@ using System; using UniGLTF.Extensions.VRMC_vrm; -using UnityEditor; using UnityEngine; namespace UniVRM10 { public sealed class Vrm10RuntimeLookAt : ILookAtEyeDirectionProvider { - VRM10ObjectLookAt m_lookat; + private readonly VRM10ObjectLookAt _lookAt; + private readonly Transform _head; + private readonly Vector3 _eyeTransformLocalPosition; + private readonly Quaternion _eyeTransformLocalRotation; - private Transform m_head; - private Transform m_leftEye; - private Transform m_rightEye; - private ILookAtEyeDirectionApplicable _eyeDirectionApplicable; + public float Yaw { get; private set; } + public float Pitch { get; private set; } - internal ILookAtEyeDirectionApplicable EyeDirectionApplicable => _eyeDirectionApplicable; + internal ILookAtEyeDirectionApplicable EyeDirectionApplicable { get; } public LookAtEyeDirection EyeDirection { get; private set; } - #region LookAtTargetTypes.CalcYawPitchToGaze - // 座標計算用のempty - Transform m_lookAtSpace; - public Transform GetLookAtOrigin(Transform head) + public Transform EyeTransform { get; } + + internal Vrm10RuntimeLookAt(VRM10ObjectLookAt lookAt, UniHumanoid.Humanoid humanoid, Transform head) { - if (!Application.isPlaying) + _lookAt = lookAt; + _head = head; + EyeTransform = InitializeEyePositionTransform(_head, _lookAt.OffsetFromHead); + _eyeTransformLocalPosition = EyeTransform.localPosition; + _eyeTransformLocalRotation = EyeTransform.localRotation; + + var leftEyeBone = humanoid.GetBoneTransform(HumanBodyBones.LeftEye); + var rightEyeBone = humanoid.GetBoneTransform(HumanBodyBones.RightEye); + if (_lookAt.LookAtType == LookAtType.bone && leftEyeBone != null && rightEyeBone != null) { - return null; - } - if (m_lookAtSpace == null) - { - m_lookAtSpace = new GameObject("_lookat_origin_").transform; - m_lookAtSpace.SetParent(head); - } - return m_lookAtSpace; - } - - /// - /// Headローカルの注視点からYaw, Pitch角を計算する - /// - (float, float) CalcLookAtYawPitch(Vector3 targetWorldPosition, Transform head) - { - 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); - } - #endregion - - #region LookAtTargetTypes.SetYawPitch - float m_yaw; - float m_pitch; - - /// - /// LookAtTargetTypes.SetYawPitch時の視線の角度を指定する - /// - /// Headボーンのforwardに対するyaw角(度) - /// Headボーンのforwardに対するpitch角(度) - public void SetLookAtYawPitch(float yaw, float pitch) - { - m_yaw = yaw; - m_pitch = pitch; - } - #endregion - - /// - /// LookAtTargetType に応じた yaw, pitch を得る - /// - /// Headボーンのforwardに対するyaw角(度), pitch角(度) - public (float, float) GetLookAtYawPitch(Transform head, VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gaze) - { - switch (lookAtTargetType) - { - case VRM10ObjectLookAt.LookAtTargetTypes.CalcYawPitchToGaze: - // Gaze(Transform)のワールド位置に対して計算する - return CalcLookAtYawPitch(gaze.position, head); - - case VRM10ObjectLookAt.LookAtTargetTypes.SetYawPitch: - // 事前にSetYawPitchした値を使う - return (m_yaw, m_pitch); - } - - throw new NotImplementedException(); - } - - internal Vrm10RuntimeLookAt(VRM10ObjectLookAt lookat, UniHumanoid.Humanoid humanoid, Transform head, VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gaze) - { - // 初期姿勢で初期化する! - GetLookAtOrigin(head); - - m_lookat = lookat; - - m_head = head; - m_leftEye = humanoid.GetBoneTransform(HumanBodyBones.LeftEye); - m_rightEye = humanoid.GetBoneTransform(HumanBodyBones.RightEye); - - var isRuntimeAsset = true; -#if UNITY_EDITOR - isRuntimeAsset = Application.isPlaying && !PrefabUtility.IsPartOfAnyPrefab(m_head); -#endif - if (isRuntimeAsset && lookAtTargetType == VRM10ObjectLookAt.LookAtTargetTypes.CalcYawPitchToGaze && gaze == null) - { - gaze = new GameObject().transform; - gaze.name = "__LOOKAT_GAZE__"; - gaze.SetParent(m_head); - gaze.localPosition = Vector3.forward; - } - - // bone が無いときのエラー防止。マイグレーション失敗? - if (m_lookat.LookAtType == LookAtType.bone && m_leftEye != null && m_rightEye != null) - { - _eyeDirectionApplicable = new LookAtEyeDirectionApplicableToBone(m_leftEye, m_rightEye, m_lookat.HorizontalOuter, m_lookat.HorizontalInner, m_lookat.VerticalDown, m_lookat.VerticalUp); + EyeDirectionApplicable = new LookAtEyeDirectionApplicableToBone(leftEyeBone, rightEyeBone, _lookAt.HorizontalOuter, _lookAt.HorizontalInner, _lookAt.VerticalDown, _lookAt.VerticalUp); } else { - _eyeDirectionApplicable = new LookAtEyeDirectionApplicableToExpression(m_lookat.HorizontalOuter, m_lookat.HorizontalInner, m_lookat.VerticalDown, m_lookat.VerticalUp); + EyeDirectionApplicable = new LookAtEyeDirectionApplicableToExpression(_lookAt.HorizontalOuter, _lookAt.HorizontalInner, _lookAt.VerticalDown, _lookAt.VerticalUp); } } - internal void Process(VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gaze) + internal void Process(VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gazeTarget) { - var (yaw, pitch) = GetLookAtYawPitch(m_head, lookAtTargetType, gaze); - EyeDirection = new LookAtEyeDirection(yaw, pitch, 0, 0); + EyeTransform.localPosition = _eyeTransformLocalPosition; + EyeTransform.localRotation = _eyeTransformLocalRotation; + + switch (lookAtTargetType) + { + case VRM10ObjectLookAt.LookAtTargetTypes.Auto: + // NOTE: 指定された Gaze Transform の位置を向くように Yaw/Pitch を計算して適用する + if (gazeTarget != null) + { + var value = CalculateYawPitchFromGazePosition(gazeTarget.position); + SetYawPitchManually(value.Yaw, value.Pitch); + } + break; + case VRM10ObjectLookAt.LookAtTargetTypes.Manual: + // NOTE: 直接 Set された Yaw/Pitch を使って計算する + break; + } + + EyeDirection = new LookAtEyeDirection(Yaw, Pitch, 0, 0); } + + /// + /// LookAtTargetTypes.Manual 時に考慮される Yaw/Pitch 値を設定する + /// + /// Headボーンのforwardに対するyaw角(度) + /// Headボーンのforwardに対するpitch角(度) + public void SetYawPitchManually(float yaw, float pitch) + { + Yaw = yaw; + Pitch = pitch; + } + + private (float Yaw, float Pitch) CalculateYawPitchFromGazePosition(Vector3 gazeWorldPosition) + { + var localPosition = EyeTransform.worldToLocalMatrix.MultiplyPoint(gazeWorldPosition); + Matrix4x4.identity.CalcYawPitch(localPosition, out var yaw, out var pitch); + return (yaw, pitch); + } + + private static Transform InitializeEyePositionTransform(Transform head, Vector3 eyeOffsetValue) + { + if (!Application.isPlaying) return null; + + // NOTE: このメソッドを実行するとき、モデル全体は初期姿勢(T-Pose)でなければならない。 + var eyePositionTransform = new GameObject("_eye_transform_").transform; + eyePositionTransform.SetParent(head); + eyePositionTransform.localPosition = eyeOffsetValue; + eyePositionTransform.rotation = Quaternion.identity; + + return eyePositionTransform; + } + +#region Obsolete + [Obsolete] + public Transform GetLookAtOrigin(Transform head) + { + return EyeTransform; + } + + [Obsolete] + public void SetLookAtYawPitch(float yaw, float pitch) + { + SetYawPitchManually(yaw, pitch); + } + + [Obsolete] + public (float, float) GetLookAtYawPitch( + Transform head, + VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, + Transform gaze) + { + switch (lookAtTargetType) + { + case VRM10ObjectLookAt.LookAtTargetTypes.Auto: + return CalculateYawPitchFromGazePosition(gaze.position); + case VRM10ObjectLookAt.LookAtTargetTypes.Manual: + return (Yaw, Pitch); + default: + throw new ArgumentOutOfRangeException(nameof(lookAtTargetType), lookAtTargetType, null); + } + } +#endregion } } diff --git a/Assets/VRM10_Samples/VRM10Viewer/VRM10Loaded.cs b/Assets/VRM10_Samples/VRM10Viewer/VRM10Loaded.cs index 861c2e783..cefcb8e06 100644 --- a/Assets/VRM10_Samples/VRM10Viewer/VRM10Loaded.cs +++ b/Assets/VRM10_Samples/VRM10Viewer/VRM10Loaded.cs @@ -69,7 +69,7 @@ namespace UniVRM10.VRM10Viewer m_blink = instance.gameObject.AddComponent(); m_autoExpression = instance.gameObject.AddComponent(); - m_controller.LookAtTargetType = VRM10ObjectLookAt.LookAtTargetTypes.CalcYawPitchToGaze; + m_controller.LookAtTargetType = VRM10ObjectLookAt.LookAtTargetTypes.Auto; m_controller.Gaze = lookAtTarget; } } From dbfd5212c7fd6845f1aaadb2e640f6ea4e1f113f Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 14 Feb 2023 22:14:48 +0900 Subject: [PATCH 2/6] make CalculateYawPitchFromGazePosition public --- .../VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs index 18b17a309..fc9d9bd80 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs @@ -74,7 +74,7 @@ namespace UniVRM10 Pitch = pitch; } - private (float Yaw, float Pitch) CalculateYawPitchFromGazePosition(Vector3 gazeWorldPosition) + public (float Yaw, float Pitch) CalculateYawPitchFromGazePosition(Vector3 gazeWorldPosition) { var localPosition = EyeTransform.worldToLocalMatrix.MultiplyPoint(gazeWorldPosition); Matrix4x4.identity.CalcYawPitch(localPosition, out var yaw, out var pitch); From b376a94d754c51a9c74c7168b63287f73a373e94 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 14 Feb 2023 22:56:16 +0900 Subject: [PATCH 3/6] rename gaze to LookAt --- .../EditorTool/VRM10LookAtEditorTool.cs | 14 +++++----- .../VRM10Object/VRM10ObjectLookAt.cs | 6 ++-- .../Components/Vrm10Instance/Vrm10Instance.cs | 28 ++++++++++++++++--- .../Components/Vrm10Runtime/Vrm10Runtime.cs | 2 +- .../Vrm10Runtime/Vrm10RuntimeLookAt.cs | 25 +++++++++-------- .../VRM10_Samples/VRM10Viewer/VRM10Loaded.cs | 4 +-- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs index 63ce8ace9..800b081d1 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs @@ -86,7 +86,7 @@ namespace UniVRM10 if (Application.isPlaying) { - OnSceneGUILookAt(root.Vrm.LookAt, root.Runtime.LookAt, root.LookAtTargetType, root.Gaze); + OnSceneGUILookAt(root.Vrm.LookAt, root.Runtime.LookAt, root.LookAtTargetType, root.LookAtTarget); } else { @@ -125,22 +125,22 @@ namespace UniVRM10 const float RADIUS = 0.5f; - static void OnSceneGUILookAt(VRM10ObjectLookAt lookAt, Vrm10RuntimeLookAt runtime, VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gazeTarget) + static void OnSceneGUILookAt(VRM10ObjectLookAt lookAt, Vrm10RuntimeLookAt runtime, VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform lookAtTarget) { - if (lookAtTargetType == VRM10ObjectLookAt.LookAtTargetTypes.Auto && gazeTarget != null) + if (lookAtTargetType == VRM10ObjectLookAt.LookAtTargetTypes.SpecifiedTransform && lookAtTarget != null) { { EditorGUI.BeginChangeCheck(); - var newTargetPosition = Handles.PositionHandle(gazeTarget.position, Quaternion.identity); + var newTargetPosition = Handles.PositionHandle(lookAtTarget.position, Quaternion.identity); if (EditorGUI.EndChangeCheck()) { - Undo.RecordObject(gazeTarget, "Change Look At Target Position"); - gazeTarget.position = newTargetPosition; + Undo.RecordObject(lookAtTarget, "Change Look At Target Position"); + lookAtTarget.position = newTargetPosition; } } Handles.color = new Color(1, 1, 1, 0.6f); - Handles.DrawDottedLine(runtime.EyeTransform.position, gazeTarget.position, 4.0f); + Handles.DrawDottedLine(runtime.EyeTransform.position, lookAtTarget.position, 4.0f); } var yaw = runtime.Yaw; diff --git a/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectLookAt.cs b/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectLookAt.cs index 158529fb8..01a0261c8 100644 --- a/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectLookAt.cs +++ b/Assets/VRM10/Runtime/Components/VRM10Object/VRM10ObjectLookAt.cs @@ -10,13 +10,13 @@ namespace UniVRM10 public enum LookAtTargetTypes { /// - /// Vrm10Instance に設定した Gaze Transform を見ます. + /// Vrm10Instance に設定した Transform を見ます. /// - Auto = 0, + SpecifiedTransform = 0, /// /// Vrm10RuntimeLookAt に設定した Yaw/Pitch 値に従います. /// - Manual = 1, + YawPitchValue = 1, [Obsolete] CalcYawPitchToGaze = 0, [Obsolete] SetYawPitch = 1, diff --git a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs index 2a8e7eeba..b7f42c40a 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Instance/Vrm10Instance.cs @@ -1,5 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using UnityEngine; +using UnityEngine.Serialization; namespace UniVRM10 @@ -43,11 +45,20 @@ namespace UniVRM10 public bool DrawLookAtGizmo = true; /// - /// LookAtTargetTypes.CalcYawPitchToGaze時の注視点 + /// The model looks at position of the Transform specified in this field. + /// That behaviour is available only when LookAtTargetType is SpecifiedTransform. + /// + /// モデルはここで指定した Transform の位置の方向に目を向けます。 + /// LookAtTargetType を SpecifiedTransform に設定したときのみ有効です。 /// - [SerializeField] - public Transform Gaze; + [SerializeField, FormerlySerializedAs("Gaze")] + public Transform LookAtTarget; + /// + /// Specify "LookAt" behaviour at runtime. + /// + /// 実行時の目の動かし方を指定します。 + /// [SerializeField] public VRM10ObjectLookAt.LookAtTargetTypes LookAtTargetType; @@ -157,6 +168,15 @@ namespace UniVRM10 return true; } + #region Obsolete + [Obsolete] + public Transform Gaze + { + get => LookAtTarget; + set => LookAtTarget = value; + } + + #endregion } } diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs index 261ff44a8..07e419ede 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs @@ -191,7 +191,7 @@ namespace UniVRM10 } // 3. Gaze control - LookAt.Process(m_target.LookAtTargetType, m_target.Gaze); + LookAt.Process(m_target.LookAtTargetType, m_target.LookAtTarget); // 4. Expression Expression.Process(); diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs index fc9d9bd80..09a38fb05 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs @@ -40,22 +40,22 @@ namespace UniVRM10 } } - internal void Process(VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform gazeTarget) + internal void Process(VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform lookAtTarget) { EyeTransform.localPosition = _eyeTransformLocalPosition; EyeTransform.localRotation = _eyeTransformLocalRotation; switch (lookAtTargetType) { - case VRM10ObjectLookAt.LookAtTargetTypes.Auto: - // NOTE: 指定された Gaze Transform の位置を向くように Yaw/Pitch を計算して適用する - if (gazeTarget != null) + case VRM10ObjectLookAt.LookAtTargetTypes.SpecifiedTransform: + // NOTE: 指定された Transform の位置を向くように Yaw/Pitch を計算して適用する + if (lookAtTarget != null) { - var value = CalculateYawPitchFromGazePosition(gazeTarget.position); + var value = CalculateYawPitchFromLookAtPosition(lookAtTarget.position); SetYawPitchManually(value.Yaw, value.Pitch); } break; - case VRM10ObjectLookAt.LookAtTargetTypes.Manual: + case VRM10ObjectLookAt.LookAtTargetTypes.YawPitchValue: // NOTE: 直接 Set された Yaw/Pitch を使って計算する break; } @@ -64,7 +64,8 @@ namespace UniVRM10 } /// - /// LookAtTargetTypes.Manual 時に考慮される Yaw/Pitch 値を設定する + /// Yaw/Pitch 値を直接設定します。 + /// LookAtTargetTypes が SpecifiedTransform の場合、ここで設定しても値は上書きされます。 /// /// Headボーンのforwardに対するyaw角(度) /// Headボーンのforwardに対するpitch角(度) @@ -74,9 +75,9 @@ namespace UniVRM10 Pitch = pitch; } - public (float Yaw, float Pitch) CalculateYawPitchFromGazePosition(Vector3 gazeWorldPosition) + public (float Yaw, float Pitch) CalculateYawPitchFromLookAtPosition(Vector3 lookAtWorldPosition) { - var localPosition = EyeTransform.worldToLocalMatrix.MultiplyPoint(gazeWorldPosition); + var localPosition = EyeTransform.worldToLocalMatrix.MultiplyPoint(lookAtWorldPosition); Matrix4x4.identity.CalcYawPitch(localPosition, out var yaw, out var pitch); return (yaw, pitch); } @@ -115,9 +116,9 @@ namespace UniVRM10 { switch (lookAtTargetType) { - case VRM10ObjectLookAt.LookAtTargetTypes.Auto: - return CalculateYawPitchFromGazePosition(gaze.position); - case VRM10ObjectLookAt.LookAtTargetTypes.Manual: + case VRM10ObjectLookAt.LookAtTargetTypes.SpecifiedTransform: + return CalculateYawPitchFromLookAtPosition(gaze.position); + case VRM10ObjectLookAt.LookAtTargetTypes.YawPitchValue: return (Yaw, Pitch); default: throw new ArgumentOutOfRangeException(nameof(lookAtTargetType), lookAtTargetType, null); diff --git a/Assets/VRM10_Samples/VRM10Viewer/VRM10Loaded.cs b/Assets/VRM10_Samples/VRM10Viewer/VRM10Loaded.cs index cefcb8e06..60dc9e012 100644 --- a/Assets/VRM10_Samples/VRM10Viewer/VRM10Loaded.cs +++ b/Assets/VRM10_Samples/VRM10Viewer/VRM10Loaded.cs @@ -69,8 +69,8 @@ namespace UniVRM10.VRM10Viewer m_blink = instance.gameObject.AddComponent(); m_autoExpression = instance.gameObject.AddComponent(); - m_controller.LookAtTargetType = VRM10ObjectLookAt.LookAtTargetTypes.Auto; - m_controller.Gaze = lookAtTarget; + m_controller.LookAtTargetType = VRM10ObjectLookAt.LookAtTargetTypes.SpecifiedTransform; + m_controller.LookAtTarget = lookAtTarget; } } From b25e58c3ffd109236bae45f22f24d733ab382c01 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 14 Feb 2023 23:05:12 +0900 Subject: [PATCH 4/6] reorder --- .../Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs index 09a38fb05..a3335191f 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs @@ -11,13 +11,11 @@ namespace UniVRM10 private readonly Vector3 _eyeTransformLocalPosition; private readonly Quaternion _eyeTransformLocalRotation; - public float Yaw { get; private set; } - public float Pitch { get; private set; } - internal ILookAtEyeDirectionApplicable EyeDirectionApplicable { get; } + public float Yaw { get; private set; } + public float Pitch { get; private set; } public LookAtEyeDirection EyeDirection { get; private set; } - public Transform EyeTransform { get; } internal Vrm10RuntimeLookAt(VRM10ObjectLookAt lookAt, UniHumanoid.Humanoid humanoid, Transform head) From 02aae32acbb863cf14450feaf4b415a75361a27e Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 14 Feb 2023 23:34:22 +0900 Subject: [PATCH 5/6] rename --- .../VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs index 800b081d1..013e85b77 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs @@ -69,18 +69,18 @@ namespace UniVRM10 { EditorGUI.BeginChangeCheck(); - var worldOffset = head.localToWorldMatrix.MultiplyPoint(root.Vrm.LookAt.OffsetFromHead); - worldOffset = Handles.PositionHandle(worldOffset, head.rotation); + var eyeWorldPosition = head.localToWorldMatrix.MultiplyPoint(root.Vrm.LookAt.OffsetFromHead); + eyeWorldPosition = Handles.PositionHandle(eyeWorldPosition, head.rotation); - Handles.DrawDottedLine(head.position, worldOffset, 5); + Handles.DrawDottedLine(head.position, eyeWorldPosition, 5); Handles.SphereHandleCap(0, head.position, Quaternion.identity, 0.02f, Event.current.type); - Handles.SphereHandleCap(0, worldOffset, Quaternion.identity, 0.02f, Event.current.type); + Handles.SphereHandleCap(0, eyeWorldPosition, Quaternion.identity, 0.02f, Event.current.type); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(root.Vrm, "LookAt.OffsetFromHead"); - root.Vrm.LookAt.OffsetFromHead = head.worldToLocalMatrix.MultiplyPoint(worldOffset); + root.Vrm.LookAt.OffsetFromHead = head.worldToLocalMatrix.MultiplyPoint(eyeWorldPosition); } } From 95ef1b839f677635cb245d29ea7645bbb66d5455 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 15 Feb 2023 00:07:05 +0900 Subject: [PATCH 6/6] rename and add comment --- .../EditorTool/VRM10LookAtEditorTool.cs | 4 +-- .../Vrm10Runtime/Vrm10RuntimeLookAt.cs | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs index 013e85b77..b98e66db3 100644 --- a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs @@ -140,12 +140,12 @@ namespace UniVRM10 } Handles.color = new Color(1, 1, 1, 0.6f); - Handles.DrawDottedLine(runtime.EyeTransform.position, lookAtTarget.position, 4.0f); + Handles.DrawDottedLine(runtime.LookAtOriginTransform.position, lookAtTarget.position, 4.0f); } var yaw = runtime.Yaw; var pitch = runtime.Pitch; - var lookAtOriginMatrix = runtime.EyeTransform.localToWorldMatrix; + var lookAtOriginMatrix = runtime.LookAtOriginTransform.localToWorldMatrix; Handles.matrix = lookAtOriginMatrix; var p = lookAt.OffsetFromHead; Handles.Label(Vector3.zero, diff --git a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs index a3335191f..fef7e15d0 100644 --- a/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs +++ b/Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10RuntimeLookAt.cs @@ -8,23 +8,35 @@ namespace UniVRM10 { private readonly VRM10ObjectLookAt _lookAt; private readonly Transform _head; - private readonly Vector3 _eyeTransformLocalPosition; - private readonly Quaternion _eyeTransformLocalRotation; + private readonly Vector3 _lookAtOriginTransformLocalPosition; + private readonly Quaternion _lookAtOriginTransformLocalRotation; internal ILookAtEyeDirectionApplicable EyeDirectionApplicable { get; } public float Yaw { get; private set; } public float Pitch { get; private set; } public LookAtEyeDirection EyeDirection { get; private set; } - public Transform EyeTransform { get; } + + /// + /// Transform that indicates the position center of eyes. + /// This only represents the position of center of eyes, not the viewing direction. + /// Local +Z axis represents forward vector of the head. + /// Local +Y axis represents up vector of the head. + /// + /// 目の位置を示す Transform。 + /// 視線方向は反映されない。 + /// ローカル +Z 軸が頭の正面方向を表す。 + /// ローカル +Y 軸が頭の上方向を表す。 + /// + public Transform LookAtOriginTransform { get; } internal Vrm10RuntimeLookAt(VRM10ObjectLookAt lookAt, UniHumanoid.Humanoid humanoid, Transform head) { _lookAt = lookAt; _head = head; - EyeTransform = InitializeEyePositionTransform(_head, _lookAt.OffsetFromHead); - _eyeTransformLocalPosition = EyeTransform.localPosition; - _eyeTransformLocalRotation = EyeTransform.localRotation; + LookAtOriginTransform = InitializeEyePositionTransform(_head, _lookAt.OffsetFromHead); + _lookAtOriginTransformLocalPosition = LookAtOriginTransform.localPosition; + _lookAtOriginTransformLocalRotation = LookAtOriginTransform.localRotation; var leftEyeBone = humanoid.GetBoneTransform(HumanBodyBones.LeftEye); var rightEyeBone = humanoid.GetBoneTransform(HumanBodyBones.RightEye); @@ -40,8 +52,8 @@ namespace UniVRM10 internal void Process(VRM10ObjectLookAt.LookAtTargetTypes lookAtTargetType, Transform lookAtTarget) { - EyeTransform.localPosition = _eyeTransformLocalPosition; - EyeTransform.localRotation = _eyeTransformLocalRotation; + LookAtOriginTransform.localPosition = _lookAtOriginTransformLocalPosition; + LookAtOriginTransform.localRotation = _lookAtOriginTransformLocalRotation; switch (lookAtTargetType) { @@ -75,7 +87,7 @@ namespace UniVRM10 public (float Yaw, float Pitch) CalculateYawPitchFromLookAtPosition(Vector3 lookAtWorldPosition) { - var localPosition = EyeTransform.worldToLocalMatrix.MultiplyPoint(lookAtWorldPosition); + var localPosition = LookAtOriginTransform.worldToLocalMatrix.MultiplyPoint(lookAtWorldPosition); Matrix4x4.identity.CalcYawPitch(localPosition, out var yaw, out var pitch); return (yaw, pitch); } @@ -97,7 +109,7 @@ namespace UniVRM10 [Obsolete] public Transform GetLookAtOrigin(Transform head) { - return EyeTransform; + return LookAtOriginTransform; } [Obsolete]