SkeletonClipping.cs 11 KB

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