源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

234 行
9.4 KiB

  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. namespace Spine {
  32. /// <summary>
  33. /// Collects each BoundingBoxAttachment that is visible and computes the world vertices for its polygon.
  34. /// The polygon vertices are provided along with convenience methods for doing hit detection.
  35. /// </summary>
  36. public class SkeletonBounds {
  37. private ExposedList<Polygon> polygonPool = new ExposedList<Polygon>();
  38. private float minX, minY, maxX, maxY;
  39. public ExposedList<BoundingBoxAttachment> BoundingBoxes { get; private set; }
  40. public ExposedList<Polygon> Polygons { get; private set; }
  41. public float MinX { get { return minX; } set { minX = value; } }
  42. public float MinY { get { return minY; } set { minY = value; } }
  43. public float MaxX { get { return maxX; } set { maxX = value; } }
  44. public float MaxY { get { return maxY; } set { maxY = value; } }
  45. public float Width { get { return maxX - minX; } }
  46. public float Height { get { return maxY - minY; } }
  47. public SkeletonBounds () {
  48. BoundingBoxes = new ExposedList<BoundingBoxAttachment>();
  49. Polygons = new ExposedList<Polygon>();
  50. }
  51. /// <summary>
  52. /// Clears any previous polygons, finds all visible bounding box attachments,
  53. /// and computes the world vertices for each bounding box's polygon.</summary>
  54. /// <param name="skeleton">The skeleton.</param>
  55. /// <param name="updateAabb">
  56. /// If true, the axis aligned bounding box containing all the polygons is computed.
  57. /// If false, the SkeletonBounds AABB methods will always return true.
  58. /// </param>
  59. public void Update (Skeleton skeleton, bool updateAabb) {
  60. ExposedList<BoundingBoxAttachment> boundingBoxes = BoundingBoxes;
  61. ExposedList<Polygon> polygons = Polygons;
  62. ExposedList<Slot> slots = skeleton.slots;
  63. int slotCount = slots.Count;
  64. boundingBoxes.Clear();
  65. for (int i = 0, n = polygons.Count; i < n; i++)
  66. polygonPool.Add(polygons.Items[i]);
  67. polygons.Clear();
  68. for (int i = 0; i < slotCount; i++) {
  69. Slot slot = slots.Items[i];
  70. BoundingBoxAttachment boundingBox = slot.attachment as BoundingBoxAttachment;
  71. if (boundingBox == null) continue;
  72. boundingBoxes.Add(boundingBox);
  73. Polygon polygon = null;
  74. int poolCount = polygonPool.Count;
  75. if (poolCount > 0) {
  76. polygon = polygonPool.Items[poolCount - 1];
  77. polygonPool.RemoveAt(poolCount - 1);
  78. } else
  79. polygon = new Polygon();
  80. polygons.Add(polygon);
  81. int count = boundingBox.worldVerticesLength;
  82. polygon.Count = count;
  83. if (polygon.Vertices.Length < count) polygon.Vertices = new float[count];
  84. boundingBox.ComputeWorldVertices(slot, polygon.Vertices);
  85. }
  86. if (updateAabb) {
  87. AabbCompute();
  88. } else {
  89. minX = int.MinValue;
  90. minY = int.MinValue;
  91. maxX = int.MaxValue;
  92. maxY = int.MaxValue;
  93. }
  94. }
  95. private void AabbCompute () {
  96. float minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue;
  97. ExposedList<Polygon> polygons = Polygons;
  98. for (int i = 0, n = polygons.Count; i < n; i++) {
  99. Polygon polygon = polygons.Items[i];
  100. float[] vertices = polygon.Vertices;
  101. for (int ii = 0, nn = polygon.Count; ii < nn; ii += 2) {
  102. float x = vertices[ii];
  103. float y = vertices[ii + 1];
  104. minX = Math.Min(minX, x);
  105. minY = Math.Min(minY, y);
  106. maxX = Math.Max(maxX, x);
  107. maxY = Math.Max(maxY, y);
  108. }
  109. }
  110. this.minX = minX;
  111. this.minY = minY;
  112. this.maxX = maxX;
  113. this.maxY = maxY;
  114. }
  115. /// <summary>Returns true if the axis aligned bounding box contains the point.</summary>
  116. public bool AabbContainsPoint (float x, float y) {
  117. return x >= minX && x <= maxX && y >= minY && y <= maxY;
  118. }
  119. /// <summary>Returns true if the axis aligned bounding box intersects the line segment.</summary>
  120. public bool AabbIntersectsSegment (float x1, float y1, float x2, float y2) {
  121. float minX = this.minX;
  122. float minY = this.minY;
  123. float maxX = this.maxX;
  124. float maxY = this.maxY;
  125. if ((x1 <= minX && x2 <= minX) || (y1 <= minY && y2 <= minY) || (x1 >= maxX && x2 >= maxX) || (y1 >= maxY && y2 >= maxY))
  126. return false;
  127. float m = (y2 - y1) / (x2 - x1);
  128. float y = m * (minX - x1) + y1;
  129. if (y > minY && y < maxY) return true;
  130. y = m * (maxX - x1) + y1;
  131. if (y > minY && y < maxY) return true;
  132. float x = (minY - y1) / m + x1;
  133. if (x > minX && x < maxX) return true;
  134. x = (maxY - y1) / m + x1;
  135. if (x > minX && x < maxX) return true;
  136. return false;
  137. }
  138. /// <summary>Returns true if the axis aligned bounding box intersects the axis aligned bounding box of the specified bounds.</summary>
  139. public bool AabbIntersectsSkeleton (SkeletonBounds bounds) {
  140. return minX < bounds.maxX && maxX > bounds.minX && minY < bounds.maxY && maxY > bounds.minY;
  141. }
  142. /// <summary>Returns true if the polygon contains the point.</summary>
  143. public bool ContainsPoint (Polygon polygon, float x, float y) {
  144. float[] vertices = polygon.Vertices;
  145. int nn = polygon.Count;
  146. int prevIndex = nn - 2;
  147. bool inside = false;
  148. for (int ii = 0; ii < nn; ii += 2) {
  149. float vertexY = vertices[ii + 1];
  150. float prevY = vertices[prevIndex + 1];
  151. if ((vertexY < y && prevY >= y) || (prevY < y && vertexY >= y)) {
  152. float vertexX = vertices[ii];
  153. if (vertexX + (y - vertexY) / (prevY - vertexY) * (vertices[prevIndex] - vertexX) < x) inside = !inside;
  154. }
  155. prevIndex = ii;
  156. }
  157. return inside;
  158. }
  159. /// <summary>Returns the first bounding box attachment that contains the point, or null. When doing many checks, it is usually more
  160. /// efficient to only call this method if {@link #aabbContainsPoint(float, float)} returns true.</summary>
  161. public BoundingBoxAttachment ContainsPoint (float x, float y) {
  162. ExposedList<Polygon> polygons = Polygons;
  163. for (int i = 0, n = polygons.Count; i < n; i++)
  164. if (ContainsPoint(polygons.Items[i], x, y)) return BoundingBoxes.Items[i];
  165. return null;
  166. }
  167. /// <summary>Returns the first bounding box attachment that contains the line segment, or null. When doing many checks, it is usually
  168. /// more efficient to only call this method if {@link #aabbIntersectsSegment(float, float, float, float)} returns true.</summary>
  169. public BoundingBoxAttachment IntersectsSegment (float x1, float y1, float x2, float y2) {
  170. ExposedList<Polygon> polygons = Polygons;
  171. for (int i = 0, n = polygons.Count; i < n; i++)
  172. if (IntersectsSegment(polygons.Items[i], x1, y1, x2, y2)) return BoundingBoxes.Items[i];
  173. return null;
  174. }
  175. /// <summary>Returns true if the polygon contains the line segment.</summary>
  176. public bool IntersectsSegment (Polygon polygon, float x1, float y1, float x2, float y2) {
  177. float[] vertices = polygon.Vertices;
  178. int nn = polygon.Count;
  179. float width12 = x1 - x2, height12 = y1 - y2;
  180. float det1 = x1 * y2 - y1 * x2;
  181. float x3 = vertices[nn - 2], y3 = vertices[nn - 1];
  182. for (int ii = 0; ii < nn; ii += 2) {
  183. float x4 = vertices[ii], y4 = vertices[ii + 1];
  184. float det2 = x3 * y4 - y3 * x4;
  185. float width34 = x3 - x4, height34 = y3 - y4;
  186. float det3 = width12 * height34 - height12 * width34;
  187. float x = (det1 * width34 - width12 * det2) / det3;
  188. if (((x >= x3 && x <= x4) || (x >= x4 && x <= x3)) && ((x >= x1 && x <= x2) || (x >= x2 && x <= x1))) {
  189. float y = (det1 * height34 - height12 * det2) / det3;
  190. if (((y >= y3 && y <= y4) || (y >= y4 && y <= y3)) && ((y >= y1 && y <= y2) || (y >= y2 && y <= y1))) return true;
  191. }
  192. x3 = x4;
  193. y3 = y4;
  194. }
  195. return false;
  196. }
  197. public Polygon GetPolygon (BoundingBoxAttachment attachment) {
  198. int index = BoundingBoxes.IndexOf(attachment);
  199. return index == -1 ? null : Polygons.Items[index];
  200. }
  201. }
  202. public class Polygon {
  203. public float[] Vertices { get; set; }
  204. public int Count { get; set; }
  205. public Polygon () {
  206. Vertices = new float[16];
  207. }
  208. }
  209. }