SkeletonClipping.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, 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. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. namespace Spine {
  31. public class SkeletonClipping {
  32. internal readonly Triangulator triangulator = new Triangulator();
  33. internal readonly ExposedList<float> clippingPolygon = new ExposedList<float>();
  34. internal readonly ExposedList<float> clipOutput = new ExposedList<float>(128);
  35. internal readonly ExposedList<float> clippedVertices = new ExposedList<float>(128);
  36. internal readonly ExposedList<int> clippedTriangles = new ExposedList<int>(128);
  37. internal readonly ExposedList<float> clippedUVs = new ExposedList<float>(128);
  38. internal readonly ExposedList<float> scratch = new ExposedList<float>();
  39. internal ClippingAttachment clipAttachment;
  40. internal ExposedList<ExposedList<float>> clippingPolygons;
  41. public ExposedList<float> ClippedVertices { get { return clippedVertices; } }
  42. public ExposedList<int> ClippedTriangles { get { return clippedTriangles; } }
  43. public ExposedList<float> ClippedUVs { get { return clippedUVs; } }
  44. public bool IsClipping { get { return clipAttachment != null; } }
  45. public int ClipStart (Slot slot, ClippingAttachment clip) {
  46. if (clipAttachment != null) return 0;
  47. clipAttachment = clip;
  48. int n = clip.worldVerticesLength;
  49. float[] vertices = clippingPolygon.Resize(n).Items;
  50. clip.ComputeWorldVertices(slot, 0, n, vertices, 0, 2);
  51. MakeClockwise(clippingPolygon);
  52. clippingPolygons = triangulator.Decompose(clippingPolygon, triangulator.Triangulate(clippingPolygon));
  53. foreach (var polygon in clippingPolygons) {
  54. MakeClockwise(polygon);
  55. polygon.Add(polygon.Items[0]);
  56. polygon.Add(polygon.Items[1]);
  57. }
  58. return clippingPolygons.Count;
  59. }
  60. public void ClipEnd (Slot slot) {
  61. if (clipAttachment != null && clipAttachment.endSlot == slot.data) ClipEnd();
  62. }
  63. public void ClipEnd () {
  64. if (clipAttachment == null) return;
  65. clipAttachment = null;
  66. clippingPolygons = null;
  67. clippedVertices.Clear();
  68. clippedTriangles.Clear();
  69. clippingPolygon.Clear();
  70. }
  71. public void ClipTriangles (float[] vertices, int verticesLength, int[] triangles, int trianglesLength, float[] uvs) {
  72. ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
  73. var clippedTriangles = this.clippedTriangles;
  74. var polygons = clippingPolygons.Items;
  75. int polygonsCount = clippingPolygons.Count;
  76. int index = 0;
  77. clippedVertices.Clear();
  78. clippedUVs.Clear();
  79. clippedTriangles.Clear();
  80. //outer:
  81. for (int i = 0; i < trianglesLength; i += 3) {
  82. int vertexOffset = triangles[i] << 1;
  83. float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
  84. float u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1];
  85. vertexOffset = triangles[i + 1] << 1;
  86. float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
  87. float u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1];
  88. vertexOffset = triangles[i + 2] << 1;
  89. float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
  90. float u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1];
  91. for (int p = 0; p < polygonsCount; p++) {
  92. int s = clippedVertices.Count;
  93. if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) {
  94. int clipOutputLength = clipOutput.Count;
  95. if (clipOutputLength == 0) continue;
  96. float d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1;
  97. float d = 1 / (d0 * d2 + d1 * (y1 - y3));
  98. int clipOutputCount = clipOutputLength >> 1;
  99. float[] clipOutputItems = clipOutput.Items;
  100. float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items;
  101. float[] clippedUVsItems = clippedUVs.Resize(s + clipOutputCount * 2).Items;
  102. for (int ii = 0; ii < clipOutputLength; ii += 2) {
  103. float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
  104. clippedVerticesItems[s] = x;
  105. clippedVerticesItems[s + 1] = y;
  106. float c0 = x - x3, c1 = y - y3;
  107. float a = (d0 * c0 + d1 * c1) * d;
  108. float b = (d4 * c0 + d2 * c1) * d;
  109. float c = 1 - a - b;
  110. clippedUVsItems[s] = u1 * a + u2 * b + u3 * c;
  111. clippedUVsItems[s + 1] = v1 * a + v2 * b + v3 * c;
  112. s += 2;
  113. }
  114. s = clippedTriangles.Count;
  115. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3 * (clipOutputCount - 2)).Items;
  116. clipOutputCount--;
  117. for (int ii = 1; ii < clipOutputCount; ii++) {
  118. clippedTrianglesItems[s] = index;
  119. clippedTrianglesItems[s + 1] = index + ii;
  120. clippedTrianglesItems[s + 2] = index + ii + 1;
  121. s += 3;
  122. }
  123. index += clipOutputCount + 1;
  124. } else {
  125. float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * 2).Items;
  126. float[] clippedUVsItems = clippedUVs.Resize(s + 3 * 2).Items;
  127. clippedVerticesItems[s] = x1;
  128. clippedVerticesItems[s + 1] = y1;
  129. clippedVerticesItems[s + 2] = x2;
  130. clippedVerticesItems[s + 3] = y2;
  131. clippedVerticesItems[s + 4] = x3;
  132. clippedVerticesItems[s + 5] = y3;
  133. clippedUVsItems[s] = u1;
  134. clippedUVsItems[s + 1] = v1;
  135. clippedUVsItems[s + 2] = u2;
  136. clippedUVsItems[s + 3] = v2;
  137. clippedUVsItems[s + 4] = u3;
  138. clippedUVsItems[s + 5] = v3;
  139. s = clippedTriangles.Count;
  140. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;
  141. clippedTrianglesItems[s] = index;
  142. clippedTrianglesItems[s + 1] = index + 1;
  143. clippedTrianglesItems[s + 2] = index + 2;
  144. index += 3;
  145. break; //continue outer;
  146. }
  147. }
  148. }
  149. }
  150. /** Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping
  151. * area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list. */
  152. internal bool Clip (float x1, float y1, float x2, float y2, float x3, float y3, ExposedList<float> clippingArea, ExposedList<float> output) {
  153. var originalOutput = output;
  154. var clipped = false;
  155. // Avoid copy at the end.
  156. ExposedList<float> input = null;
  157. if (clippingArea.Count % 4 >= 2) {
  158. input = output;
  159. output = scratch;
  160. } else {
  161. input = scratch;
  162. }
  163. input.Clear();
  164. input.Add(x1);
  165. input.Add(y1);
  166. input.Add(x2);
  167. input.Add(y2);
  168. input.Add(x3);
  169. input.Add(y3);
  170. input.Add(x1);
  171. input.Add(y1);
  172. output.Clear();
  173. float[] clippingVertices = clippingArea.Items;
  174. int clippingVerticesLast = clippingArea.Count - 4;
  175. for (int i = 0; ; i += 2) {
  176. float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1];
  177. float edgeX2 = clippingVertices[i + 2], edgeY2 = clippingVertices[i + 3];
  178. float deltaX = edgeX - edgeX2, deltaY = edgeY - edgeY2;
  179. float[] inputVertices = input.Items;
  180. int inputVerticesLength = input.Count - 2, outputStart = output.Count;
  181. for (int ii = 0; ii < inputVerticesLength; ii += 2) {
  182. float inputX = inputVertices[ii], inputY = inputVertices[ii + 1];
  183. float inputX2 = inputVertices[ii + 2], inputY2 = inputVertices[ii + 3];
  184. bool side2 = deltaX * (inputY2 - edgeY2) - deltaY * (inputX2 - edgeX2) > 0;
  185. if (deltaX * (inputY - edgeY2) - deltaY * (inputX - edgeX2) > 0) {
  186. if (side2) { // v1 inside, v2 inside
  187. output.Add(inputX2);
  188. output.Add(inputY2);
  189. continue;
  190. }
  191. // v1 inside, v2 outside
  192. float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
  193. float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY);
  194. if (Math.Abs(s) > 0.000001f) {
  195. float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s;
  196. output.Add(edgeX + (edgeX2 - edgeX) * ua);
  197. output.Add(edgeY + (edgeY2 - edgeY) * ua);
  198. } else {
  199. output.Add(edgeX);
  200. output.Add(edgeY);
  201. }
  202. } else if (side2) { // v1 outside, v2 inside
  203. float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
  204. float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY);
  205. if (Math.Abs(s) > 0.000001f) {
  206. float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s;
  207. output.Add(edgeX + (edgeX2 - edgeX) * ua);
  208. output.Add(edgeY + (edgeY2 - edgeY) * ua);
  209. } else {
  210. output.Add(edgeX);
  211. output.Add(edgeY);
  212. }
  213. output.Add(inputX2);
  214. output.Add(inputY2);
  215. }
  216. clipped = true;
  217. }
  218. if (outputStart == output.Count) { // All edges outside.
  219. originalOutput.Clear();
  220. return true;
  221. }
  222. output.Add(output.Items[0]);
  223. output.Add(output.Items[1]);
  224. if (i == clippingVerticesLast) break;
  225. var temp = output;
  226. output = input;
  227. output.Clear();
  228. input = temp;
  229. }
  230. if (originalOutput != output) {
  231. originalOutput.Clear();
  232. for (int i = 0, n = output.Count - 2; i < n; i++)
  233. originalOutput.Add(output.Items[i]);
  234. } else
  235. originalOutput.Resize(originalOutput.Count - 2);
  236. return clipped;
  237. }
  238. public static void MakeClockwise (ExposedList<float> polygon) {
  239. float[] vertices = polygon.Items;
  240. int verticeslength = polygon.Count;
  241. float area = vertices[verticeslength - 2] * vertices[1] - vertices[0] * vertices[verticeslength - 1], p1x, p1y, p2x, p2y;
  242. for (int i = 0, n = verticeslength - 3; i < n; i += 2) {
  243. p1x = vertices[i];
  244. p1y = vertices[i + 1];
  245. p2x = vertices[i + 2];
  246. p2y = vertices[i + 3];
  247. area += p1x * p2y - p2x * p1y;
  248. }
  249. if (area < 0) return;
  250. for (int i = 0, lastX = verticeslength - 2, n = verticeslength >> 1; i < n; i += 2) {
  251. float x = vertices[i], y = vertices[i + 1];
  252. int other = lastX - i;
  253. vertices[i] = vertices[other];
  254. vertices[i + 1] = vertices[other + 1];
  255. vertices[other] = x;
  256. vertices[other + 1] = y;
  257. }
  258. }
  259. }
  260. }