TongueCtrl.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using kt.Utility;
  5. using UnityEngine;
  6. public class TongueCtrl
  7. {
  8. public TongueCtrl(TBodySkin tgt_face, GameObject face_obj)
  9. {
  10. TongueCtrl $this = this;
  11. this.face = tgt_face;
  12. this.body = this.face.body;
  13. Action<string> action = delegate(string bone_name)
  14. {
  15. Transform transform = face_obj.transform.SearchChildByName(bone_name);
  16. $this.BoneDataDic[transform] = new TongueCtrl.BoneData(transform);
  17. };
  18. action("Bero1");
  19. action("Bero2");
  20. action("Bero3");
  21. action("Bero4");
  22. action("Bero5");
  23. action("Bero_nub");
  24. foreach (TongueCtrl.FaceMorph faceMorph in (TongueCtrl.FaceMorph[])Enum.GetValues(typeof(TongueCtrl.FaceMorph)))
  25. {
  26. this.FaceMorphDataDic[faceMorph] = new TongueCtrl.MorphData(faceMorph, this.face.morph as TMorphSkin);
  27. }
  28. }
  29. public void TongueReset()
  30. {
  31. this.LastIsEnable = this.isEnable;
  32. this.isEnable = false;
  33. this.IsFirstFrame = true;
  34. this.BlendStatusReset();
  35. foreach (TongueCtrl.BoneData boneData in this.BoneDataDic.Values)
  36. {
  37. Transform bone = boneData.bone;
  38. boneData.chacheLocalPos = bone.localPosition;
  39. boneData.chacheLocalAngles = bone.localEulerAngles;
  40. bone.localPosition = boneData.initLocalPos;
  41. bone.localEulerAngles = boneData.initLocalAngles;
  42. }
  43. foreach (KeyValuePair<TongueCtrl.FaceMorph, TongueCtrl.MorphData> keyValuePair in this.FaceMorphDataDic)
  44. {
  45. TongueCtrl.MorphData value = keyValuePair.Value;
  46. value.SaveBlendBaseValue();
  47. if (TongueCtrl.TongueUseMorph.Contains(keyValuePair.Key))
  48. {
  49. value.targetValue = 0f;
  50. }
  51. }
  52. }
  53. public void BlendStatusReset()
  54. {
  55. this.BlendWeight = 0f;
  56. this.ElapsedTime = 0f;
  57. }
  58. public void SetTargetValue(TongueCtrl.FaceMorph morph, float value)
  59. {
  60. this.GetMorphData(morph).targetValue = value;
  61. }
  62. public TongueCtrl.MorphData GetMorphData(TongueCtrl.FaceMorph morph)
  63. {
  64. return this.FaceMorphDataDic[morph];
  65. }
  66. public void TongueUpdate()
  67. {
  68. bool flag = this.BlendWeight < 1f;
  69. if (flag)
  70. {
  71. this.ElapsedTime += Time.deltaTime;
  72. this.BlendWeight = ((this.blendTime <= 0f) ? 1f : Mathf.Clamp01(this.ElapsedTime / this.blendTime));
  73. }
  74. if (this.isEnable)
  75. {
  76. foreach (TongueCtrl.FaceMorph key in TongueCtrl.TongueNotUseMorph)
  77. {
  78. this.FaceMorphDataDic[key].targetValue = 0f;
  79. this.FaceMorphDataDic[key].Blend(this.BlendWeight);
  80. }
  81. foreach (TongueCtrl.FaceMorph key2 in TongueCtrl.TongueUseMorph)
  82. {
  83. this.FaceMorphDataDic[key2].Blend(this.BlendWeight);
  84. }
  85. bool boBallGAG = this.face.morph.boBallGAG;
  86. bool boLookTooth = this.face.morph.boLookTooth;
  87. bool boLipSync = this.face.morph.boLipSync;
  88. this.face.morph.boBallGAG = false;
  89. this.face.morph.boLookTooth = false;
  90. this.face.morph.boLipSync = false;
  91. this.face.morph.FixBlendValues_Face();
  92. this.face.morph.boBallGAG = boBallGAG;
  93. this.face.morph.boLookTooth = boLookTooth;
  94. this.face.morph.boLipSync = boLipSync;
  95. }
  96. else
  97. {
  98. if (!flag || !this.LastIsEnable)
  99. {
  100. this.IsFirstFrame = false;
  101. return;
  102. }
  103. if (this.IsFirstFrame)
  104. {
  105. foreach (TongueCtrl.FaceMorph key3 in TongueCtrl.TongueUseMorph)
  106. {
  107. this.FaceMorphDataDic[key3].targetValue = this.FaceMorphDataDic[key3].morphValue;
  108. }
  109. }
  110. foreach (TongueCtrl.FaceMorph key4 in TongueCtrl.TongueUseMorph)
  111. {
  112. this.FaceMorphDataDic[key4].Blend(this.BlendWeight);
  113. }
  114. this.face.morph.FixBlendValues_Face();
  115. foreach (TongueCtrl.BoneData boneData in this.BoneDataDic.Values)
  116. {
  117. Transform bone = boneData.bone;
  118. Vector3 localPosition = Vector3.Lerp(boneData.chacheLocalPos, boneData.initLocalPos, this.BlendWeight);
  119. Vector3 localEulerAngles = Vector3.Lerp(boneData.chacheLocalAngles, boneData.initLocalAngles, this.BlendWeight);
  120. bone.localPosition = localPosition;
  121. bone.localEulerAngles = localEulerAngles;
  122. }
  123. }
  124. this.IsFirstFrame = false;
  125. }
  126. public static readonly TongueCtrl.FaceMorph[] TongueNotUseMorph = new TongueCtrl.FaceMorph[]
  127. {
  128. TongueCtrl.FaceMorph.TangA,
  129. TongueCtrl.FaceMorph.TangOpen,
  130. TongueCtrl.FaceMorph.TangOut,
  131. TongueCtrl.FaceMorph.TangS,
  132. TongueCtrl.FaceMorph.TangUp
  133. };
  134. public static readonly TongueCtrl.FaceMorph[] TongueUseMorph = new TongueCtrl.FaceMorph[]
  135. {
  136. TongueCtrl.FaceMorph.TangRoll,
  137. TongueCtrl.FaceMorph.TangWidth,
  138. TongueCtrl.FaceMorph.TangI,
  139. TongueCtrl.FaceMorph.MouthFera,
  140. TongueCtrl.FaceMorph.MouthFeraR,
  141. TongueCtrl.FaceMorph.MouthA,
  142. TongueCtrl.FaceMorph.MouthS
  143. };
  144. public readonly TBody body;
  145. public readonly TBodySkin face;
  146. private readonly Dictionary<TongueCtrl.FaceMorph, TongueCtrl.MorphData> FaceMorphDataDic = new Dictionary<TongueCtrl.FaceMorph, TongueCtrl.MorphData>();
  147. public bool isEnable;
  148. private bool LastIsEnable;
  149. private float ElapsedTime;
  150. public float blendTime = 0.5f;
  151. private float BlendWeight;
  152. private Dictionary<Transform, TongueCtrl.BoneData> BoneDataDic = new Dictionary<Transform, TongueCtrl.BoneData>();
  153. private bool IsFirstFrame;
  154. public enum FaceMorph
  155. {
  156. TangRoll,
  157. TangWidth,
  158. TangA,
  159. TangI,
  160. TangOpen,
  161. TangOut,
  162. TangS,
  163. TangUp,
  164. MouthFera,
  165. MouthFeraR,
  166. MouthA,
  167. MouthS
  168. }
  169. public class MorphData
  170. {
  171. public MorphData(TongueCtrl.FaceMorph morph_type, TMorphSkin morph_skin)
  172. {
  173. this.morphName = morph_type.ToString().ToLower();
  174. this.morph = morph_skin;
  175. }
  176. public int morphIndex
  177. {
  178. get
  179. {
  180. return (int)this.morph.hash[this.morphName];
  181. }
  182. }
  183. public float morphValue
  184. {
  185. get
  186. {
  187. return this.morph.GetBlendValues(this.morphIndex);
  188. }
  189. private set
  190. {
  191. this.morph.SetBlendValues(this.morphIndex, value);
  192. }
  193. }
  194. public void SaveBlendBaseValue()
  195. {
  196. this.BlendBaseValue = this.morph.GetBlendValues(this.morphIndex);
  197. }
  198. public void Blend(float weight)
  199. {
  200. float morphValue = Mathf.Lerp(this.BlendBaseValue, this.targetValue, weight);
  201. this.morphValue = morphValue;
  202. }
  203. public void ApplyTargetValue()
  204. {
  205. this.morphValue = this.targetValue;
  206. }
  207. public readonly TMorphSkin morph;
  208. public readonly string morphName;
  209. private float BlendBaseValue;
  210. public float targetValue;
  211. }
  212. private class BoneData
  213. {
  214. public BoneData(Transform tongue_bone)
  215. {
  216. this.bone = tongue_bone;
  217. this.initLocalPos = this.bone.localPosition;
  218. this.initLocalAngles = this.bone.localEulerAngles;
  219. }
  220. public readonly Transform bone;
  221. public readonly Vector3 initLocalPos;
  222. public readonly Vector3 initLocalAngles;
  223. public Vector3 chacheLocalPos;
  224. public Vector3 chacheLocalAngles;
  225. }
  226. }