CubismFadeMotionData.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. using Live2D.Cubism.Framework.Json;
  9. namespace Live2D.Cubism.Framework.MotionFade
  10. {
  11. public class CubismFadeMotionData : ScriptableObject
  12. {
  13. /// <summary>
  14. /// Name of motion.
  15. /// </summary>
  16. [SerializeField]
  17. public string MotionName;
  18. /// <summary>
  19. /// Store time to fade in from .model3.json.
  20. /// NOTE: It is used to save `FadeInTime` from .model3.json. Please use <see cref="FadeInTime"/> instead of using it directly.
  21. /// </summary>
  22. [HideInInspector, SerializeField]
  23. public float ModelFadeInTime = -1.0f;
  24. /// <summary>
  25. /// Store time to fade out from .model3.json.
  26. /// NOTE: It is used to save `FadeOutTime` from .model3.json. Please use <see cref="FadeOutTime"/> instead of using it directly.
  27. /// </summary>
  28. [HideInInspector, SerializeField]
  29. public float ModelFadeOutTime = -1.0f;
  30. /// <summary>
  31. /// Time to fade in.
  32. /// </summary>
  33. [SerializeField]
  34. public float FadeInTime;
  35. /// <summary>
  36. /// Time to fade out.
  37. /// </summary>
  38. [SerializeField]
  39. public float FadeOutTime;
  40. /// <summary>
  41. /// Parameter ids.
  42. /// </summary>
  43. [SerializeField]
  44. public string[] ParameterIds;
  45. /// <summary>
  46. /// Parameter curves.
  47. /// </summary>
  48. [SerializeField]
  49. public AnimationCurve[] ParameterCurves;
  50. /// <summary>
  51. /// Fade in time parameters.
  52. /// </summary>
  53. [SerializeField]
  54. public float[] ParameterFadeInTimes;
  55. /// <summary>
  56. /// Fade out time parameters.
  57. /// </summary>
  58. [SerializeField]
  59. public float[] ParameterFadeOutTimes;
  60. /// <summary>
  61. /// Motion length.
  62. /// </summary>
  63. [SerializeField]
  64. public float MotionLength;
  65. /// <summary>
  66. /// Create CubismFadeMotionData from CubismMotion3Json.
  67. /// </summary>
  68. /// <param name="motion3Json">Motion3json as the creator.</param>
  69. /// <param name="motionName">Motion name of interest.</param>
  70. /// <param name="motionLength">Length of target motion.</param>
  71. /// <param name="shouldImportAsOriginalWorkflow">Whether the original work flow or not.</param>
  72. /// <param name="isCallFromModelJson">Whether it is a call from the model json.</param>
  73. /// <param name="model3Json">.model3.json to retrieve the fade time.</param>
  74. /// <returns>Fade data created based on motion3json.</returns>
  75. public static CubismFadeMotionData CreateInstance(
  76. CubismMotion3Json motion3Json, string motionName, float motionLength,
  77. bool shouldImportAsOriginalWorkflow = false, bool isCallFromModelJson = false, CubismModel3Json model3Json = null)
  78. {
  79. var fadeMotion = CreateInstance<CubismFadeMotionData>();
  80. var curveCount = motion3Json.Curves.Length;
  81. fadeMotion.ParameterIds = new string[curveCount];
  82. fadeMotion.ParameterFadeInTimes = new float[curveCount];
  83. fadeMotion.ParameterFadeOutTimes = new float[curveCount];
  84. fadeMotion.ParameterCurves = new AnimationCurve[curveCount];
  85. return CreateInstance(fadeMotion, motion3Json, motionName, motionLength, shouldImportAsOriginalWorkflow, isCallFromModelJson, model3Json);
  86. }
  87. /// <summary>
  88. /// Put motion3json's fade information back into fade motion data.
  89. /// </summary>
  90. /// <param name="fadeMotion">Instance containing fade information.</param>
  91. /// <param name="motion3Json">Target motion3json.</param>
  92. /// <param name="motionName">Motion name of interest.</param>
  93. /// <param name="motionLength">Motion length.</param>
  94. /// <param name="shouldImportAsOriginalWorkflow">Whether the original work flow or not.</param>
  95. /// <param name="isCallFormModelJson">Whether it is a call from the model json.</param>
  96. /// <param name="model3Json">.model3.json to retrieve the fade time.</param>
  97. /// <returns>Fade data created based on fademotiondata.</returns>
  98. public static CubismFadeMotionData CreateInstance(
  99. CubismFadeMotionData fadeMotion, CubismMotion3Json motion3Json, string motionName, float motionLength,
  100. bool shouldImportAsOriginalWorkflow = false, bool isCallFormModelJson = false, CubismModel3Json model3Json = null)
  101. {
  102. if (model3Json != null)
  103. {
  104. GetFadeDataFromModel3Json(model3Json, fadeMotion);
  105. }
  106. if (motion3Json == null)
  107. {
  108. return fadeMotion;
  109. }
  110. fadeMotion.MotionName = motionName;
  111. fadeMotion.MotionLength = motionLength;
  112. if (fadeMotion.ModelFadeInTime < 0.0f)
  113. {
  114. fadeMotion.FadeInTime = (motion3Json.Meta.FadeInTime < 0.0f) ? 1.0f : motion3Json.Meta.FadeInTime;
  115. }
  116. else
  117. {
  118. fadeMotion.FadeInTime = fadeMotion.ModelFadeInTime;
  119. }
  120. if (fadeMotion.ModelFadeOutTime < 0.0f)
  121. {
  122. fadeMotion.FadeOutTime = (motion3Json.Meta.FadeOutTime < 0.0f) ? 1.0f : motion3Json.Meta.FadeOutTime;
  123. }
  124. else
  125. {
  126. fadeMotion.FadeOutTime = fadeMotion.ModelFadeOutTime;
  127. }
  128. for (var i = 0; i < motion3Json.Curves.Length; ++i)
  129. {
  130. var curve = motion3Json.Curves[i];
  131. // In original workflow mode, skip add part opacity curve when call not from model3.json.
  132. if (curve.Target == "PartOpacity" && shouldImportAsOriginalWorkflow && !isCallFormModelJson)
  133. {
  134. continue;
  135. }
  136. fadeMotion.ParameterIds[i] = curve.Id;
  137. fadeMotion.ParameterFadeInTimes[i] = (curve.FadeInTime < 0.0f) ? -1.0f : curve.FadeInTime;
  138. fadeMotion.ParameterFadeOutTimes[i] = (curve.FadeOutTime < 0.0f) ? -1.0f : curve.FadeOutTime;
  139. fadeMotion.ParameterCurves[i] = new AnimationCurve(CubismMotion3Json.ConvertCurveSegmentsToKeyframes(curve.Segments));
  140. }
  141. return fadeMotion;
  142. }
  143. private static void GetFadeDataFromModel3Json(CubismModel3Json modelJson, CubismFadeMotionData fadeMotion)
  144. {
  145. var motions = modelJson.FileReferences.Motions.Motions;
  146. for (var groupIndex = 0; groupIndex < motions?.Length; groupIndex++)
  147. {
  148. if (motions[groupIndex] == null)
  149. {
  150. continue;
  151. }
  152. for (var motionIndex = 0; motionIndex < motions[groupIndex]?.Length; motionIndex++)
  153. {
  154. var motion = motions[groupIndex][motionIndex];
  155. // Set FadeInTime.
  156. if (!(motion.FadeInTime < 0.0f))
  157. {
  158. fadeMotion.ModelFadeInTime = motion.FadeInTime;
  159. fadeMotion.FadeInTime = fadeMotion.ModelFadeInTime;
  160. }
  161. // Set FadeOutTime.
  162. if (!(motion.FadeOutTime < 0.0f))
  163. {
  164. fadeMotion.ModelFadeOutTime = motion.FadeOutTime;
  165. fadeMotion.FadeInTime = fadeMotion.ModelFadeInTime;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }