/**
* Copyright(c) Live2D Inc. All rights reserved.
*
* Use of this source code is governed by the Live2D Open Software license
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
*/
using UnityEngine;
using Live2D.Cubism.Framework.Json;
namespace Live2D.Cubism.Framework.MotionFade
{
public class CubismFadeMotionData : ScriptableObject
{
///
/// Name of motion.
///
[SerializeField]
public string MotionName;
///
/// Store time to fade in from .model3.json.
/// NOTE: It is used to save `FadeInTime` from .model3.json. Please use instead of using it directly.
///
[HideInInspector, SerializeField]
public float ModelFadeInTime = -1.0f;
///
/// Store time to fade out from .model3.json.
/// NOTE: It is used to save `FadeOutTime` from .model3.json. Please use instead of using it directly.
///
[HideInInspector, SerializeField]
public float ModelFadeOutTime = -1.0f;
///
/// Time to fade in.
///
[SerializeField]
public float FadeInTime;
///
/// Time to fade out.
///
[SerializeField]
public float FadeOutTime;
///
/// Parameter ids.
///
[SerializeField]
public string[] ParameterIds;
///
/// Parameter curves.
///
[SerializeField]
public AnimationCurve[] ParameterCurves;
///
/// Fade in time parameters.
///
[SerializeField]
public float[] ParameterFadeInTimes;
///
/// Fade out time parameters.
///
[SerializeField]
public float[] ParameterFadeOutTimes;
///
/// Motion length.
///
[SerializeField]
public float MotionLength;
///
/// Create CubismFadeMotionData from CubismMotion3Json.
///
/// Motion3json as the creator.
/// Motion name of interest.
/// Length of target motion.
/// Whether the original work flow or not.
/// Whether it is a call from the model json.
/// .model3.json to retrieve the fade time.
/// Fade data created based on motion3json.
public static CubismFadeMotionData CreateInstance(
CubismMotion3Json motion3Json, string motionName, float motionLength,
bool shouldImportAsOriginalWorkflow = false, bool isCallFromModelJson = false, CubismModel3Json model3Json = null)
{
var fadeMotion = CreateInstance();
var curveCount = motion3Json.Curves.Length;
fadeMotion.ParameterIds = new string[curveCount];
fadeMotion.ParameterFadeInTimes = new float[curveCount];
fadeMotion.ParameterFadeOutTimes = new float[curveCount];
fadeMotion.ParameterCurves = new AnimationCurve[curveCount];
return CreateInstance(fadeMotion, motion3Json, motionName, motionLength, shouldImportAsOriginalWorkflow, isCallFromModelJson, model3Json);
}
///
/// Put motion3json's fade information back into fade motion data.
///
/// Instance containing fade information.
/// Target motion3json.
/// Motion name of interest.
/// Motion length.
/// Whether the original work flow or not.
/// Whether it is a call from the model json.
/// .model3.json to retrieve the fade time.
/// Fade data created based on fademotiondata.
public static CubismFadeMotionData CreateInstance(
CubismFadeMotionData fadeMotion, CubismMotion3Json motion3Json, string motionName, float motionLength,
bool shouldImportAsOriginalWorkflow = false, bool isCallFormModelJson = false, CubismModel3Json model3Json = null)
{
if (model3Json != null)
{
GetFadeDataFromModel3Json(model3Json, fadeMotion);
}
if (motion3Json == null)
{
return fadeMotion;
}
fadeMotion.MotionName = motionName;
fadeMotion.MotionLength = motionLength;
if (fadeMotion.ModelFadeInTime < 0.0f)
{
fadeMotion.FadeInTime = (motion3Json.Meta.FadeInTime < 0.0f) ? 1.0f : motion3Json.Meta.FadeInTime;
}
else
{
fadeMotion.FadeInTime = fadeMotion.ModelFadeInTime;
}
if (fadeMotion.ModelFadeOutTime < 0.0f)
{
fadeMotion.FadeOutTime = (motion3Json.Meta.FadeOutTime < 0.0f) ? 1.0f : motion3Json.Meta.FadeOutTime;
}
else
{
fadeMotion.FadeOutTime = fadeMotion.ModelFadeOutTime;
}
for (var i = 0; i < motion3Json.Curves.Length; ++i)
{
var curve = motion3Json.Curves[i];
// In original workflow mode, skip add part opacity curve when call not from model3.json.
if (curve.Target == "PartOpacity" && shouldImportAsOriginalWorkflow && !isCallFormModelJson)
{
continue;
}
fadeMotion.ParameterIds[i] = curve.Id;
fadeMotion.ParameterFadeInTimes[i] = (curve.FadeInTime < 0.0f) ? -1.0f : curve.FadeInTime;
fadeMotion.ParameterFadeOutTimes[i] = (curve.FadeOutTime < 0.0f) ? -1.0f : curve.FadeOutTime;
fadeMotion.ParameterCurves[i] = new AnimationCurve(CubismMotion3Json.ConvertCurveSegmentsToKeyframes(curve.Segments));
}
return fadeMotion;
}
private static void GetFadeDataFromModel3Json(CubismModel3Json modelJson, CubismFadeMotionData fadeMotion)
{
var motions = modelJson.FileReferences.Motions.Motions;
for (var groupIndex = 0; groupIndex < motions?.Length; groupIndex++)
{
if (motions[groupIndex] == null)
{
continue;
}
for (var motionIndex = 0; motionIndex < motions[groupIndex]?.Length; motionIndex++)
{
var motion = motions[groupIndex][motionIndex];
// Set FadeInTime.
if (!(motion.FadeInTime < 0.0f))
{
fadeMotion.ModelFadeInTime = motion.FadeInTime;
fadeMotion.FadeInTime = fadeMotion.ModelFadeInTime;
}
// Set FadeOutTime.
if (!(motion.FadeOutTime < 0.0f))
{
fadeMotion.ModelFadeOutTime = motion.FadeOutTime;
fadeMotion.FadeInTime = fadeMotion.ModelFadeInTime;
}
}
}
}
}
}