Guest User

Untitled

a guest
Oct 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. #if LIVE2D_AVAILABLE
  2.  
  3. using Live2D.Cubism.Core;
  4. using Live2D.Cubism.Framework;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityCommon;
  8. using UnityEngine;
  9.  
  10. namespace Naninovel
  11. {
  12. [RequireComponent(typeof(CubismModel))]
  13. public class Live2DAppearanceController : MonoBehaviour
  14. {
  15. [System.Serializable]
  16. public class ParametersMap : SerializableMap<string, float> { }
  17.  
  18. [System.Serializable]
  19. public class AppearanceData
  20. {
  21. [Tooltip("Appearance to bind parameters for.")]
  22. public string Appearance = string.Empty;
  23. [Tooltip("List of parameters' name and value to bind.")]
  24. public ParametersMap Params = default;
  25. }
  26.  
  27. private struct ModifyRequest
  28. {
  29. public float SmoothedValue => Mathf.SmoothStep(startValue, targetValue, (Time.time - startTime) / normalizedDuration);
  30.  
  31. private readonly float targetValue;
  32. private readonly float startValue;
  33. private readonly float startTime;
  34. private readonly float normalizedDuration;
  35.  
  36. public ModifyRequest (CubismParameter param, float targetValue, float duration)
  37. {
  38. this.targetValue = targetValue;
  39. startValue = param.Value;
  40. startTime = Time.time;
  41.  
  42. // In case we're starting the animation when the param is not at default value: reduce the duration accordingly.
  43. var curValueProgress = 1f - ((targetValue - param.Value) / (targetValue - param.DefaultValue));
  44. var curTimeProgress = InversedSmoothStep(curValueProgress);
  45. normalizedDuration = duration * (1f - curTimeProgress);
  46. print(normalizedDuration);
  47. }
  48.  
  49. private static float InversedSmoothStep (float v) => v + (v - (v * v * (3f - 2f * v)));
  50. }
  51.  
  52. [SerializeField] private List<AppearanceData> appearanceMap = default;
  53. [SerializeField] private CubismParameterBlendMode blendMode = CubismParameterBlendMode.Override;
  54. [SerializeField] private string headXAngleParamId = "PARAM_ANGLE_X";
  55.  
  56. private const float headXAngleDelta = 30f;
  57. private static CubismParameter headXParam;
  58. private CubismModel cubismModel;
  59. private Dictionary<string, ModifyRequest> appearanceRequests = new Dictionary<string, ModifyRequest>();
  60. private ModifyRequest lookDirRequest;
  61.  
  62. public void SetAppearance (string appearance, float duration)
  63. {
  64. appearanceRequests.Clear();
  65. var appearanceData = appearanceMap?.FirstOrDefault(a => a.Appearance == appearance);
  66. if (appearanceData is null) return;
  67. foreach (var kv in appearanceData.Params)
  68. appearanceRequests[kv.Key] = new ModifyRequest(cubismModel.Parameters.FindById(kv.Key), kv.Value, duration);
  69. }
  70.  
  71. public void SetLookDirection (CharacterLookDirection lookDirection, float duration)
  72. {
  73. if (headXParam is null) return;
  74.  
  75. var targetValue = default(float);
  76. switch (lookDirection)
  77. {
  78. case CharacterLookDirection.Center:
  79. targetValue = 0f;
  80. break;
  81. case CharacterLookDirection.Left:
  82. targetValue = -headXAngleDelta;
  83. break;
  84. case CharacterLookDirection.Right:
  85. targetValue = headXAngleDelta;
  86. break;
  87. }
  88. lookDirRequest = new ModifyRequest(headXParam, targetValue, duration);
  89. }
  90.  
  91. private void Start ()
  92. {
  93. cubismModel = this.FindCubismModel();
  94. headXParam = cubismModel.Parameters.FindById(headXAngleParamId);
  95. }
  96.  
  97. private void LateUpdate ()
  98. {
  99. for (int i = 0; i < cubismModel.Parameters.Length; i++)
  100. {
  101. var param = cubismModel.Parameters[i];
  102.  
  103. var value = !appearanceRequests.ContainsKey(param.Id) ? param.DefaultValue : appearanceRequests[param.Id].SmoothedValue;
  104. if (Mathf.Approximately(param.Value, value)) continue;
  105.  
  106. param.BlendToValue(blendMode, value);
  107. }
  108.  
  109. if (headXParam != null) headXParam.BlendToValue(blendMode, lookDirRequest.SmoothedValue);
  110. }
  111. }
  112. }
  113.  
  114. #endif
Add Comment
Please, Sign In to add comment