TimelineExtensions.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. namespace Spine.Unity.AnimationTools {
  33. public static class TimelineExtensions {
  34. /// <summary>Evaluates the resulting value of a TranslateTimeline at a given time.
  35. /// SkeletonData can be accessed from Skeleton.Data or from SkeletonDataAsset.GetSkeletonData.
  36. /// If no SkeletonData is provided, values are returned as difference to setup pose
  37. /// instead of absolute values.</summary>
  38. public static Vector2 Evaluate (this TranslateTimeline timeline, float time, SkeletonData skeletonData = null) {
  39. if (time < timeline.Frames[0]) return Vector2.zero;
  40. float x, y;
  41. timeline.GetCurveValue(out x, out y, time);
  42. if (skeletonData == null) {
  43. return new Vector2(x, y);
  44. } else {
  45. BoneData boneData = skeletonData.Bones.Items[timeline.BoneIndex];
  46. return new Vector2(boneData.X + x, boneData.Y + y);
  47. }
  48. }
  49. /// <summary>Evaluates the resulting value of a pair of split translate timelines at a given time.
  50. /// SkeletonData can be accessed from Skeleton.Data or from SkeletonDataAsset.GetSkeletonData.
  51. /// If no SkeletonData is given, values are returned as difference to setup pose
  52. /// instead of absolute values.</summary>
  53. public static Vector2 Evaluate (TranslateXTimeline xTimeline, TranslateYTimeline yTimeline,
  54. float time, SkeletonData skeletonData = null) {
  55. float x = 0, y = 0;
  56. if (xTimeline != null && time > xTimeline.Frames[0]) x = xTimeline.GetCurveValue(time);
  57. if (yTimeline != null && time > yTimeline.Frames[0]) y = yTimeline.GetCurveValue(time);
  58. if (skeletonData == null) {
  59. return new Vector2(x, y);
  60. } else {
  61. BoneData[] bonesItems = skeletonData.Bones.Items;
  62. BoneData boneDataX = bonesItems[xTimeline.BoneIndex];
  63. BoneData boneDataY = bonesItems[yTimeline.BoneIndex];
  64. return new Vector2(boneDataX.X + x, boneDataY.Y + y);
  65. }
  66. }
  67. /// <summary>Evaluates the resulting value of a RotateTimeline at a given time.
  68. /// SkeletonData can be accessed from Skeleton.Data or from SkeletonDataAsset.GetSkeletonData.
  69. /// If no SkeletonData is given, values are returned as difference to setup pose
  70. /// instead of absolute values.</summary>
  71. public static float Evaluate (this RotateTimeline timeline, float time, SkeletonData skeletonData = null) {
  72. if (time < timeline.Frames[0]) return 0f;
  73. float rotation = timeline.GetCurveValue(time);
  74. if (skeletonData == null) {
  75. return rotation;
  76. } else {
  77. BoneData boneData = skeletonData.Bones.Items[timeline.BoneIndex];
  78. return (boneData.Rotation + rotation);
  79. }
  80. }
  81. /// <summary>Evaluates the resulting X and Y translate mix values of a
  82. /// TransformConstraintTimeline at a given time.</summary>
  83. public static Vector2 EvaluateTranslateXYMix (this TransformConstraintTimeline timeline, float time) {
  84. if (time < timeline.Frames[0]) return Vector2.zero;
  85. float rotate, mixX, mixY, scaleX, scaleY, shearY;
  86. timeline.GetCurveValue(out rotate, out mixX, out mixY, out scaleX, out scaleY, out shearY, time);
  87. return new Vector2(mixX, mixY);
  88. }
  89. /// <summary>Evaluates the resulting rotate mix values of a
  90. /// TransformConstraintTimeline at a given time.</summary>
  91. public static float EvaluateRotateMix (this TransformConstraintTimeline timeline, float time) {
  92. if (time < timeline.Frames[0]) return 0;
  93. float rotate, mixX, mixY, scaleX, scaleY, shearY;
  94. timeline.GetCurveValue(out rotate, out mixX, out mixY, out scaleX, out scaleY, out shearY, time);
  95. return rotate;
  96. }
  97. /// <summary>Gets the translate timeline for a given boneIndex.
  98. /// You can get the boneIndex using SkeletonData.FindBone().Index.
  99. /// The root bone is always boneIndex 0.
  100. /// This will return null if a TranslateTimeline is not found.</summary>
  101. public static TranslateTimeline FindTranslateTimelineForBone (this Animation a, int boneIndex) {
  102. foreach (Timeline timeline in a.Timelines) {
  103. if (timeline.GetType().IsSubclassOf(typeof(TranslateTimeline)))
  104. continue;
  105. TranslateTimeline translateTimeline = timeline as TranslateTimeline;
  106. if (translateTimeline != null && translateTimeline.BoneIndex == boneIndex)
  107. return translateTimeline;
  108. }
  109. return null;
  110. }
  111. /// <summary>Gets the IBoneTimeline timeline of a given type for a given boneIndex.
  112. /// You can get the boneIndex using SkeletonData.FindBoneIndex.
  113. /// The root bone is always boneIndex 0.
  114. /// This will return null if a timeline of the given type is not found.</summary>
  115. public static T FindTimelineForBone<T> (this Animation a, int boneIndex) where T : class, IBoneTimeline {
  116. foreach (Timeline timeline in a.Timelines) {
  117. T translateTimeline = timeline as T;
  118. if (translateTimeline != null && translateTimeline.BoneIndex == boneIndex)
  119. return translateTimeline;
  120. }
  121. return null;
  122. }
  123. /// <summary>Gets the transform constraint timeline for a given boneIndex.
  124. /// You can get the boneIndex using SkeletonData.FindBone().Index.
  125. /// The root bone is always boneIndex 0.
  126. /// This will return null if a TranslateTimeline is not found.</summary>
  127. public static TransformConstraintTimeline FindTransformConstraintTimeline (this Animation a, int transformConstraintIndex) {
  128. foreach (Timeline timeline in a.Timelines) {
  129. if (timeline.GetType().IsSubclassOf(typeof(TransformConstraintTimeline)))
  130. continue;
  131. TransformConstraintTimeline transformConstraintTimeline = timeline as TransformConstraintTimeline;
  132. if (transformConstraintTimeline != null &&
  133. transformConstraintTimeline.TransformConstraintIndex == transformConstraintIndex)
  134. return transformConstraintTimeline;
  135. }
  136. return null;
  137. }
  138. }
  139. }