CubismAutoEyeBlinkInput.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using UnityEngine;
  8. namespace Live2D.Cubism.Framework
  9. {
  10. /// <summary>
  11. /// Automatic mouth movement.
  12. /// </summary>
  13. public sealed class CubismAutoEyeBlinkInput : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// Mean time between eye blinks in seconds.
  17. /// </summary>
  18. [SerializeField, Range(1f, 10f)]
  19. public float Mean = 2.5f;
  20. /// <summary>
  21. /// Maximum deviation from <see cref="Mean"/> in seconds.
  22. /// </summary>
  23. [SerializeField, Range(0.5f, 5f)]
  24. public float MaximumDeviation = 2f;
  25. /// <summary>
  26. /// Timescale.
  27. /// </summary>
  28. [SerializeField, Range(1f, 20f)]
  29. public float Timescale = 10f;
  30. /// <summary>
  31. /// Target controller.
  32. /// </summary>
  33. private CubismEyeBlinkController Controller { get; set; }
  34. /// <summary>
  35. /// Control over whether output should be evaluated.
  36. /// </summary>
  37. private Phase CurrentPhase { get; set; }
  38. /// <summary>
  39. /// Used for switching from <see cref="Phase.ClosingEyes"/> to <see cref="Phase.OpeningEyes"/> and back to <see cref="Phase.Idling"/>.
  40. /// </summary>
  41. private float LastValue { get; set; }
  42. /// <summary>
  43. /// Totalized delta time [s].
  44. /// </summary>
  45. private float UserTimeSeconds { get; set; }
  46. /// <summary>
  47. /// Time when the current state started [sec].
  48. /// </summary>
  49. private float StateStartTimeSeconds { get; set; }
  50. /// <summary>
  51. /// Duration of eyelid closing motion [sec]
  52. /// </summary>
  53. private float ClosingSeconds { get; set; }
  54. /// <summary>
  55. /// Duration of eyelid closed state [sec]
  56. /// </summary>
  57. private float ClosedSeconds { get; set; }
  58. /// <summary>
  59. /// Duration of eyelid opening motion [sec]
  60. /// </summary>
  61. private float OpeningSeconds { get; set; }
  62. /// <summary>
  63. /// Next blinking time.
  64. /// </summary>
  65. private float NextBlinkingTime { get; set; }
  66. /// <summary>
  67. /// Resets the input.
  68. /// </summary>
  69. public void Reset()
  70. {
  71. CurrentPhase = Phase.Idling;
  72. NextBlinkingTime = Mean + Random.Range(-MaximumDeviation, MaximumDeviation);
  73. }
  74. /// <summary>
  75. /// Calculate the value when the eyes are closed.
  76. /// </summary>
  77. /// <returns>Eye closing value.</returns>
  78. private float UpdateEyeBlinkClosing()
  79. {
  80. var t = (UserTimeSeconds - StateStartTimeSeconds) / ClosingSeconds;
  81. if (t >= 1.0f)
  82. {
  83. CurrentPhase = Phase.ClosedEyes;
  84. StateStartTimeSeconds = UserTimeSeconds;
  85. }
  86. var value = 1.0f - t;
  87. return value;
  88. }
  89. /// <summary>
  90. /// Calculate the value when the eyes are closed.
  91. /// </summary>
  92. /// <returns>Eye closed value.</returns>
  93. private float UpdateEyeBlinkClosed()
  94. {
  95. var t = (UserTimeSeconds - StateStartTimeSeconds) / ClosedSeconds;
  96. if (t >= 1.0f)
  97. {
  98. CurrentPhase = Phase.OpeningEyes;
  99. StateStartTimeSeconds = UserTimeSeconds;
  100. }
  101. var value = 0.0f;
  102. return value;
  103. }
  104. /// <summary>
  105. /// Calculate the value when the eyes are opening.
  106. /// </summary>
  107. /// <returns>Eye opening value.</returns>
  108. private float UpdateEyeBlinkOpening()
  109. {
  110. var t = (UserTimeSeconds - StateStartTimeSeconds) / OpeningSeconds;
  111. if (t >= 1.0f)
  112. {
  113. t = 1.0f;
  114. CurrentPhase = Phase.Idling;
  115. NextBlinkingTime = Mean + Random.Range(-MaximumDeviation, MaximumDeviation);
  116. }
  117. var value = t;
  118. return value;
  119. }
  120. /// <summary>
  121. /// Calculate the value when the eyes are opened.
  122. /// </summary>
  123. /// <returns>Eye opened value.</returns>
  124. private float UpdateEyeBlinkIdling()
  125. {
  126. NextBlinkingTime -= Time.deltaTime;
  127. if (NextBlinkingTime < 0.0f)
  128. {
  129. CurrentPhase = Phase.ClosingEyes;
  130. StateStartTimeSeconds = UserTimeSeconds;
  131. }
  132. var value = 1.0f;
  133. return value;
  134. }
  135. /// <summary>
  136. /// Update eye blink.
  137. /// </summary>
  138. private void UpdateEyeBlink()
  139. {
  140. UserTimeSeconds += (Time.deltaTime * Timescale);
  141. var value = 0.0f;
  142. switch (CurrentPhase)
  143. {
  144. case Phase.ClosingEyes:
  145. {
  146. value = UpdateEyeBlinkClosing();
  147. break;
  148. }
  149. case Phase.ClosedEyes:
  150. {
  151. value = UpdateEyeBlinkClosed();
  152. break;
  153. }
  154. case Phase.OpeningEyes:
  155. {
  156. value = UpdateEyeBlinkOpening();
  157. break;
  158. }
  159. case Phase.Idling:
  160. {
  161. value = UpdateEyeBlinkIdling();
  162. break;
  163. }
  164. }
  165. Controller.EyeOpening = value;
  166. }
  167. /// <summary>
  168. /// Set the details of the blinking motion.
  169. /// </summary>
  170. /// <param name="closing">Duration of eyelid closing motion [sec].</param>
  171. /// <param name="closed">Duration of eyelid closed state [sec].</param>
  172. /// <param name="opening">Duration of eyelid opening motion [sec].</param>
  173. public void SetBlinkingSettings(float closing, float closed, float opening)
  174. {
  175. ClosingSeconds = closing;
  176. ClosedSeconds = closed;
  177. OpeningSeconds = opening;
  178. }
  179. #region Unity Event Handling
  180. /// <summary>
  181. /// Called by Unity. Initializes input.
  182. /// </summary>
  183. private void Start()
  184. {
  185. Controller = GetComponent<CubismEyeBlinkController>();
  186. CurrentPhase = Phase.Idling;
  187. NextBlinkingTime = Mean + Random.Range(-MaximumDeviation, MaximumDeviation);
  188. SetBlinkingSettings(1.0f, 0.5f, 1.5f);
  189. }
  190. /// <summary>
  191. /// Called by Unity. Updates controller.
  192. /// </summary>
  193. /// <remarks>
  194. /// Make sure this method is called after any animations are evaluated.
  195. /// </remarks>
  196. private void LateUpdate()
  197. {
  198. // Fail silently.
  199. if (Controller == null)
  200. {
  201. return;
  202. }
  203. UpdateEyeBlink();
  204. }
  205. #endregion
  206. /// <summary>
  207. /// Internal states.
  208. /// </summary>
  209. private enum Phase
  210. {
  211. /// <summary>
  212. /// Idle state.
  213. /// </summary>
  214. Idling,
  215. /// <summary>
  216. /// State when closing eyes.
  217. /// </summary>
  218. ClosingEyes,
  219. /// <summary>
  220. /// State when closed eyes.
  221. /// </summary>
  222. ClosedEyes,
  223. /// <summary>
  224. /// State when opening eyes.
  225. /// </summary>
  226. OpeningEyes
  227. }
  228. }
  229. }