SkeletonRootMotion.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, 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
  13. * or 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
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using UnityEngine;
  30. using System.Collections.Generic;
  31. using Spine.Unity.AnimationTools;
  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. Canvas canvas;
  54. public override Vector2 GetRemainingRootMotion (int trackIndex) {
  55. TrackEntry track = animationState.GetCurrent(trackIndex);
  56. if (track == null)
  57. return Vector2.zero;
  58. var 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. var animation = track.Animation;
  68. float time = track.AnimationTime;
  69. return GetAnimationRootMotionInfo(track.Animation, time);
  70. }
  71. protected override float AdditionalScale {
  72. get {
  73. return canvas ? canvas.referencePixelsPerUnit: 1.0f;
  74. }
  75. }
  76. protected override void Reset () {
  77. base.Reset();
  78. animationTrackFlags = DefaultAnimationTrackFlags;
  79. }
  80. protected override void Start () {
  81. base.Start();
  82. var animstateComponent = skeletonComponent as IAnimationStateComponent;
  83. this.animationState = (animstateComponent != null) ? animstateComponent.AnimationState : null;
  84. if (this.GetComponent<CanvasRenderer>() != null) {
  85. canvas = this.GetComponentInParent<Canvas>();
  86. }
  87. }
  88. protected override Vector2 CalculateAnimationsMovementDelta () {
  89. Vector2 localDelta = Vector2.zero;
  90. int trackCount = animationState.Tracks.Count;
  91. for (int trackIndex = 0; trackIndex < trackCount; ++trackIndex) {
  92. // note: animationTrackFlags != -1 below covers trackIndex >= 32,
  93. // with -1 corresponding to entry "everything" of the dropdown list.
  94. if (animationTrackFlags != -1 && (animationTrackFlags & 1 << trackIndex) == 0)
  95. continue;
  96. TrackEntry track = animationState.GetCurrent(trackIndex);
  97. TrackEntry next = null;
  98. while (track != null) {
  99. var animation = track.Animation;
  100. float start = track.animationLast;
  101. float end = track.AnimationTime;
  102. var currentDelta = GetAnimationRootMotion(start, end, animation);
  103. if (currentDelta != Vector2.zero) {
  104. ApplyMixAlphaToDelta(ref currentDelta, next, track);
  105. localDelta += currentDelta;
  106. }
  107. // Traverse mixingFrom chain.
  108. next = track;
  109. track = track.mixingFrom;
  110. }
  111. }
  112. return localDelta;
  113. }
  114. void ApplyMixAlphaToDelta (ref Vector2 currentDelta, TrackEntry next, TrackEntry track) {
  115. // Apply mix alpha to the delta position (based on AnimationState.cs).
  116. float mix;
  117. if (next != null) {
  118. if (next.mixDuration == 0) { // Single frame mix to undo mixingFrom changes.
  119. mix = 1;
  120. }
  121. else {
  122. mix = next.mixTime / next.mixDuration;
  123. if (mix > 1) mix = 1;
  124. }
  125. float mixAndAlpha = track.alpha * next.interruptAlpha * (1 - mix);
  126. currentDelta *= mixAndAlpha;
  127. }
  128. else {
  129. if (track.mixDuration == 0) {
  130. mix = 1;
  131. }
  132. else {
  133. mix = track.alpha * (track.mixTime / track.mixDuration);
  134. if (mix > 1) mix = 1;
  135. }
  136. currentDelta *= mix;
  137. }
  138. }
  139. }
  140. }