CubismPhysicsController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 System;
  8. using Live2D.Cubism.Core;
  9. using Live2D.Cubism.Rendering;
  10. using Live2D.Cubism.Rendering.Masking;
  11. using UnityEngine;
  12. namespace Live2D.Cubism.Framework.Physics
  13. {
  14. /// <summary>
  15. /// Physics simulation controller.
  16. /// </summary>
  17. [CubismMoveOnReimportCopyComponentsOnly]
  18. public class CubismPhysicsController : MonoBehaviour, ICubismUpdatable
  19. {
  20. /// <summary>
  21. /// Simulation target rig.
  22. /// </summary>
  23. private CubismPhysicsRig Rig
  24. {
  25. get { return _rig; }
  26. set { _rig = value; }
  27. }
  28. [SerializeField]
  29. private CubismPhysicsRig _rig;
  30. /// <summary>
  31. /// Cubism model parameters for simulation.
  32. /// </summary>
  33. public CubismParameter[] Parameters { get; private set; }
  34. /// <summary>
  35. /// Model has update controller component.
  36. /// </summary>
  37. [HideInInspector]
  38. public bool HasUpdateController { get; set; }
  39. public int ExecutionOrder
  40. {
  41. get { return CubismUpdateExecutionOrder.CubismPhysicsController; }
  42. }
  43. public bool NeedsUpdateOnEditing
  44. {
  45. get { return false; }
  46. }
  47. public void OnLateUpdate()
  48. {
  49. if (!enabled)
  50. {
  51. return;
  52. }
  53. var deltaTime = Time.deltaTime;
  54. // Use fixed delta time if required.
  55. if (CubismPhysics.UseFixedDeltaTime)
  56. {
  57. deltaTime = Time.fixedDeltaTime;
  58. }
  59. // Evaluate rig.
  60. Rig.Evaluate(deltaTime);
  61. }
  62. /// <summary>
  63. /// Calculate until the physics is stable and update the model information.
  64. /// </summary>
  65. public void Stabilization()
  66. {
  67. Rig.Stabilization();
  68. var renderController = gameObject.GetComponent<CubismRenderController>();
  69. var maskController = gameObject.GetComponent<CubismMaskController>();
  70. renderController.OnLateUpdate();
  71. if (maskController)
  72. {
  73. maskController.OnLateUpdate();
  74. }
  75. }
  76. /// <summary>
  77. /// Sets rig and initializes <see langword="this"/>.
  78. /// </summary>
  79. /// <param name="rig"></param>
  80. public void Initialize(CubismPhysicsRig rig)
  81. {
  82. Rig = rig;
  83. Awake();
  84. }
  85. /// <summary>
  86. /// Set <see cref="CubismPhysicsOutput.AngleScale"/> to the ratio by the argument to the original value.
  87. /// </summary>
  88. /// <param name="subRig"></param>
  89. /// <param name="ratio">Ratio to original value</param>
  90. public void SetPhysicsSubRigOutputAngleScaleRatio(CubismPhysicsSubRig subRig, float ratio)
  91. {
  92. if (subRig == null)
  93. {
  94. return;
  95. }
  96. for (int i = 0; i < subRig.Output.Length; i++)
  97. {
  98. var original = subRig.OriginalOutput[i];
  99. subRig.Output[i].AngleScale = Math.Max(original.AngleScale * ratio, 0);
  100. subRig.Output[i].InitializeGetter();
  101. }
  102. }
  103. /// <summary>
  104. /// Set <see cref="CubismPhysicsOutput.IsInverted"/> whether or not to invert for the original bool value.
  105. /// </summary>
  106. /// <param name="subRig"></param>
  107. /// <param name="isInvert">Invert the original bool value</param>
  108. public void SetPhysicsSubRigOutputIsInverted(CubismPhysicsSubRig subRig, bool isInvert)
  109. {
  110. if (subRig == null)
  111. {
  112. return;
  113. }
  114. for (int i = 0; i < subRig.Output.Length; i++)
  115. {
  116. var original = subRig.OriginalOutput[i];
  117. subRig.Output[i].IsInverted = isInvert ? !original.IsInverted : original.IsInverted;
  118. subRig.Output[i].InitializeGetter();
  119. }
  120. }
  121. #region Unity Event Handling
  122. /// <summary>
  123. /// Called by Unity or <see cref="Initialize"/>. Initializes <see langword="this"/> if <see cref="Rig"/> exists.
  124. /// </summary>
  125. public void Awake()
  126. {
  127. // Check rig existence.
  128. if (Rig == null)
  129. {
  130. return;
  131. }
  132. // Initialize rig.
  133. Rig.Controller = this;
  134. for (var i = 0; i < Rig.SubRigs.Length; ++i)
  135. {
  136. Rig.SubRigs[i].Rig = Rig;
  137. }
  138. Parameters = this.FindCubismModel().Parameters;
  139. Rig.Initialize();
  140. }
  141. /// <summary>
  142. /// Called by Unity.
  143. /// </summary>
  144. public void Start()
  145. {
  146. // Get cubism update controller.
  147. HasUpdateController = (GetComponent<CubismUpdateController>() != null);
  148. }
  149. /// <summary>
  150. /// Called by Unity. Updates controller.
  151. /// </summary>
  152. /// <remarks>Must be call after animation update.</remarks>
  153. private void LateUpdate()
  154. {
  155. if (!HasUpdateController)
  156. {
  157. OnLateUpdate();
  158. }
  159. }
  160. #endregion
  161. }
  162. }