SkeletonClipping.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * 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 (ExposedList<float> 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[] triangles, int trianglesLength) {
  72. ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
  73. ExposedList<int> clippedTriangles = this.clippedTriangles;
  74. ExposedList<float>[] polygons = clippingPolygons.Items;
  75. int polygonsCount = clippingPolygons.Count;
  76. int index = 0;
  77. clippedVertices.Clear();
  78. clippedTriangles.Clear();
  79. for (int i = 0; i < trianglesLength; i += 3) {
  80. int vertexOffset = triangles[i] << 1;
  81. float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
  82. vertexOffset = triangles[i + 1] << 1;
  83. float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
  84. vertexOffset = triangles[i + 2] << 1;
  85. float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
  86. for (int p = 0; p < polygonsCount; p++) {
  87. int s = clippedVertices.Count;
  88. if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) {
  89. int clipOutputLength = clipOutput.Count;
  90. if (clipOutputLength == 0) continue;
  91. int clipOutputCount = clipOutputLength >> 1;
  92. float[] clipOutputItems = clipOutput.Items;
  93. float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items;
  94. for (int ii = 0; ii < clipOutputLength; ii += 2, s += 2) {
  95. float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
  96. clippedVerticesItems[s] = x;
  97. clippedVerticesItems[s + 1] = y;
  98. }
  99. s = clippedTriangles.Count;
  100. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3 * (clipOutputCount - 2)).Items;
  101. clipOutputCount--;
  102. for (int ii = 1; ii < clipOutputCount; ii++, s += 3) {
  103. clippedTrianglesItems[s] = index;
  104. clippedTrianglesItems[s + 1] = index + ii;
  105. clippedTrianglesItems[s + 2] = index + ii + 1;
  106. }
  107. index += clipOutputCount + 1;
  108. } else {
  109. float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * 2).Items;
  110. clippedVerticesItems[s] = x1;
  111. clippedVerticesItems[s + 1] = y1;
  112. clippedVerticesItems[s + 2] = x2;
  113. clippedVerticesItems[s + 3] = y2;
  114. clippedVerticesItems[s + 4] = x3;
  115. clippedVerticesItems[s + 5] = y3;
  116. s = clippedTriangles.Count;
  117. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;
  118. clippedTrianglesItems[s] = index;
  119. clippedTrianglesItems[s + 1] = index + 1;
  120. clippedTrianglesItems[s + 2] = index + 2;
  121. index += 3;
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. public void ClipTriangles (float[] vertices, int[] triangles, int trianglesLength, float[] uvs) {
  128. ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
  129. ExposedList<int> clippedTriangles = this.clippedTriangles;
  130. ExposedList<float>[] polygons = clippingPolygons.Items;
  131. int polygonsCount = clippingPolygons.Count;
  132. int index = 0;
  133. clippedVertices.Clear();
  134. clippedUVs.Clear();
  135. clippedTriangles.Clear();
  136. for (int i = 0; i < trianglesLength; i += 3) {
  137. int vertexOffset = triangles[i] << 1;
  138. float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
  139. float u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1];
  140. vertexOffset = triangles[i + 1] << 1;
  141. float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
  142. float u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1];
  143. vertexOffset = triangles[i + 2] << 1;
  144. float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
  145. float u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1];
  146. for (int p = 0; p < polygonsCount; p++) {
  147. int s = clippedVertices.Count;
  148. if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) {
  149. int clipOutputLength = clipOutput.Count;
  150. if (clipOutputLength == 0) continue;
  151. float d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1;
  152. float d = 1 / (d0 * d2 + d1 * (y1 - y3));
  153. int clipOutputCount = clipOutputLength >> 1;
  154. float[] clipOutputItems = clipOutput.Items;
  155. float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items;
  156. float[] clippedUVsItems = clippedUVs.Resize(s + clipOutputCount * 2).Items;
  157. for (int ii = 0; ii < clipOutputLength; ii += 2, s += 2) {
  158. float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
  159. clippedVerticesItems[s] = x;
  160. clippedVerticesItems[s + 1] = y;
  161. float c0 = x - x3, c1 = y - y3;
  162. float a = (d0 * c0 + d1 * c1) * d;
  163. float b = (d4 * c0 + d2 * c1) * d;
  164. float c = 1 - a - b;
  165. clippedUVsItems[s] = u1 * a + u2 * b + u3 * c;
  166. clippedUVsItems[s + 1] = v1 * a + v2 * b + v3 * c;
  167. }
  168. s = clippedTriangles.Count;
  169. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3 * (clipOutputCount - 2)).Items;
  170. clipOutputCount--;
  171. for (int ii = 1; ii < clipOutputCount; ii++, s += 3) {
  172. clippedTrianglesItems[s] = index;
  173. clippedTrianglesItems[s + 1] = index + ii;
  174. clippedTrianglesItems[s + 2] = index + ii + 1;
  175. }
  176. index += clipOutputCount + 1;
  177. } else {
  178. float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * 2).Items;
  179. float[] clippedUVsItems = clippedUVs.Resize(s + 3 * 2).Items;
  180. clippedVerticesItems[s] = x1;
  181. clippedVerticesItems[s + 1] = y1;
  182. clippedVerticesItems[s + 2] = x2;
  183. clippedVerticesItems[s + 3] = y2;
  184. clippedVerticesItems[s + 4] = x3;
  185. clippedVerticesItems[s + 5] = y3;
  186. clippedUVsItems[s] = u1;
  187. clippedUVsItems[s + 1] = v1;
  188. clippedUVsItems[s + 2] = u2;
  189. clippedUVsItems[s + 3] = v2;
  190. clippedUVsItems[s + 4] = u3;
  191. clippedUVsItems[s + 5] = v3;
  192. s = clippedTriangles.Count;
  193. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;
  194. clippedTrianglesItems[s] = index;
  195. clippedTrianglesItems[s + 1] = index + 1;
  196. clippedTrianglesItems[s + 2] = index + 2;
  197. index += 3;
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. ///<summary>Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping
  204. /// area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list.</summary>
  205. internal bool Clip (float x1, float y1, float x2, float y2, float x3, float y3, ExposedList<float> clippingArea, ExposedList<float> output) {
  206. ExposedList<float> originalOutput = output;
  207. bool clipped = false;
  208. // Avoid copy at the end.
  209. ExposedList<float> input = null;
  210. if (clippingArea.Count % 4 >= 2) {
  211. input = output;
  212. output = scratch;
  213. } else {
  214. input = scratch;
  215. }
  216. input.Clear();
  217. input.Add(x1);
  218. input.Add(y1);
  219. input.Add(x2);
  220. input.Add(y2);
  221. input.Add(x3);
  222. input.Add(y3);
  223. input.Add(x1);
  224. input.Add(y1);
  225. output.Clear();
  226. int clippingVerticesLast = clippingArea.Count - 4;
  227. float[] clippingVertices = clippingArea.Items;
  228. for (int i = 0; ; i += 2) {
  229. float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1];
  230. float ex = edgeX - clippingVertices[i + 2], ey = edgeY - clippingVertices[i + 3];
  231. int outputStart = output.Count;
  232. float[] inputVertices = input.Items;
  233. for (int ii = 0, nn = input.Count - 2; ii < nn;) {
  234. float inputX = inputVertices[ii], inputY = inputVertices[ii + 1];
  235. ii += 2;
  236. float inputX2 = inputVertices[ii], inputY2 = inputVertices[ii + 1];
  237. bool s2 = ey * (edgeX - inputX2) > ex * (edgeY - inputY2);
  238. float s1 = ey * (edgeX - inputX) - ex * (edgeY - inputY);
  239. if (s1 > 0) {
  240. if (s2) { // v1 inside, v2 inside
  241. output.Add(inputX2);
  242. output.Add(inputY2);
  243. continue;
  244. }
  245. // v1 inside, v2 outside
  246. float ix = inputX2 - inputX, iy = inputY2 - inputY, t = s1 / (ix * ey - iy * ex);
  247. if (t >= 0 && t <= 1) {
  248. output.Add(inputX + ix * t);
  249. output.Add(inputY + iy * t);
  250. } else {
  251. output.Add(inputX2);
  252. output.Add(inputY2);
  253. continue;
  254. }
  255. } else if (s2) { // v1 outside, v2 inside
  256. float ix = inputX2 - inputX, iy = inputY2 - inputY, t = s1 / (ix * ey - iy * ex);
  257. if (t >= 0 && t <= 1) {
  258. output.Add(inputX + ix * t);
  259. output.Add(inputY + iy * t);
  260. output.Add(inputX2);
  261. output.Add(inputY2);
  262. } else {
  263. output.Add(inputX2);
  264. output.Add(inputY2);
  265. continue;
  266. }
  267. }
  268. clipped = true;
  269. }
  270. if (outputStart == output.Count) { // All edges outside.
  271. originalOutput.Clear();
  272. return true;
  273. }
  274. output.Add(output.Items[0]);
  275. output.Add(output.Items[1]);
  276. if (i == clippingVerticesLast) break;
  277. ExposedList<float> temp = output;
  278. output = input;
  279. output.Clear();
  280. input = temp;
  281. }
  282. if (originalOutput != output) {
  283. originalOutput.Clear();
  284. for (int i = 0, n = output.Count - 2; i < n; i++)
  285. originalOutput.Add(output.Items[i]);
  286. } else
  287. originalOutput.Resize(originalOutput.Count - 2);
  288. return clipped;
  289. }
  290. public static void MakeClockwise (ExposedList<float> polygon) {
  291. float[] vertices = polygon.Items;
  292. int verticeslength = polygon.Count;
  293. float area = vertices[verticeslength - 2] * vertices[1] - vertices[0] * vertices[verticeslength - 1], p1x, p1y, p2x, p2y;
  294. for (int i = 0, n = verticeslength - 3; i < n; i += 2) {
  295. p1x = vertices[i];
  296. p1y = vertices[i + 1];
  297. p2x = vertices[i + 2];
  298. p2y = vertices[i + 3];
  299. area += p1x * p2y - p2x * p1y;
  300. }
  301. if (area < 0) return;
  302. for (int i = 0, lastX = verticeslength - 2, n = verticeslength >> 1; i < n; i += 2) {
  303. float x = vertices[i], y = vertices[i + 1];
  304. int other = lastX - i;
  305. vertices[i] = vertices[other];
  306. vertices[i + 1] = vertices[other + 1];
  307. vertices[other] = x;
  308. vertices[other + 1] = y;
  309. }
  310. }
  311. }
  312. }