PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/Spine/Runtime/spine-csharp/SkeletonClipping.cs

https://gitlab.com/hoangduy09100/falling-box
C# | 296 lines | 231 code | 32 blank | 33 comment | 40 complexity | 08ae74ca1f1b90b9e1811d6ce6a42dc6 MD5 | raw file
  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. }
  125. else {
  126. float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * 2).Items;
  127. float[] clippedUVsItems = clippedUVs.Resize(s + 3 * 2).Items;
  128. clippedVerticesItems[s] = x1;
  129. clippedVerticesItems[s + 1] = y1;
  130. clippedVerticesItems[s + 2] = x2;
  131. clippedVerticesItems[s + 3] = y2;
  132. clippedVerticesItems[s + 4] = x3;
  133. clippedVerticesItems[s + 5] = y3;
  134. clippedUVsItems[s] = u1;
  135. clippedUVsItems[s + 1] = v1;
  136. clippedUVsItems[s + 2] = u2;
  137. clippedUVsItems[s + 3] = v2;
  138. clippedUVsItems[s + 4] = u3;
  139. clippedUVsItems[s + 5] = v3;
  140. s = clippedTriangles.Count;
  141. int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;
  142. clippedTrianglesItems[s] = index;
  143. clippedTrianglesItems[s + 1] = index + 1;
  144. clippedTrianglesItems[s + 2] = index + 2;
  145. index += 3;
  146. break; //continue outer;
  147. }
  148. }
  149. }
  150. }
  151. /** Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping
  152. * area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list. */
  153. internal bool Clip (float x1, float y1, float x2, float y2, float x3, float y3, ExposedList<float> clippingArea, ExposedList<float> output) {
  154. var originalOutput = output;
  155. var clipped = false;
  156. // Avoid copy at the end.
  157. ExposedList<float> input = null;
  158. if (clippingArea.Count % 4 >= 2) {
  159. input = output;
  160. output = scratch;
  161. } else {
  162. input = scratch;
  163. }
  164. input.Clear();
  165. input.Add(x1);
  166. input.Add(y1);
  167. input.Add(x2);
  168. input.Add(y2);
  169. input.Add(x3);
  170. input.Add(y3);
  171. input.Add(x1);
  172. input.Add(y1);
  173. output.Clear();
  174. float[] clippingVertices = clippingArea.Items;
  175. int clippingVerticesLast = clippingArea.Count - 4;
  176. for (int i = 0; ; i += 2) {
  177. float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1];
  178. float edgeX2 = clippingVertices[i + 2], edgeY2 = clippingVertices[i + 3];
  179. float deltaX = edgeX - edgeX2, deltaY = edgeY - edgeY2;
  180. float[] inputVertices = input.Items;
  181. int inputVerticesLength = input.Count - 2, outputStart = output.Count;
  182. for (int ii = 0; ii < inputVerticesLength; ii += 2) {
  183. float inputX = inputVertices[ii], inputY = inputVertices[ii + 1];
  184. float inputX2 = inputVertices[ii + 2], inputY2 = inputVertices[ii + 3];
  185. bool side2 = deltaX * (inputY2 - edgeY2) - deltaY * (inputX2 - edgeX2) > 0;
  186. if (deltaX * (inputY - edgeY2) - deltaY * (inputX - edgeX2) > 0) {
  187. if (side2) { // v1 inside, v2 inside
  188. output.Add(inputX2);
  189. output.Add(inputY2);
  190. continue;
  191. }
  192. // v1 inside, v2 outside
  193. float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
  194. float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY);
  195. if (Math.Abs(s) > 0.000001f) {
  196. float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s;
  197. output.Add(edgeX + (edgeX2 - edgeX) * ua);
  198. output.Add(edgeY + (edgeY2 - edgeY) * ua);
  199. } else {
  200. output.Add(edgeX);
  201. output.Add(edgeY);
  202. }
  203. }
  204. else if (side2) { // v1 outside, v2 inside
  205. float c0 = inputY2 - inputY, c2 = inputX2 - inputX;
  206. float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY);
  207. if (Math.Abs(s) > 0.000001f) {
  208. float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s;
  209. output.Add(edgeX + (edgeX2 - edgeX) * ua);
  210. output.Add(edgeY + (edgeY2 - edgeY) * ua);
  211. } else {
  212. output.Add(edgeX);
  213. output.Add(edgeY);
  214. }
  215. output.Add(inputX2);
  216. output.Add(inputY2);
  217. }
  218. clipped = true;
  219. }
  220. if (outputStart == output.Count) { // All edges outside.
  221. originalOutput.Clear();
  222. return true;
  223. }
  224. output.Add(output.Items[0]);
  225. output.Add(output.Items[1]);
  226. if (i == clippingVerticesLast) break;
  227. var temp = output;
  228. output = input;
  229. output.Clear();
  230. input = temp;
  231. }
  232. if (originalOutput != output) {
  233. originalOutput.Clear();
  234. for (int i = 0, n = output.Count - 2; i < n; i++) {
  235. originalOutput.Add(output.Items[i]);
  236. }
  237. } else {
  238. originalOutput.Resize(originalOutput.Count - 2);
  239. }
  240. return clipped;
  241. }
  242. public static void MakeClockwise (ExposedList<float> polygon) {
  243. float[] vertices = polygon.Items;
  244. int verticeslength = polygon.Count;
  245. float area = vertices[verticeslength - 2] * vertices[1] - vertices[0] * vertices[verticeslength - 1], p1x, p1y, p2x, p2y;
  246. for (int i = 0, n = verticeslength - 3; i < n; i += 2) {
  247. p1x = vertices[i];
  248. p1y = vertices[i + 1];
  249. p2x = vertices[i + 2];
  250. p2y = vertices[i + 3];
  251. area += p1x * p2y - p2x * p1y;
  252. }
  253. if (area < 0) return;
  254. for (int i = 0, lastX = verticeslength - 2, n = verticeslength >> 1; i < n; i += 2) {
  255. float x = vertices[i], y = vertices[i + 1];
  256. int other = lastX - i;
  257. vertices[i] = vertices[other];
  258. vertices[i + 1] = vertices[other + 1];
  259. vertices[other] = x;
  260. vertices[other + 1] = y;
  261. }
  262. }
  263. }
  264. }