三国卡牌客户端基础资源仓库
yyl
5 天以前 cec146fc3fe287928e075c79ece20a20a9b16b20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/******************************************************************************
 * Spine Runtimes License Agreement
 * Last updated July 28, 2023. Replaces all prior versions.
 *
 * Copyright (c) 2013-2023, Esoteric Software LLC
 *
 * Integration of the Spine Runtimes into software or otherwise creating
 * derivative works of the Spine Runtimes is permitted under the terms and
 * conditions of Section 2 of the Spine Editor License Agreement:
 * http://esotericsoftware.com/spine-editor-license
 *
 * Otherwise, it is permitted to integrate the Spine Runtimes into software or
 * otherwise create derivative works of the Spine Runtimes (collectively,
 * "Products"), provided that each user of the Products must obtain their own
 * Spine Editor license and redistribution of the Products in any form must
 * include this license and copyright notice.
 *
 * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
 * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
 * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
 
using System;
 
namespace Spine {
    public class SkeletonClipping {
        internal readonly Triangulator triangulator = new Triangulator();
        internal readonly ExposedList<float> clippingPolygon = new ExposedList<float>();
        internal readonly ExposedList<float> clipOutput = new ExposedList<float>(128);
        internal readonly ExposedList<float> clippedVertices = new ExposedList<float>(128);
        internal readonly ExposedList<int> clippedTriangles = new ExposedList<int>(128);
        internal readonly ExposedList<float> clippedUVs = new ExposedList<float>(128);
        internal readonly ExposedList<float> scratch = new ExposedList<float>();
 
        internal ClippingAttachment clipAttachment;
        internal ExposedList<ExposedList<float>> clippingPolygons;
 
        public ExposedList<float> ClippedVertices { get { return clippedVertices; } }
        public ExposedList<int> ClippedTriangles { get { return clippedTriangles; } }
        public ExposedList<float> ClippedUVs { get { return clippedUVs; } }
 
        public bool IsClipping { get { return clipAttachment != null; } }
 
        public int ClipStart (Slot slot, ClippingAttachment clip) {
            if (clipAttachment != null) return 0;
            clipAttachment = clip;
 
            int n = clip.worldVerticesLength;
            float[] vertices = clippingPolygon.Resize(n).Items;
            clip.ComputeWorldVertices(slot, 0, n, vertices, 0, 2);
            MakeClockwise(clippingPolygon);
            clippingPolygons = triangulator.Decompose(clippingPolygon, triangulator.Triangulate(clippingPolygon));
            foreach (ExposedList<float> polygon in clippingPolygons) {
                MakeClockwise(polygon);
                polygon.Add(polygon.Items[0]);
                polygon.Add(polygon.Items[1]);
            }
            return clippingPolygons.Count;
        }
 
        public void ClipEnd (Slot slot) {
            if (clipAttachment != null && clipAttachment.endSlot == slot.data) ClipEnd();
        }
 
        public void ClipEnd () {
            if (clipAttachment == null) return;
            clipAttachment = null;
            clippingPolygons = null;
            clippedVertices.Clear();
            clippedTriangles.Clear();
            clippingPolygon.Clear();
        }
 
        public void ClipTriangles (float[] vertices, int[] triangles, int trianglesLength) {
            ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
            ExposedList<int> clippedTriangles = this.clippedTriangles;
            ExposedList<float>[] polygons = clippingPolygons.Items;
            int polygonsCount = clippingPolygons.Count;
 
            int index = 0;
            clippedVertices.Clear();
            clippedTriangles.Clear();
            for (int i = 0; i < trianglesLength; i += 3) {
                int vertexOffset = triangles[i] << 1;
                float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
 
                vertexOffset = triangles[i + 1] << 1;
                float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
 
                vertexOffset = triangles[i + 2] << 1;
                float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
 
                for (int p = 0; p < polygonsCount; p++) {
                    int s = clippedVertices.Count;
                    if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) {
                        int clipOutputLength = clipOutput.Count;
                        if (clipOutputLength == 0) continue;
 
                        int clipOutputCount = clipOutputLength >> 1;
                        float[] clipOutputItems = clipOutput.Items;
                        float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items;
                        for (int ii = 0; ii < clipOutputLength; ii += 2, s += 2) {
                            float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
                            clippedVerticesItems[s] = x;
                            clippedVerticesItems[s + 1] = y;
                        }
 
                        s = clippedTriangles.Count;
                        int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3 * (clipOutputCount - 2)).Items;
                        clipOutputCount--;
                        for (int ii = 1; ii < clipOutputCount; ii++, s += 3) {
                            clippedTrianglesItems[s] = index;
                            clippedTrianglesItems[s + 1] = index + ii;
                            clippedTrianglesItems[s + 2] = index + ii + 1;
                        }
                        index += clipOutputCount + 1;
                    } else {
                        float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * 2).Items;
                        clippedVerticesItems[s] = x1;
                        clippedVerticesItems[s + 1] = y1;
                        clippedVerticesItems[s + 2] = x2;
                        clippedVerticesItems[s + 3] = y2;
                        clippedVerticesItems[s + 4] = x3;
                        clippedVerticesItems[s + 5] = y3;
 
                        s = clippedTriangles.Count;
                        int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;
                        clippedTrianglesItems[s] = index;
                        clippedTrianglesItems[s + 1] = index + 1;
                        clippedTrianglesItems[s + 2] = index + 2;
                        index += 3;
                        break;
                    }
                }
            }
        }
 
        public void ClipTriangles (float[] vertices, int[] triangles, int trianglesLength, float[] uvs) {
            ExposedList<float> clipOutput = this.clipOutput, clippedVertices = this.clippedVertices;
            ExposedList<int> clippedTriangles = this.clippedTriangles;
            ExposedList<float>[] polygons = clippingPolygons.Items;
            int polygonsCount = clippingPolygons.Count;
 
            int index = 0;
            clippedVertices.Clear();
            clippedUVs.Clear();
            clippedTriangles.Clear();
 
            for (int i = 0; i < trianglesLength; i += 3) {
                int vertexOffset = triangles[i] << 1;
                float x1 = vertices[vertexOffset], y1 = vertices[vertexOffset + 1];
                float u1 = uvs[vertexOffset], v1 = uvs[vertexOffset + 1];
 
                vertexOffset = triangles[i + 1] << 1;
                float x2 = vertices[vertexOffset], y2 = vertices[vertexOffset + 1];
                float u2 = uvs[vertexOffset], v2 = uvs[vertexOffset + 1];
 
                vertexOffset = triangles[i + 2] << 1;
                float x3 = vertices[vertexOffset], y3 = vertices[vertexOffset + 1];
                float u3 = uvs[vertexOffset], v3 = uvs[vertexOffset + 1];
 
                for (int p = 0; p < polygonsCount; p++) {
                    int s = clippedVertices.Count;
                    if (Clip(x1, y1, x2, y2, x3, y3, polygons[p], clipOutput)) {
                        int clipOutputLength = clipOutput.Count;
                        if (clipOutputLength == 0) continue;
                        float d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1;
                        float d = 1 / (d0 * d2 + d1 * (y1 - y3));
 
                        int clipOutputCount = clipOutputLength >> 1;
                        float[] clipOutputItems = clipOutput.Items;
                        float[] clippedVerticesItems = clippedVertices.Resize(s + clipOutputCount * 2).Items;
                        float[] clippedUVsItems = clippedUVs.Resize(s + clipOutputCount * 2).Items;
                        for (int ii = 0; ii < clipOutputLength; ii += 2, s += 2) {
                            float x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
                            clippedVerticesItems[s] = x;
                            clippedVerticesItems[s + 1] = y;
                            float c0 = x - x3, c1 = y - y3;
                            float a = (d0 * c0 + d1 * c1) * d;
                            float b = (d4 * c0 + d2 * c1) * d;
                            float c = 1 - a - b;
                            clippedUVsItems[s] = u1 * a + u2 * b + u3 * c;
                            clippedUVsItems[s + 1] = v1 * a + v2 * b + v3 * c;
                        }
 
                        s = clippedTriangles.Count;
                        int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3 * (clipOutputCount - 2)).Items;
                        clipOutputCount--;
                        for (int ii = 1; ii < clipOutputCount; ii++, s += 3) {
                            clippedTrianglesItems[s] = index;
                            clippedTrianglesItems[s + 1] = index + ii;
                            clippedTrianglesItems[s + 2] = index + ii + 1;
                        }
                        index += clipOutputCount + 1;
                    } else {
                        float[] clippedVerticesItems = clippedVertices.Resize(s + 3 * 2).Items;
                        float[] clippedUVsItems = clippedUVs.Resize(s + 3 * 2).Items;
                        clippedVerticesItems[s] = x1;
                        clippedVerticesItems[s + 1] = y1;
                        clippedVerticesItems[s + 2] = x2;
                        clippedVerticesItems[s + 3] = y2;
                        clippedVerticesItems[s + 4] = x3;
                        clippedVerticesItems[s + 5] = y3;
 
                        clippedUVsItems[s] = u1;
                        clippedUVsItems[s + 1] = v1;
                        clippedUVsItems[s + 2] = u2;
                        clippedUVsItems[s + 3] = v2;
                        clippedUVsItems[s + 4] = u3;
                        clippedUVsItems[s + 5] = v3;
 
                        s = clippedTriangles.Count;
                        int[] clippedTrianglesItems = clippedTriangles.Resize(s + 3).Items;
                        clippedTrianglesItems[s] = index;
                        clippedTrianglesItems[s + 1] = index + 1;
                        clippedTrianglesItems[s + 2] = index + 2;
                        index += 3;
                        break;
                    }
                }
            }
        }
 
        ///<summary>Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping
        /// area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list.</summary>
        internal bool Clip (float x1, float y1, float x2, float y2, float x3, float y3, ExposedList<float> clippingArea, ExposedList<float> output) {
            ExposedList<float> originalOutput = output;
            bool clipped = false;
 
            // Avoid copy at the end.
            ExposedList<float> input = null;
            if (clippingArea.Count % 4 >= 2) {
                input = output;
                output = scratch;
            } else {
                input = scratch;
            }
 
            input.Clear();
            input.Add(x1);
            input.Add(y1);
            input.Add(x2);
            input.Add(y2);
            input.Add(x3);
            input.Add(y3);
            input.Add(x1);
            input.Add(y1);
            output.Clear();
 
            int clippingVerticesLast = clippingArea.Count - 4;
            float[] clippingVertices = clippingArea.Items;
            for (int i = 0; ; i += 2) {
                float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1];
                float ex = edgeX - clippingVertices[i + 2], ey = edgeY - clippingVertices[i + 3];
 
                int outputStart = output.Count;
                float[] inputVertices = input.Items;
                for (int ii = 0, nn = input.Count - 2; ii < nn;) {
 
                    float inputX = inputVertices[ii], inputY = inputVertices[ii + 1];
                    ii += 2;
                    float inputX2 = inputVertices[ii], inputY2 = inputVertices[ii + 1];
                    bool s2 = ey * (edgeX - inputX2) > ex * (edgeY - inputY2);
                    float s1 = ey * (edgeX - inputX) - ex * (edgeY - inputY);
                    if (s1 > 0) {
                        if (s2) { // v1 inside, v2 inside
                            output.Add(inputX2);
                            output.Add(inputY2);
                            continue;
                        }
                        // v1 inside, v2 outside
                        float ix = inputX2 - inputX, iy = inputY2 - inputY, t = s1 / (ix * ey - iy * ex);
                        if (t >= 0 && t <= 1) {
                            output.Add(inputX + ix * t);
                            output.Add(inputY + iy * t);
                        } else {
                            output.Add(inputX2);
                            output.Add(inputY2);
                            continue;
                        }
                    } else if (s2) { // v1 outside, v2 inside
                        float ix = inputX2 - inputX, iy = inputY2 - inputY, t = s1 / (ix * ey - iy * ex);
                        if (t >= 0 && t <= 1) {
                            output.Add(inputX + ix * t);
                            output.Add(inputY + iy * t);
                            output.Add(inputX2);
                            output.Add(inputY2);
                        } else {
                            output.Add(inputX2);
                            output.Add(inputY2);
                            continue;
                        }
                    }
                    clipped = true;
                }
 
                if (outputStart == output.Count) { // All edges outside.
                    originalOutput.Clear();
                    return true;
                }
 
                output.Add(output.Items[0]);
                output.Add(output.Items[1]);
 
                if (i == clippingVerticesLast) break;
                ExposedList<float> temp = output;
                output = input;
                output.Clear();
                input = temp;
            }
 
            if (originalOutput != output) {
                originalOutput.Clear();
                for (int i = 0, n = output.Count - 2; i < n; i++)
                    originalOutput.Add(output.Items[i]);
            } else
                originalOutput.Resize(originalOutput.Count - 2);
 
            return clipped;
        }
 
        public static void MakeClockwise (ExposedList<float> polygon) {
            float[] vertices = polygon.Items;
            int verticeslength = polygon.Count;
 
            float area = vertices[verticeslength - 2] * vertices[1] - vertices[0] * vertices[verticeslength - 1], p1x, p1y, p2x, p2y;
            for (int i = 0, n = verticeslength - 3; i < n; i += 2) {
                p1x = vertices[i];
                p1y = vertices[i + 1];
                p2x = vertices[i + 2];
                p2y = vertices[i + 3];
                area += p1x * p2y - p2x * p1y;
            }
            if (area < 0) return;
 
            for (int i = 0, lastX = verticeslength - 2, n = verticeslength >> 1; i < n; i += 2) {
                float x = vertices[i], y = vertices[i + 1];
                int other = lastX - i;
                vertices[i] = vertices[other];
                vertices[i + 1] = vertices[other + 1];
                vertices[other] = x;
                vertices[other + 1] = y;
            }
        }
    }
}