CubismFadeMotionData.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.Json;
  8. using UnityEngine;
  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. /// Time to fade in.
  20. /// </summary>
  21. [SerializeField]
  22. public float FadeInTime;
  23. /// <summary>
  24. /// Time to fade out.
  25. /// </summary>
  26. [SerializeField]
  27. public float FadeOutTime;
  28. /// <summary>
  29. /// Parameter ids.
  30. /// </summary>
  31. [SerializeField]
  32. public string[] ParameterIds;
  33. /// <summary>
  34. /// Parameter curves.
  35. /// </summary>
  36. [SerializeField]
  37. public AnimationCurve[] ParameterCurves;
  38. /// <summary>
  39. /// Fade in time parameters.
  40. /// </summary>
  41. [SerializeField]
  42. public float[] ParameterFadeInTimes;
  43. /// <summary>
  44. /// Fade out time parameters.
  45. /// </summary>
  46. [SerializeField]
  47. public float[] ParameterFadeOutTimes;
  48. /// <summary>
  49. /// Motion length.
  50. /// </summary>
  51. [SerializeField]
  52. public float MotionLength;
  53. /// <summary>
  54. /// Create CubismFadeMotionData from CubismMotion3Json.
  55. /// </summary>
  56. /// <param name="motion3Json">Motion3json as the creator.</param>
  57. /// <param name="motionName">Motion name of interest.</param>
  58. /// <param name="motionLength">Length of target motion.</param>
  59. /// <param name="shouldImportAsOriginalWorkflow">Whether the original work flow or not.</param>
  60. /// <param name="isCallFromModelJson">Whether it is a call from the model json.</param>
  61. /// <returns>Fade data created based on motion3json.</returns>
  62. public static CubismFadeMotionData CreateInstance(
  63. CubismMotion3Json motion3Json, string motionName, float motionLength,
  64. bool shouldImportAsOriginalWorkflow = false, bool isCallFromModelJson = false)
  65. {
  66. var fadeMotion = CreateInstance<CubismFadeMotionData>();
  67. var curveCount = motion3Json.Curves.Length;
  68. fadeMotion.ParameterIds = new string[curveCount];
  69. fadeMotion.ParameterFadeInTimes = new float[curveCount];
  70. fadeMotion.ParameterFadeOutTimes = new float[curveCount];
  71. fadeMotion.ParameterCurves = new AnimationCurve[curveCount];
  72. return CreateInstance(fadeMotion, motion3Json, motionName, motionLength, shouldImportAsOriginalWorkflow, isCallFromModelJson);
  73. }
  74. /// <summary>
  75. /// Put motion3json's fade information back into fade motion data.
  76. /// </summary>
  77. /// <param name="fadeMotion">Instance containing fade information.</param>
  78. /// <param name="motion3Json">Target motion3json.</param>
  79. /// <param name="motionName">Motion name of interest.</param>
  80. /// <param name="motionLength">Motion length.</param>
  81. /// <param name="shouldImportAsOriginalWorkflow">Whether the original work flow or not.</param>
  82. /// <param name="isCallFormModelJson">Whether it is a call from the model json.</param>
  83. /// <returns>Fade data created based on fademotiondata.</returns>
  84. public static CubismFadeMotionData CreateInstance(
  85. CubismFadeMotionData fadeMotion, CubismMotion3Json motion3Json, string motionName, float motionLength,
  86. bool shouldImportAsOriginalWorkflow = false, bool isCallFormModelJson = false)
  87. {
  88. fadeMotion.MotionName = motionName;
  89. fadeMotion.MotionLength = motionLength;
  90. fadeMotion.FadeInTime = (motion3Json.Meta.FadeInTime < 0.0f) ? 1.0f : motion3Json.Meta.FadeInTime;
  91. fadeMotion.FadeOutTime = (motion3Json.Meta.FadeOutTime < 0.0f) ? 1.0f : motion3Json.Meta.FadeOutTime;
  92. for (var i = 0; i < motion3Json.Curves.Length; ++i)
  93. {
  94. var curve = motion3Json.Curves[i];
  95. // In original workflow mode, skip add part opacity curve when call not from model3.json.
  96. if (curve.Target == "PartOpacity" && shouldImportAsOriginalWorkflow && !isCallFormModelJson)
  97. {
  98. continue;
  99. }
  100. fadeMotion.ParameterIds[i] = curve.Id;
  101. fadeMotion.ParameterFadeInTimes[i] = (curve.FadeInTime < 0.0f) ? -1.0f : curve.FadeInTime;
  102. fadeMotion.ParameterFadeOutTimes[i] = (curve.FadeOutTime < 0.0f) ? -1.0f : curve.FadeOutTime;
  103. fadeMotion.ParameterCurves[i] = new AnimationCurve(CubismMotion3Json.ConvertCurveSegmentsToKeyframes(curve.Segments));
  104. }
  105. return fadeMotion;
  106. }
  107. }
  108. }