SkeletonRootMotion.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software or
  13. * otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using Spine.Unity.AnimationTools;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. namespace Spine.Unity {
  33. /// <summary>
  34. /// Add this component to a SkeletonAnimation or SkeletonGraphic GameObject
  35. /// to turn motion of a selected root bone into Transform or RigidBody motion.
  36. /// Local bone translation movement is used as motion.
  37. /// All top-level bones of the skeleton are moved to compensate the root
  38. /// motion bone location, keeping the distance relationship between bones intact.
  39. /// </summary>
  40. /// <remarks>
  41. /// Only compatible with SkeletonAnimation (or other components that implement
  42. /// ISkeletonComponent, ISkeletonAnimation and IAnimationStateComponent).
  43. /// For <c>SkeletonMecanim</c> please use
  44. /// <see cref="SkeletonMecanimRootMotion">SkeletonMecanimRootMotion</see> instead.
  45. /// </remarks>
  46. [HelpURL("http://esotericsoftware.com/spine-unity#SkeletonRootMotion")]
  47. public class SkeletonRootMotion : SkeletonRootMotionBase {
  48. #region Inspector
  49. const int DefaultAnimationTrackFlags = -1;
  50. public int animationTrackFlags = DefaultAnimationTrackFlags;
  51. #endregion
  52. AnimationState animationState;
  53. SkeletonGraphic skeletonGraphic;
  54. public override Vector2 GetRemainingRootMotion (int trackIndex) {
  55. TrackEntry track = animationState.GetCurrent(trackIndex);
  56. if (track == null)
  57. return Vector2.zero;
  58. Animation animation = track.Animation;
  59. float start = track.AnimationTime;
  60. float end = animation.Duration;
  61. return GetAnimationRootMotion(start, end, animation);
  62. }
  63. public override RootMotionInfo GetRootMotionInfo (int trackIndex) {
  64. TrackEntry track = animationState.GetCurrent(trackIndex);
  65. if (track == null)
  66. return new RootMotionInfo();
  67. Animation animation = track.Animation;
  68. float time = track.AnimationTime;
  69. return GetAnimationRootMotionInfo(track.Animation, time);
  70. }
  71. protected override float AdditionalScale {
  72. get {
  73. return skeletonGraphic ? skeletonGraphic.MeshScale : 1.0f;
  74. }
  75. }
  76. protected override void Reset () {
  77. base.Reset();
  78. animationTrackFlags = DefaultAnimationTrackFlags;
  79. }
  80. public override void Initialize () {
  81. base.Initialize();
  82. IAnimationStateComponent animstateComponent = skeletonComponent as IAnimationStateComponent;
  83. this.animationState = (animstateComponent != null) ? animstateComponent.AnimationState : null;
  84. skeletonGraphic = this.GetComponent<SkeletonGraphic>();
  85. }
  86. protected override Vector2 CalculateAnimationsMovementDelta () {
  87. Vector2 localDelta = Vector2.zero;
  88. int trackCount = animationState.Tracks.Count;
  89. for (int trackIndex = 0; trackIndex < trackCount; ++trackIndex) {
  90. // note: animationTrackFlags != -1 below covers trackIndex >= 32,
  91. // with -1 corresponding to entry "everything" of the dropdown list.
  92. if (animationTrackFlags != -1 && (animationTrackFlags & 1 << trackIndex) == 0)
  93. continue;
  94. TrackEntry track = animationState.GetCurrent(trackIndex);
  95. TrackEntry next = null;
  96. while (track != null) {
  97. Animation animation = track.Animation;
  98. float start = track.AnimationLast;
  99. float end = track.AnimationTime;
  100. Vector2 currentDelta = GetAnimationRootMotion(start, end, animation);
  101. if (currentDelta != Vector2.zero) {
  102. ApplyMixAlphaToDelta(ref currentDelta, next, track);
  103. localDelta += currentDelta;
  104. }
  105. // Traverse mixingFrom chain.
  106. next = track;
  107. track = track.MixingFrom;
  108. }
  109. }
  110. return localDelta;
  111. }
  112. protected override float CalculateAnimationsRotationDelta () {
  113. float localDelta = 0;
  114. int trackCount = animationState.Tracks.Count;
  115. for (int trackIndex = 0; trackIndex < trackCount; ++trackIndex) {
  116. // note: animationTrackFlags != -1 below covers trackIndex >= 32,
  117. // with -1 corresponding to entry "everything" of the dropdown list.
  118. if (animationTrackFlags != -1 && (animationTrackFlags & 1 << trackIndex) == 0)
  119. continue;
  120. TrackEntry track = animationState.GetCurrent(trackIndex);
  121. TrackEntry next = null;
  122. while (track != null) {
  123. Animation animation = track.Animation;
  124. float start = track.AnimationLast;
  125. float end = track.AnimationTime;
  126. float currentDelta = GetAnimationRootMotionRotation(start, end, animation);
  127. if (currentDelta != 0) {
  128. ApplyMixAlphaToDelta(ref currentDelta, next, track);
  129. localDelta += currentDelta;
  130. }
  131. // Traverse mixingFrom chain.
  132. next = track;
  133. track = track.MixingFrom;
  134. }
  135. }
  136. return localDelta;
  137. }
  138. void ApplyMixAlphaToDelta (ref Vector2 currentDelta, TrackEntry next, TrackEntry track) {
  139. float mixAlpha = 1;
  140. GetMixAlpha(ref mixAlpha, next, track);
  141. currentDelta *= mixAlpha;
  142. }
  143. void ApplyMixAlphaToDelta (ref float currentDelta, TrackEntry next, TrackEntry track) {
  144. float mixAlpha = 1;
  145. GetMixAlpha(ref mixAlpha, next, track);
  146. currentDelta *= mixAlpha;
  147. }
  148. void GetMixAlpha (ref float cumulatedMixAlpha, TrackEntry next, TrackEntry track) {
  149. // code below based on AnimationState.cs
  150. float mix;
  151. if (next != null) {
  152. if (next.MixDuration == 0) { // Single frame mix to undo mixingFrom changes.
  153. mix = 1;
  154. } else {
  155. mix = next.MixTime / next.MixDuration;
  156. if (mix > 1) mix = 1;
  157. }
  158. float mixAndAlpha = track.Alpha * next.InterruptAlpha * (1 - mix);
  159. cumulatedMixAlpha *= mixAndAlpha;
  160. } else {
  161. if (track.MixDuration == 0) {
  162. mix = 1;
  163. } else {
  164. mix = track.Alpha * (track.MixTime / track.MixDuration);
  165. if (mix > 1) mix = 1;
  166. }
  167. cumulatedMixAlpha *= mix;
  168. }
  169. }
  170. }
  171. }