Merge duplicate code and optimize code

This commit is contained in:
CroakFang
2022-11-01 13:48:26 +08:00
parent fe4e709b81
commit b96df921bb
9 changed files with 213 additions and 358 deletions
@@ -107,25 +107,26 @@ namespace Gallop
}
}
}
FacialReset();
FacialResetAll();
MouthMorphs[3].Weight = 1;
if (UmaViewerUI.Instance) { UmaViewerUI.Instance.LoadFacialPanels(this); }
if (UmaViewerUI.Instance) { UmaViewerUI.Instance.LoadEmotionPanels(this); }
if (UmaViewerUI.Instance) { UmaViewerUI.Instance.currentMorph = this; }
if (UmaViewerUI.Instance)
{
UmaViewerUI.Instance.LoadFacialPanels(this);
}
}
public void ChangeMorph()
{
FacialReset();
FacialResetAll();
foreach (FacialMorph morph in EyeBrowMorphs)
{
ProcessMorph(morph);
ProcessMorph(morph);
}
foreach (FacialMorph morph in EyeMorphs)
{
ProcessMorph(morph);
ProcessMorph(morph);
}
foreach (FacialMorph morph in MouthMorphs)
@@ -167,76 +168,37 @@ namespace Gallop
}
}
public void FacialReset()
public void FacialResetAll()
{
foreach (TrsArray trs in BaseLEyeBrowMorph.trsArray)
{
if (trs.transform)
{
trs.transform.localPosition = trs._position;
trs.transform.localScale = trs._scale;
RotationRecorder[trs.transform] = trs._rotation;
};
}
foreach (TrsArray trs in BaseREyeBrowMorph.trsArray)
{
if (trs.transform)
{
trs.transform.localPosition = trs._position;
trs.transform.localScale = trs._scale;
RotationRecorder[trs.transform] = trs._rotation;
};
}
foreach (TrsArray trs in BaseLEyeMorph.trsArray)
{
if (trs.transform)
{
trs.transform.localPosition = trs._position;
trs.transform.localScale = trs._scale;
RotationRecorder[trs.transform] = trs._rotation;
};
}
foreach (TrsArray trs in BaseREyeMorph.trsArray)
{
if (trs.transform)
{
trs.transform.localPosition = trs._position;
trs.transform.localScale = trs._scale;
RotationRecorder[trs.transform] = trs._rotation;
};
}
foreach (TrsArray trs in BaseMouthMorph.trsArray)
{
if (trs.transform)
{
trs.transform.localPosition = trs._position;
trs.transform.localScale = trs._scale;
RotationRecorder[trs.transform] = trs._rotation;
};
}
FacialReset(BaseLEyeBrowMorph.trsArray);
FacialReset(BaseREyeBrowMorph.trsArray);
FacialReset(BaseLEyeMorph.trsArray);
FacialReset(BaseREyeMorph.trsArray);
FacialReset(BaseMouthMorph.trsArray);
ApplyRotation();
}
public void ApplyRotation(){
public void FacialReset(List<TrsArray> trsArrays)
{
foreach (TrsArray trs in trsArrays)
{
if (trs.transform)
{
trs.transform.localPosition = trs._position;
trs.transform.localScale = trs._scale;
RotationRecorder[trs.transform] = trs._rotation;
};
}
}
public void ApplyRotation()
{
foreach (var trs in RotationRecorder)
{
trs.Key.localRotation = RotationConvert.fromMaya(trs.Value);
}
}
}
}
}
@@ -0,0 +1,115 @@
using Gallop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
public class FaceEmotionKeyTarget : ScriptableObject
{
public List<FaceTypeData> FaceEmotionKey;
public FaceDrivenKeyTarget FaceDrivenKeyTarget;
public void Initialize()
{
foreach (var emotion in FaceEmotionKey)
{
if (emotion.label == "Base") continue;
emotion.target = this;
InitializeEmotionTarget(ref emotion.mouthTarget,ref emotion.mouthTarget, FaceDrivenKeyTarget.MouthMorphs, emotion.mouth, false);
InitializeEmotionTarget(ref emotion.eyeLTarget, ref emotion.eyeLTarget, FaceDrivenKeyTarget.EyeMorphs, emotion.eye_l, true);
InitializeEmotionTarget(ref emotion.eyeRTarget, ref emotion.eyeRTarget, FaceDrivenKeyTarget.EyeMorphs, emotion.eye_r, false);
InitializeEmotionTarget(ref emotion.eyebrowLTarget, ref emotion.eyebrowLTarget, FaceDrivenKeyTarget.EyeBrowMorphs, emotion.eyebrow_l, true);
InitializeEmotionTarget(ref emotion.eyebrowRTarget, ref emotion.eyebrowRTarget, FaceDrivenKeyTarget.EyeBrowMorphs, emotion.eyebrow_r, false);
}
if (UmaViewerUI.Instance)
{
UmaViewerUI.Instance.LoadEmotionPanels(this);
}
}
private void InitializeEmotionTarget(ref List<EmotionKey> targets, ref List<EmotionKey> emotionKeys,List<FacialMorph> morphs,string tags,bool direction)
{
targets = new List<EmotionKey>();
foreach (var morphName in tags.Split('|'))
{
EmotionKey newValue = new EmotionKey();
if (morphName.Contains("__"))
{
var splitArray = Regex.Split(morphName, "__", RegexOptions.IgnoreCase);
if (splitArray[0] == "Base")
{
continue;
}
else
{
newValue.morph = morphs.Where(a => a.tag == splitArray[0] && a.direction == direction).First();
}
newValue.weight = Convert.ToInt32(splitArray[1]);
emotionKeys.Add(newValue);
Debug.Log(newValue.weight);
}
else
{
if (morphName == "Base")
{
continue;
}
else
{
newValue.morph = morphs.Where(a => a.tag == morphName && a.direction == direction).First();
}
newValue.weight = 100;
emotionKeys.Add(newValue);
Debug.Log(morphName);
}
}
}
public void UpdateAllFacialKeyTargets()
{
if (FaceDrivenKeyTarget == null) return;
FaceDrivenKeyTarget.ClearMorph();
foreach (var emotion in FaceEmotionKey)
{
if (emotion.mouthTarget != null)
{
foreach (var key in emotion.mouthTarget)
{
key.morph.Weight += key.weight / 100 * emotion.Weight;
}
}
if (emotion.eyeLTarget != null)
{
foreach (var key in emotion.eyeLTarget)
{
key.morph.Weight += key.weight / 100 * emotion.Weight;
}
}
if (emotion.eyeRTarget != null)
{
foreach (var key in emotion.eyeRTarget)
{
key.morph.Weight += key.weight / 100 * emotion.Weight;
}
}
if (emotion.eyebrowLTarget != null)
{
foreach (var key in emotion.eyeRTarget)
{
key.morph.Weight += key.weight / 100 * emotion.Weight;
}
}
if (emotion.eyebrowRTarget != null)
{
foreach (var key in emotion.eyeRTarget)
{
key.morph.Weight += key.weight / 100 * emotion.Weight;
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 43ba40a9c767e6e43a635ce59cb7942e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -24,7 +24,7 @@ namespace Gallop
set
{
_weight = value;
target.needUpdate = true;
target.ChangeMorph();
}
}
public List<TrsArray> trsArray;