CubismPoseController.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 System;
  9. using UnityEngine;
  10. namespace Live2D.Cubism.Framework.Pose
  11. {
  12. /// <summary>
  13. /// Cubism pose controller.
  14. /// </summary>
  15. public sealed class CubismPoseController : MonoBehaviour, ICubismUpdatable
  16. {
  17. #region variable
  18. /// <summary>
  19. /// Default visible pose index.
  20. /// </summary>
  21. [SerializeField]
  22. public int defaultPoseIndex = 0;
  23. /// <summary>
  24. /// Back opacity threshold.
  25. /// </summary>
  26. private const float BackOpacityThreshold = 0.15f;
  27. /// <summary>
  28. /// Cubism model cache.
  29. /// </summary>
  30. private CubismModel _model;
  31. /// <summary>
  32. /// Model has update controller component.
  33. /// </summary>
  34. [HideInInspector]
  35. public bool HasUpdateController { get; set; }
  36. /// <summary>
  37. /// Pose data.
  38. /// </summary>
  39. private CubismPoseData[][] _poseData;
  40. #endregion
  41. #region Function
  42. /// <summary>
  43. /// update hidden part opacity.
  44. /// </summary>
  45. public void Refresh()
  46. {
  47. _model = this.FindCubismModel();
  48. // Fail silently...
  49. if (_model == null)
  50. {
  51. return;
  52. }
  53. var tags = _model
  54. .Parts
  55. .GetComponentsMany<CubismPosePart>();
  56. for(var i = 0; i < tags.Length; ++i)
  57. {
  58. var groupIndex = tags[i].GroupIndex;
  59. var partIndex = tags[i].PartIndex;
  60. if(_poseData == null || _poseData.Length <= groupIndex)
  61. {
  62. Array.Resize(ref _poseData, groupIndex + 1);
  63. }
  64. if(_poseData[groupIndex] == null || _poseData[groupIndex].Length <= partIndex)
  65. {
  66. Array.Resize(ref _poseData[groupIndex], partIndex + 1);
  67. }
  68. _poseData[groupIndex][partIndex].PosePart = tags[i];
  69. _poseData[groupIndex][partIndex].Part= tags[i].GetComponent<CubismPart>();
  70. defaultPoseIndex = (defaultPoseIndex < 0) ? 0 : defaultPoseIndex;
  71. if (partIndex != defaultPoseIndex)
  72. {
  73. _poseData[groupIndex][partIndex].Part.Opacity = 0.0f;
  74. }
  75. _poseData[groupIndex][partIndex].Opacity = _poseData[groupIndex][partIndex].Part.Opacity;
  76. if(tags[i].Link == null || tags[i].Link.Length == 0)
  77. {
  78. continue;
  79. }
  80. _poseData[groupIndex][partIndex].LinkParts = new CubismPart[tags[i].Link.Length];
  81. for(var j = 0; j < tags[i].Link.Length; ++j)
  82. {
  83. var linkId = tags[i].Link[j];
  84. _poseData[groupIndex][partIndex].LinkParts[j] = _model.Parts.FindById(linkId);
  85. }
  86. }
  87. // Get cubism update controller.
  88. HasUpdateController = (GetComponent<CubismUpdateController>() != null);
  89. }
  90. /// <summary>
  91. /// update hidden part opacity.
  92. /// </summary>
  93. private void DoFade()
  94. {
  95. for(var groupIndex = 0; groupIndex < _poseData.Length; ++groupIndex)
  96. {
  97. var appearPartsGroupIndex = -1;
  98. var appearPartsGroupOpacity = 1.0f;
  99. // Find appear parts group index and opacity.
  100. for (var i = 0; i < _poseData[groupIndex].Length; ++i)
  101. {
  102. var part = _poseData[groupIndex][i].Part;
  103. if(part.Opacity > _poseData[groupIndex][i].Opacity)
  104. {
  105. appearPartsGroupIndex = i;
  106. appearPartsGroupOpacity = part.Opacity;
  107. break;
  108. }
  109. }
  110. // Fail silently...
  111. if(appearPartsGroupIndex < 0)
  112. {
  113. return;
  114. }
  115. // Delay disappearing parts groups disappear.
  116. for (var i = 0; i < _poseData[groupIndex].Length; ++i)
  117. {
  118. // Fail silently...
  119. if(i == appearPartsGroupIndex)
  120. {
  121. continue;
  122. }
  123. var part = _poseData[groupIndex][i].Part;
  124. var delayedOpacity = part.Opacity;
  125. var backOpacity = (1.0f - delayedOpacity) * (1.0f - appearPartsGroupOpacity);
  126. // When restricting the visible proportion of the background
  127. if (backOpacity > BackOpacityThreshold)
  128. {
  129. delayedOpacity = 1.0f - BackOpacityThreshold / (1.0f - appearPartsGroupOpacity);
  130. }
  131. // Overwrite the opacity if it's greater than the delayed opacity.
  132. if (part.Opacity > delayedOpacity)
  133. {
  134. part.Opacity = delayedOpacity;
  135. }
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// Copy opacity to linked parts.
  141. /// </summary>
  142. private void CopyPartOpacities()
  143. {
  144. for(var groupIndex = 0; groupIndex < _poseData.Length; ++groupIndex)
  145. {
  146. for (var partIndex = 0; partIndex < _poseData[groupIndex].Length; ++partIndex)
  147. {
  148. var linkParts = _poseData[groupIndex][partIndex].LinkParts;
  149. if(linkParts == null)
  150. {
  151. continue;
  152. }
  153. var opacity = _poseData[groupIndex][partIndex].Part.Opacity;
  154. for (var linkIndex = 0; linkIndex < linkParts.Length; ++linkIndex)
  155. {
  156. var linkPart = linkParts[linkIndex];
  157. if(linkPart != null)
  158. {
  159. linkPart.Opacity = opacity;
  160. }
  161. }
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// Save parts opacity.
  167. /// </summary>
  168. private void SavePartOpacities()
  169. {
  170. for(var groupIndex = 0; groupIndex < _poseData.Length; ++groupIndex)
  171. {
  172. for (var partIndex = 0; partIndex < _poseData[groupIndex].Length; ++partIndex)
  173. {
  174. _poseData[groupIndex][partIndex].Opacity = _poseData[groupIndex][partIndex].Part.Opacity;
  175. }
  176. }
  177. }
  178. /// <summary>
  179. /// Called by cubism update controller. Order to invoke OnLateUpdate.
  180. /// </summary>
  181. public int ExecutionOrder
  182. {
  183. get { return CubismUpdateExecutionOrder.CubismPoseController; }
  184. }
  185. /// <summary>
  186. /// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing.
  187. /// </summary>
  188. public bool NeedsUpdateOnEditing
  189. {
  190. get { return false; }
  191. }
  192. /// <summary>
  193. /// Called by cubism update manager. Updates controller.
  194. /// </summary>
  195. public void OnLateUpdate()
  196. {
  197. // Fail silently...
  198. if (!enabled || _model == null || _poseData == null)
  199. {
  200. return;
  201. }
  202. DoFade();
  203. CopyPartOpacities();
  204. SavePartOpacities();
  205. }
  206. #endregion
  207. #region Unity Event Handling
  208. /// <summary>
  209. /// Called by Unity. Makes sure cache is initialized.
  210. /// </summary>
  211. private void OnEnable()
  212. {
  213. Refresh();
  214. }
  215. /// <summary>
  216. /// Called by Unity.
  217. /// </summary>
  218. private void LateUpdate()
  219. {
  220. if(!HasUpdateController)
  221. {
  222. OnLateUpdate();
  223. }
  224. }
  225. #endregion
  226. }
  227. }