AnimationMatchModifierAsset.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated May 1, 2019. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2019, 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. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. * NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. * INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. using Spine;
  33. using Spine.Unity;
  34. namespace Spine.Unity.Examples {
  35. //[CreateAssetMenu(menuName = "Spine/SkeletonData Modifiers/Animation Match", order = 200)]
  36. public class AnimationMatchModifierAsset : SkeletonDataModifierAsset {
  37. public bool matchAllAnimations = true;
  38. public override void Apply (SkeletonData skeletonData) {
  39. if (matchAllAnimations)
  40. AnimationTools.MatchAnimationTimelines(skeletonData.Animations, skeletonData);
  41. }
  42. public static class AnimationTools {
  43. #region Filler Timelines
  44. /// <summary>
  45. /// Matches the animation timelines across the given set of animations.
  46. /// This allows unkeyed properties to assume setup pose when animations are naively mixed using Animation.Apply.
  47. /// </summary>
  48. /// <param name="animations">An enumerable collection animations whose timelines will be matched.</param>
  49. /// <param name="skeletonData">The SkeletonData where the animations belong.</param>
  50. public static void MatchAnimationTimelines (IEnumerable<Spine.Animation> animations, SkeletonData skeletonData) {
  51. if (animations == null) return;
  52. if (skeletonData == null) throw new System.ArgumentNullException("skeletonData", "Timelines can't be matched without a SkeletonData source.");
  53. // Build a reference collection of timelines to match
  54. // and a collection of dummy timelines that can be used to fill-in missing items.
  55. var timelineDictionary = new Dictionary<int, Spine.Timeline>();
  56. foreach (var animation in animations) {
  57. foreach (var timeline in animation.Timelines) {
  58. if (timeline is EventTimeline) continue;
  59. int propertyID = timeline.PropertyId;
  60. if (!timelineDictionary.ContainsKey(propertyID)) {
  61. timelineDictionary.Add(propertyID, GetFillerTimeline(timeline, skeletonData));
  62. }
  63. }
  64. }
  65. var idsToMatch = new List<int>(timelineDictionary.Keys);
  66. // For each animation in the list, check for and add missing timelines.
  67. var currentAnimationIDs = new HashSet<int>();
  68. foreach (var animation in animations) {
  69. currentAnimationIDs.Clear();
  70. foreach (var timeline in animation.Timelines) {
  71. if (timeline is EventTimeline) continue;
  72. currentAnimationIDs.Add(timeline.PropertyId);
  73. }
  74. var animationTimelines = animation.Timelines;
  75. foreach (int propertyID in idsToMatch) {
  76. if (!currentAnimationIDs.Contains(propertyID))
  77. animationTimelines.Add(timelineDictionary[propertyID]);
  78. }
  79. }
  80. // These are locals, but sometimes Unity's GC does weird stuff. So let's clean up.
  81. timelineDictionary.Clear();
  82. timelineDictionary = null;
  83. idsToMatch.Clear();
  84. idsToMatch = null;
  85. currentAnimationIDs.Clear();
  86. currentAnimationIDs = null;
  87. }
  88. static Timeline GetFillerTimeline (Timeline timeline, SkeletonData skeletonData) {
  89. if (timeline is RotateTimeline)
  90. return GetFillerTimeline((RotateTimeline)timeline, skeletonData);
  91. if (timeline is TranslateTimeline)
  92. return GetFillerTimeline((TranslateTimeline)timeline, skeletonData);
  93. if (timeline is ScaleTimeline)
  94. return GetFillerTimeline((ScaleTimeline)timeline, skeletonData);
  95. if (timeline is ShearTimeline)
  96. return GetFillerTimeline((ShearTimeline)timeline, skeletonData);
  97. if (timeline is AttachmentTimeline)
  98. return GetFillerTimeline((AttachmentTimeline)timeline, skeletonData);
  99. if (timeline is ColorTimeline)
  100. return GetFillerTimeline((ColorTimeline)timeline, skeletonData);
  101. if (timeline is TwoColorTimeline)
  102. return GetFillerTimeline((TwoColorTimeline)timeline, skeletonData);
  103. if (timeline is DeformTimeline)
  104. return GetFillerTimeline((DeformTimeline)timeline, skeletonData);
  105. if (timeline is DrawOrderTimeline)
  106. return GetFillerTimeline((DrawOrderTimeline)timeline, skeletonData);
  107. if (timeline is IkConstraintTimeline)
  108. return GetFillerTimeline((IkConstraintTimeline)timeline, skeletonData);
  109. if (timeline is TransformConstraintTimeline)
  110. return GetFillerTimeline((TransformConstraintTimeline)timeline, skeletonData);
  111. if (timeline is PathConstraintPositionTimeline)
  112. return GetFillerTimeline((PathConstraintPositionTimeline)timeline, skeletonData);
  113. if (timeline is PathConstraintSpacingTimeline)
  114. return GetFillerTimeline((PathConstraintSpacingTimeline)timeline, skeletonData);
  115. if (timeline is PathConstraintMixTimeline)
  116. return GetFillerTimeline((PathConstraintMixTimeline)timeline, skeletonData);
  117. return null;
  118. }
  119. static RotateTimeline GetFillerTimeline (RotateTimeline timeline, SkeletonData skeletonData) {
  120. var t = new RotateTimeline(1);
  121. t.BoneIndex = timeline.BoneIndex;
  122. t.SetFrame(0, 0, 0);
  123. return t;
  124. }
  125. static TranslateTimeline GetFillerTimeline (TranslateTimeline timeline, SkeletonData skeletonData) {
  126. var t = new TranslateTimeline(1);
  127. t.BoneIndex = timeline.BoneIndex;
  128. t.SetFrame(0, 0, 0, 0);
  129. return t;
  130. }
  131. static ScaleTimeline GetFillerTimeline (ScaleTimeline timeline, SkeletonData skeletonData) {
  132. var t = new ScaleTimeline(1);
  133. t.BoneIndex = timeline.BoneIndex;
  134. t.SetFrame(0, 0, 0, 0);
  135. return t;
  136. }
  137. static ShearTimeline GetFillerTimeline (ShearTimeline timeline, SkeletonData skeletonData) {
  138. var t = new ShearTimeline(1);
  139. t.BoneIndex = timeline.BoneIndex;
  140. t.SetFrame(0, 0, 0, 0);
  141. return t;
  142. }
  143. static AttachmentTimeline GetFillerTimeline (AttachmentTimeline timeline, SkeletonData skeletonData) {
  144. var t = new AttachmentTimeline(1);
  145. t.SlotIndex = timeline.SlotIndex;
  146. var slotData = skeletonData.Slots.Items[t.SlotIndex];
  147. t.SetFrame(0, 0, slotData.AttachmentName);
  148. return t;
  149. }
  150. static ColorTimeline GetFillerTimeline (ColorTimeline timeline, SkeletonData skeletonData) {
  151. var t = new ColorTimeline(1);
  152. t.SlotIndex = timeline.SlotIndex;
  153. var slotData = skeletonData.Slots.Items[t.SlotIndex];
  154. t.SetFrame(0, 0, slotData.R, slotData.G, slotData.B, slotData.A);
  155. return t;
  156. }
  157. static TwoColorTimeline GetFillerTimeline (TwoColorTimeline timeline, SkeletonData skeletonData) {
  158. var t = new TwoColorTimeline(1);
  159. t.SlotIndex = timeline.SlotIndex;
  160. var slotData = skeletonData.Slots.Items[t.SlotIndex];
  161. t.SetFrame(0, 0, slotData.R, slotData.G, slotData.B, slotData.A, slotData.R2, slotData.G2, slotData.B2);
  162. return t;
  163. }
  164. static DeformTimeline GetFillerTimeline (DeformTimeline timeline, SkeletonData skeletonData) {
  165. var t = new DeformTimeline(1);
  166. t.SlotIndex = timeline.SlotIndex;
  167. t.Attachment = timeline.Attachment;
  168. if (t.Attachment.IsWeighted()) {
  169. t.SetFrame(0, 0, new float[t.Attachment.Vertices.Length]);
  170. } else {
  171. t.SetFrame(0, 0, t.Attachment.Vertices.Clone() as float[]);
  172. }
  173. return t;
  174. }
  175. static DrawOrderTimeline GetFillerTimeline (DrawOrderTimeline timeline, SkeletonData skeletonData) {
  176. var t = new DrawOrderTimeline(1);
  177. t.SetFrame(0, 0, null); // null means use setup pose in DrawOrderTimeline.Apply.
  178. return t;
  179. }
  180. static IkConstraintTimeline GetFillerTimeline (IkConstraintTimeline timeline, SkeletonData skeletonData) {
  181. var t = new IkConstraintTimeline(1);
  182. var ikConstraintData = skeletonData.IkConstraints.Items[timeline.IkConstraintIndex];
  183. t.SetFrame(0, 0, ikConstraintData.Mix, ikConstraintData.Softness, ikConstraintData.BendDirection, ikConstraintData.Compress, ikConstraintData.Stretch);
  184. return t;
  185. }
  186. static TransformConstraintTimeline GetFillerTimeline (TransformConstraintTimeline timeline, SkeletonData skeletonData) {
  187. var t = new TransformConstraintTimeline(1);
  188. var data = skeletonData.TransformConstraints.Items[timeline.TransformConstraintIndex];
  189. t.SetFrame(0, 0, data.RotateMix, data.TranslateMix, data.ScaleMix, data.ShearMix);
  190. return t;
  191. }
  192. static PathConstraintPositionTimeline GetFillerTimeline (PathConstraintPositionTimeline timeline, SkeletonData skeletonData) {
  193. var t = new PathConstraintPositionTimeline(1);
  194. var data = skeletonData.PathConstraints.Items[timeline.PathConstraintIndex];
  195. t.SetFrame(0, 0, data.Position);
  196. return t;
  197. }
  198. static PathConstraintSpacingTimeline GetFillerTimeline (PathConstraintSpacingTimeline timeline, SkeletonData skeletonData) {
  199. var t = new PathConstraintSpacingTimeline(1);
  200. var data = skeletonData.PathConstraints.Items[timeline.PathConstraintIndex];
  201. t.SetFrame(0, 0, data.Spacing);
  202. return t;
  203. }
  204. static PathConstraintMixTimeline GetFillerTimeline (PathConstraintMixTimeline timeline, SkeletonData skeletonData) {
  205. var t = new PathConstraintMixTimeline(1);
  206. var data = skeletonData.PathConstraints.Items[timeline.PathConstraintIndex];
  207. t.SetFrame(0, 0, data.RotateMix, data.TranslateMix);
  208. return t;
  209. }
  210. #endregion
  211. }
  212. }
  213. }