三国卡牌客户端基础资源仓库
hch
1 天以前 e250c3a8790dd521922244b5443a2aac7da89acf
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/******************************************************************************
 * 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 {
    using Physics = Skeleton.Physics;
 
    /// <summary>
    /// <para>
    /// Stores the current pose for an IK constraint. An IK constraint adjusts the rotation of 1 or 2 constrained bones so the tip of
    /// the last bone is as close to the target bone as possible.</para>
    /// <para>
    /// See <a href="http://esotericsoftware.com/spine-ik-constraints">IK constraints</a> in the Spine User Guide.</para>
    /// </summary>
    public class IkConstraint : IUpdatable {
        internal readonly IkConstraintData data;
        internal readonly ExposedList<Bone> bones = new ExposedList<Bone>();
        internal Bone target;
        internal int bendDirection;
        internal bool compress, stretch;
        internal float mix = 1, softness;
 
        internal bool active;
 
        public IkConstraint (IkConstraintData data, Skeleton skeleton) {
            if (data == null) throw new ArgumentNullException("data", "data cannot be null.");
            this.data = data;
 
            bones = new ExposedList<Bone>(data.bones.Count);
            foreach (BoneData boneData in data.bones)
                bones.Add(skeleton.bones.Items[boneData.index]);
 
            target = skeleton.bones.Items[data.target.index];
 
            mix = data.mix;
            softness = data.softness;
            bendDirection = data.bendDirection;
            compress = data.compress;
            stretch = data.stretch;
        }
 
        /// <summary>Copy constructor.</summary>
        public IkConstraint (IkConstraint constraint, Skeleton skeleton)
            : this(constraint.data, skeleton) {
 
            mix = constraint.mix;
            softness = constraint.softness;
            bendDirection = constraint.bendDirection;
            compress = constraint.compress;
            stretch = constraint.stretch;
        }
 
        public void SetToSetupPose () {
            IkConstraintData data = this.data;
            mix = data.mix;
            softness = data.softness;
            bendDirection = data.bendDirection;
            compress = data.compress;
            stretch = data.stretch;
        }
 
        public void Update (Physics physics) {
            if (mix == 0) return;
            Bone target = this.target;
            Bone[] bones = this.bones.Items;
            switch (this.bones.Count) {
            case 1:
                Apply(bones[0], target.worldX, target.worldY, compress, stretch, data.uniform, mix);
                break;
            case 2:
                Apply(bones[0], bones[1], target.worldX, target.worldY, bendDirection, stretch, data.uniform, softness, mix);
                break;
            }
        }
 
        /// <summary>The bones that will be modified by this IK constraint.</summary>
        public ExposedList<Bone> Bones {
            get { return bones; }
        }
 
        /// <summary>The bone that is the IK target.</summary>
        public Bone Target {
            get { return target; }
            set { target = value; }
        }
 
        /// <summary>A percentage (0-1) that controls the mix between the constrained and unconstrained rotation.
        /// <para>
        /// For two bone IK: if the parent bone has local nonuniform scale, the child bone's local Y translation is set to 0.
        /// </para></summary>
        public float Mix {
            get { return mix; }
            set { mix = value; }
        }
 
        /// <summary>For two bone IK, the target bone's distance from the maximum reach of the bones where rotation begins to slow. The bones
        /// will not straighten completely until the target is this far out of range.</summary>
        public float Softness {
            get { return softness; }
            set { softness = value; }
        }
 
        /// <summary>For two bone IK, controls the bend direction of the IK bones, either 1 or -1.</summary>
        public int BendDirection {
            get { return bendDirection; }
            set { bendDirection = value; }
        }
 
        /// <summary>For one bone IK, when true and the target is too close, the bone is scaled to reach it.</summary>
        public bool Compress {
            get { return compress; }
            set { compress = value; }
        }
 
        /// <summary>When true and the target is out of range, the parent bone is scaled to reach it.
        /// <para>
        /// For two bone IK: 1) the child bone's local Y translation is set to 0,
        /// 2) stretch is not applied if <see cref="Softness"/> is > 0,
        /// and 3) if the parent bone has local nonuniform scale, stretch is not applied.
        /// </para></summary>
        public bool Stretch {
            get { return stretch; }
            set { stretch = value; }
        }
 
        public bool Active {
            get { return active; }
        }
 
        /// <summary>The IK constraint's setup pose data.</summary>
        public IkConstraintData Data {
            get { return data; }
        }
 
        override public string ToString () {
            return data.name;
        }
 
        /// <summary>Applies 1 bone IK. The target is specified in the world coordinate system.</summary>
        static public void Apply (Bone bone, float targetX, float targetY, bool compress, bool stretch, bool uniform,
                                float alpha) {
            if (bone == null) throw new ArgumentNullException("bone", "bone cannot be null.");
            Bone p = bone.parent;
 
            float pa = p.a, pb = p.b, pc = p.c, pd = p.d;
            float rotationIK = -bone.ashearX - bone.arotation;
            float tx = 0, ty = 0;
 
            switch (bone.inherit) {
            case Inherit.OnlyTranslation:
                tx = (targetX - bone.worldX) * Math.Sign(bone.skeleton.ScaleX);
                ty = (targetY - bone.worldY) * Math.Sign(bone.skeleton.ScaleY);
                break;
            case Inherit.NoRotationOrReflection: {
                float s = Math.Abs(pa * pd - pb * pc) / Math.Max(0.0001f, pa * pa + pc * pc);
                float sa = pa / bone.skeleton.scaleX;
                float sc = pc / bone.skeleton.ScaleY;
                pb = -sc * s * bone.skeleton.scaleX;
                pd = sa * s * bone.skeleton.ScaleY;
                rotationIK += MathUtils.Atan2Deg(sc, sa);
                goto default; // Fall through.
            }
            default: {
                float x = targetX - p.worldX, y = targetY - p.worldY;
                float d = pa * pd - pb * pc;
                if (Math.Abs(d) <= 0.0001f) {
                    tx = 0;
                    ty = 0;
                } else {
                    tx = (x * pd - y * pb) / d - bone.ax;
                    ty = (y * pa - x * pc) / d - bone.ay;
                }
                break;
            }
            }
 
            rotationIK += MathUtils.Atan2Deg(ty, tx);
            if (bone.ascaleX < 0) rotationIK += 180;
            if (rotationIK > 180)
                rotationIK -= 360;
            else if (rotationIK < -180) //
                rotationIK += 360;
 
            float sx = bone.ascaleX, sy = bone.ascaleY;
            if (compress || stretch) {
                switch (bone.inherit) {
                case Inherit.NoScale:
                case Inherit.NoScaleOrReflection:
                    tx = targetX - bone.worldX;
                    ty = targetY - bone.worldY;
                    break;
                }
                float b = bone.data.length * sx;
                if (b > 0.0001f) {
                    float dd = tx * tx + ty * ty;
                    if ((compress && dd < b * b) || (stretch && dd > b * b)) {
                        float s = ((float)Math.Sqrt(dd) / b - 1) * alpha + 1;
                        sx *= s;
                        if (uniform) sy *= s;
                    }
                }
            }
            bone.UpdateWorldTransform(bone.ax, bone.ay, bone.arotation + rotationIK * alpha, sx, sy, bone.ashearX, bone.ashearY);
        }
 
        /// <summary>Applies 2 bone IK. The target is specified in the world coordinate system.</summary>
        /// <param name="child">A direct descendant of the parent bone.</param>
        static public void Apply (Bone parent, Bone child, float targetX, float targetY, int bendDir, bool stretch, bool uniform,
            float softness, float alpha) {
            if (parent == null) throw new ArgumentNullException("parent", "parent cannot be null.");
            if (child == null) throw new ArgumentNullException("child", "child cannot be null.");
            if (parent.inherit != Inherit.Normal || child.inherit != Inherit.Normal) return;
            float px = parent.ax, py = parent.ay, psx = parent.ascaleX, psy = parent.ascaleY, sx = psx, sy = psy, csx = child.ascaleX;
            int os1, os2, s2;
            if (psx < 0) {
                psx = -psx;
                os1 = 180;
                s2 = -1;
            } else {
                os1 = 0;
                s2 = 1;
            }
            if (psy < 0) {
                psy = -psy;
                s2 = -s2;
            }
            if (csx < 0) {
                csx = -csx;
                os2 = 180;
            } else
                os2 = 0;
            float cx = child.ax, cy, cwx, cwy, a = parent.a, b = parent.b, c = parent.c, d = parent.d;
            bool u = Math.Abs(psx - psy) <= 0.0001f;
            if (!u || stretch) {
                cy = 0;
                cwx = a * cx + parent.worldX;
                cwy = c * cx + parent.worldY;
            } else {
                cy = child.ay;
                cwx = a * cx + b * cy + parent.worldX;
                cwy = c * cx + d * cy + parent.worldY;
            }
            Bone pp = parent.parent;
            a = pp.a;
            b = pp.b;
            c = pp.c;
            d = pp.d;
            float id = a * d - b * c, x = cwx - pp.worldX, y = cwy - pp.worldY;
            id = Math.Abs(id) <= 0.0001f ? 0 : 1 / id;
            float dx = (x * d - y * b) * id - px, dy = (y * a - x * c) * id - py;
            float l1 = (float)Math.Sqrt(dx * dx + dy * dy), l2 = child.data.length * csx, a1, a2;
            if (l1 < 0.0001f) {
                Apply(parent, targetX, targetY, false, stretch, false, alpha);
                child.UpdateWorldTransform(cx, cy, 0, child.ascaleX, child.ascaleY, child.ashearX, child.ashearY);
                return;
            }
            x = targetX - pp.worldX;
            y = targetY - pp.worldY;
            float tx = (x * d - y * b) * id - px, ty = (y * a - x * c) * id - py;
            float dd = tx * tx + ty * ty;
            if (softness != 0) {
                softness *= psx * (csx + 1) * 0.5f;
                float td = (float)Math.Sqrt(dd), sd = td - l1 - l2 * psx + softness;
                if (sd > 0) {
                    float p = Math.Min(1, sd / (softness * 2)) - 1;
                    p = (sd - softness * (1 - p * p)) / td;
                    tx -= p * tx;
                    ty -= p * ty;
                    dd = tx * tx + ty * ty;
                }
            }
            if (u) {
                l2 *= psx;
                float cos = (dd - l1 * l1 - l2 * l2) / (2 * l1 * l2);
                if (cos < -1) {
                    cos = -1;
                    a2 = MathUtils.PI * bendDir;
                } else if (cos > 1) {
                    cos = 1;
                    a2 = 0;
                    if (stretch) {
                        a = ((float)Math.Sqrt(dd) / (l1 + l2) - 1) * alpha + 1;
                        sx *= a;
                        if (uniform) sy *= a;
                    }
                } else
                    a2 = (float)Math.Acos(cos) * bendDir;
                a = l1 + l2 * cos;
                b = l2 * (float)Math.Sin(a2);
                a1 = (float)Math.Atan2(ty * a - tx * b, tx * a + ty * b);
            } else {
                a = psx * l2;
                b = psy * l2;
                float aa = a * a, bb = b * b, ta = (float)Math.Atan2(ty, tx);
                c = bb * l1 * l1 + aa * dd - aa * bb;
                float c1 = -2 * bb * l1, c2 = bb - aa;
                d = c1 * c1 - 4 * c2 * c;
                if (d >= 0) {
                    float q = (float)Math.Sqrt(d);
                    if (c1 < 0) q = -q;
                    q = -(c1 + q) * 0.5f;
                    float r0 = q / c2, r1 = c / q;
                    float r = Math.Abs(r0) < Math.Abs(r1) ? r0 : r1;
                    r0 = dd - r * r;
                    if (r0 >= 0) {
                        y = (float)Math.Sqrt(r0) * bendDir;
                        a1 = ta - (float)Math.Atan2(y, r);
                        a2 = (float)Math.Atan2(y / psy, (r - l1) / psx);
                        goto break_outer; // break outer;
                    }
                }
                float minAngle = MathUtils.PI, minX = l1 - a, minDist = minX * minX, minY = 0;
                float maxAngle = 0, maxX = l1 + a, maxDist = maxX * maxX, maxY = 0;
                c = -a * l1 / (aa - bb);
                if (c >= -1 && c <= 1) {
                    c = (float)Math.Acos(c);
                    x = a * (float)Math.Cos(c) + l1;
                    y = b * (float)Math.Sin(c);
                    d = x * x + y * y;
                    if (d < minDist) {
                        minAngle = c;
                        minDist = d;
                        minX = x;
                        minY = y;
                    }
                    if (d > maxDist) {
                        maxAngle = c;
                        maxDist = d;
                        maxX = x;
                        maxY = y;
                    }
                }
                if (dd <= (minDist + maxDist) * 0.5f) {
                    a1 = ta - (float)Math.Atan2(minY * bendDir, minX);
                    a2 = minAngle * bendDir;
                } else {
                    a1 = ta - (float)Math.Atan2(maxY * bendDir, maxX);
                    a2 = maxAngle * bendDir;
                }
            }
            break_outer:
            float os = (float)Math.Atan2(cy, cx) * s2;
            float rotation = parent.arotation;
            a1 = (a1 - os) * MathUtils.RadDeg + os1 - rotation;
            if (a1 > 180)
                a1 -= 360;
            else if (a1 < -180)
                a1 += 360;
            parent.UpdateWorldTransform(px, py, rotation + a1 * alpha, sx, sy, 0, 0);
            rotation = child.arotation;
            a2 = ((a2 + os) * MathUtils.RadDeg - child.ashearX) * s2 + os2 - rotation;
            if (a2 > 180)
                a2 -= 360;
            else if (a2 < -180)
                a2 += 360;
            child.UpdateWorldTransform(cx, cy, rotation + a2 * alpha, child.ascaleX, child.ascaleY, child.ashearX, child.ashearY);
        }
    }
}