SkeletonRootMotion.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. public class SkeletonRootMotion : SkeletonRootMotionBase {
  47. #region Inspector
  48. const int DefaultAnimationTrackFlags = -1;
  49. public int animationTrackFlags = DefaultAnimationTrackFlags;
  50. #endregion
  51. AnimationState animationState;
  52. Canvas canvas;
  53. protected override float AdditionalScale {
  54. get {
  55. return canvas ? canvas.referencePixelsPerUnit: 1.0f;
  56. }
  57. }
  58. protected override void Reset () {
  59. base.Reset();
  60. animationTrackFlags = DefaultAnimationTrackFlags;
  61. }
  62. protected override void Start () {
  63. base.Start();
  64. var animstateComponent = skeletonComponent as IAnimationStateComponent;
  65. this.animationState = (animstateComponent != null) ? animstateComponent.AnimationState : null;
  66. if (this.GetComponent<CanvasRenderer>() != null) {
  67. canvas = this.GetComponentInParent<Canvas>();
  68. }
  69. }
  70. protected override Vector2 CalculateAnimationsMovementDelta () {
  71. Vector2 localDelta = Vector2.zero;
  72. int trackCount = animationState.Tracks.Count;
  73. for (int trackIndex = 0; trackIndex < trackCount; ++trackIndex) {
  74. // note: animationTrackFlags != -1 below covers trackIndex >= 32,
  75. // with -1 corresponding to entry "everything" of the dropdown list.
  76. if (animationTrackFlags != -1 && (animationTrackFlags & 1 << trackIndex) == 0)
  77. continue;
  78. TrackEntry track = animationState.GetCurrent(trackIndex);
  79. TrackEntry next = null;
  80. while (track != null) {
  81. var animation = track.Animation;
  82. var timeline = animation.FindTranslateTimelineForBone(rootMotionBoneIndex);
  83. if (timeline != null) {
  84. var currentDelta = GetTrackMovementDelta(track, timeline, animation, next);
  85. localDelta += currentDelta;
  86. }
  87. // Traverse mixingFrom chain.
  88. next = track;
  89. track = track.mixingFrom;
  90. }
  91. }
  92. return localDelta;
  93. }
  94. Vector2 GetTrackMovementDelta (TrackEntry track, TranslateTimeline timeline,
  95. Animation animation, TrackEntry next) {
  96. float start = track.animationLast;
  97. float end = track.AnimationTime;
  98. Vector2 currentDelta = GetTimelineMovementDelta(start, end, timeline, animation);
  99. ApplyMixAlphaToDelta(ref currentDelta, next, track);
  100. return currentDelta;
  101. }
  102. void ApplyMixAlphaToDelta (ref Vector2 currentDelta, TrackEntry next, TrackEntry track) {
  103. // Apply mix alpha to the delta position (based on AnimationState.cs).
  104. float mix;
  105. if (next != null) {
  106. if (next.mixDuration == 0) { // Single frame mix to undo mixingFrom changes.
  107. mix = 1;
  108. }
  109. else {
  110. mix = next.mixTime / next.mixDuration;
  111. if (mix > 1) mix = 1;
  112. }
  113. float mixAndAlpha = track.alpha * next.interruptAlpha * (1 - mix);
  114. currentDelta *= mixAndAlpha;
  115. }
  116. else {
  117. if (track.mixDuration == 0) {
  118. mix = 1;
  119. }
  120. else {
  121. mix = track.alpha * (track.mixTime / track.mixDuration);
  122. if (mix > 1) mix = 1;
  123. }
  124. currentDelta *= mix;
  125. }
  126. }
  127. }
  128. }