SkeletonBounds.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Spine
  4. {
  5. public class SkeletonBounds
  6. {
  7. public SkeletonBounds()
  8. {
  9. this.BoundingBoxes = new List<BoundingBoxAttachment>();
  10. this.Polygons = new List<Polygon>();
  11. }
  12. public List<BoundingBoxAttachment> BoundingBoxes { get; private set; }
  13. public List<Polygon> Polygons { get; private set; }
  14. public float MinX
  15. {
  16. get
  17. {
  18. return this.minX;
  19. }
  20. set
  21. {
  22. this.minX = value;
  23. }
  24. }
  25. public float MinY
  26. {
  27. get
  28. {
  29. return this.minY;
  30. }
  31. set
  32. {
  33. this.minY = value;
  34. }
  35. }
  36. public float MaxX
  37. {
  38. get
  39. {
  40. return this.maxX;
  41. }
  42. set
  43. {
  44. this.maxX = value;
  45. }
  46. }
  47. public float MaxY
  48. {
  49. get
  50. {
  51. return this.maxY;
  52. }
  53. set
  54. {
  55. this.maxY = value;
  56. }
  57. }
  58. public float Width
  59. {
  60. get
  61. {
  62. return this.maxX - this.minX;
  63. }
  64. }
  65. public float Height
  66. {
  67. get
  68. {
  69. return this.maxY - this.minY;
  70. }
  71. }
  72. public void Update(Skeleton skeleton, bool updateAabb)
  73. {
  74. List<BoundingBoxAttachment> boundingBoxes = this.BoundingBoxes;
  75. List<Polygon> polygons = this.Polygons;
  76. List<Slot> slots = skeleton.slots;
  77. int count = slots.Count;
  78. boundingBoxes.Clear();
  79. foreach (Polygon item in polygons)
  80. {
  81. this.polygonPool.Add(item);
  82. }
  83. polygons.Clear();
  84. for (int i = 0; i < count; i++)
  85. {
  86. Slot slot = slots[i];
  87. BoundingBoxAttachment boundingBoxAttachment = slot.attachment as BoundingBoxAttachment;
  88. if (boundingBoxAttachment != null)
  89. {
  90. boundingBoxes.Add(boundingBoxAttachment);
  91. int count2 = this.polygonPool.Count;
  92. Polygon polygon;
  93. if (count2 > 0)
  94. {
  95. polygon = this.polygonPool[count2 - 1];
  96. this.polygonPool.RemoveAt(count2 - 1);
  97. }
  98. else
  99. {
  100. polygon = new Polygon();
  101. }
  102. polygons.Add(polygon);
  103. int num = boundingBoxAttachment.Vertices.Length;
  104. polygon.Count = num;
  105. if (polygon.Vertices.Length < num)
  106. {
  107. polygon.Vertices = new float[num];
  108. }
  109. boundingBoxAttachment.ComputeWorldVertices(slot.bone, polygon.Vertices);
  110. }
  111. }
  112. if (updateAabb)
  113. {
  114. this.aabbCompute();
  115. }
  116. }
  117. private void aabbCompute()
  118. {
  119. float val = 2.14748365E+09f;
  120. float val2 = 2.14748365E+09f;
  121. float val3 = -2.14748365E+09f;
  122. float val4 = -2.14748365E+09f;
  123. List<Polygon> polygons = this.Polygons;
  124. int i = 0;
  125. int count = polygons.Count;
  126. while (i < count)
  127. {
  128. Polygon polygon = polygons[i];
  129. float[] vertices = polygon.Vertices;
  130. int j = 0;
  131. int count2 = polygon.Count;
  132. while (j < count2)
  133. {
  134. float val5 = vertices[j];
  135. float val6 = vertices[j + 1];
  136. val = Math.Min(val, val5);
  137. val2 = Math.Min(val2, val6);
  138. val3 = Math.Max(val3, val5);
  139. val4 = Math.Max(val4, val6);
  140. j += 2;
  141. }
  142. i++;
  143. }
  144. this.minX = val;
  145. this.minY = val2;
  146. this.maxX = val3;
  147. this.maxY = val4;
  148. }
  149. public bool AabbContainsPoint(float x, float y)
  150. {
  151. return x >= this.minX && x <= this.maxX && y >= this.minY && y <= this.maxY;
  152. }
  153. public bool AabbIntersectsSegment(float x1, float y1, float x2, float y2)
  154. {
  155. float num = this.minX;
  156. float num2 = this.minY;
  157. float num3 = this.maxX;
  158. float num4 = this.maxY;
  159. if ((x1 <= num && x2 <= num) || (y1 <= num2 && y2 <= num2) || (x1 >= num3 && x2 >= num3) || (y1 >= num4 && y2 >= num4))
  160. {
  161. return false;
  162. }
  163. float num5 = (y2 - y1) / (x2 - x1);
  164. float num6 = num5 * (num - x1) + y1;
  165. if (num6 > num2 && num6 < num4)
  166. {
  167. return true;
  168. }
  169. num6 = num5 * (num3 - x1) + y1;
  170. if (num6 > num2 && num6 < num4)
  171. {
  172. return true;
  173. }
  174. float num7 = (num2 - y1) / num5 + x1;
  175. if (num7 > num && num7 < num3)
  176. {
  177. return true;
  178. }
  179. num7 = (num4 - y1) / num5 + x1;
  180. return num7 > num && num7 < num3;
  181. }
  182. public bool AabbIntersectsSkeleton(SkeletonBounds bounds)
  183. {
  184. return this.minX < bounds.maxX && this.maxX > bounds.minX && this.minY < bounds.maxY && this.maxY > bounds.minY;
  185. }
  186. public bool ContainsPoint(Polygon polygon, float x, float y)
  187. {
  188. float[] vertices = polygon.Vertices;
  189. int count = polygon.Count;
  190. int num = count - 2;
  191. bool flag = false;
  192. for (int i = 0; i < count; i += 2)
  193. {
  194. float num2 = vertices[i + 1];
  195. float num3 = vertices[num + 1];
  196. if ((num2 < y && num3 >= y) || (num3 < y && num2 >= y))
  197. {
  198. float num4 = vertices[i];
  199. if (num4 + (y - num2) / (num3 - num2) * (vertices[num] - num4) < x)
  200. {
  201. flag = !flag;
  202. }
  203. }
  204. num = i;
  205. }
  206. return flag;
  207. }
  208. public BoundingBoxAttachment ContainsPoint(float x, float y)
  209. {
  210. List<Polygon> polygons = this.Polygons;
  211. int i = 0;
  212. int count = polygons.Count;
  213. while (i < count)
  214. {
  215. if (this.ContainsPoint(polygons[i], x, y))
  216. {
  217. return this.BoundingBoxes[i];
  218. }
  219. i++;
  220. }
  221. return null;
  222. }
  223. public BoundingBoxAttachment IntersectsSegment(float x1, float y1, float x2, float y2)
  224. {
  225. List<Polygon> polygons = this.Polygons;
  226. int i = 0;
  227. int count = polygons.Count;
  228. while (i < count)
  229. {
  230. if (this.IntersectsSegment(polygons[i], x1, y1, x2, y2))
  231. {
  232. return this.BoundingBoxes[i];
  233. }
  234. i++;
  235. }
  236. return null;
  237. }
  238. public bool IntersectsSegment(Polygon polygon, float x1, float y1, float x2, float y2)
  239. {
  240. float[] vertices = polygon.Vertices;
  241. int count = polygon.Count;
  242. float num = x1 - x2;
  243. float num2 = y1 - y2;
  244. float num3 = x1 * y2 - y1 * x2;
  245. float num4 = vertices[count - 2];
  246. float num5 = vertices[count - 1];
  247. for (int i = 0; i < count; i += 2)
  248. {
  249. float num6 = vertices[i];
  250. float num7 = vertices[i + 1];
  251. float num8 = num4 * num7 - num5 * num6;
  252. float num9 = num4 - num6;
  253. float num10 = num5 - num7;
  254. float num11 = num * num10 - num2 * num9;
  255. float num12 = (num3 * num9 - num * num8) / num11;
  256. if (((num12 >= num4 && num12 <= num6) || (num12 >= num6 && num12 <= num4)) && ((num12 >= x1 && num12 <= x2) || (num12 >= x2 && num12 <= x1)))
  257. {
  258. float num13 = (num3 * num10 - num2 * num8) / num11;
  259. if (((num13 >= num5 && num13 <= num7) || (num13 >= num7 && num13 <= num5)) && ((num13 >= y1 && num13 <= y2) || (num13 >= y2 && num13 <= y1)))
  260. {
  261. return true;
  262. }
  263. }
  264. num4 = num6;
  265. num5 = num7;
  266. }
  267. return false;
  268. }
  269. public Polygon getPolygon(BoundingBoxAttachment attachment)
  270. {
  271. int num = this.BoundingBoxes.IndexOf(attachment);
  272. return (num != -1) ? this.Polygons[num] : null;
  273. }
  274. private List<Polygon> polygonPool = new List<Polygon>();
  275. private float minX;
  276. private float minY;
  277. private float maxX;
  278. private float maxY;
  279. }
  280. }