From 71c0068652cdfb31ad97f1c936b73936f6f5dd0c Mon Sep 17 00:00:00 2001 From: SDraw Date: Tue, 4 Oct 2022 19:58:10 +0000 Subject: [PATCH] Better tracking for all avatars --- README.md | 2 +- ml_dht/{FaceTracked.cs => HeadTracked.cs} | 42 +++++++++-------------- ml_dht/Main.cs | 4 +-- ml_dht/Properties/AssemblyInfo.cs | 6 ++-- ml_dht/Utils.cs | 12 +++++++ ml_dht/ml_dht.csproj | 3 +- 6 files changed, 36 insertions(+), 33 deletions(-) rename ml_dht/{FaceTracked.cs => HeadTracked.cs} (69%) create mode 100644 ml_dht/Utils.cs diff --git a/README.md b/README.md index fc6c747..1280fbc 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Merged set of MelonLoader mods for ChilloutVR. |-----------|------------|----------------|-----------------------------------------------------------------|----------------|-------| | Avatar Change Info | ml_aci | 1.0.2 | Yes | Working | | Avatar Motion Tweaker | ml_amt | 1.1.1 | On review | Working | -| Desktop Head Tracking | ml_dht | 1.0.3 | On review | Working | +| Desktop Head Tracking | ml_dht | 1.0.4 | On review | Working | | Desktop Reticle Switch | ml_drs | 1.0.0 | Yes | Working | | Four Point Tracking | ml_fpt | 1.0.7 | On review | Working | | Leap Motion Extension | ml_lme | 1.2.0 | On review | Working | diff --git a/ml_dht/FaceTracked.cs b/ml_dht/HeadTracked.cs similarity index 69% rename from ml_dht/FaceTracked.cs rename to ml_dht/HeadTracked.cs index 7ce29c6..0fb4b5c 100644 --- a/ml_dht/FaceTracked.cs +++ b/ml_dht/HeadTracked.cs @@ -4,14 +4,15 @@ using UnityEngine; namespace ml_dht { - class FaceTracked : MonoBehaviour + [DisallowMultipleComponent] + class HeadTracked : MonoBehaviour { bool m_enabled = false; float m_smoothing = 0.5f; bool m_mirrored = false; bool m_faceOverride = true; - CVRAvatar m_avatarDescriptior = null; + CVRAvatar m_avatarDescriptor = null; RootMotion.FinalIK.LookAtIK m_lookIK = null; Transform m_camera = null; Transform m_headBone = null; @@ -45,8 +46,8 @@ namespace ml_dht { if(m_enabled && (m_headBone != null)) { - m_lastHeadRotation = Quaternion.Slerp(m_lastHeadRotation, m_headRotation * m_bindRotation, m_smoothing); - m_headBone.localRotation = m_lastHeadRotation; + m_lastHeadRotation = Quaternion.Slerp(m_lastHeadRotation, m_avatarDescriptor.transform.rotation * (m_headRotation * m_bindRotation), m_smoothing); + m_headBone.rotation = m_lastHeadRotation; } } @@ -68,26 +69,15 @@ namespace ml_dht { if(m_enabled && m_faceOverride) { - if(m_avatarDescriptior != null) - m_avatarDescriptior.useVisemeLipsync = false; + if(m_avatarDescriptor != null) + m_avatarDescriptor.useVisemeLipsync = false; + + float l_weight = Mathf.Clamp(Mathf.InverseLerp(0.25f, 1f, Mathf.Abs(m_mouthShapes.y)), 0f, 1f) * 100f; p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Jaw_Open] = m_mouthShapes.x * 100f; - - if(m_mouthShapes.y < 0f) - { - float l_weight = Mathf.Clamp(Mathf.InverseLerp(0.25f, 1f, -m_mouthShapes.y), 0f, 1f) * 100f; - p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Pout] = 0f; - p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Smile_Left] = l_weight; - p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Smile_Right] = l_weight; - } - if(m_mouthShapes.y > 0f) - { - float l_weight = Mathf.Clamp(Mathf.InverseLerp(0.25f, 1f, m_mouthShapes.y), 0f, 1f) * 100f; - p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Pout] = l_weight; - p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Smile_Left] = 0f; - p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Smile_Right] = 0f; - } - + p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Pout] = ((m_mouthShapes.y > 0f) ? l_weight : 0f); + p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Smile_Left] = ((m_mouthShapes.y < 0f) ? l_weight : 0f); + p_component.BlendShapeValues[(int)ViveSR.anipal.Lip.LipShape_v2.Mouth_Smile_Right] = ((m_mouthShapes.y < 0f) ? l_weight : 0f); p_component.LipSyncWasUpdated = true; p_component.UpdateLipShapes(); } @@ -95,12 +85,12 @@ namespace ml_dht public void OnCalibrateAvatar() { - m_avatarDescriptior = PlayerSetup.Instance._avatar.GetComponent(); + m_avatarDescriptor = PlayerSetup.Instance._avatar.GetComponent(); m_headBone = PlayerSetup.Instance._animator.GetBoneTransform(HumanBodyBones.Head); m_lookIK = PlayerSetup.Instance._avatar.GetComponent(); if(m_headBone != null) - m_bindRotation = m_headBone.localRotation; + m_bindRotation = (m_avatarDescriptor.transform.GetMatrix().inverse * m_headBone.GetMatrix()).rotation; if(m_lookIK != null) m_lookIK.solver.OnPostUpdate += this.OnLookIKPostUpdate; @@ -108,7 +98,7 @@ namespace ml_dht } public void OnAvatarClear() { - m_avatarDescriptior = null; + m_avatarDescriptor = null; m_lookIK = null; m_headBone = null; m_lastHeadRotation = Quaternion.identity; @@ -121,7 +111,7 @@ namespace ml_dht { m_enabled = p_state; if(m_enabled) - m_lastHeadRotation = m_bindRotation; + m_lastHeadRotation = ((m_headBone != null) ? m_headBone.rotation : m_bindRotation); } } public void SetSmoothing(float p_value) diff --git a/ml_dht/Main.cs b/ml_dht/Main.cs index 549c227..6466b6e 100644 --- a/ml_dht/Main.cs +++ b/ml_dht/Main.cs @@ -11,7 +11,7 @@ namespace ml_dht byte[] m_buffer = null; TrackingData m_trackingData; - FaceTracked m_localTracked = null; + HeadTracked m_localTracked = null; public override void OnApplicationStart() { @@ -59,7 +59,7 @@ namespace ml_dht while(PlayerSetup.Instance == null) yield return null; - m_localTracked = PlayerSetup.Instance.gameObject.AddComponent(); + m_localTracked = PlayerSetup.Instance.gameObject.AddComponent(); m_localTracked.SetEnabled(Settings.Enabled); m_localTracked.SetMirrored(Settings.Mirrored); m_localTracked.SetSmoothing(Settings.Smoothing); diff --git a/ml_dht/Properties/AssemblyInfo.cs b/ml_dht/Properties/AssemblyInfo.cs index 3de679d..f05061d 100644 --- a/ml_dht/Properties/AssemblyInfo.cs +++ b/ml_dht/Properties/AssemblyInfo.cs @@ -1,10 +1,10 @@ using System.Reflection; [assembly: AssemblyTitle("DesktopHeadTracking")] -[assembly: AssemblyVersion("1.0.3")] -[assembly: AssemblyFileVersion("1.0.3")] +[assembly: AssemblyVersion("1.0.4")] +[assembly: AssemblyFileVersion("1.0.4")] -[assembly: MelonLoader.MelonInfo(typeof(ml_dht.DesktopHeadTracking), "DesktopHeadTracking", "1.0.3", "SDraw", "https://github.com/SDraw/ml_mods_cvr")] +[assembly: MelonLoader.MelonInfo(typeof(ml_dht.DesktopHeadTracking), "DesktopHeadTracking", "1.0.4", "SDraw", "https://github.com/SDraw/ml_mods_cvr")] [assembly: MelonLoader.MelonGame(null, "ChilloutVR")] [assembly: MelonLoader.MelonPlatform(MelonLoader.MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)] [assembly: MelonLoader.MelonPlatformDomain(MelonLoader.MelonPlatformDomainAttribute.CompatibleDomains.MONO)] \ No newline at end of file diff --git a/ml_dht/Utils.cs b/ml_dht/Utils.cs new file mode 100644 index 0000000..7d2651c --- /dev/null +++ b/ml_dht/Utils.cs @@ -0,0 +1,12 @@ +using UnityEngine; + +namespace ml_dht +{ + static class Utils + { + public static Matrix4x4 GetMatrix(this Transform p_transform, bool p_pos = true, bool p_rot = true, bool p_scl = false) + { + return Matrix4x4.TRS(p_pos ? p_transform.position : Vector3.zero, p_rot ? p_transform.rotation : Quaternion.identity, p_scl ? p_transform.localScale : Vector3.one); + } + } +} diff --git a/ml_dht/ml_dht.csproj b/ml_dht/ml_dht.csproj index 8572b09..76ca014 100644 --- a/ml_dht/ml_dht.csproj +++ b/ml_dht/ml_dht.csproj @@ -73,13 +73,14 @@ - + +