CubismFadeController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 Live2D.Cubism.Core;
  8. using Live2D.Cubism.Framework.Motion;
  9. using UnityEngine;
  10. namespace Live2D.Cubism.Framework.MotionFade
  11. {
  12. /// <summary>
  13. /// Cubism fade controller.
  14. /// </summary>
  15. [RequireComponent(typeof(Animator))]
  16. public class CubismFadeController : MonoBehaviour, ICubismUpdatable
  17. {
  18. #region Variable
  19. /// <summary>
  20. /// Cubism fade motion list.
  21. /// </summary>
  22. [SerializeField]
  23. public CubismFadeMotionList CubismFadeMotionList;
  24. /// <summary>
  25. /// Parameters cache.
  26. /// </summary>
  27. private CubismParameter[] DestinationParameters { get; set; }
  28. /// <summary>
  29. /// Parts cache.
  30. /// </summary>
  31. private CubismPart[] DestinationParts { get; set; }
  32. /// <summary>
  33. /// Model has motion controller component.
  34. /// </summary>
  35. private CubismMotionController _motionController;
  36. /// <summary>
  37. /// Model has cubism update controller component.
  38. /// </summary>
  39. [HideInInspector]
  40. public bool HasUpdateController { get; set; }
  41. /// <summary>
  42. /// Fade state machine behavior set in the animator.
  43. /// </summary>
  44. private ICubismFadeState[] _fadeStates;
  45. /// <summary>
  46. /// Model has animator component.
  47. /// </summary>
  48. private Animator _animator;
  49. /// <summary>
  50. /// Restore parameter value.
  51. /// </summary>
  52. private CubismParameterStore _parameterStore;
  53. /// <summary>
  54. /// Fading flags for each layer.
  55. /// </summary>
  56. private bool[] _isFading;
  57. #endregion
  58. #region Function
  59. /// <summary>
  60. /// Refreshes the controller. Call this method after adding and/or removing <see cref="CubismFadeParameter"/>s.
  61. /// </summary>
  62. public void Refresh()
  63. {
  64. _animator = GetComponent<Animator>();
  65. // Fail silently...
  66. if (_animator == null)
  67. {
  68. return;
  69. }
  70. DestinationParameters = this.FindCubismModel().Parameters;
  71. DestinationParts = this.FindCubismModel().Parts;
  72. _motionController = GetComponent<CubismMotionController>();
  73. _parameterStore = GetComponent<CubismParameterStore>();
  74. // Get cubism update controller.
  75. HasUpdateController = (GetComponent<CubismUpdateController>() != null);
  76. _fadeStates = (ICubismFadeState[])_animator.GetBehaviours<CubismFadeStateObserver>();
  77. if ((_fadeStates == null || _fadeStates.Length == 0) && _motionController != null)
  78. {
  79. _fadeStates = _motionController.GetFadeStates();
  80. }
  81. if (_fadeStates == null)
  82. {
  83. return;
  84. }
  85. _isFading = new bool[_fadeStates.Length];
  86. }
  87. /// <summary>
  88. /// Called by cubism update controller. Order to invoke OnLateUpdate.
  89. /// </summary>
  90. public int ExecutionOrder
  91. {
  92. get { return CubismUpdateExecutionOrder.CubismFadeController; }
  93. }
  94. /// <summary>
  95. /// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing.
  96. /// </summary>
  97. public bool NeedsUpdateOnEditing
  98. {
  99. get { return false; }
  100. }
  101. /// <summary>
  102. /// Called by cubism update controller. Updates controller.
  103. /// </summary>
  104. /// <remarks>
  105. /// Make sure this method is called after any animations are evaluated.
  106. /// </remarks>
  107. public void OnLateUpdate()
  108. {
  109. // Fail silently.
  110. if (!enabled || _fadeStates == null || _parameterStore == null
  111. || DestinationParameters == null || DestinationParts == null)
  112. {
  113. return;
  114. }
  115. var time = Time.time;
  116. for (var i = 0; i < _fadeStates.Length; ++i)
  117. {
  118. _isFading[i] = false;
  119. var playingMotions = _fadeStates[i].GetPlayingMotions();
  120. if (playingMotions == null || playingMotions.Count <= 1)
  121. {
  122. continue;
  123. }
  124. var latestPlayingMotion = playingMotions[playingMotions.Count - 1];
  125. var playingMotionData = latestPlayingMotion.Motion;
  126. var elapsedTime = time - latestPlayingMotion.StartTime;
  127. for (var j = 0; j < playingMotionData.ParameterFadeInTimes.Length; j++)
  128. {
  129. if ((elapsedTime <= playingMotionData.FadeInTime) ||
  130. ((0 <= playingMotionData.ParameterFadeInTimes[j]) &&
  131. (elapsedTime <= playingMotionData.ParameterFadeInTimes[j])))
  132. {
  133. _isFading[i] = true;
  134. break;
  135. }
  136. }
  137. }
  138. var isFadingAllFinished = true;
  139. for (var i = 0; i < _fadeStates.Length; ++i)
  140. {
  141. var playingMotions = _fadeStates[i].GetPlayingMotions();
  142. var playingMotionCount = playingMotions.Count - 1;
  143. if (_isFading[i])
  144. {
  145. isFadingAllFinished = false;
  146. continue;
  147. }
  148. for (var j = playingMotionCount; j >= 0; --j)
  149. {
  150. if (playingMotions.Count <= 1)
  151. {
  152. break;
  153. }
  154. var playingMotion = playingMotions[j];
  155. if (time <= playingMotion.EndTime)
  156. {
  157. continue;
  158. }
  159. // If fade-in has been completed, delete the motion that has been played back.
  160. _fadeStates[i].StopAnimation(j);
  161. }
  162. }
  163. if (isFadingAllFinished)
  164. {
  165. return;
  166. }
  167. _parameterStore.RestoreParameters();
  168. // Update sources and destinations.
  169. for (var i = 0; i < _fadeStates.Length; ++i)
  170. {
  171. if (!_isFading[i])
  172. {
  173. continue;
  174. }
  175. UpdateFade(_fadeStates[i]);
  176. }
  177. }
  178. /// <summary>
  179. /// Update motion fade.
  180. /// </summary>
  181. /// <param name="fadeState">Fade state observer.</param>
  182. private void UpdateFade(ICubismFadeState fadeState)
  183. {
  184. var playingMotions = fadeState.GetPlayingMotions();
  185. if (playingMotions == null)
  186. {
  187. // Do not process if there is only one motion, if it does not switch.
  188. return;
  189. }
  190. // Weight set for the layer being processed.
  191. // (In the case of the layer located at the top, it is forced to 1.)
  192. var layerWeight = fadeState.GetLayerWeight();
  193. var time = Time.time;
  194. // Set playing motions end time.
  195. if ((playingMotions.Count > 0) && (playingMotions[playingMotions.Count - 1].Motion != null) && (playingMotions[playingMotions.Count - 1].IsLooping))
  196. {
  197. var motion = playingMotions[playingMotions.Count - 1];
  198. var newEndTime = time + motion.Motion.FadeOutTime;
  199. motion.EndTime = newEndTime;
  200. while (true)
  201. {
  202. if ((motion.StartTime + motion.Motion.MotionLength) >= time)
  203. {
  204. break;
  205. }
  206. motion.StartTime += motion.Motion.MotionLength;
  207. }
  208. playingMotions[playingMotions.Count - 1] = motion;
  209. }
  210. // Calculate MotionFade.
  211. for (var i = 0; i < playingMotions.Count; i++)
  212. {
  213. var playingMotion = playingMotions[i];
  214. var fadeMotion = playingMotion.Motion;
  215. if (fadeMotion == null)
  216. {
  217. continue;
  218. }
  219. var elapsedTime = time - playingMotion.StartTime;
  220. var endTime = playingMotion.EndTime - elapsedTime;
  221. var fadeInTime = fadeMotion.FadeInTime;
  222. var fadeOutTime = fadeMotion.FadeOutTime;
  223. var fadeInWeight = (fadeInTime <= 0.0f)
  224. ? 1.0f
  225. : CubismFadeMath.GetEasingSine(elapsedTime / fadeInTime);
  226. var fadeOutWeight = (fadeOutTime <= 0.0f)
  227. ? 1.0f
  228. : CubismFadeMath.GetEasingSine((playingMotion.EndTime - Time.time) / fadeOutTime);
  229. playingMotions[i] = playingMotion;
  230. var motionWeight = (i == 0)
  231. ? (fadeInWeight * fadeOutWeight)
  232. : (fadeInWeight * fadeOutWeight * layerWeight);
  233. // Apply to parameter values
  234. for (var j = 0; j < DestinationParameters.Length; ++j)
  235. {
  236. var index = -1;
  237. for (var k = 0; k < fadeMotion.ParameterIds.Length; ++k)
  238. {
  239. if (fadeMotion.ParameterIds[k] != DestinationParameters[j].Id)
  240. {
  241. continue;
  242. }
  243. index = k;
  244. break;
  245. }
  246. if (index < 0)
  247. {
  248. // There is not target ID curve in motion.
  249. continue;
  250. }
  251. DestinationParameters[j].Value = Evaluate(
  252. fadeMotion.ParameterCurves[index], elapsedTime, endTime,
  253. fadeInWeight, fadeOutWeight,
  254. fadeMotion.ParameterFadeInTimes[index], fadeMotion.ParameterFadeOutTimes[index],
  255. motionWeight, DestinationParameters[j].Value);
  256. }
  257. // Apply to part opacities
  258. for (var j = 0; j < DestinationParts.Length; ++j)
  259. {
  260. var index = -1;
  261. for (var k = 0; k < fadeMotion.ParameterIds.Length; ++k)
  262. {
  263. if (fadeMotion.ParameterIds[k] != DestinationParts[j].Id)
  264. {
  265. continue;
  266. }
  267. index = k;
  268. break;
  269. }
  270. if (index < 0)
  271. {
  272. // There is not target ID curve in motion.
  273. continue;
  274. }
  275. DestinationParts[j].Opacity = Evaluate(
  276. fadeMotion.ParameterCurves[index], elapsedTime, endTime,
  277. fadeInWeight, fadeOutWeight,
  278. fadeMotion.ParameterFadeInTimes[index], fadeMotion.ParameterFadeOutTimes[index],
  279. motionWeight, DestinationParts[j].Opacity);
  280. }
  281. }
  282. }
  283. /// <summary>
  284. /// Evaluate fade curve.
  285. /// </summary>
  286. /// <param name="curve">Curves to be evaluated.</param>
  287. /// <param name="elapsedTime">Elapsed Time.</param>
  288. /// <param name="endTime">Fading end time.</param>
  289. /// <param name="fadeInTime">Fade in time.</param>
  290. /// <param name="fadeOutTime">Fade out time.</param>
  291. /// <param name="parameterFadeInTime">Fade in time parameter.</param>
  292. /// <param name="parameterFadeOutTime">Fade out time parameter.</param>
  293. /// <param name="motionWeight">Motion weight.</param>
  294. /// <param name="currentValue">Current value with weight applied.</param>
  295. public float Evaluate(
  296. AnimationCurve curve, float elapsedTime, float endTime,
  297. float fadeInTime, float fadeOutTime,
  298. float parameterFadeInTime, float parameterFadeOutTime,
  299. float motionWeight, float currentValue)
  300. {
  301. if (curve.length <= 0)
  302. {
  303. return currentValue;
  304. }
  305. // Motion fade.
  306. if (parameterFadeInTime < 0.0f &&
  307. parameterFadeOutTime < 0.0f)
  308. {
  309. return currentValue + (curve.Evaluate(elapsedTime) - currentValue) * motionWeight;
  310. }
  311. // Parameter fade.
  312. float fadeInWeight, fadeOutWeight;
  313. if (parameterFadeInTime < 0.0f)
  314. {
  315. fadeInWeight = fadeInTime;
  316. }
  317. else
  318. {
  319. fadeInWeight = (parameterFadeInTime < float.Epsilon)
  320. ? 1.0f
  321. : CubismFadeMath.GetEasingSine(elapsedTime / parameterFadeInTime);
  322. }
  323. if (parameterFadeOutTime < 0.0f)
  324. {
  325. fadeOutWeight = fadeOutTime;
  326. }
  327. else
  328. {
  329. fadeOutWeight = (parameterFadeOutTime < float.Epsilon)
  330. ? 1.0f
  331. : CubismFadeMath.GetEasingSine(endTime / parameterFadeOutTime);
  332. }
  333. var parameterWeight = fadeInWeight * fadeOutWeight;
  334. return currentValue + (curve.Evaluate(elapsedTime) - currentValue) * parameterWeight;
  335. }
  336. #endregion
  337. #region Unity Events Handling
  338. /// <summary>
  339. /// Initializes instance.
  340. /// </summary>
  341. private void OnEnable()
  342. {
  343. // Initialize cache.
  344. Refresh();
  345. }
  346. /// <summary>
  347. /// Called by Unity.
  348. /// </summary>
  349. private void LateUpdate()
  350. {
  351. if (!HasUpdateController)
  352. {
  353. OnLateUpdate();
  354. }
  355. }
  356. #endregion
  357. }
  358. }