CubismPhysicsOutput.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.Physics
  11. {
  12. /// <summary>
  13. /// Output data of physics.
  14. /// </summary>
  15. [Serializable]
  16. public struct CubismPhysicsOutput
  17. {
  18. /// <summary>
  19. /// Delegation of function of getting output value.
  20. /// </summary>
  21. /// <param name="translation">Translation.</param>
  22. /// <param name="parameter">Parameter.</param>
  23. /// <param name="particles">Particles.</param>
  24. /// <param name="particleIndex">Index of particle.</param>
  25. /// <param name="gravity">Gravity.</param>
  26. /// <returns>Output value.</returns>
  27. public delegate float ValueGetter(
  28. Vector2 translation,
  29. CubismParameter parameter,
  30. CubismPhysicsParticle[] particles,
  31. int particleIndex,
  32. Vector2 gravity
  33. );
  34. /// <summary>
  35. /// Delegation of function of getting output scale.
  36. /// </summary>
  37. /// <returns>Output scale.</returns>
  38. public delegate float ScaleGetter();
  39. /// <summary>
  40. /// Gets output for translation X-axis.
  41. /// </summary>
  42. /// <param name="translation">Translation.</param>
  43. /// <param name="parameter">Parameter.</param>
  44. /// <param name="particles">Particles.</param>
  45. /// <param name="particleIndex">Index of particle.</param>
  46. /// <param name="gravity">Gravity.</param>
  47. /// <returns>Output value.</returns>
  48. private float GetOutputTranslationX(
  49. Vector2 translation,
  50. CubismParameter parameter,
  51. CubismPhysicsParticle[] particles,
  52. int particleIndex,
  53. Vector2 gravity
  54. )
  55. {
  56. var outputValue = translation.x;
  57. if (IsInverted)
  58. {
  59. outputValue *= -1.0f;
  60. }
  61. return outputValue;
  62. }
  63. /// <summary>
  64. /// Gets output for translation Y-axis.
  65. /// </summary>
  66. /// <param name="translation">Translation.</param>
  67. /// <param name="parameter">Parameter.</param>
  68. /// <param name="particles">Particles.</param>
  69. /// <param name="particleIndex">Index of particle.</param>
  70. /// <param name="gravity">Gravity.</param>
  71. /// <returns>Output value.</returns>
  72. private float GetOutputTranslationY(
  73. Vector2 translation,
  74. CubismParameter parameter,
  75. CubismPhysicsParticle[] particles,
  76. int particleIndex,
  77. Vector2 gravity
  78. )
  79. {
  80. var outputValue = translation.y;
  81. if (IsInverted)
  82. {
  83. outputValue *= -1.0f;
  84. }
  85. return outputValue;
  86. }
  87. /// <summary>
  88. /// Gets output for angle.
  89. /// </summary>
  90. /// <param name="translation">Translation.</param>
  91. /// <param name="parameter">Parameter.</param>
  92. /// <param name="particles">Particles.</param>
  93. /// <param name="particleIndex">Index of particle.</param>
  94. /// <param name="gravity">Gravity.</param>
  95. /// <returns>Output value.</returns>
  96. private float GetOutputAngle(
  97. Vector2 translation,
  98. CubismParameter parameter,
  99. CubismPhysicsParticle[] particles,
  100. int particleIndex,
  101. Vector2 gravity
  102. )
  103. {
  104. var parentGravity = Vector2.zero;
  105. if (CubismPhysics.UseAngleCorrection)
  106. {
  107. if (particleIndex < 2)
  108. {
  109. parentGravity = gravity;
  110. parentGravity.y *= -1.0f;
  111. }
  112. else
  113. {
  114. parentGravity = particles[particleIndex - 1].Position -
  115. particles[particleIndex - 2].Position;
  116. }
  117. }
  118. else
  119. {
  120. parentGravity = gravity;
  121. parentGravity.y *= -1.0f;
  122. }
  123. var outputValue = CubismPhysicsMath.DirectionToRadian(parentGravity, translation);
  124. if (IsInverted)
  125. {
  126. outputValue *= -1.0f;
  127. }
  128. return outputValue;
  129. }
  130. /// <summary>
  131. /// Gets output scale for translation X-axis.
  132. /// </summary>
  133. /// <returns>Output scale.</returns>
  134. private float GetOutputScaleTranslationX()
  135. {
  136. return TranslationScale.x;
  137. }
  138. /// <summary>
  139. /// Gets output scale for translation Y-axis.
  140. /// </summary>
  141. /// <returns>Output scale.</returns>
  142. private float GetOutputScaleTranslationY()
  143. {
  144. return TranslationScale.y;
  145. }
  146. /// <summary>
  147. /// Gets output scale for angle.
  148. /// </summary>
  149. /// <returns>Output scale.</returns>
  150. private float GetOutputScaleAngle()
  151. {
  152. return AngleScale;
  153. }
  154. public void InitializeGetter()
  155. {
  156. switch (SourceComponent)
  157. {
  158. case CubismPhysicsSourceComponent.X:
  159. {
  160. GetScale =
  161. GetOutputScaleTranslationX;
  162. GetValue =
  163. GetOutputTranslationX;
  164. }
  165. break;
  166. case CubismPhysicsSourceComponent.Y:
  167. {
  168. GetScale =
  169. GetOutputScaleTranslationY;
  170. GetValue =
  171. GetOutputTranslationY;
  172. }
  173. break;
  174. case CubismPhysicsSourceComponent.Angle:
  175. {
  176. GetScale =
  177. GetOutputScaleAngle;
  178. GetValue =
  179. GetOutputAngle;
  180. }
  181. break;
  182. }
  183. }
  184. /// <summary>
  185. /// Parameter ID of destination.
  186. /// </summary>
  187. [SerializeField]
  188. public string DestinationId;
  189. /// <summary>
  190. /// Index of particle.
  191. /// </summary>
  192. [SerializeField]
  193. public int ParticleIndex;
  194. /// <summary>
  195. /// Scale of transition.
  196. /// </summary>
  197. [SerializeField]
  198. public Vector2 TranslationScale;
  199. /// <summary>
  200. /// Scale of angle.
  201. /// </summary>
  202. [SerializeField]
  203. public float AngleScale;
  204. /// <summary>
  205. /// Weight.
  206. /// </summary>
  207. [SerializeField]
  208. public float Weight;
  209. /// <summary>
  210. /// Component of source.
  211. /// </summary>
  212. [SerializeField]
  213. public CubismPhysicsSourceComponent SourceComponent;
  214. /// <summary>
  215. /// True if value is inverted; otherwise.
  216. /// </summary>
  217. [SerializeField]
  218. public bool IsInverted;
  219. /// <summary>
  220. /// The value that below minimum.
  221. /// </summary>
  222. [NonSerialized]
  223. public float ValueBelowMinimum;
  224. /// <summary>
  225. /// The value that exceeds maximum.
  226. /// </summary>
  227. [NonSerialized]
  228. public float ValueExceededMaximum;
  229. /// <summary>
  230. /// Destination data from parameter.
  231. /// </summary>
  232. [NonSerialized]
  233. public CubismParameter Destination;
  234. /// <summary>
  235. /// Function of getting output value.
  236. /// </summary>
  237. [NonSerialized]
  238. public ValueGetter GetValue;
  239. /// <summary>
  240. /// Function of getting output scale.
  241. /// </summary>
  242. [NonSerialized]
  243. public ScaleGetter GetScale;
  244. }
  245. }