CubismMotionLayer.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.Framework.MotionFade;
  8. using System;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using UnityEngine.Animations;
  12. using UnityEngine.Playables;
  13. namespace Live2D.Cubism.Framework.Motion
  14. {
  15. /// <summary>
  16. /// Cubism motion layer.
  17. /// </summary>
  18. public class CubismMotionLayer : ICubismFadeState
  19. {
  20. #region Action
  21. /// <summary>
  22. /// Action animation end handler.
  23. /// </summary>
  24. public Action<int, int> AnimationBeginHandler;
  25. /// <summary>
  26. /// Action animation end handler.
  27. /// </summary>
  28. public Action<int, int> AnimationEndHandler;
  29. #endregion
  30. #region Variable
  31. /// <summary>
  32. /// Playable output.
  33. /// </summary>
  34. public AnimationMixerPlayable PlayableOutput { get; private set; }
  35. /// <summary>
  36. /// Playable output.
  37. /// </summary>
  38. private PlayableGraph _playableGraph;
  39. /// <summary>
  40. /// Cubism playing motions.
  41. /// </summary>
  42. private List<CubismFadePlayingMotion> _playingMotions;
  43. /// <summary>
  44. /// Cubism playing motions.
  45. /// </summary>
  46. private CubismMotionState _motionState;
  47. /// <summary>
  48. /// List of cubism fade motion.
  49. /// </summary>
  50. private CubismFadeMotionList _cubismFadeMotionList;
  51. /// <summary>
  52. /// Layer index.
  53. /// </summary>
  54. private int _layerIndex;
  55. /// <summary>
  56. /// Layer weight.
  57. /// </summary>
  58. private float _layerWeight;
  59. /// <summary>
  60. /// Animation is finished.
  61. /// </summary>
  62. private bool _isFinished;
  63. /// <summary>
  64. /// Is finished.
  65. /// </summary>
  66. /// <returns>True if the animation is finished, false otherwise.</returns>
  67. public bool IsFinished
  68. {
  69. get { return _isFinished; }
  70. }
  71. #endregion
  72. #region Fade State Interface
  73. /// <summary>
  74. /// Get cubism playing motion list.
  75. /// </summary>
  76. /// <returns>Cubism playing motion list.</returns>
  77. public List<CubismFadePlayingMotion> GetPlayingMotions()
  78. {
  79. return _playingMotions;
  80. }
  81. /// <summary>
  82. /// Is default state.
  83. /// </summary>
  84. /// <returns><see langword="true"/> State is default; <see langword="false"/> otherwise.</returns>
  85. public bool IsDefaultState()
  86. {
  87. return false;
  88. }
  89. /// <summary>
  90. /// Get layer weight.
  91. /// </summary>
  92. /// <returns>Layer weight.</returns>
  93. public float GetLayerWeight()
  94. {
  95. return _layerWeight;
  96. }
  97. /// <summary>
  98. /// Get state transition finished.
  99. /// </summary>
  100. /// <returns><see langword="true"/> State transition is finished; <see langword="false"/> otherwise.</returns>
  101. public bool GetStateTransitionFinished()
  102. {
  103. return true;
  104. }
  105. /// <summary>
  106. /// Set state transition finished.
  107. /// </summary>
  108. /// <param name="isFinished">State is finished.</param>
  109. public void SetStateTransitionFinished(bool isFinished) {}
  110. /// <summary>
  111. /// Stop animation.
  112. /// </summary>
  113. /// <param name="index">Playing motion index.</param>
  114. public void StopAnimation(int index)
  115. {
  116. // Remove from playing motion list.
  117. _playingMotions.RemoveAt(index);
  118. }
  119. /// <summary>
  120. /// Stop animation.
  121. /// </summary>
  122. public void StopAnimationClip()
  123. {
  124. // Remove from motion state list.
  125. if (_motionState == null)
  126. {
  127. return;
  128. }
  129. _playableGraph.Disconnect(_motionState.ClipMixer, 0);
  130. _motionState = null;
  131. _isFinished = true;
  132. StopAllAnimation();
  133. }
  134. #endregion
  135. #region Function
  136. /// <summary>
  137. /// Initialize motion layer.
  138. /// </summary>
  139. /// <param name="playableGraph">.</param>
  140. /// <param name="fadeMotionList">.</param>
  141. /// <param name="layerWeight">.</param>
  142. public static CubismMotionLayer CreateCubismMotionLayer(PlayableGraph playableGraph, CubismFadeMotionList fadeMotionList, int layerIndex, float layerWeight = 1.0f)
  143. {
  144. var ret = new CubismMotionLayer();
  145. ret._playableGraph = playableGraph;
  146. ret._cubismFadeMotionList = fadeMotionList;
  147. ret._layerIndex = layerIndex;
  148. ret._layerWeight = layerWeight;
  149. ret._isFinished = true;
  150. ret._motionState = null;
  151. ret._playingMotions = new List<CubismFadePlayingMotion>();
  152. ret.PlayableOutput = AnimationMixerPlayable.Create(playableGraph, 1);
  153. return ret;
  154. }
  155. /// <summary>
  156. /// Create fade playing motion.
  157. /// </summary>
  158. /// <param name="clip">Animator clip.</param>
  159. /// <param name="speed">Animation speed.</param>
  160. private CubismFadePlayingMotion CreateFadePlayingMotion(AnimationClip clip, bool isLooping, float speed = 1.0f)
  161. {
  162. var ret = new CubismFadePlayingMotion();
  163. var isNotFound = true;
  164. var instanceId = -1;
  165. var events = clip.events;
  166. for(var i = 0; i < events.Length; ++i)
  167. {
  168. if(events[i].functionName != "InstanceId")
  169. {
  170. continue;
  171. }
  172. instanceId = events[i].intParameter;
  173. }
  174. for (int i = 0; i < _cubismFadeMotionList.MotionInstanceIds.Length; i++)
  175. {
  176. if(_cubismFadeMotionList.MotionInstanceIds[i] != instanceId)
  177. {
  178. continue;
  179. }
  180. isNotFound = false;
  181. ret.Speed = speed;
  182. ret.StartTime = Time.time;
  183. ret.FadeInStartTime = Time.time;
  184. ret.Motion = _cubismFadeMotionList.CubismFadeMotionObjects[i];
  185. ret.EndTime = (ret.Motion.MotionLength <= 0)
  186. ? -1
  187. : ret.StartTime + ret.Motion.MotionLength / speed;
  188. ret.IsLooping = isLooping;
  189. ret.Weight = 0.0f;
  190. ret.InstanceId = instanceId;
  191. ret.IsAnimationEndEventInvoked = false;
  192. AnimationBeginHandler(_layerIndex, instanceId);
  193. break;
  194. }
  195. if(isNotFound)
  196. {
  197. Debug.LogError("CubismMotionController : Not found motion from CubismFadeMotionList.");
  198. }
  199. return ret;
  200. }
  201. /// <summary>
  202. /// Play animation.
  203. /// </summary>
  204. /// <param name="clip">Animation clip.</param>
  205. /// <param name="isLoop">Animation is loop.</param>
  206. /// <param name="speed">Animation speed.</param>
  207. public void PlayAnimation(AnimationClip clip, bool isLoop = true, float speed = 1.0f)
  208. {
  209. if (_motionState != null)
  210. {
  211. _playableGraph.Disconnect(_motionState.ClipMixer, 0);
  212. }
  213. // Create cubism motion state.
  214. _motionState = CubismMotionState.CreateCubismMotionState(_playableGraph, clip, isLoop, speed);
  215. #if UNITY_2018_2_OR_NEWER
  216. PlayableOutput.DisconnectInput(0);
  217. #else
  218. PlayableOutput.GetGraph().Disconnect(PlayableOutput, 0);
  219. #endif
  220. PlayableOutput.ConnectInput(0, _motionState.ClipMixer, 0);
  221. PlayableOutput.SetInputWeight(0, 1.0f);
  222. // Set last motion end time and fade in start time;
  223. if ((_playingMotions.Count > 0) && (_playingMotions[_playingMotions.Count - 1].Motion != null))
  224. {
  225. var motion = _playingMotions[_playingMotions.Count - 1];
  226. var time = Time.time;
  227. var newEndTime = time + motion.Motion.FadeOutTime;
  228. if (newEndTime < 0.0f || newEndTime < motion.EndTime)
  229. {
  230. motion.EndTime = newEndTime;
  231. }
  232. while (motion.IsLooping)
  233. {
  234. if ((motion.StartTime + motion.Motion.MotionLength) >= time)
  235. {
  236. break;
  237. }
  238. motion.StartTime += motion.Motion.MotionLength;
  239. }
  240. motion.IsLooping = false;
  241. _playingMotions[_playingMotions.Count - 1] = motion;
  242. }
  243. // Create fade playing motion.
  244. var playingMotion = CreateFadePlayingMotion(clip, isLoop, speed);
  245. _playingMotions.Add(playingMotion);
  246. _isFinished = false;
  247. }
  248. /// <summary>
  249. /// Stop all animation.
  250. /// </summary>
  251. public void StopAllAnimation()
  252. {
  253. for(var i = _playingMotions.Count - 1; i >= 0; --i)
  254. {
  255. StopAnimation(i);
  256. }
  257. }
  258. /// <summary>
  259. /// Set layer weight.
  260. /// </summary>
  261. /// <param name="weight">Layer weight.</param>
  262. public void SetLayerWeight(float weight)
  263. {
  264. _layerWeight = weight;
  265. }
  266. /// <summary>
  267. /// Set state speed.
  268. /// </summary>
  269. /// <param name="index">index of playing motion list.</param>
  270. /// <param name="speed">Animation speed.</param>
  271. public void SetStateSpeed(int index, float speed)
  272. {
  273. // Fail silently...
  274. if(index < 0)
  275. {
  276. return;
  277. }
  278. var playingMotionData = _playingMotions[index];
  279. playingMotionData.Speed = speed;
  280. playingMotionData.EndTime = (playingMotionData.EndTime - Time.time) / speed;
  281. _playingMotions[index] = playingMotionData;
  282. _motionState.ClipMixer.SetSpeed(speed);
  283. _motionState.ClipPlayable.SetDuration(_motionState.Clip.length / speed - 0.0001f);
  284. }
  285. /// <summary>
  286. /// Set state is loop.
  287. /// </summary>
  288. /// <param name="index">index of playing motion list.</param>
  289. /// <param name="isLoop">Animation is loop.</param>
  290. public void SetStateIsLoop(int index, bool isLoop)
  291. {
  292. // Fail silently...
  293. if(index < 0)
  294. {
  295. return;
  296. }
  297. if(isLoop)
  298. {
  299. _motionState.ClipPlayable.SetDuration(double.MaxValue);
  300. }
  301. else
  302. {
  303. _motionState.ClipPlayable.SetDuration(_motionState.Clip.length - 0.0001f);
  304. }
  305. }
  306. #endregion
  307. public void Update()
  308. {
  309. var isFinished = true;
  310. for (var i = 0; i < _playingMotions.Count; i++)
  311. {
  312. var playingMotion = _playingMotions[i];
  313. if (playingMotion.IsLooping)
  314. {
  315. isFinished = false;
  316. continue;
  317. }
  318. if (playingMotion.IsAnimationEndEventInvoked)
  319. {
  320. continue;
  321. }
  322. if (Time.time > playingMotion.EndTime)
  323. {
  324. playingMotion.IsAnimationEndEventInvoked = true;
  325. _playingMotions[i] = playingMotion;
  326. if (playingMotion.InstanceId.HasValue)
  327. {
  328. var instanceId = _playingMotions[i].InstanceId.Value;
  329. AnimationEndHandler?.Invoke(_layerIndex, instanceId);
  330. }
  331. }
  332. else
  333. {
  334. isFinished = false;
  335. }
  336. }
  337. if (isFinished)
  338. {
  339. _isFinished = true;
  340. }
  341. }
  342. }
  343. }