From aca8c8057336be27fbe181baa73cf7af5b0bdf59 Mon Sep 17 00:00:00 2001 From: CroakFang <1605296300@qq.com> Date: Wed, 2 Nov 2022 13:47:38 +0800 Subject: [PATCH] Add eye tracking --- Assets/Resources/clip_2.anim | 2 +- Assets/Scenes/Version2.unity | 4 +-- Assets/Scripts/UmaContainer.cs | 21 ++++++++++++ Assets/Scripts/UmaViewerBuilder.cs | 1 + .../umamusume/Gallop/FaceDrivenKeyTarget.cs | 34 ++++++++++++++----- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Assets/Resources/clip_2.anim b/Assets/Resources/clip_2.anim index 3b5bcb6..20a7bb2 100644 --- a/Assets/Resources/clip_2.anim +++ b/Assets/Resources/clip_2.anim @@ -7,7 +7,7 @@ AnimationClip: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_Name: clip_2 - serializedVersion: 7 + serializedVersion: 6 m_Legacy: 0 m_Compressed: 0 m_UseHighQualityCurve: 1 diff --git a/Assets/Scenes/Version2.unity b/Assets/Scenes/Version2.unity index c178e06..de1f78e 100644 --- a/Assets/Scenes/Version2.unity +++ b/Assets/Scenes/Version2.unity @@ -19980,8 +19980,8 @@ MonoBehaviour: m_TargetGraphic: {fileID: 1882365343} m_HandleRect: {fileID: 1882365342} m_Direction: 2 - m_Value: 0 - m_Size: 1 + m_Value: 0.999995 + m_Size: 0.9765434 m_NumberOfSteps: 0 m_OnValueChanged: m_PersistentCalls: diff --git a/Assets/Scripts/UmaContainer.cs b/Assets/Scripts/UmaContainer.cs index 1ca461f..1effd40 100644 --- a/Assets/Scripts/UmaContainer.cs +++ b/Assets/Scripts/UmaContainer.cs @@ -22,6 +22,9 @@ public class UmaContainer : MonoBehaviour [Header("Face")] public FaceDrivenKeyTarget FaceDrivenKeyTarget; public FaceEmotionKeyTarget FaceEmotionKeyTarget; + public GameObject HeadBone; + public GameObject TrackTarget; + public float EyeHeight; [Header("Generic")] public bool IsGeneric = false; @@ -32,6 +35,13 @@ public class UmaContainer : MonoBehaviour public bool IsMini = false; public List MiniHeadTextures = new List(); + private void Start() + { + HeadBone = (GameObject)Body.GetComponent()._assetTable.list.FindLast(a => a.Key == "head").Value; + EyeHeight = 0.1f; + TrackTarget = Camera.main.gameObject; + } + public void MergeModel() { if (!Body) return; @@ -124,4 +134,15 @@ public class UmaContainer : MonoBehaviour } } + private void FixedUpdate() + { + if (TrackTarget) + { + var targetPosotion = TrackTarget.transform.position - new Vector3(0, EyeHeight, 0); + var deltaPos = HeadBone.transform.InverseTransformPoint(targetPosotion); + var deltaRotation = HeadBone.transform.InverseTransformDirection(deltaPos.normalized); + var finalRotation = new Vector2(Mathf.Clamp(deltaRotation.x / 0.86f,-1,1), Mathf.Clamp(deltaRotation.y / 0.86f, -1, 1));//Limited to the angle of view + FaceDrivenKeyTarget.SetEyeRange(finalRotation.x, finalRotation.y, finalRotation.x, -finalRotation.y); + } + } } diff --git a/Assets/Scripts/UmaViewerBuilder.cs b/Assets/Scripts/UmaViewerBuilder.cs index cf5cf47..2e5b3d5 100644 --- a/Assets/Scripts/UmaViewerBuilder.cs +++ b/Assets/Scripts/UmaViewerBuilder.cs @@ -278,6 +278,7 @@ public class UmaViewerBuilder : MonoBehaviour FaceDriven.Initialize(firsehead.GetComponentsInChildren().ToList()); var emotionDriven = ScriptableObject.CreateInstance(); + emotionDriven.name = $"char{id}_{costumeId}_emotion_target"; CurrentUMAContainer.FaceEmotionKeyTarget = emotionDriven; emotionDriven.FaceDrivenKeyTarget = FaceDriven; emotionDriven.FaceEmotionKey = UmaDatabaseController.Instance.FaceTypeData.ToList(); diff --git a/Assets/Scripts/umamusume/Gallop/FaceDrivenKeyTarget.cs b/Assets/Scripts/umamusume/Gallop/FaceDrivenKeyTarget.cs index 643e12e..def5226 100644 --- a/Assets/Scripts/umamusume/Gallop/FaceDrivenKeyTarget.cs +++ b/Assets/Scripts/umamusume/Gallop/FaceDrivenKeyTarget.cs @@ -1,9 +1,7 @@ -using System.Collections; -using System.Collections.Generic; -using System.Text; -using UnityEngine; using Gallop.Live.Cutt; using System; +using System.Collections.Generic; +using UnityEngine; namespace Gallop { @@ -17,6 +15,7 @@ namespace Gallop private FacialMorph BaseLEyeBrowMorph, BaseREyeBrowMorph; private FacialMorph BaseLEyeMorph, BaseREyeMorph; private FacialMorph BaseMouthMorph; + public FacialMorph LeftEyeXrange, LeftEyeYrange, RightEyeXrange, RightEyeYrange; public List EyeBrowMorphs = new List(); public List EyeMorphs = new List(); @@ -47,8 +46,8 @@ namespace Gallop } if (i == 0) { - if (morph.direction) BaseREyeBrowMorph = morph; - else BaseLEyeBrowMorph = morph; + if (morph.direction) BaseLEyeBrowMorph = morph; + else BaseREyeBrowMorph = morph; } else { @@ -73,11 +72,22 @@ namespace Gallop } if (i == 0) { - if (morph.direction) BaseREyeMorph = morph; - else BaseLEyeMorph = morph; + if (morph.direction) BaseLEyeMorph = morph; + else BaseREyeMorph = morph; } else { + if (i == 20) + { + if (morph.direction) LeftEyeXrange = morph; + else RightEyeXrange = morph; + } + else if (i == 21) + { + if (morph.direction) LeftEyeYrange = morph; + else RightEyeYrange = morph; + } + EyeMorphs.Add(morph); } } @@ -206,6 +216,14 @@ namespace Gallop ChangeMorph(); } + public void SetEyeRange(float lx, float ly,float rx, float ry) + { + LeftEyeXrange.weight = lx; + LeftEyeYrange.weight = ly; + RightEyeXrange.weight = rx; + RightEyeYrange.weight = ry; + ChangeMorph(); + } } }