SpriteCacheExtensions.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal static class SpriteCacheExtensions
  7. {
  8. public static MeshCache GetMesh(this SpriteCache sprite)
  9. {
  10. if (sprite != null)
  11. return sprite.skinningCache.GetMesh(sprite);
  12. return null;
  13. }
  14. public static MeshPreviewCache GetMeshPreview(this SpriteCache sprite)
  15. {
  16. if (sprite != null)
  17. return sprite.skinningCache.GetMeshPreview(sprite);
  18. return null;
  19. }
  20. public static SkeletonCache GetSkeleton(this SpriteCache sprite)
  21. {
  22. if (sprite != null)
  23. return sprite.skinningCache.GetSkeleton(sprite);
  24. return null;
  25. }
  26. public static CharacterPartCache GetCharacterPart(this SpriteCache sprite)
  27. {
  28. if (sprite != null)
  29. return sprite.skinningCache.GetCharacterPart(sprite);
  30. return null;
  31. }
  32. public static bool IsVisible(this SpriteCache sprite)
  33. {
  34. var isVisible = true;
  35. var characterPart = sprite.GetCharacterPart();
  36. if (sprite.skinningCache.mode == SkinningMode.Character && characterPart != null)
  37. isVisible = characterPart.isVisible;
  38. return isVisible;
  39. }
  40. public static Matrix4x4 GetLocalToWorldMatrixFromMode(this SpriteCache sprite)
  41. {
  42. var skinningCache = sprite.skinningCache;
  43. if (skinningCache.mode == SkinningMode.SpriteSheet)
  44. return sprite.localToWorldMatrix;
  45. var characterPart = sprite.GetCharacterPart();
  46. Debug.Assert(characterPart != null);
  47. return characterPart.localToWorldMatrix;
  48. }
  49. public static BoneCache[] GetBonesFromMode(this SpriteCache sprite)
  50. {
  51. var skinningCache = sprite.skinningCache;
  52. if (skinningCache.mode == SkinningMode.SpriteSheet)
  53. return sprite.GetSkeleton().bones;
  54. var characterPart = sprite.GetCharacterPart();
  55. Debug.Assert(characterPart != null);
  56. return characterPart.bones;
  57. }
  58. public static void UpdateMesh(this SpriteCache sprite, BoneCache[] bones)
  59. {
  60. var mesh = sprite.GetMesh();
  61. var previewMesh = sprite.GetMeshPreview();
  62. Debug.Assert(mesh != null);
  63. Debug.Assert(previewMesh != null);
  64. mesh.bones = bones;
  65. previewMesh.SetWeightsDirty();
  66. }
  67. public static void SmoothFill(this SpriteCache sprite)
  68. {
  69. var mesh = sprite.GetMesh();
  70. if (mesh == null)
  71. return;
  72. var controller = new SpriteMeshDataController();
  73. controller.spriteMeshData = mesh;
  74. controller.SmoothFill();
  75. }
  76. public static void RestoreBindPose(this SpriteCache sprite)
  77. {
  78. var skinningCache = sprite.skinningCache;
  79. var skeleton = sprite.GetSkeleton();
  80. Debug.Assert(skeleton != null);
  81. skeleton.RestoreDefaultPose();
  82. skinningCache.events.skeletonPreviewPoseChanged.Invoke(skeleton);
  83. }
  84. public static bool AssociateAllBones(this SpriteCache sprite)
  85. {
  86. var skinningCache = sprite.skinningCache;
  87. if (skinningCache.mode == SkinningMode.SpriteSheet)
  88. return false;
  89. var character = skinningCache.character;
  90. Debug.Assert(character != null);
  91. Debug.Assert(character.skeleton != null);
  92. var characterPart = sprite.GetCharacterPart();
  93. Debug.Assert(characterPart != null);
  94. var bones = character.skeleton.bones.Where(x => x.isVisible).ToArray();
  95. characterPart.bones = bones;
  96. characterPart.sprite.UpdateMesh(bones);
  97. return true;
  98. }
  99. public static bool AssociatePossibleBones(this SpriteCache sprite)
  100. {
  101. var skinningCache = sprite.skinningCache;
  102. if (skinningCache.mode == SkinningMode.SpriteSheet)
  103. return false;
  104. var character = skinningCache.character;
  105. Debug.Assert(character != null);
  106. Debug.Assert(character.skeleton != null);
  107. var characterPart = sprite.GetCharacterPart();
  108. Debug.Assert(characterPart != null);
  109. var bones = character.skeleton.bones.Where(x => x.isVisible).ToArray();
  110. var possibleBones = new List<BoneCache>();
  111. // check if any of the bones overlapped
  112. BoneCache shortestBoneDistance = null;
  113. var minDistances = float.MaxValue;
  114. var characterSpriteRect = new Rect(characterPart.position.x , characterPart.position.y, characterPart.sprite.textureRect.width, characterPart.sprite.textureRect.height);
  115. foreach (var bone in bones)
  116. {
  117. var startPoint = bone.position;
  118. var endPoint = bone.endPosition;
  119. if (IntersectsSegment(characterSpriteRect, startPoint, endPoint))
  120. possibleBones.Add(bone);
  121. if (possibleBones.Count == 0)
  122. {
  123. // compare bone start end with rect's 4 line
  124. // compare rect point with bone line
  125. var points = new Vector2[] { startPoint, endPoint };
  126. var rectLinePoints = new []
  127. {
  128. new Vector2Int(0, 1),
  129. new Vector2Int(0, 2),
  130. new Vector2Int(1, 3),
  131. new Vector2Int(2, 3),
  132. };
  133. var rectPoints = new []
  134. {
  135. new Vector2(characterSpriteRect.xMin, characterSpriteRect.yMin),
  136. new Vector2(characterSpriteRect.xMin, characterSpriteRect.yMax),
  137. new Vector2(characterSpriteRect.xMax, characterSpriteRect.yMin),
  138. new Vector2(characterSpriteRect.xMax, characterSpriteRect.yMax)
  139. };
  140. foreach (var point in points)
  141. {
  142. foreach (var rectLine in rectLinePoints)
  143. {
  144. var distance = PointToLineSegmentDistance(point, rectPoints[rectLine.x], rectPoints[rectLine.y]);
  145. if (distance < minDistances)
  146. {
  147. minDistances = distance;
  148. shortestBoneDistance = bone;
  149. }
  150. }
  151. }
  152. foreach (var rectPoint in rectPoints)
  153. {
  154. var distance = PointToLineSegmentDistance(rectPoint, startPoint, endPoint);
  155. if (distance < minDistances)
  156. {
  157. minDistances = distance;
  158. shortestBoneDistance = bone;
  159. }
  160. }
  161. }
  162. }
  163. // if none overlapped, we use the bone that is closest to us
  164. if (possibleBones.Count == 0 && shortestBoneDistance != null)
  165. {
  166. possibleBones.Add(shortestBoneDistance);
  167. }
  168. characterPart.bones = possibleBones.ToArray();
  169. characterPart.sprite.UpdateMesh(possibleBones.ToArray());
  170. return true;
  171. }
  172. static float PointToLineSegmentDistance(Vector2 p, Vector2 a, Vector2 b)
  173. {
  174. Vector2 n = b - a;
  175. Vector2 pa = a - p;
  176. float c = Vector2.Dot(n, pa);
  177. // Closest point is a
  178. if (c > 0.0f)
  179. return Vector2.Dot(pa, pa);
  180. Vector2 bp = p - b;
  181. // Closest point is b
  182. if (Vector2.Dot(n, bp) > 0.0f)
  183. return Vector2.Dot(bp, bp);
  184. // Closest point is between a and b
  185. Vector2 e = pa - n * (c / Vector2.Dot(n, n));
  186. return Vector2.Dot( e, e );
  187. }
  188. static bool IntersectsSegment(Rect rect, Vector2 p1, Vector2 p2)
  189. {
  190. float minX = Mathf.Min(p1.x, p2.x);
  191. float maxX = Mathf.Max(p1.x, p2.x);
  192. if (maxX > rect.xMax)
  193. {
  194. maxX = rect.xMax;
  195. }
  196. if (minX < rect.xMin)
  197. {
  198. minX = rect.xMin;
  199. }
  200. if (minX > maxX)
  201. {
  202. return false;
  203. }
  204. float minY = Mathf.Min(p1.y, p2.y);
  205. float maxY = Mathf.Max(p1.y, p2.y);
  206. float dx = p2.x - p1.x;
  207. if (Mathf.Abs(dx) > float.Epsilon)
  208. {
  209. float a = (p2.y - p1.y) / dx;
  210. float b = p1.y - a * p1.x;
  211. minY = a * minX + b;
  212. maxY = a * maxX + b;
  213. }
  214. if (minY > maxY)
  215. {
  216. float tmp = maxY;
  217. maxY = minY;
  218. minY = tmp;
  219. }
  220. if (maxY > rect.yMax)
  221. {
  222. maxY = rect.yMax;
  223. }
  224. if (minY < rect.yMin)
  225. {
  226. minY = rect.yMin;
  227. }
  228. if (minY > maxY)
  229. {
  230. return false;
  231. }
  232. return true;
  233. }
  234. public static void DeassociateUnusedBones(this SpriteCache sprite)
  235. {
  236. var skinningCache = sprite.skinningCache;
  237. Debug.Assert(skinningCache.mode == SkinningMode.Character);
  238. var characterPart = sprite.GetCharacterPart();
  239. Debug.Assert(characterPart != null);
  240. characterPart.DeassociateUnusedBones();
  241. }
  242. public static void DeassociateAllBones(this SpriteCache sprite)
  243. {
  244. var skinningCache = sprite.skinningCache;
  245. if (skinningCache.mode == SkinningMode.SpriteSheet)
  246. return;
  247. var part = sprite.GetCharacterPart();
  248. if (part.bones.Length == 0)
  249. return;
  250. Debug.Assert(part.sprite != null);
  251. part.bones = new BoneCache[0];
  252. part.sprite.UpdateMesh(part.bones);
  253. skinningCache.events.characterPartChanged.Invoke(part);
  254. }
  255. }
  256. }