Add Extrar Morph Support(WIP)

This commit is contained in:
CroakFang
2022-11-18 20:44:46 +08:00
parent 379de0c072
commit fece4442a9
11 changed files with 540 additions and 641 deletions
@@ -8,6 +8,8 @@ namespace Gallop
{
public class FaceDrivenKeyTarget : ScriptableObject
{
public UmaContainer Container;
public List<EyeTarget> _eyeTarget;
public List<EyebrowTarget> _eyebrowTarget;
public List<MouthTarget> _mouthTarget;
@@ -20,18 +22,19 @@ namespace Gallop
private FacialMorph BaseMouthMorph;
public FacialMorph LeftEyeXrange, LeftEyeYrange, RightEyeXrange, RightEyeYrange;
public List<FacialMorph> EarMorphs = new List<FacialMorph>();
public List<FacialMorph> EyeBrowMorphs = new List<FacialMorph>();
public List<FacialMorph> EyeMorphs = new List<FacialMorph>();
public List<FacialMorph> MouthMorphs = new List<FacialMorph>();
public List<FacialExtraMorph> OtherMorphs = new List<FacialExtraMorph>();
public GameObject DrivenKeyLocator;
public GameObject EyeballCtrl_L_Locator, EyeballCtrl_R_Locator, EyeballCtrl_All_Locator;
public UmaContainer Container;
public Transform DrivenKeyLocator;
public Transform EyeballCtrl_L_Locator, EyeballCtrl_R_Locator, EyeballCtrl_All_Locator;
Dictionary<Transform, Vector3> RotationRecorder = new Dictionary<Transform, Vector3>();
Dictionary<Particle, Vector3> ParticleRecorder = new Dictionary<Particle, Vector3>();
public void Initialize(List<Transform> objs)
{
Objs = objs;
@@ -50,7 +53,6 @@ namespace Gallop
for (int j = 0; j < _earTarget[i]._faceGroupInfo.Count; j++)
{
FacialMorph morph = new FacialMorph();
morph.target = this;
morph.direction = j > 0;
morph.name = "Ear_" + i + "_" + (morph.direction ? "L" : "R");
morph.tag = Enum.GetName(typeof(EarType), i);
@@ -82,7 +84,6 @@ namespace Gallop
for (int j = 0; j < _eyebrowTarget[i]._faceGroupInfo.Count; j++)
{
FacialMorph morph = new FacialMorph();
morph.target = this;
morph.direction = j > 0;
morph.name = "EyeBrow_" + i + "_" + (morph.direction ? "L" : "R");
morph.tag = Enum.GetName(typeof(LiveTimelineDefine.FacialEyebrowId), i);
@@ -108,7 +109,6 @@ namespace Gallop
for (int j = 0; j < _eyeTarget[i]._faceGroupInfo.Count; j++)
{
FacialMorph morph = new FacialMorph();
morph.target = this;
morph.direction = j > 0;
morph.name = "Eye_" + i + "_" + (morph.direction ? "L" : "R");
morph.tag = Enum.GetName(typeof(LiveTimelineDefine.FacialEyeId), i);
@@ -145,7 +145,6 @@ namespace Gallop
for (int j = 0; j < _mouthTarget[i]._faceGroupInfo.Count; j++)
{
FacialMorph morph = new FacialMorph();
morph.target = this;
morph.direction = j > 0;
morph.name = "Mouth_" + i + "_" + j;
morph.tag = Enum.GetName(typeof(LiveTimelineDefine.FacialMouthId), i);
@@ -165,105 +164,168 @@ namespace Gallop
}
}
ChangeMorphWeight(MouthMorphs[3], 1);
InitializeExtra();
SetupAnimator();
if (UmaViewerUI.Instance)
{
UmaViewerUI.Instance.LoadFacialPanels(this);
}
public void InitializeExtra()
{
if (Container.CheekTex_0)
{
var CheekMorph = new FacialExtraMorph
{
name = "Cheek_Ctrl",
locator = DrivenKeyLocator.Find("Cheek_Ctrl"),
BindGameObject = Container.Head.transform.Find("M_Cheek").gameObject
};
var bindMaterial = CheekMorph.BindGameObject.GetComponent<SkinnedMeshRenderer>().material;
BindProperty cheekProperty = new BindProperty()
{
BindMaterial = bindMaterial,
Part = BindProperty.LocatorPart.ScaX,
Type = BindProperty.BindType.Texture,
BindTexture = Container.CheekTex_0,
PropertyName = "_CheekRate"
};
CheekMorph.BindProperties.Add(cheekProperty);
if (Container.CheekTex_1)
{
BindProperty cheekProperty1 = new BindProperty()
{
BindMaterial = bindMaterial,
Part = BindProperty.LocatorPart.ScaY,
Type = BindProperty.BindType.Texture,
BindTexture = Container.CheekTex_1,
PropertyName = "_CheekRate"
};
CheekMorph.BindProperties.Add(cheekProperty1);
}
if (Container.CheekTex_2)
{
BindProperty cheekProperty2 = new BindProperty()
{
BindMaterial = bindMaterial,
Part = BindProperty.LocatorPart.ScaZ,
Type = BindProperty.BindType.Texture,
BindTexture = Container.CheekTex_2,
PropertyName = "_CheekRate"
};
CheekMorph.BindProperties.Add(cheekProperty2);
}
OtherMorphs.Add(CheekMorph);
}
Container.FaceMaterial = Container.Head.transform.Find("M_Face").GetComponent<SkinnedMeshRenderer>().material;
if (Container.FaceMaterial)
{
FacialExtraMorph FaceShadowMorph = new FacialExtraMorph()
{
name = "Shade_Ctrl",
locator = DrivenKeyLocator.Find("Shade_Ctrl")
};
FaceShadowMorph.BindProperties.Add(
new BindProperty()
{
BindMaterial = Container.FaceMaterial,
Part = BindProperty.LocatorPart.ScaX,
Type = BindProperty.BindType.Shader,
PropertyName = "_faceShadowAlpha"
});
OtherMorphs.Add(FaceShadowMorph);
}
FacialExtraMorph FaceMangaMorph = new FacialExtraMorph()
{
name = "Manga_Ctrl",
locator = DrivenKeyLocator.Find("Manga_Ctrl")
};
var mangaObjects = new List<GameObject>();
mangaObjects.AddRange(Container.LeftMangaObject);
mangaObjects.AddRange(Container.RightMangaObject);
FaceMangaMorph.BindProperties.Add(new BindProperty()
{
Part = BindProperty.LocatorPart.ScaY,
Type = BindProperty.BindType.EyeSelect,
BindPrefab = mangaObjects
});
FaceMangaMorph.BindProperties.Add(new BindProperty()
{
Part = BindProperty.LocatorPart.ScaX,
Type = BindProperty.BindType.Bool,
BindPrefab = mangaObjects
});
OtherMorphs.Add(FaceMangaMorph);
}
public void SetupAnimator()
{
DrivenKeyLocator = new GameObject("DrivenKeyLocator");
DrivenKeyLocator.transform.SetParent(Container.transform);
SetupLocator("Ear_L_Ctrl", "Ear_L__", EarMorphs.FindAll(a => a.direction == true));
SetupLocator("Ear_R_Ctrl", "Ear_R__", EarMorphs.FindAll(a => a.direction == false));
SetupLocator("Eye_L_Base_Ctrl", "Eye_L__", EyeMorphs.FindAll(a => a.direction == true));
SetupLocator("Eye_R_Base_Ctrl", "Eye_R__", EyeMorphs.FindAll(a => a.direction == false));
SetupLocator("Eyebrow_L_Base_Ctrl", "Eyebrow_L__", EyeBrowMorphs.FindAll(a => a.direction == true));
SetupLocator("Eyebrow_R_Base_Ctrl", "Eyebrow_R__", EyeBrowMorphs.FindAll(a => a.direction == false));
SetupLocator("Mouth_Base_Ctrl", "Mouth__", MouthMorphs.FindAll(a => a.direction == false));
if (!DrivenKeyLocator) return;
var root = new GameObject("Eyeball_L_Ctrl");
root.transform.SetParent(DrivenKeyLocator.transform);
EyeballCtrl_L_Locator = root;
root = new GameObject("Eyeball_R_Ctrl");
root.transform.SetParent(DrivenKeyLocator.transform);
EyeballCtrl_R_Locator = root;
root = new GameObject("Eyeball_all_Ctrl");
root.transform.SetParent(DrivenKeyLocator.transform);
EyeballCtrl_All_Locator = root;
SetupLocator("Ear_L_Ctrl", EarMorphs.FindAll(a => a.direction == true));
SetupLocator("Ear_R_Ctrl", EarMorphs.FindAll(a => a.direction == false));
SetupLocator("Eye_L_Base_Ctrl", EyeMorphs.FindAll(a => a.direction == true));
SetupLocator("Eye_R_Base_Ctrl", EyeMorphs.FindAll(a => a.direction == false));
SetupLocator("Eyebrow_L_Base_Ctrl", EyeBrowMorphs.FindAll(a => a.direction == true));
SetupLocator("Eyebrow_R_Base_Ctrl", EyeBrowMorphs.FindAll(a => a.direction == false));
SetupLocator("Mouth_Base_Ctrl", MouthMorphs.FindAll(a => a.direction == false));
Container.UmaFaceAnimator = DrivenKeyLocator.AddComponent<Animator>();
Container.UmaFaceAnimator.avatar = AvatarBuilder.BuildGenericAvatar(DrivenKeyLocator, "DrivenKeyLocator");
EyeballCtrl_L_Locator = DrivenKeyLocator.transform.Find("Eyeball_L_Ctrl");
EyeballCtrl_R_Locator = DrivenKeyLocator.transform.Find("Eyeball_R_Ctrl");
EyeballCtrl_All_Locator = DrivenKeyLocator.transform.Find("Eyeball_all_Ctrl");
Container.UmaFaceAnimator = DrivenKeyLocator.GetComponent<Animator>();
Container.UmaFaceAnimator.avatar = AvatarBuilder.BuildGenericAvatar(DrivenKeyLocator.gameObject, "DrivenKeyLocator");
Container.FaceOverrideController = Instantiate(UmaViewerBuilder.Instance.FaceOverrideController);
Container.UmaFaceAnimator.runtimeAnimatorController = Container.FaceOverrideController;
}
public void SetupLocator(string rootName, string prefix, List<FacialMorph> morphs)
public void SetupLocator(string rootName, List<FacialMorph> morphs)
{
var root = new GameObject(rootName);
root.transform.SetParent(DrivenKeyLocator.transform);
var root = DrivenKeyLocator.transform.Find(rootName);
var locators = new List<Transform>(root.GetComponentsInChildren<Transform>());
morphs.ForEach(morph =>
{
var locator = new GameObject(prefix + morph.tag);
locator.transform.SetParent(root.transform);
morph.locator = locator.transform;
var locator = locators.Find(a => a.name.EndsWith(morph.tag));
if (locator)
{
morph.locator = locator;
}
});
}
public void ChangeMorph()
{
FacialResetAll();
foreach (FacialMorph morph in EarMorphs)
{
ProcessMorph(morph);
}
foreach (FacialMorph morph in EyeBrowMorphs)
{
ProcessMorph(morph);
}
foreach (FacialMorph morph in EyeMorphs)
{
ProcessMorph(morph);
}
foreach (FacialMorph morph in MouthMorphs)
{
ProcessMorph(morph);
}
EarMorphs.ForEach(morph => ProcessMorph(morph));
EyeBrowMorphs.ForEach(morph => ProcessMorph(morph));
EyeMorphs.ForEach(morph => ProcessMorph(morph));
MouthMorphs.ForEach(morph => ProcessMorph(morph));
OtherMorphs.ForEach(morph => ProcessExtraMorph(morph));
ApplyRotation();
}
public void ClearMorph()
{
foreach (FacialMorph morph in EarMorphs)
{
morph.weight = 0;
}
EarMorphs.ForEach(morph => morph.weight = 0);
EyeBrowMorphs.ForEach(morph => morph.weight = 0);
EyeMorphs.ForEach(morph => morph.weight = 0);
MouthMorphs.ForEach(morph => morph.weight = 0);
foreach (FacialMorph morph in EyeBrowMorphs)
OtherMorphs.ForEach(morph =>
{
morph.weight = 0;
}
foreach (FacialMorph morph in EyeMorphs)
{
morph.weight = 0;
}
foreach (FacialMorph morph in MouthMorphs)
{
morph.weight = 0;
}
morph.BindProperties.ForEach(property =>
{
property.Value = morph.GetLocatorValue(property.Part);
});
});
}
private void ProcessMorph(FacialMorph morph)
@@ -275,7 +337,7 @@ namespace Gallop
trs.physicsParticle.m_InitLocalPosition += trs._position * morph.weight;
ParticleRecorder[trs.physicsParticle] += trs._rotation * morph.weight;
}
else if(trs.transform)
else if (trs.transform)
{
trs.transform.localScale += trs._scale * morph.weight;
trs.transform.localPosition += trs._position * morph.weight;
@@ -284,6 +346,50 @@ namespace Gallop
}
}
private void ProcessExtraMorph(FacialExtraMorph morph)
{
foreach (var property in morph.BindProperties)
{
switch (property.Type)
{
case BindProperty.BindType.Texture:
if (property.Value > 0.01f)
{
morph.BindGameObject.SetActive(true);
property.BindMaterial.mainTexture = property.BindTexture;
property.BindMaterial.SetFloat(property.PropertyName, property.Value);
return;
}
else
{
if (morph.BindProperties.IndexOf(property) == morph.BindProperties.Count - 1)
{
morph.BindGameObject.SetActive(false);
}
}
break;
case BindProperty.BindType.Shader:
property.BindMaterial.SetFloat(property.PropertyName, property.Value);
break;
case BindProperty.BindType.Select:
property.BindPrefab.ForEach(a=>a.SetActive((int)property.Value == property.BindPrefab.IndexOf(a)));
break;
case BindProperty.BindType.EyeSelect:
var count = property.BindPrefab.Count / 2;
var val = (int)property.Value;
property.BindPrefab.ForEach(a =>
a.SetActive(val + count < property.BindPrefab.Count && (val == property.BindPrefab.IndexOf(a) || val + count == property.BindPrefab.IndexOf(a))));
break;
case BindProperty.BindType.Bool:
if (property.Value <= 0)
{
property.BindPrefab.ForEach(a =>a.SetActive(false));
}
break;
}
}
}
public void FacialResetAll()
{
FacialReset(BaseLEarMorph.trsArray);
@@ -336,6 +442,13 @@ namespace Gallop
ChangeMorph();
}
public void ChangeMorphWeight(BindProperty property, float val)
{
Container.isAnimatorControl = false;
property.Value = val < 0 ? 0 : val;
ChangeMorph();
}
public void SetEyeRange(float lx, float ly, float rx, float ry)
{
LeftEyeXrange.weight = lx;
@@ -349,26 +462,32 @@ namespace Gallop
{
EyeBrowMorphs.ForEach(morph =>
{
morph.weight = morph.locator.transform.localPosition.x * -100;
if (morph.locator)
morph.weight = morph.locator.transform.localPosition.x * -100;
});
EarMorphs.ForEach(morph =>
{
morph.weight = morph.locator.transform.localPosition.x * -100;
if (morph.locator)
morph.weight = morph.locator.transform.localPosition.x * -100;
});
MouthMorphs.ForEach(morph =>
{
morph.weight = morph.locator.transform.localPosition.x * -100;
if (morph.weight < 0)
morph.weight = -morph.weight;
if (morph.locator)
{
morph.weight = morph.locator.transform.localPosition.x * -100;
if (morph.weight < 0)
morph.weight = -morph.weight;
}
});
EyeMorphs.ForEach(morph =>
{
if (!(morph == LeftEyeXrange || morph == LeftEyeYrange || morph == RightEyeXrange || morph == RightEyeYrange))
{
morph.weight = morph.locator.transform.localPosition.x * -100;
if (morph.locator)
morph.weight = morph.locator.transform.localPosition.x * -100;
}
});
@@ -378,39 +497,20 @@ namespace Gallop
LeftEyeYrange.weight = -(EyeballCtrl_L_Locator.transform.localPosition.y * -100 + EyeballCtrl_All_Locator.transform.localPosition.y * -100);
RightEyeYrange.weight = (EyeballCtrl_R_Locator.transform.localPosition.y * -100 + EyeballCtrl_All_Locator.transform.localPosition.y * -100);
OtherMorphs.ForEach(morph =>
{
morph.BindProperties.ForEach(property =>
{
property.Value = morph.GetLocatorValue(property.Part);
});
});
ChangeMorph();
}
public void ResetLocator()
{
EarMorphs.ForEach(morph =>
{
morph.weight = 0;
morph.locator.transform.localPosition = Vector3.zero;
});
EyeBrowMorphs.ForEach(morph =>
{
morph.weight = 0;
morph.locator.transform.localPosition = Vector3.zero;
});
EyeMorphs.ForEach(morph =>
{
morph.weight = 0;
morph.locator.transform.localPosition = Vector3.zero;
});
MouthMorphs.ForEach(morph =>
{
morph.weight = 0;
morph.locator.transform.localPosition = Vector3.zero;
});
EyeballCtrl_L_Locator.transform.localPosition = Vector3.zero;
EyeballCtrl_R_Locator.transform.localPosition = Vector3.zero;
EyeballCtrl_All_Locator.transform.localPosition = Vector3.zero;
Container.UmaFaceAnimator.Rebind();
ChangeMorph();
}
}