源战役客户端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

285 wiersze
11 KiB

1 miesiąc temu
  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. public class SkeletonClipping {
  33. internal readonly Triangulator triangulator = new Triangulator();
  34. internal readonly ExposedList<float> clippingPolygon = new ExposedList<float>();
  35. internal readonly ExposedList<float> clipOutput = new ExposedList<float>(128);
  36. internal readonly ExposedList<float> clippedVertices = new ExposedList<float>(128);
  37. internal readonly ExposedList<int> clippedTriangles = new ExposedList<int>(128);
  38. internal readonly ExposedList<float> clippedUVs = new ExposedList<float>(128);
  39. internal readonly ExposedList<float> scratch = new ExposedList<float>();
  40. internal ClippingAttachment clipAttachment;
  41. internal ExposedList<ExposedList<float>> clippingPolygons;
  42. public ExposedList<float> ClippedVertices { get { return clippedVertices; } }
  43. public ExposedList<int> ClippedTriangles { get { return clippedTriangles; } }
  44. public ExposedList<float> ClippedUVs { get { return clippedUVs; } }
  45. public bool IsClipping { get { return clipAttachment != null; } }
  46. public int ClipStart (Slot slot, ClippingAttachment clip) {
  47. if (clipAttachment != null) return 0;
  48. clipAttachment = clip;
  49. int n = clip.worldVerticesLength;
  50. float[] vertices = clippingPolygon.Resize(n).Items;
  51. clip.ComputeWorldVertices(slot, 0, n, vertices, 0, 2);
  52. MakeClockwise(clippingPolygon);
  53. clippingPolygons = triangulator.Decompose(clippingPolygon, triangulator.Triangulate(clippingPolygon));
  54. foreach (var polygon in clippingPolygons) {
  55. MakeClockwise(polygon);
  56. polygon.Add(polygon.Items[0]);
  57. polygon.Add(polygon.Items[1]);
  58. }
  59. return clippingPolygons.Count;
  60. }
  61. public void ClipEnd (Slot slot) {
  62. if (clipAttachment != null && clipAttachment.endSlot == slot.data) ClipEnd();
  63. }
  64. public void ClipEnd () {
  65. if (clipAttachment == null) return;
  66. clipAttachment = null;
  67. clippingPolygons = null;
  68. clippedVertices.Clear();
  69. clippedTriangles.Clear();
  70. clippingPolygon.Clear();
  71. }
  72. public void ClipTriangles (float[] vertices, int verticesLength, int[] triangles, int trianglesLength, float[] uvs) {
  73. ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
  74. var clippedTriangles = this.clippedTriangles;
  75. var polygons = clippingPolygons.Items;
  76. int polygonsCount = clippingPolygons.Count;
  77. int index = 0;
  78. clippedVertices.Clear();
  79. clippedUVs.Clear();
  80. clippedTriangles.Clear();
  81. //outer:
  82. for (int i = 0; i < trianglesLength; i += 3) {
  83. int vertexOffset = triangles[i] << 1;
  84. float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
  85. float u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1];
  86. vertexOffset = triangles[i + 1] << 1;
  87. float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
  88. float u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1];
  89. vertexOffset = triangles[i + 2] << 1;
  90. float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
  91. float u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1];
  92. for (int p = 0; p < polygonsCount; p++) {
  93. int s = clippedVertices.Count;
  94. if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) {
  95. int clipOutputLength = clipOutput.Count;
  96. if (clipOutputLength == 0) continue;
  97. float d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1;
  98. float d = 1 / (d0 * d2 + d1 * (y1 - y3));
  99. int clipOutputCount = clipOutputLength >> 1;
  100. float[] clipOutputItems = clipOutput.Items;
  101. float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items;
  102. float[] clippedUVsItems = clippedUVs.Resize(s + clipOutputCount * 2).Items;
  103. for (int ii = 0; ii < clipOutputLength; ii += 2) {
  104. float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
  105. clippedVerticesItems[s] = x;
  106. clippedVerticesItems[s + 1] = y;
  107. float c0 = x - x3, c1 = y - y3;
  108. float a = (d0 * c0 + d1 * c1) * d;
  109. float b = (d4 * c0 + d2 * c1) * d;
  110. float c = 1 - a - b;
  111. clippedUVsItems[s] = u1 * a + u2 * b + u3 * c;
  112. clippedUVsItems[s + 1] = v1 * a + v2 * b + v3 * c;
  113. s += 2;
  114. }
  115. s = clippedTriangles.Count;
  116. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3 * (clipOutputCount - 2)).Items;
  117. clipOutputCount--;
  118. for (int ii = 1; ii < clipOutputCount; ii++) {
  119. clippedTrianglesItems[s] = index;
  120. clippedTrianglesItems[s + 1] = index + ii;
  121. clippedTrianglesItems[s + 2] = index + ii + 1;
  122. s += 3;
  123. }
  124. index += clipOutputCount + 1;
  125. }
  126. else {
  127. float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * 2).Items;
  128. float[] clippedUVsItems = clippedUVs.Resize(s + 3 * 2).Items;
  129. clippedVerticesItems[s] = x1;
  130. clippedVerticesItems[s + 1] = y1;
  131. clippedVerticesItems[s + 2] = x2;
  132. clippedVerticesItems[s + 3] = y2;
  133. clippedVerticesItems[s + 4] = x3;
  134. clippedVerticesItems[s + 5] = y3;
  135. clippedUVsItems[s] = u1;
  136. clippedUVsItems[s + 1] = v1;
  137. clippedUVsItems[s + 2] = u2;
  138. clippedUVsItems[s + 3] = v2;
  139. clippedUVsItems[s + 4] = u3;
  140. clippedUVsItems[s + 5] = v3;
  141. s = clippedTriangles.Count;
  142. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;
  143. clippedTrianglesItems[s] = index;
  144. clippedTrianglesItems[s + 1] = index + 1;
  145. clippedTrianglesItems[s + 2] = index + 2;
  146. index += 3;
  147. break; //continue outer;
  148. }
  149. }
  150. }
  151. }
  152. /** Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping
  153. * area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list. */
  154. internal bool Clip (float x1, float y1, float x2, float y2, float x3, float y3, ExposedList<float> clippingArea, ExposedList<float> output) {
  155. var originalOutput = output;
  156. var clipped = false;
  157. // Avoid copy at the end.
  158. ExposedList<float> input = null;
  159. if (clippingArea.Count % 4 >= 2) {
  160. input = output;
  161. output = scratch;
  162. } else {
  163. input = scratch;
  164. }
  165. input.Clear();
  166. input.Add(x1);
  167. input.Add(y1);
  168. input.Add(x2);
  169. input.Add(y2);
  170. input.Add(x3);
  171. input.Add(y3);
  172. input.Add(x1);
  173. input.Add(y1);
  174. output.Clear();
  175. float[] clippingVertices = clippingArea.Items;
  176. int clippingVerticesLast = clippingArea.Count - 4;
  177. for (int i = 0; ; i += 2) {
  178. float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1];
  179. float edgeX2 = clippingVertices[i + 2], edgeY2 = clippingVertices[i + 3];
  180. float deltaX = edgeX - edgeX2, deltaY = edgeY - edgeY2;
  181. float[] inputVertices = input.Items;
  182. int inputVerticesLength = input.Count - 2, outputStart = output.Count;
  183. for (int ii = 0; ii < inputVerticesLength; ii += 2) {
  184. float inputX = inputVertices[ii], inputY = inputVertices[ii + 1];
  185. float inputX2 = inputVertices[ii + 2], inputY2 = inputVertices[ii + 3];
  186. bool side2 = deltaX * (inputY2 - edgeY2) - deltaY * (inputX2 - edgeX2) > 0;
  187. if (deltaX * (inputY - edgeY2) - deltaY * (inputX - edgeX2) > 0) {
  188. if (side2) { // v1 inside, v2 inside
  189. output.Add(inputX2);
  190. output.Add(inputY2);
  191. continue;
  192. }
  193. // v1 inside, v2 outside
  194. float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
  195. float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / (c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY));
  196. output.Add(edgeX + (edgeX2 - edgeX) * ua);
  197. output.Add(edgeY + (edgeY2 - edgeY) * ua);
  198. }
  199. else if (side2) { // v1 outside, v2 inside
  200. float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
  201. float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / (c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY));
  202. output.Add(edgeX + (edgeX2 - edgeX) * ua);
  203. output.Add(edgeY + (edgeY2 - edgeY) * ua);
  204. output.Add(inputX2);
  205. output.Add(inputY2);
  206. }
  207. clipped = true;
  208. }
  209. if (outputStart == output.Count) { // All edges outside.
  210. originalOutput.Clear();
  211. return true;
  212. }
  213. output.Add(output.Items[0]);
  214. output.Add(output.Items[1]);
  215. if (i == clippingVerticesLast) break;
  216. var temp = output;
  217. output = input;
  218. output.Clear();
  219. input = temp;
  220. }
  221. if (originalOutput != output) {
  222. originalOutput.Clear();
  223. for (int i = 0, n = output.Count - 2; i < n; i++) {
  224. originalOutput.Add(output.Items[i]);
  225. }
  226. } else {
  227. originalOutput.Resize(originalOutput.Count - 2);
  228. }
  229. return clipped;
  230. }
  231. static void MakeClockwise (ExposedList<float> polygon) {
  232. float[] vertices = polygon.Items;
  233. int verticeslength = polygon.Count;
  234. float area = vertices[verticeslength - 2] * vertices[1] - vertices[0] * vertices[verticeslength - 1], p1x, p1y, p2x, p2y;
  235. for (int i = 0, n = verticeslength - 3; i < n; i += 2) {
  236. p1x = vertices[i];
  237. p1y = vertices[i + 1];
  238. p2x = vertices[i + 2];
  239. p2y = vertices[i + 3];
  240. area += p1x * p2y - p2x * p1y;
  241. }
  242. if (area < 0) return;
  243. for (int i = 0, lastX = verticeslength - 2, n = verticeslength >> 1; i < n; i += 2) {
  244. float x = vertices[i], y = vertices[i + 1];
  245. int other = lastX - i;
  246. vertices[i] = vertices[other];
  247. vertices[i + 1] = vertices[other + 1];
  248. vertices[other] = x;
  249. vertices[other + 1] = y;
  250. }
  251. }
  252. }
  253. }